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