DBA Data[Home] [Help]

PACKAGE BODY: APPS.GMF_RCV_ACCOUNTING_PKG

Source


1 PACKAGE BODY GMF_RCV_ACCOUNTING_PKG AS
2 /* $Header: gmfrcvab.pls 120.7 2010/08/27 13:19:43 uphadtar ship $ */
3 
4   /**************************
5   * Package Level Constants *
6   **************************/
7   MODULE  CONSTANT VARCHAR2(80) := 'gmf.plsql.gmf_rcv_accounting_pkg';
8   G_PKG_NAME CONSTANT VARCHAR2(30) := 'GMF_RCV_ACCOUNTING_PKG';
9   G_DEBUG CONSTANT VARCHAR2(10) := NVL(FND_PROFILE.VALUE('AFLOG_ENABLED'),'N');
10   G_LOG_HEAD CONSTANT VARCHAR2(40) := 'gmf.plsql.'||G_PKG_NAME;
11 
12   /**********************************************************************************************
13   * API name                                                                                    *
14   *   Get_TransactionAmount                                                                     *
15   * Type                                                                                        *
16   *   Private                                                                                   *
17   * Function                                                                                    *
18   *   Returns the transaction amount. Used for service line types.                              *
19   * Pre-reqs                                                                                    *
20   *                                                                                             *
21   * Parameters                                                                                  *
22   *   IN                                                                                        *
23   *     p_api_version           IN NUMBER       Required                                        *
24   *     p_init_msg_list         IN VARCHAR2     Optional Default = FND_API.G_FALSE              *
25   *     p_commit                IN VARCHAR2     Optional Default = FND_API.G_FALSE              *
26   *     p_validation_level      IN NUMBER       Optional Default = FND_API.G_VALID_LEVEL_FULL   *
27   *     p_rcv_accttxn             IN rcv_accttxn_rec_type       Required         *
28   *   OUT                                                                                       *
29   *     x_return_status         OUT     VARCHAR2(1)                                             *
30   *     x_msg_count             OUT     NUMBER                                                  *
31   *     x_msg_data              OUT     VARCHAR2(2000)                                          *
32   *     x_transaction_amount    OUT     NUMBER                                                  *
33   * Version                                                                                     *
34   *   1.0                                                                                       *
35   * Description                                                                                 *
36   *   This API returns the transaction amount. It should only be called for service line types. *
37   *                                                                                             *
38   * 22-MAY-2009 Uday Phadtare Bug 8517463. Code modified in PROCEDURE create_deliver_txns.      *
39   *   Passed proper event_type_id in case of correction to EXPENSE destination type             *
40   * 31-Jul-2009 Prasad LCM-OPM Integration, populating unit landed cost value in grat table     *
41   * 27-AUG-2010 Uday Phadtare Bug 9792380. Code modified in PROCEDURE insert_txn2.              *
42   *   Inserted proper values into columns transaction_amount and prior_unit_price.              *
43   **********************************************************************************************/
44   PROCEDURE Get_TransactionAmount
45   (
46   p_api_version           IN      	  NUMBER,
47   p_init_msg_list         IN      	  VARCHAR2 := FND_API.G_FALSE,
48   p_commit                IN      	  VARCHAR2 := FND_API.G_FALSE,
49   p_validation_level      IN      	  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
50   x_return_status         OUT NOCOPY  VARCHAR2,
51   x_msg_count             OUT NOCOPY  NUMBER,
52   x_msg_data              OUT NOCOPY  VARCHAR2,
53   p_rcv_accttxn		        IN 		      GMF_RCV_ACCOUNTING_PKG.rcv_accttxn_rec_type,
54 	x_transaction_amount	OUT NOCOPY 	  NUMBER
55   )
56   IS
57     l_api_name            CONSTANT      VARCHAR2(30)   := 'Get_TransactionAmount';
58     l_api_version         CONSTANT      NUMBER         := 1.0;
59     l_return_status       VARCHAR2(1) := fnd_api.g_ret_sts_success;
60     l_msg_count           NUMBER := 0;
61     l_msg_data            VARCHAR2(8000) := '';
62     l_stmt_num            NUMBER := 0;
63     l_api_message         VARCHAR2(1000);
64     l_transaction_amount  NUMBER;
65     l_po_amount_ordered   NUMBER;
66     l_po_amount_delivered NUMBER;
67     l_abs_rt_amount       NUMBER;
68     l_rcv_txn_type        RCV_Transactions.transaction_type%TYPE;
69     l_parent_txn_id       NUMBER;
70     l_par_rcv_txn_type    RCV_Transactions.transaction_type%TYPE;
71   BEGIN
72     /**********************************
73     * Standard start of API savepoint *
74     **********************************/
75     SAVEPOINT Get_TransactionAmount_PVT;
76 
77     l_stmt_num := 0;
78 
79     IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
80       FND_LOG.string(FND_LOG.LEVEL_PROCEDURE,G_LOG_HEAD || '.'||l_api_name||'.begin','Get_TransactionAmount <<');
81     END IF;
82 
83     /************************************************
84     * Standard call to check for call compatibility *
85     ************************************************/
86     IF NOT FND_API.Compatible_API_Call (l_api_version,p_api_version,l_api_name,G_PKG_NAME ) THEN
87       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
88     END IF;
89 
90     /************************************************************
91     * Initialize message list if p_init_msg_list is set to TRUE *
92     ************************************************************/
93     IF FND_API.to_Boolean(p_init_msg_list) THEN
94       FND_MSG_PUB.initialize;
95     END IF;
96 
97     /******************************************
98     * Initialize API return status to success *
99     ******************************************/
100     x_return_status := FND_API.G_RET_STS_SUCCESS;
101 
102     /********************************************************************************
103     * For service line types, only the source types of RECEIVING and INVOICEMATCH   *
104     * are valid. Retroactive price changes on service line types have no accounting *
105     * impact.                                                                       *
106     ********************************************************************************/
107     IF(p_rcv_accttxn.event_source = 'RECEIVING') THEN
108 
109       /**************************************************************************************
110       * If receiving transaction has a distribution, entire amount is allocated to          *
111       * the distribution. Otherwise, the amount has to be prorated based on amount ordered. *
112       **************************************************************************************/
113 	    l_stmt_num := 10;
114       SELECT decode(RT.po_distribution_id, NULL, RT.amount  * (POD.amount_ordered/POLL.amount),RT.amount)
115 	    INTO   l_transaction_amount
116 	    FROM   rcv_transactions RT,
117 	           po_distributions POD,
118 	           po_line_locations POLL
119 	    WHERE  RT.transaction_id 	= p_rcv_accttxn.rcv_transaction_id
120 	    AND    POD.po_distribution_id 	= p_rcv_accttxn.po_distribution_id
121 	    AND    POLL.line_location_id 	= p_rcv_accttxn.po_line_location_id;
122     ELSIF(p_rcv_accttxn.event_source = 'INVOICEMATCH') THEN
123       /*************************************************************************
124       * For source of invoice match, there will always be a po_distribution_id *
125       *************************************************************************/
126 	    l_stmt_num := 20;
127       SELECT APID.amount
128       INTO   l_transaction_amount
129       FROM   ap_invoice_distributions APID
130       WHERE  APID.invoice_distribution_id = p_rcv_accttxn.inv_distribution_id;
131     END IF;
132 
133     IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
134       l_api_message := 'Transaction Amount : '||l_transaction_amount;
135       FND_LOG.string(FND_LOG.LEVEL_STATEMENT,G_LOG_HEAD||'.'||l_api_name||'.'||l_stmt_num,l_api_message);
136     END IF;
137 
138     /*****************************************************************
139     * For encumbrance reversal transactions , only reverse encumbrance     *
140     * upto amount_ordered. If amount received exceeds the            *
141     * ordered amount, transaction amount should be reduced such that *
142     * it does not exceed amount ordered.                             *
143     *****************************************************************/
144     IF(p_rcv_accttxn.event_type_id = ENCUMBRANCE_REVERSAL) THEN
145       l_abs_rt_amount := ABS(l_transaction_amount);
146 
147       l_stmt_num := 40;
148       SELECT RT.transaction_type, RT.parent_transaction_id
149       INTO   l_rcv_txn_type, l_parent_txn_id
150       FROM   rcv_transactions RT
151       WHERE  RT.transaction_id = p_rcv_accttxn.rcv_transaction_id;
152 
153       l_stmt_num := 50;
154       SELECT PARENT.transaction_type
155       INTO   l_par_rcv_txn_type
156       FROM   rcv_transactions PARENT
157       WHERE  PARENT.transaction_id =l_parent_txn_id;
158 
159       l_stmt_num := 60;
160       SELECT POD.amount_ordered, POD.amount_delivered
161       INTO   l_po_amount_ordered, l_po_amount_delivered
162       FROM   po_distributions POD
163       WHERE  po_distribution_id = p_rcv_accttxn.po_distribution_id;
164 
165       IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
166         l_api_message := substr('l_rcv_txn_type : '||l_rcv_txn_type||
167         ' l_par_rcv_txn_type : '||l_par_rcv_txn_type||
168         ' l_po_amount_ordered : '||l_po_amount_ordered||
169         ' l_po_amount_delivered : '||l_po_amount_delivered, 1, 1000);
170         FND_LOG.string(FND_LOG.LEVEL_STATEMENT,G_LOG_HEAD||'.'||l_api_name||'.'||l_stmt_num,l_api_message);
171       END IF;
172 
173       l_stmt_num := 70;
174       IF(l_rcv_txn_type = 'DELIVER' OR
175       (l_rcv_txn_type = 'CORRECT' AND l_par_rcv_txn_type = 'DELIVER'
176       AND l_transaction_amount > 0) OR
177       (l_rcv_txn_type = 'CORRECT' AND l_par_rcv_txn_type = 'RETURN TO RECEIVING'
178       AND l_transaction_amount < 0)) THEN
179 	      l_po_amount_delivered := l_po_amount_delivered - l_abs_rt_amount;
180         IF (l_po_amount_delivered >= l_po_amount_ordered) THEN
181           l_transaction_amount := 0;
182         ELSIF(l_abs_rt_amount + l_po_amount_delivered <= l_po_amount_ordered) THEN
183           l_transaction_amount := l_abs_rt_amount;
184         ELSE
185           l_transaction_amount := l_po_amount_ordered - l_po_amount_delivered;
186         END IF;
187       ELSIF(l_rcv_txn_type = 'RETURN TO VENDOR' OR
188       (l_rcv_txn_type = 'CORRECT' AND l_par_rcv_txn_type = 'DELIVER'
189       AND p_rcv_accttxn.transaction_amount < 0) OR
190       (l_rcv_txn_type = 'CORRECT' AND l_par_rcv_txn_type = 'RETURN TO RECEIVING'
191       AND p_rcv_accttxn.transaction_amount > 0)) THEN
192 	      l_po_amount_delivered := l_po_amount_delivered + l_abs_rt_amount;
193         IF (l_po_amount_delivered < l_po_amount_ordered) THEN
194           l_transaction_amount := l_abs_rt_amount;
195         ELSIF(l_po_amount_delivered - l_abs_rt_amount > l_po_amount_ordered) THEN
196           l_transaction_amount := 0;
197         ELSE
198           l_transaction_amount := l_abs_rt_amount - (l_po_amount_delivered - l_po_amount_ordered);
199         END IF;
200       END IF;
201     END IF;
202 
203     x_transaction_amount := l_transaction_amount;
204 
205     IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
206       l_api_message := 'x_transaction_amount : '||x_transaction_amount;
207       FND_LOG.string(FND_LOG.LEVEL_STATEMENT,G_LOG_HEAD||'.'||l_api_name||'.'||l_stmt_num,l_api_message);
208     END IF;
209 
210     /*****************************
211     * Standard check of p_commit *
212     *****************************/
213     IF FND_API.to_Boolean(p_commit) THEN
214       COMMIT WORK;
215     END IF;
216 
217     /************************************************************************
218     * Standard Call to get message count and if count = 1, get message info *
219     ************************************************************************/
220     FND_MSG_PUB.Count_And_Get (p_count => x_msg_count,p_data => x_msg_data );
221 
222     IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
223       FND_LOG.string(FND_LOG.LEVEL_PROCEDURE,G_LOG_HEAD || '.'||l_api_name||'.end','Get_TransactionAmount >>');
224     END IF;
225   EXCEPTION
226     WHEN FND_API.g_exc_error THEN
227       ROLLBACK TO Get_TransactionAmount_PVT;
228       x_return_status := FND_API.g_ret_sts_error;
229       FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data  => x_msg_data);
230     WHEN FND_API.g_exc_unexpected_error THEN
231       ROLLBACK TO Get_TransactionAmount_PVT;
232       x_return_status := FND_API.g_ret_sts_unexp_error ;
233       FND_MSG_PUB.count_and_get( p_count  => x_msg_count, p_data   => x_msg_data);
234     WHEN OTHERS THEN
235       ROLLBACK TO Get_TransactionAmount_PVT;
236       x_return_status := fnd_api.g_ret_sts_unexp_error ;
237 
238       IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
239         FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD || '.'||l_api_name||l_stmt_num,'Get_TransactionAmount : '||l_stmt_num||' : '||substr(SQLERRM,1,200));
240       END IF;
241 
242       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
243         FND_MSG_PUB.add_exc_msg( G_PKG_NAME,l_api_name || 'Statement -'||to_char(l_stmt_num));
244       END IF;
245       FND_MSG_PUB.count_and_get(  p_count  => x_msg_count, p_data   => x_msg_data);
246   END Get_TransactionAmount;
247 
248   /****************************************************************************************************
249   * API name                                                                                          *
250   *   Get_HookAccount                                                                                 *
251   * Type                                                                                              *
252   *   Private                                                                                         *
253   * Function                                                                                          *
254   *   Call account hook to  allow customer to override default account.                               *
255   * Parameters                                                                                        *
256   *   IN                                                                                              *
257   *     p_api_version           IN NUMBER       Required                                              *
258   *     p_init_msg_list         IN VARCHAR2     Optional Default = FND_API.G_FALSE                    *
259   *     p_commit                IN VARCHAR2     Optional Default = FND_API.G_FALSE                    *
260   *     p_validation_level      IN NUMBER       Optional Default = FND_API.G_VALID_LEVEL_FULL         *
261   *     p_rcv_transaction_id    IN NUMBER       Required p_accounting_line_type  IN VARCHAR2 Required *
262   *     p_org_id                IN NUMBER       Required                                              *
263   *   OUT                                                                                             *
264   *     x_return_status         OUT     VARCHAR2(1)                                                   *
265   *     x_msg_count             OUT     NUMBER                                                        *
266   *     x_msg_data              OUT     VARCHAR2(2000)                                                *
267   *     x_distribution_acct_id  OUT     NUMBER                                                        *
268   * Description                                                                                       *
269   *   This API creates all accounting transactions for RETURN TO VENDOR transactions                  *
270   *   in gmf_rcv_accounting_txns.                                                                     *
271   ****************************************************************************************************/
272 
273   PROCEDURE Get_HookAccount
274   (
275   p_api_version           IN              NUMBER,
276   p_init_msg_list         IN              VARCHAR2 := FND_API.G_FALSE,
277   p_commit                IN              VARCHAR2 := FND_API.G_FALSE,
278   p_validation_level      IN              NUMBER   := FND_API.G_VALID_LEVEL_FULL,
279   x_return_status         OUT NOCOPY      VARCHAR2,
280   x_msg_count             OUT NOCOPY      NUMBER,
281   x_msg_data              OUT NOCOPY      VARCHAR2,
282   p_rcv_transaction_id    IN              NUMBER,
283   p_accounting_line_type  IN              VARCHAR2,
284   p_org_id                IN              NUMBER,
285   x_distribution_acct_id  OUT NOCOPY      NUMBER
286   )
287   IS
288     l_api_name   CONSTANT VARCHAR2(30)   := 'Get_HookAccount';
289     l_api_version        CONSTANT NUMBER         := 1.0;
290     l_return_status       VARCHAR2(1) := fnd_api.g_ret_sts_success;
291     l_msg_count           NUMBER := 0;
292     l_msg_data            VARCHAR2(8000) := '';
293     l_stmt_num            NUMBER := 0;
294     l_api_message         VARCHAR2(1000);
295     l_dist_acct_id	 NUMBER;
296     l_account_flag	 NUMBER;
297   BEGIN
298     /**********************************
299     * Standard start of API savepoint *
300     **********************************/
301     SAVEPOINT Get_HookAccount_PVT;
302 
303     l_stmt_num := 0;
304 
305     IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
306       FND_LOG.string(FND_LOG.LEVEL_PROCEDURE,G_LOG_HEAD || '.'||l_api_name||'.begin','Get_HookAccount <<');
307     END IF;
308 
309     /************************************************
310     * Standard call to check for call compatibility *
311     ************************************************/
312     IF NOT FND_API.Compatible_API_Call (l_api_version,p_api_version,l_api_name,G_PKG_NAME ) THEN
313       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
314     END IF;
315 
316     /************************************************************
317     * Initialize message list if p_init_msg_list is set to TRUE *
318     ************************************************************/
319     IF FND_API.to_Boolean(p_init_msg_list) THEN
320       FND_MSG_PUB.initialize;
321     END IF;
322 
323     /******************************************
324     * Initialize API return status to success *
325     ******************************************/
326     x_return_status   	:= FND_API.G_RET_STS_SUCCESS;
327     x_distribution_acct_id 	:= -1;
328 
329     l_stmt_num := 10;
330     RCV_AccountHook_PUB.Get_Account
331     (
332     p_api_version           => l_api_version,
333     x_return_status         => l_return_status,
334     x_msg_count             => l_msg_count,
335     x_msg_data              => l_msg_data,
336     p_rcv_transaction_id    => p_rcv_transaction_id,
337     p_accounting_line_type  => p_accounting_line_type,
338     x_distribution_acct_id  => l_dist_acct_id
339     );
340 
341     IF l_return_status <> FND_API.g_ret_sts_success THEN
342       l_api_message := 'Error in Account Hook';
343       IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
344         FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD || '.'||l_api_name||l_stmt_num,'Get_HookAccount : '||l_stmt_num||' : '||l_api_message);
345       END IF;
346       RAISE FND_API.g_exc_unexpected_error;
347     END IF;
348 
349     IF(l_dist_acct_id <> -1) THEN
350       l_stmt_num := 20;
351       SELECT  count(*)
352       INTO    l_account_flag
353       FROM    gl_code_combinations GCC,
354               cst_organization_definitions COD
355       WHERE   COD.operating_unit        = p_org_id
356       AND     COD.chart_of_accounts_id  = GCC.chart_of_accounts_id
357       AND     GCC.code_combination_id   = l_dist_acct_id;
358       IF (l_account_flag = 0) THEN
359         FND_MESSAGE.set_name('PO','PO_INVALID_ACCOUNT');
360         FND_MSG_pub.add;
361         IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_ERROR >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
362           FND_LOG.message(FND_LOG.LEVEL_ERROR,G_LOG_HEAD || '.'||l_api_name||l_stmt_num,FALSE);
363         END IF;
364         RAISE FND_API.g_exc_error;
365       END IF;
366     END IF;
367 
368     x_distribution_acct_id := l_dist_acct_id;
369 
370     IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
371       l_api_message := 'x_distribution_acct_id : '||x_distribution_acct_id;
372       FND_LOG.string(FND_LOG.LEVEL_STATEMENT,G_LOG_HEAD||'.'||l_api_name||'.'||l_stmt_num,l_api_message);
373     END IF;
374 
375     /*****************************
376     * Standard check of p_commit *
377     *****************************/
378     IF FND_API.to_Boolean(p_commit) THEN
379       COMMIT WORK;
380     END IF;
381 
382     /************************************************************************
383     * Standard Call to get message count and if count = 1, get message info *
384     ************************************************************************/
385     FND_MSG_PUB.Count_And_Get (p_count     => x_msg_count,p_data      => x_msg_data );
386 
387     IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
388       FND_LOG.string(FND_LOG.LEVEL_PROCEDURE,G_LOG_HEAD || '.'||l_api_name||'.end','Get_HookAccount >>');
389     END IF;
390   EXCEPTION
391     WHEN FND_API.g_exc_error THEN
392       ROLLBACK TO Get_HookAccount_PVT;
393       x_return_status := FND_API.g_ret_sts_error;
394       FND_MSG_PUB.count_and_get(  p_count => x_msg_count, p_data  => x_msg_data);
395     WHEN FND_API.g_exc_unexpected_error THEN
396       ROLLBACK TO Get_HookAccount_PVT;
397       x_return_status := FND_API.g_ret_sts_unexp_error ;
398       FND_MSG_PUB.count_and_get(  p_count  => x_msg_count, p_data   => x_msg_data);
399     WHEN OTHERS THEN
400       ROLLBACK TO Get_HookAccount_PVT;
401       x_return_status := fnd_api.g_ret_sts_unexp_error ;
402       IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
403         FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD || '.'||l_api_name||l_stmt_num,'Get_HookAccount : '||l_stmt_num||' : '||substr(SQLERRM,1,200));
404       END IF;
405       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)THEN
406         FND_MSG_PUB.add_exc_msg(  G_PKG_NAME,l_api_name || 'Statement -'||to_char(l_stmt_num));
407       END IF;
408       FND_MSG_PUB.count_and_get(  p_count  => x_msg_count, p_data   => x_msg_data);
409   END Get_HookAccount;
410 
411    /********************************************************************************************
412    * API name                                                                                  *
413    *   Get_Quantity                                                                            *
414    * Type                                                                                      *
415    *   Private                                                                                 *
416    * Function                                                                                  *
417    *   Returns the quantity in source doc UOM. It includes additional                          *
418    *   checks for encumbrance reversal transactions. We should only                            *
419    *   encumber upto quantity ordered. If quantity received is                                 *
420    *   greater than quantity ordered, we should not encumber for                               *
421    *   the excess.                                                                             *
422    * Parameters                                                                                *
423    *   IN                                                                                      *
424    *     p_api_version           IN NUMBER       Required                                      *
425    *     p_init_msg_list         IN VARCHAR2     Optional Default = FND_API.G_FALSE            *
426    *     p_commit                IN VARCHAR2     Optional Default = FND_API.G_FALSE            *
427    *     p_validation_level      IN NUMBER       Optional Default = FND_API.G_VALID_LEVEL_FULL *
428    *     p_rcv_accttxn           IN rcv_accttxn_rec_type       Required                        *
429    *   OUT                                                                                     *
430    *     x_return_status         OUT     VARCHAR2(1)                                           *
431    *     x_msg_count             OUT     NUMBER                                                *
432    *     x_msg_data              OUT     VARCHAR2(2000)                                        *
433    *     x_source_doc_quantity   OUT     NUMBER                                                *
434    * Description                                                                               *
435    *   This API returns the transaction quantity. It should                                    *
436    *   only be called for non-service line types.                                              *
437    ********************************************************************************************/
438    PROCEDURE get_quantity (
439       p_api_version           IN              NUMBER,
440       p_init_msg_list         IN              VARCHAR2 := fnd_api.g_false,
441       p_commit                IN              VARCHAR2 := fnd_api.g_false,
442       p_validation_level      IN              NUMBER   := fnd_api.g_valid_level_full,
443       x_return_status         OUT NOCOPY      VARCHAR2,
444       x_msg_count             OUT NOCOPY      NUMBER,
445       x_msg_data              OUT NOCOPY      VARCHAR2,
446       p_rcv_accttxn           IN              gmf_rcv_accounting_pkg.rcv_accttxn_rec_type,
447       x_source_doc_quantity   OUT NOCOPY      NUMBER
448    )
449    IS
450       l_api_name       CONSTANT VARCHAR2 (30)               := 'Get_Quantity';
451       l_api_version    CONSTANT NUMBER                                 := 1.0;
452       l_return_status           VARCHAR2 (1)     := fnd_api.g_ret_sts_success;
453       l_msg_count               NUMBER                                   := 0;
454       l_msg_data                VARCHAR2 (8000)                         := '';
455       l_stmt_num                NUMBER                                   := 0;
456       l_api_message             VARCHAR2 (1000);
457       l_source_doc_quantity     NUMBER;
458       l_po_quantity_ordered     NUMBER;
459       l_po_quantity_delivered   NUMBER;
460       l_abs_rt_quantity         NUMBER;
461       l_rcv_txn_type            rcv_transactions.transaction_type%TYPE;
462       l_parent_txn_id           NUMBER;
463       l_par_rcv_txn_type        rcv_transactions.transaction_type%TYPE;
464    BEGIN
465       /**********************************
466       * Standard start of API savepoint *
467       **********************************/
468       SAVEPOINT get_quantity_pvt;
469       l_stmt_num := 0;
470 
471       IF     g_debug = 'Y'
472          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
473       THEN
474          fnd_log.STRING (fnd_log.level_procedure,
475                          g_log_head || '.' || l_api_name || '.begin',
476                          'Get_Quantity <<'
477                         );
478       END IF;
479 
480       -- Standard call to check for call compatibility
481       IF NOT fnd_api.compatible_api_call (l_api_version,
482                                           p_api_version,
483                                           l_api_name,
484                                           g_pkg_name
485                                          )
486       THEN
487          RAISE fnd_api.g_exc_unexpected_error;
488       END IF;
489 
490       -- Initialize message list if p_init_msg_list is set to TRUE
491       IF fnd_api.to_boolean (p_init_msg_list)
492       THEN
493          fnd_msg_pub.initialize;
494       END IF;
495 
496       -- Initialize API return status to success
497       x_return_status := fnd_api.g_ret_sts_success;
498       l_stmt_num := 10;
499 
500       IF (p_rcv_accttxn.event_source = 'RECEIVING')
501       THEN
502          l_stmt_num := 20;
503 
504          SELECT DECODE (rt.po_distribution_id,
505                         NULL, rt.source_doc_quantity
506                          * pod.quantity_ordered
507                          / poll.quantity,
508                         rt.source_doc_quantity
509                        )
510            INTO l_source_doc_quantity
511            FROM rcv_transactions rt,
512                 po_line_locations poll,
513                 po_distributions pod
514           WHERE rt.transaction_id = p_rcv_accttxn.rcv_transaction_id
515             AND poll.line_location_id = p_rcv_accttxn.po_line_location_id
516             AND pod.po_distribution_id = p_rcv_accttxn.po_distribution_id;
517       ELSIF (p_rcv_accttxn.event_source = 'RETROPRICE')
518       THEN
519          IF (p_rcv_accttxn.event_type_id = adjust_receive)
520          THEN
521             l_stmt_num := 30;
522             l_source_doc_quantity :=
523                rcv_accrual_sv.get_received_quantity
524                                            (p_rcv_accttxn.rcv_transaction_id,
525                                             SYSDATE
526                                            );
527          ELSE
528             l_stmt_num := 40;
529             l_source_doc_quantity :=
530                rcv_accrual_sv.get_delivered_quantity
531                                            (p_rcv_accttxn.rcv_transaction_id,
532                                             SYSDATE
533                                            );
534          END IF;
535 
536          l_stmt_num := 50;
537 
538          SELECT DECODE (rt.po_distribution_id,
539                         NULL, l_source_doc_quantity
540                          * pod.quantity_ordered
541                          / poll.quantity,
542                         l_source_doc_quantity
543                        )
544            INTO l_source_doc_quantity
545            FROM rcv_transactions rt,
546                 po_line_locations poll,
547                 po_distributions pod
548           WHERE rt.transaction_id = p_rcv_accttxn.rcv_transaction_id
549             AND poll.line_location_id = p_rcv_accttxn.po_line_location_id
550             AND pod.po_distribution_id = p_rcv_accttxn.po_distribution_id;
551       END IF;
552 
553       -- For encumbrance reversal transactions  only match
554       -- upto quantity_ordered. If quantity received/invoiced exceeds the
555       -- ordered quantity, transaction quantity should be reduced such that
556       -- it does not exceed quantity ordered.
557       IF (p_rcv_accttxn.event_type_id = encumbrance_reversal)
558       THEN
559          l_abs_rt_quantity := ABS (l_source_doc_quantity);
560          l_stmt_num := 60;
561 
562          SELECT rt.transaction_type, rt.parent_transaction_id
563            INTO l_rcv_txn_type, l_parent_txn_id
564            FROM rcv_transactions rt
565           WHERE rt.transaction_id = p_rcv_accttxn.rcv_transaction_id;
566 
567          l_stmt_num := 70;
568 
569          SELECT PARENT.transaction_type
570            INTO l_par_rcv_txn_type
571            FROM rcv_transactions PARENT
572           WHERE PARENT.transaction_id = l_parent_txn_id;
573 
574          l_stmt_num := 80;
575 
576          SELECT pod.quantity_ordered, pod.quantity_delivered
577            INTO l_po_quantity_ordered, l_po_quantity_delivered
578            FROM po_distributions pod
579           WHERE pod.po_distribution_id = p_rcv_accttxn.po_distribution_id;
580 
581          IF     g_debug = 'Y'
582             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
583          THEN
584             l_api_message :=
585                SUBSTR (   'l_rcv_txn_type : '
586                        || l_rcv_txn_type
587                        || ' l_parent_txn_id : '
588                        || l_parent_txn_id
589                        || ' l_par_rcv_txn_type : '
590                        || l_par_rcv_txn_type
591                        || ' l_abs_rt_quantity : '
592                        || l_abs_rt_quantity
593                        || ' l_po_quantity_ordered : '
594                        || l_po_quantity_ordered
595                        || ' l_po_quantity_delivered : '
596                        || l_po_quantity_delivered,
597                        1,
598                        1000
599                       );
600             fnd_log.STRING (fnd_log.level_statement,
601                             g_log_head || '.' || l_api_name || '.'
602                             || l_stmt_num,
603                             l_api_message
604                            );
605          END IF;
606 
607          /* Bug #3333610. Receiving updates quantity delivered prior to calling the transactions API.
608             Consequently, we should subtract the current quantity from the quantity delivered to
609             get the quantity that has been delivered previously. */
610          l_stmt_num := 90;
611 
612          IF (   l_rcv_txn_type = 'DELIVER'
613              OR (    l_rcv_txn_type = 'CORRECT'
614                  AND l_par_rcv_txn_type = 'DELIVER'
615                  AND l_source_doc_quantity > 0
616                 )
617              OR (    l_rcv_txn_type = 'CORRECT'
618                  AND l_par_rcv_txn_type = 'RETURN TO RECEIVING'
619                  AND l_source_doc_quantity < 0
620                 )
621             )
622          THEN
623             l_po_quantity_delivered :=
624                                   l_po_quantity_delivered - l_abs_rt_quantity;
625 
626             IF (l_po_quantity_delivered >= l_po_quantity_ordered)
627             THEN
628                l_source_doc_quantity := 0;
629             ELSIF (l_abs_rt_quantity + l_po_quantity_delivered <=
630                                                          l_po_quantity_ordered
631                   )
632             THEN
633                l_source_doc_quantity := l_abs_rt_quantity;
634             ELSE
635                l_source_doc_quantity :=
636                               l_po_quantity_ordered - l_po_quantity_delivered;
637             END IF;
638          ELSIF (   l_rcv_txn_type = 'RETURN TO RECEIVING'
639                 OR (    l_rcv_txn_type = 'CORRECT'
640                     AND l_par_rcv_txn_type = 'DELIVER'
641                     AND l_source_doc_quantity < 0
642                    )
643                 OR (    l_rcv_txn_type = 'CORRECT'
644                     AND l_par_rcv_txn_type = 'RETURN TO RECEIVING'
645                     AND l_source_doc_quantity > 0
646                    )
647                )
648          THEN
649             l_po_quantity_delivered :=
650                                   l_po_quantity_delivered + l_abs_rt_quantity;
651 
652             IF (l_po_quantity_delivered <= l_po_quantity_ordered)
653             THEN
654                l_source_doc_quantity := l_abs_rt_quantity;
655             ELSIF (l_po_quantity_delivered - l_abs_rt_quantity >
656                                                          l_po_quantity_ordered
657                   )
658             THEN
659                l_source_doc_quantity := 0;
660             ELSE
661                l_source_doc_quantity :=
662                     l_abs_rt_quantity
663                   - (l_po_quantity_delivered - l_po_quantity_ordered);
664             END IF;
665          END IF;
666       END IF;
667 
668       x_source_doc_quantity := l_source_doc_quantity;
669 
670       IF     g_debug = 'Y'
671          AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
672       THEN
673          l_api_message := 'x_source_doc_quantity : ' || x_source_doc_quantity;
674          fnd_log.STRING (fnd_log.level_statement,
675                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
676                          l_api_message
677                         );
678       END IF;
679 
680       --- Standard check of p_commit
681       IF fnd_api.to_boolean (p_commit)
682       THEN
683          COMMIT WORK;
684       END IF;
685 
686       -- Standard Call to get message count and if count = 1, get message info
687       fnd_msg_pub.count_and_get (p_count      => x_msg_count,
688                                  p_data       => x_msg_data);
689 
690       IF     g_debug = 'Y'
691          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
692       THEN
693          fnd_log.STRING (fnd_log.level_procedure,
694                          g_log_head || '.' || l_api_name || '.end',
695                          'Get_Quantity >>'
696                         );
697       END IF;
698    EXCEPTION
699       WHEN fnd_api.g_exc_error
700       THEN
701          ROLLBACK TO get_quantity_pvt;
702          x_return_status := fnd_api.g_ret_sts_error;
703          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
704                                     p_data       => x_msg_data
705                                    );
706       WHEN fnd_api.g_exc_unexpected_error
707       THEN
708          ROLLBACK TO get_quantity_pvt;
709          x_return_status := fnd_api.g_ret_sts_unexp_error;
710          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
711                                     p_data       => x_msg_data
712                                    );
713       WHEN OTHERS
714       THEN
715          ROLLBACK TO get_quantity_pvt;
716          x_return_status := fnd_api.g_ret_sts_unexp_error;
717 
718          IF     g_debug = 'Y'
719             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
720          THEN
721             fnd_log.STRING (fnd_log.level_unexpected,
722                             g_log_head || '.' || l_api_name || l_stmt_num,
723                                'Get_Quantity : '
724                             || l_stmt_num
725                             || ' : '
726                             || SUBSTR (SQLERRM, 1, 200)
727                            );
728          END IF;
729 
730          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
731          THEN
732             fnd_msg_pub.add_exc_msg (g_pkg_name,
733                                         l_api_name
734                                      || 'Statement -'
735                                      || TO_CHAR (l_stmt_num)
736                                     );
737          END IF;
738 
739          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
740                                     p_data       => x_msg_data
741                                    );
742    END get_quantity;
743 
744 -- Start of comments
745 --      API name        : Get_UnitPrice
746 --      Type            : Private
747 --      Function        : Returns the Unit Price. Used for non-service line types.
748 --      Pre-reqs        :
749 --      Parameters      :
750 --      IN              :       p_api_version           IN NUMBER       Required
751 --                              p_init_msg_list         IN VARCHAR2     Optional
752 --                                      Default = FND_API.G_FALSE
753 --                              p_commit                IN VARCHAR2     Optional
754 --                                      Default = FND_API.G_FALSE
755 --                              p_validation_level      IN NUMBER       Optional
756 --                                      Default = FND_API.G_VALID_LEVEL_FULL
757 --                              p_rcv_accttxn             IN rcv_accttxn_rec_type       Required
758 --
759 --      OUT             :       x_return_status         OUT     VARCHAR2(1)
760 --                              x_msg_count             OUT     NUMBER
761 --                              x_msg_data              OUT     VARCHAR2(2000)
762 --          x_intercompany_pricing_option OUT   NUMBER
763 --          x_unit_price      OUT   NUMBER
764 --          x_currency_code      OUT   VARCHAR2(15)
765 --          x_incr_transfer_price   OUT   NUMBER
766 --          x_incr_currency_code OUT   VARCHAR2(15)
767 --      Version :
768 --                        Initial version       1.0
769 --
770 --
771 --      Notes           : This API returns the unit price. It should only be called for non service line types.
772 --
773 -- End of comments
774    PROCEDURE get_unitprice (
775       p_api_version                   IN              NUMBER,
776       p_init_msg_list                 IN              VARCHAR2
777             := fnd_api.g_false,
778       p_commit                        IN              VARCHAR2
779             := fnd_api.g_false,
780       p_validation_level              IN              NUMBER
781             := fnd_api.g_valid_level_full,
782       x_return_status                 OUT NOCOPY      VARCHAR2,
783       x_msg_count                     OUT NOCOPY      NUMBER,
784       x_msg_data                      OUT NOCOPY      VARCHAR2,
785       p_rcv_accttxn                   IN              gmf_rcv_accounting_pkg.rcv_accttxn_rec_type,
786       p_asset_item_pricing_option     IN              NUMBER,
787       p_expense_item_pricing_option   IN              NUMBER,
788       x_intercompany_pricing_option   OUT NOCOPY      NUMBER,
789       x_unit_price                    OUT NOCOPY      NUMBER,
790       x_currency_code                 OUT NOCOPY      VARCHAR2,
791       x_incr_transfer_price           OUT NOCOPY      NUMBER,
792       x_incr_currency_code            OUT NOCOPY      VARCHAR2
793    )
794    IS
795       l_api_name      CONSTANT VARCHAR2 (30)               := 'Get_UnitPrice';
796       l_api_version   CONSTANT NUMBER                                  := 1.0;
797       l_return_status          VARCHAR2 (1)      := fnd_api.g_ret_sts_success;
798       l_msg_count              NUMBER                                    := 0;
799       l_msg_data               VARCHAR2 (8000)                          := '';
800       l_stmt_num               NUMBER                                    := 0;
801       l_api_message            VARCHAR2 (1000);
802       l_asset_flag             VARCHAR2 (1);
803       l_ic_pricing_option      NUMBER                                    := 1;
804       l_transfer_price         NUMBER;
805       l_unit_price             NUMBER;
806       l_transaction_uom        VARCHAR2 (3);
807       l_currency_code          gmf_rcv_accounting_txns.currency_code%TYPE;
808       l_item_exists            NUMBER;
809       l_from_organization_id   NUMBER;
810       l_from_org_id            NUMBER;
811       l_to_org_id              NUMBER;
812       l_incr_currency_code     gmf_rcv_accounting_txns.currency_code%TYPE;
813       l_incr_transfer_price    NUMBER;
814    BEGIN
815       -- Standard start of API savepoint
816       SAVEPOINT get_unitprice_pvt;
817       l_stmt_num := 0;
818 
819       IF     g_debug = 'Y'
820          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
821       THEN
822          fnd_log.STRING (fnd_log.level_procedure,
823                          g_log_head || '.' || l_api_name || '.begin',
824                          'Get_UnitPrice <<'
825                         );
826       END IF;
827 
828       -- Standard call to check for call compatibility
829       IF NOT fnd_api.compatible_api_call (l_api_version,
830                                           p_api_version,
831                                           l_api_name,
832                                           g_pkg_name
833                                          )
834       THEN
835          RAISE fnd_api.g_exc_unexpected_error;
836       END IF;
837 
838       -- Initialize message list if p_init_msg_list is set to TRUE
839       IF fnd_api.to_boolean (p_init_msg_list)
840       THEN
841          fnd_msg_pub.initialize;
842       END IF;
843 
844       -- Initialize API return status to success
845       x_return_status := fnd_api.g_ret_sts_success;
846       x_incr_transfer_price := 0;
847       x_incr_currency_code := NULL;
848       l_currency_code := p_rcv_accttxn.currency_code;
849       l_stmt_num := 10;
850 
851       -- Always use PO price if :
852       -- 1. No transaction flow exists or
853       -- 2. Destination type is Shopfloor.
854       -- 3. If it is the procurement org
855       -- 4. The PO is for a one-time item.
856       IF (   p_rcv_accttxn.trx_flow_header_id IS NULL
857           OR p_rcv_accttxn.item_id IS NULL
858           OR p_rcv_accttxn.destination_type_code = 'SHOP FLOOR'
859           OR (    p_rcv_accttxn.procurement_org_flag = 'Y'
860               AND p_rcv_accttxn.event_type_id NOT IN
861                                 (intercompany_invoice, intercompany_reversal)
862              )
863          )
864       THEN
865          l_ic_pricing_option := 1;
866       ELSE
867          -- Pricing Option on the Transaction Flow form will determine whether to use
868          -- PO price or Transfer price.
869          BEGIN
870             -- Verify that item exists in organization where event is being created.
871             l_stmt_num := 30;
872 
873             SELECT COUNT (*)
874               INTO l_item_exists
875               FROM mtl_system_items msi
876              WHERE msi.inventory_item_id = p_rcv_accttxn.item_id
877                AND msi.organization_id = p_rcv_accttxn.organization_id;
878 
879             IF (l_item_exists = 0)
880             THEN
881                fnd_message.set_name ('PO', 'PO_INVALID_ITEM');
882                fnd_msg_pub.ADD;
883 
884                IF     g_debug = 'Y'
885                   AND fnd_log.level_error >= fnd_log.g_current_runtime_level
886                THEN
887                   fnd_log.MESSAGE (fnd_log.level_error,
888                                    g_log_head || '.' || l_api_name
889                                    || l_stmt_num,
890                                    FALSE
891                                   );
892                END IF;
893 
894                RAISE fnd_api.g_exc_error;
895             END IF;
896 
897             -- Use Inventory Asset Flag in the organization where the physical event occurred. This
898             -- would be the ship to organization id. Using POLL.ship_to_organization_id so it will be
899             -- available for both Invoice Match and Receiving transactions.
900             l_stmt_num := 40;
901 
902             SELECT msi.inventory_asset_flag
903               INTO l_asset_flag
904               FROM mtl_system_items msi, po_line_locations poll
905              WHERE msi.inventory_item_id = p_rcv_accttxn.item_id
906                AND msi.organization_id = poll.ship_to_organization_id
907                AND poll.line_location_id = p_rcv_accttxn.po_line_location_id;
908          EXCEPTION
909             WHEN NO_DATA_FOUND
910             THEN
911                fnd_message.set_name ('PO', 'PO_INVALID_ITEM');
912                fnd_msg_pub.ADD;
913 
914                IF     g_debug = 'Y'
915                   AND fnd_log.level_error >= fnd_log.g_current_runtime_level
916                THEN
917                   fnd_log.MESSAGE (fnd_log.level_error,
918                                    g_log_head || '.' || l_api_name
919                                    || l_stmt_num,
920                                    FALSE
921                                   );
922                END IF;
923 
924                RAISE fnd_api.g_exc_error;
925          END;
926 
927          IF (l_asset_flag = 'Y')
928          THEN
929             l_ic_pricing_option := p_asset_item_pricing_option;
930          ELSE
931             l_ic_pricing_option := p_expense_item_pricing_option;
932          END IF;
933       END IF;
934 
935       IF     g_debug = 'Y'
936          AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
937       THEN
938          l_api_message := 'l_ic_pricing_option : ' || l_ic_pricing_option;
939          fnd_log.STRING (fnd_log.level_statement,
940                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
941                          l_api_message
942                         );
943       END IF;
944 
945       -- l_ic_pricing_option of 1 => PO Price.
946       -- l_ic_pricing_option of 2 => Transfer Price.
947       IF (l_ic_pricing_option = 2)
948       THEN
949          l_stmt_num := 50;
950 
951          -- The l_ic_pricing_option can only be 2 for a source type of 'RECEIVING'.
952          -- Get the UOM of the source_doc since unit price is desired in Document's UOM
953          SELECT muom.uom_code
954            INTO l_transaction_uom
955            FROM rcv_transactions rt, mtl_units_of_measure muom
956           WHERE rt.transaction_id = p_rcv_accttxn.rcv_transaction_id
957             AND muom.unit_of_measure = rt.source_doc_unit_of_measure;
958 
959          -- While calling the transfer pricing API, the from organization id should be
960          -- passed. For Intercompany transactions, the from organization id is the same as
961          -- organization_id on the event. For the remaining transactions, the from organization
962          -- is the transfer_organization_id on the event.
963          IF (p_rcv_accttxn.event_type_id IN
964                                 (intercompany_invoice, intercompany_reversal)
965             )
966          THEN
967             l_from_organization_id := p_rcv_accttxn.organization_id;
968             l_from_org_id := p_rcv_accttxn.org_id;
969             l_to_org_id := p_rcv_accttxn.transfer_org_id;
970          ELSE
971             l_from_organization_id := p_rcv_accttxn.transfer_organization_id;
972             l_from_org_id := p_rcv_accttxn.transfer_org_id;
973             l_to_org_id := p_rcv_accttxn.org_id;
974          END IF;
975 
976          -- Alcoa enhancement. Users will be given the option to determine in which
977          -- currency intercompany invoices should be created. The get_transfer_price
978          -- API will return the transfer price in the selling OU currency as well in the
979          -- currency chosen by the user. The returned values will have to be stored
980          -- in MMT and will be used by Intercompany to determine the Currency in which
981          -- to create the intercompany invoices.
982          l_stmt_num := 60;
983 
984          IF     g_debug = 'Y'
985             AND fnd_log.level_event >= fnd_log.g_current_runtime_level
986          THEN
987             l_api_message :=
988                   'Calling get_transfer_price API : '
989                || ' l_from_org_id : '
990                || l_from_org_id
991                || ' l_to_org_id : '
992                || l_to_org_id
993                || ' l_transaction_uom : '
994                || l_transaction_uom
995                || ' item_id : '
996                || p_rcv_accttxn.item_id
997                || ' p_transaction_id : '
998                || p_rcv_accttxn.rcv_transaction_id;
999             fnd_log.STRING (fnd_log.level_event,
1000                             g_log_head || '.' || l_api_name || '.'
1001                             || l_stmt_num,
1002                             l_api_message
1003                            );
1004          END IF;
1005 
1006          inv_transaction_flow_pub.get_transfer_price
1007                         (p_api_version                  => 1.0,
1008                          x_return_status                => l_return_status,
1009                          x_msg_data                     => l_msg_data,
1010                          x_msg_count                    => l_msg_count,
1011                          x_transfer_price               => l_transfer_price,
1012                          x_currency_code                => l_currency_code,
1013                          x_incr_transfer_price          => l_incr_transfer_price,
1014                          x_incr_currency_code           => l_incr_currency_code,
1015                          p_from_org_id                  => l_from_org_id,
1016                          p_to_org_id                    => l_to_org_id,
1017                          p_transaction_uom              => l_transaction_uom,
1018                          p_inventory_item_id            => p_rcv_accttxn.item_id,
1019                          p_transaction_id               => p_rcv_accttxn.rcv_transaction_id,
1020                          p_from_organization_id         => l_from_organization_id,
1021                          p_global_procurement_flag      => 'Y',
1022                          p_drop_ship_flag               => 'N'
1023                         );
1024 
1025          IF l_return_status <> fnd_api.g_ret_sts_success
1026          THEN
1027             IF     g_debug = 'Y'
1028                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
1029             THEN
1030                l_api_message := 'Error getting transfer price';
1031                fnd_log.STRING (fnd_log.level_unexpected,
1032                                g_log_head || '.' || l_api_name || l_stmt_num,
1033                                l_api_message
1034                               );
1035             END IF;
1036 
1037             RAISE fnd_api.g_exc_unexpected_error;
1038          END IF;
1039 
1040          IF     g_debug = 'Y'
1041             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
1042          THEN
1043             l_api_message :=
1044                SUBSTR (   'l_transfer_price : '
1045                        || l_transfer_price
1046                        || ' l_currency_code : '
1047                        || l_currency_code
1048                        || ' l_incr_transfer_price : '
1049                        || l_incr_transfer_price
1050                        || ' l_incr_currency_code : '
1051                        || l_incr_currency_code,
1052                        1000
1053                       );
1054             fnd_log.STRING (fnd_log.level_statement,
1055                             g_log_head || '.' || l_api_name || l_stmt_num,
1056                                'Get_TransferPrice : '
1057                             || l_stmt_num
1058                             || ' : '
1059                             || l_api_message
1060                            );
1061          END IF;
1062 
1063          l_unit_price := l_transfer_price;
1064          x_incr_transfer_price := l_incr_transfer_price;
1065          x_incr_currency_code := l_incr_currency_code;
1066       ELSIF (   p_rcv_accttxn.event_source = 'RECEIVING'
1067              OR p_rcv_accttxn.event_source = 'RETROPRICE'
1068             )
1069       THEN
1070          l_stmt_num := 70;
1071 
1072          SELECT poll.price_override
1073            INTO l_unit_price
1074            FROM po_line_locations poll
1075           WHERE poll.line_location_id = p_rcv_accttxn.po_line_location_id;
1076       ELSIF (p_rcv_accttxn.event_source = 'INVOICEMATCH')
1077       THEN
1078          l_stmt_num := 80;
1079 
1080          SELECT apid.unit_price
1081            INTO l_unit_price
1082            FROM ap_invoice_distributions apid
1083           WHERE apid.invoice_distribution_id =
1084                                              p_rcv_accttxn.inv_distribution_id;
1085       END IF;
1086 
1087       x_intercompany_pricing_option := l_ic_pricing_option;
1088       x_unit_price := l_unit_price;
1089       x_currency_code := l_currency_code;
1090 
1091       IF     g_debug = 'Y'
1092          AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
1093       THEN
1094          l_api_message :=
1095             SUBSTR (   'x_ic_pricing_option : '
1096                     || x_intercompany_pricing_option
1097                     || ' x_unit_price : '
1098                     || x_unit_price
1099                     || ' x_currency_code : '
1100                     || x_currency_code
1101                     || ' x_incr_currency_code : '
1102                     || x_incr_currency_code
1103                     || ' x_incr_transfer_price : '
1104                     || x_incr_transfer_price,
1105                     1,
1106                     1000
1107                    );
1108          fnd_log.STRING (fnd_log.level_statement,
1109                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
1110                          l_api_message
1111                         );
1112       END IF;
1113 
1114       -- Standard check of p_commit
1115       IF fnd_api.to_boolean (p_commit)
1116       THEN
1117          COMMIT WORK;
1118       END IF;
1119 
1120       -- Standard Call to get message count and if count = 1, get message info
1121       fnd_msg_pub.count_and_get (p_count      => x_msg_count,
1122                                  p_data       => x_msg_data);
1123 
1124       IF     g_debug = 'Y'
1125          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
1126       THEN
1127          fnd_log.STRING (fnd_log.level_procedure,
1128                          g_log_head || '.' || l_api_name || '.end',
1129                          'Get_UnitPrice >>'
1130                         );
1131       END IF;
1132    EXCEPTION
1133       WHEN fnd_api.g_exc_error
1134       THEN
1135          ROLLBACK TO get_unitprice_pvt;
1136          x_return_status := fnd_api.g_ret_sts_error;
1137          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
1138                                     p_data       => x_msg_data
1139                                    );
1140       WHEN fnd_api.g_exc_unexpected_error
1141       THEN
1142          ROLLBACK TO get_unitprice_pvt;
1143          x_return_status := fnd_api.g_ret_sts_unexp_error;
1144          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
1145                                     p_data       => x_msg_data
1146                                    );
1147       WHEN OTHERS
1148       THEN
1149          ROLLBACK TO get_unitprice_pvt;
1150          x_return_status := fnd_api.g_ret_sts_unexp_error;
1151 
1152          IF     g_debug = 'Y'
1153             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
1154          THEN
1155             fnd_log.STRING (fnd_log.level_unexpected,
1156                             g_log_head || '.' || l_api_name || l_stmt_num,
1157                                'Get_UnitPrice : '
1158                             || l_stmt_num
1159                             || ' : '
1160                             || SUBSTR (SQLERRM, 1, 200)
1161                            );
1162          END IF;
1163 
1164          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
1165          THEN
1166             fnd_msg_pub.add_exc_msg (g_pkg_name,
1167                                         l_api_name
1168                                      || 'Statement -'
1169                                      || TO_CHAR (l_stmt_num)
1170                                     );
1171          END IF;
1172 
1173          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
1174                                     p_data       => x_msg_data
1175                                    );
1176    END get_unitprice;
1177 
1178 -- Start of comments
1179 --      API name        : Get_UnitTax
1180 --      Type            : Private
1181 --      Function        : Returns the recoverable and non-recoverable tax.
1182 --      Pre-reqs        :
1183 --      Parameters      :
1184 --      IN              :       p_api_version           IN NUMBER       Required
1185 --                              p_init_msg_list         IN VARCHAR2     Optional
1186 --                                      Default = FND_API.G_FALSE
1187 --                              p_commit                IN VARCHAR2     Optional
1188 --                                      Default = FND_API.G_FALSE
1189 --                              p_validation_level      IN NUMBER       Optional
1190 --                                      Default = FND_API.G_VALID_LEVEL_FULL
1191 --                              p_rcv_accttxn             IN rcv_accttxn_rec_type       Required
1192 --
1193 --      OUT             :       x_return_status         OUT     VARCHAR2(1)
1194 --                              x_msg_count             OUT     NUMBER
1195 --                              x_msg_data              OUT     VARCHAR2(2000)
1196 --          x_unit_nr_tax     OUT   NUMBER
1197 --          x_unit_rec_tax    OUT   NUMBER
1198 --          x_prior_nr_tax    OUT   NUMBER
1199 --          x_prior_rec_tax      OUT   NUMBER
1200 --      Version :
1201 --                        Initial version       1.0
1202 --
1203 --
1204 --      Notes           : This API returns the tax information.
1205 --
1206 -- End of comments
1207    PROCEDURE get_unittax (
1208       p_api_version        IN              NUMBER,
1209       p_init_msg_list      IN              VARCHAR2 := fnd_api.g_false,
1210       p_commit             IN              VARCHAR2 := fnd_api.g_false,
1211       p_validation_level   IN              NUMBER
1212             := fnd_api.g_valid_level_full,
1213       x_return_status      OUT NOCOPY      VARCHAR2,
1214       x_msg_count          OUT NOCOPY      NUMBER,
1215       x_msg_data           OUT NOCOPY      VARCHAR2,
1216       p_rcv_accttxn        IN              gmf_rcv_accounting_pkg.rcv_accttxn_rec_type,
1217       x_unit_nr_tax        OUT NOCOPY      NUMBER,
1218       x_unit_rec_tax       OUT NOCOPY      NUMBER,
1219       x_prior_nr_tax       OUT NOCOPY      NUMBER,
1220       x_prior_rec_tax      OUT NOCOPY      NUMBER
1221    )
1222    IS
1223       l_api_name         CONSTANT VARCHAR2 (30)   := 'Get_UnitTax';
1224       l_api_version      CONSTANT NUMBER          := 1.0;
1225       l_return_status             VARCHAR2 (1)   := fnd_api.g_ret_sts_success;
1226       l_msg_count                 NUMBER          := 0;
1227       l_msg_data                  VARCHAR2 (8000) := '';
1228       l_stmt_num                  NUMBER          := 0;
1229       l_api_message               VARCHAR2 (1000);
1230       l_unit_nr_tax               NUMBER          := 0;
1231       l_unit_rec_tax              NUMBER          := 0;
1232       l_prior_nr_tax              NUMBER          := 0;
1233       l_prior_rec_tax             NUMBER          := 0;
1234       l_recoverable_tax           NUMBER          := 0;
1235       l_non_recoverable_tax       NUMBER          := 0;
1236       l_old_recoverable_tax       NUMBER          := 0;
1237       l_old_non_recoverable_tax   NUMBER          := 0;
1238    BEGIN
1239       -- Standard start of API savepoint
1240       SAVEPOINT get_unittax_pvt;
1241       l_stmt_num := 0;
1242 
1243       IF     g_debug = 'Y'
1244          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
1245       THEN
1246          fnd_log.STRING (fnd_log.level_procedure,
1247                          g_log_head || '.' || l_api_name || '.begin',
1248                          'Get_UnitTax <<'
1249                         );
1250       END IF;
1251 
1252       -- Standard call to check for call compatibility
1253       IF NOT fnd_api.compatible_api_call (l_api_version,
1254                                           p_api_version,
1255                                           l_api_name,
1256                                           g_pkg_name
1257                                          )
1258       THEN
1259          RAISE fnd_api.g_exc_unexpected_error;
1260       END IF;
1261 
1262       -- Initialize message list if p_init_msg_list is set to TRUE
1263       IF fnd_api.to_boolean (p_init_msg_list)
1264       THEN
1265          fnd_msg_pub.initialize;
1266       END IF;
1267 
1268       -- Initialize API return status to success
1269       x_return_status := fnd_api.g_ret_sts_success;
1270       x_unit_nr_tax := 0;
1271       x_unit_rec_tax := 0;
1272       x_prior_nr_tax := 0;
1273       x_prior_rec_tax := 0;
1274       l_stmt_num := 10;
1275 
1276       -- No tax is applicable if pricing option is transfer price.
1277       IF (p_rcv_accttxn.intercompany_pricing_option = 2)
1278       THEN
1279          RETURN;
1280       END IF;
1281 
1282       IF (   p_rcv_accttxn.event_source = 'RECEIVING'
1283           OR p_rcv_accttxn.event_source = 'RETROPRICE'
1284          )
1285       THEN
1286          l_stmt_num := 20;
1287          -- Call PO API to get current an prior receoverable and non-recoverable tax
1288          po_tax_sv.get_all_po_tax
1289                       (p_api_version                  => l_api_version,
1290                        x_return_status                => l_return_status,
1291                        x_msg_data                     => l_msg_data,
1292                        p_distribution_id              => p_rcv_accttxn.po_distribution_id,
1293                        x_recoverable_tax              => l_recoverable_tax,
1294                        x_non_recoverable_tax          => l_non_recoverable_tax,
1295                        x_old_recoverable_tax          => l_old_recoverable_tax,
1296                        x_old_non_recoverable_tax      => l_old_non_recoverable_tax
1297                       );
1298 
1299          IF l_return_status <> fnd_api.g_ret_sts_success
1300          THEN
1301             l_api_message := 'Error getting Tax';
1302 
1303             IF     g_debug = 'Y'
1304                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
1305             THEN
1306                fnd_log.STRING (fnd_log.level_unexpected,
1307                                g_log_head || '.' || l_api_name || l_stmt_num,
1308                                   'Get_UnitPrice : '
1309                                || l_stmt_num
1310                                || ' : '
1311                                || l_api_message
1312                               );
1313             END IF;
1314 
1315             RAISE fnd_api.g_exc_unexpected_error;
1316          END IF;
1317 
1318          IF     g_debug = 'Y'
1319             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
1320          THEN
1321             l_api_message :=
1322                SUBSTR (   'l_recoverable_tax : '
1323                        || l_recoverable_tax
1324                        || ' l_non_recoverable_tax : '
1325                        || l_non_recoverable_tax
1326                        || ' l_old_recoverable_tax : '
1327                        || l_old_recoverable_tax
1328                        || ' l_old_non_recoverable_tax : '
1329                        || l_old_non_recoverable_tax,
1330                        1,
1331                        1000
1332                       );
1333             fnd_log.STRING (fnd_log.level_statement,
1334                             g_log_head || '.' || l_api_name || '.'
1335                             || l_stmt_num,
1336                             l_api_message
1337                            );
1338          END IF;
1339 
1340          IF (p_rcv_accttxn.service_flag = 'Y')
1341          THEN
1342             l_stmt_num := 30;
1343 
1344             SELECT l_non_recoverable_tax / pod.amount_ordered,
1345                    l_recoverable_tax / pod.amount_ordered
1346               INTO l_unit_nr_tax,
1347                    l_unit_rec_tax
1348               FROM po_distributions pod
1349              WHERE pod.po_distribution_id = p_rcv_accttxn.po_distribution_id;
1350          ELSE
1351             l_stmt_num := 40;
1352 
1353             SELECT l_non_recoverable_tax / pod.quantity_ordered,
1354                    l_recoverable_tax / pod.quantity_ordered
1355               INTO l_unit_nr_tax,
1356                    l_unit_rec_tax
1357               FROM po_distributions pod
1358              WHERE pod.po_distribution_id = p_rcv_accttxn.po_distribution_id;
1359          END IF;
1360       END IF;
1361 
1362       IF (p_rcv_accttxn.event_source = 'RETROPRICE')
1363       THEN
1364          l_stmt_num := 50;
1365 
1366          SELECT l_old_non_recoverable_tax / pod.quantity_ordered,
1367                 l_old_recoverable_tax / pod.quantity_ordered
1368            INTO l_prior_nr_tax,
1369                 l_prior_rec_tax
1370            FROM po_distributions pod
1371           WHERE po_distribution_id = p_rcv_accttxn.po_distribution_id;
1372       END IF;
1373 
1374       x_unit_nr_tax := NVL (l_unit_nr_tax, 0);
1375       x_unit_rec_tax := NVL (l_unit_rec_tax, 0);
1376       x_prior_nr_tax := NVL (l_prior_nr_tax, 0);
1377       x_prior_rec_tax := NVL (l_prior_rec_tax, 0);
1378 
1379       IF     g_debug = 'Y'
1380          AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
1381       THEN
1382          l_api_message :=
1383             SUBSTR (   'x_unit_nr_tax : '
1384                     || x_unit_nr_tax
1385                     || ' x_unit_rec_tax : '
1386                     || x_unit_rec_tax
1387                     || ' x_prior_nr_tax : '
1388                     || x_prior_nr_tax
1389                     || ' x_prior_rec_tax : '
1390                     || x_prior_rec_tax,
1391                     1,
1392                     1000
1393                    );
1394          fnd_log.STRING (fnd_log.level_statement,
1395                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
1396                          l_api_message
1397                         );
1398       END IF;
1399 
1400       --- Standard check of p_commit
1401       IF fnd_api.to_boolean (p_commit)
1402       THEN
1403          COMMIT WORK;
1404       END IF;
1405 
1406       -- Standard Call to get message count and if count = 1, get message info
1407       fnd_msg_pub.count_and_get (p_count      => x_msg_count,
1408                                  p_data       => x_msg_data);
1409 
1410       IF     g_debug = 'Y'
1411          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
1412       THEN
1413          fnd_log.STRING (fnd_log.level_procedure,
1414                          g_log_head || '.' || l_api_name || '.end',
1415                          'Get_UnitTax >>'
1416                         );
1417       END IF;
1418    EXCEPTION
1419       WHEN fnd_api.g_exc_error
1420       THEN
1421          ROLLBACK TO get_unittax_pvt;
1422          x_return_status := fnd_api.g_ret_sts_error;
1423          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
1424                                     p_data       => x_msg_data
1425                                    );
1426       WHEN fnd_api.g_exc_unexpected_error
1427       THEN
1428          ROLLBACK TO get_unittax_pvt;
1429          x_return_status := fnd_api.g_ret_sts_unexp_error;
1430          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
1431                                     p_data       => x_msg_data
1432                                    );
1433       WHEN OTHERS
1434       THEN
1435          ROLLBACK TO get_unittax_pvt;
1436          x_return_status := fnd_api.g_ret_sts_unexp_error;
1437 
1438          IF     g_debug = 'Y'
1439             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
1440          THEN
1441             fnd_log.STRING (fnd_log.level_unexpected,
1442                             g_log_head || '.' || l_api_name || l_stmt_num,
1443                                'Get_UnitTax : '
1444                             || l_stmt_num
1445                             || ' : '
1446                             || SUBSTR (SQLERRM, 1, 200)
1447                            );
1448          END IF;
1449 
1450          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
1451          THEN
1452             fnd_msg_pub.add_exc_msg (g_pkg_name,
1453                                         l_api_name
1454                                      || 'Statement -'
1455                                      || TO_CHAR (l_stmt_num)
1456                                     );
1457          END IF;
1458 
1459          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
1460                                     p_data       => x_msg_data
1461                                    );
1462    END get_unittax;
1463 
1464 -----------------------------------------------------------------------------
1465 -- Start of comments                                                       --
1466 --                                                                         --
1467 -- PROCEDURE                                                               --
1468 --  Convert_UOM     This function updates the record type variable         --
1469 --                  that is passed to it. It inserts the UOM into the      --
1470 --                  primary_uom field, then it updates the primary_        --
1471 --                  quantity with the transaction_quantity converted to    --
1472 --                  the new UOM and it updates the unit_price by           --
1473 --                  converting it with the new UOM.                        --
1474 --                                                                         --
1475 --                  Because there are already other modules under PO_TOP   --
1476 --                  that use the inv_convert package, we can safely use    --
1477 --                  it here without introducing new dependencies on that   --
1478 --                  product.                                               --
1479 --                                                                         --
1480 -- VERSION 1.0                                                             --
1481 --                                                                         --
1482 -- PARAMETERS                                                              --
1483 --  P_API_VERSION      API Version # - REQUIRED: enter 1.0                 --
1484 --  P_INIT_MSG_LIST    Initialize message list? True/False                 --
1485 --  P_COMMIT           Should the API commit before returning? True/False  --
1486 --  X_RETURN_STATUS    Success/Error/Unexplained error - 'S','E', or 'U'   --
1487 --  X_MSG_COUNT        Message Count - # of messages placed in message list--
1488 --  X_MSG_DATA         Message Text - returns msg contents if msg_count = 1--
1489 --  P_EVENT_REC        Record storing an RCV Accounting Event (GRAT)        --
1490 --  X_TRANSACTION_QTY  Transaction quantity converted from source doc qty  --
1491 --  X_PRIMARY_UOM      Converted UOM                                       --
1492 --  X_PRIMARY_QTY      Primary quantity converted from source doc qty      --
1493 --  X_TRX_UOM_CODE     Transaction UOM                                     --
1494 --                                                                         --
1495 -- HISTORY:                                                                --
1496 --    06/26/03     Bryan Kuntz      Created                                --
1497 -- End of comments                                                         --
1498 -----------------------------------------------------------------------------
1499    PROCEDURE convert_uom (
1500       p_api_version        IN              NUMBER,
1501       p_init_msg_list      IN              VARCHAR2 := fnd_api.g_false,
1502       p_commit             IN              VARCHAR2 := fnd_api.g_false,
1503       p_validation_level   IN              NUMBER
1504             := fnd_api.g_valid_level_full,
1505       x_return_status      OUT NOCOPY      VARCHAR2,
1506       x_msg_count          OUT NOCOPY      NUMBER,
1507       x_msg_data           OUT NOCOPY      VARCHAR2,
1508       p_event_rec          IN              gmf_rcv_accounting_pkg.rcv_accttxn_rec_type,
1509       x_transaction_qty    OUT NOCOPY      NUMBER,
1510       x_primary_uom        OUT NOCOPY      mtl_units_of_measure.unit_of_measure%TYPE,
1511       x_primary_qty        OUT NOCOPY      NUMBER,
1512       x_trx_uom_code       OUT NOCOPY      VARCHAR2
1513    )
1514    IS
1515 -- local control variables
1516       l_api_name      CONSTANT VARCHAR2 (30)                 := 'Convert_UOM';
1517       l_api_version   CONSTANT NUMBER                                  := 1.0;
1518       l_stmt_num               NUMBER                                    := 0;
1519       l_api_message            VARCHAR2 (1000);
1520 -- local data variables
1521       l_item_id                NUMBER;
1522       l_primary_uom_rate       NUMBER;
1523       l_trx_uom_rate           NUMBER;
1524       l_primary_uom_code       mtl_units_of_measure.uom_code%TYPE;
1525       l_source_doc_uom_code    mtl_units_of_measure.uom_code%TYPE;
1526       l_trx_uom_code           mtl_units_of_measure.uom_code%TYPE;
1527       l_primary_uom            mtl_units_of_measure.unit_of_measure%TYPE;
1528    BEGIN
1529       SAVEPOINT convert_uom_pvt;
1530 
1531       -- Initialize message list if p_init_msg_list is set to TRUE
1532       IF fnd_api.to_boolean (p_init_msg_list)
1533       THEN
1534          fnd_msg_pub.initialize;
1535       END IF;
1536 
1537       IF     g_debug = 'Y'
1538          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
1539       THEN
1540          fnd_log.STRING (fnd_log.level_procedure,
1541                          g_log_head || l_api_name || '.begin',
1542                          'Convert_UOM <<'
1543                         );
1544       END IF;
1545 
1546       -- Standard check for compatibility
1547       IF NOT fnd_api.compatible_api_call (l_api_version,
1548                                           p_api_version,
1549                                           l_api_name,
1550                                           g_pkg_name
1551                                          )                          -- line 90
1552       THEN
1553          RAISE fnd_api.g_exc_unexpected_error;
1554       END IF;
1555 
1556       -- Initialize API return status to success
1557       x_return_status := fnd_api.g_ret_sts_success;
1558       x_msg_count := 0;
1559       x_msg_data := '';
1560       -- API body
1561       l_stmt_num := 10;
1562       l_item_id := p_event_rec.item_id;
1563 
1564       -- Get UOM code for the source document's UOM
1565       SELECT uom_code
1566         INTO l_source_doc_uom_code
1567         FROM mtl_units_of_measure
1568        WHERE unit_of_measure = p_event_rec.source_doc_uom;
1569 
1570       -- Get UOM code for the transaction UOM
1571       SELECT uom_code
1572         INTO l_trx_uom_code
1573         FROM mtl_units_of_measure
1574        WHERE unit_of_measure = p_event_rec.transaction_uom;
1575 
1576       -- Get UOM for this item/org from MSI and populate primary_uom with it
1577       IF (l_item_id IS NULL)
1578       THEN
1579          -- for a one-time item, the primary uom is the
1580          -- base uom for the item's current uom class
1581          l_stmt_num := 20;
1582 
1583          SELECT puom.uom_code, puom.unit_of_measure
1584            INTO l_primary_uom_code, l_primary_uom
1585            FROM mtl_units_of_measure tuom, mtl_units_of_measure puom
1586           WHERE tuom.unit_of_measure = p_event_rec.source_doc_uom
1587             AND tuom.uom_class = puom.uom_class
1588             AND puom.base_uom_flag = 'Y';
1589 
1590          l_item_id := 0;
1591       ELSE
1592          l_stmt_num := 30;
1593 
1594          SELECT primary_uom_code
1595            INTO l_primary_uom_code
1596            FROM mtl_system_items
1597           WHERE organization_id = p_event_rec.organization_id
1598             AND inventory_item_id = l_item_id;
1599 
1600          l_stmt_num := 40;
1601 
1602          SELECT unit_of_measure
1603            INTO l_primary_uom
1604            FROM mtl_units_of_measure
1605           WHERE uom_code = l_primary_uom_code;
1606       END IF;
1607 
1608       -- Get the UOM rate from source_doc_uom to primary_uom
1609       l_stmt_num := 50;
1610       inv_convert.inv_um_conversion (from_unit      => l_source_doc_uom_code,
1611                                      to_unit        => l_primary_uom_code,
1612                                      item_id        => l_item_id,
1613                                      uom_rate       => l_primary_uom_rate
1614                                     );
1615 
1616       IF (l_primary_uom_rate = -99999)
1617       THEN
1618          RAISE fnd_api.g_exc_error;
1619          l_api_message :=
1620                  'inv_convert.inv_um_conversion() failed to get the UOM rate';
1621 
1622          IF     g_debug = 'Y'
1623             AND fnd_log.level_error >= fnd_log.g_current_runtime_level
1624          THEN
1625             fnd_log.STRING (fnd_log.level_error,
1626                             g_log_head || l_api_name || '.' || l_stmt_num,
1627                             l_api_message
1628                            );
1629          END IF;
1630       END IF;
1631 
1632       -- Get the UOM rate from source_doc_uom to transaction_uom
1633       l_stmt_num := 60;
1634       inv_convert.inv_um_conversion (from_unit      => l_source_doc_uom_code,
1635                                      to_unit        => l_trx_uom_code,
1636                                      item_id        => l_item_id,
1637                                      uom_rate       => l_trx_uom_rate
1638                                     );
1639 
1640       IF (l_trx_uom_rate = -99999)
1641       THEN
1642          RAISE fnd_api.g_exc_error;
1643          l_api_message :=
1644                  'inv_convert.inv_um_conversion() failed to get the UOM rate';
1645 
1646          IF     g_debug = 'Y'
1647             AND fnd_log.level_error >= fnd_log.g_current_runtime_level
1648          THEN
1649             fnd_log.STRING (fnd_log.level_error,
1650                             g_log_head || l_api_name || '.' || l_stmt_num,
1651                             l_api_message
1652                            );
1653          END IF;
1654       END IF;
1655 
1656       -- Populate output variables
1657       x_primary_uom := l_primary_uom;
1658       x_primary_qty :=
1659                ROUND (l_primary_uom_rate * p_event_rec.source_doc_quantity, 6);
1660       x_transaction_qty :=
1661                    ROUND (l_trx_uom_rate * p_event_rec.source_doc_quantity, 6);
1662       x_trx_uom_code := l_trx_uom_code;
1663 
1664       IF     g_debug = 'Y'
1665          AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
1666       THEN
1667          l_api_message :=
1668                'x_primary_uom : '
1669             || x_primary_uom
1670             || ' x_primary_qty : '
1671             || x_primary_qty
1672             || ' x_transaction_qty : '
1673             || x_transaction_qty
1674             || ' x_trx_uom_code : '
1675             || x_trx_uom_code;
1676          fnd_log.STRING (fnd_log.level_statement,
1677                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
1678                          l_api_message
1679                         );
1680       END IF;
1681 
1682       -- End of API body
1683 
1684       -- Standard check of P_COMMIT
1685       IF fnd_api.to_boolean (p_commit)
1686       THEN
1687          COMMIT WORK;
1688       END IF;
1689 
1690       IF     g_debug = 'Y'
1691          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
1692       THEN
1693          fnd_log.STRING (fnd_log.level_procedure,
1694                          g_log_head || l_api_name || '.end',
1695                          'Convert_UOM >>'
1696                         );
1697       END IF;
1698    EXCEPTION
1699       WHEN fnd_api.g_exc_error
1700       THEN
1701          ROLLBACK TO convert_uom_pvt;
1702          x_return_status := fnd_api.g_ret_sts_error;
1703          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
1704                                     p_data       => x_msg_data
1705                                    );
1706       WHEN fnd_api.g_exc_unexpected_error
1707       THEN
1708          ROLLBACK TO convert_uom_pvt;
1709          x_return_status := fnd_api.g_ret_sts_unexp_error;
1710          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
1711                                     p_data       => x_msg_data
1712                                    );
1713       WHEN NO_DATA_FOUND
1714       THEN
1715          ROLLBACK TO convert_uom_pvt;
1716          x_return_status := fnd_api.g_ret_sts_error;
1717          l_api_message :=
1718                 ': Statement # ' || TO_CHAR (l_stmt_num)
1719                 || ' - No UOM found.';
1720 
1721          IF     g_debug = 'Y'
1722             AND fnd_log.level_error >= fnd_log.g_current_runtime_level
1723          THEN
1724             fnd_log.STRING (fnd_log.level_error,
1725                             g_log_head || l_api_name || '.' || l_stmt_num,
1726                             l_api_message
1727                            );
1728          END IF;
1729 
1730          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error)
1731          THEN
1732             fnd_msg_pub.add_exc_msg (g_pkg_name, l_api_name || l_api_message);
1733          END IF;
1734 
1735          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
1736                                     p_data       => x_msg_data
1737                                    );
1738       WHEN OTHERS
1739       THEN
1740          ROLLBACK TO convert_uom_pvt;
1741          x_return_status := fnd_api.g_ret_sts_unexp_error;
1742          l_api_message :=
1743                'Unexpected Error at statement('
1744             || TO_CHAR (l_stmt_num)
1745             || '): '
1746             || TO_CHAR (SQLCODE)
1747             || '- '
1748             || SUBSTRB (SQLERRM, 1, 100);
1749 
1750          IF     g_debug = 'Y'
1751             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
1752          THEN
1753             fnd_log.STRING (fnd_log.level_unexpected,
1754                             g_log_head || l_api_name || '.' || l_stmt_num,
1755                             l_api_message
1756                            );
1757          END IF;
1758 
1759          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
1760          THEN
1761             fnd_msg_pub.add_exc_msg (g_pkg_name, l_api_name || l_api_message);
1762          END IF;
1763 
1764          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
1765                                     p_data       => x_msg_data
1766                                    );
1767    END convert_uom;
1768 
1769 -----------------------------------------------------------------------------
1770 -- Start of comments                                                       --
1771 --                                                                         --
1772 -- PROCEDURE                                                               --
1773 --  Get_Currency    This procedure returns the currency_conversion         --
1774 --                  parameters, conversion rate, date and type             --
1775 --
1776 --                  It is being coded for the purpose of providing the     --
1777 --                  currency conversion parameters for Global Procurement  --
1778 --                  and true drop shipment scenario, but may be used as a  --
1779 --                  generic API to return currency conversion rates for    --
1780 --                  Receiving transactions.                                --
1781 --                                                                         --
1782 --                  Logic:                                                 --
1783 --                  If supplier facing org, if match to po use POD.rate    --
1784 --                                          else                           --
1785 --                                          rcv_transactions.curr_conv_rate--
1786 --                  Else                                                   --
1787 --                  Get the conversion type                                --
1788 --                  Determine currency conversion rate                     --
1789 --                                                                         --
1790 --                                                                         --
1791 --                                                                         --
1792 
1793    --                                                                         --
1794 -- VERSION 1.0                                                             --
1795 --                                                                         --
1796 -- PARAMETERS                                                              --
1797 --  P_API_VERSION      API Version # - REQUIRED: enter 1.0                 --
1798 --  P_INIT_MSG_LIST    Initialize message list? True/False                 --
1799 --  P_COMMIT           Should the API commit before returning? True/False  --
1800 --  P_rcv_accttxn        Record storing an RCV Accounting Event (GRAT)        --
1801 --  X_CURRENCY_CODE                                                        --
1802 --  X_CURRENCY_CONVERSION_RATE                                             --
1803 --  X_CURRENCY_CONVERSION_TYPE                                             --
1804 --  X_CURRENCY_CONVERSION_TYPE                                             --
1805 --  X_RETURN_STATUS    Success/Error/Unexplained error - 'S','E', or 'U'   --
1806 --  X_MSG_COUNT        Message Count - # of messages placed in message list--
1807 --  X_MSG_DATA         Message Text - returns msg contents if msg_count = 1--
1808 --                                                                         --
1809 -- HISTORY:                                                                --
1810 --    08/02/03     Anju Gupta     Created                                  --
1811 -- End of comments                                                         --
1812 -----------------------------------------------------------------------------
1813    PROCEDURE get_currency (
1814       p_api_version                IN              NUMBER,
1815       p_init_msg_list              IN              VARCHAR2 := fnd_api.g_false,
1816       p_commit                     IN              VARCHAR2 := fnd_api.g_false,
1817       p_validation_level           IN              NUMBER
1818             := fnd_api.g_valid_level_full,
1819       x_return_status              OUT NOCOPY      VARCHAR2,
1820       x_msg_count                  OUT NOCOPY      NUMBER,
1821       x_msg_data                   OUT NOCOPY      VARCHAR2,
1822       p_rcv_accttxn                IN              gmf_rcv_accounting_pkg.rcv_accttxn_rec_type,
1823       x_currency_code              OUT NOCOPY      VARCHAR2,
1824       x_currency_conversion_rate   OUT NOCOPY      NUMBER,
1825       x_currency_conversion_date   OUT NOCOPY      DATE,
1826       x_currency_conversion_type   OUT NOCOPY      VARCHAR2
1827    )
1828    IS
1829 -- local control variables
1830       l_api_name          CONSTANT VARCHAR2 (30)   := 'GET_Currency';
1831       l_api_version       CONSTANT NUMBER          := 1.0;
1832       l_stmt_num                   NUMBER          := 0;
1833       l_api_message                VARCHAR2 (1000);
1834 -- local data variables
1835       l_match_option               VARCHAR2 (1);
1836       l_currency_code              VARCHAR2 (3);
1837       l_currency_conversion_rate   NUMBER;
1838       l_currency_conversion_date   DATE;
1839       l_currency_conversion_type   VARCHAR2 (30)   := '';
1840       l_ledger_id                  NUMBER;
1841       l_po_line_location_id        NUMBER;
1842       l_rcv_transaction_id         NUMBER;
1843    BEGIN
1844       SAVEPOINT get_currency_pvt;
1845 
1846 -- Standard call to check for call compatibility
1847       IF NOT fnd_api.compatible_api_call (l_api_version,
1848                                           p_api_version,
1849                                           l_api_name,
1850                                           g_pkg_name
1851                                          )
1852       THEN
1853          RAISE fnd_api.g_exc_unexpected_error;
1854       END IF;
1855 
1856 -- Initialize message list if p_init_msg_list is set to TRUE
1857       IF fnd_api.to_boolean (p_init_msg_list)
1858       THEN
1859          fnd_msg_pub.initialize;
1860       END IF;
1861 
1862 -- Initialize API return status to success
1863       x_return_status := fnd_api.g_ret_sts_success;
1864 -- API body
1865       l_stmt_num := 10;
1866 
1867       IF (    (p_rcv_accttxn.procurement_org_flag = 'Y')
1868           AND (p_rcv_accttxn.event_type_id NOT IN
1869                                 (intercompany_invoice, intercompany_reversal)
1870               )
1871          )
1872       THEN
1873          l_currency_code := p_rcv_accttxn.currency_code;
1874          l_stmt_num := 20;
1875 
1876          SELECT line_location_id
1877            INTO l_po_line_location_id
1878            FROM po_distributions
1879           WHERE po_distribution_id = p_rcv_accttxn.po_distribution_id;
1880 
1881          l_stmt_num := 30;
1882 
1883          SELECT match_option
1884            INTO l_match_option
1885            FROM po_line_locations
1886           WHERE line_location_id = l_po_line_location_id;
1887 
1888          -- Always use rate on the PO distribution for encumbrance reversals.
1889          IF (   l_match_option = 'P'
1890              OR (p_rcv_accttxn.event_type_id = encumbrance_reversal)
1891             )
1892          THEN
1893             l_stmt_num := 40;
1894 
1895             SELECT NVL (pod.rate, 1), poh.rate_type,
1896                    pod.rate_date
1897               INTO l_currency_conversion_rate, l_currency_conversion_type,
1898                    l_currency_conversion_date
1899               FROM po_distributions pod, po_headers poh
1900              WHERE pod.po_distribution_id = p_rcv_accttxn.po_distribution_id
1901                AND poh.po_header_id = pod.po_header_id;
1902          ELSE
1903             -- This is also correct for ADJUST transactions where we only create one event
1904             -- for every parent transaction. In the case of a Match to receipt PO, the
1905             -- currency conversion rate of the child transactions (DELIVER CORRECT, RTR,
1906             -- RTV) will be the same as the currency conversion rate on the parent
1907             -- RECEIVE/MATCH transaction. This will be the case even if the daily rate
1908             -- has changed between the time that the parent transaction was done and the
1909             -- time that the child transactions were done.
1910             l_stmt_num := 50;
1911 
1912             SELECT rt.currency_conversion_rate, rt.currency_conversion_type,
1913                    rt.currency_conversion_date
1914               INTO l_currency_conversion_rate, l_currency_conversion_type,
1915                    l_currency_conversion_date
1916               FROM rcv_transactions rt
1917              WHERE rt.transaction_id = p_rcv_accttxn.rcv_transaction_id;
1918          END IF;
1919       ELSE
1920          l_currency_code := p_rcv_accttxn.currency_code;
1921          l_ledger_id := p_rcv_accttxn.ledger_id;
1922          -- Use profile INV: Intercompany Currency conversion Type, to determine Conversion Type
1923          -- Ensure that INV uses the same type for conversion for GP/ DS scenarios
1924          l_stmt_num := 70;
1925          fnd_profile.get ('IC_CURRENCY_CONVERSION_TYPE',
1926                           l_currency_conversion_type
1927                          );
1928          l_stmt_num := 80;
1929          l_currency_conversion_rate :=
1930             gl_currency_api.get_rate
1931                          (x_set_of_books_id      => l_ledger_id,
1932                           x_from_currency        => l_currency_code,
1933                           x_conversion_date      => p_rcv_accttxn.transaction_date,
1934                           x_conversion_type      => l_currency_conversion_type
1935                          );
1936       END IF;
1937 
1938       x_currency_code := l_currency_code;
1939       x_currency_conversion_rate := l_currency_conversion_rate;
1940       x_currency_conversion_date := NVL (l_currency_conversion_date, SYSDATE);
1941       x_currency_conversion_type := l_currency_conversion_type;
1942 
1943       IF     g_debug = 'Y'
1944          AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
1945       THEN
1946          l_api_message :=
1947             SUBSTR (   'x_currency_code : '
1948                     || x_currency_code
1949                     || ' x_currency_conversion_rate : '
1950                     || TO_CHAR (x_currency_conversion_rate)
1951                     || ' x_currency_conversion_date : '
1952                     || TO_CHAR (x_currency_conversion_date, 'DD-MON-YY')
1953                     || ' x_currency_conversion_type : '
1954                     || x_currency_conversion_type,
1955                     1,
1956                     1000
1957                    );
1958          fnd_log.STRING (fnd_log.level_statement,
1959                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
1960                          l_api_message
1961                         );
1962       END IF;
1963 
1964 -- End of API body
1965       fnd_msg_pub.count_and_get (p_encoded      => fnd_api.g_false,
1966                                  p_count        => x_msg_count,
1967                                  p_data         => x_msg_data
1968                                 );
1969 
1970 -- Standard check of P_COMMIT
1971       IF fnd_api.to_boolean (p_commit)
1972       THEN
1973          COMMIT WORK;
1974       END IF;
1975    EXCEPTION
1976       WHEN fnd_api.g_exc_error
1977       THEN
1978          ROLLBACK TO get_currency_pvt;
1979          x_return_status := fnd_api.g_ret_sts_error;
1980          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
1981                                     p_data       => x_msg_data
1982                                    );
1983       WHEN fnd_api.g_exc_unexpected_error
1984       THEN
1985          ROLLBACK TO get_currency_pvt;
1986          x_return_status := fnd_api.g_ret_sts_unexp_error;
1987          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
1988                                     p_data       => x_msg_data
1989                                    );
1990       WHEN NO_DATA_FOUND
1991       THEN
1992          ROLLBACK TO get_currency_pvt;
1993          x_return_status := fnd_api.g_ret_sts_error;
1994          l_api_message :=
1995                'Unexpected Error: '
1996             || l_stmt_num
1997             || TO_CHAR (SQLCODE)
1998             || '- '
1999             || SUBSTRB (SQLERRM, 1, 200);
2000 
2001          IF     g_debug = 'Y'
2002             AND fnd_log.level_error >= fnd_log.g_current_runtime_level
2003          THEN
2004             fnd_log.STRING (fnd_log.level_error,
2005                             g_log_head || l_api_name || '.' || l_stmt_num,
2006                             l_api_message
2007                            );
2008          END IF;
2009 
2010          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
2011          THEN
2012             fnd_msg_pub.add_exc_msg (g_pkg_name,
2013                                         l_api_name
2014                                      || 'Statement -'
2015                                      || TO_CHAR (l_stmt_num)
2016                                     );
2017          END IF;
2018 
2019          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
2020                                     p_data       => x_msg_data
2021                                    );
2022       WHEN OTHERS
2023       THEN
2024          ROLLBACK TO get_currency_pvt;
2025          x_return_status := fnd_api.g_ret_sts_unexp_error;
2026          l_api_message :=
2027                'Unexpected Error: '
2028             || l_stmt_num
2029             || TO_CHAR (SQLCODE)
2030             || '- '
2031             || SUBSTRB (SQLERRM, 1, 200);
2032 
2033          IF     g_debug = 'Y'
2034             AND fnd_log.level_error >= fnd_log.g_current_runtime_level
2035          THEN
2036             fnd_log.STRING (fnd_log.level_error,
2037                             g_log_head || l_api_name || '.' || l_stmt_num,
2038                             l_api_message
2039                            );
2040          END IF;
2041 
2042          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
2043          THEN
2044             fnd_msg_pub.add_exc_msg (g_pkg_name,
2045                                         l_api_name
2046                                      || 'Statement -'
2047                                      || TO_CHAR (l_stmt_num)
2048                                     );
2049          END IF;
2050 
2051          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
2052                                     p_data       => x_msg_data
2053                                    );
2054    END get_currency;
2055 
2056 -- Start of comments
2057 --      API name        : Get_Accounts
2058 --      Type            : Private
2059 --      Function        : To get the credit and debit accounts for each event.
2060 --      Pre-reqs        :
2061 --      Parameters      :
2062 --      IN              :       p_api_version           IN NUMBER       Required
2063 --                              p_init_msg_list         IN VARCHAR2     Optional
2064 --                                      Default = FND_API.G_FALSE
2065 --                              p_commit                IN VARCHAR2     Optional
2066 --                                      Default = FND_API.G_FALSE
2067 --                              p_validation_level      IN NUMBER       Optional
2068 --                                      Default = FND_API.G_VALID_LEVEL_FULL
2069 --                              p_rcv_accttxn             IN rcv_accttxn_rec_type       Required
2070 --                              p_transaction_forward_flow_rec  mtl_transaction_flow_rec_type,
2071 --                              p_transaction_reverse_flow_rec  mtl_transaction_flow_rec_type,
2072 --
2073 --      OUT             :       x_return_status         OUT     VARCHAR2(1)
2074 --                              x_msg_count             OUT     NUMBER
2075 --                              x_msg_data              OUT     VARCHAR2(2000)
2076 --          x_credit_acct_id  OUT   NUMBER
2077 --          x_debit_acct_id      OUT   NUMBER
2078 --          x_ic_cogs_acct_id OUT   NUMBER
2079 --      Version :
2080 --                        Initial version       1.0
2081 --
2082 --
2083 --      Notes           : This API creates all accounting transactions for RETURN TO VENDOR transactions
2084 --                        in gmf_rcv_accounting_txns.
2085 --
2086 -- End of comments
2087    PROCEDURE get_accounts (
2088       p_api_version                    IN              NUMBER,
2089       p_init_msg_list                  IN              VARCHAR2
2090             := fnd_api.g_false,
2091       p_commit                         IN              VARCHAR2
2092             := fnd_api.g_false,
2093       p_validation_level               IN              NUMBER
2094             := fnd_api.g_valid_level_full,
2095       x_return_status                  OUT NOCOPY      VARCHAR2,
2096       x_msg_count                      OUT NOCOPY      NUMBER,
2097       x_msg_data                       OUT NOCOPY      VARCHAR2,
2098       p_rcv_accttxn                    IN              gmf_rcv_accounting_pkg.rcv_accttxn_rec_type,
2099       p_transaction_forward_flow_rec                   inv_transaction_flow_pub.mtl_transaction_flow_rec_type,
2100       p_transaction_reverse_flow_rec                   inv_transaction_flow_pub.mtl_transaction_flow_rec_type,
2101       x_credit_acct_id                 OUT NOCOPY      NUMBER,
2102       x_debit_acct_id                  OUT NOCOPY      NUMBER,
2103       x_ic_cogs_acct_id                OUT NOCOPY      NUMBER
2104    )
2105    IS
2106       l_api_name        CONSTANT VARCHAR2 (30)              := 'Get_Accounts';
2107       l_api_version     CONSTANT NUMBER                                := 1.0;
2108       l_return_status            VARCHAR2 (1)    := fnd_api.g_ret_sts_success;
2109       l_msg_count                NUMBER                                  := 0;
2110       l_msg_data                 VARCHAR2 (8000)                        := '';
2111       l_stmt_num                 NUMBER                                  := 0;
2112       l_api_message              VARCHAR2 (1000);
2113       l_credit_acct_id           NUMBER;
2114       l_debit_acct_id            NUMBER;
2115       l_dist_acct_id             NUMBER;
2116       l_ic_cogs_acct_id          NUMBER;
2117       l_ic_coss_acct_id          NUMBER;
2118       l_pod_accrual_acct_id      NUMBER;
2119       l_pod_ccid                 NUMBER;
2120       l_dest_pod_ccid            NUMBER;
2121       l_pod_budget_acct_id       NUMBER;
2122       l_receiving_insp_acct_id   NUMBER;
2123       l_clearing_acct_id         NUMBER;
2124       l_retroprice_adj_acct_id   NUMBER;
2125       l_overlaid_acct            NUMBER;
2126       l_trx_type                 rcv_transactions.transaction_type%TYPE;
2127       l_parent_trx_type          rcv_transactions.transaction_type%TYPE;
2128       l_parent_trx_id            NUMBER;
2129       l_account_flag             NUMBER                                  := 0;
2130    BEGIN
2131       -- Standard start of API savepoint
2132       SAVEPOINT get_accounts_pvt;
2133       l_stmt_num := 0;
2134 
2135       IF     g_debug = 'Y'
2136          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
2137       THEN
2138          fnd_log.STRING (fnd_log.level_procedure,
2139                          g_log_head || '.' || l_api_name || '.begin',
2140                          'Get_Accounts <<'
2141                         );
2142       END IF;
2143 
2144       -- Standard call to check for call compatibility
2145       IF NOT fnd_api.compatible_api_call (l_api_version,
2146                                           p_api_version,
2147                                           l_api_name,
2148                                           g_pkg_name
2149                                          )
2150       THEN
2151          RAISE fnd_api.g_exc_unexpected_error;
2152       END IF;
2153 
2154       -- Initialize message list if p_init_msg_list is set to TRUE
2155       IF fnd_api.to_boolean (p_init_msg_list)
2156       THEN
2157          fnd_msg_pub.initialize;
2158       END IF;
2159 
2160       -- Initialize API return status to success
2161       x_return_status := fnd_api.g_ret_sts_success;
2162       x_credit_acct_id := NULL;
2163       x_debit_acct_id := NULL;
2164       x_ic_cogs_acct_id := NULL;
2165       -- No accounts are stored for IC transactions.
2166       l_stmt_num := 5;
2167 
2168       IF (p_rcv_accttxn.event_type_id IN
2169                                 (intercompany_invoice, intercompany_reversal)
2170          )
2171       THEN
2172          RETURN;
2173       END IF;
2174 
2175       l_stmt_num := 10;
2176 
2177       SELECT pod.accrual_account_id, pod.code_combination_id,
2178              NVL (pod.dest_charge_account_id, pod.code_combination_id),
2179              pod.budget_account_id
2180         INTO l_pod_accrual_acct_id, l_pod_ccid,
2181              l_dest_pod_ccid,
2182              l_pod_budget_acct_id
2183         FROM po_distributions pod
2184        WHERE pod.po_distribution_id = p_rcv_accttxn.po_distribution_id;
2185 
2186       l_stmt_num := 20;
2187 
2188       IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
2189         fnd_log.STRING  (
2190                         fnd_log.level_procedure,
2191                         g_log_head || '.' || l_api_name || 'stmt num 20',
2192                         p_rcv_accttxn.organization_id
2193                         );
2194       END IF;
2195 
2196       SELECT receiving_account_id, clearing_account_id,
2197              retroprice_adj_account_id
2198         INTO l_receiving_insp_acct_id, l_clearing_acct_id,
2199              l_retroprice_adj_acct_id
2200         FROM rcv_parameters
2201        WHERE organization_id = p_rcv_accttxn.organization_id;
2202 
2203        IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
2204         fnd_log.STRING  (
2205                         fnd_log.level_procedure,
2206                         g_log_head || '.' || l_api_name || 'stmt num 20-2',
2207                         p_rcv_accttxn.organization_id
2208                         );
2209        END IF;
2210 
2211       -- Changes for JFMIP. Bug # 3076229. Call API to override the balancing segment
2212       -- of the Receiving Inspection account for expense destination types. The option
2213       -- (Auto Offset Override on PO_SYSTEM_PARAMETERS) will only be available in orgs
2214       -- where encumbrance is enabled. Hence this is not applicable to Global Procurement,
2215       -- Drop Ship or retroactive pricing.
2216       IF (    p_rcv_accttxn.trx_flow_header_id IS NULL
2217           AND p_rcv_accttxn.event_type_id IN
2218                  (receive,
2219                   match,
2220                   deliver,
2221                   correct,
2222                   return_to_receiving,
2223                   return_to_vendor
2224                  )
2225           AND p_rcv_accttxn.destination_type_code = 'EXPENSE'
2226          )
2227       THEN
2228          l_stmt_num := 30;
2229          po_accounting_grp.build_offset_account
2230                                     (p_api_version          => 1.0,
2231                                      p_init_msg_list        => fnd_api.g_false,
2232                                      x_return_status        => l_return_status,
2233                                      p_base_ccid            => l_receiving_insp_acct_id,
2234                                      p_overlay_ccid         => l_dest_pod_ccid,
2235                                      p_accounting_date      => SYSDATE,
2236                                      p_org_id               => p_rcv_accttxn.org_id,
2237                                      x_result_ccid          => l_overlaid_acct
2238                                     );
2239          l_receiving_insp_acct_id := l_overlaid_acct;
2240       END IF;
2241 
2242       IF (p_rcv_accttxn.event_type_id = correct)
2243       THEN
2244          l_stmt_num := 40;
2245 
2246          SELECT parent_trx.transaction_type
2247            INTO l_parent_trx_type
2248            FROM rcv_transactions trx, rcv_transactions parent_trx
2249           WHERE trx.transaction_id = p_rcv_accttxn.rcv_transaction_id
2250             AND trx.parent_transaction_id = parent_trx.transaction_id;
2251       END IF;
2252 
2253       l_stmt_num := 50;
2254 
2255       IF (   (p_rcv_accttxn.event_type_id = receive)
2256           OR (p_rcv_accttxn.event_type_id = match)
2257           OR (    p_rcv_accttxn.event_type_id = correct
2258               AND l_parent_trx_type = 'RECEIVE'
2259              )
2260           OR     p_rcv_accttxn.event_type_id = correct
2261              AND l_parent_trx_type = 'MATCH'
2262          )
2263       THEN
2264          l_debit_acct_id := l_receiving_insp_acct_id;
2265 
2266          IF (p_rcv_accttxn.procurement_org_flag = 'Y')
2267          THEN
2268             l_credit_acct_id := l_pod_accrual_acct_id;
2269          ELSIF (p_rcv_accttxn.item_id IS NULL)
2270          THEN
2271             l_credit_acct_id :=
2272                     p_transaction_reverse_flow_rec.expense_accrual_account_id;
2273          ELSE
2274             l_credit_acct_id :=
2275                   p_transaction_reverse_flow_rec.inventory_accrual_account_id;
2276          END IF;
2277       ELSIF (p_rcv_accttxn.event_type_id = logical_receive)
2278       THEN
2279          -- Use clearing account for :
2280          --     a. destination type of Inventory
2281          --     b. destination type of Expense for inventory items.
2282          -- Use Cost of Sales account for
2283          --     a. destination type of Shop Floor.
2284          --     b. destination type of Expense for one time items
2285          IF (   p_rcv_accttxn.destination_type_code = 'INVENTORY'
2286              OR (    p_rcv_accttxn.destination_type_code = 'EXPENSE'
2287                  AND p_rcv_accttxn.item_id IS NOT NULL
2288                 )
2289             )
2290          THEN
2291             l_debit_acct_id := l_clearing_acct_id;
2292          ELSIF (p_rcv_accttxn.procurement_org_flag = 'Y')
2293          THEN
2294             l_debit_acct_id := l_pod_ccid;
2295          ELSE
2296             l_stmt_num := 60;
2297 
2298             SELECT cost_of_sales_account
2299               INTO l_ic_coss_acct_id
2300               FROM mtl_parameters mp
2301              WHERE mp.organization_id = p_rcv_accttxn.organization_id;
2302 
2303             l_stmt_num := 70;
2304             get_hookaccount
2305                     (p_api_version               => l_api_version,
2306                      x_return_status             => l_return_status,
2307                      x_msg_count                 => l_msg_count,
2308                      x_msg_data                  => l_msg_data,
2309                      p_rcv_transaction_id        => p_rcv_accttxn.rcv_transaction_id,
2310                      p_accounting_line_type      => 'IC Cost Of Sales',
2311                      p_org_id                    => p_rcv_accttxn.org_id,
2312                      x_distribution_acct_id      => l_dist_acct_id
2313                     );
2314 
2315             IF l_return_status <> fnd_api.g_ret_sts_success
2316             THEN
2317                l_api_message := 'Error in Get_HookAccount';
2318 
2319                IF     g_debug = 'Y'
2320                   AND fnd_log.level_unexpected >=
2321                                                fnd_log.g_current_runtime_level
2322                THEN
2323                   fnd_log.STRING (fnd_log.level_unexpected,
2324                                   g_log_head || '.' || l_api_name
2325                                   || l_stmt_num,
2326                                      'Get_Accounts : '
2327                                   || l_stmt_num
2328                                   || ' : '
2329                                   || l_api_message
2330                                  );
2331                END IF;
2332 
2333                RAISE fnd_api.g_exc_unexpected_error;
2334             END IF;
2335 
2336             IF (l_dist_acct_id = -1)
2337             THEN
2338                l_debit_acct_id := l_ic_coss_acct_id;
2339             ELSE
2340                l_debit_acct_id := l_dist_acct_id;
2341             END IF;
2342          END IF;
2343 
2344          l_stmt_num := 80;
2345 
2346          IF (p_rcv_accttxn.procurement_org_flag = 'Y')
2347          THEN
2348             l_credit_acct_id := l_pod_accrual_acct_id;
2349          ELSIF (p_rcv_accttxn.item_id IS NULL)
2350          THEN
2351             l_credit_acct_id :=
2352                     p_transaction_reverse_flow_rec.expense_accrual_account_id;
2353          ELSE
2354             l_credit_acct_id :=
2355                   p_transaction_reverse_flow_rec.inventory_accrual_account_id;
2356          END IF;
2357       ELSIF (   p_rcv_accttxn.event_type_id = deliver
2358              OR (    p_rcv_accttxn.event_type_id = correct
2359                  AND l_parent_trx_type = 'DELIVER'
2360                 )
2361             )
2362       THEN
2363          l_debit_acct_id := l_dest_pod_ccid;
2364          l_credit_acct_id := l_receiving_insp_acct_id;
2365       ELSIF (   p_rcv_accttxn.event_type_id = return_to_vendor
2366              OR (    p_rcv_accttxn.event_type_id = correct
2367                  AND l_parent_trx_type = 'RETURN TO VENDOR'
2368                 )
2369             )
2370       THEN
2371          l_credit_acct_id := l_receiving_insp_acct_id;
2372 
2373          IF (p_rcv_accttxn.procurement_org_flag = 'Y')
2374          THEN
2375             l_debit_acct_id := l_pod_accrual_acct_id;
2376          ELSIF (p_rcv_accttxn.item_id IS NULL)
2377          THEN
2378             l_debit_acct_id :=
2379                     p_transaction_reverse_flow_rec.expense_accrual_account_id;
2380          ELSE
2381             l_debit_acct_id :=
2382                   p_transaction_reverse_flow_rec.inventory_accrual_account_id;
2383          END IF;
2384       ELSIF (p_rcv_accttxn.event_type_id = logical_return_to_vendor)
2385       THEN
2386          IF (   p_rcv_accttxn.destination_type_code = 'INVENTORY'
2387              OR (    p_rcv_accttxn.destination_type_code = 'EXPENSE'
2388                  AND p_rcv_accttxn.item_id IS NOT NULL
2389                 )
2390             )
2391          THEN
2392             l_credit_acct_id := l_clearing_acct_id;
2393          ELSIF (p_rcv_accttxn.procurement_org_flag = 'Y')
2394          THEN
2395             l_credit_acct_id := l_pod_ccid;
2396          ELSE
2397             l_stmt_num := 90;
2398 
2399             SELECT cost_of_sales_account
2400               INTO l_ic_coss_acct_id
2401               FROM mtl_parameters mp
2402              WHERE mp.organization_id = p_rcv_accttxn.organization_id;
2403 
2404             l_stmt_num := 100;
2405             get_hookaccount
2406                     (p_api_version               => l_api_version,
2407                      x_return_status             => l_return_status,
2408                      x_msg_count                 => l_msg_count,
2409                      x_msg_data                  => l_msg_data,
2410                      p_rcv_transaction_id        => p_rcv_accttxn.rcv_transaction_id,
2411                      p_accounting_line_type      => 'IC Cost Of Sales',
2412                      p_org_id                    => p_rcv_accttxn.org_id,
2413                      x_distribution_acct_id      => l_dist_acct_id
2414                     );
2415 
2416             IF l_return_status <> fnd_api.g_ret_sts_success
2417             THEN
2418                l_api_message := 'Error in Get_HookAccount';
2419 
2420                IF     g_debug = 'Y'
2421                   AND fnd_log.level_unexpected >=
2422                                                fnd_log.g_current_runtime_level
2423                THEN
2424                   fnd_log.STRING (fnd_log.level_unexpected,
2425                                   g_log_head || '.' || l_api_name
2426                                   || l_stmt_num,
2427                                      'Get_Accounts : '
2428                                   || l_stmt_num
2429                                   || ' : '
2430                                   || l_api_message
2431                                  );
2432                END IF;
2433 
2434                RAISE fnd_api.g_exc_unexpected_error;
2435             END IF;
2436 
2437             IF (l_dist_acct_id = -1)
2438             THEN
2439                l_credit_acct_id := l_ic_coss_acct_id;
2440             ELSE
2441                l_credit_acct_id := l_dist_acct_id;
2442             END IF;
2443          END IF;
2444 
2445          IF (p_rcv_accttxn.procurement_org_flag = 'Y')
2446          THEN
2447             l_debit_acct_id := l_pod_accrual_acct_id;
2448          ELSIF (p_rcv_accttxn.item_id IS NULL)
2449          THEN
2450             l_debit_acct_id :=
2451                     p_transaction_reverse_flow_rec.expense_accrual_account_id;
2452          ELSE
2453             l_debit_acct_id :=
2454                   p_transaction_reverse_flow_rec.inventory_accrual_account_id;
2455          END IF;
2456       ELSIF (   p_rcv_accttxn.event_type_id = return_to_receiving
2457              OR (    p_rcv_accttxn.event_type_id = correct
2458                  AND l_parent_trx_type = 'RETURN TO RECEIVING'
2459                 )
2460             )
2461       THEN
2462          l_credit_acct_id := l_dest_pod_ccid;
2463          l_debit_acct_id := l_receiving_insp_acct_id;
2464       ELSIF (p_rcv_accttxn.event_type_id = adjust_receive)
2465       THEN
2466          -- In the case of drop shipments, we always use the clearing account instead of the Receiving
2467          -- Inspection account. In these scenarios, we should be posting the adjustment for the entire
2468          -- Receipt to the retroactive price adjustment account.
2469          IF (   p_rcv_accttxn.trx_flow_header_id IS NOT NULL
2470              OR p_rcv_accttxn.drop_ship_flag IN (1, 2)
2471             )
2472          THEN
2473             -- For global procurement scenarios, the debit account is :
2474             -- Retroprice adjustment account for inv items and direct items.
2475             -- IC Cost Of Sales(Charge acct on POD) for one-time items and Expense destinations.
2476             IF (   p_rcv_accttxn.item_id IS NOT NULL
2477                 OR p_rcv_accttxn.destination_type_code = 'SHOP FLOOR'
2478                )
2479             THEN
2480                l_stmt_num := 110;
2481                get_hookaccount
2482                    (p_api_version               => l_api_version,
2483                     x_return_status             => l_return_status,
2484                     x_msg_count                 => l_msg_count,
2485                     x_msg_data                  => l_msg_data,
2486                     p_rcv_transaction_id        => p_rcv_accttxn.rcv_transaction_id,
2487                     p_accounting_line_type      => 'Retroprice Adjustment',
2488                     p_org_id                    => p_rcv_accttxn.org_id,
2489                     x_distribution_acct_id      => l_dist_acct_id
2490                    );
2491 
2492                IF l_return_status <> fnd_api.g_ret_sts_success
2493                THEN
2494                   l_api_message := 'Error in Get_HookAccount';
2495 
2496                   IF     g_debug = 'Y'
2497                      AND fnd_log.level_unexpected >=
2498                                                fnd_log.g_current_runtime_level
2499                   THEN
2500                      fnd_log.STRING (fnd_log.level_unexpected,
2501                                         g_log_head
2502                                      || '.'
2503                                      || l_api_name
2504                                      || l_stmt_num,
2505                                         'Get_Accounts : '
2506                                      || l_stmt_num
2507                                      || ' : '
2508                                      || l_api_message
2509                                     );
2510                   END IF;
2511 
2512                   RAISE fnd_api.g_exc_unexpected_error;
2513                END IF;
2514 
2515                IF (l_dist_acct_id = -1)
2516                THEN
2517                   l_debit_acct_id := l_retroprice_adj_acct_id;
2518                ELSE
2519                   l_debit_acct_id := l_dist_acct_id;
2520                END IF;
2521             ELSE
2522                l_debit_acct_id := l_pod_ccid;
2523             END IF;
2524          ELSE
2525             l_debit_acct_id := l_receiving_insp_acct_id;
2526          END IF;
2527 
2528          l_credit_acct_id := l_pod_accrual_acct_id;
2529       ELSIF (p_rcv_accttxn.event_type_id = adjust_deliver)
2530       THEN
2531          -- Redundant check. Transaction flow header id is always NULL. We only
2532          -- get ADJUST_RECEIVE transactions for global procurement.
2533          IF (    p_rcv_accttxn.trx_flow_header_id IS NULL
2534              AND p_rcv_accttxn.drop_ship_flag NOT IN (1, 2)
2535             )
2536          THEN
2537             IF (p_rcv_accttxn.destination_type_code = 'EXPENSE')
2538             THEN
2539                l_debit_acct_id := l_dest_pod_ccid;
2540             ELSE
2541                l_stmt_num := 120;
2542                get_hookaccount
2543                    (p_api_version               => l_api_version,
2544                     x_return_status             => l_return_status,
2545                     x_msg_count                 => l_msg_count,
2546                     x_msg_data                  => l_msg_data,
2547                     p_rcv_transaction_id        => p_rcv_accttxn.rcv_transaction_id,
2548                     p_accounting_line_type      => 'Retroprice Adjustment',
2549                     p_org_id                    => p_rcv_accttxn.org_id,
2550                     x_distribution_acct_id      => l_dist_acct_id
2551                    );
2552 
2553                IF l_return_status <> fnd_api.g_ret_sts_success
2554                THEN
2555                   l_api_message := 'Error in Get_HookAccount';
2556 
2557                   IF     g_debug = 'Y'
2558                      AND fnd_log.level_unexpected >=
2559                                                fnd_log.g_current_runtime_level
2560                   THEN
2561                      fnd_log.STRING (fnd_log.level_unexpected,
2562                                         g_log_head
2563                                      || '.'
2564                                      || l_api_name
2565                                      || l_stmt_num,
2566                                         'Get_Accounts : '
2567                                      || l_stmt_num
2568                                      || ' : '
2569                                      || l_api_message
2570                                     );
2571                   END IF;
2572 
2573                   RAISE fnd_api.g_exc_unexpected_error;
2574                END IF;
2575 
2576                IF (l_dist_acct_id = -1)
2577                THEN
2578                   l_debit_acct_id := l_retroprice_adj_acct_id;
2579                ELSE
2580                   l_debit_acct_id := l_dist_acct_id;
2581                END IF;
2582             END IF;
2583          END IF;
2584 
2585          l_stmt_num := 130;
2586          l_credit_acct_id := l_receiving_insp_acct_id;
2587       ELSIF (   p_rcv_accttxn.event_type_id = intercompany_invoice
2588              OR p_rcv_accttxn.event_type_id = intercompany_reversal
2589             )
2590       THEN
2591          l_credit_acct_id := NULL;
2592          l_debit_acct_id := NULL;
2593       ELSIF (p_rcv_accttxn.event_type_id = encumbrance_reversal)
2594       THEN
2595          l_stmt_num := 140;
2596 
2597          SELECT rt.transaction_type, rt.parent_transaction_id
2598            INTO l_trx_type, l_parent_trx_id
2599            FROM rcv_transactions rt
2600           WHERE rt.transaction_id = p_rcv_accttxn.rcv_transaction_id;
2601 
2602          IF (l_trx_type = 'DELIVER')
2603          THEN
2604             l_credit_acct_id := l_pod_budget_acct_id;
2605             l_debit_acct_id := NULL;
2606          ELSIF (l_trx_type = 'RETURN TO RECEIVING')
2607          THEN
2608             l_debit_acct_id := l_pod_budget_acct_id;
2609             l_credit_acct_id := NULL;
2610          ELSIF (l_trx_type = 'CORRECT')
2611          THEN
2612             l_stmt_num := 150;
2613 
2614             SELECT parent_trx.transaction_type
2615               INTO l_parent_trx_type
2616               FROM rcv_transactions parent_trx
2617              WHERE parent_trx.transaction_id = l_parent_trx_id;
2618 
2619             IF (l_parent_trx_type = 'DELIVER')
2620             THEN
2621                l_credit_acct_id := l_pod_budget_acct_id;
2622                l_debit_acct_id := NULL;
2623             ELSIF (l_parent_trx_type = 'RETURN_TO_RECEIVING')
2624             THEN
2625                l_debit_acct_id := l_pod_budget_acct_id;
2626                l_credit_acct_id := NULL;
2627             END IF;
2628          END IF;
2629       END IF;
2630 
2631       x_debit_acct_id := l_debit_acct_id;
2632       x_credit_acct_id := l_credit_acct_id;
2633       x_ic_cogs_acct_id :=
2634                    p_transaction_forward_flow_rec.intercompany_cogs_account_id;
2635 
2636       IF     g_debug = 'Y'
2637          AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
2638       THEN
2639          l_api_message :=
2640                'x_debit_acct_id : '
2641             || x_debit_acct_id
2642             || ' x_credit_acct_id : '
2643             || x_credit_acct_id
2644             || ' x_ic_cogs_acct_id : '
2645             || x_ic_cogs_acct_id;
2646          fnd_log.STRING (fnd_log.level_statement,
2647                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
2648                          l_api_message
2649                         );
2650       END IF;
2651 
2652       IF (    (l_debit_acct_id IS NULL OR l_credit_acct_id IS NULL)
2653           AND (p_rcv_accttxn.event_type_id NOT IN
2654                   (intercompany_invoice,
2655                    intercompany_reversal,
2656                    encumbrance_reversal
2657                   )
2658               )
2659          )
2660       THEN
2661          IF     g_debug = 'Y'
2662             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
2663          THEN
2664             l_api_message :=
2665                'Unable to find credit and/or debit account. Setup is incomplete. ';
2666             fnd_log.STRING (fnd_log.level_statement,
2667                             g_log_head || '.' || l_api_name || '.'
2668                             || l_stmt_num,
2669                             l_api_message
2670                            );
2671          END IF;
2672 
2673          fnd_message.set_name ('PO', 'PO_INVALID_ACCOUNT');
2674          fnd_msg_pub.ADD;
2675 
2676          IF     g_debug = 'Y'
2677             AND fnd_log.level_error >= fnd_log.g_current_runtime_level
2678          THEN
2679             fnd_log.MESSAGE (fnd_log.level_error,
2680                              g_log_head || '.' || l_api_name || l_stmt_num,
2681                              FALSE
2682                             );
2683          END IF;
2684 
2685          RAISE fnd_api.g_exc_error;
2686       END IF;
2687 
2688       -- Standard check of p_commit
2689       IF fnd_api.to_boolean (p_commit)
2690       THEN
2691          COMMIT WORK;
2692       END IF;
2693 
2694       -- Standard Call to get message count and if count = 1, get message info
2695       fnd_msg_pub.count_and_get (p_count      => x_msg_count,
2696                                  p_data       => x_msg_data);
2697 
2698       IF     g_debug = 'Y'
2699          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
2700       THEN
2701          fnd_log.STRING (fnd_log.level_procedure,
2702                          g_log_head || '.' || l_api_name || '.end',
2703                          'Get_Accounts >>'
2704                         );
2705       END IF;
2706    EXCEPTION
2707       WHEN fnd_api.g_exc_error
2708       THEN
2709          ROLLBACK TO get_accounts_pvt;
2710          x_return_status := fnd_api.g_ret_sts_error;
2711          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
2712                                     p_data       => x_msg_data
2713                                    );
2714       WHEN fnd_api.g_exc_unexpected_error
2715       THEN
2716          ROLLBACK TO get_accounts_pvt;
2717          x_return_status := fnd_api.g_ret_sts_unexp_error;
2718          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
2719                                     p_data       => x_msg_data
2720                                    );
2721       WHEN OTHERS
2722       THEN
2723          ROLLBACK TO get_accounts_pvt;
2724          x_return_status := fnd_api.g_ret_sts_unexp_error;
2725 
2726          IF     g_debug = 'Y'
2727             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
2728          THEN
2729             fnd_log.STRING (fnd_log.level_unexpected,
2730                             g_log_head || '.' || l_api_name || l_stmt_num,
2731                                'Get_Accounts : '
2732                             || l_stmt_num
2733                             || ' : '
2734                             || SUBSTR (SQLERRM, 1, 200)
2735                            );
2736          END IF;
2737 
2738          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
2739          THEN
2740             fnd_msg_pub.add_exc_msg (g_pkg_name,
2741                                         l_api_name
2742                                      || 'Statement -'
2743                                      || TO_CHAR (l_stmt_num)
2744                                     );
2745          END IF;
2746 
2747          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
2748                                     p_data       => x_msg_data
2749                                    );
2750    END get_accounts;
2751 
2752 -- Start of comments
2753 --      API name        : Get_UssglTC
2754 --      Type            : Private
2755 --      Function        : To get the USSGL Transaction code, if applicable.
2756 --      Pre-reqs        :
2757 --      Parameters      :
2758 --      IN              :       p_api_version           IN NUMBER       Required
2759 --                              p_init_msg_list         IN VARCHAR2     Optional
2760 --                                      Default = FND_API.G_FALSE
2761 --                              p_commit                IN VARCHAR2     Optional
2762 --                                      Default = FND_API.G_FALSE
2763 --                              p_validation_level      IN NUMBER       Optional
2764 --                                      Default = FND_API.G_VALID_LEVEL_FULL
2765 --                              p_rcv_accttxn             IN rcv_accttxn_rec_type       Required
2766 --
2767 --      OUT             :       x_return_status         OUT     VARCHAR2(1)
2768 --                              x_msg_count             OUT     NUMBER
2769 --                              x_msg_data              OUT     VARCHAR2(2000)
2770 --          x_ussgl_tc     OUT   VARCHAR2(30)
2771 --      Version :
2772 --                        Initial version       1.0
2773 --
2774 --
2775 --      Notes           : This procedure returns the USSGL Transaction Code for the event,
2776 --         if applicable.
2777 --
2778 -- End of comments
2779    PROCEDURE get_ussgltc (
2780       p_api_version        IN              NUMBER,
2781       p_init_msg_list      IN              VARCHAR2 := fnd_api.g_false,
2782       p_commit             IN              VARCHAR2 := fnd_api.g_false,
2783       p_validation_level   IN              NUMBER
2784             := fnd_api.g_valid_level_full,
2785       x_return_status      OUT NOCOPY      VARCHAR2,
2786       x_msg_count          OUT NOCOPY      NUMBER,
2787       x_msg_data           OUT NOCOPY      VARCHAR2,
2788       p_rcv_accttxn        IN              gmf_rcv_accounting_pkg.rcv_accttxn_rec_type,
2789       x_ussgl_tc           OUT NOCOPY      VARCHAR2
2790    )
2791    IS
2792       l_api_name      CONSTANT VARCHAR2 (30)   := 'Get_UssglTC';
2793       l_api_version   CONSTANT NUMBER          := 1.0;
2794       l_return_status          VARCHAR2 (1)    := fnd_api.g_ret_sts_success;
2795       l_msg_count              NUMBER          := 0;
2796       l_msg_data               VARCHAR2 (8000) := '';
2797       l_stmt_num               NUMBER          := 0;
2798       l_api_message            VARCHAR2 (1000);
2799       l_ussgl_tc               VARCHAR2 (30);
2800       l_ussgl_option           VARCHAR2 (1);
2801    BEGIN
2802       -- Standard start of API savepoint
2803       SAVEPOINT get_ussgltc_pvt;
2804       l_stmt_num := 0;
2805 
2806       IF     g_debug = 'Y'
2807          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
2808       THEN
2809          fnd_log.STRING (fnd_log.level_procedure,
2810                          g_log_head || '.' || l_api_name || '.begin',
2811                          'Get_UssglTC <<'
2812                         );
2813       END IF;
2814 
2815       -- Standard call to check for call compatibility
2816       IF NOT fnd_api.compatible_api_call (l_api_version,
2817                                           p_api_version,
2818                                           l_api_name,
2819                                           g_pkg_name
2820                                          )
2821       THEN
2822          RAISE fnd_api.g_exc_unexpected_error;
2823       END IF;
2824 
2825       -- Initialize message list if p_init_msg_list is set to TRUE
2826       IF fnd_api.to_boolean (p_init_msg_list)
2827       THEN
2828          fnd_msg_pub.initialize;
2829       END IF;
2830 
2831       -- Initialize API return status to success
2832       x_return_status := fnd_api.g_ret_sts_success;
2833       l_ussgl_option := NVL (fnd_profile.VALUE ('USSGL_OPTION'), 'N');
2834 
2835       IF (l_ussgl_option = 'Y')
2836       THEN
2837          IF (p_rcv_accttxn.event_type_id = encumbrance_reversal)
2838          THEN
2839             l_stmt_num := 10;
2840 
2841             SELECT ussgl_transaction_code
2842               INTO l_ussgl_tc
2843               FROM po_distributions
2844              WHERE po_distribution_id = p_rcv_accttxn.po_distribution_id;
2845          ELSIF (p_rcv_accttxn.event_type_id IN (deliver, return_to_receiving)
2846                )
2847          THEN
2848             l_stmt_num := 20;
2849 
2850             SELECT rsl.ussgl_transaction_code
2851               INTO l_ussgl_tc
2852               FROM rcv_transactions rt, rcv_shipment_lines rsl
2853              WHERE rt.transaction_id = p_rcv_accttxn.rcv_transaction_id
2854                AND rt.shipment_line_id = rsl.shipment_line_id;
2855          END IF;
2856       END IF;
2857 
2858       x_ussgl_tc := l_ussgl_tc;
2859 
2860       IF     g_debug = 'Y'
2861          AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
2862       THEN
2863          l_api_message := 'x_ussgl_tc : ' || x_ussgl_tc;
2864          fnd_log.STRING (fnd_log.level_statement,
2865                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
2866                          l_api_message
2867                         );
2868       END IF;
2869 
2870       --- Standard check of p_commit
2871       IF fnd_api.to_boolean (p_commit)
2872       THEN
2873          COMMIT WORK;
2874       END IF;
2875 
2876       -- Standard Call to get message count and if count = 1, get message info
2877       fnd_msg_pub.count_and_get (p_count      => x_msg_count,
2878                                  p_data       => x_msg_data);
2879 
2880       IF     g_debug = 'Y'
2881          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
2882       THEN
2883          fnd_log.STRING (fnd_log.level_procedure,
2884                          g_log_head || '.' || l_api_name || '.end',
2885                          'Get_UssglTC >>'
2886                         );
2887       END IF;
2888    EXCEPTION
2889       WHEN fnd_api.g_exc_error
2890       THEN
2891          ROLLBACK TO get_ussgltc_pvt;
2892          x_return_status := fnd_api.g_ret_sts_error;
2893          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
2894                                     p_data       => x_msg_data
2895                                    );
2896       WHEN fnd_api.g_exc_unexpected_error
2897       THEN
2898          ROLLBACK TO get_ussgltc_pvt;
2899          x_return_status := fnd_api.g_ret_sts_unexp_error;
2900          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
2901                                     p_data       => x_msg_data
2902                                    );
2903       WHEN OTHERS
2904       THEN
2905          ROLLBACK TO get_ussgltc_pvt;
2906          x_return_status := fnd_api.g_ret_sts_unexp_error;
2907 
2908          IF     g_debug = 'Y'
2909             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
2910          THEN
2911             fnd_log.STRING (fnd_log.level_unexpected,
2912                             g_log_head || '.' || l_api_name || l_stmt_num,
2913                                'Get_UssglTC : '
2914                             || l_stmt_num
2915                             || ' : '
2916                             || SUBSTR (SQLERRM, 1, 200)
2917                            );
2918          END IF;
2919 
2920          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
2921          THEN
2922             fnd_msg_pub.add_exc_msg (g_pkg_name,
2923                                         l_api_name
2924                                      || 'Statement -'
2925                                      || TO_CHAR (l_stmt_num)
2926                                     );
2927          END IF;
2928 
2929          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
2930                                     p_data       => x_msg_data
2931                                    );
2932    END get_ussgltc;
2933 
2934 -- Start of comments
2935 --      API name        : Check_EncumbranceFlag
2936 --      Type            : Private
2937 --      Function        : Checks to see if encumbrance entries need to be created.
2938 --      Pre-reqs        :
2939 --      Parameters      :
2940 --      IN              :       p_api_version           IN NUMBER       Required
2941 --                              p_init_msg_list         IN VARCHAR2     Optional
2942 --                                      Default = FND_API.G_FALSE
2943 --                              p_commit                IN VARCHAR2     Optional
2944 --                                      Default = FND_API.G_FALSE
2945 --                              p_validation_level      IN NUMBER       Optional
2946 --                                      Default = FND_API.G_VALID_LEVEL_FULL
2947 --                    p_rcv_ledger_id            IN      NUMBER  Required
2948 --                    p_po_header_id          IN      NUMBER  Required
2949 --
2950 --                    x_encumbrance_flag      OUT     VARCHAR2(1)
2951 --          x_ussgl_option    OUT     VARCHAR2(1)
2952 --
2953 --
2954 --      OUT             :       x_return_status         OUT     VARCHAR2(1)
2955 --                              x_msg_count             OUT     NUMBER
2956 --                              x_msg_data              OUT     VARCHAR2(2000)
2957 --      Version :
2958 --                        Initial version       1.0
2959 --
2960 --
2961 --      Notes           : This API checks to see if encumbrance entries need to
2962 --         be created.
2963 --
2964 -- End of comments
2965    PROCEDURE check_encumbranceflag (
2966       p_api_version        IN              NUMBER,
2967       p_init_msg_list      IN              VARCHAR2 := fnd_api.g_false,
2968       p_commit             IN              VARCHAR2 := fnd_api.g_false,
2969       p_validation_level   IN              NUMBER
2970             := fnd_api.g_valid_level_full,
2971       x_return_status      OUT NOCOPY      VARCHAR2,
2972       x_msg_count          OUT NOCOPY      NUMBER,
2973       x_msg_data           OUT NOCOPY      VARCHAR2,
2974       p_rcv_ledger_id      IN              NUMBER,
2975       x_encumbrance_flag   OUT NOCOPY      VARCHAR2,
2976       x_ussgl_option       OUT NOCOPY      VARCHAR2
2977    )
2978    IS
2979       l_api_name      CONSTANT VARCHAR2 (30)   := 'Check_EncumbranceFlag';
2980       l_api_version   CONSTANT NUMBER          := 1.0;
2981       l_return_status          VARCHAR2 (1)    := fnd_api.g_ret_sts_success;
2982       l_msg_count              NUMBER          := 0;
2983       l_msg_data               VARCHAR2 (8000) := '';
2984       l_stmt_num               NUMBER          := 0;
2985       l_api_message            VARCHAR2 (1000);
2986       l_encumbrance_flag       VARCHAR2 (1);
2987    BEGIN
2988       -- Standard start of API savepoint
2989       SAVEPOINT check_encumbranceflag_pvt;
2990       l_stmt_num := 0;
2991 
2992       IF     g_debug = 'Y'
2993          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
2994       THEN
2995          fnd_log.STRING (fnd_log.level_procedure,
2996                          g_log_head || '.' || l_api_name || '.begin',
2997                          'Check_EncumbranceFlag <<'
2998                         );
2999       END IF;
3000 
3001       -- Standard call to check for call compatibility
3002       IF NOT fnd_api.compatible_api_call (l_api_version,
3003                                           p_api_version,
3004                                           l_api_name,
3005                                           g_pkg_name
3006                                          )
3007       THEN
3008          RAISE fnd_api.g_exc_unexpected_error;
3009       END IF;
3010 
3011       -- Initialize message list if p_init_msg_list is set to TRUE
3012       IF fnd_api.to_boolean (p_init_msg_list)
3013       THEN
3014          fnd_msg_pub.initialize;
3015       END IF;
3016 
3017       -- Initialize API return status to success
3018       x_return_status := fnd_api.g_ret_sts_success;
3019       l_stmt_num := 10;
3020 
3021       SELECT NVL (fsp.purch_encumbrance_flag, 'N')
3022         INTO l_encumbrance_flag
3023         FROM financials_system_parameters fsp
3024        WHERE fsp.set_of_books_id = p_rcv_ledger_id;
3025 
3026       x_encumbrance_flag := l_encumbrance_flag;
3027       x_ussgl_option := NVL (fnd_profile.VALUE ('USSGL_OPTION'), 'N');
3028 
3029       IF     g_debug = 'Y'
3030          AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
3031       THEN
3032          l_api_message :=
3033                'Encumbrance Flag : '
3034             || x_encumbrance_flag
3035             || ' Ussgl Option : '
3036             || x_ussgl_option;
3037          fnd_log.STRING (fnd_log.level_statement,
3038                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
3039                          l_api_message
3040                         );
3041       END IF;
3042 
3043       -- Standard check of p_commit
3044       IF fnd_api.to_boolean (p_commit)
3045       THEN
3046          COMMIT WORK;
3047       END IF;
3048 
3049       -- Standard Call to get message count and if count = 1, get message info
3050       fnd_msg_pub.count_and_get (p_count      => x_msg_count,
3051                                  p_data       => x_msg_data);
3052 
3053       IF     g_debug = 'Y'
3054          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
3055       THEN
3056          fnd_log.STRING (fnd_log.level_procedure,
3057                          g_log_head || '.' || l_api_name || '.end',
3058                          'Check_EncumbranceFlag >>'
3059                         );
3060       END IF;
3061    EXCEPTION
3062       WHEN fnd_api.g_exc_error
3063       THEN
3064          ROLLBACK TO check_encumbranceflag_pvt;
3065          x_return_status := fnd_api.g_ret_sts_error;
3066          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
3067                                     p_data       => x_msg_data
3068                                    );
3069       WHEN fnd_api.g_exc_unexpected_error
3070       THEN
3071          ROLLBACK TO check_encumbranceflag_pvt;
3072          x_return_status := fnd_api.g_ret_sts_unexp_error;
3073          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
3074                                     p_data       => x_msg_data
3075                                    );
3076       WHEN OTHERS
3077       THEN
3078          ROLLBACK TO check_encumbranceflag_pvt;
3079          x_return_status := fnd_api.g_ret_sts_unexp_error;
3080 
3081          IF     g_debug = 'Y'
3082             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
3083          THEN
3084             fnd_log.STRING (fnd_log.level_unexpected,
3085                             g_log_head || '.' || l_api_name || l_stmt_num,
3086                                'Check_EncumbranceFlag : '
3087                             || l_stmt_num
3088                             || ' : '
3089                             || SUBSTR (SQLERRM, 1, 200)
3090                            );
3091          END IF;
3092 
3093          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
3094          THEN
3095             fnd_msg_pub.add_exc_msg (g_pkg_name,
3096                                         l_api_name
3097                                      || 'Statement -'
3098                                      || TO_CHAR (l_stmt_num)
3099                                     );
3100          END IF;
3101 
3102          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
3103                                     p_data       => x_msg_data
3104                                    );
3105    END check_encumbranceflag;
3106 
3107 -----------------------------------------------------------------------------
3108 -- Start of comments                                                       --
3109 --                                                                         --
3110 -- PROCEDURE                                                               --
3111 --  Create_MMTRecord  This API takes an GRAT record along with the          --
3112 --                    parameters listed above and converts them into a     --
3113 --                    single MMT record which will be used in a subsequent --
3114 --                    function to make the physical insert into MMT        --
3115 --                                                                         --
3116 --                                                                         --
3117 -- VERSION 1.0                                                             --
3118 --                                                                         --
3119 -- PARAMETERS                                                              --
3120 --  P_API_VERSION      API Version # - REQUIRED: enter 1.0                 --
3121 --  P_INIT_MSG_LIST    Initialize message list? True/False                 --
3122 --  P_COMMIT           Should the API commit before returning? True/False  --
3123 --  P_VALIDATION_LEVEL Specify the level of validation on the inputs       --
3124 --  X_RETURN_STATUS    Success/Error/Unexplained error - 'S','E', or 'U'   --
3125 --  X_MSG_COUNT        Message Count - # of messages placed in message list--
3126 --  X_MSG_DATA         Message Text - returns msg contents if msg_count = 1--
3127 --  P_rcv_accttxn        Represents a single GRAT, used to build the MMT entry--
3128 --  P_TXN_TYPE_ID      Txn Type ID of the new MMT row being created        --
3129 --  P_INTERCOMPANY_PRICE  The calling fcn must determine how to populate   --
3130 --                     this based on the txn type and on the OU's position --
3131 --                     in the txn flow. It will represent the transfer     --
3132 --                     price between this OU and an adjacent one.          --
3133 --  P_INTERCOMPANY_CURR_CODE This parameter represents the currency code   --
3134 --           of the intercompany price.               --
3135 --  P_ACCT_ID          Used to populate MMT.distribution_account_id        --
3136 --  P_SIGN             Used to set the signs (+/-) of the primary quantity --
3137 --                     and the transaction quantity                        --
3138 --  P_PARENT_TXN_FLAG  1 - Indicates that this is the parent transaction   --
3139 --  P_TRANSFER_ORGANIZATION_ID The calling function should pass the        --
3140 --           organization from the next event.        --
3141 --  X_INV_TRX          Returns the record that will be inserted into MMT   --
3142 --                                                                         --
3143 -- HISTORY:                                                                --
3144 --    7/21/03     Bryan Kuntz      Created                                --
3145 -- End of comments                                                         --
3146 -----------------------------------------------------------------------------
3147    PROCEDURE create_mmtrecord (
3148       p_api_version                IN              NUMBER,
3149       p_init_msg_list              IN              VARCHAR2 := fnd_api.g_false,
3150       p_commit                     IN              VARCHAR2 := fnd_api.g_false,
3151       p_validation_level           IN              NUMBER
3152             := fnd_api.g_valid_level_full,
3153       x_return_status              OUT NOCOPY      VARCHAR2,
3154       x_msg_count                  OUT NOCOPY      NUMBER,
3155       x_msg_data                   OUT NOCOPY      VARCHAR2,
3156       p_rcv_accttxn                IN              gmf_rcv_accounting_pkg.rcv_accttxn_rec_type,
3157       p_txn_type_id                IN              NUMBER,
3158       p_intercompany_price         IN              NUMBER,
3159       p_intercompany_curr_code     IN              VARCHAR2,
3160       p_acct_id                    IN              NUMBER,
3161       p_sign                       IN              NUMBER,
3162       p_parent_txn_flag            IN              NUMBER,
3163       p_transfer_organization_id   IN              NUMBER,
3164       x_inv_trx                    OUT NOCOPY      inv_logical_transaction_global.mtl_trx_rec_type
3165    )
3166    IS
3167       l_api_name      CONSTANT VARCHAR2 (30)            := 'Create_MMTRecord';
3168       l_api_version   CONSTANT NUMBER                                  := 1.0;
3169       l_api_message            VARCHAR2 (1000);
3170       l_return_status          VARCHAR2 (1)      := fnd_api.g_ret_sts_success;
3171       l_msg_count              NUMBER                                    := 0;
3172       l_msg_data               VARCHAR2 (8000)                          := '';
3173       l_stmt_num               NUMBER                                    := 0;
3174       l_ctr                    BINARY_INTEGER;
3175       l_unit_price             NUMBER;
3176       l_inv_trx                inv_logical_transaction_global.mtl_trx_rec_type;
3177       l_le_id                  NUMBER;
3178                              -- holds legal entity ID for timezone conversion
3179       l_le_txn_date            DATE;
3180          -- transaction date truncated and converted to legal entity timezone
3181       invalid_txn_type         EXCEPTION;
3182    BEGIN
3183 -- Standard start of API savepoint
3184       SAVEPOINT create_mmtrecord_pvt;
3185 
3186       IF     g_debug = 'Y'
3187          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
3188       THEN
3189          fnd_log.STRING (fnd_log.level_procedure,
3190                          g_log_head || '.' || l_api_name || '.begin',
3191                          'Create_MMTRecord <<'
3192                         );
3193       END IF;
3194 
3195 -- Standard call to check for call compatibility
3196       IF NOT fnd_api.compatible_api_call (l_api_version,
3197                                           p_api_version,
3198                                           l_api_name,
3199                                           g_pkg_name
3200                                          )
3201       THEN
3202          RAISE fnd_api.g_exc_unexpected_error;
3203       END IF;
3204 
3205 -- Initialize message list if p_init_msg_list is set to TRUE
3206       IF fnd_api.to_boolean (p_init_msg_list)
3207       THEN
3208          fnd_msg_pub.initialize;
3209       END IF;
3210 
3211 -- Initialize API return status to success
3212       x_return_status := fnd_api.g_ret_sts_success;
3213       x_msg_count := 0;
3214       x_msg_data := '';
3215 -- API Body
3216       l_inv_trx.intercompany_pricing_option :=
3217                                      p_rcv_accttxn.intercompany_pricing_option;
3218       l_stmt_num := 5;
3219 
3220       -- Assign transaction action, source type, and invoiced flag
3221       IF (p_txn_type_id = 11)
3222       THEN
3223          l_inv_trx.transaction_action_id := 9;
3224          l_inv_trx.transaction_source_type_id := 13;
3225          l_inv_trx.invoiced_flag := 'N';
3226       ELSIF (p_txn_type_id = 14)
3227       THEN
3228          l_inv_trx.transaction_action_id := 14;
3229          l_inv_trx.transaction_source_type_id := 13;
3230          l_inv_trx.invoiced_flag := 'N';
3231       ELSIF (p_txn_type_id = 69)
3232       THEN
3233          l_inv_trx.transaction_action_id := 11;
3234          l_inv_trx.transaction_source_type_id := 1;
3235 
3236          IF (p_rcv_accttxn.procurement_org_flag = 'Y')
3237          THEN
3238             l_inv_trx.invoiced_flag := NULL;
3239             l_inv_trx.intercompany_pricing_option := 1;
3240          ELSE
3241             l_inv_trx.invoiced_flag := 'N';
3242          END IF;
3243       ELSIF (p_txn_type_id = 19)
3244       THEN
3245          l_inv_trx.transaction_action_id := 26;
3246          l_inv_trx.transaction_source_type_id := 1;
3247          l_inv_trx.invoiced_flag := NULL;
3248          l_inv_trx.intercompany_pricing_option := 1;
3249       ELSIF (p_txn_type_id = 22)
3250       THEN
3251          l_inv_trx.transaction_action_id := 10;
3252          l_inv_trx.transaction_source_type_id := 13;
3253          l_inv_trx.invoiced_flag := 'N';
3254       ELSIF (p_txn_type_id = 23)
3255       THEN
3256          l_inv_trx.transaction_action_id := 13;
3257          l_inv_trx.transaction_source_type_id := 13;
3258          l_inv_trx.invoiced_flag := 'N';
3259       ELSIF (p_txn_type_id = 39)
3260       THEN
3261          l_inv_trx.transaction_action_id := 7;
3262          l_inv_trx.transaction_source_type_id := 1;
3263          l_inv_trx.invoiced_flag := NULL;
3264          l_inv_trx.intercompany_pricing_option := 1;
3265       ELSE
3266          l_api_message := 'Invalid transaction type';
3267          RAISE invalid_txn_type;
3268       END IF;
3269 
3270 -- Set currency columns
3271       l_stmt_num := 20;
3272 
3273       IF (p_txn_type_id IN (19, 39))
3274       THEN
3275          l_inv_trx.currency_code := p_rcv_accttxn.currency_code;
3276          l_inv_trx.currency_conversion_rate :=
3277                                        p_rcv_accttxn.currency_conversion_rate;
3278          l_inv_trx.currency_conversion_type :=
3279                                        p_rcv_accttxn.currency_conversion_type;
3280          l_inv_trx.currency_conversion_date := SYSDATE;
3281       ELSE
3282          l_inv_trx.currency_code := NULL;
3283          l_inv_trx.currency_conversion_rate := NULL;
3284          l_inv_trx.currency_conversion_type := NULL;
3285          l_inv_trx.currency_conversion_date := NULL;
3286       END IF;
3287 
3288       l_stmt_num := 30;
3289 
3290 -- Compute unit price and intercompany price
3291       IF (p_rcv_accttxn.intercompany_pricing_option = 2)
3292       THEN
3293          l_unit_price :=
3294               p_rcv_accttxn.unit_price
3295             * p_rcv_accttxn.source_doc_quantity
3296             / p_rcv_accttxn.primary_quantity;
3297       ELSE
3298          l_unit_price :=
3299               (p_rcv_accttxn.unit_price + p_rcv_accttxn.unit_nr_tax
3300               )
3301             * p_rcv_accttxn.source_doc_quantity
3302             / p_rcv_accttxn.primary_quantity;
3303       END IF;
3304 
3305       l_stmt_num := 40;
3306       l_api_message := 'No data';
3307 
3308 -- Main select statement to populate the l_inv_trx record
3309       SELECT p_rcv_accttxn.organization_id, p_rcv_accttxn.item_id,
3310              p_txn_type_id, rt.po_header_id,
3311              p_sign * ABS (p_rcv_accttxn.transaction_quantity),
3312              p_rcv_accttxn.trx_uom_code,
3313              p_sign * ABS (p_rcv_accttxn.primary_quantity),
3314              rt.transaction_date,
3315              DECODE (NVL (fc.minimum_accountable_unit, 0),
3316                      0, ROUND (l_unit_price * p_rcv_accttxn.primary_quantity,
3317                                fc.PRECISION
3318                               )
3319                       * p_rcv_accttxn.currency_conversion_rate
3320                       / p_rcv_accttxn.primary_quantity,
3321                        ROUND (  l_unit_price
3322                               * p_rcv_accttxn.primary_quantity
3323                               / fc.minimum_accountable_unit
3324                              )
3325                      * fc.minimum_accountable_unit
3326                      * p_rcv_accttxn.currency_conversion_rate
3327                      / p_rcv_accttxn.primary_quantity
3328                     ),
3329              'RCV', rt.transaction_id,
3330              rt.transaction_id,
3331              p_transfer_organization_id, NULL,
3332 --pod.project_id,  remove these 2 because projects will cause failure in inv's create_logical_txns
3333              NULL,
3334 --pod.task_id,     since they are only expected values in the org that does the deliver
3335                   poll.ship_to_location_id,
3336              1, p_rcv_accttxn.trx_flow_header_id,
3337              DECODE (NVL (fc.minimum_accountable_unit, 0),
3338                      0, ROUND (  p_intercompany_price
3339                                * p_rcv_accttxn.primary_quantity,
3340                                fc.PRECISION
3341                               )
3342                       / p_rcv_accttxn.primary_quantity,
3343                        ROUND (  p_intercompany_price
3344                               * p_rcv_accttxn.primary_quantity
3345                               / fc.minimum_accountable_unit
3346                              )
3347                      * fc.minimum_accountable_unit
3348                      / p_rcv_accttxn.primary_quantity
3349                     ),
3350              p_intercompany_curr_code,
3351              p_acct_id, 'N',
3352              NULL, NULL,
3353              p_parent_txn_flag, NULL
3354         INTO l_inv_trx.organization_id, l_inv_trx.inventory_item_id,
3355              l_inv_trx.transaction_type_id, l_inv_trx.transaction_source_id,
3356              l_inv_trx.transaction_quantity,
3357              l_inv_trx.transaction_uom,
3358              l_inv_trx.primary_quantity,
3359              l_inv_trx.transaction_date,
3360              l_inv_trx.transaction_cost,
3361              l_inv_trx.source_code, l_inv_trx.source_line_id,
3362              l_inv_trx.rcv_transaction_id,
3363              l_inv_trx.transfer_organization_id, l_inv_trx.project_id,
3364              l_inv_trx.task_id, l_inv_trx.ship_to_location_id,
3365              l_inv_trx.transaction_mode, l_inv_trx.trx_flow_header_id,
3366              l_inv_trx.intercompany_cost,
3367              l_inv_trx.intercompany_currency_code,
3368              l_inv_trx.distribution_account_id, l_inv_trx.costed_flag,
3369              l_inv_trx.subinventory_code, l_inv_trx.locator_id,
3370              l_inv_trx.parent_transaction_flag, l_inv_trx.trx_source_line_id
3371         FROM rcv_transactions rt,
3372              po_lines pol,
3373              po_line_locations poll,
3374              po_distributions pod,
3375              fnd_currencies fc
3376        WHERE rt.transaction_id = p_rcv_accttxn.rcv_transaction_id
3377          AND pol.po_line_id = p_rcv_accttxn.po_line_id
3378          AND poll.line_location_id = p_rcv_accttxn.po_line_location_id
3379          AND pod.po_distribution_id = p_rcv_accttxn.po_distribution_id
3380          AND fc.currency_code = p_rcv_accttxn.currency_code;
3381 
3382       l_stmt_num := 50;
3383       l_api_message := 'Inventory accounting period not open.';
3384 
3385       /* get the legal entity for timezone conversion */
3386       SELECT TO_NUMBER (org_information2)
3387         INTO l_le_id
3388         FROM hr_organization_information
3389        WHERE organization_id = p_rcv_accttxn.organization_id
3390          AND org_information_context = 'Accounting Information';
3391 
3392       l_stmt_num := 55;
3393       /* convert the transaction date into legal entity timezone (truncated) */
3394       l_le_txn_date :=
3395          inv_le_timezone_pub.get_le_day_for_server
3396                                                   (l_inv_trx.transaction_date,
3397                                                    l_le_id
3398                                                   );
3399       l_stmt_num := 60;
3400 
3401       /* retrieve the accounting period ID */
3402       SELECT acct_period_id
3403         INTO l_inv_trx.acct_period_id
3404         FROM org_acct_periods
3405        WHERE organization_id = p_rcv_accttxn.organization_id
3406          AND l_le_txn_date BETWEEN period_start_date AND schedule_close_date
3407          AND open_flag = 'Y';
3408 
3409       /* -- comment out this call for ST bug 3261222
3410       OE_DROP_SHIP_GRP.Get_Drop_Ship_Line_Ids(
3411            p_po_header_id     => p_rcv_accttxn.po_header_id,
3412            p_po_line_id       => p_rcv_accttxn.po_line_id,
3413            p_po_line_location_id    => p_rcv_accttxn.po_line_location_id,
3414            p_po_release_id    => l_po_release_id,
3415            x_line_id       => l_inv_trx.trx_source_line_id,
3416            x_num_lines     => l_so_num_lines,
3417            x_header_id     => l_so_header_id,
3418            x_org_id        => l_so_org_id);
3419       */
3420       x_inv_trx := l_inv_trx;
3421 
3422 -- ***************
3423 
3424       -- Standard check of p_commit
3425       IF fnd_api.to_boolean (p_commit)
3426       THEN
3427          COMMIT WORK;
3428       END IF;
3429 
3430 -- Standard Call to get message count and if count = 1, get message info
3431       fnd_msg_pub.count_and_get (p_count      => x_msg_count,
3432                                  p_data       => x_msg_data);
3433 
3434       IF     g_debug = 'Y'
3435          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
3436       THEN
3437          fnd_log.STRING (fnd_log.level_procedure,
3438                          g_log_head || '.' || l_api_name || '.end',
3439                          'Create_MMTRecord >>'
3440                         );
3441       END IF;
3442    EXCEPTION
3443       WHEN fnd_api.g_exc_error
3444       THEN
3445          ROLLBACK TO create_mmtrecord_pvt;
3446          x_return_status := fnd_api.g_ret_sts_error;
3447          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
3448                                     p_data       => x_msg_data
3449                                    );
3450       WHEN fnd_api.g_exc_unexpected_error
3451       THEN
3452          ROLLBACK TO create_mmtrecord_pvt;
3453          x_return_status := fnd_api.g_ret_sts_unexp_error;
3454          l_api_message :=
3455                      'Unexpected error at statement ' || TO_CHAR (l_stmt_num);
3456 
3457          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
3458          THEN
3459             fnd_msg_pub.add_exc_msg (g_pkg_name,
3460                                      l_api_name || ': ' || l_api_message
3461                                     );
3462          END IF;
3463 
3464          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
3465                                     p_data       => x_msg_data
3466                                    );
3467       WHEN invalid_txn_type
3468       THEN
3469          ROLLBACK TO create_mmtrecord_pvt;
3470          x_return_status := fnd_api.g_ret_sts_error;
3471          l_api_message :=
3472                'Unexpected transaction type passed in: '
3473             || TO_CHAR (p_txn_type_id);
3474 
3475          IF     g_debug = 'Y'
3476             AND fnd_log.level_error >= fnd_log.g_current_runtime_level
3477          THEN
3478             fnd_log.STRING (fnd_log.level_error,
3479                             g_log_head || l_api_name || '.' || l_stmt_num,
3480                             l_api_message
3481                            );
3482          END IF;
3483 
3484          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error)
3485          THEN
3486             fnd_msg_pub.add_exc_msg (g_pkg_name,
3487                                      l_api_name || ': ' || l_api_message
3488                                     );
3489          END IF;
3490 
3491          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
3492                                     p_data       => x_msg_data
3493                                    );
3494       WHEN NO_DATA_FOUND
3495       THEN
3496          ROLLBACK TO create_mmtrecord_pvt;
3497          x_return_status := fnd_api.g_ret_sts_error;
3498 
3499          IF     g_debug = 'Y'
3500             AND fnd_log.level_error >= fnd_log.g_current_runtime_level
3501          THEN
3502             fnd_log.STRING (fnd_log.level_error,
3503                             g_log_head || '.' || l_api_name || '.'
3504                             || l_stmt_num,
3505                             l_api_message
3506                            );
3507          END IF;
3508 
3509          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error)
3510          THEN
3511             fnd_msg_pub.add_exc_msg (g_pkg_name, l_api_name || l_api_message);
3512          END IF;
3513 
3514          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
3515                                     p_data       => x_msg_data
3516                                    );
3517       WHEN OTHERS
3518       THEN
3519          ROLLBACK TO create_mmtrecord_pvt;
3520          x_return_status := fnd_api.g_ret_sts_unexp_error;
3521          l_api_message :=
3522                         TO_CHAR (SQLCODE) || '- '
3523                         || SUBSTRB (SQLERRM, 1, 100);
3524 
3525          IF     g_debug = 'Y'
3526             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
3527          THEN
3528             fnd_log.STRING (fnd_log.level_unexpected,
3529                             g_log_head || '.' || l_api_name || '.'
3530                             || l_stmt_num,
3531                                'Create_MMTRecord : '
3532                             || l_stmt_num
3533                             || ' : '
3534                             || SUBSTR (SQLERRM, 1, 200)
3535                            );
3536          END IF;
3537 
3538          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error)
3539          THEN
3540             fnd_msg_pub.add_exc_msg (g_pkg_name,
3541                                         l_api_name
3542                                      || '('
3543                                      || TO_CHAR (l_stmt_num)
3544                                      || ') - '
3545                                      || l_api_message
3546                                     );
3547          END IF;
3548 
3549          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
3550                                     p_data       => x_msg_data
3551                                    );
3552    END create_mmtrecord;
3553 
3554 -----------------------------------------------------------------------------
3555 -- Start of comments                                                       --
3556 --                                                                         --
3557 -- PROCEDURE                                                               --
3558 --  Insert_MMT  This API takes a PL/SQL table as input that has one        --
3559 --                    entry for each GRAT event. It loops through the table--
3560 --                    and calls Create_MMTRecord to create logical MMT     --
3561 --                    transactions as appropriate for each event.          --
3562 --                                                                         --
3563 -- VERSION 1.0                                                             --
3564 --                                                                         --
3565 -- PARAMETERS                                                              --
3566 --  P_API_VERSION      API Version # - REQUIRED: enter 1.0                 --
3567 --  P_INIT_MSG_LIST    Initialize message list? True/False                 --
3568 --  P_COMMIT           Should the API commit before returning? True/False  --
3569 --  P_VALIDATION_LEVEL Specify the level of validation on the inputs       --
3570 --  P_rcv_accttxnS_TBL   Collection of transactions of type rcv_accttxn_rec_type     --
3571 --  X_RETURN_STATUS    Success/Error/Unexplained error - 'S','E', or 'U'   --
3572 --  X_MSG_COUNT        Message Count - # of messages placed in message list--
3573 --  X_MSG_DATA         Message Text - returns msg contents if msg_count = 1--
3574 --                                                                         --
3575 -- HISTORY:                                                                --
3576 --    06/26/03     Bryan Kuntz      Created                                --
3577 -- End of comments                                                         --
3578 -----------------------------------------------------------------------------
3579    PROCEDURE insert_mmt (
3580       p_api_version        IN              NUMBER,
3581       p_init_msg_list      IN              VARCHAR2 := fnd_api.g_false,
3582       p_commit             IN              VARCHAR2 := fnd_api.g_false,
3583       p_validation_level   IN              NUMBER
3584             := fnd_api.g_valid_level_full,
3585       x_return_status      OUT NOCOPY      VARCHAR2,
3586       x_msg_count          OUT NOCOPY      NUMBER,
3587       x_msg_data           OUT NOCOPY      VARCHAR2,
3588       p_rcv_accttxn_tbl    IN              gmf_rcv_accounting_pkg.rcv_accttxn_tbl_type
3589    )
3590    IS
3591       l_api_name          CONSTANT VARCHAR2 (30)              := 'Insert_MMT';
3592       l_api_version       CONSTANT NUMBER                              := 1.0;
3593       l_api_message                VARCHAR2 (1000);
3594       l_return_status              VARCHAR2 (1)  := fnd_api.g_ret_sts_success;
3595       l_msg_count                  NUMBER                                := 0;
3596       l_msg_data                   VARCHAR2 (8000)                      := '';
3597       l_stmt_num                   NUMBER                                := 0;
3598       l_ctr                        BINARY_INTEGER;
3599       l_inv_trx_tbl                inv_logical_transaction_global.mtl_trx_tbl_type;
3600       l_inv_trx_tbl_ctr            BINARY_INTEGER;
3601       l_correct_ind                BOOLEAN                           := FALSE;
3602                                       -- indicator variable for whether these
3603       -- transactions are for a correction or not
3604       l_rcv_txn_type               rcv_transactions.transaction_type%TYPE;
3605       l_parent_txn_flag            NUMBER                                := 1;
3606       l_intercompany_price         NUMBER;
3607                         -- may include nr tax depending on the pricing option
3608       l_intercompany_curr_code     gmf_rcv_accounting_txns.currency_code%TYPE;
3609       l_transfer_organization_id   NUMBER                             := NULL;
3610       l_rcv_accttxn                gmf_rcv_accounting_pkg.rcv_accttxn_rec_type;
3611       invalid_event                EXCEPTION;
3612    BEGIN
3613 -- Standard start of API savepoint
3614       SAVEPOINT insert_mmt_pvt;
3615       l_stmt_num := 0;
3616 
3617       IF     g_debug = 'Y'
3618          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
3619       THEN
3620          fnd_log.STRING (fnd_log.level_procedure,
3621                          g_log_head || l_api_name || '.begin',
3622                          'Insert_MMT <<'
3623                         );
3624       END IF;
3625 
3626 -- Standard call to check for call compatibility
3627       IF NOT fnd_api.compatible_api_call (l_api_version,
3628                                           p_api_version,
3629                                           l_api_name,
3630                                           g_pkg_name
3631                                          )
3632       THEN
3633          RAISE fnd_api.g_exc_unexpected_error;
3634       END IF;
3635 
3636 -- Initialize message list if p_init_msg_list is set to TRUE
3637       IF fnd_api.to_boolean (p_init_msg_list)
3638       THEN
3639          fnd_msg_pub.initialize;
3640       END IF;
3641 
3642 -- Initialize API return status to success
3643       x_return_status := fnd_api.g_ret_sts_success;
3644       x_msg_count := 0;
3645       x_msg_data := '';
3646 -- API Body
3647 -- Initialize counters
3648       l_inv_trx_tbl_ctr := 0;
3649       l_ctr := p_rcv_accttxn_tbl.FIRST;
3650       l_stmt_num := 10;
3651 
3652 -- Determine if this group of transactions are for a CORRECT txn type
3653       SELECT transaction_type
3654         INTO l_rcv_txn_type
3655         FROM rcv_transactions
3656        WHERE transaction_id = p_rcv_accttxn_tbl (l_ctr).rcv_transaction_id;
3657 
3658       IF (l_rcv_txn_type = 'CORRECT')
3659       THEN
3660          l_correct_ind := TRUE;
3661       END IF;
3662 
3663 -- Loop for every event in the table
3664       WHILE l_ctr <= p_rcv_accttxn_tbl.LAST
3665       LOOP
3666          -- Logical transactions are only seeded in Receiving but not in Inventory for :
3667          -- 1. Expense destination types for one-time items
3668          -- 2. Shop Floor destination types (for both OSP and direct items).
3669          IF (    p_rcv_accttxn_tbl (l_ctr).destination_type_code <>
3670                                                                   'SHOP FLOOR'
3671              AND (   p_rcv_accttxn_tbl (l_ctr).destination_type_code <>
3672                                                                      'EXPENSE'
3673                   OR p_rcv_accttxn_tbl (l_ctr).item_id IS NOT NULL
3674                  )
3675             )
3676          THEN
3677             IF (p_rcv_accttxn_tbl (l_ctr).ship_to_org_flag = 'N')
3678             THEN
3679                -- For GRAT events, the transfer_organization_id represents the organization from
3680                -- where the transfer price is derived. Hence in the flow :
3681                -- OU2 <-------- OU1 <--------- Supplier
3682                -- The Logical Receive in OU1 will be at PO price and the transfer_org will be NULL.
3683                -- The Recieve in OU2 could be at transfer price between OU1 and OU2. Hence trasnfer
3684                -- org will be OU1.
3685                -- However, in Inventory the Logical Receive in GRAT translates to a Logical PO Receipt
3686                -- and a Logical I/C Sales Issue. The Logical I/C Sales event could be at transfer price.
3687                -- The transfer organization should therefore be picked up from the next event. To keep
3688                -- the values for the Logical PO Receipt and the Logical I/C Sales issue consistent, we
3689                -- will follow this logic for both transactions.
3690                l_transfer_organization_id :=
3691                   p_rcv_accttxn_tbl (p_rcv_accttxn_tbl.NEXT (l_ctr)).organization_id;
3692 
3693                IF (p_rcv_accttxn_tbl (l_ctr).event_type_id = logical_receive
3694                   )
3695                THEN
3696                   l_inv_trx_tbl_ctr := l_inv_trx_tbl_ctr + 1;
3697 
3698                   IF (p_rcv_accttxn_tbl (l_ctr).intercompany_pricing_option =
3699                                                                              2
3700                      )
3701                   THEN
3702                      l_intercompany_price :=
3703                                  p_rcv_accttxn_tbl (l_ctr).intercompany_price;
3704                      l_intercompany_curr_code :=
3705                              p_rcv_accttxn_tbl (l_ctr).intercompany_curr_code;
3706                   ELSE
3707                      l_intercompany_price :=
3708                           p_rcv_accttxn_tbl (l_ctr).unit_price
3709                         + p_rcv_accttxn_tbl (l_ctr).unit_nr_tax;
3710                      l_intercompany_curr_code :=
3711                                        p_rcv_accttxn_tbl (l_ctr).currency_code;
3712                   END IF;
3713 
3714                   IF (l_correct_ind)
3715                   THEN
3716                      l_stmt_num := 20;
3717                      create_mmtrecord
3718                         (p_api_version                   => 1.0,
3719                          p_rcv_accttxn                   => p_rcv_accttxn_tbl
3720                                                                         (l_ctr),
3721                          p_txn_type_id                   => 69,
3722                          p_intercompany_price            => l_intercompany_price,
3723                          p_intercompany_curr_code        => l_intercompany_curr_code,
3724                          p_acct_id                       => p_rcv_accttxn_tbl
3725                                                                         (l_ctr).debit_account_id,
3726                          p_sign                          => SIGN
3727                                                                (p_rcv_accttxn_tbl
3728                                                                         (l_ctr).transaction_quantity
3729                                                                ),
3730                          p_parent_txn_flag               => l_parent_txn_flag,
3731                          p_transfer_organization_id      => l_transfer_organization_id,
3732                          x_return_status                 => l_return_status,
3733                          x_msg_count                     => l_msg_count,
3734                          x_msg_data                      => l_msg_data,
3735                          x_inv_trx                       => l_inv_trx_tbl
3736                                                                (l_inv_trx_tbl_ctr
3737                                                                )
3738                         );
3739                   ELSIF (p_rcv_accttxn_tbl (l_ctr).procurement_org_flag = 'Y'
3740                         )
3741                   THEN
3742                      l_stmt_num := 30;
3743                      create_mmtrecord
3744                         (p_api_version                   => 1.0,
3745                          p_rcv_accttxn                   => p_rcv_accttxn_tbl
3746                                                                         (l_ctr),
3747                          p_txn_type_id                   => 19,
3748                                                          -- Logical PO Receipt
3749                          p_intercompany_price            => l_intercompany_price,
3750                          p_intercompany_curr_code        => l_intercompany_curr_code,
3751                          p_acct_id                       => p_rcv_accttxn_tbl
3752                                                                         (l_ctr).debit_account_id,
3753                          p_sign                          => 1,
3754                          p_parent_txn_flag               => l_parent_txn_flag,
3755                          p_transfer_organization_id      => l_transfer_organization_id,
3756                          x_return_status                 => l_return_status,
3757                          x_msg_count                     => l_msg_count,
3758                          x_msg_data                      => l_msg_data,
3759                          x_inv_trx                       => l_inv_trx_tbl
3760                                                                (l_inv_trx_tbl_ctr
3761                                                                )
3762                         );
3763                   ELSE
3764                      l_stmt_num := 40;
3765                      create_mmtrecord
3766                         (p_api_version                   => 1.0,
3767                          p_rcv_accttxn                   => p_rcv_accttxn_tbl
3768                                                                         (l_ctr),
3769                          p_txn_type_id                   => 22,
3770                                             -- Logical I/C Procurement Receipt
3771                          p_intercompany_price            => l_intercompany_price,
3772                          p_intercompany_curr_code        => l_intercompany_curr_code,
3773                          p_acct_id                       => p_rcv_accttxn_tbl
3774                                                                         (l_ctr).debit_account_id,
3775                          p_sign                          => 1,
3776                          p_parent_txn_flag               => l_parent_txn_flag,
3777                          p_transfer_organization_id      => l_transfer_organization_id,
3778                          x_return_status                 => l_return_status,
3779                          x_msg_count                     => l_msg_count,
3780                          x_msg_data                      => l_msg_data,
3781                          x_inv_trx                       => l_inv_trx_tbl
3782                                                                (l_inv_trx_tbl_ctr
3783                                                                )
3784                         );
3785                   END IF;
3786 
3787                   IF (l_return_status <> fnd_api.g_ret_sts_success)
3788                   THEN
3789                      RAISE fnd_api.g_exc_error;
3790                   END IF;
3791 
3792                   l_stmt_num := 50;
3793                   l_inv_trx_tbl_ctr := l_inv_trx_tbl_ctr + 1;
3794 
3795                   IF (p_rcv_accttxn_tbl (p_rcv_accttxn_tbl.NEXT (l_ctr)).intercompany_pricing_option =
3796                                                                              2
3797                      )
3798                   THEN
3799                      l_intercompany_price :=
3800                         p_rcv_accttxn_tbl (p_rcv_accttxn_tbl.NEXT (l_ctr)).intercompany_price;
3801                      l_intercompany_curr_code :=
3802                         p_rcv_accttxn_tbl (p_rcv_accttxn_tbl.NEXT (l_ctr)).intercompany_curr_code;
3803                   ELSE
3804                      l_intercompany_price :=
3805                           p_rcv_accttxn_tbl (p_rcv_accttxn_tbl.NEXT (l_ctr)).unit_price
3806                         + p_rcv_accttxn_tbl (p_rcv_accttxn_tbl.NEXT (l_ctr)).unit_nr_tax;
3807                      l_intercompany_curr_code :=
3808                         p_rcv_accttxn_tbl (p_rcv_accttxn_tbl.NEXT (l_ctr)).currency_code;
3809                   END IF;
3810 
3811                   IF (p_rcv_accttxn_tbl (l_ctr).transaction_quantity > 0)
3812                   THEN
3813                      l_stmt_num := 60;
3814                      create_mmtrecord
3815                         (p_api_version                   => 1.0,
3816                          p_rcv_accttxn                   => p_rcv_accttxn_tbl
3817                                                                         (l_ctr),
3818                          p_txn_type_id                   => 11,
3819                                                     -- Logical I/C Sales Issue
3820                          p_intercompany_price            => l_intercompany_price,
3821                          p_intercompany_curr_code        => l_intercompany_curr_code,
3822                          p_acct_id                       => p_rcv_accttxn_tbl
3823                                                                         (l_ctr).intercompany_cogs_account_id,
3824                          p_sign                          => -1,
3825                          p_parent_txn_flag               => 0,
3826                          p_transfer_organization_id      => l_transfer_organization_id,
3827                          x_return_status                 => l_return_status,
3828                          x_msg_count                     => l_msg_count,
3829                          x_msg_data                      => l_msg_data,
3830                          x_inv_trx                       => l_inv_trx_tbl
3831                                                                (l_inv_trx_tbl_ctr
3832                                                                )
3833                         );
3834                   ELSE
3835                      l_stmt_num := 70;
3836                      l_rcv_accttxn := p_rcv_accttxn_tbl (l_ctr);
3837                      create_mmtrecord
3838                         (p_api_version                   => 1.0,
3839                          p_rcv_accttxn                   => p_rcv_accttxn_tbl
3840                                                                         (l_ctr),
3841                          p_txn_type_id                   => 14,
3842                                                    -- Logical I/C Sales Return
3843                          p_intercompany_price            => l_intercompany_price,
3844                          p_intercompany_curr_code        => l_intercompany_curr_code,
3845                          p_acct_id                       => p_rcv_accttxn_tbl
3846                                                                         (l_ctr).intercompany_cogs_account_id,
3847                          p_sign                          => 1,
3848                          p_parent_txn_flag               => 0,
3849                          p_transfer_organization_id      => l_transfer_organization_id,
3850                          x_return_status                 => l_return_status,
3851                          x_msg_count                     => l_msg_count,
3852                          x_msg_data                      => l_msg_data,
3853                          x_inv_trx                       => l_inv_trx_tbl
3854                                                                (l_inv_trx_tbl_ctr
3855                                                                )
3856                         );
3857                   END IF;
3858 
3859                   IF (l_return_status <> fnd_api.g_ret_sts_success)
3860                   THEN
3861                      RAISE fnd_api.g_exc_error;
3862                   END IF;
3863                ELSIF (p_rcv_accttxn_tbl (l_ctr).event_type_id =
3864                                                       logical_return_to_vendor
3865                      )
3866                THEN
3867                   l_stmt_num := 80;
3868                   l_inv_trx_tbl_ctr := l_inv_trx_tbl_ctr + 1;
3869 
3870                   IF (p_rcv_accttxn_tbl (l_ctr).intercompany_pricing_option =
3871                                                                              2
3872                      )
3873                   THEN
3874                      l_intercompany_price :=
3875                                          p_rcv_accttxn_tbl (l_ctr).unit_price;
3876                      l_intercompany_curr_code :=
3877                              p_rcv_accttxn_tbl (l_ctr).intercompany_curr_code;
3878                   ELSE
3879                      l_intercompany_price :=
3880                           p_rcv_accttxn_tbl (l_ctr).unit_price
3881                         + p_rcv_accttxn_tbl (l_ctr).unit_nr_tax;
3882                      l_intercompany_curr_code :=
3883                                        p_rcv_accttxn_tbl (l_ctr).currency_code;
3884                   END IF;
3885 
3886                   IF (l_correct_ind)
3887                   THEN
3888                      l_stmt_num := 90;
3889                      l_rcv_accttxn := p_rcv_accttxn_tbl (l_ctr);
3890                      create_mmtrecord
3891                         (p_api_version                   => 1.0,
3892                          p_rcv_accttxn                   => p_rcv_accttxn_tbl
3893                                                                         (l_ctr),
3894                          p_txn_type_id                   => 69,
3895                          p_intercompany_price            => l_intercompany_price,
3896                          p_intercompany_curr_code        => l_intercompany_curr_code,
3897                          p_acct_id                       => p_rcv_accttxn_tbl
3898                                                                         (l_ctr).credit_account_id,
3899                          p_sign                          =>   -1
3900                                                             * SIGN
3901                                                                  (p_rcv_accttxn_tbl
3902                                                                         (l_ctr).transaction_quantity
3903                                                                  ),
3904                          p_parent_txn_flag               => l_parent_txn_flag,
3905                          p_transfer_organization_id      => l_transfer_organization_id,
3906                          x_return_status                 => l_return_status,
3907                          x_msg_count                     => l_msg_count,
3908                          x_msg_data                      => l_msg_data,
3909                          x_inv_trx                       => l_inv_trx_tbl
3910                                                                (l_inv_trx_tbl_ctr
3911                                                                )
3912                         );
3913                   ELSIF (p_rcv_accttxn_tbl (l_ctr).procurement_org_flag = 'Y'
3914                         )
3915                   THEN
3916                      l_stmt_num := 100;
3917                      l_rcv_accttxn := p_rcv_accttxn_tbl (l_ctr);
3918                      create_mmtrecord
3919                         (p_api_version                   => 1.0,
3920                          p_rcv_accttxn                   => p_rcv_accttxn_tbl
3921                                                                         (l_ctr),
3922                          p_txn_type_id                   => 39, -- Logical RTV
3923                          p_intercompany_price            => l_intercompany_price,
3924                          p_intercompany_curr_code        => l_intercompany_curr_code,
3925                          p_acct_id                       => p_rcv_accttxn_tbl
3926                                                                         (l_ctr).credit_account_id,
3927                          p_sign                          => -1,
3928                          p_parent_txn_flag               => l_parent_txn_flag,
3929                          p_transfer_organization_id      => l_transfer_organization_id,
3930                          x_return_status                 => l_return_status,
3931                          x_msg_count                     => l_msg_count,
3932                          x_msg_data                      => l_msg_data,
3933                          x_inv_trx                       => l_inv_trx_tbl
3934                                                                (l_inv_trx_tbl_ctr
3935                                                                )
3936                         );
3937                   ELSE
3938                      l_stmt_num := 110;
3939                      l_rcv_accttxn := p_rcv_accttxn_tbl (l_ctr);
3940                      create_mmtrecord
3941                         (p_api_version                   => 1.0,
3942                          p_rcv_accttxn                   => p_rcv_accttxn_tbl
3943                                                                         (l_ctr),
3944                          p_txn_type_id                   => 23,
3945                                              -- Logical I/C Procurement Return
3946                          p_intercompany_price            => l_intercompany_price,
3947                          p_intercompany_curr_code        => l_intercompany_curr_code,
3948                          p_acct_id                       => p_rcv_accttxn_tbl
3949                                                                         (l_ctr).credit_account_id,
3950                          p_sign                          => -1,
3951                          p_parent_txn_flag               => l_parent_txn_flag,
3952                          p_transfer_organization_id      => l_transfer_organization_id,
3953                          x_return_status                 => l_return_status,
3954                          x_msg_count                     => l_msg_count,
3955                          x_msg_data                      => l_msg_data,
3956                          x_inv_trx                       => l_inv_trx_tbl
3957                                                                (l_inv_trx_tbl_ctr
3958                                                                )
3959                         );
3960                   END IF;
3961 
3962                   IF (l_return_status <> fnd_api.g_ret_sts_success)
3963                   THEN
3964                      RAISE fnd_api.g_exc_error;
3965                   END IF;
3966 
3967                   l_stmt_num := 120;
3968                   l_inv_trx_tbl_ctr := l_inv_trx_tbl_ctr + 1;
3969 
3970                   IF (p_rcv_accttxn_tbl (p_rcv_accttxn_tbl.NEXT (l_ctr)).intercompany_pricing_option =
3971                                                                              2
3972                      )
3973                   THEN
3974                      l_intercompany_price :=
3975                         p_rcv_accttxn_tbl (p_rcv_accttxn_tbl.NEXT (l_ctr)).unit_price;
3976                      l_intercompany_curr_code :=
3977                         p_rcv_accttxn_tbl (p_rcv_accttxn_tbl.NEXT (l_ctr)).intercompany_curr_code;
3978                   ELSE
3979                      l_intercompany_price :=
3980                           p_rcv_accttxn_tbl (p_rcv_accttxn_tbl.NEXT (l_ctr)).unit_price
3981                         + p_rcv_accttxn_tbl (p_rcv_accttxn_tbl.NEXT (l_ctr)).unit_nr_tax;
3982                      l_intercompany_curr_code :=
3983                         p_rcv_accttxn_tbl (p_rcv_accttxn_tbl.NEXT (l_ctr)).currency_code;
3984                   END IF;
3985 
3986                   IF (p_rcv_accttxn_tbl (l_ctr).transaction_quantity > 0)
3987                   THEN
3988                      l_stmt_num := 130;
3989                      l_rcv_accttxn := p_rcv_accttxn_tbl (l_ctr);
3990                      create_mmtrecord
3991                         (p_api_version                   => 1.0,
3992                          p_rcv_accttxn                   => p_rcv_accttxn_tbl
3993                                                                         (l_ctr),
3994                          p_txn_type_id                   => 14,
3995                                                    -- Logical I/C Sales Return
3996                          p_intercompany_price            => l_intercompany_price,
3997                          p_intercompany_curr_code        => l_intercompany_curr_code,
3998                          p_acct_id                       => p_rcv_accttxn_tbl
3999                                                                         (l_ctr).intercompany_cogs_account_id,
4000                          p_sign                          => 1,
4001                          p_parent_txn_flag               => 0,
4002                          p_transfer_organization_id      => l_transfer_organization_id,
4003                          x_return_status                 => l_return_status,
4004                          x_msg_count                     => l_msg_count,
4005                          x_msg_data                      => l_msg_data,
4006                          x_inv_trx                       => l_inv_trx_tbl
4007                                                                (l_inv_trx_tbl_ctr
4008                                                                )
4009                         );
4010                   ELSE
4011                      l_stmt_num := 140;
4012                      l_rcv_accttxn := p_rcv_accttxn_tbl (l_ctr);
4013                      create_mmtrecord
4014                         (p_api_version                   => 1.0,
4015                          p_rcv_accttxn                   => p_rcv_accttxn_tbl
4016                                                                         (l_ctr),
4017                          p_txn_type_id                   => 11,
4018                                                     -- Logical I/C Sales Issue
4019                          p_intercompany_price            => l_intercompany_price,
4020                          p_intercompany_curr_code        => l_intercompany_curr_code,
4021                          p_acct_id                       => p_rcv_accttxn_tbl
4022                                                                         (l_ctr).intercompany_cogs_account_id,
4023                          p_sign                          => -1,
4024                          p_parent_txn_flag               => 0,
4025                          p_transfer_organization_id      => l_transfer_organization_id,
4026                          x_return_status                 => l_return_status,
4027                          x_msg_count                     => l_msg_count,
4028                          x_msg_data                      => l_msg_data,
4029                          x_inv_trx                       => l_inv_trx_tbl
4030                                                                (l_inv_trx_tbl_ctr
4031                                                                )
4032                         );
4033                   END IF;
4034 
4035                   IF (l_return_status <> fnd_api.g_ret_sts_success)
4036                   THEN
4037                      RAISE fnd_api.g_exc_error;
4038                   END IF;
4039                ELSE
4040                   RAISE invalid_event;
4041                -- catch error: should never get anything but Log rcpt or Log RTV
4042                END IF;
4043 
4044                l_parent_txn_flag := 0;
4045               -- the first transaction inserted will be the parent, all others
4046               -- will be children so their flags are 0
4047             END IF;
4048          END IF;
4049 
4050          l_ctr := p_rcv_accttxn_tbl.NEXT (l_ctr);
4051       END LOOP;
4052 
4053       IF     g_debug = 'Y'
4054          AND fnd_log.level_event >= fnd_log.g_current_runtime_level
4055       THEN
4056          fnd_log.STRING (fnd_log.level_event,
4057                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
4058                          'Creating Logical Transactions in MMT'
4059                         );
4060       END IF;
4061 
4062       l_stmt_num := 150;
4063       inv_logical_transactions_pub.create_logical_transactions
4064          (x_return_status                   => l_return_status,
4065           x_msg_count                       => l_msg_count,
4066           x_msg_data                        => l_msg_data,
4067           p_api_version_number              => 1.0,
4068           p_mtl_trx_tbl                     => l_inv_trx_tbl,
4069           p_trx_flow_header_id              => p_rcv_accttxn_tbl
4070                                                       (p_rcv_accttxn_tbl.FIRST).trx_flow_header_id,
4071           p_defer_logical_transactions      => 2,
4072           p_logical_trx_type_code           => 3,
4073           p_exploded_flag                   => 1
4074          );
4075 
4076       IF (l_return_status <> fnd_api.g_ret_sts_success)
4077       THEN
4078          RAISE fnd_api.g_exc_error;
4079       END IF;
4080 
4081 -- End API Body
4082 
4083       -- Standard check of p_commit
4084       IF fnd_api.to_boolean (p_commit)
4085       THEN
4086          COMMIT WORK;
4087       END IF;
4088 
4089       IF     g_debug = 'Y'
4090          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
4091       THEN
4092          fnd_log.STRING (fnd_log.level_procedure,
4093                          g_log_head || l_api_name || '.end',
4094                          'Insert_MMT >>'
4095                         );
4096       END IF;
4097    EXCEPTION
4098       WHEN invalid_event
4099       THEN
4100          ROLLBACK TO insert_mmt_pvt;
4101          x_return_status := fnd_api.g_ret_sts_unexp_error;
4102          l_api_message :=
4103                'Unexpected event in element '
4104             || TO_CHAR (l_ctr)
4105             || ' of input parameter p_rcv_accttxn_tbl';
4106 
4107          IF     g_debug = 'Y'
4108             AND fnd_log.level_error >= fnd_log.g_current_runtime_level
4109          THEN
4110             fnd_log.STRING (fnd_log.level_error,
4111                             g_log_head || l_api_name || '.' || l_stmt_num,
4112                             l_api_message
4113                            );
4114          END IF;
4115 
4116          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
4117          THEN
4118             fnd_msg_pub.add_exc_msg (g_pkg_name,
4119                                      l_api_name || ': ' || l_api_message
4120                                     );
4121          END IF;
4122 
4123          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
4124                                     p_data       => x_msg_data
4125                                    );
4126       WHEN fnd_api.g_exc_error
4127       THEN
4128          ROLLBACK TO insert_mmt_pvt;
4129          x_return_status := fnd_api.g_ret_sts_error;
4130          l_api_message := 'Call to procedure failed';
4131 
4132          IF     g_debug = 'Y'
4133             AND fnd_log.level_error >= fnd_log.g_current_runtime_level
4134          THEN
4135             fnd_log.STRING (fnd_log.level_error,
4136                             g_log_head || l_api_name || '.' || l_stmt_num,
4137                             l_api_message
4138                            );
4139          END IF;
4140 
4141          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
4142                                     p_data       => x_msg_data
4143                                    );
4144       WHEN fnd_api.g_exc_unexpected_error
4145       THEN
4146          ROLLBACK TO insert_mmt_pvt;
4147          x_return_status := fnd_api.g_ret_sts_unexp_error;
4148          l_api_message :=
4149              'Wrong version #, expecting version ' || TO_CHAR (l_api_version);
4150 
4151          IF     g_debug = 'Y'
4152             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
4153          THEN
4154             fnd_log.STRING (fnd_log.level_unexpected,
4155                             g_log_head || l_api_name || '.' || l_stmt_num,
4156                             l_api_message
4157                            );
4158          END IF;
4159 
4160          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
4161                                     p_data       => x_msg_data
4162                                    );
4163       WHEN OTHERS
4164       THEN
4165          ROLLBACK TO insert_mmt_pvt;
4166          x_return_status := fnd_api.g_ret_sts_unexp_error;
4167          l_api_message :=
4168                'Unexpected Error: '
4169             || l_stmt_num
4170             || ': '
4171             || TO_CHAR (SQLCODE)
4172             || '- '
4173             || SUBSTRB (SQLERRM, 1, 100);
4174 
4175          IF     g_debug = 'Y'
4176             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
4177          THEN
4178             fnd_log.STRING (fnd_log.level_unexpected,
4179                             g_log_head || l_api_name || '.' || l_stmt_num,
4180                             l_api_message
4181                            );
4182          END IF;
4183 
4184          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
4185          THEN
4186             fnd_msg_pub.add_exc_msg (g_pkg_name,
4187                                      l_api_name || ': ' || l_api_message
4188                                     );
4189          END IF;
4190 
4191          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
4192                                     p_data       => x_msg_data
4193                                    );
4194    END insert_mmt;
4195 
4196 /** =========================================
4197 * Adapted from Seed_RAEEvents
4198 ** ========================================== **/
4199    PROCEDURE insert_txn (
4200       p_api_version                    IN              NUMBER := 1.0,
4201       p_init_msg_list                  IN              VARCHAR2
4202             := fnd_api.g_false,
4203       p_commit                         IN              VARCHAR2
4204             := fnd_api.g_false,
4205       p_validation_level               IN              NUMBER
4206             := fnd_api.g_valid_level_full,
4207       x_return_status                  OUT NOCOPY      VARCHAR2,
4208       x_msg_count                      OUT NOCOPY      NUMBER,
4209       x_msg_data                       OUT NOCOPY      VARCHAR2,
4210       p_event_source                   IN              VARCHAR2,
4211       p_event_type_id                  IN              NUMBER,
4212       p_rcv_transaction_id             IN              NUMBER,
4213       p_inv_distribution_id            IN              NUMBER,
4214       p_po_distribution_id             IN              NUMBER,
4215       p_direct_delivery_flag           IN              VARCHAR2,
4216       p_gl_group_id                    IN              NUMBER,
4217       p_cross_ou_flag                  IN              VARCHAR2,
4218       p_procurement_org_flag           IN              VARCHAR2,
4219       p_ship_to_org_flag               IN              VARCHAR2,
4220       p_drop_ship_flag                 IN              NUMBER,
4221       p_org_id                         IN              NUMBER,
4222       p_organization_id                IN              NUMBER,
4223       p_transfer_org_id                IN              NUMBER,
4224       p_transfer_organization_id       IN              NUMBER,
4225       p_trx_flow_header_id             IN              NUMBER,
4226       p_transaction_forward_flow_rec                   inv_transaction_flow_pub.mtl_transaction_flow_rec_type,
4227       p_transaction_reverse_flow_rec                   inv_transaction_flow_pub.mtl_transaction_flow_rec_type,
4228       p_unit_price                     IN              NUMBER,
4229       p_prior_unit_price               IN              NUMBER,
4230       x_rcv_accttxn                    OUT NOCOPY      gmf_rcv_accounting_pkg.rcv_accttxn_rec_type
4231    )
4232    IS
4233       c_log_module        CONSTANT VARCHAR2 (30)              := 'Insert_Txn';
4234       l_api_version       CONSTANT NUMBER                              := 1.0;
4235       l_return_status              VARCHAR2 (1)  := fnd_api.g_ret_sts_success;
4236       l_msg_count                  NUMBER                                := 0;
4237       l_msg_data                   VARCHAR2 (8000)                      := '';
4238       l_stmt_num                   NUMBER                                := 0;
4239       l_api_message                VARCHAR2 (1000);
4240       l_api_name          CONSTANT VARCHAR2 (30)              := 'Insert_Txn';
4241       l_rcv_accttxn                gmf_rcv_accounting_pkg.rcv_accttxn_rec_type;
4242       l_transaction_amount         NUMBER                                := 0;
4243       l_source_doc_quantity        NUMBER                                := 0;
4244       l_transaction_quantity       NUMBER                                := 0;
4245       l_ic_pricing_option          NUMBER                                := 1;
4246       l_unit_price                 NUMBER                                := 0;
4247       l_unit_nr_tax                NUMBER                                := 0;
4248       l_unit_rec_tax               NUMBER                                := 0;
4249       l_prior_nr_tax               NUMBER                                := 0;
4250       l_prior_rec_tax              NUMBER                                := 0;
4251       l_currency_code              VARCHAR2 (15);
4252       l_currency_conversion_rate   NUMBER;
4253       l_currency_conversion_date   DATE;
4254       l_currency_conversion_type   VARCHAR2 (30);
4255       l_incr_transfer_price        NUMBER                                := 0;
4256       l_incr_currency_code         VARCHAR2 (15)                      := NULL;
4257       l_dest_org_id                NUMBER;
4258       l_trx_uom_code               mtl_units_of_measure.uom_code%TYPE;
4259       l_primary_uom                mtl_units_of_measure.unit_of_measure%TYPE;
4260       l_primary_qty                NUMBER;
4261       l_credit_acct_id             NUMBER;
4262       l_debit_acct_id              NUMBER;
4263       l_ic_cogs_acct_id            NUMBER;
4264       l_ussgl_tc                   VARCHAR2 (30);
4265       l_asset_option               NUMBER;
4266       l_expense_option             NUMBER;
4267       l_detail_accounting_flag     VARCHAR2 (1);
4268       l_gl_installed               BOOLEAN                           := FALSE;
4269       l_status                     VARCHAR2 (1);
4270       l_industry                   VARCHAR2 (1);
4271       l_oracle_schema              VARCHAR2 (30);
4272       l_encumbrance_flag           VARCHAR2 (1);
4273       l_ussgl_option               VARCHAR2 (1);
4274    BEGIN
4275       -- Standard start of API savepoint
4276       SAVEPOINT insert_txn_pvt;
4277       l_stmt_num := 0;
4278 
4279       IF     g_debug = 'Y'
4280          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
4281       THEN
4282          fnd_log.STRING (fnd_log.level_procedure,
4283                          g_log_head || '.' || l_api_name || '.begin',
4284                          'Insert_Txn <<'
4285                         );
4286       END IF;
4287 
4288       -- Standard call to check for call compatibility
4289       IF NOT fnd_api.compatible_api_call (l_api_version,
4290                                           p_api_version,
4291                                           l_api_name,
4292                                           g_pkg_name
4293                                          )
4294       THEN
4295          RAISE fnd_api.g_exc_unexpected_error;
4296       END IF;
4297 
4298       -- Initialize message list if p_init_msg_list is set to TRUE
4299       IF fnd_api.to_boolean (p_init_msg_list)
4300       THEN
4301          fnd_msg_pub.initialize;
4302       END IF;
4303 
4304       -- Initialize API return status to success
4305       x_return_status := fnd_api.g_ret_sts_success;
4306 
4307       IF     g_debug = 'Y'
4308          AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
4309       THEN
4310          l_api_message :=
4311                'Insert_Txn : PARAMETERS 1:'
4312             || ' p_event_source : '
4313             || p_event_source
4314             || ' p_event_type_id : '
4315             || p_event_type_id
4316             || ' p_rcv_transaction_id : '
4317             || p_rcv_transaction_id
4318             || ' p_inv_distribution_id : '
4319             || p_inv_distribution_id
4320             || ' p_po_distribution_id : '
4321             || p_po_distribution_id
4322             || ' p_direct_delivery_flag : '
4323             || p_direct_delivery_flag
4324             || ' p_gl_group_id : '
4325             || p_gl_group_id
4326             || ' p_cross_ou_flag : '
4327             || p_cross_ou_flag;
4328          fnd_log.STRING (fnd_log.level_statement,
4329                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
4330                          l_api_message
4331                         );
4332          l_api_message :=
4333                'Insert_Txn : PARAMETERS 2:'
4334             || ' p_procurement_org_flag : '
4335             || p_procurement_org_flag
4336             || ' p_ship_to_org_flag : '
4337             || p_ship_to_org_flag
4338             || ' p_drop_ship_flag : '
4339             || p_drop_ship_flag
4340             || ' p_org_id : '
4341             || p_org_id
4342             || ' p_organization_id : '
4343             || p_organization_id
4344             || ' p_transfer_org_id : '
4345             || p_transfer_org_id
4346             || ' p_transfer_organization_id : '
4347             || p_transfer_organization_id
4348             || ' p_trx_flow_header_id : '
4349             || p_trx_flow_header_id
4350             || ' p_unit_price : '
4351             || p_unit_price
4352             || ' p_prior_unit_price : '
4353             || p_prior_unit_price;
4354          fnd_log.STRING (fnd_log.level_statement,
4355                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
4356                          l_api_message
4357                         );
4358       END IF;
4359 
4360       l_stmt_num := 15;
4361       l_rcv_accttxn.event_source := p_event_source;
4362       l_rcv_accttxn.event_type_id := p_event_type_id;
4363       l_rcv_accttxn.rcv_transaction_id := p_rcv_transaction_id;
4364       l_rcv_accttxn.cross_ou_flag := p_cross_ou_flag;
4365       l_rcv_accttxn.procurement_org_flag := p_procurement_org_flag;
4366       l_rcv_accttxn.ship_to_org_flag := p_ship_to_org_flag;
4367       l_rcv_accttxn.drop_ship_flag := p_drop_ship_flag;
4368       l_rcv_accttxn.po_distribution_id := p_po_distribution_id;
4369       l_rcv_accttxn.direct_delivery_flag := p_direct_delivery_flag;
4370 
4371       -- Initialize PO Information
4372       IF (p_event_source = 'INVOICEMATCH')
4373       THEN
4374          -- This source is only for period end accruals, one-time items
4375          IF     g_debug = 'Y'
4376             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
4377          THEN
4378             fnd_log.STRING (fnd_log.level_statement,
4379                             g_log_head || '.' || l_api_name || '.'
4380                             || l_stmt_num,
4381                             'Getting PO information from Invoice'
4382                            );
4383          END IF;
4384 
4385          l_stmt_num := 20;
4386 
4387          SELECT pod.po_header_id, pol.po_line_id,
4388                 pod.po_distribution_id,
4389                 pod.destination_type_code,
4390                 poll.line_location_id,
4391                 SYSDATE, pol.item_id,
4392                 apid.quantity_invoiced,
4393                 poll.unit_meas_lookup_code, poh.currency_code
4394            INTO l_rcv_accttxn.po_header_id, l_rcv_accttxn.po_line_id,
4395                 l_rcv_accttxn.po_distribution_id,
4396                 l_rcv_accttxn.destination_type_code,
4397                 l_rcv_accttxn.po_line_location_id,
4398                 l_rcv_accttxn.transaction_date, l_rcv_accttxn.item_id,
4399                 l_rcv_accttxn.source_doc_quantity,
4400                 l_rcv_accttxn.source_doc_uom, l_rcv_accttxn.currency_code
4401            FROM ap_invoice_distributions apid,
4402                 po_distributions pod,
4403                 po_line_locations poll,
4404                 po_lines pol,
4405                 po_headers poh
4406           WHERE apid.invoice_distribution_id = p_inv_distribution_id
4407             AND pod.po_distribution_id = apid.po_distribution_id
4408             AND pod.line_location_id = poll.line_location_id
4409             AND pol.po_line_id = poll.po_line_id
4410             AND poh.po_header_id = pod.po_header_id;
4411 
4412          l_rcv_accttxn.inv_distribution_id := p_inv_distribution_id;
4413          l_rcv_accttxn.transaction_quantity :=
4414                                              l_rcv_accttxn.source_doc_quantity;
4415          l_rcv_accttxn.transaction_uom := l_rcv_accttxn.source_doc_uom;
4416       ELSE
4417          l_stmt_num := 30;
4418 
4419          IF     g_debug = 'Y'
4420             AND fnd_log.level_event >= fnd_log.g_current_runtime_level
4421          THEN
4422             fnd_log.STRING
4423                          (fnd_log.level_event,
4424                           g_log_head || '.' || l_api_name || '.' || l_stmt_num,
4425                           'Getting PO information from Receiving Transaction'
4426                          );
4427          END IF;
4428 
4429          SELECT rt.po_header_id, rt.po_line_id,
4430                 rt.po_line_location_id,
4431                 rt.transaction_date, pol.item_id,
4432                 poll.ship_to_organization_id, rt.unit_of_measure,
4433                 rt.source_doc_unit_of_measure, poh.currency_code,
4434                 pod.destination_type_code,
4435                 /* start LCM-OPM Integration  */
4436                 rt.unit_landed_cost
4437                 /* end LCM-OPM Integration  */
4438            INTO l_rcv_accttxn.po_header_id, l_rcv_accttxn.po_line_id,
4439                 l_rcv_accttxn.po_line_location_id,
4440                 l_rcv_accttxn.transaction_date, l_rcv_accttxn.item_id,
4441                 l_dest_org_id, l_rcv_accttxn.transaction_uom,
4442                 l_rcv_accttxn.source_doc_uom, l_rcv_accttxn.currency_code,
4443                 l_rcv_accttxn.destination_type_code,
4444                 /* start LCM-OPM Integration  */
4445                 l_rcv_accttxn.unit_landed_cost
4446                 /* end LCM-OPM Integration  */
4447            FROM rcv_transactions rt,
4448                 po_lines pol,
4449                 po_line_locations poll,
4450                 po_headers poh,
4451                 po_distributions pod
4452           WHERE rt.transaction_id = p_rcv_transaction_id
4453             AND poh.po_header_id = rt.po_header_id
4454             AND pol.po_line_id = rt.po_line_id
4455             AND poll.line_location_id = rt.po_line_location_id
4456             AND pod.po_distribution_id = p_po_distribution_id;
4457       END IF;
4458 
4459       -- p_transaction_forward_flow_rec represents the transaction flow record where the
4460       -- start_org_id is the org_id where the event is being seeded.
4461       -- p_transaction_reverse_flow_rec represents the transaction flow record where the
4462       -- end_org_id is the org_id where the event is being seeded.
4463       -- We need both because some information is derived based on the forward flow and
4464       -- some based on the reverse flow :
4465       -- transfer_price : based on reverse flow
4466       -- I/C accrual : based on reverse flow
4467       -- I/C cogs : based on forward flow. (used in creating transactions in inventory.
4468       -- The transactions will be seeded such that the transfer_org_id will represent the reverse
4469       -- flow.
4470       l_stmt_num := 40;
4471       l_rcv_accttxn.org_id := p_org_id;
4472       l_rcv_accttxn.transfer_org_id := p_transfer_org_id;
4473       l_rcv_accttxn.organization_id := p_organization_id;
4474       l_rcv_accttxn.transfer_organization_id := p_transfer_organization_id;
4475       l_rcv_accttxn.trx_flow_header_id := p_trx_flow_header_id;
4476       -- Get the Set Of Books Identifier
4477       l_stmt_num := 50;
4478 
4479       SELECT set_of_books_id
4480         INTO l_rcv_accttxn.ledger_id
4481         FROM cst_organization_definitions cod
4482        WHERE organization_id = p_organization_id;
4483 
4484       -- Initialize transaction date
4485       IF (p_event_type_id IN
4486              (adjust_receive,
4487               adjust_deliver,
4488               intercompany_invoice,
4489               intercompany_reversal
4490              )
4491          )
4492       THEN
4493          l_rcv_accttxn.transaction_date := SYSDATE;
4494       END IF;
4495 
4496       -- Encumbrance cannot be enabled for global procurement scenarios.
4497       IF (l_rcv_accttxn.trx_flow_header_id IS NULL)
4498       THEN
4499          -- If GL is installed, and either encumbrance is enabled or USSGL profile is enabled,
4500          -- journal import is called by the receiving TM. The group_id passed by receiving should
4501          -- be stamped on the event in this scenario.
4502          l_stmt_num := 60;
4503          l_gl_installed :=
4504             fnd_installation.get_app_info ('SQLGL',
4505                                            l_status,
4506                                            l_industry,
4507                                            l_oracle_schema
4508                                           );
4509 
4510          IF (l_status = 'I')
4511          THEN
4512             l_stmt_num := 70;
4513 
4514             IF     g_debug = 'Y'
4515                AND fnd_log.level_event >= fnd_log.g_current_runtime_level
4516             THEN
4517                fnd_log.STRING (fnd_log.level_event,
4518                                   g_log_head
4519                                || '.'
4520                                || l_api_name
4521                                || '.'
4522                                || l_stmt_num,
4523                                'Checking if encumbrance is enabled.'
4524                               );
4525             END IF;
4526 
4527             check_encumbranceflag (p_api_version           => 1.0,
4528                                    x_return_status         => l_return_status,
4529                                    x_msg_count             => l_msg_count,
4530                                    x_msg_data              => l_msg_data,
4531                                    p_rcv_ledger_id         => l_rcv_accttxn.ledger_id,
4532                                    x_encumbrance_flag      => l_encumbrance_flag,
4533                                    x_ussgl_option          => l_ussgl_option
4534                                   );
4535 
4536             IF l_return_status <> fnd_api.g_ret_sts_success
4537             THEN
4538                l_api_message := 'Error in checking for encumbrance flag ';
4539 
4540                IF     g_debug = 'Y'
4541                   AND fnd_log.level_unexpected >=
4542                                                fnd_log.g_current_runtime_level
4543                THEN
4544                   fnd_log.STRING (fnd_log.level_unexpected,
4545                                   g_log_head || '.' || l_api_name
4546                                   || l_stmt_num,
4547                                      'Insert_Txn : '
4548                                   || l_stmt_num
4549                                   || ' : '
4550                                   || l_api_message
4551                                  );
4552                END IF;
4553 
4554                RAISE fnd_api.g_exc_unexpected_error;
4555             END IF;
4556 
4557             IF (l_encumbrance_flag = 'Y' OR l_ussgl_option = 'Y')
4558             THEN
4559                l_rcv_accttxn.gl_group_id := p_gl_group_id;
4560             END IF;
4561          END IF;
4562       END IF;
4563 
4564       l_stmt_num := 80;
4565 
4566       -- Check if event is for a service line type
4567       SELECT DECODE (poll.matching_basis, 'AMOUNT', 'Y', 'N')
4568         INTO l_rcv_accttxn.service_flag
4569         FROM po_line_locations poll
4570        WHERE poll.line_location_id = l_rcv_accttxn.po_line_location_id;
4571 
4572       l_stmt_num := 90;
4573 
4574       -- Initialize Unit Price
4575       IF (p_event_type_id IN (adjust_receive, adjust_deliver))
4576       THEN
4577          l_rcv_accttxn.unit_price := p_unit_price;
4578          l_rcv_accttxn.prior_unit_price := p_prior_unit_price;
4579       ELSIF l_rcv_accttxn.service_flag = 'Y'
4580       THEN
4581          l_stmt_num := 100;
4582 
4583          IF     g_debug = 'Y'
4584             AND fnd_log.level_event >= fnd_log.g_current_runtime_level
4585          THEN
4586             fnd_log.STRING (fnd_log.level_event,
4587                             g_log_head || '.' || l_api_name || '.'
4588                             || l_stmt_num,
4589                             'Service line type : Getting Transaction Amount'
4590                            );
4591          END IF;
4592 
4593          get_transactionamount (p_api_version             => l_api_version,
4594                                 x_return_status           => l_return_status,
4595                                 x_msg_count               => l_msg_count,
4596                                 x_msg_data                => l_msg_data,
4597                                 p_rcv_accttxn             => l_rcv_accttxn,
4598                                 x_transaction_amount      => l_transaction_amount
4599                                );
4600 
4601          IF l_return_status <> fnd_api.g_ret_sts_success
4602          THEN
4603             l_api_message := 'Error getting transaction amount';
4604 
4605             IF     g_debug = 'Y'
4606                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
4607             THEN
4608                fnd_log.STRING (fnd_log.level_unexpected,
4609                                g_log_head || '.' || l_api_name || l_stmt_num,
4610                                   'Insert_Txn : '
4611                                || l_stmt_num
4612                                || ' : '
4613                                || l_api_message
4614                               );
4615             END IF;
4616 
4617             RAISE fnd_api.g_exc_unexpected_error;
4618          END IF;
4619 
4620          l_rcv_accttxn.transaction_amount := l_transaction_amount;
4621       ELSE
4622          l_stmt_num := 110;
4623 
4624          IF     g_debug = 'Y'
4625             AND fnd_log.level_event >= fnd_log.g_current_runtime_level
4626          THEN
4627             fnd_log.STRING (fnd_log.level_event,
4628                             g_log_head || '.' || l_api_name || '.'
4629                             || l_stmt_num,
4630                             'Non Service Line Type : Getting Unit Price'
4631                            );
4632          END IF;
4633 
4634          IF (p_event_type_id NOT IN
4635                                 (intercompany_invoice, intercompany_reversal)
4636             )
4637          THEN
4638             l_asset_option :=
4639                      p_transaction_reverse_flow_rec.asset_item_pricing_option;
4640             l_expense_option :=
4641                    p_transaction_reverse_flow_rec.expense_item_pricing_option;
4642          ELSE
4643             l_asset_option :=
4644                      p_transaction_forward_flow_rec.asset_item_pricing_option;
4645             l_expense_option :=
4646                    p_transaction_forward_flow_rec.expense_item_pricing_option;
4647          END IF;
4648 
4649          get_unitprice (p_api_version                      => l_api_version,
4650                         x_return_status                    => l_return_status,
4651                         x_msg_count                        => l_msg_count,
4652                         x_msg_data                         => l_msg_data,
4653                         p_rcv_accttxn                      => l_rcv_accttxn,
4654                         p_asset_item_pricing_option        => l_asset_option,
4655                         p_expense_item_pricing_option      => l_expense_option,
4656                         x_intercompany_pricing_option      => l_ic_pricing_option,
4657                         x_unit_price                       => l_unit_price,
4658                         x_currency_code                    => l_currency_code,
4659                         x_incr_transfer_price              => l_incr_transfer_price,
4660                         x_incr_currency_code               => l_incr_currency_code
4661                        );
4662 
4663          IF l_return_status <> fnd_api.g_ret_sts_success
4664          THEN
4665             l_api_message := 'Error getting unit price';
4666 
4667             IF     g_debug = 'Y'
4668                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
4669             THEN
4670                fnd_log.STRING (fnd_log.level_unexpected,
4671                                g_log_head || '.' || l_api_name || l_stmt_num,
4672                                   'Insert_Txn : '
4673                                || l_stmt_num
4674                                || ' : '
4675                                || l_api_message
4676                               );
4677             END IF;
4678 
4679             RAISE fnd_api.g_exc_unexpected_error;
4680          END IF;
4681 
4682          l_rcv_accttxn.intercompany_pricing_option := l_ic_pricing_option;
4683          l_rcv_accttxn.currency_code := l_currency_code;
4684          l_rcv_accttxn.unit_price := l_unit_price;
4685          l_rcv_accttxn.intercompany_price := l_incr_transfer_price;
4686          l_rcv_accttxn.intercompany_curr_code := l_incr_currency_code;
4687       END IF;
4688 
4689       -- Initialize Transaction Quantity
4690       IF l_rcv_accttxn.service_flag = 'N'
4691       THEN
4692          l_stmt_num := 120;
4693 
4694          IF     g_debug = 'Y'
4695             AND fnd_log.level_event >= fnd_log.g_current_runtime_level
4696          THEN
4697             fnd_log.STRING (fnd_log.level_event,
4698                             g_log_head || '.' || l_api_name || '.'
4699                             || l_stmt_num,
4700                             'Non Service line type : Getting Quantity'
4701                            );
4702          END IF;
4703 
4704          get_quantity (p_api_version              => l_api_version,
4705                        x_return_status            => l_return_status,
4706                        x_msg_count                => l_msg_count,
4707                        x_msg_data                 => l_msg_data,
4708                        p_rcv_accttxn              => l_rcv_accttxn,
4709                        x_source_doc_quantity      => l_source_doc_quantity
4710                       );
4711 
4712          IF l_return_status <> fnd_api.g_ret_sts_success
4713          THEN
4714             l_api_message := 'Error getting quantity';
4715 
4716             IF     g_debug = 'Y'
4717                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
4718             THEN
4719                fnd_log.STRING (fnd_log.level_unexpected,
4720                                g_log_head || '.' || l_api_name || l_stmt_num,
4721                                   'Insert_Txn : '
4722                                || l_stmt_num
4723                                || ' : '
4724                                || l_api_message
4725                               );
4726             END IF;
4727 
4728             RAISE fnd_api.g_exc_unexpected_error;
4729          END IF;
4730 
4731          l_rcv_accttxn.source_doc_quantity := l_source_doc_quantity;
4732 
4733          -- If transaction quantity is 0, then no event should be seeded.
4734          IF (l_source_doc_quantity = 0)
4735          THEN
4736             x_return_status := 'W';
4737 
4738             IF     g_debug = 'Y'
4739                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
4740             THEN
4741                l_api_message :=
4742                   'Transaction Quantity is 0. Returning without seeding event.';
4743                fnd_log.STRING (fnd_log.level_unexpected,
4744                                g_log_head || '.' || l_api_name || l_stmt_num,
4745                                   'Insert_Txnt : '
4746                                || l_stmt_num
4747                                || ' : '
4748                                || l_api_message
4749                               );
4750             END IF;
4751 
4752             RETURN;
4753          END IF;
4754       END IF;
4755 
4756       l_stmt_num := 130;
4757 
4758       IF     g_debug = 'Y'
4759          AND fnd_log.level_event >= fnd_log.g_current_runtime_level
4760       THEN
4761          fnd_log.STRING (fnd_log.level_event,
4762                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
4763                          'Getting Tax'
4764                         );
4765       END IF;
4766 
4767       get_unittax (p_api_version        => l_api_version,
4768                    x_return_status      => l_return_status,
4769                    x_msg_count          => l_msg_count,
4770                    x_msg_data           => l_msg_data,
4771                    p_rcv_accttxn        => l_rcv_accttxn,
4772                    x_unit_nr_tax        => l_unit_nr_tax,
4773                    x_unit_rec_tax       => l_unit_rec_tax,
4774                    x_prior_nr_tax       => l_prior_nr_tax,
4775                    x_prior_rec_tax      => l_prior_rec_tax
4776                   );
4777 
4778       IF l_return_status <> fnd_api.g_ret_sts_success
4779       THEN
4780          l_api_message := 'Error getting tax';
4781 
4782          IF     g_debug = 'Y'
4783             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
4784          THEN
4785             fnd_log.STRING (fnd_log.level_unexpected,
4786                             g_log_head || '.' || l_api_name || l_stmt_num,
4787                                'Insert_Txn : '
4788                             || l_stmt_num
4789                             || ' : '
4790                             || l_api_message
4791                            );
4792          END IF;
4793 
4794          RAISE fnd_api.g_exc_unexpected_error;
4795       END IF;
4796 
4797       l_rcv_accttxn.unit_nr_tax := l_unit_nr_tax;
4798       l_rcv_accttxn.unit_rec_tax := l_unit_rec_tax;
4799       l_rcv_accttxn.prior_nr_tax := l_prior_nr_tax;
4800       l_rcv_accttxn.prior_rec_tax := l_prior_rec_tax;
4801       l_stmt_num := 140;
4802 
4803       IF l_rcv_accttxn.service_flag = 'N'
4804       THEN
4805          IF     g_debug = 'Y'
4806             AND fnd_log.level_event >= fnd_log.g_current_runtime_level
4807          THEN
4808             fnd_log.STRING (fnd_log.level_event,
4809                             g_log_head || '.' || l_api_name || '.'
4810                             || l_stmt_num,
4811                             'Getting UOM'
4812                            );
4813          END IF;
4814 
4815          convert_uom (p_api_version          => l_api_version,
4816                       x_return_status        => l_return_status,
4817                       x_msg_count            => l_msg_count,
4818                       x_msg_data             => l_msg_data,
4819                       p_event_rec            => l_rcv_accttxn,
4820                       x_transaction_qty      => l_transaction_quantity,
4821                       x_primary_uom          => l_primary_uom,
4822                       x_primary_qty          => l_primary_qty,
4823                       x_trx_uom_code         => l_trx_uom_code
4824                      );
4825 
4826          IF l_return_status <> fnd_api.g_ret_sts_success
4827          THEN
4828             l_api_message := 'Error Converting UOM';
4829 
4830             IF     g_debug = 'Y'
4831                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
4832             THEN
4833                fnd_log.STRING (fnd_log.level_unexpected,
4834                                g_log_head || '.' || l_api_name || l_stmt_num,
4835                                   'Insert_Txn : '
4836                                || l_stmt_num
4837                                || ' : '
4838                                || l_api_message
4839                               );
4840             END IF;
4841 
4842             RAISE fnd_api.g_exc_unexpected_error;
4843          END IF;
4844 
4845          l_rcv_accttxn.transaction_quantity := l_transaction_quantity;
4846          l_rcv_accttxn.primary_uom := l_primary_uom;
4847          l_rcv_accttxn.primary_quantity := l_primary_qty;
4848          l_rcv_accttxn.trx_uom_code := l_trx_uom_code;
4849       END IF;
4850 
4851       -- Initialize Currency Information
4852       l_stmt_num := 150;
4853 
4854       IF     g_debug = 'Y'
4855          AND fnd_log.level_event >= fnd_log.g_current_runtime_level
4856       THEN
4857          fnd_log.STRING (fnd_log.level_event,
4858                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
4859                          'Getting Currency Information'
4860                         );
4861       END IF;
4862 
4863       get_currency (p_api_version                   => l_api_version,
4864                     x_return_status                 => l_return_status,
4865                     x_msg_count                     => l_msg_count,
4866                     x_msg_data                      => l_msg_data,
4867                     p_rcv_accttxn                   => l_rcv_accttxn,
4868                     x_currency_code                 => l_currency_code,
4869                     x_currency_conversion_rate      => l_currency_conversion_rate,
4870                     x_currency_conversion_date      => l_currency_conversion_date,
4871                     x_currency_conversion_type      => l_currency_conversion_type
4872                    );
4873 
4874       IF l_return_status <> fnd_api.g_ret_sts_success
4875       THEN
4876          l_api_message := 'Error Getting Currency';
4877 
4878          IF     g_debug = 'Y'
4879             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
4880          THEN
4881             fnd_log.STRING (fnd_log.level_unexpected,
4882                             g_log_head || '.' || l_api_name || l_stmt_num,
4883                                'Insert_Txn : '
4884                             || l_stmt_num
4885                             || ' : '
4886                             || l_api_message
4887                            );
4888          END IF;
4889 
4890          RAISE fnd_api.g_exc_unexpected_error;
4891       END IF;
4892 
4893       l_rcv_accttxn.currency_code := l_currency_code;
4894       l_rcv_accttxn.currency_conversion_rate := l_currency_conversion_rate;
4895       l_rcv_accttxn.currency_conversion_date := l_currency_conversion_date;
4896       l_rcv_accttxn.currency_conversion_type := l_currency_conversion_type;
4897       -- Get Debit and Credit Accounts
4898       l_stmt_num := 160;
4899 
4900       IF     g_debug = 'Y'
4901          AND fnd_log.level_event >= fnd_log.g_current_runtime_level
4902       THEN
4903          fnd_log.STRING (fnd_log.level_event,
4904                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
4905                          'Getting Debit and Credit Accounts'
4906                         );
4907       END IF;
4908 
4909       get_accounts
4910             (p_api_version                       => l_api_version,
4911              x_return_status                     => l_return_status,
4912              x_msg_count                         => l_msg_count,
4913              x_msg_data                          => l_msg_data,
4914              p_rcv_accttxn                       => l_rcv_accttxn,
4915              p_transaction_forward_flow_rec      => p_transaction_forward_flow_rec,
4916              p_transaction_reverse_flow_rec      => p_transaction_reverse_flow_rec,
4917              x_credit_acct_id                    => l_credit_acct_id,
4918              x_debit_acct_id                     => l_debit_acct_id,
4919              x_ic_cogs_acct_id                   => l_ic_cogs_acct_id
4920             );
4921 
4922       IF l_return_status <> fnd_api.g_ret_sts_success
4923       THEN
4924          l_api_message := 'Error getting account information';
4925 
4926          IF     g_debug = 'Y'
4927             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
4928          THEN
4929             fnd_log.STRING (fnd_log.level_unexpected,
4930                             g_log_head || '.' || l_api_name || l_stmt_num,
4931                                'Insert_Txn : '
4932                             || l_stmt_num
4933                             || ' : '
4934                             || l_api_message
4935                            );
4936          END IF;
4937 
4938          RAISE fnd_api.g_exc_unexpected_error;
4939       END IF;
4940 
4941       l_rcv_accttxn.credit_account_id := l_credit_acct_id;
4942       l_rcv_accttxn.debit_account_id := l_debit_acct_id;
4943       l_rcv_accttxn.intercompany_cogs_account_id := l_ic_cogs_acct_id;
4944       -- Initialize USSGL Transaction Codes
4945       l_stmt_num := 170;
4946 
4947       IF     g_debug = 'Y'
4948          AND fnd_log.level_event >= fnd_log.g_current_runtime_level
4949       THEN
4950          fnd_log.STRING (fnd_log.level_event,
4951                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
4952                          'Getting USSGL TC'
4953                         );
4954       END IF;
4955 
4956       get_ussgltc (p_api_version        => l_api_version,
4957                    x_return_status      => l_return_status,
4958                    x_msg_count          => l_msg_count,
4959                    x_msg_data           => l_msg_data,
4960                    p_rcv_accttxn        => l_rcv_accttxn,
4961                    x_ussgl_tc           => l_ussgl_tc
4962                   );
4963 
4964       IF l_return_status <> fnd_api.g_ret_sts_success
4965       THEN
4966          l_api_message := 'Error getting USSGL Transaction Code';
4967 
4968          IF     g_debug = 'Y'
4969             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
4970          THEN
4971             fnd_log.STRING (fnd_log.level_unexpected,
4972                             g_log_head || '.' || l_api_name || l_stmt_num,
4973                                'Insert_Txn : '
4974                             || l_stmt_num
4975                             || ' : '
4976                             || l_api_message
4977                            );
4978          END IF;
4979 
4980          RAISE fnd_api.g_exc_unexpected_error;
4981       END IF;
4982 
4983       l_rcv_accttxn.ussgl_transaction_code := l_ussgl_tc;
4984       x_rcv_accttxn := l_rcv_accttxn;
4985 
4986       --- Standard check of p_commit
4987       IF fnd_api.to_boolean (p_commit)
4988       THEN
4989          COMMIT WORK;
4990       END IF;
4991 
4992       -- Standard Call to get message count and if count = 1, get message info
4993       fnd_msg_pub.count_and_get (p_count      => x_msg_count,
4994                                  p_data       => x_msg_data);
4995 
4996       IF     g_debug = 'Y'
4997          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
4998       THEN
4999          fnd_log.STRING (fnd_log.level_procedure,
5000                          g_log_head || '.' || l_api_name || '.end',
5001                          'Insert_Txn >>'
5002                         );
5003       END IF;
5004    EXCEPTION
5005       WHEN fnd_api.g_exc_error
5006       THEN
5007          ROLLBACK TO insert_txn_pvt;
5008          x_return_status := fnd_api.g_ret_sts_error;
5009          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
5010                                     p_data       => x_msg_data
5011                                    );
5012       WHEN fnd_api.g_exc_unexpected_error
5013       THEN
5014          ROLLBACK TO insert_txn_pvt;
5015          x_return_status := fnd_api.g_ret_sts_unexp_error;
5016          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
5017                                     p_data       => x_msg_data
5018                                    );
5019       WHEN OTHERS
5020       THEN
5021          ROLLBACK TO insert_txn_pvt;
5022          x_return_status := fnd_api.g_ret_sts_unexp_error;
5023 
5024          IF     g_debug = 'Y'
5025             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
5026          THEN
5027             fnd_log.STRING (fnd_log.level_unexpected,
5028                             g_log_head || '.' || l_api_name || l_stmt_num,
5029                                'Insert_Txn '
5030                             || l_stmt_num
5031                             || ' : '
5032                             || SUBSTR (SQLERRM, 1, 200)
5033                            );
5034          END IF;
5035 
5036          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
5037          THEN
5038             fnd_msg_pub.add_exc_msg (g_pkg_name,
5039                                         l_api_name
5040                                      || 'Statement -'
5041                                      || TO_CHAR (l_stmt_num)
5042                                     );
5043          END IF;
5044 
5045          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
5046                                     p_data       => x_msg_data
5047                                    );
5048    END insert_txn;
5049 
5050 /** =========================================
5051 * Adapted from Insert_Txn
5052 ** ========================================== **/
5053    PROCEDURE insert_txn2 (
5054       p_api_version        IN              NUMBER := 1.0,
5055       p_init_msg_list      IN              VARCHAR2 := fnd_api.g_false,
5056       p_commit             IN              VARCHAR2 := fnd_api.g_false,
5057       p_validation_level   IN              NUMBER
5058             := fnd_api.g_valid_level_full,
5059       x_return_status      OUT NOCOPY      VARCHAR2,
5060       x_msg_count          OUT NOCOPY      NUMBER,
5061       x_msg_data           OUT NOCOPY      VARCHAR2,
5062       p_rcv_accttxn_tbl    IN              gmf_rcv_accounting_pkg.rcv_accttxn_tbl_type
5063    )
5064    IS
5065       l_api_name        CONSTANT VARCHAR2 (30)   := 'Insert_Txn2';
5066       l_api_version     CONSTANT NUMBER          := 1.0;
5067       l_return_status            VARCHAR2 (1)    := fnd_api.g_ret_sts_success;
5068       l_msg_count                NUMBER          := 0;
5069       l_msg_data                 VARCHAR2 (8000) := '';
5070       l_stmt_num                 NUMBER          := 0;
5071       l_api_message              VARCHAR2 (1000);
5072       l_summarize_acc_flag       VARCHAR2 (1)    := 'N';
5073       l_err_num                  NUMBER;
5074       l_err_code                 VARCHAR2 (240);
5075       l_err_msg                  VARCHAR2 (240);
5076       l_return_code              NUMBER;
5077       l_rcv_transaction_id       NUMBER;
5078       l_del_transaction_id       NUMBER;
5079       l_detail_accounting_flag   VARCHAR2 (1)    := 'Y';
5080       l_accrue_on_receipt_flag   VARCHAR2 (1)    := 'N';
5081       l_accounting_txn_id        NUMBER;
5082       l_ctr_first                NUMBER;
5083    BEGIN
5084       -- Standard start of API savepoint
5085       SAVEPOINT insert_txn2_pvt;
5086       l_stmt_num := 0;
5087 
5088       IF     g_debug = 'Y'
5089          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
5090       THEN
5091          fnd_log.STRING (fnd_log.level_procedure,
5092                          g_log_head || '.' || l_api_name || '.begin',
5093                          'Insert_Txn2 <<'
5094                         );
5095       END IF;
5096 
5097       -- Standard call to check for call compatibility
5098       IF NOT fnd_api.compatible_api_call (l_api_version,
5099                                           p_api_version,
5100                                           l_api_name,
5101                                           g_pkg_name
5102                                          )
5103       THEN
5104          RAISE fnd_api.g_exc_unexpected_error;
5105       END IF;
5106 
5107       -- Initialize message list if p_init_msg_list is set to TRUE
5108       IF fnd_api.to_boolean (p_init_msg_list)
5109       THEN
5110          fnd_msg_pub.initialize;
5111       END IF;
5112 
5113       -- Initialize API return status to success
5114       x_return_status := fnd_api.g_ret_sts_success;
5115 
5116       IF     g_debug = 'Y'
5117          AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
5118       THEN
5119          l_api_message :=
5120                'Inserting '
5121             || p_rcv_accttxn_tbl.COUNT
5122             || ' transactions into GRAT';
5123          fnd_log.STRING (fnd_log.level_statement,
5124                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
5125                          l_api_message
5126                         );
5127       END IF;
5128 
5129       l_ctr_first := p_rcv_accttxn_tbl.FIRST;
5130       -- Check for accrual option. If accrual option is set to accrue at period-end, don't call the
5131       -- accounting API.
5132       l_stmt_num := 20;
5133 
5134       SELECT NVL (poll.accrue_on_receipt_flag, 'N')
5135         INTO l_accrue_on_receipt_flag
5136         FROM po_line_locations poll
5137        WHERE poll.line_location_id =
5138                            p_rcv_accttxn_tbl (l_ctr_first).po_line_location_id;
5139 
5140       <<grat_insert>>
5141       FOR i IN p_rcv_accttxn_tbl.FIRST .. p_rcv_accttxn_tbl.LAST
5142       LOOP
5143          l_stmt_num := 30;
5144 
5145          SELECT gmf_rcv_accounting_txns_s.NEXTVAL
5146            INTO l_accounting_txn_id
5147            FROM DUAL;
5148 
5149          IF     g_debug = 'Y'
5150             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
5151          THEN
5152             l_api_message :=
5153                SUBSTR (   'i : '
5154                        || i
5155                        || 'accounting_txn_id : '
5156                        || l_accounting_txn_id
5157                        || 'rcv_transaction_id : '
5158                        || p_rcv_accttxn_tbl (i).rcv_transaction_id
5159                        || 'po_line_id : '
5160                        || p_rcv_accttxn_tbl (i).po_line_id
5161                        || 'po_dist_id : '
5162                        || p_rcv_accttxn_tbl (i).po_distribution_id
5163                        || 'unit_price : '
5164                        || p_rcv_accttxn_tbl (i).unit_price
5165                        || 'currency : '
5166                        || p_rcv_accttxn_tbl (i).currency_code
5167                        || 'nr tax : '
5168                        || p_rcv_accttxn_tbl (i).unit_nr_tax
5169                        || 'rec tax : '
5170                        || p_rcv_accttxn_tbl (i).unit_rec_tax,
5171                        1,
5172                        1000
5173                       );
5174             fnd_log.STRING (fnd_log.level_statement,
5175                             g_log_head || '.' || l_api_name || '.'
5176                             || l_stmt_num,
5177                             l_api_message
5178                            );
5179          END IF;
5180 
5181          -- We are not doing a bulk insert due to a database limitation. On databases
5182          -- prior to 9i, you cannot do a bulk insert using a table of records. You have to
5183          -- multiple tables of scalar types. The expense of converting the table of records
5184          -- to multiple tables is not worthwhile in this case since we do not expect the
5185          -- number of rows to exceed 10.
5186          l_stmt_num := 40;
5187 
5188          INSERT INTO gmf_rcv_accounting_txns
5189                      (accounting_txn_id,
5190                       rcv_transaction_id,
5191                       event_type,
5192                       event_source,
5193                       event_source_id,
5194                       ledger_id,
5195                       org_id,
5196                       transfer_org_id,
5197                       organization_id,
5198                       transfer_organization_id,
5199                       debit_account_id,
5200                       credit_account_id,
5201                       transaction_date,
5202                       transaction_quantity,
5203                       transaction_unit_of_measure,
5204                       source_doc_quantity,
5205                       source_doc_unit_of_measure,
5206                       primary_quantity,
5207                       primary_unit_of_measure,
5208                       inventory_item_id,
5209                       po_header_id,
5210                       po_release_id,
5211                       po_line_id,
5212                       po_line_location_id,
5213                       po_distribution_id,
5214                       intercompany_pricing_option,
5215                       unit_price,
5216                       transaction_amount,
5217                       prior_unit_price,
5218                       nr_tax,
5219                       rec_tax, nr_tax_amount, rec_tax_amount,
5220                       prior_nr_tax,
5221                       prior_rec_tax,
5222                       currency_code,
5223                       currency_conversion_type,
5224                       currency_conversion_rate,
5225                       currency_conversion_date, accounted_flag,
5226                       procurement_org_flag,
5227                       cross_ou_flag,
5228                       trx_flow_header_id, invoiced_flag, creation_date,
5229                       created_by, last_update_date, last_updated_by,
5230                       last_update_login, request_id,
5231                       program_application_id, program_id,
5232                       program_udpate_date,
5233                       /* start LCM-OPM Integration  */
5234                       unit_landed_cost
5235                       /* end LCM-OPM Integration  */
5236                      )
5237               VALUES (gmf_rcv_accounting_txns_s.NEXTVAL,
5238                       DECODE (p_rcv_accttxn_tbl (i).event_source,
5239                               'INVOICEMATCH', p_rcv_accttxn_tbl (i).inv_distribution_id,
5240                               p_rcv_accttxn_tbl (i).rcv_transaction_id
5241                              ),
5242                       p_rcv_accttxn_tbl (i).event_type_id,
5243                       p_rcv_accttxn_tbl (i).event_source,
5244                       DECODE (p_rcv_accttxn_tbl (i).event_source,
5245                               'INVOICEMATCH', p_rcv_accttxn_tbl (i).inv_distribution_id,
5246                               p_rcv_accttxn_tbl (i).rcv_transaction_id
5247                              ),
5248                       p_rcv_accttxn_tbl (i).ledger_id,
5249                       p_rcv_accttxn_tbl (i).org_id,
5250                       p_rcv_accttxn_tbl (i).transfer_org_id,
5251                       p_rcv_accttxn_tbl (i).organization_id,
5252                       p_rcv_accttxn_tbl (i).transfer_organization_id,
5253                       p_rcv_accttxn_tbl (i).debit_account_id,
5254                       p_rcv_accttxn_tbl (i).credit_account_id,
5255                       p_rcv_accttxn_tbl (i).transaction_date,
5256                       DECODE (p_rcv_accttxn_tbl (i).service_flag,
5257                               'N', p_rcv_accttxn_tbl (i).transaction_quantity,
5258                               NULL
5259                              ),
5260                       p_rcv_accttxn_tbl (i).transaction_uom,
5261                       DECODE (p_rcv_accttxn_tbl (i).service_flag,
5262                               'N', p_rcv_accttxn_tbl (i).source_doc_quantity,
5263                               NULL
5264                              ),
5265                       p_rcv_accttxn_tbl (i).source_doc_uom,
5266                       DECODE (p_rcv_accttxn_tbl (i).service_flag,
5267                               'N', p_rcv_accttxn_tbl (i).primary_quantity,
5268                               NULL
5269                              ),
5270                       p_rcv_accttxn_tbl (i).primary_uom,
5271                       p_rcv_accttxn_tbl (i).item_id,
5272                       p_rcv_accttxn_tbl (i).po_header_id,
5273                       p_rcv_accttxn_tbl (i).po_release_id,
5274                       p_rcv_accttxn_tbl (i).po_line_id,
5275                       p_rcv_accttxn_tbl (i).po_line_location_id,
5276                       p_rcv_accttxn_tbl (i).po_distribution_id,
5277                       p_rcv_accttxn_tbl (i).intercompany_pricing_option,
5278                       DECODE (p_rcv_accttxn_tbl (i).service_flag,
5279                               'N', ( p_rcv_accttxn_tbl (i).unit_price + p_rcv_accttxn_tbl (i).unit_nr_tax ),
5280                               NULL
5281                              ),
5282                     /* Begin Bug 9792380 */
5283                       p_rcv_accttxn_tbl (i).transaction_amount,
5284                       DECODE (p_rcv_accttxn_tbl (i).event_source,
5285                               'RETROPRICE', ( p_rcv_accttxn_tbl (i).prior_unit_price + p_rcv_accttxn_tbl (i).prior_nr_tax ),
5286                               NULL
5287                              ),
5288                     /* End Bug 9792380 */
5289                       p_rcv_accttxn_tbl (i).unit_nr_tax,
5290                       p_rcv_accttxn_tbl (i).unit_rec_tax, NULL, NULL,
5291                       p_rcv_accttxn_tbl (i).prior_nr_tax,
5292                       p_rcv_accttxn_tbl (i).prior_rec_tax,
5293                       p_rcv_accttxn_tbl (i).currency_code,
5294                       p_rcv_accttxn_tbl (i).currency_conversion_type,
5295                       p_rcv_accttxn_tbl (i).currency_conversion_rate,
5296                       p_rcv_accttxn_tbl (i).currency_conversion_date, 'N',
5297                       p_rcv_accttxn_tbl (i).procurement_org_flag,
5298                       p_rcv_accttxn_tbl (i).cross_ou_flag,
5299                       p_rcv_accttxn_tbl (i).trx_flow_header_id, 'Y', SYSDATE,
5300                       fnd_global.user_id, SYSDATE, fnd_global.user_id,
5301                       fnd_global.login_id, fnd_global.conc_request_id,
5302                       fnd_global.prog_appl_id, fnd_global.conc_program_id,
5303                       SYSDATE,
5304                       /* start LCM-OPM Integration  */
5305                       p_rcv_accttxn_tbl (i).unit_landed_cost
5306                       /* end LCM-OPM Integration  */
5307                      );
5308 
5309          IF     g_debug = 'Y'
5310             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
5311          THEN
5312             l_api_message :=
5313                   'Inserted '
5314                || SQL%ROWCOUNT
5315                || 'rows in GRAT for org '
5316                || p_rcv_accttxn_tbl (i).org_id;
5317             fnd_log.STRING (fnd_log.level_statement,
5318                             g_log_head || '.' || l_api_name || '.'
5319                             || l_stmt_num,
5320                             l_api_message
5321                            );
5322          END IF;
5323 
5324         /**
5325         * rs - For process orgs, we are done at this point.
5326         * the accounting events for SLA will be created by the pre-processor
5327         * when it is run for PO transactions
5328         **/
5329 
5330       END LOOP grat_insert;
5331 
5332       --- Standard check of p_commit
5333       IF fnd_api.to_boolean (p_commit)
5334       THEN
5335          COMMIT WORK;
5336       END IF;
5337 
5338       -- Standard Call to get message count and if count = 1, get message info
5339       fnd_msg_pub.count_and_get (p_count      => x_msg_count,
5340                                  p_data       => x_msg_data);
5341 
5342       IF     g_debug = 'Y'
5343          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
5344       THEN
5345          fnd_log.STRING (fnd_log.level_procedure,
5346                          g_log_head || '.' || l_api_name || '.end',
5347                          'Insert_Txn2 >>'
5348                         );
5349       END IF;
5350    EXCEPTION
5351       WHEN fnd_api.g_exc_error
5352       THEN
5353          ROLLBACK TO insert_txn2_pvt;
5354          x_return_status := fnd_api.g_ret_sts_error;
5355          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
5356                                     p_data       => x_msg_data
5357                                    );
5358       WHEN fnd_api.g_exc_unexpected_error
5359       THEN
5360          ROLLBACK TO insert_txn2_pvt;
5361          x_return_status := fnd_api.g_ret_sts_unexp_error;
5362          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
5363                                     p_data       => x_msg_data
5364                                    );
5365       WHEN OTHERS
5366       THEN
5367          ROLLBACK TO insert_txn2_pvt;
5368          x_return_status := fnd_api.g_ret_sts_unexp_error;
5369 
5370          IF     g_debug = 'Y'
5371             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
5372          THEN
5373             fnd_log.STRING (fnd_log.level_unexpected,
5374                             g_log_head || '.' || l_api_name || l_stmt_num,
5375                                'Insert_Txn2 : '
5376                             || l_stmt_num
5377                             || ' : '
5378                             || SUBSTR (SQLERRM, 1, 200)
5379                            );
5380          END IF;
5381 
5382          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
5383          THEN
5384             fnd_msg_pub.add_exc_msg (g_pkg_name,
5385                                         l_api_name
5386                                      || 'Statement -'
5387                                      || TO_CHAR (l_stmt_num)
5388                                     );
5389          END IF;
5390 
5391          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
5392                                     p_data       => x_msg_data
5393                                    );
5394    END insert_txn2;
5395 
5396    PROCEDURE create_receive_txns (
5397       p_api_version            IN              NUMBER,
5398       p_init_msg_list          IN              VARCHAR2 := fnd_api.g_false,
5399       p_commit                 IN              VARCHAR2 := fnd_api.g_false,
5400       p_validation_level       IN              NUMBER
5401             := fnd_api.g_valid_level_full,
5402       x_return_status          OUT NOCOPY      VARCHAR2,
5403       x_msg_count              OUT NOCOPY      NUMBER,
5404       x_msg_data               OUT NOCOPY      VARCHAR2,
5405       p_rcv_transaction_id     IN              NUMBER,
5406       p_direct_delivery_flag   IN              VARCHAR2,
5407       p_gl_group_id            IN              NUMBER
5408    )
5409    IS
5410       l_api_name              CONSTANT VARCHAR2 (30)
5411                                                     := 'Create_ReceiveEvents';
5412       l_api_version           CONSTANT NUMBER                          := 1.0;
5413       l_return_status                  VARCHAR2 (1)
5414                                                  := fnd_api.g_ret_sts_success;
5415       l_msg_count                      NUMBER                            := 0;
5416       l_msg_data                       VARCHAR2 (8000)                  := '';
5417       l_stmt_num                       NUMBER                            := 0;
5418       l_api_message                    VARCHAR2 (1000);
5419       l_rcv_accttxn                    gmf_rcv_accounting_pkg.rcv_accttxn_rec_type;
5420       l_rcv_accttxn_tbl                gmf_rcv_accounting_pkg.rcv_accttxn_tbl_type;
5421       l_event_type_id                  NUMBER;
5422       l_transaction_flows_tbl          inv_transaction_flow_pub.g_transaction_flow_tbl_type;
5423       l_transaction_forward_flow_rec   inv_transaction_flow_pub.mtl_transaction_flow_rec_type;
5424       l_transaction_reverse_flow_rec   inv_transaction_flow_pub.mtl_transaction_flow_rec_type;
5425       l_trx_flow_exists_flag           NUMBER                            := 0;
5426       l_trx_flow_ctr                   NUMBER                            := 0;
5427       l_po_header_id                   NUMBER;
5428       l_po_line_id                     NUMBER;
5429       l_po_line_location_id            NUMBER;
5430       l_po_distribution_id             NUMBER;
5431       l_po_org_id                      NUMBER;
5432       l_po_ledger_id                   NUMBER;
5433       l_rcv_organization_id            NUMBER;
5434       l_rcv_org_id                     NUMBER;
5435       l_rcv_ledger_id                  NUMBER;
5436       l_org_id                         NUMBER;
5437       l_transfer_org_id                NUMBER;
5438       l_transfer_organization_id       NUMBER;
5439       l_rcv_trx_date                   DATE;
5440       l_drop_ship_flag                 NUMBER;
5441       l_destination_type               VARCHAR (25);
5442       l_item_id                        NUMBER;
5443       l_category_id                    NUMBER;
5444       l_project_id                     NUMBER;
5445       l_cross_ou_flag                  VARCHAR2 (1);
5446       l_accrual_flag                   VARCHAR2 (1);
5447       l_counter                        NUMBER;
5448       l_procurement_org_flag           VARCHAR2 (1);
5449       l_trx_flow_header_id             NUMBER;
5450       l_qualifier_code_tbl             inv_transaction_flow_pub.number_tbl;
5451       l_qualifier_value_tbl            inv_transaction_flow_pub.number_tbl;
5452 
5453       CURSOR c_po_distributions_csr (
5454          p_po_distribution_id    NUMBER,
5455          p_po_line_location_id   NUMBER
5456       )
5457       IS
5458          SELECT po_distribution_id, destination_type_code, project_id
5459            FROM po_distributions pod
5460           WHERE pod.po_distribution_id =
5461                            NVL (p_po_distribution_id, pod.po_distribution_id)
5462             AND pod.line_location_id = p_po_line_location_id;
5463    BEGIN
5464       -- Standard start of API savepoint
5465       SAVEPOINT create_receiveevents_pvt;
5466       l_stmt_num := 0;
5467 
5468       IF     g_debug = 'Y'
5469          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
5470       THEN
5471          fnd_log.STRING (fnd_log.level_procedure,
5472                          g_log_head || '.' || l_api_name || '.begin',
5473                          'Create_ReceiveEvents <<'
5474                         );
5475       END IF;
5476 
5477       -- Standard call to check for call compatibility
5478       IF NOT fnd_api.compatible_api_call (l_api_version,
5479                                           p_api_version,
5480                                           l_api_name,
5481                                           g_pkg_name
5482                                          )
5483       THEN
5484          RAISE fnd_api.g_exc_unexpected_error;
5485       END IF;
5486 
5487       -- Initialize message list if p_init_msg_list is set to TRUE
5488       IF fnd_api.to_boolean (p_init_msg_list)
5489       THEN
5490          fnd_msg_pub.initialize;
5491       END IF;
5492 
5493       -- Initialize API return status to success
5494       x_return_status := fnd_api.g_ret_sts_success;
5495       l_stmt_num := 20;
5496 
5497       SELECT rt.po_header_id, rt.po_line_id, rt.po_line_location_id,
5498              rt.po_distribution_id, rt.transaction_date,
5499              NVL (rt.dropship_type_code, 3), poh.org_id,
5500              poll.ship_to_organization_id, pol.item_id, pol.category_id,
5501              pol.project_id, NVL (poll.accrue_on_receipt_flag, 'N')
5502         INTO l_po_header_id, l_po_line_id, l_po_line_location_id,
5503              l_po_distribution_id, l_rcv_trx_date,
5504              l_drop_ship_flag, l_po_org_id,
5505              l_rcv_organization_id, l_item_id, l_category_id,
5506              l_project_id, l_accrual_flag
5507         FROM po_headers poh,
5508              po_line_locations poll,
5509              po_lines pol,
5510              rcv_transactions rt
5511        WHERE rt.transaction_id = p_rcv_transaction_id
5512          AND poh.po_header_id = rt.po_header_id
5513          AND poll.line_location_id = rt.po_line_location_id
5514          AND pol.po_line_id = rt.po_line_id;
5515 
5516       l_stmt_num := 30;
5517 
5518       -- Get Receiving Operating Unit
5519       SELECT operating_unit, set_of_books_id
5520         INTO l_rcv_org_id, l_rcv_ledger_id
5521         FROM cst_organization_definitions cod
5522        WHERE organization_id = l_rcv_organization_id;
5523 
5524       l_stmt_num := 35;
5525 
5526       -- Get PO SOB
5527       SELECT set_of_books_id
5528         INTO l_po_ledger_id
5529         FROM financials_system_parameters;
5530 
5531       l_stmt_num := 40;
5532 
5533       IF     g_debug = 'Y'
5534          AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
5535       THEN
5536          l_api_message :=
5537                'Creating Receive transactions : RCV Transaction ID : '
5538             || p_rcv_transaction_id
5539             || ', PO Header ID : '
5540             || l_po_header_id
5541             || ', PO Line ID : '
5542             || l_po_line_id
5543             || ', PO Line Location ID : '
5544             || l_po_line_location_id
5545             || ', PO Dist ID : '
5546             || l_po_distribution_id
5547             || ', Transaction Date : '
5548             || l_rcv_trx_date
5549             || ', Drop Ship Flag : '
5550             || l_drop_ship_flag
5551             || ', PO Org ID : '
5552             || l_po_org_id
5553             || ', PO LEDGER ID : '
5554             || l_po_ledger_id
5555             || ', RCV Organization ID : '
5556             || l_rcv_organization_id
5557             || ', RCV Org ID : '
5558             || l_rcv_org_id
5559             || ', RCV LEDGER ID : '
5560             || l_rcv_ledger_id
5561             || ', Category ID : '
5562             || l_category_id
5563             || ', Accrual Flag : '
5564             || l_accrual_flag;
5565          fnd_log.STRING (fnd_log.level_statement,
5566                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
5567                          l_api_message
5568                         );
5569       END IF;
5570 
5571       IF (l_po_org_id = l_rcv_org_id)
5572       THEN
5573          l_cross_ou_flag := 'N';
5574       ELSE
5575          l_cross_ou_flag := 'Y';
5576          /* For 11i10, the only supported qualifier is category id. */
5577          l_qualifier_code_tbl (l_qualifier_code_tbl.COUNT + 1) :=
5578                                     inv_transaction_flow_pub.g_qualifier_code;
5579          l_qualifier_value_tbl (l_qualifier_value_tbl.COUNT + 1) :=
5580                                                                 l_category_id;
5581          l_stmt_num := 50;
5582 
5583          IF     g_debug = 'Y'
5584             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
5585          THEN
5586             l_api_message :=
5587                   'Getting Procurement Transaction Flow :'
5588                || 'l_po_org_id : '
5589                || l_po_org_id
5590                || ' l_rcv_org_id : '
5591                || l_rcv_org_id
5592                || ' l_rcv_organization_id : '
5593                || l_rcv_organization_id;
5594             fnd_log.STRING (fnd_log.level_statement,
5595                             g_log_head || '.' || l_api_name || '.'
5596                             || l_stmt_num,
5597                             l_api_message
5598                            );
5599          END IF;
5600 
5601          inv_transaction_flow_pub.get_transaction_flow
5602                (x_return_status               => l_return_status,
5603                 x_msg_data                    => l_msg_data,
5604                 x_msg_count                   => l_msg_count,
5605                 x_transaction_flows_tbl       => l_transaction_flows_tbl,
5606                 p_api_version                 => 1.0,
5607                 p_start_operating_unit        => l_po_org_id,
5608                 p_end_operating_unit          => l_rcv_org_id,
5609                 p_flow_type                   => inv_transaction_flow_pub.g_procuring_flow_type,
5610                 p_organization_id             => l_rcv_organization_id,
5611                 p_qualifier_code_tbl          => l_qualifier_code_tbl,
5612                 p_qualifier_value_tbl         => l_qualifier_value_tbl,
5613                 p_transaction_date            => l_rcv_trx_date,
5614                 p_get_default_cost_group      => 'N'
5615                );
5616 
5617          IF (l_return_status = fnd_api.g_ret_sts_success)
5618          THEN
5619             l_trx_flow_exists_flag := 1;
5620             l_trx_flow_header_id :=
5621                l_transaction_flows_tbl (l_transaction_flows_tbl.FIRST).header_id;
5622 
5623             IF     g_debug = 'Y'
5624                AND fnd_log.level_event >= fnd_log.g_current_runtime_level
5625             THEN
5626                fnd_log.STRING (fnd_log.level_event,
5627                                   g_log_head
5628                                || '.'
5629                                || l_api_name
5630                                || '.'
5631                                || l_stmt_num,
5632                                'Transaction Flow exists'
5633                               );
5634             END IF;
5635          -- Return Status of 'W' indicates that no transaction flow exists.
5636          ELSIF (l_return_status = 'W')
5637          THEN
5638             l_trx_flow_exists_flag := 0;
5639 
5640             IF     g_debug = 'Y'
5641                AND fnd_log.level_event >= fnd_log.g_current_runtime_level
5642             THEN
5643                fnd_log.STRING (fnd_log.level_event,
5644                                   g_log_head
5645                                || '.'
5646                                || l_api_name
5647                                || '.'
5648                                || l_stmt_num,
5649                                'Transaction Flow does not exist'
5650                               );
5651             END IF;
5652 
5653                   -- If transaction flow does not exist, but the PO crosses multiple
5654             -- sets of books, error out the transaction.
5655             IF (l_po_ledger_id <> l_rcv_ledger_id)
5656             THEN
5657                l_api_message := 'Transaction Flow does not exist';
5658 
5659                IF     g_debug = 'Y'
5660                   AND fnd_log.level_unexpected >=
5661                                                fnd_log.g_current_runtime_level
5662                THEN
5663                   fnd_log.STRING (fnd_log.level_unexpected,
5664                                   g_log_head || '.' || l_api_name
5665                                   || l_stmt_num,
5666                                      'Create_ReceiveEvents : '
5667                                   || l_stmt_num
5668                                   || ' : '
5669                                   || l_api_message
5670                                  );
5671                END IF;
5672 
5673                RAISE fnd_api.g_exc_unexpected_error;
5674             END IF;
5675          ELSE
5676             l_api_message := 'Error occurred in Transaction Flow API';
5677 
5678             IF     g_debug = 'Y'
5679                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
5680             THEN
5681                fnd_log.STRING (fnd_log.level_unexpected,
5682                                g_log_head || '.' || l_api_name || l_stmt_num,
5683                                   'Create_ReceiveEvents : '
5684                                || l_stmt_num
5685                                || ' : '
5686                                || l_api_message
5687                               );
5688             END IF;
5689 
5690             RAISE fnd_api.g_exc_unexpected_error;
5691          END IF;                                         -- IF l_return_status
5692       END IF;                                                -- IF l_po_org_id
5693 
5694       -- For the receive transaction, the PO distribution may not be available in the
5695       -- case of Standard Receipt. Hence perform all steps for each applicable distribution.
5696       -- If distribution is not available the quantity will be prorated. Furthermore, if
5697       -- there is a project on any of the distributions, and the destination_type_code is
5698       -- expense, the transaction flow should be ignored for just that distribution.
5699       FOR rec_pod IN c_po_distributions_csr (l_po_distribution_id,
5700                                              l_po_line_location_id
5701                                             )
5702       LOOP
5703          l_stmt_num := 60;
5704 
5705          IF     g_debug = 'Y'
5706             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
5707          THEN
5708             l_api_message :=
5709                   'Creating Receive transactions : '
5710                || 'po_distribution_id : '
5711                || rec_pod.po_distribution_id
5712                || ' destination_type_code : '
5713                || rec_pod.destination_type_code
5714                || ' project_id : '
5715                || rec_pod.project_id;
5716             fnd_log.STRING (fnd_log.level_statement,
5717                             g_log_head || '.' || l_api_name || '.'
5718                             || l_stmt_num,
5719                             l_api_message
5720                            );
5721          END IF;
5722 
5723          l_procurement_org_flag := 'Y';
5724 
5725          -- For POs with destination type of expense, when there is a project on the
5726          -- POD, we should not look for transaction flow. This is because PA,(which transfers
5727          -- costs to Projects for expense destinations), is currently not supporting global
5728          -- procurement.
5729          IF (    (l_trx_flow_exists_flag = 1)
5730              AND (   rec_pod.project_id IS NULL
5731                   OR rec_pod.destination_type_code <> 'EXPENSE'
5732                  )
5733             )
5734          THEN
5735             l_trx_flow_ctr := l_transaction_flows_tbl.COUNT;
5736 
5737             -- Create Logical Receive transactions in each intermediate organization.
5738             FOR l_counter IN
5739                l_transaction_flows_tbl.FIRST .. l_transaction_flows_tbl.LAST
5740             LOOP
5741                IF     g_debug = 'Y'
5742                   AND fnd_log.level_event >= fnd_log.g_current_runtime_level
5743                THEN
5744                   fnd_log.STRING (fnd_log.level_event,
5745                                      g_log_head
5746                                   || '.'
5747                                   || l_api_name
5748                                   || '.'
5749                                   || l_stmt_num,
5750                                   'Seeding Logical Receive in GRAT'
5751                                  );
5752                END IF;
5753 
5754                l_stmt_num := 70;
5755                -- l_transaction_forward_flow_rec contains the transaction flow record
5756                -- where the org_id is the from_org_id.
5757                -- l_transaction_reverse_flow_rec contains the transaction flow record
5758                -- where the org_id is the to_org_id.
5759                -- Need to pass both to the Seed_RAE procedure because transfer_price is based
5760                -- on the reverse flow record and some accounts are based on the forward flow
5761                l_transaction_forward_flow_rec :=
5762                                            l_transaction_flows_tbl (l_counter);
5763 
5764                IF (l_counter = l_transaction_flows_tbl.FIRST)
5765                THEN
5766                   l_transaction_reverse_flow_rec := NULL;
5767                   l_transfer_org_id := NULL;
5768                   l_transfer_organization_id := NULL;
5769                ELSE
5770                   l_transaction_reverse_flow_rec :=
5771                                       l_transaction_flows_tbl (l_counter - 1);
5772                   l_transfer_org_id :=
5773                                    l_transaction_reverse_flow_rec.from_org_id;
5774                   l_transfer_organization_id :=
5775                           l_transaction_reverse_flow_rec.from_organization_id;
5776                END IF;
5777 
5778                l_stmt_num := 80;
5779                insert_txn
5780                   (x_return_status                     => l_return_status,
5781                    x_msg_count                         => l_msg_count,
5782                    x_msg_data                          => l_msg_data,
5783                    p_event_source                      => 'RECEIVING',
5784                    p_event_type_id                     => logical_receive,
5785                    p_rcv_transaction_id                => p_rcv_transaction_id,
5786                    p_inv_distribution_id               => NULL,
5787                    p_po_distribution_id                => rec_pod.po_distribution_id,
5788                    p_direct_delivery_flag              => p_direct_delivery_flag,
5789                    p_gl_group_id                       => p_gl_group_id,
5790                    p_cross_ou_flag                     => l_cross_ou_flag,
5791                    p_procurement_org_flag              => l_procurement_org_flag,
5792                    p_ship_to_org_flag                  => 'N',
5793                    p_drop_ship_flag                    => l_drop_ship_flag,
5794                    p_org_id                            => l_transaction_flows_tbl
5795                                                                     (l_counter).from_org_id,
5796                    p_organization_id                   => l_transaction_flows_tbl
5797                                                                     (l_counter).from_organization_id,
5798                    p_transfer_org_id                   => l_transfer_org_id,
5799                    p_transfer_organization_id          => l_transfer_organization_id,
5800                    p_trx_flow_header_id                => l_trx_flow_header_id,
5801                    p_transaction_forward_flow_rec      => l_transaction_forward_flow_rec,
5802                    p_transaction_reverse_flow_rec      => l_transaction_reverse_flow_rec,
5803                    p_unit_price                        => NULL,
5804                    p_prior_unit_price                  => NULL,
5805                    x_rcv_accttxn                       => l_rcv_accttxn
5806                   );
5807 
5808                IF l_return_status <> fnd_api.g_ret_sts_success
5809                THEN
5810                   l_api_message := 'Error creating event';
5811 
5812                   IF     g_debug = 'Y'
5813                      AND fnd_log.level_unexpected >=
5814                                                fnd_log.g_current_runtime_level
5815                   THEN
5816                      fnd_log.STRING (fnd_log.level_unexpected,
5817                                         g_log_head
5818                                      || '.'
5819                                      || l_api_name
5820                                      || l_stmt_num,
5821                                         'Create_ReceiveEvents : '
5822                                      || l_stmt_num
5823                                      || ' : '
5824                                      || l_api_message
5825                                     );
5826                   END IF;
5827 
5828                   RAISE fnd_api.g_exc_unexpected_error;
5829                END IF;
5830 
5831                l_rcv_accttxn_tbl (l_rcv_accttxn_tbl.COUNT + 1) :=
5832                                                                  l_rcv_accttxn;
5833 
5834                    -- For one-time items, if online accruals is used, seed IC Invoice event.
5835                -- For Shop Floor destination types, always seed IC Invoice events.
5836                IF (   (l_item_id IS NULL AND l_accrual_flag = 'Y')
5837                    OR (rec_pod.destination_type_code = 'SHOP FLOOR')
5838                   )
5839                THEN
5840                   l_stmt_num := 90;
5841 
5842                   IF     g_debug = 'Y'
5843                      AND fnd_log.level_event >=
5844                                                fnd_log.g_current_runtime_level
5845                   THEN
5846                      fnd_log.STRING (fnd_log.level_event,
5847                                         g_log_head
5848                                      || '.'
5849                                      || l_api_name
5850                                      || '.'
5851                                      || l_stmt_num,
5852                                      'Seeding Invoice Match in GRAT'
5853                                     );
5854                   END IF;
5855 
5856                   insert_txn
5857                      (p_api_version                       => 1.0,
5858                       x_return_status                     => l_return_status,
5859                       x_msg_count                         => l_msg_count,
5860                       x_msg_data                          => l_msg_data,
5861                       p_event_source                      => 'RECEIVING',
5862                       p_event_type_id                     => intercompany_invoice,
5863                       p_rcv_transaction_id                => p_rcv_transaction_id,
5864                       p_inv_distribution_id               => NULL,
5865                       p_po_distribution_id                => rec_pod.po_distribution_id,
5866                       p_direct_delivery_flag              => p_direct_delivery_flag,
5867                       p_gl_group_id                       => NULL,
5868                       p_cross_ou_flag                     => l_cross_ou_flag,
5869                       p_procurement_org_flag              => l_procurement_org_flag,
5870                       p_ship_to_org_flag                  => 'N',
5871                       p_drop_ship_flag                    => l_drop_ship_flag,
5872                       p_org_id                            => l_transaction_flows_tbl
5873                                                                     (l_counter).from_org_id,
5874                       p_organization_id                   => l_transaction_flows_tbl
5875                                                                     (l_counter).from_organization_id,
5876                       p_transfer_org_id                   => l_transaction_flows_tbl
5877                                                                     (l_counter).to_org_id,
5878                       p_transfer_organization_id          => l_transaction_flows_tbl
5879                                                                     (l_counter).to_organization_id,
5880                       p_trx_flow_header_id                => l_trx_flow_header_id,
5881                       p_transaction_forward_flow_rec      => l_transaction_forward_flow_rec,
5882                       p_transaction_reverse_flow_rec      => l_transaction_reverse_flow_rec,
5883                       p_unit_price                        => NULL,
5884                       p_prior_unit_price                  => NULL,
5885                       x_rcv_accttxn                       => l_rcv_accttxn
5886                      );
5887 
5888                   IF l_return_status <> fnd_api.g_ret_sts_success
5889                   THEN
5890                      l_api_message := 'Error creating event';
5891 
5892                      IF     g_debug = 'Y'
5893                         AND fnd_log.level_unexpected >=
5894                                                fnd_log.g_current_runtime_level
5895                      THEN
5896                         fnd_log.STRING (fnd_log.level_unexpected,
5897                                            g_log_head
5898                                         || '.'
5899                                         || l_api_name
5900                                         || l_stmt_num,
5901                                            'Create_ReceiveEvents : '
5902                                         || l_stmt_num
5903                                         || ' : '
5904                                         || l_api_message
5905                                        );
5906                      END IF;
5907 
5908                      RAISE fnd_api.g_exc_unexpected_error;
5909                   END IF;
5910 
5911                   l_rcv_accttxn_tbl (l_rcv_accttxn_tbl.COUNT + 1) :=
5912                                                                  l_rcv_accttxn;
5913                END IF;
5914 
5915                l_procurement_org_flag := 'N';
5916             END LOOP;
5917          END IF;
5918 
5919          l_stmt_num := 100;
5920 
5921          IF (l_trx_flow_exists_flag = 1)
5922          THEN
5923             l_transaction_forward_flow_rec := NULL;
5924             l_transaction_reverse_flow_rec :=
5925                                      l_transaction_flows_tbl (l_trx_flow_ctr);
5926             l_org_id := l_transaction_flows_tbl (l_trx_flow_ctr).to_org_id;
5927             l_transfer_org_id :=
5928                          l_transaction_flows_tbl (l_trx_flow_ctr).from_org_id;
5929             l_transfer_organization_id :=
5930                           l_transaction_reverse_flow_rec.from_organization_id;
5931          ELSE
5932             l_transaction_forward_flow_rec := NULL;
5933             l_transaction_reverse_flow_rec := NULL;
5934             l_org_id := l_po_org_id;
5935             l_transfer_org_id := NULL;
5936             l_transfer_organization_id := NULL;
5937          END IF;
5938 
5939          l_stmt_num := 110;
5940 
5941          -- If drop ship flag is 1(drop ship with new accounting) OR 2(drop ship with old accounting),
5942          -- then create a LOGICAL RECEIVE and use the clearing account. It drop ship flag is 3 (not a
5943          -- drop ship), then create a RECEIVE event
5944          IF (l_drop_ship_flag IN (1, 2))
5945          THEN
5946             -- This is a pure (external) drop ship scenario. Seed a LOGICAL_RECEIVE event in the receiving org.
5947             IF     g_debug = 'Y'
5948                AND fnd_log.level_event >= fnd_log.g_current_runtime_level
5949             THEN
5950                fnd_log.STRING (fnd_log.level_event,
5951                                   g_log_head
5952                                || '.'
5953                                || l_api_name
5954                                || '.'
5955                                || l_stmt_num,
5956                                'Drop Ship : Seeding Logical Receive in GRAT'
5957                               );
5958             END IF;
5959 
5960             l_stmt_num := 120;
5961             insert_txn
5962                (p_api_version                       => 1.0,
5963                 x_return_status                     => l_return_status,
5964                 x_msg_count                         => l_msg_count,
5965                 x_msg_data                          => l_msg_data,
5966                 p_event_source                      => 'RECEIVING',
5967                 p_event_type_id                     => logical_receive,
5968                 p_rcv_transaction_id                => p_rcv_transaction_id,
5969                 p_inv_distribution_id               => NULL,
5970                 p_po_distribution_id                => rec_pod.po_distribution_id,
5971                 p_direct_delivery_flag              => p_direct_delivery_flag,
5972                 p_gl_group_id                       => p_gl_group_id,
5973                 p_cross_ou_flag                     => l_cross_ou_flag,
5974                 p_procurement_org_flag              => l_procurement_org_flag,
5975                 p_ship_to_org_flag                  => 'Y',
5976                 p_drop_ship_flag                    => l_drop_ship_flag,
5977                 p_org_id                            => l_org_id,
5978                 p_organization_id                   => l_rcv_organization_id,
5979                 p_transfer_org_id                   => l_transfer_org_id,
5980                 p_transfer_organization_id          => l_transfer_organization_id,
5981                 p_trx_flow_header_id                => l_trx_flow_header_id,
5982                 p_transaction_forward_flow_rec      => l_transaction_forward_flow_rec,
5983                 p_transaction_reverse_flow_rec      => l_transaction_reverse_flow_rec,
5984                 p_unit_price                        => NULL,
5985                 p_prior_unit_price                  => NULL,
5986                 x_rcv_accttxn                       => l_rcv_accttxn
5987                );
5988 
5989             IF l_return_status <> fnd_api.g_ret_sts_success
5990             THEN
5991                l_api_message := 'Error creating event';
5992 
5993                IF     g_debug = 'Y'
5994                   AND fnd_log.level_unexpected >=
5995                                                fnd_log.g_current_runtime_level
5996                THEN
5997                   fnd_log.STRING (fnd_log.level_unexpected,
5998                                   g_log_head || '.' || l_api_name
5999                                   || l_stmt_num,
6000                                      'Create_ReceiveEvents : '
6001                                   || l_stmt_num
6002                                   || ' : '
6003                                   || l_api_message
6004                                  );
6005                END IF;
6006 
6007                RAISE fnd_api.g_exc_unexpected_error;
6008             END IF;
6009 
6010             l_rcv_accttxn_tbl (l_rcv_accttxn_tbl.COUNT + 1) := l_rcv_accttxn;
6011          ELSE
6012             l_stmt_num := 130;
6013 
6014             SELECT DECODE (rt.transaction_type, 'CORRECT', correct, receive)
6015               INTO l_event_type_id
6016               FROM rcv_transactions rt
6017              WHERE transaction_id = p_rcv_transaction_id;
6018 
6019             IF     g_debug = 'Y'
6020                AND fnd_log.level_event >= fnd_log.g_current_runtime_level
6021             THEN
6022                fnd_log.STRING (fnd_log.level_event,
6023                                   g_log_head
6024                                || '.'
6025                                || l_api_name
6026                                || '.'
6027                                || l_stmt_num,
6028                                'Not Drop Ship : Seeding Receive in GRAT'
6029                               );
6030             END IF;
6031 
6032             l_stmt_num := 140;
6033             insert_txn
6034                (p_api_version                       => 1.0,
6035                 x_return_status                     => l_return_status,
6036                 x_msg_count                         => l_msg_count,
6037                 x_msg_data                          => l_msg_data,
6038                 p_event_source                      => 'RECEIVING',
6039                 p_event_type_id                     => l_event_type_id,
6040                 p_rcv_transaction_id                => p_rcv_transaction_id,
6041                 p_inv_distribution_id               => NULL,
6042                 p_po_distribution_id                => rec_pod.po_distribution_id,
6043                 p_direct_delivery_flag              => p_direct_delivery_flag,
6044                 p_gl_group_id                       => p_gl_group_id,
6045                 p_cross_ou_flag                     => l_cross_ou_flag,
6046                 p_procurement_org_flag              => l_procurement_org_flag,
6047                 p_ship_to_org_flag                  => 'Y',
6048                 p_drop_ship_flag                    => l_drop_ship_flag,
6049                 p_org_id                            => l_org_id,
6050                 p_organization_id                   => l_rcv_organization_id,
6051                 p_transfer_org_id                   => l_transfer_org_id,
6052                 p_transfer_organization_id          => l_transfer_organization_id,
6053                 p_trx_flow_header_id                => l_trx_flow_header_id,
6054                 p_transaction_forward_flow_rec      => l_transaction_forward_flow_rec,
6055                 p_transaction_reverse_flow_rec      => l_transaction_reverse_flow_rec,
6056                 p_unit_price                        => NULL,
6057                 p_prior_unit_price                  => NULL,
6058                 x_rcv_accttxn                       => l_rcv_accttxn
6059                );
6060 
6061             IF l_return_status <> fnd_api.g_ret_sts_success
6062             THEN
6063                l_api_message := 'Error creating event';
6064 
6065                IF     g_debug = 'Y'
6066                   AND fnd_log.level_unexpected >=
6067                                                fnd_log.g_current_runtime_level
6068                THEN
6069                   fnd_log.STRING (fnd_log.level_unexpected,
6070                                   g_log_head || '.' || l_api_name
6071                                   || l_stmt_num,
6072                                      'Create_ReceiveEvents : '
6073                                   || l_stmt_num
6074                                   || ' : '
6075                                   || l_api_message
6076                                  );
6077                END IF;
6078 
6079                RAISE fnd_api.g_exc_unexpected_error;
6080             END IF;
6081 
6082             l_rcv_accttxn_tbl (l_rcv_accttxn_tbl.COUNT + 1) := l_rcv_accttxn;
6083          END IF;
6084       END LOOP;
6085 
6086       l_stmt_num := 150;
6087 
6088       IF     g_debug = 'Y'
6089          AND fnd_log.level_event >= fnd_log.g_current_runtime_level
6090       THEN
6091          fnd_log.STRING (fnd_log.level_event,
6092                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
6093                          'Inserting transactions into GRAT'
6094                         );
6095       END IF;
6096 
6097       insert_txn2 (x_return_status        => l_return_status,
6098                    x_msg_count            => l_msg_count,
6099                    x_msg_data             => l_msg_data,
6100                    p_rcv_accttxn_tbl      => l_rcv_accttxn_tbl
6101                   );
6102 
6103       IF l_return_status <> fnd_api.g_ret_sts_success
6104       THEN
6105          l_api_message := 'Error inserting transactions into GRAT';
6106 
6107          IF     g_debug = 'Y'
6108             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
6109          THEN
6110             fnd_log.STRING (fnd_log.level_unexpected,
6111                             g_log_head || '.' || l_api_name || l_stmt_num,
6112                                'Create_ReceiveEvents : '
6113                             || l_stmt_num
6114                             || ' : '
6115                             || l_api_message
6116                            );
6117          END IF;
6118 
6119          RAISE fnd_api.g_exc_unexpected_error;
6120       END IF;
6121 
6122       IF (l_trx_flow_exists_flag = 1 AND l_item_id IS NOT NULL)
6123       THEN
6124          l_stmt_num := 160;
6125 
6126          IF     g_debug = 'Y'
6127             AND fnd_log.level_event >= fnd_log.g_current_runtime_level
6128          THEN
6129             fnd_log.STRING (fnd_log.level_event,
6130                             g_log_head || '.' || l_api_name || '.'
6131                             || l_stmt_num,
6132                             'Inserting transactions into MMT'
6133                            );
6134          END IF;
6135 
6136          insert_mmt (p_api_version          => 1.0,
6137                      x_return_status        => l_return_status,
6138                      x_msg_count            => l_msg_count,
6139                      x_msg_data             => l_msg_data,
6140                      p_rcv_accttxn_tbl      => l_rcv_accttxn_tbl
6141                     );
6142 
6143          IF l_return_status <> fnd_api.g_ret_sts_success
6144          THEN
6145             l_api_message := 'Error inserting transactions into MMT';
6146 
6147             IF     g_debug = 'Y'
6148                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
6149             THEN
6150                fnd_log.STRING (fnd_log.level_unexpected,
6151                                g_log_head || '.' || l_api_name || l_stmt_num,
6152                                   'Create_ReceiveEvents : '
6153                                || l_stmt_num
6154                                || ' : '
6155                                || l_api_message
6156                               );
6157             END IF;
6158 
6159             RAISE fnd_api.g_exc_unexpected_error;
6160          END IF;
6161       END IF;
6162 
6163       --- Standard check of p_commit
6164       IF fnd_api.to_boolean (p_commit)
6165       THEN
6166          COMMIT WORK;
6167       END IF;
6168 
6169       -- Standard Call to get message count and if count = 1, get message info
6170       fnd_msg_pub.count_and_get (p_count      => x_msg_count,
6171                                  p_data       => x_msg_data);
6172 
6173       IF     g_debug = 'Y'
6174          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
6175       THEN
6176          fnd_log.STRING (fnd_log.level_procedure,
6177                          g_log_head || '.' || l_api_name || '.end',
6178                          'Create_ReceiveEvents >>'
6179                         );
6180       END IF;
6181    EXCEPTION
6182       WHEN fnd_api.g_exc_error
6183       THEN
6184          ROLLBACK TO create_receiveevents_pvt;
6185          x_return_status := fnd_api.g_ret_sts_error;
6186          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
6187                                     p_data       => x_msg_data
6188                                    );
6189       WHEN fnd_api.g_exc_unexpected_error
6190       THEN
6191          ROLLBACK TO create_receiveevents_pvt;
6192          x_return_status := fnd_api.g_ret_sts_unexp_error;
6193          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
6194                                     p_data       => x_msg_data
6195                                    );
6196       WHEN OTHERS
6197       THEN
6198          ROLLBACK TO create_receiveevents_pvt;
6199          x_return_status := fnd_api.g_ret_sts_unexp_error;
6200 
6201          IF     g_debug = 'Y'
6202             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
6203          THEN
6204             fnd_log.STRING (fnd_log.level_unexpected,
6205                             g_log_head || '.' || l_api_name || l_stmt_num,
6206                                'Create_ReceiveEvents : '
6207                             || l_stmt_num
6208                             || ' : '
6209                             || SUBSTR (SQLERRM, 1, 200)
6210                            );
6211          END IF;
6212 
6213          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
6214          THEN
6215             fnd_msg_pub.add_exc_msg (g_pkg_name,
6216                                         l_api_name
6217                                      || 'Statement -'
6218                                      || TO_CHAR (l_stmt_num)
6219                                     );
6220          END IF;
6221 
6222          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
6223                                     p_data       => x_msg_data
6224                                    );
6225    END create_receive_txns;
6226 
6227    PROCEDURE create_deliver_txns (
6228       p_api_version            IN              NUMBER,
6229       p_init_msg_list          IN              VARCHAR2 := fnd_api.g_false,
6230       p_commit                 IN              VARCHAR2 := fnd_api.g_false,
6231       p_validation_level       IN              NUMBER
6232             := fnd_api.g_valid_level_full,
6233       x_return_status          OUT NOCOPY      VARCHAR2,
6234       x_msg_count              OUT NOCOPY      NUMBER,
6235       x_msg_data               OUT NOCOPY      VARCHAR2,
6236       p_rcv_transaction_id     IN              NUMBER,
6237       p_direct_delivery_flag   IN              VARCHAR2,
6238       p_gl_group_id            IN              NUMBER
6239    )
6240    IS
6241       c_log_module            CONSTANT VARCHAR2 (80)
6242                                            := module || 'Create_Deliver_Txns';
6243       l_return_status                  VARCHAR2 (1)
6244                                                  := fnd_api.g_ret_sts_success;
6245       l_msg_count                      NUMBER                            := 0;
6246       l_msg_data                       VARCHAR2 (8000)                  := '';
6247       l_stmt_num                       NUMBER                            := 0;
6248       l_api_message                    VARCHAR2 (1000);
6249       l_api_name              CONSTANT VARCHAR2 (30) := 'Create_Deliver_Txns';
6250       l_rcv_accttxn                    gmf_rcv_accounting_pkg.rcv_accttxn_rec_type;
6251       l_rcv_accttxn_tbl                gmf_rcv_accounting_pkg.rcv_accttxn_tbl_type;
6252       l_event_type_id                  NUMBER;
6253       l_transaction_flows_tbl          inv_transaction_flow_pub.g_transaction_flow_tbl_type;
6254       l_transaction_reverse_flow_rec   inv_transaction_flow_pub.mtl_transaction_flow_rec_type
6255                                                                       := NULL;
6256       l_trx_flow_exists_flag           NUMBER                            := 0;
6257       l_trx_flow_ctr                   NUMBER                            := 0;
6258       l_po_header_id                   NUMBER;
6259       l_po_distribution_id             NUMBER;
6260       l_po_org_id                      NUMBER;
6261       l_po_ledger_id                   NUMBER;
6262       l_rcv_organization_id            NUMBER;
6263       l_rcv_org_id                     NUMBER;
6264       l_transfer_org_id                NUMBER                         := NULL;
6265       l_transfer_organization_id       NUMBER                         := NULL;
6266       l_rcv_ledger_id                  NUMBER;
6267       l_rcv_trx_date                   DATE;
6268       l_drop_ship_flag                 NUMBER;
6269       l_destination_type               VARCHAR (25);
6270       l_category_id                    NUMBER;
6271       l_project_id                     NUMBER;
6272       l_cross_ou_flag                  VARCHAR2 (1)                    := 'N';
6273       l_accrual_flag                   VARCHAR2 (1)                    := 'N';
6274       l_procurement_org_flag           VARCHAR2 (1)                    := 'Y';
6275       l_trx_flow_header_id             NUMBER;
6276       l_qualifier_code_tbl             inv_transaction_flow_pub.number_tbl;
6277       l_qualifier_value_tbl            inv_transaction_flow_pub.number_tbl;
6278       l_encumbrance_flag               VARCHAR2 (1);
6279       l_ussgl_option                   VARCHAR2 (1);
6280       l_rcv_quantity                   NUMBER := 0;   /* Bug 8517463 */
6281       l_rcv_transaction_type           VARCHAR2(25);  /* Bug 8517463 */
6282 
6283    BEGIN
6284       -- Standard start of API savepoint
6285       SAVEPOINT create_deliver_txns;
6286       l_stmt_num := 0;
6287 
6288       IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level)
6289       THEN
6290          fnd_log.STRING (fnd_log.level_procedure, c_log_module, 'Begin...');
6291       END IF;
6292 
6293       -- Initialize API return status to success
6294       x_return_status := fnd_api.g_ret_sts_success;
6295       -- Unlike for Receive transactions, for Deliver transactions, the po_distribution_id
6296       -- is always available.
6297       l_stmt_num := 20;
6298 
6299       SELECT rt.po_header_id, rt.po_distribution_id,
6300              pod.destination_type_code, rt.transaction_date,
6301              NVL (rt.dropship_type_code, 3), poh.org_id,
6302              poll.ship_to_organization_id, pol.category_id, pol.project_id,
6303              NVL (poll.accrue_on_receipt_flag, 'N')
6304         INTO l_po_header_id, l_po_distribution_id,
6305              l_destination_type, l_rcv_trx_date,
6306              l_drop_ship_flag, l_po_org_id,
6307              l_rcv_organization_id, l_category_id, l_project_id,
6308              l_accrual_flag
6309         FROM po_headers poh,
6310              po_line_locations poll,
6311              po_lines pol,
6312              po_distributions pod,
6313              rcv_transactions rt
6314        WHERE rt.transaction_id = p_rcv_transaction_id
6315          AND poh.po_header_id = rt.po_header_id
6316          AND poll.line_location_id = rt.po_line_location_id
6317          AND pol.po_line_id = rt.po_line_id
6318          AND pod.po_distribution_id = rt.po_distribution_id;
6319 
6320       l_stmt_num := 30;
6321 
6322       -- Get Receiving Operating Unit and SOB
6323       SELECT operating_unit, set_of_books_id
6324         INTO l_rcv_org_id, l_rcv_ledger_id
6325         FROM org_organization_definitions
6326        WHERE organization_id = l_rcv_organization_id;
6327 
6328       l_stmt_num := 35;
6329 
6330       -- Get PO SOB
6331       SELECT set_of_books_id
6332         INTO l_po_ledger_id
6333         FROM financials_system_parameters;
6334 
6335       IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)
6336       THEN
6337          l_api_message :=
6338                'Creating Deliver transactions : RCV Transaction ID : '
6339             || p_rcv_transaction_id
6340             || ', PO Header ID : '
6341             || l_po_header_id
6342             || ', PO Dist ID : '
6343             || l_po_distribution_id
6344             || ', Destination Type : '
6345             || l_destination_type
6346             || ', Transaction Date : '
6347             || l_rcv_trx_date
6348             || ', Drop Ship Flag : '
6349             || l_drop_ship_flag
6350             || ', PO Org ID : '
6351             || l_po_org_id
6352             || ', RCV Organization ID : '
6353             || l_rcv_organization_id
6354             || ', RCV Org ID : '
6355             || l_rcv_org_id
6356             || ', Project ID : '
6357             || l_project_id
6358             || ', Category ID : '
6359             || l_category_id
6360             || ', Accrual Flag : '
6361             || l_accrual_flag;
6362          fnd_log.STRING (fnd_log.level_statement,
6363                          c_log_module || '.' || l_stmt_num,
6364                          l_api_message
6365                         );
6366       END IF;
6367 
6368       -- Only create transactions for Deliver transactions for expense destination types. Other
6369       -- destination types do not have any accounting implications in the Receiving sub-ledger.
6370       IF (l_destination_type <> 'EXPENSE')
6371       THEN
6372          RETURN;
6373       END IF;
6374 
6375       IF (l_po_org_id <> l_rcv_org_id)
6376       THEN
6377          l_cross_ou_flag := 'Y';
6378       END IF;
6379 
6380       -- Get transaction flow when procuring and receiving operating units are different.
6381       -- However, for POs with destination type of expense, when there is a project on the
6382       -- PO, we should not look for transaction flow. This is because PA,(which transfers
6383       -- costs to Projects for expense destinations), is currently not supporting global
6384       -- procurement.
6385       IF (l_cross_ou_flag = 'Y' AND l_project_id IS NULL)
6386       THEN
6387          /* For 11i10, the only supported qualifier is category id. */
6388          l_qualifier_code_tbl (l_qualifier_code_tbl.COUNT + 1) :=
6389                                     inv_transaction_flow_pub.g_qualifier_code;
6390          l_qualifier_value_tbl (l_qualifier_value_tbl.COUNT + 1) :=
6391                                                                 l_category_id;
6392 
6393          IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)
6394          THEN
6395             l_api_message :=
6396                   'Getting Procurement Transaction Flow :'
6397                || 'l_po_org_id : '
6398                || l_po_org_id
6399                || ' l_rcv_org_id : '
6400                || l_rcv_org_id
6401                || ' l_rcv_organization_id : '
6402                || l_rcv_organization_id;
6403             fnd_log.STRING (fnd_log.level_statement,
6404                             c_log_module || '.' || l_stmt_num,
6405                             l_api_message
6406                            );
6407          END IF;
6408 
6409          inv_transaction_flow_pub.get_transaction_flow
6410                (x_return_status               => l_return_status,
6411                 x_msg_data                    => l_msg_data,
6412                 x_msg_count                   => l_msg_count,
6413                 x_transaction_flows_tbl       => l_transaction_flows_tbl,
6414                 p_api_version                 => 1.0,
6415                 p_start_operating_unit        => l_po_org_id,
6416                 p_end_operating_unit          => l_rcv_org_id,
6417                 p_flow_type                   => inv_transaction_flow_pub.g_procuring_flow_type,
6418                 p_organization_id             => l_rcv_organization_id,
6419                 p_qualifier_code_tbl          => l_qualifier_code_tbl,
6420                 p_qualifier_value_tbl         => l_qualifier_value_tbl,
6421                 p_transaction_date            => l_rcv_trx_date,
6422                 p_get_default_cost_group      => 'N'
6423                );
6424 
6425          IF (l_return_status = fnd_api.g_ret_sts_success)
6426          THEN
6427             l_procurement_org_flag := 'N';
6428             l_trx_flow_exists_flag := 1;
6429             l_trx_flow_header_id :=
6430                l_transaction_flows_tbl (l_transaction_flows_tbl.FIRST).header_id;
6431             l_trx_flow_ctr := l_transaction_flows_tbl.COUNT;
6432             l_transaction_reverse_flow_rec :=
6433                                      l_transaction_flows_tbl (l_trx_flow_ctr);
6434             l_transfer_org_id := l_transaction_reverse_flow_rec.from_org_id;
6435             l_transfer_organization_id :=
6436                           l_transaction_reverse_flow_rec.from_organization_id;
6437          ELSIF (l_return_status = 'W')
6438          THEN
6439             l_trx_flow_exists_flag := 0;
6440 
6441             IF (fnd_log.level_event >= fnd_log.g_current_runtime_level)
6442             THEN
6443                fnd_log.STRING (fnd_log.level_event,
6444                                c_log_module || '.' || l_stmt_num,
6445                                'Transaction Flow does not exist'
6446                               );
6447             END IF;
6448 
6449             -- If transaction flow does not exist, but the PO crosses multiple
6450             -- sets of books, error out the transaction.
6451             IF (l_po_ledger_id <> l_rcv_ledger_id)
6452             THEN
6453                l_api_message := 'Transaction Flow does not exist';
6454 
6455                IF (fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
6456                   )
6457                THEN
6458                   fnd_log.STRING (fnd_log.level_unexpected,
6459                                   c_log_module || '.' || l_stmt_num,
6460                                   l_api_message
6461                                  );
6462                END IF;
6463 
6464                RAISE fnd_api.g_exc_unexpected_error;
6465             END IF;
6466          ELSE
6467             l_api_message := 'Error occurred in Transaction Flow API';
6468 
6469             IF     g_debug = 'Y'
6470                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
6471             THEN
6472                fnd_log.STRING (fnd_log.level_unexpected,
6473                                g_log_head || '.' || l_api_name || l_stmt_num,
6474                                   'Create_DeliverEvents : '
6475                                || l_stmt_num
6476                                || ' : '
6477                                || l_api_message
6478                               );
6479             END IF;
6480 
6481             RAISE fnd_api.g_exc_unexpected_error;
6482          END IF;                                          -- End if ret_status
6483       END IF;                                         -- End cross_ou_flag = Y
6484 
6485       l_stmt_num := 50;
6486 
6487     /* Begin Bug 8517463 */
6488 
6489       /* Following SELECT commented and substituted with subsequent code */
6490       /*
6491       SELECT DECODE (rt.transaction_type, 'CORRECT', correct, deliver)
6492         INTO l_event_type_id
6493         FROM rcv_transactions rt
6494        WHERE transaction_id = p_rcv_transaction_id;
6495       */
6496 
6497       SELECT NVL(quantity,0), transaction_type
6498       INTO   l_rcv_quantity, l_rcv_transaction_type
6499       FROM   rcv_transactions rt
6500       WHERE  transaction_id = p_rcv_transaction_id;
6501 
6502       IF l_rcv_transaction_type = 'CORRECT' THEN
6503          IF l_rcv_quantity < 0 THEN
6504            l_event_type_id := return_to_receiving;
6505          ELSIF l_rcv_quantity > 0 THEN
6506            l_event_type_id := deliver;
6507          ELSE
6508            l_event_type_id := correct;
6509          END IF;
6510       ELSE
6511          l_event_type_id := deliver;
6512       END IF;
6513 
6514     /* End Bug 8517463 */
6515 
6516 
6517       l_stmt_num := 60;
6518 
6519       IF (fnd_log.level_event >= fnd_log.g_current_runtime_level)
6520       THEN
6521          fnd_log.STRING (fnd_log.level_event,
6522                          c_log_module || '.' || l_stmt_num,
6523                          'Seeding Deliver Txn'
6524                         );
6525       END IF;
6526 
6527       insert_txn
6528             (p_api_version                       => 1.0,
6529              x_return_status                     => l_return_status,
6530              x_msg_count                         => l_msg_count,
6531              x_msg_data                          => l_msg_data,
6532              p_event_source                      => 'RECEIVING',
6533              p_event_type_id                     => l_event_type_id,
6534              p_rcv_transaction_id                => p_rcv_transaction_id,
6535              p_inv_distribution_id               => NULL,
6536              p_po_distribution_id                => l_po_distribution_id,
6537              p_direct_delivery_flag              => p_direct_delivery_flag,
6538              p_gl_group_id                       => p_gl_group_id,
6539              p_cross_ou_flag                     => l_cross_ou_flag,
6540              p_procurement_org_flag              => l_procurement_org_flag,
6541              p_ship_to_org_flag                  => 'Y',
6542              p_drop_ship_flag                    => l_drop_ship_flag,
6543              p_org_id                            => l_rcv_org_id,
6544              p_organization_id                   => l_rcv_organization_id,
6545              p_transfer_org_id                   => l_transfer_org_id,
6546              p_transfer_organization_id          => l_transfer_organization_id,
6547              p_trx_flow_header_id                => l_trx_flow_header_id,
6548              p_transaction_forward_flow_rec      => NULL,
6549              p_transaction_reverse_flow_rec      => l_transaction_reverse_flow_rec,
6550              p_unit_price                        => NULL,
6551              p_prior_unit_price                  => NULL,
6552              x_rcv_accttxn                       => l_rcv_accttxn
6553             );
6554 
6555       /* Begin Bug 8517463 */
6556       IF ( l_rcv_transaction_type = 'CORRECT' AND
6557            l_event_type_id = return_to_receiving ) THEN
6558          l_rcv_accttxn.transaction_quantity := -1*l_rcv_accttxn.transaction_quantity;
6559          l_rcv_accttxn.primary_quantity     := -1*l_rcv_accttxn.primary_quantity;
6560          l_rcv_accttxn.source_doc_quantity  := -1*l_rcv_accttxn.source_doc_quantity;
6561       END IF;
6562       /* End Bug 8517463 */
6563 
6564       IF l_return_status <> fnd_api.g_ret_sts_success
6565       THEN
6566          l_api_message := 'Error creating Txn';
6567 
6568          IF (fnd_log.level_unexpected >= fnd_log.g_current_runtime_level)
6569          THEN
6570             fnd_log.STRING (fnd_log.level_unexpected,
6571                             c_log_module || '.' || l_stmt_num,
6572                             l_api_message
6573                            );
6574          END IF;
6575 
6576          RAISE fnd_api.g_exc_unexpected_error;
6577       END IF;
6578 
6579       l_rcv_accttxn_tbl (l_rcv_accttxn_tbl.COUNT + 1) := l_rcv_accttxn;
6580 
6581       -- Encumbrance cannot be enabled for global procurement scenarios.
6582       IF l_trx_flow_exists_flag = 0
6583       THEN
6584          l_stmt_num := 70;
6585 
6586          IF (fnd_log.level_event >= fnd_log.g_current_runtime_level)
6587          THEN
6588             fnd_log.STRING (fnd_log.level_event,
6589                             c_log_module || '.' || l_stmt_num,
6590                             'Checking if encumbrance txns need to be created'
6591                            );
6592          END IF;
6593 
6594          -- Reuse existing check
6595          check_encumbranceflag (p_api_version           => 1.0,
6596                                 x_return_status         => l_return_status,
6597                                 x_msg_count             => l_msg_count,
6598                                 x_msg_data              => l_msg_data,
6599                                 p_rcv_ledger_id         => l_rcv_ledger_id,
6600                                 x_encumbrance_flag      => l_encumbrance_flag,
6601                                 x_ussgl_option          => l_ussgl_option
6602                                );
6603 
6604          IF l_return_status <> fnd_api.g_ret_sts_success
6605          THEN
6606             l_api_message := 'Error in checking for encumbrance flag ';
6607 
6608             IF (fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
6609                )
6610             THEN
6611                fnd_log.STRING (fnd_log.level_unexpected,
6612                                c_log_module || '.' || l_stmt_num,
6613                                l_api_message
6614                               );
6615             END IF;
6616 
6617             RAISE fnd_api.g_exc_unexpected_error;
6618          END IF;
6619 
6620          IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)
6621          THEN
6622             l_api_message := 'Encumbrance Flag : ' || l_encumbrance_flag;
6623             fnd_log.STRING (fnd_log.level_statement,
6624                             c_log_module || '.' || l_stmt_num,
6625                             l_api_message
6626                            );
6627          END IF;
6628 
6629          IF (l_encumbrance_flag = 'Y')
6630          THEN
6631             l_stmt_num := 80;
6632 
6633             IF (fnd_log.level_event >= fnd_log.g_current_runtime_level)
6634             THEN
6635                fnd_log.STRING (fnd_log.level_event,
6636                                c_log_module || '.' || l_stmt_num,
6637                                'Seeding Encumbrance Reversal Txn'
6638                               );
6639             END IF;
6640 
6641             insert_txn
6642                (p_api_version                       => 1.0,
6643                 x_return_status                     => l_return_status,
6644                 x_msg_count                         => l_msg_count,
6645                 x_msg_data                          => l_msg_data,
6646                 p_event_source                      => 'RECEIVING',
6647                 p_event_type_id                     => encumbrance_reversal,
6648                 p_rcv_transaction_id                => p_rcv_transaction_id,
6649                 p_inv_distribution_id               => NULL,
6650                 p_po_distribution_id                => l_po_distribution_id,
6651                 p_direct_delivery_flag              => p_direct_delivery_flag,
6652                 p_gl_group_id                       => p_gl_group_id,
6653                 p_cross_ou_flag                     => l_cross_ou_flag,
6654                 p_procurement_org_flag              => l_procurement_org_flag,
6655                 p_ship_to_org_flag                  => 'Y',
6656                 p_drop_ship_flag                    => l_drop_ship_flag,
6657                 p_org_id                            => l_rcv_org_id,
6658                 p_organization_id                   => l_rcv_organization_id,
6659                 p_transfer_org_id                   => NULL,
6660                 p_transfer_organization_id          => NULL,
6661                 p_trx_flow_header_id                => NULL,
6662                 p_transaction_forward_flow_rec      => NULL,
6663                 p_transaction_reverse_flow_rec      => l_transaction_reverse_flow_rec,
6664                 p_unit_price                        => NULL,
6665                 p_prior_unit_price                  => NULL,
6666                 x_rcv_accttxn                       => l_rcv_accttxn
6667                );
6668 
6669              /**
6670             Bug #3333610. In the case of encumbrance reversals, the quantity to unencumber
6671             may turn out to be zero if the quantity delivered is greater than the quantity
6672             ordered. In such a situation, we should not error out the event.
6673             **/
6674             IF l_return_status = fnd_api.g_ret_sts_success
6675             THEN
6676                l_rcv_accttxn_tbl (l_rcv_accttxn_tbl.COUNT + 1) :=
6677                                                                 l_rcv_accttxn;
6678             ELSIF l_return_status <> 'W'
6679             THEN
6680                l_api_message := 'Error in seeding encumbrance reversal event';
6681 
6682                IF (fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
6683                   )
6684                THEN
6685                   fnd_log.STRING (fnd_log.level_unexpected,
6686                                   c_log_module || '.' || l_stmt_num,
6687                                   l_api_message
6688                                  );
6689                END IF;
6690 
6691                RAISE fnd_api.g_exc_unexpected_error;
6692             END IF;
6693          END IF;                                      -- end if encum_flag = y
6694       END IF;                               -- end if trx_flow_exists_flag = 0
6695 
6696       l_stmt_num := 90;
6697 
6698       IF (fnd_log.level_event >= fnd_log.g_current_runtime_level)
6699       THEN
6700          fnd_log.STRING (fnd_log.level_event,
6701                          c_log_module || '.' || l_stmt_num,
6702                          'Inserting txns into GRAT'
6703                         );
6704       END IF;
6705 
6706       insert_txn2 (x_return_status        => l_return_status,
6707                    x_msg_count            => l_msg_count,
6708                    x_msg_data             => l_msg_data,
6709                    p_rcv_accttxn_tbl      => l_rcv_accttxn_tbl
6710                   );
6711 
6712       IF l_return_status <> fnd_api.g_ret_sts_success
6713       THEN
6714          l_api_message := 'Error inserting txns into GRAT';
6715 
6716          IF (fnd_log.level_unexpected >= fnd_log.g_current_runtime_level)
6717          THEN
6718             fnd_log.STRING (fnd_log.level_unexpected,
6719                             c_log_module || l_stmt_num,
6720                             l_api_message
6721                            );
6722          END IF;
6723 
6724          RAISE fnd_api.g_exc_unexpected_error;
6725       END IF;
6726 
6727       IF fnd_api.to_boolean (p_commit)
6728       THEN
6729          COMMIT WORK;
6730       END IF;
6731 
6732       -- Standard Call to get message count and if count = 1, get message info
6733       fnd_msg_pub.count_and_get (p_count      => x_msg_count,
6734                                  p_data       => x_msg_data);
6735 
6736       IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level)
6737       THEN
6738          fnd_log.STRING (fnd_log.level_procedure, c_log_module, '...End');
6739       END IF;
6740    EXCEPTION
6741       WHEN fnd_api.g_exc_error
6742       THEN
6743          ROLLBACK TO create_deliver_txns;
6744          x_return_status := fnd_api.g_ret_sts_error;
6745          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
6746                                     p_data       => x_msg_data
6747                                    );
6748       WHEN fnd_api.g_exc_unexpected_error
6749       THEN
6750          ROLLBACK TO create_deliver_txns;
6751          x_return_status := fnd_api.g_ret_sts_unexp_error;
6752          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
6753                                     p_data       => x_msg_data
6754                                    );
6755       WHEN OTHERS
6756       THEN
6757          ROLLBACK TO create_deliver_txns;
6758          x_return_status := fnd_api.g_ret_sts_unexp_error;
6759 
6760          IF (fnd_log.level_unexpected >= fnd_log.g_current_runtime_level)
6761          THEN
6762             fnd_log.STRING (fnd_log.level_unexpected,
6763                             c_log_module || '.' || l_stmt_num,
6764                             SUBSTR (SQLERRM, 1, 200)
6765                            );
6766          END IF;
6767 
6768          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
6769          THEN
6770             fnd_msg_pub.add_exc_msg (g_pkg_name,
6771                                         l_api_name
6772                                      || 'Statement -'
6773                                      || TO_CHAR (l_stmt_num)
6774                                     );
6775          END IF;
6776 
6777          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
6778                                     p_data       => x_msg_data
6779                                    );
6780    END create_deliver_txns;
6781 
6782    PROCEDURE create_rtr_txns (
6783       p_api_version            IN              NUMBER := 1.0,
6784       p_init_msg_list          IN              VARCHAR2 := fnd_api.g_false,
6785       p_commit                 IN              VARCHAR2 := fnd_api.g_false,
6786       p_validation_level       IN              NUMBER
6787             := fnd_api.g_valid_level_full,
6788       x_return_status          OUT NOCOPY      VARCHAR2,
6789       x_msg_count              OUT NOCOPY      NUMBER,
6790       x_msg_data               OUT NOCOPY      VARCHAR2,
6791       p_rcv_transaction_id     IN              NUMBER,
6792       p_direct_delivery_flag   IN              VARCHAR2,
6793       p_gl_group_id            IN              NUMBER
6794    )
6795    IS
6796       l_api_name              CONSTANT VARCHAR2 (30)     := 'Create_RTR_Txns';
6797       l_api_version           CONSTANT NUMBER                          := 1.0;
6798       l_return_status                  VARCHAR2 (1)
6799                                                  := fnd_api.g_ret_sts_success;
6800       l_msg_count                      NUMBER                            := 0;
6801       l_msg_data                       VARCHAR2 (8000)                  := '';
6802       l_stmt_num                       NUMBER                            := 0;
6803       l_api_message                    VARCHAR2 (1000);
6804       l_rcv_accttxn                    gmf_rcv_accounting_pkg.rcv_accttxn_rec_type;
6805       l_rcv_accttxn_tbl                gmf_rcv_accounting_pkg.rcv_accttxn_tbl_type;
6806       l_event_type_id                  NUMBER;
6807       l_transaction_flows_tbl          inv_transaction_flow_pub.g_transaction_flow_tbl_type;
6808       l_transaction_reverse_flow_rec   inv_transaction_flow_pub.mtl_transaction_flow_rec_type
6809                                                                       := NULL;
6810       l_trx_flow_exists_flag           NUMBER                            := 0;
6811       l_trx_flow_ctr                   NUMBER                            := 0;
6812       l_po_header_id                   NUMBER;
6813       l_po_distribution_id             NUMBER;
6814       l_po_org_id                      NUMBER;
6815       l_po_ledger_id                   NUMBER;
6816       l_rcv_organization_id            NUMBER;
6817       l_rcv_org_id                     NUMBER;
6818       l_transfer_org_id                NUMBER                         := NULL;
6819       l_transfer_organization_id       NUMBER                         := NULL;
6820       l_rcv_ledger_id                  NUMBER;
6821       l_rcv_trx_date                   DATE;
6822       l_drop_ship_flag                 NUMBER;
6823       l_destination_type               VARCHAR (25);
6824       l_category_id                    NUMBER;
6825       l_project_id                     NUMBER;
6826       l_cross_ou_flag                  VARCHAR2 (1)                    := 'N';
6827       l_accrual_flag                   VARCHAR2 (1)                    := 'N';
6828       l_procurement_org_flag           VARCHAR2 (1)                    := 'Y';
6829       l_trx_flow_header_id             NUMBER;
6830       l_qualifier_code_tbl             inv_transaction_flow_pub.number_tbl;
6831       l_qualifier_value_tbl            inv_transaction_flow_pub.number_tbl;
6832       l_encumbrance_flag               VARCHAR2 (1);
6833       l_ussgl_option                   VARCHAR2 (1);
6834    BEGIN
6835       -- Standard start of API savepoint
6836       SAVEPOINT create_rtrevents_pvt;
6837       l_stmt_num := 0;
6838 
6839       IF     g_debug = 'Y'
6840          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
6841       THEN
6842          fnd_log.STRING (fnd_log.level_procedure,
6843                          g_log_head || '.' || l_api_name || '.begin',
6844                          'Create_RTREvents <<'
6845                         );
6846       END IF;
6847 
6848       -- Standard call to check for call compatibility
6849       IF NOT fnd_api.compatible_api_call (l_api_version,
6850                                           p_api_version,
6851                                           l_api_name,
6852                                           g_pkg_name
6853                                          )
6854       THEN
6855          RAISE fnd_api.g_exc_unexpected_error;
6856       END IF;
6857 
6858       -- Initialize message list if p_init_msg_list is set to TRUE
6859       IF fnd_api.to_boolean (p_init_msg_list)
6860       THEN
6861          fnd_msg_pub.initialize;
6862       END IF;
6863 
6864       -- Initialize API return status to success
6865       x_return_status := fnd_api.g_ret_sts_success;
6866       -- Unlike for RTV transactions, for RTR transactions, the po_distribution_id
6867       -- is always available.
6868       l_stmt_num := 20;
6869 
6870       SELECT rt.po_header_id, rt.po_distribution_id,
6871              pod.destination_type_code, rt.transaction_date,
6872              NVL (rt.dropship_type_code, 3), poh.org_id,
6873              poll.ship_to_organization_id, pol.category_id, pol.project_id,
6874              NVL (poll.accrue_on_receipt_flag, 'N')
6875         INTO l_po_header_id, l_po_distribution_id,
6876              l_destination_type, l_rcv_trx_date,
6877              l_drop_ship_flag, l_po_org_id,
6878              l_rcv_organization_id, l_category_id, l_project_id,
6879              l_accrual_flag
6880         FROM po_headers poh,
6881              po_line_locations poll,
6882              po_lines pol,
6883              po_distributions pod,
6884              rcv_transactions rt
6885        WHERE rt.transaction_id = p_rcv_transaction_id
6886          AND poh.po_header_id = rt.po_header_id
6887          AND poll.line_location_id = rt.po_line_location_id
6888          AND pol.po_line_id = rt.po_line_id
6889          AND pod.po_distribution_id = rt.po_distribution_id;
6890 
6891       l_stmt_num := 30;
6892 
6893       -- Get Receiving Operating Unit and SOB
6894       SELECT operating_unit, set_of_books_id
6895         INTO l_rcv_org_id, l_rcv_ledger_id
6896         FROM cst_organization_definitions cod
6897        WHERE organization_id = l_rcv_organization_id;
6898 
6899       l_stmt_num := 35;
6900 
6901       -- Get PO SOB
6902       SELECT set_of_books_id
6903         INTO l_po_ledger_id
6904         FROM financials_system_parameters;
6905 
6906       IF     g_debug = 'Y'
6907          AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
6908       THEN
6909          l_api_message :=
6910                'Creating RTR transactions : RCV Transaction ID : '
6911             || p_rcv_transaction_id
6912             || ', PO Header ID : '
6913             || l_po_header_id
6914             || ', PO Dist ID : '
6915             || l_po_distribution_id
6916             || ', Destination Type : '
6917             || l_destination_type
6918             || ', Transaction Date : '
6919             || l_rcv_trx_date
6920             || ', Drop Ship Flag : '
6921             || l_drop_ship_flag
6922             || ', PO Org ID : '
6923             || l_po_org_id
6924             || ', PO LEDGER ID : '
6925             || l_po_ledger_id
6926             || ', RCV Organization ID : '
6927             || l_rcv_organization_id
6928             || ', RCV Org ID : '
6929             || l_rcv_org_id
6930             || ', RCV LEDGER ID : '
6931             || l_rcv_ledger_id
6932             || ', Project ID : '
6933             || l_project_id
6934             || ', Category ID : '
6935             || l_category_id
6936             || ', Accrual Flag : '
6937             || l_accrual_flag;
6938          fnd_log.STRING (fnd_log.level_statement,
6939                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
6940                          l_api_message
6941                         );
6942       END IF;
6943 
6944       -- Only create transactions for RTR transactions for expense destination types. Other
6945       -- destination types do not have any accounting implications in the Receiving sub-ledger.
6946       IF (l_destination_type <> 'EXPENSE')
6947       THEN
6948          RETURN;
6949       END IF;
6950 
6951       IF (l_po_org_id <> l_rcv_org_id)
6952       THEN
6953          l_cross_ou_flag := 'Y';
6954       END IF;
6955 
6956       -- Get transaction flow when procuring and receiving operating units are different.
6957       -- However, for POs with destination type of expense, when there is a project on the
6958       -- PO, we should not look for transaction flow. This is because PA,(which transfers
6959       -- costs to Projects for expense destinations), is currently not supporting global
6960       -- procurement.
6961       IF (l_cross_ou_flag = 'Y' AND l_project_id IS NULL)
6962       THEN
6963          /* For 11i10, the only supported qualifier is category id. */
6964          l_qualifier_code_tbl (l_qualifier_code_tbl.COUNT + 1) :=
6965                                     inv_transaction_flow_pub.g_qualifier_code;
6966          l_qualifier_value_tbl (l_qualifier_value_tbl.COUNT + 1) :=
6967                                                                 l_category_id;
6968          l_stmt_num := 40;
6969 
6970          IF     g_debug = 'Y'
6971             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
6972          THEN
6973             l_api_message :=
6974                   'Getting Procurement Transaction Flow :'
6975                || 'l_po_org_id : '
6976                || l_po_org_id
6977                || ' l_rcv_org_id : '
6978                || l_rcv_org_id
6979                || ' l_rcv_organization_id : '
6980                || l_rcv_organization_id;
6981             fnd_log.STRING (fnd_log.level_statement,
6982                             g_log_head || '.' || l_api_name || '.'
6983                             || l_stmt_num,
6984                             l_api_message
6985                            );
6986          END IF;
6987 
6988          inv_transaction_flow_pub.get_transaction_flow
6989                (x_return_status               => l_return_status,
6990                 x_msg_data                    => l_msg_data,
6991                 x_msg_count                   => l_msg_count,
6992                 x_transaction_flows_tbl       => l_transaction_flows_tbl,
6993                 p_api_version                 => 1.0,
6994                 p_start_operating_unit        => l_po_org_id,
6995                 p_end_operating_unit          => l_rcv_org_id,
6996                 p_flow_type                   => inv_transaction_flow_pub.g_procuring_flow_type,
6997                 p_organization_id             => l_rcv_organization_id,
6998                 p_qualifier_code_tbl          => l_qualifier_code_tbl,
6999                 p_qualifier_value_tbl         => l_qualifier_value_tbl,
7000                 p_transaction_date            => l_rcv_trx_date,
7001                 p_get_default_cost_group      => 'N'
7002                );
7003 
7004          IF (l_return_status = fnd_api.g_ret_sts_success)
7005          THEN
7006             l_procurement_org_flag := 'N';
7007             l_trx_flow_exists_flag := 1;
7008             l_trx_flow_header_id :=
7009                l_transaction_flows_tbl (l_transaction_flows_tbl.FIRST).header_id;
7010             l_trx_flow_ctr := l_transaction_flows_tbl.COUNT;
7011             l_transaction_reverse_flow_rec :=
7012                                      l_transaction_flows_tbl (l_trx_flow_ctr);
7013             l_transfer_org_id := l_transaction_reverse_flow_rec.from_org_id;
7014             l_transfer_organization_id :=
7015                           l_transaction_reverse_flow_rec.from_organization_id;
7016          ELSIF (l_return_status = 'W')
7017          THEN
7018             l_trx_flow_exists_flag := 0;
7019 
7020             IF     g_debug = 'Y'
7021                AND fnd_log.level_event >= fnd_log.g_current_runtime_level
7022             THEN
7023                fnd_log.STRING (fnd_log.level_event,
7024                                   g_log_head
7025                                || '.'
7026                                || l_api_name
7027                                || '.'
7028                                || l_stmt_num,
7029                                'Transaction Flow does not exist'
7030                               );
7031             END IF;
7032 
7033             -- If transaction flow does not exist, but the PO crosses multiple
7034             -- sets of books, error out the transaction.
7035             IF (l_po_ledger_id <> l_rcv_ledger_id)
7036             THEN
7037                l_api_message := 'Transaction Flow does not exist';
7038 
7039                IF     g_debug = 'Y'
7040                   AND fnd_log.level_unexpected >=
7041                                                fnd_log.g_current_runtime_level
7042                THEN
7043                   fnd_log.STRING (fnd_log.level_unexpected,
7044                                   g_log_head || '.' || l_api_name
7045                                   || l_stmt_num,
7046                                      'Create_RTREvents : '
7047                                   || l_stmt_num
7048                                   || ' : '
7049                                   || l_api_message
7050                                  );
7051                END IF;
7052 
7053                RAISE fnd_api.g_exc_unexpected_error;
7054             END IF;
7055          ELSE
7056             l_api_message := 'Error occurred in Transaction Flow API';
7057 
7058             IF     g_debug = 'Y'
7059                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
7060             THEN
7061                fnd_log.STRING (fnd_log.level_unexpected,
7062                                g_log_head || '.' || l_api_name || l_stmt_num,
7063                                   'Create_RTREvents : '
7064                                || l_stmt_num
7065                                || ' : '
7066                                || l_api_message
7067                               );
7068             END IF;
7069 
7070             RAISE fnd_api.g_exc_unexpected_error;
7071          END IF;
7072       END IF;
7073 
7074       l_stmt_num := 50;
7075 
7076       SELECT DECODE (rt.transaction_type,
7077                      'CORRECT', correct,
7078                      return_to_receiving
7079                     )
7080         INTO l_event_type_id
7081         FROM rcv_transactions rt
7082        WHERE transaction_id = p_rcv_transaction_id;
7083 
7084       l_stmt_num := 60;
7085 
7086       IF     g_debug = 'Y'
7087          AND fnd_log.level_event >= fnd_log.g_current_runtime_level
7088       THEN
7089          fnd_log.STRING (fnd_log.level_event,
7090                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
7091                          'Seeding RTR Event'
7092                         );
7093       END IF;
7094 
7095       insert_txn
7096             (p_api_version                       => 1.0,
7097              x_return_status                     => l_return_status,
7098              x_msg_count                         => l_msg_count,
7099              x_msg_data                          => l_msg_data,
7100              p_event_source                      => 'RECEIVING',
7101              p_event_type_id                     => l_event_type_id,
7102              p_rcv_transaction_id                => p_rcv_transaction_id,
7103              p_inv_distribution_id               => NULL,
7104              p_po_distribution_id                => l_po_distribution_id,
7105              p_direct_delivery_flag              => p_direct_delivery_flag,
7106              p_gl_group_id                       => p_gl_group_id,
7107              p_cross_ou_flag                     => l_cross_ou_flag,
7108              p_procurement_org_flag              => l_procurement_org_flag,
7109              p_ship_to_org_flag                  => 'Y',
7110              p_drop_ship_flag                    => l_drop_ship_flag,
7111              p_org_id                            => l_rcv_org_id,
7112              p_organization_id                   => l_rcv_organization_id,
7113              p_transfer_org_id                   => l_transfer_org_id,
7114              p_transfer_organization_id          => l_transfer_organization_id,
7115              p_trx_flow_header_id                => l_trx_flow_header_id,
7116              p_transaction_forward_flow_rec      => NULL,
7117              p_transaction_reverse_flow_rec      => l_transaction_reverse_flow_rec,
7118              p_unit_price                        => NULL,
7119              p_prior_unit_price                  => NULL,
7120              x_rcv_accttxn                       => l_rcv_accttxn
7121             );
7122       l_rcv_accttxn_tbl (l_rcv_accttxn_tbl.COUNT + 1) := l_rcv_accttxn;
7123 
7124       IF l_return_status <> fnd_api.g_ret_sts_success
7125       THEN
7126          l_api_message := 'Error creating event';
7127 
7128          IF     g_debug = 'Y'
7129             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
7130          THEN
7131             fnd_log.STRING (fnd_log.level_unexpected,
7132                             g_log_head || '.' || l_api_name || l_stmt_num,
7133                                'Create_RTREvents : '
7134                             || l_stmt_num
7135                             || ' : '
7136                             || l_api_message
7137                            );
7138          END IF;
7139 
7140          RAISE fnd_api.g_exc_unexpected_error;
7141       END IF;
7142 
7143       IF l_trx_flow_exists_flag = 0
7144       THEN
7145          l_stmt_num := 70;
7146 
7147          IF     g_debug = 'Y'
7148             AND fnd_log.level_event >= fnd_log.g_current_runtime_level
7149          THEN
7150             fnd_log.STRING
7151                    (fnd_log.level_event,
7152                     g_log_head || '.' || l_api_name || '.' || l_stmt_num,
7153                     'Checking if encumbrance transactions need to be seeded.'
7154                    );
7155          END IF;
7156 
7157          check_encumbranceflag (p_api_version           => 1.0,
7158                                 x_return_status         => l_return_status,
7159                                 x_msg_count             => l_msg_count,
7160                                 x_msg_data              => l_msg_data,
7161                                 p_rcv_ledger_id         => l_rcv_ledger_id,
7162                                 x_encumbrance_flag      => l_encumbrance_flag,
7163                                 x_ussgl_option          => l_ussgl_option
7164                                );
7165 
7166          IF l_return_status <> fnd_api.g_ret_sts_success
7167          THEN
7168             l_api_message := 'Error in checking for encumbrance flag ';
7169 
7170             IF     g_debug = 'Y'
7171                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
7172             THEN
7173                fnd_log.STRING (fnd_log.level_unexpected,
7174                                g_log_head || '.' || l_api_name || l_stmt_num,
7175                                   'Create_RTREvents : '
7176                                || l_stmt_num
7177                                || ' : '
7178                                || l_api_message
7179                               );
7180             END IF;
7181 
7182             RAISE fnd_api.g_exc_unexpected_error;
7183          END IF;
7184 
7185          IF     g_debug = 'Y'
7186             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
7187          THEN
7188             l_api_message := 'Encumbrance Flag : ' || l_encumbrance_flag;
7189             fnd_log.STRING (fnd_log.level_statement,
7190                             g_log_head || '.' || l_api_name || '.'
7191                             || l_stmt_num,
7192                             l_api_message
7193                            );
7194          END IF;
7195 
7196          IF (l_encumbrance_flag = 'Y')
7197          THEN
7198             l_stmt_num := 80;
7199 
7200             IF     g_debug = 'Y'
7201                AND fnd_log.level_event >= fnd_log.g_current_runtime_level
7202             THEN
7203                fnd_log.STRING (fnd_log.level_event,
7204                                   g_log_head
7205                                || '.'
7206                                || l_api_name
7207                                || '.'
7208                                || l_stmt_num,
7209                                'Seeding Encumbrance Reversal Event'
7210                               );
7211             END IF;
7212 
7213             insert_txn
7214                (x_return_status                     => l_return_status,
7215                 x_msg_count                         => l_msg_count,
7216                 x_msg_data                          => l_msg_data,
7217                 p_event_source                      => 'RECEIVING',
7218                 p_event_type_id                     => encumbrance_reversal,
7219                 p_rcv_transaction_id                => p_rcv_transaction_id,
7220                 p_inv_distribution_id               => NULL,
7221                 p_po_distribution_id                => l_po_distribution_id,
7222                 p_direct_delivery_flag              => p_direct_delivery_flag,
7223                 p_gl_group_id                       => p_gl_group_id,
7224                 p_cross_ou_flag                     => l_cross_ou_flag,
7225                 p_procurement_org_flag              => l_procurement_org_flag,
7226                 p_ship_to_org_flag                  => 'Y',
7227                 p_drop_ship_flag                    => l_drop_ship_flag,
7228                 p_org_id                            => l_rcv_org_id,
7229                 p_organization_id                   => l_rcv_organization_id,
7230                 p_transfer_org_id                   => NULL,
7231                 p_transfer_organization_id          => NULL,
7232                 p_trx_flow_header_id                => NULL,
7233                 p_transaction_forward_flow_rec      => NULL,
7234                 p_transaction_reverse_flow_rec      => l_transaction_reverse_flow_rec,
7235                 p_unit_price                        => NULL,
7236                 p_prior_unit_price                  => NULL,
7237                 x_rcv_accttxn                       => l_rcv_accttxn
7238                );
7239 
7240             /* Bug #3333610. In the case of encumbrance reversals, the quantity to unencumber
7241                may turn out to be zero if the quantity delivered is greater than the quantity
7242                ordered. In such a situation, we should not error out the event. */
7243             IF l_return_status = fnd_api.g_ret_sts_success
7244             THEN
7245                l_rcv_accttxn_tbl (l_rcv_accttxn_tbl.COUNT + 1) :=
7246                                                                 l_rcv_accttxn;
7247             ELSIF l_return_status <> 'W'
7248             THEN
7249                l_api_message := 'Error in seeding encumbrance reversal event';
7250 
7251                IF     g_debug = 'Y'
7252                   AND fnd_log.level_unexpected >=
7253                                                fnd_log.g_current_runtime_level
7254                THEN
7255                   fnd_log.STRING (fnd_log.level_unexpected,
7256                                   g_log_head || '.' || l_api_name
7257                                   || l_stmt_num,
7258                                      'Create_RTREvents : '
7259                                   || l_stmt_num
7260                                   || ' : '
7261                                   || l_api_message
7262                                  );
7263                END IF;
7264 
7265                RAISE fnd_api.g_exc_unexpected_error;
7266             END IF;
7267          END IF;
7268       END IF;
7269 
7270       l_stmt_num := 90;
7271 
7272       IF     g_debug = 'Y'
7273          AND fnd_log.level_event >= fnd_log.g_current_runtime_level
7274       THEN
7275          fnd_log.STRING (fnd_log.level_event,
7276                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
7277                          'Inserting transactions into GRAT'
7278                         );
7279       END IF;
7280 
7281       insert_txn2 (x_return_status        => l_return_status,
7282                    x_msg_count            => l_msg_count,
7283                    x_msg_data             => l_msg_data,
7284                    p_rcv_accttxn_tbl      => l_rcv_accttxn_tbl
7285                   );
7286 
7287       IF l_return_status <> fnd_api.g_ret_sts_success
7288       THEN
7289          l_api_message := 'Error inserting transactions into GRAT';
7290 
7291          IF     g_debug = 'Y'
7292             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
7293          THEN
7294             fnd_log.STRING (fnd_log.level_unexpected,
7295                             g_log_head || '.' || l_api_name || l_stmt_num,
7296                                'Create_RTREvents : '
7297                             || l_stmt_num
7298                             || ' : '
7299                             || l_api_message
7300                            );
7301          END IF;
7302 
7303          RAISE fnd_api.g_exc_unexpected_error;
7304       END IF;
7305 
7306       -- Standard check of p_commit
7307       IF fnd_api.to_boolean (p_commit)
7308       THEN
7309          COMMIT WORK;
7310       END IF;
7311 
7312       -- Standard Call to get message count and if count = 1, get message info
7313       fnd_msg_pub.count_and_get (p_count      => x_msg_count,
7314                                  p_data       => x_msg_data);
7315 
7316       IF     g_debug = 'Y'
7317          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
7318       THEN
7319          fnd_log.STRING (fnd_log.level_procedure,
7320                          g_log_head || '.' || l_api_name || '.end',
7321                          'Create_RTREvents >>'
7322                         );
7323       END IF;
7324    EXCEPTION
7325       WHEN fnd_api.g_exc_error
7326       THEN
7327          ROLLBACK TO create_rtrevents_pvt;
7328          x_return_status := fnd_api.g_ret_sts_error;
7329          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
7330                                     p_data       => x_msg_data
7331                                    );
7332       WHEN fnd_api.g_exc_unexpected_error
7333       THEN
7334          ROLLBACK TO create_rtrevents_pvt;
7335          x_return_status := fnd_api.g_ret_sts_unexp_error;
7336          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
7337                                     p_data       => x_msg_data
7338                                    );
7339       WHEN OTHERS
7340       THEN
7341          ROLLBACK TO create_rtrevents_pvt;
7342          x_return_status := fnd_api.g_ret_sts_unexp_error;
7343 
7344          IF     g_debug = 'Y'
7345             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
7346          THEN
7347             fnd_log.STRING (fnd_log.level_unexpected,
7348                             g_log_head || '.' || l_api_name || l_stmt_num,
7349                                'Create_RTREvents : '
7350                             || l_stmt_num
7351                             || ' : '
7352                             || SUBSTR (SQLERRM, 1, 200)
7353                            );
7354          END IF;
7355 
7356          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
7357          THEN
7358             fnd_msg_pub.add_exc_msg (g_pkg_name,
7359                                         l_api_name
7360                                      || 'Statement -'
7361                                      || TO_CHAR (l_stmt_num)
7362                                     );
7363          END IF;
7364 
7365          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
7366                                     p_data       => x_msg_data
7367                                    );
7368    END create_rtr_txns;
7369 
7370 -- Start of comments
7371 --      API name        : Create_RTVEvents
7372 --      Type            : Private
7373 --      Function        : To seed accounting transactions for RETURN TO VENDOR transactions.
7374 --      Pre-reqs        :
7375 --      Parameters      :
7376 --      IN              :       p_api_version           IN NUMBER       Required
7377 --                              p_init_msg_list         IN VARCHAR2     Optional
7378 --                                      Default = FND_API.G_FALSE
7379 --                              p_commit                IN VARCHAR2     Optional
7380 --                                      Default = FND_API.G_FALSE
7381 --                              p_validation_level      IN NUMBER       Optional
7382 --                                      Default = FND_API.G_VALID_LEVEL_FULL
7383 --                              p_rcv_transaction_id    IN NUMBER       Required
7384 --                              p_direct_delivery_flag  IN VARCHAR2     Optional
7385 --                              p_gl_group_id           IN NUMBER       Optional
7386 --
7387 --      OUT             :       x_return_status         OUT     VARCHAR2(1)
7388 --                              x_msg_count             OUT     NUMBER
7389 --                              x_msg_data              OUT     VARCHAR2(2000)
7390 --      Version :
7391 --                        Initial version       1.0
7392 --
7393 --
7394 --      Notes           : This API creates all accounting transactions for RETURN TO VENDOR transactions
7395 --                        in gmf_rcv_accounting_txns.
7396 --
7397 -- End of comments
7398    PROCEDURE create_rtv_txns (
7399       p_api_version            IN              NUMBER := 1.0,
7400       p_init_msg_list          IN              VARCHAR2 := fnd_api.g_false,
7401       p_commit                 IN              VARCHAR2 := fnd_api.g_false,
7402       p_validation_level       IN              NUMBER
7403             := fnd_api.g_valid_level_full,
7404       x_return_status          OUT NOCOPY      VARCHAR2,
7405       x_msg_count              OUT NOCOPY      NUMBER,
7406       x_msg_data               OUT NOCOPY      VARCHAR2,
7407       p_rcv_transaction_id     IN              NUMBER,
7408       p_direct_delivery_flag   IN              VARCHAR2,
7409       p_gl_group_id            IN              NUMBER
7410    )
7411    IS
7412       l_api_name              CONSTANT VARCHAR2 (30)     := 'Create_RTV_Txns';
7413       l_api_version           CONSTANT NUMBER                          := 1.0;
7414       l_return_status                  VARCHAR2 (1)
7415                                                  := fnd_api.g_ret_sts_success;
7416       l_msg_count                      NUMBER                            := 0;
7417       l_msg_data                       VARCHAR2 (8000)                  := '';
7418       l_stmt_num                       NUMBER                            := 0;
7419       l_api_message                    VARCHAR2 (1000);
7420       l_rcv_accttxn                    gmf_rcv_accounting_pkg.rcv_accttxn_rec_type;
7421       l_rcv_accttxn_tbl                gmf_rcv_accounting_pkg.rcv_accttxn_tbl_type;
7422       l_event_type_id                  NUMBER;
7423       l_transaction_flows_tbl          inv_transaction_flow_pub.g_transaction_flow_tbl_type;
7424       l_transaction_forward_flow_rec   inv_transaction_flow_pub.mtl_transaction_flow_rec_type;
7425       l_transaction_reverse_flow_rec   inv_transaction_flow_pub.mtl_transaction_flow_rec_type;
7426       l_trx_flow_exists_flag           NUMBER                            := 0;
7427       l_trx_flow_ctr                   NUMBER                            := 0;
7428       l_po_header_id                   NUMBER;
7429       l_po_line_id                     NUMBER;
7430       l_po_line_location_id            NUMBER;
7431       l_po_distribution_id             NUMBER;
7432       l_po_org_id                      NUMBER;
7433       l_po_ledger_id                   NUMBER;
7434       l_rcv_organization_id            NUMBER;
7435       l_rcv_org_id                     NUMBER;
7436       l_rcv_ledger_id                  NUMBER;
7437       l_org_id                         NUMBER;
7438       l_transfer_org_id                NUMBER;
7439       l_transfer_organization_id       NUMBER;
7440       l_rcv_trx_date                   DATE;
7441       l_drop_ship_flag                 NUMBER;
7442       l_destination_type               VARCHAR (25);
7443       l_item_id                        NUMBER;
7444       l_category_id                    NUMBER;
7445       l_project_id                     NUMBER;
7446       l_cross_ou_flag                  VARCHAR2 (1);
7447       l_accrual_flag                   VARCHAR2 (1);
7448       l_counter                        NUMBER;
7449       l_procurement_org_flag           VARCHAR2 (1);
7450       l_trx_flow_header_id             NUMBER;
7451       l_qualifier_code_tbl             inv_transaction_flow_pub.number_tbl;
7452       l_qualifier_value_tbl            inv_transaction_flow_pub.number_tbl;
7453 
7454       CURSOR c_po_distributions_csr (
7455          p_po_distribution_id    NUMBER,
7456          p_po_line_location_id   NUMBER
7457       )
7458       IS
7459          SELECT po_distribution_id, destination_type_code, project_id
7460            FROM po_distributions pod
7461           WHERE pod.po_distribution_id =
7462                            NVL (p_po_distribution_id, pod.po_distribution_id)
7463             AND pod.line_location_id = p_po_line_location_id;
7464    BEGIN
7465       -- Standard start of API savepoint
7466       SAVEPOINT create_rtvevents_pvt;
7467       l_stmt_num := 0;
7468 
7469       IF     g_debug = 'Y'
7470          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
7471       THEN
7472          fnd_log.STRING (fnd_log.level_procedure,
7473                          g_log_head || '.' || l_api_name || '.begin',
7474                          'Create_RTVEvents <<'
7475                         );
7476       END IF;
7477 
7478       -- Standard call to check for call compatibility
7479       IF NOT fnd_api.compatible_api_call (l_api_version,
7480                                           p_api_version,
7481                                           l_api_name,
7482                                           g_pkg_name
7483                                          )
7484       THEN
7485          RAISE fnd_api.g_exc_unexpected_error;
7486       END IF;
7487 
7488       -- Initialize message list if p_init_msg_list is set to TRUE
7489       IF fnd_api.to_boolean (p_init_msg_list)
7490       THEN
7491          fnd_msg_pub.initialize;
7492       END IF;
7493 
7494       -- Initialize API return status to success
7495       x_return_status := fnd_api.g_ret_sts_success;
7496       l_stmt_num := 20;
7497 
7498       SELECT rt.po_header_id, rt.po_line_id, rt.po_line_location_id,
7499              rt.po_distribution_id, rt.transaction_date,
7500              NVL (rt.dropship_type_code, 3), poh.org_id,
7501              poll.ship_to_organization_id, pol.item_id, pol.category_id,
7502              pol.project_id, NVL (poll.accrue_on_receipt_flag, 'N')
7503         INTO l_po_header_id, l_po_line_id, l_po_line_location_id,
7504              l_po_distribution_id, l_rcv_trx_date,
7505              l_drop_ship_flag, l_po_org_id,
7506              l_rcv_organization_id, l_item_id, l_category_id,
7507              l_project_id, l_accrual_flag
7508         FROM po_headers poh,
7509              po_line_locations poll,
7510              po_lines pol,
7511              rcv_transactions rt
7512        WHERE rt.transaction_id = p_rcv_transaction_id
7513          AND poh.po_header_id = rt.po_header_id
7514          AND poll.line_location_id = rt.po_line_location_id
7515          AND pol.po_line_id = rt.po_line_id;
7516 
7517       l_stmt_num := 30;
7518 
7519       -- Get Receiving Operating Unit
7520       SELECT operating_unit, set_of_books_id
7521         INTO l_rcv_org_id, l_rcv_ledger_id
7522         FROM cst_organization_definitions cod
7523        WHERE organization_id = l_rcv_organization_id;
7524 
7525       l_stmt_num := 35;
7526 
7527       -- Get PO SOB
7528       SELECT set_of_books_id
7529         INTO l_po_ledger_id
7530         FROM financials_system_parameters;
7531 
7532       l_stmt_num := 40;
7533 
7534       IF     g_debug = 'Y'
7535          AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
7536       THEN
7537          l_api_message :=
7538                'Creating RTV transactions : RCV Transaction ID : '
7539             || p_rcv_transaction_id
7540             || ', PO Header ID : '
7541             || l_po_header_id
7542             || ', PO Line ID : '
7543             || l_po_line_id
7544             || ', PO Line Location ID : '
7545             || l_po_line_location_id
7546             || ', PO Dist ID : '
7547             || l_po_distribution_id
7548             || ', Transaction Date : '
7549             || l_rcv_trx_date
7550             || ', Drop Ship Flag : '
7551             || l_drop_ship_flag
7552             || ', PO Org ID : '
7553             || l_po_org_id
7554             || ', PO Ledger ID : '
7555             || l_po_ledger_id
7556             || ', RCV Organization ID : '
7557             || l_rcv_organization_id
7558             || ', RCV Org ID : '
7559             || l_rcv_org_id
7560             || ', RCV Ledger ID : '
7561             || l_rcv_ledger_id
7562             || ', Category ID : '
7563             || l_category_id
7564             || ', Accrual Flag : '
7565             || l_accrual_flag;
7566          fnd_log.STRING (fnd_log.level_statement,
7567                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
7568                          l_api_message
7569                         );
7570       END IF;
7571 
7572       IF (l_po_org_id = l_rcv_org_id)
7573       THEN
7574          l_cross_ou_flag := 'N';
7575       ELSE
7576          l_cross_ou_flag := 'Y';
7577          /* For 11i10, the only supported qualifier is category id. */
7578          l_qualifier_code_tbl (l_qualifier_code_tbl.COUNT + 1) :=
7579                                     inv_transaction_flow_pub.g_qualifier_code;
7580          l_qualifier_value_tbl (l_qualifier_value_tbl.COUNT + 1) :=
7581                                                                 l_category_id;
7582          l_stmt_num := 50;
7583 
7584          IF     g_debug = 'Y'
7585             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
7586          THEN
7587             l_api_message :=
7588                   'Getting Procurement Transaction Flow :'
7589                || 'l_po_org_id : '
7590                || l_po_org_id
7591                || ' l_rcv_org_id : '
7592                || l_rcv_org_id
7593                || ' l_rcv_organization_id : '
7594                || l_rcv_organization_id;
7595             fnd_log.STRING (fnd_log.level_statement,
7596                             g_log_head || '.' || l_api_name || '.'
7597                             || l_stmt_num,
7598                             l_api_message
7599                            );
7600          END IF;
7601 
7602          inv_transaction_flow_pub.get_transaction_flow
7603                (x_return_status               => l_return_status,
7604                 x_msg_data                    => l_msg_data,
7605                 x_msg_count                   => l_msg_count,
7606                 x_transaction_flows_tbl       => l_transaction_flows_tbl,
7607                 p_api_version                 => 1.0,
7608                 p_start_operating_unit        => l_po_org_id,
7609                 p_end_operating_unit          => l_rcv_org_id,
7610                 p_flow_type                   => inv_transaction_flow_pub.g_procuring_flow_type,
7611                 p_organization_id             => l_rcv_organization_id,
7612                 p_qualifier_code_tbl          => l_qualifier_code_tbl,
7613                 p_qualifier_value_tbl         => l_qualifier_value_tbl,
7614                 p_transaction_date            => l_rcv_trx_date,
7615                 p_get_default_cost_group      => 'N'
7616                );
7617 
7618          IF (l_return_status = fnd_api.g_ret_sts_success)
7619          THEN
7620             l_trx_flow_exists_flag := 1;
7621             l_trx_flow_header_id :=
7622                l_transaction_flows_tbl (l_transaction_flows_tbl.FIRST).header_id;
7623          -- Return Status of 'W' indicates that no transaction flow exists.
7624          ELSIF (l_return_status = 'W')
7625          THEN
7626             l_trx_flow_exists_flag := 0;
7627 
7628             IF     g_debug = 'Y'
7629                AND fnd_log.level_event >= fnd_log.g_current_runtime_level
7630             THEN
7631                fnd_log.STRING (fnd_log.level_event,
7632                                   g_log_head
7633                                || '.'
7634                                || l_api_name
7635                                || '.'
7636                                || l_stmt_num,
7637                                'Transaction Flow does not exist'
7638                               );
7639             END IF;
7640 
7641             -- If transaction flow does not exist, but the PO crosses multiple
7642             -- sets of books, error out the transaction.
7643             IF (l_po_ledger_id <> l_rcv_ledger_id)
7644             THEN
7645                l_api_message := 'Transaction Flow does not exist';
7646 
7647                IF     g_debug = 'Y'
7648                   AND fnd_log.level_unexpected >=
7649                                                fnd_log.g_current_runtime_level
7650                THEN
7651                   fnd_log.STRING (fnd_log.level_unexpected,
7652                                   g_log_head || '.' || l_api_name
7653                                   || l_stmt_num,
7654                                      'Create_RTVEvents : '
7655                                   || l_stmt_num
7656                                   || ' : '
7657                                   || l_api_message
7658                                  );
7659                END IF;
7660 
7661                RAISE fnd_api.g_exc_unexpected_error;
7662             END IF;
7663          ELSE
7664             l_api_message := 'Error occurred in Transaction Flow API';
7665 
7666             IF     g_debug = 'Y'
7667                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
7668             THEN
7669                fnd_log.STRING (fnd_log.level_unexpected,
7670                                g_log_head || '.' || l_api_name || l_stmt_num,
7671                                   'Create_RTVEvents : '
7672                                || l_stmt_num
7673                                || ' : '
7674                                || l_api_message
7675                               );
7676             END IF;
7677 
7678             RAISE fnd_api.g_exc_unexpected_error;
7679          END IF;                                         -- IF l_return_status
7680       END IF;                                                -- IF l_po_org_id
7681 
7682       -- For the RTV transaction, the PO distribution may not be available in the
7683       -- case of Standard Receipt. Hence perform all steps for each applicable distribution.
7684       -- If distribution is not available the quantity will be prorated. Furthermore, if
7685       -- there is a project on any of the distributions, and the destination_type_code is
7686       -- expense, the transaction flow should be ignored for just that distribution.
7687       FOR rec_pod IN c_po_distributions_csr (l_po_distribution_id,
7688                                              l_po_line_location_id
7689                                             )
7690       LOOP
7691          l_stmt_num := 50;
7692 
7693          IF     g_debug = 'Y'
7694             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
7695          THEN
7696             l_api_message :=
7697                   'Creating  transactions : '
7698                || 'po_distribution_id : '
7699                || rec_pod.po_distribution_id
7700                || ' destination_type_code : '
7701                || rec_pod.destination_type_code
7702                || ' project_id : '
7703                || rec_pod.project_id;
7704             fnd_log.STRING (fnd_log.level_statement,
7705                             g_log_head || '.' || l_api_name || '.'
7706                             || l_stmt_num,
7707                             l_api_message
7708                            );
7709          END IF;
7710 
7711          l_procurement_org_flag := 'Y';
7712 
7713          -- For POs with destination type of expense, when there is a project on the
7714          -- POD, we should not look for transaction flow. This is because PA,(which transfers
7715          -- costs to Projects for expense destinations), is currently not supporting global
7716          -- procurement.
7717          IF (    (l_trx_flow_exists_flag = 1)
7718              AND (   rec_pod.project_id IS NULL
7719                   OR rec_pod.destination_type_code <> 'EXPENSE'
7720                  )
7721             )
7722          THEN
7723             l_trx_flow_ctr := l_transaction_flows_tbl.COUNT;
7724 
7725             -- Create Logical RTV transactions in each intermediate organization.
7726             FOR l_counter IN
7727                l_transaction_flows_tbl.FIRST .. l_transaction_flows_tbl.LAST
7728             LOOP
7729                IF     g_debug = 'Y'
7730                   AND fnd_log.level_event >= fnd_log.g_current_runtime_level
7731                THEN
7732                   fnd_log.STRING (fnd_log.level_event,
7733                                      g_log_head
7734                                   || '.'
7735                                   || l_api_name
7736                                   || '.'
7737                                   || l_stmt_num,
7738                                   'Seeding Logical RTV in GRAT'
7739                                  );
7740                END IF;
7741 
7742                l_stmt_num := 60;
7743                -- l_transaction_forward_flow_rec contains the transaction flow record
7744                -- where the org_id is the from_org_id.
7745                -- l_transaction_reverse_flow_rec contains the transaction flow record
7746                -- where the org_id is the to_org_id.
7747                -- Need to pass both to the Seed_GRAT procedure because transfer_price is based
7748                -- on the reverse flow record and some accounts are based on the forward flow
7749                l_transaction_forward_flow_rec :=
7750                                            l_transaction_flows_tbl (l_counter);
7751 
7752                IF (l_counter = l_transaction_flows_tbl.FIRST)
7753                THEN
7754                   l_transaction_reverse_flow_rec := NULL;
7755                   l_transfer_org_id := NULL;
7756                   l_transfer_organization_id := NULL;
7757                ELSE
7758                   l_transaction_reverse_flow_rec :=
7759                                       l_transaction_flows_tbl (l_counter - 1);
7760                   l_transfer_org_id :=
7761                                    l_transaction_reverse_flow_rec.from_org_id;
7762                   l_transfer_organization_id :=
7763                           l_transaction_reverse_flow_rec.from_organization_id;
7764                END IF;
7765 
7766                insert_txn
7767                   (x_return_status                     => l_return_status,
7768                    x_msg_count                         => l_msg_count,
7769                    x_msg_data                          => l_msg_data,
7770                    p_event_source                      => 'RECEIVING',
7771                    p_event_type_id                     => logical_return_to_vendor,
7772                    p_rcv_transaction_id                => p_rcv_transaction_id,
7773                    p_inv_distribution_id               => NULL,
7774                    p_po_distribution_id                => rec_pod.po_distribution_id,
7775                    p_direct_delivery_flag              => p_direct_delivery_flag,
7776                    p_gl_group_id                       => p_gl_group_id,
7777                    p_cross_ou_flag                     => l_cross_ou_flag,
7778                    p_procurement_org_flag              => l_procurement_org_flag,
7779                    p_ship_to_org_flag                  => 'N',
7780                    p_drop_ship_flag                    => l_drop_ship_flag,
7781                    p_org_id                            => l_transaction_flows_tbl
7782                                                                     (l_counter).from_org_id,
7783                    p_organization_id                   => l_transaction_flows_tbl
7784                                                                     (l_counter).from_organization_id,
7785                    p_transfer_org_id                   => l_transfer_org_id,
7786                    p_transfer_organization_id          => l_transfer_organization_id,
7787                    p_trx_flow_header_id                => l_trx_flow_header_id,
7788                    p_transaction_forward_flow_rec      => l_transaction_forward_flow_rec,
7789                    p_transaction_reverse_flow_rec      => l_transaction_reverse_flow_rec,
7790                    p_unit_price                        => NULL,
7791                    p_prior_unit_price                  => NULL,
7792                    x_rcv_accttxn                       => l_rcv_accttxn
7793                   );
7794 
7795                IF l_return_status <> fnd_api.g_ret_sts_success
7796                THEN
7797                   l_api_message := 'Error creating event';
7798 
7799                   IF     g_debug = 'Y'
7800                      AND fnd_log.level_unexpected >=
7801                                                fnd_log.g_current_runtime_level
7802                   THEN
7803                      fnd_log.STRING (fnd_log.level_unexpected,
7804                                         g_log_head
7805                                      || '.'
7806                                      || l_api_name
7807                                      || l_stmt_num,
7808                                         'Create_RTVEvents : '
7809                                      || l_stmt_num
7810                                      || ' : '
7811                                      || l_api_message
7812                                     );
7813                   END IF;
7814 
7815                   RAISE fnd_api.g_exc_unexpected_error;
7816                END IF;
7817 
7818                l_rcv_accttxn_tbl (l_rcv_accttxn_tbl.COUNT + 1) :=
7819                                                                  l_rcv_accttxn;
7820 
7821                -- For one-time items, if online accruals is used, seed IC Invoice event.
7822                -- For Shop Floor destination types, always seed IC Invoice events.
7823                IF (   (l_item_id IS NULL AND l_accrual_flag = 'Y')
7824                    OR (rec_pod.destination_type_code = 'SHOP FLOOR')
7825                   )
7826                THEN
7827                   l_stmt_num := 70;
7828 
7829                   IF     g_debug = 'Y'
7830                      AND fnd_log.level_event >=
7831                                                fnd_log.g_current_runtime_level
7832                   THEN
7833                      fnd_log.STRING (fnd_log.level_event,
7834                                         g_log_head
7835                                      || '.'
7836                                      || l_api_name
7837                                      || '.'
7838                                      || l_stmt_num,
7839                                      'Seeding Invoice Match in GRAT'
7840                                     );
7841                   END IF;
7842 
7843                   insert_txn
7844                      (x_return_status                     => l_return_status,
7845                       x_msg_count                         => l_msg_count,
7846                       x_msg_data                          => l_msg_data,
7847                       p_event_source                      => 'RECEIVING',
7848                       p_event_type_id                     => intercompany_reversal,
7849                       p_rcv_transaction_id                => p_rcv_transaction_id,
7850                       p_inv_distribution_id               => NULL,
7851                       p_po_distribution_id                => rec_pod.po_distribution_id,
7852                       p_direct_delivery_flag              => p_direct_delivery_flag,
7853                       p_gl_group_id                       => NULL,
7854                       p_cross_ou_flag                     => l_cross_ou_flag,
7855                       p_procurement_org_flag              => l_procurement_org_flag,
7856                       p_ship_to_org_flag                  => 'N',
7857                       p_drop_ship_flag                    => l_drop_ship_flag,
7858                       p_org_id                            => l_transaction_flows_tbl
7859                                                                     (l_counter).from_org_id,
7860                       p_organization_id                   => l_transaction_flows_tbl
7861                                                                     (l_counter).from_organization_id,
7862                       p_transfer_org_id                   => l_transaction_flows_tbl
7863                                                                     (l_counter).to_org_id,
7864                       p_transfer_organization_id          => l_transaction_flows_tbl
7865                                                                     (l_counter).to_organization_id,
7866                       p_trx_flow_header_id                => l_trx_flow_header_id,
7867                       p_transaction_forward_flow_rec      => l_transaction_forward_flow_rec,
7868                       p_transaction_reverse_flow_rec      => l_transaction_reverse_flow_rec,
7869                       p_unit_price                        => NULL,
7870                       p_prior_unit_price                  => NULL,
7871                       x_rcv_accttxn                       => l_rcv_accttxn
7872                      );
7873 
7874                   IF l_return_status <> fnd_api.g_ret_sts_success
7875                   THEN
7876                      l_api_message := 'Error creating event';
7877 
7878                      IF     g_debug = 'Y'
7879                         AND fnd_log.level_unexpected >=
7880                                                fnd_log.g_current_runtime_level
7881                      THEN
7882                         fnd_log.STRING (fnd_log.level_unexpected,
7883                                            g_log_head
7884                                         || '.'
7885                                         || l_api_name
7886                                         || l_stmt_num,
7887                                            'Create_RTVEvents : '
7888                                         || l_stmt_num
7889                                         || ' : '
7890                                         || l_api_message
7891                                        );
7892                      END IF;
7893 
7894                      RAISE fnd_api.g_exc_unexpected_error;
7895                   END IF;
7896 
7897                   l_rcv_accttxn_tbl (l_rcv_accttxn_tbl.COUNT + 1) :=
7898                                                                  l_rcv_accttxn;
7899                END IF;
7900 
7901                l_procurement_org_flag := 'N';
7902             END LOOP;
7903          END IF;
7904 
7905          l_stmt_num := 80;
7906 
7907          IF (l_trx_flow_exists_flag = 1)
7908          THEN
7909             l_transaction_forward_flow_rec := NULL;
7910             l_transaction_reverse_flow_rec :=
7911                                      l_transaction_flows_tbl (l_trx_flow_ctr);
7912             l_org_id := l_transaction_flows_tbl (l_trx_flow_ctr).to_org_id;
7913             l_transfer_org_id :=
7914                          l_transaction_flows_tbl (l_trx_flow_ctr).from_org_id;
7915             l_transfer_organization_id :=
7916                           l_transaction_reverse_flow_rec.from_organization_id;
7917          ELSE
7918             l_transaction_forward_flow_rec := NULL;
7919             l_transaction_reverse_flow_rec := NULL;
7920             l_org_id := l_po_org_id;
7921             l_transfer_org_id := NULL;
7922             l_transfer_organization_id := NULL;
7923          END IF;
7924 
7925          l_stmt_num := 90;
7926          -- The drop ship flag is not applicable in the case of returns. There will always
7927          -- be a physical receipt in the procuring org.
7928          l_stmt_num := 110;
7929 
7930          SELECT DECODE (rt.transaction_type,
7931                         'CORRECT', correct,
7932                         return_to_vendor
7933                        )
7934            INTO l_event_type_id
7935            FROM rcv_transactions rt
7936           WHERE transaction_id = p_rcv_transaction_id;
7937 
7938          IF     g_debug = 'Y'
7939             AND fnd_log.level_event >= fnd_log.g_current_runtime_level
7940          THEN
7941             fnd_log.STRING (fnd_log.level_event,
7942                             g_log_head || '.' || l_api_name || '.'
7943                             || l_stmt_num,
7944                             'Seeding RTV in GRAT'
7945                            );
7946          END IF;
7947 
7948          l_stmt_num := 120;
7949          insert_txn
7950             (x_return_status                     => l_return_status,
7951              x_msg_count                         => l_msg_count,
7952              x_msg_data                          => l_msg_data,
7953              p_event_source                      => 'RECEIVING',
7954              p_event_type_id                     => l_event_type_id,
7955              p_rcv_transaction_id                => p_rcv_transaction_id,
7956              p_inv_distribution_id               => NULL,
7957              p_po_distribution_id                => rec_pod.po_distribution_id,
7958              p_direct_delivery_flag              => p_direct_delivery_flag,
7959              p_gl_group_id                       => p_gl_group_id,
7960              p_cross_ou_flag                     => l_cross_ou_flag,
7961              p_procurement_org_flag              => l_procurement_org_flag,
7962              p_ship_to_org_flag                  => 'Y',
7963              p_drop_ship_flag                    => l_drop_ship_flag,
7964              p_org_id                            => l_org_id,
7965              p_organization_id                   => l_rcv_organization_id,
7966              p_transfer_org_id                   => l_transfer_org_id,
7967              p_transfer_organization_id          => l_transfer_organization_id,
7968              p_trx_flow_header_id                => l_trx_flow_header_id,
7969              p_transaction_forward_flow_rec      => l_transaction_forward_flow_rec,
7970              p_transaction_reverse_flow_rec      => l_transaction_reverse_flow_rec,
7971              p_unit_price                        => NULL,
7972              p_prior_unit_price                  => NULL,
7973              x_rcv_accttxn                       => l_rcv_accttxn
7974             );
7975 
7976          IF l_return_status <> fnd_api.g_ret_sts_success
7977          THEN
7978             l_api_message := 'Error creating event';
7979 
7980             IF     g_debug = 'Y'
7981                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
7982             THEN
7983                fnd_log.STRING (fnd_log.level_unexpected,
7984                                g_log_head || '.' || l_api_name || l_stmt_num,
7985                                   'Create_RTVEvents : '
7986                                || l_stmt_num
7987                                || ' : '
7988                                || l_api_message
7989                               );
7990             END IF;
7991 
7992             RAISE fnd_api.g_exc_unexpected_error;
7993          END IF;
7994 
7995          l_rcv_accttxn_tbl (l_rcv_accttxn_tbl.COUNT + 1) := l_rcv_accttxn;
7996       END LOOP;
7997 
7998       l_stmt_num := 130;
7999 
8000       IF     g_debug = 'Y'
8001          AND fnd_log.level_event >= fnd_log.g_current_runtime_level
8002       THEN
8003          fnd_log.STRING (fnd_log.level_event,
8004                          g_log_head || '.' || l_api_name || '.' || l_stmt_num,
8005                          'Inserting transactions into GRAT'
8006                         );
8007       END IF;
8008 
8009       insert_txn2 (x_return_status        => l_return_status,
8010                    x_msg_count            => l_msg_count,
8011                    x_msg_data             => l_msg_data,
8012                    p_rcv_accttxn_tbl      => l_rcv_accttxn_tbl
8013                   );
8014 
8015       IF l_return_status <> fnd_api.g_ret_sts_success
8016       THEN
8017          l_api_message := 'Error inserting transactions into GRAT';
8018 
8019          IF     g_debug = 'Y'
8020             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
8021          THEN
8022             fnd_log.STRING (fnd_log.level_unexpected,
8023                             g_log_head || '.' || l_api_name || l_stmt_num,
8024                                'Create_RTVEvents : '
8025                             || l_stmt_num
8026                             || ' : '
8027                             || l_api_message
8028                            );
8029          END IF;
8030 
8031          RAISE fnd_api.g_exc_unexpected_error;
8032       END IF;
8033 
8034       IF (l_trx_flow_exists_flag = 1 AND l_item_id IS NOT NULL)
8035       THEN
8036          l_stmt_num := 140;
8037 
8038          IF     g_debug = 'Y'
8039             AND fnd_log.level_event >= fnd_log.g_current_runtime_level
8040          THEN
8041             fnd_log.STRING (fnd_log.level_event,
8042                             g_log_head || '.' || l_api_name || '.'
8043                             || l_stmt_num,
8044                             'Inserting transactions into MMT'
8045                            );
8046          END IF;
8047 
8048          insert_mmt (p_api_version          => 1.0,
8049                      x_return_status        => l_return_status,
8050                      x_msg_count            => l_msg_count,
8051                      x_msg_data             => l_msg_data,
8052                      p_rcv_accttxn_tbl      => l_rcv_accttxn_tbl
8053                     );
8054 
8055          IF l_return_status <> fnd_api.g_ret_sts_success
8056          THEN
8057             l_api_message := 'Error inserting transactions into MMT';
8058 
8059             IF     g_debug = 'Y'
8060                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
8061             THEN
8062                fnd_log.STRING (fnd_log.level_unexpected,
8063                                g_log_head || '.' || l_api_name || l_stmt_num,
8064                                   'Create_RTVEvents : '
8065                                || l_stmt_num
8066                                || ' : '
8067                                || l_api_message
8068                               );
8069             END IF;
8070 
8071             RAISE fnd_api.g_exc_unexpected_error;
8072          END IF;
8073       END IF;
8074 
8075       -- Standard check of p_commit
8076       IF fnd_api.to_boolean (p_commit)
8077       THEN
8078          COMMIT WORK;
8079       END IF;
8080 
8081       -- Standard Call to get message count and if count = 1, get message info
8082       fnd_msg_pub.count_and_get (p_count      => x_msg_count,
8083                                  p_data       => x_msg_data);
8084 
8085       IF     g_debug = 'Y'
8086          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
8087       THEN
8088          fnd_log.STRING (fnd_log.level_procedure,
8089                          g_log_head || '.' || l_api_name || '.end',
8090                          'Create_RTVEvents >>'
8091                         );
8092       END IF;
8093    EXCEPTION
8094       WHEN fnd_api.g_exc_error
8095       THEN
8096          ROLLBACK TO create_rtvevents_pvt;
8097          x_return_status := fnd_api.g_ret_sts_error;
8098          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
8099                                     p_data       => x_msg_data
8100                                    );
8101       WHEN fnd_api.g_exc_unexpected_error
8102       THEN
8103          ROLLBACK TO create_rtvevents_pvt;
8104          x_return_status := fnd_api.g_ret_sts_unexp_error;
8105          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
8106                                     p_data       => x_msg_data
8107                                    );
8108       WHEN OTHERS
8109       THEN
8110          ROLLBACK TO create_rtvevents_pvt;
8111          x_return_status := fnd_api.g_ret_sts_unexp_error;
8112 
8113          IF     g_debug = 'Y'
8114             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
8115          THEN
8116             fnd_log.STRING (fnd_log.level_unexpected,
8117                             g_log_head || '.' || l_api_name || l_stmt_num,
8118                                'Create_RTVEvents : '
8119                             || l_stmt_num
8120                             || ' : '
8121                             || SUBSTR (SQLERRM, 1, 200)
8122                            );
8123          END IF;
8124 
8125          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
8126          THEN
8127             fnd_msg_pub.add_exc_msg (g_pkg_name,
8128                                         l_api_name
8129                                      || 'Statement -'
8130                                      || TO_CHAR (l_stmt_num)
8131                                     );
8132          END IF;
8133 
8134          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
8135                                     p_data       => x_msg_data
8136                                    );
8137    END create_rtv_txns;
8138 
8139    PROCEDURE create_accounting_txns (
8140       p_api_version            IN              NUMBER,
8141       p_init_msg_list          IN              VARCHAR2,
8142       p_commit                 IN              VARCHAR2,
8143       p_validation_level       IN              NUMBER,
8144       x_return_status          OUT NOCOPY      VARCHAR2,
8145       x_msg_count              OUT NOCOPY      NUMBER,
8146       x_msg_data               OUT NOCOPY      VARCHAR2,
8147       p_source_type	       IN VARCHAR2,
8148       p_rcv_transaction_id     IN              NUMBER,
8149       p_direct_delivery_flag   IN              VARCHAR2
8150    )
8151    IS
8152       c_log_module   CONSTANT VARCHAR2 (80)
8153                                        := module || '.create_accounting_txns';
8154       l_stmt_num              NUMBER (10)                                := 0;
8155       l_consigned_flag        rcv_transactions.consigned_flag%TYPE;
8156       l_source_doc_code       rcv_transactions.source_document_code%TYPE;
8157       l_transaction_type      rcv_transactions.transaction_type%TYPE;
8158       l_parent_trx_id         rcv_transactions.transaction_id%TYPE;
8159       l_parent_trx_type       rcv_transactions.transaction_type%TYPE;
8160       l_grparent_trx_id       rcv_transactions.transaction_id%TYPE;
8161       l_grparent_trx_type     rcv_transactions.transaction_type%TYPE;
8162       l_po_header_id          po_headers_all.po_header_id%TYPE;
8163       l_po_line_location_id   po_line_locations_all.line_location_id%TYPE;
8164       l_shipment_type         po_line_locations_all.shipment_type%TYPE;
8165       l_rcv_accttxn_tbl       gmf_rcv_accounting_pkg.rcv_accttxn_tbl_type;
8166       l_api_version           NUMBER                                   := 1.0;
8167       l_return_status         VARCHAR2 (100);
8168       l_msg_count             NUMBER (38);
8169       l_msg_data              VARCHAR2 (4000);
8170       l_api_message           VARCHAR2 (4000);
8171       l_api_name              VARCHAR2 (30)       := 'CREATE_ACCOUNTING_TXNS';
8172 
8173       p_gl_group_id	NUMBER := NULL;  /* Should be removed */
8174 
8175    BEGIN
8176       SAVEPOINT s_create_accounting_txns;
8177 
8178       IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level)
8179       THEN
8180          fnd_log.STRING (fnd_log.level_procedure, c_log_module, 'Begin...');
8181       END IF;
8182 
8183       -- Standard call to check for call compatibility
8184       IF NOT fnd_api.compatible_api_call (l_api_version,
8185                                           p_api_version,
8186                                           l_api_name,
8187                                           g_pkg_name
8188                                          )
8189       THEN
8190          RAISE fnd_api.g_exc_unexpected_error;
8191       END IF;
8192 
8193       -- Initialize message list if p_init_msg_list is set to TRUE
8194       IF fnd_api.to_boolean (p_init_msg_list)
8195       THEN
8196          fnd_msg_pub.initialize;
8197       END IF;
8198 
8199       -- Initialize API return status to success
8200       x_return_status := fnd_api.g_ret_sts_success;
8201 
8202       SELECT rt.consigned_flag, rt.source_document_code, rt.transaction_type,
8203              rt.parent_transaction_id, rt.po_header_id,
8204              rt.po_line_location_id             --12i Complex Work Procurement
8205         INTO l_consigned_flag, l_source_doc_code, l_transaction_type,
8206              l_parent_trx_id, l_po_header_id,
8207              l_po_line_location_id              --12i Complex Work Procurement
8208         FROM rcv_transactions rt
8209        WHERE transaction_id = p_rcv_transaction_id;
8210 
8211       -- If receiving transaction is for a REQ, or an RMA, we do not
8212       -- do not do any accounting.
8213       IF (l_source_doc_code <> 'PO')
8214       THEN
8215          RETURN;
8216       END IF;
8217 
8218       IF (l_transaction_type IN ('UNORDERED', 'ACCEPT', 'REJECT', 'TRANSFER')
8219          )
8220       THEN
8221          RETURN;
8222       END IF;
8223 
8224       IF (l_parent_trx_id NOT IN (0, -1))
8225       THEN
8226          l_stmt_num := 60;
8227 
8228 -- Get Parent Transaction Type
8229          SELECT transaction_type, parent_transaction_id
8230            INTO l_parent_trx_type, l_grparent_trx_id
8231            FROM rcv_transactions
8232           WHERE transaction_id = l_parent_trx_id;
8233 
8234          IF (l_grparent_trx_id NOT IN (0, -1))
8235          THEN
8236             l_stmt_num := 70;
8237 
8238             -- Get Grand Parent Transaction Type
8239             SELECT transaction_type
8240               INTO l_grparent_trx_type
8241               FROM rcv_transactions
8242              WHERE transaction_id = l_grparent_trx_id;
8243          END IF;
8244       END IF;
8245 
8246       IF (    (   l_transaction_type = 'CORRECT'
8247                OR l_transaction_type = 'RETURN TO VENDOR'
8248               )
8249           AND (l_parent_trx_type = 'UNORDERED')
8250          )
8251       THEN
8252          RETURN;
8253       END IF;
8254 
8255       IF (    (l_transaction_type = 'CORRECT')
8256           AND (l_parent_trx_type = 'RETURN TO VENDOR')
8257           AND (l_grparent_trx_type = 'UNORDERED')
8258          )
8259       THEN
8260          RETURN;
8261       END IF;
8262 
8263 /* Bug 8910852 : moved following code from above*/
8264 	   -- r12: complex work procurement
8265       -- Exclude any transactions whose POLL.shipment_type = 'PREPAYMENT'
8266       SELECT shipment_type
8267         INTO l_shipment_type
8268         FROM po_line_locations
8269        WHERE line_location_id = l_po_line_location_id;
8270 
8271       IF (l_shipment_type = 'PREPAYMENT')
8272       THEN
8273          IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)
8274          THEN
8275             fnd_log.STRING
8276                (fnd_log.level_statement,
8277                 c_log_module,
8278                 'Shipment Type is Prepayment. No Receive accounting required'
8279                );
8280          END IF;
8281 
8282          RETURN;
8283       END IF;
8284 
8285 	  /*Bug 8910852 End */
8286 
8287       IF (   (l_transaction_type = 'RECEIVE')
8288           OR (l_transaction_type = 'MATCH')
8289           OR (l_transaction_type = 'CORRECT' AND l_parent_trx_type = 'RECEIVE'
8290              )
8291           OR (l_transaction_type = 'CORRECT' AND l_parent_trx_type = 'MATCH'
8292              )
8293          )
8294       THEN
8295          l_stmt_num := 80;
8296 
8297          IF     g_debug = 'Y'
8298             AND fnd_log.level_event >= fnd_log.g_current_runtime_level
8299          THEN
8300             fnd_log.STRING (fnd_log.level_event,
8301                             c_log_module || '.' || l_stmt_num,
8302                             'Creating txns For RECEIVE transaction'
8303                            );
8304          END IF;
8305 
8306          create_receive_txns
8307                             (p_api_version               => 1.0,
8308                              x_return_status             => l_return_status,
8309                              x_msg_count                 => l_msg_count,
8310                              x_msg_data                  => l_msg_data,
8311                              p_rcv_transaction_id        => p_rcv_transaction_id,
8312                              p_direct_delivery_flag      => p_direct_delivery_flag,
8313                              p_gl_group_id               => p_gl_group_id
8314                             );
8315       ELSIF (   (l_transaction_type = 'DELIVER')
8316              OR (    l_transaction_type = 'CORRECT'
8317                  AND l_parent_trx_type = 'DELIVER'
8318                 )
8319             )
8320       THEN
8321          l_stmt_num := 90;
8322 
8323          IF (fnd_log.level_event >= fnd_log.g_current_runtime_level)
8324          THEN
8325             fnd_log.STRING (fnd_log.level_event,
8326                             c_log_module || l_stmt_num,
8327                             'Creating Txns For DELIVER transaction'
8328                            );
8329          END IF;
8330 
8331          create_deliver_txns
8332                             (p_api_version               => 1.0,
8333                              x_return_status             => l_return_status,
8334                              x_msg_count                 => l_msg_count,
8335                              x_msg_data                  => l_msg_data,
8336                              p_rcv_transaction_id        => p_rcv_transaction_id,
8337                              p_direct_delivery_flag      => p_direct_delivery_flag,
8338                              p_gl_group_id               => p_gl_group_id
8339                             );
8340       ELSIF (   (l_transaction_type = 'RETURN TO RECEIVING')
8341              OR (    l_transaction_type = 'CORRECT'
8342                  AND l_parent_trx_type = 'RETURN TO RECEIVING'
8343                 )
8344             )
8345       THEN
8346          l_stmt_num := 100;
8347 
8348          IF (fnd_log.level_event >= fnd_log.g_current_runtime_level)
8349          THEN
8350             fnd_log.STRING
8351                          (fnd_log.level_event,
8352                           c_log_module || l_stmt_num,
8353                           'Creating Txns For RETURN TO RECEIVING transaction'
8354                          );
8355          END IF;
8356 
8357          create_rtr_txns (x_return_status             => l_return_status,
8358                           x_msg_count                 => l_msg_count,
8359                           x_msg_data                  => l_msg_data,
8360                           p_rcv_transaction_id        => p_rcv_transaction_id,
8361                           p_direct_delivery_flag      => p_direct_delivery_flag,
8362                           p_gl_group_id               => p_gl_group_id
8363                          );
8364       ELSIF (   (l_transaction_type = 'RETURN TO VENDOR')
8365              OR (    l_transaction_type = 'CORRECT'
8366                  AND l_parent_trx_type = 'RETURN TO VENDOR'
8367                 )
8368             )
8369       THEN
8370          l_stmt_num := 110;
8371 
8372          IF (fnd_log.level_event >= fnd_log.g_current_runtime_level)
8373          THEN
8374             fnd_log.STRING
8375                     (fnd_log.level_event,
8376                      c_log_module || l_stmt_num,
8377                      'Creating transactions For RETURN TO VENDOR transaction'
8378                     );
8379          END IF;
8380 
8381          create_rtv_txns (x_return_status             => l_return_status,
8382                           x_msg_count                 => l_msg_count,
8383                           x_msg_data                  => l_msg_data,
8384                           p_rcv_transaction_id        => p_rcv_transaction_id,
8385                           p_direct_delivery_flag      => p_direct_delivery_flag,
8386                           p_gl_group_id               => p_gl_group_id
8387                          );
8388       END IF;
8389 
8390       IF l_return_status <> fnd_api.g_ret_sts_success
8391       THEN
8392          l_api_message := 'Error creating event';
8393 
8394          IF     g_debug = 'Y'
8395             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
8396          THEN
8397             fnd_log.STRING (fnd_log.level_unexpected,
8398                             c_log_module || l_stmt_num,
8399                                'Create_ReceivingEvents : '
8400                             || l_stmt_num
8401                             || ' : '
8402                             || l_api_message
8403                            );
8404          END IF;
8405 
8406          RAISE fnd_api.g_exc_unexpected_error;
8407       END IF;
8408 
8409       l_stmt_num := 120;
8410 
8411       --- Standard check of p_commit
8412       IF fnd_api.to_boolean (p_commit)
8413       THEN
8414          COMMIT WORK;
8415       END IF;
8416 
8417       -- Standard Call to get message count and if count = 1, get message info
8418       fnd_msg_pub.count_and_get (p_count      => x_msg_count,
8419                                  p_data       => x_msg_data);
8420 
8421       IF (fnd_log.level_procedure >= fnd_log.g_current_runtime_level)
8422       THEN
8423          fnd_log.STRING (fnd_log.level_procedure, c_log_module, '...End');
8424       END IF;
8425 
8426    EXCEPTION
8427       -- rseshadr - return error back to caller
8428       WHEN fnd_api.g_exc_unexpected_error
8429       THEN
8430          ROLLBACK TO s_create_accounting_txns;
8431          x_return_status := fnd_api.g_ret_sts_unexp_error;
8432          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
8433                                     p_data       => x_msg_data
8434                                    );
8435       WHEN OTHERS
8436       THEN
8437          ROLLBACK TO s_create_accounting_txns;
8438          x_return_status := fnd_api.g_ret_sts_unexp_error;
8439 
8440          IF     g_debug = 'Y'
8441             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
8442          THEN
8443             fnd_log.STRING (fnd_log.level_unexpected,
8444                             g_log_head || '.' || l_api_name || l_stmt_num,
8445                                'Create_Accounting_Txns : '
8446                             || l_stmt_num
8447                             || ' : '
8448                             || SUBSTR (SQLERRM, 1, 200)
8449                            );
8450          END IF;
8451 
8452          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
8453          THEN
8454             fnd_msg_pub.add_exc_msg (g_pkg_name,
8455                                         l_api_name
8456                                      || 'Statement -'
8457                                      || TO_CHAR (l_stmt_num)
8458                                     );
8459          END IF;
8460 
8461          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
8462                                     p_data       => x_msg_data
8463                                    );
8464 
8465    END create_accounting_txns;
8466 
8467 --      API name        : Create_AdjustEvents
8468 --      Type            : Private
8469 --      Function        : To seed accounting events for retroactive price adjustments.
8470 --      Pre-reqs        :
8471 --      Parameters      :
8472 --      IN              :       p_api_version           IN NUMBER       Required
8473 --                              p_init_msg_list         IN VARCHAR2     Optional
8474 --                                      Default = FND_API.G_FALSE
8475 --                              p_commit                IN VARCHAR2     Optional
8476 --                                      Default = FND_API.G_FALSE
8477 --                              p_validation_level      IN NUMBER       Optional
8478 --                                      Default = FND_API.G_VALID_LEVEL_FULL
8479 --                              p_po_header_id          IN NUMBER       Required
8480 --                              p_po_release_id         IN NUMBER       Optional
8481 --                              p_po_line_id            IN NUMBER       Optional
8482 --                              p_po_line_location_id   IN NUMBER       Required
8483 --                              p_old_po_price          IN NUMBER       Required
8484 --                              p_new_po_price          IN NUMBER       Required
8485 --
8486 --      OUT             :       x_return_status         OUT     VARCHAR2(1)
8487 --                              x_msg_count                     OUT     NUMBER
8488 --                              x_msg_data                      OUT     VARCHAR2(2000)
8489 --      Version :
8490 --                        Initial version       1.0
8491 --
8492 --      Notes           : This API creates all accounting events for retroactive price adjustments
8493 --                        in GMF_RCV_ACCOUNTING_TXNS. For online accruals, it also generates
8494 --                        the accounting entries for the event.
8495 --
8496 -- End of comments
8497 -------------------------------------------------------------------------------
8498    PROCEDURE create_adjust_txns (
8499       p_api_version           IN              NUMBER,
8500       p_init_msg_list         IN              VARCHAR2,
8501       p_commit                IN              VARCHAR2,
8502       p_validation_level      IN              NUMBER,
8503       x_return_status         OUT NOCOPY      VARCHAR2,
8504       x_msg_count             OUT NOCOPY      NUMBER,
8505       x_msg_data              OUT NOCOPY      VARCHAR2,
8506       p_po_header_id          IN              NUMBER,
8507       p_po_release_id         IN              NUMBER,
8508       p_po_line_id            IN              NUMBER,
8509       p_po_line_location_id   IN              NUMBER,
8510       p_old_po_price          IN              NUMBER,
8511       p_new_po_price          IN              NUMBER
8512    )
8513    IS
8514       l_api_name      CONSTANT VARCHAR2 (30)          := 'Create_Adjust_Txns';
8515       l_api_version   CONSTANT NUMBER                                  := 1.0;
8516       l_return_status          VARCHAR2 (1)      := fnd_api.g_ret_sts_success;
8517       l_msg_count              NUMBER                                    := 0;
8518       l_msg_data               VARCHAR2 (8000)                          := '';
8519       l_stmt_num               NUMBER                                    := 0;
8520       l_api_message            VARCHAR2 (1000);
8521       l_rcv_accttxn            gmf_rcv_accounting_pkg.rcv_accttxn_rec_type;
8522       l_rcv_accttxn_tbl        gmf_rcv_accounting_pkg.rcv_accttxn_tbl_type;
8523       l_rae_count              NUMBER;
8524 -- 12i Complex Work Procurement------------------------------------
8525       l_matching_basis         po_line_locations.matching_basis%TYPE;
8526       l_shipment_type          po_line_locations.shipment_type%TYPE;
8527 -------------------------------------------------------------------
8528       l_proc_operating_unit    NUMBER;
8529       l_po_distribution_id     NUMBER;
8530       l_organization_id        NUMBER;
8531       l_rcv_quantity           NUMBER                                    := 0;
8532       l_delived_quantity       NUMBER                                    := 0;
8533       l_trx_flow_header_id     NUMBER                                 := NULL;
8534       l_drop_ship_flag         NUMBER                                 := NULL;
8535       l_opm_flag               NUMBER;
8536       l_cr_flag                BOOLEAN;
8537       l_process_enabled_flag   mtl_parameters.process_enabled_flag%TYPE; /* INVCONV ANTHIYAG Bug#5529309 18-Sep-2006 */
8538 
8539 -- Cursor to get all parent receive transactions
8540 -- for a given po_header or po_release
8541       CURSOR c_parent_receive_txns_csr
8542       IS
8543       SELECT      a.transaction_id, a.organization_id
8544       FROM        rcv_transactions a, mtl_parameters b
8545       WHERE       (
8546                   (a.transaction_type = 'RECEIVE' AND a.parent_transaction_id = -1)
8547                   OR
8548                   a.transaction_type = 'MATCH'
8549                   )
8550       AND         NVL (a.consigned_flag, 'N') <> 'Y'
8551       AND         a.po_header_id = p_po_header_id
8552       AND         a.organization_id = b.organization_id
8553       AND         NVL(b.process_enabled_flag, 'N') = 'Y'
8554       AND         a.po_line_location_id = p_po_line_location_id
8555       AND         NVL (a.po_release_id, -1) = NVL (p_po_release_id, -1);
8556 
8557 -- Cursor to get all deliver transactions for
8558 -- a parent receive transaction.
8559       CURSOR c_deliver_txns_csr (l_par_txn IN NUMBER)
8560       IS
8561       SELECT      a.transaction_id, a.po_distribution_id
8562       FROM        rcv_transactions a, mtl_parameters b
8563       WHERE       a.transaction_type = 'DELIVER'
8564       AND         a.organization_id = b.organization_id
8565       AND         NVL(b.process_enabled_flag, 'N') = 'Y'
8566       AND         a.parent_transaction_id = l_par_txn;
8567 
8568 -- Cursor to get all distributions corresponding
8569 -- to a po_line_location.
8570 -- The PO line location corresponds to the parent rcv_transaction
8571       CURSOR c_po_dists_csr (l_rcv_txn IN NUMBER)
8572       IS
8573          SELECT pod.po_distribution_id
8574            FROM po_distributions pod,
8575                 po_line_locations poll,
8576                 rcv_transactions rt
8577           WHERE pod.line_location_id = poll.line_location_id
8578             AND poll.line_location_id = rt.po_line_location_id
8579             AND rt.transaction_id = l_rcv_txn;
8580    BEGIN
8581 
8582 /* INVCONV ANTHIYAG Bug#5529309 18-Sep-2006 Start */
8583      BEGIN
8584       SELECT        nvl(b.process_enabled_flag, 'N')
8585       INTO          l_process_enabled_flag
8586       FROM          po_line_locations_all a,
8587                     mtl_parameters b
8588       WHERE         a.line_location_id = p_po_line_location_id
8589       AND           b.organization_id = a.ship_to_organization_id;
8590      EXCEPTION
8591        WHEN OTHERS THEN
8592          l_process_enabled_flag := 'N';
8593      END;
8594      IF nvl(l_process_enabled_flag, 'N') <> 'Y' THEN
8595        x_return_status := fnd_api.g_ret_sts_success;
8596        x_msg_count := 0;
8597        x_msg_data := NULL;
8598        RETURN;
8599      END IF;
8600 /* INVCONV ANTHIYAG Bug#5529309 18-Sep-2006 End */
8601 
8602       -- Standard start of API savepoint
8603       SAVEPOINT create_adjust_txns_pvt;
8604       l_stmt_num := 0;
8605 
8606       IF     g_debug = 'Y'
8607          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
8608       THEN
8609          fnd_log.STRING (fnd_log.level_procedure,
8610                          g_log_head || '.' || l_api_name || '.begin',
8611                          'Create_Adjust_Txns <<'
8612                         );
8613       END IF;
8614 
8615       -- Standard call to check for call compatibility
8616       IF NOT fnd_api.compatible_api_call (l_api_version,
8617                                           p_api_version,
8618                                           l_api_name,
8619                                           g_pkg_name
8620                                          )
8621       THEN
8622          RAISE fnd_api.g_exc_unexpected_error;
8623       END IF;
8624 
8625       -- Initialize message list if p_init_msg_list is set to TRUE
8626       IF fnd_api.to_boolean (p_init_msg_list)
8627       THEN
8628          fnd_msg_pub.initialize;
8629       END IF;
8630 
8631       -- Initialize API return status to success
8632       x_return_status := fnd_api.g_ret_sts_success;
8633 
8634       -- If the old and new price are the same, return
8635       IF p_old_po_price = p_new_po_price
8636       THEN
8637          IF     g_debug = 'Y'
8638             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
8639          THEN
8640             fnd_log.STRING
8641                (fnd_log.level_statement,
8642                 g_log_head || '.' || l_api_name,
8643                 'Old and New Prices are same. No Adjust Transactions created'
8644                );
8645          END IF;
8646 
8647          RETURN;
8648       END IF;
8649 
8650       -- If OPM PO and Common Purchasing is installed, we do not do any
8651       -- accounting.
8652       l_stmt_num := 5;
8653       l_opm_flag := gml_opm_po.check_opm_po (p_po_header_id);
8654       l_stmt_num := 10;
8655       l_cr_flag := gml_po_for_process.check_po_for_proc;
8656 
8657       IF (l_opm_flag = 1 AND l_cr_flag = FALSE)
8658       THEN
8659          RETURN;
8660       END IF;
8661 
8662       l_stmt_num := 20;
8663 
8664       -- Get Matching Basis and Shipment Type
8665       SELECT poll.matching_basis, poll.shipment_type
8666         INTO l_matching_basis, l_shipment_type
8667         FROM po_line_locations poll
8668        WHERE poll.line_location_id = p_po_line_location_id;
8669 
8670       l_stmt_num := 30;
8671 
8672       -- If Line Type is Service (matching basis = AMOUNT), then return without doing anything
8673       IF (l_matching_basis IS NOT NULL AND l_matching_basis = 'AMOUNT')
8674       THEN
8675          IF     g_debug = 'Y'
8676             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
8677          THEN
8678             fnd_log.STRING
8679                          (fnd_log.level_statement,
8680                           g_log_head || '.' || l_api_name,
8681                           'Service Line Type. No Adjust Transactions created'
8682                          );
8683          END IF;
8684 
8685          RETURN;
8686       END IF;
8687 
8688       l_stmt_num := 35;
8689 
8690       -- If Shipment Type is Prepayment, then return without doing anything
8691       IF (l_shipment_type = 'PREPAYMENT')
8692       THEN
8693          IF     g_debug = 'Y'
8694             AND fnd_log.level_statement >= fnd_log.g_current_runtime_level
8695          THEN
8696             fnd_log.STRING
8697                (fnd_log.level_statement,
8698                 g_log_head || '.' || l_api_name,
8699                 'Shipment Type is Prepayment. No Adjust Transactions created'
8700                );
8701          END IF;
8702 
8703          RETURN;
8704       END IF;
8705 
8706       l_stmt_num := 40;
8707 
8708       -- Get Procuring operating unit
8709       SELECT org_id
8710         INTO l_proc_operating_unit
8711         FROM po_headers
8712        WHERE po_header_id = p_po_header_id;
8713 
8714       -- Loop Through all Parent Transactions
8715       FOR c_par_txn IN c_parent_receive_txns_csr
8716       LOOP
8717          -- Get the Organization in which event is to be seeded
8718          -- Get the row in GRAT with the RCV_TRANSACTION as that of the parent txn
8719          -- and procurement_org_flag = 'Y'
8720          l_stmt_num := 50;
8721 
8722          SELECT COUNT (*)
8723            INTO l_rae_count
8724            FROM gmf_rcv_accounting_txns
8725           WHERE rcv_transaction_id = c_par_txn.transaction_id;
8726 
8727          IF l_rae_count > 0
8728          THEN
8729             -- Rownum check is there since there might be multiple events in
8730             -- GRAT for a particular Receive transaction in RCV_TRANSACTIONS
8731             l_stmt_num := 60;
8732 
8733             SELECT grat.organization_id, grat.trx_flow_header_id,
8734                    NVL (rt.dropship_type_code, 3)
8735               INTO l_organization_id, l_trx_flow_header_id,
8736                    l_drop_ship_flag
8737               FROM gmf_rcv_accounting_txns grat, rcv_transactions rt
8738              WHERE grat.rcv_transaction_id = c_par_txn.transaction_id
8739                AND rt.transaction_id = grat.rcv_transaction_id
8740                AND grat.procurement_org_flag = 'Y'
8741                AND ROWNUM = 1;
8742          ELSE
8743             l_organization_id := c_par_txn.organization_id;
8744          END IF;
8745 
8746          -- One event is seeded per PO distribution
8747          -- If RCV_TRANSACTIONS has the po_distribution_id populated, we
8748          -- use that. Otherwise, we use cursor c_po_dists_csr to seed as many events
8749          -- as the number of distributions for the line_location
8750          l_stmt_num := 70;
8751 
8752          SELECT NVL (po_distribution_id, -1)
8753            INTO l_po_distribution_id
8754            FROM rcv_transactions
8755           WHERE transaction_id = c_par_txn.transaction_id;
8756 
8757          IF l_po_distribution_id <> -1
8758          THEN
8759             l_stmt_num := 80;
8760             insert_txn (x_return_status                     => l_return_status,
8761                         x_msg_count                         => l_msg_count,
8762                         x_msg_data                          => l_msg_data,
8763                         p_event_source                      => 'RETROPRICE',
8764                         p_event_type_id                     => adjust_receive,
8765                         p_rcv_transaction_id                => c_par_txn.transaction_id,
8766                         p_inv_distribution_id               => NULL,
8767                         p_po_distribution_id                => l_po_distribution_id,
8768                         p_direct_delivery_flag              => NULL,
8769                         p_gl_group_id                       => NULL,
8770                         p_cross_ou_flag                     => NULL,
8771                         p_procurement_org_flag              => 'Y',
8772                         p_ship_to_org_flag                  => NULL,
8773                         p_drop_ship_flag                    => l_drop_ship_flag,
8774                         p_org_id                            => l_proc_operating_unit,
8775                         p_organization_id                   => l_organization_id,
8776                         p_transfer_org_id                   => NULL,
8777                         p_transfer_organization_id          => NULL,
8778                         p_trx_flow_header_id                => l_trx_flow_header_id,
8779                         p_transaction_forward_flow_rec      => NULL,
8780                         p_transaction_reverse_flow_rec      => NULL,
8781                         p_unit_price                        => p_new_po_price,
8782                         p_prior_unit_price                  => p_old_po_price,
8783                         x_rcv_accttxn                       => l_rcv_accttxn
8784                        );
8785 
8786             -- Suppose there is no net quantity for this receipt (all received quantity has been
8787             -- returned), there is no need to seed an event, since there is no accrual to adjust.
8788             -- If transaction quantity is 0, the Insert_Txn API will return a warning. In the
8789             -- case of Adjust events, this warning is normal and should be ignored.
8790             IF (l_return_status = fnd_api.g_ret_sts_success)
8791             THEN
8792                l_rcv_accttxn_tbl (l_rcv_accttxn_tbl.COUNT + 1) :=
8793                                                                 l_rcv_accttxn;
8794             ELSIF (l_return_status <> 'W')
8795             THEN
8796                l_api_message := 'Error seeding Transactions';
8797 
8798                IF     g_debug = 'Y'
8799                   AND fnd_log.level_unexpected >=
8800                                                fnd_log.g_current_runtime_level
8801                THEN
8802                   fnd_log.STRING (fnd_log.level_unexpected,
8803                                   g_log_head || '.' || l_api_name
8804                                   || l_stmt_num,
8805                                      'Create_Adjust_Txns : '
8806                                   || l_stmt_num
8807                                   || ' : '
8808                                   || l_api_message
8809                                  );
8810                END IF;
8811 
8812                RAISE fnd_api.g_exc_unexpected_error;
8813             END IF;
8814          ELSE
8815             FOR c_po_dist IN c_po_dists_csr (c_par_txn.transaction_id)
8816             LOOP
8817                l_stmt_num := 90;
8818                insert_txn
8819                        (x_return_status                     => l_return_status,
8820                         x_msg_count                         => l_msg_count,
8821                         x_msg_data                          => l_msg_data,
8822                         p_event_source                      => 'RETROPRICE',
8823                         p_event_type_id                     => adjust_receive,
8824                         p_rcv_transaction_id                => c_par_txn.transaction_id,
8825                         p_inv_distribution_id               => NULL,
8826                         p_po_distribution_id                => c_po_dist.po_distribution_id,
8827                         p_direct_delivery_flag              => NULL,
8828                         p_gl_group_id                       => NULL,
8829                         p_cross_ou_flag                     => NULL,
8830                         p_procurement_org_flag              => 'Y',
8831                         p_ship_to_org_flag                  => NULL,
8832                         p_drop_ship_flag                    => l_drop_ship_flag,
8833                         p_org_id                            => l_proc_operating_unit,
8834                         p_organization_id                   => l_organization_id,
8835                         p_transfer_org_id                   => NULL,
8836                         p_transfer_organization_id          => NULL,
8837                         p_trx_flow_header_id                => l_trx_flow_header_id,
8838                         p_transaction_forward_flow_rec      => NULL,
8839                         p_transaction_reverse_flow_rec      => NULL,
8840                         p_unit_price                        => p_new_po_price,
8841                         p_prior_unit_price                  => p_old_po_price,
8842                         x_rcv_accttxn                       => l_rcv_accttxn
8843                        );
8844 
8845                -- Suppose there is no net quantity for this receipt (all received quantity has been
8846                -- returned), there is no need to seed an event, since there is no accrual to adjust.
8847                -- If transaction quantity is 0, the Insert_Txn API will return a warning. In the
8848                -- case of Adjust events, this warning is normal and should be ignored.
8849                IF (l_return_status = fnd_api.g_ret_sts_success)
8850                THEN
8851                   l_rcv_accttxn_tbl (l_rcv_accttxn_tbl.COUNT + 1) :=
8852                                                                 l_rcv_accttxn;
8853                ELSIF (l_return_status <> 'W')
8854                THEN
8855                   l_api_message := 'Error seeding Transactions';
8856 
8857                   IF     g_debug = 'Y'
8858                      AND fnd_log.level_unexpected >=
8859                                                fnd_log.g_current_runtime_level
8860                   THEN
8861                      fnd_log.STRING (fnd_log.level_unexpected,
8862                                         g_log_head
8863                                      || '.'
8864                                      || l_api_name
8865                                      || l_stmt_num,
8866                                         'Create_Adjust_Txns : '
8867                                      || l_stmt_num
8868                                      || ' : '
8869                                      || l_api_message
8870                                     );
8871                   END IF;
8872 
8873                   RAISE fnd_api.g_exc_unexpected_error;
8874                END IF;
8875             END LOOP;
8876          END IF;
8877 
8878          -- Adjust Deliver events are not created for global procurement scenarios.
8879          IF (    l_trx_flow_header_id IS NULL
8880              AND (l_drop_ship_flag IS NULL OR l_drop_ship_flag NOT IN (1, 2)
8881                  )
8882             )
8883          THEN
8884             l_stmt_num := 100;
8885 
8886             FOR c_del_txn IN c_deliver_txns_csr (c_par_txn.transaction_id)
8887             LOOP
8888                insert_txn
8889                        (x_return_status                     => l_return_status,
8890                         x_msg_count                         => l_msg_count,
8891                         x_msg_data                          => l_msg_data,
8892                         p_event_source                      => 'RETROPRICE',
8893                         p_event_type_id                     => adjust_deliver,
8894                         p_rcv_transaction_id                => c_del_txn.transaction_id,
8895                         p_inv_distribution_id               => NULL,
8896                         p_po_distribution_id                => c_del_txn.po_distribution_id,
8897                         p_direct_delivery_flag              => NULL,
8898                         p_gl_group_id                       => NULL,
8899                         p_cross_ou_flag                     => NULL,
8900                         p_procurement_org_flag              => 'Y',
8901                         p_ship_to_org_flag                  => NULL,
8902                         p_drop_ship_flag                    => l_drop_ship_flag,
8903                         p_org_id                            => l_proc_operating_unit,
8904                         p_organization_id                   => l_organization_id,
8905                         p_transfer_org_id                   => NULL,
8906                         p_transfer_organization_id          => NULL,
8907                         p_trx_flow_header_id                => l_trx_flow_header_id,
8908                         p_transaction_forward_flow_rec      => NULL,
8909                         p_transaction_reverse_flow_rec      => NULL,
8910                         p_unit_price                        => p_new_po_price,
8911                         p_prior_unit_price                  => p_old_po_price,
8912                         x_rcv_accttxn                       => l_rcv_accttxn
8913                        );
8914 
8915                -- Suppose there is no net quantity for this deliver (all delivered quantity has been
8916                -- returned), there is no need to seed an event, since there is no accrual to adjust.
8917                -- If transaction quantity is 0, the Insert_Txn API will return a warning. In the
8918                -- case of Adjust events, this warning is normal and should be ignored.
8919                IF (l_return_status = fnd_api.g_ret_sts_success)
8920                THEN
8921                   l_rcv_accttxn_tbl (l_rcv_accttxn_tbl.COUNT + 1) :=
8922                                                                 l_rcv_accttxn;
8923                ELSIF (l_return_status <> 'W')
8924                THEN
8925                   l_api_message := 'Error seeding Transactions';
8926 
8927                   IF     g_debug = 'Y'
8928                      AND fnd_log.level_unexpected >=
8929                                                fnd_log.g_current_runtime_level
8930                   THEN
8931                      fnd_log.STRING (fnd_log.level_unexpected,
8932                                         g_log_head
8933                                      || '.'
8934                                      || l_api_name
8935                                      || l_stmt_num,
8936                                         'Create_Adjust_Txns : '
8937                                      || l_stmt_num
8938                                      || ' : '
8939                                      || l_api_message
8940                                     );
8941                   END IF;
8942 
8943                   RAISE fnd_api.g_exc_unexpected_error;
8944                END IF;
8945             END LOOP;                                             -- C_DEL_TXN
8946          END IF;                                 -- If Trx Flow does not exist
8947       END LOOP;                                                  -- C_PAR_TXNS
8948 
8949       IF (l_rcv_accttxn_tbl.COUNT > 0)
8950       THEN
8951          l_stmt_num := 110;
8952          insert_txn2 (x_return_status        => l_return_status,
8953                       x_msg_count            => l_msg_count,
8954                       x_msg_data             => l_msg_data,
8955                       p_rcv_accttxn_tbl      => l_rcv_accttxn_tbl
8956                      );
8957 
8958          IF l_return_status <> fnd_api.g_ret_sts_success
8959          THEN
8960             l_api_message := 'Error inserting Transactions into GRAT';
8961 
8962             IF     g_debug = 'Y'
8963                AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
8964             THEN
8965                fnd_log.STRING (fnd_log.level_unexpected,
8966                                g_log_head || '.' || l_api_name || l_stmt_num,
8967                                   'Create_Adjust_Txns : '
8968                                || l_stmt_num
8969                                || ' : '
8970                                || l_api_message
8971                               );
8972             END IF;
8973 
8974             RAISE fnd_api.g_exc_unexpected_error;
8975          END IF;
8976       END IF;
8977 
8978       --- Standard check of p_commit
8979       IF fnd_api.to_boolean (p_commit)
8980       THEN
8981          COMMIT WORK;
8982       END IF;
8983 
8984       -- Standard Call to get message count and if count = 1, get message info
8985       fnd_msg_pub.count_and_get (p_count      => x_msg_count,
8986                                  p_data       => x_msg_data);
8987 
8988       IF     g_debug = 'Y'
8989          AND fnd_log.level_procedure >= fnd_log.g_current_runtime_level
8990       THEN
8991          fnd_log.STRING (fnd_log.level_procedure,
8992                          g_log_head || '.' || l_api_name || '.end',
8993                          'Create_Adjust_Txns >>'
8994                         );
8995       END IF;
8996    EXCEPTION
8997       WHEN fnd_api.g_exc_error
8998       THEN
8999          ROLLBACK TO create_adjust_txns_pvt;
9000          x_return_status := fnd_api.g_ret_sts_error;
9001          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
9002                                     p_data       => x_msg_data
9003                                    );
9004       WHEN fnd_api.g_exc_unexpected_error
9005       THEN
9006          ROLLBACK TO create_adjust_txns_pvt;
9007          x_return_status := fnd_api.g_ret_sts_unexp_error;
9008          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
9009                                     p_data       => x_msg_data
9010                                    );
9011       WHEN OTHERS
9012       THEN
9013          ROLLBACK TO create_adjust_txns_pvt;
9014          x_return_status := fnd_api.g_ret_sts_unexp_error;
9015 
9016          IF     g_debug = 'Y'
9017             AND fnd_log.level_unexpected >= fnd_log.g_current_runtime_level
9018          THEN
9019             fnd_log.STRING (fnd_log.level_unexpected,
9020                             g_log_head || '.' || l_api_name || l_stmt_num,
9021                                'Create_Adjust_Txns : '
9022                             || l_stmt_num
9023                             || ' : '
9024                             || SUBSTR (SQLERRM, 1, 200)
9025                            );
9026          END IF;
9027 
9028          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_unexp_error)
9029          THEN
9030             fnd_msg_pub.add_exc_msg (g_pkg_name,
9031                                         l_api_name
9032                                      || 'Statement -'
9033                                      || TO_CHAR (l_stmt_num)
9034                                     );
9035          END IF;
9036 
9037          fnd_msg_pub.count_and_get (p_count      => x_msg_count,
9038                                     p_data       => x_msg_data
9039                                    );
9040    END create_adjust_txns;
9041 END gmf_rcv_accounting_pkg;