DBA Data[Home] [Help]

PACKAGE BODY: APPS.PO_COMMUNICATION_PVT

Source


1 PACKAGE BODY PO_COMMUNICATION_PVT AS
2 /* $Header: POXVCOMB.pls 120.61.12010000.5 2008/10/01 06:37:07 lgoyal ship $ */
3 
4 -- Read the profile option that enables/disables the debug log
5 g_po_wf_debug VARCHAR2(1) := NVL(FND_PROFILE.VALUE('PO_SET_DEBUG_WORKFLOW_ON'),'N');
6 G_PKG_NAME CONSTANT  VARCHAR2(30) := 'PO_COMMUNICATION_PVT';
7 g_log_head    CONSTANT VARCHAR2(30) := 'po.plsql.PO_COMMUNICATION_PVT.';
8 g_ship_cont_phone  VARCHAR2(200);
9 g_ship_cont_email  VARCHAR2(2000);
10 g_deliver_cont_phone   VARCHAR2(200);
11 g_deliver_cont_email   VARCHAR2(2000);
12 g_ship_cont_name   VARCHAR2(400);
13 g_deliver_cont_name  VARCHAR2(400);
14 g_ship_cust_name   VARCHAR2(400);
15 g_ship_cust_location   VARCHAR2(2000);
16 g_deliver_cust_name    VARCHAR2(400);
17 g_deliver_cust_location  VARCHAR2(2000);
18 g_ship_contact_fax    VARCHAR2(200);
19 g_deliver_contact_name    VARCHAR2(400);
20 g_deliver_contact_fax   VARCHAR2(200);
21 g_shipping_method   VARCHAR2(240);
22 g_shipping_instructions   VARCHAR2(2000);
23 g_packing_instructions    VARCHAR2(2000);
24 g_customer_product_desc   VARCHAR2(1000);
25 g_customer_po_number    VARCHAR2(50);
26 g_customer_po_line_num    VARCHAR2(50);
27 g_customer_po_shipment_num  VARCHAR2(50);
28 g_document_id   NUMBER;
29 g_revision_num    NUMBER;
30 g_vendor_id PO_HEADERS_ALL.vendor_id%type;
31 g_cover_message   VARCHAR2(2001);
32 g_amendment_message VARCHAR2(2001);
33 g_test_flag   VARCHAR2(1);
34 g_release_header_id PO_HEADERS_ALL.po_header_id%type;
35 g_location_id   number;
36 g_address_line1 HR_LOCATIONS.ADDRESS_LINE_1%type := null;
37 g_address_line2 HR_LOCATIONS.ADDRESS_LINE_2%type := null;
38 g_address_line3 HR_LOCATIONS.ADDRESS_LINE_3%type := null;
39 g_Territory_short_name FND_TERRITORIES_TL.TERRITORY_SHORT_NAME%type := null;
40 g_address_info varchar2(500) := null;
41 g_org_id PO_HEADERS_ALL.ORG_ID%type := null;
42 
43 -- Global variables to hold the Operating Unit details --
44 g_ou_name HR_ORGANIZATION_UNITS_V.NAME%type := null;
45 g_ou_address_line_1 HR_ORGANIZATION_UNITS_V.ADDRESS_LINE_1 %type := null;
46 g_ou_address_line_2 HR_ORGANIZATION_UNITS_V.ADDRESS_LINE_2%type := null;
47 g_ou_address_line_3 HR_ORGANIZATION_UNITS_V.ADDRESS_LINE_3%type := null;
48 g_ou_town_or_city HR_ORGANIZATION_UNITS_V.TOWN_OR_CITY%type := null;
49 g_ou_region2 HR_ORGANIZATION_UNITS_V.REGION_1%type := null;
50 g_ou_postal_code HR_ORGANIZATION_UNITS_V.POSTAL_CODE%type := null;
51 g_ou_country HR_ORGANIZATION_UNITS_V.COUNTRY%type := null;
52 -- End of Operation Unit detail variables --
53 
54 g_header_id PO_HEADERS_ALL.PO_HEADER_ID%type := null;
55 g_quote_number  PO_HEADERS_ALL.QUOTE_VENDOR_QUOTE_NUMBER%type := null;
56 g_agreement_number PO_HEADERS_ALL.SEGMENT1%type := null;
57 g_agreement_flag PO_HEADERS_ALL.GLOBAL_AGREEMENT_FLAG%type :=null;
58 g_agreementLine_number PO_LINES_ALL.LINE_NUM%type :=null;
59 g_line_id PO_LINES_ALL.FROM_LINE_ID%type :=null;
60 g_arcBuyer_fname PER_ALL_PEOPLE_F.FIRST_NAME%type :=null;
61 g_arcBuyer_lname PER_ALL_PEOPLE_F.LAST_NAME%type :=null;
62 g_arcBuyer_title PER_ALL_PEOPLE_F.TITLE%type :=null;
63 g_arcAgent_id PO_HEADERS_ARCHIVE_ALL.AGENT_ID%type :=null;
64 g_header_id1 PO_HEADERS_ALL.PO_HEADER_ID%type := null;
65 g_release_id PO_RELEASES_ALL.PO_RELEASE_ID%type :=null;
66 g_timezone VARCHAR2(255) :=NULL;
67 g_vendor_address_line_2 PO_VENDOR_SITES.ADDRESS_LINE2%type := null;
68 g_vendor_address_line_3 PO_VENDOR_SITES.ADDRESS_LINE3%type := null;
69 g_vendor_country FND_TERRITORIES_TL.TERRITORY_SHORT_NAME%type :=null;
70 g_vendor_city_state_zipInfo varchar2(500) :=null;
71 g_vendor_site_id PO_HEADERS_ALL.vendor_site_id%type :=null;
72 g_job_id PO_LINES_ALL.JOB_ID%type :=null;
73 g_job_name PER_JOBS_VL.name%type :=null;
74 g_phone HR_LOCATIONS.TELEPHONE_NUMBER_1%type :=null;
75 --Bug 4504228 START
76 g_person_id PER_ALL_PEOPLE_F.PERSON_ID%type :=null;
77 g_buyer_email_address PER_ALL_PEOPLE_F.EMAIL_ADDRESS%type :=null;
78 g_buyer_phone PER_ALL_PEOPLE_F.office_number%type :=null;
79 --Bug 4504228 END
80 g_buyer_fax HR_LOCATIONS.TELEPHONE_NUMBER_2%type :=null; --Bug5671523 Adding g_buyer_fax
81 g_fax HR_LOCATIONS.TELEPHONE_NUMBER_2%type :=null;
82 g_location_name HR_LOCATIONS.LOCATION_CODE%type :=null;
83 g_documentType PO_DOCUMENT_TYPES_TL.TYPE_NAME%type;
84 g_currency_code PO_HEADERS_ALL.CURRENCY_CODE%type :=null;
85 g_current_currency_code PO_HEADERS_ALL.CURRENCY_CODE%type :=null;
86 g_format_mask varchar2(100) :=null;
87 g_buyer_org HR_ALL_ORGANIZATION_UNITS.NAME%type := NULL;
88 g_address_line4 HZ_LOCATIONS.ADDRESS4%TYPE := NULL; -- bug: 3463617
89 g_vendor_address_line_4 HZ_LOCATIONS.ADDRESS4%TYPE := NULL; -- bug: 3463617
90 --bug#3438608 added the three global variables g_town_or_city
91 --g_postal_code and g_state_or_province
92 g_town_or_city  HR_LOCATIONS.town_or_city%type :=NULL;
93 g_postal_code   HR_LOCATIONS.postal_code%type :=NULL;
94 g_state_or_province  varchar2(100) :=NULL;
95 
96 --Start of global variables to hold the legal entity details --
97 
98 g_legal_entity_name HR_ORGANIZATION_UNITS_V.NAME%type := null;
99 g_legal_entity_address_line_1 HR_LOCATIONS.ADDRESS_LINE_1 %type := null;
100 g_legal_entity_address_line_2 HR_LOCATIONS.ADDRESS_LINE_2%type := null;
101 g_legal_entity_address_line_3 HR_LOCATIONS.ADDRESS_LINE_3%type := null;
102 g_legal_entity_town_or_city HR_LOCATIONS.TOWN_OR_CITY%type := null;
103 g_legal_entity_state HR_LOCATIONS.REGION_1%type := null;
104 g_legal_entity_postal_code HR_LOCATIONS.POSTAL_CODE%type := null;
105 g_legal_entity_country FND_TERRITORIES_TL.TERRITORY_SHORT_NAME%type := null;
106 g_legal_entity_org_id PO_HEADERS_ALL.ORG_ID%type := null;
107 
108 -- End of Legal Entity details ----
109 
110 g_dist_shipto_count number := NULL ; -- Variable which holds count of distinct shipment ship to ids
111 g_line_org_amount number := NULL;
112 
113 /*Bug#35833910 the variable determines whether the po has Terms and Conditions */
114 g_with_terms    po_headers_all.conterms_exist_flag%type;
115 
116 -- <Bug 3619689 Start> Use proper debug logging
117 g_debug_stmt CONSTANT BOOLEAN := PO_DEBUG.is_debug_stmt_on;
118 g_debug_unexp CONSTANT BOOLEAN := PO_DEBUG.is_debug_unexp_on;
119 -- <Bug 3619689 End>
120 
121 g_documentName varchar2(200) :=null; --bug#3630737:Holds concatinated value of DocumentType, po number and revision number
122 
123 --Start Bug#3771735
124 g_documentTypeCode  PO_DOCUMENT_TYPES_TL.DOCUMENT_TYPE_CODE%type;
125 --End Bug#3771735
126 
127 -- Bug 4026592
128 g_is_contract_attached_doc  varchar2(1);
129 
130 
131 -- <Complex Work R12 Start>
132 g_is_complex_work_po   VARCHAR2(1);
133 
134 --Bug 4568471/6829381
135  g_is_one_time_location varchar2(2) :='N';
136 
137 PROCEDURE setIsComplexWorkPO(
138   p_document_id    IN  NUMBER
139 , p_revision_num   IN  NUMBER    DEFAULT NULL
140 , p_which_tables   IN  VARCHAR2  DEFAULT 'MAIN'
141 );
142 -- <Complex Work R12 End>
143 
144 
145 -- <Word Integration 11.5.10+: Forward declare helper function>
146 FUNCTION getDocFileName(p_document_type varchar2,
147                         p_terms varchar2,
148                         p_orgid number,
149                         p_document_id varchar2,
150                         p_revision_num number,
151                         p_language_code varchar2,
152                         p_extension varchar2) RETURN varchar2;
153 
154 
155 /*=======================================================================+
156  | FILENAME
157  |   POXVCOMB.pls
158  |
159  | DESCRIPTION
160  |   PL/SQL body for package:  PO_COMMUNICATION_PVT
161  |
162  | NOTES        VSANJAY Created  08/07/2003
163  | MODIFIED    (MM/DD/YY)
164  | VSANJAY      08/07/2003
165  | AMRITUNJ     09/29/2003   - API Change and added commit after fnd_request.submit_request
166  |                            As specified in AOL standards guide for concurrent request API
167  |                            It can have side effects. For more info, search for COMMIT_NOTE
168  |                            in this file.
169  *=======================================================================*/
170 
171 PROCEDURE GENERATE_PDF (itemtype IN VARCHAR2,
172                                            itemkey  IN VARCHAR2,
173                                            actid    IN NUMBER,
174                                            funcmode IN VARCHAR2,
175                                            resultout   OUT NOCOPY VARCHAR2)
176     IS
177 
178    l_document_id number;
179    l_document_subtype po_headers.type_lookup_code%TYPE;
180    l_revision_num  number;
181    l_request_id number;
182    l_authorization_status varchar2(25);
183    x_progress  varchar2(100);
184    l_with_terms PO_HEADERS_ALL.CONTERMS_EXIST_FLAG%TYPE;
185 
186 
187 BEGIN
188 
189   x_progress := 'PO_COMMUNICATION_PVT.GENERATE_PDF';
190 
191   IF (g_po_wf_debug = 'Y') THEN
192    PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,x_progress);
193   END IF;
194 
195   l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
196                                                      itemkey  => itemkey,
197                                                      aname    => 'DOCUMENT_ID');
198 
199   l_document_subtype := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
200                                                itemkey  => itemkey,
201                                                 aname    => 'DOCUMENT_SUBTYPE');
202 
203   l_revision_num := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype  => itemtype,
204                                                       itemkey   => itemkey,
205                                           aname           => 'REVISION_NUMBER');
206 
207   l_authorization_status := PO_WF_UTIL_PKG.GetItemAttrText (itemtype =>itemtype,
208                                                       itemkey   => itemkey,
209                                           aname  => 'AUTHORIZATION_STATUS');
210 
211   l_with_terms := PO_WF_UTIL_PKG.GetItemAttrText (itemtype =>itemtype,
212                                                         itemkey   => itemkey,
213                                                 aname  => 'WITH_TERMS');
214 
215 x_progress := 'PO_COMMUNICATION_PVT.GENERATE_PDF :launching the java concurrent program ';
216 
217   IF (g_po_wf_debug = 'Y') THEN
218    PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,x_progress);
219   END IF;
220 
221 --if the po has T's and C's then launch the concurrent request to generate the pdf with T's and C's
222 --Bug5080617 Pass the parameters P_PO_TEMPLATE_CODE and P_CONTRACT_TEMPLATE_CODE as null
223 
224 IF l_document_subtype in ('STANDARD','BLANKET','CONTRACT') THEN
225 IF (l_with_terms = 'Y') THEN
226 
227 --<R12 MOAC START>
228 po_moac_utils_pvt.set_request_context(po_moac_utils_pvt.get_current_org_id);
229 --<R12 MOAC END>
230 
231 l_request_id := fnd_request.submit_request('PO',
232          'POXPOPDF',
233          null,
234          null,
235          false,
236          'R',--P_report_type
237          null  ,--P_agend_id
238          null,--P_po_num_from
239          null           ,--P_po_num_to
240          null           ,--P_relaese_num_from
241          null           ,--P_release_num_to
242          null           ,--P_date_from
243          null           ,--P_date_to
244          null           ,--P_approved_flag
245          'N',--P_test_flag
246          null           ,--P_print_releases
247          null           ,--P_sortby
248          null           ,--P_user_id
249          null           ,--P_fax_enable
250          null           ,--P_fax_number
251          null           ,--P_BLANKET_LINES
252          'View'           ,--View_or_Communicate,
253          'Y',--P_WITHTERMS
254          'Y',--P_storeFlag
255          'N',--P_PRINT_FLAG
256          l_document_id,--P_DOCUMENT_ID
257          l_revision_num,--P_REVISION_NUM
258          l_authorization_status,--P_AUTHORIZATION_STATUS
259          l_document_subtype,--P_DOCUMENT_TYPE
260          0,--P_max_zip_size, <PO Attachment Support 11i.11>
261          null, -- P_PO_TEMPLATE_CODE
262          null, -- P_CONTRACT_TEMPLATE_CODE
263          fnd_global.local_chr(0),
264          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
265          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
266          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
267          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
268          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
269          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
270          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
271          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
272          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
273          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
274          NULL, NULL);
275 
276 	 PO_WF_UTIL_PKG.SetItemAttrNumber ( itemtype   => itemtype,
277                                        itemkey    => itemkey,
278                                          aname      => 'REQUEST_ID',
279                                          avalue     => l_request_id);
280 
281   x_progress := 'PO_COMMUNICATION_PVT.GENERATE_PDF : Request id is  '|| l_request_id;
282 
283   IF (g_po_wf_debug = 'Y') THEN
284    PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,x_progress);
285   END IF;
286 
287 END IF;
288 END IF;
289 
290 EXCEPTION
291 
292   WHEN OTHERS THEN
293   x_progress :=  'PO_COMMUNICATION_PVT.GENERATE_PDF : In Exception handler';
294   IF (g_po_wf_debug = 'Y') THEN
295       PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
296   END IF;
297   wf_core.context('PO_COMMUNICATION_PVT','GENERATE_PDF',x_progress);
298   RAISE;
299 
300 END GENERATE_PDF;
301 
302 PROCEDURE PO_NEW_COMMUNICATION    (itemtype IN VARCHAR2,
303                                            itemkey  IN VARCHAR2,
304                                            actid    IN NUMBER,
305                                            funcmode IN VARCHAR2,
306                                            resultout   OUT NOCOPY VARCHAR2) is
307 x_progress  varchar2(100);
308 l_document_subtype po_headers.type_lookup_code%TYPE;
309 l_document_type     po_headers.type_lookup_code%TYPE;
310 
311 Begin
312  x_progress := 'PO_COMMUNICATION_PVT.PO_NEW_COMMUNICATION';
313 
314   IF (g_po_wf_debug = 'Y') THEN
315   PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
316   END IF;
317 
318 
319   -- <Bug 4100416 Start>: Do nothing in cancel or timeout modes.
320   IF (funcmode <> wf_engine.eng_run)
321   THEN
322     resultout := wf_engine.eng_null;
323     return;
324   END IF;
325   -- <Bug 4100416 End>
326 
327 
328 --Get the document type
329 
330 l_document_type := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
331                                                itemkey  => itemkey,
332                                                      aname    => 'DOCUMENT_TYPE');
333 
334 
335 l_document_subtype := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
336                                                itemkey  => itemkey,
337                                                      aname    => 'DOCUMENT_SUBTYPE');
338 
339   x_progress := 'PO_COMMUNICATION_PVT.PO_NEW_COMMUNICATION: Verify whether XDO Product is installed or not';
340 
341   IF (g_po_wf_debug = 'Y') THEN
342   PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
343   END IF;
344 
345 
346 IF PO_COMMUNICATION_PROFILE = 'T' THEN
347   IF l_document_type in ('PO','PA') and l_document_subtype in ('STANDARD','BLANKET','CONTRACT')
348       or (l_document_type = 'RELEASE' and l_document_subtype = 'BLANKET' ) THEN
349        resultout := wf_engine.eng_completed || ':' ||  'Y';
350      ELSE
351        resultout := wf_engine.eng_completed || ':' ||  'N';
352     END IF;
353 
354 Else
355     resultout := wf_engine.eng_completed || ':' ||  'N';
356 END IF;
357 
358 EXCEPTION
359 
360   WHEN OTHERS THEN
361   x_progress :=  'PO_COMMUNICATION_PVT.PO_NEW_COMMUNICATION: In Exception handler';
362   IF (g_po_wf_debug = 'Y') THEN
363         PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
364   END IF;
365   wf_core.context('PO_COMMUNICATION_PVT','PO_NEW_COMMUNICATION',x_progress);
366   raise;
367 
368 END  PO_NEW_COMMUNICATION;
369 
370 PROCEDURE DELETE_PDF_ATTACHMENTS  (itemtype IN VARCHAR2,
371                                            itemkey  IN VARCHAR2,
372                                            actid    IN NUMBER,
373                                            funcmode IN VARCHAR2,
374                                            resultout   OUT NOCOPY VARCHAR2) is
375    l_document_id       number;
376    l_document_subtype     po_headers.type_lookup_code%TYPE;
377    l_revision_num      number;
378    l_orgid             number;
379    l_entity_name       varchar2(30);
380    l_language_code     fnd_languages.language_code%type;
381    x_progress          varchar2(100);
382    l_document_type     po_headers.type_lookup_code%TYPE;
383 
384 Begin
385  x_progress := 'PO_COMMUNICATION_PVT.DELETE_PDF_ATTACHMENTS';
386 
387 IF (g_po_wf_debug = 'Y') THEN
388       PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
389 END IF;
390 
391   -- <Bug 4100416 Start>: Do nothing in cancel or timeout modes.
392   IF (funcmode <> wf_engine.eng_run)
393   THEN
394     resultout := wf_engine.eng_null;
395     return;
396   END IF;
397   -- <Bug 4100416 End>
398 
399 
400 l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype =>itemtype,
401                                                    itemkey  => itemkey,
402                                                    aname    => 'DOCUMENT_ID');
403 
404 l_revision_num := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype =>itemtype,
405                                                 itemkey => itemkey,
406                                                     aname =>'REVISION_NUMBER');
407 
408 l_document_type := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
409                                                itemkey  => itemkey,
410                                                      aname    => 'DOCUMENT_TYPE');
411 
412 
413 l_document_subtype := PO_WF_UTIL_PKG.GetItemAttrText (itemtype =>itemtype,
414                                                       itemkey =>itemkey,
415                                                       aname   =>'DOCUMENT_SUBTYPE');
416 
417 IF l_document_type in ('PO','PA') and  l_document_subtype in ( 'STANDARD','BLANKET','CONTRACT') THEN
418   l_entity_name := 'PO_HEAD';
419 ELSIF l_document_type = 'RELEASE' and l_document_subtype = 'BLANKET' THEN
420   l_entity_name :='PO_REL';
421 END IF;
422 
423 x_progress := 'PO_COMMUNICATION_PVT.DELETE_PDF_ATTACHMENTS :Calling the Delete attachments procedure';
424 
425 IF (g_po_wf_debug = 'Y') THEN
426       PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
427 END IF;
428 
429 FND_ATTACHED_DOCUMENTS2_PKG.delete_attachments(X_entity_name => l_entity_name,
430                                              X_pk1_value    =>to_char(l_document_id),
431                                              X_pk2_value    =>to_char(l_revision_num),
432                                              X_pk3_value    =>null,
433                                              X_pk4_value    =>null,
434                                              X_pk5_value    =>null,
435                                              X_delete_document_flag=>'Y',
436                                              X_automatically_added_flag=>'N');
437 
438 -- Bug 4088074 Set the REQUEST_ID item attribute to Null after deleting pdf
439 PO_WF_UTIL_PKG.SetItemAttrNumber ( itemtype   => itemtype,
440                                          itemkey    => itemkey,
441                                          aname      => 'REQUEST_ID',
442                                          avalue     => NULL);
443 
444 
445 EXCEPTION
446 
447   WHEN OTHERS THEN
448   x_progress :=  'PO_COMMUNICATION_PVT.DELETE_PDF_ATTACHMENTS:In Exception handler';
449   IF (g_po_wf_debug = 'Y') THEN
450           PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
451   END IF;
452   wf_core.context('PO_COMMUNICATION_PVT','DELETE_PDF_ATTACHMENTS',x_progress);
453   raise;
454 
455 
456 END DELETE_PDF_ATTACHMENTS;
457 
458 PROCEDURE PO_PDF_EXISTS (itemtype IN VARCHAR2,
459                                            itemkey  IN VARCHAR2,
460                                            actid    IN NUMBER,
461                                            funcmode IN VARCHAR2,
462                                            resultout   OUT NOCOPY VARCHAR2) is
463 
464 l_language_code  fnd_languages.language_code%type;
465 l_document_id    number;
466 l_revision_num   number;
467 l_terms_flag     po_headers_all.CONTERMS_EXIST_FLAG%type;
468 l_document_subtype   po_headers_all.type_lookup_code%TYPE;
469 l_document_type  po_headers_all.type_lookup_code%TYPE;
470 l_count          number;
471 l_filename       fnd_lobs.file_name%type;
472 l_orgid          number;
473 x_progress       varchar2(100);
474 l_with_terms     PO_HEADERS_ALL.CONTERMS_EXIST_FLAG%TYPE;
475 l_terms          varchar2(10);
476 
477 Begin
478 x_progress := 'PO_COMMUNICATION_PVT.PO_PDF_EXISTS';
479 
480 IF (g_po_wf_debug = 'Y') THEN
481      /* DEBUG */  PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
482 END IF;
483 
484 
485 l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
486                                             itemkey  => itemkey,
487                     aname    => 'DOCUMENT_ID');
488 
489 l_revision_num := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype        => itemtype,
490                                          itemkey         => itemkey,
491                                          aname           => 'REVISION_NUMBER');
492 
493 l_document_type := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
494                                          itemkey  => itemkey,
495                                          aname    => 'DOCUMENT_TYPE');
496 
497 
498 l_document_subtype := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
499                                          itemkey  => itemkey,
500                                          aname    => 'DOCUMENT_SUBTYPE');
501 
502 l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
503                                          itemkey  => itemkey,
504                                          aname    => 'ORG_ID');
505 
506 l_language_code := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => itemtype,
507                                          itemkey  => itemkey,
508                                          aname    =>'LANGUAGE_CODE');
509 
510 l_with_terms := PO_WF_UTIL_PKG.GetItemAttrText (itemtype =>itemtype,
511                                                  itemkey   => itemkey,
512                                                 aname  => 'WITH_TERMS');
513 IF  l_with_terms = 'Y' THEN
514  l_terms := '_TERMS_';
515 ELSE
516  l_terms := '_';
517 END IF;
518 
519 
520 --frame the file name based on po_has_terms_conditions (eg POTERMS_204_1234_1_US.pdf, PO_204_1234_1_US.pdf)
521 
522 --bug#3463617:
523 l_filename := po_communication_pvt.getPDFFileName(l_document_type,l_terms,l_orgid,l_document_id,l_revision_num,l_language_code);
524 
525 x_progress := 'PO_COMMUNICATION_PVT.PO_PDF_EXISTS: Verify whether the pdf exists for the document';
526 
527 IF (g_po_wf_debug = 'Y') THEN
528      /* DEBUG */  PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
529 END IF;
530 
531 BEGIN
532 
533 IF l_with_terms = 'Y' THEN
534 --search in contracts repository
535 x_progress := 'PO_COMMUNICATION_PVT.PO_PDF_EXISTS:Searching in the Contracts Repository';
536 
537 IF (g_po_wf_debug = 'Y') THEN
538      /* DEBUG */  PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
539 END IF;
540 
541 
542 SELECT count(*) into l_count from fnd_lobs fl,fnd_attached_docs_form_vl fad
543 WHERE
544 fl.file_id = fad.media_id and
545 fad.pk2_value=to_char(l_document_id) and
546 fad.pk3_value=to_char(l_revision_num) and
547 fl.file_name =l_filename and
548 fad.entity_name in ('PO_HEAD', 'PO_REL');
549 ELSE
550 --search in PO repository
551 x_progress := 'PO_COMMUNICATION_PVT.PO_PDF_EXISTS: Searching in the PO Repository';
552 
553 IF (g_po_wf_debug = 'Y') THEN
554      /* DEBUG */  PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
555 END IF;
556 
557 
558 -- Bug6139548
559 -- Added TO_CHAR() wherever a numeric value is compared with pk1_value or pk2_value.
560 
561 -- bug4931216
562 -- Add enttiy name filtering to utilize the index
563 SELECT count(*) into l_count from fnd_lobs fl,fnd_attached_docs_form_vl fad
564 WHERE
565 fl.file_id = fad.media_id and
566 fad.pk1_value=to_char(l_document_id) and
567 fad.pk2_value=to_char(l_revision_num) and
568 fl.file_name =l_filename and
569 fad.entity_name IN ('PO_HEAD', 'PO_REL');
570 
571 END IF;
572 
573 Exception
574     WHEN OTHERS THEN
575       l_count := 0;
576 END ;
577 
578 
579 IF  l_count >0  THEN
580          resultout := wf_engine.eng_completed || ':' ||  'Y';
581 Else
582          resultout := wf_engine.eng_completed || ':' ||  'N';
583 End if;
584 
585 EXCEPTION
586 When others then
587   x_progress :=  'PO_COMMUNICATION_PVT.PO_PDF_EXISTS: In Exception handler';
588   IF (g_po_wf_debug = 'Y') THEN
589               PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
590   END IF;
591   wf_core.context('PO_COMMUNICATION_PVT','PO_PDF_EXISTS',x_progress);
592   resultout := wf_engine.eng_completed || ':' ||  'N';
593   raise;
594 
595 END PO_PDF_EXISTS;
596 
597 PROCEDURE Start_Email_WF_Process (p_document_id             NUMBER,
598                                   p_revision_num            NUMBER,
599                                   p_document_type           VARCHAR2,
600                                   p_document_subtype        VARCHAR2,
601                                   p_email_address           VARCHAR2,
602                                   p_language_code           VARCHAR2,
603                                   p_store_flag              VARCHAR2,
604                                   p_with_terms              VARCHAR2 ) is
605 
606 l_progress          varchar2(100);
607 l_seq_for_item_key  varchar2(6);
608 l_itemkey           varchar2(60);
609 l_itemtype          po_document_types.wf_approval_itemtype%type;
610 l_workflow_process  po_document_types.wf_approval_process%type;
611 l_vendor_site_code varchar2(15);
612 l_vendor_site_id number;
613 l_vendor_site_lang PO_VENDOR_SITES.LANGUAGE%TYPE;
614 l_adhocuser_lang WF_LANGUAGES.NLS_LANGUAGE%TYPE;
615 l_adhocuser_territory WF_LANGUAGES.NLS_TERRITORY%TYPE;
616 l_po_email_add_prof       WF_USERS.EMAIL_ADDRESS%TYPE;
617 l_po_email_performer  WF_USERS.NAME%TYPE;
618 l_display_name        WF_USERS.DISPLAY_NAME%TYPE;
619 l_performer_exists number;
620 l_notification_preference varchar2(20) := 'MAILHTML';
621 l_orgid             number;
622 --l_legal_name   hr_all_organization_units.name%TYPE;
623 --bug##3682458 replaced legal entity name with operating unit
624 l_operating_unit hr_all_organization_units.name%TYPE;
625 
626 l_document_id    PO_HEADERS_ALL.po_header_id%TYPE;
627 l_docNumber       PO_HEADERS_ALL.SEGMENT1%TYPE;
628 l_doc_num_rel     varchar2(30);
629 l_release_num      PO_RELEASES.release_num%TYPE; -- Bug 3215186;
630 l_ga_flag varchar2(1) := null;  -- Bug # 3290385
631 l_doc_display_name  FND_NEW_MESSAGES.message_text%TYPE; -- Bug 3215186
632 l_attachments_exist    VARCHAR2(1); --<PO Attachment Support 11i.11>
633 
634 -- Bug 4099027. length 50 because this variable is a concatenation of
635 -- document_type_code and document_subtype
636 l_okc_doc_type  varchar2(50);
637 
638 BEGIN
639 
640 select to_char (PO_WF_ITEMKEY_S.NEXTVAL) into l_seq_for_item_key from sys.dual;
641 
642 l_itemkey := to_char(p_document_id) || '-' || l_seq_for_item_key;
643 
644 l_itemtype := 'POAPPRV';
645 
646 
647 l_progress :=  'PO_COMMUNICATION_PVT.Start_Email_WF_Process: at beginning of Start_Email_WF_Process';
648 
649 IF (g_po_wf_debug = 'Y') THEN
650  PO_WF_DEBUG_PKG.insert_debug (l_itemtype, l_itemkey,l_progress);
651 END IF;
652 
653 
654 l_workflow_process := 'EMAIL_PO_PDF';
655 
656 wf_engine.CreateProcess( ItemType => l_itemtype,
657                          ItemKey  => l_itemkey,
658                          process  => l_workflow_process );
659 
660 
661 PO_WF_UTIL_PKG.SetItemAttrNumber ( itemtype   => l_itemtype,
662                                    itemkey    => l_itemkey,
663                                    aname      => 'DOCUMENT_ID',
664                                    avalue     => p_document_id);
665 
666 PO_WF_UTIL_PKG.SetItemAttrNumber ( itemtype   => l_itemtype,
667                                    itemkey    => l_itemkey,
668                                    aname      => 'REVISION_NUMBER',
669                                    avalue     => p_revision_num);
670 
671 PO_WF_UTIL_PKG.SetItemAttrText (itemtype        => l_itemtype,
672                                 itemkey         => l_itemkey,
673                                 aname           => 'DOCUMENT_TYPE',
674                                 avalue          =>  p_document_type);
675 
676 PO_WF_UTIL_PKG.SetItemAttrText (itemtype        => l_itemtype,
677               itemkey         => l_itemkey,
678                                 aname           => 'DOCUMENT_SUBTYPE',
679                                 avalue          =>  p_document_subtype);
680 
681 PO_WF_UTIL_PKG.SetItemAttrText (itemtype  => l_itemtype,
682                                 itemkey   => l_itemkey,
683                                 aname     => 'EMAIL_ADDRESS',
684                                 avalue    =>  p_email_address);
685 
686 PO_WF_UTIL_PKG.SetItemAttrText (itemtype  => l_itemtype,
687                                 itemkey   => l_itemkey,
688                                 aname     => 'WITH_TERMS',
689                                 avalue    =>  p_with_terms);
690 
691 PO_WF_UTIL_PKG.SetItemAttrText (itemtype        => l_itemtype,
692               itemkey         => l_itemkey,
693                                 aname           => 'LANGUAGE_CODE',
694                                 avalue          =>  p_language_code);
695 
696 PO_WF_UTIL_PKG.SetItemAttrText(itemtype => l_itemtype,
697                            itemkey => l_itemkey,
698                            aname => 'EMAIL_TEXT_WITH_PDF',
699                       avalue=>FND_MESSAGE.GET_STRING('PO','PO_PDF_EMAIL_TEXT'));
700 
701 l_orgid := po_moac_utils_pvt.get_current_org_id; --<R12 MOAC>
702 
703 IF  l_orgid is not null  THEN
704 --bug#3682458 replaced the sql that retrieves legal entity
705 --name with sql that retrieves operating unit name
706  BEGIN
707       SELECT hou.name
708       into   l_operating_unit
709       FROM
710              hr_organization_units hou
711       WHERE
712              hou.organization_id = l_orgid;
713  EXCEPTION
714       WHEN OTHERS THEN
715          l_operating_unit:=null;
716  END;
717 END IF;
718 
719 PO_WF_UTIL_PKG.SetItemAttrNumber ( itemtype   => l_itemtype,
720                                    itemkey    => l_itemkey,
721                                    aname      => 'ORG_ID',
722                                    avalue     =>l_orgid );
723 --bug#3682458 replaced legal_entity_name with operating_unit_name
724 PO_WF_UTIL_PKG.SetItemAttrText(itemtype => l_itemtype,
725                            itemkey => l_itemkey,
726                            aname => 'OPERATING_UNIT_NAME',
727                            avalue=>l_operating_unit);
728 
729 -- Bug # 3290385 Start
730 if p_document_type = 'RELEASE' then
731 select po_header_id,release_num  into l_document_id,l_release_num
732 from po_releases_all
733 where
734 po_release_id=p_document_id;
735 else
736   l_document_id:=p_document_id;
737 end if;
738 
739 select segment1,global_agreement_flag into l_docNumber,l_ga_flag
740 from po_headers_all
741 where po_header_id = l_document_id;
742 
743 wf_engine.SetItemAttrText (   itemtype   => l_itemtype,
744                                         itemkey    => l_itemkey,
745                                         aname      => 'DOCUMENT_NUMBER',
746                                         avalue     => l_docNumber);
747 
748 
749 select DECODE(p_document_subtype,'BLANKET',FND_MESSAGE.GET_STRING('POS','POS_POTYPE_BLKT'),
750         'CONTRACT',FND_MESSAGE.GET_STRING('POS','POS_POTYPE_CNTR'),
751         'STANDARD',FND_MESSAGE.GET_STRING('POS','POS_POTYPE_STD'),
752         'PLANNED',FND_MESSAGE.GET_STRING('POS','POS_POTYPE_PLND')) into l_doc_display_name from dual;
753 if l_ga_flag = 'Y' then
754     l_doc_display_name := FND_MESSAGE.GET_STRING('PO','PO_GA_TYPE');
755 end if;
756 
757 if p_document_type = 'RELEASE' then
758   l_doc_num_rel := l_docNumber || '-' || l_release_num;
759   l_doc_display_name := FND_MESSAGE.GET_STRING('POS','POS_POTYPE_BLKTR');
760 else
761   l_doc_num_rel := l_docNumber;
762 end if;
763 if l_doc_num_rel is not null then
764 wf_engine.SetItemAttrText (itemtype        => l_itemtype,
765                                  itemkey         => l_itemkey,
766                                  aname           => 'DOCUMENT_NUM_REL',
767                                  avalue          =>  l_doc_num_rel);
768 end if;
769 
770 IF (p_document_type = 'PA' AND p_document_subtype IN ('BLANKET','CONTRACT')) OR
771    (p_document_type = 'PO' AND p_document_subtype = 'STANDARD')  THEN
772 
773        l_doc_display_name := PO_DOC_STYLE_PVT.GET_STYLE_DISPLAY_NAME(l_document_id);
774 
775 END IF;
776 
777 
778 wf_engine.SetItemAttrText (itemtype        => l_itemtype,
779                                  itemkey         => l_itemkey,
780                                  aname           => 'DOCUMENT_DISPLAY_NAME',
781                                  avalue          => l_doc_display_name );
782 -- Bug # 3290385 End
783 
784 l_progress :=  'PO_COMMUNICATION_PVT.Start_Email_WF_Process: Get the Supplier site language';
785 
786 IF (g_po_wf_debug = 'Y') THEN
787  PO_WF_DEBUG_PKG.insert_debug (l_itemtype, l_itemkey,l_progress);
788 END IF;
789 
790 if p_document_type = 'RELEASE' then
791         select poh.vendor_site_id, pvs.vendor_site_code, pvs.language
792         into l_vendor_site_id, l_vendor_site_code, l_vendor_site_lang
793         from po_headers poh, po_vendor_sites pvs, po_releases por
794         where pvs.vendor_site_id = poh.vendor_site_id
795         and poh.po_header_id = por.po_header_id
796         and por.po_release_id =  p_document_id;
797 else
798         select poh.vendor_site_id, pvs.vendor_site_code, pvs.language
799         into l_vendor_site_id, l_vendor_site_code, l_vendor_site_lang
800         from po_headers poh, po_vendor_sites pvs
801         where pvs.vendor_site_id = poh.vendor_site_id
802         and poh.po_header_id =  p_document_id;
803 end if;
804 
805  IF l_vendor_site_lang is  NOT NULL then
806 
807  SELECT wfl.nls_language, wfl.nls_territory INTO l_adhocuser_lang, l_adhocuser_territory
808  FROM wf_languages wfl, fnd_languages_vl flv
809  WHERE wfl.code = flv.language_code AND flv.nls_language = l_vendor_site_lang;
810 
811 ELSE
812 
813  SELECT wfl.nls_language, wfl.nls_territory into l_adhocuser_lang, l_adhocuser_territory
814  FROM wf_languages wfl, fnd_languages_vl flv
815  WHERE wfl.code = flv.language_code AND flv.installed_flag = 'B';
816 
817 END IF;
818 
819 l_po_email_performer := p_email_address||'.'||l_adhocuser_lang;
820 l_po_email_performer := upper(l_po_email_performer);
821 l_display_name := p_email_address; -- Bug # 3290385
822 
823 l_progress :=  'PO_COMMUNICATION_PVT.Start_Email_WF_Process: Verify whether the role exists in wf_users';
824 
825 IF (g_po_wf_debug = 'Y') THEN
826  PO_WF_DEBUG_PKG.insert_debug (l_itemtype, l_itemkey,l_progress);
827 END IF;
828 
829 
830 select count(1) into l_performer_exists
831 from wf_users where name = l_po_email_performer;
832 
833  if (l_performer_exists = 0) then
834 
835 -- Pass in the correct adhocuser language and territory for CreateAdHocUser and SetAdhocUserAttr instead of null
836 
837 WF_DIRECTORY.CreateAdHocUser(l_po_email_performer, l_display_name, l_adhocuser_lang, l_adhocuser_territory, null, l_notification_preference,p_email_address, null, 'ACTIVE', null);
838 
839 else
840 
841 WF_DIRECTORY.SETADHOCUSERATTR(l_po_email_performer, l_display_name, l_notification_preference, l_adhocuser_lang, l_adhocuser_territory, p_email_address,null);
842 
843 end if;
844 
845 PO_WF_UTIL_PKG.SetItemAttrText ( itemtype  => l_itemtype,
846                                     itemkey   => l_itemkey,
847                                     aname     => 'PO_PDF_EMAIL_PERFORMER',
848                                     avalue    =>  l_po_email_performer);
849 PO_WF_UTIL_PKG.SetItemAttrText (itemtype => l_itemtype,
850                            itemkey => l_itemkey,
851                            aname => 'PDF_ATTACHMENT',
852 avalue => 'PLSQLBLOB:PO_COMMUNICATION_PVT.PDF_ATTACH_SUPP/'||l_itemtype||':'||l_itemkey);
853 
854 --Bug6998166, to increase print_count
855 PO_REQAPPROVAL_INIT1.update_print_count(p_document_id,p_document_type);
856 
857   --<Bug 4099027 Start> Set up okc doc attachment attribute, if necessary
858   IF (p_with_terms = 'Y') THEN
859     l_okc_doc_type := PO_CONTERMS_UTL_GRP.get_po_contract_doctype(p_document_subtype);
860 
861     IF ( ('STRUCTURED' <> OKC_TERMS_UTIL_GRP.get_contract_source_code
862                           ( p_document_type => l_okc_doc_type
863                           , p_document_id   => p_document_id))
864          AND
865          ('N' = OKC_TERMS_UTIL_GRP.is_primary_terms_doc_mergeable
866                 ( p_document_type => l_okc_doc_type
867                 , p_document_id   => p_document_id))
868        ) THEN
869 
870         PO_WF_UTIL_PKG.SetItemAttrText
871         ( itemtype => l_itemtype,
872           itemkey  => l_itemkey,
873           aname    => 'OKC_DOC_ATTACHMENT',
874           avalue   => 'PLSQLBLOB:PO_COMMUNICATION_PVT.OKC_DOC_ATTACH/'||
875                                                  l_itemtype||':'||l_itemkey);
876     END IF; -- not structured and not mergeable
877 
878   END IF; --IF (p_with_terms = 'Y')
879   --<Bug 4099027 End>
880 
881 
882   --<PO Attachment Support 11i.11>
883   -- Get the 'Maximum Attachment Size' value from Purchasing Options
884   -- A value of 0 means Zip Attachments are not supported.
885   IF get_max_zip_size(l_itemtype,l_itemkey) > 0 THEN
886       Begin
887           l_attachments_exist := check_for_attachments(p_document_type => p_document_type,
888                                                        p_document_id   => p_document_id);
889       Exception when others then
890           null;  -- Do not show the Zip attachment link if there are no attachments
891                  -- or in case of any exceptions
892       END;
893       IF l_attachments_exist = 'Y' THEN
894           PO_WF_UTIL_PKG.SetItemAttrText (itemtype => l_itemtype,
895                                           itemkey  => l_itemkey,
896                                           aname    => 'ZIP_ATTACHMENT',
897                                           avalue   => 'PLSQLBLOB:PO_COMMUNICATION_PVT.ZIP_ATTACH/'||l_itemtype||':'||l_itemkey);
898       END IF;
899   END IF;
900 
901 l_progress :=  'PO_COMMUNICATION_PVT.Start_Email_WF_Process:Start the workflow process';
902 
903 IF (g_po_wf_debug = 'Y') THEN
904  PO_WF_DEBUG_PKG.insert_debug (l_itemtype, l_itemkey,l_progress);
905 END IF;
906 
907 wf_engine. StartProcess (itemtype => l_itemtype, itemkey => l_itemkey);
908 
909 
910 EXCEPTION
911  WHEN OTHERS THEN
912 
913 l_progress :=  'PO_COMMUNICATION_PVT.Start_WF_Process_Email: In Exception handler';
914 
915    IF (g_po_wf_debug = 'Y') THEN
916    PO_WF_DEBUG_PKG.insert_debug(l_itemtype,l_itemkey,l_progress);
917    END IF;
918 
919    RAISE;
920 
921 END  Start_Email_WF_Process;
922 
923 
924 --<FP 11i10+ - R12 Contract ER TC Sup Lang Start >
925 -- Generates the pdf doc with terms in suppliers language
926 PROCEDURE GENERATE_PDF_SUPP_TC (itemtype IN VARCHAR2,
927                                 itemkey  IN VARCHAR2,
928                                 actid    IN NUMBER,
929                                 funcmode IN VARCHAR2,
930                                 resultout   OUT NOCOPY VARCHAR2)
931     IS
932 
933    l_document_id           po_headers.po_header_id%TYPE;
934    l_revision_num          po_headers.revision_num%TYPE;
935    l_document_subtype      po_headers.type_lookup_code%TYPE;
936    l_document_type         po_headers.type_lookup_code%TYPE;
937    l_territory             fnd_languages.nls_territory%type;
938    l_language_code         fnd_languages.language_code%type;
939    l_supp_lang             po_vendor_sites_all.language%TYPE;
940    l_language              fnd_languages.nls_language%type;
941    l_authorization_status  po_headers.authorization_status%TYPE;
942    l_header_id             po_headers.po_header_id%TYPE;
943 
944    l_with_terms  varchar2(1);
945    --l_old_request_id  number; --Bug 7299381
946    l_request_id number;
947    l_set_lang   boolean;
948 
949    x_progress  varchar2(100);
950 
951 begin
952 x_progress := 'PO_COMMUNICATION_PVT.GENERATE_PDF_SUPP';
953 
954   IF (g_po_wf_debug = 'Y') THEN
955    PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,x_progress);
956   END IF;
957 
958   l_document_type := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
959  	                                             itemkey  => itemkey,
960                                                      aname    => 'DOCUMENT_TYPE');
961 
962   l_document_subtype := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
963                                          itemkey  => itemkey,
964                                         aname    => 'DOCUMENT_SUBTYPE');
965 
966   l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
967                                          itemkey  => itemkey,
968                                          aname    => 'DOCUMENT_ID');
969 
970   l_revision_num := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
971                                          itemkey         => itemkey,
972                                          aname           => 'REVISION_NUMBER');
973 
974   l_language_code := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
975                                                     itemkey  => itemkey,
976                                                     aname    =>'LANGUAGE_CODE');
977 
978   l_with_terms := PO_WF_UTIL_PKG.GetItemAttrText (itemtype =>itemtype,
979                                                   itemkey   => itemkey,
980                                                   aname  => 'WITH_TERMS');
981 
982   --Bug# 5498523: We need the correct Authurization status so the logic in PoGenerateDocument.StorePDF
983   --will store the PDF in the contracts repository.
984   l_authorization_status := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => itemtype,
985                                                            itemkey  => itemkey,
986                                                            aname    => 'AUTHORIZATION_STATUS');
987 
988 IF l_document_type in ('PO','PA') and
989    l_document_subtype in ('STANDARD','BLANKET','CONTRACT') and
990    l_with_terms = 'Y' THEN
991 
992    l_header_id := l_document_id;
993 
994    SELECT pv.language
995    INTO   l_supp_lang
996    FROM po_vendor_sites_all pv,
997         po_headers_all ph
998    WHERE  ph.po_header_id = l_header_id
999    AND ph.vendor_site_id = pv.vendor_site_id;
1000 
1001 
1002   x_progress := 'PO_COMMUNICATION_PVT.GENERATE_PDF_SUPP_TC :launching the Dispatch Purchase Order concurrent program ';
1003 
1004   IF (g_po_wf_debug = 'Y') THEN
1005    PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,x_progress);
1006   END IF;
1007 
1008 
1009 	--set the suppliers language before launching the concurrent request
1010 	--Bug6841986/6528046 userenv('LANG')==> l_language_code
1011 	SELECT nls_language
1012 	INTO l_language
1013 	FROM fnd_languages
1014 	WHERE language_code = l_language_code;
1015 	--End Bug6841986/6528046
1016 
1017   IF  l_language <> l_supp_lang  then
1018      SELECT nls_territory
1019      INTO l_territory
1020      FROM fnd_languages
1021      WHERE nls_language = l_supp_lang;
1022 
1023      l_set_lang := fnd_request.set_options('NO', 'NO', l_supp_lang,l_territory, NULL);
1024 
1025   --<Bug 5373928 START>- Set the org context
1026   --<R12 MOAC START>
1027   po_moac_utils_pvt.set_request_context(po_moac_utils_pvt.get_current_org_id);
1028   --<R12 MOAC END>
1029   --<Bug 5373928 END>
1030   --Bug5080617 Pass the parameters P_PO_TEMPLATE_CODE and P_CONTRACT_TEMPLATE_CODE as null
1031      l_request_id := fnd_request.submit_request('PO',
1032         'POXPOPDF',
1033         null,
1034         null,
1035         false,
1036         'R',--P_report_type
1037         null  ,--P_agend_id
1038         null,--P_po_num_from
1039 	null           ,--P_po_num_to
1040 	null           ,--P_relaese_num_from
1041 	null           ,--P_release_num_to
1042 	null           ,--P_date_from
1043 	null           ,--P_date_to
1044 	null           ,--P_approved_flag
1045         'N',--P_test_flag
1046 	null           ,--P_print_releases
1047 	null           ,--P_sortby
1048 	null           ,--P_user_id
1049 	null           ,--P_fax_enable
1050 	null           ,--P_fax_number
1051 	null           ,--P_BLANKET_LINES
1052 	'View'           ,--View_or_Communicate,
1053          l_with_terms,--P_WITHTERMS
1054          'Y',--P_storeFlag
1055          'N',--P_PRINT_FLAG
1056          l_document_id,--P_DOCUMENT_ID
1057          l_revision_num,--P_REVISION_NUM
1058          l_authorization_status,--P_AUTHORIZATION_STATUS
1059          l_document_subtype,--P_DOCUMENT_TYPE
1060          null, -- P_PO_TEMPLATE_CODE
1061          null, -- P_CONTRACT_TEMPLATE_CODE
1062          fnd_global.local_chr(0),
1063          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1064          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1065          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1066          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1067          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1068          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1069          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1070          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1071          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1072          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1073          NULL, NULL, NULL);
1074 
1075 
1076 	-- Bug 7299381
1077 	/* Changed the order of calling 4 PDF generation CPs for different cases.
1078 	   Setting the REQUEST_ID attribute (w/o any condition) in all 4 procedures */
1079 	PO_WF_UTIL_PKG.SetItemAttrNumber (	itemtype   => itemtype,
1080 						itemkey    => itemkey,
1081 						aname      => 'REQUEST_ID',
1082 						avalue     => l_request_id);
1083 	-- End Bug 7299381
1084 
1085    x_progress := 'PO_COMMUNICATION_PVT.GENERATE_PDF_SUPP_TC : Request id is - '|| l_request_id;
1086 
1087    IF (g_po_wf_debug = 'Y') THEN
1088     PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,x_progress);
1089    END IF;
1090 
1091  END IF; -- language <> supplier language
1092 
1093 END IF; -- if with terms = 'Y' and doc type = std, blanket, contract
1094 
1095 EXCEPTION
1096 
1097   WHEN OTHERS THEN
1098   x_progress :=  'PO_COMMUNICATION_PVT.GENERATE_PDF_SUPP_TC: In Exception handler';
1099 
1100   IF (g_po_wf_debug = 'Y') THEN
1101     	 PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
1102   END IF;
1103   wf_core.context('PO_COMMUNICATION_PVT','GENERATE_PDF_SUPP_TC',x_progress);
1104   raise;
1105 
1106 END GENERATE_PDF_SUPP_TC;
1107 --<FP 11i10+ - R12 Contract ER TC Sup Lang End >
1108 
1109 PROCEDURE GENERATE_PDF_BUYER (itemtype IN VARCHAR2,
1110                                            itemkey  IN VARCHAR2,
1111                                            actid    IN NUMBER,
1112                                            funcmode IN VARCHAR2,
1113                                            resultout   OUT NOCOPY VARCHAR2)
1114     IS
1115 
1116    l_document_id number;
1117    l_document_subtype po_headers.type_lookup_code%TYPE;
1118    l_document_type po_headers.type_lookup_code%TYPE;
1119    l_revision_num  number;
1120    l_request_id number;
1121    l_conterm_exists PO_HEADERS_ALL.CONTERMS_EXIST_FLAG%TYPE;
1122    l_authorization_status varchar2(25);
1123    l_progress  varchar2(200);
1124    --l_old_request_id  number; --Bug 7299381
1125    l_withterms  varchar2(1);
1126 
1127    --<PO Attachment Support 11i.11 Start>
1128    l_attachments_exist varchar2(1);   -- holds 'Y' if there are any supplier
1129                                       -- file attachments
1130    l_duplicate_filenames varchar2(1); -- holds 'Y' if there are any supplier
1131                                       -- file attachments with same filename
1132    l_error_flag number; -- determines if the error condition (same file name
1133                         -- but different file lengths has been met or not)
1134    l_max_attachment_size po_system_parameters_all.max_attachment_size%type;
1135    l_filename fnd_lobs.file_name%type;
1136    l_filename_new fnd_lobs.file_name%type;
1137    l_length number;
1138    l_length_new number;
1139 
1140    -- bug4931216
1141    -- Join directly to the base table to improve performance
1142    cursor l_get_po_attachments_csr(l_po_header_id number) is
1143        select fl.file_name,dbms_lob.getlength(fl.file_data)
1144        from   fnd_documents d,
1145               fnd_attached_documents ad,
1146               fnd_doc_category_usages dcu,
1147               fnd_attachment_functions af,
1148               fnd_lobs fl
1149        where ((ad.pk1_value=to_char(l_po_header_id) and ad.entity_name='PO_HEADERS')
1150               OR
1151               (ad.pk1_value=to_char((select vendor_id from po_headers_all
1152                               where po_header_id=l_po_header_id)) and ad.entity_name='PO_VENDORS')
1153               OR
1154               (ad.pk1_value in (select po_line_id from po_lines_all
1155                                  where po_header_id=l_po_header_id
1156                                 ) and ad.entity_name='PO_LINES')
1157               OR
1158               (ad.pk1_value in (select from_header_id from po_lines_all
1159                                  where po_header_id=l_po_header_id
1160                                  and from_header_id is not null
1161                                 ) and ad.entity_name='PO_HEADERS')
1162               OR
1163               (ad.pk1_value in (select from_line_id from po_lines_all
1164                                  where po_header_id=l_po_header_id
1165                                  and from_line_id is not null
1166                                 ) and ad.entity_name='PO_LINES')
1167               OR
1168               (ad.pk1_value in (select line_location_id from po_line_locations_all
1169                                  where po_header_id=l_po_header_id
1170                                  and shipment_type in ('PRICE BREAK','STANDARD', 'PREPAYMENT')  -- <Complex Work R12>
1171                                 ) and ad.entity_name='PO_SHIPMENTS')
1172               OR
1173               (ad.pk2_value in (select item_id from po_lines_all
1174                                  where po_header_id=l_po_header_id
1175                                  and to_char(PO_COMMUNICATION_PVT.getInventoryOrgId())=ad.pk1_value --Bug 4673653 Use Inventory OrgId
1176                                  and item_id is not null
1177                                 ) and ad.entity_name='MTL_SYSTEM_ITEMS')
1178              )
1179        and d.document_id = ad.document_id
1180        and dcu.category_id = d.category_id
1181        and dcu.attachment_function_id = af.attachment_function_id
1182        and d.datatype_id=6
1183        and af.function_name='PO_PRINTPO'
1184        and d.media_id=fl.file_id
1185        and dcu.enabled_flag = 'Y'
1186        group by fl.file_name,dbms_lob.getlength(fl.file_data)
1187        order by fl.file_name;
1188 
1189    -- bug4931216
1190    -- Join directly to the base table to improve performance
1191    cursor l_get_release_attachments_csr(l_po_release_id number) is
1192        select fl.file_name,dbms_lob.getlength(fl.file_data)
1193        from   fnd_documents d,
1194               fnd_attached_documents ad,
1195               fnd_doc_category_usages dcu,
1196               fnd_attachment_functions af,
1197               fnd_lobs fl
1198        where ((ad.pk1_value=to_char((select po_header_id from po_releases_all
1199                               where po_release_id=l_po_release_id
1200                              )) and ad.entity_name='PO_HEADERS')
1201               OR
1202               (ad.pk1_value=to_char(l_po_release_id) and ad.entity_name='PO_RELEASES')
1203               OR
1204               (ad.pk1_value=to_char((select pha.vendor_id
1205                               from po_headers_all pha,po_releases_all pra
1206                               where pra.po_release_id=l_po_release_id
1207                               and pha.po_header_id=pra.po_header_id
1208                              )) and ad.entity_name='PO_VENDORS')
1209               OR
1210               (ad.pk1_value in (select po_line_id from po_line_locations_all
1211                                  where po_release_id=l_po_release_id
1212                                  and shipment_type='BLANKET'
1213                                 ) and ad.entity_name='PO_LINES')
1214               OR
1215               (ad.pk1_value in (select line_location_id from po_line_locations_all
1216                                  where po_release_id=l_po_release_id
1217                                  and shipment_type='BLANKET'
1218                                 ) and ad.entity_name='PO_SHIPMENTS')
1219               OR
1220               (ad.pk2_value in (select pl.item_id
1221                                  from po_lines_all pl, po_line_locations_all pll
1222                                  where pll.po_release_id=l_po_release_id
1223                                  and pll.shipment_type='BLANKET'
1224                                  and pll.po_line_id=pl.po_line_id
1225                                  and to_char(PO_COMMUNICATION_PVT.getInventoryOrgId())=ad.pk1_value --Bug 4673653 Use Inventory OrgId
1226                                  and pl.item_id is not null
1227                                 ) AND ad.entity_name='MTL_SYSTEM_ITEMS')
1228              )
1229        and d.document_id = ad.document_id
1230        and dcu.category_id = d.category_id
1231        and dcu.attachment_function_id = af.attachment_function_id
1232        and d.datatype_id=6
1233        and af.function_name='PO_PRINTPO'
1234        and d.media_id=fl.file_id
1235        and dcu.enabled_flag = 'Y'
1236        group by fl.file_name,dbms_lob.getlength(fl.file_data)
1237        order by fl.file_name;
1238    --<PO Attachment Support 11i.11 End>
1239 
1240 begin
1241 l_progress := 'PO_COMMUNICATION_PVT.generate_pdf_buyer ';
1242 
1243   IF (g_po_wf_debug = 'Y') THEN
1244    PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,l_progress);
1245   END IF;
1246 
1247   -- <Bug 4100416 Start>: Do nothing in cancel or timeout modes.
1248   IF (funcmode <> wf_engine.eng_run)
1249   THEN
1250     resultout := wf_engine.eng_null;
1251     return;
1252   END IF;
1253   -- <Bug 4100416 End>
1254 
1255 
1256   l_document_type := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
1257                                                itemkey  => itemkey,
1258                                                      aname    => 'DOCUMENT_TYPE');
1259 
1260   l_document_subtype := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
1261                                          itemkey  => itemkey,
1262                                         aname    => 'DOCUMENT_SUBTYPE');
1263 
1264   l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
1265                                          itemkey  => itemkey,
1266                                          aname    => 'DOCUMENT_ID');
1267 
1268   l_revision_num := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype   => itemtype,
1269                                                      itemkey         => itemkey,
1270                                  aname           => 'REVISION_NUMBER');
1271 
1272   l_authorization_status := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => itemtype,
1273                                                      itemkey         => itemkey,
1274                                  aname           => 'AUTHORIZATION_STATUS');
1275 /*Bug#3583910 Modified the name of the attribute to WITH_TERMS from WITHTERMS */
1276   l_withterms := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
1277                                                  itemkey  => itemkey,
1278                                                  aname    => 'WITH_TERMS');
1279 
1280   --Bug 7299381
1281   /*
1282   l_old_request_id := PO_WF_UTIL_PKG.GetItemAttrNumber(itemtype   => itemtype,
1283                                                     itemkey    => itemkey,
1284                 aname=>'REQUEST_ID');
1285   */
1286 
1287 	/*  Bug6841986/6528046
1288 		PO Approval Workflow makes some calls to these procedures, to create pdf files, based on
1289 		some criteria like Terms and Conditions attached or not and whether Supplier language is
1290 		different from Buyer's language or not.
1291 		From those different calls, these procedures return the CP request IDs based on different
1292 		priorities. Corrected the calls order and Request ID updation criteria based on their priorities.
1293 	*/
1294 	/* Bug6841986/6528046
1295 	IF l_document_type in ('PO','PA') and l_document_subtype in ('STANDARD','BLANKET','CONTRACT') THEN
1296 		 IF l_old_request_id is null and l_withterms = 'Y' THEN
1297 			l_withterms := 'Y' ;
1298 		 ELSIF l_old_request_id is not null THEN
1299 			l_withterms := 'N';
1300 		 END IF;
1301 	  ELSE
1302 		 l_withterms :='N';
1303 	  END IF;
1304 	*/ --End Bug6841986/6528046
1305 
1306 l_progress := 'PO_COMMUNICATION_PVT.generate_pdf_buyer :Launching the Dispatch Purchase Order program ';
1307 
1308   IF (g_po_wf_debug = 'Y') THEN
1309    PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,l_progress);
1310   END IF;
1311 
1312 --Bug#3481824 Need to pass document type as
1313 --RELEASE in case of a release to the concurrent program
1314 
1315 IF l_document_type ='RELEASE' THEN
1316    l_document_subtype :='RELEASE';
1317 END IF;
1318 
1319     --<PO Attachment Support 11i.11 Start>
1320     -- In the whole of Zip generation process, all unexpected exceptions must
1321     -- be handled and none should be raised to the workflow because that will
1322     -- stop the workflow process would prevent sending the error notification.
1323     -- In case of any unexpected exceptions, the exception should be handled
1324     -- and workflow attribute ZIP_ERROR_CODE should be set to 'UNEXPECTED' so
1325     -- that corresponding error notification can be sent to buyer and supplier.
1326     -- Also in case of exception, l_max_attachment_size should be set to 0 so
1327     -- that Zip file is not generated.
1328     Begin
1329         -- Get the 'Maximum Attachment Size' value from Purchasing Options
1330         -- A value of 0 means Zip Attachments are not supported
1331         l_progress := 'PO_COMMUNICATION_PVT.generate_pdf_buyer: Get max zip attachment size ';
1332         l_max_attachment_size := get_max_zip_size(itemtype,itemkey);
1333 
1334         IF l_max_attachment_size > 0 THEN
1335             -- If PO has no 'To Supplier' file attachments then 'Zip Attachment' link
1336             -- should not show up in the notifications and Zip file should not be generated
1337             l_attachments_exist := 'N';
1338             l_progress := 'PO_COMMUNICATION_PVT.generate_pdf_buyer: Checking for supplier file attachments';
1339             Begin
1340                 l_attachments_exist := check_for_attachments(p_document_type => l_document_type,
1341                                                              p_document_id      => l_document_id);
1342             Exception when no_data_found then
1343                 l_progress := 'PO_COMMUNICATION_PVT.generate_pdf_buyer: No supplier file attachments exist for this document';
1344                 IF (g_po_wf_debug = 'Y') THEN
1345                     PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,l_progress);
1346                 END IF;
1347                 l_max_attachment_size := 0; -- No need to generate zip file if no 'To Supplier' file attachments exist
1348             End;
1349 
1350             IF l_attachments_exist = 'Y' THEN
1351                 l_progress := 'PO_COMMUNICATION_PVT.generate_pdf_buyer: Setting workflow attribute to display Zip Attachment';
1352                 PO_WF_UTIL_PKG.SetItemAttrText (itemtype => itemtype,
1353                                                 itemkey  => itemkey,
1354                                                 aname    => 'ZIP_ATTACHMENT',
1355                                                 avalue   => 'PLSQLBLOB:PO_COMMUNICATION_PVT.ZIP_ATTACH/'||itemtype||':'||itemkey);
1356 
1357 
1358                 -- An error condition is when two or more file attachments have the same file name
1359                 -- but different file sizes. In this case a zip error notification should be sent
1360                 -- and zip file should not be generated.
1361                 -- Following two cases are ok:
1362                 --   1. There are no duplicate file names in the PO Attachments
1363                 --   2. Files with same name also have the same sizes
1364                 -- Case 1 would be most common and is given highest priority in terms of performance.
1365                 -- So a separate query for finding duplicate file names is written. If no duplicate
1366                 -- file names then cursors for checking the error condition are not opened.
1367 
1368                 l_progress := 'PO_COMMUNICATION_PVT.generate_pdf_buyer : Check for duplicate filenames';
1369                 l_duplicate_filenames := 'N';
1370 
1371 
1372                 IF (l_document_subtype='RELEASE') THEN
1373 
1374                     -- bug4917605
1375                     -- User base table to improve performance
1376                     Begin
1377                         select 'Y' into l_duplicate_filenames from dual
1378                         where exists
1379                         (
1380                             select fl.file_name
1381                             from fnd_documents d,
1382                                  fnd_attached_documents ad,
1383                                  fnd_doc_category_usages dcu,
1384                                  fnd_attachment_functions af,
1385                                  fnd_lobs fl
1386                             where ((ad.pk1_value=to_char((select po_header_id from po_releases_all
1387                                                    where po_release_id=l_document_id
1388                                                   )) and ad.entity_name='PO_HEADERS')
1389                                    OR
1390                                    (ad.pk1_value=to_char(l_document_id) and ad.entity_name='PO_RELEASES')
1391                                    OR
1392                                    (ad.pk1_value=(select pha.vendor_id
1393                                                    from po_headers_all pha,po_releases_all pra
1394                                                    where pra.po_release_id=l_document_id
1395                                                    and pha.po_header_id=pra.po_header_id
1396                                                   ) and ad.entity_name='PO_VENDORS')
1397                                    OR
1398                                    (ad.pk1_value in (select po_line_id from po_line_locations_all
1399                                                       where po_release_id=l_document_id
1400                                                       and shipment_type='BLANKET'
1401                                                      ) and ad.entity_name='PO_LINES')
1402                                    OR
1403                                    (ad.pk1_value in (select line_location_id from po_line_locations_all
1404                                                       where po_release_id=l_document_id
1405                                                       and shipment_type='BLANKET'
1406                                                      ) and ad.entity_name='PO_SHIPMENTS')
1407                                    OR
1408                                    (ad.pk2_value in (select pl.item_id
1409                                                       from po_lines_all pl, po_line_locations_all pll
1410                                                       where pll.po_release_id=l_document_id
1411                                                       and pll.shipment_type='BLANKET'
1412                                                       and pll.po_line_id=pl.po_line_id
1413                                                       and to_char(PO_COMMUNICATION_PVT.getInventoryOrgId())=ad.pk1_value --Bug 4673653 Use Inventory OrgId
1414                                                       and pl.item_id is not null
1415                                                      ) AND ad.entity_name='MTL_SYSTEM_ITEMS')
1416                                   )
1417                             and d.document_id = ad.document_id
1418                             and dcu.category_id = d.category_id
1419                             and dcu.attachment_function_id = af.attachment_function_id
1420                             and d.datatype_id=6
1421                             and af.function_name='PO_PRINTPO'
1422                             and d.media_id=fl.file_id
1423                             and dcu.enabled_flag = 'Y'
1424                             group by fl.file_name
1425                             having count(*)>1
1426                         );
1427                     -- If no_data_found then let l_duplicate_filename remain 'N'
1428                     -- so that cursor is not opened. All other exceptions raised
1429                     -- until caught by outer exception handler
1430                     Exception when no_data_found then
1431                         l_progress := 'PO_COMMUNICATION_PVT.generate_pdf_buyer: No duplicate filenames exist in the attachments for this Release';
1432                         IF (g_po_wf_debug = 'Y') THEN
1433                             PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,l_progress);
1434                         END IF;
1435                     End;
1436                 ELSE
1437                     -- bug4917605
1438                     -- User base table to improve performance
1439                     Begin
1440                         select 'Y' into l_duplicate_filenames from dual
1441                         where exists
1442                         (
1443                             select fl.file_name
1444                             from fnd_documents d,
1445                                  fnd_attached_documents ad,
1446                                  fnd_doc_category_usages dcu,
1447                                  fnd_attachment_functions af,
1448                                  fnd_lobs fl
1449                             where ((ad.pk1_value=to_char(l_document_id) and ad.entity_name='PO_HEADERS')
1450                                    OR
1451                                    (ad.pk1_value=to_char((select vendor_id from po_headers_all
1452                                                    where po_header_id=l_document_id)) and ad.entity_name='PO_VENDORS')
1453                                    OR
1454                                    (ad.pk1_value in (select po_line_id from po_lines_all
1455                                                       where po_header_id=l_document_id
1456                                                      ) and ad.entity_name='PO_LINES')
1457                                    OR
1458                                    (ad.pk1_value in (select from_header_id from po_lines_all
1459                                                       where po_header_id=l_document_id
1460                                                       and from_header_id is not null
1461                                                      ) and ad.entity_name='PO_HEADERS')
1462                                    OR
1463                                    (ad.pk1_value in (select from_line_id from po_lines_all
1464                                                       where po_header_id=l_document_id
1465                                                       and from_line_id is not null
1466                                                      ) and ad.entity_name='PO_LINES')
1467                                    OR
1468                                    (ad.pk1_value in (select line_location_id from po_line_locations_all
1469                                                       where po_header_id=l_document_id
1470                                                       and shipment_type in ('PRICE BREAK','STANDARD','PREPAYMENT')  -- <Complex Work R12>
1471                                                      ) and ad.entity_name='PO_SHIPMENTS')
1472                                    OR
1473                                    (ad.pk2_value in (select item_id from po_lines_all
1474                                                       where po_header_id=l_document_id
1475                                                       and to_char(PO_COMMUNICATION_PVT.getInventoryOrgId())=ad.pk1_value --Bug 4673653 Use Inventory OrgId
1476                                                       and item_id is not null
1477                                                      ) and ad.entity_name='MTL_SYSTEM_ITEMS')
1478                                   )
1479                             and d.document_id = ad.document_id
1480                             and dcu.category_id = d.category_id
1481                             and dcu.attachment_function_id = af.attachment_function_id
1482                             and d.datatype_id=6
1483                             and af.function_name='PO_PRINTPO'
1484                             and d.media_id=fl.file_id
1485                             and dcu.enabled_flag = 'Y'
1486                             group by fl.file_name
1487                             having count(*)>1
1488                         );
1489                     -- If no_data_found then let l_duplicate_filename remain 'N'
1490                     -- so that cursor is not opened. All other exceptions raised
1491                     -- until caught by outer exception handler
1492                     Exception when no_data_found then
1493                         l_progress := 'PO_COMMUNICATION_PVT.generate_pdf_buyer: No duplicate filenames exist in the attachments for this PO';
1494                         IF (g_po_wf_debug = 'Y') THEN
1495                             PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,l_progress);
1496                         END IF;
1497                     End;
1498                 END IF;
1499 
1500                 IF l_duplicate_filenames = 'Y' THEN
1501                     l_progress := 'PO_COMMUNICATION_PVT.generate_pdf_buyer : Duplicate filenames found.';
1502                     IF (l_document_subtype='RELEASE') THEN
1503                         open l_get_release_attachments_csr(l_document_id);
1504                     ELSE
1505                         open l_get_po_attachments_csr(l_document_id);
1506                     END IF;
1507 
1508                     l_progress := 'PO_COMMUNICATION_PVT.generate_pdf_buyer : execute loop to get duplicate filenames with error condition';
1509                     IF (g_po_wf_debug = 'Y') THEN
1510                         PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,l_progress);
1511                     END IF;
1512 
1513                     l_error_flag := 0;
1514                     LOOP
1515                         IF (l_document_subtype='RELEASE') THEN
1516                             fetch l_get_release_attachments_csr into l_filename_new,l_length_new;
1517                             exit when (l_get_release_attachments_csr%notfound);
1518                         ELSE
1519                             fetch l_get_po_attachments_csr into l_filename_new,l_length_new;
1520                             exit when (l_get_po_attachments_csr%notfound);
1521                         END IF;
1522                         IF (l_filename_new = l_filename AND l_length_new <> l_length) THEN
1523                             l_error_flag := 1;
1524                             exit;
1525                         END IF;
1526                         l_filename := l_filename_new; l_length := l_length_new;
1527                     END LOOP;
1528                     IF (l_document_subtype='RELEASE') THEN
1529                         close l_get_release_attachments_csr;
1530                     ELSE
1531                         close l_get_po_attachments_csr;
1532                     END IF;
1533 
1534                     l_progress := 'PO_COMMUNICATION_PVT.generate_pdf_buyer : error flag = '||l_error_flag;
1535                     IF (g_po_wf_debug = 'Y') THEN
1536                         PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,l_progress);
1537                     END IF;
1538 
1539                     IF l_error_flag = 1 THEN
1540                         set_zip_error_code(itemtype,itemkey,'DUPLICATE_FILENAME');
1541                         l_max_attachment_size := 0; -- No need to generate zip file if it is an error
1542                     END IF;
1543                 END IF; --IF l_duplicate_filenames = 'Y'
1544             END IF; --IF l_attachments_exist = 'Y'
1545         END IF; --IF l_max_attachment_size > 0
1546     Exception when others then
1547         IF (g_po_wf_debug = 'Y') THEN
1548             PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,l_progress);
1549             PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,'PO_COMMUNICATION_PVT.generate_pdf_buyer: Caught Zip generation exception '||SQLERRM);
1550         END IF;
1551         set_zip_error_code(itemtype,itemkey,'UNEXPECTED');
1552         l_max_attachment_size := 0; -- No need to generate zip file if it is an error
1553     End;
1554     --<PO Attachment Support 11i.11 End>
1555 
1556 
1557   -- Generate the pdf in the Buyers language without T's and C's
1558 
1559 --<R12 MOAC START>
1560 po_moac_utils_pvt.set_request_context(po_moac_utils_pvt.get_current_org_id);
1561 --<R12 MOAC END>
1562   --Bug5080617 Pass the parameters P_PO_TEMPLATE_CODE and P_CONTRACT_TEMPLATE_CODE as null
1563  l_request_id := fnd_request.submit_request('PO',
1564          'POXPOPDF',
1565          null,
1566          null,
1567          false,
1568          'R',--P_report_type
1569          null  ,--P_agend_id
1570          null,--P_po_num_from
1571          null           ,--P_po_num_to
1572    null           ,--P_relaese_num_from
1573    null           ,--P_release_num_to
1574    null           ,--P_date_from
1575    null           ,--P_date_to
1576    null           ,--P_approved_flag
1577          'N',--P_test_flag
1578    null           ,--P_print_releases
1579    null           ,--P_sortby
1580    null           ,--P_user_id
1581    null           ,--P_fax_enable
1582    null           ,--P_fax_number
1583    null           ,--P_BLANKET_LINES
1584   'View'           ,--View_or_Communicate,
1585 		--Bug6841986/6528046
1586 		--l_withterms,--P_WITHTERMS
1587 		'N',--P_WITHTERMS
1588         'Y',--P_storeFlag
1589         'N',--P_PRINT_FLAG
1590         l_document_id,--P_DOCUMENT_ID
1591         l_revision_num,--P_REVISION_NUM
1592         l_authorization_status,--P_AUTHORIZATION_STATUS
1593         l_document_subtype,--P_DOCUMENT_TYPE
1594         l_max_attachment_size,--P_max_zip_size, <PO Attachment Support 11i.11>
1595         null, -- P_PO_TEMPLATE_CODE
1596         null, -- P_CONTRACT_TEMPLATE_CODE
1597         fnd_global.local_chr(0),
1598         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1599         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1600         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1601         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1602         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1603         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1604         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1605         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1606         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1607         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1608         NULL, NULL);
1609 
1610   l_progress := 'PO_COMMUNICATION_PVT.generate_pdf_buyer : Request id is - '|| l_request_id;
1611 
1612   IF (g_po_wf_debug = 'Y') THEN
1613    PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,l_progress);
1614   END IF;
1615 
1616      PO_WF_UTIL_PKG.SetItemAttrNumber ( itemtype   => itemtype,
1617                                   itemkey    => itemkey,
1618                                   aname      => 'REQUEST_ID',
1619                                   avalue     => l_request_id);
1620 
1621 EXCEPTION
1622 
1623   WHEN OTHERS THEN
1624 
1625   IF (g_po_wf_debug = 'Y') THEN
1626      PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,l_progress);
1627   END IF;
1628 
1629   wf_core.context('PO_COMMUNICATION_PVT','GENERATE_PDF_BUYER',l_progress);
1630   raise;
1631 
1632 END GENERATE_PDF_BUYER;
1633 
1634 PROCEDURE GENERATE_PDF_SUPP (itemtype IN VARCHAR2,
1635                                            itemkey  IN VARCHAR2,
1636                                            actid    IN NUMBER,
1637                                            funcmode IN VARCHAR2,
1638                                            resultout   OUT NOCOPY VARCHAR2)
1639     IS
1640 
1641    l_document_id number;
1642    l_document_subtype po_headers.type_lookup_code%TYPE;
1643    l_document_type po_headers.type_lookup_code%TYPE;
1644    l_revision_num  number;
1645    l_request_id number;
1646    l_territory  varchar2(30);
1647    l_set_lang   boolean;
1648    x_progress  varchar2(100);
1649    l_language_code  fnd_languages.language_code%type;
1650    l_supp_lang       varchar2(30);
1651    l_language        varchar2(25);
1652    l_authorization_status varchar2(25);
1653    --l_old_request_id  number; --Bug 7299381
1654    l_header_id number;
1655    --Bug6841986/6528046
1656    l_withterms  varchar2(1);
1657    --End Bug6841986/6528046
1658 begin
1659 x_progress := 'PO_COMMUNICATION_PVT.GENERATE_PDF_SUPP';
1660 
1661   IF (g_po_wf_debug = 'Y') THEN
1662    PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,x_progress);
1663   END IF;
1664 
1665   l_document_type := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
1666                                                itemkey  => itemkey,
1667                                                      aname    => 'DOCUMENT_TYPE');
1668 
1669   l_document_subtype := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
1670                                          itemkey  => itemkey,
1671                                         aname    => 'DOCUMENT_SUBTYPE');
1672 
1673   l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
1674                                          itemkey  => itemkey,
1675                                          aname    => 'DOCUMENT_ID');
1676 
1677   l_revision_num := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
1678                                          itemkey         => itemkey,
1679                                          aname           => 'REVISION_NUMBER');
1680 
1681   l_language_code := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
1682                                          itemkey  => itemkey,
1683                                          aname    =>'LANGUAGE_CODE');
1684 	--Bug6841986/6528046 Added
1685 	l_withterms := PO_WF_UTIL_PKG.GetItemAttrText (	itemtype => itemtype,
1686 													itemkey  => itemkey,
1687 													aname    => 'WITH_TERMS');
1688 
1689 	--Bug 7299381
1690 	/*l_old_request_id := PO_WF_UTIL_PKG.GetItemAttrNumber(itemtype   => itemtype,
1691 										 itemkey    => itemkey,
1692 										 aname=>'REQUEST_ID');
1693 	*/
1694 	--End Bug6841986/6528046
1695 
1696 
1697  IF l_document_type in ('PO','PA') and l_document_subtype in ('STANDARD','BLANKET','CONTRACT') THEN
1698 l_header_id := l_document_id;
1699 ELSE
1700 SELECT po_header_id into l_header_id FROM po_releases_all
1701 WHERE  po_release_id = l_document_id;
1702 END IF;
1703 
1704  SELECT pv.language into l_supp_lang
1705  FROM po_vendor_sites_all pv,po_headers_all ph
1706  WHERE
1707  ph.po_header_id = l_header_id and ph.vendor_site_id = pv.vendor_site_id;
1708 
1709 
1710 x_progress := 'PO_COMMUNICATION_PVT.GENERATE_PDF_SUPP :launching the Dispatch Purchase Order concurrent program ';
1711 
1712   IF (g_po_wf_debug = 'Y') THEN
1713    PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,x_progress);
1714   END IF;
1715 
1716 	--set the suppliers language before launching the concurrent request
1717 	-- Bug6841986/6528046 changed userenv('LANG')==> l_language_code
1718 	SELECT nls_language INTO l_language
1719 	FROM fnd_languages
1720 	WHERE language_code = l_language_code;
1721 	--End Bug6841986/6528046
1722 
1723 if  l_language <> l_supp_lang  then
1724 
1725    select nls_territory into l_territory  from fnd_languages where
1726    nls_language = l_supp_lang;
1727 
1728    l_set_lang := fnd_request.set_options('NO', 'NO', l_supp_lang,l_territory, NULL);
1729 
1730 --Bug#3481824 Need to pass document type as
1731 --RELEASE in case of a release to the concurrent program
1732 IF l_document_type ='RELEASE' THEN
1733    l_document_subtype :='RELEASE';
1734 END IF;
1735 
1736 --<R12 MOAC START>
1737 po_moac_utils_pvt.set_request_context(po_moac_utils_pvt.get_current_org_id);
1738 --<R12 MOAC END>
1739 --Bug5080617 Pass the parameters P_PO_TEMPLATE_CODE and P_CONTRACT_TEMPLATE_CODE as null
1740 l_request_id := fnd_request.submit_request('PO',
1741         'POXPOPDF',
1742         null,
1743         null,
1744         false,
1745         'R',--P_report_type
1746         null  ,--P_agend_id
1747         null,--P_po_num_from
1748   null           ,--P_po_num_to
1749   null           ,--P_relaese_num_from
1750   null           ,--P_release_num_to
1751   null           ,--P_date_from
1752   null           ,--P_date_to
1753   null           ,--P_approved_flag
1754         'N',--P_test_flag
1755   null           ,--P_print_releases
1756   null           ,--P_sortby
1757   null           ,--P_user_id
1758   null           ,--P_fax_enable
1759   null           ,--P_fax_number
1760   null           ,--P_BLANKET_LINES
1761   'View'           ,--View_or_Communicate,
1762          'N',--P_WITHTERMS
1763          'Y',--P_storeFlag
1764          'N',--P_PRINT_FLAG
1765          l_document_id,--P_DOCUMENT_ID
1766          l_revision_num,--P_REVISION_NUM
1767          l_authorization_status,--P_AUTHORIZATION_STATUS
1768          l_document_subtype,--P_DOCUMENT_TYPE
1769          0,--P_max_zip_size, <PO Attachment Support 11i.11>
1770          null, --P_PO_TEMPLATE_CODE
1771          null, --P_CONTRACT_TEMPLATE_CODE
1772          fnd_global.local_chr(0),
1773          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1774          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1775          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1776          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1777          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1778          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1779          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1780          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1781          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1782          NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1783          NULL, NULL);
1784 
1785 
1786   x_progress := 'PO_COMMUNICATION_PVT.GENERATE_PDF_SUPP : Request id is - '|| l_request_id;
1787 
1788   IF (g_po_wf_debug = 'Y') THEN
1789    PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,x_progress);
1790   END IF;
1791 
1792   -- Bug 7299381
1793   PO_WF_UTIL_PKG.SetItemAttrNumber ( itemtype   => itemtype,
1794 					itemkey    => itemkey,
1795 					aname      => 'REQUEST_ID',
1796                                         avalue     => l_request_id);
1797   -- End Bug 7299381
1798 
1799 end if;
1800 
1801 EXCEPTION
1802 
1803   WHEN OTHERS THEN
1804   x_progress :=  'PO_COMMUNICATION_PVT.GENERATE_PDF_SUPP: In Exception handler';
1805 
1806   IF (g_po_wf_debug = 'Y') THEN
1807        PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
1808   END IF;
1809   wf_core.context('PO_COMMUNICATION_PVT','GENERATE_PDF_SUPP',x_progress);
1810   raise;
1811 
1812 END GENERATE_PDF_SUPP;
1813 
1814 PROCEDURE GENERATE_PDF_EMAIL_PROCESS (itemtype IN VARCHAR2,
1815                                            itemkey  IN VARCHAR2,
1816                                            actid    IN NUMBER,
1817                                            funcmode IN VARCHAR2,
1818                                            resultout   OUT NOCOPY VARCHAR2)
1819     IS
1820 
1821    l_document_id number;
1822    l_document_subtype po_headers.type_lookup_code%TYPE;
1823    l_revision_num  number;
1824    l_request_id number;
1825    l_language_code   varchar2(25);
1826    x_progress  varchar2(200);
1827    l_withterms  varchar2(1);
1828    l_set_lang   boolean;
1829    l_territory  varchar2(30);
1830    l_authorization_status varchar2(25);
1831    l_language   varchar2(10);
1832 begin
1833 x_progress := 'PO_COMMUNICATION_PVT.GENERATE_PDF_EMAIL_PROCESS';
1834 
1835   IF (g_po_wf_debug = 'Y') THEN
1836  PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,x_progress);
1837   END IF;
1838 
1839  l_language_code := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
1840                                          itemkey  => itemkey,
1841                                          aname    =>'LANGUAGE_CODE');
1842 
1843 --set the suppliers language before launching the concurrent request
1844 
1845  select nls_territory into l_territory  from fnd_languages where
1846  language_code = l_language_code;
1847 
1848 
1849  l_document_subtype := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
1850                                          itemkey  => itemkey,
1851                                         aname    => 'DOCUMENT_SUBTYPE');
1852 
1853  l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
1854                                          itemkey  => itemkey,
1855                                          aname    => 'DOCUMENT_ID');
1856 
1857  l_withterms := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
1858                                          itemkey  => itemkey,
1859                                          aname    => 'WITH_TERMS');
1860 
1861   l_revision_num := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype  => itemtype,
1862                                      itemkey         => itemkey,
1863                                          aname           => 'REVISION_NUMBER');
1864 
1865   l_authorization_status := PO_WF_UTIL_PKG.GetItemAttrText (itemtype =>itemtype,
1866                                                       itemkey   => itemkey,
1867                                               aname  => 'AUTHORIZATION_STATUS');
1868 
1869   x_progress := 'PO_COMMUNICATION_PVT.GENERATE_PDF_EMAIL_PROCESS:launching the Dispatch Purchase Order concurrent program ';
1870 
1871   IF (g_po_wf_debug = 'Y') THEN
1872    PO_WF_DEBUG_PKG.INSERT_DEBUG(ITEMTYPE, ITEMKEY,x_progress);
1873   END IF;
1874 
1875  IF l_language_code <> userenv('LANG') THEN
1876 
1877 --set the suppliers language before launching the concurrent request
1878 
1879  select nls_language,nls_territory into l_language , l_territory  from fnd_languages where
1880  language_code = l_language_code;
1881 
1882 
1883     l_set_lang := fnd_request.set_options('NO', 'NO', l_language,l_territory, NULL);
1884 
1885 END IF;
1886 
1887 --<R12 MOAC START>
1888 po_moac_utils_pvt.set_request_context(po_moac_utils_pvt.get_current_org_id);
1889 --<R12 MOAC END>
1890 --Bug5080617 Pass the parameters P_PO_TEMPLATE_CODE and P_CONTRACT_TEMPLATE_CODE as null
1891 l_request_id := fnd_request.submit_request('PO',
1892         'POXPOPDF',
1893         null,
1894         null,
1895         false,
1896         'R',--P_report_type
1897         null  ,--P_agent_name
1898         null,--P_po_num_from
1899         null           ,--P_po_num_to
1900   null           ,--P_relaese_num_from
1901   null           ,--P_release_num_to
1902   null           ,--P_date_from
1903   null           ,--P_date_to
1904   null           ,--P_approved_flag
1905         'N',--P_test_flag
1906   null           ,--P_print_releases
1907   null           ,--P_sortby
1908   null           ,--P_user_id
1909   null           ,--P_fax_enable
1910   null           ,--P_fax_number
1911   null           ,--P_BLANKET_LINES
1912   'Communicate'           ,--View_or_Communicate,
1913         l_withterms,--P_WITHTERMS
1914        'Y',--P_storeFlag
1915        'N',--P_PRINT_FLAG
1916        l_document_id,--P_DOCUMENT_ID
1917        l_revision_num,--P_REVISION_NUM
1918        l_authorization_status,--P_AUTHORIZATION_STATUS
1919        l_document_subtype,--P_DOCUMENT_TYPE
1920        0,--P_max_zip_size, <PO Attachment Support 11i.11>
1921        null, -- P_PO_TEMPLATE_CODE
1922        null, -- P_CONTRACT_TEMPLATE_CODE
1923        fnd_global.local_chr(0),
1924        NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1925        NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1926        NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1927        NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1928        NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1929        NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1930        NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1931        NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1932        NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1933        NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1934        NULL, NULL);
1935 
1936 
1937   PO_WF_UTIL_PKG.SetItemAttrNumber ( itemtype   => itemtype,
1938                                       itemkey    => itemkey,
1939                                       aname      => 'REQUEST_ID',
1940                                       avalue     => l_request_id);
1941 
1942 
1943 EXCEPTION
1944 
1945   WHEN OTHERS THEN
1946   x_progress :=  'PO_COMMUNICATION_PVT.GENERATE_PDF_EMAIL_PROCESS: In Exception handler';
1947 
1948   IF (g_po_wf_debug = 'Y') THEN
1949          PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
1950   END IF;
1951   wf_core.context('PO_COMMUNICATION_PVT','GENERATE_PDF_EMAIL_PROCESS',x_progress);
1952   RAISE ;
1953 
1954 END GENERATE_PDF_EMAIL_PROCESS;
1955 
1956 
1957 
1958 PROCEDURE launch_communicate(p_mode in varchar2,
1959            p_document_id in number ,
1960                  p_revision_number in number ,
1961                              p_document_type in  varchar2,
1962                              p_authorization_status in varchar2,
1963                              p_language_code in varchar2,
1964                              p_fax_enable in varchar2,
1965            p_fax_num in varchar2,
1966            p_with_terms in varchar2,
1967                              p_print_flag in varchar2,
1968                              p_store_flag in varchar2,
1969                              p_request_id out NOCOPY number) is
1970 
1971 l_po_num            po_headers.segment1%type := NULL;
1972 l_po_header_id      po_headers.po_header_id%type := NULL;
1973 l_po_release_id      po_releases.po_release_id%type := NULL;
1974 l_communication      varchar2(1);
1975 l_api_name       CONSTANT   VARCHAR2(25):= 'launch_communicate';
1976 
1977 
1978 BEGIN
1979 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1980   FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name || '.begin','launch_communicate');
1981 END IF;
1982 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1983   FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name || 'Communication method ' ,p_mode);
1984 END IF;
1985 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
1986   FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name || 'Document Type  ' ,p_document_type);
1987 END IF;
1988 
1989 if p_mode = 'PRINT' then
1990 
1991 if p_document_type in ('STANDARD','BLANKET','CONTRACT') then
1992 
1993 --<R12 MOAC START>
1994 po_moac_utils_pvt.set_request_context(po_moac_utils_pvt.get_current_org_id);
1995 --<R12 MOAC END>
1996 --Bug5080617 Pass the parameters P_PO_TEMPLATE_CODE and P_CONTRACT_TEMPLATE_CODE as null
1997 p_request_id := fnd_request.submit_request('PO',
1998         'POXPOPDF',
1999         null,
2000         null,
2001         false,
2002         'R',--P_report_type
2003         null  ,--P_agent_name
2004         null,--P_po_num_from
2005         null           ,--P_po_num_to
2006         null           ,--P_relaese_num_from
2007   null           ,--P_release_num_to
2008   null           ,--P_date_from
2009   null           ,--P_date_to
2010   null           ,--P_approved_flag
2011         'N',--P_test_flag
2012   null           ,--P_print_releases
2013   null           ,--P_sortby
2014   null           ,--P_user_id
2015   null           ,--P_fax_enable
2016   null           ,--P_fax_number
2017   null           ,--P_BLANKET_LINES
2018   'Communicate'           ,--View_or_Communicate,
2019         p_with_terms,--P_WITHTERMS
2020         p_store_flag,--P_storeFlag
2021         p_print_flag,--P_PRINT_FLAG
2022         p_document_id,--P_DOCUMENT_ID
2023         p_revision_number,--P_REVISION_NUM
2024         p_authorization_status,--P_AUTHORIZATION_STATUS
2025         p_document_type,--P_DOCUMENT_TYPE
2026         0,--P_max_zip_size, <PO Attachment Support 11i.11>
2027         null, --P_PO_TEMPLATE_CODE
2028         null, --P_CONTRACT_TEMPLATE_CODE
2029         fnd_global.local_chr(0),
2030         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2031         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2032         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2033         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2034         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2035         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2036         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2037         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2038         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2039         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2040         NULL, NULL);
2041 
2042 
2043 
2044 elsif p_document_type = 'RELEASE'  then
2045 
2046 --<R12 MOAC START>
2047 po_moac_utils_pvt.set_request_context(po_moac_utils_pvt.get_current_org_id);
2048 --<R12 MOAC END>
2049 --Bug5080617 Pass the parameters P_PO_TEMPLATE_CODE and P_CONTRACT_TEMPLATE_CODE as null
2050 p_request_id := fnd_request.submit_request('PO',
2051         'POXPOPDF',
2052         null,
2053         null,
2054         false,
2055         'R',--P_report_type
2056         null  ,--P_agent_name
2057         null,--P_po_num_from
2058         null           ,--P_po_num_to
2059         null           ,--P_relaese_num_from
2060   null           ,--P_release_num_to
2061   null           ,--P_date_from
2062   null           ,--P_date_to
2063   null           ,--P_approved_flag
2064         'N',--P_test_flag
2065   null           ,--P_print_releases
2066   null           ,--P_sortby
2067   null           ,--P_user_id
2068   null           ,--P_fax_enable
2069   null           ,--P_fax_number
2070   null           ,--P_BLANKET_LINES
2071   'Communicate'           ,--View_or_Communicate,
2072         p_with_terms,--P_WITHTERMS
2073         p_store_flag,--P_storeFlag
2074         p_print_flag,--P_PRINT_FLAG
2075         p_document_id,--P_DOCUMENT_ID
2076         p_revision_number,--P_REVISION_NUM
2077         p_authorization_status,--P_AUTHORIZATION_STATUS
2078         p_document_type,--P_DOCUMENT_TYPE
2079         0,--P_max_zip_size, <PO Attachment Support 11i.11>
2080         null, --P_PO_TEMPLATE_CODE
2081         null, --P_CONTRACT_TEMPLATE_CODE
2082         fnd_global.local_chr(0),
2083         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2084         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2085         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2086         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2087         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2088         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2089         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2090         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2091         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2092         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2093         NULL, NULL);
2094 
2095  end if;
2096 
2097 end if;
2098 
2099 
2100 if   p_mode = 'FAX' then
2101 
2102 if p_document_type in ('STANDARD','BLANKET','CONTRACT') then
2103 
2104 --<R12 MOAC START>
2105 po_moac_utils_pvt.set_request_context(po_moac_utils_pvt.get_current_org_id);
2106 --<R12 MOAC END>
2107 --Bug5080617 Pass the parameters P_PO_TEMPLATE_CODE and P_CONTRACT_TEMPLATE_CODE as null
2108 p_request_id := fnd_request.submit_request('PO',
2109         'POXPOFAX'	,--Bug 6332444
2110         null,
2111         null,
2112         false,
2113         'R'		,--P_report_type
2114         null		,--P_agend_id
2115         null		,--P_po_num_from
2116         null           ,--P_po_num_to
2117         null           ,--P_relaese_num_from
2118 	null           ,--P_release_num_to
2119 	null           ,--P_date_from
2120 	null           ,--P_date_to
2121 	null           ,--P_approved_flag
2122         'N'		,--P_test_flag
2123 	null           ,--P_print_releases
2124 	null           ,--P_sortby
2125 	null           ,--P_user_id
2126 	p_fax_enable   ,--P_fax_enable
2127 	p_fax_num      ,--P_fax_number
2128 	null           ,--P_BLANKET_LINES
2129 	'Communicate'  ,--View_or_Communicate,
2130         p_with_terms,--P_WITHTERMS
2131         p_store_flag,--P_storeFlag
2132         p_print_flag,--P_PRINT_FLAG
2133         p_document_id,--P_DOCUMENT_ID
2134         p_revision_number,--P_REVISION_NUM
2135         p_authorization_status,--P_AUTHORIZATION_STATUS
2136         p_document_type,--P_DOCUMENT_TYPE
2137         0,--P_max_zip_size, <PO Attachment Support 11i.11>
2138         null, --P_PO_TEMPLATE_CODE
2139         null, --P_CONTRACT_TEMPLATE_CODE
2140         fnd_global.local_chr(0),
2141         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2142         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2143         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2144         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2145         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2146         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2147         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2148         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2149         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2150         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2151         NULL, NULL);
2152 
2153 
2154 elsif p_document_type = 'RELEASE'  then
2155 
2156 --<R12 MOAC START>
2157 po_moac_utils_pvt.set_request_context(po_moac_utils_pvt.get_current_org_id);
2158 --<R12 MOAC END>
2159 --Bug5080617 Pass the parameters P_PO_TEMPLATE_CODE and P_CONTRACT_TEMPLATE_CODE as null
2160 
2161 /*Bug5134811 For release fax the fax number and fax enable parameters are not passed due to which
2162  	 it was not possible to communicate the release through fax using Tools->Communicate.Passed
2163  	 the parameters as faxing is based on these two parameters*/
2164 
2165 p_request_id := fnd_request.submit_request('PO',
2166         'POXPOFAX'	,--Bug 6332444
2167         null,
2168         null,
2169         false,
2170         'R'		,--P_report_type
2171         null		,--P_agent_name
2172         null		,--P_po_num_from
2173         null           ,--P_po_num_to
2174         null           ,--P_relaese_num_from
2175 	null           ,--P_release_num_to
2176 	null           ,--P_date_from
2177 	null           ,--P_date_to
2178 	null           ,--P_approved_flag
2179         'N'		,--P_test_flag
2180 	null           ,--P_print_releases
2181 	null           ,--P_sortby
2182 	null           ,--P_user_id
2183 	p_fax_enable   ,--P_fax_enable Bug5134811
2184 	p_fax_num      ,--P_fax_number Bug5134811
2185 	null           ,--P_BLANKET_LINES
2186 	'Communicate'  ,--View_or_Communicate,
2187         p_with_terms,--P_WITHTERMS
2188         p_store_flag,--P_storeFlag
2189         p_print_flag,--P_PRINT_FLAG
2190         p_document_id,--P_DOCUMENT_ID
2191         p_revision_number,--P_REVISION_NUM
2192         p_authorization_status,--P_AUTHORIZATION_STATUS
2193         p_document_type,--P_DOCUMENT_TYPE
2194         0,--P_max_zip_size, <PO Attachment Support 11i.11>
2195         null, --P_PO_TEMPLATE_CODE
2196         null, --P_CONTRACT_TEMPLATE_CODE
2197         fnd_global.local_chr(0),
2198         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2199         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2200         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2201         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2202         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2203         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2204         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2205         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2206         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2207         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2208         NULL, NULL);
2209 
2210 
2211 
2212 end if;
2213 
2214 end if;
2215 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
2216   FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name || 'After launching the Dispatch Purchase order CP.' , 0);
2217 END IF;
2218 
2219 EXCEPTION
2220 when others then
2221 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_EXCEPTION) THEN
2222   FND_LOG.string(FND_LOG.LEVEL_EXCEPTION,g_log_head || l_api_name ||'.EXCEPTION',
2223  'launch_communicate: Inside exception :'|| '000' ||sqlcode);
2224 END IF;
2225 
2226 
2227 end launch_communicate;
2228 
2229 /* <Bug 3619689> Restructured the following procedure
2230  * Removed redundant code and SQLs
2231  * Used proper debug logging
2232  * Introduced l_progress info and exception handling
2233  * Modified the SQLs used for selecting from PO and OKC Repository
2234  * Now selecting release revision number from po_release_archives_all
2235  */
2236 procedure   Communicate(p_authorization_status in varchar2,
2237                         p_with_terms in varchar2,
2238                         p_language_code in varchar2,
2239                         p_mode     in varchar2,
2240                         p_document_id in number ,
2241                         p_revision_number in number,
2242                         p_document_type in varchar2,
2243                         p_fax_number in varchar2,
2244                         p_email_address in varchar2,
2245                         p_request_id out nocopy number)
2246 IS
2247 
2248 l_conterm_exists_flag        po_headers_all.CONTERMS_EXIST_FLAG%type;
2249 l_supp_lang                  fnd_languages.nls_language%type;
2250 l_territory                  fnd_languages.nls_territory%type;
2251 l_revision_num               number;
2252 l_set_lang                   boolean;
2253 l_doctype                    po_document_types_all.document_type_code%type;
2254 l_document_subtype           po_document_types_all.document_subtype%type;
2255 l_language_code              fnd_languages.language_code%type;
2256 l_api_name       CONSTANT    VARCHAR2(25):= 'Communicate';
2257 
2258 l_pdf_tc_buyer_exists        number(1); -- Whether PDF with Terms in buyers language already exists in Contracts Repository
2259 l_pdf_nt_buyer_exists        number(1); -- Whether PDF without Terms in buyers language already exists in PO Repository
2260 l_pdf_nt_sup_exists          number(1); -- Whether PDF without Terms in suppliers language already exists in PO Repository
2261 l_pdf_tc_sup_exists          number(1); -- Whether PDF with Terms in suppliers language already exists in Contracts Repository
2262 
2263 l_tc_buyer_gen_flag          varchar2(1); -- Whether PDF with Terms in buyers language needs to be generated
2264 l_nt_buyer_gen_flag          varchar2(1); -- Whether PDF without Terms in buyers language needs to be generated
2265 l_nt_sup_gen_flag            varchar2(1); -- Whether PDF without Terms in suppliers language needs to be generated
2266 l_tc_sup_gen_flag            varchar2(1); -- Whether PDF without Terms in suppliers language needs to be generated
2267 
2268 l_store_flag                 varchar2(1);  -- To store PDF or not
2269 
2270 l_org_id                     varchar2(10);
2271 
2272 l_request_id                 number := NULL;
2273 
2274 l_progress                   VARCHAR2(3);
2275 l_entity_name                fnd_attached_documents.entity_name%type;
2276 l_buyer_language_code        fnd_documents_tl.language%type;
2277 l_pdf_file_name              fnd_lobs.file_name%type;  --<FP 11i10+- R12 Contract ER TC Sup Lang>
2278 
2279 BEGIN
2280   l_progress := '000';
2281   IF g_debug_stmt THEN
2282      PO_DEBUG.debug_begin(p_log_head => g_log_head||l_api_name);
2283      PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2284                          p_token    => l_progress,
2285                          p_message  => 'Communication method '||p_mode);
2286      PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2287                          p_token    => l_progress,
2288                          p_message  => 'Document Type '||p_document_type);
2289      PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2290                          p_token    => l_progress,
2291                          p_message  => 'Authorization Status '||p_authorization_status);
2292      PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2293                          p_token    => l_progress,
2294                          p_message  => 'Document Id '||p_document_id);
2295      PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2296                          p_token    => l_progress,
2297                          p_message  => 'With Terms '||p_with_terms);
2298   END IF;
2299 
2300   l_org_id := po_moac_utils_pvt.get_current_org_id; --<R12 MOAC>
2301 
2302   l_doctype := p_document_type;
2303   if p_document_type in ('BLANKET','CONTRACT') then
2304     l_doctype := 'PA';
2305   end if;
2306 
2307   if p_document_type = 'STANDARD' then
2308     l_doctype :='PO';
2309   end if;
2310 
2311   l_tc_buyer_gen_flag := 'N';
2312   l_nt_buyer_gen_flag := 'N';
2313   l_tc_sup_gen_flag   := 'N';
2314   l_nt_sup_gen_flag   := 'N';
2315 
2316   l_store_flag        := 'N';
2317 
2318   l_progress := '010';
2319   begin
2320     if p_document_type in ('STANDARD','BLANKET','CONTRACT') then
2321       l_entity_name := 'PO_HEAD';
2322       select pvs.language into l_supp_lang from po_vendor_sites pvs , po_headers_all ph
2323         where po_header_id = p_document_id and ph.vendor_site_id = pvs.vendor_site_id ;
2324     else
2325       l_entity_name := 'PO_REL';
2326       select pvs.language into l_supp_lang from po_vendor_sites pvs , po_headers_all ph,po_releases_all pr
2327         where  ph.po_header_id = pr.po_header_id and pr.po_release_id = p_document_id and
2328                  ph.vendor_site_id = pvs.vendor_site_id ;
2329     end if;
2330     IF g_debug_stmt THEN
2331        PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2332                            p_token    => l_progress,
2333                            p_message  => 'Supplier Language: '||l_supp_lang);
2334     END IF;
2335   exception
2336     when others then l_supp_lang := NULL;
2337     IF g_debug_stmt THEN
2338        PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2339                            p_token    => l_progress,
2340                            p_message  => 'Supplier Language not found');
2341     END IF;
2342   end;
2343 
2344   l_progress := '020';
2345   if l_supp_lang is not null then
2346     select language_code,nls_territory into l_language_code,l_territory  from fnd_languages fl where
2347       fl.nls_language =  l_supp_lang;
2348   end if;
2349 
2350   l_buyer_language_code := userenv('LANG');
2351   begin
2352     select NVL(conterms_exist_flag, 'N') into l_conterm_exists_flag from po_headers_all
2353     where
2354       po_header_id = p_document_id and revision_num = p_revision_number;
2355 
2356   exception
2357     when others then l_conterm_exists_flag := 'N';
2358   end;
2359 
2360   if (p_authorization_status = 'APPROVED' or p_authorization_status = 'PRE-APPROVED') then
2361 
2362     l_revision_num := p_revision_number;
2363 
2364     if l_conterm_exists_flag = 'Y' then
2365       l_progress := '030';
2366       IF g_debug_stmt THEN
2367          PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2368                              p_token    => l_progress,
2369                              p_message  => 'Checking for latest PDF with terms in Contracts Repository');
2370       END IF;
2371 
2372       --<FP:11i10+-R12 Contract ER TC Sup Lang>
2373       -- Brought the call out of the select
2374       l_pdf_file_name := po_communication_pvt.getPDFFileName(l_doctype,'_TERMS_',l_org_id,p_document_id,
2375                                                            l_revision_num,l_buyer_language_code);
2376       --Bug #4865352 - Replaced fnd_documents_tl with fnd_documents_vl
2377       select count(1) into l_pdf_tc_buyer_exists from fnd_lobs fl,fnd_attached_documents fad, fnd_documents_vl fdl
2378       where
2379         fad.pk2_value = TO_CHAR(p_document_id) and
2380         fad.pk3_value = TO_CHAR(l_revision_num) and
2381         fad.entity_name = 'OKC_CONTRACT_DOCS' and
2382         fdl.document_id = fad.document_id and
2383         fdl.media_id = fl.file_id and
2384         fl.file_name = l_pdf_file_name;
2385 
2386       --<FP 11i10+ - R12 Contract ER TC Sup Lang Start >
2387         -- Check if the document with terms exist in suppliers language in the repository
2388         -- if the supplier language is provided
2389         if l_supp_lang is null then
2390 
2391              l_pdf_tc_sup_exists := 1;
2392 
2393         else
2394              l_progress := '031';
2395              IF g_debug_stmt THEN
2396                   PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2397                            p_token    => l_progress,
2398                           p_message  => 'Checking for latest PDF without terms in suppliers language in PO Repository');
2399              END IF;
2400 
2401              l_pdf_file_name := po_communication_pvt.getPDFFileName(l_doctype,'_TERMS_',l_org_id,p_document_id,
2402                                                            l_revision_num,l_buyer_language_code); --bug#3463617
2403              --Bug #4865352 - Added a join with fnd_documents
2404              select count(1) into l_pdf_tc_sup_exists from fnd_lobs fl,fnd_attached_documents fad, fnd_documents fd, fnd_documents_tl fdl
2405              where
2406              fad.pk1_value = TO_CHAR(p_document_id) and
2407              fad.pk2_value = TO_CHAR(l_revision_num) and
2408              fad.entity_name = l_entity_name and
2409              fdl.document_id = fad.document_id and
2410              fd.media_id = fl.file_id and
2411              fd.document_id  = fdl.document_id and
2412              fdl.language = l_language_code and
2413              fl.file_name = l_pdf_file_name;
2414 
2415          end if;
2416 
2417          --<FP 11i10+ - R12  Contract ER TC Sup Lang End>
2418     else
2419 
2420       l_pdf_tc_buyer_exists := 0;
2421       l_pdf_tc_sup_exists := 0;   --<FP 11i10+ - R12  Contract ER TC Sup Lang>
2422 
2423     end if;
2424 
2425     l_progress := '040';
2426     IF g_debug_stmt THEN
2427        PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2428                            p_token    => l_progress,
2429                            p_message  => 'Checking for latest PDF without terms in buyers language in PO Repository');
2430     END IF;
2431 
2432     --<FP 11i10+ - R12 Contract ER TC Sup Lang>
2433     -- Brought the call out of the select
2434     l_pdf_file_name := po_communication_pvt.getPDFFileName(l_doctype,'_TERMS_',l_org_id,p_document_id,
2435                                                            l_revision_num,l_buyer_language_code);
2436     --Bug #4865352
2437     select count(1) into l_pdf_nt_buyer_exists from fnd_lobs fl,fnd_attached_documents fad, fnd_documents_vl fdl
2438     where
2439       fad.pk1_value = TO_CHAR(p_document_id) and
2440       fad.pk2_value = TO_CHAR(l_revision_num) and
2441       fad.entity_name = l_entity_name and
2442       fdl.document_id = fad.document_id and
2443       fdl.media_id = fl.file_id and
2444       fl.file_name =  l_pdf_file_name;
2445 
2446 
2447     if l_supp_lang is null then
2448 
2449       l_pdf_nt_sup_exists := 1;
2450 
2451     else
2452       l_progress := '050';
2453       IF g_debug_stmt THEN
2454          PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2455                              p_token    => l_progress,
2456                              p_message  => 'Checking for latest PDF without terms in suppliers language in PO Repository');
2457       END IF;
2458 
2459       --<FP 11i10+ - R12 Contract ER TC Sup Lang>
2460       -- Brought the call out of the select
2461       l_pdf_file_name := po_communication_pvt.getPDFFileName(l_doctype,'_TERMS_',l_org_id,p_document_id,
2462                                                            l_revision_num,l_language_code);
2463       --Bug #4865352 - Added a join with fnd_documents
2464       select count(1) into l_pdf_nt_sup_exists from fnd_lobs fl,fnd_attached_documents fad, fnd_documents fd, fnd_documents_tl fdl
2465       where
2466         fad.pk1_value = TO_CHAR(p_document_id) and
2467         fad.pk2_value = TO_CHAR(l_revision_num) and
2468         fad.entity_name = l_entity_name and
2469         fdl.document_id = fad.document_id and
2470         fd.media_id = fl.file_id and
2471         fd.document_id = fdl.document_id and
2472         fdl.language = l_language_code and
2473         fl.file_name = l_pdf_file_name;
2474 
2475     end if;
2476 
2477   else -- Authorization status is not in (Approved or Pre-Approved)
2478 
2479     l_progress := '060';
2480     Begin
2481       IF p_document_type in ('STANDARD','BLANKET','CONTRACT') THEN
2482           select max(revision_num)
2483           into l_revision_num
2484           from po_headers_archive_all
2485           where po_header_id = p_document_id
2486           and authorization_status = 'APPROVED';
2487       ELSE
2488           select max(revision_num)
2489           into l_revision_num
2490           from po_releases_archive_all
2491           where po_release_id = p_document_id
2492           and authorization_status = 'APPROVED';
2493       END IF;
2494     Exception
2495     When others then
2496         l_progress := '070';
2497         IF g_debug_unexp THEN
2498            PO_DEBUG.debug_exc(p_log_head => g_log_head||l_api_name,
2499                               p_progress => l_progress);
2500         END IF;
2501         raise;
2502     End;
2503     -- select max(revision_num) would not raise a no_data_found
2504     -- Instead it would return null, so raise exception explicitly
2505     IF l_revision_num IS NULL THEN
2506         l_progress := '080';
2507         IF g_debug_unexp THEN
2508            PO_DEBUG.debug_exc(p_log_head => g_log_head||l_api_name,
2509                               p_progress => l_progress);
2510         END IF;
2511         raise no_data_found;
2512     END IF;
2513 
2514     -- No cache documents are to be generated if status is any
2515     -- other than 'Approved' or 'Pre-Approved'
2516     l_pdf_tc_buyer_exists := 1;
2517     l_pdf_nt_buyer_exists := 1;
2518     l_pdf_tc_sup_exists := 1;   --<FP 11i10+ - R12 Contract ER TC Sup Lang>
2519     l_pdf_nt_sup_exists := 1;
2520 
2521   end if; -- if (p_authorization_status = 'APPROVED' or p_authorization_status = 'PRE-APPROVED')
2522 
2523   l_progress := '090';
2524   IF g_debug_stmt THEN
2525      PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2526                          p_token    => l_progress,
2527                          p_message  => 'Decide on which PDFs to generate and store');
2528   END IF;
2529 
2530   if p_with_terms = 'Y' then
2531 
2532     --<FP 11i10+ - R12 Contract ER TC Sup Lang Start>
2533     -- Modified the following logic such that -
2534     -- If the doc with terms does not exist in suppliers language
2535     -- generate and store it depending on the language passed in
2536 
2537     if p_language_code = l_buyer_language_code then
2538 
2539      if l_pdf_tc_buyer_exists = 0 then
2540        l_store_flag := 'Y';
2541      end if;
2542 
2543      if l_pdf_nt_buyer_exists = 0 then
2544       l_nt_buyer_gen_flag := 'Y';
2545      end if;
2546 
2547     else  -- if p_language_code = l_buyer_language_code
2548 
2549       l_progress := '095';
2550       -- Bug 4116063: Set the language if different from buyers lang
2551       select nls_language,nls_territory into l_supp_lang,l_territory  from fnd_languages fl where
2552         fl.language_code = p_language_code ;
2553       l_set_lang := fnd_request.set_options('NO', 'NO', l_supp_lang,l_territory, NULL);
2554 
2555      if l_pdf_tc_sup_exists = 0 then
2556        l_store_flag := 'Y';
2557      end if;
2558 
2559      if l_pdf_nt_sup_exists = 0 then
2560        l_nt_sup_gen_flag := 'Y';
2561      end if;
2562 
2563     end if;
2564     --<FP 11i10+ - R12 Contract ER TC Sup Lang End>
2565 
2566   else -- if p_with_terms = 'N'
2567 
2568      --<FP 11i10+ - R12 Contract ER TC Sup Lang >
2569     -- If the doc with terms does not exist in suppliers language
2570     -- generate it.
2571     if l_conterm_exists_flag = 'Y' and l_pdf_tc_buyer_exists = 0 then
2572       l_tc_buyer_gen_flag := 'Y';
2573     elsif  l_conterm_exists_flag = 'Y' and l_pdf_tc_sup_exists = 0 then
2574       l_tc_sup_gen_flag := 'Y';
2575     end if;
2576 
2577     if p_language_code = l_buyer_language_code then
2578 
2579       if l_pdf_nt_buyer_exists = 0 then
2580         l_store_flag := 'Y';
2581       end if;
2582 
2583       if l_pdf_nt_sup_exists = 0 then
2584         l_nt_sup_gen_flag := 'Y';
2585       end if;
2586 
2587     else  -- if p_language_code = l_buyer_language_code
2588 
2589       l_progress := '100';
2590       select nls_language,nls_territory into l_supp_lang,l_territory  from fnd_languages fl where
2591         fl.language_code = p_language_code ;
2592       l_set_lang := fnd_request.set_options('NO', 'NO', l_supp_lang,l_territory, NULL);
2593 
2594       if l_pdf_nt_sup_exists = 0 then
2595         l_store_flag := 'Y';
2596       end if;
2597 
2598       if l_pdf_nt_buyer_exists = 0 then
2599         l_nt_buyer_gen_flag := 'Y';
2600       end if;
2601 
2602     end if; -- if p_language_code = l_buyer_language_code
2603 
2604   end if; -- if p_with_terms = 'Y'
2605 
2606   l_progress := '110';
2607   IF g_debug_stmt THEN
2608      PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2609                          p_token    => l_progress,
2610                          p_message  => 'Lanuch Communicate Requests');
2611   END IF;
2612 
2613   if p_mode = 'PRINT' then
2614     l_progress := '120';
2615     launch_communicate( p_mode,
2616                         p_document_id,
2617                         l_revision_num,
2618                         p_document_type,
2619                         p_authorization_status,
2620                         p_language_code,
2621                         null,
2622                         null,
2623                         p_with_terms, -- with terms
2624                         'Y', -- print flag
2625                         l_store_flag,
2626                         p_request_id);
2627 
2628   elsif p_mode = 'FAX' then
2629     l_progress := '130';
2630     launch_communicate( p_mode,
2631                         p_document_id,
2632                         l_revision_num,
2633                         p_document_type,
2634             p_authorization_status,
2635                         p_language_code,
2636                         'Y',  -- fax enable
2637                         p_fax_number,
2638                         p_with_terms, -- with terms
2639                         'Y', -- print flag
2640                         l_store_flag,
2641                         p_request_id);
2642 
2643   elsif p_mode = 'EMAIL' then
2644     if p_document_type in ('STANDARD','BLANKET','CONTRACT') then
2645       l_progress := '140';
2646       Start_Email_WF_Process(       p_document_id,
2647                                     l_revision_num,
2648                                     l_doctype,
2649                                     p_document_type,
2650                                     p_email_address ,
2651                                     p_language_code,
2652                                     l_store_flag,
2653                                     p_with_terms) ; -- with terms
2654     elsif  p_document_type = 'RELEASE'  then
2655        l_progress := '150';
2656        Start_Email_WF_Process(      p_document_id,
2657                                     l_revision_num,
2658                                     p_document_type,
2659                                     'BLANKET',
2660                                     p_email_address,
2661                                     p_language_code,
2662                                     l_store_flag,
2663                                     p_with_terms);  -- with terms
2664     end if;
2665 
2666   end if; -- if p_mode = 'PRINT'
2667   commit;
2668 
2669   -- Now make cache documents
2670   l_progress := '160';
2671   IF g_debug_stmt THEN
2672      PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2673                          p_token    => l_progress,
2674                          p_message  => 'Generate Cache PDFs and store them in the repository');
2675   END IF;
2676 
2677   if l_tc_buyer_gen_flag = 'Y' then
2678     select nls_language,nls_territory into l_supp_lang,l_territory  from fnd_languages fl where
2679       fl.language_code = l_buyer_language_code;
2680     l_set_lang := fnd_request.set_options('NO', 'NO', l_supp_lang,l_territory, NULL);
2681 
2682     l_progress := '170';
2683     launch_communicate( 'PRINT',
2684                         p_document_id,
2685                         l_revision_num,
2686                         p_document_type,
2687                         p_authorization_status,
2688                         p_language_code,
2689                         null,
2690                         null,
2691                         'Y',  -- with terms
2692                         'N', -- print flag
2693                         'Y',  -- store flag
2694                         l_request_id);
2695     commit;
2696   end if;
2697 
2698   l_progress := '180';
2699   if l_nt_buyer_gen_flag = 'Y' then
2700     select nls_language,nls_territory into l_supp_lang,l_territory  from fnd_languages fl where
2701       fl.language_code = l_buyer_language_code;
2702     l_set_lang := fnd_request.set_options('NO', 'NO', l_supp_lang,l_territory, NULL);
2703 
2704     l_progress := '190';
2705     launch_communicate( 'PRINT',
2706                         p_document_id,
2707                         l_revision_num,
2708                         p_document_type,
2709                         p_authorization_status,
2710                         p_language_code,
2711                         null,
2712                         null,
2713                         'N',  -- with terms
2714                         'N', -- print flag
2715                         'Y',  -- store flag
2716                         l_request_id);
2717     commit;
2718   end if;
2719 
2720   l_progress := '200';
2721     if p_document_type in ('STANDARD','BLANKET','CONTRACT') then
2722       select pvs.language into l_supp_lang from po_vendor_sites pvs , po_headers_all ph
2723         where po_header_id = p_document_id and ph.vendor_site_id = pvs.vendor_site_id ;
2724     else
2725       select pvs.language into l_supp_lang from po_vendor_sites pvs , po_headers_all ph,po_releases_all pr
2726         where  ph.po_header_id = pr.po_header_id and pr.po_release_id = p_document_id and
2727                  ph.vendor_site_id = pvs.vendor_site_id ;
2728     end if;
2729 
2730   if l_nt_sup_gen_flag = 'Y' then
2731 
2732     l_progress := '210';
2733     if l_supp_lang is not null then
2734       select language_code,nls_territory into l_language_code,l_territory  from fnd_languages fl where
2735         fl.nls_language =  l_supp_lang;
2736       l_set_lang := fnd_request.set_options('NO', 'NO', l_supp_lang,l_territory, NULL);
2737 
2738       l_progress := '220';
2739       launch_communicate( 'PRINT',
2740                         p_document_id,
2741                         l_revision_num,
2742                         p_document_type,
2743                         p_authorization_status,
2744                         p_language_code,
2745                         null,
2746                         null,
2747                         'N',  -- with terms
2748                         'N', -- print flag
2749                         'Y',  -- store flag
2750                         l_request_id);
2751       commit;
2752     end if;
2753   end if;
2754 
2755    --<FP 11i10+ - R12 Contract ER TC Sup Lang Start>
2756    if l_tc_sup_gen_flag = 'Y' then
2757 
2758     l_progress := '220';
2759     if l_supp_lang is not null then
2760       select language_code,nls_territory into l_language_code,l_territory  from fnd_languages fl where
2761         fl.nls_language =  l_supp_lang;
2762       l_set_lang := fnd_request.set_options('NO', 'NO', l_supp_lang,l_territory, NULL);
2763 
2764       l_progress := '220';
2765       launch_communicate( 'PRINT',
2766                         p_document_id,
2767                         l_revision_num,
2768                         p_document_type,
2769                         p_authorization_status,
2770                         p_language_code,
2771                         null,
2772                         null,
2773                         'Y',  -- with terms
2774                         'N', -- print flag
2775                         'Y',  -- store flag
2776                         l_request_id);
2777       commit;
2778     end if;
2779   end if;
2780   --<FP 11i10+ - R12 Contract ER TC Sup Lang End>
2781 
2782   commit;
2783 
2784   l_progress := '230';
2785   IF g_debug_stmt THEN
2786      PO_DEBUG.debug_end(p_log_head => g_log_head||l_api_name);
2787   END IF;
2788 
2789 exception
2790 when others then
2791   IF g_debug_unexp THEN
2792      PO_DEBUG.debug_exc(p_log_head => g_log_head||l_api_name,
2793                         p_progress => l_progress);
2794   END IF;
2795   raise;
2796 end Communicate;
2797 
2798 
2799 function  po_communication_profile  RETURN VARCHAR2 IS
2800 l_communication varchar2(1);
2801 l_format   po_system_parameters_all.po_output_format%type;
2802 BEGIN
2803 
2804 select po_output_format into l_format from po_system_parameters;
2805 
2806   IF (l_format ='PDF' ) THEN
2807     RETURN FND_API.G_TRUE;
2808   ELSE
2809     RETURN FND_API.G_FALSE;
2810   END IF;
2811 
2812 END po_communication_profile;
2813 
2814 /* Bug # 3222207: Added the following function to return whether XDO is installed or not*/
2815 function  IS_PON_PRINTING_ENABLED RETURN VARCHAR2 IS
2816     l_communication varchar2(1);
2817   BEGIN
2818     IF (po_core_s.get_product_install_status('XDO') = 'I' ) THEN
2819       RETURN FND_API.G_TRUE;
2820     ELSE
2821       RETURN FND_API.G_FALSE;
2822     END IF;
2823 END IS_PON_PRINTING_ENABLED;
2824 
2825 function USER_HAS_ACCESS_TC RETURN VARCHAR2 IS
2826   BEGIN
2827     IF (fnd_function.test('PO_CONTRACT_TERMS')) THEN
2828       RETURN 'Y';
2829     ELSE
2830       RETURN 'N';
2831     END IF;
2832 END USER_HAS_ACCESS_TC;
2833 
2834 --<PO Attachment Support 11i.11>
2835 -- Generalized the existing procedure Store_PDF for adding any type of Blob
2836 -- Introduced exception handling
2837 procedure Store_Blob(p_document_id number,
2838                     p_revision_number number ,
2839                     p_document_type varchar2,
2840                     p_file_name varchar2,
2841                     p_blob_type IN varchar2, --<PO Attachment Support 11i.11>
2842                     x_media_id out nocopy number)
2843 IS
2844 
2845         Row_id_tmp varchar2(100);
2846         Document_id_tmp number;
2847         Media_id_tmp number;
2848         l_blob_data blob;
2849         l_entity_name varchar2(30);
2850         Seq_num    number;
2851         l_category_id number;
2852         l_count      number;
2853 
2854         --<PO Attachment Support 11i.11>
2855         l_file_name              fnd_lobs.file_name%type;
2856         l_file_content_type      fnd_lobs.file_content_type%type;
2857         l_org_id                 po_headers_all.org_id%type;
2858         l_api_name   CONSTANT    VARCHAR2(25):= 'Store_Blob';
2859         l_progress               VARCHAR2(3);
2860 
2861 Begin
2862     l_progress := '000';
2863     IF g_debug_stmt THEN
2864         PO_DEBUG.debug_begin(p_log_head => g_log_head||l_api_name);
2865         PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2866                             p_token    => l_progress,
2867                             p_message  => p_document_type||' '||p_document_id||' '||p_revision_number||' '||p_blob_type);
2868     END IF;
2869 
2870     --<PO Attachment Support 11i.11>
2871     IF p_blob_type = 'PDF' THEN
2872         l_file_content_type := 'application/pdf';
2873         l_file_name := p_file_name;
2874     ELSIF p_blob_type = 'ZIP' THEN
2875         l_org_id := po_moac_utils_pvt.get_current_org_id; --<R12 MOAC>
2876         IF g_debug_stmt THEN
2877             PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2878                                 p_token    => l_progress,
2879                                 p_message  => 'Calculated org_id = '||l_org_id);
2880         END IF;
2881         l_file_content_type := 'application/x-zip-compressed';
2882         --Bug# 5240634 Pass the org_id to getZIPFileName
2883         l_file_name := getZIPFileName(l_org_id);
2884     END IF;
2885 
2886     l_progress := '010';
2887 
2888 l_blob_data := empty_blob();
2889 l_count :=0;
2890 
2891     l_progress := '020';
2892 --Assign the Entity name depending on the document type
2893 
2894 
2895 if p_document_type in ('PO','PA') then
2896 l_entity_name:= 'PO_HEAD';
2897 else
2898 l_entity_name:='PO_REL';
2899 end if;
2900 
2901     l_progress := '030';
2902     --<PO Attachment Support 11i.11>
2903     -- For PDF preventing duplicate records is taken care of by explicitly deleting
2904     -- them before generating it everytime. For ZIP, it will be done by returning
2905     -- the same file id as the existing one so that an explicit delete would not be
2906     -- necessary
2907     --Bug #4865352 - Replaced fnd_documents_tl with fnd_documents_vl
2908     IF p_blob_type = 'ZIP' THEN
2909         Begin
2910             SELECT fdl.media_id
2911             INTO x_media_id
2912             FROM fnd_attached_documents fad,
2913                  fnd_documents_vl  fdl
2914             WHERE fad.pk1_value=to_char(p_document_id)
2915             and fad.pk2_value=to_char(p_revision_number)
2916             and fad.entity_name = l_entity_name
2917             and fdl.document_id = fad.document_id;
2918 
2919         Exception when others Then
2920             -- do nothing. If no_data_found then x_media_id will be null and a new lob
2921             -- locator will be inserted into fnd_lobs. If too_many_rows (though should
2922             -- not occur because the locator gets overwritten everytime) then the first blob found will be overwritten
2923             null;
2924         End;
2925     END IF;
2926     l_progress := '040';
2927     IF g_debug_stmt THEN
2928         PO_DEBUG.debug_stmt(p_log_head => g_log_head||l_api_name,
2929                             p_token    => l_progress,
2930                             p_message  => 'x_media_id = '||x_media_id);
2931     END IF;
2932 
2933 IF x_media_id IS NULL THEN
2934 
2935     l_progress := '050';
2936 --Get the Category Id of 'PO Documents' Category
2937 SELECT category_id into l_category_id from fnd_document_categories
2938 where  name   = 'CUSTOM2446' ;
2939 
2940         l_progress := '006';
2941         FND_DOCUMENTS_PKG.Insert_Row(
2942         row_id_tmp,
2943         document_id_tmp,
2944         SYSDATE,
2945         1,              --NVL(X_created_by,0),
2946         SYSDATE,
2947         1,             --NVL(X_created_by,0),
2948         1,              --X_last_update_login,
2949         6,
2950         l_category_id, --Get the value for the category id 'PO Documents'
2951         1,--null,--security_type,
2952         null,--security_id,
2953         'Y',--null,--publish_flag,
2954         null,--image_type,
2955         null,--storage_type,
2956         'O',--usage_type,
2957         sysdate,--start_date_active,
2958         null,--end_date_active,
2959         null,--X_request_id, --null
2960         null,--X_program_application_id, --null
2961         null,--X_program_id,--null
2962         SYSDATE,
2963         null,--language,
2964         null,--description,
2965         l_file_name,
2966         x_media_id);
2967 
2968        l_progress := '060';
2969 
2970        INSERT INTO fnd_lobs (
2971           file_id,
2972           File_name,
2973           file_content_type,
2974           upload_date,
2975           expiration_date,
2976           program_name,
2977           program_tag,
2978           file_data,
2979           language,
2980           oracle_charset,
2981           file_format)
2982           VALUES
2983            (x_media_id,
2984           l_file_name, --<PO Attachment Support 11i.11> Changed p_file_name to l_file_name
2985           l_file_content_type, --<PO Attachment Support 11i.11> Changed hardcoded value to l_file_content_type
2986           sysdate,
2987           null,
2988           null,
2989           null,
2990           l_blob_data,
2991           null,
2992           null,
2993                 'binary');
2994 
2995         l_progress := '070';
2996 
2997         INSERT INTO fnd_attached_documents (attached_document_id,
2998         document_id,
2999         creation_date,
3000          created_by,
3001          last_update_date,
3002         last_updated_by,
3003           last_update_login,
3004         seq_num,
3005          entity_name,
3006         pk1_value,
3007          pk2_value,
3008         pk3_value,
3009         pk4_value,
3010          pk5_value,
3011         automatically_added_flag,
3012         program_application_id,
3013          program_id,
3014          program_update_date,
3015         request_id,
3016         attribute_category,
3017          attribute1,
3018         attribute2,
3019         attribute3,
3020         attribute4,
3021         attribute5,
3022         attribute6,
3023         attribute7,
3024         attribute8,
3025          attribute9,
3026          attribute10,
3027         attribute11,
3028         attribute12,
3029          attribute13,
3030         attribute14,
3031          attribute15,
3032          column1)
3033         VALUES
3034          (fnd_attached_documents_s.nextval,
3035         document_id_tmp,
3036         sysdate,
3037         1,--NVL(X_created_by,0),
3038         sysdate,
3039         1,--NVL(X_created_by,0),
3040         null,-- X_last_update_login,
3041         10,
3042          l_entity_name,
3043          to_char(p_document_id),
3044          to_char(p_revision_number),
3045          null,
3046          null,
3047          null,
3048          'N',
3049         null,
3050         null,
3051         sysdate,
3052         null,
3053         null,
3054         null,
3055         null,
3056   null,
3057   null,
3058   null,
3059   null,
3060   null,
3061   null,
3062   null,
3063   null,
3064   null,
3065   null,
3066   null,
3067   null,
3068   null,
3069   null);
3070     l_progress := '080';
3071 END IF;
3072 
3073     IF g_debug_stmt THEN
3074         PO_DEBUG.debug_end(p_log_head => g_log_head||l_api_name);
3075     END IF;
3076 
3077 Exception
3078    WHEN OTHERS THEN
3079        IF g_debug_unexp THEN
3080           PO_DEBUG.debug_exc(p_log_head => g_log_head||l_api_name,
3081                              p_progress => l_progress);
3082        END IF;
3083        raise;
3084 end Store_Blob;
3085 
3086 procedure pdf_attach_app  (document_id    in     varchar2,
3087                            content_type   in     varchar2,
3088                            document       in out NOCOPY blob,
3089                            document_type  in out NOCOPY varchar2) IS
3090 l_filename  fnd_lobs.file_name%type;
3091 l_document_id   number;
3092 l_document_type po_headers.type_lookup_code%TYPE;
3093 l_org_id number;
3094 l_revision_number number;
3095 l_language fnd_languages.language_code%type;
3096 l_entity_name  varchar2(30);
3097 l_itemtype po_document_types.wf_approval_itemtype%type;
3098 l_itemkey varchar2(60);
3099 l_document blob;
3100 l_withTerms varchar2(1);
3101 l_document_length number;
3102 l_message FND_NEW_MESSAGES.message_text%TYPE; --Bug 3274081
3103 x_progress              varchar2(300);
3104 
3105 BEGIN
3106 
3107 l_itemtype := substr(document_id, 1, instr(document_id, ':') - 1);
3108 
3109 l_itemkey := substr(document_id, instr(document_id, ':') + 1, length(document_id) - 2);
3110 
3111 x_progress := 'PO_COMMUNICATION_PVT.pdf_attach_app ';
3112 
3113   IF (g_po_wf_debug = 'Y') THEN
3114    PO_WF_DEBUG_PKG.INSERT_DEBUG(l_itemtype, l_itemkey,x_progress);
3115   END IF;
3116 
3117 l_document_id :=wf_engine.GetItemAttrNumber (itemtype => l_itemtype,
3118                                          itemkey  => l_itemkey,
3119                                          aname    => 'DOCUMENT_ID');
3120 
3121 l_org_id := wf_engine.GetItemAttrNumber (itemtype => l_itemtype,
3122                                          itemkey  => l_itemkey,
3123                                          aname    => 'ORG_ID');
3124 
3125 l_document_type := wf_engine.GetItemAttrText (itemtype => l_itemtype,
3126                                          itemkey  => l_itemkey,
3127                                          aname    => 'DOCUMENT_TYPE');
3128 
3129 l_language := wf_engine.GetItemAttrText (itemtype => l_itemtype,
3130                                          itemkey  => l_itemkey,
3131                                          aname    => 'LANGUAGE_CODE');
3132 
3133 l_revision_number := wf_engine.GetItemAttrNumber (itemtype => l_itemtype,
3134                                          itemkey  => l_itemkey,
3135                                          aname    => 'REVISION_NUMBER');
3136 
3137 l_withTerms :=  wf_engine.GetItemAttrText (itemtype => l_itemtype,
3138                                          itemkey  => l_itemkey,
3139                                          aname    => 'WITH_TERMS');
3140 
3141 
3142 /* Bug 7424634
3143    PDF without TERMS should be sent to Buyer
3144    Commenting the condition for TERMS */
3145 
3146 --if   PO_CONTERMS_UTL_GRP.is_procurement_contract_doc(document_id) then
3147 --if l_withTerms ='Y' then
3148 
3149   --bug#3463617
3150   --l_filename := po_communication_pvt.getPDFFileName(l_document_type,'_TERMS_',l_org_id,l_document_id,l_revision_number,l_language);
3151 --else
3152   --bug#3463617
3153   l_filename := po_communication_pvt.getPDFFileName(l_document_type,'_',l_org_id,l_document_id,l_revision_number,l_language);
3154 --end if;
3155 /* End Bug 7424634 */
3156 
3157 if l_document_type = 'RELEASE' then
3158   l_entity_name :='PO_REL';
3159 end if;
3160 
3161 if l_document_type in ('PO','PA') then
3162   l_entity_name :='PO_HEAD';
3163 end if;
3164 
3165 --Bug #4865352 - Added join with fnd_documents and selected media_id from it
3166 --Bug #5232999 - Added file name as criterion so that we get only the PDF
3167 SELECT file_data into l_document
3168 FROM fnd_lobs fl,
3169      fnd_attached_documents fad,
3170      fnd_documents fd,
3171      fnd_documents_tl fdl
3172 WHERE fad.pk1_value=to_char(l_document_id)  and fad.pk2_value=to_char(l_revision_number)
3173 and fdl.document_id = fad.document_id and fdl.document_id = fd.document_id and fd.media_id = fl.file_id
3174 and fad.entity_name = l_entity_name and fdl.language=l_language
3175 and fl.file_name = l_filename ;
3176 
3177        l_document_length := dbms_lob.GetLength(l_document);
3178        dbms_lob.copy(document, l_document, l_document_length, 1, 1);
3179 
3180 document_type:='application/pdf; name='||l_filename;
3181 
3182 EXCEPTION
3183         WHEN OTHERS THEN
3184    --l_document:=fnd_message.get_string('PO','PO_PDF_FAILED');
3185    --WF_NOTIFICATION.WriteToBlob(document, l_document);
3186   x_progress := 'PO_COMMUNICATION_PVT.pdf_attach_app-Exception ';
3187 
3188   IF (g_po_wf_debug = 'Y') THEN
3189    PO_WF_DEBUG_PKG.INSERT_DEBUG(l_itemtype, l_itemkey,x_progress);
3190   END IF;
3191 
3192 --Bug#3274081 Display the message to the user incase the pdf fails.
3193 document_type:='text/html; name='||l_filename;
3194 l_message := fnd_message.get_string('PO','PO_PDF_FAILED');
3195 DBMS_LOB.write(document, lengthb(l_message), 1, UTL_RAW.cast_to_raw(l_message));
3196 
3197 END pdf_attach_app;
3198 
3199 -- Bug 3823799. Recoded following procedure. This procedure is to
3200 -- Communicate the PDF document in the language selected in
3201 -- Communicate window. Earlier this procedure was not used at all
3202 -- and PDF_ATTACH was being used for the same purpose
3203 PROCEDURE pdf_attach_supp(document_id    in     varchar2,
3204                           content_type   in     varchar2,
3205                           document       in out nocopy blob,
3206                           document_type  in out nocopy varchar2) IS
3207 l_filename   fnd_lobs.file_name%type;
3208 l_document_id   number;
3209 l_document_type po_headers.type_lookup_code%TYPE;
3210 l_org_id number;
3211 l_revision_number number;
3212 l_language fnd_languages.language_code%type;
3213 l_entity_name  varchar2(30);
3214 l_itemtype po_document_types.wf_approval_itemtype%type;
3215 l_itemkey varchar2(60);
3216 l_document blob;
3217 l_withTerms varchar2(1);
3218 l_document_length number;
3219 l_message  FND_NEW_MESSAGES.message_text%TYPE;
3220 
3221 x_progress varchar2(300);
3222 
3223 BEGIN
3224     x_progress := 'PO_COMMUNICATION_PVT.pdf_attach_supp';
3225 
3226     l_itemtype := substr(document_id, 1, instr(document_id, ':') - 1);
3227     l_itemkey := substr(document_id, instr(document_id, ':') + 1, length(document_id) - 2);
3228 
3229     IF (g_po_wf_debug = 'Y') THEN
3230         PO_WF_DEBUG_PKG.INSERT_DEBUG(l_itemtype, l_itemkey,x_progress);
3231     END IF;
3232 
3233     l_document_id :=wf_engine.GetItemAttrNumber (itemtype => l_itemtype,
3234                                                  itemkey  => l_itemkey,
3235                                                  aname    => 'DOCUMENT_ID');
3236 
3237     l_org_id := wf_engine.GetItemAttrNumber (itemtype => l_itemtype,
3238                                              itemkey  => l_itemkey,
3239                                              aname    => 'ORG_ID');
3240 
3241     l_document_type := wf_engine.GetItemAttrText (itemtype => l_itemtype,
3242                                                   itemkey  => l_itemkey,
3243                                                   aname    => 'DOCUMENT_TYPE');
3244 
3245     l_language := wf_engine.GetItemAttrText (itemtype => l_itemtype,
3246                                              itemkey  => l_itemkey,
3247                                              aname    => 'LANGUAGE_CODE');
3248 
3249     l_revision_number := wf_engine.GetItemAttrNumber (itemtype => l_itemtype,
3250                                                       itemkey  => l_itemkey,
3251                                                       aname    => 'REVISION_NUMBER');
3252 
3253     l_withTerms :=  wf_engine.GetItemAttrText (itemtype => l_itemtype,
3254                                                itemkey  => l_itemkey,
3255                                                aname    => 'WITH_TERMS');
3256 
3257     IF l_withTerms ='Y' THEN
3258         l_filename := po_communication_pvt.getPDFFileName(l_document_type,'_TERMS_',l_org_id,l_document_id,l_revision_number,l_language);
3259     ELSE
3260         l_filename := po_communication_pvt.getPDFFileName(l_document_type,'_',l_org_id,l_document_id,l_revision_number,l_language);
3261     END IF;
3262     --Bug #4865352 - Added join with fnd_documents and selected media_id from it
3263     -- Bug 4047688
3264     -- Added join condition on file name of PDF
3265     IF l_withTerms='Y' AND l_document_type in ('PO','PA') THEN
3266         SELECT file_data into l_document
3267         FROM fnd_lobs fl,
3268              fnd_attached_documents fad,
3269              fnd_documents fd,
3270              fnd_documents_tl  fdl
3271         WHERE fad.pk2_value=to_char(l_document_id)  and fad.pk3_value=to_char(l_revision_number)
3272         and fdl.document_id = fad.document_id and fdl.document_id = fd.document_id and fd.media_id = fl.file_id
3273         and fad.entity_name = 'OKC_CONTRACT_DOCS' and fdl.language=l_language
3274         and fl.file_name = l_filename;   -- Bug 4047688
3275     END IF;
3276 
3277     IF l_document_type in ('PO','PA') THEN
3278         l_entity_name :='PO_HEAD';
3279     ELSIF l_document_type = 'RELEASE' THEN
3280         l_entity_name :='PO_REL';
3281     END IF;
3282     --Bug #4865352 - Added a join with fnd_documents
3283     IF l_document_type in ('PO','PA','RELEASE') AND l_withTerms ='N' THEN
3284         SELECT file_data into l_document
3285         FROM fnd_lobs fl,
3286              fnd_attached_documents fad,
3287              fnd_documents fd,
3288              fnd_documents_tl  fdl
3289         WHERE fad.pk1_value=to_char(l_document_id)  and fad.pk2_value=to_char(l_revision_number)
3290         and fdl.document_id = fad.document_id and fd.media_id = fl.file_id
3291         and fd.document_id = fdl.document_id
3292         and fad.entity_name = l_entity_name and fl.file_name = l_filename and fdl.language=l_language;
3293     END IF;
3294 
3295     l_document_length := dbms_lob.GetLength(l_document);
3296     dbms_lob.copy(document, l_document, l_document_length, 1, 1);
3297     document_type:='application/pdf; name='||l_filename;
3298 
3299 EXCEPTION
3300     WHEN OTHERS THEN
3301     x_progress := 'PO_COMMUNICATION_PVT.pdf_attach_supp - Exception ';
3302 
3303     IF (g_po_wf_debug = 'Y') THEN
3304         PO_WF_DEBUG_PKG.INSERT_DEBUG(l_itemtype, l_itemkey,x_progress);
3305     END IF;
3306 
3307     -- Display the message to the user incase the pdf fails.
3308     -- Bug 4047688
3309     -- Removed concatenation of l_filename from document_type
3310     document_type:='text/html';
3311     l_message := fnd_message.get_string('PO','PO_PDF_FAILED');
3312     DBMS_LOB.write(document, lengthb(l_message), 1, UTL_RAW.cast_to_raw(l_message));
3313 END  pdf_attach_supp;
3314 
3315 
3316 procedure pdf_attach(document_id    in     varchar2,
3317                            content_type   in     varchar2,
3318                            document       in out nocopy blob,
3319                            document_type  in out nocopy varchar2) IS
3320 l_filename   fnd_lobs.file_name%type;
3321 l_document_id   number;
3322 l_document_type po_headers.type_lookup_code%TYPE;
3323 l_org_id number;
3324 l_revision_number number;
3325 l_language fnd_languages.language_code%type;
3326 l_entity_name  varchar2(30);
3327 l_itemtype po_document_types.wf_approval_itemtype%type;
3328 l_itemkey varchar2(60);
3329 l_document blob;
3330 l_withTerms varchar2(1);
3331 l_document_length number;
3332 l_message  FND_NEW_MESSAGES.message_text%TYPE; --Bug#3274081
3333 
3334 
3335 x_progress varchar2(300);
3336 
3337 begin
3338 x_progress := 'PO_COMMUNICATION_PVT.pdf_attach';
3339 
3340 l_itemtype := substr(document_id, 1, instr(document_id, ':') - 1);
3341 l_itemkey := substr(document_id, instr(document_id, ':') + 1, length(document_id) - 2);
3342 
3343   IF (g_po_wf_debug = 'Y') THEN
3344    PO_WF_DEBUG_PKG.INSERT_DEBUG(l_itemtype, l_itemkey,x_progress);
3345   END IF;
3346 
3347 l_document_id :=wf_engine.GetItemAttrNumber (itemtype => l_itemtype,
3348                                          itemkey  => l_itemkey,
3349                                          aname    => 'DOCUMENT_ID');
3350 
3351 l_org_id := wf_engine.GetItemAttrNumber (itemtype => l_itemtype,
3352                                          itemkey  => l_itemkey,
3353                                          aname    => 'ORG_ID');
3354 
3355 l_document_type := wf_engine.GetItemAttrText (itemtype => l_itemtype,
3356                                          itemkey  => l_itemkey,
3357                                          aname    => 'DOCUMENT_TYPE');
3358 
3359 l_language := wf_engine.GetItemAttrText (itemtype => l_itemtype,
3360                                          itemkey  => l_itemkey,
3361                                          aname    => 'LANGUAGE_CODE');
3362 
3363 l_revision_number := wf_engine.GetItemAttrNumber (itemtype => l_itemtype,
3364                                          itemkey  => l_itemkey,
3365                                          aname    => 'REVISION_NUMBER');
3366 
3367 l_withTerms :=  wf_engine.GetItemAttrText (itemtype => l_itemtype,
3368                                          itemkey  => l_itemkey,
3369                                          aname    => 'WITH_TERMS');
3370 
3371 if l_withTerms ='Y' then
3372   --bug#3463617
3373   l_filename := po_communication_pvt.getPDFFileName(l_document_type,'_TERMS_',l_org_id,l_document_id,l_revision_number,l_language);
3374 else
3375     /* Bug 3849854. PDF is not communicated in Suppliers language
3376        According to the document_id (po_header_id/po_release_id),
3377        the language is found from po_vendor_sites and corresponding
3378        PDF is retrieved
3379        Bug 3851357. Changed po_vendor_sites to po_vendor_sites_all because
3380        po_vendor_sites is an org striped view. The query was failing in the
3381        particular case when the MO:Operating unit site level value was
3382        different from buyer's user level value */
3383     Begin
3384 
3385         if l_document_type in ('PO','PA') then
3386             select fl.language_code into l_language
3387             from po_vendor_sites_all pvs,po_headers_all ph,fnd_languages fl
3388             where ph.vendor_site_id = pvs.vendor_site_id
3389             and ph.po_header_id = l_document_id
3390             and pvs.language = fl.nls_language;
3391         elsif l_document_type = 'RELEASE' then
3392             select fl.language_code into l_language
3393             from po_vendor_sites_all pvs , po_headers_all ph,
3394                  po_releases_all pr, fnd_languages fl
3395             where ph.po_header_id = pr.po_header_id
3396             and pr.po_release_id = l_document_id
3397             and ph.vendor_site_id = pvs.vendor_site_id
3398             and pvs.language = fl.nls_language;
3399         end if;
3400     Exception when others Then
3401         -- A no_data_found exception will be raised if language preference is
3402         -- left null in the vendor sites form. In this case communicate the
3403         -- PDF in buyer's language only.
3404         -- If there is any other exception then also leave the language to
3405         -- buyer's as selected from the workflow attribute above
3406         null;
3407     End;
3408 
3409   --bug#3463617
3410   l_filename := po_communication_pvt.getPDFFileName(l_document_type,'_',l_org_id,l_document_id,l_revision_number,l_language);
3411 
3412 end if;
3413 
3414 
3415 IF l_document_type in ('PO','PA') THEN
3416 
3417  IF l_withTerms='Y' THEN
3418 --Bug #4865352 - Replaced fnd_documents_tl with fnd_documents_vl
3419 -- Bug 4047688
3420 -- Appended join condition on file name of document to prevent return of multiple rows
3421 SELECT file_data into l_document
3422 FROM fnd_lobs fl,
3423      fnd_attached_documents fad,
3424      fnd_documents_vl  fdl
3425 WHERE fad.pk2_value=to_char(l_document_id)  and fad.pk3_value=to_char(l_revision_number)
3426 and fdl.document_id = fad.document_id and fdl.media_id = fl.file_id and fad.entity_name = 'OKC_CONTRACT_DOCS'
3427 and fl.file_name = l_filename; --Bug 4047688
3428 
3429 END IF;
3430 
3431 END IF;
3432 
3433 
3434 if l_document_type in ('PO','PA') then
3435 l_entity_name :='PO_HEAD';
3436 end if;
3437 
3438 if l_document_type = 'RELEASE' then
3439 l_entity_name :='PO_REL';
3440 end if;
3441 
3442 
3443 if l_document_type in ('PO','PA','RELEASE') and l_withTerms ='N' then
3444 
3445 --Bug #4865352 - Added a join with fnd_documents
3446 SELECT file_data into l_document
3447 FROM fnd_lobs fl,
3448      fnd_attached_documents fad,
3449      fnd_documents fd,
3450     fnd_documents_tl  fdl
3451 WHERE fad.pk1_value=to_char(l_document_id)  and fad.pk2_value=to_char(l_revision_number)
3452 and fdl.document_id = fad.document_id and fd.media_id = fl.file_id and fd.document_id = fdl.document_id
3453 and fad.entity_name = l_entity_name and fl.file_name = l_filename and fdl.language=l_language;
3454 
3455 END IF;
3456 
3457        l_document_length := dbms_lob.GetLength(l_document);
3458        dbms_lob.copy(document, l_document, l_document_length, 1, 1);
3459 
3460   document_type:='application/pdf; name='||l_filename;
3461 
3462 EXCEPTION
3463         WHEN OTHERS THEN
3464    --l_document:=fnd_message.get_string('PO','PO_PDF_FAILED');
3465    --WF_NOTIFICATION.WriteToBlob(document, l_document);
3466     x_progress := 'PO_COMMUNICATION_PVT.pdf_attach - Exception ';
3467 
3468   IF (g_po_wf_debug = 'Y') THEN
3469    PO_WF_DEBUG_PKG.INSERT_DEBUG(l_itemtype, l_itemkey,x_progress);
3470   END IF;
3471 
3472 --Bug#3274081 Display the message to the user incase the pdf fails.
3473 --Bug 4047688: Removed concatenation of l_filename from document_type
3474 document_type:='text/html';
3475 l_message := fnd_message.get_string('PO','PO_PDF_FAILED');
3476 DBMS_LOB.write(document, lengthb(l_message), 1, UTL_RAW.cast_to_raw(l_message));
3477 
3478 END pdf_attach;
3479 
3480 
3481 -- <Start Word Integration 11.5.10+>
3482 -------------------------------------------------------------------------------
3483 --Start of Comments
3484 --Name: okc_doc_attach
3485 --Pre-reqs:
3486 --  Should only be called if contracts document exists and is not merged
3487 --  into the PO PDF file.
3488 --Modifies:
3489 --  None.
3490 --Locks:
3491 --  None.
3492 --Function:
3493 --  Attaches "attached document" contract terms from contracts.
3494 --Parameters:
3495 --IN:
3496 --  Follows the workflow document attachment API specification.
3497 --Testing:
3498 --
3499 --End of Comments
3500 -------------------------------------------------------------------------------
3501 PROCEDURE okc_doc_attach(document_id    in     varchar2,
3502                          content_type   in     varchar2,
3503                          document       in out nocopy blob,
3504                          document_type  in out nocopy varchar2)
3505 IS
3506 
3507 l_okc_file_id           fnd_lobs.file_id%TYPE;
3508 l_okc_file_name         fnd_lobs.file_name%TYPE;
3509 l_okc_file_data         fnd_lobs.file_data%TYPE;
3510 l_okc_file_content_type fnd_lobs.file_content_type%TYPE;
3511 
3512 l_po_document_id       number;
3513 l_po_document_type     po_headers.type_lookup_code%TYPE;
3514 l_po_document_subtype  po_headers.type_lookup_code%TYPE;
3515 l_po_org_id            number;
3516 l_po_revision_number   number;
3517 l_language         fnd_languages.language_code%type;
3518 l_withTerms        varchar2(1);
3519 
3520 l_itemtype  po_document_types.wf_approval_itemtype%type;
3521 l_itemkey   PO_HEADERS_ALL.wf_item_key%TYPE;
3522 l_message  FND_NEW_MESSAGES.message_text%TYPE;
3523 
3524 l_okc_doc_length       number;   -- Bug 4173198
3525 
3526 x_progress varchar2(300);
3527 
3528 BEGIN
3529 
3530   x_progress := 'PO_COMMUNICATION_PVT.okc_doc_attach:010';
3531 
3532   l_itemtype := substr(document_id, 1, instr(document_id, ':') - 1);
3533   l_itemkey := substr(document_id, instr(document_id, ':') + 1,
3534                                    length(document_id) - 2);
3535 
3536   IF (g_po_wf_debug = 'Y') THEN
3537     PO_WF_DEBUG_PKG.insert_debug(l_itemtype, l_itemkey, x_progress);
3538   END IF;
3539 
3540   l_po_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
3541                                 itemtype => l_itemtype,
3542                                 itemkey  => l_itemkey,
3543                                 aname    => 'DOCUMENT_ID');
3544 
3545   l_po_org_id := PO_WF_UTIL_PKG.GetItemAttrNumber(
3546                                 itemtype => l_itemtype,
3547                                 itemkey  => l_itemkey,
3548                                 aname    => 'ORG_ID');
3549 
3550   l_po_document_type := PO_WF_UTIL_PKG.GetItemAttrText (
3551                                 itemtype => l_itemtype,
3552                                 itemkey  => l_itemkey,
3553                                 aname    => 'DOCUMENT_TYPE');
3554 
3555   l_po_document_subtype := PO_WF_UTIL_PKG.GetItemAttrText(
3556                                 itemtype => l_itemtype,
3557                                 itemkey  => l_itemkey,
3558                                 aname    => 'DOCUMENT_SUBTYPE');
3559 
3560   l_language := PO_WF_UTIL_PKG.GetItemAttrText (
3561                                 itemtype => l_itemtype,
3562                                 itemkey  => l_itemkey,
3563                                 aname    => 'LANGUAGE_CODE');
3564 
3565   l_po_revision_number := PO_WF_UTIL_PKG.GetItemAttrNumber (
3566                                 itemtype => l_itemtype,
3567                                 itemkey  => l_itemkey,
3568                                 aname  => 'REVISION_NUMBER');
3569 
3570   l_withTerms :=  PO_WF_UTIL_PKG.GetItemAttrText (
3571                                 itemtype => l_itemtype,
3572                                 itemkey  => l_itemkey,
3573                                 aname    => 'WITH_TERMS');
3574 
3575   x_progress := '020';
3576 
3577   IF (g_po_wf_debug = 'Y') THEN
3578     PO_WF_DEBUG_PKG.insert_debug(l_itemtype, l_itemkey, x_progress);
3579   END IF;
3580 
3581   IF l_withTerms <> 'Y' THEN
3582     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3583   END IF;
3584 
3585   l_okc_file_id := OKC_TERMS_UTIL_GRP.GET_PRIMARY_TERMS_DOC_FILE_ID(
3586                   P_document_type =>
3587                     PO_CONTERMS_UTL_GRP.get_po_contract_doctype(l_po_document_subtype)
3588                 , P_document_id => l_po_document_id
3589                  );
3590 
3591   x_progress := '030; l_okc_file_id = ' || l_okc_file_id;
3592 
3593   IF (g_po_wf_debug = 'Y') THEN
3594     PO_WF_DEBUG_PKG.insert_debug(l_itemtype, l_itemkey, x_progress);
3595   END IF;
3596 
3597   IF (l_okc_file_id > 0)
3598   THEN
3599 
3600     -- Bug 4173198: Select file_data from fnd_lobs into local variable
3601     -- l_okc_file_data first and then use dbms_lob.copy
3602 
3603     SELECT fl.file_name, fl.file_content_type, fl.file_data
3604     INTO l_okc_file_name, l_okc_file_content_type, l_okc_file_data
3605     FROM fnd_lobs fl
3606     WHERE fl.file_id = l_okc_file_id;
3607 
3608     document_type := l_okc_file_content_type ||  '; name=' || l_okc_file_name;
3609 
3610     l_okc_doc_length := dbms_lob.GetLength(l_okc_file_data);
3611     dbms_lob.copy(document, l_okc_file_data, l_okc_doc_length, 1, 1);
3612 
3613   ELSE
3614 
3615     /* file does not exist; return a null */
3616     document := NULL;
3617     document_type := NULL;
3618 
3619   END IF;  /* l_okc_file_id > 0 */
3620 
3621   x_progress := 'END OF okc_doc_attach';
3622 
3623   IF (g_po_wf_debug = 'Y') THEN
3624     PO_WF_DEBUG_PKG.insert_debug(l_itemtype, l_itemkey, x_progress);
3625   END IF;
3626 
3627 EXCEPTION
3628 
3629   /* Handle Exceptions */
3630   WHEN others THEN
3631     x_progress := 'PO_COMMUNICATION_PVT.pdf_attach - Exception ';
3632 
3633     IF (g_po_wf_debug = 'Y') THEN
3634        PO_WF_DEBUG_PKG.INSERT_DEBUG(l_itemtype, l_itemkey,x_progress);
3635     END IF;
3636 
3637     document_type:='text/html; name='||l_okc_file_name;
3638     l_message := fnd_message.get_string('PO','PO_OKC_DOC_FAILED');
3639     DBMS_LOB.write(document, lengthb(l_message), 1, UTL_RAW.cast_to_raw(l_message));
3640 
3641 END okc_doc_attach;
3642 
3643 
3644 -- <End Word Integration 11.5.10+>
3645 
3646 
3647 
3648 -------------------------------------------------------------------------------
3649 --Start of Comments
3650 --Name: zip_attach
3651 --Pre-reqs:
3652 --  Zip file should be generated correctly and stored in the database
3653 --Modifies:
3654 --  None.
3655 --Locks:
3656 --  None.
3657 --Function:
3658 --  Queries the Zip Blob for specified document and returns it as a blob
3659 --  to attach to the workflow notification
3660 --Parameters:
3661 --  Follows workflow standatd API specification for documents
3662 --IN:
3663 --document_id
3664 --  A string the uniquely identifies the document to be attached
3665 --content_type
3666 --  For PL/SQL Blob documents, the values is ''
3667 --IN OUT:
3668 --document
3669 --  Outbound Lob locator for the Blob to be attached
3670 --document_type
3671 --  String buffer that contains the content type of outbound Blob
3672 --Notes:
3673 --  Added as a part of <PO Attachment Support 11i.11>
3674 --End of Comments
3675 -------------------------------------------------------------------------------
3676 PROCEDURE zip_attach(document_id      in     varchar2,
3677                      content_type     in     varchar2,
3678                      document         in out nocopy blob,
3679                      document_type    in out nocopy varchar2) IS
3680 l_filename fnd_lobs.file_name%type;
3681 l_document_id po_headers_all.po_header_id%type;
3682 l_document_number po_headers_all.segment1%type;
3683 l_document_type po_headers_all.type_lookup_code%TYPE;
3684 l_org_id po_headers_all.org_id%type;
3685 l_revision_number po_headers_all.revision_num%type;
3686 l_language fnd_languages.language_code%type;
3687 l_entity_name fnd_attached_documents.entity_name%type;
3688 l_itemtype wf_items.item_type%type;
3689 l_itemkey wf_items.item_key%type;
3690 l_document fnd_lobs.file_data%type;
3691 l_document_length number;
3692 l_progress varchar2(300);
3693 l_filecontent_type fnd_lobs.file_content_type%type;
3694 l_message  FND_NEW_MESSAGES.message_text%TYPE;
3695 
3696 
3697 BEGIN
3698     l_progress := 'PO_COMMUNICATION_PVT.zip_attach : Begin';
3699 
3700     l_itemtype := substr(document_id, 1, instr(document_id, ':') - 1);
3701     l_itemkey := substr(document_id, instr(document_id, ':') + 1, length(document_id) - 2);
3702 
3703     IF (g_po_wf_debug = 'Y') THEN
3704         PO_WF_DEBUG_PKG.INSERT_DEBUG(l_itemtype, l_itemkey,l_progress);
3705     END IF;
3706 
3707     l_progress := 'PO_COMMUNICATION_PVT.zip_attach : Get item attributes';
3708     l_document_id :=wf_engine.GetItemAttrNumber (itemtype => l_itemtype,
3709                                                  itemkey  => l_itemkey,
3710                                                  aname    => 'DOCUMENT_ID');
3711 
3712     l_org_id := wf_engine.GetItemAttrNumber (itemtype => l_itemtype,
3713                                              itemkey  => l_itemkey,
3714                                              aname    => 'ORG_ID');
3715 
3716     l_document_type := wf_engine.GetItemAttrText (itemtype => l_itemtype,
3717                                                   itemkey  => l_itemkey,
3718                                                   aname    => 'DOCUMENT_TYPE');
3719 
3720     l_language := wf_engine.GetItemAttrText (itemtype => l_itemtype,
3721                                              itemkey  => l_itemkey,
3722                                              aname    => 'LANGUAGE_CODE');
3723 
3724     l_revision_number := wf_engine.GetItemAttrNumber (itemtype => l_itemtype,
3725                                                       itemkey  => l_itemkey,
3726                                                       aname    => 'REVISION_NUMBER');
3727 
3728     l_progress := 'PO_COMMUNICATION_PVT.zip_attach : Decide Entity to query';
3729     IF l_document_type in ('PO','PA') THEN
3730         l_entity_name :='PO_HEAD';
3731     ELSIF l_document_type = 'RELEASE' THEN
3732         l_entity_name :='PO_REL';
3733     END IF;
3734 
3735     l_progress := 'PO_COMMUNICATION_PVT.zip_attach : Get Zip file name';
3736     Begin
3737         --Bug# 5240634 Pass in the org_id to getZIPFileName
3738         l_filename := getZIPFileName(l_org_id);
3739     Exception When others Then
3740         l_progress := 'PO_COMMUNICATION_PVT.zip_attach : Exception in getZIPFileName';
3741         raise;
3742     End;
3743 
3744     l_progress := 'PO_COMMUNICATION_PVT.zip_attach : Query the Zip blob';
3745     --Bug #4865352 - Added a join with fnd_documents and selected media_id from it
3746     Begin
3747         SELECT fl.file_data,fl.file_content_type
3748         INTO l_document,l_filecontent_type
3749         FROM fnd_lobs fl,
3750              fnd_attached_documents fad,
3751              fnd_documents fd,
3752              fnd_documents_tl  fdl
3753         WHERE fad.pk1_value=to_char(l_document_id)
3754         and fad.pk2_value=to_char(l_revision_number)
3755         and fad.entity_name = l_entity_name
3756         and fdl.document_id = fad.document_id
3757         and fdl.document_id = fd.document_id
3758         and fdl.language = l_language
3759         --Bug 5017976 selecting media_id from fd instead of fdl
3760         and fd.media_id = fl.file_id
3761         and fl.file_name = l_filename;
3762     Exception
3763         When others Then
3764             l_progress := 'PO_COMMUNICATION_PVT.zip_attach : no_data_found';
3765             raise;
3766     End;
3767 
3768     l_progress := 'PO_COMMUNICATION_PVT.zip_attach : Get blob length';
3769     l_document_length := dbms_lob.GetLength(l_document);
3770 
3771     l_progress := 'PO_COMMUNICATION_PVT.zip_attach : Copy zip blob';
3772     dbms_lob.copy(document, l_document, l_document_length, 1, 1);
3773 
3774     l_progress := 'PO_COMMUNICATION_PVT.zip_attach : Set document type';
3775     document_type:=l_filecontent_type||'; name='||l_filename;
3776 
3777     l_progress := 'PO_COMMUNICATION_PVT.zip_attach : End';
3778 EXCEPTION
3779     WHEN OTHERS THEN
3780 
3781         IF (g_po_wf_debug = 'Y') THEN
3782             PO_WF_DEBUG_PKG.INSERT_DEBUG(l_itemtype, l_itemkey,l_progress||' '||SQLERRM);
3783         END IF;
3784 
3785         document_type:='text/html; name='||l_filename;
3786         l_message := fnd_message.get_string('PO','PO_ZIP_FAILED');
3787         DBMS_LOB.write(document, lengthb(l_message), 1, UTL_RAW.cast_to_raw(l_message));
3788 
3789 END zip_attach;
3790 
3791 -------------------------------------------------------------------------------
3792 --Start of Comments
3793 --Name: set_zip_error_code
3794 --Pre-reqs:
3795 --  None.
3796 --Modifies:
3797 --  Workflow Attribute zip_error_code
3798 --Locks:
3799 --  None.
3800 --Function:
3801 --  Sets the workflow attribute zip_error_code to the value passed in
3802 --Parameters:
3803 --IN:
3804 --p_itemtype
3805 --  String that identifies the workflow process
3806 --p_itemkey
3807 --  Uniquely identifies the current instance of the workflow process
3808 --p_zip_error_code
3809 --  Value to which the workflow attribute zip_error_code should be
3810 --  set to. Possible values can be OVERSIZED, DUPLICATE_FILENAME
3811 --  and UNEXPECTED (values of lookup type POAPPRV_ZIP_ERROR_CODE)
3812 --Notes:
3813 --  Added as a part of <PO Attachment Support 11i.11>
3814 --End of Comments
3815 -------------------------------------------------------------------------------
3816 PROCEDURE set_zip_error_code (p_itemtype       IN VARCHAR2,
3817                               p_itemkey        IN VARCHAR2,
3818                               p_zip_error_code IN VARCHAR2) IS
3819 BEGIN
3820     -- Set the value of the workflow attribute zip_error_code
3821     PO_WF_UTIL_PKG.SetItemAttrText(itemtype => p_itemtype,
3822                                    itemkey  => p_itemkey,
3823                                    aname    => 'ZIP_ERROR_CODE',
3824                                    avalue   => p_zip_error_code);
3825 END set_zip_error_code;
3826 
3827 -------------------------------------------------------------------------------
3828 --Start of Comments
3829 --Name: get_zip_error_code
3830 --Pre-reqs:
3831 --  None.
3832 --Modifies:
3833 --  None.
3834 --Locks:
3835 --  None.
3836 --Function:
3837 --  Returns the value of workflow attribute ZIP_ERROR_CODE
3838 --Parameters:
3839 --  Follows standard workflow API specification
3840 --IN:
3841 --itemtype
3842 --  String that identifies the workflow process
3843 --itemkey
3844 --  Uniquely identifies the current instance of the workflow process
3845 --actid
3846 --  The ID number of the activity from which this procedure is callled
3847 --funcmode
3848 --  The execution mode of the activity
3849 --OUT:
3850 --resultout
3851 --  Holds the value of the workflow attribute zip_error_code. Possible
3852 --  values can be OVERSIZED, DUPLICATE_FILENAME and UNEXPECTED
3853 --  (values of lookup type POAPPRV_ZIP_ERROR_CODE)
3854 --Notes:
3855 --  Added as a part of <PO Attachment Support 11i.11>
3856 --End of Comments
3857 -------------------------------------------------------------------------------
3858 PROCEDURE get_zip_error_code (itemtype IN VARCHAR2,
3859                               itemkey  IN VARCHAR2,
3860                               actid    IN NUMBER,
3861                               funcmode IN VARCHAR2,
3862                               resultout   OUT NOCOPY VARCHAR2) IS
3863     l_progress varchar2(200);
3864 BEGIN
3865     -- Get the value of the workflow attribute zip_error_code
3866     l_progress := 'PO_COMMUNICATION_PVT.get_zip_error_code: 01';
3867     resultout := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => itemtype,
3868                                                 itemkey  => itemkey,
3869                                                 aname    => 'ZIP_ERROR_CODE');
3870     l_progress := 'PO_COMMUNICATION_PVT.get_zip_error_code: 02';
3871     resultout := wf_engine.eng_completed || ':' ||resultout;
3872 EXCEPTION when others THEN
3873     WF_CORE.context('PO_COMMUNICATION_PVT', 'get_zip_error_code', l_progress);
3874     raise;
3875 END get_zip_error_code;
3876 
3877 -------------------------------------------------------------------------------
3878 --Start of Comments
3879 --Name: get_max_zip_size
3880 --Pre-reqs:
3881 --  Column max_attachment_size should exist in table po_system_parameters_all
3882 --  Org context should be set correctly to enable querying from org striped
3883 --  view po_system_parameters
3884 --Modifies:
3885 --  None.
3886 --Locks:
3887 --  None.
3888 --Function:
3889 --  Queries the value of 'Maximum Attachment Size' which is the maximum
3890 --  allowed size of the Zip file.
3891 --Parameters:
3892 --IN:
3893 --itemtype
3894 --  String that identifies the workflow process
3895 --itemkey
3896 --  Uniquely identifies the current instance of the workflow process
3897 --Returns:
3898 --  For functions:
3899 --    Returns the values of 'Maximum Attachment Size' in Purchasing Options
3900 --    Setup form. This is the maximum allowed size of the Zip file.
3901 --Notes:
3902 --  Added as a part of <PO Attachment Support 11i.11>
3903 --  A value of 0 means Zip Attachments are not supported
3904 --End of Comments
3905 -------------------------------------------------------------------------------
3906 FUNCTION get_max_zip_size (p_itemtype IN VARCHAR2,
3907                            p_itemkey  IN VARCHAR2) RETURN NUMBER IS
3908     l_max_attachment_size po_system_parameters_all.max_attachment_size%type;
3909     l_progress varchar2(200);
3910 BEGIN
3911     l_progress := 'PO_COMMUNICATION_PVT.get_max_zip_size : Querying max_attachment_size';
3912     IF (g_po_wf_debug = 'Y') THEN
3913         PO_WF_DEBUG_PKG.insert_debug(p_itemtype,p_itemkey,l_progress);
3914     END IF;
3915     select nvl(psp.max_attachment_size,0)
3916     into l_max_attachment_size
3917     from po_system_parameters psp;
3918     l_progress := 'PO_COMMUNICATION_PVT.get_max_zip_size : maximum attachment size = '||l_max_attachment_size;
3919     IF (g_po_wf_debug = 'Y') THEN
3920         PO_WF_DEBUG_PKG.insert_debug(p_itemtype,p_itemkey,l_progress);
3921     END IF;
3922     return l_max_attachment_size;
3923 Exception
3924     when others then
3925         l_progress := 'PO_COMMUNICATION_PVT.get_max_zip_size : handled exception while getting Maximum Attachment Size ';
3926         IF (g_po_wf_debug = 'Y') THEN
3927             PO_WF_DEBUG_PKG.insert_debug(p_itemtype,p_itemkey,l_progress);
3928         END IF;
3929         l_max_attachment_size := 0;
3930 END get_max_zip_size;
3931 
3932 
3933 -------------------------------------------------------------------------------
3934 --Start of Comments
3935 --Name: check_for_attachments
3936 --Pre-reqs:
3937 --  None.
3938 --Modifies:
3939 --  None.
3940 --Locks:
3941 --  None.
3942 --Function:
3943 --  Checks if any attachments of category 'To Supplier' and type 'File'
3944 --  exist for the current PO/Release
3945 --Parameters:
3946 --IN:
3947 --p_document_type
3948 --  Document Type, can be PO, PA or RELEASE
3949 --p_document_id
3950 --  po_header_id for PO, PA and po_release_id for RELEASE
3951 --Returns:
3952 --  For functions:
3953 --    Returns 'Y' if any attachments of category 'To Supplier' and type 'File'
3954 --    exist for the current PO/Release
3955 --Notes:
3956 --  Added as a part of <PO Attachment Support 11i.11>
3957 --  Exception handling for this light weight function is left to the calling
3958 --  code. This is because the exception handling will be different in
3959 --  different calls
3960 --End of Comments
3961 -------------------------------------------------------------------------------
3962 FUNCTION check_for_attachments(p_document_type IN VARCHAR2,
3963                                p_document_id      IN NUMBER) RETURN VARCHAR2 IS
3964     l_attachments_exist    VARCHAR2(1);
3965 BEGIN
3966     IF (p_document_type='RELEASE') THEN
3967 
3968         -- bug4931216
3969         -- Go directly to the base tables for better performance
3970         select 'Y' into l_attachments_exist from dual
3971         where exists
3972         (
3973             select fl.file_name
3974             from   fnd_documents d,
3975                    fnd_attached_documents ad,
3976                    fnd_doc_category_usages dcu,
3977                    fnd_attachment_functions af,
3978                    fnd_lobs fl
3979             where ((ad.pk1_value=to_char((select po_header_id from po_releases_all
3980                                    where po_release_id=p_document_id
3981                                   )) and ad.entity_name='PO_HEADERS')
3982                    OR
3983                    (ad.pk1_value=to_char(p_document_id) and ad.entity_name='PO_RELEASES')
3984                    OR
3985                    (ad.pk1_value=to_char((select pha.vendor_id
3986                                    from po_headers_all pha,po_releases_all pra
3987                                    where pra.po_release_id=p_document_id
3988                                    and pha.po_header_id=pra.po_header_id
3989                                   )) and ad.entity_name='PO_VENDORS')
3990                    OR
3991                    (ad.pk1_value in (select po_line_id from po_line_locations_all
3992                                       where po_release_id=p_document_id
3993                                       and shipment_type='BLANKET'
3994                                      ) and ad.entity_name='PO_LINES')
3995                    OR
3996                    (ad.pk1_value in (select line_location_id from po_line_locations_all
3997                                       where po_release_id=p_document_id
3998                                       and shipment_type='BLANKET'
3999                                      ) and ad.entity_name='PO_SHIPMENTS')
4000                    OR
4001                    (ad.pk2_value in (select pl.item_id
4002                                       from po_lines_all pl, po_line_locations_all pll
4003                                       where pll.po_release_id=p_document_id
4004                                       and pll.shipment_type='BLANKET'
4005                                       and pll.po_line_id=pl.po_line_id
4006                                       and to_char(PO_COMMUNICATION_PVT.getInventoryOrgId())=ad.pk1_value --Bug 4673653 Use Inventory OrgId
4007                                       and pl.item_id is not null
4008                                      ) AND ad.entity_name='MTL_SYSTEM_ITEMS')
4009                   )
4010             and d.document_id = ad.document_id
4011             and dcu.category_id = d.category_id
4012             and dcu.attachment_function_id = af.attachment_function_id
4013             and d.datatype_id=6
4014             and af.function_name='PO_PRINTPO'
4015             and d.media_id=fl.file_id
4016             and dcu.enabled_flag = 'Y'
4017         );
4018     ELSE
4019 
4020 
4021         -- bug4931216
4022         -- Go directly to the base tables for better performance
4023         select 'Y' into l_attachments_exist from dual
4024         where exists
4025         (
4026             select fl.file_name
4027             from fnd_documents d,
4028                  fnd_attached_documents ad,
4029                  fnd_doc_category_usages dcu,
4030                  fnd_attachment_functions af,
4031                  fnd_lobs fl
4032             where ((ad.pk1_value=to_char(p_document_id) and ad.entity_name='PO_HEADERS')
4033                    OR
4034                    (ad.pk1_value=to_char((select vendor_id from po_headers_all
4035                                    where po_header_id=p_document_id)) and ad.entity_name='PO_VENDORS')
4036                    OR
4037                    (ad.pk1_value in (select po_line_id from po_lines_all
4038                                       where po_header_id=p_document_id
4039                                      ) and ad.entity_name='PO_LINES')
4040                    OR
4041                    (ad.pk1_value in (select from_header_id from po_lines_all
4042                                       where po_header_id=p_document_id
4043                                       and from_header_id is not null
4044                                      ) and ad.entity_name='PO_HEADERS')
4045                    OR
4046                    (ad.pk1_value in (select from_line_id from po_lines_all
4047                                       where po_header_id=p_document_id
4048                                       and from_line_id is not null
4049                                      ) and ad.entity_name='PO_LINES')
4050                    OR
4051                    (ad.pk1_value in (select line_location_id from po_line_locations_all
4052                                       where po_header_id=p_document_id
4053                                       and shipment_type in ('PRICE BREAK','STANDARD','PREPAYMENT')  -- <Complex Work R12>
4054                                      ) and ad.entity_name='PO_SHIPMENTS')
4055                    OR
4056                    (ad.pk2_value in (select item_id from po_lines_all
4057                                       where po_header_id=p_document_id
4058                                       and to_char(PO_COMMUNICATION_PVT.getInventoryOrgId())=ad.pk1_value --Bug 4673653 Use Inventory OrgId
4059                                       and item_id is not null
4060                                      ) and ad.entity_name='MTL_SYSTEM_ITEMS')
4061                   )
4062             and d.document_id = ad.document_id
4063             and dcu.category_id = d.category_id
4064             and dcu.attachment_function_id = af.attachment_function_id
4065             and d.datatype_id=6
4066             and af.function_name='PO_PRINTPO'
4067             and d.media_id=fl.file_id
4068             and dcu.enabled_flag = 'Y'
4069         );
4070     END IF;
4071     return l_attachments_exist;
4072 END check_for_attachments;
4073 
4074 FUNCTION POXMLGEN(p_api_version in  NUMBER,
4075             p_document_id       in  NUMBER,
4076             p_revision_num      in  NUMBER,
4077             p_document_type     in  VARCHAR2,
4078             p_document_subtype  in  VARCHAR2,
4079             p_test_flag         in  VARCHAR2,
4080             p_which_tables      in  VARCHAR2,
4081             p_with_terms            in      VARCHAR2 --Bug#3583910
4082             -- Bug 3690810. Removed the file.encoding parameter
4083                  ) RETURN clob IS
4084 
4085   l_api_name CONSTANT VARCHAR2(30):= 'POXMLGEN';
4086   l_api_version CONSTANT NUMBER := 1.0;
4087   l_xml_result    CLOB;
4088   l_version   varchar2(20);
4089   l_compatibility varchar2(20);
4090   l_majorVersion  number;
4091   l_queryCtx    DBMS_XMLquery.ctxType;
4092   l_xml_query   varchar2(32000);
4093   l_xml_message_query varchar2(6000);
4094   l_xml9_stmt   varchar2(8000);
4095   l_head_short_attachment_query varchar2(6000);
4096   l_line_short_attachment_query varchar2(6000);
4097   l_shipment_short_attach_query varchar2(6000);
4098     --<PO Attachment Support 11i.11 Start>
4099     l_head_url_attachment_query     varchar2(6000);
4100     l_head_file_attachment_query    varchar2(6000);
4101     l_line_url_attachment_query     varchar2(6000);
4102     l_line_file_attachment_query    varchar2(6000);
4103     l_shipment_url_attach_query     varchar2(6000);
4104     l_shipment_file_attach_query    varchar2(6000);
4105     --<PO Attachment Support 11i.11 End>
4106   l_headerAttachments clob;
4107   l_headerAttachmentsQuery  varchar2(1000);
4108   l_count number;
4109   g_log_head    CONSTANT VARCHAR2(30) := 'po.plsql.PO_COMMUNICATION_PVT.';
4110   l_eventType  varchar2(20);
4111   l_lineAttachQuery varchar2(1200); --Bug5213932 increase length
4112   l_line_Attachments clob;
4113   l_shipmentAttachmentQuery varchar2(1200); --Bug5213932 increase length
4114   l_disAttachments clob;
4115   l_time varchar2(50);
4116   l_vendor_id PO_HEADERS_ALL.vendor_id%type;
4117   l_release_header_id PO_HEADERS_ALL.po_header_id%type;
4118   l_supp_org  PO_VENDORS.VENDOR_NAME%type;
4119   l_po_number PO_HEADERS.SEGMENT1%type;
4120   l_message varchar2(2001);
4121   l_ammendment_message varchar2(2001);
4122   l_change_summary PO_HEADERS.CHANGE_SUMMARY%type;
4123   l_timezone HZ_TIMEZONES_VL.NAME%TYPE;
4124   l_timezone_id varchar2(10);
4125   l_agreement_assign_query varchar2(2001);
4126   l_arc_agreement_assign_query varchar2(2001);
4127   l_fileClob CLOB := NULL;
4128   l_variablePosition number :=0;
4129   l_resultOffset number ; -- to store the offset
4130   l_tempXMLResult clob; -- temp xml clob;
4131   l_offset HZ_TIMEZONES_VL.GMT_DEVIATION_HOURS%type; -- to store GMT time difference
4132   l_address_details clob; -- bug#3580225: Clob to hold the address details XML
4133 
4134   l_okc_doc_type VARCHAR2(20);  -- <Word Integration 11.5.10+>
4135 
4136   -- <Complex Work R12 Start>
4137 
4138   l_adv_amount_query    VARCHAR2(160)   := '';
4139   l_complex_lloc_query  VARCHAR2(500)   := '';
4140   l_complex_dist_query  VARCHAR2(1300)   := '';
4141 
4142   -- <Complex Work R12 End>
4143    /*Bug5983107 */
4144    l_legal_entity_id NUMBER;
4145     x_legalentity_info  xle_utilities_grp.LegalEntity_Rec;
4146     x_return_status	VARCHAR2(20) ;
4147     x_msg_count		NUMBER ;
4148     x_msg_data		VARCHAR2(4000) ;
4149     l_org_id      number;
4150   /*Bug 5983107*/
4151 
4152 BEGIN
4153 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4154   FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||'Document Id:',p_document_id);
4155 END IF;
4156 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4157   FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||'Document Type:',p_document_type);
4158 END IF;
4159 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4160   FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||'Document SubType:',p_document_subtype);
4161 END IF;
4162 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4163   FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||'Table Type:',p_which_tables);
4164 END IF;
4165 
4166 /* Check the package name and version. IF wrong package or version raise the exception and exit */
4167 IF NOT FND_API.COMPATIBLE_API_CALL( l_api_version,
4168           p_api_version,
4169           l_api_name,
4170           G_PKG_NAME)
4171 THEN
4172   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4173 END IF;
4174 
4175 PO_COMMUNICATION_PVT.g_document_id := p_document_id;
4176 PO_COMMUNICATION_PVT.g_revision_num := p_revision_num;
4177 PO_COMMUNICATION_PVT.g_test_flag   := p_test_flag;
4178 
4179 
4180 --Start Bug#3771735
4181 --Assigned the Document Type Code to global variable
4182 PO_COMMUNICATION_PVT.g_documentTypeCode := p_document_type;
4183 --End Bug#3771735
4184 
4185 /*Bug#3583910 Assigned the parameter value to the g_with_terms variable*/
4186 PO_COMMUNICATION_PVT.g_with_terms  := p_with_terms;
4187 
4188 
4189 -- SQl What:  Querying for document type.
4190 -- SQL why: To display the Document type at header level in PO cover and details page.
4191 -- SQL Join:
4192 
4193 PO_COMMUNICATION_PVT.g_documentType := null;
4194 --Bug#3279968 Added the language column to the below sql statement to fetch onlyone record.
4195 
4196 SELECT TYPE_NAME into PO_COMMUNICATION_PVT.g_documentType FROM PO_DOCUMENT_TYPES_TL
4197 WHERE document_type_code = p_document_type and document_subtype = p_document_subtype and language = USERENV('LANG');
4198 
4199 /* For balnket documents eventtype is 'BLANKET LINE' and
4200 for other documents 'PO LINE' is event type, to get the price differentials*/
4201 
4202 IF(p_document_subtype <>  'BLANKET') THEN
4203   l_eventType := 'PO LINE';
4204 ELSE
4205   l_eventType := 'BLANKET LINE';
4206 END IF;
4207 
4208 -- SQl What:  Querying for buyer organisation, supplier organisation, PO number, change summary, vendor id and currency code
4209 -- SQL why: To get long attachments from headers that are attached by vendor, Vendor Id is required.
4210 --    Buyer, supplier organisations, po number and change summary is used to replace the
4211 --    tokens in message text of PO_FO_COVERING_MESSAGE and PO_FO_AMENDMENT_MESSAGE.
4212 -- SQL Join:  vendor_id and org_id
4213 -- Logic: Based on the p_document_type and p_which_tables table names will change
4214 -- Added the sql conditions to find the distinct count of shipment level ship to from header level ship to. This count is
4215 -- used in XSL to identify what to display in ship to address at header and shipment level
4216 
4217 BEGIN
4218 PO_COMMUNICATION_PVT.g_current_currency_code :=null;
4219 
4220 IF(p_document_type in ('PO','PA')) THEN
4221   IF p_which_tables = 'MAIN' THEN
4222 
4223   /*Bug5983107 Commenting out the below sql and writing the new sql below without reference to  hr_all_organization_units*/
4224   /* SELECT hle.name, vn.vendor_name, ph.segment1, ph.change_summary, ph.vendor_id, ph.currency_code
4225 	INTO po_communication_pvt.g_buyer_org, l_supp_org, l_po_number, l_change_summary, l_vendor_id, g_current_currency_code
4226     FROM hr_all_organization_units hle,  po_vendors vn, po_headers_all ph
4227     WHERE to_char(hle.organization_id) =  (select org_information2 from hr_organization_information where
4228     org_information_context = 'Operating Unit Information'  and organization_id = ph.org_id) AND vn.vendor_id = ph.vendor_id
4229     AND ph.po_header_id = p_document_id AND ph.revision_num = p_revision_num; */
4230 
4231     SELECT vn.vendor_name, ph.segment1, ph.change_summary, ph.vendor_id, ph.currency_code
4232 	INTO  l_supp_org, l_po_number, l_change_summary, l_vendor_id, g_current_currency_code
4233     FROM  po_vendors vn, po_headers_all ph
4234     WHERE vn.vendor_id = ph.vendor_id
4235     AND ph.po_header_id = p_document_id AND ph.revision_num = p_revision_num;
4236 
4237 
4238     SELECT count(distinct(plla.SHIP_TO_LOCATION_ID)) INTO PO_COMMUNICATION_PVT.g_dist_shipto_count
4239     FROM po_line_locations_all plla
4240     WHERE plla.po_header_id = p_document_id
4241     AND NVL(plla.payment_type, 'NONE') NOT IN ('ADVANCE', 'DELIVERY');  -- <Complex Work R12>
4242 
4243 
4244   ELSIF p_which_tables = 'ARCHIVE' THEN
4245 
4246     /*Bug5983107 Modifying the below sql by removing reference to  hr_all_organization_units*/
4247 
4248     SELECT vn.vendor_name, ph.segment1, ph.change_summary, ph.vendor_id, ph.currency_code
4249 	INTO  l_supp_org, l_po_number, l_change_summary, l_vendor_id, g_current_currency_code
4250     FROM  po_vendors vn, po_headers_archive_all ph
4251     WHERE  vn.vendor_id = ph.vendor_id
4252     AND ph.po_header_id = p_document_id AND ph.revision_num = p_revision_num;
4253 
4254     SELECT count(distinct(plla.SHIP_TO_LOCATION_ID)) INTO PO_COMMUNICATION_PVT.g_dist_shipto_count
4255     FROM po_line_locations_archive_all plla
4256     WHERE plla.po_header_id = p_document_id
4257     and plla.revision_num = p_revision_num
4258     AND NVL(plla.payment_type, 'NONE') NOT IN ('ADVANCE', 'DELIVERY');  -- <Complex Work R12>
4259 
4260   END IF;
4261 
4262   /*Bug5983107  Use the below API to get the legal entity name */
4263    BEGIN
4264      select org_id into l_org_id from po_headers_all where po_header_id= p_document_id ;
4265      l_legal_entity_id :=  PO_CORE_S.get_default_legal_entity_id(l_org_id);
4266 
4267            XLE_UTILITIES_GRP.Get_LegalEntity_Info(
4268        		                        x_return_status,
4269          	     	                x_msg_count,
4270        		                        x_msg_data,
4271                	                        null,
4272                	                        l_legal_entity_id,
4273            	                        x_legalentity_info);
4274 
4275      PO_COMMUNICATION_PVT.g_buyer_org := x_legalentity_info.name;
4276 
4277  EXCEPTION
4278  WHEN OTHERS then
4279  po_communication_pvt.g_buyer_org := null;
4280  END;
4281 
4282   /*Bug5983107 */
4283 
4284         -- bug#3698674: inserted header/release id and revision num into global temp table
4285         -- bug#3823799: Moved the query from top to here to insert data in table based on the document type.
4286         --              po_release_id is inserted as null
4287   -- bug#3853109: Added the column names in the insert clause as per the review comments
4288         insert into  PO_COMMUNICATION_GT(po_header_id, po_release_id, revision_number, format_mask)
4289                       values(p_document_id, null, p_revision_num, PO_COMMUNICATION_PVT.getFormatMask);
4290 ELSE
4291     -- Modified as a part of bug #3274076
4292     -- Vendor id is same for revisied and non revised documents. So vendor id is retreived from the releases table.
4293 
4294     -- select the header id into g_release_header_id global variable for a given release id.
4295     SELECT po_header_id INTO PO_COMMUNICATION_PVT.g_release_header_id FROM po_releases_all WHERE po_release_id = p_document_id;
4296 
4297     SELECT  ph.vendor_id, ph.currency_code INTO l_vendor_id, g_current_currency_code
4298     FROM po_vendors vn, po_headers_all ph
4299     WHERE  vn.vendor_id = ph.vendor_id
4300     AND ph.po_header_id = PO_COMMUNICATION_PVT.g_release_header_id ;
4301 
4302     IF p_which_tables = 'MAIN' THEN
4303       SELECT count(distinct(plla.SHIP_TO_LOCATION_ID)) INTO PO_COMMUNICATION_PVT.g_dist_shipto_count
4304       FROM po_line_locations_all plla
4305       WHERE plla.po_release_id = p_document_id;
4306 
4307     ELSE
4308       SELECT count(distinct(plla.SHIP_TO_LOCATION_ID)) INTO PO_COMMUNICATION_PVT.g_dist_shipto_count
4309       FROM po_line_locations_archive_all plla
4310       WHERE plla.po_release_id = p_document_id
4311       and plla.revision_num  = p_revision_num;
4312     END IF;
4313 
4314         -- bug#3698674: inserted header/release id and revision num into global temp table
4315         -- bug#3823799: Moved the query from top to here to insert data in table based on the document type.
4316         --              po_header_id is inserted as null
4317   -- bug#3853109: Added the column names in the insert clause as per the review comments
4318         insert into  PO_COMMUNICATION_GT(po_header_id, po_release_id, revision_number, format_mask)
4319                             values(null, p_document_id, p_revision_num, PO_COMMUNICATION_PVT.getFormatMask);
4320 END IF;
4321 
4322 EXCEPTION
4323   WHEN OTHERS then
4324    --Bug5983107  po_communication_pvt.g_buyer_org := null;
4325     l_supp_org := null;
4326     l_po_number := null;
4327     l_change_summary := null;
4328     l_vendor_id := null;
4329 END;
4330 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4331   /*
4332   To find the version of the database. If the db version is >8 AND <9
4333   XMLQUERY is used to generate the XML AND IF the version is 9 XMLGEN is used.
4334 */
4335 
4336 
4337 
4338 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||'POXMLGEN', 'Executing DB Version');
4339 END IF;
4340 
4341 DBMS_UTILITY.DB_VERSION(l_version, l_compatibility);
4342 l_majorVersion := to_number(substr(l_version, 1, instr(l_version,'.')-1));
4343 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4344   FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name , 'DB Version'||l_majorVersion);
4345 END IF;
4346 
4347 /*Bug 6692126 Call the procedure gettandc which in turn calls
4348 the procedure get_preparer_profile which gets the profile option values
4349 in preparers context */
4350 /* get terms and conditions message*/
4351 /*IF FND_PROFILE.VALUE('PO_EMAIL_TERMS_FILE_NAME') IS NOT NULL THEN
4352    PO_XML_UTILS_GRP.getTandC(fnd_global.user_id(), fnd_global.resp_id(), fnd_global.resp_appl_id(), l_fileClob );
4353   IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4354     FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name , 'After getting the fileClob');
4355   END IF;
4356 END IF; */
4357 
4358 PO_XML_UTILS_GRP.getTandC(p_document_id, p_document_type,l_fileClob);  --Bug 6692126
4359 if (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) then
4360 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name , 'After getting the fileClob');
4361 end if;
4362 
4363 -- <Start Word Integration 11.5.10+>
4364 l_okc_doc_type := PO_CONTERMS_UTL_GRP.get_po_contract_doctype(p_document_subtype);
4365 
4366 IF ( ('STRUCTURED' =
4367           OKC_TERMS_UTIL_GRP.get_contract_source_code(p_document_id => p_document_id
4368                                                     , p_document_type => l_okc_doc_type))
4369        OR
4370      ('Y' =
4371           OKC_TERMS_UTIL_GRP.is_primary_terms_doc_mergeable(p_document_id => p_document_id
4372                                                           , p_document_type => l_okc_doc_type))
4373    )
4374 THEN
4375 
4376   -- contract terms are structured and/or mergeable
4377   -- so, show old cover page message (with no mention to look elsewhere for terms)
4378 
4379   /*Get the messages in covering page by replacing the tokens with correponding value.*/
4380   FND_MESSAGE.SET_NAME('PO','PO_FO_COVERING_MESSAGE');
4381   FND_MESSAGE.SET_TOKEN('BUYER_ORG',po_communication_pvt.g_buyer_org);
4382   FND_MESSAGE.SET_TOKEN('SUPP_ORG',l_supp_org);
4383   PO_COMMUNICATION_PVT.g_cover_message := FND_MESSAGE.GET;
4384 
4385 
4386   FND_MESSAGE.SET_NAME('PO','PO_FO_AMENDMENT_MESSAGE');
4387   FND_MESSAGE.SET_TOKEN('PO_NUM',l_po_number);
4388   FND_MESSAGE.SET_TOKEN('CHANGE_SUMMARY',l_change_summary);
4389   PO_COMMUNICATION_PVT.g_amendment_message	:= FND_MESSAGE.GET;
4390 
4391   g_is_contract_attached_doc := 'N';  -- bug4026592
4392 
4393 ELSIF (-1 <> OKC_TERMS_UTIL_GRP.get_primary_terms_doc_file_id(p_document_id => p_document_id
4394                                                             , p_document_type => l_okc_doc_type))
4395 THEN
4396 
4397   -- Primary document exists, but is not mergeable
4398 
4399   /*Get the messages in covering page by replacing the tokens with correponding value.*/
4400   FND_MESSAGE.SET_NAME('PO','PO_FO_COVER_MSG_NOT_MERGED');
4401   FND_MESSAGE.SET_TOKEN('BUYER_ORG',po_communication_pvt.g_buyer_org);
4402   FND_MESSAGE.SET_TOKEN('SUPP_ORG',l_supp_org);
4403   PO_COMMUNICATION_PVT.g_cover_message := FND_MESSAGE.GET;
4404 
4405 
4406   FND_MESSAGE.SET_NAME('PO','PO_FO_AMEND_MSG_NOT_MERGED');
4407   FND_MESSAGE.SET_TOKEN('PO_NUM',l_po_number);
4408   FND_MESSAGE.SET_TOKEN('CHANGE_SUMMARY',l_change_summary);
4409   PO_COMMUNICATION_PVT.g_amendment_message	:= FND_MESSAGE.GET;
4410 
4411   g_is_contract_attached_doc := 'Y'; -- bug4026592
4412 
4413 ELSE
4414 
4415   -- Primary attached document does not exist!
4416   -- Bug 4014230: Get buyer and supplier org tokens
4417 
4418   FND_MESSAGE.SET_NAME('PO','PO_FO_MSG_TERMS_DOC_MISSING');
4419   FND_MESSAGE.SET_TOKEN('BUYER_ORG',po_communication_pvt.g_buyer_org);
4420   FND_MESSAGE.SET_TOKEN('SUPP_ORG',l_supp_org);
4421   PO_COMMUNICATION_PVT.g_cover_message := FND_MESSAGE.GET;
4422 
4423   FND_MESSAGE.SET_NAME('PO','PO_FO_MSG_TERMS_DOC_MISSING');
4424   FND_MESSAGE.SET_TOKEN('BUYER_ORG',po_communication_pvt.g_buyer_org);
4425   FND_MESSAGE.SET_TOKEN('SUPP_ORG',l_supp_org);
4426   PO_COMMUNICATION_PVT.g_amendment_message := FND_MESSAGE.GET;
4427 
4428   g_is_contract_attached_doc := 'Y'; -- bug4026592
4429 
4430 END IF;
4431 -- <End Word Integration 11.5.10+>
4432 
4433 --Bug 6692126 Get the profile value based on prepares context
4434 --Bug 6692126 IF fnd_profile.value('ENABLE_TIMEZONE_CONVERSIONS')='Y' THEN
4435 
4436 IF get_preparer_profile(p_document_id,p_document_type,'ENABLE_TIMEZONE_CONVERSIONS') = 'Y' THEN  --Bug 6692126
4437   BEGIN
4438      --Bug 6692126 Get the profile value based on prepares context
4439      --Bug 6692126 SELECT fnd_profile.value('SERVER_TIMEZONE_ID') into l_timezone_id from dual;
4440      l_timezone_id   := get_preparer_profile(p_document_id,p_document_type,'SERVER_TIMEZONE_ID'); --Bug 6692126
4441      SELECT name, gmt_deviation_hours into l_timezone, l_offset from HZ_TIMEZONES_VL where timezone_id=to_number(l_timezone_id);
4442   EXCEPTION
4443     WHEN OTHERS THEN
4444       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4445         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||'Timezone: ','Inside Timezone Exception Handler');
4446       END IF;
4447       RAISE;
4448   END;
4449   FND_MESSAGE.SET_NAME('PO','PO_FO_TIMEZONE');
4450   FND_MESSAGE.SET_TOKEN('TIME_OFFSET',l_offset);
4451   FND_MESSAGE.SET_TOKEN('TIMEZONE_NAME',l_timezone);
4452   PO_COMMUNICATION_PVT.g_timezone :=FND_MESSAGE.GET;
4453 END IF;
4454 
4455 PO_COMMUNICATION_PVT.g_vendor_id := l_vendor_id;
4456 
4457 /*bug#3630737.
4458 Retrieve PO_FO_DOCUMENT_NAME from fnd_new_messages by passing
4459 DocumentType, po number and revision number as tokens*/
4460 FND_MESSAGE.SET_NAME('PO','PO_FO_DOCUMENT_NAME');
4461 FND_MESSAGE.SET_TOKEN('DOCUMENT_TYPE',PO_COMMUNICATION_PVT.g_documentType);
4462 FND_MESSAGE.SET_TOKEN('PO_NUMBER',l_po_number);
4463 FND_MESSAGE.SET_TOKEN('REVISION_NUMBER',PO_COMMUNICATION_PVT.g_revision_num);
4464 PO_COMMUNICATION_PVT.g_documentName := FND_MESSAGE.GET;
4465 
4466 -- SQl What:  Querying for header short attachments
4467 -- SQL why: To display note to supplier data in header part of pdf document.
4468 -- SQL Join:  vendor_id and header_id
4469 
4470 --bug#3760632 replaced the function PO_POXPOEPO
4471 --with PO_PRINTPO
4472 
4473 --bug#3768142 added the condtion if p_document_type='RELEASE'
4474 --so that the attachments for Releases are also displayed
4475 --correctly. An order by is used so that first the
4476 --PO_HEADERS(BPA) attachments are printed followed by PO_RELEASES
4477 --attachments and then finally PO_VENDORS. This is necessary
4478 --only for the Releases because you can display the BPA header
4479 --attachments also with a release.
4480 
4481 if(p_document_type='RELEASE')then
4482 --Bug#4683170
4483 --Appended fad.datatype_id=1 condition for retrieving the
4484 --short_text attachment for the current document only.
4485 --bug6133951 added seq_num in order_by clause.
4486 l_head_short_attachment_query := 'CURSOR( SELECT fds.short_text
4487 				  FROM
4488 					fnd_attached_docs_form_vl fad,
4489 					fnd_documents_short_text fds
4490 				 WHERE  ((entity_name=''PO_HEADERS'' AND
4491 					  pk1_value=to_char(phx.po_header_id))OR
4492 					(entity_name = ''PO_RELEASES'' AND
4493 					pk1_value = to_char(phx.po_release_id)) OR
4494 					(entity_name = ''PO_VENDORS'' AND
4495 				        pk1_value = to_char(phx.vendor_id))  OR
4496                                         (entity_name = ''PO_VENDOR_SITES'' AND
4497                                         pk1_value = to_char(phx.vendor_site_id))) AND  -- bug6154354
4498 				        function_name = ''PO_PRINTPO''
4499 				        AND fad.media_id = fds.media_id
4500 					AND fad.datatype_id=1
4501 					order by entity_name, seq_num) AS header_short_text'; --bug6133951
4502     --<PO Attachment Support 11i.11 Start>
4503     l_head_url_attachment_query := 'CURSOR(
4504         SELECT fad.url web_page --Bug#4958642
4505         FROM fnd_attached_docs_form_vl fad
4506         WHERE ((fad.entity_name=''PO_HEADERS'' AND fad.pk1_value=to_char(phx.po_header_id)) OR
4507                (fad.entity_name=''PO_RELEASES'' AND fad.pk1_value=to_char(phx.po_release_id)) OR
4508                (fad.entity_name=''PO_VENDORS'' AND fad.pk1_value=to_char(phx.vendor_id)) OR
4509                (fad.entity_name=''PO_VENDOR_SITES'' AND fad.pk1_value=to_char(phx.vendor_site_id)) --bug6154354
4510               )
4511         AND fad.datatype_id=5
4512         AND fad.function_name=''PO_PRINTPO''
4513         order by fad.entity_name,fad.seq_num) AS header_url_attachments'; --bug6133951
4514 
4515     l_head_file_attachment_query := 'CURSOR(
4516         SELECT fad.file_name
4517         FROM fnd_attached_docs_form_vl fad
4518         WHERE ((fad.entity_name=''PO_HEADERS'' AND fad.pk1_value=to_char(phx.po_header_id)) OR
4519                (fad.entity_name=''PO_RELEASES'' AND fad.pk1_value=to_char(phx.po_release_id)) OR
4520                (fad.entity_name=''PO_VENDORS'' AND fad.pk1_value=to_char(phx.vendor_id)) OR
4521                (fad.entity_name=''PO_VENDOR_SITES'' AND fad.pk1_value=to_char(phx.vendor_site_id))  --bug6154354
4522               )
4523         AND fad.datatype_id=6
4524         AND fad.function_name=''PO_PRINTPO''
4525         order by fad.entity_name,fad.seq_num) AS header_file_attachments';  --bug6133951
4526     --<PO Attachment Support 11i.11 End>
4527 
4528 else
4529 --Bug#4683170
4530 --Appended fad.datatype_id=1 condition for retrieving the
4531 --short_text attachment for the current document only.
4532 --bug6133951 added seq_num in order_by clause.
4533 l_head_short_attachment_query := 'CURSOR( SELECT fds.short_text
4534 				  FROM
4535 					fnd_attached_docs_form_vl fad,
4536 					fnd_documents_short_text fds
4537 				 WHERE  ((entity_name = ''PO_HEADERS'' AND
4538 					pk1_value = to_char(phx.po_header_id)) OR
4539 					(entity_name = ''PO_VENDORS'' AND
4540 				        pk1_value = to_char(phx.vendor_id))  OR
4541                                         (entity_name = ''PO_VENDOR_SITES'' AND
4542                                         pk1_value = to_char(phx.vendor_site_id))) AND  -- bug6154354
4543 				        function_name = ''PO_PRINTPO''
4544 				        AND fad.media_id = fds.media_id
4545 					AND fad.datatype_id=1 order by fad.seq_num) AS header_short_text'; --bug6133951
4546 
4547     --<PO Attachment Support 11i.11 Start>
4548     l_head_url_attachment_query := 'CURSOR(
4549         SELECT fad.url web_page --Bug#4958642
4550         FROM fnd_attached_docs_form_vl fad
4551         WHERE ((fad.entity_name=''PO_HEADERS'' AND fad.pk1_value=to_char(phx.po_header_id)) OR
4552                (fad.entity_name=''PO_VENDORS'' AND fad.pk1_value=to_char(phx.vendor_id)) OR
4553                (fad.entity_name=''PO_VENDOR_SITES'' AND fad.pk1_value=to_char(phx.vendor_site_id))  --bug6154354
4554               )
4555         AND fad.datatype_id=5
4556         AND fad.function_name=''PO_PRINTPO''
4557         order by fad.entity_name,fad.seq_num) AS header_url_attachments';  --bug6133951
4558 
4559     l_head_file_attachment_query := 'CURSOR(
4560         SELECT fad.file_name
4561         FROM fnd_attached_docs_form_vl fad
4562         WHERE ((fad.entity_name=''PO_HEADERS'' AND fad.pk1_value=to_char(phx.po_header_id)) OR
4563                (fad.entity_name=''PO_VENDORS'' AND fad.pk1_value=to_char(phx.vendor_id)) OR
4564                (fad.entity_name=''PO_VENDOR_SITES'' AND fad.pk1_value=to_char(phx.vendor_site_id))  --bug6154354
4565               )
4566         AND fad.datatype_id=6
4567         AND fad.function_name=''PO_PRINTPO''
4568         order by fad.entity_name,fad.seq_num) AS header_file_attachments';   --bug6133951
4569     --<PO Attachment Support 11i.11 End>
4570 
4571 end if;
4572 --bug3768142 end
4573 
4574 -- SQl What:  Querying for line short attachments
4575 -- SQL why: To display note to supplier data at line level in pdf document.
4576 -- SQL Join:  vendor_id and header_id
4577 
4578 
4579 --bug#3760632 replaced the function PO_POXPOEPO
4580 --with PO_PRINTPO
4581 	--Bug#4683170
4582 	--Appended fad.datatype_id=1 condition for retrieving the
4583 	--short_text attachment for the current document only.
4584 	--Bug 4673653 - Added condition to show item level short text attachments
4585 	l_line_short_attachment_query := ' CURSOR( SELECT plx.po_line_id , fds.short_text
4586 	 FROM
4587 		fnd_attached_docs_form_vl fad,
4588 		fnd_documents_short_text fds
4589 	 WHERE ((fad.entity_name=''PO_LINES'' AND fad.pk1_value=to_char(plx.po_line_id))
4590 	         OR
4591 	         (fad.entity_name=''MTL_SYSTEM_ITEMS'' AND
4592 	          fad.pk1_value=to_char(PO_COMMUNICATION_PVT.getInventoryOrgId()) AND --Bug6139548
4593 	          fad.pk2_value=to_char(plx.item_id) AND plx.item_id is not null)
4594 	       ) AND
4595 	       function_name = ''PO_PRINTPO''
4596 	       AND fad.media_id = fds.media_id
4597    	       AND fad.datatype_id=1 order by fad.seq_num) AS line_short_text';  --bug6133951
4598 
4599     --<PO Attachment Support 11i.11 Start>
4600     --Bug 4673653 - Use inventory org id instead of Org Id to get the item attachments
4601     l_line_url_attachment_query := 'CURSOR(
4602         SELECT fad.url web_page --Bug#4958642
4603         FROM fnd_attached_docs_form_vl fad
4604         WHERE ((fad.entity_name=''PO_LINES'' AND fad.pk1_value=to_char(plx.po_line_id))
4605                OR
4606                (fad.entity_name=''PO_HEADERS'' AND fad.pk1_value=to_char(plx.from_header_id)
4607                 AND plx.from_header_id IS NOT NULL)
4608                OR
4609                (fad.entity_name=''PO_LINES'' AND fad.pk1_value=to_char(plx.from_line_id)
4610                 AND plx.from_line_id IS NOT NULL)
4611                OR
4612                (fad.entity_name=''MTL_SYSTEM_ITEMS'' AND
4613                 fad.pk1_value=to_char(PO_COMMUNICATION_PVT.getInventoryOrgId()) AND
4614                 fad.pk2_value=to_char(plx.item_id) AND plx.item_id is not null)
4615               )
4616         AND fad.datatype_id=5
4617         AND fad.function_name=''PO_PRINTPO'' order by fad.seq_num) AS line_url_attachments'; --bug6133951
4618 
4619     --Bug 4673653 - Use inventory org id instead of Org Id to get the item attachments
4620     l_line_file_attachment_query := 'CURSOR(
4621         SELECT fad.file_name
4622         FROM fnd_attached_docs_form_vl fad
4623         WHERE ((fad.entity_name=''PO_LINES'' AND fad.pk1_value=to_char(plx.po_line_id))
4624                OR
4625                (fad.entity_name=''PO_HEADERS'' AND fad.pk1_value=to_char(plx.from_header_id)
4626                 AND plx.from_header_id IS NOT NULL)
4627                OR
4628                (fad.entity_name=''PO_LINES'' AND fad.pk1_value=to_char(plx.from_line_id)
4629                 AND plx.from_line_id IS NOT NULL)
4630                OR
4631                (fad.entity_name=''MTL_SYSTEM_ITEMS'' AND
4632                 fad.pk1_value=to_char(PO_COMMUNICATION_PVT.getInventoryOrgId()) AND
4633                 fad.pk2_value=to_char(plx.item_id) AND plx.item_id is not null)
4634               )
4635         AND fad.datatype_id=6
4636         AND fad.function_name=''PO_PRINTPO''
4637         order by fad.seq_num) AS line_file_attachments'; --bug6133951
4638     --<PO Attachment Support 11i.11 End>
4639 
4640 -- SQl What:  Querying for shipment short attachments
4641 -- SQL why: To display note to supplier data at shipmentlevel in pdf document.
4642 -- SQL Join:  vendor_id and header_id
4643 
4644 --bug#3760632 replaced the function PO_POXPOEPO
4645 --with PO_PRINTPO
4646 --Bug#4683170
4647 	--Appended fad.datatype_id=1 condition for retrieving the
4648         --short_text attachment for the current document only
4649 	 l_shipment_short_attach_query := 'CURSOR( SELECT pllx.line_location_id, fds.short_text
4650 	 FROM
4651 		fnd_attached_docs_form_vl fad,
4652 		fnd_documents_short_text fds
4653 	 WHERE entity_name = ''PO_SHIPMENTS'' AND
4654 		 pk1_value = to_char(pllx.line_location_id) AND
4655 	       function_name = ''PO_PRINTPO''
4656 	       AND fad.media_id = fds.media_id
4657   	       AND fad.datatype_id=1
4658                order by fad.seq_num) AS line_loc_short_text';  --bug6133951
4659 
4660 --<PO Attachment Support 11i.11 Start>
4661     l_shipment_url_attach_query := 'CURSOR(
4662         SELECT fad.url web_page --Bug#4958642
4663         FROM fnd_attached_docs_form_vl fad
4664         WHERE fad.entity_name=''PO_SHIPMENTS''
4665         AND fad.pk1_value=to_char(pllx.line_location_id)
4666         AND fad.datatype_id=5
4667         AND fad.function_name=''PO_PRINTPO''
4668         order by fad.seq_num ) AS line_loc_url_attachments';  --bug6133951
4669 
4670     l_shipment_file_attach_query := 'CURSOR(
4671         SELECT fad.file_name
4672         FROM fnd_attached_docs_form_vl fad
4673         WHERE fad.entity_name=''PO_SHIPMENTS''
4674         AND fad.pk1_value=to_char(pllx.line_location_id)
4675         AND fad.datatype_id=6
4676         AND fad.function_name=''PO_PRINTPO''
4677         order by fad.seq_num) AS line_loc_file_attachments';  --bug6133951
4678 --<PO Attachment Support 11i.11 End>
4679 
4680 
4681 -- SQl What:  Querying for boiler plate text
4682 -- SQL why: To display boiler plate text in pdf document.
4683 -- SQL Join:
4684 -- Change: Commented some message tokens and added new message tokens as part of new layout changes.
4685 
4686 -- Bug#3823799: removed the inline comments and placed here, other wise XML generation failing using dbms_xmlquery.
4687 --Bug 3670603: Added "PO_WF_NOTIF_PROMISED_DATE" message name
4688 --Bug3670603: Added "PO_WF_NOTIF_NEEDBY_DATE" message name
4689 --Bug3836856: Addded "PO_FO_PAGE"for retrieving the page number message
4690 
4691 l_xml_message_query :='CURSOR (SELECT message_name message, message_text text FROM fnd_new_messages WHERE message_name in (
4692 ''PO_WF_NOTIF_REVISION'',
4693 ''PO_WF_NOTIF_VENDOR_NO'',
4694 ''PO_WF_NOTIF_PAYMENT_TERMS'',
4695 ''PO_WF_NOTIF_FREIGHT_TERMS'',
4696 ''PO_WF_NOTIF_FOB'',
4697 ''PO_WF_NOTIF_SHIP_VIA'',
4698 ''PO_WF_NOTIF_CONFIRM_TO_TELE'',
4699 ''PO_WF_NOTIF_REQUESTER_DELIVER'',
4700 ''PO_WF_NOTIF_DESCRIPTION'',
4701 ''PO_WF_NOTIF_TAX'',
4702 ''PO_WF_NOTIF_UOM'',
4703 ''PO_WF_NOTIF_UNIT_PRICE'',
4704 ''PO_WF_NOTIF_QUANTITY'',
4705 ''PO_WF_NOTIF_PURCHASE_ORDER'',
4706 ''PO_WF_NOTIF_BUYER'',
4707 ''PO_WF_NOTIF_AMOUNT'',
4708 ''PO_WF_NOTIF_EFFECTIVE_DATE'',
4709 ''PO_WF_NOTIF_HEADER_NOTE'',
4710 ''PO_WF_NOTIF_LINE_NUMBER'',
4711 ''PO_WF_NOTIF_LINE_PAYITEM_NUM'',  -- <Complex Work R12>
4712 ''PO_WF_NOTIF_MULTIPLE'',
4713 ''PO_WF_NOTIF_PART_NO_DESC'',
4714 ''PO_WF_NOTIF_SUPPLIER_ITEM'',
4715 ''PO_WF_NOTIF_TOTAL'',
4716 ''PO_WF_NOTIF_NOTE'',
4717 ''PO_FO_PACKING_INSTRUCTION'',
4718 ''PO_FO_CUST_PO_NUMBER'',
4719 ''PO_FO_CUST_ITEM_DESC'',
4720 ''PO_FO_LINE_NUMBER'',
4721 ''PO_FO_SHIP_NUMBER'',
4722 ''PO_FO_AMOUNT_BASED'',
4723 ''PO_FO_CONTRACTOR_NAME'',
4724 ''PO_FO_START_DATE'',
4725 ''PO_FO_END_DATE'',
4726 ''PO_FO_WORK_SCHEDULE'',
4727 ''PO_FO_SHIFT_PATTERN'',
4728 ''PO_FO_PRICE_DIFFERENTIALS'',
4729 ''PO_FO_DELIVER_TO_LOCATION'',
4730 ''PO_FO_EFFECTIVE_START_DATE'',
4731 ''PO_FO_AMOUNT_AGREED'',
4732 ''PO_FO_ADVANCE'',                  -- <Complex Work R12>
4733 ''PO_FO_RETAINAGE_RATE'',           -- <Complex Work R12>
4734 ''PO_FO_MAX_RETAINAGE_AMOUNT'',     -- <Complex Work R12>
4735 ''PO_FO_PROGRESS_PAYMENT_RATE'',    -- <Complex Work R12>
4736 ''PO_FO_RECOUPMENT_RATE'',          -- <Complex Work R12>
4737 ''PO_FO_PRICE_BREAK'',
4738 ''PO_FO_CHARGE_ACCOUNT'',
4739 ''PO_FO_CONTRACTOR'',
4740 ''PO_FO_CONTACT_NAME'',
4741 ''PO_FO_TELEPHONE'',
4742 ''PO_FO_FAX'',
4743 ''PO_FO_NAME'',
4744 ''PO_FO_TITLE'',
4745 ''PO_FO_DATE'',
4746 ''PO_FO_REVISION'',
4747 ''PO_FO_AMENDMENT'',
4748 ''PO_FO_SHIP_METHOD'',
4749 ''PO_FO_SHIPPING_INSTRUCTION'',
4750 ''PO_FO_DRAFT'',
4751 ''PO_FO_PROPRIETARY_INFORMATION'',
4752 ''PO_FO_TRANSPORTAION_ARRANGED'',
4753 ''PO_FO_DELIVER_TO_LOCATION'',
4754 ''PO_FO_NO'',
4755 ''PO_FO_COMPANY'',
4756 ''PO_FO_SUBMIT_RESPONSE'',
4757 ''PO_FO_EMAIL'',
4758 ''PO_WF_NOTIF_EXPIRES_ON'',
4759 ''PO_FO_TEST'',
4760 ''PO_FO_ORG_AGR_ASS'',
4761 ''PO_FO_EFFECTIVE_END_DATE'',
4762 ''PO_FO_PURCHASING_ORGANIZATION'',
4763 ''PO_FO_PURCHASING_SUPPLIER_SITE'',
4764 ''PO_FO_TRANSPORTATION_ARRANGED'',
4765 ''PO_WF_NOTIF_ADDRESS'',
4766 ''PO_WF_NOTIF_ORDER'',
4767 ''PO_WF_NOTIF_ORDER_DATE'',
4768 ''PO_FO_VENDOR'',
4769 ''PO_FO_SHIP_TO'',
4770 ''PO_FO_BILL_TO'',
4771 ''PO_FO_CONFIRM_NOT_DUPLICATE'',
4772 ''PO_FO_AGREEMENT_CANCELED'',
4773 ''PO_FO_FORMAL_ACCEPT'',
4774 ''PO_FO_TYPE'',
4775 ''PO_FO_REVISION_DATE'',
4776 ''PO_FO_REVISED_BY'',
4777 ''PO_FO_PRICES_EXPRESSED'',
4778 ''PO_FO_NOTES'',
4779 ''PO_WF_NOTIF_PREPARER'',
4780 ''PO_FO_SUPPLIER_CONFIGURATION'',
4781 ''PO_FO_DELIVER_DATE_TIME'',
4782 ''PO_FO_LINE_REF_BPA'',
4783 ''PO_FO_LINE_REF_CONTRACT'',
4784 ''PO_FO_LINE_SUPPLIER_QUOTATION'',
4785 ''PO_FO_USE_SHIP_ADDRESS_TOP'',
4786 ''PO_FO_LINE_CANCELED'',
4787 ''PO_FO_ORIGINAL_QTY_ORDERED'',
4788 ''PO_FO_QUANTITY_CANCELED'',
4789 ''PO_FO_SHIPMENT_CANCELED'',
4790 ''PO_FO_ORIGINAL_SHIPMENT_QTY'',
4791 ''PO_FO_CUSTOMER_ACCOUNT_NO'',
4792 ''PO_FO_RELEASE_CANCELED'',
4793 ''PO_FO_PO_CANCELED'',
4794 ''PO_FO_TOTAL'',
4795 ''PO_FO_SUPPLIER_ITEM'',
4796 ''PO_FO_ORIGINAL_AMOUNT_ORDERED'',
4797 ''PO_FO_AMOUNT_CANCELED'',
4798 ''PO_FO_UN_NUMBER'',
4799 ''PO_WF_NOTIF_PROMISED_DATE'',
4800 ''PO_WF_NOTIF_NEEDBY_DATE'',
4801 ''PO_FO_HAZARD_CLASS'',
4802 ''PO_FO_PAGE'',
4803 ''PO_FO_REFERENCE_DOCUMENTS'',
4804 ''PO_FO_PAYITEM_CANCELED'', --<Bug#: 4899200>
4805 ''PO_FO_ORIGINAL_PAYITEM_QTY'', --<Bug#: 4899200>
4806 ''PO_FO_PAYITEM_QTY_CANCELED'', --<Bug#: 4899200>
4807 ''PO_FO_ORIGINAL_PAYITEM_AMT'', --<Bug#: 5464968>
4808 ''PO_FO_PAYITEM_AMT_CANCELED'' --<Bug#: 5464968>
4809 ) AND application_id = 201 AND language_code = '''||userenv('LANG')||''') AS message';
4810 
4811 
4812 -- <Complex Work R12 Start>
4813 -- Set complex work global and query strings
4814 
4815 IF (p_document_type = 'PO') THEN
4816 
4817   setIsComplexWorkPO(
4818     p_document_id  => p_document_id
4819   , p_revision_num => p_revision_num
4820   , p_which_tables => p_which_tables
4821   );
4822 
4823 ELSE
4824 
4825   g_is_complex_work_po := 'N';
4826 
4827 END IF;
4828 
4829 IF (g_is_complex_work_po = 'Y') THEN
4830 
4831   -- set up dynamic SQL query strings for complex work
4832 
4833   IF (p_which_tables = 'MAIN') THEN
4834 
4835     l_adv_amount_query :=
4836       'PO_COMPLEX_WORK_PVT.get_advance_amount(plx.po_line_id) advance_amount, ';
4837 
4838     l_complex_lloc_query :=
4839        ', CURSOR(SELECT del.* FROM po_line_locations_xml del'
4840     || ' WHERE del.po_line_id = plx.po_line_id AND del.payment_type = ''DELIVERY'')'
4841     || ' AS line_delivery ';
4842 
4843     l_complex_dist_query :=
4844        ', CURSOR(SELECT adv.* FROM po_distribution_xml adv, po_line_locations_xml pllx2'
4845     || ' WHERE pllx2.po_line_id = plx.po_line_id AND pllx2.payment_type = ''ADVANCE'''
4846     || ' AND adv.line_location_id = pllx2.line_location_id) AS line_advance_distributions,'
4847     || ' CURSOR(SELECT del.*, CURSOR(SELECT deldist.* FROM po_distribution_xml deldist'
4848     || ' WHERE deldist.line_location_id = del.line_location_id) AS distributions'
4849     || ' FROM po_line_locations_xml del WHERE del.po_line_id = plx.po_line_id'
4850     || ' AND del.payment_type = ''DELIVERY'') AS line_delivery ';
4851 
4852 
4853   ELSIF (p_which_tables = 'ARCHIVE') THEN
4854 
4855     l_adv_amount_query :=
4856        'PO_COMPLEX_WORK_PVT.get_advance_amount(plx.po_line_id, pcgt.revision_num,'
4857     || '''ARCHIVE'') advance_amount, ';
4858 
4859 
4860     l_complex_lloc_query :=
4861        ', CURSOR(SELECT del.* FROM po_line_locations_archive_xml del'
4862     || ' WHERE del.po_line_id = plx.po_line_id AND del.payment_type = ''DELIVERY'''
4863     || ' AND del.revision_num = (SELECT max(dela.revision_num)'
4864     || ' FROM po_line_locations_archive_all dela WHERE del.line_location_id = '
4865     || ' dela.line_location_id AND del.revision_num <= pcgt.revision_number) '
4866     || ' ) AS line_delivery ';
4867 
4868 
4869 
4870     l_complex_dist_query :=
4871        ', CURSOR(SELECT adv.* FROM po_distribution_archive_xml adv,'
4872     || ' po_line_locations_archive_xml pllx2 WHERE pllx2.po_line_id = plx.po_line_id'
4873     || ' AND pllx2.payment_type = ''ADVANCE'' AND adv.line_location_id = pllx2.line_location_id'
4874     || ' AND adv.revision_num = (SELECT max(adva.revision_num)'
4875     || ' FROM po_distributions_archive_all adva WHERE adv.po_distribution_id ='
4876     || ' adv.po_distribution_id AND adva.revision_num <= pcgt.revision_number))'
4877     || ' AS line_advance_distributions, CURSOR(SELECT del.*, CURSOR(SELECT deldist.*'
4878     || ' FROM po_distribution_xml deldist WHERE deldist.line_location_id = del.line_location_id'
4879     || ' AND deldist.revision_num = (SELECT max(deldista.revision_num)'
4880     || ' FROM po_distributions_archive_all deldista WHERE deldist.po_distribution_id ='
4881     || ' deldista.po_distribution_id AND deldista.revision_num <= pcgt.revision_number))'
4882     || ' AS distributions FROM po_line_locations_xml del WHERE del.po_line_id = plx.po_line_id'
4883     || ' AND del.payment_type = ''DELIVERY'' AND del.revision_num = ('
4884     || ' SELECT max(dela.revision_num) FROM po_line_locations_archive_all dela'
4885     || ' WHERE del.line_location_id = dela.line_location_id'
4886     || ' AND del.revision_num <= pcgt.revision_number)) AS line_delivery';
4887 
4888 
4889 
4890     END IF;  -- if p_which_tables = ...
4891 
4892   END IF;  -- if g_is_complex_work_po
4893 
4894 
4895 
4896 -- <Complex Work R12 End>
4897 
4898 
4899 /*
4900   These are the queries used to get purchasing organization and purchasing supplier details for main
4901   and archive tables.
4902 */
4903 l_agreement_assign_query := ' CURSOR( select rownum, PO_COMMUNICATION_PVT.GETOPERATIONINFO(PGA.PURCHASING_ORG_ID) OU_NAME,
4904       PO_COMMUNICATION_PVT.getVendorAddressLine1(PGA.vendor_site_id) VENDOR_ADDRESS_LINE1,
4905       PO_COMMUNICATION_PVT.getVendorAddressLine2() VENDOR_ADDRESS_LINE2,
4906       PO_COMMUNICATION_PVT.getVendorAddressLine3() VENDOR_ADDRESS_LINE3,
4907       PO_COMMUNICATION_PVT.getVendorCityStateZipInfo() VENDOR_CITY_STATE_ZIP,
4908       PO_COMMUNICATION_PVT.getVendorCountry() VENDOR_COUNTRY
4909       FROM po_ga_org_assignments PGA
4910       WHERE PGA.ENABLED_FLAG = ''Y'' and PGA.PO_HEADER_ID = PHX.PO_HEADER_ID) as organization_details ';
4911 
4912 l_arc_agreement_assign_query := ' CURSOR( select rownum, PO_COMMUNICATION_PVT.GETOPERATIONINFO(PGA.PURCHASING_ORG_ID) OU_NAME,
4913       PO_COMMUNICATION_PVT.getVendorAddressLine1(PGA.vendor_site_id) VENDOR_ADDRESS_LINE1,
4914       PO_COMMUNICATION_PVT.getVendorAddressLine2() VENDOR_ADDRESS_LINE2,
4915       PO_COMMUNICATION_PVT.getVendorAddressLine3() VENDOR_ADDRESS_LINE3,
4916       PO_COMMUNICATION_PVT.getVendorCityStateZipInfo() VENDOR_CITY_STATE_ZIP,
4917       PO_COMMUNICATION_PVT.getVendorCountry() VENDOR_COUNTRY
4918       FROM po_ga_org_assignments_archive PGA
4919       WHERE PGA.ENABLED_FLAG = ''Y'' and PGA.PO_HEADER_ID = PHX.PO_HEADER_ID) as organization_details ';
4920 
4921 -- SQl What:  Query for header, line, locations, line locations, shipments and distribution information based on
4922 --    document and document type
4923 -- SQL why: To get xml which is used to generate the pdf document.
4924 -- SQL Join:
4925 /* Logic for framing the query:-
4926 
4927   1. If the document type is PO or PA then query has to be join with headers else with Releases.
4928 */
4929 
4930 /*Bug#3583910 Added the function getWithTerms() in the below sql strings such
4931 that the generated xml document will have the value */
4932 
4933 -- bug4026592: Added the function call getIsContermsAttachedDoc()
4934 -- to the xml sql strings below
4935 
4936 /*bug#3630737.
4937 Added the function getDocumentName which returns concatinated value of
4938 DocumentType, po number and revision number*/
4939 /*
4940    bug#3823799: Removed the join with pllx.po_line_id = plx.po_line_id as it appears twice.
4941    Removed the join condition of shipment header id with headers header id as there is a condition
4942    with lines.
4943 */
4944 
4945 -- <Complex Work R12>: Modify queries below to include complex work information
4946 
4947 IF(p_document_type in ('PO','PA')) THEN
4948   IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
4949     FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||'NON Release:','Entered into Non Release Query Loop');
4950   END IF;
4951 
4952   PO_COMMUNICATION_PVT.g_release_header_id := p_document_id; -- For documents other than Releases join is based on header id for getting the attachments.
4953   IF p_which_tables = 'MAIN' THEN
4954 
4955     l_xml_query := 'SELECT phx.*, PO_COMMUNICATION_PVT.getIsComplexWorkPO() is_complex_work_po, PO_COMMUNICATION_PVT.getDocumentType() document_type,
4956 	PO_COMMUNICATION_PVT.getCoverMessage() cover_message,PO_COMMUNICATION_PVT.getTimezone() timezone,
4957     PO_COMMUNICATION_PVT.getAmendmentMessage() ammendment_message,PO_COMMUNICATION_PVT.getTestFlag() test_flag,
4958     PO_COMMUNICATION_PVT.getDocumentName() document_name,
4959     PO_COMMUNICATION_PVT.IsDocumentSigned(PO_COMMUNICATION_PVT.getDocumentId()) Signed,
4960     fnd_profile.value(''PO_GENERATE_AMENDMENT_DOCS'') amendment_profile,
4961     PO_COMMUNICATION_PVT.getWithTerms() With_Terms , PO_COMMUNICATION_PVT.getIsContractAttachedDoc() Is_Attached_Doc , '|| l_xml_message_query || ','|| l_head_short_attachment_query||','||l_head_url_attachment_query||','||l_head_file_attachment_query||'
4962       FROM PO_HEADERS_XML phx WHERE phx.PO_HEADER_ID = PO_COMMUNICATION_PVT.getDocumentId() AND phx.revision_num = PO_COMMUNICATION_PVT.getRevisionNum()';
4963 
4964     IF(p_document_subtype <> 'CONTRACTS') THEN -- contracts will have only headers
4965 
4966       SELECT count(*) into l_count FROM po_lines_all  WHERE po_header_id = p_document_id;
4967 
4968       IF l_count >0 THEN
4969 
4970         /*
4971           for getting the price differentials FROM po_price_differentials_v
4972           the entity _name is 'PO LINE' except for blanket AND entity_name is 'BLANKET LINE'
4973           for blanket
4974         */
4975           --Bug 5506417: Added order by plx.line_num clause
4976           l_xml_query := 'SELECT phx.*, PO_COMMUNICATION_PVT.getIsComplexWorkPO() is_complex_work_po, PO_COMMUNICATION_PVT.getDocumentType() document_type, PO_COMMUNICATION_PVT.getCoverMessage() cover_message,
4977               PO_COMMUNICATION_PVT.getTimezone() timezone,PO_COMMUNICATION_PVT.getAmendmentMessage() ammendment_message,PO_COMMUNICATION_PVT.getTestFlag() test_flag,
4978               PO_COMMUNICATION_PVT.getDistinctShipmentCount() DIST_SHIPMENT_COUNT,
4979               PO_COMMUNICATION_PVT.getDocumentName() document_name,
4980               PO_COMMUNICATION_PVT.IsDocumentSigned(PO_COMMUNICATION_PVT.getDocumentId()) Signed,
4981               fnd_profile.value(''PO_GENERATE_AMENDMENT_DOCS'') amendment_profile,PO_COMMUNICATION_PVT.getWithTerms() With_Terms , PO_COMMUNICATION_PVT.getIsContractAttachedDoc() Is_Attached_Doc , '||
4982                             l_xml_message_query || ','|| l_head_short_attachment_query ||','||l_head_url_attachment_query||','||l_head_file_attachment_query||',
4983               CURSOR(SELECT plx.*, '||l_adv_amount_query||' CURSOR(SELECT  PRICE_TYPE_DSP PRICE_TYPE, MULTIPLIER,MIN_MULTIPLIER, MAX_MULTIPLIER FROM po_price_differentials_v
4984               WHERE entity_type='''||l_eventType||''' AND entity_id = plx.po_line_id and enabled_flag=''Y'') AS price_diff,
4985               '|| l_line_short_attachment_query ||','||l_line_url_attachment_query||','||l_line_file_attachment_query||' FROM po_lines_xml plx
4986               WHERE  plx.po_header_id = phx.po_header_id and not exists
4987               (select ''x'' from po_lines_archive_all  plaa where
4988                 plaa.po_line_id = plx.po_line_id and
4989                       plaa.cancel_flag= ''Y'' and plaa.revision_num< PO_COMMUNICATION_PVT.getRevisionNum() ) order by plx.line_num) AS lines
4990               FROM PO_HEADERS_XML phx WHERE phx.PO_HEADER_ID = PO_COMMUNICATION_PVT.getDocumentId() AND phx.revision_num = PO_COMMUNICATION_PVT.getRevisionNum()';
4991 
4992       END IF;
4993 
4994       SELECT count(*) into l_count FROM po_line_locations_all  WHERE po_header_id = p_document_id;
4995 
4996       IF l_count >0 THEN
4997 
4998         /*  Bug#3574748: Added the condition SHIPMENT_TYPE in ('BLANKET','STANDARD') in shipment query. */
4999         --Bug 5506417: Added order by pllx.shipment_num and order by plx.line_num clauses
5000         l_xml_query := 'SELECT phx.*, PO_COMMUNICATION_PVT.getIsComplexWorkPO() is_complex_work_po, PO_COMMUNICATION_PVT.getDocumentType() document_type, PO_COMMUNICATION_PVT.getCoverMessage() cover_message,
5001             PO_COMMUNICATION_PVT.getTimezone() timezone,PO_COMMUNICATION_PVT.getAmendmentMessage() ammendment_message,PO_COMMUNICATION_PVT.getTestFlag() test_flag,
5002             PO_COMMUNICATION_PVT.getDistinctShipmentCount() DIST_SHIPMENT_COUNT,
5003             PO_COMMUNICATION_PVT.getDocumentName() document_name,
5004             PO_COMMUNICATION_PVT.IsDocumentSigned( PO_COMMUNICATION_PVT.getDocumentId()) Signed,
5005             fnd_profile.value(''PO_GENERATE_AMENDMENT_DOCS'') amendment_profile, PO_COMMUNICATION_PVT.getWithTerms() With_Terms  , '||
5006         ' PO_COMMUNICATION_PVT.getIsContractAttachedDoc() Is_Attached_Doc , ' ||  l_xml_message_query || ','|| l_head_short_attachment_query ||','||l_head_url_attachment_query||','||l_head_file_attachment_query||',
5007             CURSOR(SELECT plx.*, '||l_adv_amount_query||' CURSOR(SELECT  PRICE_TYPE_DSP PRICE_TYPE, MULTIPLIER, MIN_MULTIPLIER,MAX_MULTIPLIER FROM po_price_differentials_v
5008               WHERE entity_type='''||l_eventType||''' AND entity_id = plx.po_line_id and enabled_flag=''Y'') AS price_diff,
5009               '|| l_line_short_attachment_query ||','||l_line_url_attachment_query||','||l_line_file_attachment_query||',
5010             CURSOR(SELECT pllx.*,';
5011             IF (p_document_subtype <> 'STANDARD') THEN
5012               l_xml_query := l_xml_query||'CURSOR(SELECT PRICE_TYPE_DSP PRICE_TYPE, MIN_MULTIPLIER,  MAX_MULTIPLIER FROM po_price_differentials_v
5013               WHERE entity_type=''PRICE BREAK'' AND entity_id = pllx.line_location_id and enabled_flag=''Y'') AS price_break,';
5014             END IF;
5015 
5016             l_xml_query := l_xml_query || l_shipment_short_attach_query ||','||l_shipment_url_attach_query||','||l_shipment_file_attach_query||'
5017             FROM po_line_locations_xml pllx WHERE pllx.po_line_id = plx.po_line_id and SHIPMENT_TYPE in (''BLANKET'',''STANDARD'',''PREPAYMENT'') AND NVL(pllx.payment_type,''NONE'') NOT IN (''ADVANCE'',''DELIVERY'') and not exists
5018             (select ''x'' from po_line_locations_archive_all pllaa where pllaa.line_location_id = pllx.line_location_id
5019             and pllaa.cancel_flag=''Y'' and pllaa.revision_num < PO_COMMUNICATION_PVT.getRevisionNum()) order by pllx.shipment_num ) AS line_locations' || l_complex_lloc_query || '
5020             FROM po_lines_xml plx
5021             WHERE  plx.po_header_id = phx.po_header_id and not exists
5022               (select ''x'' from po_lines_archive_all  plaa where
5023                 plaa.po_line_id = plx.po_line_id and
5024                       plaa.cancel_flag= ''Y'' and plaa.revision_num< PO_COMMUNICATION_PVT.getRevisionNum() ) order by plx.line_num) AS lines
5025             FROM PO_HEADERS_XML phx WHERE phx.PO_HEADER_ID = PO_COMMUNICATION_PVT.getDocumentId() AND phx.revision_num = PO_COMMUNICATION_PVT.getRevisionNum()';
5026       END IF;
5027 
5028       IF(p_document_subtype <> 'BLANKET') THEN -- blankets will not have distributions
5029         SELECT count(*) into l_count FROM po_distributions_all  WHERE po_header_id = p_document_id;
5030 
5031         IF l_count >0 THEN
5032           --Bug 5506417 :Added order by pllx.shipment_num and order by plx.line_num clauses
5033           l_xml_query := 'SELECT phx.*, PO_COMMUNICATION_PVT.getIsComplexWorkPO() is_complex_work_po, PO_COMMUNICATION_PVT.getDocumentType() document_type, PO_COMMUNICATION_PVT.getCoverMessage() cover_message,
5034               PO_COMMUNICATION_PVT.getTimezone() timezone,PO_COMMUNICATION_PVT.getAmendmentMessage() ammendment_message,PO_COMMUNICATION_PVT.getTestFlag() test_flag,
5035               PO_COMMUNICATION_PVT.getDistinctShipmentCount() DIST_SHIPMENT_COUNT,
5036               PO_COMMUNICATION_PVT.getDocumentName() document_name,
5037               PO_COMMUNICATION_PVT.IsDocumentSigned(PO_COMMUNICATION_PVT.getDocumentId()) Signed,
5038               fnd_profile.value(''PO_GENERATE_AMENDMENT_DOCS'') amendment_profile,PO_COMMUNICATION_PVT.getWithTerms() With_Terms  , '||
5039              ' PO_COMMUNICATION_PVT.getIsContractAttachedDoc() Is_Attached_Doc , ' || l_xml_message_query || ','|| l_head_short_attachment_query ||','||l_head_url_attachment_query||','||l_head_file_attachment_query||',
5040               CURSOR(SELECT plx.*, '||l_adv_amount_query||' CURSOR(SELECT  PRICE_TYPE_DSP PRICE_TYPE, MULTIPLIER FROM po_price_differentials_v
5041               WHERE entity_type=''PO LINE'' AND entity_id = plx.po_line_id and enabled_flag=''Y'') AS price_diff,
5042               '|| l_line_short_attachment_query ||','||l_line_url_attachment_query||','||l_line_file_attachment_query||',
5043               CURSOR(SELECT pllx.*, '|| l_shipment_short_attach_query ||','||l_shipment_url_attach_query||','||l_shipment_file_attach_query||',
5044               CURSOR(SELECT pdx.* FROM po_distribution_xml pdx WHERE pdx.po_header_id = phx.po_header_id and pdx.LINE_LOCATION_ID = pllx.LINE_LOCATION_ID) AS distributions
5045               FROM po_line_locations_xml pllx WHERE pllx.po_line_id = plx.po_line_id AND NVL(pllx.payment_type,''NONE'') NOT IN (''ADVANCE'',''DELIVERY'') and not exists
5046             (select ''x'' from po_line_locations_archive_all pllaa where pllaa.line_location_id = pllx.line_location_id
5047             and pllaa.cancel_flag=''Y'' and pllaa.revision_num < PO_COMMUNICATION_PVT.getRevisionNum()) order by pllx.shipment_num ) AS line_locations' || l_complex_dist_query || '
5048               FROM po_lines_xml plx WHERE plx.po_header_id = phx.po_header_id and not exists
5049               (select ''x'' from po_lines_archive_all  plaa where
5050                 plaa.po_line_id = plx.po_line_id and
5051                       plaa.cancel_flag= ''Y'' and plaa.revision_num< PO_COMMUNICATION_PVT.getRevisionNum() ) order by plx.line_num) AS lines
5052               FROM PO_HEADERS_XML phx WHERE phx.PO_HEADER_ID = PO_COMMUNICATION_PVT.getDocumentId() AND
5053               phx.revision_num = PO_COMMUNICATION_PVT.getRevisionNum()';
5054 
5055         END IF;
5056       END IF;
5057 
5058       /*As per the new layouts there is no block for displaying Purchasing organization
5059             and Purchasing site information for a Global contract and Blanket agreement.
5060             Removed the condition part, which will add the agreement assignment query to main query.*/
5061     END IF;
5062 
5063   ELSIF p_which_tables = 'ARCHIVE' THEN
5064 
5065   /*  Bug#3574748: Added the condition SHIPMENT_TYPE in ('BLANKET','STANDARD') in shipment query. */
5066         /*  Bug#3698674: SQL for generation of XML is framed by checking whether the values are exists at each
5067             level i.e line level, shipment level and distribution level. If the sql query is not framed with out
5068             checking the values exists in the corresponding levels in 8i "Exhausted Result" error is raised.
5069         */
5070   l_xml_query := 'SELECT phx.*, PO_COMMUNICATION_PVT.getIsComplexWorkPO() is_complex_work_po, PO_COMMUNICATION_PVT.getDocumentType() document_type,
5071     PO_COMMUNICATION_PVT.getCoverMessage() cover_message,PO_COMMUNICATION_PVT.getTimezone() timezone,
5072     PO_COMMUNICATION_PVT.getAmendmentMessage() ammendment_message,PO_COMMUNICATION_PVT.getTestFlag()
5073     test_flag,
5074     PO_COMMUNICATION_PVT.getDocumentName() document_name,
5075     PO_COMMUNICATION_PVT.IsDocumentSigned(PO_COMMUNICATION_PVT.getDocumentId()) Signed,
5076     fnd_profile.value(''PO_GENERATE_AMENDMENT_DOCS'') amendment_profile, PO_COMMUNICATION_PVT.getWithTerms() With_Terms , PO_COMMUNICATION_PVT.getIsContractAttachedDoc() Is_Attached_Doc , '||
5077         l_xml_message_query || ','|| l_head_short_attachment_query ||','||l_head_url_attachment_query||','||l_head_file_attachment_query||'
5078     FROM PO_HEADERS_ARCHIVE_XML phx WHERE phx.PO_HEADER_ID = PO_COMMUNICATION_PVT.getDocumentId() AND phx.revision_num = PO_COMMUNICATION_PVT.getRevisionNum()';
5079 
5080     IF(p_document_subtype <> 'CONTRACTS') THEN -- contracts will have only headers
5081 
5082       SELECT count(*) into l_count FROM po_lines_archive_all  WHERE po_header_id = p_document_id;
5083 
5084       IF l_count >0 THEN
5085 
5086       /* for getting the price differentials FROM po_price_differentials_v
5087          the entity _name is 'PO LINE' except for blanket AND entity_name is 'BLANKET LINE'
5088          for blanket */
5089       --Bug 5506417: Added order by plx.line_num clause
5090       l_xml_query := 'SELECT phx.*, PO_COMMUNICATION_PVT.getIsComplexWorkPO() is_complex_work_po, PO_COMMUNICATION_PVT.getDocumentType() document_type, PO_COMMUNICATION_PVT.getCoverMessage() cover_message,
5091           PO_COMMUNICATION_PVT.getTimezone() timezone,PO_COMMUNICATION_PVT.getAmendmentMessage() ammendment_message,PO_COMMUNICATION_PVT.getTestFlag() test_flag,
5092           PO_COMMUNICATION_PVT.getDistinctShipmentCount() DIST_SHIPMENT_COUNT,
5093           PO_COMMUNICATION_PVT.getDocumentName() document_name,
5094           PO_COMMUNICATION_PVT.IsDocumentSigned(PO_COMMUNICATION_PVT.getDocumentId()) Signed,
5095           fnd_profile.value(''PO_GENERATE_AMENDMENT_DOCS'') amendment_profile,PO_COMMUNICATION_PVT.getWithTerms() With_Terms  , PO_COMMUNICATION_PVT.getIsContractAttachedDoc() Is_Attached_Doc , '||
5096                     l_xml_message_query || ','|| l_head_short_attachment_query ||','||l_head_url_attachment_query||','||l_head_file_attachment_query||',
5097           CURSOR(SELECT plx.*, '||l_adv_amount_query||' CURSOR(SELECT  PRICE_TYPE_DSP PRICE_TYPE, MULTIPLIER,MIN_MULTIPLIER, MAX_MULTIPLIER FROM po_price_differentials_v
5098               WHERE entity_type='''||l_eventType||''' AND entity_id = plx.po_line_id and enabled_flag=''Y'') AS price_diff,
5099           '|| l_line_short_attachment_query ||','||l_line_url_attachment_query||','||l_line_file_attachment_query||' FROM PO_LINES_ARCHIVE_XML plx WHERE plx.po_header_id = phx.po_header_id
5100           AND plx.REVISION_NUM = (select max(revision_num) from po_lines_archive_all pla where pla.po_line_id = plx.po_line_id
5101           and pla.revision_num <= pcgt.revision_number ) and  ''Y'' = decode(nvl(plx.cancel_flag,''N''),''N'',''Y'',''Y'',decode(plx.revision_num,PO_COMMUNICATION_PVT.getRevisionNum(),''Y'',''N'') )  order by plx.line_num) AS lines
5102           FROM PO_HEADERS_ARCHIVE_XML phx, PO_COMMUNICATION_GT pcgt
5103           WHERE phx.PO_HEADER_ID = PO_COMMUNICATION_PVT.getDocumentId()
5104           AND phx.revision_num = PO_COMMUNICATION_PVT.getRevisionNum()';
5105 
5106       END IF;
5107 
5108       SELECT count(*) into l_count FROM po_line_locations_archive_all  WHERE po_header_id = p_document_id;
5109 
5110       IF l_count >0 THEN
5111       --Bug 5506417: Added order by pllx.shipment_num and order by plx.line_num clauses
5112       l_xml_query := 'SELECT phx.*, PO_COMMUNICATION_PVT.getIsComplexWorkPO() is_complex_work_po, PO_COMMUNICATION_PVT.getDocumentType() document_type, PO_COMMUNICATION_PVT.getCoverMessage() cover_message,
5113           PO_COMMUNICATION_PVT.getTimezone() timezone,PO_COMMUNICATION_PVT.getAmendmentMessage() ammendment_message,PO_COMMUNICATION_PVT.getTestFlag() test_flag,
5114           PO_COMMUNICATION_PVT.getDistinctShipmentCount() DIST_SHIPMENT_COUNT,
5115           PO_COMMUNICATION_PVT.getDocumentName() document_name,
5116           PO_COMMUNICATION_PVT.IsDocumentSigned( PO_COMMUNICATION_PVT.getDocumentId()) Signed,
5117           fnd_profile.value(''PO_GENERATE_AMENDMENT_DOCS'') amendment_profile, PO_COMMUNICATION_PVT.getWithTerms() With_Terms  , PO_COMMUNICATION_PVT.getIsContractAttachedDoc() Is_Attached_Doc , '||
5118                     l_xml_message_query || ','|| l_head_short_attachment_query ||','||l_head_url_attachment_query||','||l_head_file_attachment_query||',
5119           CURSOR(SELECT plx.*, '||l_adv_amount_query||' CURSOR(SELECT  PRICE_TYPE_DSP PRICE_TYPE, MULTIPLIER, MIN_MULTIPLIER,MAX_MULTIPLIER FROM po_price_differentials_v
5120             WHERE entity_type='''||l_eventType||''' AND entity_id = plx.po_line_id and enabled_flag=''Y'') AS price_diff,
5121             '|| l_line_short_attachment_query ||','||l_line_url_attachment_query||','||l_line_file_attachment_query||',
5122           CURSOR(SELECT pllx.*,';
5123           IF (p_document_subtype <> 'STANDARD') THEN
5124             l_xml_query := l_xml_query||'CURSOR(SELECT PRICE_TYPE_DSP PRICE_TYPE, MIN_MULTIPLIER,  MAX_MULTIPLIER FROM po_price_differentials_v
5125             WHERE entity_type=''PRICE BREAK'' AND entity_id = pllx.line_location_id and enabled_flag=''Y'') AS price_break,';
5126           END IF;
5127 
5128           l_xml_query := l_xml_query || l_shipment_short_attach_query ||','||l_shipment_url_attach_query||','||l_shipment_file_attach_query||'
5129           FROM PO_LINE_LOCATIONS_ARCHIVE_XML pllx WHERE pllx.po_line_id = plx.po_line_id and SHIPMENT_TYPE in (''BLANKET'',''STANDARD'',''PREPAYMENT'') AND NVL(pllx.payment_type, ''NONE'') NOT IN (''ADVANCE'',''DELIVERY'')
5130           and pllx.revision_num = (SELECT MAX(plla.REVISION_NUM) FROM PO_LINE_LOCATIONS_ARCHIVE_ALL plla
5131           where plla.LINE_LOCATION_ID = pllx.LINE_LOCATION_ID and plla.revision_num <= pcgt.revision_number  )
5132           and ''Y'' = decode(nvl(pllx.cancel_flag,''N''),''N'',''Y'',''Y'',decode(pllx.revision_num,PO_COMMUNICATION_PVT.getRevisionNum(),''Y'',''N'') ) order by pllx.shipment_num ) AS line_locations' || l_complex_lloc_query || '
5133           FROM PO_LINES_ARCHIVE_XML plx WHERE plx.po_header_id = phx.po_header_id
5134           AND plx.REVISION_NUM = (select max(revision_num) from po_lines_archive_all pla where pla.po_line_id = plx.po_line_id
5135           and pla.revision_num <= pcgt.revision_number  ) and  ''Y'' = decode(nvl(plx.cancel_flag,''N''),''N'',''Y'',''Y'',decode(plx.revision_num,PO_COMMUNICATION_PVT.getRevisionNum(),''Y'',''N'') ) order by plx.line_num) AS lines
5136           FROM PO_HEADERS_ARCHIVE_XML phx, PO_COMMUNICATION_GT pcgt   WHERE phx.PO_HEADER_ID = PO_COMMUNICATION_PVT.getDocumentId() AND phx.revision_num = PO_COMMUNICATION_PVT.getRevisionNum()';
5137       END IF;
5138 
5139       IF(p_document_subtype <> 'BLANKET') THEN -- blankets will not have distributions
5140         SELECT count(*) into l_count FROM po_distributions_archive_all  WHERE po_header_id = p_document_id;
5141 
5142         IF l_count >0 THEN
5143         --Bug 5506417: Added order by pllx.shipment_num and order by plx.line_num clauses
5144         l_xml_query := 'SELECT phx.*, PO_COMMUNICATION_PVT.getIsComplexWorkPO() is_complex_work_po, PO_COMMUNICATION_PVT.getDocumentType() document_type, PO_COMMUNICATION_PVT.getCoverMessage() cover_message,
5145             PO_COMMUNICATION_PVT.getTimezone() timezone,PO_COMMUNICATION_PVT.getAmendmentMessage() ammendment_message,PO_COMMUNICATION_PVT.getTestFlag() test_flag,
5146             PO_COMMUNICATION_PVT.getDistinctShipmentCount() DIST_SHIPMENT_COUNT,
5147             PO_COMMUNICATION_PVT.getDocumentName() document_name,
5148             PO_COMMUNICATION_PVT.IsDocumentSigned(PO_COMMUNICATION_PVT.getDocumentId()) Signed,
5149             fnd_profile.value(''PO_GENERATE_AMENDMENT_DOCS'') amendment_profile, PO_COMMUNICATION_PVT.getWithTerms() With_Terms  , PO_COMMUNICATION_PVT.getIsContractAttachedDoc() Is_Attached_Doc , '||
5150                         l_xml_message_query || ','|| l_head_short_attachment_query ||','||l_head_url_attachment_query||','||l_head_file_attachment_query||',
5151             CURSOR(SELECT plx.*, '||l_adv_amount_query||' CURSOR(SELECT  PRICE_TYPE_DSP PRICE_TYPE, MULTIPLIER FROM po_price_differentials_v
5152             WHERE entity_type=''PO LINE'' AND entity_id = plx.po_line_id and enabled_flag=''Y'') AS price_diff,
5153             '|| l_line_short_attachment_query ||','||l_line_url_attachment_query||','||l_line_file_attachment_query||',
5154             CURSOR(SELECT pllx.*, '|| l_shipment_short_attach_query ||','||l_shipment_url_attach_query||','||l_shipment_file_attach_query||',
5155             CURSOR(SELECT pdx.* FROM po_distribution_archive_xml pdx WHERE pdx.po_header_id = phx.po_header_id and pdx.LINE_LOCATION_ID = pllx.LINE_LOCATION_ID
5156             and pdx.REVISION_NUM = (SELECT MAX(pda.REVISION_NUM) FROM PO_DISTRIBUTIONS_ARCHIVE_ALL pda
5157             WHERE pda.PO_DISTRIBUTION_ID = pdx.PO_DISTRIBUTION_ID AND pda.REVISION_NUM <= pcgt.revision_number ) ) AS distributions
5158             FROM PO_LINE_LOCATIONS_ARCHIVE_XML pllx WHERE pllx.po_line_id = plx.po_line_id and SHIPMENT_TYPE in (''BLANKET'',''STANDARD'',''PREPAYMENT'') AND NVL(pllx.payment_type,''NONE'') NOT IN (''ADVANCE'',''DELIVERY'')
5159             and pllx.revision_num = (SELECT MAX(plla.REVISION_NUM) FROM PO_LINE_LOCATIONS_ARCHIVE_ALL plla
5160             where plla.LINE_LOCATION_ID = pllx.LINE_LOCATION_ID and plla.revision_num <= pcgt.revision_number )
5161             and ''Y'' = decode(nvl(pllx.cancel_flag,''N''),''N'',''Y'',''Y'',decode(pllx.revision_num,PO_COMMUNICATION_PVT.getRevisionNum(),''Y'',''N'') ) order by pllx.shipment_num ) AS line_locations'|| l_complex_dist_query || '
5162             FROM PO_LINES_ARCHIVE_XML plx WHERE plx.po_header_id = phx.po_header_id
5163             AND plx.REVISION_NUM = (select max(revision_num) from po_lines_archive_all pla where pla.po_line_id = plx.po_line_id
5164             and pla.revision_num <= pcgt.revision_number ) and  ''Y'' = decode(nvl(plx.cancel_flag,''N''),''N'',''Y'',''Y'',decode(plx.revision_num,PO_COMMUNICATION_PVT.getRevisionNum(),''Y'',''N'') ) order by plx.line_num) AS lines
5165             FROM PO_HEADERS_ARCHIVE_XML phx, PO_COMMUNICATION_GT pcgt WHERE phx.PO_HEADER_ID = PO_COMMUNICATION_PVT.getDocumentId() AND
5166             phx.revision_num = PO_COMMUNICATION_PVT.getRevisionNum()';
5167 
5168         END IF; -- end of
5169       END IF; -- end of balnket if condition
5170     END IF; -- end of Contracts if condition
5171 
5172     /*As per the new layouts there is no block for displaying Purchasing organization
5173     and Purchasing site information for a Global contract and Blanket agreement.
5174     Removed the condition part, which will add the agreement assignment query to main query.*/
5175 
5176   END IF; -- end of else if
5177 
5178 else
5179    /*  Bug#3698674: In 8i db, the functions used to retrieve revision number and release id are not working
5180              properly. Created a global temporary table po_communication_pvt and retrieved the values from the
5181              global temporary table in both main and archive queries.*/
5182   IF p_which_tables = 'MAIN' THEN
5183     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5184       FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||'Releases:','Entered into Release loop');
5185     END IF;
5186 
5187     l_xml_query := 'SELECT phx.*, PO_COMMUNICATION_PVT.getDocumentType() document_type, PO_COMMUNICATION_PVT.getCoverMessage() cover_message,
5188         PO_COMMUNICATION_PVT.getTimezone() timezone,PO_COMMUNICATION_PVT.getAmendmentMessage() ammendment_message,PO_COMMUNICATION_PVT.getTestFlag() test_flag,
5189         PO_COMMUNICATION_PVT.getDistinctShipmentCount() DIST_SHIPMENT_COUNT,
5190         PO_COMMUNICATION_PVT.getDocumentName() document_name,
5191         fnd_profile.value(''PO_GENERATE_AMENDMENT_DOCS'') amendment_profile, PO_COMMUNICATION_PVT.getWithTerms() With_Terms  , PO_COMMUNICATION_PVT.getIsContractAttachedDoc() Is_Attached_Doc , '||
5192                 l_xml_message_query || ','|| l_head_short_attachment_query ||','||l_head_url_attachment_query||','||l_head_file_attachment_query||'
5193         FROM PO_RELEASE_XML phx WHERE phx.PO_RELEASE_ID = PO_COMMUNICATION_PVT.getDocumentId() AND phx.revision_num = PO_COMMUNICATION_PVT.getRevisionNum()';
5194 
5195 
5196       SELECT count(*) into l_count FROM po_line_locations_all  WHERE po_release_id = p_document_id ;
5197 
5198       IF l_count >0 THEN
5199         IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5200           FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||'Releases:','Assigning Releases line/line locations query');
5201         END IF;
5202         -- Added release id in condition
5203         --Bug 5506417: Added order by pllx.shipment_num and order by plx.line_num clauses
5204         l_xml_query := 'SELECT phx.*, PO_COMMUNICATION_PVT.getDocumentType() document_type, PO_COMMUNICATION_PVT.getCoverMessage() cover_message,
5205             PO_COMMUNICATION_PVT.getTimezone() timezone,PO_COMMUNICATION_PVT.getAmendmentMessage() ammendment_message,PO_COMMUNICATION_PVT.getTestFlag() test_flag,
5206             PO_COMMUNICATION_PVT.getDistinctShipmentCount() DIST_SHIPMENT_COUNT,
5207             PO_COMMUNICATION_PVT.getDocumentName() document_name,
5208             fnd_profile.value(''PO_GENERATE_AMENDMENT_DOCS'') amendment_profile, PO_COMMUNICATION_PVT.getWithTerms() With_Terms  , PO_COMMUNICATION_PVT.getIsContractAttachedDoc() Is_Attached_Doc , '||
5209                         l_xml_message_query || ','|| l_head_short_attachment_query ||','||l_head_url_attachment_query||','||l_head_file_attachment_query||',
5210             CURSOR(SELECT plx.*,CURSOR(SELECT  PRICE_TYPE_DSP PRICE_TYPE, MULTIPLIER FROM po_price_differentials_v
5211               WHERE entity_type='''||l_eventType||''' AND entity_id = plx.po_line_id and enabled_flag=''Y'') AS price_diff,
5212               '|| l_line_short_attachment_query ||','||l_line_url_attachment_query||','||l_line_file_attachment_query||',
5213             CURSOR(SELECT pllx.*,'|| l_shipment_short_attach_query ||','||l_shipment_url_attach_query||','||l_shipment_file_attach_query||',
5214             CURSOR(SELECT pd.*
5215             FROM po_distribution_xml pd WHERE pd.po_release_id = pllx.po_release_id and pd.LINE_LOCATION_ID = pllx.LINE_LOCATION_ID) AS distributions
5216             FROM po_line_locations_xml pllx WHERE pllx.po_release_id in (select po_release_id from PO_COMMUNICATION_GT) and pllx.po_line_id = plx.po_line_id
5217             and not exists (select ''x'' from po_line_locations_archive_all  pllaa where
5218                 pllaa.line_location_id = pllx.line_location_id and
5219                       pllaa.cancel_flag= ''Y'' and pllaa.revision_num< PO_COMMUNICATION_PVT.getRevisionNum() ) order by pllx.shipment_num ) AS line_locations
5220             FROM po_lines_xml plx WHERE  exists (SELECT ''x'' from po_line_locations_all
5221             WHERE po_line_locations_all.po_line_id = plx.po_line_id and  po_release_id = phx.po_release_id and not exists (select ''x'' from po_line_locations_archive_all  pllaa where
5222                 pllaa.line_location_id = po_line_locations_all.line_location_id and
5223                       pllaa.cancel_flag= ''Y'' and pllaa.revision_num< PO_COMMUNICATION_PVT.getRevisionNum() ) ) and plx.po_header_id = phx.po_header_id  order by plx.line_num) AS lines
5224             FROM PO_RELEASE_XML phx WHERE phx.PO_RELEASE_ID = PO_COMMUNICATION_PVT.getDocumentId() AND phx.revision_num = PO_COMMUNICATION_PVT.getRevisionNum()';
5225 
5226       END IF;
5227 
5228 
5229 
5230   ELSIF p_which_tables = 'ARCHIVE' THEN
5231   IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5232     FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||'Release Archive:','Assigning Releases Archive Query');
5233   END IF;
5234 
5235         /* The following query gets the release details, the outermost cursor selects headers information,
5236             and we move to the details (line, shipments, distributions) as we move inside each cursor. The
5237             lines have to be selected from the corresponding blanket since they are not present in the release */
5238         -- Bug 3727808. Use blanket revision number rather than release revision number. Added the max(pb.revision_num) query in lines SQL
5239         -- Bug 5506417: Added order by pllx.shipment_num and order by plx.line_num clauses
5240     l_xml_query := 'SELECT phx.*, PO_COMMUNICATION_PVT.getDocumentType() document_type, PO_COMMUNICATION_PVT.getCoverMessage() cover_message,PO_COMMUNICATION_PVT.getTimezone() timezone,
5241       PO_COMMUNICATION_PVT.getAmendmentMessage() ammendment_message,PO_COMMUNICATION_PVT.getTestFlag() test_flag,
5242       PO_COMMUNICATION_PVT.getDistinctShipmentCount() DIST_SHIPMENT_COUNT,
5243       PO_COMMUNICATION_PVT.getDocumentName() document_name,
5244       fnd_profile.value(''PO_GENERATE_AMENDMENT_DOCS'') amendment_profile, PO_COMMUNICATION_PVT.getWithTerms() With_Terms  , PO_COMMUNICATION_PVT.getIsContractAttachedDoc() Is_Attached_Doc , '||
5245             l_xml_message_query || ','|| l_head_short_attachment_query ||','||l_head_url_attachment_query||','||l_head_file_attachment_query||',
5246       CURSOR(SELECT plx.*,CURSOR(SELECT  PRICE_TYPE_DSP PRICE_TYPE, MULTIPLIER FROM po_price_differentials_v
5247         WHERE entity_type='''||l_eventType||''' AND entity_id = plx.po_line_id and enabled_flag=''Y'') AS price_diff,
5248         '|| l_line_short_attachment_query ||','||l_line_url_attachment_query||','||l_line_file_attachment_query||',
5249       CURSOR(SELECT pllx.*,'|| l_shipment_short_attach_query ||','||l_shipment_url_attach_query||','||l_shipment_file_attach_query||',
5250       CURSOR(SELECT pd.*
5251       FROM po_distribution_archive_xml pd WHERE pd.po_release_id = pllx.po_release_id and pd.line_location_id  = pllx.line_location_id
5252       and pd.REVISION_NUM = (SELECT MAX(pda.REVISION_NUM) FROM PO_DISTRIBUTIONS_ARCHIVE_ALL pda
5253       WHERE pda.PO_DISTRIBUTION_ID = pd.PO_DISTRIBUTION_ID AND pda.REVISION_NUM <= PO_COMMUNICATION_PVT.getRevisionNum() ) ) AS distributions
5254       FROM PO_LINE_LOCATIONS_ARCHIVE_XML pllx WHERE pllx.po_release_id = pcgt.po_release_id  and pllx.po_line_id = plx.po_line_id
5255       and pllx.revision_num = (SELECT MAX(plla.REVISION_NUM) FROM PO_LINE_LOCATIONS_ARCHIVE_ALL plla
5256       where plla.LINE_LOCATION_ID = pllx.LINE_LOCATION_ID and plla.revision_num <= pcgt.revision_number  ) order by pllx.shipment_num ) AS line_locations
5257       FROM PO_LINES_ARCHIVE_XML plx
5258       WHERE exists (SELECT ''x'' from po_line_locations_archive_all pllaa
5259       WHERE pllaa.po_line_id = plx.po_line_id and  po_release_id = phx.po_release_id
5260       and pllaa.REVISION_NUM = (select max(revision_num) from po_line_locations_archive_all pllaa1 where pllaa1.line_location_id = pllaa.line_location_id
5261       and pllaa1.revision_num <= pcgt.revision_number  )
5262       and ''Y'' = decode(nvl(pllaa.cancel_flag,''N''),''N'',''Y'',''Y'',decode(pllaa.revision_num,PO_COMMUNICATION_PVT.getRevisionNum(),''Y'',''N'') ) )
5263       and plx.po_header_id = phx.po_header_id
5264       AND plx.REVISION_NUM = (select max(revision_num) from po_lines_archive_all pla where pla.po_line_id = plx.po_line_id
5265       and pla.revision_num <= (select max(pb.revision_num)
5266                                                                  from po_headers_archive_all pb, po_releases_archive_all pr
5267                                                                  where pb.po_header_id = pr.po_header_id
5268                                                                  and pr.po_release_id = pcgt.po_release_id
5269                                                                  and pr.revision_num= pcgt.revision_number
5270                                                                  and pb.approved_date <= pr.approved_date
5271                                                                 ) )   order by plx.line_num desc) AS lines
5272       FROM PO_RELEASE_ARCHIVE_XML phx, PO_COMMUNICATION_GT pcgt WHERE phx.PO_RELEASE_ID = PO_COMMUNICATION_PVT.getDocumentId() AND phx.revision_num = PO_COMMUNICATION_PVT.getRevisionNum()';
5273 
5274   END IF;
5275 
5276 
5277 END IF;
5278 
5279 
5280 --bug#3760632 replaced the function PO_POXPOEPO
5281 --with PO_PRINTPO
5282   /* for header long text */
5283 --bug#3768142 also added the condition to check if the document
5284 --type is a release so that even the release header documents
5285 --are retrieved. An order by is used so that first the
5286 --PO_HEADERS(BPA) attachments are printed followed by PO_RELEASES
5287 --attachments and then finally PO_VENDORS. This is necessary
5288 --only for the Releases because you can display the BPA header
5289 --attachments also with a release.
5290 --bug#3823799: Replaced the hard coded p_document id with PO_COMMUNICATION_PVT.getDocumentId() function
5291 if(p_document_type='RELEASE')then
5292 	--Bug#4683170
5293 	--Appended fad.datatype_id=2 condition for retrieving the
5294         --long_text attachment for the current document only.
5295 
5296 	/*Bug5213932 : To convert nonxml characters into their escape equivalents
5297  	  long text should be converted to clob. Hence calling function get_clob.
5298  	  Replacing fdl.long_text with get_clob(fdl.rowid) */
5299 
5300 	l_headerAttachmentsQuery := 'select PO_COMMUNICATION_PVT.get_clob(fdl.rowid) long_text
5301 	 FROM
5302 		fnd_attached_docs_form_vl fad,
5303 		fnd_documents_long_text fdl
5304 	 WHERE ( (entity_name=''PO_RELEASES'' AND
5305 		 pk1_value= to_char(PO_COMMUNICATION_PVT.getDocumentId()) ) OR
5306 		 (entity_name = ''PO_HEADERS'' AND
5307 		 pk1_value = to_char(PO_COMMUNICATION_PVT.getReleaseHeaderId())) OR --Bug6139548
5308 		 (entity_name = ''PO_VENDORS'' AND
5309 		 pk1_value = to_char(PO_COMMUNICATION_PVT.getVendorId()))) AND
5310 		 function_name = ''PO_PRINTPO''
5311 		 and fad.media_id = fdl.media_id
5312 		 and fad.datatype_id=2
5313 		 order by entity_name,seq_num';  --bug6133951
5314 
5315 else
5316 	--Bug#4683170
5317 	--Appended fad.datatype_id=2 condition for retrieving the
5318         --long_text attachment for the current document only.
5319 
5320 	/*Bug5213932 : To convert nonxml characters into their escape equivalents
5321  	   long text should be converted to clob.Hence calling function get_clob.
5322  	   Replacing fdl.long_text with get_clob(fdl.rowid) */
5323 
5324 	l_headerAttachmentsQuery := 'select PO_COMMUNICATION_PVT.get_clob(fdl.rowid) long_text
5325 	 FROM
5326 		fnd_attached_docs_form_vl fad,
5327 		fnd_documents_long_text fdl
5328 	 WHERE ((entity_name = ''PO_HEADERS'' AND
5329 		 pk1_value = to_char(PO_COMMUNICATION_PVT.getReleaseHeaderId())) OR --Bug6139548
5330 		(entity_name = ''PO_VENDORS'' AND
5331 		 pk1_value = to_char(PO_COMMUNICATION_PVT.getVendorId()))) AND
5332 		 function_name = ''PO_PRINTPO''
5333 		 and fad.media_id = fdl.media_id
5334 		 AND fad.datatype_id=2
5335                  order by seq_num'; --bug6133951
5336 end if;
5337 --bug#3760632 replaced the function PO_POXPOEPO
5338 --with PO_PRINTPO
5339 	--Bug#4683170
5340 	--Appended fad.datatype_id=2 condition for retrieving the
5341         --long_text attachment for the current document only.
5342 	-- Bug 4673653 - Added condition to show item level long text attachments
5343   /* for line long attachments */
5344 
5345 	  /*Bug5213932 : To convert nonxml characters into their escape equivalents
5346  	 long text should be converted to clob. Hence calling function get_clob.
5347  	 Replacing fdl.long_text with get_clob(fds.rowid) */
5348 
5349 	l_lineAttachQuery :='SELECT  PO_COMMUNICATION_PVT.get_clob(fds.rowid) text, plx.po_line_id id
5350 	 FROM
5351 		fnd_attached_docs_form_vl fad,
5352 		fnd_documents_long_text fds,
5353 		po_lines_all plx
5354 	WHERE ((fad.entity_name=''PO_LINES'' AND fad.pk1_value=to_char(plx.po_line_id))
5355 	         OR
5356 	         (fad.entity_name=''MTL_SYSTEM_ITEMS'' AND
5357 	          fad.pk1_value=to_char(PO_COMMUNICATION_PVT.getInventoryOrgId()) AND --Bug6139548
5358 	          fad.pk2_value=to_char(plx.item_id) AND plx.item_id is not null)
5359 	       ) AND
5360 	       function_name = ''PO_PRINTPO''
5361 	       AND fad.media_id = fds.media_id
5362 	       AND fad.datatype_id=2
5363 	       AND plx.po_header_id = PO_COMMUNICATION_PVT.getReleaseHeaderId()
5364                order by seq_num';  --bug6133951
5365 --bug#3760632 replaced the function PO_POXPOEPO
5366 --with PO_PRINTPO
5367 	--Bug#4683170
5368 	--Appended fad.datatype_id=2 condition for retrieving the
5369         --long_text attachment for the current document only.
5370    /* for shipments long attachments */
5371 
5372 	    /*Bug52139320 : To convert nonxml characters into their escape equivalents
5373  	    long text should be converted to clob.Hence calling function get_clob.
5374  	    Replacing fdl.long_text with get_clob(fds.rowid) */
5375 
5376 	/* Bug 4568471/6829381 Exclude the One Time Address attachments when printing shipment long text.
5377 		as this text is already printed as shipto Location */
5378 	l_shipmentAttachmentQuery:=  'SELECT PO_COMMUNICATION_PVT.get_clob(fds.rowid) long_text, pllx.LINE_LOCATION_ID
5379 	 FROM
5380 		fnd_attached_docs_form_vl fad,
5381 		fnd_documents_long_text fds,
5382 		po_line_locations_all pllx
5383 	WHERE entity_name = ''PO_SHIPMENTS'' AND
5384 		 pk1_value =  to_char(pllx.LINE_LOCATION_ID) AND
5385 	       function_name = ''PO_PRINTPO''
5386 	       AND fad.media_id = fds.media_id
5387 		   AND fad.document_description not like ''POR%''
5388 	       AND fad.datatype_id=2
5389 	       AND pllx.po_header_id = PO_COMMUNICATION_PVT.getReleaseHeaderId()
5390                order by seq_num'; --bug6133951
5391 
5392 select TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') into l_time from dual;
5393 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5394   FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||'Start of executing queries', l_time);
5395 END IF;
5396 
5397 
5398   IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5399     FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||'POXMLGEN','Generating XML using XMLGEN');
5400   END IF;
5401   l_xml9_stmt := 'declare
5402        context DBMS_XMLGEN.ctxHandle;
5403                    l_xml_query varchar2(15000) ;
5404        l_headerAttach_query varchar2(1000);
5405        l_lineAttach_query varchar2(1200) ; --Bug5213932 increase length
5406        l_disAttach_query varchar2(1200) ; --Bug5213932 increase length
5407        l_time varchar2(50);
5408        g_log_head    CONSTANT VARCHAR2(30) := ''po.plsql.PO_COMMUNICATION_PVT.'';
5409        l_api_name CONSTANT VARCHAR2(30):= ''POXMLGEN'';
5410        TYPE ref_cursorType IS REF CURSOR;
5411        refcur ref_cursorType;
5412        l_fileClob CLOB := NULL;
5413           Begin
5414 
5415       l_xml_query := :1 ;
5416       l_headerAttach_query := :2;
5417       l_lineAttach_query := :3;
5418       l_disAttach_query := :4;
5419       l_fileClob := :5;
5420 
5421       select TO_CHAR(SYSDATE, ''DD-MON-YYYY HH24:MI:SS'') into l_time from dual;
5422       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5423         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||''Before Executing the Main Query'', l_time);
5424       END IF;
5425 
5426       context := dbms_xmlgen.newContext(l_xml_query);
5427       dbms_xmlgen.setRowsetTag(context,''PO_DATA'');
5428       dbms_xmlgen.setRowTag(context,NULL);
5429       dbms_xmlgen.setConvertSpecialChars ( context, TRUE);
5430       :xresult := dbms_xmlgen.getXML(context,DBMS_XMLGEN.NONE);
5431       dbms_xmlgen.closeContext(context);
5432 
5433       select TO_CHAR(SYSDATE, ''DD-MON-YYYY HH24:MI:SS'') into l_time from dual;
5434       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5435         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||''After Executing the Main Query'', l_time);
5436       END IF;
5437 
5438       context := dbms_xmlgen.newContext(l_headerAttach_query);
5439       dbms_xmlgen.setRowsetTag(context,''HEADER_ATTACHMENTS'');
5440       dbms_xmlgen.setRowTag(context,NULL);
5441       dbms_xmlgen.setConvertSpecialChars ( context, TRUE);
5442       :xheaderAttach := dbms_xmlgen.getXML(context,DBMS_XMLGEN.NONE);
5443       dbms_xmlgen.closeContext(context);
5444 
5445       select TO_CHAR(SYSDATE, ''DD-MON-YYYY HH24:MI:SS'') into l_time from dual;
5446       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5447         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||''After Executing the header attachment Query'', l_time);
5448       END IF;
5449 
5450       context := dbms_xmlgen.newContext(l_lineAttach_query);
5451       dbms_xmlgen.setRowsetTag(context,''LINE_ATTACHMENTS'');
5452       dbms_xmlgen.setRowTag(context,NULL);
5453       dbms_xmlgen.setConvertSpecialChars ( context, TRUE);
5454       :xlineAttach := dbms_xmlgen.getXML(context,DBMS_XMLGEN.NONE);
5455       dbms_xmlgen.closeContext(context);
5456 
5457       select TO_CHAR(SYSDATE, ''DD-MON-YYYY HH24:MI:SS'') into l_time from dual;
5458       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5459         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||''After Executing the line attachment Query'', l_time);
5460       END IF;
5461 
5462       context := dbms_xmlgen.newContext(l_disAttach_query);
5463       dbms_xmlgen.setRowsetTag(context,''SHIPMENT_ATTACHMENTS'');
5464       dbms_xmlgen.setRowTag(context,NULL);
5465       dbms_xmlgen.setConvertSpecialChars ( context, TRUE);
5466       :xdisAttach := dbms_xmlgen.getXML(context,DBMS_XMLGEN.NONE);
5467       dbms_xmlgen.closeContext(context);
5468 
5469       select TO_CHAR(SYSDATE, ''DD-MON-YYYY HH24:MI:SS'') into l_time from dual;
5470       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5471         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||''After Executing the shipment attachment Query'', l_time);
5472       END IF;
5473 
5474       IF l_fileClob is not null THEN
5475 
5476             open refcur for ''select :l_fileClob1 as text_file from dual'' using l_fileClob;
5477             context := DBMS_XMLGEN.newContext(refcur);
5478             DBMS_XMLGEN.setRowTag(context,NULL);
5479             DBMS_XMLGEN.setRowSetTag(context,NULL);
5480             :xfileClob := DBMS_XMLGEN.getXML(context,DBMS_XMLGEN.NONE);
5481             DBMS_XMLGEN.closeContext(context);
5482             close refcur;
5483             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5484               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name , ''After executing the file clob'');
5485             END IF;
5486       ELSE
5487             IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5488               FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name , ''Value of File clob is null'');
5489             END IF;
5490            :xfileClob := null;
5491       END IF;
5492 
5493       -- bug#3580225 Start --
5494 
5495       select TO_CHAR(SYSDATE, ''DD-MON-YYYY HH24:MI:SS'') into l_time from dual;
5496       IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5497         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||''Before calling PO_HR_LOCATION.populate_gt'', l_time);
5498       END IF;
5499 
5500       /* Call PO_HR_LOCATION.populate_gt procedure to insert address values into global temp table from PL/SQL table*/
5501       PO_HR_LOCATION.populate_gt();
5502 
5503       BEGIN
5504         context := dbms_xmlgen.newContext(''select * from po_address_details_gt '');
5505         dbms_xmlgen.setRowsetTag(context,''ADDRESS_DETAILS'');
5506         dbms_xmlgen.setRowTag(context,''ADDRESS_DETAILS_ROW'');
5507         :xaddrDetails := dbms_xmlgen.getXML(context,DBMS_XMLGEN.NONE);
5508         dbms_xmlgen.closeContext(context);
5509       EXCEPTION
5510        WHEN OTHERS THEN
5511           NULL;
5512       END;
5513       -- bug#3580225 Start --
5514 
5515 
5516           End;';
5517 
5518   execute immediate l_xml9_stmt USING l_xml_query , l_headerAttachmentsQuery, l_lineAttachQuery, l_shipmentAttachmentQuery, l_fileClob,
5519         OUT l_xml_result, OUT l_headerAttachments, OUT l_line_Attachments, OUT l_disAttachments, OUT l_fileClob, OUT l_address_details;
5520 
5521 
5522 select TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') into l_time from dual;
5523 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5524   FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ||'End of executing queries', l_time);
5525 END IF;
5526 
5527 /*Delete the records from global temp table*/
5528 DELETE po_address_details_gt;
5529 DELETE po_communication_gt ; -- Added this line for bug:3698674
5530 
5531 
5532 
5533 
5534 IF dbms_lob.getlength(l_xml_result) >0 THEN
5535   IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5536     FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name , 'inside manuplating l_xml_result');
5537   END IF;
5538   -- add charset.
5539   l_resultOffset := DBMS_LOB.INSTR(l_xml_result,'>');
5540   l_tempXMLResult := l_xml_result;
5541   dbms_lob.write(l_xml_result,length('<?xml version="1.0" encoding="UTF-16"?>'),1,'<?xml version="1.0" encoding="UTF-16"?>');
5542   dbms_lob.copy(l_xml_result,l_tempXMLResult,dbms_lob.getlength(l_tempXMLResult)-l_resultOffset,length('<?xml version="1.0" encoding="UTF-16"?>'),l_resultOffset);
5543 
5544   IF dbms_lob.getlength(l_headerAttachments) >0 THEN
5545 
5546     l_variablePosition := DBMS_LOB.INSTR(l_headerAttachments,'>');
5547     dbms_lob.copy(l_xml_result, l_headerAttachments, dbms_lob.getlength(l_headerAttachments)- l_variablePosition, (dbms_lob.getlength(l_xml_result)- length('</PO_DATA>') ), l_variablePosition+1);
5548     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5549       FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ,'Added header attachments to XML');
5550     END IF;
5551 
5552   END IF;
5553 
5554   IF dbms_lob.getlength(l_line_Attachments) >0 THEN
5555 
5556     l_variablePosition := DBMS_LOB.INSTR(l_line_Attachments,'>');
5557 
5558     IF(DBMS_LOB.INSTR(l_xml_result,'</PO_DATA>') > 0) THEN
5559       dbms_lob.copy(l_xml_result, l_line_Attachments, dbms_lob.getlength(l_line_Attachments)- l_variablePosition, (dbms_lob.getlength(l_xml_result)- length('</PO_DATA>') ), l_variablePosition+1);
5560     ELSE
5561       dbms_lob.copy(l_xml_result, l_line_Attachments, dbms_lob.getlength(l_line_Attachments)- l_variablePosition, dbms_lob.getlength(l_xml_result), l_variablePosition+1);
5562     END IF;
5563     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5564       FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ,'Added line attachments to XML');
5565     END IF;
5566 
5567 
5568   END IF;
5569 
5570   IF dbms_lob.getlength(l_disAttachments) >0 THEN
5571 
5572     l_variablePosition := DBMS_LOB.INSTR(l_disAttachments,'>');
5573     IF(DBMS_LOB.INSTR(l_xml_result,'</PO_DATA>') > 0) THEN
5574       dbms_lob.copy(l_xml_result, l_disAttachments, dbms_lob.getlength(l_disAttachments)- l_variablePosition, (dbms_lob.getlength(l_xml_result)- length('</PO_DATA>') ), l_variablePosition+1);
5575     ELSE
5576       dbms_lob.copy(l_xml_result, l_disAttachments, dbms_lob.getlength(l_disAttachments)- l_variablePosition, dbms_lob.getlength(l_xml_result), l_variablePosition+1);
5577     END IF;
5578     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5579       FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ,'Added distribution attachments to XML');
5580     END IF;
5581 
5582   END IF;
5583 
5584   IF dbms_lob.getlength(l_fileClob) >0 THEN
5585 
5586     l_variablePosition := DBMS_LOB.INSTR(l_fileClob,'>');
5587     IF(DBMS_LOB.INSTR(l_xml_result,'</PO_DATA>') > 0) THEN
5588       dbms_lob.copy(l_xml_result, l_fileClob, dbms_lob.getlength(l_fileClob)- l_variablePosition, (dbms_lob.getlength(l_xml_result)- length('</PO_DATA>') ), l_variablePosition+1);
5589     ELSE
5590       dbms_lob.copy(l_xml_result, l_fileClob, dbms_lob.getlength(l_fileClob)- l_variablePosition, dbms_lob.getlength(l_xml_result), l_variablePosition+1);
5591     END IF;
5592     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5593       FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ,'Added file to XML');
5594     END IF;
5595 
5596 
5597   END IF;
5598 
5599   IF dbms_lob.getlength(l_address_details) >0 THEN  -- bug#3580225 Start --
5600 
5601     --Add l_address_details to final XML
5602 
5603     l_variablePosition := DBMS_LOB.INSTR(l_address_details,'>');
5604     IF(DBMS_LOB.INSTR(l_xml_result,'</PO_DATA>') > 0) THEN
5605       dbms_lob.copy(l_xml_result, l_address_details, dbms_lob.getlength(l_address_details)- l_variablePosition, (dbms_lob.getlength(l_xml_result)- length('</PO_DATA>') ), l_variablePosition+1);
5606     ELSE
5607       dbms_lob.copy(l_xml_result, l_address_details, dbms_lob.getlength(l_address_details)- l_variablePosition, dbms_lob.getlength(l_xml_result), l_variablePosition+1);
5608     END IF;
5609     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5610       FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ,'Added Address details to XML');
5611     END IF;
5612 
5613 
5614   END IF; -- bug#3580225 end --
5615 
5616   IF(DBMS_LOB.INSTR(l_xml_result,'</PO_DATA>') = 0) THEN
5617     dbms_lob.write(l_xml_result,10,dbms_lob.getlength(l_xml_result),'</PO_DATA>');
5618   END IF;
5619 
5620 END IF;
5621 
5622 /*
5623   If the test flasg is D then the query is executing as part of debugging processos.
5624   Add the final xml query in the clob.
5625 */
5626 IF(p_test_flag = 'D') then
5627 
5628   dbms_lob.write(l_xml_result,11,dbms_lob.getlength(l_xml_result)-9,'<XML_QUERY>');
5629   dbms_lob.write(l_xml_result,length(l_xml_query||'</XML_QUERY> </PO_DATA>'),dbms_lob.getlength(l_xml_result)+1,l_xml_query||'</XML_QUERY> </PO_DATA>');
5630 
5631 END IF;
5632 IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5633   FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name ,'END OF POXMLGEN');
5634 END IF;
5635 
5636 
5637 RETURN l_xml_result;
5638  EXCEPTION
5639 
5640   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5641     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5642       FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name , SQLERRM);
5643     END IF;
5644     RAISE;
5645   WHEN OTHERS THEN
5646     IF (FND_LOG.G_CURRENT_RUNTIME_LEVEL <= FND_LOG.LEVEL_STATEMENT) THEN
5647       FND_LOG.string(FND_LOG.LEVEL_STATEMENT,g_log_head || l_api_name , SQLERRM);
5648     END IF;
5649     RAISE;
5650 
5651 END;
5652 
5653 /**
5654   drop ship details
5655 */
5656 
5657 function get_drop_ship_details(p_location_id in number) RETURN number is
5658 
5659 
5660 l_po_header_id                 NUMBER          ;
5661 l_po_line_id                   NUMBER          ;
5662 l_po_release_id                 NUMBER          ;
5663 X_ORDER_LINE_INFO_REC          OE_DROP_SHIP_GRP.Order_Line_Info_Rec_Type;
5664 X_MSG_DATA                     VARCHAR2(100)       ;
5665 X_MSG_COUNT                    NUMBER         ;
5666 X_RETURN_STATUS                VARCHAR2(100)       ;
5667 
5668 
5669 BEGIN
5670 
5671 OE_DROP_SHIP_GRP.get_order_line_info(
5672 P_API_VERSION=>1.0,
5673 P_PO_HEADER_ID =>l_po_header_id,
5674 P_PO_LINE_ID  =>l_po_line_id,
5675 P_PO_LINE_LOCATION_ID  =>p_location_id,
5676 P_PO_RELEASE_ID =>l_po_release_id,
5677 P_MODE => 2 ,
5678 X_ORDER_LINE_INFO_REC => X_ORDER_LINE_INFO_REC,
5679 X_MSG_DATA => X_MSG_DATA,
5680 X_MSG_COUNT => X_MSG_COUNT,
5681 X_RETURN_STATUS => X_RETURN_STATUS );
5682 
5683 g_ship_cont_phone := x_order_line_info_rec.SHIP_TO_CONTACT_PHONE;
5684 g_ship_cont_email := x_order_line_info_rec.SHIP_TO_CONTACT_EMAIL;
5685 g_deliver_cont_phone  := x_order_line_info_rec.DELIVER_TO_CONTACT_PHONE;
5686 g_deliver_cont_email  := x_order_line_info_rec.DELIVER_TO_CONTACT_EMAIL;
5687 g_ship_cont_name  := x_order_line_info_rec.SHIP_TO_CONTACT_NAME;
5688 g_deliver_cont_name := x_order_line_info_rec.DELIVER_TO_CONTACT_NAME;
5689 g_ship_cust_name  := x_order_line_info_rec.SHIP_TO_CUSTOMER_NAME;
5690 g_ship_cust_location  := x_order_line_info_rec.SHIP_TO_CUSTOMER_LOCATION;
5691 g_deliver_cust_name     := x_order_line_info_rec.DELIVER_TO_CUSTOMER_NAME;
5692 g_deliver_cust_location := x_order_line_info_rec.DELIVER_TO_CUSTOMER_LOCATION;
5693 g_ship_contact_fax  := x_order_line_info_rec.SHIP_TO_CONTACT_FAX;
5694 g_deliver_contact_name  := x_order_line_info_rec.DELIVER_TO_CONTACT_NAME;
5695 g_deliver_contact_fax := x_order_line_info_rec.DELIVER_TO_CONTACT_FAX;
5696 g_shipping_method := x_order_line_info_rec.SHIPPING_METHOD;
5697 g_shipping_instructions := x_order_line_info_rec.SHIPPING_INSTRUCTIONS;
5698 g_packing_instructions  := x_order_line_info_rec.PACKING_INSTRUCTIONS;
5699 g_customer_product_desc := x_order_line_info_rec.CUSTOMER_PRODUCT_DESCRIPTION;
5700 g_customer_po_number  := x_order_line_info_rec.CUSTOMER_PO_NUMBER;
5701 g_customer_po_line_num  := x_order_line_info_rec.CUSTOMER_PO_LINE_NUMBER;
5702 g_customer_po_shipment_num := x_order_line_info_rec.CUSTOMER_PO_SHIPMENT_NUMBER;
5703 
5704 RETURN 1.0;
5705 END ;
5706 
5707 
5708 function getShipContPhone RETURN VARCHAR2 is
5709 begin
5710 RETURN g_ship_cont_phone;
5711 END ;
5712 
5713 function getShipContEmail RETURN VARCHAR2 is
5714 begin
5715 RETURN g_ship_cont_email;
5716 END ;
5717 
5718 function getDeliverContPhone RETURN VARCHAR2 is
5719 begin
5720 RETURN g_deliver_cont_phone;
5721 END ;
5722 
5723 function getDeliverContEmail RETURN VARCHAR2 is
5724 begin
5725 RETURN g_deliver_cont_email;
5726 END ;
5727 
5728 function getShipContName RETURN VARCHAR2 is
5729 begin
5730 RETURN g_ship_cont_name;
5731 END ;
5732 
5733 function getDeliverContName RETURN VARCHAR2 is
5734 begin
5735 RETURN g_deliver_cont_name;
5736 END ;
5737 
5738 function getShipCustName RETURN VARCHAR2 is
5739 begin
5740 RETURN g_ship_cust_name;
5741 END ;
5742 
5743 function getShipCustLocation RETURN VARCHAR2 is
5744 begin
5745 RETURN g_ship_cust_location;
5746 END ;
5747 
5748 function getDeliverCustName RETURN VARCHAR2 is
5749 begin
5750 RETURN g_deliver_cust_name;
5751 END ;
5752 
5753 
5754 function getDeliverCustLocation RETURN VARCHAR2 is
5755 begin
5756 RETURN g_deliver_cust_location;
5757 END ;
5758 
5759 function getShipContactfax  return VARCHAR2 is
5760 begin
5761   return g_ship_contact_fax;
5762 end;
5763 function getDeliverContactName  return VARCHAR2 is
5764 begin
5765   return g_deliver_contact_name;
5766 end;
5767 function getDeliverContactFax return VARCHAR2 is
5768 begin
5769   return g_deliver_contact_fax;
5770 end;
5771 function getShippingMethod  return VARCHAR2 is
5772 begin
5773   return g_shipping_method;
5774 end;
5775 function getShippingInstructions return VARCHAR2 is
5776 begin
5777   return g_shipping_instructions;
5778 end;
5779 function getPackingInstructions return VARCHAR2 is
5780 begin
5781   return g_packing_instructions;
5782 end;
5783 function getCustomerProductDesc return VARCHAR2 is
5784 begin
5785   return g_customer_product_desc;
5786 end;
5787 function getCustomerPoNumber  return VARCHAR2 is
5788 begin
5789   return g_customer_po_number;
5790 end;
5791 function getCustomerPoLineNum return VARCHAR2 is
5792 begin
5793   return g_customer_po_line_num;
5794 end;
5795 function getCustomerPoShipmentNum return VARCHAR2 is
5796 begin
5797   return g_customer_po_shipment_num;
5798 end;
5799 
5800 function getDocumentId RETURN NUMBER is
5801 begin
5802 RETURN g_document_id;
5803 END ;
5804 
5805 
5806 function getRevisionNum RETURN NUMBER is
5807 begin
5808 RETURN g_revision_num;
5809 END ;
5810 
5811 function getVendorId RETURN NUMBER is
5812 begin
5813 RETURN g_vendor_id;
5814 END ;
5815 
5816 function getCoverMessage RETURN VARCHAR2 is
5817 begin
5818 RETURN g_cover_message;
5819 END ;
5820 
5821 function getAmendmentMessage RETURN VARCHAR2 is
5822 begin
5823 RETURN g_amendment_message;
5824 END ;
5825 
5826 function getTimezone RETURN VARCHAR2 is
5827 begin
5828   RETURN g_timezone;
5829 end;
5830 
5831 function getTestFlag RETURN VARCHAR2 is
5832 begin
5833 RETURN g_test_flag;
5834 END ;
5835 
5836 function getReleaseHeaderId RETURN VARCHAR2 is
5837 begin
5838 RETURN g_release_header_id ;
5839 END ;
5840 
5841 function getLocationInfo(p_location_id in number) return number is
5842 begin
5843 
5844   if PO_COMMUNICATION_PVT.g_location_id <> p_location_id  or
5845      PO_COMMUNICATION_PVT.g_location_id is null then
5846 
5847      PO_COMMUNICATION_PVT.g_location_id := p_location_id;
5848 
5849   PO_COMMUNICATION_PVT.g_address_line1 := null;
5850   PO_COMMUNICATION_PVT.g_address_line2 := null;
5851   PO_COMMUNICATION_PVT.g_address_line3 := null;
5852   PO_COMMUNICATION_PVT.g_Territory_short_name := null;
5853   PO_COMMUNICATION_PVT.g_address_info := null;
5854   PO_COMMUNICATION_PVT.g_location_name := null;
5855   PO_COMMUNICATION_PVT.g_phone := null;
5856   PO_COMMUNICATION_PVT.g_fax := null;
5857   PO_COMMUNICATION_PVT.g_address_line4 := null;
5858 --bug#3438608
5859         PO_COMMUNICATION_PVT.g_town_or_city:=null;
5860   PO_COMMUNICATION_PVT.g_state_or_province :=null;
5861   PO_COMMUNICATION_PVT.g_postal_code :=null;
5862 --bug#3438608
5863 
5864 --bug#3438608 passed the out variables PO_COMMUNICATION_PVT.g_town_or_city
5865 --PO_COMMUNICATION_PVT.g_postal_code,PO_COMMUNICATION_PVT.g_state_or_province
5866 --to the procedure PO_HR_LOCATION.get_alladdress_lines
5867 
5868       -- bug#3580225: changed the procedure name to  get_alladdress_lines from get_address--
5869       po_hr_location.get_alladdress_lines(p_location_id,
5870                                 PO_COMMUNICATION_PVT.g_address_line1,
5871                                 PO_COMMUNICATION_PVT.g_address_line2,
5872                                 PO_COMMUNICATION_PVT.g_address_line3,
5873                                 PO_COMMUNICATION_PVT.g_Territory_short_name,
5874                                 PO_COMMUNICATION_PVT.g_address_info,
5875         PO_COMMUNICATION_PVT.g_location_name,
5876         PO_COMMUNICATION_PVT.g_phone,
5877         PO_COMMUNICATION_PVT.g_fax,
5878         PO_COMMUNICATION_PVT.g_address_line4,
5879         PO_COMMUNICATION_PVT.g_town_or_city,
5880         PO_COMMUNICATION_PVT.g_postal_code,
5881         PO_COMMUNICATION_PVT.g_state_or_province);
5882 
5883   end if;
5884   return p_location_id;
5885 
5886 end;
5887 
5888 
5889 function getAddressLine1 return varchar2 is
5890 begin
5891    return PO_COMMUNICATION_PVT.g_address_line1;
5892 end;
5893 function getAddressLine2 return varchar2 is
5894 begin
5895    return PO_COMMUNICATION_PVT.g_address_line2;
5896 end;
5897 function getAddressLine3 return varchar2 is
5898 begin
5899    return PO_COMMUNICATION_PVT.g_address_line3;
5900 end;
5901 
5902 function getTerritoryShortName return varchar2 is
5903 begin
5904    return PO_COMMUNICATION_PVT.g_Territory_short_name;
5905 end;
5906 
5907 function getAddressInfo return varchar2 is
5908 begin
5909    return PO_COMMUNICATION_PVT.g_address_info;
5910 end;
5911 --bug#3438608 added three function getTownOrCity
5912 --getPostalCode and getStateOrProvince
5913 --toreturn the values in global variables
5914 --po_communication_pvt.g_town_or_city
5915 --po_communication_pvt.g_postal_code
5916 --and po_communication_pvt.g_state_or_province.
5917 --These functions are  called by the PO_HEADERS_CHANGE_PRINT
5918 --report
5919 
5920 function getTownOrCity return varchar2 is
5921 begin
5922   return PO_COMMUNICATION_PVT.g_town_or_city;
5923 end;
5924 
5925 function getPostalCode return varchar2 is
5926 begin
5927   return PO_COMMUNICATION_PVT.g_postal_code;
5928 end;
5929 
5930 function getStateOrProvince return varchar2 is
5931 begin
5932   return PO_COMMUNICATION_PVT.g_state_or_province;
5933 end;
5934 --bug#3438608
5935 
5936 /*Bug 4504228 START Retrieving the phone and email contact of buyer from
5937  per_all_people_f rather than the hr_locations.So commenting out
5938  the below function and adding two functions getPhone(p_agent_id)
5939  and getEmail()*/
5940 
5941 /*function getPhone return varchar2 is
5942 begin
5943 	return PO_COMMUNICATION_PVT.g_phone;
5944 end; */
5945 
5946 function getPhone(p_agent_id in number) return varchar2 is
5947  begin
5948      if PO_COMMUNICATION_PVT.g_person_id <> p_agent_id  or
5949      PO_COMMUNICATION_PVT.g_person_id is null then
5950 
5951       PO_COMMUNICATION_PVT.g_person_id := p_agent_id;
5952       PO_COMMUNICATION_PVT.g_buyer_phone := null;
5953       PO_COMMUNICATION_PVT.g_buyer_email_address := null;
5954       PO_COMMUNICATION_PVT.g_buyer_fax := null; --Bug5671523
5955 
5956      /* Bug5191404  Buyer phone number was incorrectly taken from office details
5957          tab.Now with this fix the buyers phone willbe taken from per_phones work
5958          phone type.Commenting out the below sql and adding a new sql*/
5959 
5960 -- bug#5999438 modified the below sqls where clause date checking condition for work phone and work fax.
5961 -- added nvl to pph.date_from
5962 
5963        SELECT
5964             pap.email_address,
5965             pph.phone_number
5966        INTO PO_COMMUNICATION_PVT.g_buyer_email_address,
5967             PO_COMMUNICATION_PVT.g_buyer_phone
5968        FROM per_phones pph,
5969             per_all_people_f pap
5970        WHERE pph.parent_id(+)=pap.person_id
5971             AND pph.parent_table(+)='PER_ALL_PEOPLE_F'
5972             AND pph.phone_type (+)= 'W1'
5973             AND pap.person_id = p_agent_id
5974             AND trunc(sysdate) BETWEEN pap.effective_start_date AND pap.effective_end_date
5975 	    AND trunc(sysdate) BETWEEN nvl(pph.date_from,trunc(sysdate)) AND nvl(pph.date_to,trunc(sysdate))
5976 	    AND ROWNUM = 1;  --	Bug5671523
5977       /* Bug5191404   End */
5978 
5979       --Bug5671523 start
5980       SELECT
5981              pph.phone_number
5982         INTO PO_COMMUNICATION_PVT.g_buyer_fax
5983         FROM per_phones pph,
5984              per_all_people_f pap
5985        WHERE pph.parent_id(+)=pap.person_id
5986          AND pph.parent_table(+)='PER_ALL_PEOPLE_F'
5987          AND pph.phone_type(+)='WF'
5988          AND pap.person_id = p_agent_id
5989          AND trunc(sysdate) BETWEEN pap.effective_start_date AND pap.effective_end_date
5990          AND trunc(sysdate) BETWEEN nvl(pph.date_from,trunc(sysdate)) AND nvl(pph.date_to,trunc(sysdate))  -- bug#5999438
5991          AND ROWNUM = 1;
5992    --Bug5671523 end
5993 
5994      end if;
5995 
5996 --Bug4686436    return PO_COMMUNICATION_PVT.g_phone;
5997    return PO_COMMUNICATION_PVT.g_buyer_phone;
5998  end;
5999 
6000 function getEmail return varchar2 is
6001  begin
6002  return PO_COMMUNICATION_PVT.g_buyer_email_address;
6003 end;
6004 
6005 /*Bug 4504228 END */
6006 
6007 function getFax return varchar2 is
6008 begin
6009  --Bug5671523 return PO_COMMUNICATION_PVT.g_fax;
6010  return PO_COMMUNICATION_PVT.g_buyer_fax;
6011 end;
6012 function getLocationName return varchar2 is
6013 begin
6014   return PO_COMMUNICATION_PVT.g_location_name;
6015 end;
6016 
6017 /* Bug#3580225: Changed the function to call po_hr_location.get_alladdress_lines PROCEDURE*/
6018 function getOperationInfo(p_org_id in NUMBER) return varchar2 is
6019 l_address_line4 varchar2(240) :=null;
6020 l_ou_location_code HR_LOCATIONS.LOCATION_CODE%type := null;
6021 l_ou_phone  HR_LOCATIONS.TELEPHONE_NUMBER_1%type := null;
6022 l_ou_fax  HR_LOCATIONS.TELEPHONE_NUMBER_2%type := null;
6023 l_address_info varchar2(500) := null;
6024 l_location_id PO_HR_LOCATIONS.LOCATION_ID%type := null;
6025 
6026 begin
6027   if PO_COMMUNICATION_PVT.g_org_id <> p_org_id  or
6028        PO_COMMUNICATION_PVT.g_org_id is null then
6029 
6030        PO_COMMUNICATION_PVT.g_org_id := p_org_id;
6031 
6032     PO_COMMUNICATION_PVT.g_ou_name  := null;
6033     PO_COMMUNICATION_PVT.g_ou_address_line_1  := null;
6034     PO_COMMUNICATION_PVT.g_ou_address_line_2  := null;
6035     PO_COMMUNICATION_PVT.g_ou_address_line_3  := null;
6036     PO_COMMUNICATION_PVT.g_ou_town_or_city  := null;
6037     PO_COMMUNICATION_PVT.g_ou_region2  := null;
6038     PO_COMMUNICATION_PVT.g_ou_postal_code  := null;
6039     PO_COMMUNICATION_PVT.g_ou_country  := null;
6040 
6041     /*select name and location id from hr_all_organization_units*/
6042 
6043     SELECT name, location_id into PO_COMMUNICATION_PVT.g_ou_name, l_location_id
6044     FROM hr_all_organization_units
6045     WHERE organization_id = p_org_id;
6046 
6047     /* Call get_alladdress_lines procedure to retrieve address details*/
6048 
6049     po_hr_location.get_alladdress_lines(l_location_id,
6050                                 PO_COMMUNICATION_PVT.g_ou_address_line_1,
6051                                 PO_COMMUNICATION_PVT.g_ou_address_line_2,
6052                                 PO_COMMUNICATION_PVT.g_ou_address_line_3,
6053                                 PO_COMMUNICATION_PVT.g_ou_country,
6054                                 l_address_info,
6055         l_ou_location_code,
6056         l_ou_phone,
6057         l_ou_fax,
6058         l_address_line4,
6059         PO_COMMUNICATION_PVT.g_ou_town_or_city,
6060         PO_COMMUNICATION_PVT.g_ou_postal_code,
6061         PO_COMMUNICATION_PVT.g_ou_region2);
6062 
6063 
6064   end if;
6065 return PO_COMMUNICATION_PVT.g_ou_name;
6066 end;
6067 
6068 
6069 function getOUAddressLine1 return varchar2 is
6070 begin
6071   return PO_COMMUNICATION_PVT.g_ou_address_line_1;
6072 end;
6073 function getOUAddressLine2 return varchar2 is
6074 begin
6075   return PO_COMMUNICATION_PVT.g_ou_address_line_2;
6076 end;
6077 function getOUAddressLine3 return varchar2 is
6078 begin
6079   return PO_COMMUNICATION_PVT.g_ou_address_line_3;
6080 end;
6081 function getOUTownCity return varchar2 is
6082 begin
6083   return PO_COMMUNICATION_PVT.g_ou_town_or_city;
6084 end;
6085 function getOURegion2 return varchar2 is
6086 begin
6087   return PO_COMMUNICATION_PVT.g_ou_region2;
6088 end;
6089 function getOUPostalCode return varchar2 is
6090 begin
6091   return PO_COMMUNICATION_PVT.g_ou_postal_code;
6092 end;
6093 
6094 /*  Function retuns the Operation Unit country value that
6095   retreived in getOperationInfo function.
6096 */
6097 
6098 function getOUCountry return varchar2 is
6099 begin
6100   return PO_COMMUNICATION_PVT.g_ou_country;
6101 end;
6102 
6103 
6104 
6105 function getSegmentNum(p_header_id in NUMBER) return VARCHAR2 is
6106 begin
6107 
6108   -- bug5386806
6109   -- If p_header_id is null, set all dependent fields to NULL
6110   IF (p_header_id IS NULL) THEN
6111 
6112     PO_COMMUNICATION_PVT.g_header_id := NULL;
6113     PO_COMMUNICATION_PVT.g_quote_number := NULL;
6114     PO_COMMUNICATION_PVT.g_agreement_number := NULL;
6115     PO_COMMUNICATION_PVT.g_agreement_flag := NULL;
6116 
6117   ELSIF PO_COMMUNICATION_PVT.g_header_id <> p_header_id  or
6118        PO_COMMUNICATION_PVT.g_header_id is null then
6119 
6120     PO_COMMUNICATION_PVT.g_header_id := p_header_id;
6121 
6122     Select ph.QUOTE_VENDOR_QUOTE_NUMBER, ph.SEGMENT1, ph.GLOBAL_AGREEMENT_FLAG into
6123       PO_COMMUNICATION_PVT.g_quote_number, PO_COMMUNICATION_PVT.g_agreement_number,
6124       PO_COMMUNICATION_PVT.g_agreement_flag
6125     FROM
6126       po_headers_all ph
6127     WHERE
6128       ph.PO_HEADER_ID =  p_header_id;
6129 
6130 
6131   end if;
6132 
6133   RETURN PO_COMMUNICATION_PVT.g_agreement_number;
6134 
6135 end;
6136 
6137 function getAgreementLineNumber return VARCHAR2 is
6138 begin
6139   return PO_COMMUNICATION_PVT.g_agreementLine_number;
6140 end;
6141 function getQuoteNumber return VARCHAR2 is
6142 begin
6143   return PO_COMMUNICATION_PVT.g_quote_number;
6144 end;
6145 
6146 function getAgreementFlag return VARCHAR2 is
6147 begin
6148   return PO_COMMUNICATION_PVT.g_agreement_flag;
6149 end;
6150 
6151 function getAgreementLineNumber(p_line_id in NUMBER) return NUMBER is
6152 begin
6153   if PO_COMMUNICATION_PVT.g_line_id <> p_line_id  or
6154        PO_COMMUNICATION_PVT.g_line_id is null then
6155 
6156     PO_COMMUNICATION_PVT.g_line_id := p_line_id;
6157 
6158     Select LINE_NUM into PO_COMMUNICATION_PVT.g_agreementLine_number
6159     FROM PO_LINES_ALL
6160     WHERE PO_LINE_ID = p_line_id;
6161   end if;
6162 return PO_COMMUNICATION_PVT.g_agreementLine_number;
6163 
6164 end;
6165 
6166 function getArcBuyerAgentID(p_header_id in NUMBER) return NUMBER is
6167 begin
6168   if PO_COMMUNICATION_PVT.g_header_id1 <> p_header_id  or
6169        PO_COMMUNICATION_PVT.g_header_id1 is null then
6170 
6171     PO_COMMUNICATION_PVT.g_header_id1 := p_header_id;
6172 
6173     PO_COMMUNICATION_PVT.g_arcBuyer_fname := null;
6174     PO_COMMUNICATION_PVT.g_arcBuyer_lname := null;
6175     PO_COMMUNICATION_PVT.g_arcAgent_id := null;
6176 
6177     SELECT  HRE.FIRST_NAME,
6178       HRE.LAST_NAME,
6179       HRE.TITLE,
6180       PHA.AGENT_ID
6181     INTO PO_COMMUNICATION_PVT.g_arcBuyer_fname, PO_COMMUNICATION_PVT.g_arcBuyer_lname,
6182          PO_COMMUNICATION_PVT.g_arcBuyer_title, PO_COMMUNICATION_PVT.g_arcAgent_id
6183 
6184     FROM
6185       PER_ALL_PEOPLE_F HRE,
6186       PO_HEADERS_ARCHIVE_ALL PHA
6187     WHERE
6188       HRE.PERSON_ID = PHA.AGENT_ID AND
6189       --HRE.EMPLOYEE_NUMBER IS NOT NULL AND    --<R12 CWK Enhancemment>
6190       TRUNC(SYSDATE) BETWEEN HRE.EFFECTIVE_START_DATE AND HRE.EFFECTIVE_END_DATE AND
6191       PHA.PO_HEADER_ID = p_header_id AND
6192       PHA.REVISION_NUM = 0 ;
6193   end if;
6194 
6195   return g_arcAgent_id;
6196 
6197 end;
6198 
6199 function getArcBuyerFName return VARCHAR2 is
6200 begin
6201   return PO_COMMUNICATION_PVT.g_arcBuyer_fname;
6202 end;
6203 
6204 function getArcBuyerLName return VARCHAR2 is
6205 begin
6206   return PO_COMMUNICATION_PVT.g_arcBuyer_lname;
6207 end;
6208 
6209 function getArcBuyerTitle return VARCHAR2 is
6210 begin
6211   return PO_COMMUNICATION_PVT.g_arcBuyer_title;
6212 end;
6213 
6214 
6215 function getRelArcBuyerAgentID(p_release_id in NUMBER) return NUMBER is
6216 begin
6217   if PO_COMMUNICATION_PVT.g_release_id <> p_release_id  or
6218        PO_COMMUNICATION_PVT.g_release_id is null then
6219 
6220     PO_COMMUNICATION_PVT.g_release_id := p_release_id;
6221 
6222     PO_COMMUNICATION_PVT.g_arcBuyer_fname := null;
6223     PO_COMMUNICATION_PVT.g_arcBuyer_lname := null;
6224     PO_COMMUNICATION_PVT.g_arcAgent_id := null;
6225 
6226     SELECT  HRE.FIRST_NAME,
6227       HRE.LAST_NAME,
6228       PHA.AGENT_ID
6229     INTO PO_COMMUNICATION_PVT.g_arcBuyer_fname, PO_COMMUNICATION_PVT.g_arcBuyer_lname, PO_COMMUNICATION_PVT.g_arcAgent_id
6230 
6231     FROM
6232       PER_ALL_PEOPLE_F HRE,
6233       PO_RELEASES_ARCHIVE_ALL PHA
6234     WHERE
6235       HRE.PERSON_ID = PHA.AGENT_ID AND
6236       -- HRE.EMPLOYEE_NUMBER IS NOT NULL AND   --<R12 CWK Enhancemment>
6237       TRUNC(SYSDATE) BETWEEN HRE.EFFECTIVE_START_DATE AND HRE.EFFECTIVE_END_DATE AND
6238       PHA.PO_RELEASE_ID = p_release_id AND
6239       PHA.REVISION_NUM = 0 ;
6240   end if;
6241 
6242   return g_arcAgent_id;
6243 
6244 end;
6245 
6246 function getVendorAddressLine1(p_vendor_site_id in NUMBER) return VARCHAR2 is
6247 
6248 l_city    PO_VENDOR_SITES.city%type := null;
6249 l_state   PO_VENDOR_SITES.state%type := null;
6250 l_zip   PO_VENDOR_SITES.zip%type := null;
6251 l_address_line_1  PO_VENDOR_SITES.ADDRESS_LINE1%type := null;
6252 
6253 begin
6254 
6255 if PO_COMMUNICATION_PVT.g_vendor_site_id <> p_vendor_site_id  or
6256        PO_COMMUNICATION_PVT.g_vendor_site_id is null then
6257 
6258   PO_COMMUNICATION_PVT.g_vendor_address_line_2 :=null;
6259   PO_COMMUNICATION_PVT.g_vendor_address_line_3 :=null;
6260   PO_COMMUNICATION_PVT.g_vendor_country :=null;
6261   PO_COMMUNICATION_PVT.g_vendor_city_state_zipInfo :=null;
6262   PO_COMMUNICATION_PVT.g_vendor_address_line_4 :=null; --bug: 3463617
6263 
6264   --bug: 3463617 : Retreived address_line4 from po_vendor_sites_all.
6265   SELECT  PVS.ADDRESS_LINE1 ,
6266     PVS.ADDRESS_LINE2 ,
6267     PVS.ADDRESS_LINE3 ,
6268     PVS.CITY ,
6269     DECODE(PVS.STATE, NULL, DECODE(PVS.PROVINCE, NULL, PVS.COUNTY, PVS.PROVINCE), PVS.STATE),
6270     PVS.ZIP ,
6271     FTE.TERRITORY_SHORT_NAME,
6272     PVS.ADDRESS_LINE4 --bug: 3463617
6273     INTO
6274     l_address_line_1, PO_COMMUNICATION_PVT.g_vendor_address_line_2, PO_COMMUNICATION_PVT.g_vendor_address_line_3,
6275     l_city, l_state, l_zip, PO_COMMUNICATION_PVT.g_vendor_country, PO_COMMUNICATION_PVT.g_vendor_address_line_4
6276   FROM
6277     PO_VENDOR_SITES_ALL PVS,
6278     FND_TERRITORIES_TL FTE
6279   WHERE
6280     PVS.COUNTRY = FTE.TERRITORY_CODE  AND
6281     DECODE(FTE.TERRITORY_CODE, NULL, '1', FTE.LANGUAGE) = DECODE(FTE.TERRITORY_CODE, NULL, '1', USERENV('LANG')) AND
6282     PVS.VENDOR_SITE_ID = p_vendor_site_id ;
6283 
6284 
6285   If (l_city is null) then
6286            PO_COMMUNICATION_PVT.g_vendor_city_state_zipInfo := l_state||' '|| l_zip;
6287         else
6288            PO_COMMUNICATION_PVT.g_vendor_city_state_zipInfo := l_city||','||l_state||' '|| l_zip;
6289         end if;
6290 end if;
6291 
6292   return l_address_line_1;
6293 
6294 end;
6295 
6296 function getVendorAddressLine2 return VARCHAR2 is
6297 begin
6298   return PO_COMMUNICATION_PVT.g_vendor_address_line_2;
6299 end;
6300 function getVendorAddressLine3 return VARCHAR2 is
6301 begin
6302   return PO_COMMUNICATION_PVT.g_vendor_address_line_3;
6303 end;
6304 function getVendorCityStateZipInfo return VARCHAR2 is
6305 begin
6306   return PO_COMMUNICATION_PVT.g_vendor_city_state_zipInfo;
6307 end;
6308 function getVendorCountry return VARCHAR2 is
6309 begin
6310   return PO_COMMUNICATION_PVT.g_vendor_country ;
6311 end;
6312 
6313 
6314 function getJob(p_job_id in NUMBER) return VARCHAR2 is
6315 begin
6316   if PO_COMMUNICATION_PVT.g_job_id <> p_job_id  or
6317        PO_COMMUNICATION_PVT.g_job_id is null then
6318 
6319     PO_COMMUNICATION_PVT.g_job_name := null;
6320 
6321     SELECT
6322       name
6323     INTO
6324       PO_COMMUNICATION_PVT.g_job_name
6325     FROM
6326       PER_JOBS_VL
6327     WHERE
6328       job_id = p_job_id;
6329   end if;
6330 
6331   return PO_COMMUNICATION_PVT.g_job_name;
6332 end;
6333 
6334 function getDocumentType return VARCHAR2 is
6335 begin
6336   return PO_COMMUNICATION_PVT.g_documentType;
6337 end;
6338 
6339 function getFormatMask return VARCHAR2 is
6340 begin
6341   if PO_COMMUNICATION_PVT.g_currency_code <> g_current_currency_code  or
6342        PO_COMMUNICATION_PVT.g_currency_code  is null then
6343 
6344     PO_COMMUNICATION_PVT.g_currency_code := PO_COMMUNICATION_PVT.g_current_currency_code;
6345     PO_COMMUNICATION_PVT.g_format_mask :=null;
6346 
6347     g_format_mask := FND_CURRENCY.GET_FORMAT_MASK(g_current_currency_code,60);
6348   end if;
6349 
6350   return PO_COMMUNICATION_PVT.g_format_mask;
6351 
6352 end;
6353 
6354 function getLegalEntityName return VARCHAR2 is
6355 begin
6356   return PO_COMMUNICATION_PVT.g_buyer_org;
6357 end;
6358 
6359 function IsDocumentSigned(p_header_id in Number) return VARCHAR2 is
6360 l_signed   boolean;
6361 l_signatures VARCHAR2(1) := 'N'; -- bug#3297926
6362 begin
6363 
6364  -- bug#3297926 Start --
6365  --l_signed :=  PO_SIGNATURE_PVT.Was_Signature_Required(p_document_id => p_header_id);
6366  -- SQL What:Checks if there is any record in the PO_ACTION_HISTORY with the
6367  --          action code as 'SIGNED' and revision less than current revision.
6368  -- SQL Why :To find out if the document was ever signed
6369    begin
6370          SELECT 'Y'
6371            INTO l_signatures
6372            FROM dual
6373           WHERE EXISTS (SELECT 1
6374                           FROM PO_ACTION_HISTORY
6375                          WHERE object_id = p_header_id
6376                            AND object_type_code IN ('PO','PA')
6377                            AND action_code = 'SIGNED'
6378          AND OBJECT_REVISION_NUM < PO_COMMUNICATION_PVT.g_revision_num);
6379 
6380      IF l_signatures = 'Y' THEN
6381         l_signed := TRUE;
6382      ELSE
6383         l_signed := FALSE;
6384      END IF;
6385 
6386    EXCEPTION
6387      WHEN NO_DATA_FOUND THEN
6388        l_signed := FALSE;
6389 END; -- End of bug#3297926  --
6390 
6391 IF l_signed  THEN
6392   RETURN FND_API.G_TRUE;
6393 ELSE
6394    RETURN FND_API.G_FALSE;
6395 END IF;
6396 
6397 
6398 end;
6399 
6400 -- <Start Word Integration 11.5.10+>
6401 
6402 /*
6403 	This function frames a document's file name
6404    given the passed in parameters.
6405 */
6406 FUNCTION getDocFileName(p_document_type varchar2,
6407                         p_terms varchar2,
6408                         p_orgid number,
6409                         p_document_id varchar2,
6410                         p_revision_num number,
6411                         p_language_code varchar2,
6412                         p_extension varchar2) RETURN varchar2 IS
6413 
6414   l_po_number po_headers_all.segment1%type;
6415   l_language_code fnd_languages.language_code%type;
6416   l_api_name       CONSTANT    VARCHAR2(25):= 'PDFFileName';
6417   l_file_name  fnd_lobs.file_name%type;
6418   l_progress   VARCHAR2(3);
6419 BEGIN
6420 
6421   l_progress := '000';
6422 
6423   IF g_debug_stmt THEN
6424      PO_DEBUG.debug_begin(p_log_head => g_log_head || l_api_name);
6425      PO_DEBUG.debug_var(g_log_head || l_api_name, l_progress, 'p_document_id', p_document_id);
6426      PO_DEBUG.debug_var(g_log_head || l_api_name, l_progress, 'p_document_type', p_document_type);
6427   END IF;
6428 
6429   BEGIN
6430 
6431     -- If the language code is null the get the userenv language.
6432     IF p_language_code IS NULL THEN
6433       SELECT userenv('LANG') INTO l_language_code FROM dual;
6434     ELSE
6435       l_language_code := p_language_code;
6436     END IF;
6437 
6438     l_progress := '020';
6439 
6440     IF g_debug_stmt THEN
6441        PO_DEBUG.debug_var(g_log_head || l_api_name, l_progress, 'l_language_code', l_language_code);
6442     END IF;
6443 
6444     -- Query for getting the PO number i.e segment1.
6445     IF p_document_type in ('PO','PA') THEN
6446       SELECT ph.segment1 into l_po_number
6447       FROM  po_headers_all ph
6448       WHERE po_header_id = p_document_id ;
6449     ELSE
6450       SELECT ph.segment1  into l_po_number
6451       FROM po_headers_all ph,po_releases_all pr
6452       WHERE  ph.po_header_id = pr.po_header_id and pr.po_release_id = p_document_id ;
6453     END IF;
6454 
6455   EXCEPTION
6456     WHEN others THEN l_po_number := NULL;
6457   END;
6458 
6459   --if the po number is null assign the document id to po number.
6460   IF l_po_number IS NULL THEN
6461     l_po_number := p_document_id;
6462   END IF;
6463 
6464   l_file_name := p_document_type||p_terms||p_orgid||'_'||l_po_number||'_'||p_revision_num||'_'||l_language_code||p_extension;
6465 
6466   l_progress := '900';
6467   IF g_debug_stmt THEN
6468      PO_DEBUG.debug_var(g_log_head || l_api_name, l_progress, 'l_file_name', l_file_name);
6469      PO_DEBUG.debug_end(g_log_head || l_api_name);
6470   END IF;
6471 
6472   RETURN  l_file_name;
6473 
6474 END getDocFileName;
6475 
6476 
6477 -------------------------------------------------------------------------------
6478 --Start of Comments
6479 --Name: getPDFFileName
6480 --Pre-reqs:
6481 --  None.
6482 --Modifies:
6483 --  None.
6484 --Locks:
6485 --  None.
6486 --Function:
6487 --  Given parameters, returns a file name for an pdf file to use when
6488 --  representing the document.
6489 --Parameters:
6490 --IN:
6491 -- p_document_type: either 'PO' or 'PA'
6492 -- p_terms: either '_' or '_TERMS_'
6493 -- p_orgid: org id of the document
6494 -- p_document_id: document id of a document.
6495 -- p_revision_num: revision of the document
6496 -- p_language_code: language short code, e.g. 'US' or 'KO'
6497 --Testing:
6498 --
6499 --End of Comments
6500 -------------------------------------------------------------------------------
6501 FUNCTION getPDFFileName(p_document_type varchar2,
6502                         p_terms varchar2,
6503                         p_orgid number,
6504                         p_document_id varchar2,
6505                         p_revision_num number,
6506                         p_language_code varchar2) RETURN varchar2 IS
6507 BEGIN
6508 
6509   RETURN getDocFileName( p_document_type => p_document_type
6510                        , p_terms => p_terms
6511                        , p_orgid => p_orgid
6512                        , p_document_id => p_document_id
6513                        , p_revision_num => p_revision_num
6514                        , p_language_code => p_language_code
6515                        , p_extension => '.pdf' );
6516 
6517 END getPDFFileName;
6518 
6519 -------------------------------------------------------------------------------
6520 --Start of Comments
6521 --Name: getRTFFileName
6522 --Pre-reqs:
6523 --  None.
6524 --Modifies:
6525 --  None.
6526 --Locks:
6527 --  None.
6528 --Function:
6529 --  Given parameters, returns a file name for an rtf file to use when
6530 --  representing the document.
6531 --Parameters:
6532 --IN:
6533 -- p_document_type: either 'PO' or 'PA'
6534 -- p_terms: either '_' or '_TERMS_'
6535 -- p_orgid: org id of the document
6536 -- p_document_id: document id of a document.
6537 -- p_revision_num: revision of the document
6538 -- p_language_code: language short code, e.g. 'US' or 'KO'
6539 --Testing:
6540 --
6541 --End of Comments
6542 -------------------------------------------------------------------------------
6543 FUNCTION getRTFFileName(p_document_type varchar2,
6544                         p_terms varchar2,
6545                         p_orgid number,
6546                         p_document_id varchar2,
6547                         p_revision_num number,
6548                         p_language_code varchar2) RETURN varchar2 IS
6549 BEGIN
6550 
6551   RETURN getDocFileName( p_document_type => p_document_type
6552                        , p_terms => p_terms
6553                        , p_orgid => p_orgid
6554                        , p_document_id => p_document_id
6555                        , p_revision_num => p_revision_num
6556                        , p_language_code => p_language_code
6557                        , p_extension => '.rtf' );
6558 
6559 END getRTFFileName;
6560 
6561 -- <End Word Integration 11.5.10+>
6562 
6563 -------------------------------------------------------------------------------
6564 --Start of Comments
6565 --Name: getZipFileName
6566 --Pre-reqs:
6567 --  Column EMAIL_ATTACHMENT_FILENAME should exist in table po_system_parameters_all
6568 --Modifies:
6569 --  None.
6570 --Locks:
6571 --  None.
6572 --Function:
6573 --  Queries the value of 'Email Attachment Filename' which acts as filename of the
6574 --  zipped file sent in the communication
6575 --Parameters:
6576 --  p_org_id Id of the organization that we will get the value for.
6577 --Returns:
6578 --  For functions:
6579 --    Returns the values of 'Email Attachment Filename' in Purchasing Options
6580 --    Setup form. Returns 'Attachments.zip' if the value in the database is null.
6581 --Notes:
6582 --  Added as a part of <PO Attachment Support 11i.11>
6583 --  History
6584 --    Bug# 5240634 Changed the signature to take in a org_id
6585 --
6586 --    The function used to construct the zip file name using
6587 --    document type, orgid, document id and revision num as
6588 --    p_document_type||'_'||p_orgid||'_'||l_document_number||'_'||p_revision_num||'.zip';
6589 --    This was changed as per ECO Bug #43877577 to return static string 'Attachments.zip'
6590 --    This was then made a parameter in po_system_parameters_all as per ECO Bug #5069318
6591 --End of Comments
6592 -------------------------------------------------------------------------------
6593 FUNCTION getZIPFileName(p_org_id in number) RETURN VARCHAR2 IS
6594     l_email_attachment_filename po_system_parameters_all.email_attachment_filename%type;
6595     l_progress varchar2(200);
6596     d_progress NUMBER;
6597     d_module   VARCHAR2(70) := 'PO_COMMUNICATION_PVT.getZIPFileName';
6598 BEGIN
6599     d_progress := 0;
6600     IF (PO_LOG.d_proc) THEN
6601       PO_LOG.proc_begin(d_module);
6602     END IF;
6603 
6604     select nvl(psp.email_attachment_filename,'Attachments.zip')
6605     into l_email_attachment_filename
6606     from po_system_parameters_all psp
6607     where org_id = p_org_id;
6608 
6609     d_progress := 10;
6610     IF (PO_LOG.d_proc) THEN
6611       PO_LOG.proc_begin(d_module, 'Email Attachment Filename', l_email_attachment_filename);
6612       PO_LOG.proc_end(d_module);
6613     END IF;
6614     return l_email_attachment_filename;
6615 Exception
6616     when others then
6617     IF (PO_LOG.d_exc) THEN
6618       PO_LOG.exc(d_module, d_progress, SQLCODE || SQLERRM);
6619     END IF;
6620 END getZIPFileName;
6621 
6622 --bug:346361
6623 function getAddressLine4 return varchar2 is
6624 begin
6625    return PO_COMMUNICATION_PVT.g_address_line4;
6626 end;
6627 
6628 --bug:346361
6629 function getVendorAddressLine4 return VARCHAR2 is
6630 begin
6631   return PO_COMMUNICATION_PVT.g_vendor_address_line_4;
6632 end;
6633 
6634 
6635 /* function to retrieve legal entity details for given Inventory Organization */
6636 
6637 function getLegalEntityDetails(p_org_id in NUMBER) return varchar2 is
6638 
6639 l_location_id HR_LOCATIONS.location_id%type :=null;
6640 l_address_line4 varchar2(240) :=null;
6641 l_legal_entity_location_code HR_LOCATIONS.LOCATION_CODE%type := null;
6642 l_legal_entity_phone  HR_LOCATIONS.TELEPHONE_NUMBER_1%type := null;
6643 l_legal_entity_fax  HR_LOCATIONS.TELEPHONE_NUMBER_2%type := null;
6644 l_address_info varchar2(500) := null;
6645 
6646 /*Bug5983107 */
6647 l_legal_entity_id NUMBER;
6648 x_legalentity_info  xle_utilities_grp.LegalEntity_Rec;
6649 x_return_status	VARCHAR2(20) ;
6650 x_msg_count    NUMBER ;
6651 x_msg_data    VARCHAR2(4000) ;
6652 /*Bug 5983107*/
6653 
6654 
6655 begin
6656 
6657   if PO_COMMUNICATION_PVT.g_legal_entity_org_id <> p_org_id  or
6658        PO_COMMUNICATION_PVT.g_legal_entity_org_id is null then
6659 
6660        PO_COMMUNICATION_PVT.g_legal_entity_org_id := p_org_id;
6661 
6662     PO_COMMUNICATION_PVT.g_legal_entity_name := null;
6663     PO_COMMUNICATION_PVT.g_legal_entity_address_line_1 := null;
6664     PO_COMMUNICATION_PVT.g_legal_entity_address_line_2 := null;
6665     PO_COMMUNICATION_PVT.g_legal_entity_address_line_3 := null;
6666     PO_COMMUNICATION_PVT.g_legal_entity_town_or_city := null;
6667     PO_COMMUNICATION_PVT.g_legal_entity_state := null;
6668     PO_COMMUNICATION_PVT.g_legal_entity_postal_code := null;
6669 
6670     /*Bug5983107 Commenting out the sql below and replacing the API's provided by XLE*/
6671 
6672    /* SELECT name, LOCATION_ID
6673     INTO PO_COMMUNICATION_PVT.g_legal_entity_name, l_location_id
6674     FROM hr_all_organization_units
6675     WHERE to_char(organization_id) = ( SELECT org_information2 FROM hr_organization_information WHERE  org_information_context = 'Accounting Information'
6676               and organization_id = p_org_id ) ;  */
6677 
6678     l_legal_entity_id :=  PO_CORE_S.get_default_legal_entity_id(p_org_id);
6679 
6680      XLE_UTILITIES_GRP.Get_LegalEntity_Info(
6681        		              x_return_status,
6682          	     	      x_msg_count,
6683        		              x_msg_data,
6684                	              null,
6685                	              l_legal_entity_id,
6686            	              x_legalentity_info);
6687 
6688      PO_COMMUNICATION_PVT.g_legal_entity_name := x_legalentity_info.name;
6689      l_location_id := x_legalentity_info.location_id;
6690 
6691      /*End bug5983107 */
6692 
6693 
6694 /* call procedure get_address in po_hr_location package to retrieve
6695     address information for given location id*/
6696 
6697     po_hr_location.get_alladdress_lines(l_location_id,
6698                                 PO_COMMUNICATION_PVT.g_legal_entity_address_line_1,
6699                                 PO_COMMUNICATION_PVT.g_legal_entity_address_line_2,
6700                                 PO_COMMUNICATION_PVT.g_legal_entity_address_line_3,
6701                                 PO_COMMUNICATION_PVT.g_legal_entity_country,
6702                                 l_address_info,
6703         l_legal_entity_location_code,
6704         l_legal_entity_phone,
6705         l_legal_entity_fax,
6706         l_address_line4,
6707         PO_COMMUNICATION_PVT.g_legal_entity_town_or_city,
6708         PO_COMMUNICATION_PVT.g_legal_entity_postal_code,
6709         PO_COMMUNICATION_PVT.g_legal_entity_state);
6710 
6711   end if;
6712   return PO_COMMUNICATION_PVT.g_legal_entity_name ;
6713 
6714 EXCEPTION
6715   WHEN OTHERS THEN
6716   PO_COMMUNICATION_PVT.g_legal_entity_name := null;
6717   PO_COMMUNICATION_PVT.g_legal_entity_address_line_1 := null;
6718   PO_COMMUNICATION_PVT.g_legal_entity_address_line_2 := null;
6719   PO_COMMUNICATION_PVT.g_legal_entity_address_line_3 := null;
6720   PO_COMMUNICATION_PVT.g_legal_entity_town_or_city := null;
6721   PO_COMMUNICATION_PVT.g_legal_entity_state := null;
6722   PO_COMMUNICATION_PVT.g_legal_entity_postal_code := null;
6723   return PO_COMMUNICATION_PVT.g_legal_entity_name ;
6724 
6725 
6726 end getLegalEntityDetails;
6727 
6728 /* start of functions to return legal entity address details */
6729 
6730 function getLEAddressLine1 return varchar2 is
6731 begin
6732    return PO_COMMUNICATION_PVT.g_legal_entity_address_line_1;
6733 end;
6734 
6735 function getLEAddressLine2 return varchar2 is
6736 begin
6737    return PO_COMMUNICATION_PVT.g_legal_entity_address_line_2;
6738 end;
6739 
6740 function getLEAddressLine3 return varchar2 is
6741 begin
6742    return PO_COMMUNICATION_PVT.g_legal_entity_address_line_3;
6743 end;
6744 
6745 function getLECountry return varchar2 is
6746 begin
6747    return PO_COMMUNICATION_PVT.g_legal_entity_country;
6748 end;
6749 
6750 function getLETownOrCity return varchar2 is
6751 begin
6752   return PO_COMMUNICATION_PVT.g_legal_entity_town_or_city;
6753 end;
6754 
6755 function getLEPostalCode return varchar2 is
6756 begin
6757   return PO_COMMUNICATION_PVT.g_legal_entity_postal_code;
6758 end;
6759 
6760 function getLEStateOrProvince return varchar2 is
6761 begin
6762   return PO_COMMUNICATION_PVT.g_legal_entity_state;
6763 end;
6764 
6765 -- end of functions to return legal entity address details --
6766 
6767 /*
6768   Function returns distinct count of shipment level ship to from header level ship to. This count is
6769   used in XSL to identify what to display in ship to address at header and shipment level
6770 */
6771 function getDistinctShipmentCount return number is
6772 begin
6773   return PO_COMMUNICATION_PVT.g_dist_shipto_count;
6774 end;
6775 
6776 /*
6777   Function to retrieve cancel date for Standard, Blanket and Contract PO's
6778 */
6779 
6780 function getPOCancelDate(p_po_header_id in NUMBER) return date is
6781 l_cancel_date date := null;
6782 begin
6783   SELECT   action_date
6784   INTO l_cancel_date
6785   FROM     po_action_history        pah
6786   WHERE    pah.object_id            = p_po_header_id
6787   AND      ((pah.object_type_code   = 'PO'
6788   AND      pah.object_sub_type_code in ('PLANNED','STANDARD'))
6789   OR       (pah.object_type_code    = 'PA'
6790   AND      pah.object_sub_type_code in ('BLANKET','CONTRACT')))
6791   AND      pah.action_code          = 'CANCEL';
6792 
6793 return l_cancel_date;
6794 EXCEPTION
6795     WHEN OTHERS THEN
6796     l_cancel_date :=null;
6797     return l_cancel_date;
6798 
6799 end getPOCancelDate;
6800 
6801 
6802 /*******************************************************************************
6803   FUNCTION NAME :  getCanceledAmount
6804 
6805   Description   : This function retreives Canceled Line amount and Total
6806   line amount for given line id. Returns canceled_amount and populates
6807   g_line_org_amount global variable with original line amount
6808 
6809   Referenced by :
6810   parameters    : p_po_line_id of type number as IN parameter
6811       p_po_revision_num of type number as IN parameter
6812       p_po_header_id of type number as IN parameter
6813 
6814   CHANGE History: Created    MANRAM
6815 ********************************************************************************/
6816 function getCanceledAmount(p_po_line_id IN NUMBER,
6817         p_po_revision_num IN NUMBER,
6818         p_po_header_id IN NUMBER) return varchar2 is
6819 
6820 l_canceled_amount number := null;
6821 l_amount number := null;
6822 begin
6823 
6824   SELECT sum(AMOUNT_CANCELLED), pl.amount
6825   INTO l_canceled_amount, l_amount
6826         FROM po_line_locations_all pll,
6827              po_lines_all pl
6828         WHERE pll.po_line_id = p_po_line_id AND
6829         pll.po_header_id = p_po_header_id AND
6830         pl.po_line_id = pll.po_line_id AND
6831         pll.CANCEL_FLAG = 'Y'
6832         AND pll.shipment_type <> 'PREPAYMENT'  -- <Complex Work R12>
6833   group by pl.amount;
6834 
6835   PO_COMMUNICATION_PVT.g_line_org_amount :=  l_canceled_amount + l_amount ;
6836 
6837 return l_canceled_amount;
6838 
6839   EXCEPTION
6840     WHEN NO_DATA_FOUND THEN
6841       begin
6842         SELECT sum(AMOUNT_CANCELLED), pl.amount
6843         INTO l_canceled_amount, l_amount
6844         FROM po_line_locations_archive_all plla,
6845              po_lines_all pl
6846         WHERE plla.po_line_id = p_po_line_id AND
6847               plla.po_header_id = p_po_header_id AND
6848               plla.revision_num = p_po_revision_num AND
6849               pl.po_line_id = plla.po_line_id AND
6850               plla.CANCEL_FLAG = 'Y'
6851               AND plla.shipment_type <> 'PREPAYMENT'  -- <Complex Work R12>
6852               group by pl.amount;
6853 
6854         PO_COMMUNICATION_PVT.g_line_org_amount :=  l_canceled_amount + l_amount ;
6855 
6856       EXCEPTION
6857         WHEN OTHERS THEN
6858         l_canceled_amount :=null;
6859         l_amount := null;
6860         PO_COMMUNICATION_PVT.g_line_org_amount :=null;
6861         return l_canceled_amount;
6862       end;
6863 
6864 end getCanceledAmount;
6865 
6866 
6867 function getLineOriginalAmount return number is
6868 begin
6869   return PO_COMMUNICATION_PVT.g_line_org_amount;
6870 end;
6871 
6872 /*Bug#3583910 return the global variable g_with_terms */
6873 function getWithTerms return varchar2 is
6874 begin
6875   return PO_COMMUNICATION_PVT.g_with_terms;
6876 end;
6877 
6878 
6879 /*******************************************************************************
6880   bug#3630737.
6881   PROCEDURE NAME : getOUDocumentDetails
6882 
6883   Description   :  This procedure is called from the PoGenerateDocument.java
6884   file. This procedure retrieves and returns OperatingUnitName, Draft message
6885   from and concatinated message of DocumentType, po number and revision number.
6886 
6887   Referenced by : PoGenerateDocument.java
6888    CHANGE History: Created    MANRAM
6889 ********************************************************************************/
6890 
6891 PROCEDURE getOUDocumentDetails(p_documentID IN NUMBER,
6892                                x_pendingSignatureFlag OUT NOCOPY VARCHAR2,
6893              x_documentName OUT NOCOPY VARCHAR2,
6894              x_organizationName OUT NOCOPY VARCHAR2,
6895              x_draft OUT NOCOPY VARCHAR2) IS
6896 
6897 BEGIN
6898 
6899   -- Bug 4044904: Get organization name from database
6900   -- as PO_COMMUNICATION_PVT.g_ou_name was never being populated anywhere
6901   -- Moved query up from below
6902 
6903   SELECT NVL(poh.pending_signature_flag, 'N')
6904        , hou.name
6905   INTO x_pendingSignatureFlag
6906      , PO_COMMUNICATION_PVT.g_ou_name
6907   FROM po_headers_all poh
6908      , hr_all_organization_units hou
6909   WHERE poh.po_header_id = p_documentID
6910     AND hou.organization_id = poh.org_id;
6911 
6912   x_organizationName := PO_COMMUNICATION_PVT.g_ou_name; -- operating unit name
6913   x_documentName := PO_COMMUNICATION_PVT.g_documentName; -- document name
6914 
6915   -- Bug 4044904 : Moved query above
6916 
6917   --retrieve draf from fnd_new_messages.
6918   FND_MESSAGE.SET_NAME('PO','PO_FO_DRAFT');
6919   x_draft := FND_MESSAGE.GET;
6920 
6921   EXCEPTION
6922        WHEN OTHERS THEN
6923        x_pendingSignatureFlag  := 'N';
6924        x_documentName := null;
6925        x_organizationName := null;
6926        x_draft := null;
6927 
6928 
6929 END;
6930 
6931 function getDocumentName return VARCHAR2 is
6932 BEGIN
6933   return PO_COMMUNICATION_PVT.g_documentName;
6934 END;
6935 
6936 --Start Bug#3771735
6937 --The function returns DocumentTypeCode
6938 function getDocumentTypeCode return VARCHAR2 is
6939 BEGIN
6940   return PO_COMMUNICATION_PVT.g_documentTypeCode;
6941 END;
6942 --End Bug#3771735
6943 
6944 -- Start Bug 4026592
6945 FUNCTION getIsContractAttachedDoc return VARCHAR2
6946 IS
6947 BEGIN
6948   return PO_COMMUNICATION_PVT.g_is_contract_attached_doc;
6949 END getIsContractAttachedDoc;
6950 -- End Bug 4026592
6951 
6952 -- <Complex Work R12 Start>
6953 
6954 -- Calls complex work APIs to determine if a document is a complex work doc.
6955 -- If so, sets the global variable g_is_complex_work_po to 'Y', otherwise 'N'
6956 PROCEDURE setIsComplexWorkPO(
6957   p_document_id    IN  NUMBER
6958 , p_revision_num   IN  NUMBER    DEFAULT NULL
6959 , p_which_tables   IN  VARCHAR2  DEFAULT 'MAIN'
6960 )
6961 IS
6962 
6963   d_progress NUMBER;
6964   d_module   VARCHAR2(70) := 'po.plsql.PO_COMMUNICATION_PVT.setIsComplexWorkPO';
6965   l_is_complex BOOLEAN;
6966   l_style_id   PO_HEADERS_ALL.style_id%TYPE;
6967 
6968 
6969 BEGIN
6970 
6971   d_progress := 0;
6972 
6973   IF (PO_LOG.d_proc) THEN
6974     PO_LOG.proc_begin(d_module);
6975     PO_LOG.proc_begin(d_module, 'p_document_id', p_document_id);
6976     PO_LOG.proc_begin(d_module, 'p_revision_num', p_revision_num);
6977     PO_LOG.proc_begin(d_module, 'p_which_tables', p_which_tables);
6978   END IF;
6979 
6980 
6981   IF (p_which_tables = 'MAIN') THEN
6982 
6983     d_progress := 10;
6984 
6985     l_is_complex := PO_COMPLEX_WORK_PVT.is_complex_work_po(
6986                       p_po_header_id => p_document_id
6987                     );
6988 
6989     d_progress := 15;
6990 
6991   ELSE
6992 
6993     d_progress := 20;
6994 
6995     SELECT poha.style_id
6996     INTO l_style_id
6997     FROM po_headers_archive_all poha
6998     WHERE poha.po_header_id = p_document_id
6999       AND poha.revision_num = p_revision_num;
7000 
7001     d_progress := 25;
7002 
7003     l_is_complex := PO_COMPLEX_WORK_PVT.is_complex_work_style(
7004                       p_style_id => l_style_id
7005                     );
7006 
7007     d_progress := 30;
7008 
7009   END IF;
7010 
7011   IF (PO_LOG.d_stmt) THEN
7012     PO_LOG.stmt(d_module, d_progress, 'l_is_complex', l_is_complex);
7013   END IF;
7014 
7015   IF (l_is_complex) THEN
7016     g_is_complex_work_po := 'Y';
7017   ELSE
7018     g_is_complex_work_po := 'N';
7019   END IF;
7020 
7021   d_progress := 50;
7022 
7023   IF (PO_LOG.d_proc) THEN
7024     PO_LOG.stmt(d_module, d_progress, 'g_is_complex_work_po', g_is_complex_work_po);
7025     PO_LOG.proc_end(d_module);
7026   END IF;
7027 
7028 EXCEPTION
7029   WHEN OTHERS THEN
7030     IF (PO_LOG.d_exc) THEN
7031       PO_LOG.exc(d_module, d_progress, SQLCODE || SQLERRM);
7032     END IF;
7033     g_is_complex_work_po := 'N';
7034 END setIsComplexWorkPO;
7035 
7036 
7037 FUNCTION getIsComplexWorkPO RETURN VARCHAR2
7038 IS
7039 BEGIN
7040   RETURN g_is_complex_work_po;
7041 END;
7042 
7043 -- <Complex Work R12 End>
7044 --Bug 4673653 START
7045 /*********************************************************************************
7046 Returns the Inventory Org Id stored in FINANCIALS_SYSTEM_PARAMETERS.
7047 
7048 We do not store this value in a global variable, as it would not change even when the
7049 inventory org is changed.  See bug 4431545 for more info.
7050 **********************************************************************************/
7051  FUNCTION getInventoryOrgId return NUMBER
7052  IS
7053  	 l_inventory_org_id FINANCIALS_SYSTEM_PARAMETERS.INVENTORY_ORGANIZATION_ID%type;
7054  BEGIN
7055   BEGIN
7056    SELECT INVENTORY_ORGANIZATION_ID
7057    INTO l_inventory_org_id
7058    FROM FINANCIALS_SYSTEM_PARAMETERS;
7059   EXCEPTION
7060    WHEN OTHERS THEN
7061     IF (PO_LOG.d_exc) THEN
7062       PO_LOG.exc('PO_COMMUNICATION_PVT.getInventoryOrgId', 0, SQLCODE || SQLERRM);
7063     END IF;
7064     l_inventory_org_id := null;
7065   END;
7066    return l_inventory_org_id;
7067  END getInventoryOrgId;
7068 --Bug 4673653 END
7069 
7070 -- Package Body
7071 -- Added for bug 6692126
7072 -- The function is intended to return the profile option value as per the
7073 -- submitter context.
7074 -- Incase if we do not get hold of submitter context, then we get the profile
7075 -- from current context.
7076 
7077 FUNCTION get_preparer_profile (p_document_id    NUMBER,
7078                                p_document_type  VARCHAR2,
7079                                p_profile_option VARCHAR2) RETURN VARCHAR2
7080 IS
7081 
7082 x_item_type         po_headers_all.wf_item_type%TYPE;
7083 x_item_key          po_headers_all.wf_item_key%TYPE;
7084 l_profile_value     fnd_profile_option_values.profile_option_value%TYPE;
7085 l_progress          VARCHAR2(10);
7086 
7087 l_preparer_user_id       NUMBER;
7088 l_preparer_resp_id       NUMBER;
7089 l_preparer_resp_appl_id  NUMBER;
7090 
7091 l_api_name               CONSTANT VARCHAR2(25):= 'Get_Preparer_Profile';
7092 
7093 BEGIN
7094   l_progress := '000';
7095 
7096   IF g_debug_stmt THEN
7097      PO_DEBUG.debug_begin(p_log_head => g_log_head || l_api_name);
7098      PO_DEBUG.debug_var(g_log_head || l_api_name, l_progress, 'p_document_id', p_document_id);
7099      PO_DEBUG.debug_var(g_log_head || l_api_name, l_progress, 'p_document_type', p_document_type);
7100      PO_DEBUG.debug_var(g_log_head || l_api_name, l_progress, 'p_profile_option', p_profile_option);
7101   END IF;
7102 
7103   IF p_document_type = 'RELEASE' THEN
7104      SELECT wf_item_type, wf_item_key
7105        INTO x_item_type, x_item_key
7106        FROM po_releases_all
7107       WHERE po_release_id = p_document_id;
7108 
7109      l_progress := '001';
7110 
7111   ELSE
7112      SELECT wf_item_type, wf_item_key
7113        INTO x_item_type, x_item_key
7114        FROM po_headers_all
7115       WHERE po_header_id = p_document_id;
7116 
7117      l_progress := '002';
7118 
7119   END IF;
7120 
7121   IF g_debug_stmt THEN
7122     PO_DEBUG.debug_var(g_log_head || l_api_name, l_progress, 'x_item_type', x_item_type);
7123     PO_DEBUG.debug_var(g_log_head || l_api_name, l_progress, 'x_item_key', x_item_key);
7124   END IF;
7125 
7126   l_progress := '003';
7127   IF x_item_type IS NOT NULL AND
7128      x_item_key IS NOT NULL  AND
7129      PO_APPROVAL_REMINDER_SV.is_active(x_item_type, x_item_key)
7130   THEN
7131       l_progress := '004';
7132       l_preparer_user_id := PO_WF_UTIL_PKG.GetItemAttrNumber
7133                                     (itemtype => x_item_type,
7134                                      itemkey  => x_item_key,
7135                                      aname    => 'USER_ID');
7136 
7137       l_progress := '005';
7138       l_preparer_resp_id := PO_WF_UTIL_PKG.GetItemAttrNumber
7139                                     (itemtype => x_item_type,
7140                                      itemkey  => x_item_key,
7141                                      aname    => 'RESPONSIBILITY_ID');
7142 
7143       l_progress := '006';
7144       l_preparer_resp_appl_id := PO_WF_UTIL_PKG.GetItemAttrNumber
7145                                     (itemtype => x_item_type,
7146                                      itemkey  => x_item_key,
7147                                      aname    => 'APPLICATION_ID');
7148 
7149       IF g_debug_stmt THEN
7150         PO_DEBUG.debug_var(g_log_head || l_api_name, l_progress, 'l_preparer_user_id', l_preparer_user_id);
7151         PO_DEBUG.debug_var(g_log_head || l_api_name, l_progress, 'l_preparer_resp_id', l_preparer_resp_id);
7152         PO_DEBUG.debug_var(g_log_head || l_api_name, l_progress, 'l_preparer_resp_appl_id', l_preparer_resp_appl_id);
7153       END IF;
7154 
7155       l_progress := '007';
7156 
7157     IF Nvl(l_preparer_user_id,-1) <> -1 AND
7158        Nvl(l_preparer_resp_id,-1) <> -1 AND
7159        Nvl(l_preparer_resp_appl_id,-1) <> -1 THEN
7160 
7161        l_progress := '008';
7162 
7163        l_profile_value :=  FND_PROFILE.VALUE_SPECIFIC(
7164                                 name              => p_profile_option,
7165                                 user_id           => l_preparer_user_id,
7166                                 responsibility_id => l_preparer_resp_id,
7167                                 application_id    => l_preparer_resp_appl_id);
7168 
7169        l_progress := '009';
7170 
7171     ELSE
7172       l_progress := '010';
7173       FND_PROFILE.GET(p_profile_option,l_profile_value);
7174       l_progress := '011';
7175     END IF;
7176   ELSE
7177     l_progress := '012';
7178     FND_PROFILE.GET(p_profile_option,l_profile_value);
7179     l_progress := '013';
7180   END IF;
7181 
7182   IF g_debug_stmt THEN
7183     PO_DEBUG.debug_var(g_log_head || l_api_name, l_progress, 'l_profile_value', l_profile_value);
7184   END IF;
7185 
7186   RETURN l_profile_value;
7187 
7188 EXCEPTION
7189   WHEN OTHERS THEN
7190       IF g_debug_stmt THEN
7191         PO_DEBUG.debug_var(g_log_head || l_api_name, l_progress, 'IN Exception sqlcode', SQLCODE);
7192       END IF;
7193 
7194       FND_PROFILE.GET(p_profile_option,l_profile_value);
7195 
7196       IF g_debug_stmt THEN
7197         PO_DEBUG.debug_var(g_log_head || l_api_name, l_progress, 'l_profile_value', l_profile_value);
7198         PO_DEBUG.debug_end(g_log_head || l_api_name);
7199       END IF;
7200 
7201       RETURN l_profile_value;
7202 
7203 END get_preparer_profile;
7204 
7205 --Bug5213932 start
7206  FUNCTION  get_clob(p_row_id IN ROWID) RETURN CLOB is
7207    l_long LONG;
7208    l_clob CLOB;
7209  BEGIN
7210 	SELECT fds.long_text  INTO
7211 	l_long
7212 	FROM
7213 	fnd_documents_long_text fds
7214 	WHERE fds.ROWID= p_row_id;
7215 
7216  l_clob := l_long;
7217 
7218  RETURN l_clob;
7219  END get_clob;
7220  --Bug5213932 end
7221 
7222 /*Bug4568471/6829381 Below function would determine if the ship-to-location is onetime location*/
7223  function get_oneTime_loc(p_location_id in number)  return  varchar2 is
7224   l_one_time_location varchar2(3000);
7225   l_location_id number;
7226  begin
7227 
7228    l_one_time_location := FND_PROFILE.VALUE('POR_ONE_TIME_LOCATION');
7229 
7230    if l_one_time_location is not null then
7231        SELECT location_id  into l_location_id FROM hr_locations
7232        where location_code=l_one_time_location;
7233    end if;
7234 
7235    if (l_location_id = p_location_id) then
7236     g_is_one_time_location :='Y';
7237    else
7238       g_is_one_time_location :='N';
7239    end if;
7240 
7241  return g_is_one_time_location;
7242  end get_oneTime_loc;
7243 
7244 /*Bug4568471/6829381  The below function returns the one time address for the line_location
7245   if the shipment has one time location as an attachment */
7246 function get_oneTime_address(p_line_location_id in number)  return  CLOB is
7247   l_one_time_address_details long;
7248   l_one_time_address_clob clob;
7249  begin
7250 
7251 if ( g_is_one_time_location ='Y') then
7252 
7253    SELECT fds.long_text
7254    INTO
7255     l_one_time_address_details
7256    FROM
7257    fnd_attached_docs_form_vl fad,
7258    fnd_documents_long_text fds
7259  	WHERE entity_name = 'PO_SHIPMENTS' AND
7260  	pk1_value =  To_Char(p_line_location_id) AND
7261     function_name = 'PO_PRINTPO'
7262     AND fad.media_id = fds.media_id
7263    AND fad.document_description like 'POR%'
7264    AND ROWNUM = 1;
7265  end if;
7266 
7267    l_one_time_address_clob := l_one_time_address_details;
7268 return   l_one_time_address_clob;
7269 end get_oneTime_address;
7270 /*Bug4568471/6829381 end*/
7271 
7272 end PO_COMMUNICATION_PVT;