DBA Data[Home] [Help]

PACKAGE BODY: APPS.IEX_DUNNING_PVT

Source


1 PACKAGE BODY IEX_DUNNING_PVT AS
2 /* $Header: iexvdunb.pls 120.34.12010000.3 2008/11/21 10:03:19 barathsr ship $ */
3 
4 
5 G_PKG_NAME  CONSTANT VARCHAR2(30):= 'IEX_DUNNING_PVT';
6 G_FILE_NAME CONSTANT VARCHAR2(12) := 'iexvdunb.pls';
7 
8 PG_DEBUG NUMBER ;
9 
10 Procedure WriteLog      (  p_msg                     IN VARCHAR2           ,
11                            p_flag                    IN NUMBER DEFAULT NULL)
12 IS
13 BEGIN
14 
15      IF (FND_LOG.LEVEL_EVENT >= PG_DEBUG) THEN
16         iex_debug_pub.LogMessage (p_msg);
17      END IF;
18 
19 END WriteLog;
20 
21 
22 /*
23     Returns location_id from hz_locations for specified site_use_id
24 */
25 FUNCTION GET_DUNNING_LOCATION(P_SITE_USE_ID NUMBER) RETURN NUMBER
26 IS
27 /*-----------------------------------------------------------------------+
28  | Local Variable Declarations and initializations                       |
29  +-----------------------------------------------------------------------*/
30     l_api_name          CONSTANT VARCHAR2(30) := 'GET_DUNNING_LOCATION';
31     l_return            NUMBER;
32 
33 /*-----------------------------------------------------------------------+
34  | Cursor Declarations                                                   |
35  +-----------------------------------------------------------------------*/
36     cursor get_data_crs(P_SITE_USE_ID number) is
37         select par_site.location_id
38         from
39             HZ_CUST_SITE_USES_ALL site_use,
40             HZ_CUST_ACCT_SITES_ALL acct_site,
41             hz_party_sites par_site
42         where
43             site_use.site_use_id = P_SITE_USE_ID and
44             acct_site.cust_acct_site_id = site_use.cust_acct_site_id and
45             par_site.party_site_id = acct_site.party_site_id;
46 
47 BEGIN
48 
49     l_return := null;
50 
51     WriteLog(l_api_name || ' input parameters:');
52     WriteLog('P_SITE_USE_ID: ' || P_SITE_USE_ID);
53 
54     if P_SITE_USE_ID is null then
55         WriteLog('Not all input parameters have value');
56         return l_return;
57     end if;
58 
59     OPEN get_data_crs(P_SITE_USE_ID);
60     FETCH get_data_crs INTO l_return;
61     CLOSE get_data_crs;
62 
63     WriteLog(l_api_name || ' returns ' || l_return);
64     return l_return;
65 
66 EXCEPTION
67     WHEN OTHERS THEN
68         WriteLog (l_api_name || ': In exception');
69         return l_return;
70 END;
71 
72 
73 
74 /*
75     Returns CUSTOMER location_id, contact party_id and contact_pont_id for specified party_id and contact_point_type the OLD WAY
76 */
77 Procedure GET_CUST_DUNNING_DATA_OW(p_api_version             IN NUMBER := 1.0,
78                                 p_init_msg_list           IN VARCHAR2 ,
79                                 p_commit                  IN VARCHAR2 ,
80                                 P_PARTY_ID                IN NUMBER,
81                                 P_CONTACT_POINT_TYPE      IN VARCHAR2,
82                                 X_LOCATION_ID             OUT NOCOPY NUMBER,
83                                 X_CONTACT_ID              OUT NOCOPY NUMBER,
84                                 X_CONTACT_POINT_ID        OUT NOCOPY NUMBER,
85                                 x_return_status           OUT NOCOPY VARCHAR2,
86                                 x_msg_count               OUT NOCOPY NUMBER,
87                                 x_msg_data                OUT NOCOPY VARCHAR2)
88 IS
89 /*-----------------------------------------------------------------------+
90  | Local Variable Declarations and initializations                       |
91  +-----------------------------------------------------------------------*/
92     l_api_name                  CONSTANT VARCHAR2(30) := 'GET_CUST_DUNNING_DATA_OW';
93     l_primary_flag              VARCHAR2(1);
94     l_purpose_flag              varchar2(1);
95     l_rel_party_id              NUMBER;
96     l_contact_id                NUMBER;
97     l_contact_point_id          NUMBER;
98     l_location_id               NUMBER;
99     l_order                     NUMBER;
100     l_rel_type                  VARCHAR2(30);
101     l_def_rel_type              VARCHAR2(30);
102 
103 /*-----------------------------------------------------------------------+
104  | Cursor Declarations                                                   |
105  +-----------------------------------------------------------------------*/
106 
107     -- get contact_id and contact_point_id
108     cursor get_old_contact_crs(P_REL_TYPE varchar2, P_PARTY_ID number, P_CONTACT_POINT_TYPE varchar2) is
109         select
110             decode(P_REL_TYPE, rel.relationship_type,
111                     decode(point.contact_point_purpose,
112                       'DUNNING', decode(point.primary_by_purpose, 'Y', 1, decode(point.primary_flag, 'Y', 2, 3
113                                                                           )
114                                  ), 4
115                     ),
116                     decode(point.contact_point_purpose,
117                       'DUNNING', decode(point.primary_by_purpose, 'Y', 5, decode(point.primary_flag, 'Y', 6, 7
118                                                                           )
119                                  ), 8
120                     )
121             ) Display_Order,
122             rel.party_id,
123             rel.subject_id,
124             point.contact_point_id,
125             rel.relationship_type
126         from HZ_RELATIONSHIPS rel,
127             hz_contact_points point
128         where rel.object_id = P_PARTY_ID and
129             rel.relationship_type in ('DUNNING', 'COLLECTIONS') and
130             rel.status = 'A' and
131             rel.party_id = point.owner_table_ID and
132             point.owner_table_name = 'HZ_PARTIES' and
133             upper(point.contact_point_type) = decode(P_CONTACT_POINT_TYPE, 'EMAIL', 'EMAIL', 'PHONE', 'PHONE', 'FAX', 'PHONE') and
134             nvl(point.phone_line_type, 'EMAIL') = decode(P_CONTACT_POINT_TYPE, 'EMAIL', 'EMAIL', 'PHONE', 'GEN', 'FAX', 'FAX') and
135             NVL(point.do_not_use_flag, 'N') = 'N' and
136             (point.status = 'A' OR point.status <> 'I')
137         order by Display_Order;
138 
139     -- get contact_id
140     cursor get_old_contact_crs1(P_REL_TYPE varchar2, P_PARTY_ID number) is
141         select
142             decode(P_REL_TYPE, rel.relationship_type, 1, 2) Display_Order,
143             rel.party_id,
144             rel.subject_id,
145             rel.relationship_type
146         from HZ_RELATIONSHIPS rel
147         where rel.object_id = P_PARTY_ID and
148             rel.relationship_type in ('DUNNING', 'COLLECTIONS') and
149             rel.status = 'A'
150         order by Display_Order;
151 
152     -- get relationship location_id
153     cursor get_old_loc_crs(P_REL_PARTY_ID number) is
154         select location_id
155         from hz_party_sites
156         where party_id = P_REL_PARTY_ID and
157             status in ('A', 'I');
158 
159     --start for bug 6500750 gnramasa 13-Nov-07
160     cursor get_site_loc(P_CONTACT_ID number) is
161         select location_id
162         from ast_locations_v where party_site_id =
163         	(select party_site_id
164 		from hz_cust_acct_sites_all where cust_acct_site_id =
165                       (select address_id
166                        from ar_contacts_v where contact_party_id = P_CONTACT_ID));
167     --End for bug 6500750 gnramasa 13-Nov-07
168 
169     -- get organization location_id
170     cursor get_old_loc_crs1(P_ORG_PARTY_ID number) is
171         select location_id
172         from ast_locations_v
173         where party_id = P_ORG_PARTY_ID and
174             primary_flag = 'Y';
175 
176 BEGIN
177 
178     IF FND_API.to_Boolean( p_init_msg_list )
179     THEN
180         FND_MSG_PUB.initialize;
181     END IF;
182 
183     x_return_status := FND_API.G_RET_STS_SUCCESS;
184 
185     WriteLog('----------' || l_api_name || '----------');
186     WriteLog(l_api_name || ': input parameters:');
187     WriteLog(l_api_name || ': P_PARTY_ID: ' || P_PARTY_ID);
188     WriteLog(l_api_name || ': P_CONTACT_POINT_TYPE: ' || P_CONTACT_POINT_TYPE);
189 
190     X_LOCATION_ID := null;
191     X_CONTACT_ID := null;
192     X_CONTACT_POINT_ID := null;
193 
194     -- verify input parameters and if they are not set return immediately
195     if P_PARTY_ID is null or P_CONTACT_POINT_TYPE is null then
196         WriteLog(l_api_name || ': Not all input parameters have value');
197         return;
198     end if;
199 
200     WriteLog(l_api_name || ': Searching location_id, contact_id and contact_point_id the old way...');
201 
202     l_def_rel_type := nvl(fnd_profile.value('IEX_DEF_CORRESP_REL_TYPE'), 'DUNNING');
203     WriteLog(l_api_name || ': IEX_DEF_CORRESP_REL_TYPE profile value: ' || l_def_rel_type);
204 
205     if P_CONTACT_POINT_TYPE = 'PRINTER' then
206 
207         WriteLog(l_api_name || ': For printer searching for contact only...');
208 
209         -- try to get contact_id the old way
210         OPEN get_old_contact_crs1(l_def_rel_type, P_PARTY_ID);
211         fetch get_old_contact_crs1 into l_order,
212                                 l_rel_party_id,
213                                 l_contact_id,
214                                 l_rel_type;
215         CLOSE get_old_contact_crs1;
216 
217         if l_rel_party_id is not null then
218             WriteLog(l_api_name || ': Found ' || l_rel_type || ' contact:');
219             WriteLog(l_api_name || ': l_rel_party_id: ' || l_rel_party_id);
220             WriteLog(l_api_name || ': l_contact_id: ' || l_contact_id);
221         else
222             WriteLog(l_api_name || ': No contact found');
223         end if;
224 
225     ELSE
226 
227         -- try to get contact_id and contact_point_id the old way
228         OPEN get_old_contact_crs(l_def_rel_type, P_PARTY_ID, P_CONTACT_POINT_TYPE);
229         fetch get_old_contact_crs into l_order,
230                                 l_rel_party_id,
231                                 l_contact_id,
232                                 l_contact_point_id,
233                                 l_rel_type;
234         CLOSE get_old_contact_crs;
235 
236         if l_rel_party_id is not null then
237           WriteLog(l_api_name || ': Found ' || l_rel_type || ' contact with ' || P_CONTACT_POINT_TYPE || ' contact point:');
238           WriteLog(l_api_name || ': l_rel_party_id: ' || l_rel_party_id);
239           WriteLog(l_api_name || ': l_contact_id: ' || l_contact_id);
240           WriteLog(l_api_name || ': l_contact_point_id: ' || l_contact_point_id);
241         else
242             WriteLog(l_api_name || ': No contact and contact point found');
243         end if;
244 
245     END IF;
246 
247     if l_rel_party_id is not null then
248 
249         WriteLog(l_api_name || ': Searching for relationship location...');
250 
251         -- get relationship location_id
252         OPEN get_old_loc_crs(l_rel_party_id);
253         fetch get_old_loc_crs into l_location_id;
254         CLOSE get_old_loc_crs;
255 
256         if l_location_id is not null then
257             WriteLog(l_api_name || ': Found relationship location: ' || l_location_id);
258         else
259             WriteLog(l_api_name || ': No relationship location found');
260 
261 	    --start for bug 6500750 gnramasa 13-Nov-07
262 	    OPEN get_site_loc(l_contact_id);
263             fetch get_site_loc into l_location_id;
264             CLOSE get_site_loc;
265 		if l_location_id is not null then
266                     WriteLog(l_api_name || ': Found contact site location: ' || l_location_id);
267 		else
268                     WriteLog(l_api_name || ': No contact site location found');
269 	    --End for bug 6500750 gnramasa 13-Nov-07
270 
271 		    WriteLog(l_api_name || ': Searching for organization location...');
272 
273 		    -- get relationship location_id
274 		    OPEN get_old_loc_crs1(P_PARTY_ID);
275 		    fetch get_old_loc_crs1 into l_location_id;
276 		    CLOSE get_old_loc_crs1;
277 
278 		    if l_location_id is not null then
279 			WriteLog(l_api_name || ': Found organization location: ' || l_location_id);
280 		    else
281 			WriteLog(l_api_name || ': No organization location found');
282 		    end if;
283 		end if;
284 
285         end if;
286 
287     end if;
288 
289     X_LOCATION_ID := l_location_id;
290     X_CONTACT_ID := l_contact_id;
291     X_CONTACT_POINT_ID := l_contact_point_id;
292 
293     WriteLog(l_api_name || ': X_LOCATION_ID = ' || X_LOCATION_ID);
294     WriteLog(l_api_name || ': X_CONTACT_ID = ' || X_CONTACT_ID);
295     WriteLog(l_api_name || ': X_CONTACT_POINT_ID = ' || X_CONTACT_POINT_ID);
296 
297 EXCEPTION
298   WHEN FND_API.G_EXC_ERROR THEN
299       x_return_status := FND_API.G_RET_STS_ERROR;
300       FND_MSG_PUB.Count_And_Get
301       (  p_count          =>   x_msg_count,
302          p_data           =>   x_msg_data );
303       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception error='||SQLERRM);
304 
305   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
306       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
307       FND_MSG_PUB.Count_And_Get
308       (  p_count          =>   x_msg_count,
309          p_data           =>   x_msg_data );
310       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExc Exception error='||SQLERRM);
311 
312   WHEN OTHERS THEN
313       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
314       FND_MSG_PUB.Count_And_Get
315       (  p_count          =>   x_msg_count,
316          p_data           =>   x_msg_data );
317       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception error='||SQLERRM);
318 END;
319 
320 /*
321     Returns ACCOUNT SITE location, contact party_id and contact_pont_id based on specified site_use_id and contact_point_type
322 */
323 Procedure GET_SITE_DUNNING_DATA(p_api_version             IN NUMBER := 1.0,
324                                 p_init_msg_list           IN VARCHAR2 ,
325                                 p_commit                  IN VARCHAR2 ,
326                                 P_SITE_USE_ID             IN NUMBER,
327                                 P_CONTACT_POINT_TYPE      IN VARCHAR2,
328                                 X_LOCATION_ID             OUT NOCOPY NUMBER,
329                                 X_CONTACT_ID              OUT NOCOPY NUMBER,
330                                 X_CONTACT_POINT_ID        OUT NOCOPY NUMBER,
331                                 x_return_status           OUT NOCOPY VARCHAR2,
332                                 x_msg_count               OUT NOCOPY NUMBER,
333                                 x_msg_data                OUT NOCOPY VARCHAR2)
334 IS
335 /*-----------------------------------------------------------------------+
336  | Local Variable Declarations and initializations                       |
337  +-----------------------------------------------------------------------*/
338     l_api_name              CONSTANT VARCHAR2(30) := 'GET_SITE_DUNNING_DATA';
339     l_order                 NUMBER;
340     l_per_party_id          NUMBER;
341     l_rel_party_id          NUMBER;
342     l_contact_point_id      NUMBER;
343     l_count                 NUMBER;
344     l_responsibility_type   VARCHAR2(30);
345     l_dun_contact_level  VARCHAR2(30); --Added for bug 6500750 gnramasa 13-Nov-07
346 
347 /*-----------------------------------------------------------------------+
348  | Cursor Declarations                                                   |
349  +-----------------------------------------------------------------------*/
350 
351     -- get contacts for a cust account site order by responsibility type
352     cursor get_data_crs(P_SITE_USE_ID number) is
353         select
354             decode(role_resp.responsibility_type,
355                 'DUN', decode(role_resp.primary_flag, 'Y', 1, 2),
356                 'BILL_TO', decode(role_resp.primary_flag, 'Y', 3, 4),
357                 'INV', decode(role_resp.primary_flag, 'Y', 5, 6),
358                 'SHIP_TO', decode(role_resp.primary_flag, 'Y', 7, 8), 9
359             ) Display_Order,
360             role_resp.responsibility_type,
361             party.party_id,
362             sub_party.party_id
363         from
364             HZ_CUST_SITE_USES_ALL site_use,
365             HZ_CUST_ACCOUNT_ROLES acct_role,
366             HZ_ROLE_RESPONSIBILITY role_resp,
367             HZ_RELATIONSHIPS rel,
368             hz_parties party,
369             hz_parties sub_party
370         where
371             site_use.site_use_id = P_SITE_USE_ID and
372             acct_role.cust_acct_site_id = site_use.cust_acct_site_id and
373             acct_role.status = 'A' and
374             role_resp.cust_account_role_id = acct_role.cust_account_role_id and
375             acct_role.party_id = party.party_id and
376             party.status = 'A' and
377             rel.party_id = party.party_id and
378             rel.subject_type = 'PERSON' and
379             rel.status = 'A' and
380             decode(rel.object_type, 'PERSON', rel.directional_flag, 1) = decode(rel.object_type, 'PERSON', 'F', 1) and
381             sub_party.party_id = rel.subject_id and
382             sub_party.status = 'A'
383         order by Display_Order, sub_party.party_name;
384 
385     -- get CONTACT_POINT_ID for specified CONTACT_POINT_TYPE and party_id
386     cursor get_data_crs1(P_PARTY_ID number, P_CONTACT_POINT_TYPE varchar2) is
387         select
388             decode(cont_point.contact_point_purpose,
389                 'DUNNING', decode(cont_point.primary_flag, 'Y', 1, decode(cont_point.primary_by_purpose, 'Y', 2, 3)),
390                 'COLLECTIONS', decode(cont_point.primary_flag, 'Y', 4, decode(cont_point.primary_by_purpose, 'Y', 5, 6)),
391                 'BUSINESS', decode(cont_point.primary_flag, 'Y', 7, decode(cont_point.primary_by_purpose, 'Y', 8, 9)),
392                 null, decode(cont_point.primary_flag, 'Y', 10, decode(cont_point.primary_by_purpose, 'Y', 11, 12))
393             ) Display_Order
394             ,cont_point.CONTACT_POINT_ID
395         from hz_contact_points cont_point
396         where
397             cont_point.owner_table_id = P_PARTY_ID and
398             cont_point.owner_table_name = 'HZ_PARTIES' and
399             cont_point.contact_point_type = decode(P_CONTACT_POINT_TYPE, 'EMAIL', 'EMAIL', 'PHONE', 'PHONE', 'FAX', 'PHONE') and
400             nvl(cont_point.phone_line_type, 'EMAIL') = decode(P_CONTACT_POINT_TYPE, 'EMAIL', 'EMAIL', 'PHONE', 'GEN', 'FAX', 'FAX') and
401             NVL(cont_point.do_not_use_flag, 'N') = 'N' and
402             (cont_point.status = 'A' OR cont_point.status <> 'I')
403         order by Display_Order;
404 
405     -- get CONTACT_POINT_ID for specified CONTACT_POINT_TYPE and party_site_id
406     cursor get_data_crs2(P_SITE_USE_ID number, P_CONTACT_POINT_TYPE varchar2) is
407         select
408             decode(cont_point.contact_point_purpose,
409                 'DUNNING', decode(cont_point.primary_flag, 'Y', 1, decode(cont_point.primary_by_purpose, 'Y', 2, 3)),
410                 'COLLECTIONS', decode(cont_point.primary_flag, 'Y', 4, decode(cont_point.primary_by_purpose, 'Y', 5, 6)),
411                 'BUSINESS', decode(cont_point.primary_flag, 'Y', 7, decode(cont_point.primary_by_purpose, 'Y', 8, 9)),
412                 null, decode(cont_point.primary_flag, 'Y', 10, decode(cont_point.primary_by_purpose, 'Y', 11, 12))
413             ) Display_Order
414             ,cont_point.CONTACT_POINT_ID
415         from
416             HZ_CUST_SITE_USES_ALL site_use,
417             HZ_CUST_ACCT_SITES_ALL acct_site,
418             hz_contact_points cont_point
419         where
420             site_use.site_use_id = P_SITE_USE_ID and
421             acct_site.cust_acct_site_id = site_use.cust_acct_site_id and
422             cont_point.owner_table_id = acct_site.party_site_id and
423             cont_point.owner_table_name = 'HZ_PARTY_SITES' and
424             cont_point.contact_point_type = decode(P_CONTACT_POINT_TYPE, 'EMAIL', 'EMAIL', 'PHONE', 'PHONE', 'FAX', 'PHONE') and
425             nvl(cont_point.phone_line_type, 'EMAIL') = decode(P_CONTACT_POINT_TYPE, 'EMAIL', 'EMAIL', 'PHONE', 'GEN', 'FAX', 'FAX') and
426             NVL(cont_point.do_not_use_flag, 'N') = 'N' and
427             (cont_point.status = 'A' OR cont_point.status <> 'I')
428         order by Display_Order;
429 
430     -- get LOCATION_ID for specified site_use_id
431     cursor get_data_crs3(P_SITE_USE_ID number) is
432         select par_site.location_id
433         from
434             HZ_CUST_SITE_USES_ALL site_use,
435             HZ_CUST_ACCT_SITES_ALL acct_site,
436             hz_party_sites par_site
437         where
438             site_use.site_use_id = P_SITE_USE_ID and
439             acct_site.cust_acct_site_id = site_use.cust_acct_site_id and
440             par_site.party_site_id = acct_site.party_site_id;
441 
442 BEGIN
443 
444     IF FND_API.to_Boolean( p_init_msg_list )
445     THEN
446         FND_MSG_PUB.initialize;
447     END IF;
448 
449     l_dun_contact_level := nvl(fnd_profile.value('IEX_DUNNING_CONTACT_SELECTION_METHOD'), 'ALL');  --Added for bug 6500750 gnramasa 13-Nov-07
450 
451     x_return_status := FND_API.G_RET_STS_SUCCESS;
452 
453     WriteLog('----------' || l_api_name || '----------');
454     WriteLog(l_api_name || ': input parameters:');
455     WriteLog(l_api_name || ': P_SITE_USE_ID: ' || P_SITE_USE_ID);
456     WriteLog(l_api_name || ': P_CONTACT_POINT_TYPE: ' || P_CONTACT_POINT_TYPE);
457 
458     X_LOCATION_ID := null;
459     X_CONTACT_ID := null;
460     X_CONTACT_POINT_ID := null;
461 
462     -- verify input parameters and if they are not set return immediately
463     if P_SITE_USE_ID is null or P_CONTACT_POINT_TYPE is null then
464         WriteLog(l_api_name || ': Not all input parameters have value');
465         return;
466     end if;
467 
468     WriteLog(l_api_name || ': Searching for ACCOUNT SITE location...');
469     OPEN get_data_crs3(P_SITE_USE_ID);
470     FETCH get_data_crs3 INTO X_LOCATION_ID;
471     CLOSE get_data_crs3;
472     WriteLog(l_api_name || ': X_LOCATION_ID = ' || X_LOCATION_ID);
473 
474     WriteLog(l_api_name || ': Searching for ACCOUNT SITE level contacts with ' || P_CONTACT_POINT_TYPE || ' contact point...');
475 
476     l_count := 0;
477     OPEN get_data_crs(P_SITE_USE_ID);
478     LOOP
479 
480         l_contact_point_id := null;
481         fetch get_data_crs into
482             l_order,
483             l_responsibility_type,
484             l_rel_party_id,
485             l_per_party_id;
486         exit when get_data_crs%NOTFOUND;
487 
488 	-- Start for bug 6500750 gnramasa 13-Nov-07
489 	if l_dun_contact_level = 'ALL' or l_dun_contact_level is null then
490 		WriteLog(l_api_name || ': l_dun_contact_level : ' || l_dun_contact_level);
491 
492 		l_count := l_count + 1;
493 		WriteLog(l_api_name || ': Found #' || l_count || ' contact = ' || l_per_party_id);
494 		WriteLog(l_api_name || ': l_rel_party_id = ' || l_rel_party_id);
495 		WriteLog(l_api_name || ': l_responsibility_type = ' || l_responsibility_type);
496 
497 		if P_CONTACT_POINT_TYPE = 'PRINTER' then
498 
499 		    WriteLog(l_api_name || ': For contact point PRINTER return first found contact');
500 		    X_CONTACT_ID := l_per_party_id;
501 		    X_CONTACT_POINT_ID := null;
502 		    WriteLog(l_api_name || ': X_CONTACT_ID = ' || X_CONTACT_ID);
503 		    WriteLog(l_api_name || ': X_CONTACT_POINT_ID = ' || X_CONTACT_POINT_ID);
504 		    return;
505 
506 		else
507 
508 		    OPEN get_data_crs1(l_rel_party_id, P_CONTACT_POINT_TYPE);
509 		    FETCH get_data_crs1 INTO l_order, l_contact_point_id;
510 		    CLOSE get_data_crs1;
511 
512 		    if l_contact_point_id is not null then
513 
514 			WriteLog(l_api_name || ': Found contact_point_id = ' || l_contact_point_id);
515 			X_CONTACT_ID := l_per_party_id;
516 			X_CONTACT_POINT_ID := l_contact_point_id;
517 			WriteLog(l_api_name || ': X_CONTACT_ID = ' || X_CONTACT_ID);
518 			WriteLog(l_api_name || ': X_CONTACT_POINT_ID = ' || X_CONTACT_POINT_ID);
519 			return;
520 
521 		    else
522 
523 			WriteLog(l_api_name || ': No contact points found for this contact');
524 
525 		    end if;
526 
527 		end if;
528 	elsif l_dun_contact_level ='DUNNING' then
529 	   WriteLog(l_api_name || ': l_dun_contact_level : ' || l_dun_contact_level);
530 	   if l_order in (1,2) then
531 		WriteLog(l_api_name || ': l_order : ' || l_order);
532 		l_count := l_count + 1;
533 		WriteLog(l_api_name || ': Found #' || l_count || ' contact = ' || l_per_party_id);
534 		WriteLog(l_api_name || ': l_rel_party_id = ' || l_rel_party_id);
535 		WriteLog(l_api_name || ': l_responsibility_type = ' || l_responsibility_type);
536 
537 		if P_CONTACT_POINT_TYPE = 'PRINTER' then
538 
539 		    WriteLog(l_api_name || ': For contact point PRINTER return first found contact');
540 		    X_CONTACT_ID := l_per_party_id;
541 		    X_CONTACT_POINT_ID := null;
542 		    WriteLog(l_api_name || ': X_CONTACT_ID = ' || X_CONTACT_ID);
543 		    WriteLog(l_api_name || ': X_CONTACT_POINT_ID = ' || X_CONTACT_POINT_ID);
544 		    return;
545 
546 		else
547 
548 		    OPEN get_data_crs1(l_rel_party_id, P_CONTACT_POINT_TYPE);
549 		    FETCH get_data_crs1 INTO l_order, l_contact_point_id;
550 		    CLOSE get_data_crs1;
551 
552 		    if l_contact_point_id is not null then
553 
554 			WriteLog(l_api_name || ': Found contact_point_id = ' || l_contact_point_id);
555 			X_CONTACT_ID := l_per_party_id;
556 			X_CONTACT_POINT_ID := l_contact_point_id;
557 			WriteLog(l_api_name || ': X_CONTACT_ID = ' || X_CONTACT_ID);
558 			WriteLog(l_api_name || ': X_CONTACT_POINT_ID = ' || X_CONTACT_POINT_ID);
559 			return;
560 
561 		    else
562 
563 			WriteLog(l_api_name || ': No contact points found for this contact');
564 
565 		    end if;
566 
567 		end if;
568 	    end if;  -- l_order
569 	end if;  -- l_dun_contact_level
570         -- End for bug 6500750 gnramasa 13-Nov-07
571 
572     END LOOP;
573     CLOSE get_data_crs;
574 
575     if l_count = 0 then
576         WriteLog(l_api_name || ': No ACCOUNT SITE level contacts found');
577     end if;
578 
579     -- if no ACCOUNT SITE contacts with contact point found - search for ACCOUNT SITE level contact points
580     if X_CONTACT_ID is null and X_CONTACT_POINT_ID is null then
581 
582         WriteLog(l_api_name || ': Searching for ACCOUNT SITE level ' || P_CONTACT_POINT_TYPE || ' contact point...');
583         OPEN get_data_crs2(P_SITE_USE_ID, P_CONTACT_POINT_TYPE);
584         FETCH get_data_crs2 INTO l_order, l_contact_point_id;
585         CLOSE get_data_crs2;
586 
587         if l_contact_point_id is not null then
588 
589             WriteLog(l_api_name || ': Found contact_point_id = ' || l_contact_point_id);
590             X_CONTACT_ID := null;
591             X_CONTACT_POINT_ID := l_contact_point_id;
592             WriteLog(l_api_name || ': X_CONTACT_ID = ' || X_CONTACT_ID);
593             WriteLog(l_api_name || ': X_CONTACT_POINT_ID = ' || X_CONTACT_POINT_ID);
594             return;
595 
596         else
597 
598             WriteLog(l_api_name || ': No contact points found');
599 
600         end if;
601 
602     end if;
603 
604     WriteLog(l_api_name || ': X_CONTACT_ID = ' || X_CONTACT_ID);
605     WriteLog(l_api_name || ': X_CONTACT_POINT_ID = ' || X_CONTACT_POINT_ID);
606 
607     FND_MSG_PUB.Count_And_Get
608     (  p_count          =>   x_msg_count,
609        p_data           =>   x_msg_data );
610 
611 EXCEPTION
612   WHEN FND_API.G_EXC_ERROR THEN
613       x_return_status := FND_API.G_RET_STS_ERROR;
614       FND_MSG_PUB.Count_And_Get
615       (  p_count          =>   x_msg_count,
616          p_data           =>   x_msg_data );
617       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception error='||SQLERRM);
618 
619   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
620       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
621       FND_MSG_PUB.Count_And_Get
622       (  p_count          =>   x_msg_count,
623          p_data           =>   x_msg_data );
624       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExc Exception error='||SQLERRM);
625 
626   WHEN OTHERS THEN
627       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
628       FND_MSG_PUB.Count_And_Get
629       (  p_count          =>   x_msg_count,
630          p_data           =>   x_msg_data );
631       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception error='||SQLERRM);
632 END;
633 
634 
635 
636 /*
637     Returns ACCOUNT location, contact party_id and contact_pont_id for specified cust_account_id and contact_point_type
638 */
639 Procedure GET_ACCT_DUNNING_DATA(p_api_version             IN NUMBER := 1.0,
640                                 p_init_msg_list           IN VARCHAR2 ,
641                                 p_commit                  IN VARCHAR2 ,
642                                 P_CUST_ACCT_ID            IN NUMBER,
643                                 P_CONTACT_POINT_TYPE      IN VARCHAR2,
644                                 X_LOCATION_ID             OUT NOCOPY NUMBER,
645                                 X_CONTACT_ID              OUT NOCOPY NUMBER,
646                                 X_CONTACT_POINT_ID        OUT NOCOPY NUMBER,
647                                 x_return_status           OUT NOCOPY VARCHAR2,
648                                 x_msg_count               OUT NOCOPY NUMBER,
649                                 x_msg_data                OUT NOCOPY VARCHAR2)
650 IS
651 /*-----------------------------------------------------------------------+
652  | Local Variable Declarations and initializations                       |
653  +-----------------------------------------------------------------------*/
654     l_api_name              CONSTANT VARCHAR2(30) := 'GET_ACCT_DUNNING_DATA';
655     l_order                 NUMBER;
656     l_per_party_id          NUMBER;
657     l_rel_party_id          NUMBER;
658     l_contact_point_id      NUMBER;
659     l_count                 NUMBER;
660     l_responsibility_type   VARCHAR2(30);
661     l_org_party_id          NUMBER;
662     l_bill_to_count         NUMBER;
663     l_display_order         NUMBER;
664     l_site_use_code         VARCHAR2(30);
665     l_location_id           NUMBER;
666     l_dun_contact_level     VARCHAR2(30); --Added for bug 6500750 gnramasa 13-Nov-07
667 
668 /*-----------------------------------------------------------------------+
669  | Cursor Declarations                                                   |
670  +-----------------------------------------------------------------------*/
671 
672     -- get contacts for a cust account order by responsibility type
673     cursor get_data_crs(P_CUST_ACCOUNT_ID number) is
674         select
675             decode(role_resp.responsibility_type,
676                 'DUN', decode(role_resp.primary_flag, 'Y', 1, 2),
677                 'BILL_TO', decode(role_resp.primary_flag, 'Y', 3, 4),
678                 'INV', decode(role_resp.primary_flag, 'Y', 5, 6),
679                 'SHIP_TO', decode(role_resp.primary_flag, 'Y', 7, 8), 9
680             ) Display_Order,
681             role_resp.responsibility_type,
682             party.party_id,
683             sub_party.party_id,
684             rel.object_id
685         from
686             HZ_CUST_ACCOUNT_ROLES acct_role,
687             HZ_ROLE_RESPONSIBILITY role_resp,
688             HZ_RELATIONSHIPS rel,
689             hz_parties party,
690             hz_parties sub_party
691         where
692             acct_role.cust_account_id = P_CUST_ACCOUNT_ID and
693             acct_role.cust_acct_site_id is null and
694             acct_role.status = 'A' and
695             acct_role.cust_account_role_id = role_resp.cust_account_role_id and
696             acct_role.party_id = party.party_id and
697             party.status = 'A' and
698             rel.party_id = party.party_id and
699             rel.subject_type = 'PERSON' and
700             rel.status = 'A' and
701             decode(rel.object_type, 'PERSON', rel.directional_flag, 1) = decode(rel.object_type, 'PERSON', 'F', 1) and
702             sub_party.party_id = rel.subject_id and
703             sub_party.status = 'A'
704         order by Display_Order, sub_party.party_name;
705 
706     -- get CONTACT_POINT_ID for specified CONTACT_POINT_TYPE and party_id
707     cursor get_data_crs1(P_PARTY_ID number, P_CONTACT_POINT_TYPE varchar2) is
708         select
709             decode(cont_point.contact_point_purpose,
710                 'DUNNING', decode(cont_point.primary_flag, 'Y', 1, decode(cont_point.primary_by_purpose, 'Y', 2, 3)),
711                 'COLLECTIONS', decode(cont_point.primary_flag, 'Y', 4, decode(cont_point.primary_by_purpose, 'Y', 5, 6)),
712                 'BUSINESS', decode(cont_point.primary_flag, 'Y', 7, decode(cont_point.primary_by_purpose, 'Y', 8, 9)),
713                 null, decode(cont_point.primary_flag, 'Y', 10, decode(cont_point.primary_by_purpose, 'Y', 11, 12))
714             ) Display_Order
715             ,cont_point.CONTACT_POINT_ID
716         from hz_contact_points cont_point
717         where
718             cont_point.owner_table_id = P_PARTY_ID and
719             cont_point.owner_table_name = 'HZ_PARTIES' and
720             cont_point.contact_point_type = decode(P_CONTACT_POINT_TYPE, 'EMAIL', 'EMAIL', 'PHONE', 'PHONE', 'FAX', 'PHONE') and
721             nvl(cont_point.phone_line_type, 'EMAIL') = decode(P_CONTACT_POINT_TYPE, 'EMAIL', 'EMAIL', 'PHONE', 'GEN', 'FAX', 'FAX') and
722             NVL(cont_point.do_not_use_flag, 'N') = 'N' and
723             (cont_point.status = 'A' OR cont_point.status <> 'I')
724         order by Display_Order;
725 
726     -- get party_id from cust_account_id
727     cursor get_party_crs(P_CUST_ACCOUNT_ID number) is
728         select party_id from hz_cust_accounts where cust_account_id = P_CUST_ACCOUNT_ID;
729 
730     -- get LOCATION_ID for specified cust_account_id
731     cursor get_data_crs2(P_CUST_ACCOUNT_ID number) is
732         select
733             decode(site_use.site_use_code,
734                 'DUN', 1,
735                 'BILL_TO', decode(site_use.primary_flag, 'Y', 2, 3)) display_order,
736             site_use.site_use_code,
737             par_site.location_id
738         from
739             hz_party_sites par_site,
740             HZ_CUST_ACCT_SITES_ALL acct_site,
741             HZ_CUST_SITE_USES_ALL site_use
742         where
743             acct_site.cust_account_id = P_CUST_ACCOUNT_ID and
744             acct_site.status = 'A' and
745             acct_site.cust_acct_site_id = site_use.cust_acct_site_id and
746             site_use.status = 'A' and
747             par_site.party_site_id = acct_site.party_site_id and
748             par_site.status in ('A', 'I')
749         order by display_order;
750 
751     -- get count on not primary bill-to locations
752     cursor get_data_crs3(P_CUST_ACCOUNT_ID number) is
753         select count(1)
754         from
755             hz_party_sites par_site,
756             HZ_CUST_ACCT_SITES_ALL acct_site,
757             HZ_CUST_SITE_USES_ALL site_use
758         where
759             acct_site.cust_account_id = P_CUST_ACCOUNT_ID and
760             acct_site.status = 'A' and
761             acct_site.cust_acct_site_id = site_use.cust_acct_site_id and
762             site_use.status = 'A' and
763             par_site.party_site_id = acct_site.party_site_id and
764             par_site.status in ('A', 'I') and
765             site_use.site_use_code = 'BILL_TO' and
766             site_use.primary_flag <> 'Y';
767 
768     -- Start for bug 6500750 gnramasa 13-Nov-07
769 
770      cursor get_old_loc_crs1(P_ORG_PARTY_ID number) is
771         select location_id
772         from ast_locations_v
773         where party_id = P_ORG_PARTY_ID and
774             primary_flag = 'Y';
775 
776     -- End for bug 6500750 gnramasa 13-Nov-07
777 
778 BEGIN
779 
780     IF FND_API.to_Boolean( p_init_msg_list )
781     THEN
782         FND_MSG_PUB.initialize;
783     END IF;
784 
785     l_dun_contact_level := nvl(fnd_profile.value('IEX_DUNNING_CONTACT_SELECTION_METHOD'), 'ALL');  --Added for bug 6500750 gnramasa 13-Nov-07
786 
787     x_return_status := FND_API.G_RET_STS_SUCCESS;
788 
789     WriteLog('----------' || l_api_name || '----------');
790     WriteLog(l_api_name || ': input parameters:');
791     WriteLog(l_api_name || ': P_CUST_ACCT_ID: ' || P_CUST_ACCT_ID);
792     WriteLog(l_api_name || ': P_CONTACT_POINT_TYPE: ' || P_CONTACT_POINT_TYPE);
793 
794     X_LOCATION_ID := null;
795     X_CONTACT_ID := null;
796     X_CONTACT_POINT_ID := null;
797 
798     -- verify input parameters and if they are not set return immediately
799     if P_CUST_ACCT_ID is null or P_CONTACT_POINT_TYPE is null then
800         WriteLog(l_api_name || ': Not all input parameters have value');
801         return;
802     end if;
803 
804     -- Start for bug 6500750 gnramasa 13-Nov-07
805     IF l_dun_contact_level = 'ALL' OR l_dun_contact_level IS NULL then
806 
807 	    WriteLog(l_api_name || ': l_dun_contact_level : ' || l_dun_contact_level);
808 	    WriteLog(l_api_name || ': Searching for ACCOUNT location...');
809 	    OPEN get_data_crs2(P_CUST_ACCT_ID);
810 	    LOOP
811 		FETCH get_data_crs2 INTO l_display_order,
812 					 l_site_use_code,
813 					 l_location_id;
814 		exit when (get_data_crs2%NOTFOUND or l_display_order is null);
815 
816 		WriteLog(l_api_name || ': Found location:');
817 		WriteLog(l_api_name || ': l_display_order: ' || l_display_order);
818 		WriteLog(l_api_name || ': l_site_use_code: ' || l_site_use_code);
819 		WriteLog(l_api_name || ': l_location_id: ' || l_location_id);
820 
821 		if l_display_order = 1 or l_display_order = 2 then  -- dunning or primary bill-to
822 
823 		    X_LOCATION_ID := l_location_id;
824 		    exit;
825 
826 		elsif l_display_order = 3 then    -- regular bill-to
827 
828 		    OPEN get_data_crs3(P_CUST_ACCT_ID);
829 		    FETCH get_data_crs3 INTO l_bill_to_count;
830 		    CLOSE get_data_crs3;
831 
832 		    -- if more then 1 regular bill-to then set error
833 		    if l_bill_to_count > 1 then
834 			X_LOCATION_ID := null;
835 			WriteLog(l_api_name || ': ERROR: Multiple Bill-To locations found');
836 			FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_MULT_LOC');
837 			FND_MESSAGE.Set_Token('USAGE', 'bill-to', FALSE);
838 			FND_MSG_PUB.Add;
839 			x_return_status := FND_API.G_RET_STS_ERROR;
840 		    else
841 			X_LOCATION_ID := l_location_id;
842 		    end if;
843 		    exit;
844 
845 		end if;
846 
847 	    END LOOP;
848 	    CLOSE get_data_crs2;
849 
850 	    WriteLog(l_api_name || ': X_LOCATION_ID = ' || X_LOCATION_ID);
851         end if;
852 
853     WriteLog(l_api_name || ': Searching for ACCOUNT level contacts with ' || P_CONTACT_POINT_TYPE || ' contact point...');
854 
855     l_count := 0;
856     OPEN get_data_crs(P_CUST_ACCT_ID);
857     LOOP
858 
859         l_contact_point_id := null;
860         fetch get_data_crs into
861             l_order,
862             l_responsibility_type,
863             l_rel_party_id,
864             l_per_party_id,
865             l_org_party_id;
866         exit when get_data_crs%NOTFOUND;
867 
868 	IF l_dun_contact_level = 'ALL' OR l_dun_contact_level IS NULL then
869 	        WriteLog(l_api_name || ': l_dun_contact_level : ' || l_dun_contact_level);
870 
871 		l_count := l_count + 1;
872 		WriteLog(l_api_name || ': Found #' || l_count || ' contact = ' || l_per_party_id);
873 		WriteLog(l_api_name || ': l_rel_party_id = ' || l_rel_party_id);
874 		WriteLog(l_api_name || ': l_responsibility_type = ' || l_responsibility_type);
875 
876 		if P_CONTACT_POINT_TYPE = 'PRINTER' then
877 
878 		    WriteLog(l_api_name || ': For contact point PRINTER return first found contact');
879 		    X_CONTACT_ID := l_per_party_id;
880 		    X_CONTACT_POINT_ID := null;
881 		    WriteLog(l_api_name || ': X_CONTACT_ID = ' || X_CONTACT_ID);
882 		    WriteLog(l_api_name || ': X_CONTACT_POINT_ID = ' || X_CONTACT_POINT_ID);
883 		    return;
884 
885 		else
886 
887 		    OPEN get_data_crs1(l_rel_party_id, P_CONTACT_POINT_TYPE);
888 		    FETCH get_data_crs1 INTO l_order, l_contact_point_id;
889 		    CLOSE get_data_crs1;
890 
891 		    if l_contact_point_id is not null then
892 
893 			WriteLog(l_api_name || ': Found contact_point_id = ' || l_contact_point_id);
894 			X_CONTACT_ID := l_per_party_id;
895 			X_CONTACT_POINT_ID := l_contact_point_id;
896 			WriteLog(l_api_name || ': X_CONTACT_ID = ' || X_CONTACT_ID);
897 			WriteLog(l_api_name || ': X_CONTACT_POINT_ID = ' || X_CONTACT_POINT_ID);
898 			return;
899 
900 		    else
901 
902 			WriteLog(l_api_name || ': No contact points found for this contact');
903 
904 		    end if;
905 
906 		end if;
907 	ELSIF l_dun_contact_level='DUNNING' then
908 
909 	 WriteLog(l_api_name || ': l_dun_contact_level : ' || l_dun_contact_level);
910 	 if l_order in (1,2) then
911 
912 		l_count := l_count + 1;
913 		WriteLog(l_api_name || ': Found #' || l_count || ' contact = ' || l_per_party_id);
914 		WriteLog(l_api_name || ': l_rel_party_id = ' || l_rel_party_id);
915 		WriteLog(l_api_name || ': l_responsibility_type = ' || l_responsibility_type);
916 
917 		if P_CONTACT_POINT_TYPE = 'PRINTER' then
918 
919 		    WriteLog(l_api_name || ': For contact point PRINTER return first found contact');
920 		    X_CONTACT_ID := l_per_party_id;
921 		    X_CONTACT_POINT_ID := null;
922 		    WriteLog(l_api_name || ': X_CONTACT_ID = ' || X_CONTACT_ID);
923 		    WriteLog(l_api_name || ': X_CONTACT_POINT_ID = ' || X_CONTACT_POINT_ID);
924 		    --return;
925 		    exit;
926 
927 		else
928 
929 		    OPEN get_data_crs1(l_rel_party_id, P_CONTACT_POINT_TYPE);
930 		    FETCH get_data_crs1 INTO l_order, l_contact_point_id;
931 		    CLOSE get_data_crs1;
932 
933 		    if l_contact_point_id is not null then
934 
935 			WriteLog(l_api_name || ': Found contact_point_id = ' || l_contact_point_id);
936 			X_CONTACT_ID := l_per_party_id;
937 			X_CONTACT_POINT_ID := l_contact_point_id;
938 			WriteLog(l_api_name || ': X_CONTACT_ID = ' || X_CONTACT_ID);
939 			WriteLog(l_api_name || ': X_CONTACT_POINT_ID = ' || X_CONTACT_POINT_ID);
940 			--return;
941 			exit;
942 
943 		    else
944 
945 			WriteLog(l_api_name || ': No contact points found for this contact');
946 
947 		    end if;
948 
949 		end if;
950 	    end if;
951 
952 	END IF; --l_dun_contact_level
953     --End for bug 6500750 gnramasa 13-Nov-07
954 
955     END LOOP;
956     CLOSE get_data_crs;
957 
958     if l_count = 0 then
959         WriteLog(l_api_name || ': No ACCOUNT level contacts found');
960     end if;
961 
962     -- if no ACCOUNT contacts with contact point found - search for ACCOUNT SITE level contact points
963     if X_CONTACT_ID is null and X_CONTACT_POINT_ID is null then
964 
965         WriteLog(l_api_name || ': Searching for ACCOUNT level ' || P_CONTACT_POINT_TYPE || ' contact point...');
966 
967         OPEN get_party_crs(P_CUST_ACCT_ID);
968         FETCH get_party_crs INTO l_org_party_id;
969         CLOSE get_party_crs;
970 
971         WriteLog(l_api_name || ': l_org_party_id = ' || l_org_party_id);
972 
973         OPEN get_data_crs1(l_org_party_id, P_CONTACT_POINT_TYPE);
974         FETCH get_data_crs1 INTO l_order, l_contact_point_id;
975         CLOSE get_data_crs1;
976 
977         if l_contact_point_id is not null then
978 
979             WriteLog(l_api_name || ': Found contact_point_id = ' || l_contact_point_id);
980             X_CONTACT_ID := null;
981             X_CONTACT_POINT_ID := l_contact_point_id;
982             WriteLog(l_api_name || ': X_CONTACT_ID = ' || X_CONTACT_ID);
983             WriteLog(l_api_name || ': X_CONTACT_POINT_ID = ' || X_CONTACT_POINT_ID);
984             --return;
985 
986         else
987 
988             WriteLog(l_api_name || ': No contact points found');
989 
990         end if;
991 
992     end if;
993 
994         -- Start for bug 6500750 gnramasa 13-Nov-07
995     if l_dun_contact_level = 'DUNNING' then
996 	WriteLog(l_api_name || ': l_dun_contact_level : ' || l_dun_contact_level);
997 	WriteLog(l_api_name || ': Searching for ACCOUNT location...');
998 	    OPEN get_data_crs2(P_CUST_ACCT_ID);
999 	    LOOP
1000 		FETCH get_data_crs2 INTO l_display_order,
1001 					         l_site_use_code,
1002 					         l_location_id;
1003 		exit when (get_data_crs2%NOTFOUND or l_display_order is null);
1004 
1005 		WriteLog(l_api_name || ': Found location:');
1006 		WriteLog(l_api_name || ': l_display_order: ' || l_display_order);
1007 		WriteLog(l_api_name || ': l_site_use_code: ' || l_site_use_code);
1008 		WriteLog(l_api_name || ': l_location_id: ' || l_location_id);
1009 
1010 		if l_display_order = 1 then  -- dunning
1011 
1012 		    X_LOCATION_ID := l_location_id;
1013 		    exit;
1014 
1015 		else  -- no dunning purpose address found, so send it to identifying address.
1016 		    WriteLog(l_api_name || ' no dunning purpose address found, so send it to Identifying address');
1017 		    -- get relationship location_id
1018 		    OPEN get_party_crs(P_CUST_ACCT_ID);
1019 		    FETCH get_party_crs INTO l_org_party_id;
1020 		    CLOSE get_party_crs;
1021 
1022 		    OPEN get_old_loc_crs1(l_org_party_id);
1023 		    fetch get_old_loc_crs1 into l_location_id;
1024 		    CLOSE get_old_loc_crs1;
1025 
1026 		    if l_location_id is not null then
1027 			WriteLog(l_api_name || ': Found organization location: ' || l_location_id);
1028 		    else
1029 			WriteLog(l_api_name || ': No organization location found');
1030 		    end if;
1031 
1032 		    X_LOCATION_ID := l_location_id;
1033 		    exit;
1034 
1035 	        end if;
1036 
1037           END LOOP;
1038     CLOSE get_data_crs2;
1039 
1040     WriteLog(l_api_name || ': X_LOCATION_ID = ' || X_LOCATION_ID);
1041 
1042     end if;
1043     --End for bug 6500750 gnramasa 13-Nov-07
1044 
1045     WriteLog(l_api_name || ': X_CONTACT_ID = ' || X_CONTACT_ID);
1046     WriteLog(l_api_name || ': X_CONTACT_POINT_ID = ' || X_CONTACT_POINT_ID);
1047 
1048     FND_MSG_PUB.Count_And_Get
1049     (  p_count          =>   x_msg_count,
1050        p_data           =>   x_msg_data );
1051 
1052 EXCEPTION
1053   WHEN FND_API.G_EXC_ERROR THEN
1054       x_return_status := FND_API.G_RET_STS_ERROR;
1055       FND_MSG_PUB.Count_And_Get
1056       (  p_count          =>   x_msg_count,
1057          p_data           =>   x_msg_data );
1058       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception error='||SQLERRM);
1059 
1060   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1061       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1062       FND_MSG_PUB.Count_And_Get
1063       (  p_count          =>   x_msg_count,
1064          p_data           =>   x_msg_data );
1065       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExc Exception error='||SQLERRM);
1066 
1067   WHEN OTHERS THEN
1068       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1069       FND_MSG_PUB.Count_And_Get
1070       (  p_count          =>   x_msg_count,
1071          p_data           =>   x_msg_data );
1072       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception error='||SQLERRM);
1073 END;
1074 
1075 
1076 
1077 /*
1078     Returns CUSTOMER location_id, contact party_id and contact_pont_id for specified cust_account_id and contact_point_type
1079 */
1080 Procedure GET_CUST_DUNNING_DATA(p_api_version             IN NUMBER := 1.0,
1081                                 p_init_msg_list           IN VARCHAR2 ,
1082                                 p_commit                  IN VARCHAR2 ,
1083                                 P_PARTY_ID                IN NUMBER,
1084                                 P_CONTACT_POINT_TYPE      IN VARCHAR2,
1085                                 X_LOCATION_ID             OUT NOCOPY NUMBER,
1086                                 X_CONTACT_ID              OUT NOCOPY NUMBER,
1087                                 X_CONTACT_POINT_ID        OUT NOCOPY NUMBER,
1088                                 x_return_status           OUT NOCOPY VARCHAR2,
1089                                 x_msg_count               OUT NOCOPY NUMBER,
1090                                 x_msg_data                OUT NOCOPY VARCHAR2)
1091 IS
1092 /*-----------------------------------------------------------------------+
1093  | Local Variable Declarations and initializations                       |
1094  +-----------------------------------------------------------------------*/
1095     l_api_name                  CONSTANT VARCHAR2(30) := 'GET_CUST_DUNNING_DATA';
1096     l_cust_account_id           NUMBER;
1097     l_display_order             NUMBER;
1098     l_site_use_code             VARCHAR2(30);
1099     l_identifying_address_flag  VARCHAR2(1);
1100     l_primary_flag              VARCHAR2(1);
1101     l_count                     NUMBER;
1102     l_purpose_flag              varchar2(1);
1103     l_rel_party_id              NUMBER;
1104     l_contact_id                NUMBER;
1105     l_contact_point_id          NUMBER;
1106     l_location_id               NUMBER;
1107 
1108 /*-----------------------------------------------------------------------+
1109  | Cursor Declarations                                                   |
1110  +-----------------------------------------------------------------------*/
1111 
1112     -- get cust_account_id for the party identifying address
1113     cursor get_data_crs(P_PARTY_ID number) is
1114         select
1115             decode(site_use.site_use_code,
1116                 'DUN', decode(par_site.identifying_address_flag, 'Y', 1, 'N', 4),
1117                 'BILL_TO', decode(par_site.identifying_address_flag,
1118                               'Y', decode(site_use.primary_flag, 'Y', 2, 3),
1119                               'N', decode(site_use.primary_flag, 'Y', 5, 6))) Display_Order ,
1120             acct_site.cust_account_id,
1121             site_use.site_use_code,
1122             par_site.identifying_address_flag,
1123             site_use.primary_flag
1124         from  HZ_CUST_SITE_USES_ALL site_use,
1125             HZ_CUST_ACCT_SITES_ALL acct_site,
1126             hz_party_sites par_site
1127         where  par_site.party_id = P_PARTY_ID and
1128             par_site.status in ('A', 'I') and
1129             par_site.party_site_id = acct_site.party_site_id and
1130             acct_site.status = 'A' and
1131             acct_site.cust_acct_site_id = site_use.cust_acct_site_id and
1132             site_use.status = 'A'
1133         order by Display_Order;
1134 
1135     -- get count on locations
1136     cursor get_data_crs1(P_PARTY_ID number,
1137                          P_SITE_USE_CODE varchar2,
1138                          P_IDENT_FLAG varchar2,
1139                          P_PRIMARY_FLAG varchar2) is
1140         select count(1)
1141         from  HZ_CUST_SITE_USES_ALL site_use,
1142             HZ_CUST_ACCT_SITES_ALL acct_site,
1143             hz_party_sites par_site
1144         where  par_site.party_id = P_PARTY_ID and
1145             par_site.status in ('A', 'I') and
1146             par_site.party_site_id = acct_site.party_site_id and
1147             acct_site.status = 'A' and
1148             acct_site.cust_acct_site_id = site_use.cust_acct_site_id and
1149             site_use.status = 'A' and
1150             site_use.site_use_code = P_SITE_USE_CODE and
1151             par_site.identifying_address_flag = P_IDENT_FLAG and
1152             site_use.primary_flag = P_PRIMARY_FLAG;
1153 BEGIN
1154 
1155     IF FND_API.to_Boolean( p_init_msg_list )
1156     THEN
1157         FND_MSG_PUB.initialize;
1158     END IF;
1159 
1160     x_return_status := FND_API.G_RET_STS_SUCCESS;
1161 
1162     WriteLog('----------' || l_api_name || '----------');
1163     WriteLog(l_api_name || ': input parameters:');
1164     WriteLog(l_api_name || ': P_PARTY_ID: ' || P_PARTY_ID);
1165     WriteLog(l_api_name || ': P_CONTACT_POINT_TYPE: ' || P_CONTACT_POINT_TYPE);
1166 
1167     X_LOCATION_ID := null;
1168     X_CONTACT_ID := null;
1169     X_CONTACT_POINT_ID := null;
1170 
1171     -- verify input parameters and if they are not set return immediately
1172     if P_PARTY_ID is null or P_CONTACT_POINT_TYPE is null then
1173         WriteLog(l_api_name || ': Not all input parameters have value');
1174         return;
1175     end if;
1176 
1177     WriteLog(l_api_name || ': Searching for account...');
1178 
1179     -- searching for account
1180     OPEN get_data_crs(P_PARTY_ID);
1181     fetch get_data_crs into l_display_order,
1182                             l_cust_account_id,
1183                             l_site_use_code,
1184                             l_identifying_address_flag,
1185                             l_primary_flag;
1186     CLOSE get_data_crs;
1187 
1188     if l_cust_account_id is null then
1189         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_ACCOUNT');
1190         FND_MSG_PUB.ADD;
1191         RAISE FND_API.G_EXC_ERROR;
1192     end if;
1193 
1194     WriteLog(l_api_name || ': Found account:');
1195     WriteLog(l_api_name || ': l_display_order: ' || l_display_order);
1196     WriteLog(l_api_name || ': l_cust_account_id = ' || l_cust_account_id);
1197     WriteLog(l_api_name || ': l_site_use_code: ' || l_site_use_code);
1198     WriteLog(l_api_name || ': l_identifying_address_flag: ' || l_identifying_address_flag);
1199     WriteLog(l_api_name || ': l_primary_flag: ' || l_primary_flag);
1200 
1201     OPEN get_data_crs1(P_PARTY_ID, l_site_use_code, l_identifying_address_flag, l_primary_flag);
1202     FETCH get_data_crs1 INTO l_count;
1203     CLOSE get_data_crs1;
1204 
1205     WriteLog(l_api_name || ': Locations count = ' || l_count);
1206 
1207     -- if more then 1 regular bill-to then set error
1208     if l_count > 1 then
1209         WriteLog(l_api_name || ': ERROR: Multiple locations found');
1210         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_MULT_LOC');
1211         FND_MESSAGE.Set_Token('USAGE', l_site_use_code, FALSE);
1212         FND_MSG_PUB.Add;
1213         x_return_status := FND_API.G_RET_STS_ERROR;
1214     end if;
1215 
1216     -- call GET_ACCT_DUNNING_DATA for found account
1217     WriteLog(l_api_name || ': Calling GET_ACCT_DUNNING_DATA for account ' || l_cust_account_id);
1218     GET_ACCT_DUNNING_DATA(p_api_version => 1.0,
1219                           p_init_msg_list => FND_API.G_FALSE,
1220                           p_commit => FND_API.G_FALSE,
1221                           P_CUST_ACCT_ID => l_cust_account_id,
1222                           P_CONTACT_POINT_TYPE => P_CONTACT_POINT_TYPE,
1223                           X_LOCATION_ID => X_LOCATION_ID,
1224                           X_CONTACT_ID => X_CONTACT_ID,
1225                           X_CONTACT_POINT_ID => X_CONTACT_POINT_ID,
1226                           x_return_status => x_return_status,
1227                           x_msg_count => x_msg_count,
1228                           x_msg_data => x_msg_data);
1229 
1230     WriteLog('---------- continue ' || l_api_name || '----------');
1231     WriteLog(l_api_name || ': x_return_status: ' || x_return_status);
1232     WriteLog(l_api_name || ': x_msg_count: ' || x_msg_count);
1233 
1234     WriteLog(l_api_name || ': X_LOCATION_ID = ' || X_LOCATION_ID);
1235     WriteLog(l_api_name || ': X_CONTACT_ID = ' || X_CONTACT_ID);
1236     WriteLog(l_api_name || ': X_CONTACT_POINT_ID = ' || X_CONTACT_POINT_ID);
1237 
1238     FND_MSG_PUB.Count_And_Get
1239     (  p_count          =>   x_msg_count,
1240        p_data           =>   x_msg_data );
1241 
1242 EXCEPTION
1243   WHEN FND_API.G_EXC_ERROR THEN
1244       x_return_status := FND_API.G_RET_STS_ERROR;
1245       FND_MSG_PUB.Count_And_Get
1246       (  p_count          =>   x_msg_count,
1247          p_data           =>   x_msg_data );
1248       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception error='||SQLERRM);
1249 
1250   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1251       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1252       FND_MSG_PUB.Count_And_Get
1253       (  p_count          =>   x_msg_count,
1254          p_data           =>   x_msg_data );
1255       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExc Exception error='||SQLERRM);
1256 
1257   WHEN OTHERS THEN
1258       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1259       FND_MSG_PUB.Count_And_Get
1260       (  p_count          =>   x_msg_count,
1261          p_data           =>   x_msg_data );
1262       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception error='||SQLERRM);
1263 END;
1264 
1265 
1266 
1267 -- get dafault dunning destination
1268 procedure GET_DEFAULT_DUN_DEST(p_api_version              IN NUMBER := 1.0,
1269                              p_init_msg_list            IN VARCHAR2,
1270                              p_commit                   IN VARCHAR2,
1271                              p_level                    in varchar2,
1272                              p_source_id                in number,
1273                              p_send_method              in varchar2,
1274                              X_LOCATION_ID              OUT NOCOPY NUMBER,
1275                              X_CONTACT_ID               OUT NOCOPY NUMBER,
1276                              X_CONTACT_POINT_ID         OUT NOCOPY NUMBER,
1277                              x_return_status            OUT NOCOPY VARCHAR2,
1278                              x_msg_count                OUT NOCOPY NUMBER,
1279                              x_msg_data                 OUT NOCOPY VARCHAR2)
1280 is
1281 
1282     l_api_name                constant varchar2(25) := 'GET_DEFAULT_DUN_DEST';
1283     l_temp_site_use_id        number;
1284     l_party_id                number;
1285 
1286     cursor c_get_party_from_acc(p_cust_account_id number) is
1287         select party_id
1288         from hz_cust_accounts
1289         where cust_account_id = p_cust_account_id;
1290 
1291     cursor c_get_party_from_site(p_site_use_id number) is
1292         select cust.party_id
1293         from hz_cust_accounts cust,
1294         hz_cust_acct_sites_all acc_site,
1295         hz_cust_site_uses_all site_use
1296         where site_use.site_use_id = p_site_use_id and
1297         site_use.cust_acct_site_id = acc_site.cust_acct_site_id and
1298         acc_site.cust_account_id = cust.cust_account_id;
1299 
1300     cursor c_get_party_from_del(p_delinquency_id number) is
1301         select party_cust_id, customer_site_use_id
1302         from iex_delinquencies_all
1303         where delinquency_id = p_delinquency_id;
1304 
1305 begin
1306 
1307     IF FND_API.to_Boolean( p_init_msg_list )
1308     THEN
1309         FND_MSG_PUB.initialize;
1310     END IF;
1311 
1312     x_return_status := FND_API.G_RET_STS_SUCCESS;
1313 
1314     WriteLog('----------' || l_api_name || '----------');
1315     WriteLog(l_api_name || ': input parameters:');
1316     WriteLog(l_api_name || ': p_level: ' || p_level);
1317     WriteLog(l_api_name || ': p_source_id: ' || p_source_id);
1318     WriteLog(l_api_name || ': p_send_method: ' || p_send_method);
1319 
1320     if p_level = 'CUSTOMER' then
1321         l_party_id := p_source_id;
1322     elsif p_level = 'ACCOUNT' then
1323         WriteLog(l_api_name || ': getting party from account...');
1324         open c_get_party_from_acc(p_source_id);
1325         fetch c_get_party_from_acc into l_party_id;
1326         close c_get_party_from_acc;
1327     elsif p_level = 'BILL_TO' then
1328         WriteLog(l_api_name || ': getting party from site...');
1329         open c_get_party_from_site(p_source_id);
1330         fetch c_get_party_from_site into l_party_id;
1331         close c_get_party_from_site;
1332     elsif p_level = 'DELINQUENCY' then
1333         WriteLog(l_api_name || ': getting party from delinquency...');
1334         open c_get_party_from_del(p_source_id);
1335         fetch c_get_party_from_del into l_party_id, l_temp_site_use_id;
1336         close c_get_party_from_del;
1337     end if;
1338     WriteLog(l_api_name || ': party_id = ' || l_party_id);
1339 
1340     WriteLog(l_api_name || ': Calling GET_CUST_DUNNING_DATA_OW...');
1341     GET_CUST_DUNNING_DATA_OW(p_api_version => 1.0,
1342                           p_init_msg_list => FND_API.G_TRUE,
1343                           p_commit => FND_API.G_FALSE,
1344                           P_PARTY_ID => l_party_id,
1345                           P_CONTACT_POINT_TYPE => p_send_method,
1346                           X_LOCATION_ID => X_LOCATION_ID,
1347                           X_CONTACT_ID => X_CONTACT_ID,
1348                           X_CONTACT_POINT_ID => X_CONTACT_POINT_ID,
1349                           x_return_status => x_return_status,
1350                           x_msg_count => x_msg_count,
1351                           x_msg_data => x_msg_data);
1352 
1353     if (p_send_method = 'PRINTER' and (X_LOCATION_ID is null or X_CONTACT_ID is null)) or
1354        (p_send_method <> 'PRINTER' and (X_LOCATION_ID is null or X_CONTACT_ID is null or X_CONTACT_POINT_ID is null)) then
1355 
1356         WriteLog('---------- continue ' || l_api_name || '----------');
1357         X_LOCATION_ID := null;
1358         X_CONTACT_ID := null;
1359         X_CONTACT_POINT_ID := null;
1360 
1361         WriteLog(l_api_name || ': Did not find data the old way. Continue with new way');
1362         if p_level = 'CUSTOMER' then
1363 
1364             WriteLog(l_api_name || ': Calling GET_CUST_DUNNING_DATA...');
1365             GET_CUST_DUNNING_DATA(p_api_version => 1.0,
1366                                   p_init_msg_list => FND_API.G_TRUE,
1367                                   p_commit => FND_API.G_FALSE,
1368                                   P_PARTY_ID => p_source_id,
1369                                   P_CONTACT_POINT_TYPE => p_send_method,
1370                                   X_LOCATION_ID => X_LOCATION_ID,
1371                                   X_CONTACT_ID => X_CONTACT_ID,
1372                                   X_CONTACT_POINT_ID => X_CONTACT_POINT_ID,
1373                                   x_return_status => x_return_status,
1374                                   x_msg_count => x_msg_count,
1375                                   x_msg_data => x_msg_data);
1376 
1377         elsif p_level = 'ACCOUNT' then
1378 
1379             WriteLog(l_api_name || ': Calling GET_ACCT_DUNNING_DATA...');
1380             GET_ACCT_DUNNING_DATA(p_api_version => 1.0,
1381                                   p_init_msg_list => FND_API.G_TRUE,
1382                                   p_commit => FND_API.G_FALSE,
1383                                   P_CUST_ACCT_ID => p_source_id,
1384                                   P_CONTACT_POINT_TYPE => p_send_method,
1385                                   X_LOCATION_ID => X_LOCATION_ID,
1386                                   X_CONTACT_ID => X_CONTACT_ID,
1387                                   X_CONTACT_POINT_ID => X_CONTACT_POINT_ID,
1388                                   x_return_status => x_return_status,
1389                                   x_msg_count => x_msg_count,
1390                                   x_msg_data => x_msg_data);
1391 
1392         elsif p_level = 'BILL_TO' then
1393 
1394             WriteLog(l_api_name || ': Calling GET_SITE_DUNNING_DATA...');
1395             GET_SITE_DUNNING_DATA(p_api_version => 1.0,
1396                                   p_init_msg_list => FND_API.G_TRUE,
1397                                   p_commit => FND_API.G_FALSE,
1398                                   P_SITE_USE_ID => p_source_id,
1399                                   P_CONTACT_POINT_TYPE => p_send_method,
1400                                   X_LOCATION_ID => X_LOCATION_ID,
1401                                   X_CONTACT_ID => X_CONTACT_ID,
1402                                   X_CONTACT_POINT_ID => X_CONTACT_POINT_ID,
1403                                   x_return_status => x_return_status,
1404                                   x_msg_count => x_msg_count,
1405                                   x_msg_data => x_msg_data);
1406 
1407         elsif p_level = 'DELINQUENCY' then
1408 
1409             WriteLog(l_api_name || ': site_use_id = ' || l_temp_site_use_id);
1410             WriteLog(l_api_name || ': Calling GET_SITE_DUNNING_DATA...');
1411             GET_SITE_DUNNING_DATA(p_api_version => 1.0,
1412                                   p_init_msg_list => FND_API.G_TRUE,
1413                                   p_commit => FND_API.G_FALSE,
1414                                   P_SITE_USE_ID => l_temp_site_use_id,
1415                                   P_CONTACT_POINT_TYPE => p_send_method,
1416                                   X_LOCATION_ID => X_LOCATION_ID,
1417                                   X_CONTACT_ID => X_CONTACT_ID,
1418                                   X_CONTACT_POINT_ID => X_CONTACT_POINT_ID,
1419                                   x_return_status => x_return_status,
1420                                   x_msg_count => x_msg_count,
1421                                   x_msg_data => x_msg_data);
1422 
1423         end if;
1424 
1425     end if;
1426 
1427     WriteLog('---------- continue ' || l_api_name || '----------');
1428     WriteLog(l_api_name || ': LOCATION_ID: ' || X_LOCATION_ID);
1429     WriteLog(l_api_name || ': CONTACT_ID: ' || X_CONTACT_ID);
1430     WriteLog(l_api_name || ': CONTACT_POINT_ID: ' || X_CONTACT_POINT_ID);
1431 
1432     WriteLog(l_api_name || ': x_return_status: ' || x_return_status);
1433     WriteLog(l_api_name || ': x_msg_count: ' || x_msg_count);
1434 
1435 EXCEPTION
1436   WHEN FND_API.G_EXC_ERROR THEN
1437       x_return_status := FND_API.G_RET_STS_ERROR;
1438       FND_MSG_PUB.Count_And_Get
1439       (  p_count          =>   x_msg_count,
1440          p_data           =>   x_msg_data );
1441       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception error='||SQLERRM);
1442 
1443   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1444       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1445       FND_MSG_PUB.Count_And_Get
1446       (  p_count          =>   x_msg_count,
1447          p_data           =>   x_msg_data );
1448       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExc Exception error='||SQLERRM);
1449 
1450   WHEN OTHERS THEN
1451       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1452       FND_MSG_PUB.Count_And_Get
1453       (  p_count          =>   x_msg_count,
1454          p_data           =>   x_msg_data );
1455       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception error='||SQLERRM);
1456 END;
1457 
1458 
1459 
1460 -- get dafault dunning data
1461 procedure GET_DEFAULT_DUN_DATA(p_api_version              IN NUMBER := 1.0,
1462                              p_init_msg_list            IN VARCHAR2,
1463                              p_commit                   IN VARCHAR2,
1464                              p_level                    in varchar2,
1465                              p_source_id                in number,
1466                              p_send_method              in varchar2,
1467                              p_resend                   IN VARCHAR2 ,
1468                              p_object_code              IN VARCHAR2 ,
1469                              p_object_id                IN NUMBER,
1470                              p_fulfillment_bind_tbl     in out nocopy IEX_DUNNING_PVT.FULFILLMENT_BIND_TBL,
1471                              x_return_status            OUT NOCOPY VARCHAR2,
1472                              x_msg_count                OUT NOCOPY NUMBER,
1473                              x_msg_data                 OUT NOCOPY VARCHAR2)
1474 
1475 is
1476 
1477     l_fulfillment_bind_tbl    IEX_DUNNING_PVT.FULFILLMENT_BIND_TBL;
1478     l_bind_count              number;
1479     l_del_index               number;
1480     l_location_index          number;
1481     l_contact_index           number;
1482     l_site_use_index          number;
1483     l_cust_account_index      number;
1484     l_party_index             number;
1485     l_delinquency_id          number;
1486     l_customer_site_use_id    number;
1487     l_cust_account_id         number;
1488     l_party_id                number;
1489     l_location_id             number;
1490     l_contact_id              number;
1491     l_display                 number; --placeholder for display order
1492     l_api_name                constant varchar2(25) := 'GET_DEFAULT_DUN_DATA';
1493     Type refCur               is Ref Cursor;
1494     sql_cur                   refCur;
1495     vPLSQL                    VARCHAR2(2000);
1496     l_temp_site_use_id        number;
1497     l_contact_point_id        number;
1498     l_receipt_index           number;
1499     l_payment_index           number;
1500     l_promise_index           number;
1501     l_invoice_index           number;
1502     l_dispute_index           number;
1503     l_cm_request_index        number;
1504     l_adjustment_index        number;
1505     l_contact_point_index     number;
1506     l_temp_party_id           number;
1507     l_temp_acct_id            number;
1508     l_dispute_id              number;
1509 
1510     cursor c_get_del_info(p_delinquency_id number) is
1511         select party_cust_id, cust_account_id, customer_site_use_id
1512         from iex_delinquencies_all
1513         where delinquency_id = p_delinquency_id;
1514 
1515     cursor c_get_acct_info(p_cust_account_id number) is
1516         select party_id
1517         from hz_cust_accounts
1518         where cust_account_id = p_cust_account_id;
1519 
1520     cursor c_get_site_info(p_site_use_id number) is
1521         select cust.party_id, cust.cust_account_id
1522         from hz_cust_accounts cust,
1523         hz_cust_acct_sites_all acc_site,
1524         hz_cust_site_uses_all site_use
1525         where site_use.site_use_id = p_site_use_id and
1526         site_use.cust_acct_site_id = acc_site.cust_acct_site_id and
1527         acc_site.cust_account_id = cust.cust_account_id;
1528 
1529     cursor c_get_dispute(p_cm_request_id number) is
1530         select dispute_id
1531         from IEX_DISPUTES
1532         where cm_request_id = p_cm_request_id;
1533 
1534 begin
1535 
1536     IF FND_API.to_Boolean( p_init_msg_list )
1537     THEN
1538         FND_MSG_PUB.initialize;
1539     END IF;
1540 
1541     x_return_status := FND_API.G_RET_STS_SUCCESS;
1542 
1543     WriteLog('----------' || l_api_name || '----------');
1544     WriteLog(l_api_name || ': input parameters:');
1545     WriteLog(l_api_name || ': p_level: ' || p_level);
1546     WriteLog(l_api_name || ': p_source_id: ' || p_source_id);
1547 
1548     l_fulfillment_bind_tbl    := p_fulfillment_bind_tbl;
1549     l_del_index               := 0;
1550     l_site_use_index          := 0;
1551     l_cust_account_index      := 0;
1552     l_party_index             := 0;
1553     l_location_id             := 0;
1554     l_contact_id              := 0;
1555     l_location_index          := 0;
1556     l_contact_index           := 0;
1557     l_receipt_index           := 0;
1558     l_payment_index           := 0;
1559     l_promise_index           := 0;
1560     l_invoice_index           := 0;
1561     l_dispute_index           := 0;
1562     l_cm_request_index        := 0;
1563     l_adjustment_index        := 0;
1564     l_contact_point_index     := 0;
1565 
1566     WriteLog(l_api_name || ': input bind table:');
1567     -- index all bind variables
1568     l_bind_count := l_fulfillment_bind_tbl.count;
1569     for k in 1..l_bind_count loop
1570 
1571       l_fulfillment_bind_tbl(k).key_name := upper(l_fulfillment_bind_tbl(k).key_name);
1572 
1573       WriteLog(l_api_name || ' - #' || k || ' - ' ||
1574           l_fulfillment_bind_tbl(k).key_name || ' = ' || l_fulfillment_bind_tbl(k).key_value);
1575 
1576     end loop;
1577 
1578     -- Adding and filling missing bind vars
1579     WriteLog(l_api_name || ': Adding and filling missing bind vars...');
1580 
1581     if p_resend = 'Y' then
1582 
1583         l_bind_count := l_bind_count + 1;
1584         if p_object_code = 'IEX_INVOICES' then
1585 
1586             l_fulfillment_bind_tbl(l_bind_count).key_name := 'INVOICE_ID';
1587             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1588             l_fulfillment_bind_tbl(l_bind_count).key_value := p_object_id;
1589             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1590 
1591         elsif p_object_code = 'IEX_PAYMENT' then
1592 
1593             l_fulfillment_bind_tbl(l_bind_count).key_name := 'PAYMENT_ID';
1594             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1595             l_fulfillment_bind_tbl(l_bind_count).key_value := p_object_id;
1596             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1597 
1598         elsif p_object_code = 'IEX_ADJUSTMENT' then
1599 
1600             l_fulfillment_bind_tbl(l_bind_count).key_name := 'ADJUSTMENT_ID';
1601             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1602             l_fulfillment_bind_tbl(l_bind_count).key_value := p_object_id;
1603             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1604 
1605         elsif p_object_code = 'IEX_PROMISE' then
1606 
1607             l_fulfillment_bind_tbl(l_bind_count).key_name := 'PROMISE_DETAIL_ID';
1608             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1609             l_fulfillment_bind_tbl(l_bind_count).key_value := p_object_id;
1610             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1611 
1612         elsif p_object_code = 'IEX_REVERSAL' then
1613 
1614             l_fulfillment_bind_tbl(l_bind_count).key_name := 'RECEIPT_ID';
1615             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1616             l_fulfillment_bind_tbl(l_bind_count).key_value := p_object_id;
1617             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1618 
1619         elsif p_object_code = 'IEX_DISPUTE' then
1620 
1621             -- this case for resend of dispute corespondance
1622             open c_get_dispute(p_object_id);
1623             fetch c_get_dispute into l_dispute_id;
1624             close c_get_dispute;
1625 
1626             if l_dispute_id is not null then
1627                 l_fulfillment_bind_tbl(l_bind_count).key_name := 'DISPUTE_ID';
1628                 l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1629                 l_fulfillment_bind_tbl(l_bind_count).key_value := l_dispute_id;
1630                 WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1631             end if;
1632 
1633         elsif p_object_code = 'IEX_ACCOUNT' then
1634 
1635             l_fulfillment_bind_tbl(l_bind_count).key_name := 'ACCOUNT_ID';
1636             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1637             l_fulfillment_bind_tbl(l_bind_count).key_value := p_object_id;
1638             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1639 
1640         elsif p_object_code = 'IEX_BILLTO' then
1641 
1642             l_fulfillment_bind_tbl(l_bind_count).key_name := 'CUSTOMER_SITE_USE_ID';
1643             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1644             l_fulfillment_bind_tbl(l_bind_count).key_value := p_object_id;
1645             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1646 
1647         elsif p_object_code = 'IEX_DELINQUENCY' then
1648 
1649             l_fulfillment_bind_tbl(l_bind_count).key_name := 'DELINQUENCY_ID';
1650             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1651             l_fulfillment_bind_tbl(l_bind_count).key_value := p_object_id;
1652             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1653 
1654         elsif p_object_code = 'IEX_STRATEGY' then
1655 
1656             IF (p_level = 'CUSTOMER') then
1657                 l_fulfillment_bind_tbl(l_bind_count).key_name := 'PARTY_ID';
1658                 l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1659                 l_fulfillment_bind_tbl(l_bind_count).key_value := p_source_id;
1660                 WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1661             ELSIF (p_level = 'ACCOUNT') then
1662                 l_fulfillment_bind_tbl(l_bind_count).key_name := 'ACCOUNT_ID';
1663                 l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1664                 l_fulfillment_bind_tbl(l_bind_count).key_value := p_source_id;
1665                 WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1666             ELSIF (p_level = 'BILL_TO') then
1667                 l_fulfillment_bind_tbl(l_bind_count).key_name := 'CUSTOMER_SITE_USE_ID';
1668                 l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1669                 l_fulfillment_bind_tbl(l_bind_count).key_value := p_source_id;
1670                 WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1671             ELSIF (p_level = 'DELINQUENCY') then
1672                 l_fulfillment_bind_tbl(l_bind_count).key_name := 'DELINQUENCY_ID';
1673                 l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1674                 l_fulfillment_bind_tbl(l_bind_count).key_value := p_source_id;
1675                 WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1676             END IF;
1677 
1678         end if;
1679 
1680     end if;
1681 
1682     -- index all bind variables
1683     l_bind_count := l_fulfillment_bind_tbl.count;
1684     for k in 1..l_bind_count loop
1685 
1686       if l_fulfillment_bind_tbl(k).key_name = 'DELINQUENCY_ID' then
1687             l_del_index          := k;
1688       elsif l_fulfillment_bind_tbl(k).key_name = 'CUSTOMER_SITE_USE_ID' then
1689             l_site_use_index     := k;
1690       elsif l_fulfillment_bind_tbl(k).key_name = 'ACCOUNT_ID' or l_fulfillment_bind_tbl(k).key_name = 'CUST_ACCOUNT_ID' then
1691             l_fulfillment_bind_tbl(k).key_name := 'ACCOUNT_ID';
1692             l_cust_account_index := k;
1693       elsif l_fulfillment_bind_tbl(k).key_name = 'PARTY_ID' then
1694             l_party_index        := k;
1695       elsif l_fulfillment_bind_tbl(k).key_name = 'LOCATION_ID' then
1696             l_location_index     := k;
1697       elsif l_fulfillment_bind_tbl(k).key_name = 'CONTACT_ID' then
1698             l_contact_index      := k;
1699       elsif l_fulfillment_bind_tbl(k).key_name = 'CONTACT_POINT_ID' then
1700             l_contact_point_index      := k;
1701       elsif l_fulfillment_bind_tbl(k).key_name = 'RECEIPT_ID' then
1702             l_receipt_index      := k;
1703       elsif l_fulfillment_bind_tbl(k).key_name = 'PAYMENT_ID' then
1704             l_payment_index      := k;
1705       elsif l_fulfillment_bind_tbl(k).key_name = 'PROMISE_DETAIL_ID' then
1706             l_promise_index      := k;
1707       elsif l_fulfillment_bind_tbl(k).key_name = 'INVOICE_ID' then
1708             l_invoice_index      := k;
1709       elsif l_fulfillment_bind_tbl(k).key_name = 'DISPUTE_ID' then
1710             l_dispute_index      := k;
1711       elsif l_fulfillment_bind_tbl(k).key_name = 'ADJUSTMENT_ID' then
1712             l_adjustment_index   := k;
1713       end if;
1714 
1715     end loop;
1716 
1717     -- filling missing but required bind vars
1718     if p_level = 'CUSTOMER' then
1719 
1720         if l_party_index = 0 then
1721             l_bind_count := l_bind_count + 1;
1722             l_fulfillment_bind_tbl(l_bind_count).key_name  := 'PARTY_ID';
1723             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1724             l_fulfillment_bind_tbl(l_bind_count).key_value := p_source_id;
1725             l_party_index := l_bind_count;
1726             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1727         elsif l_party_index > 0 and l_fulfillment_bind_tbl(l_party_index).key_value is null then
1728             l_fulfillment_bind_tbl(l_party_index).key_value := p_source_id;
1729             WriteLog(l_api_name || ': Updated ' || l_fulfillment_bind_tbl(l_party_index).key_name || ' value in bind table');
1730         end if;
1731 
1732         WriteLog(l_api_name || ': PARTY_ID: ' || l_fulfillment_bind_tbl(l_party_index).key_value);
1733 
1734     elsif p_level = 'ACCOUNT' then
1735 
1736         if l_cust_account_index = 0 then
1737             l_bind_count := l_bind_count + 1;
1738             l_fulfillment_bind_tbl(l_bind_count).key_name  := 'ACCOUNT_ID';
1739             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1740             l_fulfillment_bind_tbl(l_bind_count).key_value := p_source_id;
1741             l_cust_account_index := l_bind_count;
1742             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1743         elsif l_cust_account_index > 0 and l_fulfillment_bind_tbl(l_cust_account_index).key_value is null then
1744             l_fulfillment_bind_tbl(l_cust_account_index).key_value := p_source_id;
1745             WriteLog(l_api_name || ': Updated ' || l_fulfillment_bind_tbl(l_cust_account_index).key_name || ' value in bind table');
1746         end if;
1747 
1748         open c_get_acct_info(l_fulfillment_bind_tbl(l_cust_account_index).key_value);
1749         fetch c_get_acct_info into l_temp_party_id;
1750         close c_get_acct_info;
1751 
1752         if l_party_index = 0 then
1753             l_bind_count := l_bind_count + 1;
1754             l_fulfillment_bind_tbl(l_bind_count).key_name  := 'PARTY_ID';
1755             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1756             l_fulfillment_bind_tbl(l_bind_count).key_value := l_temp_party_id;
1757             l_party_index := l_bind_count;
1758             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1759         elsif l_party_index > 0 and l_fulfillment_bind_tbl(l_party_index).key_value is null then
1760             l_fulfillment_bind_tbl(l_party_index).key_value := l_temp_party_id;
1761             WriteLog(l_api_name || ': Updated ' || l_fulfillment_bind_tbl(l_party_index).key_name || ' value in bind table');
1762         end if;
1763 
1764         WriteLog(l_api_name || ': PARTY_ID: ' || l_fulfillment_bind_tbl(l_party_index).key_value);
1765         WriteLog(l_api_name || ': ACCOUNT_ID: ' || l_fulfillment_bind_tbl(l_cust_account_index).key_value);
1766 
1767     elsif p_level = 'BILL_TO' then
1768 
1769         if l_site_use_index = 0 then
1770             l_bind_count := l_bind_count + 1;
1771             l_fulfillment_bind_tbl(l_bind_count).key_name  := 'CUSTOMER_SITE_USE_ID';
1772             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1773             l_fulfillment_bind_tbl(l_bind_count).key_value := p_source_id;
1774             l_site_use_index := l_bind_count;
1775             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1776         elsif l_site_use_index > 0 and l_fulfillment_bind_tbl(l_site_use_index).key_value is null then
1777             l_fulfillment_bind_tbl(l_site_use_index).key_value := p_source_id;
1778             WriteLog(l_api_name || ': Updated ' || l_fulfillment_bind_tbl(l_site_use_index).key_name || ' value in bind table');
1779         end if;
1780 
1781         open c_get_site_info(l_fulfillment_bind_tbl(l_site_use_index).key_value);
1782         fetch c_get_site_info into l_temp_party_id, l_temp_acct_id;
1783         close c_get_site_info;
1784 
1785         if l_party_index = 0 then
1786             l_bind_count := l_bind_count + 1;
1787             l_fulfillment_bind_tbl(l_bind_count).key_name  := 'PARTY_ID';
1788             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1789             l_fulfillment_bind_tbl(l_bind_count).key_value := l_temp_party_id;
1790             l_party_index := l_bind_count;
1791             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1792         elsif l_party_index > 0 and l_fulfillment_bind_tbl(l_party_index).key_value is null then
1793             l_fulfillment_bind_tbl(l_party_index).key_value := l_temp_party_id;
1794             WriteLog(l_api_name || ': Updated ' || l_fulfillment_bind_tbl(l_party_index).key_name || ' value in bind table');
1795         end if;
1796 
1797         if l_cust_account_index = 0 then
1798             l_bind_count := l_bind_count + 1;
1799             l_fulfillment_bind_tbl(l_bind_count).key_name  := 'ACCOUNT_ID';
1800             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1801             l_fulfillment_bind_tbl(l_bind_count).key_value := l_temp_acct_id;
1802             l_cust_account_index := l_bind_count;
1803             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1804         elsif l_cust_account_index > 0 and l_fulfillment_bind_tbl(l_cust_account_index).key_value is null then
1805             l_fulfillment_bind_tbl(l_cust_account_index).key_value := l_temp_acct_id;
1806             WriteLog(l_api_name || ': Updated ' || l_fulfillment_bind_tbl(l_cust_account_index).key_name || ' value in bind table');
1807         end if;
1808 
1809         WriteLog(l_api_name || ': PARTY_ID: ' || l_fulfillment_bind_tbl(l_party_index).key_value);
1810         WriteLog(l_api_name || ': ACCOUNT_ID: ' || l_fulfillment_bind_tbl(l_cust_account_index).key_value);
1811         WriteLog(l_api_name || ': CUSTOMER_SITE_USE_ID: ' || l_fulfillment_bind_tbl(l_site_use_index).key_value);
1812 
1813     elsif p_level = 'DELINQUENCY' then
1814 
1815         if l_del_index = 0 then
1816             l_bind_count := l_bind_count + 1;
1817             l_fulfillment_bind_tbl(l_bind_count).key_name  := 'DELINQUENCY_ID';
1818             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1819             l_fulfillment_bind_tbl(l_bind_count).key_value := p_source_id;
1820             l_del_index := l_bind_count;
1821             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1822         elsif l_del_index > 0 and l_fulfillment_bind_tbl(l_del_index).key_value is null then
1823             l_fulfillment_bind_tbl(l_del_index).key_value := p_source_id;
1824             WriteLog(l_api_name || ': Updated ' || l_fulfillment_bind_tbl(l_del_index).key_name || ' value in bind table');
1825         end if;
1826 
1827         open c_get_del_info(l_fulfillment_bind_tbl(l_del_index).key_value);
1828         fetch c_get_del_info into l_temp_party_id, l_temp_acct_id, l_temp_site_use_id;
1829         close c_get_del_info;
1830 
1831         if l_party_index = 0 then
1832             l_bind_count := l_bind_count + 1;
1833             l_fulfillment_bind_tbl(l_bind_count).key_name  := 'PARTY_ID';
1834             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1835             l_fulfillment_bind_tbl(l_bind_count).key_value := l_temp_party_id;
1836             l_party_index := l_bind_count;
1837             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1838         elsif l_party_index > 0 and l_fulfillment_bind_tbl(l_party_index).key_value is null then
1839             l_fulfillment_bind_tbl(l_party_index).key_value := l_temp_party_id;
1840             WriteLog(l_api_name || ': Updated ' || l_fulfillment_bind_tbl(l_party_index).key_name || ' value in bind table');
1841         end if;
1842 
1843         if l_cust_account_index = 0 then
1844             l_bind_count := l_bind_count + 1;
1845             l_fulfillment_bind_tbl(l_bind_count).key_name  := 'ACCOUNT_ID';
1846             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1847             l_fulfillment_bind_tbl(l_bind_count).key_value := l_temp_acct_id;
1848             l_cust_account_index := l_bind_count;
1849             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1850         elsif l_cust_account_index > 0 and l_fulfillment_bind_tbl(l_cust_account_index).key_value is null then
1851             l_fulfillment_bind_tbl(l_cust_account_index).key_value := l_temp_acct_id;
1852             WriteLog(l_api_name || ': Updated ' || l_fulfillment_bind_tbl(l_cust_account_index).key_name || ' value in bind table');
1853         end if;
1854 
1855         if l_site_use_index = 0 then
1856             l_bind_count := l_bind_count + 1;
1857             l_fulfillment_bind_tbl(l_bind_count).key_name  := 'CUSTOMER_SITE_USE_ID';
1858             l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1859             l_fulfillment_bind_tbl(l_bind_count).key_value := l_temp_site_use_id;
1860             l_site_use_index := l_bind_count;
1861             WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1862         elsif l_site_use_index > 0 and l_fulfillment_bind_tbl(l_site_use_index).key_value is null then
1863             l_fulfillment_bind_tbl(l_site_use_index).key_value := l_temp_site_use_id;
1864             WriteLog(l_api_name || ': Updated ' || l_fulfillment_bind_tbl(l_site_use_index).key_name || ' value in bind table');
1865         end if;
1866 
1867         WriteLog(l_api_name || ': PARTY_ID: ' || l_fulfillment_bind_tbl(l_party_index).key_value);
1868         WriteLog(l_api_name || ': ACCOUNT_ID: ' || l_fulfillment_bind_tbl(l_cust_account_index).key_value);
1869         WriteLog(l_api_name || ': CUSTOMER_SITE_USE_ID: ' || l_fulfillment_bind_tbl(l_site_use_index).key_value);
1870         WriteLog(l_api_name || ': DELINQUENCY_ID: ' || l_fulfillment_bind_tbl(l_del_index).key_value);
1871 
1872     end if;
1873 
1874     WriteLog(l_api_name || ': Calling GET_DEFAULT_DUN_DEST...');
1875     GET_DEFAULT_DUN_DEST(p_api_version => 1.0,
1876                          p_init_msg_list => FND_API.G_TRUE,
1877                          p_commit => FND_API.G_FALSE,
1878                          p_level => p_level,
1879                          p_source_id => p_source_id,
1880                          p_send_method => p_send_method,
1881                          X_LOCATION_ID => l_location_id,
1882                          X_CONTACT_ID => l_contact_id,
1883                          X_CONTACT_POINT_ID => l_contact_point_id,
1884                          x_return_status => x_return_status,
1885                          x_msg_count => x_msg_count,
1886                          x_msg_data => x_msg_data);
1887 
1888     WriteLog('---------- continue ' || l_api_name || '----------');
1889     WriteLog(l_api_name || ': LOCATION_ID: ' || l_location_id);
1890     WriteLog(l_api_name || ': CONTACT_ID: ' || l_contact_id);
1891     WriteLog(l_api_name || ': CONTACT_POINT_ID: ' || l_contact_point_id);
1892 
1893     if l_location_index = 0 then
1894          l_bind_count := l_bind_count + 1;
1895          l_fulfillment_bind_tbl(l_bind_count).key_name  := 'LOCATION_ID';
1896          l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1897          l_fulfillment_bind_tbl(l_bind_count).key_value := l_location_id;
1898          l_location_index := l_bind_count;
1899          WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1900     elsif l_location_index > 0 and l_fulfillment_bind_tbl(l_location_index).key_value is null then
1901          l_fulfillment_bind_tbl(l_location_index).key_value := l_location_id;
1902          WriteLog(l_api_name || ': Updated ' || l_fulfillment_bind_tbl(l_location_index).key_name || ' value in bind table');
1903     end if;
1904 
1905     if l_contact_index = 0 then
1906          l_bind_count := l_bind_count + 1;
1907          l_fulfillment_bind_tbl(l_bind_count).key_name  := 'CONTACT_ID';
1908          l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1909          l_fulfillment_bind_tbl(l_bind_count).key_value := l_contact_id;
1910          l_contact_index := l_bind_count;
1911          WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1912     elsif l_contact_index > 0 and l_fulfillment_bind_tbl(l_contact_index).key_value is null then
1913          l_fulfillment_bind_tbl(l_contact_index).key_value := l_contact_id;
1914          WriteLog(l_api_name || ': Updated ' || l_fulfillment_bind_tbl(l_contact_index).key_name || ' value in bind table');
1915     end if;
1916 
1917     if l_contact_point_index = 0 then
1918          l_bind_count := l_bind_count + 1;
1919          l_fulfillment_bind_tbl(l_bind_count).key_name  := 'CONTACT_POINT_ID';
1920          l_fulfillment_bind_tbl(l_bind_count).key_type  := 'NUMBER';
1921          l_fulfillment_bind_tbl(l_bind_count).key_value := l_contact_point_id;
1922          l_contact_point_index := l_bind_count;
1923          WriteLog(l_api_name || ': Added ' || l_fulfillment_bind_tbl(l_bind_count).key_name || ' to bind table');
1924     elsif l_contact_point_index > 0 and l_fulfillment_bind_tbl(l_contact_point_index).key_value is null then
1925          l_fulfillment_bind_tbl(l_contact_point_index).key_value := l_contact_point_id;
1926          WriteLog(l_api_name || ': Updated ' || l_fulfillment_bind_tbl(l_contact_point_index).key_name || ' value in bind table');
1927     end if;
1928 
1929     -- print out output bind table
1930     WriteLog(l_api_name || ': output bind table:');
1931     l_bind_count := l_fulfillment_bind_tbl.count;
1932     for k in 1..l_bind_count loop
1933         WriteLog(l_api_name || ' - #' || k || ' - ' ||
1934           l_fulfillment_bind_tbl(k).key_name || ' = ' || l_fulfillment_bind_tbl(k).key_value);
1935     end loop;
1936 
1937     p_fulfillment_bind_tbl := l_fulfillment_bind_tbl;
1938 
1939     WriteLog(l_api_name || ': x_return_status: ' || x_return_status);
1940     WriteLog(l_api_name || ': x_msg_count: ' || x_msg_count);
1941 
1942 EXCEPTION
1943   WHEN FND_API.G_EXC_ERROR THEN
1944       x_return_status := FND_API.G_RET_STS_ERROR;
1945       FND_MSG_PUB.Count_And_Get
1946       (  p_count          =>   x_msg_count,
1947          p_data           =>   x_msg_data );
1948       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception error='||SQLERRM);
1949 
1950   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1951       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1952       FND_MSG_PUB.Count_And_Get
1953       (  p_count          =>   x_msg_count,
1954          p_data           =>   x_msg_data );
1955       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExc Exception error='||SQLERRM);
1956 
1957   WHEN OTHERS THEN
1958       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1959       FND_MSG_PUB.Count_And_Get
1960       (  p_count          =>   x_msg_count,
1961          p_data           =>   x_msg_data );
1962       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception error='||SQLERRM);
1963 end;
1964 
1965 
1966 
1967 Procedure Validate_Delinquency(
1968     P_Init_Msg_List              IN   VARCHAR2     ,
1969     P_Delinquency_ID             IN   NUMBER,
1970     X_Return_Status              OUT NOCOPY  VARCHAR2,
1971     X_Msg_Count                  OUT NOCOPY  NUMBER,
1972     X_Msg_Data                   OUT NOCOPY  VARCHAR2
1973     )
1974 IS
1975   CURSOR C_GET_Del (IN_del_ID NUMBER) IS
1976     SELECT delinquency_id
1977       FROM iex_delinquencies
1978      WHERE delinquency_ID = IN_del_ID;
1979   --
1980   l_delinquency_id NUMBER;
1981 
1982 BEGIN
1983       -- Initialize message list IF p_init_msg_list is set to TRUE.
1984       IF FND_API.to_Boolean( p_init_msg_list )
1985       THEN
1986           FND_MSG_PUB.initialize;
1987       END IF;
1988 
1989       -- Initialize API return status to SUCCESS
1990       x_return_status := FND_API.G_RET_STS_SUCCESS;
1991 
1992       IF P_delinquency_ID is NULL
1993          or  P_delinquency_ID = FND_API.G_MISS_NUM
1994       THEN
1995                 FND_MESSAGE.Set_Name('IEX', 'API_INVALID_ID');
1996                 FND_MESSAGE.Set_Token('COLUMN', 'delinquency_ID', FALSE);
1997                 FND_MESSAGE.Set_Token('VALUE', p_delinquency_id, FALSE);
1998                 FND_MSG_PUB.Add;
1999                 x_return_status := FND_API.G_RET_STS_ERROR;
2000 
2001       ELSE
2002 
2003           OPEN C_Get_Del (p_delinquency_id);
2004           FETCH C_Get_Del INTO l_delinquency_ID;
2005 
2006           IF (C_Get_Del%NOTFOUND)
2007           THEN
2008             IF FND_MSG_PUB.CHECK_MSG_LEVEL (FND_MSG_PUB.G_MSG_LVL_ERROR)
2009             THEN
2010                 FND_MESSAGE.Set_Name('IEX', 'API_INVALID_ID');
2011                 FND_MESSAGE.Set_Token('COLUMN', 'delinquency_ID', FALSE);
2012                 FND_MESSAGE.Set_Token('VALUE', p_delinquency_id, FALSE);
2013                 FND_MSG_PUB.Add;
2014             END IF;
2015             x_return_status := FND_API.G_RET_STS_ERROR;
2016           END IF;
2017           CLOSE C_GET_Del;
2018       END IF;
2019 
2020       -- Standard call to get message count and IF count is 1, get message info.
2021       FND_MSG_PUB.Count_And_Get
2022       (  p_count          =>   x_msg_count,
2023          p_data           =>   x_msg_data );
2024 
2025 END Validate_Delinquency;
2026 
2027 
2028 
2029 Procedure Create_AG_DN_XREF
2030            (p_api_version             IN NUMBER := 1.0,
2031             p_init_msg_list           IN VARCHAR2 ,
2032             p_commit                  IN VARCHAR2 ,
2033             P_AG_DN_XREF_REC          IN IEX_DUNNING_PUB.AG_DN_XREF_REC_TYPE,
2034             x_return_status           OUT NOCOPY VARCHAR2,
2035             x_msg_count               OUT NOCOPY NUMBER,
2036             x_msg_data                OUT NOCOPY VARCHAR2,
2037             x_AG_DN_XREF_ID           OUT NOCOPY NUMBER)
2038 IS
2039     l_api_name                    CONSTANT VARCHAR2(30) := 'Create_AG_DN';
2040     l_api_version_number          CONSTANT NUMBER   := 1.0;
2041     l_return_status               VARCHAR2(1);
2042     l_msg_count                   NUMBER;
2043     l_msg_data                    VARCHAR2(32767);
2044     l_rowid                       Varchar2(50);
2045     l_AG_DN_XREF_REC              IEX_DUNNING_PUB.AG_DN_XREF_REC_TYPE ;
2046     errmsg                        VARCHAR2(32767);
2047 
2048 
2049 BEGIN
2050       -- Standard Start of API savepoint
2051       SAVEPOINT CREATE_AG_DN_PVT;
2052 
2053       l_ag_dn_xref_rec := p_ag_dn_xref_rec;
2054 
2055       -- Standard call to check for call compatibility.
2056       IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
2057                                            p_api_version,
2058                                            l_api_name,
2059                                            G_PKG_NAME)
2060       THEN
2061           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2062       END IF;
2063 
2064       -- Initialize message list if p_init_msg_list is set to TRUE.
2065       IF FND_API.to_Boolean( p_init_msg_list )
2066       THEN
2067           FND_MSG_PUB.initialize;
2068       END IF;
2069 
2070       WriteLog(l_api_name || G_PKG_NAME || ' ' || l_api_name || ' - Start');
2071 
2072 
2073       -- Initialize API return status to SUCCESS
2074       x_return_status := FND_API.G_RET_STS_SUCCESS;
2075 
2076       --
2077       -- API body
2078       --
2079       WriteLog(l_api_name || G_PKG_NAME || ' ' || l_api_name || ' - CreateAgDn:Calling Validation');
2080 
2081       -- Invoke validation procedures
2082       -- Validate Data
2083       -- Validate Aging_Bucket_line
2084       -- Validate Template
2085       -- Validate Score Range (for the same bucket_line_id, don't overlap)
2086       WriteLog(l_api_name || G_PKG_NAME || ' ' || l_api_name || ' -CreateAgDn:InsertRow');
2087 
2088       -- Create AG_DN_XREF
2089       IEX_AG_DN_PKG.insert_row(
2090           px_rowid                         => l_rowid
2091         , px_AG_DN_XREF_id                 => x_AG_DN_XREF_id
2092         , p_last_update_date               => sysdate
2093         , p_last_updated_by                => FND_GLOBAL.USER_ID
2094         , p_creation_date                  => sysdate
2095         , p_created_by                     => FND_GLOBAL.USER_ID
2096         , p_last_update_login              => FND_GLOBAL.USER_ID
2097         , p_aging_bucket_id                => l_AG_DN_XREF_rec.aging_bucket_id
2098         , p_aging_bucket_line_id           => l_AG_DN_XREF_rec.aging_bucket_line_id
2099         , p_callback_flag                  => l_AG_DN_XREF_rec.callback_flag
2100         , p_callback_days                  => l_AG_DN_XREF_rec.callback_days
2101         , p_fm_method                      => l_AG_DN_XREF_rec.fm_method
2102         , p_dunning_level                  => l_AG_DN_XREF_rec.dunning_level
2103         , p_template_id                    => l_AG_DN_XREF_rec.template_id
2104         , p_xdo_template_id                => l_AG_DN_XREF_rec.xdo_template_id
2105         , p_score_range_low                => l_AG_DN_XREF_rec.score_range_low
2106         , p_score_range_high               => l_AG_DN_XREF_rec.score_range_high
2107         , p_object_version_number          => l_AG_DN_XREF_rec.object_version_number
2108      );
2109 
2110       --
2111       -- End of API body
2112       --
2113 
2114       -- Standard check for p_commit
2115       IF FND_API.to_Boolean( p_commit )
2116       THEN
2117           COMMIT WORK;
2118       END IF;
2119 
2120       WriteLog(l_api_name || G_PKG_NAME || ' ' || l_api_name || ' - End');
2121 
2122 
2123 
2124       FND_MSG_PUB.Count_And_Get
2125       (  p_count          =>   x_msg_count,
2126          p_data           =>   x_msg_data
2127       );
2128 
2129 EXCEPTION
2130           WHEN FND_API.G_EXC_ERROR THEN
2131               x_return_status := FND_API.G_RET_STS_ERROR;
2132               FND_MSG_PUB.Count_And_Get
2133               (  p_count          =>   x_msg_count,
2134                  p_data           =>   x_msg_data );
2135               ROLLBACK TO CREATE_AG_DN_PVT;
2136               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM);
2137 
2138           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2139               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2140               FND_MSG_PUB.Count_And_Get
2141               (  p_count          =>   x_msg_count,
2142                  p_data           =>   x_msg_data );
2143               ROLLBACK TO CREATE_AG_DN_PVT;
2144               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM);
2145 
2146           WHEN OTHERS THEN
2147               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2148               FND_MSG_PUB.Count_And_Get
2149               (  p_count          =>   x_msg_count,
2150                  p_data           =>   x_msg_data );
2151               ROLLBACK TO CREATE_AG_DN_PVT;
2152               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM);
2153 
2154 END CREATE_AG_DN_XREF;
2155 
2156 
2157 Procedure Update_AG_DN_XREF
2158            (p_api_version             IN NUMBER := 1.0,
2159             p_init_msg_list           IN VARCHAR2 ,
2160             p_commit                  IN VARCHAR2 ,
2161             P_AG_DN_XREF_REC          IN IEX_DUNNING_PUB.AG_DN_XREF_REC_TYPE,
2162             p_AG_DN_XREF_ID           IN NUMBER,
2163             x_return_status           OUT NOCOPY VARCHAR2,
2164             x_msg_count               OUT NOCOPY NUMBER,
2165             x_msg_data                OUT NOCOPY VARCHAR2)
2166 
2167 IS
2168     CURSOR C_get_AG_DN_XREF_Rec (IN_AG_DN_XREF_ID NUMBER) is
2169        select  ROWID,
2170                AG_DN_XREF_ID,
2171                AGING_BUCKET_ID,
2172                AGING_BUCKET_LINE_ID,
2173                CALLBACK_FLAG,
2174                CALLBACK_DAYS,
2175                FM_METHOD,
2176                TEMPLATE_ID,
2177                XDO_TEMPLATE_ID,
2178                SCORE_RANGE_LOW,
2179                SCORE_RANGE_HIGH,
2180                DUNNING_LEVEL,
2181                OBJECT_VERSION_NUMBER ,
2182                LAST_UPDATE_DATE,
2183                LAST_UPDATED_BY,
2184                CREATION_DATE,
2185                CREATED_BY ,
2186                LAST_UPDATE_LOGIN
2187          from iex_AG_DN_XREF
2188         where AG_DN_XREF_id = in_AG_DN_XREF_id
2189         FOR UPDATE NOWAIT;
2190     --
2191     l_api_name                    CONSTANT VARCHAR2(30) := 'Update_AG_DN';
2192     l_api_version_number          CONSTANT NUMBER   := 1.0;
2193     l_return_status   VARCHAR2(1);
2194     l_msg_count       NUMBER;
2195     l_msg_data        VARCHAR2(32767);
2196     l_rowid                       Varchar2(50);
2197     l_AG_DN_XREF_REC              IEX_DUNNING_PUB.AG_DN_XREF_REC_TYPE ;
2198     l_AG_DN_XREF_id               NUMBER ;
2199     l_AG_DN_XREF_REF_REC          IEX_DUNNING_PUB.AG_DN_XREF_REC_TYPE;
2200     errmsg                        VARCHAR2(32767);
2201 
2202 
2203 BEGIN
2204 
2205       -- Standard Start of API savepoint
2206       SAVEPOINT UPDATE_AG_DN_PVT;
2207 
2208       l_AG_DN_XREF_REC       := p_AG_DN_XREF_rec;
2209 
2210       -- Standard call to check for call compatibility.
2211       IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
2212                          	             p_api_version,
2213                                            l_api_name,
2214                                            G_PKG_NAME)
2215       THEN
2216           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2217       END IF;
2218 
2219       -- Initialize message list if p_init_msg_list is set to TRUE.
2220       IF FND_API.to_Boolean( p_init_msg_list )
2221       THEN
2222           FND_MSG_PUB.initialize;
2223       END IF;
2224 
2225       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Start');
2226       x_return_status := FND_API.G_RET_STS_SUCCESS;
2227       --
2228       -- Api body
2229       --
2230       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Open C_GET_AGDN_XREF_REC');
2231       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - agdnxrefid='||l_ag_dn_xref_rec.ag_dn_xref_id);
2232 
2233 
2234       Open C_Get_AG_DN_XREF_Rec(l_AG_DN_XREF_rec.AG_DN_XREF_ID);
2235       Fetch C_Get_AG_DN_XREF_Rec into
2236          l_rowid,
2237          l_AG_DN_XREF_ref_rec.AG_DN_XREF_ID,
2238          l_AG_DN_XREF_ref_rec.AGING_BUCKET_ID,
2239          l_AG_DN_XREF_ref_rec.aging_bucket_line_id,
2240          l_AG_DN_XREF_ref_rec.callback_FLAG,
2241          l_AG_DN_XREF_ref_rec.callback_DAYS,
2242          l_AG_DN_XREF_ref_rec.fm_method,
2243          l_AG_DN_XREF_ref_rec.template_id,
2244          l_AG_DN_XREF_ref_rec.xdo_template_id,
2245          l_AG_DN_XREF_ref_rec.score_range_low,
2246          l_AG_DN_XREF_ref_rec.score_range_high,
2247          l_AG_DN_XREF_ref_rec.dunning_level,
2248          l_AG_DN_XREF_ref_rec.object_version_number,
2249          l_AG_DN_XREF_ref_rec.LAST_UPDATE_DATE,
2250          l_AG_DN_XREF_ref_rec.LAST_UPDATED_BY,
2251          l_AG_DN_XREF_ref_rec.CREATION_DATE,
2252          l_AG_DN_XREF_ref_rec.CREATED_BY,
2253          l_AG_DN_XREF_ref_rec.LAST_UPDATE_LOGIN;
2254 
2255       If ( C_Get_AG_DN_XREF_REC%NOTFOUND) Then
2256         IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR)
2257         THEN
2258             FND_MESSAGE.Set_Name('AS', 'API_MISSING_UPDATE_TARGET');
2259             FND_MESSAGE.Set_Token ('INFO', 'IEX_AG_DN_XREF', FALSE);
2260             FND_MSG_PUB.Add;
2261         END IF;
2262         raise FND_API.G_EXC_ERROR;
2263       END IF;
2264 
2265       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Close C_GET_AGDN_XREF_REC');
2266       Close C_Get_AG_DN_XREF_Rec;
2267 
2268       If (l_ag_Dn_xref_rec.last_update_date is NULL or
2269          l_ag_Dn_xref_rec.last_update_date = FND_API.G_MISS_Date )
2270       Then
2271           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR)
2272           THEN
2273               FND_MESSAGE.Set_Name('IEX', 'API_MISSING_ID');
2274               FND_MESSAGE.Set_Token('COLUMN', 'Last_Update_Date', FALSE);
2275               FND_MSG_PUB.ADD;
2276           END IF;
2277           raise FND_API.G_EXC_ERROR;
2278       End if;
2279       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Transfer Data into target rec');
2280 
2281       -- Transfer Data into target record
2282       l_ag_dn_xref_rec.CREATION_DATE := l_ag_dn_xref_ref_rec.CREATION_DATE;
2283       l_ag_dn_xref_rec.CREATED_BY := l_ag_dn_xref_ref_rec.CREATED_BY;
2284 
2285       -- dont update aging_bucket_id and aging_bucket_line_id
2286       -- dont update dunning_level
2287 
2288       IF (l_ag_dn_xref_rec.callback_flag = FND_API.G_MISS_CHAR or
2289           l_ag_dn_xref_rec.callback_flag is null ) Then
2290          l_ag_dn_xref_rec.callback_flag := l_ag_dn_xref_REF_rec.callback_flag;
2291       END IF;
2292       IF (l_ag_dn_xref_rec.callback_days = FND_API.G_MISS_NUM or
2293           l_ag_dn_xref_rec.callback_days is null ) Then
2294          l_ag_dn_xref_rec.callback_days := l_ag_dn_xref_REF_rec.callback_days;
2295       END IF;
2296       IF (l_ag_dn_xref_rec.fm_method = FND_API.G_MISS_CHAR or
2297           l_ag_dn_xref_rec.fm_method is null) Then
2298          l_ag_dn_xref_rec.fm_method := l_ag_dn_xref_REF_rec.fm_method;
2299       END IF;
2300       IF (l_ag_dn_xref_rec.template_ID = FND_API.G_MISS_NUM or
2301           l_ag_dn_xref_rec.template_id is null ) Then
2302          l_ag_dn_xref_rec.template_ID := l_ag_dn_xref_ref_rec.template_ID;
2303       END IF;
2304       IF (l_ag_dn_xref_rec.xdo_template_ID = FND_API.G_MISS_NUM or
2305           l_ag_dn_xref_rec.xdo_template_id is null ) Then
2306          l_ag_dn_xref_rec.xdo_template_ID := l_ag_dn_xref_ref_rec.xdo_template_ID;
2307       END IF;
2308       IF (l_ag_dn_xref_rec.score_range_low = FND_API.G_MISS_NUM or
2309           l_ag_dn_xref_rec.score_range_low is null ) Then
2310          l_ag_dn_xref_rec.score_range_low := l_ag_dn_xref_ref_rec.score_range_low;
2311       END IF;
2312       IF (l_ag_dn_xref_rec.score_range_high = FND_API.G_MISS_NUM or
2313           l_ag_dn_xref_rec.score_range_high is null ) Then
2314          l_ag_dn_xref_rec.score_range_high := l_ag_dn_xref_ref_rec.score_range_high;
2315       END IF;
2316       IF (l_ag_dn_xref_rec.object_version_number = FND_API.G_MISS_NUM or
2317           l_ag_dn_xref_rec.object_version_number is null ) Then
2318          l_ag_dn_xref_rec.object_version_number := l_ag_dn_xref_ref_rec.object_version_number;
2319       END IF;
2320 
2321       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UpdateRow ');
2322 
2323       IEX_AG_DN_PKG.update_row(
2324           p_rowid                          => l_rowid
2325         , p_AG_DN_XREF_id                  => p_AG_DN_XREF_id
2326         , p_last_update_date               => sysdate
2327         , p_last_updated_by                => FND_GLOBAL.USER_ID
2328         , p_creation_date                  => l_AG_DN_XREF_rec.creation_date
2329         , p_created_by                     => l_AG_DN_XREF_rec.created_by
2330         , p_last_update_login              => FND_GLOBAL.USER_ID
2331         , p_aging_bucket_id                => l_AG_DN_XREF_rec.aging_bucket_id
2332         , p_aging_bucket_line_id           => l_AG_DN_XREF_rec.aging_bucket_line_id
2333         , p_callback_flag                  => l_AG_DN_XREF_rec.callback_flag
2334         , p_callback_days                  => l_AG_DN_XREF_rec.callback_days
2335         , p_fm_method                      => l_AG_DN_XREF_rec.fm_method
2336         , p_template_id                    => l_AG_DN_XREF_rec.template_id
2337         , p_xdo_template_id                => l_AG_DN_XREF_rec.xdo_template_id
2338         , p_score_range_low                => l_AG_DN_XREF_rec.score_range_low
2339         , p_score_range_high               => l_AG_DN_XREF_rec.score_range_high
2340         , p_dunning_level                  => l_AG_DN_XREF_rec.dunning_level
2341         , p_object_version_number          => l_ag_dn_xref_rec.object_version_number
2342      );
2343 
2344       --
2345       -- End of API body.
2346       --
2347 
2348       -- Standard check for p_commit
2349       IF FND_API.to_Boolean( p_commit )
2350       THEN
2351           COMMIT WORK;
2352       END IF;
2353 
2354       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UpdateAgDn:End ');
2355 
2356       FND_MSG_PUB.Count_And_Get
2357       (  p_count          =>   x_msg_count,
2358          p_data           =>   x_msg_data );
2359 
2360       EXCEPTION
2361           WHEN FND_API.G_EXC_ERROR THEN
2362               x_return_status := FND_API.G_RET_STS_ERROR;
2363               FND_MSG_PUB.Count_And_Get
2364               (  p_count          =>   x_msg_count,
2365                  p_data           =>   x_msg_data );
2366               ROLLBACK TO UPDATE_AG_DN_PVT;
2367               errmsg := SQLERRM;
2368               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM);
2369 
2370           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2371               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2372               FND_MSG_PUB.Count_And_Get
2373               (  p_count          =>   x_msg_count,
2374                  p_data           =>   x_msg_data );
2375               ROLLBACK TO UPDATE_AG_DN_PVT;
2376               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM);
2377 
2378           WHEN OTHERS THEN
2379               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2380               FND_MSG_PUB.Count_And_Get
2381               (  p_count          =>   x_msg_count,
2382                  p_data           =>   x_msg_data );
2383               ROLLBACK TO UPDATE_AG_DN_PVT;
2384               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM);
2385 
2386 END Update_AG_DN_XREF;
2387 
2388 
2389 Procedure Delete_AG_DN_XREF
2390            (p_api_version             IN NUMBER := 1.0,
2391             p_init_msg_list           IN VARCHAR2 ,
2392             p_commit                  IN VARCHAR2 ,
2393             P_AG_DN_XREF_ID           IN NUMBER,
2394             x_return_status           OUT NOCOPY VARCHAR2,
2395             x_msg_count               OUT NOCOPY NUMBER,
2396             x_msg_data                OUT NOCOPY VARCHAR2)
2397 
2398 IS
2399     CURSOR C_GET_AG_DN_XREF (IN_AG_DN_XREF_ID NUMBER) IS
2400       SELECT AG_DN_XREF_ID
2401         FROM IEX_AG_DN_XREF
2402        WHERE AG_DN_XREF_ID = IN_AG_DN_XREF_ID;
2403     --
2404     l_AG_DN_XREF_id         NUMBER;
2405     l_api_name              CONSTANT VARCHAR2(30) := 'Delete_AG_DN';
2406     l_api_version_number    CONSTANT NUMBER   := 1.0;
2407     l_return_status         VARCHAR2(1);
2408     l_msg_count             NUMBER;
2409     l_msg_data              VARCHAR2(32767);
2410     l_rowid                 Varchar2(50);
2411     errmsg                  VARCHAR2(32767);
2412 
2413 BEGIN
2414       -- Standard Start of API savepoint
2415       SAVEPOINT DELETE_AG_DN_PVT;
2416 
2417       -- Standard call to check for call compatibility.
2418       IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
2419                                            p_api_version,
2420                                            l_api_name,
2421                                            G_PKG_NAME)
2422       THEN
2423           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2424       END IF;
2425 
2426       -- Initialize message list if p_init_msg_list is set to TRUE.
2427       IF FND_API.to_Boolean( p_init_msg_list )
2428       THEN
2429           FND_MSG_PUB.initialize;
2430       END IF;
2431 
2432       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Start');
2433       x_return_status := FND_API.G_RET_STS_SUCCESS;
2434       --
2435       -- Api body
2436       --
2437       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Open Cursor');
2438 
2439       Open C_Get_AG_DN_XREF(p_AG_DN_XREF_id);
2440       Fetch C_Get_AG_DN_XREF into
2441          l_AG_DN_XREF_ID;
2442 
2443       If ( C_Get_AG_DN_XREF%NOTFOUND) Then
2444         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - NotFound');
2445 
2446 
2447         IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR)
2448         THEN
2449             FND_MESSAGE.Set_Name('IEX', 'API_MISSING_UPDATE_TARGET');
2450             FND_MESSAGE.Set_Token ('INFO', 'IEX_AG_DN_XREF', FALSE);
2451             FND_MSG_PUB.Add;
2452         END IF;
2453         raise FND_API.G_EXC_ERROR;
2454       END IF;
2455 
2456       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - agdnxerfid='||l_ag_dn_xref_id);
2457       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Close Cursor');
2458       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Delete Row');
2459       Close C_Get_AG_DN_XREF;
2460 
2461       -- Invoke table handler
2462       IEX_AG_DN_PKG.Delete_Row(
2463              p_AG_DN_XREF_ID  => l_AG_DN_XREF_ID);
2464 
2465       IF x_return_status = FND_API.G_RET_STS_ERROR then
2466                 raise FND_API.G_EXC_ERROR;
2467       elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR then
2468                raise FND_API.G_EXC_UNEXPECTED_ERROR;
2469       END IF;
2470 
2471       --
2472       -- End of API body
2473       --
2474 
2475       -- Standard check for p_commit
2476       IF FND_API.to_Boolean( p_commit )
2477       THEN
2478           COMMIT WORK;
2479       END IF;
2480 
2481       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - End');
2482 
2483       FND_MSG_PUB.Count_And_Get
2484       (  p_count          =>   x_msg_count,
2485          p_data           =>   x_msg_data );
2486 
2487       EXCEPTION
2488           WHEN FND_API.G_EXC_ERROR THEN
2489               x_return_status := FND_API.G_RET_STS_ERROR;
2490               FND_MSG_PUB.Count_And_Get
2491               (  p_count          =>   x_msg_count,
2492                  p_data           =>   x_msg_data );
2493               ROLLBACK TO DELETE_AG_DN_PVT;
2494               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - DeleteAgDn:error='||SQLERRM);
2495 
2496           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2497               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2498               FND_MSG_PUB.Count_And_Get
2499               (  p_count          =>   x_msg_count,
2500                  p_data           =>   x_msg_data );
2501               ROLLBACK TO DELETE_AG_DN_PVT;
2502               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - DeleteAgDn:error='||SQLERRM);
2503 
2504           WHEN OTHERS THEN
2505               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2506               FND_MSG_PUB.Count_And_Get
2507               (  p_count          =>   x_msg_count,
2508                  p_data           =>   x_msg_data );
2509               ROLLBACK TO DELETE_AG_DN_PVT;
2510               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - DeleteAgDn:error='||SQLERRM);
2511 
2512 END Delete_AG_DN_XREF;
2513 
2514 
2515 Procedure Create_Dunning
2516            (p_api_version             IN NUMBER := 1.0,
2517             p_init_msg_list           IN VARCHAR2 ,
2518             p_commit                  IN VARCHAR2 ,
2519             P_Dunning_REC             IN IEX_DUNNING_PUB.DUNNING_REC_TYPE,
2520             x_return_status           OUT NOCOPY VARCHAR2,
2521             x_msg_count               OUT NOCOPY NUMBER,
2522             x_msg_data                OUT NOCOPY VARCHAR2,
2523             x_Dunning_ID              OUT NOCOPY NUMBER)
2524 IS
2525     l_api_name                    CONSTANT VARCHAR2(30) := 'Create_Dunning';
2526     l_api_version_number          CONSTANT NUMBER   := 1.0;
2527     l_return_status               VARCHAR2(1);
2528     l_msg_count                   NUMBER;
2529     l_msg_data                    VARCHAR2(32767);
2530     l_rowid                       Varchar2(50);
2531     l_Dunning_REC                 IEX_DUNNING_PUB.Dunning_REC_TYPE ;
2532     errmsg                        VARCHAR2(32767);
2533 
2534 
2535 BEGIN
2536       -- Standard Start of API savepoint
2537       SAVEPOINT CREATE_DUNNING_PVT;
2538 
2539       l_dunning_rec := p_dunning_rec;
2540 
2541       -- Standard call to check for call compatibility.
2542       IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
2543                                            p_api_version,
2544                                            l_api_name,
2545                                            G_PKG_NAME)
2546       THEN
2547           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2548       END IF;
2549 
2550       -- Initialize message list if p_init_msg_list is set to TRUE.
2551       IF FND_API.to_Boolean( p_init_msg_list )
2552       THEN
2553           FND_MSG_PUB.initialize;
2554       END IF;
2555 
2556       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - START');
2557       x_return_status := FND_API.G_RET_STS_SUCCESS;
2558       --
2559       -- API body
2560       --
2561       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - InsertRow');
2562 
2563       -- Create Dunning
2564       IEX_Dunnings_PKG.insert_row(
2565           px_rowid                         => l_rowid
2566         , px_dunning_id                    => x_dunning_id
2567         , p_last_update_date               => sysdate
2568         , p_last_updated_by                => FND_GLOBAL.USER_ID
2569         , p_creation_date                  => sysdate
2570         , p_created_by                     => FND_GLOBAL.USER_ID
2571         , p_last_update_login              => FND_GLOBAL.USER_ID
2572 				, p_request_id                     => FND_GLOBAL.CONC_REQUEST_ID
2573         , p_template_id                    => l_dunning_rec.template_id
2574         , p_callback_yn                    => l_dunning_rec.callback_yn
2575         , p_callback_date                  => l_dunning_rec.callback_date
2576         , p_campaign_sched_id              => l_dunning_rec.campaign_sched_id
2577         , p_status                         => l_dunning_rec.status
2578         , p_delinquency_id                 => l_dunning_rec.delinquency_id
2579         , p_ffm_request_id                 => l_dunning_rec.ffm_request_id
2580         , p_xml_request_id                 => l_dunning_rec.xml_request_id
2581         , p_xml_template_id                => l_dunning_rec.xml_template_id
2582         , p_object_id                      => l_dunning_rec.object_id
2583         , p_object_type                    => l_dunning_rec.object_type
2584         , p_dunning_object_id              => l_dunning_rec.dunning_object_id
2585         , p_dunning_level                  => l_dunning_rec.dunning_level
2586         , p_dunning_method                 => l_dunning_rec.dunning_method
2587         , p_amount_due_remaining           => l_dunning_rec.amount_due_remaining
2588         , p_currency_code                  => l_dunning_rec.currency_code
2589         , p_delivery_status                => l_dunning_rec.delivery_status
2590         , p_parent_dunning_id              => l_dunning_rec.PARENT_DUNNING_ID
2591         , p_dunning_plan_id                => l_dunning_rec.dunning_plan_id
2592         , p_contact_destination            => l_dunning_rec.contact_destination
2593         , p_contact_party_id               => l_dunning_rec.contact_party_id
2594       );
2595 
2596 
2597       IF x_return_status = FND_API.G_RET_STS_ERROR then
2598                 raise FND_API.G_EXC_ERROR;
2599       elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR then
2600                raise FND_API.G_EXC_UNEXPECTED_ERROR;
2601       END IF;
2602 
2603       --
2604       -- End of API body
2605       --
2606 
2607       -- Standard check for p_commit
2608       IF FND_API.to_Boolean( p_commit )
2609       THEN
2610           COMMIT WORK;
2611       END IF;
2612 
2613       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - END');
2614       FND_MSG_PUB.Count_And_Get
2615       (  p_count          =>   x_msg_count,
2616          p_data           =>   x_msg_data
2617       );
2618 
2619 EXCEPTION
2620           WHEN FND_API.G_EXC_ERROR THEN
2621               x_return_status := FND_API.G_RET_STS_ERROR;
2622               FND_MSG_PUB.Count_And_Get
2623               (  p_count          =>   x_msg_count,
2624                  p_data           =>   x_msg_data );
2625               ROLLBACK TO CREATE_DUNNING_PVT;
2626               WriteLog(G_PKG_NAME || ' ' || l_api_name || 'exception ' || SQLERRM);
2627 
2628           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2629               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2630               FND_MSG_PUB.Count_And_Get
2631               (  p_count          =>   x_msg_count,
2632                  p_data           =>   x_msg_data );
2633               ROLLBACK TO CREATE_DUNNING_PVT;
2634               WriteLog(G_PKG_NAME || ' ' || l_api_name || 'error ' || SQLERRM);
2635 
2636           WHEN OTHERS THEN
2637               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2638               FND_MSG_PUB.Count_And_Get
2639               (  p_count          =>   x_msg_count,
2640                  p_data           =>   x_msg_data );
2641               ROLLBACK TO CREATE_DUNNING_PVT;
2642               WriteLog(G_PKG_NAME || ' ' || l_api_name || 'error ' || SQLERRM);
2643 
2644 END CREATE_DUNNING;
2645 
2646 
2647 
2648 Procedure Update_Dunning
2649            (p_api_version             IN NUMBER := 1.0,
2650             p_init_msg_list           IN VARCHAR2 ,
2651             p_commit                  IN VARCHAR2 ,
2652             P_Dunning_REC             IN IEX_DUNNING_PUB.DUNNING_REC_TYPE,
2653             x_return_status           OUT NOCOPY VARCHAR2,
2654             x_msg_count               OUT NOCOPY NUMBER,
2655             x_msg_data                OUT NOCOPY VARCHAR2)
2656 
2657 IS
2658     CURSOR C_get_DUNNING_Rec (IN_DUNNING_ID NUMBER) is
2659        select  ROWID,
2660                DUNNING_ID,
2661                TEMPLATE_ID,
2662                CALLBACK_YN,
2663                CALLBACK_DATE,
2664                CAMPAIGN_SCHED_ID,
2665                STATUS,
2666                DELINQUENCY_ID,
2667                FFM_REQUEST_ID,
2668                XML_REQUEST_ID,
2669                XML_TEMPLATE_ID,
2670                OBJECT_ID,
2671                OBJECT_TYPE,
2672                DUNNING_OBJECT_ID,
2673                DUNNING_LEVEL,
2674                DUNNING_METHOD,
2675                AMOUNT_DUE_REMAINING,
2676                CURRENCY_CODE,
2677                LAST_UPDATE_DATE,
2678                LAST_UPDATED_BY,
2679                CREATION_DATE,
2680                CREATED_BY ,
2681                LAST_UPDATE_LOGIN,
2682                delivery_status,
2683                PARENT_DUNNING_ID,
2684                financial_charge,   -- bug 3955222
2685                letter_name,   -- bug 3955222
2686                interest_amt,   -- bug 3955222
2687                dunning_plan_id,   -- bug 3955222
2688                contact_destination,   -- bug 3955222
2689                contact_party_id   -- bug 3955222
2690          from iex_DUNNINGS
2691         where dunning_id = in_dunning_id
2692         FOR UPDATE NOWAIT;
2693     --
2694     l_api_name                    CONSTANT VARCHAR2(30) := 'Update_DUNNING';
2695     l_api_version_number          CONSTANT NUMBER   := 1.0;
2696     l_return_status               VARCHAR2(1);
2697     l_msg_count                   NUMBER;
2698     l_msg_data                    VARCHAR2(32767);
2699     l_rowid                       Varchar2(50);
2700     l_DUNNING_REC                 IEX_DUNNING_PUB.DUNNING_REC_TYPE ;
2701     l_dunning_id                  NUMBER ;
2702     l_DUNNING_REF_REC             IEX_DUNNING_PUB.DUNNING_REC_TYPE;
2703     errmsg                        VARCHAR2(32767);
2704 
2705 
2706 BEGIN
2707 
2708       -- Standard Start of API savepoint
2709       SAVEPOINT UPDATE_dunning_PVT;
2710 
2711       l_dunning_rec := p_dunning_rec;
2712 
2713       -- Standard call to check for call compatibility.
2714       IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
2715                          	             p_api_version,
2716                                            l_api_name,
2717                                            G_PKG_NAME)
2718       THEN
2719           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2720       END IF;
2721 
2722       -- Initialize message list if p_init_msg_list is set to TRUE.
2723       IF FND_API.to_Boolean( p_init_msg_list )
2724       THEN
2725           FND_MSG_PUB.initialize;
2726       END IF;
2727 
2728       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - START');
2729       x_return_status := FND_API.G_RET_STS_SUCCESS;
2730       --
2731       -- Api body
2732       --
2733       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Open C_GET_DUNNING_REC');
2734       WriteLog('l_dunning_rec.dunning_ID: '||l_dunning_rec.dunning_ID);
2735 
2736 
2737       Open C_Get_DUNNING_Rec(l_dunning_rec.dunning_ID);
2738       Fetch C_Get_DUNNING_Rec into
2739          l_rowid,
2740          l_DUNNING_REF_REC.DUNNING_ID,
2741          l_DUNNING_REF_REC.template_id,
2742          l_DUNNING_REF_REC.callback_YN,
2743          l_DUNNING_REF_REC.callback_Date,
2744          l_DUNNING_REF_REC.campaign_sched_id,
2745          l_DUNNING_REF_REC.status,
2746          l_DUNNING_REF_REC.delinquency_id,
2747          l_DUNNING_REF_REC.ffm_request_id,
2748          l_DUNNING_REF_REC.xml_request_id,
2749          l_DUNNING_REF_REC.xml_template_id,
2750          l_DUNNING_REF_REC.object_id,
2751          l_DUNNING_REF_REC.object_type,
2752          l_DUNNING_REF_REC.dunning_object_id,
2753          l_DUNNING_REF_REC.dunning_level,
2754          l_DUNNING_REF_REC.dunning_method,
2755          l_DUNNING_REF_REC.amount_due_remaining,
2756          l_DUNNING_REF_REC.currency_code,
2757          l_DUNNING_REF_REC.LAST_UPDATE_DATE,
2758          l_DUNNING_REF_REC.LAST_UPDATED_BY,
2759          l_DUNNING_REF_REC.CREATION_DATE,
2760          l_DUNNING_REF_REC.CREATED_BY,
2761          l_DUNNING_REF_REC.LAST_UPDATE_LOGIN,
2762          l_DUNNING_REF_REC.delivery_status,
2763          l_DUNNING_REF_REC.parent_dunning_id,
2764          l_DUNNING_REF_REC.financial_charge,  -- bug 3955222
2765          l_DUNNING_REF_REC.letter_name,  -- bug 3955222
2766          l_DUNNING_REF_REC.interest_amt,  -- bug 3955222
2767          l_DUNNING_REF_REC.dunning_plan_id,  -- bug 3955222
2768          l_DUNNING_REF_REC.contact_destination,  -- bug 3955222
2769          l_DUNNING_REF_REC.contact_party_id;  -- bug 3955222
2770 
2771       If ( C_GET_DUNNING_REC%NOTFOUND) Then
2772         IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR)
2773         THEN
2774             FND_MESSAGE.Set_Name('IEX', 'API_MISSING_UPDATE_TARGET');
2775             FND_MESSAGE.Set_Token ('INFO', 'iex_DUNNINGs', FALSE);
2776             FND_MSG_PUB.Add;
2777         END IF;
2778         raise FND_API.G_EXC_ERROR;
2779       END IF;
2780 
2781       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CLOSE C_GET_DUNNING_REC');
2782       Close C_Get_DUNNING_REC;
2783 -- Start bug 5924158 05/06/07 by gnramasa
2784    /*
2785       If (l_dunning_rec.last_update_date is NULL or
2786          l_dunning_rec.last_update_date = FND_API.G_MISS_Date )
2787       Then
2788           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR)
2789           THEN
2790               FND_MESSAGE.Set_Name('IEX', 'API_MISSING_ID');
2791               FND_MESSAGE.Set_Token('COLUMN', 'Last_Update_Date', FALSE);
2792               FND_MSG_PUB.ADD;
2793           END IF;
2794           raise FND_API.G_EXC_ERROR;
2795       End if;
2796 */
2797 -- End bug 5924158 05/06/07 by gnramasa
2798       -- Transfer Data into target record
2799       l_dunning_rec.CREATION_DATE       := l_dunning_ref_rec.CREATION_DATE;
2800       l_dunning_rec.CREATED_BY          := l_dunning_ref_rec.CREATED_BY;
2801       l_dunning_rec.delinquency_id      := l_dunning_ref_rec.delinquency_id;
2802       --l_dunning_rec.ffm_request_id := l_dunning_ref_rec.ffm_request_id;
2803       l_dunning_rec.dunning_method      := l_dunning_ref_rec.dunning_method;
2804       l_dunning_rec.amount_due_remaining := l_dunning_ref_rec.amount_due_remaining;
2805       l_dunning_rec.currency_code        := l_dunning_ref_rec.currency_code;
2806       l_dunning_rec.dunning_object_id 	 := l_dunning_ref_rec.dunning_object_id;
2807       l_dunning_rec.dunning_level 			 := l_dunning_ref_rec.dunning_level;
2808       l_dunning_rec.financial_charge 		 := l_dunning_ref_rec.financial_charge;  -- bug 3955222
2809       l_dunning_rec.letter_name 				 := l_dunning_ref_rec.letter_name;  -- bug 3955222
2810       l_dunning_rec.interest_amt 				 := l_dunning_ref_rec.interest_amt;  -- bug 3955222
2811       l_dunning_rec.dunning_plan_id 		 := l_dunning_ref_rec.dunning_plan_id;  -- bug 3955222
2812       --l_dunning_rec.delivery_status   	 := l_dunning_ref_rec.delivery_status;
2813       --l_dunning_rec.parent_dunning_id    := l_dunning_ref_rec.parent_dunning_id;
2814 
2815       -- dont update dunning_id and delinquency_id
2816       -- dont update dunning_method
2817       -- dont update dunning_object_id and dunning_level
2818 
2819       IF (l_dunning_rec.xml_request_id = FND_API.G_MISS_NUM OR
2820           l_dunning_rec.xml_request_id is null) Then
2821          l_dunning_rec.xml_request_id := l_dunning_ref_rec.xml_request_id;
2822       END IF;
2823       IF (l_dunning_rec.xml_template_id = FND_API.G_MISS_NUM OR
2824           l_dunning_rec.xml_template_id is null) Then
2825          l_dunning_rec.xml_template_id := l_dunning_ref_rec.xml_template_id;
2826       END IF;
2827       IF (l_dunning_rec.ffm_request_id = FND_API.G_MISS_NUM OR
2828           l_dunning_rec.ffm_request_id is null) Then
2829          l_dunning_rec.ffm_request_id := l_dunning_ref_rec.ffm_request_id;
2830       END IF;
2831       IF (l_dunning_rec.callback_YN = FND_API.G_MISS_CHAR or
2832           l_dunning_rec.callback_YN is null ) Then
2833          l_dunning_rec.callback_YN := l_dunning_ref_rec.callback_YN;
2834       END IF;
2835       IF (l_dunning_rec.callback_date = FND_API.G_MISS_DATE or
2836           l_dunning_rec.callback_date is null ) Then
2837          l_dunning_rec.callback_date := l_dunning_ref_rec.callback_date;
2838       END IF;
2839       IF (l_dunning_rec.status = FND_API.G_MISS_CHAR or
2840           l_dunning_rec.status is null ) Then
2841          l_dunning_rec.status := l_dunning_ref_rec.status;
2842       END IF;
2843       IF (l_dunning_rec.template_ID = FND_API.G_MISS_NUM or
2844           l_dunning_rec.template_id is null ) Then
2845          l_dunning_rec.template_ID := l_dunning_ref_rec.template_ID;
2846       END IF;
2847       IF (l_dunning_rec.campaign_sched_id = FND_API.G_MISS_NUM or
2848           l_dunning_rec.campaign_sched_id is null ) Then
2849          l_dunning_rec.campaign_sched_id := l_dunning_ref_rec.campaign_sched_id;
2850       END IF;
2851        IF (l_dunning_rec.object_ID = FND_API.G_MISS_NUM or
2852           l_dunning_rec.object_id is null ) Then
2853          l_dunning_rec.object_ID := l_dunning_ref_rec.object_ID;
2854       END IF;
2855       IF (l_dunning_rec.object_type = FND_API.G_MISS_CHAR or
2856           l_dunning_rec.object_type is null ) Then
2857          l_dunning_rec.object_type := l_dunning_ref_rec.object_type;
2858       END IF;
2859       IF (l_dunning_rec.delivery_status = FND_API.G_MISS_CHAR or
2860           l_dunning_rec.delivery_status is null ) Then
2861          l_dunning_rec.delivery_status := l_dunning_ref_rec.delivery_status;
2862       END IF;
2863       IF (l_dunning_rec.PARENT_DUNNING_ID = FND_API.G_MISS_NUM or
2864           l_dunning_rec.PARENT_DUNNING_ID is null ) Then
2865          l_dunning_rec.PARENT_DUNNING_ID := l_dunning_ref_rec.PARENT_DUNNING_ID;
2866       END IF;
2867 
2868       -- begin bug 3955222 ctlee 10/05/2005
2869       IF (l_dunning_rec.contact_destination = FND_API.G_MISS_CHAR or
2870           l_dunning_rec.contact_destination is null ) Then
2871          l_dunning_rec.contact_destination := l_dunning_ref_rec.contact_destination;
2872       END IF;
2873 
2874       IF (l_dunning_rec.contact_party_id = FND_API.G_MISS_NUM or
2875           l_dunning_rec.contact_party_id is null ) Then
2876          l_dunning_rec.contact_party_id := l_dunning_ref_rec.contact_party_id;
2877       END IF;
2878       -- end bug 3955222 ctlee 10/05/2005
2879       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Update Row');
2880       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - dunningid='||l_dunning_rec.dunning_id );
2881       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - delivery_status= ' || l_dunning_rec.delivery_status);
2882 
2883           -- Invoke table handler
2884           IEX_DUNNINGS_PKG.Update_Row(
2885              p_rowid                          => l_rowid
2886            , p_dunning_id                     => l_dunning_rec.dunning_id
2887            , p_last_update_date               => sysdate
2888            , p_last_updated_by                => FND_GLOBAL.USER_ID
2889            , p_creation_date                  => l_dunning_rec.creation_date
2890            , p_created_by                     => l_dunning_rec.created_by
2891            , p_last_update_login              => FND_GLOBAL.USER_ID
2892 					 , p_request_id                     => FND_GLOBAL.CONC_REQUEST_ID
2893            , p_template_id                    => l_dunning_rec.template_id
2894            , p_callback_yn                    => l_dunning_rec.callback_yn
2895            , p_callback_date                  => l_dunning_rec.callback_date
2896            , p_campaign_sched_id              => l_dunning_rec.campaign_sched_id
2897            , p_status                         => l_dunning_rec.status
2898            , p_delinquency_id                 => l_dunning_rec.delinquency_id
2899            , p_ffm_request_id                 => l_dunning_rec.ffm_request_id
2900            , p_xml_request_id                 => l_dunning_rec.xml_request_id
2901            , p_xml_template_id                => l_dunning_rec.xml_template_id
2902            , p_object_id                      => l_dunning_rec.object_id
2903            , p_object_type                    => l_dunning_rec.object_type
2904            , p_dunning_object_id              => l_dunning_rec.dunning_object_id
2905            , p_dunning_level                  => l_dunning_rec.dunning_level
2906            , p_dunning_method                 => l_dunning_rec.dunning_method
2907            , p_amount_due_remaining           => l_dunning_rec.amount_due_remaining
2908            , p_currency_code                  => l_dunning_rec.currency_code
2909            , p_delivery_status                => l_dunning_rec.delivery_status
2910            , p_parent_dunning_id              => l_dunning_rec.PARENT_DUNNING_ID
2911            , p_financial_charge               => l_dunning_rec.financial_charge   -- bug 3955222
2912            , p_letter_name                    => l_dunning_rec.letter_name   -- bug 3955222
2913            , p_interest_amt                   => l_dunning_rec.interest_amt   -- bug 3955222
2914            , p_dunning_plan_id                => l_dunning_rec.dunning_plan_id   -- bug 3955222
2915            , p_contact_destination            => l_dunning_rec.contact_destination   -- bug 3955222
2916            , p_contact_party_id               => l_dunning_rec.contact_party_id   -- bug 3955222
2917 
2918           );
2919 
2920       IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2921           RAISE FND_API.G_EXC_ERROR;
2922       elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR then
2923           raise FND_API.G_EXC_UNEXPECTED_ERROR;
2924       END IF;
2925 
2926       --
2927       -- End of API body.
2928       --
2929 
2930       -- Standard check for p_commit
2931       IF FND_API.to_Boolean( p_commit )
2932       THEN
2933           COMMIT WORK;
2934       END IF;
2935 
2936       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - END');
2937 
2938       FND_MSG_PUB.Count_And_Get
2939       (  p_count          =>   x_msg_count,
2940          p_data           =>   x_msg_data );
2941 
2942       EXCEPTION
2943           WHEN FND_API.G_EXC_ERROR THEN
2944               x_return_status := FND_API.G_RET_STS_ERROR;
2945               FND_MSG_PUB.Count_And_Get
2946               (  p_count          =>   x_msg_count,
2947                  p_data           =>   x_msg_data );
2948               ROLLBACK TO UPDATE_DUNNING_PVT;
2949               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception error='||SQLERRM);
2950 
2951           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2952               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2953               FND_MSG_PUB.Count_And_Get
2954               (  p_count          =>   x_msg_count,
2955                  p_data           =>   x_msg_data );
2956               ROLLBACK TO UPDATE_DUNNING_PVT;
2957               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExc Exception error='||SQLERRM);
2958 
2959           WHEN OTHERS THEN
2960               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2961               FND_MSG_PUB.Count_And_Get
2962               (  p_count          =>   x_msg_count,
2963                  p_data           =>   x_msg_data );
2964               ROLLBACK TO UPDATE_DUNNING_PVT;
2965               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception error='||SQLERRM);
2966 
2967 END Update_DUNNING;
2968 
2969 
2970 
2971 
2972 /*=========================================================================
2973    clchang update 10/16/2002 -
2974     Send Dunning can be in Customer, Account and Delinquency levels in 11.5.9;
2975     Send_Level_Dunning is for Customer and Account level;
2976     Send_Dunning keeps the same, and is for Delinquency Level;
2977    clchang update 04/21/2003 -
2978     new level 'BILL_TO' in 11.5.10.
2979 *=========================================================================*/
2980 Procedure Send_Level_Dunning
2981            (p_api_version             IN NUMBER := 1.0,
2982             p_init_msg_list           IN VARCHAR2 ,
2983             p_commit                  IN VARCHAR2 ,
2984             p_running_level           IN VARCHAR2,
2985             p_dunning_plan_id         in number,
2986             p_resend_flag             IN VARCHAR2,
2987             p_delinquencies_tbl       IN IEX_DELINQUENCY_PUB.DELINQUENCY_TBL_TYPE,
2988             p_previous_request_id     IN NUMBER,
2989             x_return_status           OUT NOCOPY VARCHAR2,
2990             x_msg_count               OUT NOCOPY NUMBER,
2991             x_msg_data                OUT NOCOPY VARCHAR2)
2992 
2993 IS
2994     CURSOR C_GET_DEL (IN_del_ID NUMBER) IS
2995       SELECT delinquency_ID
2996         FROM IEX_DELINQUENCIES
2997        WHERE delinquency_ID = in_del_ID;
2998     --
2999     -- begin bug 4914799 ctlee 12/30/2005 add p_dunning_plan_id
3000     CURSOR C_GET_SCORE (IN_ID NUMBER, IN_CODE VARCHAR2, p_dunning_plan_id number) IS
3001       SELECT a.score_value
3002         FROM IEX_SCORE_HISTORIES a
3003              , IEX_DUNNING_PLANS_VL c  -- bug 4914799 ctlee 12/30/2005
3004        WHERE a.score_object_ID = in_ID
3005          AND a.score_object_code = IN_CODE
3006          and c.score_id = a.score_id   -- bug 4914799 ctlee 12/30/2005
3007          and c.dunning_plan_id = p_dunning_plan_id -- bug 4914799 ctlee 12/30/2005
3008          AND a.creation_date = (select max(b.creation_date)
3009                                   from iex_score_histories b
3010                                  where b.score_object_id = in_id
3011                                    AND b.score_object_code = IN_CODE
3012 				   AND b.score_id = a.score_id );
3013     -- end bug 4914799 ctlee 12/30/2005 add p_dunning_plan_id
3014 
3015     CURSOR C_GET_TEMPLATE (l_line_id NUMBER,
3016                            l_score NUMBER, in_LEVEL VARCHAR2, p_dunning_plan_id number) IS
3017       SELECT x.template_id,
3018              x.xdo_template_id,
3019              x.fm_method,
3020              upper(x.callback_flag),
3021              x.callback_days
3022         FROM IEX_AG_DN_XREF x,
3023              ar_aging_buckets ar,
3024              iex_dunning_plans_vl d
3025        WHERE x.aging_bucket_line_ID = l_line_ID
3026          and x.dunning_plan_id = p_dunning_plan_id
3027          AND l_score between x.score_range_low and x.score_range_high
3028          AND x.aging_bucket_id = ar.aging_bucket_id
3029          and ar.aging_bucket_id = d.aging_bucket_id
3030          AND ar.status = 'A'
3031          AND x.dunning_level = IN_LEVEL ;
3032 
3033     --Start bug 7197038 gnramasa 8th july 08
3034      -- cursor for checking if the number of delinquencies NOT disputed
3035     /*
3036     CURSOR C_DISPUTED_AMOUNT(P_PARTY_ID NUMBER, P_CUST_ACCOUNT_ID NUMBER, P_SITE_USE_ID NUMBER ) IS
3037      select sum(a.amount_in_dispute) - sum(a.amount_due_remaining)
3038       from iex_delinquencies d
3039           ,ar_payment_schedules a
3040      where a.payment_schedule_id  = d.payment_schedule_id
3041        and d.party_cust_id        = nvl(p_party_id, d.party_cust_id)
3042        and d.cust_account_id      = nvl(P_CUST_ACCOUNT_ID, d.cust_account_id )
3043        and d.customer_site_use_id = nvl(p_site_use_id, d.customer_site_use_id )
3044        and d.status IN ('DELINQUENT', 'PREDELINQUENT');
3045      */
3046      CURSOR C_DISPUTED_AMOUNT_PARTY(P_PARTY_ID NUMBER) IS
3047      select sum(a.amount_in_dispute) - sum(a.amount_due_remaining)
3048       from iex_delinquencies d
3049           ,ar_payment_schedules a
3050      where a.payment_schedule_id  = d.payment_schedule_id
3051        and d.party_cust_id        = p_party_id
3052        and d.status IN ('DELINQUENT', 'PREDELINQUENT');
3053 
3054     CURSOR C_DISPUTED_AMOUNT_ACCOUNT(P_CUST_ACCOUNT_ID NUMBER) IS
3055      select sum(a.amount_in_dispute) - sum(a.amount_due_remaining)
3056       from iex_delinquencies d
3057           ,ar_payment_schedules a
3058      where a.payment_schedule_id  = d.payment_schedule_id
3059        and d.cust_account_id      = P_CUST_ACCOUNT_ID
3060        and d.status IN ('DELINQUENT', 'PREDELINQUENT');
3061 
3062     CURSOR C_DISPUTED_AMOUNT_BILLTO(P_SITE_USE_ID NUMBER ) IS
3063      select sum(a.amount_in_dispute) - sum(a.amount_due_remaining)
3064       from iex_delinquencies d
3065           ,ar_payment_schedules a
3066      where a.payment_schedule_id  = d.payment_schedule_id
3067        and d.customer_site_use_id = p_site_use_id
3068        and d.status IN ('DELINQUENT', 'PREDELINQUENT');
3069 
3070     --End bug 7197038 gnramasa 8th july 08
3071 
3072     l_DUNNING_id            NUMBER;
3073     l_delinquency_id        NUMBER;
3074     l_party_cust_id         NUMBER;
3075     l_account_id            NUMBER;
3076     l_customer_site_use_id  NUMBER;
3077     l_noskip                NUMBER := 0;
3078     l_DUNNING_rec           IEX_DUNNING_PUB.DUNNING_REC_TYPE;
3079     l_DUNNING_tbl           IEX_DUNNING_PUB.DUNNING_TBL_TYPE;
3080     l_dunning_rec_upd       IEX_DUNNING_PUB.DUNNING_REC_TYPE;
3081     l_score                 NUMBER;
3082     l_bucket_line_id        NUMBER;
3083     l_campaign_sched_id     NUMBER;
3084     l_template_id           NUMBER;
3085     l_xdo_template_id       NUMBER;
3086     l_method                VARCHAR2(10);
3087     l_callback_flag         VARCHAR2(1);
3088     l_callback_days         NUMBER;
3089     l_callback_date         DATE;
3090     l_request_id            NUMBER;
3091     l_outcome_code          varchar2(20);
3092     l_api_name              CONSTANT VARCHAR2(30) := 'Send_Level_Dunning';
3093     l_api_version_number    CONSTANT NUMBER   := 1.0;
3094     l_return_status         VARCHAR2(1);
3095     l_msg_count             NUMBER;
3096     l_msg_data              VARCHAR2(32767);
3097     errmsg                  VARCHAR2(32767);
3098     --
3099     nIdx                    NUMBER := 0;
3100     TYPE Del_ID_TBL_type is Table of IEX_DELINQUENCIES_ALL.DELINQUENCY_ID%TYPE
3101                             INDEX BY BINARY_INTEGER;
3102     Del_Tbl                 Del_ID_TBL_TYPE;
3103     l_bind_tbl              IEX_DUNNING_PVT.FULFILLMENT_BIND_TBL;
3104     l_bind_rec              IEX_DUNNING_PVT.FULFILLMENT_BIND_REC;
3105     l_org_id                NUMBER ;
3106     l_object_Code           VARCHAR2(25);
3107     l_object_id             NUMBER;
3108     --l_delid_tbl             IEX_DUNNING_PUB.DelId_NumList;
3109     l_del_tbl               IEX_DELINQUENCY_PUB.DELINQUENCY_TBL_TYPE;
3110     l_curr_code             VARCHAR2(15);
3111     l_amount                NUMBER;
3112     l_write                 NUMBER;
3113     l_ffm_cnt               NUMBER := 0;
3114     l_dunn_cnt              NUMBER := 0;
3115     l_curr_dmethod          VARCHAR2(10);
3116     -- begin raverma 03/09/06 add contact point selection
3117     l_location_id           number;
3118     l_amount_disputed       number;
3119     l_contact_id            number;
3120     l_warning_flag          varchar2(1);
3121     l_contact_point_id      number;
3122 
3123     l_delinquency_id_check        NUMBER;
3124     l_party_cust_id_check         NUMBER;
3125     l_account_id_check            NUMBER;
3126     l_customer_site_use_id_check  NUMBER;
3127     l_contact_destination         varchar2(240);  -- bug 3955222
3128     l_contact_party_id            number; -- bug 3955222
3129 
3130 BEGIN
3131       -- Standard Start of API savepoint
3132       SAVEPOINT Send_Level_DUNNING_PVT;
3133 
3134       --Bug#4679639 schekuri 20-OCT-2005
3135       --Value of profile ORG_ID shouldn't be used for getting ORG_ID after MOAC implementation
3136       --l_org_id := fnd_profile.value('ORG_ID');
3137       l_org_id:= mo_global.get_current_org_id;
3138 
3139 
3140       -- Standard call to check for call compatibility.
3141       IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
3142                                            p_api_version,
3143                                            l_api_name,
3144                                            G_PKG_NAME)
3145       THEN
3146           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3147       END IF;
3148 
3149       -- Initialize message list if p_init_msg_list is set to TRUE.
3150       IF FND_API.to_Boolean( p_init_msg_list )
3151       THEN
3152           FND_MSG_PUB.initialize;
3153       END IF;
3154 
3155       if (p_resend_flag = 'Y') then
3156           -- don't write into FILE
3157           l_write := 0;
3158       else
3159           l_write := 1;
3160       end if;
3161 
3162       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Start');
3163       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - running_level = ' || p_running_level);
3164       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - resend_flag =   ' || p_resend_flag);
3165       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - p_previous_request_id ' || p_previous_request_id);
3166       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - delcnt= ' || p_delinquencies_tbl.count);
3167 
3168       -- Initialize API return status to SUCCESS
3169       x_return_status := FND_API.G_RET_STS_SUCCESS;
3170       --
3171       -- Api body
3172       --
3173 
3174       l_party_cust_id := p_delinquencies_tbl(1).party_cust_id;
3175       l_account_id := p_delinquencies_tbl(1).cust_account_id;
3176       l_customer_site_use_id := p_delinquencies_tbl(1).customer_site_use_id;
3177 
3178       if (p_running_level = 'CUSTOMER') then
3179           l_object_Code                     := 'PARTY';
3180           l_object_id                       := p_delinquencies_tbl(1).party_cust_id;
3181           l_del_tbl(1).party_cust_id        := p_delinquencies_tbl(1).party_cust_id;
3182           l_del_tbl(1).cust_account_id      := 0;
3183           l_del_tbl(1).customer_site_use_id := 0;
3184           l_amount                          := party_amount_due_remaining(l_object_id);
3185           l_curr_code                       := party_currency_code(l_object_id);
3186           --Start bug 7197038 gnramasa 8th july 08
3187 	  /*
3188 	  open C_DISPUTED_AMOUNT(P_PARTY_ID          => p_delinquencies_tbl(1).party_cust_id
3189                                 ,P_CUST_ACCOUNT_ID   => null
3190                                 ,P_SITE_USE_ID       => null);
3191 	  */
3192 	  open C_DISPUTED_AMOUNT_PARTY(p_delinquencies_tbl(1).party_cust_id);
3193 
3194       elsif (p_running_level = 'ACCOUNT') then
3195           l_object_Code                     := 'IEX_ACCOUNT';
3196           l_object_id                       := p_delinquencies_tbl(1).cust_account_id;
3197           l_del_tbl(1).party_cust_id        := p_delinquencies_tbl(1).party_cust_id;
3198           l_del_tbl(1).cust_account_id      := p_delinquencies_tbl(1).cust_account_id;
3199           l_del_tbl(1).customer_site_use_id := 0;
3200           l_amount                          := acct_amount_due_remaining(l_object_id);
3201           l_curr_code                       := acct_currency_code(l_object_id);
3202           /*
3203 	  open C_DISPUTED_AMOUNT(P_PARTY_ID          => null
3204                                 ,P_CUST_ACCOUNT_ID   => p_delinquencies_tbl(1).party_cust_id
3205                                 ,P_SITE_USE_ID       => null);
3206 	  */
3207 	  open C_DISPUTED_AMOUNT_ACCOUNT(p_delinquencies_tbl(1).cust_account_id);
3208 
3209       elsif (p_running_level = 'BILL_TO') then
3210           l_object_Code                     := 'IEX_BILLTO';
3211           l_object_id                       := p_delinquencies_tbl(1).customer_site_use_id;
3212           l_del_tbl(1).party_cust_id        := p_delinquencies_tbl(1).party_cust_id;
3213           l_del_tbl(1).cust_account_id      := p_delinquencies_tbl(1).cust_account_id;
3214           l_del_tbl(1).customer_site_use_id := p_delinquencies_tbl(1).customer_site_use_id;
3215           l_amount                          := site_amount_due_remaining(l_object_id);
3216           l_curr_code                       := site_currency_code(l_object_id);
3217           /*
3218 	  open C_DISPUTED_AMOUNT(P_PARTY_ID          => null
3219                                 ,P_CUST_ACCOUNT_ID   => p_delinquencies_tbl(1).customer_site_use_id
3220                                 ,P_SITE_USE_ID       => null);
3221 	  */
3222 	  open C_DISPUTED_AMOUNT_BILLTO(p_delinquencies_tbl(1).customer_site_use_id);
3223 
3224       end if;
3225 
3226 
3227       /*==================================================================
3228        * l_noskip is used to trace the del data is all disputed or not;
3229        * if any one del not disputed, then l_noskip=1;
3230        * if l_noskip=0, then means all del are disputed,
3231        *    => for this customer/account, skip it;
3232        *==================================================================*/
3233       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - counting disputed delinquencies ');
3234       --fetch C_DISPUTED_AMOUNT into l_amount_disputed;
3235       --    close C_DISPUTED_AMOUNT;
3236       if (p_running_level = 'CUSTOMER') then
3237 		fetch C_DISPUTED_AMOUNT_PARTY into l_amount_disputed;
3238 		close C_DISPUTED_AMOUNT_PARTY;
3239       elsif (p_running_level = 'ACCOUNT') then
3240 		fetch C_DISPUTED_AMOUNT_ACCOUNT into l_amount_disputed;
3241 		close C_DISPUTED_AMOUNT_ACCOUNT;
3242       elsif (p_running_level = 'BILL_TO') then
3243 		fetch C_DISPUTED_AMOUNT_BILLTO into l_amount_disputed;
3244 		close C_DISPUTED_AMOUNT_BILLTO;
3245       end if;
3246 
3247       --End bug 7197038 gnramasa 8th july 08
3248 
3249       if (l_amount_disputed >= 0) OR (p_resend_flag = 'Y' ) then
3250            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - setting no skip = 0 ');
3251            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - l_amount_disputed ' || l_amount_disputed);
3252            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - p_resend_flag = ' || p_resend_flag);
3253            l_noskip := 0;
3254 			ELSE
3255            l_noskip := 1;
3256       end if;
3257 
3258       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - amount disputed less delinquency amount = ' || l_amount_disputed);
3259       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - object_code='||l_object_code);
3260       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - object_id='||l_object_id);
3261       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - party_id='||l_party_cust_id);
3262       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - amount_due_remaining='||l_amount);
3263       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - currency_code='||l_curr_code);
3264       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - l_noskip='||l_noskip);
3265 
3266       IF (l_noskip > 0) THEN
3267 
3268          -- init the msg (not including the msg from dispute api)
3269          FND_MSG_PUB.initialize;
3270 
3271 
3272           /*===========================================
3273            * Get Score From IEX_SCORE_HISTORIES
3274            * If NotFound => Call API to getScore;
3275            *===========================================*/
3276 	         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Get Score');
3277 	         Open C_Get_SCORE(l_object_id, l_object_Code, p_dunning_plan_id);
3278 	         Fetch C_Get_SCORE into l_score;
3279 
3280 	         If ( C_GET_SCORE%NOTFOUND) Then
3281 	              FND_MESSAGE.Set_Name('IEX', 'IEX_NO_SCORE');
3282 	              FND_MSG_PUB.Add;
3283 	              FND_FILE.PUT_LINE(FND_FILE.LOG, 'Missing Score');
3284 	              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' -  Missing Score');
3285 	              Close C_Get_SCORE;
3286 	              RAISE FND_API.G_EXC_ERROR;
3287 	         END IF;
3288 	         Close C_Get_SCORE;
3289 	         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get Score='||l_score);
3290 
3291           /*===================================================
3292            * in 11.5.11, support aging bucket line for all level;
3293            * clchang added 11/20/04.
3294            * Get Aging_Bucket_Line_id for each party/acct/site
3295            *===================================================*/
3296 
3297            WriteLog('iexvdunb:SendLevelDunn:GetAgingBucketLineId');
3298 
3299                 AGING_DEL(
3300                   p_api_version              => p_api_version
3301                 , p_init_msg_list            => p_init_msg_list
3302                 , p_commit                   => p_commit
3303                 , p_delinquency_id           => null
3304                 , p_dunning_plan_id          => p_dunning_plan_id
3305                 , p_bucket                   => null
3306                 , p_object_code              => l_object_code
3307                 , p_object_id                => l_object_id
3308                 , x_return_status            => x_return_status
3309                 , x_msg_count                => x_msg_count
3310                 , x_msg_data                 => x_msg_data
3311                 , x_aging_bucket_line_id     => l_bucket_line_id);
3312 
3313          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - AgingDel status='||x_return_status);
3314          If ( x_return_status <> FND_API.G_RET_STS_SUCCESS) Then
3315               FND_MESSAGE.Set_Name('IEX', 'IEX_NO_AGINGBUCKETLINE');
3316               FND_MSG_PUB.Add;
3317               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - AgingBucketLineId notfound');
3318               FND_FILE.PUT_LINE(FND_FILE.LOG, 'AgingBucketLineId NotFound');
3319               RAISE FND_API.G_EXC_ERROR;
3320          END IF;
3321          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - lineid='||l_bucket_line_id);
3322 
3323           /*===========================================
3324            * Get Template_ID From iex_ag_dn_xref
3325            *===========================================*/
3326 
3327            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - GET Template');
3328            Open C_Get_TEMPLATE(l_bucket_line_id, l_score, p_running_level, p_dunning_plan_id);
3329            Fetch C_Get_TEMPLATE into
3330                        l_template_id,
3331                        l_xdo_template_id,
3332                        l_method,
3333                        l_callback_flag,
3334                        l_callback_days;
3335 
3336            If ( C_GET_TEMPLATE%NOTFOUND) Then
3337                 --FND_MESSAGE.Set_Name('IEX', 'API_MISSING_UPDATE_TARGET');
3338                 --FND_MESSAGE.Set_Token ('INFO', 'Template_ID', FALSE);
3339                 FND_MESSAGE.Set_Name('IEX', 'IEX_NO_TEMPLATE');
3340                 FND_MSG_PUB.Add;
3341                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Missing corresponding template');
3342 	              FND_FILE.PUT_LINE(FND_FILE.LOG, 'Missing corresponding template');
3343 	              RAISE FND_API.G_EXC_ERROR;
3344          END IF;
3345 
3346            --WriteLog('iexvdunb:SendLevelDunn:close C_GET_TEMPLATE');
3347            Close C_Get_TEMPLATE;
3348 
3349         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get ffm_template_id='||l_template_id);
3350         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get xdo_template_id='||l_xdo_template_id);
3351 
3352 
3353          /*===========================================
3354           * Check template
3355           *  in 11.5.11, IEX supports fulfillment and xml publisher.
3356           *  if the current setup for delivery id FFM,
3357           *  then template_id is necessary;
3358           *  if XML, xdo_template_id is necessary;
3359           *===========================================*/
3360 
3361          l_curr_dmethod := IEX_SEND_XML_PVT.getCurrDeliveryMethod;
3362          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - curr d_method='||l_curr_dmethod);
3363          if ( (l_curr_dmethod is null or l_curr_dmethod = '') or
3364               (l_curr_dmethod = 'FFM' and l_template_id is null)  or
3365               (l_curr_dmethod = 'XML' and l_xdo_template_id is null ) ) then
3366               FND_MESSAGE.Set_Name('IEX', 'IEX_NO_TEMPLATE');
3367               FND_MSG_PUB.Add;
3368               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Missing corresponding template');
3369               FND_FILE.PUT_LINE(FND_FILE.LOG, 'Missing corresponding template');
3370               RAISE FND_API.G_EXC_ERROR;
3371 
3372          end if;
3373 
3374 	       /*===========================================
3375           * Check profile before send dunning
3376           *===========================================*/
3377          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' -  begin check customer profile');
3378 
3379           -- ctlee - check the hz_customer_profiles.dunning_letter
3380            FND_FILE.PUT_LINE(FND_FILE.LOG, 'l_party_cust_id = ' || l_party_cust_id);
3381            FND_FILE.PUT_LINE(FND_FILE.LOG, 'l_account_id = ' || l_account_id);
3382            FND_FILE.PUT_LINE(FND_FILE.LOG, 'l_customer_site_use_id = ' || l_customer_site_use_id);
3383            FND_FILE.PUT_LINE(FND_FILE.LOG, 'l_delinquency_id = ' || l_delinquency_id);
3384             -- ctlee - check the hz_customer_profiles.dunning_letter
3385             l_party_cust_id_check := l_party_cust_id;
3386             l_account_id_check := l_account_id;
3387             l_customer_site_use_id_check := l_customer_site_use_id;
3388             l_delinquency_id_check := l_delinquency_id;
3389            if (p_running_level = 'CUSTOMER') then
3390               l_account_id_check := null;
3391               l_customer_site_use_id_check := null;
3392               l_delinquency_id_check := null;
3393            elsif  (p_running_level = 'ACCOUNT') then
3394               l_customer_site_use_id_check := null;
3395               l_delinquency_id_check := null;
3396            elsif  (p_running_level = 'BILL_TO') then
3397               l_delinquency_id_check := null;
3398            end if;
3399            if ( iex_utilities.DunningProfileCheck (
3400                    p_party_id => l_party_cust_id_check
3401                    , p_cust_account_id => l_account_id_check
3402                    , p_site_use_id => l_customer_site_use_id_check
3403                    , p_delinquency_id => l_delinquency_id_check     ) = 'N'
3404               ) then
3405                 FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_DUNNING_PROFILE_NO');
3406                 FND_MSG_PUB.Add;
3407 	              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' -  Send dunning in customer profile set to no ');
3408                 FND_FILE.PUT_LINE(FND_FILE.LOG, 'Send dunning in customer profile set to no ');
3409                 x_return_status := FND_API.G_RET_STS_ERROR;
3410                 GOTO end_api;
3411            end if;
3412 
3413 
3414            -- ctlee - check the hz_customer_profiles_amt min_dunning_invoice_amount and min_dunning_amount
3415            if ( iex_utilities.DunningMinAmountCheck (
3416                     p_cust_account_id => l_account_id_check
3417                     , p_site_use_id => l_customer_site_use_id_check)  = 'N'
3418               ) then
3419                 FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_DUNNING_AMOUNT_MIN');
3420                 FND_MSG_PUB.Add;
3421 	              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' -  Required min Dunning amount in customer profile ');
3422                 FND_FILE.PUT_LINE(FND_FILE.LOG, 'Required min Dunning amount in customer profile ');
3423                 x_return_status := FND_API.G_RET_STS_ERROR;
3424                 GOTO end_api;
3425            end if;
3426 
3427          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' -  end check customer profile');
3428 
3429           /*===========================================
3430            * Close OPEN Dunnings for each party/account
3431            *===========================================*/
3432           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CloseDunning');
3433 
3434           Close_DUNNING(
3435                 p_api_version              => p_api_version
3436               , p_init_msg_list            => p_init_msg_list
3437               , p_commit                   => p_commit
3438               , p_delinquencies_tbl        => l_del_tbl
3439               , p_running_level            => p_running_level
3440               , x_return_status            => x_return_status
3441               , x_msg_count                => x_msg_count
3442               , x_msg_data                 => x_msg_data);
3443 
3444           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CloseDunning status='|| x_return_status);
3445 
3446           IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3447                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot Close Dunning');
3448                 FND_FILE.PUT_LINE(FND_FILE.LOG, 'Cannot Close Dunning');
3449                 x_return_status := FND_API.G_RET_STS_ERROR;
3450                 GOTO end_api;
3451           END IF;
3452           --WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - EndCloseDunn');
3453 
3454 
3455 
3456           /*===========================================
3457            * Create Dunning Record
3458            *===========================================*/
3459           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Get CallbackDate');
3460           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackFlag='||l_callback_flag);
3461           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackDays='||l_callback_days);
3462 
3463             /*===================================================
3464              * clchang updated 02/13/2003
3465              * callback_days could be null if callback_yn = 'N';
3466              * and if callback_yn = 'N', not get callback_date;
3467              *==================================================*/
3468              IF (l_callback_flag = 'Y') THEN
3469 
3470 		             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackFlag is Y: GetCallbackDate');
3471 		             Get_Callback_Date(p_init_msg_list          => FND_API.G_FALSE
3472 		                              ,p_callback_days          => l_callback_days
3473 		                              ,x_callback_date          => l_callback_date
3474 		                              ,x_return_status          => x_return_status
3475 		                              ,x_msg_count              => x_msg_count
3476 		                              ,x_msg_data               => x_msg_data);
3477 
3478 		             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - GetCallbackDate status='|| x_return_status);
3479 
3480 		             IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3481 		               FND_MESSAGE.Set_Name('IEX', 'IEX_NO_CALLBACKDATE');
3482 		               FND_MSG_PUB.Add;
3483 		               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot find callback date');
3484 		               FND_FILE.PUT_LINE(FND_FILE.LOG, 'Cannot find callback date');
3485 		               x_return_status := FND_API.G_RET_STS_ERROR;
3486 		               GOTO end_api;
3487 		             END IF;
3488 
3489              ELSE
3490                  WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackFlag is N: NOTGetCallbackDate');
3491 
3492              END IF;
3493 
3494 
3495              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackDate='||l_callback_date);
3496              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Create Dunning');
3497              l_dunning_rec.dunning_level := p_running_level;
3498              l_dunning_rec.dunning_object_id := l_object_id;
3499              l_dunning_rec.callback_yn := l_callback_flag;
3500              l_dunning_rec.callback_date := l_callback_date;
3501              l_dunning_rec.status := 'OPEN';
3502              l_dunning_rec.dunning_method := l_method;
3503 
3504              if (l_curr_dmethod = 'FFM') then
3505                  l_dunning_rec.template_id:= l_template_id;
3506                  l_dunning_rec.ffm_request_id := l_request_id;
3507              else
3508                  l_dunning_rec.xml_template_id:= l_xdo_template_id;
3509                  l_dunning_rec.xml_request_id := l_request_id;
3510              end if;
3511              l_dunning_rec.object_type := l_object_code;
3512              l_dunning_rec.object_id := l_object_id;
3513              l_dunning_rec.amount_due_remaining := l_amount;
3514              l_dunning_rec.currency_code := l_curr_code;
3515              l_dunning_rec.dunning_plan_id := p_dunning_plan_id;
3516              l_dunning_rec.contact_destination := l_contact_destination;  -- bug 3955222
3517              l_dunning_rec.contact_party_id := l_contact_party_id;  -- bug 3955222
3518 	           l_dunning_rec.request_id           := FND_GLOBAL.Conc_Request_Id;
3519 
3520              CREATE_DUNNING(
3521                    p_api_version              => p_api_version
3522                  , p_init_msg_list            => p_init_msg_list
3523                  , p_commit                   => p_commit
3524                  , p_dunning_rec              => l_dunning_rec
3525                  , x_dunning_id               => l_dunning_id
3526                  , x_return_status            => x_return_status
3527                  , x_msg_count                => x_msg_count
3528                  , x_msg_data                 => x_msg_data);
3529 
3530               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CreateDunning status='|| x_return_status);
3531 
3532               IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3533                    WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot Create Dunning');
3534                    FND_FILE.PUT_LINE(FND_FILE.LOG, 'Cannot Create Dunning');
3535                    x_return_status := FND_API.G_RET_STS_ERROR;
3536                    GOTO end_api;
3537               END IF;
3538 
3539               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - DunningId='||l_dunning_id);
3540               FND_FILE.PUT_LINE(FND_FILE.LOG, 'Create Dunning Id='|| l_dunning_id);
3541               l_dunn_cnt := l_dunn_cnt + 1;
3542 
3543           /*===========================================
3544            * Send letter thru fulfillment
3545            *===========================================*/
3546          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - SEND_FFM');
3547          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - org_id='||l_org_id);
3548          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - party_id='||l_party_cust_id);
3549          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - method='||l_method);
3550 
3551          l_bind_tbl(1).key_name := 'party_id';
3552          l_bind_tbl(1).key_type := 'NUMBER';
3553          l_bind_tbl(1).key_value := l_party_cust_id;
3554          l_bind_tbl(2).key_name := 'org_id';
3555          l_bind_tbl(2).key_type := 'NUMBER';
3556          l_bind_tbl(2).key_value := l_org_id;
3557          l_bind_tbl(3).key_name := 'account_id';
3558          l_bind_tbl(3).key_type := 'NUMBER';
3559          l_bind_tbl(3).key_value := l_account_id;
3560          -- new bind rec since 11.5.10 (for BILL_TO)
3561          l_bind_tbl(4).key_name := 'customer_site_use_id';
3562          l_bind_tbl(4).key_type := 'NUMBER';
3563          l_bind_tbl(4).key_value := l_customer_site_use_id;
3564          l_bind_tbl(5).key_name := 'DUNNING_ID';
3565          l_bind_tbl(5).key_type := 'NUMBER';
3566          l_bind_tbl(5).key_value := l_dunning_id;
3567 
3568         /**
3569          * in 11.5.11, IEX supports fulfillment and xml publisher.
3570          * it depends on the set up in IEX ADMIN/SETUP.
3571          */
3572          if (l_curr_dmethod = 'FFM') then
3573 
3574           Send_Fulfillment(p_api_version              => p_api_version
3575                           ,p_init_msg_list            => FND_API.G_FALSE
3576                           ,p_commit                   => FND_API.G_TRUE
3577                           ,p_fulfillment_bind_tbl     => l_bind_tbl
3578                           ,p_template_id              => l_template_id
3579                           ,p_method                   => l_method
3580                           ,p_party_id                 => l_party_cust_id
3581                           ,x_request_id               => l_request_id
3582                           ,x_return_status            => x_return_status
3583                           ,x_msg_count                => x_msg_count
3584                           ,x_msg_data                 => x_msg_data
3585                           ,x_contact_destination      => l_contact_destination
3586                           ,x_contact_party_id         => l_contact_party_id
3587 													);
3588          else
3589 
3590           Send_XML(p_api_version              => p_api_version
3591                   ,p_init_msg_list            => FND_API.G_FALSE
3592                   ,p_commit                   => FND_API.G_TRUE
3593                   ,p_resend                   => 'N'
3594                   ,p_request_id               => null
3595                   ,p_fulfillment_bind_tbl     => l_bind_tbl
3596                   ,p_template_id              => l_xdo_template_id
3597                   ,p_method                   => l_method
3598                   ,p_party_id                 => l_party_cust_id
3599                   ,p_level                    => p_running_level
3600                   ,p_source_id                => l_object_id
3601                   ,p_object_code              => l_object_code
3602                   ,p_object_id                => l_object_id
3603                   ,x_request_id               => l_request_id
3604                   ,x_return_status            => x_return_status
3605                   ,x_msg_count                => x_msg_count
3606                   ,x_msg_data                 => x_msg_data
3607                   ,x_contact_destination      => l_contact_destination
3608                   ,x_contact_party_id         => l_contact_party_id);
3609          end if;
3610 
3611          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - SEND status='|| x_return_status);
3612 
3613          IF x_return_status <> FND_API.G_RET_STS_SUCCESS and x_return_status <> 'W'  THEN
3614              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' -  Sending process failed');
3615              FND_FILE.PUT_LINE(FND_FILE.LOG, 'Sending process failed ');
3616              x_return_status := FND_API.G_RET_STS_ERROR;
3617              GOTO end_api;
3618          elsif x_return_status = 'W' then
3619              l_warning_flag := 'W';
3620          END IF;
3621 
3622           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - request_id = ' ||l_request_id);
3623           FND_FILE.PUT_LINE(FND_FILE.LOG, 'Request Id = ' || l_request_id);
3624           l_ffm_cnt := l_ffm_cnt + 1;
3625 
3626             l_dunning_rec_upd.dunning_id := l_dunning_id;
3627             if (l_curr_dmethod = 'FFM') then
3628                 l_dunning_rec_upd.ffm_request_id := l_request_id;
3629             else
3630                 l_dunning_rec_upd.xml_request_id := l_request_id;
3631             end if;
3632 
3633             IEX_DUNNING_PVT.Update_DUNNING(
3634                 p_api_version              => 1.0
3635                 , p_init_msg_list            => FND_API.G_FALSE
3636                 , p_commit                   => FND_API.G_TRUE
3637                 , p_dunning_rec              => l_dunning_rec_upd
3638                 , x_return_status            => l_return_status
3639                 , x_msg_count                => l_msg_count
3640                 , x_msg_data                 => l_msg_data
3641             );
3642 
3643           <<end_api>>
3644 
3645           if (x_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
3646               FND_MSG_PUB.Count_And_Get
3647               (  p_count          =>   x_msg_count,
3648                  p_data           =>   x_msg_data );
3649               for i in 1..x_msg_count loop
3650                 errmsg := FND_MSG_PUB.Get(p_msg_index => i,
3651                                           p_encoded => 'F');
3652                 FND_FILE.PUT_LINE(FND_FILE.LOG, 'Error:'||errmsg);
3653                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' -  errmsg=' || errmsg);
3654               end loop;
3655           end if;
3656           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end_api');
3657 
3658      ELSE -- l_noskip = 0
3659           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - all del disputed');
3660           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - skip this party/accnt/site');
3661           FND_FILE.PUT_LINE(FND_FILE.LOG, 'all delinquencies disputed' );
3662           FND_FILE.PUT_LINE(FND_FILE.LOG, 'skip this party/account/site' );
3663           FND_MSG_PUB.Count_And_Get(  p_count          =>   x_msg_count,
3664                                       p_data           =>   x_msg_data );
3665           x_return_status := 'SKIP'; --FND_API.G_EXC_ERROR;
3666 
3667      end if; -- end of if (l_noskip>0)
3668 
3669      if l_warning_flag = 'W' then
3670       x_return_status := 'W';
3671      end if;
3672 
3673      --
3674      -- End of API body
3675      --
3676 
3677      COMMIT WORK;
3678 
3679      WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - END');
3680 
3681      FND_MSG_PUB.Count_And_Get
3682      (  p_count          =>   x_msg_count,
3683         p_data           =>   x_msg_data );
3684 
3685      EXCEPTION
3686          WHEN FND_API.G_EXC_ERROR THEN
3687              COMMIT WORK;
3688              x_return_status := FND_API.G_RET_STS_ERROR;
3689              FND_MSG_PUB.Count_And_Get
3690              (  p_count          =>   x_msg_count,
3691                 p_data           =>   x_msg_data );
3692              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception');
3693              FND_FILE.PUT_LINE(FND_FILE.LOG, 'expect exception' );
3694              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM);
3695 
3696 
3697          WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3698              COMMIT WORK;
3699              x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3700              FND_MSG_PUB.Count_And_Get
3701              (  p_count          =>   x_msg_count,
3702                 p_data           =>   x_msg_data );
3703              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExc Exception');
3704              FND_FILE.PUT_LINE(FND_FILE.LOG, 'unexpect exception' );
3705              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM);
3706 
3707 
3708          WHEN OTHERS THEN
3709              COMMIT WORK;
3710              x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3711              FND_MSG_PUB.Count_And_Get
3712              (  p_count          =>   x_msg_count,
3713                 p_data           =>   x_msg_data );
3714              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception');
3715              FND_FILE.PUT_LINE(FND_FILE.LOG, 'unexpect exception' );
3716              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM);
3717 
3718 END Send_Level_Dunning;
3719 
3720 
3721 
3722 /*==========================================================================
3723  * clchang updated 09/19/2002 -
3724  *     insert ffm_request_id into iex_dunnings after CALL_FFM successfully;
3725  *     Create_Dunning and Update_Dunning also updated;
3726  *
3727  *==========================================================================*/
3728 Procedure Send_Dunning
3729            (p_api_version             IN NUMBER := 1.0,
3730             p_init_msg_list           IN VARCHAR2 ,
3731             p_commit                  IN VARCHAR2 ,
3732             p_dunning_plan_id         in number,
3733             p_resend_flag             IN VARCHAR2,
3734             p_delinquencies_tbl       IN IEX_DELINQUENCY_PUB.DELINQUENCY_TBL_TYPE,
3735             p_previous_request_id     IN NUMBER,
3736             x_return_status           OUT NOCOPY VARCHAR2,
3737             x_msg_count               OUT NOCOPY NUMBER,
3738             x_msg_data                OUT NOCOPY VARCHAR2)
3739 IS
3740     CURSOR C_GET_DEL (IN_del_ID NUMBER) IS
3741       SELECT delinquency_ID,
3742              party_cust_id,
3743              cust_account_id,
3744              customer_site_use_id,
3745              score_value
3746         FROM IEX_DELINQUENCIES
3747        WHERE delinquency_ID = in_del_ID;
3748     --
3749     CURSOR C_GET_SCORE (IN_ID NUMBER) IS
3750       SELECT a.score_value
3751         FROM IEX_SCORE_HISTORIES a
3752        WHERE a.score_object_ID = in_ID
3753          AND a.score_object_code = 'IEX_DELINQUENCY'
3754          AND a.creation_date = (select max(b.creation_date)
3755                                   from iex_score_histories b
3756                                  where b.score_object_id = in_id
3757                                    AND b.score_object_code = 'IEX_DELINQUENCY');
3758     --
3759     CURSOR C_GET_TEMPLATE (l_line_id NUMBER,
3760                            l_score NUMBER, p_dunning_plan_id number) IS
3761       SELECT x.template_id,
3762              x.xdo_template_id,
3763              x.fm_method,
3764              upper(x.callback_flag),
3765              x.callback_days,
3766              ar.bucket_name
3767         FROM IEX_AG_DN_XREF x,
3768              ar_aging_buckets ar,
3769              iex_dunning_plans_vl d
3770        WHERE x.aging_bucket_line_ID = l_line_ID
3771          and x.dunning_plan_id = p_dunning_plan_id
3772          AND l_score between x.score_range_low and x.score_range_high
3773          AND x.aging_bucket_id = ar.aging_bucket_id
3774          and ar.aging_bucket_id = d.aging_bucket_id
3775          AND ar.status = 'A'
3776          AND x.dunning_level = 'DELINQUENCY' ;
3777     --
3778     cursor c_amount (IN_ID number) is
3779      select ps.amount_due_remaining,
3780             ps.invoice_currency_code
3781        from ar_payment_schedules_all ps,
3782             --iex_delinquencies_all del
3783             iex_delinquencies del
3784       where ps.payment_schedule_id (+)= del.payment_schedule_id
3785         and del.delinquency_id = in_id;
3786     --
3787 
3788     l_AMOUNT                NUMBER;
3789     l_CURR_CODE             VARCHAR2(15);
3790     l_DUNNING_id            NUMBER;
3791     l_delinquency_id        NUMBER;
3792     l_party_cust_id         NUMBER;
3793     l_account_id            NUMBER;
3794     l_customer_site_use_id  NUMBER;
3795     l_location_id           number;
3796     l_dunning_rec           IEX_DUNNING_PUB.DUNNING_REC_TYPE;
3797     l_dunning_rec_upd       IEX_DUNNING_PUB.DUNNING_REC_TYPE;
3798     l_score                 NUMBER;
3799     l_bucket_line_id        NUMBER;
3800     l_campaign_sched_id     NUMBER;
3801     l_template_id           NUMBER;
3802     l_xdo_template_id       NUMBER;
3803     l_method                VARCHAR2(10);
3804     l_callback_flag         VARCHAR2(1);
3805     l_callback_days         NUMBER;
3806     l_callback_date         DATE;
3807     l_request_id            NUMBER;
3808     l_outcome_code          varchar2(20);
3809     l_api_name              CONSTANT VARCHAR2(30) := 'Send_Dunning';
3810     l_api_version_number    CONSTANT NUMBER   := 1.0;
3811     l_return_status         VARCHAR2(1);
3812     l_msg_count             NUMBER;
3813     l_msg_data              VARCHAR2(32767);
3814     errmsg                  VARCHAR2(32767);
3815     --
3816     nIdx                    NUMBER := 0;
3817     TYPE Del_ID_TBL_type is Table of IEX_DELINQUENCIES_ALL.DELINQUENCY_ID%TYPE
3818                             INDEX BY BINARY_INTEGER;
3819     Del_Tbl                 Del_ID_TBL_TYPE;
3820     l_bind_tbl              IEX_DUNNING_PVT.FULFILLMENT_BIND_TBL;
3821     l_bind_rec              IEX_DUNNING_PVT.FULFILLMENT_BIND_REC;
3822     l_org_id                NUMBER ;
3823     --l_delid_tbl             IEX_DUNNING_PUB.DelId_NumList;
3824     l_del_tbl               IEX_DELINQUENCY_PUB.DELINQUENCY_TBL_TYPE;
3825     l_ffm_cnt               NUMBER := 0;
3826     l_dunn_cnt              NUMBER := 0;
3827     l_bucket                VARCHAR2(100);
3828 
3829     l_running_level         VARCHAR2(25);
3830     l_object_Code           VARCHAR2(25);
3831     l_object_id             NUMBER;
3832 
3833     l_curr_dmethod          VARCHAR2(10);
3834     l_contact_destination   varchar2(240);  -- bug 3955222
3835     l_contact_party_id      number; -- bug 3955222
3836     l_contact_id            number;
3837     l_warning_flag          varchar2(1);
3838     l_contact_point_id      number;
3839 
3840 BEGIN
3841       -- Standard Start of API savepoint
3842       SAVEPOINT Send_DUNNING_PVT;
3843 
3844       --Bug#4679639 schekuri 20-OCT-2005
3845       --Value of profile ORG_ID shouldn't be used for getting ORG_ID after MOAC implementation
3846       --l_org_id        := fnd_profile.value('ORG_ID');
3847       l_org_id:= mo_global.get_current_org_id;
3848 
3849       -- Standard call to check for call compatibility.
3850       IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
3851                                            p_api_version,
3852                                            l_api_name,
3853                                            G_PKG_NAME)
3854       THEN
3855           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3856       END IF;
3857 
3858       -- Initialize message list if p_init_msg_list is set to TRUE.
3859       IF FND_API.to_Boolean( p_init_msg_list )
3860       THEN
3861           FND_MSG_PUB.initialize;
3862       END IF;
3863 
3864       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Start');
3865 
3866       -- Initialize API return status to SUCCESS
3867       x_return_status := FND_API.G_RET_STS_SUCCESS;
3868 
3869       --
3870       -- Api body
3871       --
3872       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ' || p_resend_flag);
3873 
3874       FOR i in 1..p_delinquencies_tbl.count
3875       LOOP
3876           l_delinquency_id := p_delinquencies_tbl(i).delinquency_id;
3877           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ==================');
3878           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ' || l_delinquency_Id);
3879           FND_FILE.PUT_LINE(FND_FILE.LOG, 'delinquency_id='||l_delinquency_id );
3880 
3881           -- Validate Data
3882 
3883          /*=============================================================
3884           *  For each Delinquency,
3885           *=============================================================*/
3886 
3887          l_party_cust_id := p_delinquencies_tbl(i).party_cust_id;
3888          l_account_id := p_delinquencies_tbl(i).cust_account_id;
3889          l_customer_site_use_id := p_delinquencies_tbl(i).customer_site_use_id;
3890          l_score := p_delinquencies_tbl(i).score_value;
3891 
3892 
3893           --WriteLog('iexvdunb.pls:SendDunn:open del='||l_delinquency_Id);
3894           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - '||l_party_cust_id);
3895           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - '||l_account_id);
3896           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - '||l_customer_site_use_id);
3897           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - '||l_score);
3898 
3899           /*================================================================
3900            * IsDispute ?
3901            * If yes => stop (exit);
3902            * else continue;
3903            *
3904            * it returns values :
3905            * 1) it returns 'F' if no dispute exists for the delinquency
3906            * 2) it returns 'T' if dispute exists and is pending
3907            * 3) it returns 'F' if dispute exists and its staus is "COMPLETE"
3908            *================================================================*/
3909 
3910           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Chk IsDispute');
3911 
3912             IEX_DISPUTE_PVT.Is_Delinquency_Dispute(
3913               p_api_version              => p_api_version
3914             , p_init_msg_list            => p_init_msg_list
3915             , p_delinquency_id           => l_delinquency_id
3916             , x_return_status            => x_return_status
3917             , x_msg_count                => x_msg_count
3918             , x_msg_data                 => x_msg_data);
3919 
3920             IF x_return_status = 'T' THEN
3921                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Dispute exists and is pending');
3922                FND_FILE.PUT_LINE(FND_FILE.LOG, 'Dispute exists and is pending' );
3923                GOTO end_loop;
3924             elsif x_return_status = 'F' THEN
3925                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No Dispute or status is COMPLETE');
3926             END IF;
3927 
3928            /*===========================================
3929             * Get Score From IEX_SCORE_HISTORIES
3930             * If NotFound => Call API to getScore;
3931             *===========================================*/
3932 
3933            /*===========================================
3934             * get Aging_Bucket_Line_Id for each Del
3935             *===========================================*/
3936 
3937             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - GetAgingBucketLineId');
3938             --WriteLog('iexvdunb:SendDunn:delid='||l_delinquency_id);
3939 
3940               AGING_DEL(p_api_version              => p_api_version
3941 				               , p_init_msg_list            => p_init_msg_list
3942 				               , p_commit                   => p_commit
3943 				               , p_delinquency_id           => l_delinquency_id
3944 				               , p_dunning_plan_id          => p_dunning_plan_id
3945 				               , p_bucket                   => l_bucket
3946 				               , x_return_status            => x_return_status
3947 				               , x_msg_count                => x_msg_count
3948 				               , x_msg_data                 => x_msg_data
3949 				               , x_aging_bucket_line_id     => l_bucket_line_id);
3950 
3951             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - AgingDel status='||x_return_status);
3952 
3953             IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3954                --FND_MESSAGE.Set_Name('IEX', 'API_MISSING_UPDATE_TARGET');
3955                --FND_MESSAGE.Set_Token ('INFO', 'iex:AginBucketLineId', FALSE);
3956                 FND_MESSAGE.Set_Name('IEX', 'IEX_NO_AGINGBUCKETLINE');
3957                 FND_MSG_PUB.Add;
3958                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot Get AgingBucketLineId');
3959                 FND_FILE.PUT_LINE(FND_FILE.LOG, 'Cannot AgingBucketLineId' );
3960                 --msg
3961                 FND_MSG_PUB.Count_And_Get
3962                 (  p_count          =>   x_msg_count,
3963                    p_data           =>   x_msg_data );
3964                 for i in 1..x_msg_count loop
3965                     errmsg := FND_MSG_PUB.Get(p_msg_index => i,
3966                                               p_encoded => 'F');
3967                     FND_FILE.PUT_LINE(FND_FILE.LOG, 'Error:'||errmsg);
3968                     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - errmsg=' || errmsg);
3969                 end loop;
3970 
3971                 GOTO end_loop;
3972               END IF;
3973 
3974            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - lineid='||l_bucket_line_id);
3975            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - EndAgingDel');
3976 
3977 
3978            /*===========================================
3979             * Get Template_ID From iex_ag_dn_xref
3980             *===========================================*/
3981 
3982            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - GET Template');
3983 
3984            Open C_Get_TEMPLATE(l_bucket_line_ID, l_score, p_dunning_plan_id);
3985            Fetch C_Get_TEMPLATE into
3986                  l_template_id,
3987                  l_xdo_template_id,
3988                  l_method,
3989                  l_callback_flag,
3990                  l_callback_days,
3991                  l_bucket;
3992 
3993            If ( C_GET_TEMPLATE%NOTFOUND) Then
3994                FND_MESSAGE.Set_Name('IEX', 'IEX_NO_TEMPLATE');
3995                FND_MSG_PUB.Add;
3996                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Missing corresponding template');
3997                FND_FILE.PUT_LINE(FND_FILE.LOG, 'Missing corresponding template' );
3998                Close C_Get_TEMPLATE;
3999                GOTO end_loop;
4000            END IF;
4001 
4002            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - close C_GET_TEMPLATE');
4003            Close C_Get_TEMPLATE;
4004 
4005            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get ffm_template_id='||l_template_id);
4006            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get xdo_template_id='||l_xdo_template_id);
4007            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get bucket='||l_bucket);
4008 
4009           /*===========================================
4010            * Check template
4011            *  in 11.5.11, IEX supports fulfillment and xml publisher.
4012            *  if the current setup for delivery id FFM,
4013            *  then template_id is necessary;
4014            *  if XML, xdo_template_id is necessary;
4015            *===========================================*/
4016 
4017            l_curr_dmethod := IEX_SEND_XML_PVT.getCurrDeliveryMethod;
4018            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - curr d_method='||l_curr_dmethod);
4019            if ( (l_curr_dmethod is null or l_curr_dmethod = '') or
4020                 (l_curr_dmethod = 'FFM' and l_template_id is null)  or
4021                 (l_curr_dmethod = 'XML' and l_xdo_template_id is null ) ) then
4022                 --FND_MESSAGE.Set_Name('IEX', 'API_MISSING_UPDATE_TARGET');
4023                 --FND_MESSAGE.Set_Token ('INFO', 'Template_ID', FALSE);
4024                 FND_MESSAGE.Set_Name('IEX', 'IEX_NO_TEMPLATE');
4025                 FND_MSG_PUB.Add;
4026                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Missing corresponding template');
4027                 FND_FILE.PUT_LINE(FND_FILE.LOG, 'Missing corresponding template' );
4028                 x_return_status := FND_API.G_RET_STS_ERROR;
4029                 GOTO end_loop;
4030            end if;
4031 
4032           /*===========================================
4033            * Check profile before send dunning
4034            *===========================================*/
4035 
4036            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - begin check customer profile');
4037             -- ctlee - check the hz_customer_profiles.dunning_letter
4038            if ( iex_utilities.DunningProfileCheck (
4039                    p_party_id => l_party_cust_id
4040                    , p_cust_account_id => l_account_id
4041                    , p_site_use_id => l_customer_site_use_id
4042                    , p_delinquency_id => l_delinquency_id     ) = 'N'
4043               ) then
4044                 FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_DUNNING_PROFILE_NO');
4045                 FND_MSG_PUB.Add;
4046                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Send dunning in customer profile set to no ');
4047                 FND_FILE.PUT_LINE(FND_FILE.LOG, 'Send dunning in customer profile set to no ');
4048                 GOTO end_loop;
4049            end if;
4050 
4051 
4052            -- ctlee - check the hz_customer_profiles_amt min_dunning_invoice_amount and min_dunning_amount
4053            if ( iex_utilities.DunningMinAmountCheck (
4054                     p_cust_account_id => l_account_id
4055                     , p_site_use_id => l_customer_site_use_id)  = 'N'
4056               ) then
4057                 FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_DUNNING_AMOUNT_MIN');
4058                 FND_MSG_PUB.Add;
4059                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Required min Dunning amount in customer profile ');
4060                 FND_FILE.PUT_LINE(FND_FILE.LOG, 'Required min Dunning amount in customer profile ');
4061                 GOTO end_loop;
4062            end if;
4063 
4064            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end check customer profile');
4065 
4066            /*===========================================
4067             * Close OPEN Dunnings for each Del
4068             *===========================================*/
4069 
4070               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CloseDunning');
4071               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - runninglevel=DELINQUENCY');
4072               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - delid='||l_delinquency_id);
4073               l_del_tbl(1).delinquency_id := l_delinquency_id;
4074 
4075               Close_DUNNING(p_api_version              => p_api_version
4076 					                , p_init_msg_list            => p_init_msg_list
4077 					                , p_commit                   => p_commit
4078 					                , p_delinquencies_tbl        => l_del_tbl
4079 					                , p_running_level            => 'DELINQUENCY'
4080 					                , x_return_status            => x_return_status
4081 					                , x_msg_count                => x_msg_count
4082 					                , x_msg_data                 => x_msg_data);
4083 
4084               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CloseDunning status='|| x_return_status);
4085 
4086               IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4087                  WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot Close Dunning');
4088                  --msg
4089                  GOTO end_loop;
4090               END IF;
4091               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - EndCloseDunn');
4092 
4093            /*===========================================
4094             * Create Dunning Record
4095             *===========================================*/
4096 
4097               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Get CallbackDate');
4098               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackFlag='||l_callback_flag);
4099               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackDays='||l_callback_days);
4100 
4101            /*===================================================
4102             * clchang updated 02/13/2003
4103             * callback_days could be null if callback_yn = 'N';
4104             * and if callback_yn = 'N', not get callback_date;
4105             *==================================================*/
4106             IF (l_callback_flag = 'Y') THEN
4107                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackFlag is Y: NOTGetCallbackDate');
4108                Get_Callback_Date(p_init_msg_list        => p_init_msg_list
4109 		                           , p_callback_days        => l_callback_days
4110 		                           , x_callback_date        => l_callback_date
4111 		                           , x_return_status        => x_return_status
4112 		                           , x_msg_count            => x_msg_count
4113 		                           , x_msg_data             => x_msg_data);
4114 
4115                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - GetCallbackDate status='|| x_return_status);
4116                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackDate='||l_callback_date);
4117 
4118                IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4119                  WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Not Get CallbackDate');
4120                  GOTO end_loop;
4121                END IF;
4122 
4123             ELSE
4124                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackFlag is N: NOTGetCallbackDate');
4125             END IF;
4126 
4127 
4128             /* get the current amount_due_remaining and currency_code */
4129             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - GET AMOUNT');
4130 
4131             Open C_AMOUNT(l_delinquency_id);
4132             Fetch C_AMOUNT into
4133                 l_amount,
4134                 l_curr_code;
4135 
4136             If ( C_AMOUNT%NOTFOUND) Then
4137                FND_MESSAGE.Set_Name('IEX', 'API_MISSING_UPDATE_TARGET');
4138                FND_MESSAGE.Set_Token ('INFO', 'iex:amount', FALSE);
4139                FND_MSG_PUB.Add;
4140                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - amount notfound');
4141             END IF;
4142 
4143             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - close C_AMOUNT');
4144             Close C_AMOUNT;
4145 
4146             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get amount='||l_amount);
4147             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get currcode='||l_curr_code);
4148             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CreateDunning');
4149             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - delid='||l_delinquency_id);
4150             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - object_id='||l_delinquency_id);
4151 
4152             l_dunning_rec.delinquency_id := l_delinquency_id;
4153             l_dunning_rec.callback_yn := l_callback_flag;
4154             l_dunning_rec.callback_date := l_callback_date;
4155             l_dunning_rec.status := 'OPEN';
4156             l_dunning_rec.dunning_method:= l_method;
4157             l_dunning_rec.template_id:= l_template_id;
4158             l_dunning_rec.xml_template_id:= l_xdo_template_id;
4159             l_dunning_rec.campaign_sched_id := l_campaign_sched_id;
4160             l_dunning_rec.xml_request_id := l_request_id;
4161             l_dunning_rec.dunning_object_id := l_delinquency_id;
4162             l_dunning_rec.dunning_level := 'DELINQUENCY';
4163             l_dunning_rec.object_type := 'IEX_DELINQUENCY';
4164             l_dunning_rec.object_id := l_delinquency_id;
4165             l_dunning_rec.amount_due_remaining := l_amount;
4166             l_dunning_rec.currency_code := l_curr_code;
4167             l_dunning_rec.dunning_plan_id := p_dunning_plan_id;
4168             l_dunning_rec.contact_destination := l_contact_destination;  -- bug 3955222
4169             l_dunning_rec.contact_party_id := l_contact_party_id;  -- bug 3955222
4170 
4171             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - InsertRow');
4172 
4173             CREATE_DUNNING(p_api_version              => p_api_version
4174 							            , p_init_msg_list            => p_init_msg_list
4175 							            , p_commit                   => p_commit
4176 							            , p_dunning_rec              => l_dunning_rec
4177 							            , x_dunning_id               => l_dunning_id
4178 							            , x_return_status            => x_return_status
4179 							            , x_msg_count                => x_msg_count
4180 							            , x_msg_data                 => x_msg_data);
4181 
4182             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CreateDunning status='|| x_return_status);
4183 
4184             IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4185               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot Create Dunning');
4186               GOTO end_loop;
4187             END IF;
4188 
4189             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - DunningId='||l_dunning_id);
4190             --FND_FILE.PUT_LINE(FND_FILE.LOG, 'Create Dunning Id=' ||l_dunning_id);
4191             l_dunn_cnt := l_dunn_cnt + 1;
4192 
4193            /*===========================================
4194             * Send Letter through Fulfillment
4195             *===========================================*/
4196             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Send_Ffm');
4197             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - org_id = '|| l_org_id);
4198             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - party_id = '|| l_party_cust_id);
4199             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - acct_id = '|| l_account_id);
4200             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - site_use_id = '|| l_customer_site_use_id);
4201             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - bucket_line_id = '|| l_bucket_line_id);
4202             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - delinquency_id = '|| l_delinquency_id);
4203 
4204             l_bind_tbl(1).key_name := 'party_id';
4205             l_bind_tbl(1).key_type := 'NUMBER';
4206             l_bind_tbl(1).key_value := l_party_cust_id;
4207             l_bind_tbl(2).key_name := 'org_id';
4208             l_bind_tbl(2).key_type := 'NUMBER';
4209             l_bind_tbl(2).key_value := l_org_id;
4210             l_bind_tbl(3).key_name := 'bucket_line_id';
4211             l_bind_tbl(3).key_type := 'NUMBER';
4212             l_bind_tbl(3).key_value := l_bucket_line_id;
4213             l_bind_tbl(4).key_name := 'account_id';
4214             l_bind_tbl(4).key_type := 'NUMBER';
4215             l_bind_tbl(4).key_value := l_account_id;
4216             l_bind_tbl(5).key_name := 'delinquency_id';
4217             l_bind_tbl(5).key_type := 'NUMBER';
4218             l_bind_tbl(5).key_value := l_delinquency_id;
4219             -- added for BILL_TO in 11.5.10.
4220             l_bind_tbl(6).key_name := 'customer_site_use_id';
4221             l_bind_tbl(6).key_type := 'NUMBER';
4222             l_bind_tbl(6).key_value := l_customer_site_use_id;
4223             l_bind_tbl(7).key_name := 'DUNNING_ID';
4224             l_bind_tbl(7).key_type := 'NUMBER';
4225             l_bind_tbl(7).key_value := l_dunning_id;
4226 
4227             if (l_curr_dmethod = 'FFM') then
4228                Send_Fulfillment(p_api_version              => p_api_version
4229                                ,p_init_msg_list            => FND_API.G_TRUE
4230                                ,p_commit                   => FND_API.G_TRUE
4231                                ,p_fulfillment_bind_tbl     => l_bind_tbl
4232                                ,p_template_id              => l_template_id
4233                                ,p_method                   => l_method
4234                                ,p_party_id                 => l_party_cust_id
4235                                ,x_request_id               => l_request_id
4236                                ,x_return_status            => x_return_status
4237                                ,x_msg_count                => x_msg_count
4238 						                   ,x_msg_data                 => x_msg_data
4239 							                 ,x_contact_destination      => l_contact_destination
4240 							                 ,x_contact_party_id         => l_contact_party_id );
4241             else
4242                l_running_level := 'DELINQUENCY';
4243                l_object_code := 'IEX_DELINQUENCY';
4244                l_object_id := l_delinquency_id;
4245 
4246                Send_XML(p_api_version              => p_api_version
4247                        ,p_init_msg_list            => FND_API.G_TRUE
4248                        ,p_commit                   => FND_API.G_TRUE
4249                        ,p_resend                   => 'N'
4250                        ,p_request_id               => null
4251                        ,p_fulfillment_bind_tbl     => l_bind_tbl
4252                        ,p_template_id              => l_xdo_template_id
4253                        ,p_method                   => l_method
4254                        ,p_party_id                 => l_party_cust_id
4255                        ,p_level                    => l_running_level
4256                        ,p_source_id                => l_object_id
4257                        ,p_object_code              => l_object_code
4258                        ,p_object_id                => l_object_id
4259                        ,x_request_id               => l_request_id
4260                        ,x_return_status            => x_return_status
4261                        ,x_msg_count                => x_msg_count
4262  		                   ,x_msg_data                 => x_msg_data
4263 		                   ,x_contact_destination      => l_contact_destination
4264 		                   ,x_contact_party_id         => l_contact_party_id);
4265 
4266             end if;
4267 
4268             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Send status = ' || x_return_status);
4269 
4270             IF x_return_status <> FND_API.G_RET_STS_SUCCESS and x_return_status <> 'W' THEN
4271                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Not Sending Letters');
4272                FND_MSG_PUB.Count_And_Get(p_count          =>   x_msg_count,
4273                                          p_data           =>   x_msg_data );
4274                for i in 1..x_msg_count loop
4275                   errmsg := FND_MSG_PUB.Get(p_msg_index => i,
4276                                             p_encoded => 'F');
4277                   FND_FILE.PUT_LINE(FND_FILE.LOG, 'Error:'||errmsg);
4278                   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - errmsg=' || errmsg);
4279                end loop;
4280                GOTO end_loop;
4281             elsif x_return_status = 'W' then
4282               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - setting warning flag');
4283               l_warning_flag := 'W';
4284             end if;
4285 
4286             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - request_id='||l_request_id);
4287 
4288             l_dunning_rec_upd.dunning_id := l_dunning_id;
4289             if (l_curr_dmethod = 'FFM') then
4290                 l_dunning_rec_upd.ffm_request_id := l_request_id;
4291             else
4292                 l_dunning_rec_upd.xml_request_id := l_request_id;
4293             end if;
4294 
4295             IEX_DUNNING_PVT.Update_DUNNING(p_api_version              => 1.0
4296 													                , p_init_msg_list            => FND_API.G_TRUE
4297 													                , p_commit                   => FND_API.G_TRUE
4298 													                , p_dunning_rec              => l_dunning_rec_upd
4299 													                , x_return_status            => l_return_status
4300 													                , x_msg_count                => l_msg_count
4301 													                , x_msg_data                 => l_msg_data);
4302 
4303             l_ffm_cnt := l_ffm_cnt + 1;
4304 
4305            /*===========================================
4306             * Update Delinquency
4307             * Set DUNN_YN = 'N'
4308             *===========================================*/
4309               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UpdateDel');
4310 
4311               nIdx := nIdx + 1;
4312               del_tbl(nIdx) := l_delinquency_id;
4313 
4314             <<end_loop>>
4315             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end_loop');
4316             NULL;
4317 
4318       END LOOP; -- end of DELINQUENCIES_TBL loop
4319 
4320      WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ==========Summarty==========');
4321      WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - SendFFM Cnt='||l_ffm_cnt);
4322      WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CreateDunn Cnt='||l_dunn_cnt);
4323      WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ==========End Summarty==========');
4324 
4325       --
4326       -- End of API body
4327       --
4328       if l_warning_flag = 'W' then
4329         x_return_status := 'W';
4330       end if;
4331 
4332       COMMIT WORK;
4333 
4334       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - return status = ' || x_return_status);
4335       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - END');
4336 
4337       FND_MSG_PUB.Count_And_Get
4338       (  p_count          =>   x_msg_count,
4339          p_data           =>   x_msg_data );
4340 
4341       EXCEPTION
4342           WHEN FND_API.G_EXC_ERROR THEN
4343               COMMIT WORK;
4344               x_return_status := FND_API.G_RET_STS_ERROR;
4345               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM);
4346               FND_MSG_PUB.Count_And_Get
4347               (  p_count          =>   x_msg_count,
4348                  p_data           =>   x_msg_data );
4349 
4350           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4351               COMMIT WORK;
4352               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4353               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM);
4354               FND_MSG_PUB.Count_And_Get
4355               (  p_count          =>   x_msg_count,
4356                  p_data           =>   x_msg_data );
4357 
4358           WHEN OTHERS THEN
4359               COMMIT WORK;
4360               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4361               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM);
4362               FND_MSG_PUB.Count_And_Get
4363               (  p_count          =>   x_msg_count,
4364                  p_data           =>   x_msg_data );
4365 
4366 END Send_Dunning;
4367 
4368 
4369 
4370 /*=========================================================================
4371    clchang added 03/04/2003 -
4372      Resend_Level_Dunning and Resend_Dunning are called by FORM,
4373      and just for resend;
4374      Only one record once, not loop;
4375    clchang updated 04/21/2003 -
4376      added one new level 'BILL_TO' in 11.5.10.
4377 *=========================================================================*/
4378 Procedure Resend_Level_Dunning
4379            (p_api_version             IN NUMBER := 1.0,
4380             p_init_msg_list           IN VARCHAR2 ,
4381             p_commit                  IN VARCHAR2 ,
4382             p_dunning_plan_id         in number,
4383             p_running_level           IN VARCHAR2,
4384             p_delinquencies_tbl       IN IEX_DELINQUENCY_PUB.DELINQUENCY_TBL_TYPE,
4385             x_request_id              OUT NOCOPY NUMBER,
4386             x_return_status           OUT NOCOPY VARCHAR2,
4387             x_msg_count               OUT NOCOPY NUMBER,
4388             x_msg_data                OUT NOCOPY VARCHAR2)
4389 
4390 IS
4391     CURSOR C_GET_DEL (IN_del_ID NUMBER) IS
4392       SELECT delinquency_ID
4393         FROM IEX_DELINQUENCIES
4394        WHERE delinquency_ID = in_del_ID;
4395     --
4396     -- begin bug 4914799 ctlee 12/30/2005 add p_dunning_plan_id
4397     CURSOR C_GET_SCORE (IN_ID NUMBER, IN_CODE VARCHAR2, p_dunning_plan_id number) IS
4398       SELECT a.score_value
4399         FROM IEX_SCORE_HISTORIES a
4400              , IEX_DUNNING_PLANS_VL c  -- bug 4914799 ctlee 12/30/2005
4401        WHERE a.score_object_ID = in_ID
4402          AND a.score_object_code = IN_CODE
4403          and c.score_id = a.score_id   -- bug 4914799 ctlee 12/30/2005
4404          and c.dunning_plan_id = p_dunning_plan_id -- bug 4914799 ctlee 12/30/2005
4405          AND a.creation_date = (select max(b.creation_date)
4406                                   from iex_score_histories b
4407                                  where b.score_object_id = in_id
4408                                    AND b.score_object_code = IN_CODE);
4409     -- end bug 4914799 ctlee 12/30/2005 add p_dunning_plan_id
4410     --
4411     CURSOR C_GET_TEMPLATE (l_line_id NUMBER,
4412                            l_score NUMBER, in_LEVEL VARCHAR2, p_dunning_plan_id number) IS
4413       SELECT x.template_id,
4414              x.xdo_template_id,
4415              x.fm_method,
4416              upper(x.callback_flag),
4417              x.callback_days
4418         FROM IEX_AG_DN_XREF x,
4419              ar_aging_buckets ar,
4420              iex_dunning_plans_vl d
4421        WHERE x.aging_bucket_line_ID = l_line_ID
4422          and x.dunning_plan_id = p_dunning_plan_id
4423          AND l_score between x.score_range_low and x.score_range_high
4424          AND x.aging_bucket_id = ar.aging_bucket_id
4425          and ar.aging_bucket_id = d.aging_bucket_id
4426          AND ar.status = 'A'
4427          AND x.dunning_level = IN_LEVEL ;
4428     --
4429 
4430     l_DUNNING_id            NUMBER;
4431     l_delinquency_id        NUMBER;
4432     l_party_cust_id         NUMBER;
4433     l_account_id            NUMBER;
4434     l_customer_site_use_id  NUMBER;
4435     l_noskip                NUMBER := 0;
4436     l_DUNNING_rec           IEX_DUNNING_PUB.DUNNING_REC_TYPE;
4437     l_DUNNING_tbl           IEX_DUNNING_PUB.DUNNING_TBL_TYPE;
4438     l_score                 NUMBER;
4439     l_bucket_line_id        NUMBER;
4440     l_campaign_sched_id     NUMBER;
4441     l_template_id           NUMBER;
4442     l_xdo_template_id       NUMBER;
4443     l_method                VARCHAR2(10);
4444     l_callback_flag         VARCHAR2(1);
4445     l_callback_days         NUMBER;
4446     l_callback_date         DATE;
4447     l_request_id            NUMBER;
4448     l_outcome_code          varchar2(20);
4449     l_api_name              CONSTANT VARCHAR2(30) := 'Resend_Level_Dunning';
4450     l_api_version_number    CONSTANT NUMBER   := 1.0;
4451     l_return_status         VARCHAR2(1);
4452     l_msg_count             NUMBER;
4453     l_msg_data              VARCHAR2(32767);
4454     errmsg                  VARCHAR2(32767);
4455     --
4456     nIdx                    NUMBER := 0;
4457     TYPE Del_ID_TBL_type is Table of IEX_DELINQUENCIES_ALL.DELINQUENCY_ID%TYPE
4458                             INDEX BY BINARY_INTEGER;
4459     Del_Tbl                 Del_ID_TBL_TYPE;
4460     l_bind_tbl              IEX_DUNNING_PVT.FULFILLMENT_BIND_TBL;
4461     l_bind_rec              IEX_DUNNING_PVT.FULFILLMENT_BIND_REC;
4462     l_org_id                NUMBER ;
4463     l_running_level         VARCHAR2(25);
4464     l_object_Code           VARCHAR2(25);
4465     l_object_id             NUMBER;
4466     --l_delid_tbl             IEX_DUNNING_PUB.DelId_NumList;
4467     l_del_tbl               IEX_DELINQUENCY_PUB.DELINQUENCY_TBL_TYPE;
4468     l_curr_code             VARCHAR2(15);
4469     l_amount                NUMBER;
4470     l_write                 NUMBER;
4471     l_ffm_cnt               NUMBER := 0;
4472     l_dunn_cnt              NUMBER := 0;
4473     l_curr_dmethod          VARCHAR2(10);
4474 
4475     l_delinquency_id_check        NUMBER;
4476     l_party_cust_id_check         NUMBER;
4477     l_account_id_check            NUMBER;
4478     l_customer_site_use_id_check  NUMBER;
4479     l_contact_destination         varchar2(240);  -- bug 3955222
4480     l_contact_party_id            number; -- bug 3955222
4481 BEGIN
4482       -- Standard Start of API savepoint
4483       SAVEPOINT Resend_Level_DUNNING_PVT;
4484 
4485       --Bug#4679639 schekuri 20-OCT-2005
4486       --Value of profile ORG_ID shouldn't be used for getting ORG_ID after MOAC implementation
4487       l_org_id:= mo_global.get_current_org_id;
4488 
4489       -- Standard call to check for call compatibility.
4490       IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
4491                                            p_api_version,
4492                                            l_api_name,
4493                                            G_PKG_NAME)
4494       THEN
4495           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4496       END IF;
4497 
4498       -- Initialize message list if p_init_msg_list is set to TRUE.
4499       IF FND_API.to_Boolean( p_init_msg_list )
4500       THEN
4501           FND_MSG_PUB.initialize;
4502       END IF;
4503 
4504       -- don't write into FILE
4505       l_write := 0;
4506 
4507       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Start');
4508       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - running_level='||p_running_level);
4509 
4510       -- Initialize API return status to SUCCESS
4511       x_return_status := FND_API.G_RET_STS_SUCCESS;
4512 
4513       --
4514       -- Api body
4515       --
4516 
4517       l_party_cust_id := p_delinquencies_tbl(1).party_cust_id;
4518       l_account_id := p_delinquencies_tbl(1).cust_account_id;
4519       l_customer_site_use_id := p_delinquencies_tbl(1).customer_site_use_id;
4520 
4521       if (p_running_level = 'CUSTOMER') then
4522           l_object_Code := 'PARTY';
4523           l_object_id := p_delinquencies_tbl(1).party_cust_id;
4524           l_del_tbl(1).party_cust_id := p_delinquencies_tbl(1).party_cust_id;
4525           l_amount := party_amount_due_remaining(l_object_id);
4526           l_curr_code := party_currency_code(l_object_id);
4527       --
4528       elsif (p_running_level = 'ACCOUNT') then
4529           l_object_Code := 'IEX_ACCOUNT';
4530           l_object_id := p_delinquencies_tbl(1).cust_account_id;
4531           l_del_tbl(1).cust_account_id := p_delinquencies_tbl(1).cust_account_id;
4532           l_amount := acct_amount_due_remaining(l_object_id);
4533           l_curr_code := acct_currency_code(l_object_id);
4534       --
4535       elsif (p_running_level = 'BILL_TO') then
4536           l_object_Code := 'IEX_BILLTO';
4537           l_object_id := p_delinquencies_tbl(1).customer_site_use_id;
4538           l_del_tbl(1).customer_site_use_id := p_delinquencies_tbl(1).customer_site_use_id;
4539           l_amount := site_amount_due_remaining(l_object_id);
4540           l_curr_code := site_currency_code(l_object_id);
4541       end if;
4542 
4543       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - object_code='||l_object_code);
4544       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - object_id='||l_object_id);
4545       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - party_id='||l_party_cust_id);
4546       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - amount_due_remaining='||l_amount);
4547       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - currency_code='||l_curr_code);
4548 
4549       /*==================================================================
4550        * l_noskip is used to trace the del data is all disputed or not;
4551        * if any one del not disputed, then l_noskip=1;
4552        * if l_noskip=0, then means all del are disputed,
4553        *    => for this customer/account, skip it;
4554        *==================================================================*/
4555       l_noskip := 0;
4556 
4557       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - delcnt='||p_delinquencies_tbl.count);
4558 
4559         FOR i in 1..p_delinquencies_tbl.count
4560         LOOP
4561 
4562           l_delinquency_id := p_delinquencies_tbl(i).delinquency_id;
4563 
4564          /*=============================================================
4565           *  For each Delinquency,
4566           *=============================================================*/
4567 
4568           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - open del='||l_delinquency_Id);
4569 
4570          /*=============================================================
4571           * IsDispute ?
4572           * If yes => stop (exit);
4573           * else continue;
4574           *
4575           * it returns values :
4576           * 1) it returns 'F' if no dispute exists for the delinquency
4577           * 2) it returns 'T' if dispute exists and is pending
4578           * 3) it returns 'F' if dispute exists and its staus is "COMPLETE"
4579           *===========================================================*/
4580 
4581           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ResendLevelDunn:Chk IsDispute');
4582 
4583            IEX_DISPUTE_PVT.Is_Delinquency_Dispute(p_api_version              => p_api_version
4584 																			           , p_init_msg_list            => p_init_msg_list
4585 																			           , p_delinquency_id           => l_delinquency_id
4586 																			           , x_return_status            => x_return_status
4587 																			           , x_msg_count                => x_msg_count
4588 																			           , x_msg_data                 => x_msg_data);
4589 
4590            IF x_return_status = 'T' THEN
4591              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Dispute exists and is pending for del '|| l_delinquency_id);
4592              GOTO end_del;
4593            elsif x_return_status = 'F' THEN
4594               -- if one del is not disputed, then l_noskip=1;
4595               l_noskip := 1;
4596               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No Dispute or status is COMPLETE');
4597            END IF;
4598 
4599            nIdx := nIdx + 1;
4600            del_tbl(nIdx) := l_delinquency_id; --in order to update del)
4601 
4602           <<end_del>>
4603           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Close C_GET_DEL');
4604 
4605        END LOOP; -- end of DELINQUENCIES_TBL loop
4606        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - END LOOP');
4607 
4608       IF (l_noskip > 0) THEN
4609 
4610          -- init the msg (not including the msg from dispute api)
4611          FND_MSG_PUB.initialize;
4612 
4613           /*===========================================
4614            * Get Score From IEX_SCORE_HISTORIES
4615            * If NotFound => Call API to getScore;
4616            *===========================================*/
4617            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Get Score');
4618            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - objectCode='||l_object_Code);
4619 
4620            Open C_Get_SCORE(l_object_id, l_object_Code, p_dunning_plan_id);
4621            Fetch C_Get_SCORE into l_score;
4622 
4623            If ( C_GET_SCORE%NOTFOUND) Then
4624                 --FND_MESSAGE.Set_Name('IEX', 'API_MISSING_UPDATE_TARGET');
4625                 --FND_MESSAGE.Set_Token ('INFO', 'Score', FALSE);
4626                 FND_MESSAGE.Set_Name('IEX', 'IEX_NO_SCORE');
4627                 FND_MSG_PUB.Add;
4628                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Score NotFound');
4629                 Close C_Get_SCORE;
4630                 x_return_status := FND_API.G_RET_STS_ERROR;
4631                 GOTO end_api;
4632            END IF;
4633 
4634            --WriteLog('iexvdunb:ResendLevelDunn:CLOSE C_GET_SCORE', l_write);
4635            Close C_Get_SCORE;
4636 
4637            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get Score='||l_score);
4638 
4639 
4640           /*===================================================
4641            * in 11.5.11, support aging bucket line for all level;
4642            * clchang added 11/20/04.
4643            * Get Aging_Bucket_Line_id for each party/acct/site
4644            *===================================================*/
4645 
4646            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - GetAgingBucketLineId');
4647 
4648            AGING_DEL(p_api_version              => p_api_version
4649 		                , p_init_msg_list            => p_init_msg_list
4650 		                , p_commit                   => p_commit
4651 		                , p_delinquency_id           => null
4652 		                , p_dunning_plan_id          => p_dunning_plan_id
4653 		                , p_bucket                   => null
4654 		                , p_object_code              => l_object_code
4655 		                , p_object_id                => l_object_id
4656 		                , x_return_status            => x_return_status
4657 		                , x_msg_count                => x_msg_count
4658 		                , x_msg_data                 => x_msg_data
4659 		                , x_aging_bucket_line_id     => l_bucket_line_id);
4660 
4661            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - AgingDel status='||x_return_status);
4662 
4663            If ( x_return_status <> FND_API.G_RET_STS_SUCCESS) Then
4664                 --FND_MESSAGE.Set_Name('IEX', 'API_MISSING_UPDATE_TARGET');
4665                 --FND_MESSAGE.Set_Token ('INFO', 'AgingBucketLineId', FALSE);
4666                 FND_MESSAGE.Set_Name('IEX', 'IEX_NO_AGINGBUCKETLINE');
4667                 FND_MSG_PUB.Add;
4668                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - AgingBucketLineId notfound');
4669                 x_return_status := FND_API.G_RET_STS_ERROR;
4670                 GOTO end_api;
4671            END IF;
4672 
4673            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - lineid='||l_bucket_line_id);
4674 
4675           /*===========================================
4676            * Get Template_ID From iex_ag_dn_xref
4677            *===========================================*/
4678 
4679            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - GET Template');
4680 
4681            Open C_Get_TEMPLATE(l_bucket_line_id, l_score, p_running_level, p_dunning_plan_id);
4682            Fetch C_Get_TEMPLATE into
4683                        l_template_id,
4684                        l_xdo_template_id,
4685                        l_method,
4686                        l_callback_flag,
4687                        l_callback_days;
4688 
4689            If ( C_GET_TEMPLATE%NOTFOUND) Then
4690                 FND_MESSAGE.Set_Name('IEX', 'IEX_NO_TEMPLATE');
4691                 FND_MSG_PUB.Add;
4692                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - template notfound');
4693                 x_return_status := FND_API.G_RET_STS_ERROR;
4694                 GOTO end_api;
4695            END IF;
4696 
4697            Close C_Get_TEMPLATE;
4698 
4699            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get template_id='||l_template_id);
4700            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get xdo_template_id='||l_xdo_template_id, l_write);
4701 
4702           /*===========================================
4703            * Check template
4704            *  in 11.5.11, IEX supports fulfillment and xml publisher.
4705            *  if the current setup for delivery id FFM,
4706            *  then template_id is necessary;
4707            *  if XML, xdo_template_id is necessary;
4708            *===========================================*/
4709 
4710            l_curr_dmethod := IEX_SEND_XML_PVT.getCurrDeliveryMethod;
4711            WriteLog('iexvdunb:ResendLevelDunn:curr d_method='||l_curr_dmethod);
4712            if ( (l_curr_dmethod is null or l_curr_dmethod = '') or
4713                 (l_curr_dmethod = 'FFM' and l_template_id is null)  or
4714                 (l_curr_dmethod = 'XML' and l_xdo_template_id is null ) ) then
4715                 --FND_MESSAGE.Set_Name('IEX', 'API_MISSING_UPDATE_TARGET');
4716                 --FND_MESSAGE.Set_Token ('INFO', 'Template_ID', FALSE);
4717                 FND_MESSAGE.Set_Name('IEX', 'IEX_NO_TEMPLATE');
4718                 FND_MSG_PUB.Add;
4719                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Template notfound');
4720                 x_return_status := FND_API.G_RET_STS_ERROR;
4721                 GOTO end_api;
4722            end if;
4723 
4724           /*===========================================
4725            * Check profile before send dunning
4726            *===========================================*/
4727 
4728            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - begin check customer profile');
4729             -- ctlee - check the hz_customer_profiles.dunning_letter
4730            FND_FILE.PUT_LINE(FND_FILE.LOG, 'l_party_cust_id = ' || l_party_cust_id);
4731            FND_FILE.PUT_LINE(FND_FILE.LOG, 'l_account_id = ' || l_account_id);
4732            FND_FILE.PUT_LINE(FND_FILE.LOG, 'l_customer_site_use_id = ' || l_customer_site_use_id);
4733            FND_FILE.PUT_LINE(FND_FILE.LOG, 'l_delinquency_id = ' || l_delinquency_id);
4734             -- ctlee - check the hz_customer_profiles.dunning_letter
4735             l_party_cust_id_check := l_party_cust_id;
4736             l_account_id_check := l_account_id;
4737             l_customer_site_use_id_check := l_customer_site_use_id;
4738             l_delinquency_id_check := l_delinquency_id;
4739            if (p_running_level = 'CUSTOMER') then
4740               l_account_id_check := null;
4741               l_customer_site_use_id_check := null;
4742               l_delinquency_id_check := null;
4743            elsif  (p_running_level = 'ACCOUNT') then
4744               l_customer_site_use_id_check := null;
4745               l_delinquency_id_check := null;
4746            elsif  (p_running_level = 'BILL_TO') then
4747               l_delinquency_id_check := null;
4748            end if;
4749 
4750            if ( iex_utilities.DunningProfileCheck (
4751                    p_party_id => l_party_cust_id_check
4752                    , p_cust_account_id => l_account_id_check
4753                    , p_site_use_id => l_customer_site_use_id_check
4754                    , p_delinquency_id => l_delinquency_id_check     ) = 'N'
4755               ) then
4756                 FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_DUNNING_PROFILE_NO');
4757                 FND_MSG_PUB.Add;
4758                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Send dunning in customer profile set to no ');
4759                 x_return_status := FND_API.G_RET_STS_ERROR;
4760                 GOTO end_api;
4761            end if;
4762 
4763 
4764            -- ctlee - check the hz_customer_profiles_amt min_dunning_invoice_amount and min_dunning_amount
4765            if ( iex_utilities.DunningMinAmountCheck (
4766                     p_cust_account_id => l_account_id_check
4767                     , p_site_use_id => l_customer_site_use_id_check)  = 'N'
4768               ) then
4769                 FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_DUNNING_AMOUNT_MIN');
4770                 FND_MSG_PUB.Add;
4771                 WriteLog('iexvdunb:ResendLevelDunn: Required min Dunning amount in customer profile ');
4772                 x_return_status := FND_API.G_RET_STS_ERROR;
4773                 GOTO end_api;
4774            end if;
4775 
4776            WriteLog('iexvdunb:ResendLevelDunn: end check customer profile');
4777 
4778 
4779           /*===========================================
4780            * Send Letter through Fulfillment
4781            *===========================================*/
4782 
4783            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - SEND_FFM');
4784            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - org_id='||l_org_id);
4785            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - party_id='||l_party_cust_id);
4786            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - method='||l_method);
4787 
4788            l_bind_rec.key_name := 'party_id';
4789            l_bind_rec.key_type := 'NUMBER';
4790            l_bind_rec.key_value := l_party_cust_id;
4791            l_bind_tbl(1) := l_bind_rec;
4792            l_bind_rec.key_name := 'org_id';
4793            l_bind_rec.key_type := 'NUMBER';
4794            l_bind_rec.key_value := l_org_id;
4795            l_bind_tbl(2) := l_bind_rec;
4796            l_bind_rec.key_name := 'account_id';
4797            l_bind_rec.key_type := 'NUMBER';
4798            l_bind_rec.key_value := l_account_id;
4799            l_bind_tbl(3) := l_bind_rec;
4800            -- added for new level 'BILL_TO' since 11.5.10.
4801            l_bind_rec.key_name := 'customer_site_use_id';
4802            l_bind_rec.key_type := 'NUMBER';
4803            l_bind_rec.key_value := l_customer_site_use_id;
4804            l_bind_tbl(4) := l_bind_rec;
4805 
4806          /**
4807           * in 11.5.11, IEX supports fulfillment and xml publisher.
4808           * it depends on the set up in IEX ADMIN/SETUP.
4809           */
4810           if (l_curr_dmethod = 'FFM') then
4811 
4812            Send_Fulfillment(
4813                    p_api_version              => p_api_version
4814                  , p_init_msg_list            => p_init_msg_list
4815                  , p_commit                   => p_commit
4816                  , p_fulfillment_bind_tbl     => l_bind_tbl
4817                  , p_template_id              => l_template_id
4818                  , p_method                   => l_method
4819                  , p_party_id                 => l_party_cust_id
4820                  , x_request_id               => l_request_id
4821                  , x_return_status            => x_return_status
4822                  , x_msg_count                => x_msg_count
4823                  , x_msg_data                 => x_msg_data
4824                  , x_contact_destination      => l_contact_destination  -- bug 3955222
4825                  , x_contact_party_id         => l_contact_party_id  -- bug 3955222
4826                  );
4827           else
4828 
4829            Send_XML(
4830                    p_api_version              => p_api_version
4831                    , p_init_msg_list            => p_init_msg_list
4832                    , p_commit                   => p_commit
4833                    , p_resend                   => 'N'
4834                    , p_request_id               => null
4835                    , p_fulfillment_bind_tbl     => l_bind_tbl
4836                    , p_template_id              => l_xdo_template_id
4837                    , p_method                   => l_method
4838                    , p_party_id                 => l_party_cust_id
4839                    , p_level                    => p_running_level
4840                    , p_source_id                => l_object_id
4841                    , p_object_code              => l_object_code
4842                    , p_object_id                => l_object_id
4843                    , x_request_id               => l_request_id
4844                    , x_return_status            => x_return_status
4845                    , x_msg_count                => x_msg_count
4846                    , x_msg_data                 => x_msg_data
4847                    , x_contact_destination      => l_contact_destination  -- bug 3955222
4848                    , x_contact_party_id         => l_contact_party_id  -- bug 3955222
4849                    );
4850 
4851           end if;
4852 
4853           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - SEND status='|| x_return_status);
4854 
4855            IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4856               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Not Sending Letters', l_write);
4857               x_return_status := FND_API.G_RET_STS_ERROR;
4858               GOTO end_api;
4859            END IF;
4860 
4861 
4862            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - request_id='||l_request_id, l_write);
4863            l_ffm_cnt := l_ffm_cnt + 1;
4864 
4865           /*===========================================
4866            * Close OPEN Dunnings for each party/account
4867            *===========================================*/
4868 
4869              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CloseDunning', l_write);
4870              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - runninglevel='||p_running_level, l_write);
4871              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - del cnt='||l_del_tbl.count, l_write);
4872              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - partyid='||l_del_tbl(1).party_cust_id, l_write);
4873              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - accntid='||l_del_tbl(1).cust_account_id, l_write);
4874              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - siteid='||l_del_tbl(1).customer_site_use_id, l_write);
4875 
4876              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallCloseDunning', l_write);
4877 
4878              Close_DUNNING(p_api_version              => p_api_version
4879 				                 , p_init_msg_list            => p_init_msg_list
4880 				                 , p_commit                   => p_commit
4881 				                 , p_delinquencies_tbl        => l_del_tbl
4882 				                 , p_running_level            => p_running_level
4883 				                 , x_return_status            => x_return_status
4884 				                 , x_msg_count                => x_msg_count
4885 				                 , x_msg_data                 => x_msg_data);
4886 
4887              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CloseDunning status='|| x_return_status, l_write);
4888 
4889              IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4890                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot Close Dunning', l_write);
4891                 x_return_status := FND_API.G_RET_STS_ERROR;
4892                 GOTO end_api;
4893              END IF;
4894              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - EndCloseDunn', l_write);
4895 
4896           /*===========================================
4897            * Create Dunning Record
4898            *===========================================*/
4899 
4900              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Get CallbackDate', l_write);
4901              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackFlag='||l_callback_flag, l_write);
4902              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackDays='||l_callback_days, l_write);
4903 
4904             /*===================================================
4905              * clchang updated 02/13/2003
4906              * callback_days could be null if callback_yn = 'N';
4907              * and if callback_yn = 'N', not get callback_date;
4908              *==================================================*/
4909              IF (l_callback_flag = 'Y') THEN
4910 
4911                  WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackFlag is Y: GetCallbackDate',l_write);
4912                  Get_Callback_Date( p_init_msg_list          => p_init_msg_list
4913                             , p_callback_days          => l_callback_days
4914                             , x_callback_date          => l_callback_date
4915                             , x_return_status          => x_return_status
4916                             , x_msg_count              => x_msg_count
4917                             , x_msg_data               => x_msg_data);
4918 
4919                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - GetCallbackDate status='|| x_return_status, l_write);
4920 
4921                 IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4922                   FND_MESSAGE.Set_Name('IEX', 'IEX_NO_CALLBACKDATE');
4923                   FND_MSG_PUB.Add;
4924                   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Not Get CallbackDate', l_write);
4925                   x_return_status := FND_API.G_RET_STS_ERROR;
4926                   GOTO end_api;
4927                 END IF;
4928 
4929              ELSE
4930                  WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackFlag is N: NOTGetCallbackDate',l_write);
4931 
4932              END IF;
4933 
4934 
4935              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackDate='||l_callback_date, l_write);
4936              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - DunningRec', l_write);
4937              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - objectid='||l_object_id, l_write);
4938              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - objecttype='||p_running_level, l_write);
4939              l_dunning_rec.dunning_level := p_running_level;
4940              l_dunning_rec.dunning_object_id := l_object_id;
4941              l_dunning_rec.callback_yn := l_callback_flag;
4942              l_dunning_rec.callback_date := l_callback_date;
4943              l_dunning_rec.status := 'OPEN';
4944              l_dunning_rec.dunning_method := l_method;
4945              if (l_curr_dmethod = 'FFM') then
4946                l_dunning_rec.template_id:= l_template_id;
4947                l_dunning_rec.ffm_request_id := l_request_id;
4948              else
4949                l_dunning_rec.xml_template_id:= l_xdo_template_id;
4950                l_dunning_rec.xml_request_id := l_request_id;
4951              end if;
4952              l_dunning_rec.amount_due_remaining := l_amount;
4953              l_dunning_rec.currency_code := l_curr_code;
4954              l_dunning_rec.object_type := l_object_code;
4955              l_dunning_rec.object_id := l_object_id;
4956              l_dunning_rec.dunning_plan_id := p_dunning_plan_id;
4957              l_dunning_rec.contact_destination := l_contact_destination;  -- bug 3955222
4958              l_dunning_rec.contact_party_id := l_contact_party_id;  -- bug 3955222
4959 
4960              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Insert Row', l_write);
4961 
4962              CREATE_DUNNING(
4963                    p_api_version              => p_api_version
4964                  , p_init_msg_list            => p_init_msg_list
4965                  , p_commit                   => p_commit
4966                  , p_dunning_rec              => l_dunning_rec
4967                  , x_dunning_id               => l_dunning_id
4968                  , x_return_status            => x_return_status
4969                  , x_msg_count                => x_msg_count
4970                  , x_msg_data                 => x_msg_data);
4971 
4972               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CreateDunning status='|| x_return_status, l_write);
4973 
4974               IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4975                    WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot Create Dunning', l_write);
4976                    x_return_status := FND_API.G_RET_STS_ERROR;
4977                    GOTO end_api;
4978               END IF;
4979 
4980               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - DunningId='||l_dunning_id, l_write);
4981               l_dunn_cnt := l_dunn_cnt + 1;
4982 
4983 
4984 
4985           /*===========================================
4986            * Update Delinquency
4987            * Set DUNN_YN = 'N'
4988            *===========================================*/
4989 
4990              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ==========Summary===========');
4991              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - For RunningLevel='||p_running_level);
4992              --WriteLog('iexvdunb:ResendLevelDunn:Resend - UpdateDel', l_write);
4993              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Resend - object_id='||l_object_id, l_write);
4994 
4995              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - SendFFM Cnt='|| l_ffm_cnt, l_write);
4996              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CreateDunn Cnt='|| l_dunn_cnt, l_write);
4997              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ==========End Summary===========');
4998 
4999 
5000            <<end_api>>
5001            if (x_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
5002                ROLLBACK TO Resend_Level_DUNNING_PVT;
5003                FND_MSG_PUB.Count_And_Get
5004                (  p_count          =>   x_msg_count,
5005                   p_data           =>   x_msg_data );
5006                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end_api:error on this party/accnt/site', l_write);
5007                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end_api:status='||x_return_status, l_write);
5008                --Resend need return the exact status
5009                --x_return_status := 'SKIP'; --FND_API.G_EXC_ERROR;
5010            end if;
5011            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end_api', l_write);
5012 
5013       ELSE -- l_noskip = 0
5014            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - all del disputed', l_write);
5015            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - skip this party/accnt/site', l_write);
5016            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end_api:status='||x_return_status, l_write);
5017            FND_MSG_PUB.Count_And_Get
5018            (  p_count          =>   x_msg_count,
5019               p_data           =>   x_msg_data );
5020            --Resend need return the exact status
5021            --x_return_status := 'SKIP'; --FND_API.G_EXC_ERROR;
5022 
5023       end if; -- end of if (l_noskip>0)
5024       --
5025       -- End of API body
5026       --
5027 
5028       x_request_id := l_request_id;
5029       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - request_id='||x_request_id, l_write);
5030       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - status='||x_return_status, l_write);
5031 
5032       -- Standard check for p_commit
5033       IF FND_API.to_Boolean( p_commit )
5034       THEN
5035           COMMIT WORK;
5036       END IF;
5037 
5038       WriteLog('iexvdunb:ResendLevelDunn:END', l_write);
5039 
5040       FND_MSG_PUB.Count_And_Get
5041       (  p_count          =>   x_msg_count,
5042          p_data           =>   x_msg_data );
5043 
5044       EXCEPTION
5045           WHEN FND_API.G_EXC_ERROR THEN
5046               x_return_status := FND_API.G_RET_STS_ERROR;
5047               FND_MSG_PUB.Count_And_Get
5048               (  p_count          =>   x_msg_count,
5049                  p_data           =>   x_msg_data );
5050               ROLLBACK TO Resend_Level_DUNNING_PVT;
5051               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM, l_write);
5052 
5053           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5054               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5055               FND_MSG_PUB.Count_And_Get
5056               (  p_count          =>   x_msg_count,
5057                  p_data           =>   x_msg_data );
5058               ROLLBACK TO Resend_Level_DUNNING_PVT;
5059               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM, l_write);
5060 
5061           WHEN OTHERS THEN
5062               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5063               FND_MSG_PUB.Count_And_Get
5064               (  p_count          =>   x_msg_count,
5065                  p_data           =>   x_msg_data );
5066               ROLLBACK TO Resend_Level_DUNNING_PVT;
5067               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM, l_write);
5068 
5069 END Resend_Level_Dunning;
5070 
5071 
5072 
5073 Procedure Resend_Dunning
5074            (p_api_version             IN NUMBER := 1.0,
5075             p_init_msg_list           IN VARCHAR2 ,
5076             p_commit                  IN VARCHAR2 ,
5077             p_dunning_plan_id         in number,
5078             p_delinquencies_tbl       IN IEX_DELINQUENCY_PUB.DELINQUENCY_TBL_TYPE,
5079             x_request_id              OUT NOCOPY NUMBER,
5080             x_return_status           OUT NOCOPY VARCHAR2,
5081             x_msg_count               OUT NOCOPY NUMBER,
5082             x_msg_data                OUT NOCOPY VARCHAR2)
5083 IS
5084     CURSOR C_GET_DEL (IN_del_ID NUMBER) IS
5085       SELECT delinquency_ID,
5086              party_cust_id,
5087              cust_account_id,
5088              customer_site_use_id,
5089              score_value
5090         FROM IEX_DELINQUENCIES
5091        WHERE delinquency_ID = in_del_ID;
5092     --
5093     CURSOR C_GET_SCORE (IN_ID NUMBER) IS
5094       SELECT a.score_value
5095         FROM IEX_SCORE_HISTORIES a
5096        WHERE a.score_object_ID = in_ID
5097          AND a.score_object_code = 'IEX_DELINQUENCY'
5098          AND a.creation_date = (select max(b.creation_date)
5099                                   from iex_score_histories b
5100                                  where b.score_object_id = in_id
5101                                    AND b.score_object_code = 'IEX_DELINQUENCY');
5102     --
5103     --
5104     CURSOR C_GET_TEMPLATE (l_line_id NUMBER,
5105                            l_score NUMBER, p_dunning_plan_id number) IS
5106       SELECT x.template_id,
5107              x.xdo_template_id,
5108              x.fm_method,
5109              upper(x.callback_flag),
5110              x.callback_days,
5111              ar.bucket_name
5112         FROM IEX_AG_DN_XREF x,
5113              ar_aging_buckets ar,
5114              iex_dunning_plans_vl d
5115        WHERE x.aging_bucket_line_ID = l_line_ID
5116          and x.dunning_plan_id = p_dunning_plan_id
5117          AND l_score between x.score_range_low and x.score_range_high
5118          AND x.aging_bucket_id = ar.aging_bucket_id
5119          and ar.aging_bucket_id = d.aging_bucket_id
5120          AND ar.status = 'A'
5121          AND x.dunning_level = 'DELINQUENCY' ;
5122     --
5123     cursor c_amount (IN_ID number) is
5124      select ps.amount_due_remaining,
5125             ps.invoice_currency_code
5126        from ar_payment_schedules_all ps,
5127             --iex_delinquencies_all del
5128             iex_delinquencies del
5129       where ps.payment_schedule_id (+)= del.payment_schedule_id
5130         and del.delinquency_id = in_id;
5131     --
5132 
5133     l_AMOUNT                NUMBER;
5134     l_CURR_CODE             VARCHAR2(15);
5135     l_DUNNING_id            NUMBER;
5136     l_delinquency_id        NUMBER;
5137     l_party_cust_id         NUMBER;
5138     l_account_id            NUMBER;
5139     l_customer_site_use_id  NUMBER;
5140     l_DUNNING_rec           IEX_DUNNING_PUB.DUNNING_REC_TYPE;
5141     l_score                 NUMBER;
5142     l_bucket_line_id        NUMBER;
5143     l_campaign_sched_id     NUMBER;
5144     l_template_id           NUMBER;
5145     l_xdo_template_id       NUMBER;
5146     l_method                VARCHAR2(10);
5147     l_callback_flag         VARCHAR2(1);
5148     l_callback_days         NUMBER;
5149     l_callback_date         DATE;
5150     l_request_id            NUMBER;
5151     l_outcome_code          varchar2(20);
5152     l_api_name              CONSTANT VARCHAR2(30) := 'Resend_Dunning';
5153     l_api_version_number    CONSTANT NUMBER   := 1.0;
5154     l_return_status         VARCHAR2(1);
5155     l_msg_count             NUMBER;
5156     l_msg_data              VARCHAR2(32767);
5157     errmsg                  VARCHAR2(32767);
5158     --
5159     nIdx                    NUMBER := 0;
5160     TYPE Del_ID_TBL_type is Table of IEX_DELINQUENCIES_ALL.DELINQUENCY_ID%TYPE
5161                             INDEX BY BINARY_INTEGER;
5162     Del_Tbl                 Del_ID_TBL_TYPE;
5163     l_bind_tbl              IEX_DUNNING_PVT.FULFILLMENT_BIND_TBL;
5164     l_bind_rec              IEX_DUNNING_PVT.FULFILLMENT_BIND_REC;
5165     l_org_id                NUMBER ;
5166     --l_delid_tbl             IEX_DUNNING_PUB.DelId_NumList;
5167     l_del_tbl               IEX_DELINQUENCY_PUB.DELINQUENCY_TBL_TYPE;
5168     l_ffm_cnt               NUMBER := 0;
5169     l_dunn_cnt              NUMBER := 0;
5170     l_bucket                VARCHAR2(100);
5171     i                       NUMBER := 0;
5172     l_running_level         VARCHAR2(25);
5173     l_object_Code           VARCHAR2(25);
5174     l_object_id             NUMBER;
5175     l_curr_dmethod          VARCHAR2(10);
5176     l_contact_destination         varchar2(240);  -- bug 3955222
5177     l_contact_party_id            number; -- bug 3955222
5178 
5179 BEGIN
5180       -- Standard Start of API savepoint
5181       SAVEPOINT Resend_DUNNING_PVT;
5182 
5183       --Bug#4679639 schekuri 20-OCT-2005
5184       --Value of profile ORG_ID shouldn't be used for getting ORG_ID after MOAC implementation
5185       --l_org_id := fnd_profile.value('ORG_ID');
5186       l_org_id:= mo_global.get_current_org_id;
5187 
5188       -- Standard call to check for call compatibility.
5189       IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
5190                                            p_api_version,
5191                                            l_api_name,
5192                                            G_PKG_NAME)
5193       THEN
5194           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5195       END IF;
5196 
5197       -- Initialize message list if p_init_msg_list is set to TRUE.
5198       IF FND_API.to_Boolean( p_init_msg_list )
5199       THEN
5200           FND_MSG_PUB.initialize;
5201       END IF;
5202 
5203       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Start');
5204 
5205       -- Initialize API return status to SUCCESS
5206       x_return_status := FND_API.G_RET_STS_SUCCESS;
5207 
5208       --dont write into FND_FILE
5209       --l_write := 0;
5210 
5211       --
5212       -- Api body
5213       --
5214 
5215       -- only one del record
5216       i := 1;
5217 
5218       l_delinquency_id := p_delinquencies_tbl(i).delinquency_id;
5219       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ==================');
5220       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - delid='||l_delinquency_Id);
5221 
5222       -- Validate Data
5223 
5224      /*=============================================================
5225       *  For each Delinquency,
5226       *=============================================================*/
5227 
5228       Open C_Get_DEL(l_delinquency_id);
5229       Fetch C_Get_DEL into
5230                l_delinquency_id,
5231                l_party_cust_id,
5232                l_account_id,
5233                l_customer_site_use_id,
5234                l_score;
5235 
5236       If ( C_GET_DEL%NOTFOUND) Then
5237            WriteLog('iexvdunb.pls:ResendDunn:No Open Del');
5238 
5239       else
5240 
5241         WriteLog('iexvdunb.pls:ResendDunn:open del='||l_delinquency_Id);
5242 
5243         /*===============================================================
5244          * IsDispute ?
5245          * If yes => stop (exit);
5246          * else continue;
5247          *
5248          * it returns values :
5249          * 1) it returns 'F' if no dispute exists for the delinquency
5250          * 2) it returns 'T' if dispute exists and is pending
5251          * 3) it returns 'F' if dispute exists and its staus is "COMPLETE"
5252          *==============================================================*/
5253 
5254         WriteLog('iexvdunb.pls:ResendDunn:Chk IsDispute');
5255 
5256         IEX_DISPUTE_PVT.Is_Delinquency_Dispute(p_api_version              => p_api_version
5257 			 														            , p_init_msg_list            => p_init_msg_list
5258 			 														            , p_delinquency_id           => l_delinquency_id
5259 			 														            , x_return_status            => x_return_status
5260 			 														            , x_msg_count                => x_msg_count
5261 			 														            , x_msg_data                 => x_msg_data);
5262 
5263         IF x_return_status = 'T' THEN
5264            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Dispute exists and is pending');
5265            GOTO end_loop;
5266         elsif x_return_status = 'F' THEN
5267            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No Dispute or status is COMPLETE');
5268         END IF;
5269 
5270         -- init msg (not from dispute api)
5271         FND_MSG_PUB.initialize;
5272 
5273        /*===========================================
5274         * Get Score From IEX_SCORE_HISTORIES
5275         * If NotFound => Call API to getScore;
5276         *===========================================*/
5277         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Score='|| l_score);
5278        /*===========================================
5279         * get Aging_Bucket_Line_Id for each Del
5280         *===========================================*/
5281 
5282        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - GetAgingBucketLineId');
5283        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - delid='||l_delinquency_id);
5284 
5285           AGING_DEL(p_api_version              => p_api_version
5286 			              , p_init_msg_list            => p_init_msg_list
5287 			              , p_commit                   => p_commit
5288 			              , p_delinquency_id           => l_delinquency_id
5289 			              , p_dunning_plan_id          => p_dunning_plan_id
5290 			              , p_bucket                   => l_bucket
5291 			              , x_return_status            => x_return_status
5292 			              , x_msg_count                => x_msg_count
5293 			              , x_msg_data                 => x_msg_data
5294 			              , x_aging_bucket_line_id     => l_bucket_line_id);
5295 
5296        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - AgingDel status='||x_return_status);
5297 
5298        IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5299          FND_MESSAGE.Set_Name('IEX', 'IEX_NO_AGINGBUCKETLINE');
5300          FND_MSG_PUB.Add;
5301          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot Get AgingBucketLineId');
5302          GOTO end_loop;
5303        END IF;
5304 
5305        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - lineid='||l_bucket_line_id);
5306        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - EndAgingDel');
5307 
5308        /*==========================================
5309        * Get Template_ID From iex_ag_dn_xref
5310        *===========================================*/
5311 
5312        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - GET Template');
5313 
5314        Open C_Get_TEMPLATE(l_bucket_line_ID, l_score, p_dunning_plan_id);
5315        Fetch C_Get_TEMPLATE into
5316              l_template_id,
5317              l_xdo_template_id,
5318              l_method,
5319              l_callback_flag,
5320              l_callback_days,
5321              l_bucket;
5322 
5323        If ( C_GET_TEMPLATE%NOTFOUND) Then
5324            FND_MESSAGE.Set_Name('IEX', 'IEX_NO_TEMPLATE');
5325            FND_MSG_PUB.Add;
5326            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - template notfound');
5327            Close C_Get_TEMPLATE;
5328            GOTO end_loop;
5329        END IF;
5330 
5331        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - close C_GET_TEMPLATE');
5332        Close C_Get_TEMPLATE;
5333 
5334        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get template_id='||l_template_id);
5335        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get xdo_template_id='||l_xdo_template_id);
5336        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get bucket='||l_bucket);
5337 
5338 
5339       /*===========================================
5340        * Check template
5341        *  in 11.5.11, IEX supports fulfillment and xml publisher.
5342        *  if the current setup for delivery id FFM,
5343        *  then template_id is necessary;
5344        *  if XML, xdo_template_id is necessary;
5345        *===========================================*/
5346 
5347        l_curr_dmethod := IEX_SEND_XML_PVT.getCurrDeliveryMethod;
5348        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - curr d_method='||l_curr_dmethod);
5349        if ( (l_curr_dmethod is null or l_curr_dmethod = '') or
5350             (l_curr_dmethod = 'FFM' and l_template_id is null)  or
5351             (l_curr_dmethod = 'XML' and l_xdo_template_id is null ) ) then
5352             FND_MESSAGE.Set_Name('IEX', 'IEX_NO_TEMPLATE');
5353             FND_MSG_PUB.Add;
5354             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Template notfound');
5355             x_return_status := FND_API.G_RET_STS_ERROR;
5356             GOTO end_loop;
5357        end if;
5358 
5359       /*===========================================
5360        * Check profile before send dunning
5361        *===========================================*/
5362 
5363        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - begin check customer profile');
5364         -- ctlee - check the hz_customer_profiles.dunning_letter
5365        if ( iex_utilities.DunningProfileCheck (
5366                p_party_id => l_party_cust_id
5367                , p_cust_account_id => l_account_id
5368                , p_site_use_id => l_customer_site_use_id
5369                , p_delinquency_id => l_delinquency_id     ) = 'N'
5370           ) then
5371             FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_DUNNING_PROFILE_NO');
5372             FND_MSG_PUB.Add;
5373             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Send dunning in customer profile set to no ');
5374             GOTO end_loop;
5375        end if;
5376 
5377 
5378        -- ctlee - check the hz_customer_profiles_amt min_dunning_invoice_amount and min_dunning_amount
5379        if ( iex_utilities.DunningMinAmountCheck (
5380                 p_cust_account_id => l_account_id
5381                 , p_site_use_id => l_customer_site_use_id)  = 'N'
5382           ) then
5383             FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_DUNNING_AMOUNT_MIN');
5384             FND_MSG_PUB.Add;
5385             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Required min Dunning amount in customer profile ');
5386             GOTO end_loop;
5387        end if;
5388 
5389        WriteLog('iexvdunb:ResendDunn: end check customer profile');
5390 
5391 
5392        /*===========================================
5393         * Send Letter through Fulfillment
5394         *===========================================*/
5395 
5396         WriteLog('iexvdunb.pls:ResendDunn:Send_Ffm');
5397         WriteLog('iexvdunb.pls:ResendDunn:orgid='||l_org_id);
5398         WriteLog('iexvdunb.pls:ResendDunn:partyid='||l_party_cust_id);
5399         WriteLog('iexvdunb.pls:ResendDunn:acctid='||l_account_id);
5400         WriteLog('iexvdunb.pls:ResendDunn:lineid='||l_bucket_line_id);
5401         WriteLog('iexvdunb.pls:ResendDunn:delid='||l_delinquency_id);
5402 
5403         l_bind_rec.key_name := 'party_id';
5404         l_bind_rec.key_type := 'NUMBER';
5405         l_bind_rec.key_value := l_party_cust_id;
5406         l_bind_tbl(1) := l_bind_rec;
5407         l_bind_rec.key_name := 'org_id';
5408         l_bind_rec.key_type := 'NUMBER';
5409         l_bind_rec.key_value := l_org_id;
5410         l_bind_tbl(2) := l_bind_rec;
5411         l_bind_rec.key_name := 'bucket_line_id';
5412         l_bind_rec.key_type := 'NUMBER';
5413         l_bind_rec.key_value := l_bucket_line_id;
5414         l_bind_tbl(3) := l_bind_rec;
5415         l_bind_rec.key_name := 'account_id';
5416         l_bind_rec.key_type := 'NUMBER';
5417         l_bind_rec.key_value := l_account_id;
5418         l_bind_tbl(4) := l_bind_rec;
5419         l_bind_rec.key_name := 'delinquency_id';
5420         l_bind_rec.key_type := 'NUMBER';
5421         l_bind_rec.key_value := l_delinquency_id;
5422         l_bind_tbl(5) := l_bind_rec;
5423         -- added for BILL_TO in 11.5.10
5424         l_bind_rec.key_name := 'customer_site_use_id';
5425         l_bind_rec.key_type := 'NUMBER';
5426         l_bind_rec.key_value := l_customer_site_use_id;
5427         l_bind_tbl(6) := l_bind_rec;
5428 
5429         /**
5430          * in 11.5.11, IEX supports fulfillment and xml publisher.
5431          * it depends on the set up in IEX ADMIN/SETUP.
5432          */
5433 
5434         if (l_curr_dmethod = 'FFM') then
5435 
5436           Send_Fulfillment(
5437             p_api_version              => p_api_version
5438           , p_init_msg_list            => p_init_msg_list
5439           , p_commit                   => p_commit
5440           , p_fulfillment_bind_tbl     => l_bind_tbl
5441           , p_template_id              => l_template_id
5442           , p_method                   => l_method
5443           , p_party_id                 => l_party_cust_id
5444           , x_request_id               => l_request_id
5445           , x_return_status            => x_return_status
5446           , x_msg_count                => x_msg_count
5447           , x_msg_data                 => x_msg_data
5448           , x_contact_destination      => l_contact_destination  -- bug 3955222
5449           , x_contact_party_id         => l_contact_party_id  -- bug 3955222
5450           );
5451 
5452         else
5453           l_running_level := 'DELINQUENCY';
5454           l_object_code := 'IEX_DELINQUENCY';
5455           l_object_id := l_delinquency_id;
5456 
5457           Send_XML(
5458             p_api_version              => p_api_version
5459           , p_init_msg_list            => p_init_msg_list
5460           , p_commit                   => p_commit
5461           , p_resend                   => 'N'
5462           , p_request_id               => null
5463           , p_fulfillment_bind_tbl     => l_bind_tbl
5464           , p_template_id              => l_xdo_template_id
5465           , p_method                   => l_method
5466           , p_party_id                 => l_party_cust_id
5467           , p_level                    => l_running_level
5468           , p_source_id                => l_object_id
5469           , p_object_code              => l_object_code
5470           , p_object_id                => l_object_id
5471           , x_request_id               => l_request_id
5472           , x_return_status            => x_return_status
5473           , x_msg_count                => x_msg_count
5474           , x_msg_data                 => x_msg_data
5475           , x_contact_destination      => l_contact_destination  -- bug 3955222
5476           , x_contact_party_id         => l_contact_party_id  -- bug 3955222
5477           );
5478        end if;
5479 
5480        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - send status='|| x_return_status);
5481 
5482        IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5483          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Sending process failed');
5484          GOTO end_loop;
5485        END IF;
5486 
5487        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - request_id='||l_request_id);
5488        l_ffm_cnt := l_ffm_cnt + 1;
5489 
5490        /*===========================================
5491         * Close OPEN Dunnings for each Del
5492         *===========================================*/
5493 
5494        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CloseDunning');
5495        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - runninglevel=DELINQUENCY');
5496        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - delid='||l_delinquency_id);
5497        l_del_tbl(1).delinquency_id := l_delinquency_id;
5498 
5499        Close_DUNNING(
5500            p_api_version              => p_api_version
5501          , p_init_msg_list            => p_init_msg_list
5502          , p_commit                   => p_commit
5503          , p_delinquencies_tbl        => l_del_tbl
5504          , p_running_level            => 'DELINQUENCY'
5505          , x_return_status            => x_return_status
5506          , x_msg_count                => x_msg_count
5507          , x_msg_data                 => x_msg_data);
5508 
5509        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CloseDunning status='|| x_return_status);
5510 
5511        IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5512           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot Close Dunning');
5513           GOTO end_loop;
5514        END IF;
5515        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - EndCloseDunn');
5516 
5517        /*===========================================
5518         * Create Dunning Record
5519         *===========================================*/
5520 
5521        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Get CallbackDate');
5522        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackFlag='||l_callback_flag);
5523        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackDays='||l_callback_days);
5524 
5525       /*===================================================
5526        * clchang updated 02/13/2003
5527        * callback_days could be null if callback_yn = 'N';
5528        * and if callback_yn = 'N', not get callback_date;
5529        *==================================================*/
5530        IF (l_callback_flag = 'Y') THEN
5531           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackFlag is Y: NOTGetCallbackDate');
5532           Get_Callback_Date( p_init_msg_list          => p_init_msg_list
5533                       , p_callback_days          => l_callback_days
5534                       , x_callback_date          => l_callback_date
5535                       , x_return_status          => x_return_status
5536                       , x_msg_count              => x_msg_count
5537                       , x_msg_data               => x_msg_data);
5538 
5539           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - GetCallbackDate status='|| x_return_status);
5540           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackDate='||l_callback_date);
5541 
5542           IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5543             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Not Get CallbackDate');
5544             GOTO end_loop;
5545           END IF;
5546 
5547        ELSE
5548           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CallbackFlag is N: NOTGetCallbackDate');
5549        END IF;
5550 
5551 
5552        /* get the current amount_due_remaining and currency_code */
5553        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - GET AMOUNT');
5554 
5555        Open C_AMOUNT(l_delinquency_id);
5556        Fetch C_AMOUNT into
5557            l_amount,
5558            l_curr_code;
5559 
5560        If ( C_AMOUNT%NOTFOUND) Then
5561           FND_MESSAGE.Set_Name('IEX', 'API_MISSING_UPDATE_TARGET');
5562           FND_MESSAGE.Set_Token ('INFO', 'amount', FALSE);
5563           FND_MSG_PUB.Add;
5564           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - amount notfound');
5565        END IF;
5566 
5567        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - close C_AMOUNT');
5568        Close C_AMOUNT;
5569 
5570        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get amount='||l_amount);
5571        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get currcode='||l_curr_code);
5572        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CreateDunning');
5573        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - delid='||l_delinquency_id);
5574        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - object_id='||l_delinquency_id);
5575 
5576        l_dunning_rec.delinquency_id := l_delinquency_id;
5577        l_dunning_rec.callback_yn := l_callback_flag;
5578        l_dunning_rec.callback_date := l_callback_date;
5579        l_dunning_rec.status := 'OPEN';
5580        l_dunning_rec.dunning_method:= l_method;
5581        if (l_curr_dmethod = 'FFM') then
5582          l_dunning_rec.template_id:= l_template_id;
5583          l_dunning_rec.ffm_request_id := l_request_id;
5584        else
5585          l_dunning_rec.xml_template_id:= l_xdo_template_id;
5586          l_dunning_rec.xml_request_id := l_request_id;
5587        end if;
5588        l_dunning_rec.campaign_sched_id := l_campaign_sched_id;
5589        l_dunning_rec.dunning_object_id := l_delinquency_id;
5590        l_dunning_rec.dunning_level := 'DELINQUENCY';
5591        l_dunning_rec.amount_due_remaining := l_amount;
5592        l_dunning_rec.currency_code := l_curr_code;
5593        l_dunning_rec.object_type := 'IEX_DELINQUENCY';
5594        l_dunning_rec.object_id := l_delinquency_id;
5595        l_dunning_rec.dunning_plan_id := p_dunning_plan_id;
5596        l_dunning_rec.contact_destination := l_contact_destination;  -- bug 3955222
5597        l_dunning_rec.contact_party_id := l_contact_party_id;  -- bug 3955222
5598 
5599        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - InsertRow');
5600 
5601        CREATE_DUNNING(
5602          p_api_version              => p_api_version
5603        , p_init_msg_list            => p_init_msg_list
5604        , p_commit                   => p_commit
5605        , p_dunning_rec              => l_dunning_rec
5606        , x_dunning_id               => l_dunning_id
5607        , x_return_status            => x_return_status
5608        , x_msg_count                => x_msg_count
5609        , x_msg_data                 => x_msg_data);
5610 
5611        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CreateDunning status='|| x_return_status);
5612 
5613        IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5614          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot Create Dunning');
5615          GOTO end_loop;
5616        END IF;
5617 
5618        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - DunningId='||l_dunning_id);
5619        l_dunn_cnt := l_dunn_cnt + 1;
5620 
5621 
5622        /*===========================================
5623         * Update Delinquency
5624         * Set DUNN_YN = 'N'
5625         *===========================================*/
5626 
5627          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UpdateDel');
5628 
5629          nIdx := nIdx + 1;
5630          del_tbl(nIdx) := l_delinquency_id;
5631 
5632 
5633        <<end_loop>>
5634        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end_loop');
5635        NULL;
5636 
5637      END IF;
5638 
5639      WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Close C_GET_DEL');
5640 
5641      Close C_Get_DEL;
5642 
5643      x_request_id := l_request_id;
5644 
5645      WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ==========Summarty==========');
5646      WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - SendFFM Cnt='||l_ffm_cnt);
5647      WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CreateDunn Cnt='||l_dunn_cnt);
5648      WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - request_id='||x_request_id);
5649      WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ==========End Summarty==========');
5650 
5651       --
5652       -- End of API body
5653       --
5654 
5655       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - return status='||x_return_status);
5656       IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5657           RAISE FND_API.G_EXC_ERROR;
5658       elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR then
5659           raise FND_API.G_EXC_UNEXPECTED_ERROR;
5660       END IF;
5661 
5662       -- Standard check for p_commit
5663       IF FND_API.to_Boolean( p_commit )
5664       THEN
5665           COMMIT WORK;
5666       END IF;
5667 
5668       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - return status='||x_return_status);
5669       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - END');
5670 
5671       FND_MSG_PUB.Count_And_Get
5672       (  p_count          =>   x_msg_count,
5673          p_data           =>   x_msg_data );
5674 
5675 
5676       EXCEPTION
5677           WHEN FND_API.G_EXC_ERROR THEN
5678               x_return_status := FND_API.G_RET_STS_ERROR;
5679               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception');
5680               FND_MSG_PUB.Count_And_Get
5681               (  p_count          =>   x_msg_count,
5682                  p_data           =>   x_msg_data );
5683               errmsg := SQLERRM;
5684               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||errmsg);
5685               ROLLBACK TO Resend_DUNNING_PVT;
5686 
5687           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5688               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5689               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExc Exception');
5690               FND_MSG_PUB.Count_And_Get
5691               (  p_count          =>   x_msg_count,
5692                  p_data           =>   x_msg_data );
5693               errmsg := SQLERRM;
5694               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||errmsg);
5695               ROLLBACK TO Resend_DUNNING_PVT;
5696 
5697           WHEN OTHERS THEN
5698               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5699               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception');
5700               FND_MSG_PUB.Count_And_Get
5701               (  p_count          =>   x_msg_count,
5702                  p_data           =>   x_msg_data );
5703               errmsg := SQLERRM;
5704               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||errmsg);
5705               ROLLBACK TO Resend_DUNNING_PVT;
5706 
5707 END Resend_Dunning;
5708 
5709 /* clchang added (for 11.5.9)
5710    no iex aging in 11.5.9;
5711    in send_dunning, aging_bucket_line_id is not from iex_delinquencies;
5712    we need to get by ourselves;
5713 
5714    updated for 11.5.11  - 11/22/04
5715    dunning support aging in 11.5.11 at all levels.
5716    added 2 more parameters: p_object_code, and p_object_id.
5717    so this procedure can age not just del level, but all levels.
5718  */
5719 Procedure AGING_DEL(
5720             p_api_version             IN NUMBER := 1.0,
5721             p_init_msg_list           IN VARCHAR2 ,
5722             p_commit                  IN VARCHAR2 ,
5723             p_delinquency_id          IN NUMBER,
5724             p_dunning_plan_id         in number,
5725             p_bucket                  IN VARCHAR2,
5726             p_object_code             IN VARCHAR2,
5727             p_object_id               IN NUMBER,
5728             x_return_status           OUT NOCOPY VARCHAR2,
5729             x_msg_count               OUT NOCOPY NUMBER,
5730             x_msg_data                OUT NOCOPY VARCHAR2,
5731             x_AGING_Bucket_line_ID    OUT NOCOPY NUMBER)
5732 
5733 IS
5734 -- begin bug 4914799, add max function and check del status
5735     CURSOR C_GET_PARTY_DAYS (in_party_id number) IS
5736       SELECT max(sysdate - ar.due_date) days
5737         FROM iex_delinquencies del,
5738              ar_payment_schedules ar
5739        WHERE del.party_cust_id = in_party_id
5740          AND del.payment_schedule_id = ar.payment_schedule_id
5741          and del.status in ('DELINQUENT', 'PREDELINQUENT');
5742     --
5743     CURSOR C_GET_ACCT_DAYS (in_acct_id number) IS
5744       SELECT max(sysdate - ar.due_date) days
5745         FROM iex_delinquencies del,
5746              ar_payment_schedules ar
5747        WHERE del.cust_account_id = in_acct_id
5748          AND del.payment_schedule_id = ar.payment_schedule_id
5749          and del.status in ('DELINQUENT', 'PREDELINQUENT');
5750     --
5751     CURSOR C_GET_SITE_DAYS (in_site_id number) IS
5752       SELECT max(sysdate - ar.due_date) days
5753         FROM iex_delinquencies del,
5754              ar_payment_schedules ar
5755        WHERE del.customer_site_use_id = in_site_id
5756          AND del.payment_schedule_id = ar.payment_schedule_id
5757          and del.status in ('DELINQUENT', 'PREDELINQUENT');
5758     --
5759     CURSOR C_GET_DAYS (IN_DEL_ID NUMBER) IS
5760       SELECT (sysdate - ar.due_date) days
5761         FROM iex_delinquencies del,
5762              ar_payment_schedules ar
5763        WHERE delinquency_ID = in_DEL_ID
5764          AND del.payment_schedule_id = ar.payment_schedule_id
5765          and del.status in ('DELINQUENT', 'PREDELINQUENT');
5766 -- end bug 4914799, add max function and check del status
5767     --
5768 
5769     CURSOR C_GET_BUCKET_LINE (IN_DAYS NUMBER, p_dunning_plan_id  number) IS
5770        SELECT l.aging_bucket_line_id
5771          FROM ar_aging_bucket_lines l,
5772               ar_aging_buckets b,
5773               iex_dunning_plans_vl d
5774          WHERE d.dunning_plan_id = p_dunning_plan_id
5775             and  d.aging_bucket_id = b.aging_bucket_id
5776             and  b.aging_bucket_id = l.aging_bucket_id
5777             AND round(IN_DAYS) between l.days_start and l.days_to
5778             and exists (select 1 from iex_ag_dn_xref x
5779                          where d.dunning_plan_id = x.dunning_plan_id
5780                          and d.aging_bucket_id = x.aging_bucket_id
5781                          and x.aging_bucket_line_id = l.aging_bucket_line_id);
5782     --
5783 l_api_name          CONSTANT VARCHAR2(30) := 'AGING_DEL';
5784 l_api_version       NUMBER := 1.0;
5785 l_commit            VARCHAR2(5) ;
5786 --
5787 l_days              NUMBER;
5788 l_bucket_line_id    NUMBER;
5789 --
5790 l_msg_count         NUMBER;
5791 l_msg_data          VARCHAR2(1000);
5792 l_return_status     VARCHAR2(1000);
5793 --
5794 nIdx                NUMBER := 0;
5795 
5796 errmsg                        VARCHAR2(32767);
5797 
5798 BEGIN
5799      -- Standard Start of API savepoint
5800      SAVEPOINT AGING_DEL_PVT;
5801 
5802      l_commit  := FND_API.G_TRUE;
5803 
5804     -- Initialize message list IF p_init_msg_list is set to TRUE.
5805     IF FND_API.to_Boolean( p_init_msg_list )
5806     THEN
5807         FND_MSG_PUB.initialize;
5808     END IF;
5809 
5810     -- Initialize API return status to SUCCESS
5811     l_return_status := FND_API.G_RET_STS_SUCCESS;
5812 
5813     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - p_object_code='||p_object_code);
5814     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - p_object_id='||p_object_id);
5815 
5816     IF ( p_object_Code = 'PARTY') then
5817       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Party Level');
5818       Open C_Get_PARTY_DAYS(p_object_id);
5819       Fetch C_Get_PARTY_DAYS into l_days;
5820       If ( C_GET_PARTY_DAYS%NOTFOUND) Then
5821             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - PaymentSchedId NotFound');
5822             FND_MESSAGE.Set_Name('IEX', 'IEX_NO_PAYMENTSCHEDULE');
5823             FND_MSG_PUB.Add;
5824             l_return_status := FND_API.G_RET_STS_ERROR;
5825             Close C_Get_PARTY_DAYS;
5826             GOTO end_get_line;
5827       end if;
5828       --
5829     ELSIF ( p_object_Code = 'IEX_ACCOUNT') then
5830       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Acct Level');
5831       Open C_Get_ACCT_DAYS(p_object_id);
5832       Fetch C_Get_ACCT_DAYS into l_days;
5833       If ( C_GET_ACCT_DAYS%NOTFOUND) Then
5834             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - PaymentSchedId NotFound');
5835             FND_MESSAGE.Set_Name('IEX', 'IEX_NO_PAYMENTSCHEDULE');
5836             FND_MSG_PUB.Add;
5837             l_return_status := FND_API.G_RET_STS_ERROR;
5838             Close C_Get_ACCT_DAYS;
5839             GOTO end_get_line;
5840       end if;
5841       --
5842     ELSIF ( p_object_Code = 'IEX_BILLTO') then
5843       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - BillTO Level');
5844       Open C_Get_SITE_DAYS(p_object_id);
5845       Fetch C_Get_SITE_DAYS into l_days;
5846       If ( C_GET_SITE_DAYS%NOTFOUND) Then
5847             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - PaymentSchedId NotFound');
5848             FND_MESSAGE.Set_Name('IEX', 'IEX_NO_PAYMENTSCHEDULE');
5849             FND_MSG_PUB.Add;
5850             l_return_status := FND_API.G_RET_STS_ERROR;
5851             Close C_Get_SITE_DAYS;
5852             GOTO end_get_line;
5853       end if;
5854       --
5855     ELSE
5856       -- delinquency level
5857 
5858       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - delid='||p_delinquency_id);
5859       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - bucket='||p_bucket);
5860 
5861       Open C_Get_DAYS(p_delinquency_id);
5862       Fetch C_Get_DAYS into l_days;
5863       If ( C_GET_DAYS%NOTFOUND) Then
5864             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - PaymentSchedId NotFound');
5865             FND_MESSAGE.Set_Name('IEX', 'IEX_NO_PAYMENTSCHEDULE');
5866             FND_MSG_PUB.Add;
5867             l_return_status := FND_API.G_RET_STS_ERROR;
5868             Close C_Get_DAYS;
5869             GOTO end_get_line;
5870       end if;
5871       --
5872     END IF;
5873       --
5874            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - days='||l_days);
5875            FND_FILE.PUT_LINE(FND_FILE.LOG, 'l_days = ' || l_days);
5876 
5877            Open C_Get_BUCKET_LINE(l_days, p_dunning_plan_id);
5878            Fetch C_Get_BUCKET_LINE into l_bucket_line_id;
5879            If ( C_GET_BUCKET_LINE%NOTFOUND) Then
5880               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - BucketLine NotFound');
5881               FND_MESSAGE.Set_Name('IEX', 'IEX_NO_BUCKET_LINE');
5882               FND_MSG_PUB.Add;
5883               l_return_status := FND_API.G_RET_STS_ERROR;
5884               Close C_Get_BUCKET_LINE;
5885               GOTO end_get_line;
5886            else
5887               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - bucketlineid='||l_bucket_line_id);
5888            end if;
5889       --
5890 
5891     <<end_get_line>>
5892     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end_get_line');
5893     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - status=' || l_return_status);
5894 
5895     x_return_status := l_return_status;
5896     if (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5897         l_bucket_line_id := 0;
5898     END IF;
5899     x_aging_bucket_line_id := l_bucket_line_id;
5900 
5901     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - return_status:'||x_return_status);
5902     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - lineId:'||x_aging_bucket_line_id);
5903 
5904     FND_MSG_PUB.Count_And_Get
5905     (  p_count          =>   x_msg_count,
5906        p_data           =>   x_msg_data );
5907 
5908     EXCEPTION
5909           WHEN FND_API.G_EXC_ERROR THEN
5910               x_return_status := FND_API.G_RET_STS_ERROR;
5911               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception');
5912               errmsg := SQLERRM;
5913               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||errmsg);
5914               ROLLBACK TO AGING_DEL_PVT;
5915               FND_MSG_PUB.Count_And_Get
5916               (  p_count          =>   x_msg_count,
5917                  p_data           =>   x_msg_data);
5918 
5919           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5920 		   x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5921               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExc Exception');
5922               errmsg := SQLERRM;
5923               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||errmsg);
5924               ROLLBACK TO AGING_DEL_PVT;
5925               FND_MSG_PUB.Count_And_Get
5926               (  p_count          =>   x_msg_count,
5927                  p_data           =>   x_msg_data);
5928 
5929           WHEN OTHERS THEN
5930               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5931               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception');
5932               errmsg := SQLERRM;
5933               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||errmsg);
5934               ROLLBACK TO AGING_DEL_PVT;
5935               FND_MSG_PUB.Count_And_Get
5936               (  p_count          =>   x_msg_count,
5937                  p_data           =>   x_msg_data);
5938 
5939 END AGING_DEL;
5940 
5941 /* begin raverma 10222001
5942    changed spec to work with delinquencies, receipts, promises, disputes,
5943   added:
5944   p_key_name (should be like 'delinquency_id')
5945   p_key_id   (should be like 10001)
5946  */
5947 Procedure Call_FFM(
5948             p_api_version             IN NUMBER := 1.0,
5949             p_init_msg_list           IN VARCHAR2 ,
5950             p_commit                  IN VARCHAR2 ,
5951             p_key_name                IN VARCHAR2,
5952             p_key_id                  IN NUMBER,
5953             p_template_id             IN NUMBER,
5954             p_method                  IN VARCHAR2,
5955             p_party_id                IN NUMBER,
5956             x_return_status           OUT NOCOPY VARCHAR2,
5957             x_msg_count               OUT NOCOPY NUMBER,
5958             x_msg_data                OUT NOCOPY VARCHAR2,
5959             x_REQUEST_ID              OUT NOCOPY NUMBER)
5960 
5961 IS
5962     CURSOR C_GET_CONTENTS (IN_TEMPLATE_ID NUMBER) IS
5963       SELECT  --content_NUMBER,
5964              upper(substr(content_name,instr(content_name,'.')+1,length(content_name)-instr(content_name,'.'))) DocType,
5965              mes_doc_ID
5966         FROM JTF_FM_TEMPLATE_CONTENTS
5967        WHERE template_ID = in_template_ID
5968          AND nvl(upper(f_deletedflag),'0') <>'D'
5969        --bug 3090268
5970        --ORDER BY content_number;
5971        ORDER BY creation_date;
5972     --
5973     CURSOR C_GET_ORG_EMAIL (IN_PARTY_ID NUMBER) IS
5974       SELECT email_address
5975         FROM HZ_PARTIES
5976        WHERE party_ID = in_party_ID;
5977     --
5978     CURSOR C_GET_CONTACT_EMAIL (IN_PARTY_ID NUMBER) IS
5979       SELECT email_address
5980         FROM HZ_CONTACT_POINTS
5981        WHERE owner_table_ID = in_party_ID
5982          AND Contact_point_type = 'EMAIL'
5983          AND primary_flag = 'Y';
5984     --
5985     CURSOR C_GET_CONTENT_TYPE (IN_MES_DOC_ID NUMBER) IS
5986       SELECT mes.query_id
5987         FROM jtf_FM_query_mes mes,
5988              jtf_FM_query q
5989        WHERE mes.MES_DOC_ID = in_mes_doc_id
5990          AND mes.query_id = q.query_id;
5991     --
5992 l_api_name          CONSTANT VARCHAR2(30) := 'Call_FFM';
5993 l_api_version       NUMBER := 1.0;
5994 l_commit            VARCHAR2(5) ;
5995 --
5996 l_Content_tbl       IEX_SEND_FFM_PVT.CONTENT_TBL_TYPE;
5997 l_Content_rec       IEX_SEND_FFM_PVT.CONTENT_REC_TYPE;
5998 l_content_id        NUMBER;
5999 l_doc_type          VARCHAR2(50);
6000 l_bind_var          JTF_FM_REQUEST_GRP.G_VARCHAR_TBL_TYPE;
6001 l_bind_var_type     JTF_FM_REQUEST_GRP.G_VARCHAR_TBL_TYPE;
6002 l_bind_val          JTF_FM_REQUEST_GRP.G_VARCHAR_TBL_TYPE;
6003 --
6004 l_content_nm        VARCHAR2(100);
6005 l_party_id          NUMBER;
6006 l_user_id           NUMBER;
6007 l_server_id         NUMBER;
6008 l_request_id        NUMBER;
6009 l_subject           VARCHAR2(100);
6010 --
6011 l_msg_count         NUMBER;
6012 l_msg_data          VARCHAR2(1000);
6013 l_return_status     VARCHAR2(1000);
6014 --
6015 l_content_xml1      VARCHAR2(1000);
6016 l_content_xml       VARCHAR2(10000);
6017 --
6018 l_email             VARCHAR2(2000);
6019 l_printer       VARCHAR2(2000);
6020 l_request_type      VARCHAR2(10);
6021 l_query_id          NUMBER;
6022 G_MISS_NUM          CONSTANT NUMBER := 9.99E125;
6023 nIdx                NUMBER := 0;
6024 
6025 errmsg                        VARCHAR2(32767);
6026 
6027 BEGIN
6028 
6029     SAVEPOINT CALL_FFM_PVT;
6030 
6031     IF FND_API.to_Boolean( p_init_msg_list )
6032     THEN
6033         FND_MSG_PUB.initialize;
6034     END IF;
6035 
6036     l_return_status := FND_API.G_RET_STS_SUCCESS;
6037 
6038     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - start');
6039     if (p_template_id is null) then
6040         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No TEMPLATE_ID');
6041         FND_MESSAGE.Set_Name('IEX', 'API_FAIL_SEND_FFM');
6042         FND_MESSAGE.Set_Token ('INFO', 'No Template_ID');
6043         FND_MSG_PUB.Add;
6044         l_return_status := FND_API.G_RET_STS_ERROR;
6045         GOTO end_call_ffm;
6046     end if;
6047     if (p_party_id is null) then
6048         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No PARTY_ID');
6049         FND_MESSAGE.Set_Name('IEX', 'API_FAIL_SEND_FFM');
6050         FND_MESSAGE.Set_Token ('INFO', 'No Party_Id');
6051         FND_MSG_PUB.Add;
6052         l_return_status := FND_API.G_RET_STS_ERROR;
6053         GOTO end_call_ffm;
6054     end if;
6055     if (p_method is null) then
6056         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No METHOD');
6057         FND_MESSAGE.Set_Name('IEX', 'API_FAIL_SEND_FFM');
6058         FND_MESSAGE.Set_Token ('INFO', 'No Method');
6059         FND_MSG_PUB.Add;
6060         l_return_status := FND_API.G_RET_STS_ERROR;
6061         GOTO end_call_ffm;
6062     end if;
6063 
6064     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - TEMPLATE_ID='||p_template_id);
6065     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - PARTY_ID='||p_party_id);
6066     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - METHOD='||p_method);
6067 
6068    /*=======================================
6069     * get the primary contact email first;
6070     * if not found, get org email address;
6071     =======================================*/
6072     -- clchang added 06/13/2002 for bug 2344867 (FM support EMAIL and PRINT)
6073     if (upper(p_method) = 'EMAIL') then
6074     --
6075       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get Email');
6076 
6077 
6078       Open C_Get_CONTACT_EMAIL(p_party_id);
6079       Fetch C_Get_CONTACT_EMAIL into l_email;
6080       If ( C_GET_CONTACT_EMAIL%NOTFOUND) Then
6081          Open C_Get_ORG_EMAIL(p_party_id);
6082          Fetch C_Get_ORG_EMAIL into l_email;
6083          If ( C_GET_ORG_EMAIL%NOTFOUND) Then
6084             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Customer NotFound');
6085             FND_MESSAGE.Set_Name('IEX', 'API_FAIL_SEND_FFM');
6086             FND_MESSAGE.Set_Token ('INFO', 'Customer NotFound');
6087             FND_MSG_PUB.Add;
6088             l_return_status := FND_API.G_RET_STS_ERROR;
6089             Close C_Get_ORG_EMAIL;
6090             Close C_Get_CONTACT_EMAIL;
6091             GOTO end_call_ffm;
6092          end if;
6093          --
6094          If ( l_email is null ) Then
6095             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No Email');
6096             FND_MESSAGE.Set_Name('IEX', 'API_FAIL_SEND_FFM');
6097             FND_MESSAGE.Set_Token ('INFO', 'No Email_Address');
6098             FND_MSG_PUB.Add;
6099             l_return_status := FND_API.G_RET_STS_ERROR;
6100             Close C_Get_ORG_EMAIL;
6101             Close C_Get_CONTACT_EMAIL;
6102             GOTO end_call_ffm;
6103          end if;
6104          --
6105          Close C_Get_ORG_EMAIL;
6106       end if;
6107 
6108       If ( l_email is null ) Then
6109          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No Email');
6110          FND_MESSAGE.Set_Name('IEX', 'API_FAIL_SEND_FFM');
6111          FND_MESSAGE.Set_Token ('INFO', 'No Email_Address');
6112          FND_MSG_PUB.Add;
6113          l_return_status := FND_API.G_RET_STS_ERROR;
6114          Close C_Get_CONTACT_EMAIL;
6115          GOTO end_call_ffm;
6116       END if;
6117 
6118       Close C_Get_CONTACT_EMAIL;
6119 
6120     elsif (upper(p_method) = 'PRINTER' or upper(p_method)='PRINT' ) then
6121       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Get Printer');
6122       l_printer := NVL(fnd_profile.value('IEX_FFM_PRINTER'), '');
6123       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Printer:'||l_printer);
6124       if (l_printer = '' or l_printer is null) then
6125          IF (FND_LOG.LEVEL_EVENT >= PG_DEBUG) THEN
6126             IEX_DEBUG_PUB.LogMessage(G_PKG_NAME || ' ' || l_api_name || ' - No Printer');
6127          END IF;
6128          FND_MESSAGE.Set_Name('IEX', 'API_FAIL_SEND_FFM');
6129          FND_MESSAGE.Set_Token ('INFO', 'No Printer');
6130          FND_MSG_PUB.Add;
6131          l_return_status := FND_API.G_RET_STS_ERROR;
6132          GOTO end_call_ffm;
6133       end if;
6134     END IF; -- end of p_method=EMAIL
6135 
6136 
6137     IF (FND_LOG.LEVEL_EVENT >= PG_DEBUG) THEN
6138        IEX_DEBUG_PUB.LogMessage(G_PKG_NAME || ' ' || l_api_name || ' - Get Contents');
6139     END IF;
6140 
6141     Open C_Get_CONTENTS(p_template_id);
6142     LOOP
6143          Fetch C_Get_CONTENTS into l_doc_type, l_content_id;
6144 
6145          If ( C_GET_CONTENTS%NOTFOUND ) Then
6146               if (nIdx = 0) then
6147                   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No Contents');
6148                   FND_MESSAGE.Set_Name('IEX', 'API_FAIL_SEND_FFM');
6149                   FND_MESSAGE.Set_Token ('INFO', 'No Contents for selected template ');
6150                   FND_MSG_PUB.Add;
6151                   l_return_status := FND_API.G_RET_STS_ERROR;
6152               end if;
6153               exit;
6154          else
6155               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Content_Id='||l_content_id);
6156 
6157               IF (FND_LOG.LEVEL_EVENT >= PG_DEBUG) THEN
6158                  IEX_DEBUG_PUB.LogMessage(G_PKG_NAME || ' ' || l_api_name || ' - Get Content Type');
6159               END IF;
6160 
6161               nIdx := nIdx + 1;
6162 
6163               Open C_Get_CONTENT_TYPE(l_content_id);
6164               Fetch C_Get_CONTENT_TYPE into l_query_id;
6165               If ( C_GET_CONTENT_TYPE%NOTFOUND) Then
6166                   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Content Type:DATA');
6167                   l_request_type := 'DATA';
6168               else   -- l_query_id is not null
6169                   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Content Type:QUERY');
6170 
6171 
6172                   l_request_type := 'QUERY';
6173               end if;
6174               Close C_Get_CONTENT_TYPE;
6175 
6176               l_content_rec.content_id :=  l_content_id;
6177               l_content_rec.request_type := l_request_type; -- 'DATA' or 'QUERY'
6178               l_content_rec.media_type := p_method;         --'EMAIL', 'FAX'
6179               l_content_rec.document_type := l_doc_type; --'HTML';  -- 'WORD';
6180               l_content_rec.user_note := ' ';
6181               l_content_rec.email  := l_email;
6182               l_content_rec.printer  := l_printer;
6183               l_content_rec.file_path := NULL;
6184               l_content_rec.fax  := '9999999999';
6185               ------------------------------------
6186 
6187               -- raverma 10222001 change this to work for any NUMBER id passed
6188               l_bind_var(nIdx)      := p_key_name;
6189               l_bind_var_type(nIdx) := 'NUMBER'; -- 'VARCHAR2'
6190               l_bind_val(nIdx)      := p_key_id;
6191               l_content_tbl(nIdx) := l_content_rec;
6192 
6193          end if;
6194 
6195     END LOOP;
6196 
6197     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Close C_GET_CONTENTS');
6198     Close C_Get_CONTENTS;
6199     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - TotalContents='||nIdx);
6200     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CurrUser='||FND_GLOBAL.USER_ID);
6201     l_content_nm := nIdx; -- Num of Contents you want send out NOCOPY
6202     l_user_id    := FND_GLOBAL.USER_ID; --1001247; -- IEXTEST
6203     l_server_id  := NULL;  -- Using Default Server
6204     l_subject    := NVL(FND_PROFILE.value('IEX_FULFILL_SUBJECT'), '');
6205 
6206     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - subject=;||l_subject');
6207     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CAll SEND_FFM');
6208 
6209     IEX_SEND_FFM_PVT.Send_FFM(p_api_version_number  => l_api_version,
6210                               p_init_msg_list       => p_init_msg_list,
6211                               p_commit              => l_commit,
6212                               p_content_nm          => l_content_nm,
6213                               p_user_id             => l_user_id,
6214                               p_server_id           => l_server_id,
6215                               p_party_id            => p_party_id,
6216                               p_subject             => l_subject,
6217                               p_content_tbl         => l_content_tbl,
6218                               p_bind_var            => l_bind_var,
6219                               p_bind_val            => l_bind_val,
6220                               p_bind_var_type       => l_bind_var_type,
6221                               x_request_id          => l_request_id,
6222                               x_return_status       => l_return_status,
6223                               x_msg_count           => l_msg_count,
6224                               x_msg_data            => l_msg_data);
6225 
6226     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - after send_ffm:'||l_return_status);
6227 
6228     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6229         l_return_status := FND_API.G_RET_STS_ERROR;
6230 	   x_msg_count := l_msg_count;
6231 	   x_msg_data := l_msg_data;
6232         --handle error_msg in main procedure (send_dunning)
6233     END IF;
6234 
6235     <<end_call_ffm>>
6236     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end_call_ffm');
6237     x_request_id := l_request_id;
6238     x_return_status := l_return_status;
6239     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - return_status:'||x_return_status);
6240 
6241     FND_MSG_PUB.Count_And_Get
6242     (  p_count          =>   x_msg_count,
6243        p_data           =>   x_msg_data );
6244 
6245     EXCEPTION
6246           WHEN FND_API.G_EXC_ERROR THEN
6247               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM);
6248               ROLLBACK TO CALL_FFM_PVT;
6249               x_return_status := FND_API.G_RET_STS_ERROR;
6250               FND_MSG_PUB.Count_And_Get
6251               (  p_count          =>   x_msg_count,
6252                  p_data           =>   x_msg_data
6253                );
6254 
6255           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6256               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExc Exception');
6257               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM);
6258               ROLLBACK TO CALL_FFM_PVT;
6259               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6260               FND_MSG_PUB.Count_And_Get
6261               (  p_count          =>   x_msg_count,
6262                  p_data           =>   x_msg_data
6263                );
6264 
6265           WHEN OTHERS THEN
6266               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception');
6267               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - error='||SQLERRM);
6268               ROLLBACK TO CALL_FFM_PVT;
6269               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6270               FND_MSG_PUB.Count_And_Get
6271               (  p_count          =>   x_msg_count,
6272                  p_data           =>   x_msg_data
6273                );
6274 
6275 END Call_FFM;
6276 
6277 
6278 
6279 Procedure Get_Callback_Date(
6280             p_init_msg_list           IN VARCHAR2 ,
6281             p_callback_days              IN   NUMBER,
6282             x_callback_date              OUT NOCOPY  DATE,
6283             X_Return_Status              OUT NOCOPY  VARCHAR2,
6284             X_Msg_Count                  OUT NOCOPY  NUMBER,
6285             X_Msg_Data                   OUT NOCOPY  VARCHAR2)
6286 IS
6287    l_result         DATE;
6288    l_result2        NUMBER;
6289    v_cursor         NUMBER;
6290    v_create_string  varchar2(1000);
6291    v_numrows        NUMBER;
6292    l_callback_days  NUMBER ;
6293    errmsg           varchar2(32767);
6294 	 l_api_name       varchar2(30);
6295 
6296    -- fixed for sql bind var 05/07/2003
6297    vstr1            varchar2(100) ;
6298    vstr2            varchar2(100)  ;
6299    vstr3            varchar2(100) ;
6300 
6301    vstr4            varchar2(100) ;
6302    vstr5            varchar2(100) ;
6303 
6304 BEGIN
6305 
6306    l_callback_days   := p_callback_days;
6307    l_api_name     := 'get_callback_date';
6308    vstr1          := 'SELECT SYSDATE + ';
6309    vstr2          := ' , TO_NUMBER(TO_CHAR(SYSDATE + ' ;
6310    vstr3          := ' ,' || '''D''' || ')) FROM DUAL ';
6311 
6312    vstr4          := 'SELECT SYSDATE + ' ;
6313    vstr5          := ' FROM DUAL ';
6314 
6315    -- Initialize message list IF p_init_msg_list is set to TRUE.
6316    IF FND_API.to_Boolean( p_init_msg_list )
6317    THEN
6318       FND_MSG_PUB.initialize;
6319    END IF;
6320 
6321    -- Initialize API return status to SUCCESS
6322    x_return_status := FND_API.G_RET_STS_SUCCESS;
6323 
6324    WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Start');
6325 
6326 
6327   v_cursor := DBMS_SQL.OPEN_CURSOR;
6328 
6329   -- clchang updated for sql bind var 05/07/2003
6330   v_create_string := vstr1 || l_callback_days || vstr2 || l_callback_days ||
6331                      vstr3;
6332  /*
6333   v_create_string := 'SELECT SYSDATE + ' || l_callback_days ||
6334                      ', TO_NUMBER(TO_CHAR(SYSDATE + ' || l_callback_days || ',' || '''D''' || ')) FROM DUAL ';
6335  */
6336   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - string='||v_create_string);
6337 
6338   DBMS_SQL.parse(v_cursor, v_create_string, 1);
6339   DBMS_SQL.DEFINE_COLUMN(v_cursor, 1, l_result);
6340   DBMS_SQL.DEFINE_COLUMN(v_cursor, 2, l_result2);
6341 
6342   v_numrows := DBMS_SQL.EXECUTE(v_cursor);
6343   v_numrows := DBMS_SQL.FETCH_ROWS(v_cursor);
6344   DBMS_SQL.COLUMN_VALUE(v_cursor, 1, l_result);
6345   DBMS_SQL.COLUMN_VALUE(v_cursor, 2, l_result2);
6346   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - l_result='||l_result);
6347   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - l_result2='||l_result2);
6348 
6349   DBMS_SQL.CLOSE_CURSOR(v_cursor);
6350 
6351   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Close Cursor');
6352 
6353   -- If Weekend => Monday
6354   -- 6 => Firday
6355   -- 1 => Sunday
6356 
6357   if (l_result2 = 7) then
6358       l_callback_days := l_callback_days + 2;
6359   elsif (l_result2 = 1) then
6360       l_callback_days := l_callback_days + 1;
6361   end if;
6362 
6363   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - callbackdays='||l_callback_days);
6364 
6365   v_cursor := DBMS_SQL.OPEN_CURSOR;
6366   -- clchang updated for sql bind var 05/07/2003
6367   v_create_string := vstr4 || l_callback_days || vstr5;
6368   --v_create_string := 'SELECT SYSDATE + ' || l_callback_days || ' FROM DUAL ';
6369 
6370   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - createstring='||v_create_string);
6371 
6372   DBMS_SQL.parse(v_cursor, v_create_string, 1);
6373   DBMS_SQL.DEFINE_COLUMN(v_cursor, 1, l_result);
6374 
6375   v_numrows := DBMS_SQL.EXECUTE(v_cursor);
6376   v_numrows := DBMS_SQL.FETCH_ROWS(v_cursor);
6377   DBMS_SQL.COLUMN_VALUE(v_cursor, 1, l_result);
6378   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - result='||l_result);
6379 
6380   DBMS_SQL.CLOSE_CURSOR(v_cursor);
6381   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Close Cursor2');
6382 
6383   x_callback_date := l_result;
6384   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - callback_date='||x_callback_date);
6385 
6386   -- Standard call to get message count and IF count is 1, get message info.
6387 
6388  FND_MSG_PUB.Count_And_Get
6389  (  p_count          =>   x_msg_count,
6390     p_data           =>   x_msg_data );
6391 
6392 EXCEPTION
6393   WHEN OTHERS THEN
6394     x_return_status := FND_API.G_RET_STS_ERROR;
6395     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - exception');
6396     errmsg := SQLERRM;
6397     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - errmsg='||errmsg);
6398 
6399 END GET_CALLBACK_DATE;
6400 
6401 
6402 /*
6403 || Overview:  This procedure is an extension of Call_FFM. Call_FFM only allows one bind variable/value
6404 ||            This will allow you to pass in unlimited bind variables in a name/value pair structure
6405 ||
6406 || Parameter: p_FULFILLMENT_BIND_TBL = name/value pairs for bind variables
6407 ||            p_template_id   = fulfillment template
6408 ||            p_method = Fulfillment Type, currently only 'EMAIL' is supported
6409 ||            p_party_id  = pk to hz_parties
6410 ||
6411 || Source Tables:  JTF_FM_TEMPLATE_CONTENTS, HZ_PARTIES, HZ_CONTACT_POINTS,
6412 ||                 jtf_FM_query_mes
6413 ||                 jtf_FM_query
6414 ||
6415 || Target Tables:
6416 ||
6417 || Creation date:       03/07/02 11:36:AM
6418 ||
6419 || Major Modifications: when               who                   what
6420 ||                      03/07/02 11:36:AM  raverma               created
6421 ||                      08/06/02           pjgomes               added parameter p_email
6422 ||                      08/19/02 02:00:PM  pjgomes               Changed default value of p_email to NULL
6423 ||   07/10/03 clchang    p_email could be email/fax
6424 ||   05/06/03 clchang    added 4 parameters p_object_code,p_object_id,
6425 ||                                          p_level,p_source_id
6426 ||                       if p_object_code is null, work as before.
6427 ||                       if null, create dunning after sending ffm;
6428 ||                       p_level could be CUSTOMER,ACCOUNT,BILL_TO,DELINQUENCY.
6429 */
6430 Procedure Send_Fulfillment(p_api_version             IN NUMBER := 1.0,
6431                            p_init_msg_list           IN VARCHAR2 ,
6432                            p_commit                  IN VARCHAR2 ,
6433                            p_FULFILLMENT_BIND_TBL    IN IEX_DUNNING_PVT.FULFILLMENT_BIND_TBL,
6434                            p_template_id             IN NUMBER,
6435                            p_method                  IN VARCHAR2,
6436                            p_party_id                IN NUMBER,
6437                            p_user_id                 IN NUMBER ,
6438                            p_email                   IN VARCHAR2 ,
6439                            p_level                   IN VARCHAR2 ,
6440                            p_source_id               IN NUMBER,
6441                            p_object_code             IN VARCHAR2 ,
6442                            p_object_id               IN NUMBER,
6443                            x_return_status           OUT NOCOPY VARCHAR2,
6444                            x_msg_count               OUT NOCOPY NUMBER,
6445                            x_msg_data                OUT NOCOPY VARCHAR2,
6446                            x_REQUEST_ID              OUT NOCOPY NUMBER,
6447                            x_contact_destination     OUT NOCOPY varchar2,  -- bug 3955222
6448                            x_contact_party_id        OUT NOCOPY NUMBER)  -- bug 3955222
6449 IS
6450     CURSOR C_GET_CONTENTS (IN_TEMPLATE_ID NUMBER) IS
6451       SELECT  --content_NUMBER,
6452          upper(substr(content_name,instr(content_name,'.')+1,length(content_name)-instr(content_name,'.'))) DocType,
6453              mes_doc_ID
6454         FROM JTF_FM_TEMPLATE_CONTENTS
6455        WHERE template_ID = in_template_ID
6456          AND nvl(upper(f_deletedflag),'0') <>'D'
6457        --bug 3090268
6458        --ORDER BY content_number;
6459        ORDER BY creation_date;
6460     --
6461     CURSOR C_GET_ORG_EMAIL (IN_PARTY_ID NUMBER) IS
6462       SELECT email_address
6463         FROM HZ_PARTIES
6464        WHERE party_ID = in_party_ID;
6465     --
6466     CURSOR C_GET_CONTACT_EMAIL (IN_PARTY_ID NUMBER) IS
6467       SELECT email_address
6468         FROM HZ_CONTACT_POINTS
6469        WHERE owner_table_ID = in_party_ID
6470          AND Contact_point_type = 'EMAIL'
6471          AND primary_flag = 'Y';
6472     --
6473     CURSOR C_GET_CONTENT_TYPE (IN_MES_DOC_ID NUMBER) IS
6474       SELECT mes.query_id
6475         FROM jtf_FM_query_mes mes,
6476              jtf_FM_query q
6477        WHERE mes.MES_DOC_ID = in_mes_doc_id
6478          AND mes.query_id = q.query_id;
6479     --
6480 l_api_name          CONSTANT VARCHAR2(30) := 'Send_Fulfillment';
6481 l_api_version       NUMBER := 1.0;
6482 l_commit            VARCHAR2(5) ;
6483 --
6484 l_Content_tbl       IEX_SEND_FFM_PVT.CONTENT_TBL_TYPE;
6485 l_Content_rec       IEX_SEND_FFM_PVT.CONTENT_REC_TYPE;
6486 l_content_id        NUMBER;
6487 l_doc_type          VARCHAR2(50);
6488 l_bind_var          JTF_FM_REQUEST_GRP.G_VARCHAR_TBL_TYPE;
6489 l_bind_var_type     JTF_FM_REQUEST_GRP.G_VARCHAR_TBL_TYPE;
6490 l_bind_val          JTF_FM_REQUEST_GRP.G_VARCHAR_TBL_TYPE;
6491 l_bind_cnt_tbl      NUMBER;
6492 --
6493 l_content_nm    VARCHAR2(100);
6494 l_party_id      NUMBER;
6495 l_user_id       NUMBER;
6496 l_server_id     NUMBER;
6497 l_request_id    NUMBER;
6498 l_subject       VARCHAR2(100);
6499 --
6500 l_msg_count     NUMBER;
6501 l_msg_data      VARCHAR2(1000);
6502 l_return_status VARCHAR2(1000);
6503 --
6504 l_content_xml1  VARCHAR2(1000);
6505 l_content_xml   VARCHAR2(10000);
6506 --
6507 l_email         VARCHAR2(2000);
6508 l_printer       VARCHAR2(2000);
6509 l_fax           VARCHAR2(2000);
6510 l_contact       VARCHAR2(2000);
6511 l_request_type  VARCHAR2(10);
6512 l_query_id      NUMBER;
6513 l_keep_content  NUMBER;
6514 
6515 --G_MISS_NUM     CONSTANT    NUMBER       := 9.99E125;
6516 
6517 nIdx           NUMBER := 0;
6518 nOrgIdx        NUMBER := 0;
6519 errmsg         VARCHAR2(30000);
6520 
6521 nOrgFound      NUMBER := 0;
6522 l_org_id       NUMBER ;
6523 
6524 
6525  l_DUNNING_rec           IEX_DUNNING_PUB.DUNNING_REC_TYPE;
6526  l_dunning_id            NUMBER;
6527 
6528  l_contact_party_id    number;  --#bug 3955222
6529 
6530 BEGIN
6531 
6532      SAVEPOINT Send_Fulfillment_PVT;
6533 
6534     l_commit := p_commit;
6535 
6536     --Bug#4679639 schekuri 20-OCT-2005
6537     --Value of profile ORG_ID shouldn't be used for getting ORG_ID after MOAC implementation
6538     l_org_id:= mo_global.get_current_org_id;
6539 
6540     IF FND_API.to_Boolean( p_init_msg_list )
6541     THEN
6542         FND_MSG_PUB.initialize;
6543     END IF;
6544 
6545     l_return_status := FND_API.G_RET_STS_SUCCESS;
6546 
6547     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - SEND_FFM:');
6548     if (p_template_id is null) then
6549         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No TEMPLATE_ID');
6550         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_TEMPLATE');
6551         FND_MSG_PUB.Add;
6552         l_return_status := FND_API.G_RET_STS_ERROR;
6553         GOTO end_call_ffm;
6554     end if;
6555     if (p_party_id is null AND (p_email IS NULL OR p_email = FND_API.G_MISS_CHAR)) then
6556         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No PARTY_ID or EMAIL');
6557         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_PARTY');
6558         FND_MSG_PUB.Add;
6559         l_return_status := FND_API.G_RET_STS_ERROR;
6560         GOTO end_call_ffm;
6561     end if;
6562     if (p_method is null) then
6563         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No METHOD');
6564         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_METHOD');
6565         FND_MSG_PUB.Add;
6566         l_return_status := FND_API.G_RET_STS_ERROR;
6567         GOTO end_call_ffm;
6568     end if;
6569 
6570     --clchang updated 07/18/2003 subject cannot be hardcoded
6571     -- bug 3058620
6572     l_subject    := NVL(FND_PROFILE.value('IEX_FULFILL_SUBJECT'), '');
6573     if (l_subject is null or l_subject = '') then
6574         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No FFM Subject',0);
6575         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_SUBJECT');
6576         FND_MSG_PUB.Add;
6577         l_return_status := FND_API.G_RET_STS_ERROR;
6578         GOTO end_call_ffm;
6579     end if;
6580 
6581     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - TEMPLATE_ID='||p_template_id);
6582     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - PARTY_ID='||p_party_id);
6583     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - EMAIL='||p_email);
6584     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - METHOD='||p_method);
6585     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - SUBJECT='||l_subject);
6586 
6587    /*=======================================
6588     * get the primary contact email first;
6589     * if not found, get org email address;
6590     =======================================*/
6591 
6592     if(p_email IS NULL OR p_email = FND_API.G_MISS_CHAR) THEN
6593 
6594        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Call GetContactInfo');
6595 
6596        GetContactInfo(
6597                    p_api_version              => p_api_version
6598                  , p_init_msg_list            => p_init_msg_list
6599                  , p_commit                   => p_commit
6600                  , p_method                   => p_method
6601                  , p_party_id                 => p_party_id
6602                  , p_dunning_level            => p_level
6603                  , p_cust_site_use_id         => null
6604                  , x_return_status            => l_return_status
6605                  , x_msg_count                => l_msg_count
6606                  , x_msg_data                 => l_msg_data
6607                  , x_contact                  => l_contact
6608                  , x_contact_party_id         => l_contact_party_id  -- bug 3955222
6609        );
6610 
6611       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' -  status='||l_return_status);
6612 
6613       IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6614          l_return_status := FND_API.G_RET_STS_ERROR;
6615          x_msg_count := l_msg_count;
6616          x_msg_data := l_msg_data;
6617          --handle error_msg in main procedure
6618          GOTO end_call_ffm;
6619       END IF;
6620 
6621       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ContactInfo:'||l_contact);
6622 
6623     END IF;
6624 
6625     -- user could pass fax/email by p_email parameter,
6626     -- then dont call GetContactInfo
6627     --
6628     IF (p_method = 'EMAIL') THEN
6629       if(p_email IS NULL OR p_email = FND_API.G_MISS_CHAR) THEN
6630         l_email := l_contact;
6631       else
6632         l_email := p_email;
6633       end if;
6634       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - EMAIL:'||l_email);
6635         l_printer := 'DEVPRT';
6636         l_fax := '99999999';
6637     --
6638     ELSIF (p_method = 'PRINTER' or p_method = 'PRINT') THEN
6639       -- p_email could be fax or email
6640       if(p_email IS NULL OR p_email = FND_API.G_MISS_CHAR) THEN
6641         l_printer := l_contact;
6642       else
6643         l_printer := p_email;
6644       end if;
6645       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - PRINTER:'||l_printer);
6646         l_email := '';
6647         l_fax := '99999999';
6648     --
6649     ELSIF (p_method = 'FAX') THEN
6650       -- clchang 07/09/03 updated
6651       -- p_email could be fax or email
6652       if(p_email IS NULL OR p_email = FND_API.G_MISS_CHAR) THEN
6653         l_fax := l_contact;
6654       else
6655         l_fax := p_email;
6656       end if;
6657       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - FAX:'||l_fax);
6658         --l_fax := l_contact;
6659         l_printer := 'DEVPRT';
6660         l_email := '';
6661     END IF;
6662 
6663 
6664 
6665     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - template_id=' || p_template_id);
6666 
6667     Open C_Get_CONTENTS(p_template_id);
6668     LOOP
6669          Fetch C_Get_CONTENTS into l_doc_type, l_content_id;
6670 
6671          If ( C_GET_CONTENTS%NOTFOUND ) Then
6672               if (nIdx = 0) then
6673                   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Contents NotFound');
6674                   FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_FFMCONTENT');
6675                   FND_MSG_PUB.Add;
6676                   l_return_status := FND_API.G_RET_STS_ERROR;
6677               end if;
6678               GOTO end_call_ffm;
6679               --exit;
6680          else
6681               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Content_id='||l_content_id);
6682               nIdx := nIdx + 1;
6683               l_keep_content := 1;
6684               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - nIdx='||nIdx);
6685 
6686               Open C_Get_CONTENT_TYPE(l_content_id);
6687               Fetch C_Get_CONTENT_TYPE into l_query_id;
6688               If ( C_GET_CONTENT_TYPE%NOTFOUND) Then
6689                   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ContentType=DATA');
6690                   l_request_type := 'DATA';
6691 
6692               else   -- l_query_id is not null
6693                   WriteLog('iexvdunb.pls:SEND_FFM:ContentType=QUERY');
6694                  /*=================================================================
6695                   * the following part is special for IEXFmAcctDelQuery and IEXFmAcctPreDelQuery;
6696                   * if IEXFmAcctDelQuery (query_id 2002),
6697                   *    if no rows selected based on the bind data,
6698                   *    skip IEXFmAcctDel.htm content;
6699                   * if IEXFmAcctPreDelQuery (query_id 2003),
6700                   *    if no rows selected based on the bind data,
6701                   *    skip IEXFmPart2.htm and IEXFmAcctPreDel.htm contents;
6702                   * the reason we handle it is because
6703                   *    FFM engine set status FAILURE if no rows selected for one content.
6704                   *    then our template will be FAILURE
6705                   *    just because one content has no table data;
6706                   *===========================================================*/
6707 
6708                   --check skip or not only if our sample doc and sample queries
6709                   --if (l_query_id = 2002 or l_query_id = 2003) then
6710                   if ( (l_content_id = 2002 and l_query_id = 2002 ) or
6711                        (l_content_id = 2004 and l_query_id = 2003 ))
6712                   then
6713                       CHK_QUERY_DATA( l_query_id, p_FULFILLMENT_BIND_TBL, l_keep_content);
6714                       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - l_keep_content='||l_keep_content);
6715                       if (l_keep_content = 0 ) then
6716                           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - skip this content');
6717                           Close C_Get_CONTENT_TYPE;
6718                           GOTO end_content;
6719                       end if;
6720                   end if;
6721                   -- end of checking
6722 
6723                   l_request_type := 'QUERY';
6724               end if;
6725               Close C_Get_CONTENT_TYPE;
6726 
6727               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CLOSE ContentType');
6728 
6729               l_content_rec.content_id    := l_content_id;
6730               l_content_rec.request_type  := l_request_type; -- 'DATA' or 'QUERY'
6731               l_content_rec.media_type    := p_method;         --'EMAIL', 'FAX'
6732               l_content_rec.document_type := l_doc_type; --'HTML';  -- 'WORD';
6733               l_content_rec.user_note     := ' ';
6734               l_content_rec.email         := l_email;
6735               l_content_rec.printer       := l_printer;
6736               l_content_rec.file_path     := NULL;
6737               l_content_rec.fax           := l_fax;
6738               ------------------------------------
6739 
6740               -- this is dumb because i am assuming all binds for 1 content, not mutliple contents
6741               l_bind_cnt_tbl := p_FULFILLMENT_BIND_TBL.count;
6742               WriteLog('iexvdunb.pls:SEND_FFM:bind_tbl_cnt'||l_bind_cnt_tbl);
6743               nOrgFound := 0;
6744               for j in 1..l_bind_cnt_tbl
6745               loop
6746                 l_bind_var(j)      := p_FULFILLMENT_BIND_TBL(j).Key_name;
6747                 l_bind_var_type(j) := p_FULFILLMENT_BIND_TBL(j).Key_Type;
6748                 l_bind_val(j)      := p_FULFILLMENT_BIND_TBL(j).Key_Value;
6749 
6750                 -- clchang updated 07/08/2003 for bug 3026860
6751                 if (upper(l_bind_var(j)) = 'ORG_ID') then
6752                     nOrgFound := 1;
6753                 end if;
6754                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - bind_var:found org_id? '||nOrgFound);
6755 
6756                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - bind_var='||l_bind_var(j));
6757                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - bind_var_type='||l_bind_var_type(j));
6758                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - bind_val='||l_bind_val(j));
6759               end loop;
6760 
6761               -- clchang updated 07/08/2003 for bug 3026860
6762               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - after loop of bind_var:found org_id? '||nOrgFound);
6763               if (nOrgFound = 0) then
6764                 nOrgIdx := l_bind_cnt_tbl + 1;
6765                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - bind_var:add org_id at index '||nOrgIdx);
6766                 l_bind_var(nOrgIdx)      := 'org_id';
6767                 l_bind_var_type(nOrgIdx) := 'NUMBER';
6768                 l_bind_val(nOrgIdx)      := l_org_id;
6769               end if;
6770 
6771               l_content_tbl(nIdx) := l_content_rec;
6772 
6773               <<end_content>>
6774               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end_content');
6775               if (l_content_id = 2002 and l_keep_content = 0 and l_query_id = 2002 ) then
6776                   nIdx := nIdx - 1;
6777                   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - skip this content');
6778               elsif (l_content_id = 2004 and l_keep_content = 0 and l_query_id = 2003 ) then
6779                   nIdx := nIdx - 2;
6780                   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - skip this content and IEXFmPart2.htm');
6781               end if;
6782          end if;
6783 
6784     END LOOP;
6785 
6786     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - total contents='||nIdx);
6787 
6788     Close C_Get_CONTENTS;
6789 
6790     l_content_nm := nIdx; -- Num of Contents you want send out NOCOPY
6791     if  (p_user_id is null) then
6792          l_user_id    := FND_GLOBAL.USER_ID; --1001247; -- IEXTEST
6793          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - p_user_id is null');
6794     else
6795          l_user_id := p_user_id;
6796     end if;
6797     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - curruser='||l_user_id);
6798 
6799     l_server_id  := NULL;  -- Using Default Server
6800 
6801     --
6802     -- If any errors(like template no contents), dont call send_ffm;
6803     --
6804     IF l_return_status = FND_API.G_RET_STS_SUCCESS THEN
6805 
6806         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Call SEND_FFM');
6807         IEX_SEND_FFM_PVT.Send_FFM(
6808                               p_api_version_number => l_api_version,
6809                               p_init_msg_list   => p_init_msg_list,
6810                               p_commit          => l_commit,
6811                               p_content_nm      => l_content_nm,
6812                               p_user_id         => l_user_id,
6813                               p_server_id       => l_server_id,
6814                               p_party_id        => p_party_id,
6815                               p_subject         => l_subject,
6816                               p_content_tbl     => l_content_tbl,
6817                               p_bind_var        => l_bind_var,
6818                               p_bind_val        => l_bind_val,
6819                               p_bind_var_type   => l_bind_var_type,
6820                               x_request_id      => l_request_id,
6821                               x_return_status   => l_return_status,
6822                               x_msg_count       => l_msg_count,
6823                               x_msg_data        => l_msg_data);
6824 
6825        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - After SEND_FFM:'||l_return_status);
6826 
6827        IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6828          l_return_status := FND_API.G_RET_STS_ERROR;
6829          x_msg_count := l_msg_count;
6830          x_msg_data := l_msg_data;
6831          --handle error_msg in main procedure (send_dunning)
6832          GOTO end_call_ffm;
6833        END IF;
6834 
6835     END IF;
6836     --
6837     -- updated by clchang 05/06/04 for bug 3088968
6838     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - level='||p_level);
6839     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - source_id='||p_source_id);
6840     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - object='||p_object_code);
6841     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - object_id='||p_object_id);
6842     IF( p_object_code is null ) THEN
6843         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - no object');
6844         x_contact_destination := l_contact; -- bug 3955222
6845         x_contact_party_id := l_contact_party_id; -- bug 3955222
6846     ELSIF( p_object_code = 'PARTY' or p_object_code = 'IEX_ACCOUNT' or
6847            p_object_code = 'IEX_BILLTO' or
6848            p_object_code = 'IEX_DELINQUENCY' or
6849            p_object_code = 'IEX_STRATEGY') THEN
6850         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - dunning rec=> dont create rec');
6851         x_contact_destination := l_contact; -- bug 3955222
6852         x_contact_party_id := l_contact_party_id; -- bug 3955222
6853     ELSE
6854         l_dunning_rec.dunning_level := p_level;
6855         l_dunning_rec.dunning_object_id := p_source_id;
6856         l_dunning_rec.callback_yn := 'N';
6857         l_dunning_rec.status := 'OPEN';
6858         l_dunning_rec.dunning_method:= p_method;
6859         l_dunning_rec.template_id:= p_template_id;
6860         l_dunning_rec.ffm_request_id := l_request_id;
6861         l_dunning_rec.object_type := p_object_code;
6862         l_dunning_rec.object_id := p_object_id;
6863         l_dunning_rec.contact_destination := l_contact;   -- bug 3955222
6864         l_dunning_rec.contact_party_id := l_contact_party_id;   -- bug 3955222
6865 
6866         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - InsertRow');
6867         CREATE_DUNNING(
6868                    p_api_version              => p_api_version
6869                  , p_init_msg_list            => p_init_msg_list
6870                  , p_commit                   => p_commit
6871                  , p_dunning_rec              => l_dunning_rec
6872                  , x_dunning_id               => l_dunning_id
6873                  , x_return_status            => x_return_status
6874                  , x_msg_count                => x_msg_count
6875                  , x_msg_data                 => x_msg_data);
6876 
6877         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CreateDunning status='|| x_return_status);
6878         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - dunning_id='||l_dunning_id);
6879 
6880         IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6881            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot Create Dunning');
6882            l_return_status := FND_API.G_RET_STS_ERROR;
6883            x_msg_count := l_msg_count;
6884            x_msg_data := l_msg_data;
6885         END IF;
6886     END IF; -- end of p_object
6887 
6888     <<end_call_ffm>>
6889     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end_call_ffm');
6890     x_request_id := l_request_id;
6891     x_return_status := l_return_status;
6892 
6893     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - return_status:'||x_return_status);
6894 
6895     FND_MSG_PUB.Count_And_Get
6896     (  p_count          =>   x_msg_count,
6897        p_data           =>   x_msg_data );
6898 
6899     EXCEPTION
6900           WHEN FND_API.G_EXC_ERROR THEN
6901                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception');
6902                errmsg := SQLERRM;
6903                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
6904                ROLLBACK TO SEND_FULFILLMENT_PVT;
6905                x_return_status := FND_API.G_RET_STS_ERROR;
6906                FND_MSG_PUB.Count_And_Get
6907                (  p_count          =>   x_msg_count,
6908                   p_data           =>   x_msg_data
6909                );
6910 
6911           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6912                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExc Exception');
6913                errmsg := SQLERRM;
6914                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
6915                ROLLBACK TO SEND_FULFILLMENT_PVT;
6916                x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6917                FND_MSG_PUB.Count_And_Get
6918                (  p_count          =>   x_msg_count,
6919                   p_data           =>   x_msg_data
6920                );
6921 
6922           WHEN OTHERS THEN
6923                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception');
6924                errmsg := SQLERRM;
6925                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
6926                ROLLBACK TO SEND_FULFILLMENT_PVT;
6927                x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6928                FND_MSG_PUB.Count_And_Get
6929                (  p_count          =>   x_msg_count,
6930                   p_data           =>   x_msg_data
6931                );
6932 
6933 END Send_Fulfillment;
6934 
6935 
6936 /*
6937 || Overview:  This procedure is new for 11.5.11. To replace Fulfillment
6938 ||            with XML Publisher.
6939 ||            similar as Send_Fulfillment.
6940 ||
6941 ||   11/08/04 clchang    added for 11.5.11.
6942 ||
6943 */
6944 Procedure Send_XML(p_api_version             IN NUMBER := 1.0,
6945                    p_init_msg_list           IN VARCHAR2 ,
6946                    p_commit                  IN VARCHAR2 ,
6947                    p_resend                  IN VARCHAR2 ,
6948                    p_request_id              IN NUMBER,
6949                    p_FULFILLMENT_BIND_TBL    IN IEX_DUNNING_PVT.FULFILLMENT_BIND_TBL,
6950                    p_template_id             IN NUMBER,
6951                    p_method                  IN VARCHAR2,
6952                    p_party_id                IN NUMBER,
6953                    p_user_id                 IN NUMBER ,
6954                    p_email                   IN VARCHAR2 ,
6955                    p_level                   IN VARCHAR2 ,
6956                    p_source_id               IN NUMBER,
6957                    p_object_code             IN VARCHAR2 ,
6958                    p_object_id               IN NUMBER,
6959                    p_resource_id             IN NUMBER,
6960                    x_return_status           OUT NOCOPY VARCHAR2,
6961                    x_msg_count               OUT NOCOPY NUMBER,
6962                    x_msg_data                OUT NOCOPY VARCHAR2,
6963                    x_REQUEST_ID              OUT NOCOPY NUMBER,
6964                    x_contact_destination     OUT NOCOPY varchar2,  -- bug 3955222
6965                    x_contact_party_id        OUT NOCOPY NUMBER)  -- bug 3955222
6966 
6967 IS
6968     -- new cursor to get tmp query for xml publisher design 11.5.11
6969     CURSOR C_GET_QUERY (IN_TEMPLATE_ID NUMBER, IN_LEVEL VARCHAR2) IS
6970       SELECT q.query_id
6971         FROM iex_query_temp_xref xref,
6972              iex_xml_queries q
6973        WHERE xref.template_id = IN_TEMPLATE_ID
6974          AND xref.query_id = q.query_id
6975          AND q.query_level = IN_LEVEL;
6976     --
6977     -- new cursor to get tmp query for xml publisher design 11.5.11
6978     CURSOR C_GET_RS (IN_USER_ID NUMBER) IS
6979       SELECT j.resource_id
6980         FROM jtf_rs_resource_extns j
6981        WHERE j.user_id = in_user_id;
6982     --
6983     CURSOR C_GET_ORG_EMAIL (IN_PARTY_ID NUMBER) IS
6984       SELECT email_address
6985         FROM HZ_PARTIES
6986        WHERE party_ID = in_party_ID;
6987     --
6988     CURSOR C_GET_CONTACT_EMAIL (IN_PARTY_ID NUMBER) IS
6989       SELECT email_address
6990         FROM HZ_CONTACT_POINTS
6991        WHERE owner_table_ID = in_party_ID
6992          AND Contact_point_type = 'EMAIL'
6993          AND primary_flag = 'Y';
6994 
6995     cursor c_get_contact_point(p_contact_point_id number, p_contact_type varchar2) is
6996     select decode(p_contact_type , 'EMAIL', c.email_address,
6997                                    'PHONE', c.phone_country_code || c.phone_area_code || c.phone_number,
6998                                      'FAX', c.phone_country_code || c.phone_area_code || c.phone_number, null)
6999      from hz_contact_points c
7000     where contact_point_id = p_contact_point_id;
7001 
7002     cursor c_get_resend_data(p_request_id number) is
7003         select QUERY_TEMP_ID, DESTINATION
7004         from IEX_XML_REQUEST_HISTORIES
7005         where XML_REQUEST_ID = p_request_id;
7006 
7007    --Bug5233002. Fix by LKKUMAR on 31-May-2006. Start.
7008    CURSOR C_GET_RES_USER_ID  IS
7009       SELECT j.user_id
7010         FROM jtf_rs_resource_extns j
7011        WHERE j.resource_id = p_resource_id;
7012    l_resource_user_id NUMBER;
7013    --Bug5233002. Fix by LKKUMAR on 31-May-2006. End.
7014 
7015     l_fulfillment_bind_tbl  IEX_DUNNING_PVT.FULFILLMENT_BIND_TBL;
7016     --
7017 		l_api_name          CONSTANT VARCHAR2(30) := 'Send_XML';
7018 		l_api_version       NUMBER := 1.0;
7019 		l_commit            VARCHAR2(5) ;
7020 		--
7021 		--
7022 		l_party_id      NUMBER;
7023 		l_user_id       NUMBER;
7024 		l_server_id     NUMBER;
7025 		l_request_id    NUMBER;
7026 		l_subject       VARCHAR2(100);
7027 		--
7028 		l_msg_count     NUMBER;
7029 		l_msg_data      VARCHAR2(1000);
7030 		l_return_status VARCHAR2(10);
7031 		--
7032 		l_content_xml1  VARCHAR2(1000);
7033 		l_content_xml   VARCHAR2(10000);
7034 		--
7035 		l_email         VARCHAR2(2000);
7036 		l_printer       VARCHAR2(2000);
7037 		l_fax           VARCHAR2(2000);
7038 		l_contact       VARCHAR2(2000);
7039 		l_request_type  VARCHAR2(10);
7040 		l_query_id      NUMBER;
7041 		l_keep_content  NUMBER;
7042 
7043 		nIdx           NUMBER := 0;
7044 		nOrgIdx        NUMBER := 0;
7045 		errmsg         VARCHAR2(30000);
7046 		nOrgFound      NUMBER := 0;
7047 		l_org_id       NUMBER ;
7048 		l_DUNNING_rec           IEX_DUNNING_PUB.DUNNING_REC_TYPE;
7049 		l_dunning_id            NUMBER;
7050 
7051 		l_level        VARCHAR2(20);
7052 		l_temp_level   VARCHAR2(20);
7053 		l_dest         VARCHAR2(2000);
7054 		l_resource_id  NUMBER;
7055 		l_resend       VARCHAR2(5);
7056 		l_contact_party_id number;   --#bug 39555222
7057     l_customer_site_use_id  number;
7058     l_contact_point_id      number;
7059     l_info                  varchar2(500);
7060     l_location_id           number;
7061     l_cust_account_id       number;
7062     l_delinquency_id        number;
7063     Type refCur             is Ref Cursor;
7064     sql_cur                 refCur;
7065     vSQL                    VARCHAR2(2000);
7066     l_contact_id            number;
7067 
7068     l_msg_count1            NUMBER;
7069     l_msg_data1             VARCHAR2(1000);
7070     l_return_status1        VARCHAR2(10);
7071     l_msg                   varchar2(32000);
7072     l_app                   varchar2(50);
7073     l_msg_name              varchar2(30);
7074     l_count                 number;
7075     k                       number;
7076 
7077 BEGIN
7078 
7079     SAVEPOINT Send_XML_PVT;
7080     WriteLog('----------' || l_api_name || '----------');
7081     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Input parameters:');
7082     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - LEVEL = '|| p_level);
7083     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - RESEND = '|| p_resend);
7084     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - TEMPLATE_ID = '|| p_template_id);
7085     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - PARTY_ID = '|| p_party_id);
7086     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - EMAIL = ' ||p_email);
7087     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - METHOD = ' ||p_method);
7088     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - source_id ='||p_source_id);
7089     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - object ='||p_object_code);
7090     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - object_id ='||p_object_id);
7091     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - bind values count = ' || p_fulfillment_bind_tbl.count);
7092 
7093     l_commit := p_commit;
7094 
7095     --Bug#4679639 schekuri 20-OCT-2005
7096     --Value of profile ORG_ID shouldn't be used for getting ORG_ID after MOAC implementation
7097     --l_org_id        := fnd_profile.value('ORG_ID');
7098     l_org_id:= mo_global.get_current_org_id;
7099 
7100     -- clchang added 11/08/04 for 11.5.11
7101     l_level := p_level;
7102     l_resource_id := p_resource_id;
7103     l_resend      := p_resend;
7104     IF (l_level is null) then
7105         l_level := 'CUSTOMER';
7106     END IF;
7107     l_resend := p_resend;
7108     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - level=' || l_level);
7109     IF (l_resend is null) then
7110         l_resend := 'N';
7111     END IF;
7112     -- end clchang added 11/08/04 for 11.5.11
7113 
7114     FND_MSG_PUB.initialize;
7115 
7116     l_return_status := FND_API.G_RET_STS_SUCCESS;
7117 
7118     if (p_object_code is null) then
7119         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No OBJECT_ID',0);
7120         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_OBJECT');
7121         FND_MSG_PUB.Add;
7122         l_return_status := FND_API.G_RET_STS_ERROR;
7123         GOTO end_call_ffm;
7124     end if;
7125 
7126     if (p_template_id is null) then
7127         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No TEMPLATE_ID',0);
7128         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_TEMPLATE');
7129         FND_MSG_PUB.Add;
7130         l_return_status := FND_API.G_RET_STS_ERROR;
7131         GOTO end_call_ffm;
7132     end if;
7133 
7134     if (p_party_id is null AND (p_email IS NULL OR p_email = FND_API.G_MISS_CHAR)) then
7135         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No PARTY_ID or EMAIL',0);
7136         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_PARTY');
7137         FND_MSG_PUB.Add;
7138         l_return_status := FND_API.G_RET_STS_ERROR;
7139         GOTO end_call_ffm;
7140     end if;
7141 
7142     if (p_method is null) then
7143         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No METHOD',0);
7144         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_METHOD');
7145         FND_MSG_PUB.Add;
7146         l_return_status := FND_API.G_RET_STS_ERROR;
7147         GOTO end_call_ffm;
7148     end if;
7149 
7150     --clchang updated 07/18/2003 subject cannot be hardcoded
7151     -- bug 3058620
7152     l_subject    := NVL(FND_PROFILE.value('IEX_FULFILL_SUBJECT'), '');
7153     if (l_subject is null or l_subject = '') then
7154         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No XML Subject');
7155         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_SUBJECT');
7156         FND_MSG_PUB.Add;
7157         l_return_status := FND_API.G_RET_STS_ERROR;
7158         GOTO end_call_ffm;
7159     end if;
7160 
7161     if  (p_user_id is null) then
7162          l_user_id    := FND_GLOBAL.USER_ID;
7163          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - p_user_id is null');
7164     else
7165          l_user_id := p_user_id;
7166     end if;
7167     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - l_user_id = '||l_user_id);
7168 
7169     -- chk resource_id
7170     if (l_resource_id is null) then
7171       Open C_Get_RS(l_user_id);
7172       Fetch C_Get_RS into l_resource_id;
7173       Close C_Get_RS;
7174     end if;
7175     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - resource_id = ' || l_resource_id);
7176 
7177     if (l_resource_id is null) then
7178         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No Resource for user '||l_user_id);
7179         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_RESOURCE');
7180         FND_MSG_PUB.Add;
7181         l_return_status := FND_API.G_RET_STS_ERROR;
7182         GOTO end_call_ffm;
7183     end if;
7184     l_fulfillment_bind_tbl := p_fulfillment_bind_tbl;
7185 
7186     -- for resend=Y look for dunning_id and assign it to PARENT_DUNNING_ID
7187     if l_resend = 'Y' then
7188         for k in 1..l_fulfillment_bind_tbl.count loop
7189             if upper(l_fulfillment_bind_tbl(k).key_name) = 'DUNNING_ID' then
7190                 l_dunning_rec.PARENT_DUNNING_ID := l_fulfillment_bind_tbl(k).key_value;
7191                 exit;
7192             end if;
7193         end loop;
7194     end if;
7195     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - PARENT_DUNNING_ID = ' || l_dunning_rec.PARENT_DUNNING_ID);
7196 
7197     if l_resend = 'Y' and p_request_id is not null then
7198 
7199         Open c_get_resend_data(p_request_id);
7200         Fetch c_get_resend_data into l_query_id, l_dest;
7201         Close c_get_resend_data;
7202 
7203         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - l_query_id = ' || l_query_id);
7204         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - l_dest = ' || l_dest);
7205 
7206         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Call SEND_COPY');
7207 
7208         BEGIN
7209             IEX_SEND_XML_PVT.Send_COPY(p_api_version_number => l_api_version
7210                                   ,p_init_msg_list   => FND_API.G_FALSE
7211                                   ,p_commit          => FND_API.G_TRUE
7212                                   ,p_resend          => l_resend
7213                                   ,p_request_id      => p_request_id
7214                                   ,p_user_id         => l_user_id
7215                                   ,p_party_id        => p_party_id
7216                                   ,p_subject         => l_subject
7217                                   ,p_template_id     => p_template_id
7218                                   ,p_resource_id     => l_resource_id
7219                                   ,p_query_id        => l_query_id
7220                                   ,p_method          => p_method
7221                                   ,p_dest            => l_dest
7222                                   ,p_bind_tbl        => l_fulfillment_bind_tbl
7223                                   ,p_level           => l_level
7224                                   ,p_source_id       => p_source_id
7225                                   ,p_object_type     => p_object_code
7226                                   ,p_object_id       => p_object_id
7227                                   ,x_request_id      => l_request_id
7228                                   ,x_return_status   => l_return_status
7229                                   ,x_msg_count       => l_msg_count
7230                                   ,x_msg_data        => l_msg_data);
7231 
7232             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - After SEND_COPY:'||l_return_status);
7233 
7234            IF l_return_status <> FND_API.G_RET_STS_SUCCESS or l_request_id is null THEN
7235              l_return_status := FND_API.G_RET_STS_ERROR;
7236              x_msg_count := l_msg_count;
7237              x_msg_data := l_msg_data;
7238            END IF;
7239 
7240         EXCEPTION
7241             WHEN OTHERS THEN
7242                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' throws exception');
7243                 l_return_status := FND_API.G_RET_STS_ERROR;
7244                 FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_DELIVERY');
7245                 FND_MSG_PUB.Add;
7246         END;
7247 
7248     else    -- either resending after error or sending for the first time
7249 
7250         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Calling GET_DEFAULT_DUN_DATA...');
7251 
7252         GET_DEFAULT_DUN_DATA(p_api_version => 1.0,
7253                            p_init_msg_list => FND_API.G_TRUE,
7254                            p_commit => FND_API.G_FALSE,
7255                            p_level => l_level,
7256                            p_source_id => p_source_id,
7257                            p_send_method => p_method,
7258                            p_resend => l_resend,
7259                            p_object_code => p_object_code,
7260                            p_object_id => p_object_id,
7261                            p_fulfillment_bind_tbl => l_fulfillment_bind_tbl,
7262                            x_return_status => l_return_status,
7263                            x_msg_count => l_msg_count,
7264                            x_msg_data => l_msg_data);
7265 
7266         WriteLog('---------- continue ' || l_api_name || '----------');
7267         WriteLog(G_PKG_NAME || ' ' || l_api_name || ': l_return_status: ' || l_return_status);
7268         WriteLog(G_PKG_NAME || ' ' || l_api_name || ': l_msg_count: ' || l_msg_count);
7269 
7270         IF l_return_status <> FND_API.G_RET_STS_SUCCESS then
7271             GOTO end_call_ffm;
7272         end if;
7273 
7274         --reviewing default dunning data
7275         for k in 1..l_fulfillment_bind_tbl.count loop
7276             if l_fulfillment_bind_tbl(k).key_name  = 'CONTACT_POINT_ID' then
7277                 l_contact_point_id := l_fulfillment_bind_tbl(k).key_value;
7278             elsif l_fulfillment_bind_tbl(k).key_name  = 'LOCATION_ID' then
7279                 l_location_id := l_fulfillment_bind_tbl(k).key_value;
7280             end if;
7281         end loop;
7282 
7283         -- checking for location
7284         IF l_location_id is null then
7285             WriteLog(G_PKG_NAME || ' ' || l_api_name || ': No location found');
7286             FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_ADDRESS');
7287             FND_MSG_PUB.Add;
7288             l_return_status := FND_API.G_RET_STS_ERROR;
7289             GOTO end_call_ffm;
7290         end if;
7291 
7292         -- setting destination
7293         IF (p_method = 'PRINTER' or p_method = 'PRINT') THEN
7294 
7295 
7296            if(p_email IS NULL OR p_email = FND_API.G_MISS_CHAR) THEN
7297            --Bug5233002. Fix by LKKUMAR on 31-May-2006. Start.
7298            OPEN  C_GET_RES_USER_ID;
7299 				   FETCH C_GET_RES_USER_ID into l_resource_user_id;
7300 				   CLOSE C_GET_RES_USER_ID;
7301            l_printer := NVL(fnd_profile.value_specific(NAME => 'IEX_PRT_IPP_PRINTER_NAME',USER_ID => l_resource_user_id),Null);
7302             --Bug5233002. Fix by LKKUMAR on 31-May-2006. End.
7303 	   IF (l_printer is null) then
7304             WriteLog(G_PKG_NAME || ' ' || l_api_name || ': Setting printer from profile');
7305             l_printer := NVL(fnd_profile.value('IEX_PRT_IPP_PRINTER_NAME'), '');
7306 	   END IF;
7307 
7308                IF (l_printer = '' or l_printer is null)  THEN
7309                    WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No Printer',0);
7310                    FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_PRINTER');
7311                    FND_MSG_PUB.Add;
7312                    l_return_status := FND_API.G_RET_STS_ERROR;
7313                    GOTO end_call_ffm;
7314                 END IF;
7315 
7316             else
7317                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ': Setting printer as passed');
7318                 l_printer := p_email;
7319             end if;
7320 
7321             l_dest := l_printer;
7322             l_email := '';
7323             l_fax := '';
7324             WriteLog(G_PKG_NAME || ' ' || l_api_name || ': Will use printer: ' || l_dest);
7325 
7326         ELSE -- method is not PRINTER
7327 
7328             if(p_email IS NULL OR p_email = FND_API.G_MISS_CHAR) THEN
7329 
7330                 if l_contact_point_id is null then
7331                    if p_method = 'EMAIL' then
7332                        WriteLog(G_PKG_NAME || ' ' || l_api_name || ': No email found');
7333                        FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_EMAIL');
7334                        FND_MSG_PUB.Add;
7335                    ELSIF p_method = 'FAX' THEN
7336                        WriteLog(G_PKG_NAME || ' ' || l_api_name || ': No fax found');
7337                        FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_FAX');
7338                        FND_MSG_PUB.Add;
7339                    end if;
7340 
7341                    l_return_status := FND_API.G_RET_STS_ERROR;
7342                    GOTO end_call_ffm;
7343                 end if;
7344 
7345                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ': Setting destination from defaulting data');
7346                 open c_get_contact_point(l_contact_point_id, p_method);
7347                 fetch c_get_contact_point into l_contact;
7348                 close c_get_contact_point;
7349 
7350                 l_dest := l_contact;
7351             else
7352                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ': Setting destination as passed');
7353                 l_dest := p_email;
7354             end if;
7355 
7356             l_printer := '';
7357             l_fax := '';
7358 
7359             if p_method = 'EMAIL' then
7360                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ': Will use email: ' || l_dest);
7361             ELSIF p_method = 'FAX' THEN
7362                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ': Will use fax: ' || l_dest);
7363             end if;
7364         END IF;
7365         -- for the following object,
7366         -- create dunning with ACCOUNT level, but xml template is CUSTOMER level;
7367         --
7368         l_temp_level := l_level;
7369         IF( p_object_code = 'IEX_REVERSAL' or
7370           p_object_code = 'IEX_ADJUSTMENT' or
7371           p_object_code = 'IEX_PROMISE' or
7372           p_object_code = 'IEX_PAYMENT' or
7373           p_object_code = 'IEX_CNSLD' or
7374           p_object_code = 'IEX_INVOICES' or
7375           p_object_code = 'IEX_DISPUTE' ) then
7376             l_temp_level := 'CUSTOMER';
7377         end if;
7378 
7379         -- get query_id
7380         Open C_Get_QUERY(p_template_id, l_temp_level);
7381         LOOP
7382          Fetch C_Get_QUERY into l_query_id;
7383          If ( C_GET_QUERY%NOTFOUND ) Then
7384               if (nIdx = 0) then
7385                   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Query NotFound');
7386                   FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_QUERY');
7387                   FND_MSG_PUB.Add;
7388                   l_return_status := FND_API.G_RET_STS_ERROR;
7389                   GOTO end_call_ffm;
7390               end if;
7391               exit;
7392          else
7393               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - query_id='||l_query_id);
7394               nIdx := nIdx + 1;
7395          end if;
7396         END LOOP;
7397         --------------------------------------------
7398 
7399         l_server_id  := NULL;  -- Using Default Server
7400         IF l_return_status = FND_API.G_RET_STS_SUCCESS THEN
7401             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Call SEND_COPY');
7402 
7403             BEGIN
7404                 IEX_SEND_XML_PVT.Send_COPY(p_api_version_number => l_api_version
7405                                       ,p_init_msg_list   => FND_API.G_FALSE
7406                                       ,p_commit          => FND_API.G_TRUE
7407                                       ,p_resend          => l_resend
7408                                       ,p_request_id      => p_request_id
7409                                       ,p_user_id         => l_user_id
7410                                       ,p_party_id        => p_party_id
7411                                       ,p_subject         => l_subject
7412                                       ,p_template_id     => p_template_id
7413                                       ,p_resource_id     => l_resource_id
7414                                       ,p_query_id        => l_query_id
7415                                       ,p_method          => p_method
7416                                       ,p_dest            => l_dest
7417                                       ,p_bind_tbl        => l_fulfillment_bind_tbl
7418                                       ,p_level           => l_level
7419                                       ,p_source_id       => p_source_id
7420                                       ,p_object_type     => p_object_code
7421                                       ,p_object_id       => p_object_id
7422                                       ,x_request_id      => l_request_id
7423                                       ,x_return_status   => l_return_status
7424                                       ,x_msg_count       => l_msg_count
7425                                       ,x_msg_data        => l_msg_data);
7426 
7427                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - After SEND_COPY:'||l_return_status);
7428 
7429                IF l_return_status <> FND_API.G_RET_STS_SUCCESS or l_request_id is null THEN
7430                  l_return_status := FND_API.G_RET_STS_ERROR;
7431                  x_msg_count := l_msg_count;
7432                  x_msg_data := l_msg_data;
7433                END IF;
7434 
7435             EXCEPTION
7436                 WHEN OTHERS THEN
7437                     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' throws exception');
7438                     l_return_status := FND_API.G_RET_STS_ERROR;
7439                     FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_DELIVERY');
7440                     FND_MSG_PUB.Add;
7441             END;
7442 
7443         END IF;
7444 
7445     end if;
7446 
7447     <<end_call_ffm>>
7448 
7449     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - l_return_status='||l_return_status);
7450 
7451     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
7452 
7453         l_msg := FND_MSG_PUB.Get(p_msg_index => 1, p_encoded => 'T');
7454         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' -  l_msg: ' || l_msg);
7455         FND_MESSAGE.PARSE_ENCODED(l_msg, l_app, l_msg_name);
7456         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' -  Error name: ' || l_msg_name);
7457 
7458         l_dunning_rec.DELIVERY_STATUS := l_msg_name;
7459 
7460     end if;
7461 
7462     if l_resend = 'N' then
7463 
7464         IF(p_object_code = 'PARTY' or
7465            p_object_code = 'IEX_ACCOUNT' or
7466            p_object_code = 'IEX_BILLTO' or
7467            p_object_code = 'IEX_DELINQUENCY' or
7468            p_object_code = 'IEX_STRATEGY') THEN
7469 
7470             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' -  dunning rec=> dont create rec');
7471 
7472             if l_dunning_rec.DELIVERY_STATUS is not null then
7473 
7474                 for i in 1..l_fulfillment_bind_tbl.count loop
7475                     if l_fulfillment_bind_tbl(i).key_name = 'DUNNING_ID' then
7476                         l_dunning_rec.DUNNING_ID := to_number(l_fulfillment_bind_tbl(i).KEY_VALUE);
7477                         exit;
7478                     end if;
7479                 end loop;
7480 
7481                 if l_dunning_rec.DUNNING_ID is not null then
7482                     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UpdateRow');
7483 
7484                     IEX_DUNNING_PVT.Update_DUNNING(
7485                            p_api_version              => 1.0
7486                          , p_init_msg_list            => FND_API.G_FALSE
7487                          , p_commit                   => FND_API.G_TRUE
7488                          , p_dunning_rec              => l_dunning_rec
7489                          , x_return_status            => l_return_status1
7490                          , x_msg_count                => l_msg_count1
7491                          , x_msg_data                 => l_msg_data1);
7492 
7493                     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UpdateDunning status='|| l_return_status1);
7494                 end if;
7495 
7496             end if;
7497 
7498         ELSE  -- always create dunning record
7499 
7500             --l_dunning_rec.dunning_level := p_level;
7501             l_dunning_rec.dunning_level := l_level;
7502             l_dunning_rec.dunning_object_id := p_source_id;
7503             l_dunning_rec.callback_yn := 'N';
7504             l_dunning_rec.status := 'OPEN';
7505             l_dunning_rec.dunning_method:= p_method;
7506             l_dunning_rec.xml_template_id:= p_template_id;
7507             l_dunning_rec.xml_request_id := l_request_id;
7508             l_dunning_rec.object_type := p_object_code;
7509             l_dunning_rec.object_id := p_object_id;
7510 		        l_dunning_rec.contact_destination := l_contact;   -- bug 3955222
7511 		        l_dunning_rec.contact_party_id := l_contact_party_id;   -- bug 3955222
7512 
7513             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - InsertRow');
7514             CREATE_DUNNING(p_api_version              => 1.0
7515                           ,p_init_msg_list            => FND_API.G_FALSE
7516                           ,p_commit                   => FND_API.G_TRUE
7517                           ,p_dunning_rec              => l_dunning_rec
7518                           ,x_dunning_id               => l_dunning_id
7519                           ,x_return_status            => l_return_status1
7520                           ,x_msg_count                => l_msg_count1
7521                           ,x_msg_data                 => l_msg_data1);
7522 
7523             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CreateDunning status='|| l_return_status1);
7524             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - dunning_id='||l_dunning_id);
7525 
7526             IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
7527                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot Create Dunning');
7528             END IF;
7529 
7530         END IF; -- end of p_object
7531 
7532 
7533     else  -- if resend - always create new dunning record
7534 
7535         l_dunning_rec.dunning_level := l_level;
7536         l_dunning_rec.dunning_object_id := p_source_id;
7537         l_dunning_rec.callback_yn := 'N';
7538         l_dunning_rec.status := 'OPEN';
7539         l_dunning_rec.dunning_method:= p_method;
7540         l_dunning_rec.xml_template_id:= p_template_id;
7541         l_dunning_rec.xml_request_id := l_request_id;
7542         l_dunning_rec.object_type := p_object_code;
7543         l_dunning_rec.object_id := p_object_id;
7544         l_dunning_rec.contact_destination := l_contact;   -- bug 3955222
7545         l_dunning_rec.contact_party_id := l_contact_party_id;   -- bug 3955222
7546 
7547         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - InsertRow');
7548         CREATE_DUNNING(p_api_version              => 1.0
7549                       ,p_init_msg_list            => FND_API.G_FALSE
7550                       ,p_commit                   => FND_API.G_TRUE
7551                       ,p_dunning_rec              => l_dunning_rec
7552                       ,x_dunning_id               => l_dunning_id
7553                       ,x_return_status            => l_return_status1
7554                       ,x_msg_count                => l_msg_count1
7555                       ,x_msg_data                 => l_msg_data1);
7556 
7557         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CreateDunning status='|| l_return_status1);
7558         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - dunning_id='||l_dunning_id);
7559 
7560         IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
7561            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot Create Dunning');
7562         END IF;
7563 
7564     end if;
7565 
7566     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end_call_ffm');
7567 
7568     commit work;
7569     x_request_id    := l_request_id;
7570     x_return_status := l_return_status;
7571 
7572 
7573 
7574     FND_MSG_PUB.Count_And_Get
7575     (  p_count          =>   x_msg_count,
7576        p_data           =>   x_msg_data );
7577 
7578     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - x_return_status: '||x_return_status);
7579     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - x_msg_count: '||x_msg_count);
7580     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - x_msg_data: '||x_msg_data);
7581 
7582     EXCEPTION
7583           WHEN FND_API.G_EXC_ERROR THEN
7584                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception');
7585                errmsg := SQLERRM;
7586                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
7587 --               ROLLBACK TO SEND_XML_PVT;
7588                x_return_status := FND_API.G_RET_STS_ERROR;
7589                FND_MSG_PUB.Count_And_Get
7590               (  p_count          =>   x_msg_count,
7591                  p_data           =>   x_msg_data
7592               );
7593 
7594           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7595                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExc Exception');
7596                errmsg := SQLERRM;
7597                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
7598 --               ROLLBACK TO SEND_XML_PVT;
7599                x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7600                FND_MSG_PUB.Count_And_Get
7601               (  p_count          =>   x_msg_count,
7602                  p_data           =>   x_msg_data
7603               );
7604 
7605           WHEN OTHERS THEN
7606                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception');
7607                errmsg := SQLERRM;
7608                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
7609 --               ROLLBACK TO SEND_XML_PVT;
7610                x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7611                FND_MSG_PUB.Count_And_Get
7612               (  p_count          =>   x_msg_count,
7613                  p_data           =>   x_msg_data
7614               );
7615 
7616 END Send_XML;
7617 
7618 
7619 
7620 /*========================================================================
7621  * clchang updated 07/08/2003
7622  *                 for the new design of using 'Contact Purpose'.
7623  *                 - contact point is based on purpose, primary,
7624  *                   type, status;
7625  *                   Get the contact point for contact_point_purpose is
7626  *                   'DUNNING';
7627  *                   If null, get the contact point for all type;
7628  *                 - get contact point for one specific party,
7629  *                   if null, get relation party_id of relationship_type
7630  *                   is 'DUNNING', and get contact point by the relationship
7631  *                   org id;
7632  *
7633  * clchang updated 01/17/2004
7634  *                 - the party_id could be rel_party_id, person_party_id, or
7635  *                   org_party_id.
7636  *                   1. chk the party_type by chk_party_type;
7637  *                   2. if rel_party_id, call GetContactPoint first;
7638  *                      if null, (if null, same as #3)
7639  *                      => get org_party_id for this rel
7640  *                      => get org_party_id with DUNNING relationship_type
7641  *                      => call GetContactPoint
7642  *                      if null => get org_party_id directly
7643  *                   3. if org_party_id,
7644  *                      => get org_party_id with DUNNING relationship_type
7645  *                      => call GetContactPoint
7646  *                      if null => get org_party_id directly
7647  *                              => call GetContactPoint
7648  *                   4. if person_party_id,
7649  *                      => call GetContactPoint (person has no dunning rel)
7650  *
7651  *=======================================================================*/
7652 
7653 Procedure GetContactInfo ( p_api_version             IN NUMBER := 1.0,
7654                            p_init_msg_list           IN VARCHAR2 ,
7655                            p_commit                  IN VARCHAR2 ,
7656                            p_method                  IN VARCHAR2,
7657                            p_party_id                IN NUMBER,
7658                            p_dunning_level           IN VARCHAR2,
7659                            p_cust_site_use_id        IN VARCHAR2,
7660                            x_return_status           OUT NOCOPY VARCHAR2,
7661                            x_msg_count               OUT NOCOPY NUMBER,
7662                            x_msg_data                OUT NOCOPY VARCHAR2,
7663                            x_contact                 OUT NOCOPY VARCHAR2,
7664                            x_contact_party_id        OUT NOCOPY number)
7665 
7666 IS
7667     --
7668     CURSOR C_GET_ORG_EMAIL (IN_PARTY_ID NUMBER) IS
7669       SELECT email_address
7670         FROM HZ_PARTIES
7671        WHERE party_ID = in_party_ID;
7672     --
7673     CURSOR C_GET_DUNNING_ORG (IN_PARTY_ID NUMBER) IS
7674       SELECT party_id
7675         FROM HZ_RELATIONSHIPS
7676        WHERE object_id = in_party_ID
7677          AND relationship_type = 'DUNNING'
7678          AND status = 'A';
7679     --
7680     CURSOR C_GET_ORG_PARTY (IN_REL_PARTY_ID NUMBER) IS
7681       SELECT r.object_id --org party id
7682         FROM HZ_PARTIES p, HZ_RELATIONSHIPS r
7683        WHERE r.party_id = in_rel_party_ID
7684          AND p.party_id = r.object_id
7685          AND p.party_type = 'ORGANIZATION';
7686     --
7687     CURSOR C_GET_PARTY_TYPE (IN_PARTY_ID NUMBER) IS
7688       SELECT p.party_type
7689         FROM HZ_PARTIES p
7690        WHERE p.party_id = in_party_ID;
7691     --
7692 		l_api_name          CONSTANT VARCHAR2(30) := 'GetContactInfo';
7693 		l_api_version       NUMBER := 1.0;
7694 		l_commit            VARCHAR2(5);
7695 		l_party_id      NUMBER;
7696 		l_dunning_party_id  NUMBER;
7697 		l_party_type    VARCHAR2(30);
7698 		nIdx            NUMBER;
7699 		l_msg_count     NUMBER;
7700 		l_msg_data      VARCHAR2(1000);
7701 		errmsg          VARCHAR2(32767);
7702 		l_return_status VARCHAR2(1000);
7703 		l_email         VARCHAR2(2000);
7704 		l_printer       VARCHAR2(2000);
7705 		l_fax           VARCHAR2(2000);
7706 		l_primary       VARCHAR2(10);
7707 
7708 BEGIN
7709 
7710      -- Standard Start of API savepoint
7711      SAVEPOINT GetContactInfo_PVT;
7712 
7713      l_commit     := p_commit;
7714      x_contact_party_id := p_party_id; -- default to origal party_id until updated #3955222
7715 
7716     -- Initialize message list IF p_init_msg_list is set to TRUE.
7717     IF FND_API.to_Boolean( p_init_msg_list )
7718     THEN
7719         FND_MSG_PUB.initialize;
7720     END IF;
7721 
7722     -- Initialize API return status to SUCCESS
7723     l_return_status := FND_API.G_RET_STS_SUCCESS;
7724     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Start');
7725 
7726     if (p_party_id is null) then
7727         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No PARTY_ID');
7728         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_PARTY');
7729         FND_MSG_PUB.Add;
7730         l_return_status := FND_API.G_RET_STS_ERROR;
7731         GOTO end_getcontact;
7732     end if;
7733     if (p_method is null) then
7734         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No METHOD');
7735         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_METHOD');
7736         FND_MSG_PUB.Add;
7737         l_return_status := FND_API.G_RET_STS_ERROR;
7738         GOTO end_getcontact;
7739     end if;
7740 
7741     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Party_id='||p_party_id);
7742     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - method='||p_method);
7743 
7744    /*=======================================
7745     * get the primary contact email first;
7746     * if not found, get org email address;
7747     =======================================*/
7748     if instr( p_method, 'PRINT' ) > 0 then
7749       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Get Printer');
7750       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - userid= '||FND_GLOBAL.USER_ID);
7751       l_printer := NVL(fnd_profile.value('IEX_FFM_PRINTER'), '');
7752       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Printer:'||l_printer);
7753       if (l_printer = '' or l_printer is null) then
7754          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No Printer');
7755          FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_PRINTER');
7756          FND_MSG_PUB.Add;
7757          l_return_status := FND_API.G_RET_STS_ERROR;
7758          GOTO end_getcontact;
7759       end if;
7760       x_contact := l_printer;
7761 
7762     else
7763       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get party type');
7764       Open C_Get_PARTY_TYPE(p_party_id);
7765       Fetch C_Get_PARTY_TYPE into l_party_type;
7766       If ( C_GET_PARTY_TYPE%NOTFOUND ) Then
7767          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - cannot find party type');
7768          FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_PARTYTYPE');
7769          FND_MSG_PUB.Add;
7770          l_return_status := FND_API.G_RET_STS_ERROR;
7771          Close C_Get_PARTY_TYPE;
7772          GOTO end_getcontact;
7773       END IF;
7774       Close C_Get_PARTY_TYPE;
7775       --
7776       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - party_type=' ||l_party_type);
7777       --
7778       -- if organization, need to get the org party id  with dunning type first.
7779       --
7780       IF (l_party_type = 'PERSON' or l_party_type = 'PARTY_RELATIONSHIP') then
7781         GetContactPoint(
7782                    p_api_version              => p_api_version
7783                  , p_init_msg_list            => p_init_msg_list
7784                  , p_commit                   => p_commit
7785                  , p_method                   => p_method
7786                  , p_party_id                 => p_party_id
7787                  , x_return_status            => l_return_status
7788                  , x_msg_count                => l_msg_count
7789                  , x_msg_data                 => l_msg_data
7790                  , x_contact                  => x_contact
7791         );
7792 
7793         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - status='||l_return_status);
7794         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - contact='||x_contact);
7795       END IF;
7796 
7797       -- person doesnt have dunning type
7798       -- so, we only check person contact point by GetContactPoint.
7799       IF (l_party_type = 'PERSON' and
7800           (x_contact is null or l_return_status <> FND_API.G_RET_STS_SUCCESS))
7801       then
7802           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - person contact NotFound');
7803           FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_CONTACTINFO');
7804           FND_MSG_PUB.Add;
7805           l_return_status := FND_API.G_RET_STS_ERROR;
7806           GOTO end_getcontact;
7807       end if;
7808       --
7809       -- if relationship or organization,
7810       -- get the org_party_id with relationship type DUNNING first,
7811       --
7812       -- one party_is could have more than one rel party id with type DUNNING.
7813       -- using LOOP to fetch data until Contact Point is found.
7814       --
7815       nIdx := 0;
7816       if (l_party_type = 'ORGANIZATION') then
7817           l_party_id := p_party_id;
7818       else
7819           -- get org_party_id
7820           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get_org_party');
7821           Open C_Get_ORG_PARTY(p_party_id);
7822           Fetch C_Get_ORG_PARTY into l_party_id;
7823           If ( C_GET_ORG_PARTY%NOTFOUND ) Then
7824              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - cannot find org party');
7825              FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_ORG');
7826              FND_MSG_PUB.Add;
7827              l_return_status := FND_API.G_RET_STS_ERROR;
7828              Close C_Get_ORG_PARTY;
7829              GOTO end_getcontact;
7830           end if;
7831           x_contact_party_id := l_party_id; -- default to org party_id until updated #3955222
7832           Close C_Get_ORG_PARTY;
7833       end if;
7834       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - org_party_id='||l_party_id);
7835       --
7836       if (l_party_type <> 'PERSON' and
7837           (x_contact is null or l_return_status <> FND_API.G_RET_STS_SUCCESS))
7838       THEN
7839 
7840           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get dunning party_id');
7841           Open C_Get_DUNNING_ORG(l_party_id);
7842 
7843             Fetch C_Get_DUNNING_ORG into l_dunning_party_id;
7844 
7845             If ( C_GET_DUNNING_ORG%NOTFOUND or l_dunning_party_id is null) Then
7846               if (nIdx = 0 ) then
7847                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No Dunning PartyId');
7848                 l_return_status := FND_API.G_RET_STS_ERROR;
7849               end if;
7850 
7851             ELSE
7852               nIdx := nIdx + 1;
7853               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - rel party_id = '||l_dunning_party_id );
7854               l_return_status := FND_API.G_RET_STS_SUCCESS;
7855               GetContactPoint(
7856                    p_api_version              => p_api_version
7857                  , p_init_msg_list            => p_init_msg_list
7858                  , p_commit                   => p_commit
7859                  , p_method                   => p_method
7860                  , p_party_id                 => l_dunning_party_id
7861                  , x_return_status            => l_return_status
7862                  , x_msg_count                => l_msg_count
7863                  , x_msg_data                 => l_msg_data
7864                  , x_contact                  => x_contact
7865               );
7866               x_contact_party_id := l_dunning_party_id; -- default to dunning party_id until updated #3955222
7867 
7868               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - status='||l_return_status);
7869               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - contact='||x_contact);
7870 
7871               if (x_contact is not null and
7872                   l_return_status = FND_API.G_RET_STS_SUCCESS) THEN
7873                   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Found ContactPint!');
7874               end if;
7875 
7876            end if;
7877 
7878           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - x_contact='||x_contact);
7879           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - close c_get_dunning_org');
7880           Close C_Get_DUNNING_ORG;
7881 
7882 
7883           -- if cannot find the contact point for dunning party id,
7884           -- try to get the contact point by the org party id (no dunning type).
7885           --
7886           if (x_contact is null or
7887               l_return_status <> FND_API.G_RET_STS_SUCCESS)
7888           THEN
7889           --
7890               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No ContactPoint for Duning Party');
7891               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get party_id contactpoint');
7892               -- get ContactPoint by org_party_id directyly (no Dunning type)
7893               GetContactPoint(
7894                    p_api_version              => p_api_version
7895                  , p_init_msg_list            => p_init_msg_list
7896                  , p_commit                   => p_commit
7897                  , p_method                   => p_method
7898                  , p_party_id                 => l_party_id
7899                  , x_return_status            => l_return_status
7900                  , x_msg_count                => l_msg_count
7901                  , x_msg_data                 => l_msg_data
7902                  , x_contact                  => x_contact
7903                  );
7904              x_contact_party_id := l_party_id; -- default to origal party_id until updated #3955222
7905            end if;
7906            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - x_contact='||x_contact);
7907 
7908 
7909       end if; -- ( end of if x_contact of original party_id is null)
7910     --
7911     END IF;  -- ( end of method <> printer)
7912 
7913 
7914     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - x_contact:'||x_contact);
7915 
7916 
7917     <<end_getcontact>>
7918     x_return_status := l_return_status;
7919     if (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
7920         x_contact := '';
7921     END IF;
7922 
7923     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - return_status:'||x_return_status);
7924     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ContactInfo:'||x_contact);
7925 
7926     FND_MSG_PUB.Count_And_Get
7927     (  p_count          =>   x_msg_count,
7928        p_data           =>   x_msg_data );
7929 
7930     EXCEPTION
7931           WHEN FND_API.G_EXC_ERROR THEN
7932               ROLLBACK TO GetContactInfo_PVT;
7933               x_return_status := FND_API.G_RET_STS_ERROR;
7934               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exp Exception');
7935               errmsg := SQLERRM;
7936               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
7937               FND_MSG_PUB.Count_And_Get
7938               (  p_count          =>   x_msg_count,
7939                  p_data           =>   x_msg_data
7940               );
7941 
7942           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7943               ROLLBACK TO GetContactInfo_PVT;
7944               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7945               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExp Exception');
7946               errmsg := SQLERRM;
7947               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
7948               FND_MSG_PUB.Count_And_Get
7949               (  p_count          =>   x_msg_count,
7950                  p_data           =>   x_msg_data
7951               );
7952 
7953           WHEN OTHERS THEN
7954               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception');
7955               ROLLBACK TO GetContactInfo_PVT;
7956               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7957               errmsg := SQLERRM;
7958               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
7959               FND_MSG_PUB.Count_And_Get
7960               (  p_count          =>   x_msg_count,
7961                  p_data           =>   x_msg_data
7962               );
7963 
7964 END GetContactInfo;
7965 
7966 -- new procedure clchang added 07/08/2003 in 11.5.10
7967 -- only for Fax and Email method
7968 Procedure GetContactPoint ( p_api_version             IN NUMBER := 1.0,
7969                            p_init_msg_list           IN VARCHAR2 ,
7970                            p_commit                  IN VARCHAR2 ,
7971                            p_method                  IN VARCHAR2,
7972                            p_party_id                IN NUMBER,
7973                            x_return_status           OUT NOCOPY VARCHAR2,
7974                            x_msg_count               OUT NOCOPY NUMBER,
7975                            x_msg_data                OUT NOCOPY VARCHAR2,
7976                            x_contact                 OUT NOCOPY VARCHAR2)
7977 IS
7978     --
7979     -- clchang updated 07/08/2003 to get fax and email on the new design
7980     -- of using 'Contact Purpose'
7981     -- (since 11.5.10)
7982     --
7983     CURSOR C_GET_CONTACT_EMAIL (IN_PARTY_ID NUMBER, IN_TYPE VARCHAR2) IS
7984       SELECT email_address,
7985              decode(primary_by_purpose, 'Y',1,2) purpose,
7986              decode(primary_flag, 'Y',1,2) primary
7987         FROM HZ_CONTACT_POINTS
7988        WHERE owner_table_ID = in_party_ID
7989          AND owner_table_name = 'HZ_PARTIES'
7990          --AND Contact_point_type = 'EMAIL'
7991          AND upper(Contact_point_type) = IN_TYPE
7992          AND Contact_point_purpose = 'DUNNING'
7993          AND NVL(do_not_use_flag, 'N') = 'N'
7994          AND (status = 'A' OR status <> 'I')
7995        order by purpose, primary;
7996     --
7997     CURSOR C_GET_CONTACT_EMAIL2 (IN_PARTY_ID NUMBER, IN_TYPE VARCHAR2) IS
7998       SELECT email_address,
7999              decode(primary_flag, 'Y',1,2) primary
8000         FROM HZ_CONTACT_POINTS
8001        WHERE owner_table_ID = in_party_ID
8002          AND owner_table_name = 'HZ_PARTIES'
8003          --AND Contact_point_type = 'EMAIL'
8004          AND upper(Contact_point_type) = IN_TYPE
8005          AND NVL(do_not_use_flag, 'N') = 'N'
8006          AND (status = 'A' OR status <> 'I')
8007        order by primary;
8008     --
8009     CURSOR C_GET_CONTACT_FAX (IN_PARTY_ID NUMBER) IS
8010       SELECT phone_country_code || phone_area_code||phone_number faxnum,
8011              decode(primary_by_purpose, 'Y',1,2) purpose,
8012              decode(primary_flag, 'Y', 1, 2) primary
8013         FROM HZ_CONTACT_POINTS
8014        WHERE owner_table_ID = in_party_ID
8015          AND owner_table_name = 'HZ_PARTIES'
8016          AND upper(Contact_point_type) = 'PHONE'
8017          AND upper(phone_line_type) = 'FAX'
8018          AND Contact_point_purpose = 'DUNNING'
8019          AND NVL(do_not_use_flag, 'N') = 'N'
8020          AND (status = 'A' OR status <> 'I')
8021        order by purpose, primary;
8022     --
8023     CURSOR C_GET_CONTACT_FAX2 (IN_PARTY_ID NUMBER) IS
8024       SELECT phone_country_code || phone_area_code||phone_number faxnum,
8025              decode(primary_flag, 'Y', 1, 2) primary
8026         FROM HZ_CONTACT_POINTS
8027        WHERE owner_table_ID = in_party_ID
8028          AND owner_table_name = 'HZ_PARTIES'
8029          AND upper(Contact_point_type) = 'PHONE'
8030          AND upper(phone_line_type) = 'FAX'
8031          AND NVL(do_not_use_flag, 'N') = 'N'
8032          AND (status = 'A' OR status <> 'I')
8033        order by primary;
8034     --
8035     --
8036 l_api_name          CONSTANT VARCHAR2(30) := 'GetContactPoint';
8037 l_api_version       NUMBER := 1.0;
8038 l_commit            VARCHAR2(5) ;
8039 --
8040 l_party_id      NUMBER;
8041 --
8042 l_msg_count     NUMBER;
8043 l_msg_data      VARCHAR2(1000);
8044 errmsg          VARCHAR2(32767);
8045 l_return_status VARCHAR2(1000);
8046 --
8047 --
8048 l_email         VARCHAR2(2000);
8049 l_printer       VARCHAR2(2000);
8050 l_fax           VARCHAR2(2000);
8051 l_primary       VARCHAR2(10);
8052 l_purpose       VARCHAR2(10);
8053 --
8054 
8055 BEGIN
8056 
8057      -- Standard Start of API savepoint
8058      SAVEPOINT GetContactPoint_PVT;
8059 
8060      l_commit    := p_commit;
8061 
8062     -- Initialize message list IF p_init_msg_list is set to TRUE.
8063     IF FND_API.to_Boolean( p_init_msg_list )
8064     THEN
8065         FND_MSG_PUB.initialize;
8066     END IF;
8067 
8068     -- Initialize API return status to SUCCESS
8069     l_return_status := FND_API.G_RET_STS_SUCCESS;
8070 
8071     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Start');
8072 
8073     if (p_party_id is null) then
8074         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No PARTY_ID');
8075         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_PARTY');
8076         FND_MSG_PUB.Add;
8077         l_return_status := FND_API.G_RET_STS_ERROR;
8078         GOTO end_getcontact;
8079     end if;
8080     if (p_method is null) then
8081         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - No METHOD');
8082         FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_METHOD');
8083         FND_MSG_PUB.Add;
8084         l_return_status := FND_API.G_RET_STS_ERROR;
8085         GOTO end_getcontact;
8086     end if;
8087 
8088     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Party_id='||p_party_id);
8089     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - method='||p_method);
8090    /*=======================================
8091     * get the primary contact email first;
8092     * if not found, get org email address;
8093     =======================================*/
8094     if (upper(p_method) = 'EMAIL') then
8095     --
8096       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - get Email');
8097       Open C_Get_CONTACT_EMAIL(p_party_id, 'EMAIL');
8098       Fetch C_Get_CONTACT_EMAIL into l_email, l_purpose, l_primary;
8099 
8100       -- 1. there's record in contact table with type = EMAIL,
8101       --    but the email is null;
8102       -- 2. or no record in contact table with type = EMAIL;
8103 
8104       If ( C_GET_CONTACT_EMAIL%NOTFOUND OR l_email is null) Then
8105 
8106          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Email NotFound in C_GET_CONTACT_EMAIL');
8107          Open C_Get_CONTACT_EMAIL2(p_party_id, 'EMAIL');
8108          Fetch C_Get_CONTACT_EMAIL2 into l_email, l_primary;
8109 
8110          If ( C_GET_CONTACT_EMAIL2%NOTFOUND) Then
8111             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Customer NotFound');
8112             FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_EMAIL');
8113             FND_MSG_PUB.Add;
8114             l_return_status := FND_API.G_RET_STS_ERROR;
8115             Close C_Get_CONTACT_EMAIL2;
8116             Close C_Get_CONTACT_EMAIL;
8117             GOTO end_getcontact;
8118          end if;
8119          --
8120          If ( l_email is null ) Then
8121             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - EmailAddress NotFound in C_GET_ORG_EMAIL');
8122             FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_EMAIL');
8123             FND_MSG_PUB.Add;
8124             l_return_status := FND_API.G_RET_STS_ERROR;
8125             Close C_Get_CONTACT_EMAIL2;
8126             Close C_Get_CONTACT_EMAIL;
8127             GOTO end_getcontact;
8128          end if;
8129          --
8130          Close C_Get_CONTACT_EMAIL2;
8131       end if;
8132 
8133       If ( l_email is null ) Then
8134             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - EmailAddress is null');
8135             FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_EMAIL');
8136             FND_MSG_PUB.Add;
8137          l_return_status := FND_API.G_RET_STS_ERROR;
8138          Close C_Get_CONTACT_EMAIL;
8139          GOTO end_getcontact;
8140       end if;
8141 
8142       x_contact := l_email;
8143 
8144       Close C_Get_CONTACT_EMAIL;
8145       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - email='||l_email);
8146 
8147     --
8148     --
8149     elsif (upper(p_method) = 'FAX') then
8150     --
8151       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Get Fax');
8152       Open C_Get_CONTACT_FAX(p_party_id);
8153       Fetch C_Get_CONTACT_FAX into l_fax, l_purpose, l_primary;
8154 
8155      If ( C_GET_CONTACT_FAX%NOTFOUND OR l_fax is null) Then
8156 
8157          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Fax NotFound in C_GET_CONTACT_FAX');
8158          Open C_Get_CONTACT_FAX2(p_party_id);
8159          Fetch C_Get_CONTACT_FAX2 into l_fax, l_primary;
8160 
8161          If ( C_GET_CONTACT_FAX2%NOTFOUND) Then
8162             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Customer NotFound');
8163             FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_FAX');
8164             FND_MSG_PUB.Add;
8165             l_return_status := FND_API.G_RET_STS_ERROR;
8166             Close C_Get_CONTACT_FAX2;
8167             Close C_Get_CONTACT_FAX;
8168             GOTO end_getcontact;
8169          end if;
8170          --
8171          If ( l_fax is null ) Then
8172             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Fax NotFound in C_GET_CONTACT_FAX');
8173             FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_FAX');
8174             FND_MSG_PUB.Add;
8175             l_return_status := FND_API.G_RET_STS_ERROR;
8176             Close C_Get_CONTACT_FAX2;
8177             Close C_Get_CONTACT_FAX;
8178             GOTO end_getcontact;
8179          end if;
8180          --
8181          Close C_Get_CONTACT_FAX2;
8182       end if;
8183 
8184       If ( l_fax is null ) Then
8185             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - FAX NotFound');
8186             FND_MESSAGE.Set_Name('IEX', 'IEX_SEND_FAILED_NO_FAX');
8187             FND_MSG_PUB.Add;
8188          l_return_status := FND_API.G_RET_STS_ERROR;
8189          Close C_Get_CONTACT_FAX;
8190          GOTO end_getcontact;
8191       end if;
8192 
8193       x_contact := l_fax;
8194 
8195       Close C_Get_CONTACT_FAX;
8196       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - fax= '||l_fax);
8197 
8198     --
8199     END IF; -- end of p_method=EMAIL
8200 
8201 
8202     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - email:'||l_email);
8203     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - fax:'||l_fax);
8204 
8205     <<end_getcontact>>
8206     x_return_status := l_return_status;
8207     if (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
8208         x_contact := '';
8209     END IF;
8210 
8211     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - return_status:'||x_return_status);
8212     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ContactInfo:'||x_contact);
8213 
8214     FND_MSG_PUB.Count_And_Get
8215     (  p_count          =>   x_msg_count,
8216        p_data           =>   x_msg_data );
8217 
8218     EXCEPTION
8219           WHEN FND_API.G_EXC_ERROR THEN
8220              ROLLBACK TO GetContactPoint_PVT;
8221              x_return_status := FND_API.G_RET_STS_ERROR;
8222              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exp Exception');
8223              errmsg := SQLERRM;
8224              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
8225              FND_MSG_PUB.Count_And_Get
8226              (  p_count          =>   x_msg_count,
8227                 p_data           =>   x_msg_data
8228              );
8229 
8230           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8231              ROLLBACK TO GetContactPoint_PVT;
8232              x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8233              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExp Exception');
8234              errmsg := SQLERRM;
8235              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
8236              FND_MSG_PUB.Count_And_Get
8237              (  p_count          =>   x_msg_count,
8238                 p_data           =>   x_msg_data
8239              );
8240 
8241           WHEN OTHERS THEN
8242              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception');
8243              ROLLBACK TO GetContactPoint_PVT;
8244              x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8245              errmsg := SQLERRM;
8246              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
8247              FND_MSG_PUB.Count_And_Get
8248              (  p_count          =>   x_msg_count,
8249                 p_data           =>   x_msg_data
8250              );
8251 END GetContactPoint;
8252 
8253 
8254 Procedure CHK_QUERY_DATA(  p_query_id                IN NUMBER,
8255                            p_FULFILLMENT_BIND_TBL    IN IEX_DUNNING_PVT.FULFILLMENT_BIND_TBL,
8256                            x_keep_flag               OUT NOCOPY NUMBER)
8257 IS
8258     CURSOR C_GET_QUERY (IN_ID NUMBER) IS
8259       SELECT upper(query_string)
8260         FROM jtf_fm_queries_all
8261        WHERE query_id = IN_ID
8262          AND nvl(upper(f_deletedflag),'0') <>'D';
8263    --
8264 
8265 		l_bind_var          JTF_FM_REQUEST_GRP.G_VARCHAR_TBL_TYPE;
8266 		l_bind_var_type     JTF_FM_REQUEST_GRP.G_VARCHAR_TBL_TYPE;
8267 		l_bind_val          JTF_FM_REQUEST_GRP.G_VARCHAR_TBL_TYPE;
8268 		l_bind_cnt_tbl      NUMBER;
8269 		l_keep_flag         NUMBER := 1;
8270 		l_query             VARCHAR2(4000);
8271 		l_part_query        VARCHAR2(4000);
8272 		l_party_id          NUMBER;
8273 		l_org_id            NUMBER;
8274 		l_account_id        NUMBER;
8275 		l_del_id            NUMBER;
8276 		l_result            NUMBER;
8277 		v_cursor            NUMBER;
8278 		v_create_string     varchar2(1000);
8279 		v_numrows           NUMBER;
8280 		errmsg              varchar2(32767);
8281 		l_found             NUMBER;
8282 		l_len               NUMBER;
8283 
8284 		-- clchang updated for sql bind var 05/07/2003
8285 		vstr1               VARCHAR2(100) ;
8286 		l_api_name          CONSTANT VARCHAR2(30) := 'CHK_QUERY_DATA';
8287 
8288 
8289 BEGIN
8290 
8291   WriteLog (G_PKG_NAME || ' ' || l_api_name || ' - start');
8292   WriteLog (G_PKG_NAME || ' ' || l_api_name || ' - queryid='||p_query_id);
8293   vstr1           := 'SELECT COUNT(*) ';
8294 
8295   l_bind_cnt_tbl := p_FULFILLMENT_BIND_TBL.count;
8296   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - bind_tbl_cnt'||l_bind_cnt_tbl);
8297 
8298   for j in 1..l_bind_cnt_tbl
8299   loop
8300       l_bind_var(j)      := p_FULFILLMENT_BIND_TBL(j).Key_name;
8301       l_bind_var_type(j) := p_FULFILLMENT_BIND_TBL(j).Key_Type;
8302       l_bind_val(j)      := p_FULFILLMENT_BIND_TBL(j).Key_Value;
8303       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - bind_var='||l_bind_var(j));
8304       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - bind_var_type='||l_bind_var_type(j));
8305       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - bind_val='||l_bind_val(j));
8306       if (l_bind_var(j) = 'party_id' ) then
8307           l_party_id := l_bind_val(j);
8308       elsif (l_bind_var(j) = 'org_id' ) then
8309           l_org_id := l_bind_val(j);
8310       elsif (l_bind_var(j) = 'account_id' ) then
8311           l_account_id := l_bind_val(j);
8312       elsif (l_bind_var(j) = 'delinquency_id' ) then
8313           l_del_id := l_bind_val(j);
8314       end if;
8315   end loop;
8316 
8317   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - partyid='||l_party_id);
8318   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - orgid='||l_org_id);
8319   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - acctid='||l_account_id);
8320   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - delid='||l_del_id);
8321 
8322   OPEN C_GET_QUERY (p_query_id);
8323   FETCH C_GET_QUERY INTO l_query;
8324   If ( C_GET_QUERY%NOTFOUND ) Then
8325        WriteLog (G_PKG_NAME || ' ' || l_api_name || ' - NoQUERY');
8326        l_keep_flag := 1;
8327        GOTO end_query;
8328   else
8329        WriteLog (G_PKG_NAME || ' ' || l_api_name || ' - string='||l_query);
8330        l_len := LENGTH(l_query);
8331        l_found := INSTR(l_query, 'FROM');
8332        l_part_query := SUBSTR(l_query, l_found, (l_len-l_found+1));
8333        -- clchang updated for sql bind var 05/07/2003
8334        --l_query := 'SELECT COUNT(*) ' || l_part_query;
8335        l_query := vstr1 || l_part_query;
8336        WriteLog (G_PKG_NAME || ' ' || l_api_name || ' - new string='||l_query);
8337 
8338        l_query := replace(l_query, ':PARTY_ID', to_char(l_party_id) );
8339        WriteLog (G_PKG_NAME || ' ' || l_api_name || ' - string1='||l_query);
8340        l_query := replace(l_query, ':ORG_ID', to_char(l_org_id) );
8341        WriteLog (G_PKG_NAME || ' ' || l_api_name || ' - string2='||l_query);
8342        l_query := replace(l_query, ':DELINQUENCY_ID', to_char(l_del_id) );
8343        WriteLog (G_PKG_NAME || ' ' || l_api_name || ' - string3='||l_query);
8344        l_query := replace(l_query, ':ACCOUNT_ID', to_Char(l_account_id) );
8345        WriteLog (G_PKG_NAME || ' ' || l_api_name || ' - final string='||l_query);
8346   end if;
8347   CLOSE C_GET_QUERY;
8348 
8349   v_cursor := DBMS_SQL.OPEN_CURSOR;
8350   v_create_string := l_query;
8351   DBMS_SQL.parse(v_cursor, v_create_string, 1);
8352   DBMS_SQL.DEFINE_COLUMN(v_cursor, 1, l_result);
8353   v_numrows := DBMS_SQL.EXECUTE(v_cursor);
8354   v_numrows := DBMS_SQL.FETCH_ROWS(v_cursor);
8355   DBMS_SQL.COLUMN_VALUE(v_cursor, 1, l_result);
8356   WriteLog (G_PKG_NAME || ' ' || l_api_name || ' - result='||l_result);
8357 
8358   DBMS_SQL.CLOSE_CURSOR(v_cursor);
8359 
8360   if (l_result > 0) then
8361       l_keep_flag := 1;
8362   else
8363       l_keep_flag := 0;
8364   end if;
8365 
8366   <<end_query>>
8367   x_keep_flag := l_keep_flag;
8368   WriteLog (G_PKG_NAME || ' ' || l_api_name || ' - x_keep_flag='||l_keep_flag);
8369   WriteLog (G_PKG_NAME || ' ' || l_api_name || ' - end');
8370 
8371     EXCEPTION
8372           WHEN FND_API.G_EXC_ERROR THEN
8373               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception');
8374               errmsg := SQLERRM;
8375               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
8376               x_keep_flag := 1;
8377 
8378           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8379               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExc Exception');
8380               errmsg := SQLERRM;
8381               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
8382               x_keep_flag := 1;
8383 
8384           WHEN OTHERS THEN
8385               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Other Exception');
8386               errmsg := SQLERRM;
8387               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
8388               x_keep_flag := 1;
8389 
8390 END CHK_QUERY_DATA;
8391 
8392 
8393 
8394 PROCEDURE Close_Dunning
8395            (p_api_version             IN NUMBER,
8396             p_init_msg_list           IN VARCHAR2 ,
8397             p_commit                  IN VARCHAR2 ,
8398             p_delinquencies_tbl       IN IEX_DELINQUENCY_PUB.DELINQUENCY_TBL_TYPE,
8399             p_running_level           IN VARCHAR2,
8400             x_return_status           OUT NOCOPY VARCHAR2,
8401             x_msg_count               OUT NOCOPY NUMBER,
8402             x_msg_data                OUT NOCOPY VARCHAR2)
8403 
8404 IS
8405     CURSOR C_GET_DUNNING (IN_ID NUMBER, IN_TYPE VARCHAR2) IS
8406       SELECT dunning_ID
8407         FROM IEX_DUNNINGS
8408        WHERE STATUS = 'OPEN'
8409          AND dunning_level = IN_TYPE
8410          AND dunning_object_id = IN_ID;
8411     --
8412     l_DUNNING_id            NUMBER;
8413     l_delinquency_id        NUMBER;
8414     l_dunning_object_id     NUMBER;
8415     l_dunning_level         VARCHAR2(30);
8416     l_DUNNING_rec           IEX_DUNNING_PUB.DUNNING_REC_TYPE;
8417     l_api_name              CONSTANT VARCHAR2(30) := 'Close_Dunning';
8418     l_api_version_number    CONSTANT NUMBER   := 1.0;
8419     l_return_status         VARCHAR2(1);
8420     l_msg_count             NUMBER;
8421     l_msg_data              VARCHAR2(32767);
8422     errmsg                  VARCHAR2(32767);
8423     nIdx                    NUMBER := 0;
8424     nCnt                    NUMBER := 0;
8425     --
8426     TYPE Dunning_ID_TBL_type is Table of IEX_DUNNINGS.DUNNING_ID%TYPE
8427 				        INDEX BY BINARY_INTEGER;
8428     dunning_tbl             Dunning_ID_TBL_TYPE;
8429     --
8430 BEGIN
8431       SAVEPOINT CLOSE_DUNNING_PVT;
8432 
8433       IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
8434                                            p_api_version,
8435                                            l_api_name,
8436                                            G_PKG_NAME)
8437       THEN
8438           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8439       END IF;
8440 
8441       IF FND_API.to_Boolean( p_init_msg_list )
8442       THEN
8443           FND_MSG_PUB.initialize;
8444       END IF;
8445 
8446       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - start');
8447       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - running_level='||p_running_level);
8448 
8449       x_return_status := FND_API.G_RET_STS_SUCCESS;
8450       l_dunning_level := p_running_level;
8451 
8452       if (p_running_level = 'CUSTOMER') then
8453           l_dunning_object_id := p_delinquencies_tbl(1).party_cust_id;
8454       elsif (p_running_level = 'ACCOUNT') THEN
8455           l_dunning_object_id := p_delinquencies_tbl(1).cust_account_id;
8456       elsif (p_running_level = 'BILL_TO') THEN
8457           l_dunning_object_id := p_delinquencies_tbl(1).customer_site_use_id;
8458       else
8459           l_dunning_object_id := p_delinquencies_tbl(1).delinquency_id;
8460       end if;
8461 
8462       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - dunning_level='||l_dunning_level);
8463       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - dunning_object_id='||l_dunning_object_id);
8464 
8465          --=============================================================
8466          --  Suppose one del has at most one open dunning.
8467          --  If we find out NOCOPY more than one open dunning, close all.
8468          --=============================================================
8469          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - GetDunning');
8470 
8471           nIdx := 1;
8472           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - del cnt=' ||p_delinquencies_tbl.count);
8473 
8474           FOR i in 1..p_delinquencies_tbl.count
8475           LOOP
8476              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Loop:i=' ||i);
8477 
8478              nCnt := 0;
8479              OPEN C_GET_DUNNING (l_dunning_object_id, l_dunning_level);
8480              LOOP
8481                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - nCnt='||nCnt);
8482                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - nIdx='||nIdx);
8483                 FETCH C_GET_DUNNING INTO l_dunning_id;
8484                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - dunningId='||l_dunning_id);
8485                 dunning_tbl(nIdx) := l_dunning_id;
8486                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - dunn='||dunning_tbl(nIdx));
8487 
8488                 If ( C_GET_DUNNING%NOTFOUND ) Then
8489                     if (nCnt = 0) then
8490                       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - NoOpenDunning');
8491                     end if;
8492                     GOTO end_dunning_loop;
8493                 else
8494                     nCnt := nCnt + 1;
8495                     nIdx := nIdx+1;
8496                 end if;
8497               END LOOP;
8498 
8499               <<end_dunning_loop>>
8500               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - End_Dunning_Loop');
8501 
8502           CLOSE C_GET_DUNNING;
8503           END LOOP;
8504 
8505           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - DunnCnt='||nIdx||';'||dunning_tbl.count);
8506 
8507           FORALL i in 1..dunning_tbl.count
8508 				  UPDATE IEX_DUNNINGS
8509 				     SET CALLBACK_YN = 'N',
8510 	                STATUS = 'CLOSE',
8511 	                LAST_UPDATE_DATE = sysdate
8512             WHERE Dunning_id = dunning_tbl(i);
8513           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - End');
8514 
8515       --
8516       -- End of API body
8517       --
8518 
8519       -- Standard check for p_commit
8520       IF FND_API.to_Boolean( p_commit )
8521       THEN
8522           COMMIT WORK;
8523       END IF;
8524 
8525       FND_MSG_PUB.Count_And_Get
8526       (  p_count          =>   x_msg_count,
8527          p_data           =>   x_msg_data );
8528 
8529       EXCEPTION
8530           WHEN FND_API.G_EXC_ERROR THEN
8531              ROLLBACK TO CLOSE_DUNNING_PVT;
8532               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception');
8533               errmsg := SQLERRM;
8534               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
8535 					   x_return_status := FND_API.G_RET_STS_ERROR;
8536 					   FND_MSG_PUB.Count_And_Get
8537 					   (  p_count          =>   x_msg_count,
8538 					      p_data           =>   x_msg_data
8539 					    );
8540 
8541           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8542              ROLLBACK TO CLOSE_DUNNING_PVT;
8543               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception');
8544               errmsg := SQLERRM;
8545               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
8546 					   x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8547 					   FND_MSG_PUB.Count_And_Get
8548 					   (  p_count          =>   x_msg_count,
8549 					      p_data           =>   x_msg_data
8550 					    );
8551 
8552           WHEN OTHERS THEN
8553              ROLLBACK TO CLOSE_DUNNING_PVT;
8554               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception');
8555               errmsg := SQLERRM;
8556               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
8557 					   x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8558 					   FND_MSG_PUB.Count_And_Get
8559 					   (  p_count          =>   x_msg_count,
8560 					      p_data           =>   x_msg_data
8561 					    );
8562 
8563 END Close_Dunning;
8564 
8565 
8566 Procedure Daily_Dunning
8567            (p_api_version             IN NUMBER := 1.0,
8568             p_init_msg_list           IN VARCHAR2 ,
8569             p_commit                  IN VARCHAR2 ,
8570             p_running_level           IN VARCHAR2,
8571             x_return_status           OUT NOCOPY VARCHAR2,
8572             x_msg_count               OUT NOCOPY NUMBER,
8573             x_msg_data                OUT NOCOPY VARCHAR2)
8574 IS
8575     CURSOR C_GET_DUNNING (IN_LEVEL VARCHAR2) IS
8576       SELECT dunning_ID,
8577              delinquency_id,
8578              dunning_object_id,
8579              to_char(callback_date, 'YYYYMMDD')
8580         FROM IEX_DUNNINGS
8581        WHERE STATUS = 'OPEN'
8582         AND CALLBACK_YN = 'Y'
8583         AND dunning_level = IN_LEVEL
8584         AND to_char(callback_date, 'YYYYMMDD') <= to_char(sysdate,'YYYYMMDD');
8585     --
8586     CURSOR C_CHK_DEL (IN_del_ID NUMBER) IS
8587       SELECT 1
8588         FROM IEX_DELINQUENCIES d
8589        WHERE d.delinquency_ID = in_del_ID
8590          AND STATUS in ('DELINQUENT', 'PREDELINQUENT');
8591     --
8592     CURSOR C_CHK_ACCOUNT (IN_ACCT_ID NUMBER) IS
8593       SELECT 1
8594         FROM IEX_DELINQUENCIES d
8595        WHERE d.cust_account_ID = in_ACCT_ID
8596          AND STATUS in ('DELINQUENT', 'PREDELINQUENT');
8597     --
8598     CURSOR C_CHK_CUSTOMER (IN_PARTY_ID NUMBER) IS
8599       SELECT 1
8600         FROM IEX_DELINQUENCIES d
8601        WHERE d.party_cust_id = in_party_id
8602          AND STATUS in ('DELINQUENT', 'PREDELINQUENT');
8603     --
8604     CURSOR C_CHK_SITE (IN_SITE_ID NUMBER) IS
8605       SELECT 1
8606         FROM IEX_DELINQUENCIES d
8607        WHERE d.customer_site_use_id = in_SITE_ID
8608          AND STATUS in ('DELINQUENT', 'PREDELINQUENT');
8609     --
8610     l_DUNNING_id            NUMBER;
8611     l_delinquency_id        NUMBER;
8612     l_callback_date         varchar2(10);
8613     l_DUNNING_rec           IEX_DUNNING_PUB.DUNNING_REC_TYPE;
8614     l_curr_date             varchar2(10);
8615     l_task_id               NUMBER;
8616     l_api_name              CONSTANT VARCHAR2(30) := 'Daily_Dunning';
8617     l_api_version_number    CONSTANT NUMBER   := 1.0;
8618     l_return_status         VARCHAR2(1);
8619     l_msg_count             NUMBER;
8620     l_msg_data              VARCHAR2(32767);
8621     errmsg                  VARCHAR2(32767);
8622     nIdx                    NUMBER := 0;
8623     nCnt                    NUMBER := 0;
8624     l_chk                   NUMBER := 0;
8625     l_task_cnt              NUMBER := 0;
8626     l_skip                  NUMBER := 0;
8627     --
8628     TYPE Dunning_ID_TBL_type is Table of IEX_DUNNINGS.DUNNING_ID%TYPE
8629 				        INDEX BY BINARY_INTEGER;
8630     dunning_tbl             Dunning_ID_TBL_TYPE;
8631     l_dunning_object_id     NUMBER;
8632 
8633 --Begin bug 7317666 21-Nov-2008 barathsr
8634     l_task_query varchar2(4000);
8635     TYPE c_cur_type IS REF CURSOR;
8636     c_invalid_tasks c_cur_type;
8637     l_inv_task_id JTF_TASKS_B.TASK_ID%TYPE;
8638     l_object_version_number JTF_TASKS_B.OBJECT_VERSION_NUMBER%TYPE;
8639 --End bug 7317666 21-Nov-2008 barathsr
8640 
8641 BEGIN
8642       -- Standard Start of API savepoint
8643       SAVEPOINT DAILY_DUNNING_PVT;
8644 
8645       -- Standard call to check for call compatibility.
8646       IF NOT FND_API.Compatible_API_Call ( l_api_version_number,
8647                                            p_api_version,
8648                                            l_api_name,
8649                                            G_PKG_NAME)
8650       THEN
8651           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8652       END IF;
8653 
8654       -- Initialize message list if p_init_msg_list is set to TRUE.
8655       IF FND_API.to_Boolean( p_init_msg_list )
8656       THEN
8657           FND_MSG_PUB.initialize;
8658       END IF;
8659 
8660       -- Debug Message
8661       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Start') ;
8662       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - running_level='||p_running_level) ;
8663 
8664       -- Initialize API return status to SUCCESS
8665       x_return_status := FND_API.G_RET_STS_SUCCESS;
8666 
8667       l_curr_date := to_char(sysdate, 'YYYYMMDD');
8668       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CurrDate='||l_curr_date) ;
8669       FND_FILE.PUT_LINE(FND_FILE.LOG, 'current date=' || l_curr_date);
8670       --
8671       -- Api body
8672       --
8673 
8674       nIdx := 0;
8675       Open C_Get_DUNNING (p_running_level);
8676       LOOP
8677 
8678          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ================') ;
8679          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Open C_Get_Dunning');
8680 
8681            Fetch C_Get_DUNNING into
8682                    l_dunning_id,
8683                    l_delinquency_id,
8684                    l_dunning_object_id,
8685                    l_callback_date;
8686 
8687            If ( C_GET_DUNNING%NOTFOUND ) Then
8688                 if (nIdx = 0) then
8689                     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - NoOpenDunning with callback_yn=Y');
8690                     FND_FILE.PUT_LINE(FND_FILE.LOG, 'NoOpenDunning with callback_yn=Y');
8691 
8692                 end if;
8693                 exit;
8694            ELSE
8695 
8696                 l_skip := 0;
8697                 x_return_status := FND_API.G_RET_STS_SUCCESS;
8698                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - DunningID='||l_dunning_id) ;
8699                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - DunningObjectID='||l_dunning_object_id) ;
8700                 WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Callback_Date='||l_callback_date) ;
8701                 FND_FILE.PUT_LINE(FND_FILE.LOG, '*****dunning_id='||l_dunning_id||'*****');
8702 
8703                 /*=========================================
8704                  * bug 2816550 - clchan updatd 02/21/2003
8705                  * for this current dunning,
8706                  * if the associated delinquenty is closed, no callback;
8707                  *=======================================================*/
8708                  IF (p_running_level = 'CUSTOMER') THEN
8709                      WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Chk Customer:'||l_dunning_object_id) ;
8710                      Open C_CHK_CUSTOMER(l_dunning_object_id);
8711                      Fetch C_CHK_CUSTOMER into l_chk;
8712 
8713                      If ( C_CHK_CUSTOMER%NOTFOUND) Then
8714                           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - All Del Closed for this customer');
8715                           FND_FILE.PUT_LINE(FND_FILE.LOG, 'All Delinquencies Closed for this customer');
8716                           Close C_CHK_CUSTOMER;
8717                           l_skip := 1;
8718                     else
8719                         l_skip := 0;
8720                         Close C_CHK_CUSTOMER;
8721                         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - can continue') ;
8722                     end if;
8723                  --
8724                  ELSIF (p_running_level = 'ACCOUNT') THEN
8725                      WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Chk Account:'||l_dunning_object_id) ;
8726                      Open C_CHK_Account(l_dunning_object_id);
8727                      Fetch C_CHK_Account into l_chk;
8728 
8729                      If ( C_CHK_Account%NOTFOUND) Then
8730                           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - All Del Closed for this Account');
8731                           FND_FILE.PUT_LINE(FND_FILE.LOG, 'All Delinquencies Closed for this account');
8732                           Close C_CHK_Account;
8733                           --GOTO end_dunning_if;
8734                           l_skip := 1;
8735                     else
8736                         l_skip := 0;
8737                         Close C_CHK_Account;
8738                         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - can continue') ;
8739                     end if;
8740                  --
8741                  ELSIF (p_running_level = 'BILL_TO') THEN
8742                      WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Chk Bill To:'||l_dunning_object_id) ;
8743                      Open C_CHK_Site(l_dunning_object_id);
8744                      Fetch C_CHK_Site into l_chk;
8745 
8746                      If ( C_CHK_Site%NOTFOUND) Then
8747                           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - All Del Closed for this site');
8748                           FND_FILE.PUT_LINE(FND_FILE.LOG, 'All Delinquencies Closed for this site');
8749                           Close C_CHK_Site;
8750                           --GOTO end_dunning_if;
8751                           l_skip := 1;
8752                     else
8753                         l_skip := 0;
8754                         Close C_CHK_Site;
8755                         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - can continue') ;
8756                     end if;
8757                  --
8758                  ELSIF (p_running_level = 'DELINQUENCY') THEN
8759                      WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Chk DEL:'||l_delinquency_id) ;
8760                      Open C_CHK_DEL(l_delinquency_id);
8761                      Fetch C_CHK_DEL into l_chk;
8762 
8763                      If ( C_CHK_DEL%NOTFOUND) Then
8764                           WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Del Closed for this del');
8765                           FND_FILE.PUT_LINE(FND_FILE.LOG, 'Delinquency Closed');
8766                           Close C_CHK_DEL;
8767                           --GOTO end_dunning_if;
8768                           l_skip := 1;
8769                     else
8770                         l_skip := 0;
8771                         Close C_CHK_DEL;
8772                         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - can continue') ;
8773                     end if;
8774                  END IF; -- end of chk running_level
8775 
8776 
8777               WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - l_skip='||l_skip) ;
8778               IF (l_skip = 0) THEN
8779 
8780                  nIdx := nIdx + 1;
8781                  WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - OPEN DUNNING:Num'||nIdx||'=========') ;
8782                  WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - New_Task');
8783 
8784                  New_Task(
8785                    p_api_version              => p_api_version
8786                  , p_init_msg_list            => p_init_msg_list
8787                  , p_commit                   => p_commit
8788                  , p_delinquency_id           => l_delinquency_id
8789                  , p_dunning_id               => l_dunning_id
8790                  , p_dunning_object_id        => l_dunning_object_id
8791                  , p_dunning_level            => p_running_level
8792                  , x_task_id                  => l_task_id
8793                  , x_return_status            => x_return_status
8794                  , x_msg_count                => x_msg_count
8795                  , x_msg_data                 => x_msg_data
8796                  );
8797 
8798 
8799                  IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
8800                     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot create new Task');
8801                     FND_FILE.PUT_LINE(FND_FILE.LOG, 'Cannot create new task');
8802                     FND_MSG_PUB.Count_And_Get
8803                     (  p_count          =>   x_msg_count,
8804                        p_data           =>   x_msg_data );
8805                     for i in 1..x_msg_count loop
8806                         errmsg := FND_MSG_PUB.Get(p_msg_index => i,
8807                                                   p_encoded => 'F');
8808                         FND_FILE.PUT_LINE(FND_FILE.LOG, 'Error:'||errmsg);
8809                         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - errmsg=' || errmsg);
8810                     end loop;
8811                     l_skip := 1;
8812                  else
8813                    l_skip := 0;
8814                    WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - taskid='||l_task_id);
8815                    --FND_FILE.PUT_LINE(FND_FILE.LOG, 'task_id='||l_task_id);
8816                    l_task_cnt := l_task_cnt + 1;
8817                  END IF;  -- end of create_task
8818 
8819              /*======================
8820               * Update Dunning
8821               *=====================*/
8822                IF (l_skip = 0) THEN
8823                    nCnt := nCnt + 1;
8824                    dunning_tbl(nCnt) := l_dunning_id;
8825                END IF;
8826 
8827             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end_if (l_skip)');
8828             END IF; -- end of (l_skip)
8829 
8830            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end_if (FoundDunningData)');
8831            END IF; -- end of (FoundDunningData)
8832 
8833            <<end_dunning_loop>>
8834            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - end_dunning_loop');
8835            NULL;
8836 
8837       end loop;  -- end of CURSOR loop
8838 
8839 
8840       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - update_dunnings cnt='||nCnt);
8841 
8842       IF (nCnt > 0) THEN
8843 
8844          FORALL i in 1..dunning_tbl.count
8845 			   UPDATE IEX_DUNNINGS
8846             SET CALLBACK_YN = 'N',
8847                 LAST_UPDATE_DATE = sysdate
8848           WHERE Dunning_id = dunning_tbl(i);
8849 
8850       END IF;
8851 
8852       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Close C_GET_DUNNING');
8853       Close C_Get_DUNNING;
8854 
8855       --
8856       -- End of API body
8857       --
8858 
8859       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ==========SUMMARY=========');
8860       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - TotalOpenDunn='||nIdx);
8861       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - TaskCreatedNum='||l_task_cnt);
8862       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - DunnUpdatedNum='||nCnt);
8863       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - DunnUpdatedNum='||dunning_tbl.count);
8864       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ==========END SUMMARY=========');
8865 
8866       -- Standard check for p_commit
8867       IF FND_API.to_Boolean( p_commit )
8868       THEN
8869           COMMIT WORK;
8870       END IF;
8871 
8872 
8873       --Begin bug 7317666 21-Nov-2008 barathsr
8874       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' Cancelling the Dunning callback tasks correpsonding to current objects...');
8875       l_task_query := 'select tsk.task_id,'
8876                ||'tsk.object_version_number'
8877                ||' from jtf_tasks_b tsk,'
8878                ||' jtf_task_types_tl typ,'
8879                ||' jtf_task_statuses_b st,'
8880                ||' jtf_task_references_b ref,'
8881                ||' iex_dunnings dun'
8882                ||' where tsk.task_type_id=typ.task_type_id'
8883                ||' and typ.name=''Callback'''
8884                ||' and tsk.task_status_id=st.task_status_id'
8885                ||' and  nvl(st.closed_flag,   ''N'') <>''Y'''
8886                ||' and  nvl(st.cancelled_flag,   ''N'')<>''Y'''
8887                ||' and  nvl(st.completed_flag,   ''N'')<>''Y'''
8888                ||' and tsk.task_id=ref.task_id'
8889                ||' and ref.object_type_code=''IEX_DUNNING'''
8890                ||' and ref.object_id=dun.dunning_id'
8891                ||' and dun.dunning_level='''||p_running_level||'''';
8892 
8893       IF (p_running_level = 'CUSTOMER') THEN
8894       l_task_query:=l_task_query||' and not exists(select 1 from iex_delinquencies_all del '
8895                   ||' where  del.status in (''DELINQUENT'',''PREDELINQUENT'') '
8896                   ||' and dun.dunning_object_id=del.party_cust_id)';
8897       ELSIF(p_running_level = 'ACCOUNT') THEN
8898       l_task_query:=l_task_query||' and not exists(select 1 from iex_delinquencies_all del '
8899                   ||' where  del.status in (''DELINQUENT'',''PREDELINQUENT'') '
8900                   ||' and dun.dunning_object_id=del.cust_account_id)';
8901       ELSIF(p_running_level = 'BILL_TO') THEN
8902       l_task_query:=l_task_query||' and not exists(select 1 from iex_delinquencies_all del '
8903                   ||' where  del.status in (''DELINQUENT'',''PREDELINQUENT'') '
8904                   ||' and dun.dunning_object_id=del.customer_site_use_id)';
8905       ELSE
8906       l_task_query:=l_task_query||' and not exists(select 1 from iex_delinquencies_all del '
8907                   ||' where  del.status in (''DELINQUENT'',''PREDELINQUENT'') '
8908                   ||' and dun.dunning_object_id=del.delinquency_id)';
8909       END IF;
8910       WriteLog(G_PKG_NAME || ' ' || l_api_name || 'Query: '||l_task_query);
8911 
8912 	OPEN c_invalid_tasks for l_task_query;
8913 	LOOP
8914 		FETCH c_invalid_tasks INTO l_inv_task_id,l_object_version_number;
8915 		EXIT WHEN c_invalid_tasks%NOTFOUND;
8916 		WriteLog(G_PKG_NAME || ' ' || l_api_name || 'Cancelling callback Task Id:'||l_inv_task_id);
8917                 IF l_inv_task_id IS NOT NULL THEN
8918 			JTF_TASKS_PUB.UPDATE_TASK(
8919 			P_API_VERSION           	=> p_api_version,
8920 		        P_INIT_MSG_LIST         	=> p_init_msg_list,
8921 		        P_COMMIT                	=> p_commit,
8922 			P_OBJECT_VERSION_NUMBER	=> l_object_version_number,
8923 			P_TASK_ID 			=> l_inv_task_id,
8924 			P_TASK_STATUS_NAME		=> 'Cancelled',
8925 			x_return_status		        => x_return_status,
8926 			x_msg_count			=> x_msg_count,
8927 			x_msg_data			=> x_msg_data);
8928 			WriteLog(G_PKG_NAME || ' ' || l_api_name || 'Cancelling callback Task return status:'||x_return_status);
8929 		END IF;
8930 
8931 	END LOOP;
8932 	WriteLog(G_PKG_NAME || ' ' || l_api_name || 'Completed cancelling Dunning callback tasks correpsonding to current objects...');
8933 
8934       IF FND_API.to_Boolean( p_commit )
8935       THEN
8936           COMMIT WORK;
8937       END IF;
8938       --End bug 7317666 21-Nov-2008 barathsr
8939 
8940       -- clchang added 08/15/2003
8941       -- make return_status = 'S';
8942       x_return_status := FND_API.G_RET_STS_SUCCESS;
8943       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - return status='||x_return_status);
8944       WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - END');
8945 
8946       FND_MSG_PUB.Count_And_Get
8947       (  p_count          =>   x_msg_count,
8948          p_data           =>   x_msg_data );
8949 
8950       EXCEPTION
8951           WHEN FND_API.G_EXC_ERROR THEN
8952                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exc Exception');
8953                errmsg := SQLERRM;
8954                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
8955                ROLLBACK TO DAILY_DUNNING_PVT;
8956 	       x_return_status := FND_API.G_RET_STS_ERROR;
8957                FND_MSG_PUB.Count_And_Get
8958                (  p_count          =>   x_msg_count,
8959                   p_data           =>   x_msg_data
8960                 );
8961 
8962           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8963                ROLLBACK TO DAILY_DUNNING_PVT;
8964                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - UnExc Exception');
8965                errmsg := SQLERRM;
8966                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
8967                x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8968                FND_MSG_PUB.Count_And_Get
8969                (  p_count          =>   x_msg_count,
8970                   p_data           =>   x_msg_data
8971                 );
8972 
8973           WHEN OTHERS THEN
8974               ROLLBACK TO DAILY_DUNNING_PVT;
8975                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Others Exception');
8976                errmsg := SQLERRM;
8977                WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Exception errmsg='||errmsg);
8978                x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8979                FND_MSG_PUB.Count_And_Get
8980                (  p_count          =>   x_msg_count,
8981                   p_data           =>   x_msg_data
8982                 );
8983 
8984 END Daily_Dunning;
8985 
8986 
8987 
8988 Procedure NEW_TASK(
8989             p_api_version             IN NUMBER := 1.0,
8990             p_init_msg_list           IN VARCHAR2 ,
8991             p_commit                  IN VARCHAR2 ,
8992             p_delinquency_id          IN NUMBER,
8993             p_dunning_id              IN NUMBER,
8994             p_dunning_object_id       IN NUMBER,
8995             p_dunning_level           IN VARCHAR2,
8996             x_return_status           OUT NOCOPY VARCHAR2,
8997             x_msg_count               OUT NOCOPY NUMBER,
8998             x_msg_data                OUT NOCOPY VARCHAR2,
8999             x_TASK_ID                 OUT NOCOPY NUMBER)
9000 IS
9001     CURSOR C_GET_DEL (IN_del_ID NUMBER) IS
9002       SELECT d.delinquency_ID,
9003              d.party_cust_id,
9004              d.cust_account_id,
9005              a.account_number,
9006              d.customer_site_use_id,
9007              ar.trx_number,
9008              ar.payment_schedule_id,
9009              h.party_name
9010         FROM IEX_DELINQUENCIES d,
9011              HZ_PARTIES h,
9012              HZ_CUST_ACCOUNTS a,
9013              AR_PAYMENT_SCHEDULES ar
9014        WHERE d.delinquency_ID = in_del_ID
9015          AND h.party_id = d.party_cust_id
9016          AND d.cust_account_id = a.cust_account_id
9017          AND ar.payment_schedule_id = d.payment_schedule_id
9018          AND ar.class = 'INV';
9019     --
9020     CURSOR C_GET_SITE (IN_SITE_ID NUMBER) IS
9021       SELECT d.party_cust_id,
9022              d.cust_account_id,
9023              a.account_number,
9024              d.customer_site_use_id,
9025              h.party_name
9026         FROM IEX_DELINQUENCIES d,
9027              HZ_PARTIES h,
9028              HZ_CUST_ACCOUNTS a
9029        WHERE d.customer_site_use_id = in_SITE_ID
9030 	 AND h.party_id = d.party_cust_id
9031          AND d.cust_account_id = a.cust_account_id;
9032     --
9033     CURSOR C_GET_ACCOUNT (IN_ACCT_ID NUMBER) IS
9034       SELECT d.party_cust_id,
9035              d.cust_account_id,
9036              a.account_number,
9037              h.party_name
9038         FROM IEX_DELINQUENCIES d,
9039              HZ_PARTIES h,
9040              HZ_CUST_ACCOUNTS a
9041        WHERE d.cust_account_ID = in_ACCT_ID
9042 	 AND h.party_id = d.party_cust_id
9043          AND d.cust_account_id = a.cust_account_id;
9044     --
9045     CURSOR C_GET_CUSTOMER (IN_PARTY_ID NUMBER) IS
9046       SELECT d.party_cust_id,
9047              h.party_name
9048         FROM IEX_DELINQUENCIES d,
9049              HZ_PARTIES h
9050        WHERE d.party_cust_id = in_party_id
9051          AND h.party_id = d.party_cust_id;
9052     --
9053     l_delinquency_id        NUMBER;
9054     l_dunning_id            NUMBER ;
9055     l_party_cust_id         NUMBER;
9056     l_account_id            NUMBER;
9057     l_account_num           VARCHAR2(30); --NUMBER;
9058     l_customer_site_use_id  NUMBER;
9059     l_payment_schedule_id   NUMBER;
9060     l_trx_number            varchar2(30);
9061     l_resource_id           NUMBER;
9062     l_party_name            varchar2(360);
9063     --
9064     l_task_name             varchar2(80) ;
9065     l_task_type             varchar2(30) ;
9066     l_task_status           varchar2(30) ;
9067     l_description           varchar2(4000);
9068     l_task_priority_name    varchar2(30) ;
9069     l_task_priority_id      number;
9070     l_owner_id              number;
9071     l_owner                 varchar2(4000);
9072     l_owner_type_code       varchar2(4000);
9073     l_customer_id           number;
9074     l_address_id            number;
9075     l_source_object_type_code  varchar2(30);
9076     l_source_object_id         number;
9077     l_source_object_name       varchar2(80);
9078     --
9079     l_task_notes_tbl           JTF_TASKS_PUB.TASK_NOTES_TBL;
9080     l_miss_task_assign_tbl     JTF_TASKS_PUB.TASK_ASSIGN_TBL;
9081     l_miss_task_depends_tbl    JTF_TASKS_PUB.TASK_DEPENDS_TBL;
9082     l_miss_task_rsrc_req_tbl   JTF_TASKS_PUB.TASK_RSRC_REQ_TBL;
9083     l_task_refer_rec           JTF_TASKS_PUB.TASK_REFER_REC;
9084     l_task_refer_tbl           JTF_TASKS_PUB.TASK_REFER_TBL;
9085     l_miss_task_dates_tbl      JTF_TASKS_PUB.TASK_DATES_TBL;
9086     l_miss_task_recur_rec      JTF_TASKS_PUB.TASK_RECUR_REC;
9087     l_miss_task_contacts_tbl   JTF_TASKS_PUB.TASK_CONTACTS_TBL;
9088     --
9089     errmsg                 varchar2(30000);
9090 
9091     --Added for bug#5229763 schekuri 27-Jul-2006
9092     l_resource_tab iex_utilities.resource_tab_type;
9093     l_return_status VARCHAR2(1);
9094     l_msg_count NUMBER;
9095     l_msg_data VARCHAR2(32767);
9096     l_validation_level NUMBER ;
9097     l_default_rs_id  NUMBER := fnd_profile.value('IEX_STRY_DEFAULT_RESOURCE');
9098     l_api_version   NUMBER       := 1.0;
9099 		l_api_name varchar2(25) := 'NEW_TASK';
9100 
9101   BEGIN
9102 
9103     -- Initialize API return status to SUCCESS
9104     l_validation_level := FND_API.G_VALID_LEVEL_FULL;
9105     x_return_status := FND_API.G_RET_STS_SUCCESS;
9106     l_dunning_id := p_dunning_id;
9107 
9108     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - delid='||p_delinquency_id);
9109     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - dunningobjectid='||p_dunning_object_id);
9110     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - dunningLevel='||p_dunning_level);
9111 
9112     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - OPEN CURSOR');
9113 
9114     IF (p_dunning_level = 'ACCOUNT')
9115     THEN
9116         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - OPEN ACCOUNT CURSOR');
9117         Open C_Get_ACCOUNT(p_dunning_object_id);
9118         Fetch C_Get_ACCOUNT into
9119            l_party_cust_id,
9120            l_account_id,
9121            l_account_num,
9122            l_party_name;
9123 
9124         If ( C_GET_ACCOUNT%NOTFOUND) Then
9125             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ACCOUNT NotFound');
9126             x_return_status := FND_API.G_RET_STS_ERROR;
9127             Close C_Get_ACCOUNT;
9128             GOTO end_new_task;
9129         end if;
9130         Close C_Get_ACCOUNT;
9131         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Get ACCNT Data');
9132     --
9133     ELSIF (p_dunning_level = 'CUSTOMER')
9134     THEN
9135         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - OPEN CUSTOMER CURSOR');
9136         Open C_Get_CUSTOMER(p_dunning_object_id);
9137         Fetch C_Get_CUSTOMER into
9138            l_party_cust_id,
9139            l_party_name;
9140 
9141         If ( C_GET_CUSTOMER%NOTFOUND) Then
9142             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - CUSTOMER NotFound');
9143             x_return_status := FND_API.G_RET_STS_ERROR;
9144             Close C_Get_CUSTOMER;
9145             GOTO end_new_task;
9146         end if;
9147         Close C_Get_CUSTOMER;
9148         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Get CUSTOMER Data');
9149     --
9150     -- added for new level 'BILL_TO' in 11.5.10
9151     ELSIF (p_dunning_level = 'BILL_TO')
9152     THEN
9153         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - OPEN BILL_TO CURSOR');
9154         Open C_Get_SITE(p_dunning_object_id);
9155         Fetch C_Get_SITE into
9156            l_party_cust_id,
9157            l_account_id,
9158            l_account_num,
9159            l_customer_site_use_id,
9160            l_party_name;
9161 
9162         If ( C_GET_SITE%NOTFOUND) Then
9163             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - ACCOUNT NotFound');
9164             x_return_status := FND_API.G_RET_STS_ERROR;
9165             Close C_Get_SITE;
9166             GOTO end_new_task;
9167         end if;
9168         Close C_Get_SITE;
9169         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Get SITE Data');
9170     --
9171     ELSIF (p_dunning_level = 'DELINQUENCY')
9172     THEN
9173         Open C_Get_DEL(p_delinquency_id);
9174         Fetch C_Get_DEL into
9175            l_delinquency_id,
9176            l_party_cust_id,
9177            l_account_id,
9178            l_account_num,
9179            l_customer_site_use_id,
9180            l_trx_number,
9181            l_payment_schedule_id,
9182            l_party_name;
9183 
9184         If ( C_GET_DEL%NOTFOUND) Then
9185             WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Del NotFound');
9186             x_return_status := FND_API.G_RET_STS_ERROR;
9187             Close C_Get_DEL;
9188             GOTO end_new_task;
9189         end if;
9190         Close C_Get_DEL;
9191         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Get Del Data');
9192    END IF;
9193 
9194 
9195     If ( l_party_cust_id is null ) Then
9196          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - PartyCustId NotFound');
9197          x_return_status := FND_API.G_RET_STS_ERROR;
9198          GOTO end_new_task;
9199     end if;
9200 
9201   -- clchang updated 09/20/2002 for bug 2242346
9202   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Call GET_RESOURCE');
9203   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - partyid='||l_party_cust_id);
9204   --Begin Bug#5229763 schekuri 27-Jul-2006
9205   --Called iex_utilities.get_assigned_collector to get the resource from hz_customer_profiles table
9206   --instead of as_accesses. Removed the call to "get_resource".
9207   if l_customer_site_use_id is not null then
9208 	iex_utilities.get_assigned_collector(p_api_version => l_api_version,
9209                                p_init_msg_list     => FND_API.G_TRUE,
9210                                p_commit            => FND_API.G_FALSE,
9211                                p_validation_level  => l_validation_level,
9212                                p_level             => 'DUNNING_BILLTO',
9213                                p_level_id          => l_customer_site_use_id,
9214                                x_msg_count         => l_msg_count,
9215                                x_msg_data          => l_msg_data,
9216                                x_return_status     => l_return_status,
9217                                x_resource_tab      => l_resource_tab);
9218 	IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9219 	     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot get resource');
9220 	     x_return_status := FND_API.G_RET_STS_ERROR;
9221 	     GOTO end_new_task;
9222 	END IF;
9223   end if;
9224 
9225   if l_resource_tab.count<1 and l_account_id is not null then
9226 	  iex_utilities.get_assigned_collector(p_api_version => l_api_version,
9227                                p_init_msg_list     => FND_API.G_TRUE,
9228                                p_commit            => FND_API.G_FALSE,
9229                                p_validation_level  => l_validation_level,
9230                                p_level             => 'DUNNING_ACCOUNT',
9231                                p_level_id          => l_account_id,
9232                                x_msg_count         => l_msg_count,
9233                                x_msg_data          => l_msg_data,
9234                                x_return_status     => l_return_status,
9235                                x_resource_tab      => l_resource_tab);
9236 	IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9237 	     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot get resource');
9238 	     x_return_status := FND_API.G_RET_STS_ERROR;
9239 	     GOTO end_new_task;
9240 	END IF;
9241   end if;
9242 
9243   if l_resource_tab.count<1 and l_party_cust_id is not null then
9244 	iex_utilities.get_assigned_collector(p_api_version => l_api_version,
9245                                p_init_msg_list     => FND_API.G_TRUE,
9246                                p_commit            => FND_API.G_FALSE,
9247                                p_validation_level  => l_validation_level,
9248                                p_level             => 'DUNNING_PARTY',
9249                                p_level_id          => l_party_cust_id,
9250                                x_msg_count         => l_msg_count,
9251                                x_msg_data          => l_msg_data,
9252                                x_return_status     => l_return_status,
9253                                x_resource_tab      => l_resource_tab);
9254 	IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9255 	     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot get resource');
9256 	     x_return_status := FND_API.G_RET_STS_ERROR;
9257 	     GOTO end_new_task;
9258 	END IF;
9259 
9260 	if l_resource_tab.count<1 and p_dunning_level = 'CUSTOMER' then
9261 		iex_utilities.get_assigned_collector(p_api_version => l_api_version,
9262 													               p_init_msg_list     => FND_API.G_TRUE,
9263 																				 p_commit            => FND_API.G_FALSE,
9264 																				 p_validation_level  => l_validation_level,
9265 																				 p_level             => 'DUNNING_PARTY_ACCOUNT',
9266 																				 p_level_id          => l_party_cust_id,
9267 																				 x_msg_count         => l_msg_count,
9268 																				 x_msg_data          => l_msg_data,
9269 																				 x_return_status     => l_return_status,
9270 																				 x_resource_tab      => l_resource_tab);
9271 		IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9272 		     WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot get resource');
9273 		     x_return_status := FND_API.G_RET_STS_ERROR;
9274 		     GOTO end_new_task;
9275 		END IF;
9276 	end if;
9277   end if;
9278 
9279   if l_resource_tab.count>0 then
9280     l_resource_id := l_resource_tab(1).resource_id;
9281   else
9282     if l_default_rs_id is not null then
9283        l_resource_id := l_default_rs_id;
9284     else
9285        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9286        WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot get resource from customer profile and there is no default resource set');
9287        FND_FILE.PUT_LINE(FND_FILE.LOG, 'No collector has been assigned to this customer '||l_party_name);
9288        FND_FILE.PUT_LINE(FND_FILE.LOG, 'No default collector available to assign to callback.  Please update default collector.');
9289        GOTO end_new_task;
9290     end if;
9291   end if;
9292   --End Bug#5229763 schekuri 27-Jul-2006
9293 
9294   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - resource_id='||l_resource_id);
9295   WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - dunning_id='||l_dunning_id);
9296 
9297          l_task_name   := 'Dunning Callback';
9298          l_task_status := 'Open';
9299          --l_task_type   := 'Approval'; --'Callback';
9300          l_task_type   := 'Callback';
9301          l_description := 'Oracle Collections Daily Dunnings';
9302          --l_task_priority_name := ;
9303          --l_task_priority_id := ;
9304          l_owner_type_code := 'RS_EMPLOYEE';
9305          l_owner_id := l_resource_id;
9306          l_customer_id := l_party_cust_id;
9307 
9308          -- clchang updated 04/09/2003 bug 2872385
9309          -- source object code should be based on the dunning level;
9310          -- ( in order to be seen in Collections Task tab )
9311          -- p.s. Collectons Task tab is based on the ViewBy;
9312          --      ViewBy is PARTY => only see tasks with source_object_code PARTY
9313          --      ViewBy is ACCOUNT => see tasks with source_object_code ACCOUNT
9314          --      ViewBy is DELINQUENCY => only see tasks with source_object_code DELINQUENCY
9315 
9316          --
9317          -- clchang updated 04/21/2003 for BILL_TO
9318          --
9319          --
9320          --
9321          IF (p_dunning_level = 'CUSTOMER') THEN
9322              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - partyid='||l_party_cust_id);
9323              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - partyname='||l_party_name);
9324              --
9325              l_source_object_type_code := 'PARTY';
9326              l_source_object_id := l_party_cust_id;
9327              l_source_object_name := l_party_name;
9328              --
9329              l_task_refer_rec.object_id := l_dunning_id;
9330              l_task_refer_rec.object_name := l_dunning_id;
9331              l_task_refer_rec.object_type_code := 'IEX_DUNNING';
9332              l_task_refer_tbl(1) := l_task_refer_rec;
9333 
9334          ELSIF (p_dunning_level = 'ACCOUNT') THEN
9335              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - partyid='||l_party_cust_id);
9336              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - partyname='||l_party_name);
9337              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - accntnum='||l_account_num);
9338              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - accntid='||l_account_id);
9339              --
9340              l_source_object_type_code := 'IEX_ACCOUNT';
9341              l_source_object_id := l_account_id;
9342              l_source_object_name := l_account_num;
9343              --
9344              l_task_refer_rec.object_id := l_party_cust_id;
9345              l_task_refer_rec.object_name := l_party_name;
9346              l_task_refer_rec.object_type_code := 'PARTY';
9347              l_task_refer_tbl(1) := l_task_refer_rec;
9348              l_task_refer_rec.object_id := l_dunning_id;
9349              l_task_refer_rec.object_name := l_dunning_id;
9350              l_task_refer_rec.object_type_code := 'IEX_DUNNING';
9351              l_task_refer_tbl(2) := l_task_refer_rec;
9352 
9353          ELSIF (p_dunning_level = 'BILL_TO') THEN
9354              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - partyid='||l_party_cust_id);
9355              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - partyname='||l_party_name);
9356              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - accntnum='||l_account_num);
9357              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - accntid='||l_account_id);
9358              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - customer_site_use_id='||l_customer_site_use_id);
9359              --
9360              l_source_object_type_code := 'IEX_BILLTO';
9361              l_source_object_id := l_customer_site_use_id;
9362              l_source_object_name := l_customer_site_use_id;
9363              --
9364              l_task_refer_rec.object_id := l_party_cust_id;
9365              l_task_refer_rec.object_name := l_party_name;
9366              l_task_refer_rec.object_type_code := 'PARTY';
9367              l_task_refer_tbl(1) := l_task_refer_rec;
9368              l_task_refer_rec.object_id := l_account_id;
9369              l_task_refer_rec.object_name := l_account_num;
9370              l_task_refer_rec.object_type_code := 'IEX_ACCOUNT';
9371              l_task_refer_tbl(2) := l_task_refer_rec;
9372              l_task_refer_rec.object_id := l_dunning_id;
9373              l_task_refer_rec.object_name := l_dunning_id;
9374              l_task_refer_rec.object_type_code := 'IEX_DUNNING';
9375              l_task_refer_tbl(3) := l_task_refer_rec;
9376 
9377          ELSE
9378              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - partyid='||l_party_cust_id);
9379              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - partyname='||l_party_name);
9380              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - accntnum='||l_account_num);
9381              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - accntid='||l_account_id);
9382              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - customer_site_use_id='||l_customer_site_use_id);
9383              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - delid='||l_delinquency_id);
9384              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - trxnum='||l_trx_number);
9385              WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - paymentshcheid='||l_payment_schedule_id);
9386              --
9387              l_source_object_type_code := 'IEX_DELINQUENCY';
9388              l_source_object_id := l_delinquency_id;
9389              l_source_object_name := l_delinquency_id;
9390              --
9391              l_task_refer_rec.object_id := l_party_cust_id;
9392              l_task_refer_rec.object_name := l_party_name;
9393              l_task_refer_rec.object_type_code := 'PARTY';
9394              l_task_refer_tbl(1) := l_task_refer_rec;
9395              l_task_refer_rec.object_id := l_account_id;
9396              l_task_refer_rec.object_name := l_account_num;
9397              l_task_refer_rec.object_type_code := 'IEX_ACCOUNT';
9398              l_task_refer_tbl(2) := l_task_refer_rec;
9399              l_task_refer_rec.object_id := l_customer_site_use_id;
9400              l_task_refer_rec.object_name := l_customer_site_use_id;
9401              l_task_refer_rec.object_type_code := 'IEX_BILLTO';
9402              l_task_refer_tbl(3) := l_task_refer_rec;
9403              l_task_refer_rec.object_id := l_payment_schedule_id;
9404              l_task_refer_rec.object_name := l_trx_number;
9405              l_task_refer_rec.object_type_code := 'IEX_INVOICES';
9406              l_task_refer_tbl(4) := l_task_refer_rec;
9407              l_task_refer_rec.object_id := l_dunning_id;
9408              l_task_refer_rec.object_name := l_dunning_id;
9409              l_task_refer_rec.object_type_code := 'IEX_DUNNING';
9410              l_task_refer_tbl(5) := l_task_refer_rec;
9411 
9412          END IF;
9413 
9414          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - refer_tbl_cnt='||l_task_refer_tbl.count);
9415          WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Create Task');
9416 
9417          JTF_TASKS_PUB.CREATE_TASK(
9418             p_api_version           => p_api_version,
9419             p_init_msg_list         => p_init_msg_list,
9420             p_commit                => p_commit,
9421             p_task_name             => l_task_name,
9422             p_task_type_name        => l_task_type,
9423             p_task_status_name      => l_task_status,
9424             p_owner_type_code       => l_owner_type_code,
9425             p_owner_id              => l_owner_id,
9426             p_description           => l_description,
9427             p_customer_id           => l_customer_id,
9428             p_source_object_type_code => l_source_object_type_code,
9429             p_source_object_id      => l_source_object_id,
9430             p_source_object_name    => l_source_object_name,
9431             p_task_assign_tbl       => l_miss_task_assign_tbl,
9432             p_task_depends_tbl      => l_miss_task_depends_tbl,
9433             p_task_rsrc_req_tbl     => l_miss_task_rsrc_req_tbl,
9434             p_task_refer_tbl        => l_task_refer_tbl,
9435             p_task_dates_tbl        => l_miss_task_dates_tbl,
9436             p_task_notes_tbl        => l_task_notes_tbl,
9437             p_task_recur_rec        => l_miss_task_recur_rec,
9438             p_task_contacts_tbl     => l_miss_task_contacts_tbl,
9439             x_return_status         => x_return_status,
9440             x_msg_count             => x_msg_count,
9441             x_msg_data              => x_msg_data,
9442             x_task_id               => x_task_id );
9443 
9444 
9445      <<end_new_task>>
9446      IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9447         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Cannot Create Task');
9448         FND_MSG_PUB.Count_And_Get
9449         (  p_count          =>   x_msg_count,
9450            p_data           =>   x_msg_data
9451         );
9452         for i in 1..x_msg_count loop
9453            errmsg := FND_MSG_PUB.Get(p_msg_index => i,
9454                                      p_encoded => 'F');
9455            WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - errmsg='||errmsg);
9456         end loop;
9457 
9458      ELSE
9459         WriteLog(G_PKG_NAME || ' ' || l_api_name || ' - Task-taskid='||x_task_id);
9460 
9461      END IF;
9462 
9463   END NEW_TASK;
9464 
9465 
9466 
9467 /*========================================================================
9468  * Clchang updated 09/19/2002 for Bug 2242346
9469  *   to create a callback,
9470  *      we got resource_id from iex_delinquencyies before;
9471  *      now, we get resource_id based on which agent owns the least tasks
9472  *           in PARTY level;
9473  *
9474  *      Based on the party_id:
9475  *      1. taskcount:
9476  *         get the count(task) for the existing resources in jtf_tasks_vl;
9477  *      2. notaskcount:
9478  *         get all available resource_id for this party,
9479  *         and put count as 0 for each resource;
9480  *      3. if taskcount = 0,
9481  *            if notaskcount > 0,
9482  *               task the first resource_id based on notask_cursor;
9483  *            if notaskcount = 0,
9484  *               get the default resource_id;
9485  *      4. if taskcount > 0,
9486  *            get the resource_id which has the least tasks;
9487  *
9488  *========================================================================*/
9489 --Removed all the code to replace the usage of this procedure with iex_utilities.get_assigned_collector
9490 --for bug#5229763 schekuri 27-Jul-2006
9491 --iex_utilities.get_assigned_collector uses hz_customer_profiles to get resource instead of as_accesses
9492 PROCEDURE Get_Resource(p_api_version   IN  NUMBER,
9493                        p_commit        IN  VARCHAR2,
9494                        p_init_msg_list           IN VARCHAR2 ,
9495                        p_party_id      IN  NUMBER,
9496                        x_resource_id   OUT NOCOPY NUMBER,
9497                        x_return_status OUT NOCOPY VARCHAR2,
9498                        x_msg_count     OUT NOCOPY NUMBER,
9499                        x_msg_data      OUT NOCOPY VARCHAR2)
9500 IS
9501  /* l_api_name                    VARCHAR2(50) ;
9502   l_RETURN_STATUS               VARCHAR2(30) ;
9503   l_MSG_COUNT                   NUMBER      ;
9504   l_MSG_DATA                    VARCHAR2(100) ;
9505   l_api_version                 NUMBER := 1.0;
9506 
9507   l_default_rs_id  number ;
9508   l_login  number;
9509   l_user   NUMBER ;
9510 
9511   CURSOR c_chk_party (p_party_id NUMBER) IS
9512       SELECT customer_id
9513         FROM jtf_tasks_vl
9514        WHERE customer_id = p_party_id;*/
9515 
9516   /*===================================================
9517   --  from iexvutlb.pls -  get_assign_resources procedure
9518   --  the resources should include manager;
9519   --------------------------------------------------------
9520 
9521   CURSOR c_get_person IS
9522     SELECT DISTINCT person_id, salesforce_id
9523     FROM  as_accesses acc, jtf_rs_resource_extns rs
9524     WHERE acc.customer_id = p_party_id and rs.resource_id = acc.salesforce_id
9525       and rs.user_id is not null;
9526 
9527   -- clchang 02/26/2003 updated
9528   -- updated the following cursors based on the above cursor;
9529   ----------------------------------------------------------------
9530 
9531   CURSOR c_party_resource_OLD(p_party_id NUMBER) IS
9532       SELECT DISTINCT rs.resource_id, 0
9533         FROM as_rpt_managers_v m, as_accesses acc, jtf_rs_resource_extns rs
9534        WHERE m.person_id = acc.person_id
9535          AND m.manager_person_id = rs.source_id
9536          AND acc.customer_id = p_party_id
9537          AND nvl(rs.end_date_active,sysdate) >= trunc(sysdate);
9538 
9539   CURSOR c_resource_task_count_OLD(p_party_id NUMBER) IS
9540       SELECT t.owner_id, count(t.owner_id)
9541         FROM jtf_tasks_vl t, jtf_task_statuses_vl s
9542        WHERE t.customer_id = p_party_id
9543          AND upper(t.owner_type_code) = 'RS_EMPLOYEE'
9544          AND t.task_name = 'Dunning Callback'
9545          AND t.task_status_id = s.task_status_id
9546          AND upper(s.name) = 'OPEN'
9547          AND t.owner_id in ( select DISTINCT rs.resource_id
9548                              from as_rpt_managers_v m, as_accesses acc,
9549                                   jtf_rs_resource_extns rs
9550                             where m.person_id = acc.person_id
9551                               and m.manager_person_id = rs.source_id
9552                               and acc.customer_id = p_party_id
9553                               AND nvl(rs.end_date_active,sysdate) >= trunc(sysdate) )
9554        GROUP BY t.owner_id;
9555 
9556   CURSOR c_resource_notask_count_OLD(p_party_id NUMBER) IS
9557       SELECT DISTINCT rs.resource_id, 0
9558         FROM as_rpt_managers_v m, as_accesses acc, jtf_rs_resource_extns rs
9559        WHERE m.person_id = acc.person_id
9560          AND m.manager_person_id = rs.source_id
9561          AND acc.customer_id = p_party_id
9562          AND nvl(rs.end_date_active,sysdate) >= trunc(sysdate)
9563          AND rs.resource_id not in ( select distinct t2.owner_id
9564                                        from jtf_tasks_vl t2,
9565                                             jtf_task_statuses_vl s2
9566                                       where t2.customer_id = p_party_id
9567                                        and upper(t2.owner_type_code) = 'RS_EMPLOYEE'
9568                                        AND t2.task_status_id = s2.task_status_id
9569                                        AND upper(s2.name) = 'OPEN'
9570                                        and t2.task_name = 'Dunning Callback');
9571 
9572 
9573   CURSOR c_resource_mintask(p_party_id NUMBER) IS
9574       SELECT t.owner_id  --, count(t.owner_id)
9575         FROM jtf_tasks_vl t, jtf_task_statuses_vl s
9576        WHERE t.customer_id = p_party_id
9577          AND upper(t.owner_type_code) = 'RS_EMPLOYEE'
9578          AND t.task_name = 'Dunning Callback'
9579          AND t.task_status_id = s.task_status_id
9580          AND upper(s.name) = 'OPEN'
9581        GROUP BY t.owner_id
9582       HAVING COUNT(t.owner_id) = (select min(count(t2.owner_id))
9583                                   from jtf_tasks_vl t2, jtf_task_statuses_vl s2
9584                                  WHERE t2.customer_id = p_party_id
9585                                    AND upper(t2.owner_type_code) = 'RS_EMPLOYEE'
9586                                    AND t2.task_name = 'Dunning Callback'
9587                                    AND t2.task_status_id = s2.task_status_id
9588                                    AND upper(s2.name) = 'OPEN'
9589                                  group by t2.owner_id );
9590   *=================================================== */
9591 
9592   /*CURSOR c_resource_mintask(p_party_id NUMBER) IS
9593       SELECT t.owner_id  --, count(t.owner_id)
9594         FROM jtf_tasks_vl t, jtf_task_statuses_vl s
9595        WHERE t.customer_id = p_party_id
9596          AND upper(t.owner_type_code) = 'RS_EMPLOYEE'
9597          AND t.task_name = 'Dunning Callback'
9598          AND t.task_status_id = s.task_status_id
9599          AND upper(s.name) = 'OPEN'
9600          AND t.owner_id in ( select DISTINCT rs.resource_id
9601                              from as_accesses acc,
9602                                   jtf_rs_resource_extns rs
9603                             where acc.customer_id = p_party_id
9604                               and rs.resource_id = acc.salesforce_id
9605                               and rs.category = 'EMPLOYEE'
9606                               and rs.user_id is not null
9607                               AND nvl(rs.end_date_active,sysdate) >= trunc(sysdate) )
9608        GROUP BY t.owner_id
9609       HAVING COUNT(t.owner_id) = (select min(count(t2.owner_id))
9610                                   from jtf_tasks_vl t2, jtf_task_statuses_vl s2
9611                                  WHERE t2.customer_id = p_party_id
9612                                    AND upper(t2.owner_type_code) = 'RS_EMPLOYEE'
9613                                    AND t2.task_name = 'Dunning Callback'
9614                                    AND t2.task_status_id = s2.task_status_id
9615                                    AND upper(s2.name) = 'OPEN'
9616                                    AND t2.owner_id in ( select DISTINCT rs.resource_id
9617                                        from as_accesses acc,
9618                                             jtf_rs_resource_extns rs
9619                                       where acc.customer_id = p_party_id
9620                                         and rs.resource_id = acc.salesforce_id
9621                                         and rs.category = 'EMPLOYEE'
9622                                         and rs.user_id is not null
9623                                         AND nvl(rs.end_date_active,sysdate) >= trunc(sysdate) )
9624                                  group by t2.owner_id );
9625   --
9626   --
9627   CURSOR c_resource_task_count(p_party_id NUMBER) IS
9628       SELECT t.owner_id,  count(t.owner_id)
9629         FROM jtf_tasks_vl t, jtf_task_statuses_vl s
9630        WHERE t.customer_id = p_party_id
9631          AND upper(t.owner_type_code) = 'RS_EMPLOYEE'
9632          AND t.task_name = 'Dunning Callback'
9633          AND t.task_status_id = s.task_status_id
9634          AND upper(s.name) = 'OPEN'
9635          AND t.owner_id in ( select DISTINCT rs.resource_id
9636                              from as_accesses acc,
9637                                   jtf_rs_resource_extns rs
9638                             where acc.customer_id = p_party_id
9639                               and rs.resource_id = acc.salesforce_id
9640                               and rs.category = 'EMPLOYEE'
9641                               and rs.user_id is not null
9642                               AND nvl(rs.end_date_active,sysdate) >= trunc(sysdate) )
9643        GROUP BY t.owner_id;
9644   --
9645   CURSOR c_resource_notask_count(p_party_id NUMBER) IS
9646       SELECT DISTINCT rs.resource_id, 0
9647         FROM as_accesses acc, jtf_rs_resource_extns rs
9648        WHERE acc.customer_id = p_party_id
9649          AND nvl(rs.end_date_active,sysdate) >= trunc(sysdate)
9650          AND rs.resource_id = acc.salesforce_id
9651          and rs.category = 'EMPLOYEE'
9652          AND rs.user_id is not null
9653          AND rs.resource_id not in ( select distinct t2.owner_id
9654                                        from jtf_tasks_vl t2,
9655                                             jtf_task_statuses_vl s2
9656                                       where t2.customer_id = p_party_id
9657                                        and upper(t2.owner_type_code) = 'RS_EMPLOYEE'
9658                                        AND t2.task_status_id = s2.task_status_id
9659                                        AND upper(s2.name) = 'OPEN'
9660                                        and t2.task_name = 'Dunning Callback');
9661 
9662   --
9663   TYPE number_tab_type IS TABLE OF NUMBER;
9664   l_p_rs_task_id_tab number_tab_type;
9665   l_p_rs_task_cnt_tab number_tab_type;
9666   l_p_rs_notask_id_tab number_tab_type;
9667   l_p_rs_notask_cnt_tab number_tab_type;
9668 
9669   l_errmsg varchar2(1000);
9670   i number := 0;
9671   l_party_id number := 0;
9672   l_task_count number := 0;
9673   l_notask_count number := 0;
9674   l_resource_id number := 0;*/
9675 
9676 BEGIN
9677 
9678   /*l_api_name   := 'Get_Resource';
9679   l_default_rs_id  := fnd_profile.value('IEX_STRY_DEFAULT_RESOURCE');
9680   l_login  := fnd_global.login_id;
9681   l_user   := FND_GLOBAL.USER_ID;
9682 
9683 --  IF PG_DEBUG < 10  THEN
9684   IF (FND_LOG.LEVEL_EVENT >= PG_DEBUG) THEN
9685      iex_debug_pub.LogMessage ('Get_Resource: ' || 'iexvdunb.pls:GetRS:starting');
9686   END IF;
9687 
9688 
9689   -- Standard Start of API savepoint
9690   SAVEPOINT GET_RESOURCE_PVT;
9691 
9692   -- Standard call to check for call compatibility.
9693   IF NOT FND_API.Compatible_API_Call (l_api_version,
9694                                       p_api_version,
9695                                       l_api_name,
9696                                       G_PKG_NAME)
9697   THEN
9698       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9699   END IF;
9700 
9701 
9702   -- Initialize API return status to SUCCESS
9703   x_return_status := FND_API.G_RET_STS_SUCCESS;
9704 
9705   -- Api body
9706   --
9707 
9708   WriteLog('iexvdunb.pls:GetRS:party_id='||p_party_id);
9709 
9710   -- get all resources which have tasks for this party
9711   WriteLog('iexvdunb.pls:GetRS:Get Resources with tasks');
9712   OPEN c_resource_task_count(p_party_id);
9713   FETCH c_resource_task_count
9714    BULK COLLECT INTO l_p_rs_task_id_tab, l_p_rs_task_cnt_tab;
9715   CLOSE c_resource_task_count;
9716   WriteLog('iexvdunb.pls:GetRS:Close GetRs Task count');
9717 
9718   l_task_count := l_p_rs_task_id_tab.count;
9719   WriteLog('iexvdunb.pls:GetRS:RsTaskCount='||l_task_count);
9720    if (l_task_count > 0) then
9721        for i in 1..l_task_count loop
9722            WriteLog('iexvdunb.pls:GetRS:rs_task-i='||i);
9723            WriteLog('iexvdunb.pls:GetRS:id='||l_p_rs_task_id_tab(i));
9724            WriteLog('iexvdunb.pls:GetRS:id='||l_p_rs_task_cnt_tab(i));
9725        end loop;
9726    end if;
9727 
9728   --
9729   -- get all resources which have no task for this party
9730   WriteLog('iexvdunb.pls:GetRS:Get Resources withnot task');
9731   OPEN c_resource_notask_count(p_party_id);
9732   FETCH c_resource_notask_count
9733    BULK COLLECT INTO l_p_rs_notask_id_tab, l_p_rs_notask_cnt_tab;
9734   CLOSE c_resource_notask_count;
9735   WriteLog('iexvdunb.pls:GetRS:Close Get ResourceNoTask count');
9736 
9737   l_notask_count := l_p_rs_notask_id_tab.count;
9738   WriteLog('iexvdunb.pls:GetRS:RsNoTaskCount='||l_notask_count);
9739 
9740   --
9741 
9742   if (l_notask_count > 0 )  then
9743       -- it means there're some resources haven't been assigned tasks yet
9744       l_resource_id := l_p_rs_notask_id_tab(1);
9745       WriteLog('iexvdunb.pls:GetRS:resource_id='||l_resource_id);
9746       for i in 1..l_notask_count loop
9747          WriteLog('iexvdunb.pls:GetRS:rs_notask-i='||i);
9748          WriteLog('iexvdunb.pls:GetRS:id='||l_p_rs_notask_id_tab(i));
9749          WriteLog('iexvdunb.pls:GetRS:cnt='||l_p_rs_notask_cnt_tab(i));
9750       end loop;
9751   else
9752       -- l_notask_count = 0
9753       if (l_task_count > 0) then
9754           -- all resources have tasks
9755           -- get resource_id which has the least tasks based on query
9756           OPEN c_resource_mintask(p_party_id);
9757           FETCH c_resource_mintask into l_resource_id;
9758           CLOSE c_resource_mintask;
9759       else
9760           -- l_task_count = 0 and l_notask_count = 0
9761           -- no available resource based on party_resource relationship
9762           -- get the default resource
9763           l_resource_id := l_default_rs_id;
9764       end if;
9765       WriteLog('iexvdunb.pls:GetRS:resource_id='||l_resource_id);
9766   end if;
9767 
9768   --
9769   WriteLog('iexvdunb.pls:GetRS:END_RESOURCE');
9770   x_resource_id := l_resource_id;
9771   WriteLog('iexvdunb.pls:GetRS:x_resource_id='||x_resource_id);
9772 
9773   --
9774   -- End of API body
9775   --
9776 
9777   -- Standard check for p_commit
9778   IF FND_API.to_Boolean( p_commit )
9779   THEN
9780      COMMIT WORK;
9781   END IF;
9782 
9783   WriteLog('iexvdunb.pls:GetRS:END');
9784 
9785   -- Standard call to get message count and if count is 1, get message info.
9786   FND_MSG_PUB.Count_And_Get
9787   (  p_count          =>   x_msg_count,
9788      p_data           =>   x_msg_data
9789   );
9790 
9791       EXCEPTION
9792           WHEN FND_API.G_EXC_ERROR THEN
9793              ROLLBACK TO GET_RESOURCE_PVT;
9794              x_return_status := FND_API.G_RET_STS_ERROR;
9795              l_errmsg := SQLERRM;
9796              WriteLog('iexvdunb:GetRs-G_EXC_EXCEPTION::' || l_errmsg);
9797              FND_MSG_PUB.Count_And_Get
9798              (  p_count          =>   x_msg_count,
9799                 p_data           =>   x_msg_data
9800              );
9801 
9802           WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
9803              ROLLBACK TO GET_RESOURCE_PVT;
9804              x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9805              l_errmsg := SQLERRM;
9806              WriteLog('iexvdunb:GetRs-G_EXC_UNEXP_EXCEPTION:OTHERS:' || l_errmsg);
9807              FND_MSG_PUB.Count_And_Get
9808              (  p_count          =>   x_msg_count,
9809                 p_data           =>   x_msg_data
9810              );
9811 
9812           WHEN OTHERS THEN
9813              ROLLBACK TO GET_RESOURCE_PVT;
9814              x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9815              l_errmsg := SQLERRM;
9816              WriteLog('iexvdunb:GetRs-EXCEPTION:OTHERS:' || l_errmsg);
9817              FND_MSG_PUB.Count_And_Get
9818              (  p_count          =>   x_msg_count,
9819                 p_data           =>   x_msg_data
9820              );*/
9821 NULL;
9822 
9823 
9824 END GET_RESOURCE;
9825 
9826 
9827 FUNCTION Party_currency_code(p_party_id NUMBER) RETURN VARCHAR2
9828 IS
9829   cursor c_amount (IN_ID number) is
9830      select ps.invoice_currency_code
9831        from ar_payment_schedules_all ps,
9832             iex_delinquencies del
9833       where ps.payment_schedule_id (+)= del.payment_schedule_id
9834         and del.party_cust_id = in_id;
9835   l_code VARCHAR2(15);
9836 BEGIN
9837   WriteLog ('Party_currency_code: ' || 'currency_code');
9838   WriteLog ('Party_currency_code: ' || 'party_id='||p_party_id);
9839   open c_amount(p_party_id);
9840   fetch c_amount into l_code;
9841   if c_amount%found then
9842      WriteLog('Party_currency_code: ' || 'code='||l_code);
9843   else
9844      WriteLog ('Party_currency_code: ' || 'notfound');
9845     l_code := '';
9846   END if;
9847   close c_amount;
9848 
9849   RETURN l_code;
9850 
9851 END party_currency_code;
9852 
9853 
9854 FUNCTION acct_currency_code(p_account_id NUMBER) RETURN VARCHAR2
9855 IS
9856   cursor c_amount (IN_ID number) is
9857      select ps.invoice_currency_code
9858        from ar_payment_schedules_all ps,
9859             --iex_delinquencies_all del
9860             iex_delinquencies del
9861       where ps.payment_schedule_id (+)= del.payment_schedule_id
9862         and del.cust_account_id = in_id;
9863   l_code VARCHAR2(15);
9864 BEGIN
9865   WriteLog('acct_currency_code: ' || 'currency_code');
9866   WriteLog('acct_currency_code: ' || 'account_id='||p_account_id);
9867   open c_amount(p_account_id);
9868   fetch c_amount into l_code;
9869   if c_amount%found then
9870     WriteLog('acct_currency_code: ' || 'code='||l_code);
9871   else
9872     WriteLog ('acct_currency_code: ' || 'notfound');
9873     l_code := '';
9874   END if;
9875   close c_amount;
9876 
9877   RETURN l_code;
9878 
9879 END acct_currency_code;
9880 
9881 
9882 FUNCTION site_currency_code(p_customer_site_use_id NUMBER) RETURN VARCHAR2
9883 IS
9884   cursor c_amount (IN_ID number) is
9885      select ps.invoice_currency_code
9886        from ar_payment_schedules_all ps,
9887             iex_delinquencies del
9888       where ps.payment_schedule_id (+)= del.payment_schedule_id
9889         and del.customer_site_use_id = in_id;
9890 
9891   l_code VARCHAR2(15);
9892 
9893 BEGIN
9894   WriteLog ('site_currency_code: ' || 'currency_code');
9895   WriteLog ('site_currency_code: ' || 'customer_site_use_id='||p_customer_site_use_id);
9896   open c_amount(p_customer_site_use_id);
9897   fetch c_amount into l_code;
9898   if c_amount%found then
9899      WriteLog ('site_currency_code: ' || 'code='||l_code);
9900   else
9901      WriteLog('site_currency_code: ' || 'notfound');
9902      l_code := '';
9903   END if;
9904   close c_amount;
9905 
9906   RETURN l_code;
9907 
9908 END site_currency_code;
9909 
9910 
9911 FUNCTION party_amount_due_remaining(p_party_id NUMBER) RETURN NUMBER
9912 IS
9913   cursor c_amount (IN_ID number) is
9914      select sum(ps.acctd_amount_due_remaining)
9915        from ar_payment_schedules_all ps,
9916             iex_delinquencies del
9917       where ps.payment_schedule_id (+)= del.payment_schedule_id
9918         and del.party_cust_id = in_id;
9919   l_sum number;
9920 BEGIN
9921   WriteLog('party_amount_due_remaining: ' || 'party_amount_due_remainging');
9922   WriteLog('party_amount_due_remaining: ' || 'party_id='||p_party_id);
9923   open c_amount(p_party_id);
9924   fetch c_amount into l_sum;
9925   if c_amount%found then
9926     WriteLog('party_amount_due_remaining: ' || 'sum='||l_sum);
9927   else
9928     WriteLog('party_amount_due_remaining: ' || 'notfound');
9929     l_sum := 0;
9930   END if;
9931   close c_amount;
9932 
9933   RETURN l_sum;
9934 
9935 END party_amount_due_remaining;
9936 
9937 
9938 FUNCTION acct_amount_due_remaining(p_account_id NUMBER) RETURN NUMBER
9939 IS
9940   cursor c_amount (IN_ID number) is
9941      select sum(ps.acctd_amount_due_remaining)
9942        from ar_payment_schedules_all ps,
9943             --iex_delinquencies_all del
9944             iex_delinquencies del
9945       where ps.payment_schedule_id (+)= del.payment_schedule_id
9946         and del.cust_account_id = in_id;
9947   l_sum number;
9948 BEGIN
9949   WriteLog('acct_amount_due_remaining: ' || 'acct_amount_due_remainging');
9950   WriteLog('acct_amount_due_remaining: ' || 'account_id='||p_account_id);
9951   open c_amount(p_account_id);
9952   fetch c_amount into l_sum;
9953   if c_amount%found then
9954      WriteLog ('acct_amount_due_remaining: ' || 'sum='||l_sum);
9955   else
9956      WriteLog ('acct_amount_due_remaining: ' || 'notfound');
9957     l_sum := 0;
9958   END if;
9959   close c_amount;
9960 
9961   RETURN l_sum;
9962 
9963 END acct_amount_due_remaining;
9964 
9965 
9966 FUNCTION site_amount_due_remaining(p_customer_site_use_id NUMBER) RETURN NUMBER
9967 IS
9968   cursor c_amount (IN_ID number) is
9969      select sum(ps.acctd_amount_due_remaining)
9970        from ar_payment_schedules_all ps,
9971             iex_delinquencies del
9972       where ps.payment_schedule_id (+)= del.payment_schedule_id
9973         and del.customer_site_use_id = in_id;
9974   l_sum number;
9975 --
9976 BEGIN
9977   --
9978   WriteLog ('site_amount_due_remaining: ' || 'site_amount_due_remainging');
9979   WriteLog('site_amount_due_remaining: ' || 'customer_site_use_id='||p_customer_site_use_id);
9980   --
9981   open c_amount(p_customer_site_use_id);
9982   fetch c_amount into l_sum;
9983   if c_amount%found then
9984      WriteLog ('site_amount_due_remaining: ' || 'sum='||l_sum);
9985   else
9986      WriteLog ('site_amount_due_remaining: ' || 'notfound');
9987      l_sum := 0;
9988   END if;
9989   close c_amount;
9990   --
9991   RETURN l_sum;
9992 
9993 END site_amount_due_remaining;
9994 
9995 
9996 FUNCTION get_party_id(p_account_id NUMBER) RETURN NUMBER
9997 IS
9998   cursor c_party (IN_ID number) is
9999      select del.party_cust_id
10000        from iex_delinquencies del
10001       where del.cust_account_id = in_id;
10002   l_party number;
10003 BEGIN
10004   WriteLog('get_party_id');
10005   WriteLog('get_party_id: ' || 'account_id='||p_account_id);
10006   open c_party(p_account_id);
10007   fetch c_party into l_party;
10008   if c_party%found then
10009      WriteLog ('get_party_id: ' || 'party='||l_party);
10010   else
10011      WriteLog ('get_party_id: ' || 'notfound');
10012     l_party := 0;
10013   END if;
10014   close c_party;
10015 
10016   RETURN l_party;
10017 
10018 END get_party_id;
10019 
10020 
10021 BEGIN
10022   PG_DEBUG  := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
10023 
10024 END IEX_DUNNING_PVT;