DBA Data[Home] [Help]

PACKAGE BODY: APPS.CST_PRJMFG_COST_COLLECTOR

Source


1 PACKAGE BODY CST_PRJMFG_COST_COLLECTOR as
2 /* $Header: CSTPPCCB.pls 120.32.12020000.6 2013/06/13 12:21:26 vnagasow ship $*/
3 
4 /*----------------------------------------------------------------------------*
5  |  PRIVATE FUNCTION/PROCEDURES                                               |
6  *----------------------------------------------------------------------------*/
7 
8   PROCEDURE  pm_process_txn_mmt (
9                                  p_Group_Id                     NUMBER,
10                                  p_transaction_id                NUMBER,
11                                  p_organization_id                NUMBER,
12                                  p_transaction_action_id        NUMBER,
13                                  p_transaction_source_type_id        NUMBER,
14                                  p_type_class                        NUMBER,
15                                  p_project_id                        NUMBER,
16                                  p_task_id                        NUMBER,
17                                  p_transaction_date                DATE,
18                                  p_primary_quantity                NUMBER,
19                                  p_expenditure_type                VARCHAR2,
20                                  p_item_description                VARCHAR2,
21                                  p_cost_group_id                NUMBER,
22                                  p_transfer_cost_group_id        NUMBER,
23                                  p_inventory_item_id                NUMBER,
24                                  p_transaction_source_id        NUMBER,
25                                  p_to_project_id                NUMBER,
26                                  p_to_task_id                        NUMBER,
27                                  p_source_project_id                NUMBER,
28                                  p_source_task_id                NUMBER,
29                                  p_transfer_transaction_id        NUMBER,
30                                  p_primary_cost_method                NUMBER,
31                                  p_std_cg_acct                  NUMBER, -- Added for bug 3495967
32                                  p_acct_period_id                NUMBER,
33                                  p_exp_org_id                        NUMBER,
34                                  p_distribution_account_id        NUMBER,
35                                  p_proj_job_ind                        NUMBER,
36                                  p_first_matl_se_exp_type        VARCHAR2,
37                                  p_inv_txn_source_literal        VARCHAR2,
38                                  p_cap_txn_source_literal        VARCHAR2,
39                                  p_inv_syslink_literal                VARCHAR2,
40                                  p_bur_syslink_literal                VARCHAR2,
41                                  p_wip_syslink_literal                VARCHAR2,
42                                  p_user_def_exp_type            NUMBER,
43                                  O_err_num                  OUT        NOCOPY NUMBER,
44                                  O_err_code                  OUT        NOCOPY VARCHAR2,
45                                  O_err_msg                  OUT        NOCOPY VARCHAR2,
46                                  p_transfer_organization_id     NUMBER,
47                                  p_flow_schedule                VARCHAR2,
48                                  p_si_asset_yes_no                NUMBER,
49                                  p_transfer_si_asset_yes_no        NUMBER,
50                                  p_denom_currency_code          VARCHAR2);
51 
52   PROCEDURE  pm_check_error_mmt (
53                                  p_transaction_id               NUMBER,
54                                  p_organization_id                NUMBER,
55                                  p_cost_method                        NUMBER,
56                                  p_inventory_item_id                NUMBER,
57                                  p_avg_rates_cost_type_id        NUMBER,
58                                  p_transaction_action_id        NUMBER,
59                                  p_transaction_source_type_id        NUMBER,
60                                  p_type_class                        NUMBER,
61                                  p_project_id                        NUMBER,
62                                  p_task_id                        NUMBER,
63                                  p_to_project_id                NUMBER,
64                                  p_to_task_id                        NUMBER,
65                                  p_source_project_id                NUMBER,
66                                  p_source_task_id                NUMBER,
67                                  p_transaction_source_id        NUMBER,
68                                  p_proj_job_ind                   OUT         NOCOPY NUMBER,
69                                  p_process_yn                   OUT         NOCOPY NUMBER,
70                                  p_first_matl_se_exp_type  OUT         NOCOPY VARCHAR2,
71                                  p_user_id                        NUMBER,
72                                  p_login_id                        NUMBER,
73                                  p_req_id                        NUMBER,
74                                  p_prg_appl_id                         NUMBER,
75                                  p_prg_id                         NUMBER,
76                                  O_err_num                   OUT        NOCOPY NUMBER,
77                                  O_err_code                   OUT        NOCOPY VARCHAR2,
78                                  O_err_msg                   OUT        NOCOPY VARCHAR2,
79                                  p_flow_schedule                VARCHAR2,
80                                  p_cost_group_id                NUMBER);
81 
82   PROCEDURE pm_mark_error_mmt (
83                                 p_transaction_id        NUMBER,
84                                 p_error_code                VARCHAR2,
85                                 p_error_explanation        VARCHAR2,
86                                 p_user_id                NUMBER,
87                                 p_login_id                NUMBER,
88                                 p_req_id                NUMBER,
89                                 p_prg_appl_id                 NUMBER,
90                                 p_prg_id                 NUMBER,
91                                 O_err_num            OUT        NOCOPY NUMBER,
92                                 O_err_code            OUT        NOCOPY VARCHAR2,
93                                 O_err_msg            OUT        NOCOPY VARCHAR2);
94 
95 
96   PROCEDURE  pm_process_txn_wt (
97                                 p_Group_Id                      NUMBER,
98                                 p_business_group_name           VARCHAR2,
99                                 p_transaction_id                NUMBER,
100                                 p_organization_id                NUMBER,
101                                 p_employee_number                VARCHAR2,
102                                 p_department_id                        NUMBER,
103                                 p_project_id                        NUMBER,
104                                 p_task_id                        NUMBER,
105                                 p_transaction_date                DATE,
106                                 p_base_transaction_value        NUMBER,
107                                 p_primary_quantity                NUMBER,
108                                 p_acct_period_id                NUMBER,
109                                 p_expenditure_type                VARCHAR2,
110                                 p_resource_description                VARCHAR2,
111                                 p_wt_transaction_type                NUMBER,
112                                 p_cost_element_id                NUMBER,
113                                 p_exp_org_name                        VARCHAR2,
114                                 p_wip_txn_source_literal        VARCHAR2,
115                                 p_wip_straight_time_literal     VARCHAR2,
116                                 p_wip_syslink_literal                VARCHAR2,
117                                 p_bur_syslink_literal                VARCHAR2,
118                                 O_err_num                 OUT        NOCOPY NUMBER,
119                                 O_err_code                 OUT        NOCOPY VARCHAR2,
120                                 O_err_msg                 OUT        NOCOPY VARCHAR2,
121                                 p_reference_account                NUMBER,
122                                 p_cr_account                        NUMBER,
123                                 p_wip_dr_sub_ledger_id              NUMBER,
124                                 p_wip_cr_sub_ledger_id              NUMBER,
125                                 p_wip_entity_id                        NUMBER,
126                                 p_resource_id                        NUMBER,
127                                 p_basis_resource_id                NUMBER,
128                                 p_denom_currency_code           VARCHAR2,
129 				p_person_type                   VARCHAR2);
130 
131   PROCEDURE  pm_check_error_wt (
132                                 p_transaction_id                NUMBER,
133                                      p_project_id                        NUMBER,
134                                      p_task_id                        NUMBER,
135                                      p_expenditure_type                VARCHAR2,
136                                      p_organization_id                NUMBER,
137                                      p_department_id                        NUMBER,
138                                      p_employee_number                VARCHAR2,
139                                      p_exp_org_name               OUT        NOCOPY VARCHAR2,
140                                      p_process_yn                 OUT         NOCOPY NUMBER,
141                                 p_user_id                        NUMBER,
142                                 p_login_id                        NUMBER,
143                                 p_req_id                        NUMBER,
144                                 p_prg_appl_id                         NUMBER,
145                                 p_prg_id                         NUMBER,
146                                      O_err_num                 OUT        NOCOPY NUMBER,
147                                      O_err_code                 OUT        NOCOPY VARCHAR2,
148                                      O_err_msg                 OUT        NOCOPY VARCHAR2);
149 
150   PROCEDURE  pm_mark_error_wt (
151                                 p_transaction_id                NUMBER,
152                                 p_error_code                        VARCHAR2,
153                                 p_error_explanation                VARCHAR2,
154                                 p_user_id                        NUMBER,
155                                 p_login_id                        NUMBER,
156                                 p_req_id                        NUMBER,
157                                 p_prg_appl_id                         NUMBER,
158                                 p_prg_id                         NUMBER,
159                                      O_err_num                 OUT        NOCOPY NUMBER,
160                                      O_err_code                 OUT        NOCOPY VARCHAR2,
161                                      O_err_msg                 OUT        NOCOPY VARCHAR2);
162 
163   PROCEDURE  pm_insert_pti_pvt
164                   (p_transaction_source                        VARCHAR2,
165                       p_batch_name                                VARCHAR2,
166                       p_expenditure_ending_date                DATE,
167                       p_employee_number                        VARCHAR2,
168                       p_organization_name                        VARCHAR2,
169                       p_expenditure_item_date                DATE,
170                       p_project_number                        VARCHAR2,
171                       p_task_number                        VARCHAR2,
172                       p_expenditure_type                        VARCHAR2,
173                       p_pa_quantity                        NUMBER,
174                       p_raw_cost                                NUMBER,
175                       p_expenditure_comment                VARCHAR2,
176                       p_orig_transaction_reference                VARCHAR2,
177                       p_raw_cost_rate                        NUMBER,
178                       p_unmatched_negative_txn_flag        VARCHAR2,
179                       p_gl_date                                DATE,
180                    p_org_id                                NUMBER,
181                    p_burdened_cost                        NUMBER,
182                    p_burdened_cost_rate                        NUMBER,
183                    p_system_linkage                        VARCHAR2,
184                    p_transaction_status_code                VARCHAR2,
185                    p_denom_currency_code                VARCHAR2,
186 
187                    p_transaction_id                     NUMBER,
188                    p_transaction_action_id              NUMBER,
189                    p_transaction_source_type_id         NUMBER,
190                    p_organization_id                    NUMBER,
191                    p_inventory_item_id                  NUMBER,
192                    p_cost_element_id                    NUMBER,
193                    p_resource_id                        NUMBER,
194                    p_source_flag                        NUMBER,
195                    p_variance_flag                      NUMBER,
196                    p_primary_quantity                   NUMBER,
197                    p_transfer_organization_id           NUMBER,
198                    p_fob_point                          NUMBER,
199                    p_wip_entity_id                      NUMBER,
200                    p_basis_resource                     NUMBER,
201 
202                    p_type_class                         NUMBER,
203                    p_project_id                         NUMBER,
204                    p_task_id                            NUMBER,
205                    p_transaction_date                   DATE,
206                    p_cost_group_id                      NUMBER,
207                    p_transfer_cost_group_id             NUMBER,
208                    p_transaction_source_id              NUMBER,
209                    p_to_project_id                        NUMBER,
210                    p_to_task_id                         NUMBER,
211                    p_source_project_id                  NUMBER,
212                    p_source_task_id                     NUMBER,
213                    p_transfer_transaction_id            NUMBER,
214                    p_primary_cost_method                NUMBER,
215                    p_acct_period_id                     NUMBER,
216                    p_exp_org_id                         NUMBER,
217                    p_distribution_account_id            NUMBER,
218                    p_proj_job_ind                       NUMBER,
219                    p_first_matl_se_exp_type             VARCHAR2,
220                    p_inv_txn_source_literal             VARCHAR2,
221                    p_cap_txn_source_literal             VARCHAR2,
222                    p_inv_syslink_literal                VARCHAR2,
223                    p_bur_syslink_literal                VARCHAR2,
224                    p_wip_syslink_literal                VARCHAR2,
225                    p_user_def_exp_type                  VARCHAR2,
226                    p_flow_schedule                      VARCHAR2,
227                    p_si_asset_yes_no                    NUMBER,
228                    p_transfer_si_asset_yes_no           NUMBER,
229 
230                    O_err_num                  OUT       NOCOPY NUMBER,
231                    O_err_code                 OUT       NOCOPY VARCHAR2,
232                    O_err_msg                  OUT       NOCOPY VARCHAR2
233                   );
234 
235 /*----------------------------------------------------------------------------*
236  | PUBLIC PROCEDURE                                                           |
237  |    pm_mark_non_project_world_txns                                          |
238  |                                                                            |
239  | DESCRIPTION                                                                |
240  |    This procedure would mark all non-project world transactions as Cost    |
241  |    Collected for the Given Organization and Upto the Given Date.           |
242  |                                                                            |
243  |    For MMT trasactions, all transactions that are not selected by the view |
244  |    'cst_pm_matl_txn_v' and satisfy the date and org criteria are marked as |
245  |    cost collected with no exception.                                       |
246  |                                                                            |
247  |    For WIP trasactions, transactions falling into either of these category |
248  |    are marked as cost collected.                                           |
249  |    -- All wip transaction records that refer to a non-project costed job   |
250  |    -- All wt records with transaction type as other than (1,2,3) and refer |
251  |       to a project costed job                                              |
252  |    -- All wt records with transaction type as (1,2,3) but have no records  |
253  |       in WTA because their standard_rate_flag was 2 and auto_charge_type as|
254  |       any thing other than manual resulting in the resource having no cost.|
255  |    -- All ipv transfer transactions (source_code = 'IPV'                   |
256  |                                                                            |
257  | PARAMETERS                                                                 |
258  |       Organization_Id,                                                     |
259  |       UpToDate,                                                            |
260  |       p_user_id,                                                           |
261  |       p_login_id,                                                          |
262  |       p_req_id,                                                            |
263  |       p_prg_appl_id,                                                       |
264  |       p_prg_id,                                                            |
265  |       O_err_num,                                                              |
266  |       O_err_code,                                                          |
267  |       O_err_msg                                                               */
268 
269 PROCEDURE pm_mark_non_project_world_txns (
270           p_Org_Id      NUMBER,
271           p_prior_days  NUMBER,
272           p_user_id     NUMBER,
273           p_login_id    NUMBER,
274           p_req_id      NUMBER,
275           p_prg_appl_id NUMBER,
276           p_prg_id      NUMBER,
277           O_err_num     OUT     NOCOPY NUMBER,
278           O_err_code    OUT     NOCOPY VARCHAR2,
279           O_err_msg     OUT     NOCOPY VARCHAR2)
280 IS
281 
282 l_err_num                NUMBER;
283 l_err_code               VARCHAR2(240);
284 l_err_msg                VARCHAR2(240);
285 l_stmt_num               NUMBER;
286 l_return_status  VARCHAR2(1);
287 l_msg_count              NUMBER;
288 l_msg_data               VARCHAR2(30);
289 
290 l_primary_cost_method  NUMBER;
291 l_std_cg_acct            NUMBER;
292 l_debug                  VARCHAR2(80);
293 
294 CST_FAILED_STD_CG_FLAG EXCEPTION;
295 
296 BEGIN
297   l_err_num := 0;
298   l_err_code := '';
299   l_err_msg := '';
300         l_stmt_num := 5;
301 
302         l_debug := FND_PROFILE.VALUE('MRP_DEBUG');
303   if (l_debug = 'Y') then
304     FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Reached#: pm_mark_non_project_world_txns');
305         end if;
306 
307 -- The query is to mark all non-project world txns satisfying the
308 -- arguments provided, as processed.
309 
310 ------------------------------------------------------------------
311 -- Mark all transactions as NULL for non-CG ACCT Std Organizations
312 -- Except the Proj Misc Txns.
313 ------------------------------------------------------------------
314 
315   l_stmt_num := 10;
316 
317   SELECT  mp.primary_cost_method
318   INTO    l_primary_cost_method
319   FROM    mtl_parameters mp
320   WHERE   mp.organization_id = p_org_id;
321 
322   l_stmt_num := 15;
323 
324   CST_Utility_Pub.GET_STD_CG_ACCT_FLAG
325                            (p_api_version        =>  1.0,
326                             p_organization_id    =>  p_org_id,
327                             x_cg_acct_flag       =>  l_std_cg_acct,
328                             x_return_status      =>  l_return_status,
329                             x_msg_count          =>  l_msg_count,
330                             x_msg_data           =>  l_msg_data );
331 
332   IF (l_return_status = FND_API.G_RET_STS_ERROR OR
333       l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
334     RAISE CST_FAILED_STD_CG_FLAG;
335   END IF;
336 
337   IF (l_primary_cost_method = 1 AND l_std_cg_acct <> 1) THEN
338         ------------------------------------------------------------------
339         -- Logical Expense Requisition Receipts are an Exception
340         -- These are cost collected regardless of whether the organization
341         -- is PJM enabled
342         ------------------------------------------------------------------
343     l_stmt_num := 20;
344 
345     UPDATE mtl_material_transactions mmt
346     SET mmt.pm_cost_collected      = NULL,
347         mmt.last_update_date       = sysdate,
348               mmt.last_updated_by        = p_user_id,
349               mmt.last_update_login      = p_login_id,
350               mmt.request_id             = p_req_id,
351               mmt.program_application_id = p_prg_appl_id,
352               mmt.program_id             = p_prg_id,
353         mmt.program_update_date    = sysdate
354     WHERE NOT EXISTS
355         ( SELECT NULL
356                       FROM mtl_transaction_types mtt
357                       WHERE mtt.type_class = 1
358           AND   mtt.transaction_type_id = mmt.transaction_type_id )
359           AND   mmt.organization_id = p_Org_Id
360           AND   mmt.transaction_date <= ((trunc(sysdate) - p_prior_days) + 0.99999)
361           AND   mmt.costed_flag is NULL
362           AND   mmt.pm_cost_collected = 'N'
363           AND   mmt.transaction_action_id <> 17; -- See Note about exception above
364   ELSE
365 
366    l_stmt_num := 25;
367 
368    UPDATE mtl_material_transactions mmt
369    SET mmt.pm_cost_collected      = NULL,
370        mmt.last_update_date       = sysdate,
371              mmt.last_updated_by        = p_user_id,
372              mmt.last_update_login      = p_login_id,
373              mmt.request_id             = p_req_id,
374              mmt.program_application_id = p_prg_appl_id,
375              mmt.program_id             = p_prg_id,
376        mmt.program_update_date    = sysdate
377    WHERE NOT EXISTS
378        ( SELECT NULL
379                      FROM cst_pm_matl_txn_v cpmtv
380                      WHERE cpmtv.transaction_id = mmt.transaction_id )
381          AND mmt.organization_id = p_Org_Id
382          AND mmt.transaction_date <= ((trunc(sysdate) - p_prior_days) + 0.99999)
383          AND mmt.costed_flag is NULL
384          AND mmt.pm_cost_collected = 'N';
385 
386   END IF;
387 
388   if (l_debug = 'Y') then
389     FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: NP Count MMT is #: '||to_char(SQL%ROWCOUNT) );
390   end if;
391 
392        /* Changes for consigned inventory */
393        /* Mark all transactions not owned by the current org as cost collected */
394 
395   l_stmt_num := 30;
396 
397   UPDATE
398     mtl_material_transactions mmt
399   SET mmt.pm_cost_collected = NULL,
400       mmt.last_update_date = sysdate,
401       mmt.last_updated_by = p_user_id,
402       mmt.last_update_login = p_login_id,
403       mmt.request_id = p_req_id,
404       mmt.program_application_id = p_prg_appl_id,
405       mmt.program_id = p_prg_id,
406       mmt.program_update_date = sysdate
407   WHERE
408   (
409     mmt.organization_id <> nvl(mmt.owning_organization_id, mmt.organization_id)
410     OR nvl(mmt.owning_tp_type, 2) <> 2
411   )
412   AND mmt.pm_cost_collected = 'N'
413   AND mmt.organization_id = p_Org_Id
414   AND mmt.transaction_date <= ((trunc(sysdate) - p_prior_days) + 0.99999)
415   AND mmt.costed_flag is null;
416 
417   if (l_debug = 'Y') then
418      FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Count of consigned txns in MMT - no cost collection -  is #: '||to_char(SQL%ROWCOUNT) );
419   end if;
420 
421   l_stmt_num := 35;
422 
423 /*     changes to support PJM Blue Print organizations.All logical transactions  */
424 /*     except Logical PO receipt in the case of a true ship case will be set to  */
425 /*     cost collected. Retroactive price updates will laso be set as cost  */
426 /*     collected  */
427 
428   UPDATE
429     mtl_material_transactions mmt
430   SET mmt.pm_cost_collected = NULL,
431     mmt.last_update_date = sysdate,
432     mmt.last_updated_by = p_user_id,
433     mmt.last_update_login = p_login_id,
434     mmt.request_id = p_req_id,
435     mmt.program_application_id = p_prg_appl_id,
436     mmt.program_id = p_prg_id,
437     mmt.program_update_date = sysdate
438   WHERE mmt.pm_cost_collected = 'N'
439   AND mmt.organization_id = p_org_id
440   AND mmt.transaction_date <= ((trunc(sysdate) - p_prior_days) + 0.99999)
441   AND mmt.costed_flag is null
442   AND
443   (
444     (
445       (
446         NVL(mmt.logical_transaction,2) = 1
447       )
448       AND NOT ( MMT.TRANSACTION_TYPE_ID = 19
449       AND MMT.TRANSACTION_ACTION_ID = 26
450       AND MMT.TRANSACTION_SOURCE_TYPE_ID = 1
451       AND NVL(MMT.LOGICAL_TRX_TYPE_CODE,5) = 2
452       AND EXISTS
453       (
454       SELECT
455         1
456       FROM rcv_transactions rcv
457       WHERE rcv.transaction_id = NVL(mmt.rcv_transaction_id,-9999)
458         AND rcv.organization_id = p_org_id
459       )
460       )
461       AND NOT (mmt.transaction_action_id = 17 and mmt.transaction_source_type_id = 7) /*Bug 7120525*/
462     )
463     OR NVL(mmt.logical_trx_type_code,5) = 4
464   );
465 
466   if (l_debug = 'Y') then
467     FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Count of logical txns or retroactive price updates in MMT - no cost collection -  is #: '||to_char(SQL%ROWCOUNT) );
468   end if;
469 
470 
471   l_stmt_num := 40;
472 
473 /*    Bug # 589460  PJM AUTO TASK API  */
474 /*    Check WIP Transactions Table for Prj/Tsk Ref Rather than the Job Header  */
475   UPDATE
476     wip_transactions wt
477     SET wt.pm_cost_collected = NULL,
478     wt.last_update_date = sysdate,
479     wt.last_updated_by = p_user_id,
480     wt.last_update_login = p_login_id,
481     wt.request_id = p_req_id,
482     wt.program_application_id = p_prg_appl_id,
483     wt.program_id = p_prg_id,
484     wt.program_update_date = sysdate
485   WHERE wt.organization_id = p_Org_Id
486   AND wt.transaction_date <= ((trunc(sysdate) - p_prior_days) + 0.99999)
487   AND wt.pm_cost_collected = 'N'
488   AND
489   (
490     (
491       NOT wt.transaction_type in (1,2,3,17)
492       AND wt.project_id IS NOT NULL
493     )
494     OR wt.project_id IS NULL
495     OR
496     (
497       wt.transaction_type in (1,2,3)
498       AND NOT EXISTS
499       (
500       SELECT
501         NULL
502       FROM wip_transaction_accounts wta
503       WHERE wta.transaction_id = wt.transaction_id
504       )
505     )
506     OR wt.source_code = 'IPV' -- Bug 2130771
507   );
508 
509 
510   /*
511   Bug 14601690: the assembly return transaction has no project_id in MMT, but the job has project_id in WDJ, so it will be selected by cost collection manager,
512   But if there is no MOH or even no any MTA accounting populated in MTA(like that if the subinv is expense and the job is expense), it will be error,
513   because cost collection worker will get data from MTA for that txn.
514   check in MTA, if no MOH was absorbed or not in MTA (in this case MTA doesn't even exists), if none is found mark it as not applicable*/
515 
516      l_stmt_num := 41;
517 
518    UPDATE mtl_material_transactions mmt
519    SET mmt.pm_cost_collected      = NULL,
520        mmt.last_update_date       = sysdate,
521              mmt.last_updated_by        = p_user_id,
522              mmt.last_update_login      = p_login_id,
523              mmt.request_id             = p_req_id,
524              mmt.program_application_id = p_prg_appl_id,
525              mmt.program_id             = p_prg_id,
526        mmt.program_update_date    = sysdate
527    WHERE NOT EXISTS
528        ( SELECT 1 FROM MTL_TRANSACTION_ACCOUNTS
529 		 WHERE TRANSACTION_ID = mmt.TRANSACTION_ID
530 		 AND cost_element_id = 2
531 		 AND accounting_line_type = 3
532 			)
533          AND mmt.organization_id = p_Org_Id
534          AND mmt.transaction_date <= ((trunc(sysdate) - p_prior_days) + 0.99999)
535          AND mmt.costed_flag is NULL
536          AND mmt.pm_cost_collected = 'N'
537 		 AND mmt.transaction_action_id in (31, 32);
538 
539   if (l_debug = 'Y') then
540     FND_FILE.PUT_LINE(FND_FILE.LOG,'The count of the txn of assembly completion/assembly return with no MOH absorption in MTA - no applicable to be cost collected: '||to_char(SQL%ROWCOUNT) );
541   end if;
542 
543 EXCEPTION
544   WHEN CST_FAILED_STD_CG_FLAG THEN
545     rollback;
546     O_err_num := 2001;
547     O_err_code := 'Failed CST_UTILITY_PUB.GET_STD_CG_ACCT_FLAG()'
548                                 ||' Organization_Id: '
549                                 ||p_org_id;
550     O_err_msg := 'CSTPPCCB.pm_mark_non_project_world_txns('
551                                 || to_char(l_stmt_num)
552                                 || '): ';
553         WHEN OTHERS THEN
554                 rollback;
555                 O_err_num := SQLCODE;
556                 O_err_code := NULL;
557                 O_err_msg := 'CSTPPCCB.pm_mark_non_project_world_txns('
558                                 || to_char(l_stmt_num)
559                                 || '): '
560                                 || substr(SQLERRM,1,200);
561 END pm_mark_non_project_world_txns;
562 
563 /*----------------------------------------------------------------------------*
564  | PRIVATE PROCEDURE                                                          |
565  |       assign_groups_to_mmt_txns                                            |
566  |                                                                            |
567  | DESCRIPTION                                                                |
568  |    This procedure would for every record selected from the view, update the|
569  |    MMT record to assign a group_id. The number of records to be updated is |
570  |    determined by the user_spec_group_size.                                 |
571  |                                                                            |
572  |    Every Transaction that satisfies the conditions mentioned below gets    |
573  |    a group id assigned to itself.                                          |
574  |                                                                            |
575  |    - All Project related transactions from the Project World               |
576  |                                                                            |
577  |    - All Project related transactions from the Non-Proj World              |
578  |      - Select Txns: Capital Projects related txns in the NPW               |
579  |      - Select Txns: Component Issue to Project Job from a NPW              |
580  |      - Select Txns: Component Return from Project Job to NPW               |
581  |                                                                            |
582  |    - Ensure that the transaction has not yet got any group assigned        |
583  |                                                                            |
584  |    - Ensure that the transaction took place before the Date upto which the |
585  |      Cost Collection was desired                                           |
586  |                                                                            |
587  |    - Ensure that the transaction took place in the Org for which the cost  |
588  |      Cost Collection was desired                                           |
589  |                                                                            |
590  | PARAMETERS                                                                 |
591  |      p_Org_Id,                                                             |
592  |      p_prior_days,                                                          |
593  |      p_user_spec_group_size,                                               |
594  |      p_rows_processed,                                                      |
595  |      p_group_id OUT,                                                       |
596  |      p_user_id,                                                            |
597  |      p_login_id,                                                           |
598  |      p_req_id,                                                             |
599  |      p_prg_appl_id,                                                        |
600  |      p_prg_id,                                                             |
601  |      p_proj_misc_txn_only,                                                 |
602  |      O_err_num,                                                              |
603  |      O_err_code,                                                           |
604  |      O_err_msg                                                             |
605  |                                                                            |
606  | HISTORY                                                                    |
607  |    07-SEP-96  Bhaskar Dasari Created.                                      |
608  *----------------------------------------------------------------------------*/
609   PROCEDURE assign_groups_to_mmt_txns ( p_Org_Id                NUMBER,
610                                         p_prior_days            NUMBER,
611                                         p_user_spec_group_size        NUMBER,
612                                         p_rows_processed OUT        NOCOPY NUMBER,
613                                         p_group_id         OUT        NOCOPY NUMBER,
614                                         p_user_id                NUMBER,
615                                         p_login_id                NUMBER,
616                                         p_req_id                NUMBER,
617                                         p_prg_appl_id                 NUMBER,
618                                         p_prg_id                 NUMBER,
619                                         p_proj_misc_txn_only        NUMBER,
620                                         O_err_num        OUT        NOCOPY NUMBER,
621                                         O_err_code        OUT        NOCOPY VARCHAR2,
622                                         O_err_msg        OUT        NOCOPY VARCHAR2)
623   IS
624 
625   CURSOR sel_mmt_trx (c_Org_Id               NUMBER,
626                       c_prior_days           NUMBER,
627                       c_proj_misc_txn_only   NUMBER,
628                       c_user_spec_group_size NUMBER) IS
629          SELECT  NULL
630            FROM  mtl_material_transactions mmt
631           WHERE  mmt.transaction_id  in (
632                   SELECT  cpmtv.transaction_id
633                     FROM  cst_pm_matl_txn_v cpmtv
634                    WHERE  cpmtv.organization_id = c_Org_Id
635                      AND  cpmtv.transaction_date <=
636                                 ((trunc(sysdate) - c_prior_days) + 0.99999)
637                      AND  cpmtv.pm_cost_collector_group_id is NULL
638                      AND  rownum <= c_user_spec_group_size
639                      AND ( cpmtv.type_class =  decode(c_proj_misc_txn_only,1,1,cpmtv.type_class)
640                            OR cpmtv.transaction_action_id=17)
641                 )
642             FOR UPDATE OF mmt.pm_cost_collected NOWAIT;
643   l_err_num             NUMBER;
644   l_err_code            VARCHAR2(240);
645   l_err_msg             VARCHAR2(240);
646   l_stmt_num            NUMBER;
647   l_rows                NUMBER;
648   l_debug               VARCHAR2(80);
649   BEGIN
650         -- initialize local variables
651         l_err_num := 0;
652         l_err_code := '';
653         l_err_msg := '';
654         p_rows_processed := 0;
655         l_stmt_num := 1;
656 
657         l_debug := FND_PROFILE.VALUE('MRP_DEBUG');
658         if (l_debug = 'Y') then
659             FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Reached#: assign_groups_to_mmt_txns');
660         end if;
661 
662 
663         l_stmt_num := 10;
664 
665         SELECT mtl_material_transactions_s.nextval
666           INTO p_group_id
667           FROM dual;
668 
669         l_stmt_num := 20;
670 
671         FOR sel_mmt_rec IN sel_mmt_trx( p_Org_Id,
672                                         p_prior_days,
673                                         p_proj_misc_txn_only,
674                                         p_user_spec_group_size) LOOP
675 
676 
677         UPDATE  mtl_material_transactions mmt
678            SET  mmt.pm_cost_collector_group_id = p_group_id,
679                 mmt.last_update_date            = sysdate,
680                 mmt.last_updated_by        = p_user_id,
681                 mmt.last_update_login      = p_login_id,
682                 mmt.request_id             = p_req_id,
683                 mmt.program_application_id = p_prg_appl_id,
684                 mmt.program_id             = p_prg_id,
685                 mmt.program_update_date    = sysdate
686          WHERE  current of sel_mmt_trx;
687 
688 
689         p_rows_processed := sel_mmt_trx%ROWCOUNT;
690         l_rows := sel_mmt_trx%ROWCOUNT;
691 
692         END LOOP;
693 
694 
695   EXCEPTION
696         WHEN OTHERS THEN
697                 rollback;
698                 O_err_num := SQLCODE;
699                 O_err_code := NULL;
700                 O_err_msg := 'CSTPPCCB.assign_groups_to_mmt_txns('
701                                 || to_char(l_stmt_num)
702                                 || '): '
703                                 || substr(SQLERRM,1,200);
704   END assign_groups_to_mmt_txns;
705 /*----------------------------------------------------------------------------*
706  | PRIVATE PROCEDURE                                                          |
707  |    pm_cc_worker_mmt                                                        |
708  |                                                                            |
709  | DESCRIPTION                                                                |
710  |    This procedure would Cost Collect the given transaction id. The code    |
711  |    would check if the transaction is with errors. If none, the code would  |
712  |    to make a call to a procedure that would process the transaction. On    |
713  |    successful return from the process_txn procedure, the transaction being |
714  |    is flagged as successfully cost collected by marking the flag           |
715  |    'pm_cost_collected' as NULL.                                            |
716  |                                                                            |
717  | PARAMETERS                                                                 |
718  |      Transaction_id                                                        |
719  |      Org_id                                                                   |
720  |      p_std_cg_acct    Added for bug 3495967                                |
721  |      p_inv_txn_source_literal                                              |
722  |      p_cap_txn_source_literal                                              |
723  |      p_inv_syslink_literal                                                 |
724  |      p_bur_syslink_literal                                                 |
725  |      p_wip_syslink_literal                                                 |
726  |      p_user_id,                                                            |
727  |      p_user_def_exp_type                                                   |
728  |      p_login_id,                                                           |
729  |      p_req_id,                                                             |
730  |      p_prg_appl_id,                                                        |
731  |      p_prg_id,                                                             |
732  |      O_err_num,                                                              |
733  |      O_err_code,                                                           |
734  |      O_err_msg                                                             |
735  |                                                                            |
736  | HISTORY                                                                    |
737  |    07-SEP-96  Bhaskar Dasari Created.                                      |
738  |                                                                            |
739  |    30-JUL-97  Hemant Gosain Modified to pass transfer_organization_id      |
740  |               so that fob point can be determined to pass accounting info  |
741  |               to Projects.                                                 |
742  *----------------------------------------------------------------------------*/
743   PROCEDURE pm_cc_worker_mmt (
744                                 p_transaction_id                NUMBER,
745                                 p_Org_Id                        NUMBER,
746                                 p_std_cg_acct                   NUMBER, -- Added for bug 3495967
747                                 p_inv_txn_source_literal        VARCHAR2,
748                                 p_cap_txn_source_literal        VARCHAR2,
749                                 p_inv_syslink_literal                VARCHAR2,
750                                 p_bur_syslink_literal                VARCHAR2,
751                                 p_wip_syslink_literal                VARCHAR2,
752                                 p_denom_currency_code           VARCHAR2,
753                                 p_user_def_exp_type             NUMBER,
754                                 p_user_id                        NUMBER,
755                                 p_login_id                        NUMBER,
756                                 p_req_id                        NUMBER,
757                                 p_prg_appl_id                         NUMBER,
758                                 p_prg_id                         NUMBER,
759                                 O_err_num                OUT        NOCOPY NUMBER,
760                                 O_err_code                OUT        NOCOPY VARCHAR2,
761                                 O_err_msg                OUT        NOCOPY VARCHAR2)
762   IS
763 
764   CURSOR sel_mmt_trx_to_cost (c_Transaction_Id  NUMBER,
765                               c_Organization_Id NUMBER) IS
766          SELECT cpmtv.transaction_action_id,
767                 cpmtv.transaction_source_type_id,
768                 cpmtv.type_class,
769                 cpmtv.expenditure_type,
770                 cpmtv.transaction_date,
771                 cpmtv.project_id,
772                 cpmtv.task_id,
773                 cpmtv.inventory_item_id,
774                 cpmtv.primary_quantity,
775                 cpmtv.costed_flag,
776                 cpmtv.primary_cost_method,
777                 cpmtv.avg_rates_cost_type_id,
778                 cpmtv.item_description,
779                 cpmtv.cost_group_id,
780                 cpmtv.transfer_cost_group_id,
781                 cpmtv.transaction_source_id,
782                 cpmtv.to_project_id,
783                 cpmtv.to_task_id,
784                 cpmtv.source_project_id,
785                 cpmtv.source_task_id,
786                 cpmtv.transfer_transaction_id,
787                 cpmtv.acct_period_id,
788                 cpmtv.pm_cost_collector_group_id,
789                 cpmtv.exp_org_id,
790                 cpmtv.distribution_account_id,
791                 cpmtv.transfer_organization_id,
792                 cpmtv.flow_schedule,
793                 cpmtv.si_asset_yes_no,
794                 cpmtv.transfer_si_asset_yes_no
795            FROM cst_pm_matl_txn_v cpmtv
796           WHERE cpmtv.transaction_id = c_Transaction_Id
797             AND cpmtv.organization_id = c_Organization_Id;
798 
799         l_proj_job_ind                NUMBER;
800         l_process_yn                         NUMBER;
801         l_first_matl_se_exp_type         VARCHAR2(30);
802         l_err_num                        NUMBER;
803         l_err_code                        VARCHAR2(240);
804         l_err_msg                        VARCHAR2(240);
805         l_err_msg_temp                        VARCHAR2(240);
806 
807         l_error_code                    VARCHAR2(240);
808         l_error_explanation             VARCHAR2(240);
809         PROCESS_ERROR                        EXCEPTION;
810         l_stmt_num                        NUMBER;
811         rec_to_proc                        NUMBER;
812         l_debug                         VARCHAR2(80);
813         l_count NUMBER;
814   BEGIN
815 
816 
817         -- initialize local variables
818         l_err_num := 0;
819         l_err_code := '';
820         l_err_msg := '';
821         l_err_msg_temp := '';
822         l_stmt_num := 1;
823         l_count := 0;
824 
825         l_error_code := '';
826         l_error_explanation := '';
827 
828         l_debug := FND_PROFILE.VALUE('MRP_DEBUG');
829         if (l_debug = 'Y') then
830            FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Reached#: pm_cc_worker_mmt ...');
831         end if;
832 
833         l_stmt_num := 10;
834 
835         /* The transaction may not need to be cost collected. This is because
836            the task auto assignment may have resulted in the task_id being equal
837            to the source_task_id. In this case, set the cost_collected flag to
838            NULL and return. */
839         SELECT count(*)
840         INTO l_count
841         FROM cst_pm_matl_txn_v
842         WHERE transaction_id = p_transaction_id;
843 
844         IF (l_count = 0)
845         THEN
846              if (l_debug = 'Y') then
847                FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Transfer between same project and task.' ||
848                                                 ' No need to collect cost');
849             end if;
850 
851             UPDATE mtl_material_transactions mmt
852             SET mmt.pm_cost_collected      = NULL,
853                 mmt.pm_cost_collector_group_id = NULL,
854                 mmt.last_update_date       = sysdate,
855                 mmt.last_updated_by        = p_user_id,
856                 mmt.last_update_login      = p_login_id,
857                 mmt.request_id             = p_req_id,
858                 mmt.program_application_id = p_prg_appl_id,
859                 mmt.program_id             = p_prg_id,
860                 mmt.program_update_date    = sysdate
861             WHERE
862                 mmt.transaction_id =p_transaction_id;
863 
864         END IF;
865 
866         FOR cpmtv_rec IN sel_mmt_trx_to_cost(p_transaction_id,
867                                              p_Org_Id) LOOP
868 
869         if (l_debug = 'Y') then
870            FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Processing transaction : '||to_char(p_transaction_id));
871         end if;
872 
873         savepoint pm_cc_worker_mmt;
874 
875         pm_check_error_mmt( p_transaction_id,
876                             p_Org_Id,
877                             cpmtv_rec.primary_cost_method,
878                             cpmtv_rec.inventory_item_id,
879                             cpmtv_rec.avg_rates_cost_type_id,
880                             cpmtv_rec.transaction_action_id,
881                             cpmtv_rec.transaction_source_type_id,
882                             cpmtv_rec.type_class,
883                             cpmtv_rec.project_id,
884                             cpmtv_rec.task_id,
885                             cpmtv_rec.to_project_id,
886                             cpmtv_rec.to_task_id,
887                             cpmtv_rec.source_project_id,
888                             cpmtv_rec.source_task_id,
889                             cpmtv_rec.transaction_source_id,
890                             l_proj_job_ind ,
891                             l_process_yn ,
892                             l_first_matl_se_exp_type ,
893                             p_user_id,
894                             p_login_id,
895                             p_req_id,
896                             p_prg_appl_id,
897                             p_prg_id,
898                             l_err_num,
899                             l_err_code,
900                             l_err_msg,
901                             cpmtv_rec.flow_schedule,
902                             cpmtv_rec.cost_group_id
903                             );
904 
905         IF (l_err_num <> 0) THEN
906              -- Error occured
907              raise PROCESS_ERROR;
908         END IF;
909 
910         IF l_process_yn = 1 THEN
911 
912                 pm_process_txn_mmt( cpmtv_rec.pm_cost_collector_group_id,
913                                     p_transaction_id,
914                                     p_Org_Id,
915                                     cpmtv_rec.transaction_action_id,
916                                     cpmtv_rec.transaction_source_type_id,
917                                     cpmtv_rec.type_class,
918                                     cpmtv_rec.project_id,
919                                     cpmtv_rec.task_id,
920                                     cpmtv_rec.transaction_date,
921                                     cpmtv_rec.primary_quantity,
922                                     cpmtv_rec.expenditure_type,
923                                     cpmtv_rec.item_description,
924                                     cpmtv_rec.cost_group_id,
925                                     cpmtv_rec.transfer_cost_group_id,
926                                     cpmtv_rec.inventory_item_id,
927                                     cpmtv_rec.transaction_source_id,
928                                     cpmtv_rec.to_project_id,
929                                     cpmtv_rec.to_task_id,
930                                     cpmtv_rec.source_project_id,
931                                     cpmtv_rec.source_task_id,
932                                     cpmtv_rec.transfer_transaction_id,
933                                     cpmtv_rec.primary_cost_method,
934                                     p_std_cg_acct, -- Added for bug 3495967
935                                     cpmtv_rec.acct_period_id,
936                                     cpmtv_rec.exp_org_id,
937                                     cpmtv_rec.distribution_account_id,
938                                     l_proj_job_ind,
939                                     l_first_matl_se_exp_type,
940                                     p_inv_txn_source_literal,
941                                     p_cap_txn_source_literal,
942                                     p_inv_syslink_literal,
943                                     p_bur_syslink_literal,
944                                     p_wip_syslink_literal,
945                                     p_user_def_exp_type,
946                                     l_err_num,
947                                     l_err_code,
948                                     l_err_msg,
949                                     cpmtv_rec.transfer_organization_id,
950                                     cpmtv_rec.flow_schedule,
951                                     cpmtv_rec.si_asset_yes_no,
952                                     cpmtv_rec.transfer_si_asset_yes_no,
953                                     p_denom_currency_code);
954 
955                 IF (l_err_num <> 0) THEN
956                    -- Error occured
957                    raise PROCESS_ERROR;
958                 ELSE
959 
960                         l_stmt_num := 20;
961 
962                         /* Bug 5241396.Need to update the pm_cost_collected to NULL only for direct interorg and sub
963                            inventory txfrs and not for intransit receipts that will have a txnfr txn ID */
964 
965                         /* Bug #2623627. Updating group_id for the transfer transaction_id so
966                            the transfer to projects field does not show "Not Applicable" */
967 
968                         If cpmtv_rec.transaction_action_id <> 12 then
969 
970                          UPDATE mtl_material_transactions mmt
971                            SET  mmt.pm_cost_collected = NULL,
972                                 mmt.pm_cost_collector_group_id = cpmtv_rec.pm_cost_collector_group_id,
973                                 mmt.last_update_date = sysdate,
974                                 mmt.last_updated_by        = p_user_id,
975                                 mmt.last_update_login      = p_login_id,
976                                 mmt.request_id             = p_req_id,
977                                 mmt.program_application_id = p_prg_appl_id,
978                                 mmt.program_id             = p_prg_id,
979                                 mmt.program_update_date = sysdate
980                            WHERE         mmt.transaction_id IN (p_transaction_id, cpmtv_rec.transfer_transaction_id);
981 
982                         else
983 
984                           UPDATE  mtl_material_transactions mmt
985                            SET  mmt.pm_cost_collected = NULL,
986                                 mmt.pm_cost_collector_group_id = cpmtv_rec.pm_cost_collector_group_id,
987                                 mmt.last_update_date = sysdate,
988                                 mmt.last_updated_by        = p_user_id,
989                                 mmt.last_update_login      = p_login_id,
990                                 mmt.request_id             = p_req_id,
991                                 mmt.program_application_id = p_prg_appl_id,
992                                 mmt.program_id             = p_prg_id,
993                                 mmt.program_update_date = sysdate
994                          WHERE  mmt.transaction_id = p_transaction_id;
995 
996                         end If;
997 
998                 END IF;
999 
1000         END IF;
1001 
1002         END LOOP;
1003 
1004   EXCEPTION
1005 
1006         WHEN PROCESS_ERROR THEN
1007                 IF l_err_num = 30000 THEN
1008                    /* Bug 2386069 - This situation will arise when a receiving txn
1009                       has not yet been costed but the sending txn is being cost collected.                      The sending txn is being updated with a warning but
1010                       pm_cost_collected flag is not being updated to error to prevent
1011                       user from manually resubmitting the txn for cost collection */
1012                    rollback to pm_cc_worker_mmt;
1013 
1014                    O_err_num := l_err_num;
1015                    O_err_code := l_err_code;
1016                    l_err_msg_temp := l_err_msg;
1017                    l_err_num := 0;
1018 
1019                    BEGIN
1020 
1021                    UPDATE  mtl_material_transactions mmt
1022                    SET
1023                         mmt.error_explanation      = l_err_msg,
1024                         mmt.pm_cost_collector_group_id = null,
1025                         mmt.last_update_date       = sysdate,
1026                         mmt.last_updated_by        = p_user_id,
1027                         mmt.last_update_login      = p_login_id,
1028                         mmt.request_id             = p_req_id,
1029                         mmt.program_application_id = p_prg_appl_id,
1030                         mmt.program_id             = p_prg_id,
1031                         mmt.program_update_date    = sysdate
1032                  WHERE mmt.transaction_id = p_transaction_id;
1033 
1034                  O_err_msg := SUBSTR(l_err_msg_temp
1035                                                 ||' * '
1036                                                 ||'TXN MARKED IN MMT.'
1037                                                 ,1,240);
1038                  EXCEPTION
1039                     WHEN OTHERS THEN
1040 
1041                        O_err_msg := SUBSTR(l_err_msg_temp
1042                                                 ||' * '
1043                                                 ||l_err_msg
1044                                                 ||' * '
1045                                                 ||l_err_code
1046                                                 ||' * '
1047                                                 ||'TXN NOT MARKED IN MMT!'
1048                                                 ,1,240);
1049 
1050                  END;
1051                 ELSE
1052                  DECLARE
1053                    l_actual_cost_profile VARCHAR2(80);
1054                  BEGIN
1055 
1056                    IF l_err_num < 20000 THEN
1057                         rollback to cmlcci_assign_task;      --At  MAT worker
1058                  END IF;
1059 
1060                  O_err_num := l_err_num;
1061                  O_err_code := l_err_code;
1062                  l_err_msg_temp := l_err_msg;
1063                  l_err_num := 0;
1064 
1065                  /* bug 3551579.Same IN and Out variables were being used */
1066 
1067                  l_error_explanation := substr(l_err_msg,1,240) ;
1068                  l_error_code := l_err_code;
1069 
1070                  /* Fix for Bug#4239769
1071                   * Need to check the profile value to determine if the zero
1072                   * actual cost transactions should be marked as 'E' or not.
1073                   */
1074                  IF O_err_num = 20002 THEN
1075                   l_actual_cost_profile := FND_PROFILE.VALUE('CST_ERROR_ZERO_ACTUAL_COST_TO_PROJECTS');
1076                  END IF;
1077 
1078                  IF (  (O_err_num = 20002 and l_actual_cost_profile = 1)
1079                       OR
1080                     (O_err_num <> 20002)
1081                  ) then
1082 
1083                  pm_mark_error_mmt(p_transaction_id,
1084                                   l_error_code,
1085                                   l_error_explanation,
1086                                   p_user_id,
1087                                   p_login_id,
1088                                   p_req_id,
1089                                   p_prg_appl_id,
1090                                   p_prg_id,
1091                                   l_err_num,
1092                                   l_err_code,
1093                                   l_err_msg);
1094                  IF (l_err_num <> 0) THEN
1095                         O_err_msg := SUBSTR(l_err_msg_temp
1096                                                 ||' * '
1097                                                 ||l_err_msg
1098                                                 ||' * '
1099                                                 ||l_err_code
1100                                                 ||' * '
1101                                                 ||'TXN NOT MARKED IN MMT!'
1102                                                 ,1,240);
1103                  ELSE
1104                         O_err_msg := SUBSTR(l_err_msg_temp
1105                                                 ||' * '
1106                                                 ||'TXN MARKED IN MMT.'
1107                                                 ,1,240);
1108 
1109                  END IF;
1110 
1111                 ELSE
1112 
1113                   UPDATE mtl_material_transactions mmt
1114                    SET mmt.pm_cost_collected      = NULL,
1115                        mmt.pm_cost_collector_group_id = NULL,
1116                        mmt.last_update_date       = sysdate,
1117                        mmt.last_updated_by        = p_user_id,
1118                        mmt.last_update_login      = p_login_id,
1119                        mmt.request_id             = p_req_id,
1120                        mmt.program_application_id = p_prg_appl_id,
1121                        mmt.program_id             = p_prg_id,
1122                        mmt.program_update_date    = sysdate
1123                  WHERE mmt.transaction_id =p_transaction_id;
1124 
1125                  O_err_msg := SUBSTR(l_err_msg_temp ||'Zero Cost Txn. but not marked as error in MMT' ,1,240);
1126 
1127                 END IF;
1128                 END;
1129 
1130                END IF;
1131 
1132         WHEN OTHERS THEN
1133                 rollback to cmlcci_assign_task;      --At MAT worker
1134                 O_err_num := SQLCODE;
1135                 O_err_code := NULL;
1136                 l_err_msg_temp := 'CSTPPCCB.pm_cc_worker_mmt('
1137                                 || to_char(l_stmt_num)
1138                                 || '): '
1139                                 || substr(SQLERRM,1,150);
1140                 l_err_num := 0;
1141 
1142                 /* bug 3551579.Same IN and Out variables were being used */
1143 
1144                 l_error_explanation := substr(l_err_msg,1,240) ;
1145                 l_error_code := l_err_code;
1146 
1147                 pm_mark_error_mmt(p_transaction_id,
1148                                   l_error_code,
1149                                   l_error_explanation,
1150                                   p_user_id,
1151                                   p_login_id,
1152                                   p_req_id,
1153                                   p_prg_appl_id,
1154                                   p_prg_id,
1155                                   l_err_num,
1156                                   l_err_code,
1157                                   l_err_msg);
1158                 IF (l_err_num <> 0) THEN
1159                         O_err_msg := SUBSTR(l_err_msg_temp
1160                                                 ||' * '
1161                                                 ||l_err_msg
1162                                                 ||' * '
1163                                                 ||l_err_code
1164                                                 ||' * '
1165                                                 ||'TXN NOT MARKED IN MMT!'
1166                                                 ,1,240);
1167                 ELSE
1168                         O_err_msg := SUBSTR(l_err_msg_temp
1169                                                 ||' * '
1170                                                 ||'TXN MARKED IN MMT.'
1171                                                 ,1,240);
1172 
1173                 END IF;
1174   END pm_cc_worker_mmt;
1175 
1176 /*----------------------------------------------------------------------------*
1177  | PRIVATE PROCEDURE                                                          |
1178  |       assign_groups_to_wt_txns                                            |
1179  |                                                                            |
1180  | DESCRIPTION                                                                |
1181  |    This procedure would for every record selected from the view, update the|
1182  |    WT  record to assign a group_id. The number of records to be updated is |
1183  |    determined by the user_spec_group_size. Delete all 'WITE' records if any|
1184  |    for the transactions that were assigned the group_id.                   |
1185  |                                                                            |
1186  |    Every Transaction that satisfies the conditions mentioned below gets    |
1187  |    a group id assigned to itself.                                          |
1188  |                                                                            |
1189  |    - All transactions resulting from a job that is Project related         |
1190  |                                                                            |
1191  |      - Select Txns: Resource, O/P and Overhead                             |
1192  |                                                                            |
1193  |    - Ensure that the transaction has not yet been assigned a group_id      |
1194  |                                                                            |
1195  |    - Ensure that the transaction took place before the Date upto which the |
1196  |      Cost Collection was desired                                           |
1197  |                                                                            |
1198  |    - Ensure that the transaction took place in Org for which the Cost Coll |
1199  |      was desired                                                           |
1200  |                                                                            |
1201  | PARAMETERS                                                                 |
1202  |      p_Org_Id,                                                             |
1203  |      p_prior_days,                                                          |
1204  |      p_user_spec_group_size,                                               |
1205  |      p_rows_processed,                                                      |
1206  |      p_group_id OUT,                                                       |
1207  |      p_user_id,                                                            |
1208  |      p_login_id,                                                           |
1209  |      p_req_id,                                                             |
1210  |      p_prg_appl_id,                                                        |
1211  |      p_prg_id,                                                             |
1212  |      O_err_num,                                                              |
1213  |      O_err_code,                                                           |
1214  |      O_err_msg                                                             |
1215  |                                                                            |
1216  | HISTORY                                                                    |
1217  |    07-SEP-96  Bhaskar Dasari Created.                                      |
1218  |                                                                            |
1219  |    21-NOV-97  Hemant Gosain Modified. Refer Bug# 589460 Regarding          |
1220  |               PJM Auto Task API. Get Prj/Tsk Ref from Table WT.            |
1221  *----------------------------------------------------------------------------*/
1222   PROCEDURE assign_groups_to_wt_txns (
1223                                       p_Org_Id                       NUMBER,
1224                                       p_prior_days                   NUMBER,
1225                                       p_user_spec_group_size             NUMBER,
1226                                       p_rows_processed                 OUT  NOCOPY NUMBER,
1227                                       p_group_id                 OUT  NOCOPY NUMBER,
1228                                       p_user_id                             NUMBER,
1229                                       p_login_id                     NUMBER,
1230                                       p_req_id                             NUMBER,
1231                                       p_prg_appl_id                      NUMBER,
1232                                       p_prg_id                              NUMBER,
1233                                       O_err_num                        OUT  NOCOPY NUMBER,
1234                                       O_err_code                OUT  NOCOPY VARCHAR2,
1235                                       O_err_msg                        OUT  NOCOPY VARCHAR2)
1236   IS
1237 
1238   CURSOR sel_wt_trx ( c_Org_Id               NUMBER,
1239                       c_prior_days           NUMBER,
1240                       c_user_spec_group_size NUMBER) IS
1241          SELECT  NULL
1242            FROM         wip_transactions wt
1243           WHERE  wt.organization_id = c_Org_Id
1244             AND  wt.transaction_date <= ((trunc(sysdate) - c_prior_days) + 0.99999)
1245             AND  wt.pm_cost_collected = 'N'
1246             AND  wt.transaction_type in (1,2,3,17)
1247             AND  wt.project_id IS NOT NULL -- Bug #589460
1248             AND  wt.pm_cost_collector_group_id is NULL
1249             AND  rownum <= c_user_spec_group_size
1250 
1251             FOR UPDATE OF wt.pm_cost_collected NOWAIT;
1252 
1253   l_group_id            NUMBER;
1254   l_err_num             NUMBER;
1255   l_err_code            VARCHAR2(240);
1256   l_err_msg             VARCHAR2(240);
1257   l_stmt_num            NUMBER;
1258   l_rows                NUMBER;
1259   l_debug               VARCHAR2(80);
1260 
1261   BEGIN
1262 
1263         -- initialize local variables
1264         l_err_num := 0;
1265         l_err_code := '';
1266         l_err_msg := '';
1267         p_rows_processed := 0;
1268         l_stmt_num := 1;
1269 
1270         l_debug := FND_PROFILE.VALUE('MRP_DEBUG');
1271         if (l_debug = 'Y') then
1272            FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Reached#: assign_groups_to_wt_txns ...');
1273         end if;
1274 
1275         l_stmt_num := 10;
1276 
1277         SELECT wip_transactions_S.nextval
1278           INTO l_group_id
1279           FROM dual;
1280 
1281         p_group_id := l_group_id;
1282         l_stmt_num := 20;
1283 
1284         FOR sel_wt_rec IN sel_wt_trx(   p_Org_Id,
1285                                         p_prior_days,
1286                                         p_user_spec_group_size) LOOP
1287 
1288         UPDATE  wip_transactions wt
1289            SET  wt.pm_cost_collector_group_id = l_group_id,
1290                 wt.last_update_date = sysdate,
1291                 wt.last_updated_by        = p_user_id,
1292                 wt.last_update_login      = p_login_id,
1293                 wt.request_id             = p_req_id,
1294                 wt.program_application_id = p_prg_appl_id,
1295                 wt.program_id             = p_prg_id,
1296                 wt.program_update_date = sysdate
1297          WHERE  CURRENT of sel_wt_trx;
1298 
1299         p_rows_processed := sel_wt_trx%ROWCOUNT;
1300         l_rows := sel_wt_trx%ROWCOUNT;
1301 
1302         END LOOP;
1303 
1304         l_stmt_num := 30;
1305 
1306         DELETE wip_txn_interface_errors wite
1307          WHERE wite.transaction_id in
1308                 ( SELECT wt.transaction_id
1309                     FROM wip_transactions wt
1310                    WHERE wt.pm_cost_collector_group_id = l_group_id
1311                      AND wt.pm_cost_collected = 'N' )
1312            AND wite.error_column = 'PM_COST_COLLECTED';
1313 
1314 
1315         EXCEPTION
1316                 WHEN OTHERS THEN
1317                         rollback;
1318                         O_err_num := SQLCODE;
1319                         O_err_code := NULL;
1320                         O_err_msg := 'CSTPPCCB.assign_groups_to_wt_txns('
1321                                         || to_char(l_stmt_num)
1322                                         || '): '
1323                                         || substr(SQLERRM,1,200);
1324 
1325   END assign_groups_to_wt_txns;
1326 /*----------------------------------------------------------------------------*
1327  | PRIVATE PROCEDURE                                                          |
1328  |    pm_cc_worker_wt                                                         |
1329  |                                                                            |
1330  | DESCRIPTION                                                                |
1331  |    This procedure would Cost Collect the given transaction id. The code    |
1332  |    would check if the transaction is with errors. If none, the code would  |
1333  |    to make a call to a procedure that would process the transaction. On    |
1334  |    successful return from the process_txn procedure, the transaction being |
1335  |    is flagged as successfully cost collected by marking the flag           |
1336  |    'pm_cost_collected' as NULL.                                            |
1337  |                                                                            |
1338  | PARAMETERS                                                                 |
1339  |      p_transaction_id                                                      |
1340  |      p_wip_txn_source_literal                                              |
1341  |      p_wip_syslink_literal                                                 |
1342  |      p_bur_syslink_literal                                                 |
1343  |      p_denom_currency_code                                                 |
1344  |      p_user_id,                                                            |
1345  |      p_login_id,                                                           |
1346  |      p_req_id,                                                             |
1347  |      p_prg_appl_id,                                                        |
1348  |      p_prg_id,                                                             |
1349  |      O_err_num,                                                              |
1350  |      O_err_code,                                                           |
1351  |      O_err_msg                                                             |
1352  |                                                                            |
1353  | HISTORY                                                                    |
1354  |    07-SEP-96  Bhaskar Dasari Created.                                      |
1355  |                                                                            |
1356  |    30-JUL-97  Hemant Gosain Modified.                                      |
1357  |               Enhanced Selection criteria to include WTA reference account |
1358  |               to support Project Capitalization.                              |
1359  |                                                                            |
1360  |    21-NOV-97  Hemant Gosain Modified.                                      |
1361  |               Refer Bug# 589460. Get Prj/Tsk form WT Table.                |
1362  *----------------------------------------------------------------------------*/
1363   PROCEDURE pm_cc_worker_wt  (
1364                                 p_transaction_id                NUMBER,
1365                                 p_Org_Id                        NUMBER,
1366                                 p_wip_txn_source_literal        VARCHAR2,
1367                                 p_wip_straight_time_literal     VARCHAR2,
1368                                 p_wip_syslink_literal           VARCHAR2,
1369                                 p_bur_syslink_literal                VARCHAR2,
1370                                 p_denom_currency_code           VARCHAR2,
1371                                 p_user_id                        NUMBER,
1372                                 p_login_id                        NUMBER,
1373                                 p_req_id                        NUMBER,
1374                                 p_prg_appl_id                         NUMBER,
1375                                 p_prg_id                         NUMBER,
1376                                 O_err_num                 OUT        NOCOPY NUMBER,
1377                                 O_err_code                 OUT        NOCOPY VARCHAR2,
1378                                 O_err_msg                 OUT        NOCOPY VARCHAR2)
1379   IS
1380 /* The CURSOR has been changed owing to Bug#589460 and the UNION of WDJ and CFM
1381     has been removed because we pick the prj/task reference from the WT Table
1382     rather than from the Entity Definition.
1383 */
1384 
1385 /* Added DISTINCT clause because ppf could have multiple records based on
1386    effectivity dates for the same person_id  Bug # 703956 */
1387 
1388 /*Included the business_group_id parameter in the cursor bug 2124765 */
1389 
1390   CURSOR sel_wt_trx_to_cost (C_transaction_id  NUMBER,
1391                              C_organization_id NUMBER) IS
1392          SELECT        DISTINCT wta.transaction_date                c_transaction_date,
1393                 ppf.business_group_id           c_business_group_id,
1394                 nvl(ppf.employee_number,ppf.npw_number)                c_employee_number,
1395                 wta.base_transaction_value        c_base_transaction_value,
1396                 wt.primary_quantity                c_primary_quantity,
1397                 wta.resource_id                        c_resource_id,
1398                 br.description                        c_resource_description,
1399                 br.expenditure_type                c_expenditure_type,
1400                 wt.project_id                        c_project_id,
1401                 wt.task_id                        c_task_id,
1402                 bd.pa_expenditure_org_id        c_pa_expenditure_org_id,
1403                 wt.acct_period_id                c_acct_period_id,
1404                 wt.pm_cost_collector_group_id        c_group_id,
1405                 wt.department_id                c_department_id,
1406                 wt.transaction_type                c_transaction_type,
1407                 wta.cost_element_id                c_cost_element_id,
1408                 wt.wip_entity_id                c_wip_entity_id,
1409                 wta.accounting_line_type        c_accounting_line_type,
1410                 wt.primary_uom                        c_primary_uom,
1411                 wta.basis_resource_id                c_basis_resource_id,
1412                 wta.reference_account                c_reference_account,
1413                 wta.wip_sub_ledger_id                c_wip_dr_sub_ledger_id,
1414 		nvl2(ppf.npw_number,'CTW',null) c_person_type
1415            FROM wip_transaction_accounts wta,
1416                 wip_transactions wt,
1417                 bom_resources br,
1418                 bom_departments bd,
1419                 per_people_f ppf
1420           WHERE wt.transaction_type in (1,2,3)
1421             AND wta.accounting_line_type = 7
1422             AND wt.transaction_id = wta.transaction_id
1423             AND br.resource_id = wta.resource_id
1424             AND bd.department_id = wt.department_id
1425             AND bd.organization_id = wt.organization_id
1426             AND wt.transaction_id = C_transaction_id
1427             AND wt.organization_id = C_organization_id
1428             AND ppf.person_id (+) = wt.employee_id
1429                /* Bug:2395906*/
1430             AND ppf.effective_start_date(+) <= trunc(sysdate)
1431             AND ppf.effective_end_date(+) >= trunc(sysdate)
1432             AND (ppf.employee_number is not null or ppf.person_id is null
1433 	    OR ppf.npw_number is not null)
1434 
1435       /*Added the above and condition to check if the employee_number
1436         is not null.This is modified for porting bug #1573297 in 11.0
1437         to 11.5 Bug 1660313*/
1438        /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1439          Removed the join with PJM_PROJECT_PARAMETERS  for  bug  7328006
1440          all the logic for the expenditure type  will  be handled by the
1441          already existing API CST_eamCost_PUB.get_ExpType_for_DirectItem
1442          which has been enhanced
1443        ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1444          UNION
1445          SELECT DISTINCT wta.transaction_date        	c_transaction_date,
1446                 to_number(NULL)                      	c_business_group_id,
1447                 NULL                                 	c_employee_number,
1448                 wta.base_transaction_value        	c_base_transaction_value,
1449                 wt.primary_quantity                	c_primary_quantity,
1450                 wta.resource_id                        	c_resource_id,
1451                 pla.item_description                   	c_resource_description,
1452                 NULL	        			c_expenditure_type,
1453                 wt.project_id                        	c_project_id,
1454                 wt.task_id                        	c_task_id,
1455                 wt.organization_id              	c_pa_expenditure_org_id,
1456                 wt.acct_period_id                	c_acct_period_id,
1457                 wt.pm_cost_collector_group_id        	c_group_id,
1458                 wt.department_id                	c_department_id,
1459                 wt.transaction_type                	c_transaction_type,
1460                 wta.cost_element_id                	c_cost_element_id,
1461                 wt.wip_entity_id                	c_wip_entity_id,
1462                 wta.accounting_line_type        	c_accounting_line_type,
1463                 wt.primary_uom                        	c_primary_uom,
1464                 wta.basis_resource_id                	c_basis_resource_id,
1465                 wta.reference_account                	c_reference_account,
1466                 wta.wip_sub_ledger_id                	c_wip_dr_sub_ledger_id,
1467 		NULL					c_person_type
1468            FROM wip_transaction_accounts wta,
1469                 wip_transactions wt,
1470                 pjm_project_parameters ppp,
1471                 po_lines_all pla
1472           WHERE wt.transaction_type = 17
1473             AND pla.po_line_id = wt.po_line_id
1474             AND wta.accounting_line_type = 7
1475             AND wt.transaction_id = wta.transaction_id
1476             AND wt.transaction_id = C_transaction_id
1477             AND wt.organization_id = C_organization_id;
1478 
1479         l_process_yn                         NUMBER;
1480         l_err_num                        NUMBER;
1481         l_err_code                        VARCHAR2(240);
1482         l_err_msg                        VARCHAR2(240);
1483         l_err_msg_temp                        VARCHAR2(240);
1484         l_exp_org_name                        VARCHAR2(60);
1485         PROCESS_ERROR                        EXCEPTION;
1486         l_stmt_num                        NUMBER;
1487         l_accounting_line_type                NUMBER;
1488         l_cr_code_combination_id        NUMBER;
1489         l_wip_cr_sub_ledger_id          NUMBER;
1490         l_debug                                VARCHAR2(80);
1491         l_business_group_name           VARCHAR2(80) ;
1492 
1493         /* Direct Item Enh Project */
1494         l_expenditure_type                VARCHAR2(30) ;
1495         l_return_status                        VARCHAR2(1) ;
1496         l_msg_count                        NUMBER := 0;
1497         l_msg_data                            VARCHAR2(8000) ;
1498         l_api_message                        VARCHAR2(8000);
1499 
1500         l_err_in_code                   VARCHAR2(240);
1501         l_err_in_msg                    VARCHAR2(240);
1502         l_cross_bg_profile		VARCHAR(1);
1503 
1504   BEGIN
1505 
1506         -- initialize local variables
1507         l_err_num := 0;
1508         l_err_code := '';
1509         l_err_msg := '';
1510         l_err_msg_temp := '';
1511         l_exp_org_name := '';
1512         l_accounting_line_type := 0;
1513         l_cr_code_combination_id := 0;
1514         l_stmt_num := 1;
1515         l_return_status := fnd_api.g_ret_sts_success;
1516         l_cross_bg_profile := pa_utils.IsCrossBGProfile_WNPS; /*Added or bug 8398299, using wrapping function to get profile value*/
1517 
1518         l_debug := FND_PROFILE.VALUE('MRP_DEBUG');
1519         if (l_debug = 'Y') then
1520             FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Reached#: pm_cc_worker_wt');
1521         end if;
1522 
1523         l_stmt_num := 10;
1524 
1525         FOR wt_rec IN sel_wt_trx_to_cost(p_transaction_id, p_Org_Id) LOOP
1526 
1527         if (l_debug = 'Y') then
1528            FND_FILE.PUT_LINE(FND_FILE.LOG,'Processing transaction : '||to_char(p_transaction_id));
1529         end if;
1530 
1531         savepoint pm_cc_worker_wt;
1532 
1533          l_stmt_num := 11;
1534 	 /* Check to see if there is a defined expenditure type for the direct item procurement txn */
1535          /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1536            Moving call to API   CST_eamCost_PUB.get_ExpType_for_DirectItem
1537            before the validation  pm_check_error_wt for bug 7328006
1538            ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1539 	if (wt_rec.c_transaction_type = 17) then
1540             CST_eamCost_PUB.get_ExpType_for_DirectItem(
1541                 p_api_version        =>        1.0,
1542                 x_return_status      =>        l_return_status,
1543                 x_msg_count          =>        l_msg_count,
1544                 x_msg_data           =>        l_msg_data,
1545                 p_txn_id             =>        p_transaction_id,
1546                 x_expenditure_type   =>        l_expenditure_type
1547                 );
1548                 if (l_return_status <> fnd_api.g_ret_sts_success) then
1549                    FND_FILE.put_line(FND_FILE.log, l_msg_data);
1550                    l_api_message := 'get_ExpType_for_DirectItem returned unexpected error';
1551                    FND_MESSAGE.set_name('BOM','CST_API_MESSAGE');
1552                    FND_MESSAGE.set_token('TEXT', l_api_message);
1553                    FND_MSG_pub.add;
1554                    raise fnd_api.g_exc_unexpected_error;
1555                 elsif (l_expenditure_type is not null and l_expenditure_type <> to_char(-1)) then
1556                    wt_rec.c_expenditure_type := l_expenditure_type;
1557                 end if;
1558         end if;
1559 
1560         pm_check_error_wt ( p_transaction_id,
1561                             wt_rec.c_project_id,
1562                             wt_rec.c_task_id,
1563                             wt_rec.c_expenditure_type,
1564                             p_Org_Id,
1565                             wt_rec.c_department_id,
1566                             wt_rec.c_employee_number,
1567                             l_exp_org_name,
1568                             l_process_yn ,
1569                             p_user_id,
1570                             p_login_id,
1571                             p_req_id,
1572                             p_prg_appl_id,
1573                             p_prg_id,
1574                             l_err_num,
1575                             l_err_code,
1576                             l_err_msg);
1577 
1578         IF (l_err_num <> 0) THEN
1579            -- Error occured
1580            raise PROCESS_ERROR;
1581         END IF;
1582 
1583         IF l_process_yn = 1 THEN
1584 
1585                 /* START WIP TXN CR ACCT */
1586                 l_stmt_num := 12;
1587                 -- Get the Cr Account for this Transaction
1588                 SELECT decode(wt_rec.c_transaction_type,
1589                               17, 5,                        --Rcv Inspection(Direct Item)
1590                               decode(wt_rec.c_cost_element_id,
1591                                         3, 4,                --RES ABSO;RES
1592                                         4, 4,                --RES ABSO;OSP
1593                                         5, 3,                 --OVHD ABSO
1594                                         4))
1595                 INTO l_accounting_line_type
1596                 FROM DUAL;
1597 
1598                 l_stmt_num := 14;
1599 
1600                 /* Bug 2599649
1601                    Added check for resource_id also to make
1602                    sure to the correct overhead absorption account
1603                    for a particular overhead is picked up
1604                */
1605 
1606                 SELECT NVL(reference_account,-99)
1607                 INTO l_cr_code_combination_id
1608                 FROM wip_transaction_accounts
1609                 WHERE         transaction_id = p_transaction_id
1610                 AND        organization_id = p_org_id
1611                 AND        cost_element_id = wt_rec.c_cost_element_id
1612                 AND        accounting_line_type = l_accounting_line_type
1613                 AND     NVL(resource_id,-99) = NVL(wt_rec.c_resource_id,-99);
1614 
1615                /* changes to get the WIP credit sub Ledger ID from WTA */
1616 
1617                 l_stmt_num := 16;
1618 
1619                 SELECT MAX(WIP_SUB_LEDGER_ID)
1620                   INTO l_wip_cr_sub_ledger_id
1621                   FROM wip_transaction_accounts wta
1622                  WHERE transaction_id = p_transaction_id
1623                    AND reference_account = l_cr_code_combination_id
1624                    AND organization_id = p_org_id
1625                    AND cost_element_id = wt_rec.c_cost_element_id
1626                    AND accounting_line_type = l_accounting_line_type
1627                    AND NVL(resource_id,-99) = NVL(wt_rec.c_resource_id,-99);
1628 
1629 
1630                 -- if CR account could not be obtained then
1631                 -- set Cr account = dr account.
1632                 l_stmt_num := 16;
1633                 IF l_cr_code_combination_id = -99 THEN
1634                         l_cr_code_combination_id := wt_rec.c_reference_account;
1635                         l_wip_cr_sub_ledger_id   := wt_rec.c_wip_dr_sub_ledger_id;
1636                 END IF;
1637                 /* END WIP TXN CR ACCT */
1638                 l_stmt_num := 18;
1639 
1640               /*Get the Business group Name from the business group Id and  pass it
1641                 for insertion into PA_TRANSACTION_INTERFACE.Bug Fix for
1642                 Bug 2124765   */
1643 
1644                 if wt_rec.c_business_group_id is not null THEN
1645                    Select haout.name into l_business_group_name
1646                    From hr_all_organization_units_tl haout
1647                    WHERE
1648                         haout.organization_id = wt_rec.c_business_group_id
1649                         AND haout.language = USERENV('LANG');
1650                 else /* Modified for bug 8398299 to ensure we pass the business group name
1651                         when the Cross Business Group profile has been activated*/
1652                         IF l_cross_bg_profile = 'Y' THEN
1653                        		SELECT 	HAOUT.name
1654 				INTO   	l_business_group_name
1655        				FROM   	hr_all_organization_units_tl HAOUT
1656         			WHERE 	HAOUT.organization_id =
1657                                         	(SELECT COD.business_group_id
1658                                          	FROM	cst_organization_definitions COD
1659 				     		 WHERE  COD.organization_id = p_org_id)
1660    				AND 	haout.language = USERENV('LANG');
1661 			END IF;
1662                 end if;
1663 
1664                 l_stmt_num := 19;
1665 
1666                 pm_process_txn_wt( wt_rec.c_group_id,
1667                                    l_business_group_name,
1668                                    p_transaction_id,
1669                                    p_Org_Id,
1670                                    wt_rec.c_employee_number,
1671                                    wt_rec.c_department_id,
1672                                    wt_rec.c_project_id,
1673                                    wt_rec.c_task_id,
1674                                    wt_rec.c_transaction_date,
1675                                    wt_rec.c_base_transaction_value,
1676                                    wt_rec.c_primary_quantity,
1677                                    wt_rec.c_acct_period_id,
1678                                    wt_rec.c_expenditure_type,
1679                                    wt_rec.c_resource_description,
1680                                    wt_rec.c_transaction_type,
1681                                    wt_rec.c_cost_element_id,
1682                                    l_exp_org_name,
1683                                    p_wip_txn_source_literal,
1684                                    p_wip_straight_time_literal,
1685                                    p_wip_syslink_literal,
1686                                    p_bur_syslink_literal,
1687                                    l_err_num,
1688                                    l_err_code,
1689                                    l_err_msg,
1690                                    wt_rec.c_reference_account,
1691                                    l_cr_code_combination_id,
1692                                    wt_rec.c_wip_dr_sub_ledger_id,
1693                                    l_wip_cr_sub_ledger_id,
1694                                    wt_rec.c_wip_entity_id,
1695                                    wt_rec.c_resource_id,
1696                                    wt_rec.c_basis_resource_id,
1697                                    p_denom_currency_code,
1698 				   wt_rec.c_person_type);
1699 
1700                 IF (l_err_num <> 0) THEN
1701                    -- Error occured
1702                    raise PROCESS_ERROR;
1703                 ELSE
1704 
1705                         l_stmt_num := 20;
1706 
1707                         UPDATE         wip_transactions wt
1708                                  SET         wt.pm_cost_collected = NULL,
1709                                 wt.last_update_date = sysdate,
1710                                 wt.last_updated_by        = p_user_id,
1711                                 wt.last_update_login      = p_login_id,
1712                                 wt.request_id             = p_req_id,
1713                                 wt.program_application_id = p_prg_appl_id,
1714                                 wt.program_id             = p_prg_id,
1715                                 wt.program_update_date = sysdate
1716                            WHERE         wt.transaction_id = p_transaction_id;
1717                 END IF;
1718         END IF;
1719 
1720         END LOOP;
1721 
1722         EXCEPTION
1723 
1724                 WHEN PROCESS_ERROR THEN
1725                         IF l_err_num < 20000 THEN
1726                                 rollback to cmlccw_assign_task; --At WIP worker
1727                         END IF;
1728                         O_err_num := l_err_num;
1729                         O_err_code := l_err_code;
1730                         l_err_msg_temp := l_err_msg;
1731                         l_err_num := 0;
1732                         l_err_in_code := l_err_code;
1733                         l_err_in_msg := l_err_msg;
1734                         pm_mark_error_wt(p_transaction_id,
1735                                   l_err_in_code,
1736                                   l_err_in_msg,
1737                                   p_user_id,
1738                                   p_login_id,
1739                                   p_req_id,
1740                                   p_prg_appl_id,
1741                                   p_prg_id,
1742                                   l_err_num,
1743                                   l_err_code,
1744                                   l_err_msg);
1745                         IF (l_err_num <> 0) THEN
1746                                 O_err_msg := SUBSTR(l_err_msg_temp
1747                                                 ||' * '
1748                                                 ||l_err_msg
1749                                                 ||' * '
1750                                                 ||l_err_code
1751                                                 ||' * '
1752                                                 ||'TXN NOT MARKED IN WTIE!'
1753                                                 ,1,240);
1754                         ELSE
1755                                 O_err_msg := SUBSTR(l_err_msg_temp
1756                                                 ||' * '
1757                                                 ||'TXN MARKED IN WTIE.'
1758                                                 ,1,240);
1759 
1760                         END IF;
1761 
1762                 WHEN OTHERS THEN
1763                         rollback to cmlccw_assign_task;  --At WIP Worker
1764                         O_err_num := SQLCODE;
1765                         O_err_code := NULL;
1766                         l_err_msg_temp := 'CSTPPCCB.pm_cc_worker_wt('
1767                                         || to_char(l_stmt_num)
1768                                         || '): '
1769                                         || substr(SQLERRM,1,150);
1770 
1771                         l_err_num := 0;
1772                         l_err_in_code := l_err_code;
1773                         l_err_in_msg := l_err_msg;
1774 
1775                         pm_mark_error_wt(p_transaction_id,
1776                                   l_err_in_code,
1777                                   l_err_in_msg,
1778                                   p_user_id,
1779                                   p_login_id,
1780                                   p_req_id,
1781                                   p_prg_appl_id,
1782                                   p_prg_id,
1783                                   l_err_num,
1784                                   l_err_code,
1785                                   l_err_msg);
1786                         IF (l_err_num <> 0) THEN
1787                                 O_err_msg := SUBSTR(l_err_msg_temp
1788                                                 ||' * '
1789                                                 ||l_err_msg
1790                                                 ||' * '
1791                                                 ||l_err_code
1792                                                 ||' * '
1793                                                 ||'TXN NOT MARKED IN WTIE!'
1794                                                 ,1,240);
1795                         ELSE
1796                                 O_err_msg := SUBSTR(l_err_msg_temp
1797                                                 ||' * '
1798                                                 ||'TXN MARKED IN WTIE.'
1799                                                 ,1,240);
1800 
1801                         END IF;
1802   END pm_cc_worker_wt;
1803  /*----------------------------------------------------------------------------*
1804  | PRIVATE PROCEDURE                                                          |
1805  |    pm_process_txn_mmt                                                      |
1806  |                                                                            |
1807  | DESCRIPTION                                                                |
1808  |    The procedure cost collects the given transaction. The manner of cost   |
1809  |    collection is decided based  on the type of transaction at hand. Below  |
1810  |    are the various transaction groups and their differences w.r.t cost     |
1811  |    collection.                                                             |
1812  |                                                                            |
1813  |      -- get the project costed flag for job if the txn is a job related    |
1814  |      -- assign transaction org as expenditure org                          |
1815  |      -- get the schedule close date from acct_periods                      |
1816  |                                                                            |
1817  |    All Transactions in a Standard Costing Organization with transaction    |
1818  |    type having its type_class set to 1 implying that it refers to a Capital|
1819  |    Project. Cost Collection is done at the specified cost at transaction   |
1820  |    time.                                                                   |
1821  |                                                                            |
1822  | PARAMETERS                                                                 |
1823  |                                                                            |
1824  |                        p_Group_Id,                                         |
1825  |                        p_transaction_id,                                   |
1826  |                        p_organization_id,                                  |
1827  |                        p_transaction_action_id,                            |
1828  |                        p_transaction_source_type_id,                       |
1829  |                        p_type_class,                                       |
1830  |                        p_project_id,                                       |
1831  |                        p_task_id                                           |
1832  |                        p_transaction_date                                  |
1833  |                        p_primary_quantity                                  |
1834  |                        p_expenditure_type                                  |
1835  |                        p_item_description                                  |
1836  |                        p_cost_group_id                                     |
1837  |                        p_transfer_cost_group_id                            |
1838  |                        p_inventory_item_id                                 |
1839  |                        p_transaction_source_id                             |
1840  |                        p_to_project_id,                                    |
1841  |                        p_to_task_id                                        |
1842  |                        p_source_project_id,                                |
1843  |                        p_source_task_id,                                   |
1844  |                        p_transfer_transaction_id,                          |
1845  |                        p_primary_cost_method,                              |
1846  |                        p_std_cg_acct,    Added for bug 3495967             |
1847  |                        p_acct_period_id,                                   |
1848  |                        p_exp_org_id,                                       |
1849  |                        p_distribution_account_id,                          |
1850  |                        p_proj_job_ind                                      |
1851  |                        p_first_matl_se_exp_type                            |
1852  |                        p_inv_txn_source_literal                            |
1853  |                        p_cap_txn_source_literal                            |
1854  |                        p_inv_syslink_literal                               |
1855  |                        p_bur_syslink_literal                               |
1856  |                        p_wip_syslink_literal                               |
1857  |                        p_user_def_exp_type                                 |
1858  |                        O_err_num,                                              |
1859  |                        O_err_code,                                              |
1860  |                        O_err_msg,                                              |
1861  |                        p_transfer_organization_id,                         |
1862  |                        p_flow_schedule,                                    |
1863  |                        p_si_asset_yes_no,                                  |
1864  |                        p_transfer_si_asset_yes_no                          |
1865  |                        p_denom_currency_code                               |
1866  |                                                                            |
1867  | CALLED FROM                                                                |
1868  |    pm_cc_worker_mmt()                                                      |
1869  |                                                                            |
1870  | HISTORY                                                                    |
1871  |    07-SEP-96  Bhaskar Dasari Created.                                      |
1872  |                                                                            |
1873  |    30-JUL-97  Hemant Gosain Modified.                                      |
1874  |               Added Capitalization Support.                                      |
1875  |               Added support for -ve WIP component Transactions.              |
1876  |                                                                            |
1877  |    20-AUG-97  Hemant Gosain Modified.                                      |
1878  |               Added support for CFM.                                              |
1879  |               Changed Logic for Prj. Misc Txn.                              |
1880  |                                                                            |
1881  |    21-NOV-97  Hemant Gosain Modified.                                      |
1882  |               Changed Logic for Direct IO Xfer.                                 |
1883  |                                                                            |
1884  |    22-APR-01  Hemant Gosain Modified.                                      |
1885  |               Added Support for Std PJM.                                         |
1886  *----------------------------------------------------------------------------*/
1887 
1888   PROCEDURE  pm_process_txn_mmt (
1889                                  p_Group_Id                     NUMBER,
1890                                  p_transaction_id                NUMBER,
1891                                  p_organization_id                NUMBER,
1892                                  p_transaction_action_id        NUMBER,
1893                                  p_transaction_source_type_id        NUMBER,
1894                                  p_type_class                        NUMBER,
1895                                  p_project_id                        NUMBER,
1896                                  p_task_id                        NUMBER,
1897                                  p_transaction_date                DATE,
1898                                  p_primary_quantity                NUMBER,
1899                                  p_expenditure_type                VARCHAR2,
1900                                  p_item_description                VARCHAR2,
1901                                  p_cost_group_id                NUMBER,
1902                                  p_transfer_cost_group_id        NUMBER,
1903                                  p_inventory_item_id                NUMBER,
1904                                  p_transaction_source_id        NUMBER,
1905                                  p_to_project_id                NUMBER,
1906                                  p_to_task_id                        NUMBER,
1907                                  p_source_project_id                NUMBER,
1908                                  p_source_task_id                NUMBER,
1909                                  p_transfer_transaction_id        NUMBER,
1910                                  p_primary_cost_method                NUMBER,
1911                                  p_std_cg_acct                  NUMBER, -- Added for bug 3495967
1912                                  p_acct_period_id                NUMBER,
1913                                  p_exp_org_id                        NUMBER,
1914                                  p_distribution_account_id      NUMBER,
1915                                  p_proj_job_ind                NUMBER,
1916                                  p_first_matl_se_exp_type        VARCHAR2,
1917                                  p_inv_txn_source_literal        VARCHAR2,
1918                                  p_cap_txn_source_literal        VARCHAR2,
1919                                  p_inv_syslink_literal                VARCHAR2,
1920                                  p_bur_syslink_literal                VARCHAR2,
1921                                  p_wip_syslink_literal                VARCHAR2,
1922                                  p_user_def_exp_type            NUMBER,
1923                                  O_err_num                  OUT        NOCOPY NUMBER,
1924                                  O_err_code                  OUT        NOCOPY VARCHAR2,
1925                                  O_err_msg                  OUT        NOCOPY VARCHAR2,
1926                                  p_transfer_organization_id     NUMBER,
1927                                  p_flow_schedule                VARCHAR2,
1928                                  p_si_asset_yes_no                NUMBER,
1929                                  p_transfer_si_asset_yes_no        NUMBER,
1930                                  p_denom_currency_code          VARCHAR2)
1931 
1932   IS
1933 
1934   l_err_num                     NUMBER;
1935   l_err_code                    VARCHAR2(240);
1936   l_err_msg                     VARCHAR2(240);
1937   l_stmt_num                    NUMBER;
1938 
1939   l_batch                       VARCHAR2(15); --Increased width for Bug#2218654
1940 -- UTF8 changes  l_organization_name            VARCHAR2(60);
1941   l_organization_name           hr_organization_units.name%TYPE;
1942 --  l_xfer_organization_name    VARCHAR2(60);
1943   l_xfer_organization_name      hr_organization_units.name%TYPE;
1944   l_proj_org_id                 NUMBER;
1945   l_to_proj_org_id              NUMBER;
1946   l_source_proj_org_id          NUMBER;
1947   l_project_number              VARCHAR2(25);
1948   l_to_project_number           VARCHAR2(25);
1949   l_source_project_number       VARCHAR2(25);
1950   l_task_number                  VARCHAR2(25);
1951   l_to_task_number              VARCHAR2(25);
1952   l_source_task_number          VARCHAR2(25);
1953   /*l_gl_date                   DATE;*/ /* Commented for bug 6266553 */
1954   l_exp_end_date                DATE;
1955   l_txn_of_misc_family          NUMBER;
1956   l_rownum                      NUMBER;
1957   l_fob_point                   NUMBER;
1958   l_earn_moh                    NUMBER;
1959   l_earn_tomoh                  NUMBER;
1960   l_exp_type                    VARCHAR2(30);
1961   l_xfer_currency_code          VARCHAR2(15);
1962   l_txn_type                    NUMBER;
1963   l_default_project             NUMBER;
1964   /* Bug 2386069 */
1965   l_costed_flag                 VARCHAR2(1);
1966   ROW_NOT_COSTED                EXCEPTION;
1967   /* Bug 2386069 */
1968   NO_ROWS_TO_INSERT             EXCEPTION;
1969   CST_FAILED_GET_EXPENDDATE     EXCEPTION;
1970   CST_FAILED_PROJTSK_VALID      EXCEPTION;
1971   CST_FAILED_GET_EXP_TYPE       EXCEPTION;
1972   CST_FAILED_INSERT_PTI         EXCEPTION;
1973   CST_FAILED_STD_CG_FLAG        EXCEPTION;
1974   CST_FAILED_TXN_SRC            EXCEPTION;
1975 
1976 -- UTF8 changes  l_recv_iss_organization_name  VARCHAR2(60);
1977   l_recv_iss_organization_name  hr_all_organization_units.name%TYPE;
1978   l_primary_cost_method_snd     NUMBER ;
1979   l_cost_collection_enabled_snd NUMBER ;
1980   l_return_status               VARCHAR2(1);
1981   l_msg_count                   NUMBER;
1982   l_msg_data                    VARCHAR2(30);
1983 
1984   l_std_cg_acct_snd             NUMBER;
1985 
1986   l_org_id                      NUMBER;
1987   l_inv_txn_src_literal         VARCHAR2(30);
1988   l_transaction_source          VARCHAR2(30);
1989   l_cap_inv_txn_src_literal     VARCHAR2(30);
1990   l_blue_print_enabled_flag     VARCHAR2(1);
1991   l_autoaccounting_flag         VARCHAR2(1);
1992 
1993 
1994   ppv_txfr_flag                 NUMBER;
1995 
1996   l_no_row_c_sel_prj            NUMBER;
1997   l_no_row_c_sel_toprj          NUMBER;
1998   l_no_ppv                      NUMBER;
1999   l_operating_unit              NUMBER;
2000 
2001 
2002 
2003 /* Cursor for STANDARD ORG Proj. Misc Txn */
2004 /* If the user does not specify an expenditure_type, it will be derived */
2005 /* from the Cost Element Associations */
2006 /* PJMSTD SUPPORT------------------------------ */
2007 /* This cursor is for Std Orgs which are not PJ enabled and hence only */
2008 /* Proj Misc transactions will be cost collected */
2009 /* PJMSTD SUPPORT------------------------------ */
2010   CURSOR c_sel_std_misc IS
2011                 SELECT        p_cap_txn_source_literal transaction_source,
2012                         l_batch batch_name,
2013                         l_exp_end_date expenditure_ending_date,
2014                         NULL employee_number,
2015                         l_organization_name organization_name,
2016                         p_transaction_date expenditure_item_date,
2017                         l_source_project_number project_number,
2018                         l_source_task_number task_number,
2019                         decode(mcacd.cost_element_id,
2020                                 2,0,
2021                                 5,0,
2022                                 (-1) * p_primary_quantity) quantity,
2023                         decode(mcacd.cost_element_id,
2024                                 2,0,
2025                                 5,0,
2026                                 (-1)*sum(p_primary_quantity * mcacd.actual_cost))
2027                                                                  raw_cost,
2028                         p_item_description expenditure_comment,
2029                         to_char(p_transaction_id) orig_transaction_reference,
2030                         decode(mcacd.cost_element_id,
2031                                 2,0,
2032                                 5,0,
2033                                 sum(mcacd.actual_cost)) raw_cost_rate,
2034                         'Y' unmatched_negative_txn_flag,
2035                         -999999 dr_code_combination_id,
2036                         -999999 cr_code_combination_id,
2037                         NULL cdl_system_reference1,
2038                         NULL cdl_system_reference2,
2039                         NULL cdl_system_reference3,
2040                         /*l_gl_date*/ /* Commented for bug 6266553 */ p_transaction_date gl_date,
2041                         l_source_proj_org_id org_id,
2042                         (-1)*sum(p_primary_quantity *
2043                                             mcacd.actual_cost) burdened_cost,
2044                         sum(mcacd.actual_cost) burdened_cost_rate,
2045                         decode( mcacd.cost_element_id,
2046                                         2, p_bur_syslink_literal,
2047                                         3, p_wip_syslink_literal,
2048                                         4, p_wip_syslink_literal,
2049                                         5, p_bur_syslink_literal,
2050                                         p_inv_syslink_literal) system_linkage,
2051                         'P' transaction_status_code,
2052                          mcacd.cost_element_id cost_element_id,
2053                         p_denom_currency_code denom_currency_code
2054                   FROM  mtl_cst_actual_cost_details mcacd,
2055                         mtl_material_transactions mmt
2056                  WHERE  mmt.transaction_id = p_transaction_id
2057                    AND  mmt.transaction_id = mcacd.transaction_id
2058                    AND  mcacd.organization_id = p_organization_id
2059                    AND  mcacd.inventory_item_id = p_inventory_item_id
2060                    AND  mcacd.actual_cost <> 0
2061               GROUP BY         p_transaction_id,
2062                         mcacd.cost_element_id;
2063 
2064 /* Cursor for gathering Material Overhead Costs for txns like: */
2065 /* WIP-CFM Assy Comp/Return, InterOrg or PO */
2066   CURSOR c_sel_moh_txn IS
2067                 SELECT        p_inv_txn_source_literal transaction_source,
2068                         l_batch batch_name,
2069                         l_exp_end_date expenditure_ending_date,
2070                         NULL employee_number,
2071                         l_organization_name organization_name,
2072                         p_transaction_date expenditure_item_date,
2073                         l_project_number project_number,
2074                         l_task_number task_number,
2075                         br.EXPENDITURE_TYPE expenditure_type,
2076                         0 quantity,           /* Qty=0 if l=burden */
2077                         0 raw_cost,          /* RawCost=0 if l=burden */
2078                         br.description expenditure_comment,
2079                         to_char(p_transaction_id) orig_transaction_reference,
2080                         0 raw_cost_rate,  /*RawCostrate=0 if l=burden */
2081                         'Y' unmatched_negative_txn_flag,
2082                         -999999 dr_code_combination_id,
2083                         -999999 cr_code_combination_id,
2084                         NULL cdl_system_reference1,
2085                         NULL cdl_system_reference2,
2086                         NULL cdl_system_reference3,
2087                         /*l_gl_date*/ /* Commented for bug 6266553 */ p_transaction_date gl_date,
2088                         l_proj_org_id org_id,
2089                         macs.ACTUAL_COST*p_primary_quantity burdened_cost,
2090                         macs.ACTUAL_COST burdened_cost_rate,
2091                         p_bur_syslink_literal system_linkage,
2092                         'P' transaction_status_code,
2093                         br.resource_id resource_id,
2094                         macs.cost_element_id cost_element_id,
2095                         p_denom_currency_code denom_currency_code
2096                 FROM    mtl_actual_cost_subelement macs,
2097                         bom_resources br,
2098                         mtl_parameters mp
2099                 WHERE     macs.transaction_id = p_transaction_id
2100                     AND   macs.ORGANIZATION_ID = mp.ORGANIZATION_ID
2101                     AND   mp.cost_organization_id = br.organization_id
2102                     AND   macs.cost_element_id = 2
2103                     AND   macs.level_type = 1
2104                     AND   macs.RESOURCE_ID = br.RESOURCE_ID
2105                     AND   macs.ACTUAL_COST <> 0;
2106 
2107 /* Cursor for gathering the receiving side MOH absorption */
2108 
2109 CURSOR c_sel_tomoh IS
2110 SELECT                  p_inv_txn_source_literal transaction_source,
2111                         l_batch batch_name,
2112                         l_exp_end_date expenditure_ending_date,
2113                         NULL employee_number,
2114                         l_xfer_organization_name organization_name,
2115                         p_transaction_date expenditure_item_date,
2116                         l_to_project_number project_number,
2117                         l_to_task_number task_number,
2118                         br.EXPENDITURE_TYPE expenditure_type,
2119                         0 quantity,           /* Qty=0 if l=burden */
2120                         0 raw_cost,          /* RawCost=0 if l=burden */
2121                         br.description expenditure_comment,
2122                         to_char(p_transfer_transaction_id) orig_transaction_reference,
2123                         0 raw_cost_rate,  /*RawCostrate=0 if l=burden */
2124                         'Y' unmatched_negative_txn_flag,
2125                         -999999 dr_code_combination_id,
2126                         -999999 cr_code_combination_id,
2127                         NULL cdl_system_reference1,
2128                         NULL cdl_system_reference2,
2129                         NULL cdl_system_reference3,
2130                         /*l_gl_date*/ /* Commented for bug 6266553 */ p_transaction_date gl_date,
2131                         l_to_proj_org_id org_id,
2132                         (-1)*macs.ACTUAL_COST*p_primary_quantity burdened_cost,
2133                         macs.ACTUAL_COST burdened_cost_rate,
2134                         p_bur_syslink_literal system_linkage,
2135                         'P' transaction_status_code,
2136                         br.resource_id resource_id,
2137                         macs.cost_element_id cost_element_id,
2138                         l_xfer_currency_code denom_currency_code
2139                 FROM    mtl_actual_cost_subelement macs,
2140                         bom_resources br
2141                 WHERE   macs.transaction_id = p_transfer_transaction_id
2142                   AND   macs.ORGANIZATION_ID = br.ORGANIZATION_ID
2143                   AND   macs.cost_element_id = 2
2144                   AND   macs.level_type = 1
2145                   AND   macs.RESOURCE_ID = br.RESOURCE_ID
2146                   AND   macs.ACTUAL_COST <> 0;
2147 
2148 
2149 /* Cursor for gathering Project related Locator txn costs for any txn */
2150 /* Bug#2580132 - Changed the join between ccicv and mcacd to outer join */
2151   CURSOR c_sel_prj_txn IS
2152                 SELECT        decode(p_type_class,1,
2153                         p_cap_txn_source_literal,
2154                         p_inv_txn_source_literal) transaction_source,
2155                         l_batch batch_name,
2156                         l_exp_end_date expenditure_ending_date,
2157                         NULL employee_number,
2158                         l_organization_name organization_name,
2159                         p_transaction_date expenditure_item_date,
2160                         l_project_number project_number,
2161                         l_task_number task_number,
2162           decode(mcacd.cost_element_id,2,0,
2163             decode(mcacd.cost_element_id,5,0,p_primary_quantity
2164                   )) quantity,
2165 
2166           decode(mcacd.cost_element_id,2,0,
2167             decode(mcacd.cost_element_id,5,0,
2168                 sum(mcacd.ACTUAL_COST)*p_primary_quantity
2169                    )) raw_cost,
2170 
2171                         p_item_description expenditure_comment,
2172                         to_char(p_transaction_id) orig_transaction_reference,
2173 
2174           decode(mcacd.cost_element_id,2,0,
2175             decode(mcacd.cost_element_id,5,0,sum(mcacd.ACTUAL_COST)
2176                    )) raw_cost_rate,
2177                         'Y' unmatched_negative_txn_flag,
2178                         -999999 dr_code_combination_id,
2179                         -999999 cr_code_combination_id,
2180                         NULL cdl_system_reference1,
2181                         NULL cdl_system_reference2,
2182                         NULL cdl_system_reference3,
2183                         /*l_gl_date*/ /* Commented for bug 6266553 */ p_transaction_date gl_date,
2184                         l_proj_org_id org_id,
2185                         ((sum(mcacd.ACTUAL_COST)*p_primary_quantity) - (NVL((sum(temp.actual_cost)*p_primary_quantity),0))) burdened_cost,
2186                           (sum(mcacd.ACTUAL_COST)-(NVL(sum(temp.actual_cost),0))) burdened_cost_rate,
2187 
2188                   decode(mcacd.cost_element_id,2, p_bur_syslink_literal,
2189                    decode(mcacd.cost_element_id,3, p_wip_syslink_literal,
2190                     decode(mcacd.cost_element_id,4, p_wip_syslink_literal,
2191                      decode(mcacd.cost_element_id,5, p_bur_syslink_literal,
2192                                 p_inv_syslink_literal)))) system_linkage,
2193 
2194                         'P' transaction_status_code ,
2195                         mcacd.cost_element_id cost_element_id,
2196                         p_denom_currency_code denom_currency_code
2197                   FROM         mtl_cst_actual_cost_details mcacd,
2198                         (Select SUM(actual_cost) actual_cost,
2199                                transaction_id,
2200                                organization_id,
2201                                cost_element_id,
2202                                level_type,
2203                                layer_id
2204                         from mtl_actual_cost_subelement macs
2205                         where transaction_id = p_transaction_id
2206                           and organization_id = p_organization_id
2207                         group by transaction_id,
2208                                  organization_id,
2209                                  cost_element_id,
2210                                  level_type,
2211                                  layer_id
2212                         ) temp,
2213                         cst_cg_item_costs_view ccicv --PJMSTD
2214                  WHERE        mcacd.transaction_id  = p_transaction_id
2215                    AND         mcacd.organization_id = p_organization_id
2216                 AND         mcacd.inventory_item_id = p_inventory_item_id
2217                 AND        mcacd.actual_cost <> 0
2218                 AND        mcacd.layer_id    = decode(p_primary_cost_method,
2219                                                 1, -1,ccicv.layer_id) --PJMSTD
2220                 AND     temp.transaction_id(+) =
2221                                         mcacd.transaction_id
2222                 AND     temp.organization_id(+) = mcacd.organization_id
2223                 AND     temp.cost_element_id(+) = mcacd.cost_element_id
2224                 AND     temp.level_type(+) = mcacd.level_type
2225                 AND        ccicv.organization_id  = p_organization_id
2226                 AND        ccicv.inventory_item_id = p_inventory_item_id
2227                 AND        ccicv.cost_group_id = decode(p_primary_cost_method,
2228                                                  1, 1, p_cost_group_id) --PJMSTD
2229                       GROUP BY mcacd.transaction_id,
2230                              mcacd.cost_element_id
2231         UNION ALL /* BUG #2972878, 2580132 */
2232                 SELECT  decode(p_type_class,1,
2233                         p_cap_txn_source_literal,
2234                         p_inv_txn_source_literal) transaction_source,
2235                         l_batch batch_name,
2236                         l_exp_end_date expenditure_ending_date,
2237                         NULL employee_number,
2238                         l_organization_name organization_name,
2239                         p_transaction_date expenditure_item_date,
2240                         l_project_number project_number,
2241                         l_task_number task_number,
2242           decode(mcacd.cost_element_id,2,0,
2243             decode(mcacd.cost_element_id,5,0,p_primary_quantity)) quantity,
2244 
2245           decode(mcacd.cost_element_id,2,0,
2246             decode(mcacd.cost_element_id,5,0,
2247                 sum(mcacd.ACTUAL_COST)*p_primary_quantity
2248                    ))raw_cost,
2249 
2250                         p_item_description expenditure_comment,
2251                         to_char(p_transaction_id) orig_transaction_reference,
2252 
2253           decode(mcacd.cost_element_id,2,0,
2254             decode(mcacd.cost_element_id,5,0,sum(mcacd.ACTUAL_COST)
2255                  )) raw_cost_rate,
2256 
2257                         'Y' unmatched_negative_txn_flag,
2258                         -999999 dr_code_combination_id,
2259                         -999999 cr_code_combination_id,
2260                         NULL cdl_system_reference1,
2261                         NULL cdl_system_reference2,
2262                         NULL cdl_system_reference3,
2263                         /*l_gl_date*/ /* Commented for bug 6266553 */ p_transaction_date gl_date,
2264                         l_proj_org_id org_id,
2265                         ((sum(mcacd.ACTUAL_COST)*p_primary_quantity) - (NVL((sum(temp.actual_cost)*p_primary_quantity),0))) burdened_cost,
2266                         (sum(mcacd.ACTUAL_COST)-(NVL(sum(temp.actual_cost),0)))                                             burdened_cost_rate,
2267 
2268                   decode(mcacd.cost_element_id,2, p_bur_syslink_literal,
2269                    decode(mcacd.cost_element_id,3, p_wip_syslink_literal,
2270                     decode(mcacd.cost_element_id,4, p_wip_syslink_literal,
2271                      decode(mcacd.cost_element_id,5, p_bur_syslink_literal,
2272                                 p_inv_syslink_literal)))) system_linkage,
2273 
2274                         'P' transaction_status_code ,
2275                         mcacd.cost_element_id cost_element_id,
2276                         p_denom_currency_code denom_currency_code
2277                 FROM    mtl_cst_actual_cost_details mcacd,
2278                         (Select SUM(actual_cost) actual_cost,
2279                                transaction_id,
2280                                organization_id,
2281                                cost_element_id,
2282                                level_type,
2283                                layer_id
2284                         from mtl_actual_cost_subelement macs
2285                         where transaction_id = p_transaction_id
2286                           and organization_id = p_organization_id
2287                         group by transaction_id,
2288                                  organization_id,
2289                                  cost_element_id,
2290                                  level_type,
2291                                  layer_id
2292                         ) temp,
2293                         mtl_system_items msi
2294                 WHERE   mcacd.transaction_id  = p_transaction_id
2295                 AND     mcacd.organization_id = p_organization_id
2296                 AND     mcacd.inventory_item_id = p_inventory_item_id
2297                 AND     mcacd.inventory_item_id = msi.inventory_item_id
2298                 AND     mcacd.organization_id = msi.organization_id
2299                 AND     msi.costing_enabled_flag = 'N'
2300                 AND     mcacd.actual_cost <> 0
2301               /*  AND     NOT EXISTS (
2302                         SELECT null
2303                         FROM mtl_actual_cost_subelement macs
2304                         WHERE macs.transaction_id =
2305                                         mcacd.transaction_id
2306                           AND macs.organization_id  =
2307                                         mcacd.organization_id
2308                           AND macs.cost_element_id =
2309                                         mcacd.cost_element_id
2310                           AND macs.level_type = mcacd.level_type
2311                         )*/
2312                 AND     temp.transaction_id(+) = mcacd.transaction_id
2313                 AND     temp.organization_id(+) = mcacd.organization_id
2314                 AND     temp.cost_element_id(+) = mcacd.cost_element_id
2315                 AND     temp.level_type(+) = mcacd.level_type
2316                 GROUP BY mcacd.transaction_id,
2317                          mcacd.cost_element_id;
2318 
2319 -- bug#1036498  Changed below cursor . If sending org is std (proj enabled , cost
2320 -- collection not enabled) then need to collect matl cost for it.  This will
2321 -- be done when cost collector is run on the receiving org.
2322 
2323 --    std org                       -->  avg org
2324 --    PJ enabled                         PJ enabled
2325 --    cost collection disabled           cost collection disabled
2326 --    from Project P1                    to project P2
2327 
2328 -- the change made in this cursor is that joining to CQL is removed
2329 -- since for std org. there is no data in CQL.
2330 
2331 -- Cursor changed for Alcatel Enhancements. Both Internal Order Issue
2332 -- to Expense and Receipt are cost collected.
2333 /* Cursor for gathering Transfer Side Locator Project Costs in Xfer Txns */
2334 /* Like Subinventory Xfer or Direct IO Xfer where TO_PROJECT_ID is populated */
2335 /* the cursor c_sel_tomoh has been put in to gather MOH absorption costs on the
2336    receiving side.SO the following cursor will be made to exclude those cost */
2337 
2338   CURSOR c_sel_toprj_txn IS
2339                 SELECT        p_inv_txn_source_literal transaction_source,
2340                         l_batch batch_name,
2341                         l_exp_end_date expenditure_ending_date,
2342                         NULL employee_number,
2343                         decode(p_transaction_action_id,
2344                                 3, l_xfer_organization_name,
2345                                 l_organization_name) organization_name,
2346                         p_transaction_date expenditure_item_date,
2347                         l_to_project_number project_number,
2348                         l_to_task_number task_number,
2349         decode(sign(p_primary_quantity),1, cceet.EXPENDITURE_TYPE_OUT,
2350                cceet.EXPENDITURE_TYPE_IN) expenditure_type,
2351         decode(mcacd.cost_element_id,2,0, decode(mcacd.cost_element_id,5,0,
2352            (-1) * p_primary_quantity)) quantity,
2353         decode(mcacd.cost_element_id,2,0,
2354            decode(mcacd.cost_element_id,5,0,
2355                 (-1) * sum(mcacd.ACTUAL_COST)*p_primary_quantity)) raw_cost,
2356                         p_item_description expenditure_comment,
2357                         to_char(p_transfer_transaction_id)
2358                                                 orig_transaction_reference,
2359           decode(mcacd.cost_element_id,2,0,
2360            decode(mcacd.cost_element_id,5,0,sum(mcacd.ACTUAL_COST)))
2361                                                 raw_cost_rate,
2362                         'Y' unmatched_negative_txn_flag,
2363                         -999999 dr_code_combination_id,
2364                         -999999 cr_code_combination_id,
2365                         NULL cdl_system_reference1,
2366                         NULL cdl_system_reference2,
2367                         NULL cdl_system_reference3,
2368                         /*l_gl_date*/ /* Commented for bug 6266553 */ p_transaction_date gl_date,
2369                         l_to_proj_org_id org_id,
2370                         (-1)*((sum(mcacd.ACTUAL_COST)*p_primary_quantity)-
2371                             (NVL(sum(macs.actual_cost)*p_primary_quantity,0)))
2372                                                 burdened_cost,
2373                           (sum(mcacd.ACTUAL_COST)-
2374                          (NVL(sum(macs.actual_cost),0))) burdened_cost_rate,
2375           decode(mcacd.cost_element_id,2, p_bur_syslink_literal,
2376            decode(mcacd.cost_element_id,3, p_wip_syslink_literal,
2377             decode(mcacd.cost_element_id,4, p_wip_syslink_literal,
2378              decode(mcacd.cost_element_id,5, p_bur_syslink_literal,
2379                                 p_inv_syslink_literal)))) system_linkage,
2380                         'P' transaction_status_code,
2381                          mcacd.cost_element_id cost_element_id,
2382                         decode(p_transaction_action_id,
2383                                 3, l_xfer_currency_code,
2384                                 p_denom_currency_code) denom_currency_code
2385                   FROM         mtl_cst_actual_cost_details mcacd,
2386                         cst_cost_elem_exp_types cceet,
2387                         mtl_actual_cost_subelement macs
2388                  WHERE        mcacd.transaction_id  = decode(p_transaction_action_id,
2389                                                 3, p_transfer_transaction_id ,
2390                                                 p_transaction_id)
2391                   AND   mcacd.organization_id = decode(p_transaction_action_id,
2392                                                        3, p_transfer_organization_id,
2393                                                        p_organization_id)
2394                   AND        cceet.cost_element_id = mcacd.cost_element_id
2395                      AND         mcacd.actual_cost <> 0
2396                   AND   ((l_primary_cost_method_snd = 1)
2397                          OR
2398                           EXISTS
2399                             ( SELECT 'X' from cst_quantity_layers cql
2400                               where   mcacd.layer_id = cql.layer_id
2401                               AND     cql.organization_id = mcacd.organization_id
2402                               AND     cql.inventory_item_id = p_inventory_item_id
2403                               AND     cql.cost_group_id = nvl(p_transfer_cost_group_id,1)
2404                             )
2405                          )
2406                   AND  macs.transaction_id(+) = mcacd.transaction_id
2407                   AND  macs.organization_id(+) = mcacd.organization_id
2408                   AND  macs.cost_element_id(+) = mcacd.cost_element_id
2409                   AND  macs.level_type(+) = mcacd.level_type
2410                  GROUP BY mcacd.cost_element_id,
2411                          cceet.expenditure_type_in,
2412                          cceet.expenditure_type_out;
2413 
2414 /* CURSOR for source_side projects in txns like Misc Proj, WIP Comp iss/Rec */
2415 /* For Misc Txn, if exp_type is NULL then default associations are used. */
2416   CURSOR c_sel_src_txn IS
2417                 SELECT        decode(p_type_class,1,p_cap_txn_source_literal,
2418                                   p_inv_txn_source_literal) transaction_source,
2419                         l_batch batch_name,
2420                         l_exp_end_date expenditure_ending_date,
2421                         NULL employee_number,
2422                         l_organization_name organization_name,
2423                         p_transaction_date expenditure_item_date,
2424                         l_source_project_number project_number,
2425                         l_source_task_number task_number,
2426         decode(mcacd.cost_element_id,2,0,
2427          decode(mcacd.cost_element_id,5,0,(-1) * p_primary_quantity)) quantity,
2428 
2429         decode(mcacd.cost_element_id,2,0,
2430           decode(mcacd.cost_element_id,5,0,
2431                     (-1) * sum(mcacd.ACTUAL_COST) * p_primary_quantity)) raw_cost,
2432 
2433                         p_item_description expenditure_comment,
2434                         to_char(p_transaction_id) orig_transaction_reference,
2435 
2436         decode(mcacd.cost_element_id,2,0,
2437         decode(mcacd.cost_element_id,5,0,sum(mcacd.ACTUAL_COST))) raw_cost_rate,
2438 
2439                         'Y' unmatched_negative_txn_flag,
2440                         -999999 dr_code_combination_id,
2441                         -999999 cr_code_combination_id,
2442                         NULL cdl_system_reference1,
2443                         NULL cdl_system_reference2,
2444                         NULL cdl_system_reference3,
2445                         /*l_gl_date*/ /* Commented for bug 6266553 */ p_transaction_date gl_date,
2446                         l_source_proj_org_id org_id,
2447                 (-1) * sum(mcacd.ACTUAL_COST)*p_primary_quantity burdened_cost,
2448                         sum(mcacd.ACTUAL_COST) burdened_cost_rate,
2449 
2450           decode(mcacd.cost_element_id,2, p_bur_syslink_literal,
2451            decode(mcacd.cost_element_id,3, p_wip_syslink_literal,
2452             decode(mcacd.cost_element_id,4, p_wip_syslink_literal,
2453              decode(mcacd.cost_element_id,5, p_bur_syslink_literal,
2454                                 p_inv_syslink_literal)))) system_linkage,
2455 
2456                         'P' transaction_status_code,
2457                         mcacd.cost_element_id cost_element_id,
2458                         p_denom_currency_code denom_currency_code
2459                 FROM         mtl_cst_actual_cost_details mcacd,
2460                         cst_cg_item_costs_view ccicv
2461                 WHERE        mcacd.transaction_id  = p_transaction_id
2462                   AND         mcacd.organization_id = p_organization_id
2463                   AND   mcacd.inventory_item_id = p_inventory_item_id
2464                      AND         mcacd.actual_cost <> 0
2465                   AND   mcacd.layer_id = decode(p_primary_cost_method,
2466                                                  1, -1, ccicv.layer_id) --PJMSTD
2467                   AND         NOT EXISTS (
2468                           SELECT null
2469                             FROM mtl_actual_cost_subelement macs
2470                            WHERE macs.transaction_id =
2471                                         mcacd.transaction_id
2472                                    AND macs.organization_id  =
2473                                         mcacd.organization_id
2474                                  AND macs.cost_element_id =
2475                                                 mcacd.cost_element_id
2476                                        AND macs.level_type = mcacd.level_type )
2477                   AND        ccicv.organization_id = p_organization_id
2478                   AND        ccicv.inventory_item_id = p_inventory_item_id
2479                   AND        ccicv.cost_group_id = decode(p_primary_cost_method,
2480                                                   1,1, p_cost_group_id) --PJMSTD
2481                 GROUP BY mcacd.transaction_id,
2482                          mcacd.cost_element_id;
2483 
2484 -- borrow / payback
2485   CURSOR c_sel_bp_txn IS
2486                 SELECT        p_inv_txn_source_literal transaction_source,
2487                         l_batch batch_name,
2488                         l_exp_end_date expenditure_ending_date,
2489                         NULL employee_number,
2490                         l_organization_name organization_name,
2491                         p_transaction_date expenditure_item_date,
2492                         l_project_number project_number,
2493                         l_task_number task_number,
2494         decode(sign(p_primary_quantity),1, cceet.EXPENDITURE_TYPE_OUT,
2495                cceet.EXPENDITURE_TYPE_IN) expenditure_type,
2496 -- bug 923134
2497           decode(mcacd.cost_element_id,
2498                  2,0,
2499                  5,0,p_primary_quantity) quantity,
2500 
2501           decode(mcacd.cost_element_id,
2502                  2,0,
2503                  5,0,
2504                  sum(mcacd.payback_variance_amount)* abs(p_primary_quantity))
2505                                                         raw_cost,
2506                         p_item_description expenditure_comment,
2507                         to_char(p_transaction_id) orig_transaction_reference,
2508 
2509           decode(mcacd.cost_element_id,
2510                  2,0,
2511                  5,0,
2512                  sum(mcacd.payback_variance_amount)) raw_cost_rate,
2513                         'Y' unmatched_negative_txn_flag,
2514                         -999999 dr_code_combination_id,
2515                         -999999 cr_code_combination_id,
2516                         NULL cdl_system_reference1,
2517                         NULL cdl_system_reference2,
2518                         NULL cdl_system_reference3,
2519                         /*l_gl_date*/ /* Commented for bug 6266553 */ p_transaction_date gl_date,
2520                         l_proj_org_id org_id,
2521                         sum(mcacd.payback_variance_amount)* abs(p_primary_quantity)
2522                                                         burdened_cost,
2523                           sum(mcacd.payback_variance_amount) burdened_cost_rate,
2524 
2525                   decode(mcacd.cost_element_id,2, p_bur_syslink_literal,
2526                    decode(mcacd.cost_element_id,3, p_wip_syslink_literal,
2527                     decode(mcacd.cost_element_id,4, p_wip_syslink_literal,
2528                      decode(mcacd.cost_element_id,5, p_bur_syslink_literal,
2529                                 p_inv_syslink_literal)))) system_linkage,
2530 
2531                         'P' transaction_status_code ,
2532                         mcacd.cost_element_id cost_element_id,
2533                         p_denom_currency_code denom_currency_code
2534                   FROM         mtl_cst_actual_cost_details mcacd,
2535                         cst_cost_elem_exp_types cceet,
2536                         cst_cg_item_costs_view ccicv
2537                  WHERE        mcacd.transaction_id  = p_transaction_id
2538                    AND         mcacd.organization_id = p_organization_id
2539                 AND         mcacd.inventory_item_id = p_inventory_item_id
2540                 AND        mcacd.payback_variance_amount <> 0
2541                 AND        mcacd.layer_id    = decode(p_primary_cost_method,
2542                                                 1, -1, ccicv.layer_id) --PJMSTD
2543                 AND        cceet.cost_element_id = mcacd.cost_element_id
2544                 AND        ccicv.organization_id  = p_organization_id
2545                 AND        ccicv.inventory_item_id = p_inventory_item_id
2546                 AND        ccicv.cost_group_id = decode(p_primary_cost_method,
2547                                                 1,1, p_cost_group_id) --PJMSTD
2548                       GROUP BY mcacd.transaction_id,
2549                              mcacd.cost_element_id,
2550                          cceet.expenditure_type_in,
2551                          cceet.expenditure_type_out;
2552 
2553 
2554 -- borrow /payback end
2555 
2556   ----------------------------------------------------------------------------
2557   -- PJMSTD
2558   -- This Cursor is for PO and IO Organization of Standard Cost Organizations
2559   -- The PPV is passed to project under Inventory system linkage (Material)
2560   -- Need to see how this will work for IO transactions
2561   -- For IO MMT.Variance_amount will be NOT NULL depending on FOB Point
2562   ----------------------------------------------------------------------------
2563 
2564   CURSOR c_sel_ppv IS
2565                 SELECT        decode(p_type_class,1,
2566                         p_cap_txn_source_literal,
2567                         p_inv_txn_source_literal) transaction_source,
2568                         l_batch batch_name,
2569                         l_exp_end_date expenditure_ending_date,
2570                         NULL employee_number,
2571                         decode(sign(p_primary_quantity),-1,
2572                                         decode(p_transaction_action_id,
2573                                                 1, l_organization_name,
2574                                                 29, l_organization_name, l_xfer_organization_name),
2575                                         l_organization_name) organization_name,
2576                         p_transaction_date expenditure_item_date,
2577                         decode(sign(p_primary_quantity),-1,
2578                                  decode(p_transaction_action_id,
2579                                  1, l_project_number,
2580                                  29, l_project_number, l_to_project_number),
2581                                  l_project_number) project_number,
2582                         decode(sign(p_primary_quantity),-1,
2583                                  decode(p_transaction_action_id,
2584                                  1, l_task_number,
2585                                  29, l_task_number, l_to_task_number),
2586                                  l_task_number) task_number,
2587                         nvl(ppp.ppv_expenditure_type, pop.ppv_expenditure_type) expenditure_type,
2588                         decode(sign(p_primary_quantity),-1,
2589                                 decode(p_transaction_action_id,
2590                                 1, p_primary_quantity,
2591                                 29, p_primary_quantity, (-1)*p_primary_quantity),
2592                                 p_primary_quantity) quantity,
2593                         mmt.variance_amount raw_cost,
2594                         p_item_description expenditure_comment,
2595                         decode(sign(p_primary_quantity),-1,
2596                                          decode(p_transaction_action_id,
2597                                          1, to_char(p_transaction_id),
2598                                          29, to_char(p_transaction_id),
2599 					 21, to_char(p_transaction_id),
2600                                          to_char(p_transfer_transaction_id)),
2601                                         to_char(p_transaction_id)) orig_transaction_reference,
2602                         decode(sign(p_primary_quantity),-1,
2603                                         decode(p_transaction_action_id,
2604                                         1, mmt.variance_amount/p_primary_quantity,
2605                                         29, mmt.variance_amount/p_primary_quantity,
2606                                         (-1)*mmt.variance_amount/p_primary_quantity),
2607                                         mmt.variance_amount/p_primary_quantity) raw_cost_rate,
2608                         'Y' unmatched_negative_txn_flag,
2609                         -999999 dr_code_combination_id,
2610                         -999999 cr_code_combination_id,
2611                         NULL cdl_system_reference1,
2612                         NULL cdl_system_reference2,
2613                         NULL cdl_system_reference3,
2614                         /*l_gl_date*/ /* Commented for bug 6266553 */ p_transaction_date gl_date,
2615                         l_proj_org_id org_id,
2616                         mmt.variance_amount burdened_cost,
2617                         decode(sign(p_primary_quantity),-1,
2618                                          decode(p_transaction_action_id,
2619                                          1, mmt.variance_amount/p_primary_quantity,
2620                                          29, mmt.variance_amount/p_primary_quantity,
2621                                          (-1)* mmt.variance_amount/p_primary_quantity),
2622                                          mmt.variance_amount/p_primary_quantity) burdened_cost_rate,
2623                         p_inv_syslink_literal system_linkage,
2624                         'P' transaction_status_code ,
2625                         NULL cost_element_id,
2626                         p_denom_currency_code denom_currency_code
2627                   FROM         mtl_material_transactions mmt,
2628                         pjm_project_parameters ppp,
2629                         pjm_org_parameters pop
2630                  WHERE        mmt.transaction_id  = decode(ppv_txfr_flag,1, p_transfer_transaction_id,
2631                                                 p_transaction_id)
2632                    AND         mmt.organization_id = decode(ppv_txfr_flag,1,p_transfer_organization_id,
2633                                                 p_organization_id)
2634                 AND        NVL(mmt.variance_amount,0) <> 0
2635                 AND     pop.organization_id = decode(sign(p_primary_quantity),-1,
2636                                                 decode( p_transaction_action_id,
2637                                                         1, p_organization_id,
2638                                                         29, p_organization_id,
2639                                                         p_transfer_organization_id),
2640                                                 p_organization_id)
2641                 AND         ppp.organization_id (+) = pop.organization_id
2642                 AND     ppp.project_id (+) = decode(sign(p_primary_quantity),-1,
2643                                                 decode( p_transaction_action_id,
2644                                                         1, p_project_id,
2645                                                         29, p_project_id, p_to_project_id),
2646                                                 p_project_id);
2647     l_debug             VARCHAR2(80);
2648 
2649   BEGIN
2650         -- initialize local variables
2651         l_err_num := 0;
2652         l_err_code := '';
2653         l_err_msg := '';
2654         l_rownum := 0;
2655         l_fob_point := 0;
2656         l_earn_moh := 0;
2657         l_earn_tomoh := 0;
2658         l_stmt_num := 1;
2659         ppv_txfr_flag := 0;
2660         l_costed_flag := null;
2661 
2662 
2663         /* bug 3978501 Set the variables to 0 and 1 accordingly */
2664         l_no_row_c_sel_prj := 0;
2665         l_no_row_c_sel_toprj := 0;
2666         l_no_ppv := 1;
2667 
2668 
2669         l_debug := FND_PROFILE.VALUE('MRP_DEBUG');
2670         if (l_debug = 'Y') then
2671            FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Reached#: pm_process_txn_mmt ...');
2672            FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Transaction ID: '||to_char(p_transaction_id));
2673         end if;
2674 
2675 
2676 
2677         l_stmt_num := 05;
2678 
2679         -- bug #1036498, need to find out the costing method of the from org.
2680         -- bug 2095581, need pm_cost_collection_enabled flag for sending org.
2681         -- bug 3150050 staging Txfrs
2682         IF (p_transaction_action_id IN (2,3,12,28) ) THEN
2683              SELECT primary_cost_method, decode(pm_cost_collection_enabled, 1, 1, 0)
2684              INTO   l_primary_cost_method_snd, l_cost_collection_enabled_snd
2685              FROM   mtl_parameters
2686              WHERE  organization_id = p_transfer_organization_id ;
2687         END IF;
2688 
2689         -- Initialize the batch name as CC followed by group_id
2690 
2691         l_stmt_num := 10;
2692         -- Modified for Bug#2218654
2693         -- Changed for Bug #2260708. PA import fails when you use a 15 character
2694         -- batch name. Instead I am using the last 8 characters of the group id
2695         -- so the batch name remains less than 10 chars. The likelihood of two
2696         -- batch numbers being the same is very low.
2697 
2698         SELECT 'CC'|| substr( replace( lpad(
2699                       to_char(p_Group_Id,'9999999999999'),14,'0') ,' ','0'),-8)
2700         INTO l_batch
2701         FROM DUAL;
2702 
2703 
2704 
2705         -- Initialize local variable to be used in a decode later in the code,
2706         -- if  the transaction is of Misc Family
2707 
2708         l_stmt_num := 15;
2709         IF    p_transaction_action_id in (1,27)
2710           AND p_transaction_source_type_id in (3,13,6) THEN
2711                 l_txn_of_misc_family := 1;
2712         ELSE
2713                 l_txn_of_misc_family := 0;
2714         END IF;
2715 
2716         -- get exp ending date for the current transaction's transaction_date
2717 
2718         l_stmt_num := 16;
2719         select org_information3
2720         into l_operating_unit
2721         from hr_organization_information
2722         where organization_id = p_organization_id
2723         and org_information_context ='Accounting Information';
2724 
2725         l_stmt_num :=17;
2726         begin
2727          mo_global.set_policy_context('S',l_operating_unit);
2728         end;
2729 
2730         l_stmt_num := 20;
2731 
2732         l_exp_end_date := pa_utils.GetWeekEnding(p_transaction_date);
2733 
2734         IF l_exp_end_date is NULL THEN
2735                 RAISE CST_FAILED_GET_EXPENDDATE;
2736         END IF;
2737 
2738         ----------------------------------------------------------------------
2739         -- bug 1036822
2740         -- get default project info;
2741            l_stmt_num := 20;
2742         -- bug 1291409, max is added to take care customer who
2743         -- does not have pjm installed but still wants to run cost collector
2744         ----------------------------------------------------------------------
2745 
2746          l_stmt_num := 25;
2747 
2748          SELECT max(pop.common_project_id)
2749          INTO l_default_project
2750          FROM pjm_org_parameters pop,
2751               mtl_material_transactions mmt
2752          WHERE pop.organization_id = mmt.organization_id
2753          AND mmt.transaction_id = p_transaction_id;
2754 
2755         ----------------------------------------------------------------------
2756         -- assign transaction org as expenditure org
2757         -- Bug: 1350945 Use hr_organization_units instead of
2758         -- hr_all_organization_units. hou is a view on top of
2759         -- haou and haou_tl (haou is a multilingual table, it shouldnt
2760         -- even have the translated column "name" as an attribute)
2761         ----------------------------------------------------------------------
2762 
2763         l_stmt_num := 30;
2764         SELECT         hou.name
2765           INTO         l_organization_name
2766           FROM         hr_organization_units hou
2767          WHERE         hou.organization_id = p_exp_org_id;
2768 
2769         --bug2623664
2770 
2771         l_stmt_num := 31;
2772         l_recv_iss_organization_name  := null ;
2773         IF ( (p_transaction_action_id = 27 OR p_transaction_action_id = 1 OR
2774               p_transaction_action_id = 6)
2775               AND p_type_class = 1 )  THEN
2776         SELECT  haou.name
2777         INTO    l_recv_iss_organization_name
2778         FROM    hr_all_organization_units haou
2779         WHERE   haou.organization_id = p_organization_id;
2780         END IF;
2781 
2782 
2783         /* For Dir IO Xfer with TO_PROJECT_ID Populated */
2784         l_stmt_num := 35;
2785         IF (p_transfer_organization_id IS NOT NULL
2786           AND p_to_project_id IS NOT NULL AND (p_transaction_action_id = 3 OR p_transaction_action_id = 21)) THEN
2787                 SELECT         hou.name
2788                   INTO         l_xfer_organization_name
2789                   FROM         hr_organization_units hou
2790                  WHERE         hou.organization_id = p_transfer_organization_id;
2791 
2792                 l_stmt_num := 40;
2793 
2794 		 SELECT   currency_code
2795 		 INTO     l_xfer_currency_code
2796 		 FROM     cst_organization_definitions cod
2797 		 WHERE    cod.organization_id = p_transfer_organization_id;
2798 
2799         END IF;
2800 
2801         ----------------------------------------------------------------------
2802         -- assign multi_org_id
2803 
2804         -- Get Proj/Task Numbers for Proj/Task ids.
2805         -- This is added because between the time the transaction was done
2806         -- and cost collection was run the Project/Task could have become
2807         -- Obsolete.  Therefore, we need to catch if there is an exception.
2808 
2809         -- ** Query from PJM_PROJECTS_V  and PJM_TASKS_V
2810         --instead of MTL_PROJECT_V bnd MTL_TASK_V because
2811         -- MTL_PROJECT_V shows only those projects that have been associated
2812         -- with a CG (in Project Parameters).  However, for Capital Txns
2813         -- we need all valid projects irrespective of whether they are
2814         -- set up in Project Parameters (PJM_PROJECT_PARAMETERS) or not.
2815         -- Refer to Bug# 571127 **
2816         ----------------------------------------------------------------------
2817 
2818         ----------------------------------------------------------------------
2819         -- MOAC Changes for R12:
2820         -- References to PJM_PROJECTS_V and PJM_TASKS_V has been removed and
2821         -- their base table pa_projects_all and pa_tasks are used instead.
2822         ----------------------------------------------------------------------
2823 
2824         BEGIN
2825                 l_stmt_num := 45;
2826                 IF p_project_id is NOT NULL then
2827                         SELECT         ppa.org_id,
2828                                        ppa.segment1 -- project number
2829                          INTO         l_proj_org_id,
2830                                       l_project_number
2831                          FROM         pa_projects_all ppa
2832                          WHERE        ppa.project_id = p_project_id;
2833  /*               l_stmt_num := 50;
2834                         SELECT  segment1 -- project number
2835                           INTO        l_project_number
2836                           FROM  pa_projects_all
2837                          WHERE         project_id = p_project_id; */
2838                 l_stmt_num := 55;
2839                         SELECT  task_number
2840                           INTO        l_task_number
2841                           FROM  pa_tasks
2842                          WHERE         project_id = p_project_id
2843                            AND         task_id = p_task_id;
2844                 END IF;
2845 
2846                 l_stmt_num := 60;
2847 
2848                 IF p_to_project_id is NOT NULL then
2849                         SELECT       ppa.org_id,
2850                                      ppa.segment1 -- project number
2851                         INTO         l_to_proj_org_id,
2852                                      l_to_project_number
2853                         FROM         pa_projects_all ppa
2854                         WHERE         ppa.project_id = p_to_project_id;
2855  /*               l_stmt_num := 65;
2856                         SELECT  segment1 -- project number
2857                           INTO        l_to_project_number
2858                           FROM  pa_projects_all
2859                          WHERE         project_id = p_to_project_id; */
2860                 l_stmt_num := 70;
2861                         SELECT  task_number
2862                           INTO        l_to_task_number
2863                           FROM  pa_tasks
2864                          WHERE         project_id = p_to_project_id
2865                            AND         task_id = p_to_task_id;
2866                 END IF;
2867 
2868                 l_stmt_num := 75;
2869                 IF p_source_project_id is NOT NULL then
2870                         SELECT     ppa.org_id,
2871                                    segment1
2872                         INTO       l_source_proj_org_id,
2873                                    l_source_project_number
2874                         FROM       pa_projects_all ppa
2875                         WHERE      ppa.project_id = p_source_project_id;
2876 /*                l_stmt_num := 80;
2877                         SELECT  segment1 -- project number
2878                           INTO        l_source_project_number
2879                           FROM  pa_projects_all
2880                          WHERE         project_id = p_source_project_id; */
2881                 l_stmt_num := 85;
2882                         SELECT  task_number
2883                           INTO        l_source_task_number
2884                           FROM  pa_tasks
2885                          WHERE         project_id = p_source_project_id
2886                            AND         task_id = p_source_task_id;
2887                 END IF;
2888         EXCEPTION
2889         WHEN OTHERS THEN
2890                 l_err_msg := SUBSTR(SQLERRM,1,200);
2891                 RAISE CST_FAILED_PROJTSK_VALID;
2892         END;
2893 
2894         -- get FOB POINT for Intransit Transactions
2895 
2896         l_stmt_num := 90;
2897 
2898         -- Modified for fob stamping project
2899         IF (p_transfer_organization_id IS NOT NULL) THEN
2900            IF p_transaction_action_id = 21 THEN /* Intransit Shipment */
2901                   SELECT nvl(MMT.fob_point, MSNV.fob_point) INTO l_fob_point
2902                   FROM mtl_shipping_network_view MSNV, mtl_material_transactions MMT
2903                   WHERE MSNV.from_organization_id = p_organization_id
2904                   AND MSNV.to_organization_id = p_transfer_organization_id
2905                   AND MMT.transaction_id = p_transaction_id;
2906            ELSIF p_transaction_action_id = 12 THEN /* Intransit Receipt */
2907                   SELECT nvl(MMT.fob_point, MSNV.fob_point) INTO l_fob_point
2908                   FROM mtl_shipping_network_view MSNV, mtl_material_transactions MMT
2909                   WHERE MSNV.from_organization_id = p_transfer_organization_id
2910                   AND MSNV.to_organization_id = p_organization_id
2911                   AND MMT.transaction_id = p_transaction_id;
2912            END IF;
2913 
2914            -- PJMSTD
2915 
2916            l_stmt_num := 95;
2917 
2918            CST_Utility_Pub.GET_STD_CG_ACCT_FLAG
2919                            (p_api_version        =>  1.0,
2920                             p_organization_id    =>  p_transfer_organization_id,
2921                             x_cg_acct_flag       =>  l_std_cg_acct_snd,
2922                             x_return_status      =>  l_return_status,
2923                             x_msg_count          =>  l_msg_count,
2924                             x_msg_data           =>  l_msg_data );
2925 
2926            IF (l_return_status = FND_API.G_RET_STS_ERROR OR
2927             l_return_status = FND_API.G_RET_STS_UNEXP_ERROR)
2928 
2929            THEN
2930 
2931              RAISE CST_FAILED_STD_CG_FLAG;
2932 
2933            END IF;
2934 
2935 
2936         END IF;
2937 
2938         -- get the schedule close date from acct_periods
2939 
2940         /* Commented for bug 6266553
2941         l_stmt_num := 100;
2942         SELECT        schedule_close_date
2943           INTO  l_gl_date
2944           FROM         org_acct_periods oac
2945          WHERE         oac.organization_id = p_organization_id
2946            AND         oac.acct_period_id = p_acct_period_id;*/
2947 
2948         ----------------------------------------------------------------------
2949         --This Select Statement is added to ascertain whether "This
2950         --Level MOH" is earned by the transaction.  If it is not
2951         --earned then there is no need to go through CURSOR
2952         --c_sel_moh_txn. It is necessary to
2953         --bypass the cursor if the transaction did not
2954         --earn "This Level MOH" (say no MOH subelements were
2955         --defaulted or defined for the item) because the Cursor then
2956         --raises the Exception NO_ROWS_TO_INSERT and abandons the txn.
2957         ----------------------------------------------------------------------
2958 
2959         l_stmt_num := 105;
2960 
2961         /* Bug #1944099. Only this level MOH costs are collected. */
2962         SELECT NVL(MAX(macs.transaction_id),-99)
2963         INTO l_earn_moh
2964         FROM mtl_actual_cost_subelement macs
2965         WHERE  macs.transaction_id = p_transaction_id
2966            AND macs.organization_id = p_organization_id
2967            AND macs.actual_cost <> 0
2968            AND macs.level_type = 1
2969            AND macs.cost_element_id = 2;
2970 
2971         /* If it is a direct interorg then check if the c_sel_tomoh has
2972            anything to insert */
2973         /* bug 4655264. Need to make sure that p_to_project_id is not nULL before transferring */
2974          l_stmt_num := 106;
2975 
2976         If (p_transaction_action_id = 3 AND p_to_project_id is not NULL) then
2977            select NVL(MAX(macs.transaction_id),-99)
2978            into l_earn_tomoh
2979            from mtl_actual_cost_subelement macs
2980            WHERE  macs.transaction_id = p_transfer_transaction_id
2981            AND macs.organization_id = p_transfer_organization_id
2982            AND macs.actual_cost <> 0
2983            AND macs.level_type = 1
2984            AND macs.cost_element_id = 2;
2985 
2986         else
2987           l_earn_tomoh := -99;
2988 
2989         end if;
2990 
2991 
2992         l_stmt_num := 110;
2993         SELECT transaction_type_id
2994           INTO l_txn_type
2995           FROM mtl_material_transactions
2996          WHERE transaction_id = p_transaction_id;
2997 
2998         l_stmt_num := 120;
2999 
3000         IF    (    p_primary_cost_method = 1
3001                AND p_type_class = 1
3002                AND p_std_cg_acct <> 1 --PJMSTD
3003               )
3004         THEN
3005 
3006               ----------------------------------------------------------------
3007                 /* START:CC ALL ELE AS PER FROZEN COST IN STD COST ORG */
3008 
3009                 -- All Transactions in a Standard Costing Organization with
3010                 -- transaction type having its type_class set to 1 implying that
3011                 -- it refers to a Capital Project.
3012 
3013                 -- If no expenditure_type is specified at the time of txn
3014                 -- CC will report elemental costs by deriving the default
3015                 -- CE-ET associations defined in Costing SetUp.
3016 
3017                 -- Check is added for std_cg_acct so that only std orgs that
3018                 -- are not CG Acct enabled will use this cursor
3019               ----------------------------------------------------------------
3020 
3021                 l_rownum   := 0;
3022 
3023                 l_stmt_num := 125;
3024 
3025                 if (l_debug = 'Y') then
3026                     FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Processing Project Miscellaneous Transactions ...');
3027                 end if;
3028 
3029                 FOR c_rec1 IN c_sel_std_misc  LOOP
3030 
3031                     ----------------------------------------------------------
3032                     -- bug 888190
3033                     -- the condition is for capital project,
3034                     -- if expenditure type is user entered, get it from
3035                     -- p_expenditure_type otherwise derived it
3036                     -- from the system using cceet.
3037                     -- 0 System derived
3038                     -- 1 User Entered
3039                     ----------------------------------------------------------
3040                     l_stmt_num := 130;
3041 
3042                     IF (p_user_def_exp_type = 1) THEN
3043 
3044                       IF (p_expenditure_type is null) THEN
3045 
3046                         RAISE CST_FAILED_GET_EXP_TYPE;
3047 
3048                       ELSE
3049 
3050                         l_exp_type := p_expenditure_type;
3051 
3052                       END IF;
3053 
3054                     ELSE -- system derived
3055 
3056                       l_stmt_num := 135;
3057 
3058                       SELECT
3059                           DECODE(sign(p_primary_quantity),1,
3060                                  cceet.EXPENDITURE_TYPE_OUT,
3061                                  cceet.EXPENDITURE_TYPE_IN)
3062                       INTO l_exp_type
3063                       FROM cst_cost_elem_exp_types cceet
3064                       WHERE cceet.cost_element_id = c_rec1.cost_element_id;
3065 
3066                     END IF;
3067 
3068                     l_rownum := l_rownum + 1;
3069 
3070                     l_stmt_num := 140;
3071 
3072                     pm_insert_pti_pvt
3073                         (p_transaction_source         =>
3074                                              c_rec1.transaction_source,
3075                          p_batch_name                 =>c_rec1.batch_name,
3076                          p_expenditure_ending_date    =>
3077                                              c_rec1.expenditure_ending_date,
3078                          p_employee_number            =>c_rec1.employee_number,
3079                          p_organization_name          =>
3080                                              c_rec1.organization_name,
3081                          p_expenditure_item_date      =>
3082                                              c_rec1.expenditure_item_date,
3083                          p_project_number             =>c_rec1.project_number,
3084                          p_task_number                =>c_rec1.task_number,
3085                          p_expenditure_type           =>l_exp_type,
3086                          p_pa_quantity                =>c_rec1.quantity,
3087                          p_raw_cost                   =>c_rec1.raw_cost,
3088                          p_expenditure_comment        =>
3089                                              c_rec1.expenditure_comment,
3090                          p_orig_transaction_reference =>
3091                                              c_rec1.orig_transaction_reference,
3092                          p_raw_cost_rate              =>c_rec1.raw_cost_rate,
3093                          p_unmatched_negative_txn_flag=>
3094                                              c_rec1.unmatched_negative_txn_flag,
3095                          p_gl_date                    =>c_rec1.gl_date,
3096                          p_org_id                     =>c_rec1.org_id,
3097                          p_burdened_cost              =>c_rec1.burdened_cost,
3098                          p_burdened_cost_rate         =>
3099                                              c_rec1.burdened_cost_rate,
3100                          p_system_linkage             =>c_rec1.system_linkage,
3101                          p_transaction_status_code    =>
3102                                              c_rec1.transaction_status_code,
3103                          p_denom_currency_code        =>
3104                                              c_rec1.denom_currency_code,
3105                          p_transaction_id             =>p_transaction_id,
3106 
3107                          p_transaction_action_id      =>p_transaction_action_id,
3108                          p_transaction_source_type_id =>
3109                                              p_transaction_source_type_id,
3110                          p_organization_id            =>p_organization_id,
3111                          p_inventory_item_id          =>p_inventory_item_id,
3112                          p_cost_element_id            =>c_rec1.cost_element_id,
3113                          p_resource_id                =>NULL,
3114                          p_source_flag                =>1,
3115                          p_variance_flag              =>-1,
3116                          p_primary_quantity           =>p_primary_quantity ,
3117                          p_transfer_organization_id   =>
3118                                              p_transfer_organization_id,
3119                          p_fob_point                  =>l_fob_point,
3120                          p_wip_entity_id              =>NULL,
3121                          p_basis_resource             =>NULL,
3122 
3123                          p_type_class                 =>p_type_class,
3124                          p_project_id                 =>p_project_id,
3125                          p_task_id                    =>p_task_id,
3126                          p_transaction_date           =>p_transaction_date,
3127                          p_cost_group_id              =>p_cost_group_id,
3128                          p_transfer_cost_group_id     =>
3129                                              p_transfer_cost_group_id,
3130                          p_transaction_source_id      =>
3131                                              p_transaction_source_id,
3132                          p_to_project_id              =>p_to_project_id,
3133                          p_to_task_id                 =>p_to_task_id,
3134                          p_source_project_id          =>p_source_project_id,
3135                          p_source_task_id             =>p_source_task_id,
3136                          p_transfer_transaction_id    =>
3137                                              p_transfer_transaction_id,
3138                          p_primary_cost_method        =>p_primary_cost_method,
3139                          p_acct_period_id             =>p_acct_period_id,
3140                          p_exp_org_id                 =>p_exp_org_id,
3141                          p_distribution_account_id    =>
3142                                              p_distribution_account_id,
3143                          p_proj_job_ind               =>p_proj_job_ind,
3144                          p_first_matl_se_exp_type     =>
3145                                              p_first_matl_se_exp_type,
3146                          p_inv_txn_source_literal     =>
3147                                              p_inv_txn_source_literal,
3148                          p_cap_txn_source_literal     =>
3149                                              p_cap_txn_source_literal,
3150                          p_inv_syslink_literal        =>p_inv_syslink_literal,
3151                          p_bur_syslink_literal        =>p_bur_syslink_literal,
3152                          p_wip_syslink_literal        =>p_wip_syslink_literal,
3153                          p_user_def_exp_type          =>p_user_def_exp_type,
3154                          p_flow_schedule              =>p_flow_schedule,
3155                          p_si_asset_yes_no            =>p_si_asset_yes_no,
3156                          p_transfer_si_asset_yes_no   =>
3157                                              p_transfer_si_asset_yes_no,
3158 
3159                          O_err_num                    =>l_err_num,
3160                          O_err_code                   =>l_err_code,
3161                          O_err_msg                    =>l_err_msg
3162                      );
3163 
3164                     IF (l_err_num <> 0) THEN
3165 
3166                       RAISE CST_FAILED_INSERT_PTI;
3167 
3168                     END IF;
3169 
3170                 END LOOP;
3171 
3172                 IF l_rownum = 0 THEN
3173 
3174                   RAISE NO_ROWS_TO_INSERT;
3175 
3176                 END IF;
3177 
3178 
3179               /* END: CC ALL ELE AS PER FROZEN COST IN STD COST ORG */
3180 
3181         ELSE
3182                 /* START: CC ALL TXNS OF AN CGSTD + AVG COSTING ORG */
3183 
3184 
3185 
3186                                                           /*CC MO SE ET START*/
3187 
3188                 -- CASE 1: Those Assy Completion/Rtn Transactions involving an
3189                 --         Issue / Rtn from/to a Sub that project costed as 'Y'
3190                 --           which implies that this level MO is reported on a
3191                 --         SE basis.
3192                 --
3193                 -- CASE 2: Direct or Intransit receipt transactions into an
3194                 --         asset SI of the receiving Organization irrespective
3195                 --         of type of SI the shipment transaction refers to,
3196                 --         imply that  this level MO is reported on a SE basis.
3197                 --
3198                 -- CASE 3: TXNS ARE: PO Delivery,adjustment,RTV
3199                 --
3200                 -- CASE 4: Consigned ownership transfer transactions
3201                 --
3202 /*
3203    QUESTION: Should CASE 3 restrict itself to the asset items and asset SI,
3204    as MO is earned or unearned only on a PO family transaction involving an
3205    asset item and an asset SI
3206 
3207    ADD INTERORG CONDITION if MO is to be earned only when receipt refers
3208    a asset sub-inventory and not if referring to an exp sub
3209         CONFIRMED BY JENNY that txns involving expense subs do not earn MO
3210 
3211    For both the questions above, the decision is to not make any explicit
3212    check in the CC code, since the Cost processor would create rows in
3213    macs for records only when above stated conditions are satisfied.
3214 */
3215                 IF  (l_earn_moh <> -99)                        /* MOH was earned */
3216                     AND (
3217                            ( p_transaction_action_id in (31,32)  /* CASE 1 */
3218                               AND p_project_id is not null
3219                            )
3220                                    OR  p_transaction_action_id in (12,3)  /* CASE 2 */
3221                                OR  p_transaction_source_type_id = 1  /* CASE 3 */
3222                            OR  p_transaction_action_id = 6 /* CASE 4 ownership transfer transaction */
3223                         )
3224                 THEN
3225 
3226                   l_rownum   := 0;
3227 
3228                   l_stmt_num := 145;
3229 
3230                   if (l_debug = 'Y') then
3231                      FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Collecting MOH costs ...');
3232                   end if;
3233 
3234                   FOR c_rec2 IN c_sel_moh_txn  LOOP
3235 
3236                     l_rownum := l_rownum + 1;
3237 
3238                     l_stmt_num := 150;
3239 
3240                     pm_insert_pti_pvt
3241                         (p_transaction_source         =>
3242                                              c_rec2.transaction_source,
3243                          p_batch_name                 =>c_rec2.batch_name,
3244                          p_expenditure_ending_date    =>
3245                                              c_rec2.expenditure_ending_date,
3246                          p_employee_number            =>c_rec2.employee_number,
3247                          p_organization_name          =>
3248                                              c_rec2.organization_name,
3249                          p_expenditure_item_date      =>
3250                                              c_rec2.expenditure_item_date,
3251                          p_project_number             =>c_rec2.project_number,
3252                          p_task_number                =>c_rec2.task_number,
3253                          p_expenditure_type           =>c_rec2.expenditure_type,
3254                          p_pa_quantity                =>c_rec2.quantity,
3255                          p_raw_cost                   =>c_rec2.raw_cost,
3256                          p_expenditure_comment        =>
3257                                              c_rec2.expenditure_comment,
3258                          p_orig_transaction_reference =>
3259                                              c_rec2.orig_transaction_reference,
3260                          p_raw_cost_rate              =>c_rec2.raw_cost_rate,
3261                          p_unmatched_negative_txn_flag=>
3262                                              c_rec2.unmatched_negative_txn_flag,
3263                          p_gl_date                    =>c_rec2.gl_date,
3264                          p_org_id                     =>c_rec2.org_id,
3265                          p_burdened_cost              =>c_rec2.burdened_cost,
3266                          p_burdened_cost_rate         =>
3267                                              c_rec2.burdened_cost_rate,
3268                          p_system_linkage             =>c_rec2.system_linkage,
3269                          p_transaction_status_code    =>
3270                                              c_rec2.transaction_status_code,
3271                          p_denom_currency_code        =>
3272                                              c_rec2.denom_currency_code,
3273                          p_transaction_id             =>p_transaction_id,
3274 
3275                          p_transaction_action_id      =>p_transaction_action_id,
3276                          p_transaction_source_type_id =>
3277                                              p_transaction_source_type_id,
3278                          p_organization_id            =>p_organization_id,
3279                          p_inventory_item_id          =>p_inventory_item_id,
3280                          p_cost_element_id            =>c_rec2.cost_element_id,
3281                          p_resource_id                =>c_rec2.resource_id,
3282                          p_source_flag                =>-1,
3283                          p_variance_flag              =>-1,
3284                          p_primary_quantity           =>p_primary_quantity ,
3285                          p_transfer_organization_id   =>
3286                                              p_transfer_organization_id,
3287                          p_fob_point                  =>l_fob_point,
3288                          p_wip_entity_id              =>NULL,
3289                          p_basis_resource             =>NULL,
3290 
3291                          p_type_class                 =>p_type_class,
3292                          p_project_id                 =>p_project_id,
3293                          p_task_id                    =>p_task_id,
3294                          p_transaction_date           =>p_transaction_date,
3295                          p_cost_group_id              =>p_cost_group_id,
3296                          p_transfer_cost_group_id     =>
3297                                              p_transfer_cost_group_id,
3298                          p_transaction_source_id      =>
3299                                              p_transaction_source_id,
3300                          p_to_project_id              =>p_to_project_id,
3301                          p_to_task_id                 =>p_to_task_id,
3302                          p_source_project_id          =>p_source_project_id,
3303                          p_source_task_id             =>p_source_task_id,
3304                          p_transfer_transaction_id    =>
3305                                              p_transfer_transaction_id,
3306                          p_primary_cost_method        =>p_primary_cost_method,
3307                          p_acct_period_id             =>p_acct_period_id,
3308                          p_exp_org_id                 =>p_exp_org_id,
3309                          p_distribution_account_id    =>
3310                                              p_distribution_account_id,
3311                          p_proj_job_ind               =>p_proj_job_ind,
3312                          p_first_matl_se_exp_type     =>
3313                                              p_first_matl_se_exp_type,
3314                          p_inv_txn_source_literal     =>
3315                                              p_inv_txn_source_literal,
3316                          p_cap_txn_source_literal     =>
3317                                              p_cap_txn_source_literal,
3318                          p_inv_syslink_literal        =>p_inv_syslink_literal,
3319                          p_bur_syslink_literal        =>p_bur_syslink_literal,
3320                          p_wip_syslink_literal        =>p_wip_syslink_literal,
3321                          p_user_def_exp_type          =>p_user_def_exp_type,
3322                          p_flow_schedule              =>p_flow_schedule,
3323                          p_si_asset_yes_no            =>p_si_asset_yes_no,
3324                          p_transfer_si_asset_yes_no   =>
3325                                              p_transfer_si_asset_yes_no,
3326 
3327                          O_err_num                    =>l_err_num,
3328                          O_err_code                   =>l_err_code,
3329                          O_err_msg                    =>l_err_msg
3330                      );
3331 
3332                     IF (l_err_num <> 0) THEN
3333 
3334                       RAISE CST_FAILED_INSERT_PTI;
3335 
3336                     END IF;
3337 
3338                   END LOOP;
3339 
3340                         IF l_rownum = 0 THEN
3341 
3342                     RAISE NO_ROWS_TO_INSERT;
3343 
3344                         END IF;
3345 
3346                 END IF;  /*CC MO SE ET END*/
3347 
3348     /* Insert Material Overhead absorption for the receiving side in a direct
3349        interorg transaction */
3350 
3351      If (l_earn_tomoh <> -99 AND p_transaction_id <> -99 ) then
3352 
3353       FND_FILE.PUT_LINE(FND_FILE.LOG,'Collecting the MOH absorption on the receiving side');
3354 
3355              l_rownum := 0;
3356 
3357              For c_rec9 in c_sel_tomoh LOOP
3358 
3359                 l_rownum := l_rownum + 1;
3360 
3361                 l_stmt_num := 151;
3362 
3363 
3364           /* changes to support the Blue print organizations */
3365 
3366                   /* get the org ID on the transfer Txn */
3367                    select organization_id into l_org_id
3368                    from mtl_material_transactions
3369                    where transaction_id = c_rec9.orig_transaction_reference;
3370 
3371                  /* set the transaction source accordingly if the org is BP */
3372 
3373                     select NVL(pa_posting_flag,'N'),
3374                            NVL(pa_autoaccounting_flag,'N')
3375                     into l_blue_print_enabled_flag,
3376                          l_autoaccounting_flag
3377                     from pjm_org_parameters
3378                     where organization_id = l_org_id ;
3379 
3380             If l_blue_print_enabled_flag = 'Y' then
3381 
3382               FND_FILE.PUT_LINE(FND_FILE.LOG,'Blue print org ' || l_org_id);
3383                If l_autoaccounting_flag = 'Y' then
3384 
3385                     /* BP and autoaccounting  */
3386                     Select pts1.transaction_source,
3387                            pts1.transaction_source,
3388                            pts1.transaction_source
3389                      into  l_transaction_source,
3390                            l_inv_txn_src_literal,
3391                            l_cap_inv_txn_src_literal
3392                      From  pa_transaction_sources pts1
3393                     Where  pts1.transaction_source = 'PJM_CSTBP_INV_NO_ACCOUNTS';
3394                    If (l_transaction_source is NULL) then
3395 
3396                       RAISE CST_FAILED_TXN_SRC;
3397 
3398                    end If;
3399                else
3400                /* BP and no autoaccounting */
3401                     Select pts1.transaction_source,
3402                            pts1.transaction_source,
3403                            pts1.transaction_source
3404                      into  l_transaction_source,
3405                            l_inv_txn_src_literal,
3406                            l_cap_inv_txn_src_literal
3407                      From  pa_transaction_sources pts1
3408                     Where  pts1.transaction_source = 'PJM_CSTBP_INV_ACCOUNTS';
3409 
3410                        If (l_transaction_source is NULL ) then
3411 
3412                         RAISE CST_FAILED_TXN_SRC;
3413 
3414                        end If;
3415                end if; /* end of check for auto accounting */
3416 
3417              ELSE /* non BP org */
3418 
3419                SELECT   pts1.transaction_source,
3420                         pts1.transaction_source,
3421                         pts2.transaction_source
3422                INTO     l_inv_txn_src_literal,
3423                         l_transaction_source,
3424                         l_cap_inv_txn_src_literal
3425                FROM     pa_transaction_sources pts1,
3426                         pa_transaction_sources pts2
3427                WHERE  pts1.transaction_source = 'Inventory'
3428                AND  pts2.transaction_source = 'Inventory Misc';
3429 
3430              END IF; /* check for BP org */
3431 
3432              l_stmt_num := 152;
3433 
3434         /* Now call insert routine to insert into interface */
3435 
3436                     pm_insert_pti_pvt
3437                         (p_transaction_source         =>
3438                                              l_transaction_source,
3439                          p_batch_name                 =>c_rec9.batch_name,
3440                          p_expenditure_ending_date    =>
3441                                              c_rec9.expenditure_ending_date,
3442                          p_employee_number            =>c_rec9.employee_number,
3443                          p_organization_name          =>
3444                                              c_rec9.organization_name,
3445                          p_expenditure_item_date      =>
3446                                              c_rec9.expenditure_item_date,
3447                          p_project_number             =>c_rec9.project_number,
3448                          p_task_number                =>c_rec9.task_number,
3449                          p_expenditure_type           =>c_rec9.expenditure_type,
3450                          p_pa_quantity                =>c_rec9.quantity,
3451                          p_raw_cost                   =>c_rec9.raw_cost,
3452                          p_expenditure_comment        =>
3453                                              c_rec9.expenditure_comment,
3454                          p_orig_transaction_reference =>
3455                                              c_rec9.orig_transaction_reference,
3456                          p_raw_cost_rate              =>c_rec9.raw_cost_rate,
3457                          p_unmatched_negative_txn_flag=>
3458                                              c_rec9.unmatched_negative_txn_flag,
3459                          p_gl_date                    =>c_rec9.gl_date,
3460                          p_org_id                     =>c_rec9.org_id,
3461                          p_burdened_cost              =>c_rec9.burdened_cost,
3462                          p_burdened_cost_rate         =>
3463                                              c_rec9.burdened_cost_rate,
3464                          p_system_linkage             =>c_rec9.system_linkage,
3465                          p_transaction_status_code    =>
3466                                              c_rec9.transaction_status_code,
3467                          p_denom_currency_code        =>
3468                                              c_rec9.denom_currency_code,
3469                          p_transaction_id             =>p_transaction_id,
3470                          p_transaction_action_id      =>p_transaction_action_id,
3471                          p_transaction_source_type_id =>
3472                                              p_transaction_source_type_id,
3473                          p_organization_id            =>p_organization_id,
3474                          p_inventory_item_id          =>p_inventory_item_id,
3475                          p_cost_element_id            =>c_rec9.cost_element_id,
3476                          p_resource_id                =>c_rec9.resource_id,
3477                          p_source_flag                =>1,
3478                          p_variance_flag              =>-1,
3479                          p_primary_quantity           =>p_primary_quantity ,
3480                          p_transfer_organization_id   =>
3481                                              p_transfer_organization_id,
3482                          p_fob_point                  =>l_fob_point,
3483                          p_wip_entity_id              =>NULL,
3484                          p_basis_resource             =>NULL,
3485                          p_type_class                 =>p_type_class,
3486                          p_project_id                 =>p_project_id,
3487                          p_task_id                    =>p_task_id,
3488                          p_transaction_date           =>p_transaction_date,
3489                          p_cost_group_id              =>p_cost_group_id,
3490                          p_transfer_cost_group_id     =>
3491                                              p_transfer_cost_group_id,
3492                          p_transaction_source_id      =>
3493                                              p_transaction_source_id,
3494                          p_to_project_id              =>p_to_project_id,
3495                          p_to_task_id                 =>p_to_task_id,
3496                          p_source_project_id          =>p_source_project_id,
3497                          p_source_task_id             =>p_source_task_id,
3498                          p_transfer_transaction_id    =>
3499                                              p_transfer_transaction_id,
3500                          p_primary_cost_method        =>p_primary_cost_method,
3501                          p_acct_period_id             =>p_acct_period_id,
3502                          p_exp_org_id                 =>p_exp_org_id,
3503                          p_distribution_account_id    =>
3504                                              p_distribution_account_id,
3505                          p_proj_job_ind               =>p_proj_job_ind,
3506                          p_first_matl_se_exp_type     =>
3507                                              p_first_matl_se_exp_type,
3508                          p_inv_txn_source_literal     =>
3509                                              l_inv_txn_src_literal,
3510                          p_cap_txn_source_literal     =>
3511                                              l_cap_inv_txn_src_literal,
3512                          p_inv_syslink_literal        =>p_inv_syslink_literal,
3513                          p_bur_syslink_literal        =>p_bur_syslink_literal,
3514                          p_wip_syslink_literal        =>p_wip_syslink_literal,
3515                          p_user_def_exp_type          =>p_user_def_exp_type,
3516                          p_flow_schedule              =>p_flow_schedule,
3517                          p_si_asset_yes_no            =>p_si_asset_yes_no,
3518                          p_transfer_si_asset_yes_no   =>
3519                                              p_transfer_si_asset_yes_no,
3520 
3521                          O_err_num                    =>l_err_num,
3522                          O_err_code                   =>l_err_code,
3523                          O_err_msg                    =>l_err_msg
3524                      );
3525 
3526                     IF (l_err_num <> 0) THEN
3527 
3528                       RAISE CST_FAILED_INSERT_PTI;
3529 
3530                     END IF;
3531 
3532                   END LOOP;
3533 
3534                   l_stmt_num := 154;
3535 
3536                         IF l_rownum = 0 THEN
3537 
3538                     RAISE NO_ROWS_TO_INSERT;
3539 
3540                         END IF;
3541 
3542    END IF; /* end of check for l_earn_tomoh */
3543 
3544                 --------------------------------------------------------------
3545                 -- CASE 0: All Transactions having the SI as project costed 'Y'
3546                 --           Misc Family Transactions referring a Proj SI
3547                 --           PO Family referring Proj SI
3548                 --           WIP Component Issue/Rtn referring a Proj SI
3549                 --           Issue side of a Sub-Trf referring a Proj SI
3550                 --                                        ( only neg Txn )
3551                 --           Cycle Count referring a Proj SI
3552                 --           Physical Inv referring a Proj SI
3553                 --           Issue side of Direct Org Trf referring a Proj SI
3554                 --           Receipt side of Direct Org Trf referring a Proj SI
3555                 --           Issue/Receipt side of Inter Org Trf referring a Proj
3556                 --           SI and being cost collected Organization
3557                 --
3558                 --         Exclude all Assy Completion and Returns that have
3559                 --         both the Job and SI as Project Costed 'Y' in which
3560                 --         case one needs earn only the MO earnings. The same
3561                 --         is being taken care in the previous IF statement.
3562                 --
3563                 -- CASE 1: Those Assy Completion/Rtn Transactions involving an
3564                 --         Issue / Rtn from/to a Sub that project costed as 'Y'
3565                 --         and the job as not to be project costed which
3566                 --           implies that this level MO is reported on a SE basis
3567                 --           and all the elements other than this level MO
3568                 --         are reported elementally.
3569                 --
3570 
3571                 -- It is assumed that cost_element_id will not be null in
3572                 -- mta for accounting_line_type = 1 for an average cost org.
3573                 --------------------------------------------------------------
3574 
3575 
3576                 --------------------------------------------------------------
3577                 -- this part is added for bug 1036822
3578                 -- this if statement is one level higher
3579                 -- then the older code because
3580                 -- it is trying to exclude the
3581                 -- DEFAULT PROJECT ASSEMBLY COMPLETION/RETURN
3582                 -- transactions which would be pick up by CASE 0 in the follow.
3583                 -- so this one level higher if statement will exclude those
3584                 -- transaction before it got picked up by CASE 0
3585                 -- bug#1036498
3586                 -- For Direct Interorg trf , sending org is std
3587                 --(no cost collection enabled,
3588                 -- proj ref enab)
3589                 -- and recv org is average  (cc enabled , proj ref enabled)
3590                 -- then can trf
3591                 -- betwn same proj and task.
3592                 -- Because cost collection is disabled from sending org,
3593                 -- so cost collection
3594                 -- has to be done on the receiving org.
3595                 ---------------------------------------------------------------
3596 
3597                 ---------------------------------------------------------------
3598                 -- for bug 781967, make sure that txn within the same project
3599                 -- will not get posted into pa_transaction_interface_all
3600                 -- bug 862689
3601                 -- bug 1036822
3602                 -- with DEFAULT
3603                 -- PROJECT OPTION set, the WIP COMPLETION from a common job
3604                 -- does notget pick up by cost collector.
3605                 ---------------------------------------------------------------
3606 
3607                 IF NOT(    l_default_project is not null
3608                   AND p_transaction_action_id in (31,32)
3609                   AND p_proj_job_ind = 0)
3610                 THEN
3611 
3612 
3613                   l_stmt_num := 155;
3614                 -- Bug 2095581
3615                 -- In case of an interorg transfer,
3616                 -- Cost Collection is done if sending organization in the
3617                 -- interorg transfer is not cost collection enabled.
3618 
3619                 /* Bug #2349027. Interorg transfers should be cost collected
3620                    even if the project_id = to_project_id because expenditure
3621                    orgs are different. */
3622 
3623                   IF    ((p_transaction_action_id in (3,12,21) and
3624                           p_project_id is not null)
3625                               -- p_to_project_id is not null  and
3626                        -- l_cost_collection_enabled_snd <> 1)
3627                        -- l_primary_cost_method_snd = 1)
3628                       OR
3629                       --bug#1036498
3630                       (    p_project_id is not null     /* CASE 0 */
3631                        AND NOT (    p_transaction_action_id in (31,32)
3632                                 AND p_project_id is not null
3633                                 AND p_proj_job_ind = 1
3634                                )
3635                        AND NOT( p_project_id = nvl(p_to_project_id,-9)
3636                                 and p_task_id = nvl(p_to_task_id,-9)
3637                               )
3638                       )
3639                       OR
3640                       /* Consigned ownership trasfers */
3641                       ( p_project_id is not null
3642                          AND p_transaction_action_id = 6)
3643 
3644                       /* changes for the PJM Blue print project and drop ship*/
3645                       OR
3646                       ( p_project_id is not null
3647                         AND p_transaction_action_id = 26
3648                       )
3649 
3650                          OR (    p_transaction_action_id in (31,32)
3651                        AND p_project_id is not null
3652                        AND p_proj_job_ind = 0        /* CASE 1 */
3653                        AND l_default_project is NULL
3654                       ))
3655                   THEN
3656                         /* CC ELE ET START INCLUSIVE OF All Filetered 31,32*/
3657 
3658                     l_rownum   := 0;
3659 
3660 
3661                     /* bug 3978501 */
3662                     l_no_row_c_sel_prj := 0;
3663 
3664                     l_stmt_num := 160;
3665 
3666 
3667                     if (l_debug = 'Y') then
3668                        FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Collecting Project related Locator txn costs ...');
3669                     end if;
3670 
3671                     FOR c_rec3 IN c_sel_prj_txn  LOOP
3672 
3673                       --------------------------------------------------------
3674                       -- bug 888190
3675                       -- if it is capital project and expenditure type
3676                       -- is user entered,
3677                       -- then get it from p_expenditure_type
3678                       -- otherwise derived it from the system using cceet.
3679                       -- proj. misc txn with user entered expenditure type
3680                       -- p_user_def_exp_type: 0 is system derived,
3681                       -- 1 is user entered
3682                       --------------------------------------------------------
3683 
3684                       IF (p_type_class = 1) and (p_user_def_exp_type = 1) THEN
3685 
3686                         IF (p_expenditure_type is null) THEN
3687 
3688                          RAISE CST_FAILED_GET_EXP_TYPE;
3689 
3690                         ELSE
3691 
3692                          l_exp_type := p_expenditure_type;
3693 
3694                         END IF;
3695 
3696                       ELSE
3697 
3698                         l_stmt_num := 165;
3699                         IF p_transaction_action_id = 17 THEN
3700                           SELECT
3701                             EXPENDITURE_TYPE
3702                           INTO
3703                             l_exp_type
3704                           FROM
3705                             MTL_MATERIAL_TRANSACTIONS
3706                           WHERE
3707                               transaction_id = p_transaction_id;
3708                         ELSE
3709                           SELECT
3710                             decode(c_rec3.cost_element_id,1,
3711                               decode(p_transaction_source_type_id,1,
3712                                              p_first_matl_se_exp_type,
3713                                 decode(sign(p_primary_quantity),1,
3714                                   cceet.EXPENDITURE_TYPE_IN,
3715                                   cceet.EXPENDITURE_TYPE_OUT)),
3716                               decode(sign(p_primary_quantity),1,
3717                                 cceet.EXPENDITURE_TYPE_IN,
3718                                 cceet.EXPENDITURE_TYPE_OUT))
3719                           INTO l_exp_type
3720                           FROM cst_cost_elem_exp_types cceet
3721                           WHERE cceet.cost_element_id = c_rec3.cost_element_id;
3722                         END IF; -- action_id = 7
3723 
3724                       END IF;
3725 
3726                       l_rownum := l_rownum + 1;
3727 
3728                       l_stmt_num := 170;
3729 
3730 
3731                    If c_rec3.burdened_cost = 0 then
3732                       fnd_file.put_line(fnd_file.log,'Burdened cost is zero');
3733                    end if;
3734 
3735                    If c_rec3.burdened_cost <> 0 then
3736 
3737                       pm_insert_pti_pvt
3738                         (p_transaction_source         =>
3739                                              c_rec3.transaction_source,
3740                          p_batch_name                 =>c_rec3.batch_name,
3741                          p_expenditure_ending_date    =>
3742                                              c_rec3.expenditure_ending_date,
3743                          p_employee_number            =>c_rec3.employee_number,
3744                          p_organization_name          =>
3745                                             NVL(l_recv_iss_organization_name,
3746                                              c_rec3.organization_name),
3747                          p_expenditure_item_date      =>
3748                                              c_rec3.expenditure_item_date,
3749                          p_project_number             =>c_rec3.project_number,
3750                          p_task_number                =>c_rec3.task_number,
3751                          p_expenditure_type           =>l_exp_type,
3752                          p_pa_quantity                =>c_rec3.quantity,
3753                          p_raw_cost                   =>c_rec3.raw_cost,
3754                          p_expenditure_comment        =>
3755                                              c_rec3.expenditure_comment,
3756                          p_orig_transaction_reference =>
3757                                              c_rec3.orig_transaction_reference,
3758                          p_raw_cost_rate              =>c_rec3.raw_cost_rate,
3759                          p_unmatched_negative_txn_flag=>
3760                                              c_rec3.unmatched_negative_txn_flag,
3761                          p_gl_date                    =>c_rec3.gl_date,
3762                          p_org_id                     =>c_rec3.org_id,
3763                          p_burdened_cost              =>c_rec3.burdened_cost,
3764                          p_burdened_cost_rate         =>
3765                                              c_rec3.burdened_cost_rate,
3766                          p_system_linkage             =>c_rec3.system_linkage,
3767                          p_transaction_status_code    =>
3768                                              c_rec3.transaction_status_code,
3769                          p_denom_currency_code        =>
3770                                              c_rec3.denom_currency_code,
3771                          p_transaction_id             =>p_transaction_id,
3772 
3773                          p_transaction_action_id      =>p_transaction_action_id,
3774                          p_transaction_source_type_id =>
3775                                              p_transaction_source_type_id,
3776                          p_organization_id            =>p_organization_id,
3777                          p_inventory_item_id          =>p_inventory_item_id,
3778                          p_cost_element_id            =>c_rec3.cost_element_id,
3779                          p_resource_id                =>NULL,
3780                          p_source_flag                =>-1,
3781                          p_variance_flag              =>-1,
3782                          p_primary_quantity           =>p_primary_quantity ,
3783                          p_transfer_organization_id   =>
3784                                              p_transfer_organization_id,
3785                          p_fob_point                  =>l_fob_point,
3786                          p_wip_entity_id              =>NULL,
3787                          p_basis_resource             =>NULL,
3788 
3789                          p_type_class                 =>p_type_class,
3790                          p_project_id                 =>p_project_id,
3791                          p_task_id                    =>p_task_id,
3792                          p_transaction_date           =>p_transaction_date,
3793                          p_cost_group_id              =>p_cost_group_id,
3794                          p_transfer_cost_group_id     =>
3795                                              p_transfer_cost_group_id,
3796                          p_transaction_source_id      =>
3797                                              p_transaction_source_id,
3798                          p_to_project_id              =>p_to_project_id,
3799                          p_to_task_id                 =>p_to_task_id,
3800                          p_source_project_id          =>p_source_project_id,
3801                          p_source_task_id             =>p_source_task_id,
3802                          p_transfer_transaction_id    =>
3803                                              p_transfer_transaction_id,
3804                          p_primary_cost_method        =>p_primary_cost_method,
3805                          p_acct_period_id             =>p_acct_period_id,
3806                          p_exp_org_id                 =>p_exp_org_id,
3807                          p_distribution_account_id    =>
3808                                              p_distribution_account_id,
3809                          p_proj_job_ind               =>p_proj_job_ind,
3810                          p_first_matl_se_exp_type     =>
3811                                              p_first_matl_se_exp_type,
3812                          p_inv_txn_source_literal     =>
3813                                              p_inv_txn_source_literal,
3814                          p_cap_txn_source_literal     =>
3815                                              p_cap_txn_source_literal,
3816                          p_inv_syslink_literal        =>p_inv_syslink_literal,
3817                          p_bur_syslink_literal        =>p_bur_syslink_literal,
3818                          p_wip_syslink_literal        =>p_wip_syslink_literal,
3819                          p_user_def_exp_type          =>p_user_def_exp_type,
3820                          p_flow_schedule              =>p_flow_schedule,
3821                          p_si_asset_yes_no            =>p_si_asset_yes_no,
3822                          p_transfer_si_asset_yes_no   =>
3823                                              p_transfer_si_asset_yes_no,
3824 
3825                          O_err_num                    =>l_err_num,
3826                          O_err_code                   =>l_err_code,
3827                          O_err_msg                    =>l_err_msg
3828                      );
3829 
3830                       IF (l_err_num <> 0) THEN
3831 
3832                         RAISE CST_FAILED_INSERT_PTI;
3833 
3834                       END IF;
3835 
3836                     END IF; /* end of check for burdened cost to be 0 */
3837 
3838                     END LOOP;
3839 
3840                           IF l_rownum = 0 THEN
3841 
3842                     /* bug 3978501 commenting out the excpetion raise as there maybe PPV to insert */
3843 
3844                      /* RAISE NO_ROWS_TO_INSERT; */
3845                     l_no_row_c_sel_prj := 1;
3846 
3847                           END IF;
3848 
3849                   END IF;
3850 
3851                 END IF;  -- bug 1036822
3852 
3853                 ---------------------------------------------------------------
3854                 -- for bug 781967, make sure that txn within the same
3855                 -- project won't
3856                 -- get posted into pa_transaction_interface_all
3857                 -- bug 930565, added nvl to take care sub-inventory transaction
3858                 -- from common subinventory to project subinventory
3859                 ---------------------------------------------------------------
3860 
3861                 IF    (-- p_transaction_action_id IN (2 , 3)
3862                               p_to_project_id is not null         /* CASE 0 */
3863                        AND ((p_transaction_action_id = 2
3864                             AND NOT( nvl(p_project_id,-9) = p_to_project_id
3865                                      and nvl(p_task_id,-9) = p_to_task_id
3866                                    )
3867                             )
3868                             OR
3869                             p_transaction_action_id = 3
3870                           /* bug fix for bug 3150050 */
3871                             OR
3872                             p_transaction_action_id = 28
3873                            )
3874                             )
3875                 THEN
3876                                    /* CC ELE ET START FILTERED 2 TRFSIDE */
3877 
3878                         ------------------------------------------------------
3879                         -- CASE 0: All Transactions having the TRF SI as
3880                         --         project costed 'Y' . Transaction is looked
3881                         --         from the receiving SI perspective. The SI
3882                         --         would be the Transfer SI for a -ve qty Txn.
3883                         --         Only the -ve qty txns are picked since the
3884                         --         is based on them and not the +ve ones.
3885                         --
3886                         -- Direct IO Xfer with TO_PROJECT NOT NULL
3887                         ------------------------------------------------------
3888 
3889                   l_rownum   := 0;
3890 
3891                  /* bug 3978501 set the variable to 0 to start off */
3892 
3893                   l_no_row_c_sel_toprj := 0;
3894 
3895                   l_stmt_num := 175;
3896 
3897                   if (l_debug = 'Y') then
3898                      FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Collecting Transfer Side Locator Project costs ...');
3899                   end if;
3900 
3901             FOR c_rec4 IN c_sel_toprj_txn  LOOP
3902 
3903                     l_rownum := l_rownum + 1;
3904                     /* Get Expenditure Type from MMT for Internal SO
3905                        Issue and Receipt to Expense */
3906                     l_stmt_num := 177;
3907 
3908                     IF( ( p_transaction_action_id = 1 AND p_transaction_source_type_id = 8 )
3909                         OR p_transaction_action_id = 17 ) THEN
3910                       SELECT EXPENDITURE_TYPE
3911                       INTO   c_rec4.EXPENDITURE_TYPE
3912                       FROM   MTL_MATERIAL_TRANSACTIONS
3913                       WHERE  transaction_id = p_transaction_id;
3914                     END IF;
3915 
3916                     l_stmt_num := 180;
3917 
3918         /* changes to support the Blue print organizations */
3919 
3920                   /* get the org ID on the transfer Txn */
3921                    select organization_id into l_org_id
3922                    from mtl_material_transactions
3923                    where transaction_id = c_rec4.orig_transaction_reference;
3924 
3925                  /* set the transaction source accordingly if the org is BP */
3926 
3927                     select NVL(pa_posting_flag,'N'),
3928                            NVL(pa_autoaccounting_flag,'N')
3929                     into l_blue_print_enabled_flag,
3930                          l_autoaccounting_flag
3931                     from pjm_org_parameters
3932                     where organization_id = l_org_id ;
3933 
3934           If l_blue_print_enabled_flag = 'Y' then
3935 
3936                If l_autoaccounting_flag = 'Y' then
3937 
3938                     /* BP and autoaccounting  */
3939                     Select pts1.transaction_source,
3940                            pts1.transaction_source,
3941                            pts1.transaction_source
3942                      into  l_transaction_source,
3943                            l_inv_txn_src_literal,
3944                            l_cap_inv_txn_src_literal
3945                      From  pa_transaction_sources pts1
3946                     Where  pts1.transaction_source = 'PJM_CSTBP_INV_NO_ACCOUNTS';
3947                    If (l_transaction_source is NULL) then
3948 
3949                       RAISE CST_FAILED_TXN_SRC;
3950 
3951                    end If;
3952                else
3953 
3954                     /* BP and no autoaccounting */
3955                     Select pts1.transaction_source,
3956                            pts1.transaction_source,
3957                            pts1.transaction_source
3958                      into  l_transaction_source,
3959                            l_inv_txn_src_literal,
3960                            l_cap_inv_txn_src_literal
3961                      From  pa_transaction_sources pts1
3962                     Where  pts1.transaction_source = 'PJM_CSTBP_INV_ACCOUNTS';
3963 
3964                        If (l_transaction_source is NULL ) then
3965 
3966                         RAISE CST_FAILED_TXN_SRC;
3967 
3968                        end If;
3969                end if; /* end of check for auto accounting */
3970 
3971            ELSE /* non BP org */
3972 
3973                SELECT   pts1.transaction_source,
3974                         pts1.transaction_source,
3975                         pts2.transaction_source
3976                INTO     l_inv_txn_src_literal,
3977                         l_transaction_source,
3978                         l_cap_inv_txn_src_literal
3979                FROM     pa_transaction_sources pts1,
3980                         pa_transaction_sources pts2
3981                WHERE  pts1.transaction_source = 'Inventory'
3982                AND  pts2.transaction_source = 'Inventory Misc';
3983 
3984           END IF; /* check for BP org */
3985 
3986                 If c_rec4.burdened_cost = 0 then
3987                  fnd_file.put_line(fnd_file.log,'Burdened cost is zero');
3988                 end if;
3989 
3990                 If c_rec4.burdened_cost <> 0 then
3991 
3992                     pm_insert_pti_pvt
3993                         (p_transaction_source         =>
3994                                              l_transaction_source,
3995                          p_batch_name                 =>c_rec4.batch_name,
3996                          p_expenditure_ending_date    =>
3997                                              c_rec4.expenditure_ending_date,
3998                          p_employee_number            =>c_rec4.employee_number,
3999                          p_organization_name          =>
4000                                              c_rec4.organization_name,
4001                          p_expenditure_item_date      =>
4002                                              c_rec4.expenditure_item_date,
4003                          p_project_number             =>c_rec4.project_number,
4004                          p_task_number                =>c_rec4.task_number,
4005                          p_expenditure_type           =>c_rec4.expenditure_type,
4006                          p_pa_quantity                =>c_rec4.quantity,
4007                          p_raw_cost                   =>c_rec4.raw_cost,
4008                          p_expenditure_comment        =>
4009                                              c_rec4.expenditure_comment,
4010                          p_orig_transaction_reference =>
4011                                              c_rec4.orig_transaction_reference,
4012                          p_raw_cost_rate              =>c_rec4.raw_cost_rate,
4013                          p_unmatched_negative_txn_flag=>
4014                                              c_rec4.unmatched_negative_txn_flag,
4015                          p_gl_date                    =>c_rec4.gl_date,
4016                          p_org_id                     =>c_rec4.org_id,
4017                          p_burdened_cost              =>c_rec4.burdened_cost,
4018                          p_burdened_cost_rate         =>
4019                                              c_rec4.burdened_cost_rate,
4020                          p_system_linkage             =>c_rec4.system_linkage,
4021                          p_transaction_status_code    =>
4022                                              c_rec4.transaction_status_code,
4023                          p_denom_currency_code        =>
4024                                              c_rec4.denom_currency_code,
4025                          p_transaction_id             =>p_transaction_id,
4026 
4027                          p_transaction_action_id      =>p_transaction_action_id,
4028                          p_transaction_source_type_id =>
4029                                              p_transaction_source_type_id,
4030                          p_organization_id            =>p_organization_id,
4031                          p_inventory_item_id          =>p_inventory_item_id,
4032                          p_cost_element_id            =>c_rec4.cost_element_id,
4033                          p_resource_id                =>NULL,
4034                          p_source_flag                =>1,
4035                          p_variance_flag              =>-1,
4036                          p_primary_quantity           =>p_primary_quantity ,
4037                          p_transfer_organization_id   =>
4038                                              p_transfer_organization_id,
4039                          p_fob_point                  =>l_fob_point,
4040                          p_wip_entity_id              =>NULL,
4041                          p_basis_resource             =>NULL,
4042 
4043                          p_type_class                 =>p_type_class,
4044                          p_project_id                 =>p_project_id,
4045                          p_task_id                    =>p_task_id,
4046                          p_transaction_date           =>p_transaction_date,
4047                          p_cost_group_id              =>p_cost_group_id,
4048                          p_transfer_cost_group_id     =>
4049                                              p_transfer_cost_group_id,
4050                          p_transaction_source_id      =>
4051                                              p_transaction_source_id,
4052                          p_to_project_id              =>p_to_project_id,
4053                          p_to_task_id                 =>p_to_task_id,
4054                          p_source_project_id          =>p_source_project_id,
4055                          p_source_task_id             =>p_source_task_id,
4056                          p_transfer_transaction_id    =>
4057                                              p_transfer_transaction_id,
4058                          p_primary_cost_method        =>p_primary_cost_method,
4059                          p_acct_period_id             =>p_acct_period_id,
4060                          p_exp_org_id                 =>p_exp_org_id,
4061                          p_distribution_account_id    =>
4062                                              p_distribution_account_id,
4063                          p_proj_job_ind               =>p_proj_job_ind,
4064                          p_first_matl_se_exp_type     =>
4065                                              p_first_matl_se_exp_type,
4066                          p_inv_txn_source_literal     =>
4067                                              l_inv_txn_src_literal,
4068                          p_cap_txn_source_literal     =>
4069                                              l_cap_inv_txn_src_literal,
4070                          p_inv_syslink_literal        =>p_inv_syslink_literal,
4071                          p_bur_syslink_literal        =>p_bur_syslink_literal,
4072                          p_wip_syslink_literal        =>p_wip_syslink_literal,
4073                          p_user_def_exp_type          =>p_user_def_exp_type,
4074                          p_flow_schedule              =>p_flow_schedule,
4075                          p_si_asset_yes_no            =>p_si_asset_yes_no,
4076                          p_transfer_si_asset_yes_no   =>
4077                                              p_transfer_si_asset_yes_no,
4078 
4079                          O_err_num                    =>l_err_num,
4080                          O_err_code                   =>l_err_code,
4081                          O_err_msg                    =>l_err_msg
4082                      );
4083 
4084                     IF (l_err_num <> 0) THEN
4085 
4086                       RAISE CST_FAILED_INSERT_PTI;
4087 
4088                     END IF;
4089 
4090                  END IF; /* End of check for burdened cost to be 0 */
4091 
4092                   END LOOP;
4093 
4094                         IF l_rownum = 0 THEN
4095                     /* Bug 2386069
4096                        If the receiving txn has not been costed yet, update MMT with a
4097                       warning but do not reset the pm_cost_collected flag to error.
4098                      */
4099                     SELECT mmt.costed_flag
4100                     INTO   l_costed_flag
4101                     FROM   mtl_material_transactions mmt
4102                     WHERE  mmt.transaction_id  = decode(p_transaction_action_id,
4103                                                 3, p_transfer_transaction_id ,
4104                                                 p_transaction_id)
4105                     AND   mmt.organization_id = decode(p_transaction_action_id,
4106                                                 3, p_transfer_organization_id,
4107                                                 p_organization_id);
4108 
4109                     IF (l_costed_flag = 'N') THEN
4110                        if (l_debug = 'Y') then
4111                          fnd_file.put_line(fnd_file.log, 'Receving Txn not yet costed!!!!');
4112                        end if;
4113                        RAISE ROW_NOT_COSTED;
4114                     ELSE
4115 
4116                      /* bug 3978501 commenting out the excpetion part as there may be PPV to insert */
4117 
4118                      /* RAISE NO_ROWS_TO_INSERT; */
4119                      l_no_row_c_sel_toprj := 1;
4120 
4121                     END IF;
4122 
4123                         END IF;
4124 
4125                 END IF; /* CC ALL ELE ET START FILTERED 2 TRFSIDE */
4126 
4127                 ---------------------------------------------------------------
4128                     /* CC ALL ELE ET START FILTERED WIPCompIss/Rtn,31,32 TRFSIDE */
4129 
4130                         -- CASE 1: All WIP/CFM Issue/Return and -ve WIP/CFM
4131                         --         Issue/Return Transactions having the
4132                         --         JOB as project costed 'Y', imply their CC
4133                         --         w.r.t the source project and task
4134                         --
4135                         -- CASE 2: All Transactions having the JOB/CFM project
4136                         --         costed 'Y' and SI as project costed 'N' ,
4137                         --         belonging to group of WIP Assy Completion
4138                         --         or Returns, implying their CC w.r.t the
4139                         --           source project and task
4140                         --
4141                         -- CASE 3: All Misc Family Transactions belonging to
4142                         --         Capital Project transaction type
4143                         --         ( User given project and task information
4144                         --           at transaction time are stored in the
4145                         --           source project and source task fields )
4146                         --          If user does not specify an expenditure_type
4147                         --          the default associations will be used.
4148                         --
4149                         -- NOTE: The Cost Group being used is cost_group_id and
4150                         --       the transfer_cost_group_id although the project
4151                         --       and task being used are source project and
4152                         --       source task, because MCACD has one for the Txn
4153                         --       and that is w.r.t the SI's cost_group
4154                         --
4155                 ---------------------------------------------------------------
4156 
4157 
4158                 IF    (    p_transaction_action_id in (1,27,33,34)
4159                               AND p_transaction_source_type_id = 5
4160                               AND p_proj_job_ind = 1            /* CASE 1 */
4161                       )
4162                       OR (    p_transaction_action_id in (31,32)
4163                               AND p_transaction_source_type_id = 5
4164                        AND p_project_id is null
4165                        AND p_proj_job_ind = 1            /* CASE 2 */
4166                       )
4167                    OR (p_type_class = 1)                 /* CASE 3 */
4168 
4169                 THEN
4170 
4171                   l_rownum   := 0;
4172 
4173                   l_stmt_num := 185;
4174 
4175                   if (l_debug = 'Y') then
4176                      FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Collecting source side project costs ...');
4177                   end if;
4178 
4179                   FOR c_rec5 IN c_sel_src_txn  LOOP
4180 
4181                     ---------------------------------------------------------
4182                     -- bug 888190
4183                     -- if it is capital project and expenditure type is user
4184                     -- entered,
4185                     -- then get it from p_expenditure_type
4186                     -- otherwise derived it from the system using cceet.
4187                     ---------------------------------------------------------
4188 
4189                     IF (p_type_class = 1) and (p_user_def_exp_type = 1) THEN
4190 
4191                       -- proj. misc txn with user entered expenditure type
4192                       -- p_user_def_exp_type: 0 is system derived,
4193                       -- 1 is user entered
4194 
4195                       IF (p_expenditure_type is null) THEN
4196 
4197                          RAISE CST_FAILED_GET_EXP_TYPE;
4198 
4199                       ELSE
4200 
4201                          l_exp_type := p_expenditure_type;
4202 
4203                       END IF;
4204 
4205                     ELSE
4206 
4207                       SELECT
4208                          decode(sign(p_primary_quantity),1,
4209                                 cceet.EXPENDITURE_TYPE_OUT,
4210                          cceet.EXPENDITURE_TYPE_IN)
4211                       INTO l_exp_type
4212                       FROM cst_cost_elem_exp_types cceet
4213                       WHERE cceet.cost_element_id = c_rec5.cost_element_id;
4214 
4215                     END IF;
4216 
4217                     l_rownum := l_rownum + 1;
4218 
4219                     l_stmt_num := 190;
4220 
4221                     pm_insert_pti_pvt
4222                         (p_transaction_source         =>
4223                                              c_rec5.transaction_source,
4224                          p_batch_name                 =>c_rec5.batch_name,
4225                          p_expenditure_ending_date    =>
4226                                              c_rec5.expenditure_ending_date,
4227                          p_employee_number            =>c_rec5.employee_number,
4228                          p_organization_name          =>
4229                                              c_rec5.organization_name,
4230                          p_expenditure_item_date      =>
4231                                              c_rec5.expenditure_item_date,
4232                          p_project_number             =>c_rec5.project_number,
4233                          p_task_number                =>c_rec5.task_number,
4234                          p_expenditure_type           =>l_exp_type,
4235                          p_pa_quantity                =>c_rec5.quantity,
4236                          p_raw_cost                   =>c_rec5.raw_cost,
4237                          p_expenditure_comment        =>
4238                                              c_rec5.expenditure_comment,
4239                          p_orig_transaction_reference =>
4240                                              c_rec5.orig_transaction_reference,
4241                          p_raw_cost_rate              =>c_rec5.raw_cost_rate,
4242                          p_unmatched_negative_txn_flag=>
4243                                              c_rec5.unmatched_negative_txn_flag,
4244                          p_gl_date                    =>c_rec5.gl_date,
4245                          p_org_id                     =>c_rec5.org_id,
4246                          p_burdened_cost              =>c_rec5.burdened_cost,
4247                          p_burdened_cost_rate         =>
4248                                              c_rec5.burdened_cost_rate,
4249                          p_system_linkage             =>c_rec5.system_linkage,
4250                          p_transaction_status_code    =>
4251                                              c_rec5.transaction_status_code,
4252                          p_denom_currency_code        =>
4253                                              c_rec5.denom_currency_code,
4254                          p_transaction_id             =>p_transaction_id,
4255 
4256                          p_transaction_action_id      =>p_transaction_action_id,
4257                          p_transaction_source_type_id =>
4258                                              p_transaction_source_type_id,
4259                          p_organization_id            =>p_organization_id,
4260                          p_inventory_item_id          =>p_inventory_item_id,
4261                          p_cost_element_id            =>c_rec5.cost_element_id,
4262                          p_resource_id                =>NULL,
4263                          p_source_flag                =>1,
4264                          p_variance_flag              =>-1,
4265                          p_primary_quantity           =>p_primary_quantity ,
4266                          p_transfer_organization_id   =>
4267                                              p_transfer_organization_id,
4268                          p_fob_point                  =>l_fob_point,
4269                          p_wip_entity_id              =>NULL,
4270                          p_basis_resource             =>NULL,
4271 
4272                          p_type_class                 =>p_type_class,
4273                          p_project_id                 =>p_project_id,
4274                          p_task_id                    =>p_task_id,
4275                          p_transaction_date           =>p_transaction_date,
4276                          p_cost_group_id              =>p_cost_group_id,
4277                          p_transfer_cost_group_id     =>
4278                                              p_transfer_cost_group_id,
4279                          p_transaction_source_id      =>
4280                                              p_transaction_source_id,
4281                          p_to_project_id              =>p_to_project_id,
4282                          p_to_task_id                 =>p_to_task_id,
4283                          p_source_project_id          =>p_source_project_id,
4284                          p_source_task_id             =>p_source_task_id,
4285                          p_transfer_transaction_id    =>
4286                                              p_transfer_transaction_id,
4287                          p_primary_cost_method        =>p_primary_cost_method,
4288                          p_acct_period_id             =>p_acct_period_id,
4289                          p_exp_org_id                 =>p_exp_org_id,
4290                          p_distribution_account_id    =>
4291                                              p_distribution_account_id,
4292                          p_proj_job_ind               =>p_proj_job_ind,
4293                          p_first_matl_se_exp_type     =>
4294                                              p_first_matl_se_exp_type,
4295                          p_inv_txn_source_literal     =>
4296                                              p_inv_txn_source_literal,
4297                          p_cap_txn_source_literal     =>
4298                                              p_cap_txn_source_literal,
4299                          p_inv_syslink_literal        =>p_inv_syslink_literal,
4300                          p_bur_syslink_literal        =>p_bur_syslink_literal,
4301                          p_wip_syslink_literal        =>p_wip_syslink_literal,
4302                          p_user_def_exp_type          =>p_user_def_exp_type,
4303                          p_flow_schedule              =>p_flow_schedule,
4304                          p_si_asset_yes_no            =>p_si_asset_yes_no,
4305                          p_transfer_si_asset_yes_no   =>
4306                                              p_transfer_si_asset_yes_no,
4307 
4308                          O_err_num                    =>l_err_num,
4309                          O_err_code                   =>l_err_code,
4310                          O_err_msg                    =>l_err_msg
4311                      );
4312 
4313 
4314                     IF (l_err_num <> 0) THEN
4315 
4316                       RAISE CST_FAILED_INSERT_PTI;
4317 
4318                     END IF;
4319 
4320                   END LOOP;
4321 
4322                         IF l_rownum = 0 THEN
4323 
4324                     RAISE NO_ROWS_TO_INSERT;
4325 
4326                         END IF;
4327 
4328                 END IF; /*CC ALL ELE END FILTERED WIPCompIss/Rtn,31,32 TRFSIDE*/
4329 
4330                 ---- borrow payback
4331 
4332                 /* Borrow Payback Enhancements - Bug 2665290 */
4333 
4334                 IF    (p_transaction_action_id = 2) AND (l_txn_type = 68)
4335                   /*AND (p_cost_group_id <> p_transfer_cost_group_id) */
4336                 THEN
4337 
4338                   l_rownum   := 0;
4339                   l_stmt_num := 200;
4340 
4341                   if (l_debug = 'Y') then
4342                      FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Collecting Borrow Payback transactions ...');
4343                   end if;
4344 
4345                   FOR c_rec6 IN c_sel_bp_txn  LOOP
4346 
4347                     l_rownum := l_rownum + 1;
4348 
4349                     l_stmt_num := 205;
4350 
4351                     pm_insert_pti_pvt
4352                         (p_transaction_source         =>
4353                                              c_rec6.transaction_source,
4354                          p_batch_name                 =>c_rec6.batch_name,
4355                          p_expenditure_ending_date    =>
4356                                              c_rec6.expenditure_ending_date,
4357                          p_employee_number            =>c_rec6.employee_number,
4358                          p_organization_name          =>
4359                                              c_rec6.organization_name,
4360                          p_expenditure_item_date      =>
4361                                              c_rec6.expenditure_item_date,
4362                          p_project_number             =>c_rec6.project_number,
4363                          p_task_number                =>c_rec6.task_number,
4364                          p_expenditure_type           =>c_rec6.expenditure_type,
4365                          p_pa_quantity                =>c_rec6.quantity,
4366                          p_raw_cost                   =>c_rec6.raw_cost,
4367                          p_expenditure_comment        =>
4368                                              c_rec6.expenditure_comment,
4369                          p_orig_transaction_reference =>
4370                                              c_rec6.orig_transaction_reference,
4371                          p_raw_cost_rate              =>c_rec6.raw_cost_rate,
4372                          p_unmatched_negative_txn_flag=>
4373                                              c_rec6.unmatched_negative_txn_flag,
4374                          p_gl_date                    =>c_rec6.gl_date,
4375                          p_org_id                     =>c_rec6.org_id,
4376                          p_burdened_cost              =>c_rec6.burdened_cost,
4377                          p_burdened_cost_rate         =>
4378                                              c_rec6.burdened_cost_rate,
4379                          p_system_linkage             =>c_rec6.system_linkage,
4380                          p_transaction_status_code    =>
4381                                              c_rec6.transaction_status_code,
4382                          p_denom_currency_code        =>
4383                                              c_rec6.denom_currency_code,
4384                          p_transaction_id             =>p_transaction_id,
4385 
4386                          p_transaction_action_id      =>p_transaction_action_id,
4387                          p_transaction_source_type_id =>
4388                                              p_transaction_source_type_id,
4389                          p_organization_id            =>p_organization_id,
4390                          p_inventory_item_id          =>p_inventory_item_id,
4391                          p_cost_element_id            =>c_rec6.cost_element_id,
4392                          p_resource_id                =>NULL,
4393                          p_source_flag                =>-1,
4394                          p_variance_flag              =>1,
4395                          p_primary_quantity           =>p_primary_quantity ,
4396                          p_transfer_organization_id   =>
4397                                              p_transfer_organization_id,
4398                          p_fob_point                  =>l_fob_point,
4399                          p_wip_entity_id              =>NULL,
4400                          p_basis_resource             =>NULL,
4401 
4402                          p_type_class                 =>p_type_class,
4403                          p_project_id                 =>p_project_id,
4404                          p_task_id                    =>p_task_id,
4405                          p_transaction_date           =>p_transaction_date,
4406                          p_cost_group_id              =>p_cost_group_id,
4407                          p_transfer_cost_group_id     =>
4408                                              p_transfer_cost_group_id,
4409                          p_transaction_source_id      =>
4410                                              p_transaction_source_id,
4411                          p_to_project_id              =>p_to_project_id,
4412                          p_to_task_id                 =>p_to_task_id,
4413                          p_source_project_id          =>p_source_project_id,
4414                          p_source_task_id             =>p_source_task_id,
4415                          p_transfer_transaction_id    =>
4416                                              p_transfer_transaction_id,
4417                          p_primary_cost_method        =>p_primary_cost_method,
4418                          p_acct_period_id             =>p_acct_period_id,
4419                          p_exp_org_id                 =>p_exp_org_id,
4420                          p_distribution_account_id    =>
4421                                              p_distribution_account_id,
4422                          p_proj_job_ind               =>p_proj_job_ind,
4423                          p_first_matl_se_exp_type     =>
4424                                              p_first_matl_se_exp_type,
4425                          p_inv_txn_source_literal     =>
4426                                              p_inv_txn_source_literal,
4427                          p_cap_txn_source_literal     =>
4428                                              p_cap_txn_source_literal,
4429                          p_inv_syslink_literal        =>p_inv_syslink_literal,
4430                          p_bur_syslink_literal        =>p_bur_syslink_literal,
4431                          p_wip_syslink_literal        =>p_wip_syslink_literal,
4432                          p_user_def_exp_type          =>p_user_def_exp_type,
4433                          p_flow_schedule              =>p_flow_schedule,
4434                          p_si_asset_yes_no            =>p_si_asset_yes_no,
4435                          p_transfer_si_asset_yes_no   =>
4436                                              p_transfer_si_asset_yes_no,
4437 
4438                          O_err_num                    =>l_err_num,
4439                          O_err_code                   =>l_err_code,
4440                          O_err_msg                    =>l_err_msg
4441                      );
4442 
4443                     /* For standard costing orgs, the borrow payback variance has to
4444                        be applied to both the borrowing and the lending projects*/
4445 /* Patchset J - Borrow Payback Enhancements - In actual organizations, BPV
4446    against payback transactions across the same cost group has to be collected
4447    against both the borrowing and lending project  Adding OR condition*/
4448                     IF ((p_std_cg_acct = 1 AND p_primary_cost_method = 1) OR
4449                         (p_primary_cost_method <> 1 and p_cost_group_id = p_transfer_cost_group_id))
4450                     THEN
4451 
4452                         SELECT
4453                                  decode(sign(-1 * p_primary_quantity),1,
4454                                         cceet.EXPENDITURE_TYPE_OUT,
4455                                          cceet.EXPENDITURE_TYPE_IN)
4456                         INTO l_exp_type
4457                         FROM cst_cost_elem_exp_types cceet
4458                         WHERE cceet.cost_element_id = c_rec6.cost_element_id;
4459 
4460                         pm_insert_pti_pvt
4461                                 (p_transaction_source         =>
4462                                              c_rec6.transaction_source,
4463                                 p_batch_name                 =>c_rec6.batch_name,
4464                                 p_expenditure_ending_date    =>
4465                                                      c_rec6.expenditure_ending_date,
4466                                 p_employee_number            =>c_rec6.employee_number,
4467                                 p_organization_name          =>
4468                                              c_rec6.organization_name,
4469                                 p_expenditure_item_date      =>
4470                                              c_rec6.expenditure_item_date,
4471                                 p_project_number             =>l_to_project_number,
4472                                 p_task_number                =>l_to_task_number,
4473                                 p_expenditure_type           =>l_exp_type,
4474                                 p_pa_quantity                =>-1 * c_rec6.quantity,
4475                                 p_raw_cost                   =>-1 * c_rec6.raw_cost,
4476                                 p_expenditure_comment        =>
4477                                              c_rec6.expenditure_comment,
4478                                 p_orig_transaction_reference =>to_char(p_transfer_transaction_id),
4479                                 p_raw_cost_rate              =>-1 * c_rec6.raw_cost_rate,
4480                                 p_unmatched_negative_txn_flag=>
4481                                              c_rec6.unmatched_negative_txn_flag,
4482                                 p_gl_date                    =>c_rec6.gl_date,
4483                                 p_org_id                     =>l_to_proj_org_id,
4484                                 p_burdened_cost              =>-1 * c_rec6.burdened_cost,
4485                                 p_burdened_cost_rate         =>
4486                                              -1 * c_rec6.burdened_cost_rate,
4487                                 p_system_linkage             =>c_rec6.system_linkage,
4488                                 p_transaction_status_code    =>
4489                                              c_rec6.transaction_status_code,
4490                                 p_denom_currency_code        =>
4491                                              c_rec6.denom_currency_code,
4492                                 p_transaction_id             =>p_transaction_id,
4493 
4494                                 p_transaction_action_id      =>p_transaction_action_id,
4495                                 p_transaction_source_type_id =>
4496                                              p_transaction_source_type_id,
4497                                 p_organization_id            =>p_organization_id,
4498                                 p_inventory_item_id          =>p_inventory_item_id,
4499                                   p_cost_element_id            =>c_rec6.cost_element_id,
4500                                 p_resource_id                =>NULL,
4501                                 p_source_flag                =>-1,
4502                                 p_variance_flag              =>1,
4503                                 p_primary_quantity           =>-1 * p_primary_quantity ,
4504                                 p_transfer_organization_id   =>
4505                                                     p_organization_id,
4506                                 p_fob_point                  =>l_fob_point,
4507                                 p_wip_entity_id              =>NULL,
4508                                 p_basis_resource             =>NULL,
4509 
4510                                 p_type_class                 =>p_type_class,
4511                                 p_project_id                 =>p_to_project_id,
4512                                 p_task_id                    =>p_to_task_id,
4513                                 p_transaction_date           =>p_transaction_date,
4514                                 p_cost_group_id              =>p_transfer_cost_group_id,
4515                                 p_transfer_cost_group_id     =>
4516                                              p_cost_group_id,
4517                                 p_transaction_source_id      =>
4518                                                     p_transaction_source_id,
4519                                 p_to_project_id              =>p_project_id,
4520                                 p_to_task_id                 =>p_task_id,
4521                                 p_source_project_id          =>p_source_project_id,
4522                                 p_source_task_id             =>p_source_task_id,
4523                                 p_transfer_transaction_id    =>
4524                                                     p_transaction_id,
4525                                 p_primary_cost_method        =>p_primary_cost_method,
4526                                 p_acct_period_id             =>p_acct_period_id,
4527                                 p_exp_org_id                 =>p_exp_org_id,
4528                                 p_distribution_account_id    =>
4529                                              p_distribution_account_id,
4530                                 p_proj_job_ind               =>p_proj_job_ind,
4531                                 p_first_matl_se_exp_type     =>
4532                                              p_first_matl_se_exp_type,
4533                                 p_inv_txn_source_literal     =>
4534                                              p_inv_txn_source_literal,
4535                                 p_cap_txn_source_literal     =>
4536                                              p_cap_txn_source_literal,
4537                                 p_inv_syslink_literal        =>p_inv_syslink_literal,
4538                                 p_bur_syslink_literal        =>p_bur_syslink_literal,
4539                                 p_wip_syslink_literal        =>p_wip_syslink_literal,
4540                                 p_user_def_exp_type          =>p_user_def_exp_type,
4541                                 p_flow_schedule              =>p_flow_schedule,
4542                                 p_si_asset_yes_no            =>p_si_asset_yes_no,
4543                                 p_transfer_si_asset_yes_no   =>
4544                                                     p_transfer_si_asset_yes_no,
4545 
4546                                 O_err_num                    =>l_err_num,
4547                                 O_err_code                   =>l_err_code,
4548                                 O_err_msg                    =>l_err_msg
4549                         );
4550                     END IF;
4551 
4552                     IF (l_err_num <> 0) THEN
4553 
4554                       RAISE CST_FAILED_INSERT_PTI;
4555 
4556                     END IF;
4557 
4558                   END LOOP;
4559 
4560                   /* Commenting this out -  borrow payback transactions need not
4561                   have borrow payback variance. */
4562 
4563                         /* IF l_rownum = 0 THEN
4564 
4565                     RAISE NO_ROWS_TO_INSERT;
4566 
4567                         END IF;*/
4568 
4569                 END IF; /*CC ALL BORROW PAYBACK TXN */
4570 
4571 
4572                 l_no_ppv := 1; --bug 3978501 setting the variable to 1 initially
4573 
4574                 -- PJMSTD_PPV
4575                 /* Bug # 2349027. For interorg transfers, collect PPV if project_id is the same
4576                    as to_project_id since the expenditure orgs are different. */
4577                 IF (((p_std_cg_acct = 1 AND p_primary_cost_method = 1) OR
4578                      (l_primary_cost_method_snd = 1)) AND
4579                       (
4580                         /* Transfer org does not have cost collection enabled */
4581                         /*(p_transaction_action_id in (3,12,21) and
4582                          p_to_project_id is not null  and
4583                          l_cost_collection_enabled_snd <> 1)
4584                          OR */
4585                          /* PO Receipt */
4586                          p_transaction_source_type_id = 1
4587                          OR
4588                          /* Interorg transfer between two different project/tasks. */
4589                          (p_transaction_action_id IN (12,21,3)
4590                          /* AND NOT( p_project_id = nvl(p_to_project_id,-9)
4591                                 and p_task_id = nvl(p_to_task_id,-9))*/
4592                          )
4593                         OR
4594                         /* Consigned ownership transfer transaction */
4595                         p_transaction_action_id = 6
4596                         ))
4597                 THEN
4598                   /* For direct interorg transfers from standard org to a standard org,
4599                      the PPV is always generated against the sending organization.
4600                      For direct interorg transfers from average org to a standard org,
4601                      the PPV is always generated against the receiving organization.
4602                   */
4603                   IF (
4604                         (p_primary_cost_method=1 AND l_primary_cost_method_snd = 1 AND
4605                          p_transaction_action_id=3 AND p_primary_quantity >0) OR
4606                         (p_primary_cost_method <> 1 AND l_primary_cost_method_snd = 1 AND
4607                          p_transaction_action_id  = 3 AND p_primary_quantity < 0)) THEN
4608                         ppv_txfr_flag := 1;
4609                   ELSE
4610                         ppv_txfr_flag := 0;
4611                   END IF;
4612 
4613                   l_rownum := 0;
4614 
4615                   if (l_debug = 'Y') then
4616                      FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Collecting purchase price variances ...');
4617                      FND_FILE.PUT_LINE(FND_FILE.LOG,'ppv_txfr_flag : '||ppv_txfr_flag||
4618                                                  'p_primary_cost_method : '||p_primary_cost_method||
4619                                                  'l_primary_cost_method_snd : '||l_primary_cost_method_snd||
4620                                                  ' p_organization_id : '||p_organization_id||
4621                                                  ' p_transaction_id : '||p_transaction_id||
4622                                                  ' p_transfer_organization_id : '||p_transfer_organization_id||
4623                                                  ' p_transfer_transaction_id : '||p_transfer_transaction_id||
4624                                                  ' p_primary_quantity : '||p_primary_quantity||
4625                                                  ' p_project_id : '||p_project_id||
4626                                                  ' p_to_project_id : '||p_to_project_id);
4627                 end if;
4628 
4629                   FOR c_rec7 IN c_sel_ppv LOOP
4630 
4631                     l_rownum := l_rownum + 1;
4632 
4633                     l_stmt_num := 210;
4634 
4635 
4636  /* changes to support the Blue print organizations */
4637 
4638  /* get the org ID on the transfer Txn if direct interorg*/
4639 
4640    If p_transaction_action_id = 3 then
4641 
4642                    select organization_id into l_org_id
4643                    from mtl_material_transactions
4644                    where transaction_id = c_rec7.orig_transaction_reference;
4645 
4646                  /* set the transaction source accordingly if the org is BP */
4647 
4648                     select NVL(pa_posting_flag,'N'),
4649                            NVL(pa_autoaccounting_flag,'N')
4650                     into l_blue_print_enabled_flag,
4651                          l_autoaccounting_flag
4652                     from pjm_org_parameters
4653                     where organization_id = l_org_id ;
4654 
4655           If l_blue_print_enabled_flag = 'Y' then
4656 
4657                If l_autoaccounting_flag = 'Y' then
4658 
4659                     /* BP and autoaccounting  */
4660                     Select pts1.transaction_source,
4661                            pts1.transaction_source,
4662                            pts1.transaction_source
4663                      into  l_transaction_source,
4664                            l_inv_txn_src_literal,
4665                            l_cap_inv_txn_src_literal
4666                      From  pa_transaction_sources pts1
4667                     Where  pts1.transaction_source = 'PJM_CSTBP_INV_NO_ACCOUNTS';
4668                    If (l_transaction_source is NULL) then
4669 
4670                       RAISE CST_FAILED_TXN_SRC;
4671 
4672                    end If;
4673                else
4674                     /* BP and no autoaccounting */
4675                     Select pts1.transaction_source,
4676                            pts1.transaction_source,
4677                            pts1.transaction_source
4678                      into  l_transaction_source,
4679                            l_inv_txn_src_literal,
4680                            l_cap_inv_txn_src_literal
4681                      From  pa_transaction_sources pts1
4682                     Where  pts1.transaction_source = 'PJM_CSTBP_INV_ACCOUNTS';
4683 
4684                        If (l_transaction_source is NULL ) then
4685 
4686                         RAISE CST_FAILED_TXN_SRC;
4687 
4688                        end If;
4689                end if; /* end of check for auto accounting */
4690 
4691            ELSE /* non BP org */
4692 
4693                SELECT   pts1.transaction_source,
4694                         pts2.transaction_source
4695                INTO     l_inv_txn_src_literal,
4696                         l_cap_inv_txn_src_literal
4697                FROM     pa_transaction_sources pts1,
4698                         pa_transaction_sources pts2
4699                WHERE  pts1.transaction_source = 'Inventory'
4700                AND  pts2.transaction_source = 'Inventory Misc';
4701 
4702                SELECT  decode(p_type_class,1,
4703                         l_cap_inv_txn_src_literal,
4704                         l_inv_txn_src_literal)
4705                INTO   l_transaction_source
4706                from dual;
4707 
4708 
4709           END IF; /* check for BP org */
4710 
4711     ELSE /* transaction action iD <> 3 */
4712 
4713      l_transaction_source := c_rec7.transaction_source ;
4714      l_inv_txn_src_literal := p_inv_txn_source_literal ;
4715      l_cap_inv_txn_src_literal := p_cap_txn_source_literal;
4716 
4717     END IF ; /* check for direct inter org txns */
4718 
4719 
4720                     pm_insert_pti_pvt
4721                         (p_transaction_source         =>
4722                                              l_transaction_source,
4723                          p_batch_name                 =>c_rec7.batch_name,
4724                          p_expenditure_ending_date    =>
4725                                              c_rec7.expenditure_ending_date,
4726                          p_employee_number            =>c_rec7.employee_number,
4727                          p_organization_name          =>
4728                                              c_rec7.organization_name,
4729                          p_expenditure_item_date      =>
4730                                              c_rec7.expenditure_item_date,
4731                          p_project_number             =>c_rec7.project_number,
4732                          p_task_number                =>c_rec7.task_number,
4733                          p_expenditure_type           =>c_rec7.expenditure_type,
4734                          p_pa_quantity                =>c_rec7.quantity,
4735                          p_raw_cost                   =>c_rec7.raw_cost,
4736                          p_expenditure_comment        =>
4737                                              c_rec7.expenditure_comment,
4738                          p_orig_transaction_reference =>
4739                                              c_rec7.orig_transaction_reference,
4740                          p_raw_cost_rate              =>c_rec7.raw_cost_rate,
4741                          p_unmatched_negative_txn_flag=>
4742                                              c_rec7.unmatched_negative_txn_flag,
4743                          p_gl_date                    =>c_rec7.gl_date,
4744                          p_org_id                     =>c_rec7.org_id,
4745                          p_burdened_cost              =>c_rec7.burdened_cost,
4746                          p_burdened_cost_rate         =>
4747                                              c_rec7.burdened_cost_rate,
4748                          p_system_linkage             =>c_rec7.system_linkage,
4749                          p_transaction_status_code    =>
4750                                              c_rec7.transaction_status_code,
4751                          p_denom_currency_code        =>
4752                                              c_rec7.denom_currency_code,
4753                          p_transaction_id             =>p_transaction_id,
4754 
4755                          p_transaction_action_id      =>p_transaction_action_id,
4756                          p_transaction_source_type_id =>
4757                                              p_transaction_source_type_id,
4758                          p_organization_id            =>p_organization_id,
4759                          p_inventory_item_id          =>p_inventory_item_id,
4760                          p_cost_element_id            =>c_rec7.cost_element_id,
4761                          p_resource_id                =>NULL,
4762                          p_source_flag                =>-1,
4763                          p_variance_flag              =>1,
4764                          p_primary_quantity           =>p_primary_quantity ,
4765                          p_transfer_organization_id   =>
4766                                              p_transfer_organization_id,
4767                          p_fob_point                  =>l_fob_point,
4768                          p_wip_entity_id              =>NULL,
4769                          p_basis_resource             =>NULL,
4770 
4771                          p_type_class                 =>p_type_class,
4772                          p_project_id                 =>p_project_id,
4773                          p_task_id                    =>p_task_id,
4774                          p_transaction_date           =>p_transaction_date,
4775                          p_cost_group_id              =>p_cost_group_id,
4776                          p_transfer_cost_group_id     =>
4777                                              p_transfer_cost_group_id,
4778                          p_transaction_source_id      =>
4779                                              p_transaction_source_id,
4780                          p_to_project_id              =>p_to_project_id,
4781                          p_to_task_id                 =>p_to_task_id,
4782                          p_source_project_id          =>p_source_project_id,
4783                          p_source_task_id             =>p_source_task_id,
4784                          p_transfer_transaction_id    =>
4785                                              p_transfer_transaction_id,
4786                          p_primary_cost_method        =>p_primary_cost_method,
4787                          p_acct_period_id             =>p_acct_period_id,
4788                          p_exp_org_id                 =>p_exp_org_id,
4789                          p_distribution_account_id    =>
4790                                              p_distribution_account_id,
4791                          p_proj_job_ind               =>p_proj_job_ind,
4792                          p_first_matl_se_exp_type     =>
4793                                              p_first_matl_se_exp_type,
4794                          p_inv_txn_source_literal     =>
4795                                              l_inv_txn_src_literal,
4796                          p_cap_txn_source_literal     =>
4797                                              l_cap_inv_txn_src_literal,
4798                          p_inv_syslink_literal        =>p_inv_syslink_literal,
4799                          p_bur_syslink_literal        =>p_bur_syslink_literal,
4800                          p_wip_syslink_literal        =>p_wip_syslink_literal,
4801                          p_user_def_exp_type          =>p_user_def_exp_type,
4802                          p_flow_schedule              =>p_flow_schedule,
4803                          p_si_asset_yes_no            =>p_si_asset_yes_no,
4804                          p_transfer_si_asset_yes_no   =>
4805                                              p_transfer_si_asset_yes_no,
4806 
4807                          O_err_num                    =>l_err_num,
4808                          O_err_code                   =>l_err_code,
4809                          O_err_msg                    =>l_err_msg
4810                      );
4811 
4812                     IF (l_err_num <> 0) THEN
4813 
4814                       RAISE CST_FAILED_INSERT_PTI;
4815 
4816                     END IF;
4817 
4818                   END LOOP; --c_rec7
4819 
4820 
4821                 /* bug 3978501. check to see if r have been inserted and update l_no_ppv */
4822 
4823                  if l_rownum <> 0 then
4824 
4825                     l_no_ppv := 0;
4826 
4827                  end If;
4828 
4829 
4830                 END IF; -- Check for PPV of PO or IO Txn
4831 
4832 
4833             /* bug 3978501 check to see the 3 flags and decide whether to raise the no rows exception or not */
4834 
4835              IF l_no_ppv = 1 then
4836                IF l_no_row_c_sel_prj = 1 or l_no_row_c_sel_toprj = 1 then
4837                     RAISE NO_ROWS_TO_INSERT;
4838                END IF;
4839              END IF;
4840 
4841 
4842         END IF;        /* END: CC ALL TXNS OF AN AVG+PJMSTD COSTING ORG */
4843 
4844         EXCEPTION
4845 
4846                 WHEN CST_FAILED_GET_EXPENDDATE THEN
4847 
4848                         O_err_num := 20001;
4849 
4850                         fnd_message.set_name('BOM','CST_FAILED_GET_EXPENDDATE');
4851                         l_err_msg := fnd_message.get ;
4852                         O_err_msg := substr(l_err_msg,1,240) ;
4853 
4854                         O_err_code := 'CSTPPCCB.pm_process_txn_mmt('
4855                                         || to_char(l_stmt_num)
4856                                         || '): ';
4857 
4858                 WHEN NO_ROWS_TO_INSERT THEN
4859 
4860                         O_err_num := 20002;
4861 
4862                         fnd_message.set_name('BOM',
4863                                                 'CST_NO_PROJ_COSTS_REPORTED');
4864                         l_err_msg := fnd_message.get ;
4865                         O_err_msg := substr(l_err_msg,1,240) ;
4866 
4867                         O_err_code := 'CSTPPCCB.pm_process_txn_mmt('
4868                                         || to_char(l_stmt_num)
4869                                         || '): ';
4870 
4871                 WHEN ROW_NOT_COSTED THEN
4872 
4873                         O_err_num := 30000;
4874 
4875                         fnd_message.set_name('BOM',
4876                                                 'CST_TXN_NOT_COSTED');
4877                         l_err_msg := fnd_message.get ;
4878                         O_err_msg := substr(l_err_msg,1,240) ;
4879 
4880                         O_err_code := 'CSTPPCCB.pm_process_txn_mmt('
4881                                         || to_char(l_stmt_num)
4882                                         || '): ';
4883 
4884 
4885                 WHEN CST_FAILED_PROJTSK_VALID THEN
4886 
4887                         O_err_num := 20003;
4888 
4889  /*                     fnd_message.set_name('BOM','CST_FAILED_PROJTSK_VALID');
4890                         l_err_msg := fnd_message.get ;
4891                         O_err_msg := substr(l_err_msg,1,240) ;
4892 */
4893                         O_err_msg:='Project/Task Invalid for Cost Collection.';
4894                         O_err_code  := SUBSTR('CSTPPCCB.pm_process_txn_mmt('
4895                                 ||to_char(l_stmt_num)
4896                                 ||'): '
4897                                 ||l_err_msg,1,240);
4898 
4899                 -- bug 888190, new exception is added
4900                 -- to handle expenditure type not found
4901                 WHEN CST_FAILED_GET_EXP_TYPE THEN
4902                         O_err_num := 20004;
4903                         O_err_code := NULL;
4904                         O_err_msg := SUBSTR('CSTPPCCB.pm_process_txn_mmt('
4905                                    ||to_char(l_stmt_num)
4906                                    ||'):  '
4907                                    ||'Failed to get expenditure type.',1,240);
4908 
4909                 WHEN CST_FAILED_INSERT_PTI THEN
4910                         O_err_num := 20005;
4911                         O_err_code := SUBSTR('CSTPPCCB.pm_process_txn_mmt('
4912                                    ||to_char(l_stmt_num)
4913                                    ||'):  '
4914                                    ||l_err_code, 1,240);
4915                         O_err_msg := SUBSTR(l_err_msg,1,240);
4916 
4917                 WHEN CST_FAILED_STD_CG_FLAG THEN
4918 
4919                         O_err_num := 20006;
4920                         O_err_code := SUBSTR('CSTPPCCB.pm_process_txn_mmt('
4921                                    ||to_char(l_stmt_num)
4922                                    ||'):  ',1,240);
4923                         O_err_msg :=
4924                                   'Failed CST_UTILITY_PUB.GET_STD_CG_ACCT_FLAG';
4925 
4926                WHEN CST_FAILED_TXN_SRC THEN
4927 
4928                         O_err_num := 20007;
4929                         O_err_code := SUBSTR('CSTPPCCB.pm_process_txn_mmt('
4930                                     || to_char(l_stmt_num)
4931                                     ||'): ',1,240);
4932                         O_err_msg := 'failed to get TXN source' ;
4933 
4934 
4935 
4936                 WHEN OTHERS THEN
4937 
4938                         O_err_num := SQLCODE;
4939                         O_err_code := NULL;
4940                         O_err_msg := SUBSTR('CSTPPCCB.pm_process_txn_mmt('
4941                                         || to_char(l_stmt_num)
4942                                         || '): '
4943                                         ||SQLERRM,1,240);
4944 
4945 END pm_process_txn_mmt;
4946 
4947  /*----------------------------------------------------------------------------*
4948  | PRIVATE PROCEDURE                                                          |
4949  |    pm_check_error_mmt                                                      |
4950  |                                                                            |
4951  | DESCRIPTION                                                                |
4952  |    The procedure validates the transaction to cost collect. The validations|
4953  |    it performs are                                                         |
4954  |    1. Both the project and task columns for the txn are NOT NULL           |
4955  |    2. If the txn is of PO family,the first material SE Exp_type is NOT NULL|
4956  |    3. If the txn is Assy Rtn or Completion or PO Family, the MO SE should  |
4957  |       all be having their expenditure_type as NOT NULL                     |
4958  |                                                                            |
4959  |                                                                            |
4960  |                                                                            |
4961  | PARAMETERS                                                                 |
4962  |                                                                            |
4963  |                        p_transaction_id,                                   |
4964  |                        p_organization_id,                                  |
4965  |                        p_inventory_item_id,                                |
4966  |                        p_avg_rates_cost_type_id,                           |
4967  |                        p_transaction_action_id,                            |
4968  |                        p_transaction_source_type_id,                       |
4969  |                        p_type_class,                                       |
4970  |                        p_project_id,                                       |
4971  |                        p_task_id                                           |
4972  |                        p_to_project_id,                                    |
4973  |                        p_to_task_id                                        |
4974  |                        p_source_project_id,                                |
4975  |                        p_source_task_id                                    |
4976  |                        p_transaction_source_id                             |
4977  |                        p_proj_job_ind                                   |
4978  |                        p_process_yn                                        |
4979  |                        p_first_matl_se_exp_type                            |
4980  |                        p_user_id,                                          |
4981  |                        p_login_id,                                         |
4982  |                        p_req_id,                                           |
4983  |                        p_prg_appl_id,                                      |
4984  |                        p_prg_id,                                           |
4985  |                        O_err_num,                                              |
4986  |                        O_err_code,                                              |
4987  |                        O_err_msg,                                              |
4988  |                        p_flow_schedule                                     |
4989  |                        p_cost_group_id                                     |
4990  |                                                                            |
4991  | CALLED FROM                                                                |
4992  |    pm_cc_worker_mmt()                                                      |
4993  |                                                                            |
4994  | HISTORY                                                                    |
4995  |    07-SEP-96  Bhaskar Dasari Created.                                      |
4996  |                                                                            |
4997  |    20-AUG-97  Hemant Gosain Modified.                                      |
4998  |               Added CFM Support by passing parameter p_flow_schedule.      |
4999  *----------------------------------------------------------------------------*/
5000 
5001   PROCEDURE  pm_check_error_mmt (
5002                                  p_transaction_id               NUMBER,
5003                                  p_organization_id                NUMBER,
5004                                  p_cost_method                        NUMBER,
5005                                  p_inventory_item_id                NUMBER,
5006                                  p_avg_rates_cost_type_id        NUMBER,
5007                                  p_transaction_action_id        NUMBER,
5008                                  p_transaction_source_type_id        NUMBER,
5009                                  p_type_class                        NUMBER,
5010                                  p_project_id                        NUMBER,
5011                                  p_task_id                        NUMBER,
5012                                  p_to_project_id                NUMBER,
5013                                  p_to_task_id                        NUMBER,
5014                                  p_source_project_id                NUMBER,
5015                                  p_source_task_id                NUMBER,
5016                                  p_transaction_source_id        NUMBER,
5017                                  p_proj_job_ind           OUT         NOCOPY  NUMBER,
5018                                  p_process_yn                   OUT         NOCOPY  NUMBER,
5019                                  p_first_matl_se_exp_type  OUT         NOCOPY  VARCHAR2,
5020                                  p_user_id                        NUMBER,
5021                                  p_login_id                        NUMBER,
5022                                  p_req_id                        NUMBER,
5023                                  p_prg_appl_id                         NUMBER,
5024                                  p_prg_id                         NUMBER,
5025                                  O_err_num                   OUT        NOCOPY  NUMBER,
5026                                  O_err_code                   OUT        NOCOPY  VARCHAR2,
5027                                  O_err_msg                   OUT        NOCOPY  VARCHAR2,
5028                                  p_flow_schedule                VARCHAR2,
5029                                  p_cost_group_id                NUMBER)--PJMSTD
5030   IS
5031 
5032   l_err_num             NUMBER;
5033   l_err_code            VARCHAR2(240);
5034   l_err_msg             VARCHAR2(240);
5035   l_err_msg_temp        VARCHAR2(240);
5036   l_stmt_num            NUMBER;
5037   l_proj_job_ind        NUMBER;
5038 
5039   l_exp_type            VARCHAR2(30);
5040   l_error_code          VARCHAR2(240);
5041   l_error_explanation   VARCHAR2(240);
5042 
5043   PROCESS_ERROR         EXCEPTION;
5044   SE_EXP_TYPE_NULL      EXCEPTION;
5045   NO_FMSE_DEFINED       EXCEPTION;
5046   l_debug               VARCHAR2(80);
5047 
5048   BEGIN
5049         -- initialize local variables
5050         l_err_num := 0;
5051         l_err_code := '';
5052         l_err_msg := '';
5053         l_err_msg_temp := '';
5054         l_stmt_num := 1;
5055 
5056         l_debug := FND_PROFILE.VALUE('MRP_DEBUG');
5057         if (l_debug = 'Y') then
5058             FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Reached#: pm_check_error_mmt');
5059         end if;
5060 
5061         IF(p_transaction_source_type_id = 5 AND p_source_project_id IS NOT NULL) THEN
5062            l_proj_job_ind := 1;
5063         ELSE
5064            l_proj_job_ind := 0;
5065         END IF;
5066 
5067         p_proj_job_ind := l_proj_job_ind;
5068 
5069         p_process_yn := 1; /* 1 Implies Process the Txn */
5070         --
5071         -- GENERIC CHECKS
5072         --
5073         -- TXNS demanding knowledge of both project and task ie. NOT NULL
5074 
5075         IF     p_project_id is not null AND
5076                p_task_id is NULL THEN
5077 
5078                 l_stmt_num := 20;
5079                 fnd_message.set_name('BOM','CST_NO_PROJ_OR_TASK');
5080                 l_err_msg := fnd_message.get ;
5081                 l_err_code := 'CSTPPCCB.pm_check_error_mmt('
5082                                 || to_char(l_stmt_num)
5083                                 || '): ';
5084 
5085                 l_error_explanation := substr(l_err_msg,1,240) ;
5086                 l_error_code := l_err_code;
5087 
5088 
5089                 pm_mark_error_mmt ( p_transaction_id,
5090                                     l_error_code,
5091                                     l_error_explanation,
5092                                     p_user_id,
5093                                     p_login_id,
5094                                     p_req_id,
5095                                     p_prg_appl_id,
5096                                     p_prg_id,
5097                                     l_err_num,
5098                                     l_err_code,
5099                                     l_err_msg);
5100 
5101                 IF (l_err_num <> 0) THEN
5102                         -- Error occured
5103                         raise PROCESS_ERROR;
5104                 ELSE
5105                         p_process_yn := 2;
5106                         l_err_num := 20003;
5107                         l_err_code   := l_error_code;
5108                         l_err_msg    := l_error_explanation;
5109                         raise PROCESS_ERROR;
5110                 END IF;
5111         END IF;
5112 
5113 
5114         IF     p_to_project_id is not null AND
5115                p_to_task_id is NULL   THEN
5116 
5117                 l_stmt_num := 30;
5118                 fnd_message.set_name('BOM','CST_NO_TRF_PROJ_OR_TASK');
5119                 l_err_msg := fnd_message.get ;
5120                 l_err_code := 'CSTPPCCB.pm_check_error_mmt('
5121                                 || to_char(l_stmt_num)
5122                                 || '): ';
5123 
5124                 l_error_explanation := substr(l_err_msg,1,240) ;
5125                 l_error_code := l_err_code;
5126 
5127                 pm_mark_error_mmt ( p_transaction_id,
5128                                     l_error_code,
5129                                     l_error_explanation,
5130                                     p_user_id,
5131                                     p_login_id,
5132                                     p_req_id,
5133                                     p_prg_appl_id,
5134                                     p_prg_id,
5135                                     l_err_num,
5136                                     l_err_code,
5137                                     l_err_msg);
5138 
5139                 IF (l_err_num <> 0) THEN
5140                            -- Error occured
5141                            raise PROCESS_ERROR;
5142                 ELSE
5143                         p_process_yn := 2;
5144                         l_err_num := 20004;
5145                         l_err_code   := l_error_code;
5146                         l_err_msg    := l_error_explanation;
5147                         raise PROCESS_ERROR;
5148                 END IF;
5149         END IF;
5150 
5151         IF     ( p_type_class = 1 OR l_proj_job_ind = 1 )
5152            AND ( p_source_project_id is null OR p_source_task_id is NULL ) THEN
5153 
5154                 l_stmt_num := 40;
5155                 fnd_message.set_name('BOM','CST_NO_SOURCE_PROJ_OR_TASK');
5156                 l_err_msg := fnd_message.get ;
5157                 l_err_code := 'CSTPPCCB.pm_check_error_mmt('
5158                                 || to_char(l_stmt_num)
5159                                 || '): ';
5160 
5161                 l_error_explanation := substr(l_err_msg,1,240) ;
5162                 l_error_code := l_err_code;
5163 
5164                 pm_mark_error_mmt ( p_transaction_id,
5165                                     l_error_code,
5166                                     l_error_explanation,
5167                                     p_user_id,
5168                                     p_login_id,
5169                                     p_req_id,
5170                                     p_prg_appl_id,
5171                                     p_prg_id,
5172                                     l_err_num,
5173                                     l_err_code,
5174                                     l_err_msg);
5175 
5176                 IF (l_err_num <> 0) THEN
5177                            -- Error occured
5178                            raise PROCESS_ERROR;
5179                 ELSE
5180                         p_process_yn := 2;
5181                         l_err_num := 20005;
5182                         l_err_code   := l_error_code;
5183                         l_err_msg    := l_error_explanation;
5184                         raise PROCESS_ERROR;
5185                 END IF;
5186         END IF;
5187 
5188         -- PJMSTD
5189         IF (p_cost_group_id IS NULL) THEN
5190 
5191                 l_stmt_num := 45;
5192                 fnd_message.set_name('BOM','CST_PAC_CG_INVALID');
5193                 l_err_msg := fnd_message.get ;
5194                 l_err_code := 'CSTPPCCB.pm_check_error_mmt('
5195                                 || to_char(l_stmt_num)
5196                                 || '): ';
5197 
5198                 l_error_explanation := substr(l_err_msg,1,240) ;
5199                 l_error_code := l_err_code;
5200 
5201                 pm_mark_error_mmt ( p_transaction_id,
5202                                     l_error_code,
5203                                     l_error_explanation,
5204                                     p_user_id,
5205                                     p_login_id,
5206                                     p_req_id,
5207                                     p_prg_appl_id,
5208                                     p_prg_id,
5209                                     l_err_num,
5210                                     l_err_code,
5211                                     l_err_msg);
5212 
5213                 IF (l_err_num <> 0) THEN
5214                            -- Error occured
5215                            raise PROCESS_ERROR;
5216                 ELSE
5217                         p_process_yn := 2;
5218                         l_err_num := 20008;
5219                         l_err_code   := l_error_code;
5220                         l_err_msg    := l_error_explanation;
5221                         raise PROCESS_ERROR;
5222                 END IF;
5223 
5224         END IF; -- check for NULL CG
5225 
5226         --
5227         -- TRANSACTION SPECIFIC CHECKS ----
5228         --
5229         -- TXN requires using the first matl SE logic and the routine returns
5230         -- no valid expenditure type
5231         --
5232         IF (p_transaction_source_type_id = 1
5233             OR p_transaction_action_id = 6)
5234                                                  THEN /*PO Famly FMSE logic,
5235                                                   consigned ownership transfer transaction
5236                                                   --START*/
5237 
5238                 l_exp_type := NULL;
5239                 l_stmt_num := 50;
5240 
5241                 BEGIN
5242 
5243                     SELECT decode(br.expenditure_type,NULL,
5244                                  decode(br1.expenditure_type,NULL,'NO VALUE',
5245                                                 br1.expenditure_type),
5246                                      br.expenditure_type)
5247                       INTO  l_exp_type
5248                       FROM  mtl_parameters mp,
5249                             cst_item_cost_details cicd,
5250                             bom_resources br,
5251                             bom_resources br1
5252                      WHERE  mp.organization_id = p_organization_id
5253                        AND  mp.cost_organization_id = cicd.organization_id (+)
5254                        AND  cicd.inventory_item_id (+) = p_inventory_item_id
5255                        AND  cicd.cost_type_id (+) = decode(p_cost_method, 1, 1,
5256                                                         p_avg_rates_cost_type_id)
5257                        AND  cicd.cost_element_id (+) = 1
5258                        AND  cicd.organization_id = br.organization_id (+)
5259                        AND  cicd.resource_id = br.resource_id (+)
5260                        AND  mp.cost_organization_id = br1.organization_id (+)
5261                        AND  mp.default_material_cost_id = br1.resource_id (+)
5262                        AND  rownum=1;
5263 
5264                 EXCEPTION
5265                         when NO_DATA_FOUND then
5266                                 l_exp_type := 'NO VALUE';
5267                 END;
5268 
5269                 IF l_exp_type = 'NO VALUE' THEN
5270 
5271                         RAISE NO_FMSE_DEFINED;
5272                 ELSE
5273                         p_first_matl_se_exp_type := l_exp_type;
5274 
5275                 END IF;
5276 
5277         END IF; /* PO Family FMSE logic -- END */
5278 
5279         -- TXN demands cost collecting the mo sub-elementally and the ET for
5280         -- the SE was found to be NULL
5281         -- TXNS ARE: PO receipt,adjustment,RTV,Assy completion and Return
5282         -- Consigned ownership transfer transactions
5283 
5284         IF         (p_transaction_action_id in (31,32)
5285             OR         p_transaction_source_type_id = 1
5286             OR  p_transaction_action_id = 6)
5287                                                              THEN /*Check MO SE ET START*/
5288 
5289 
5290                 l_stmt_num := 60;
5291                 l_exp_type := NULL;
5292 
5293                 BEGIN
5294                         SELECT  'NO VALUE'
5295                           INTO  l_exp_type
5296                           FROM  mtl_actual_cost_subelement macs,
5297                                 bom_resources br
5298                          WHERE  macs.transaction_id   = p_transaction_id
5299                            AND  macs.organization_id  = p_organization_id
5300                              AND  macs.cost_element_id = 2
5301                              AND  macs.level_type = 1
5302                            AND  br.RESOURCE_ID = macs.RESOURCE_ID
5303                            AND  br.ORGANIZATION_ID  = macs.ORGANIZATION_ID
5304                            AND  br.expenditure_type IS NULL;
5305                         EXCEPTION
5306                                 when NO_DATA_FOUND then
5307                                         l_exp_type := NULL;
5308                 END;
5309 
5310                 IF l_exp_type = 'NO VALUE' THEN
5311                         RAISE SE_EXP_TYPE_NULL;
5312                 END IF;
5313 
5314         END IF;  /*Check MO SE ET END*/
5315 
5316         EXCEPTION
5317                 WHEN SE_EXP_TYPE_NULL THEN
5318 
5319                         fnd_message.set_name('BOM','CST_SE_ET_IS_NULL');
5320                         l_err_msg := fnd_message.get ;
5321                         l_err_code := 'CSTPPCCB.pm_check_error_mmt('
5322                                         || to_char(l_stmt_num)
5323                                         || '): ';
5324 
5325                         l_error_explanation := substr(l_err_msg,1,240) ;
5326                         l_error_code := l_err_code;
5327 
5328                         pm_mark_error_mmt ( p_transaction_id,
5329                                                 l_error_code,
5330                                                 l_error_explanation,
5331                                                 p_user_id,
5332                                                 p_login_id,
5333                                                 p_req_id,
5334                                                 p_prg_appl_id,
5335                                                 p_prg_id,
5336                                                 l_err_num,
5337                                                 l_err_code,
5338                                                 l_err_msg);
5339 
5340                         IF (l_err_num <> 0) THEN
5341                                 O_err_num := l_err_num;
5342                                 O_err_code := l_err_code;
5343                                 O_err_msg := l_err_msg;
5344                         ELSE
5345                                 p_process_yn := 2;
5346                                 O_err_num := 20006;
5347                                 O_err_code   := l_error_code;
5348                                 O_err_msg    := l_error_explanation;
5349                         END IF;
5350 
5351                 WHEN NO_FMSE_DEFINED THEN
5352 
5353                         fnd_message.set_name('BOM','CST_NO_FMSE_DEFINED');
5354                         l_err_msg := fnd_message.get ;
5355                         l_err_code := 'CSTPPCCB.pm_check_error_mmt('
5356                                         || to_char(l_stmt_num)
5357                                         || '): ';
5358 
5359                         l_error_explanation := substr(l_err_msg,1,240) ;
5360                         l_error_code := l_err_code;
5361 
5362                         pm_mark_error_mmt ( p_transaction_id,
5363                                             l_error_code,
5364                                             l_error_explanation,
5365                                             p_user_id,
5366                                             p_login_id,
5367                                             p_req_id,
5368                                             p_prg_appl_id,
5369                                             p_prg_id,
5370                                             l_err_num,
5371                                             l_err_code,
5372                                             l_err_msg);
5373                         p_process_yn := 2;
5374 
5375                         IF (l_err_num <> 0) THEN
5376                                 O_err_num := l_err_num;
5377                                 O_err_code := l_err_code;
5378                                 O_err_msg := l_err_msg;
5379                         ELSE
5380                                 p_process_yn := 2;
5381                                 O_err_num := 20007;
5382                                 O_err_code   := l_error_code;
5383                                 O_err_msg    := l_error_explanation;
5384                         END IF;
5385 
5386                 WHEN PROCESS_ERROR THEN
5387                         O_err_num := l_err_num;
5388                         O_err_code := l_err_code;
5389                         O_err_msg := l_err_msg;
5390 
5391                 WHEN OTHERS THEN
5392                         O_err_num := SQLCODE;
5393                         O_err_code := NULL;
5394                         l_err_msg_temp := 'CSTPPCCB.pm_check_error_mmt('
5395                                 || to_char(l_stmt_num)
5396                                 || '): '
5397                                 || substr(SQLERRM,1,150);
5398 
5399                         l_err_num := 0;
5400 
5401                         l_error_explanation := substr(l_err_msg,1,240) ;
5402                         l_error_code := l_err_code;
5403 
5404                         pm_mark_error_mmt(p_transaction_id,
5405                                   l_error_code,
5406                                   l_error_explanation,
5407                                   p_user_id,
5408                                   p_login_id,
5409                                   p_req_id,
5410                                   p_prg_appl_id,
5411                                   p_prg_id,
5412                                   l_err_num,
5413                                   l_err_code,
5414                                   l_err_msg);
5415                         IF (l_err_num <> 0) THEN
5416                                 O_err_msg := SUBSTR(l_err_msg_temp
5417                                                 ||' * '
5418                                                 ||l_err_msg
5419                                                 ||' * '
5420                                                 ||l_err_code
5421                                                 ||' * '
5422                                                 ||'TXN NOT MARKED IN MMT!'
5423                                                 ,1,240);
5424                         ELSE
5425                                 O_err_msg := SUBSTR(l_err_msg_temp
5426                                                 ||' * '
5427                                                 ||'TXN MARKED IN MMT.'
5428                                                 ,1,240);
5429 
5430                         END IF;
5431 END pm_check_error_mmt;
5432 
5433  /*----------------------------------------------------------------------------*
5434  | PRIVATE PROCEDURE                                                          |
5435  |    pm_mark_error_mmt                                                       |
5436  |                                                                            |
5437  | DESCRIPTION                                                                |
5438  |                                                                            |
5439  |                                                                            |
5440  | PARAMETERS                                                                 |
5441  |                                                                            |
5442  |                        p_transaction_id,                                   |
5443  |                        p_error_code,                                       |
5444  |                        p_error_explanation,                                |
5445  |                        p_user_id,                                          |
5446  |                        p_login_id,                                         |
5447  |                        p_req_id,                                           |
5448  |                        p_prg_appl_id,                                      |
5449  |                        p_prg_id,                                           |
5450  |                        O_err_num,                                              |
5451  |                        O_err_code,                                              |
5452  |                        O_err_msg                                              |
5453  |                                                                            |
5454  | CALLED FROM                                                                |
5455  |    pm_check_error_mmt()                                                    |
5456  |                                                                            |
5457  | HISTORY                                                                    |
5458  |    07-SEP-96  Bhaskar Dasari Created.                                      |
5459  *----------------------------------------------------------------------------*/
5460 
5461   PROCEDURE  pm_mark_error_mmt (
5462                                  p_transaction_id               NUMBER,
5463                                  p_error_code                        VARCHAR2,
5464                                  p_error_explanation                VARCHAR2,
5465                                  p_user_id                        NUMBER,
5466                                  p_login_id                        NUMBER,
5467                                  p_req_id                        NUMBER,
5468                                  p_prg_appl_id                         NUMBER,
5469                                  p_prg_id                         NUMBER,
5470                                  O_err_num                OUT        NOCOPY NUMBER,
5471                                  O_err_code                OUT        NOCOPY VARCHAR2,
5472                                  O_err_msg                OUT        NOCOPY VARCHAR2 )
5473 
5474   IS
5475 
5476   l_err_num             NUMBER;
5477   l_err_code            VARCHAR2(240);
5478   l_err_msg             VARCHAR2(240);
5479   l_stmt_num            NUMBER;
5480   l_debug               VARCHAR2(80);
5481 
5482   BEGIN
5483         -- initialize local variables
5484         l_err_num := 0;
5485         l_err_code := '';
5486         l_err_msg := '';
5487 
5488 
5489         l_debug := FND_PROFILE.VALUE('MRP_DEBUG');
5490         if (l_debug = 'Y') then
5491             FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Reached#: pm_mark_error_mmt');
5492         end if;
5493 
5494         l_stmt_num := 10;
5495 
5496         /* update the errorcode and error_explanation fields inspite of the
5497            maintenance of the log file */
5498 
5499                 UPDATE         mtl_material_transactions mmt
5500                       SET         mmt.pm_cost_collected      = 'E',
5501                         mmt.error_code             = p_error_code,
5502                         mmt.error_explanation      = p_error_explanation,
5503                         mmt.last_update_date       = sysdate,
5504                         mmt.last_updated_by        = p_user_id,
5505                         mmt.last_update_login      = p_login_id,
5506                         mmt.request_id             = p_req_id,
5507                         mmt.program_application_id = p_prg_appl_id,
5508                         mmt.program_id             = p_prg_id,
5509                         mmt.program_update_date    = sysdate
5510                   WHERE mmt.transaction_id = p_transaction_id ;
5511         EXCEPTION
5512                 WHEN OTHERS THEN
5513                         O_err_num := SQLCODE;
5514                         O_err_code := NULL;
5515                         O_err_msg := 'CSTPPCCB.pm_mark_error_mmt('
5516                                 || to_char(l_stmt_num)
5517                                 || '): '
5518                                 || substr(SQLERRM,1,200);
5519   END pm_mark_error_mmt;
5520 
5521  /*----------------------------------------------------------------------------*
5522  | PRIVATE PROCEDURE                                                          |
5523  |    pm_process_txn_wt                                                       |
5524  |                                                                            |
5525  | DESCRIPTION                                                                |
5526  |                                                                            |
5527  |                                                                            |
5528  | PARAMETERS                                                                 |
5529  |                                                                            |
5530  |                        p_Group_Id,
5531                           p_business_group_name,                                     |
5532  |                        p_transaction_id,                                   |
5533  |                        p_organization_id,                                  |
5534  |                        p_employee_number,                                  |
5535  |                        p_department_id,                                    |
5536  |                        p_project_id,                                       |
5537  |                        p_task_id,                                          |
5538  |                        p_transaction_date,                                 |
5539  |                        p_base_transaction_value,                           |
5540  |                        p_primary_quantity,                                 |
5541  |                        p_acct_period_id,                                   |
5542  |                        p_expenditure_type,                                 |
5543  |                        p_resource_description,                             |
5544  |                        p_wt_transaction_type,                              |
5545  |                        p_cost_element_id                                      |
5546  |                        p_exp_org_name,                                     |
5547  |                        p_wip_txn_source_literal,                           |
5548  |                        p_wip_syslink_literal,                              |
5549  |                        p_bur_syslink_literal,                              |
5550  |                        O_err_num,                                              |
5551  |                        O_err_code,                                         |
5552  |                        O_err_msg,                                          |
5553  |                        p_reference_account,                                |
5554  |                        p_cr_account,                                       |
5555  |                        p_wip_entity_id,                                    |
5556  |                        p_resource_id,                                      |
5557  |                        p_basis_resource_id,                                |
5558  |                        p_denom_currency_code                               |
5559  |                                                                            |
5560  | CALLED FROM                                                                |
5561  |    pm_cc_worker_wt()                                                       |
5562  |                                                                            |
5563  | HISTORY                                                                    |
5564  |    07-SEP-96  Bhaskar Dasari Created.                                      |
5565  |                                                                            |
5566  |    30-JUL-97  Hemant Gosain Modified.                                      |
5567  |               Enhanced to pass accounting information parameters as support|
5568  |               for capitalization of projects.                              |
5569  *----------------------------------------------------------------------------*/
5570 
5571   PROCEDURE  pm_process_txn_wt (
5572                                 p_Group_Id                      NUMBER,
5573                                 p_business_group_name           VARCHAR2,
5574                                 p_transaction_id                NUMBER,
5575                                 p_organization_id                NUMBER,
5576                                 p_employee_number                VARCHAR2,
5577                                 p_department_id                        NUMBER,
5578                                 p_project_id                        NUMBER,
5579                                 p_task_id                        NUMBER,
5580                                 p_transaction_date                DATE,
5581                                 p_base_transaction_value        NUMBER,
5582                                 p_primary_quantity                NUMBER,
5583                                 p_acct_period_id                NUMBER,
5584                                 p_expenditure_type                VARCHAR2,
5585                                 p_resource_description                VARCHAR2,
5586                                 p_wt_transaction_type                NUMBER,
5587                                 p_cost_element_id                     NUMBER,
5588                                      p_exp_org_name                       VARCHAR2,
5589                                 p_wip_txn_source_literal        VARCHAR2,
5590                                 p_wip_straight_time_literal     VARCHAR2,
5591                                 p_wip_syslink_literal                VARCHAR2,
5592                                 p_bur_syslink_literal                VARCHAR2,
5593                                 O_err_num                  OUT        NOCOPY NUMBER,
5594                                 O_err_code                  OUT        NOCOPY VARCHAR2,
5595                                 O_err_msg                  OUT        NOCOPY VARCHAR2,
5596                                 p_reference_account                NUMBER,
5597                                 p_cr_account                        NUMBER,
5598                                 p_wip_dr_sub_ledger_id              NUMBER,
5599                                 p_wip_cr_sub_ledger_id              NUMBER,
5600                                 p_wip_entity_id                        NUMBER,
5601                                 p_resource_id                        NUMBER,
5602                                 p_basis_resource_id                NUMBER,
5603                                 p_denom_currency_code           VARCHAR2,
5604 				p_person_type			VARCHAR2)
5605 
5606   IS
5607 
5608   l_err_num             NUMBER;
5609   l_err_code            VARCHAR2(240);
5610   l_err_msg             VARCHAR2(240);
5611   l_stmt_num            NUMBER;
5612   PROCESS_ERROR                 EXCEPTION;
5613   CST_FAILED_GET_EXPENDDATE     EXCEPTION;
5614   CST_FAILED_HOOK_ACCT          EXCEPTION;
5615   CST_FAILED_PROJTSK_VALID      EXCEPTION;
5616   l_batch               VARCHAR2(15);--Increased width for Bug2218654
5617   l_multi_org_id        NUMBER;
5618   /*l_gl_date             DATE;*/ /* Commented for bug 6266553 */
5619   l_exp_end_date        DATE;
5620   l_project_number      VARCHAR2(25);
5621   l_task_number         VARCHAR2(25);
5622   l_use_hook_acct       BOOLEAN;
5623   l_debug               VARCHAR2(80);
5624 
5625   l_transaction_source  VARCHAR2(30);
5626   l_raw_cost            NUMBER;
5627   l_raw_cost_rate       NUMBER;
5628   l_burdened_cost_rate  NUMBER;
5629   l_burdened_cost       NUMBER;
5630   l_dr_code_combination_id NUMBER;
5631   l_cr_code_combination_id NUMBER;
5632   l_wip_cr_sub_ledger_id  NUMBER;
5633   l_wip_dr_sub_ledger_id  NUMBER;
5634   l_syslinkage          VARCHAR2(240);
5635 
5636   l_uom_code            VARCHAR2(30) ;
5637   l_cc_rate             NUMBER;
5638   l_cc_type             VARCHAR2(30);
5639   l_cc_date             DATE;
5640   l_cc_foreign_currency VARCHAR2(10);
5641   l_operating_unit      NUMBER;
5642   l_user_cc_type        VARCHAR2(30);
5643 
5644    /* For service line types delivered to shop floor, stamp the amount
5645       in the quantity column and set the cost_rate to 1
5646       (eAM Requirements Project - R12) */
5647 
5648   CURSOR c_sel_wt IS
5649 
5650         SELECT        p_wip_txn_source_literal        transaction_source,
5651                 l_batch                                batch_name,
5652                 l_exp_end_date                        expenditure_ending_date,
5653                 p_employee_number                employee_number,
5654                 p_exp_org_name                        organization_name,
5655                 p_transaction_date                expenditure_item_date,
5656                 l_project_number                project_number,
5657                 l_task_number                        task_number,
5658                 p_expenditure_type                expenditure_type,
5659                 p_denom_currency_code           denom_currency_code,
5660 
5661                 decode(p_cost_element_id,
5662                        5,0,
5663                        decode(p_primary_quantity,
5664                          NULL,p_base_transaction_value,
5665                           p_primary_quantity)) quantity,
5666                 decode(p_cost_element_id,5,0, p_base_transaction_value)
5667                                                   raw_cost,
5668 
5669                 p_resource_description                expenditure_comment,
5670                 to_char(p_transaction_id)        orig_transaction_reference,
5671 
5672                 decode(p_cost_element_id,
5673                   5,0,
5674                   decode(p_primary_quantity,
5675                    NULL, 1,
5676                    0, p_base_transaction_value,
5677                    p_base_transaction_value / p_primary_quantity))
5678                                                 raw_cost_rate,
5679 
5680                 'Y'                                unmatched_negative_txn_flag,
5681                 p_reference_account                dr_code_combination_id,
5682                 p_cr_account                        cr_code_combination_id,
5683                 p_wip_dr_sub_ledger_id              wip_dr_sub_ledger_id,
5684                 p_wip_cr_sub_ledger_id              wip_cr_sub_ledger_id,
5685                 NULL                                cdl_system_reference1,
5686                 NULL                                cdl_system_reference2,
5687                 NULL                                cdl_system_reference3,
5688                 /*l_gl_date*/ /* Commented for bug 6266553 */ p_transaction_date                        gl_date,
5689                 l_multi_org_id                        org_id,
5690 
5691                 p_base_transaction_value        burdened_cost,
5692 
5693                    decode(p_base_transaction_value,0,0,
5694                     decode(p_primary_quantity,
5695                         NULL,1,
5696                         0,p_base_transaction_value,
5697                         p_base_transaction_value/p_primary_quantity))
5698                                                 burdened_cost_rate,
5699 
5700                   decode(p_cost_element_id,5,
5701                         p_bur_syslink_literal, p_wip_syslink_literal)
5702                                                 system_linkage,
5703 
5704                 'P'                                transaction_status_code,
5705 		p_person_type		person_type
5706           FROM         dual
5707           WHERE  p_base_transaction_value <> 0;
5708 
5709   BEGIN
5710         -- initialize local variables
5711         l_err_num := 0;
5712         l_err_code := '';
5713         l_err_msg := '';
5714         l_use_hook_acct := FALSE;
5715         l_stmt_num := 1;
5716 
5717         l_debug := FND_PROFILE.VALUE('MRP_DEBUG');
5718         if (l_debug = 'Y') then
5719             FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Reached#: pm_process_txn_wt');
5720         end if;
5721 
5722         -- Initialize the batch name as CC followed by group_id
5723 
5724         l_stmt_num := 10;
5725         -- Modified for Bug#2218654
5726         -- Changed for Bug #2260708. PA import fails when you use a 15 character
5727         -- batch name. Instead I am using the last 8 characters of the group id
5728         -- so the batch name remains less than 10 chars. The likelihood of two
5729         -- batch numbers being the same is very low.
5730 
5731         SELECT 'CC'|| substr( replace( lpad(
5732                       to_char(p_Group_Id,'9999999999999'),14,'0') ,' ','0'),-8)
5733         INTO l_batch
5734         FROM DUAL;
5735 
5736         -- get exp ending date for the current transaction's transaction_date
5737         /* Bug 5308514 - Setting the OU context for the current organization */
5738         l_stmt_num := 16;
5739         select org_information3
5740         into l_operating_unit
5741         from hr_organization_information
5742         where organization_id = p_organization_id
5743         and org_information_context ='Accounting Information';
5744 
5745         l_stmt_num :=17;
5746         begin
5747          mo_global.set_policy_context('S',l_operating_unit);
5748         end;
5749 
5750         l_stmt_num := 20;
5751         l_exp_end_date := pa_utils.GetWeekEnding(p_transaction_date);
5752 
5753         IF l_exp_end_date is NULL THEN
5754                 RAISE CST_FAILED_GET_EXPENDDATE;
5755         END IF;
5756 
5757         /* Commented for bug 6266553
5758         l_stmt_num := 30;
5759         SELECT schedule_close_date
5760           INTO l_gl_date
5761           FROM org_acct_periods oap
5762          WHERE oap.organization_id = p_organization_id
5763            AND oap.acct_period_id = p_acct_period_id;*/
5764 
5765         -- Get Proj/Task Number for Proj/Task Ids.
5766         -- Query from PJM_PROJECTS_V and PJM_TASKS_V
5767         -- Refer to Bug# 571127
5768 
5769         ----------------------------------------------------------------------
5770         -- MOAC Changes for R12:
5771         -- References to PJM_PROJECTS_V and PJM_TASKS_V has been removed and
5772         -- their base table pa_projects_all and pa_tasks are used instead.
5773         ----------------------------------------------------------------------
5774 
5775         BEGIN
5776                 l_stmt_num := 35;
5777                 IF p_project_id is NOT NULL then
5778                         SELECT  segment1 -- project number
5779                            INTO  l_project_number
5780                            FROM  pa_projects_all
5781                           WHERE  project_id = p_project_id;
5782 
5783                         l_stmt_num := 36;
5784 
5785                         SELECT  task_number
5786                         INTO  l_task_number
5787                         FROM  pa_tasks
5788                         WHERE  project_id = p_project_id
5789                         AND  task_id = p_task_id;
5790                 END IF;
5791         EXCEPTION
5792                 WHEN OTHERS THEN
5793                         l_err_msg := SUBSTR(SQLERRM,1,200);
5794                         RAISE CST_FAILED_PROJTSK_VALID;
5795         END;
5796 
5797         -- assign multi_org_id
5798 
5799         l_stmt_num := 40;
5800 
5801         /* bug 3742735. The org_id passed should be the org_id where the transaction happened and not the OU on the project. */
5802 
5803         select to_number(org_information3)
5804         into l_multi_org_id
5805         from hr_organization_information
5806         where organization_id = p_organization_id
5807         and org_information_context ='Accounting Information';
5808 
5809 
5810         l_stmt_num := 50;
5811 
5812        FOR c_rec IN c_sel_wt LOOP
5813 
5814 /* The following changes are for the support of blue print enabled organizations */
5815        If (p_wip_txn_source_literal = 'PJM_CSTBP_WIP_ACCOUNTS')
5816           OR (p_wip_txn_source_literal = 'PJM_CSTBP_WIP_NO_ACCOUNTS') then
5817            /* Blue print enabled org, so pass as raw */
5818 
5819            l_raw_cost_rate := c_rec.burdened_cost_rate;
5820            l_raw_cost := c_rec.burdened_cost;
5821            l_burdened_cost_rate := NULL;
5822            l_burdened_cost := NULL;
5823 
5824        Else /* non BP org */
5825 
5826            l_raw_cost_rate := c_rec.raw_cost_rate;
5827            l_raw_cost := c_rec.raw_cost;
5828            l_burdened_cost_rate := c_rec.burdened_cost_rate;
5829            l_burdened_cost := c_rec.burdened_cost;
5830 
5831        End If;
5832 
5833       l_stmt_num := 56;
5834 
5835    /* check for the transaction source to be "WIP with No Accounts". If it is
5836       then dont send the accounts */
5837 
5838       If p_wip_txn_source_literal = 'PJM_CSTBP_WIP_NO_ACCOUNTS' then
5839 
5840           l_dr_code_combination_id := NULL;
5841           l_cr_code_combination_id := NULL;
5842 
5843           l_wip_dr_sub_ledger_id := NULL;
5844           l_wip_cr_sub_ledger_id := NULL;
5845 
5846       else
5847 
5848          l_dr_code_combination_id := c_rec.dr_code_combination_id;
5849          l_cr_code_combination_id := c_rec.cr_code_combination_id;
5850 
5851          l_wip_dr_sub_ledger_id := c_rec.wip_dr_sub_ledger_id;
5852          l_wip_cr_sub_ledger_id := c_rec.wip_cr_sub_ledger_id;
5853 
5854       end if;
5855 
5856     l_stmt_num := 57;
5857 
5858     /* now check if the transaction has an employee on it .If yes then stamp it with "Straight time" source*/
5859 
5860 
5861        /* Bug #3449856. Use the transaction source of straight time only for
5862         * resource cost element. If there is a resource based overhead, it
5863         * should be passed with a transaction source of WIP. */
5864        If c_rec.employee_number is NOT NULL and p_cost_element_id <> 5 then
5865 
5866         fnd_file.put_line(fnd_file.log,'Setting Straight time');
5867         fnd_file.put_line(fnd_file.log,p_wip_straight_time_literal);
5868         l_transaction_source := p_wip_straight_time_literal ;
5869 
5870         /* set the sys linkage to the system linkage to which this transaction source maps to
5871            (usually this transaction source should map to the straight time system linkage) */
5872 
5873         Select NVL(system_linkage_function,c_rec.system_linkage) into l_syslinkage
5874         from pa_transaction_sources
5875         where transaction_source = l_transaction_source;
5876 
5877        else
5878 
5879         l_transaction_source := p_wip_txn_source_literal ;
5880         l_syslinkage := c_rec.system_linkage;
5881 
5882        End If;
5883 
5884      l_stmt_num := 60;
5885 
5886     /* bug 3345746 for Blue Print organizations the system linkage should be WIP even for overhead costs(BTC) as the
5887        transaction sources do not allow burdening in MFG (allow_burden_flag = 'N') */
5888 
5889       If (l_transaction_source = 'PJM_CSTBP_WIP_ACCOUNTS') OR (l_transaction_source = 'PJM_CSTBP_WIP_NO_ACCOUNTS')
5890          OR (l_transaction_source = 'PJM_CSTBP_ST_ACCOUNTS') OR (l_transaction_source = 'PJM_CSTBP_ST_NO_ACCOUNTS') then
5891 
5892          If l_syslinkage = p_bur_syslink_literal then
5893            l_syslinkage := p_wip_syslink_literal ;
5894          end if;
5895 
5896       end If;
5897 
5898 
5899 
5900      l_stmt_num := 62;
5901 
5902 
5903    /* This following insert statement into pa_transaction_interface will be changes to insert into pa_transaction_interface_all */
5904 
5905      /* modify the insert statement to insert the wip_resource_id and primary UOM code bug 3298023 */
5906 
5907         /* get the primary UOM code directly from the transaction */
5908 
5909         select primary_uom into l_uom_code
5910         from wip_transactions
5911         where transaction_id = p_transaction_id ;
5912 
5913      l_stmt_num := 62;
5914          If l_debug = 'Y' then
5915             fnd_file.put_line(fnd_file.log,' Deriving Currency Conversion Information ');
5916          end If;
5917         /*Bug 9656218
5918           Get the currency information */
5919         BEGIN
5920         select COALESCE(wta.currency_conversion_rate, wta.base_transaction_value/wta.transaction_value),
5921                wta.currency_conversion_type,
5922                wta.currency_conversion_date,
5923                wta.currency_code
5924           into l_cc_rate,
5925                l_cc_type,
5926                l_cc_date,
5927                l_cc_foreign_currency
5928           from wip_transaction_accounts wta
5929          where wta.transaction_id = p_transaction_id
5930            and wta.transaction_value is not null
5931            and accounting_line_type <> 15 /*Changes for encumbrance SF project */
5932            and rownum<2;
5933          EXCEPTION
5934       		WHEN NO_DATA_FOUND then
5935        	 		l_cc_rate := null;
5936         		l_cc_type := null;
5937 			l_cc_date := null;
5938 			l_cc_foreign_currency := null;
5939          END;
5940         If l_debug = 'Y' then
5941            IF l_cc_rate IS NULL then
5942               fnd_file.put_line(fnd_file.log,' Not a foreign currency transaction ');
5943            ELSE
5944               fnd_file.put_line(fnd_file.log,' Currency Conversion Rate : '||to_char(l_cc_rate));
5945               fnd_file.put_line(fnd_file.log,' Currency Conversion Type : '||l_cc_type);
5946               fnd_file.put_line(fnd_file.log,' Currency Conversion Date : '||to_char(l_cc_date));
5947               fnd_file.put_line(fnd_file.log,' Foreign Currency         : '||l_cc_foreign_currency);
5948            END IF;
5949          fnd_file.put_line(fnd_file.log,' UOM code for the WIP txn : ' || l_uom_code);
5950          fnd_file.put_line(fnd_file.log,' Quantity : ' || c_rec.quantity);
5951          fnd_file.put_line(fnd_file.log,' Raw Cost Rate : ' ||  l_raw_cost_rate);
5952         end If;
5953 
5954        l_stmt_num := 63; /*BUG 10623781: Added derivation of User Conversion type to pass to PA interface*/
5955         IF l_cc_type is NULL THEN
5956            l_user_cc_type := null;
5957         ELSE
5958          Select User_Conversion_Type
5959          Into   l_User_cc_Type
5960          from   gl_daily_conversion_types
5961          where  conversion_type = l_cc_type;
5962 	END IF;
5963 
5964         If l_debug = 'Y' then
5965            IF l_user_cc_type is NOT NULL then
5966              fnd_file.put_line(fnd_file.log,'User Currency Conversion Type : '||l_user_cc_type);
5967            END IF;
5968         end If;
5969 
5970 
5971 /* This following insert statement into pa_transaction_interface will be changes to insert into pa_transaction_interface_all */
5972 
5973         INSERT INTO pa_transaction_interface_all
5974         (  transaction_source,
5975            batch_name,
5976            expenditure_ending_date,
5977            employee_number,
5978            organization_name,
5979            expenditure_item_date,
5980            project_number,
5981            task_number,
5982            expenditure_type,
5983            quantity,
5984            denom_raw_cost,
5985            acct_raw_cost,
5986            expenditure_comment,
5987            orig_transaction_reference,
5988            raw_cost_rate,
5989            unmatched_negative_txn_flag,
5990            dr_code_combination_id,
5991            cr_code_combination_id,
5992            cdl_system_reference1,
5993            cdl_system_reference2,
5994            cdl_system_reference3,
5995            gl_date,
5996            org_id,
5997            denom_burdened_cost,
5998            acct_burdened_cost,
5999            burdened_cost_rate,
6000            system_linkage,
6001            transaction_status_code,
6002            denom_currency_code,
6003            acct_exchange_rate,
6004            acct_rate_type,
6005            acct_rate_date,
6006            person_business_group_name,
6007            wip_resource_id,
6008            unit_of_measure,
6009            cdl_system_reference4,  --WIP cr. sub Ledger ID
6010            cdl_system_reference5,   --WIP Dr. sub LEDGER ID
6011 	   person_type
6012         )
6013         VALUES
6014         (  l_transaction_source,
6015            c_rec.batch_name,
6016            c_rec.expenditure_ending_date,
6017            c_rec.employee_number,
6018            c_rec.organization_name,
6019            c_rec.expenditure_item_date,
6020            c_rec.project_number,
6021            c_rec.task_number,
6022            c_rec.expenditure_type,
6023            c_rec.quantity,
6024            decode(l_cc_rate, null, null, (l_raw_cost/l_cc_rate)), /*Foreign currency value*/
6025            l_raw_cost,                                            /*Functional currency value */
6026            c_rec.expenditure_comment,
6027            c_rec.orig_transaction_reference,
6028            l_raw_cost_rate,
6029            c_rec.unmatched_negative_txn_flag,
6030            l_dr_code_combination_id,
6031            l_cr_code_combination_id,
6032            c_rec.cdl_system_reference1,
6033            c_rec.cdl_system_reference2,
6034            c_rec.cdl_system_reference3,
6035            c_rec.gl_date,
6036            c_rec.org_id,
6037            decode(l_cc_rate, null, null, (l_burdened_cost/l_cc_rate)), /*Foreign currency value*/
6038            l_burdened_cost,                                            /*Functional currency value*/
6039            l_burdened_cost_rate,
6040            l_syslinkage,
6041            c_rec.transaction_status_code,
6042            l_cc_foreign_currency,
6043            l_cc_rate,
6044            l_user_cc_type,
6045            l_cc_date,
6046            p_business_group_name,
6047            p_resource_id,
6048            l_uom_code,
6049            l_wip_cr_sub_ledger_id,
6050            l_wip_dr_sub_ledger_id,
6051 	   c_rec.person_type
6052         );
6053         END LOOP;
6054 
6055         EXCEPTION
6056                 WHEN CST_FAILED_GET_EXPENDDATE THEN
6057                         O_err_num := 20001;
6058 
6059                         fnd_message.set_name('BOM','CST_FAILED_GET_EXPENDDATE');
6060                         l_err_msg := fnd_message.get ;
6061                         O_err_msg := substr(l_err_msg,1,240) ;
6062 
6063                         O_err_code := 'CSTPPCCB.pm_process_txn_wt('
6064                                         || to_char(l_stmt_num)
6065                                         || '): ';
6066 
6067                 WHEN PROCESS_ERROR THEN
6068                         O_err_num := l_err_num;
6069                         O_err_code := l_err_code;
6070                         O_err_msg := l_err_msg;
6071 
6072                 WHEN CST_FAILED_HOOK_ACCT THEN
6073                         O_err_num := 20002;
6074 
6075                         fnd_message.set_name('BOM','CST_FAILED_HOOK_ACCT');
6076                         l_err_msg := fnd_message.get ;
6077                         O_err_msg := substr(l_err_msg,1,240) ;
6078 
6079                         O_err_code := 'CSTPPCCB.pm_process_txn_wt('
6080                                         || to_char(l_stmt_num)
6081                                         || '): '
6082                                         || ' WIP Transaction_Id: '
6083                                         || to_char(p_transaction_id)
6084                                         || ' Organization_Id: '
6085                                         || to_char(p_organization_id);
6086 
6087                 WHEN CST_FAILED_PROJTSK_VALID THEN
6088                         O_err_num := 20003;
6089 /*
6090                         fnd_message.set_name('BOM','CST_FAILED_PROJTSK_VALID');
6091                         l_err_msg := fnd_message.get ;
6092                         O_err_msg := substr(l_err_msg,1,240) ;
6093 */
6094                         O_err_msg := 'Proj/Task Invalid for Cost Collection.';
6095                         O_err_code := SUBSTR('CSTPPCCB.pm_process_txn_wt('
6096                                 || to_char(l_stmt_num)
6097                                 || '): '
6098                                 ||l_err_msg,1,240);
6099 
6100 
6101                 WHEN OTHERS THEN
6102                         O_err_num := SQLCODE;
6103                         O_err_code := NULL;
6104                         O_err_msg := SUBSTR('CSTPPCCB.pm_process_txn_wt('
6105                                         || to_char(l_stmt_num)
6106                                         || '): '
6107                                         || SQLERRM,1,240);
6108 END pm_process_txn_wt;
6109 
6110  /*----------------------------------------------------------------------------*
6111  | PRIVATE PROCEDURE                                                          |
6112  |    pm_check_error_wt                                                       |
6113  |                                                                            |
6114  | DESCRIPTION                                                                |
6115  |    The procedure validates the transaction to cost collect. The validations|
6116  |    it performs are                                                         |
6117  |    1. Both the project and task columns for the txn are NOT NULL           |
6118  |    2. If the expenditure type for the Transactions Resource Id is not NULL |
6119  |    3. If the employee id is null, pa is informed the pa_expenditure_org_id |
6120  |       which needs to be NOT NULL  for the department in which the non labor|
6121  |       resource was used                                                    |
6122  |                                                                            |
6123  | PARAMETERS                                                                 |
6124  |                        p_transaction_id,                                   |
6125  |                        p_project_id,                                       |
6126  |                        p_task_id                                           |
6127  |                        p_expenditure_type                                  |
6128  |                        p_organization_id                                      |
6129  |                        p_department_id                                      |
6130  |                        p_employee_number                                      |
6131  |                        p_user_id,                                          |
6132  |                        p_login_id,                                         |
6133  |                        p_req_id,                                           |
6134  |                        p_prg_appl_id,                                      |
6135  |                        p_prg_id,                                           |
6136  |                        O_err_num,                                              |
6137  |                        O_err_code,                                         |
6138  |                        O_err_msg                                           |
6139  |                                                                            |
6140  | CALLED FROM                                                                |
6141  |    pm_cc_worker_wt()                                                       |
6142  |                                                                            |
6143  | HISTORY                                                                    |
6144  |    07-SEP-96  Bhaskar Dasari Created.                                      |
6145  *----------------------------------------------------------------------------*/
6146 
6147   PROCEDURE  pm_check_error_wt (
6148                                 p_transaction_id                NUMBER,
6149                                      p_project_id                        NUMBER,
6150                                      p_task_id                        NUMBER,
6151                                      p_expenditure_type                VARCHAR2,
6152                                      p_organization_id                NUMBER,
6153                                      p_department_id                        NUMBER,
6154                                      p_employee_number                VARCHAR2,
6155                                      p_exp_org_name               OUT        NOCOPY VARCHAR2,
6156                                      p_process_yn                 OUT         NOCOPY NUMBER,
6157                                 p_user_id                        NUMBER,
6158                                 p_login_id                        NUMBER,
6159                                 p_req_id                        NUMBER,
6160                                 p_prg_appl_id                         NUMBER,
6161                                 p_prg_id                         NUMBER,
6162                                      O_err_num                 OUT        NOCOPY NUMBER,
6163                                      O_err_code                 OUT        NOCOPY VARCHAR2,
6164                                      O_err_msg                 OUT        NOCOPY VARCHAR2)
6165   IS
6166 
6167   l_dummy               NUMBER;
6168   l_err_num             NUMBER;
6169   l_err_code            VARCHAR2(240);
6170   l_err_msg             VARCHAR2(240);
6171   l_err_msg_temp        VARCHAR2(240);
6172   l_stmt_num            NUMBER;
6173   PROCESS_ERROR                 EXCEPTION;
6174   CST_NO_EXP_ORG_FOR_DEPT       EXCEPTION;
6175 
6176 --UTF8 changes  l_organization_name     VARCHAR2(60);
6177   l_organization_name   hr_organization_units.name%TYPE;
6178   l_exp_type            VARCHAR2(30);
6179   l_error_code          VARCHAR2(240);
6180   l_error_explanation   VARCHAR2(240);
6181   l_debug               VARCHAR2(80);
6182 
6183   l_err_in_code         VARCHAR2(240);
6184   l_err_in_msg          VARCHAR2(240);
6185 
6186   BEGIN
6187         -- initialize local variables
6188         l_err_num := 0;
6189         l_err_code := '';
6190         l_err_msg := '';
6191         l_err_msg_temp := '';
6192         l_organization_name := '';
6193 
6194         l_debug := FND_PROFILE.VALUE('MRP_DEBUG');
6195         if (l_debug = 'Y') then
6196             FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Reached#: pm_check_error_wt');
6197         end if;
6198 
6199         p_process_yn := 1; /* 1 Implies Process the Txn */
6200         --
6201         -- GENERIC CHECKS
6202         --
6203         -- TXNS demanding knowledge of both project and task ie. NOT NULL
6204 
6205         l_stmt_num := 10;
6206         IF p_task_id is NULL THEN
6207 
6208                 fnd_message.set_name('BOM','CST_NO_PROJ_OR_TASK');
6209                 l_err_msg := fnd_message.get ;
6210                 l_err_code := 'CSTPPCCB.pm_check_error_wt('
6211                                 || to_char(l_stmt_num)
6212                                 || '): ';
6213 
6214                 l_error_explanation := substr(l_err_msg,1,240) ;
6215                 l_error_code := l_err_code;
6216 
6217                 pm_mark_error_wt ( p_transaction_id,
6218                                    l_error_code,
6219                                    l_error_explanation,
6220                                    p_user_id,
6221                                    p_login_id,
6222                                    p_req_id,
6223                                    p_prg_appl_id,
6224                                    p_prg_id,
6225                                    l_err_num,
6226                                    l_err_code,
6227                                    l_err_msg);
6228 
6229                 IF (l_err_num <> 0) THEN
6230                            -- Error occured
6231                            raise PROCESS_ERROR;
6232                 ELSE
6233                         p_process_yn := 2;
6234                         l_err_num := 20003;
6235                         l_err_code   := l_error_code;
6236                         l_err_msg    := l_error_explanation;
6237                         raise PROCESS_ERROR;
6238                 END IF;
6239         END IF;
6240 
6241         l_stmt_num := 20;
6242         IF p_expenditure_type is NULL THEN
6243 
6244                 fnd_message.set_name('BOM','CST_SE_ET_IS_NULL');
6245                 l_err_msg := fnd_message.get ;
6246                 l_err_code := 'CSTPPCCB.pm_check_error_wt('
6247                                 || to_char(l_stmt_num)
6248                                 || '): ';
6249 
6250                 l_error_explanation := substr(l_err_msg,1,240) ;
6251                 l_error_code := l_err_code;
6252 
6253                 pm_mark_error_wt ( p_transaction_id,
6254                                    l_error_code,
6255                                    l_error_explanation,
6256                                    p_user_id,
6257                                    p_login_id,
6258                                    p_req_id,
6259                                    p_prg_appl_id,
6260                                    p_prg_id,
6261                                    l_err_num,
6262                                    l_err_code,
6263                                    l_err_msg);
6264 
6265                 IF (l_err_num <> 0) THEN
6266                            -- Error occured
6267                            raise PROCESS_ERROR;
6268                 ELSE
6269                         p_process_yn := 2;
6270                         l_err_num := 20006;
6271                         l_err_code   := l_error_code;
6272                         l_err_msg    := l_error_explanation;
6273                         raise PROCESS_ERROR;
6274                 END IF;
6275         END IF;
6276 
6277         l_organization_name := NULL;
6278         if p_employee_number is NULL THEN
6279                 BEGIN
6280                         l_stmt_num := 30;
6281                         SELECT         hou.name
6282                               INTO         l_organization_name
6283                               FROM         bom_departments bd,
6284                                 hr_organization_units hou
6285                            WHERE         hou.organization_id =
6286                                                 bd.pa_expenditure_org_id
6287                                  AND         bd.organization_id = p_organization_id
6288                                  AND         bd.department_id = p_department_id;
6289                 EXCEPTION
6290                         when NO_DATA_FOUND then
6291                                 l_organization_name := 'NO VALUE';
6292                 END;
6293         END IF;
6294 
6295         IF l_organization_name = 'NO VALUE'  THEN
6296                 RAISE         CST_NO_EXP_ORG_FOR_DEPT;
6297         ELSE
6298                 p_exp_org_name := l_organization_name;
6299         END IF;
6300 
6301         EXCEPTION
6302 
6303                 WHEN CST_NO_EXP_ORG_FOR_DEPT THEN
6304 
6305                         fnd_message.set_name('BOM','CST_NO_EXP_ORG_FOR_DEPT');
6306                         l_err_msg := fnd_message.get ;
6307                         l_err_code := 'CSTPPCCB.pm_check_error_wt('
6308                                         || to_char(l_stmt_num)
6309                                         || '): ';
6310 
6311                         l_error_explanation := substr(l_err_msg,1,240) ;
6312                         l_error_code := l_err_code;
6313 
6314                         pm_mark_error_wt ( p_transaction_id,
6315                                             l_error_code,
6316                                             l_error_explanation,
6317                                             p_user_id,
6318                                             p_login_id,
6319                                             p_req_id,
6320                                             p_prg_appl_id,
6321                                             p_prg_id,
6322                                             l_err_num,
6323                                             l_err_code,
6324                                             l_err_msg);
6325 
6326                         IF (l_err_num <> 0) THEN
6327                                 O_err_num := l_err_num;
6328                                 O_err_code := l_err_code;
6329                                 O_err_msg := l_err_msg;
6330                         ELSE
6331                                 p_process_yn := 2;
6332                                 O_err_num := 20008;
6333                                 O_err_code   := l_error_code;
6334                                 O_err_msg    := l_error_explanation;
6335                         END IF;
6336 
6337                 WHEN PROCESS_ERROR THEN
6338 
6339                         O_err_num := l_err_num;
6340                         O_err_code := l_err_code;
6341                         O_err_msg := l_err_msg;
6342 
6343                 WHEN OTHERS THEN
6344                         O_err_num := SQLCODE;
6345                         O_err_code := NULL;
6346                         l_err_msg_temp := 'CSTPPCCB.pm_check_error_wt('
6347                                 || to_char(l_stmt_num)
6348                                 || '): '
6349                                 || substr(SQLERRM,1,150);
6350 
6351                         l_err_num := 0;
6352 
6353                         l_err_in_code := l_err_code;
6354                         l_err_in_msg := l_err_msg;
6355 
6356                         pm_mark_error_wt(p_transaction_id,
6357                                   l_err_in_code,
6358                                   l_err_in_msg,
6359                                   p_user_id,
6360                                   p_login_id,
6361                                   p_req_id,
6362                                   p_prg_appl_id,
6363                                   p_prg_id,
6364                                   l_err_num,
6365                                   l_err_code,
6366                                   l_err_msg);
6367                         IF (l_err_num <> 0) THEN
6368                                 O_err_msg := SUBSTR(l_err_msg_temp
6369                                                 ||' * '
6370                                                 ||l_err_msg
6371                                                 ||' * '
6372                                                 ||l_err_code
6373                                                 ||' * '
6374                                                 ||'TXN NOT MARKED IN WTIE!'
6375                                                 ,1,240);
6376                         ELSE
6377                                 O_err_msg := SUBSTR(l_err_msg_temp
6378                                                 ||' * '
6379                                                 ||'TXN MARKED IN WTIE.'
6380                                                 ,1,240);
6381 
6382                         END IF;
6383 END pm_check_error_wt;
6384 
6385  /*----------------------------------------------------------------------------*
6386  | PRIVATE PROCEDURE                                                          |
6387  |    pm_mark_error_wt                                                       |
6388  |                                                                            |
6389  | DESCRIPTION                                                                |
6390  |                                                                            |
6391  |                                                                            |
6392  | PARAMETERS                                                                 |
6393  |                                                                            |
6394  |                        p_transaction_id,                                   |
6395  |                        p_error_code,                                       |
6396  |                        p_error_explanation,                                |
6397  |                        p_user_id,                                          |
6398  |                        p_login_id,                                         |
6399  |                        p_req_id,                                           |
6400  |                        p_prg_appl_id,                                      |
6401  |                        p_prg_id,                                           |
6402  |                        O_err_num,                                              |
6403  |                        O_err_code,                                         |
6404  |                        O_err_msg                                           |
6405  |                                                                            |
6406  | CALLED FROM                                                                |
6407  |    pm_check_error_wt()                                                    |
6408  |                                                                            |
6409  | HISTORY                                                                    |
6410  |    07-SEP-96  Bhaskar Dasari Created.                                      |
6411  *----------------------------------------------------------------------------*/
6412 
6413   PROCEDURE  pm_mark_error_wt (
6414                                 p_transaction_id                NUMBER,
6415                                      p_error_code                              VARCHAR2,
6416                                      p_error_explanation                      VARCHAR2,
6417                                 p_user_id                        NUMBER,
6418                                 p_login_id                        NUMBER,
6419                                 p_req_id                        NUMBER,
6420                                 p_prg_appl_id                         NUMBER,
6421                                 p_prg_id                         NUMBER,
6422                                      O_err_num              OUT        NOCOPY NUMBER,
6423                                      O_err_code              OUT        NOCOPY VARCHAR2,
6424                                      O_err_msg              OUT        NOCOPY VARCHAR2)
6425 
6426   IS
6427 
6428   l_err_num             NUMBER;
6429   l_err_code            VARCHAR2(240);
6430   l_err_msg             VARCHAR2(240);
6431   l_stmt_num            NUMBER;
6432   l_debug               VARCHAR2(80);
6433 
6434   BEGIN
6435         -- initialize local variables
6436         l_err_num := 0;
6437         l_err_code := '';
6438         l_err_msg := '';
6439         l_stmt_num := 1;
6440 
6441         l_debug := FND_PROFILE.VALUE('MRP_DEBUG');
6442         if (l_debug = 'Y') then
6443             FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Reached#: pm_mark_error_wt ' );
6444         end if;
6445 
6446 
6447         l_stmt_num := 10;
6448 
6449         UPDATE         wip_transactions wt
6450            SET         wt.pm_cost_collected = 'E',
6451                         wt.last_update_date = sysdate,
6452                         wt.last_updated_by = p_user_id,
6453                         wt.last_update_login = p_login_id,
6454                         wt.request_id = p_req_id,
6455                         wt.program_application_id = p_prg_appl_id,
6456                         wt.program_id = p_prg_id,
6457                         wt.program_update_date = sysdate
6458                   WHERE         wt.transaction_id = p_transaction_id ;
6459 
6460         l_stmt_num := 20;
6461         UPDATE         wip_txn_interface_errors wtie
6462            SET         wtie.error_message = p_error_explanation,
6463                 wtie.last_update_date = sysdate,
6464                 wtie.last_updated_by = p_user_id,
6465                 wtie.last_update_login = p_login_id,
6466                 wtie.request_id = p_req_id,
6467                 wtie.program_application_id = p_prg_appl_id,
6468                 wtie.program_id = p_prg_id,
6469                 wtie.program_update_date = sysdate
6470          WHERE         wtie.error_column='PM_COST_COLLECTED'
6471            AND  wtie.transaction_id = p_transaction_id;
6472 
6473         IF SQL%NOTFOUND THEN
6474 
6475                 l_stmt_num := 30;
6476                 INSERT INTO wip_txn_interface_errors
6477                 ( transaction_id,
6478                   error_message,
6479                   error_column,
6480                   last_update_date,
6481                   last_updated_by,
6482                   creation_date,
6483                   created_by,
6484                   last_update_login,
6485                   request_id,
6486                   program_application_id,
6487                   program_id,
6488                   program_update_date
6489                 )
6490                 VALUES ( p_transaction_id,
6491                          p_error_explanation,
6492                          'PM_COST_COLLECTED',
6493                          sysdate,
6494                          p_user_id,
6495                          sysdate,
6496                          p_user_id,
6497                          p_login_id,
6498                          p_req_id,
6499                          p_prg_appl_id,
6500                          p_prg_id,
6501                          sysdate
6502                         );
6503         END IF;
6504 
6505         EXCEPTION
6506                 WHEN OTHERS THEN
6507                         O_err_num  := SQLCODE;
6508                         O_err_code := NULL;
6509                         O_err_msg  := 'CSTPPCCB.pm_mark_error_wt('
6510                                 || to_char(l_stmt_num)
6511                                 || '): '
6512                                 || substr(SQLERRM,1,200);
6513   END pm_mark_error_wt;
6514 
6515  /*----------------------------------------------------------------------------*
6516  | PUBLIC PROCEDURE                                                           |
6517  |    pm_get_mta_accts()                                                      |
6518  |                                                                            |
6519  | DESCRIPTION                                                                |
6520  |               This procedure returns the ccid from the manufacturing       |
6521  |               accounting distribution table (MTA).  The parameters         |
6522  |               passed to this procedure relate to records in mcacd/cicd.    |
6523  |               Since project interface is only concerned with the dr column,|
6524  |               the accounting information is extracted from mta based on    |
6525  |               the sign of primary transaction quantity.  Based on          |
6526  |               transaction specific distribution, accounting line type is   |
6527  |               passed to obtain the account that will be populated in the   |
6528  |               credit column of project interface table.                    |
6529  |                                                                            |
6530  |                                                                            |
6531  | CALLED FROM                                                                |
6532  |    pm_process_txn_mmt()                                                    |
6533  -----------------------------------------------------------------------------*/
6534 
6535 PROCEDURE pm_get_mta_accts  (
6536     p_transaction_id         NUMBER,
6537     p_cost_element_id        NUMBER,
6538     p_resource_id            NUMBER,
6539     p_source_flag            NUMBER,
6540     p_variance_flag          NUMBER,
6541     O_dr_code_combination_id IN OUT NOCOPY NUMBER,
6542     O_cr_code_combination_id IN OUT NOCOPY NUMBER,
6543     O_inv_cr_sub_ledger_id   OUT NOCOPY  NUMBER,
6544     O_inv_dr_sub_ledger_id   OUT NOCOPY  NUMBER,
6545     O_cc_rate                OUT NOCOPY  NUMBER,
6546     O_cc_type                OUT NOCOPY  VARCHAR2,
6547     O_cc_date                OUT NOCOPY  DATE,
6548     O_cc_foreign_currency    OUT NOCOPY  VARCHAR2,
6549     O_cc_functional_currency OUT NOCOPY  VARCHAR2,
6550     O_err_num                OUT NOCOPY  NUMBER,
6551     O_err_code               OUT NOCOPY  VARCHAR2,
6552     O_err_msg                OUT NOCOPY  VARCHAR2)
6553 IS
6554 
6555 l_transaction_action_id       NUMBER;
6556 l_transaction_source_type_id  NUMBER;
6557 l_organization_id             NUMBER;
6558 l_mta_organization_id         NUMBER;
6559 l_mta_transaction_id          NUMBER;
6560 l_source_flag                 NUMBER;
6561 l_citw_flag                   NUMBER;
6562 l_xfer_organization_id        NUMBER;
6563 l_xfer_transaction_id         NUMBER;
6564 l_subinventory_code           VARCHAR2(11);
6565 l_transfer_subinventory       VARCHAR2(11);
6566 l_inventory_item_id           NUMBER;
6567 l_type_class                  NUMBER;
6568 l_fob_point                   NUMBER;
6569 l_si_asset_yes_no             NUMBER;
6570 l_transfer_si_asset_yes_no    NUMBER;
6571 l_exp_flag                    NUMBER;
6572 l_exp_item                    NUMBER;
6573 l_xfer_exp_flag               NUMBER;
6574 l_accounting_line_type        NUMBER;
6575 l_cost_element_id             NUMBER;
6576 l_mta_primary_quantity        NUMBER;
6577 l_mmt_primary_quantity        NUMBER;
6578 l_resource_id                 NUMBER;
6579 l_cg_id                       NUMBER;
6580 l_xfer_cg_id                  NUMBER;
6581 l_stmt_num                    NUMBER;
6582 l_txn_type_id                 NUMBER;
6583 l_debug                       VARCHAR2(80);
6584 l_cost_method                 NUMBER;
6585 l_xfer_cost_method            NUMBER;
6586 l_blue_print_enabled          VARCHAR2(1);
6587 l_autoaccounting_flag         VARCHAR2(1);
6588 l_wip_txn_source_type         NUMBER;
6589 l_cc_rate                     NUMBER;
6590 l_cc_type                     VARCHAR2(30);
6591 l_cc_date                     DATE;
6592 l_cc_foreign_currency         VARCHAR2(10);
6593 l_cc_functional_currency      VARCHAR2(10);
6594 l_user_cc_type                VARCHAR2(30);
6595 CST_NO_ACCOUNT_FOUND            EXCEPTION;
6596 
6597 BEGIN
6598 
6599   O_dr_code_combination_id := -999999;
6600   O_cr_code_combination_id := -999999;
6601   l_accounting_line_type := -99;
6602   l_cost_element_id      := -99;
6603   l_mmt_primary_quantity :=   0;
6604   l_stmt_num := 0;
6605   l_xfer_cost_method := 0;
6606   l_wip_txn_source_type := '';
6607 
6608   l_debug := FND_PROFILE.VALUE('MRP_DEBUG');
6609   if (l_debug = 'Y') then
6610     FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Reached#: pm_get_mta_accts');
6611   end if;
6612 
6613   BEGIN
6614         /******   Get Transaction Information ******/
6615   l_stmt_num := 5;
6616   SELECT
6617     mmt.transaction_action_id,
6618     mmt.transaction_source_type_id,
6619     mmt.organization_id,
6620     mmt.transfer_organization_id,
6621     mmt.transfer_transaction_id,
6622     mmt.cost_group_id,
6623     mmt.transfer_cost_group_id,
6624     mmt.transfer_subinventory,
6625     mmt.inventory_item_id,
6626     mmt.primary_quantity,
6627     mmt.transaction_type_id,
6628     mtt.type_class
6629   INTO l_transaction_action_id,
6630     l_transaction_source_type_id,
6631     l_organization_id,
6632     l_xfer_organization_id,
6633     l_xfer_transaction_id,
6634     l_cg_id,
6635     l_xfer_cg_id,
6636     l_transfer_subinventory,
6637     l_inventory_item_id,
6638     l_mmt_primary_quantity,
6639     l_txn_type_id,
6640     l_type_class
6641   FROM mtl_material_transactions mmt,
6642        mtl_transaction_types mtt
6643   WHERE mmt.transaction_id = p_transaction_id
6644   AND mtt.transaction_type_id = mmt.transaction_type_id;
6645 
6646   IF l_transaction_action_id <> 17 THEN
6647     l_stmt_num := 10;
6648     SELECT mmt.subinventory_code,
6649            msi.asset_inventory
6650     INTO
6651       l_subinventory_code,
6652       l_si_asset_yes_no
6653     FROM MTL_MATERIAL_TRANSACTIONS mmt,
6654          MTL_SECONDARY_INVENTORIES msi
6655     WHERE
6656     mmt.transaction_id = p_transaction_id
6657     AND msi.secondary_inventory_name = mmt.subinventory_code
6658     AND msi.organization_id          = mmt.organization_id;
6659 
6660     l_stmt_num := 15;
6661           -- Get Item Expense  Flag
6662     SELECT decode(inventory_asset_flag, 'Y',0,1)
6663     INTO l_exp_item
6664     FROM mtl_system_items msi
6665     WHERE msi.inventory_item_id = l_inventory_item_id
6666     AND   msi.organization_id =  l_organization_id;
6667 
6668     l_stmt_num := 20;
6669     -- Set Expense Flag
6670     SELECT decode(l_exp_item,1,1,decode(l_si_asset_yes_no,1,0,1))
6671     INTO l_exp_flag
6672     FROM mtl_secondary_inventories msi
6673     WHERE msi.secondary_inventory_name = l_subinventory_code
6674     AND   msi.organization_id = l_organization_id;
6675   ELSE
6676    l_subinventory_code := NULL;
6677    l_si_asset_yes_no   := 0;
6678    l_exp_item := 1;
6679    l_exp_flag := 1;
6680   END IF;
6681 
6682 
6683   l_stmt_num := 25;
6684   /******  Set l_xfer_exp_flag ******/
6685   IF (l_transfer_subinventory IS NOT NULL) THEN
6686     SELECT msub.asset_inventory,
6687            decode(mitems.inventory_asset_flag,
6688            'Y', decode(msub.asset_inventory,1,0,1), 1)
6689     INTO l_transfer_si_asset_yes_no,
6690          l_xfer_exp_flag
6691     FROM mtl_secondary_inventories msub,
6692          mtl_system_items mitems
6693     WHERE msub.secondary_inventory_name = l_transfer_subinventory
6694     AND   msub.organization_id = l_xfer_organization_id
6695     AND   mitems.inventory_item_id = l_inventory_item_id
6696     AND   mitems.organization_id   = l_xfer_organization_id;
6697   END IF;
6698 
6699   -- Modified for fob stamping project
6700   l_stmt_num := 30;
6701   /******   Get FOB POINT ******/
6702   IF (l_xfer_organization_id IS NOT NULL) THEN
6703     IF l_transaction_action_id = 21 THEN /* Intransit Shipment */
6704       SELECT nvl(MMT.fob_point, MSNV.fob_point) INTO l_fob_point
6705       FROM mtl_shipping_network_view MSNV,
6706            mtl_material_transactions MMT
6707       WHERE MSNV.from_organization_id = l_organization_id
6708       AND MSNV.to_organization_id = l_xfer_organization_id
6709       AND MMT.transaction_id = p_transaction_id;
6710     ELSIF l_transaction_action_id = 12 THEN /* Intransit Receipt */
6711       SELECT nvl(MMT.fob_point, MSNV.fob_point)
6712       INTO l_fob_point
6713       FROM mtl_shipping_network_view MSNV,
6714            mtl_material_transactions MMT
6715       WHERE MSNV.from_organization_id = l_xfer_organization_id
6716       AND MSNV.to_organization_id = l_organization_id
6717       AND MMT.transaction_id = p_transaction_id;
6718     END IF;
6719   END IF;
6720 
6721   l_stmt_num := 35;
6722   /* Bug #2128760. CITW only applies to average costing orgs */
6723   SELECT primary_cost_method
6724   INTO l_cost_method
6725   FROM mtl_parameters
6726   WHERE organization_id = l_organization_id;
6727 
6728   l_stmt_num := 40;
6729   /* Check for standard to standard direct interorg transfers. In this case
6730      the accounting entries are all made against the sending transaction_id. */
6731   IF (l_xfer_organization_id IS NOT NULL) THEN
6732     SELECT primary_cost_method
6733     INTO l_xfer_cost_method
6734     FROM mtl_parameters
6735     WHERE organization_id = l_xfer_organization_id;
6736   END IF;
6737 
6738   l_stmt_num := 45;
6739   /******   Set CITW FLAG ******/
6740   IF (l_transaction_action_id = 1 AND l_transaction_source_type_id = 5
6741       AND l_cg_id <> NVL(l_xfer_cg_id,-9)
6742       AND l_xfer_cg_id IS NOT NULL
6743       AND l_cost_method = 2) THEN
6744     l_citw_flag := 1;
6745   ELSE
6746                   l_citw_flag := -1;
6747   END IF;
6748 
6749   l_stmt_num := 50;
6750         /******   Start Dr Account Processing ******/
6751   l_source_flag := p_source_flag;
6752   l_mta_transaction_id := p_transaction_id;
6753   l_mta_organization_id := l_organization_id;
6754 
6755   IF (l_source_flag = 1) THEN
6756     IF l_type_class = 1 THEN
6757       l_accounting_line_type := 2;
6758       l_cost_element_id := -99;
6759     ELSIF (l_citw_flag = 1) THEN
6760       l_accounting_line_type := 1; -- Project CG is Dr
6761       l_wip_txn_source_type := 13; --INV txn SRC
6762       l_cost_element_id := NVL(p_cost_element_id, -99);
6763     ELSIF (l_transaction_action_id = 3) THEN
6764                 /* If direct interorg is between standard costing orgs,
6765                          no need for this as all mta entries will be against
6766                          the sending transaction */
6767       IF ( l_cost_method <> 1 OR
6768            l_xfer_cost_method <> 1 OR
6769            l_mmt_primary_quantity>0) THEN
6770         l_mta_transaction_id := l_xfer_transaction_id;
6771       END IF;
6772       l_mta_organization_id := l_xfer_organization_id;
6773       IF (l_xfer_exp_flag = 0) THEN
6774         l_accounting_line_type := 1;
6775       ELSE
6776         l_accounting_line_type := 2;
6777       END IF;
6778       If ( NVL(p_cost_element_id,-99)= 2 AND
6779            NVL(p_resource_id,-99) <> -99) then
6780         l_cost_element_id :=2 ; /* Mat Abs at receiving */
6781       ELSE
6782         l_cost_element_id := NVL(p_cost_element_id,-99); /* NOT Always MAT at REC ORG */
6783       End If;
6784     ELSIF l_transaction_source_type_id = 5 THEN --WIP/CFM
6785       l_accounting_line_type := 7;
6786       l_cost_element_id := NVL(p_cost_element_id, -99);
6787     /* bug 3150050 include staging Txfrs */
6788     ELSIF l_transaction_action_id in (2,28) THEN --SUB Xfer
6789       IF l_xfer_exp_flag = 0 THEN
6790         l_accounting_line_type := 1;
6791       ELSE
6792         l_accounting_line_type := 2;
6793       END IF;
6794 
6795          l_cost_element_id := NVL(p_cost_element_id, -99);
6796         -- =====================================================
6797         -- FP BUG 8614146 fix : set l_cost_element_id to -99 when
6798 	-- source flag is 1, txn_action_id in 2,28 and
6799 	-- expense flag is 1 (expense item or expense sub inv)
6800 	-- for standard costing method
6801 	-- NOTE: In MTA, it is OK to have cost_element_id NULL
6802 	-- for accounting_line_type 2 for expense destination
6803 	-- =====================================================
6804         If (l_xfer_exp_flag = 1 AND l_cost_method = 1) then
6805            l_cost_element_id := -99;
6806         End If;
6807 
6808     /* Logical Expense Requisition Receipt */
6809     ELSIF (l_transaction_action_id = 17) THEN
6810       L_accounting_line_type := 2; -- Account
6811     END IF;
6812     l_mta_primary_quantity := -1 * l_mmt_primary_quantity;
6813 
6814   ELSE /* Not Src_Flag */
6815     IF (l_transaction_action_id = 3 AND
6816         l_cost_method = 1 AND
6817         l_xfer_cost_method = 1 AND
6818         l_mmt_primary_quantity > 0) THEN
6819       l_mta_transaction_id := l_xfer_transaction_id;
6820     END IF;
6821     IF ( l_transaction_action_id = 2 )
6822         AND ( l_cg_id <> l_xfer_cg_id)
6823         AND (l_txn_type_id = 68)
6824         AND (p_variance_flag = 1) THEN
6825       l_accounting_line_type := 13; -- Payback Transaction
6826     -- STD PJM
6827     ELSIF ( p_variance_flag = 1 AND
6828           ( l_transaction_source_type_id = 1
6829             OR l_transaction_action_id IN (12,21,3)
6830             OR (l_transaction_action_id = 6 ))) THEN
6831             /* Consigned ownership transfer transactions */
6832       IF (l_transaction_action_id = 3 AND
6833           l_cost_method <> 1 AND
6834           l_xfer_cost_method = 1 AND
6835           l_mmt_primary_quantity < 0) THEN
6836          l_mta_transaction_id := l_xfer_transaction_id;
6837                            l_mta_organization_id := l_xfer_organization_id;
6838       ELSIF (l_transaction_action_id = 3 AND
6839              l_cost_method = 1 AND
6840              l_xfer_cost_method = 1 AND
6841              l_mmt_primary_quantity < 0) THEN
6842         l_mta_organization_id := l_xfer_organization_id;
6843  	       ELSIF (l_transaction_action_id = 21 AND
6844  	              l_cost_method = 1 AND
6845  	              l_xfer_cost_method = 1 AND
6846  	              l_fob_point = 1 ) THEN
6847          l_mta_organization_id := l_xfer_organization_id;
6848       END IF;
6849       -- PPV
6850       l_accounting_line_type := 6;
6851     /* Internal Sales order Issue to Expense */
6852     ELSIF(l_transaction_action_id = 1 AND l_transaction_source_type_id = 8) THEN
6853       L_accounting_line_type := 10; -- Interorg Receivables
6854     ELSIF (l_transaction_action_id = 17) THEN
6855       L_accounting_line_type := 2; -- Account
6856     ELSE  -- the following is neither payback nor PPV
6857       IF l_exp_flag = 0 THEN
6858         l_accounting_line_type := 1;
6859       ELSE
6860         l_accounting_line_type := 2;
6861       END IF;
6862       /* check for common issue to wip,if yes then set the source to be used */
6863       If l_citw_flag = 1 then
6864         l_wip_txn_source_type := 13;
6865       else
6866         l_wip_txn_source_type := '';
6867       end if;
6868     END IF;
6869 
6870     l_cost_element_id := NVL(p_cost_element_id, -99);
6871 
6872 /*      Bug 3978501.Change the l_cost_element_id to -99 for expense item or expense  */
6873 /*      sub as MTA will have the Dr entry with cost element id of NULL for expense  */
6874 /*      item receipts   */
6875 /*      Exclude Internal Order Issues to Expense  */
6876 /*      I know that's messed up.. But.  */
6877 /* bugs 4655264 and 4651130. In consistencies between standard and Average costing orgs. THe cost processor stamps a
6878    cost element ID against the AL of 2 in the case of Average costing orgs and in the case of std costing orgs
6879    there is no CE stamped against the AL 2.Need to handle them separately. */
6880 
6881 
6882     if ( l_exp_flag = 1  AND l_transaction_action_id <> 17 AND l_cost_method = 1) then
6883       l_cost_element_id := -99;
6884     End If;
6885     l_mta_primary_quantity := l_mmt_primary_quantity;
6886 
6887     /* Removed for 10191076 */
6888 
6889   END IF;
6890 
6891   if (l_debug = 'Y') then
6892     FND_FILE.PUT_LINE (FND_FILE.LOG,
6893                                 'DEBIT DATA. Txn: '||to_char(l_mta_transaction_id)
6894                                 ||' Exp_Flag: '||to_char(l_exp_flag)
6895                                 ||' Xfer_Exp_Flag: '
6896                                 ||to_char(l_xfer_exp_flag)
6897                                 ||' Org: '||to_char(l_mta_organization_id)
6898                                 ||' Item: '||to_char(l_inventory_item_id)
6899                                 ||' CE: '||to_char(l_cost_element_id)
6900                                 ||' AL: '||to_char(l_accounting_line_type)
6901                                 ||' Qty: '||to_char(l_mta_primary_quantity)
6902                                 ||' l_cost_method ' || to_char(l_cost_method)
6903                                 ||' l_xfer_cost_method '||to_char(l_xfer_cost_method)
6904                                 ||' Src Flag '||to_char(l_source_flag)
6905                                 ||' Transfer txn '||to_char(l_xfer_transaction_id)
6906                                 ||' Transfer org '||to_char(l_xfer_organization_id)
6907                                 ||' WIP Txn Src Type ' || to_char(l_wip_txn_source_type)
6908                                 );
6909   END IF;
6910 
6911   SELECT  NVL(MAX(mta.reference_account), -999999)
6912   INTO    O_dr_code_combination_id
6913   FROM    mtl_transaction_accounts mta
6914   WHERE
6915     mta.transaction_id            = l_mta_transaction_id        AND
6916     mta.organization_id           = l_mta_organization_id       AND
6917     mta.inventory_item_id         = l_inventory_item_id         AND
6918     nvl(mta.cost_element_id, -99) = l_cost_element_id           AND
6919     mta.accounting_line_type      = l_accounting_line_type    AND
6920     mta.primary_quantity          = decode(p_variance_flag, 1, mta.primary_quantity,
6921                                            decode(l_source_flag, 1,
6922                                            decode(l_transaction_action_id, 3, mta.primary_quantity,
6923                                            l_mta_primary_quantity), l_mta_primary_quantity)) AND
6924     (((l_citw_flag = 1) AND
6925       (mta.transaction_source_type_id = l_wip_txn_source_type)) OR(l_citw_flag <> 1));
6926 
6927 
6928    l_stmt_num := 51;
6929 
6930    /* Changes to get the Debit INV sub Ledger ID from MTA  for the dr account fecthed above*/
6931 
6932   If O_dr_code_combination_id <> -999999 then
6933 
6934    SELECT MAX(INV_SUB_LEDGER_ID)
6935     INTO  O_inv_dr_sub_ledger_id
6936     FROM  mtl_transaction_accounts mta
6937    WHERE
6938     mta.reference_account         = O_dr_code_combination_id    AND
6939     mta.transaction_id            = l_mta_transaction_id        AND
6940     mta.organization_id           = l_mta_organization_id       AND
6941     mta.inventory_item_id         = l_inventory_item_id         AND
6942     nvl(mta.cost_element_id, -99) = l_cost_element_id           AND
6943     mta.accounting_line_type      = l_accounting_line_type    AND
6944     mta.primary_quantity          = decode(p_variance_flag, 1, mta.primary_quantity,
6945                                            decode(l_source_flag, 1,
6946                                            decode(l_transaction_action_id, 3, mta.primary_quantity,
6947                                            l_mta_primary_quantity), l_mta_primary_quantity)) AND
6948     (((l_citw_flag = 1) AND
6949       (mta.transaction_source_type_id = l_wip_txn_source_type)) OR(l_citw_flag <> 1));
6950 
6951   End If;
6952 
6953 /*  BUG 9656218: Obtain the currency conversion information */
6954 /*  to pass it to pm_insert_pti_pvt and use it to  populate */
6955 /*  the  correct  columns in  projects  interface  for  the */
6956 /*  functional and foreign currency,  since  the  variables */
6957 /*  l_mta_transaction_id and l_mta_organization_id  already */
6958 /*  contain the logic to  select the appropiate transaction */
6959 /*  to query in MTA  including  the  cases for subinventory */
6960 /*  transfers and direct  interorg  between the   different */
6961 /*  combinations, we will query the subledger (MTA) to  get */
6962 /*  the currency data                                       */
6963 /*  In MTA only if transaction_value is populated, then we  */
6964 /*  can assume that the transaction is a foreign  currency  */
6965 /*  transaction and only in those cases we should pass the  */
6966 /*  currency information back                               */
6967 
6968    l_stmt_num := 52;
6969          If l_debug = 'Y' then
6970             fnd_file.put_line(fnd_file.log,' Deriving Currency Conversion Information ');
6971          end If;
6972          BEGIN
6973          	select 	COALESCE(mta.currency_conversion_rate, mta.base_transaction_value/mta.transaction_value),
6974                 	mta.currency_conversion_type,
6975                 	mta.currency_conversion_date,
6976                 	mta.currency_code
6977          	into   	l_cc_rate,
6978                 	l_cc_type,
6979                 	l_cc_date,
6980                 	l_cc_foreign_currency
6981          	from   	mtl_transaction_accounts mta
6982          	where  	mta.transaction_id  = l_mta_transaction_id
6983                 and     mta.organization_id = l_mta_organization_id
6984            	and  	mta.accounting_line_type <> 15    /*Added for Encumbrance project */
6985            	and  	mta.transaction_value is not null
6986            	and  	rownum<2;
6987      	 EXCEPTION
6988       		WHEN NO_DATA_FOUND then
6989        	 		l_cc_rate := null;
6990         		l_cc_type := null;
6991 			l_cc_date := null;
6992 			l_cc_foreign_currency := null;
6993 
6994       	 END;
6995      l_stmt_num := 53;
6996          select currency_code
6997          into   l_cc_functional_currency
6998          from   cst_organization_definitions
6999          where  organization_id = l_mta_organization_id;
7000 
7001         If l_debug = 'Y' then
7002            IF l_cc_rate IS NULL then
7003               fnd_file.put_line(fnd_file.log,' Not a foreign currency transaction ');
7004            ELSE
7005               fnd_file.put_line(fnd_file.log,' Currency Conversion Rate : '||to_char(l_cc_rate));
7006               fnd_file.put_line(fnd_file.log,' Currency Conversion Type : '||l_cc_type);
7007               fnd_file.put_line(fnd_file.log,' Currency Conversion Date : '||to_char(l_cc_date));
7008               fnd_file.put_line(fnd_file.log,' Foreign Currency         : '||l_cc_foreign_currency);
7009            END IF;
7010               fnd_file.put_line(fnd_file.log,' Functional Currency         : '||l_cc_functional_currency);
7011         end If;
7012 
7013       l_stmt_num := 54; /*BUG 10623781: Added derivation of User Conversion type to pass to PA interface*/
7014        IF l_cc_type is NULL THEN
7015             l_user_cc_type := null;
7016        ELSE
7017 	 select user_conversion_type
7018          into   l_user_cc_type
7019          from   gl_daily_conversion_types
7020          where  conversion_type = l_cc_type;
7021        END IF;
7022 
7023         If l_debug = 'Y' then
7024            IF l_user_cc_type is NOT NULL then
7025              fnd_file.put_line(fnd_file.log,'User Currency Conversion Type : '||l_user_cc_type);
7026            END IF;
7027         end If;
7028 
7029          O_cc_rate := l_cc_rate;
7030          O_cc_type := l_user_cc_type;
7031          O_cc_date := l_cc_date;
7032          O_cc_foreign_currency := l_cc_foreign_currency;
7033          O_cc_functional_currency := l_cc_functional_currency;
7034 
7035 /*  The decode stmt incorporated the fix for bug 917729 and 967071  */
7036 /*  bug 917729, for borrow payback txn, don't need to  */
7037 /*  join the primary quantity  */
7038 /*  Bug#967071. Changed the where clause , if sending and recv orgs have  */
7039 /*  different UOM then primary qty will be different   */
7040 
7041 
7042   l_stmt_num := 56;
7043   /******   Start Cr Account Processing ******/
7044   IF O_dr_code_combination_id <> -999999 THEN /* Valid Dr A/C */
7045     IF (l_citw_flag = 1) THEN /* MAIN IF */
7046       SELECT
7047         NVL((MAX(mta.reference_account)),-999999)
7048       INTO O_cr_code_combination_id
7049       FROM mtl_transaction_accounts mta
7050       WHERE mta.transaction_id = p_transaction_id
7051       AND mta.organization_id = l_organization_id
7052       AND mta.inventory_item_id = l_inventory_item_id
7053       AND NVL(mta.cost_element_id,-99)= NVL(p_cost_element_id,-99)
7054       AND mta.accounting_line_type = 1
7055       AND mta.primary_quantity = decode(l_source_flag,1,l_mmt_primary_quantity,((-1)*l_mmt_primary_quantity))
7056       AND mta.transaction_source_type_id = 13; --ALWAYS INV
7057 
7058       l_stmt_num := 57;
7059 
7060       /* changes to get the Credit INV sub ledger ID for the credit side  from MTA */
7061 
7062       Select MAX(INV_SUB_LEDGER_ID)
7063       into   O_inv_cr_sub_ledger_id
7064       from   mtl_transaction_accounts mta
7065       where  mta.reference_account = O_cr_code_combination_id
7066       AND    mta.transaction_id = p_transaction_id
7067       AND    mta.organization_id = l_organization_id
7068       AND    mta.inventory_item_id = l_inventory_item_id
7069       AND    NVL(mta.cost_element_id,-99)= NVL(p_cost_element_id,-99)
7070       AND    mta.accounting_line_type = 1
7071       AND    mta.primary_quantity = decode(l_source_flag,1,l_mmt_primary_quantity,((-1)*l_mmt_primary_quantity))
7072       AND    mta.transaction_source_type_id = 13; --ALWAYS INV
7073 
7074       l_stmt_num := 58;
7075 
7076       /* Check if Org is Blue print.If yes then use clearing Accts */
7077       BEGIN
7078       Select NVL(pa_posting_flag,'N'),
7079              NVL(pa_autoaccounting_flag,'N')
7080       into l_blue_print_enabled,
7081            l_autoaccounting_flag
7082       from pjm_org_parameters
7083       where organization_id = l_mta_organization_id;
7084       EXCEPTION
7085       WHEN NO_DATA_FOUND then
7086         l_blue_print_enabled := 'N';
7087         l_autoaccounting_flag := 'N';
7088       END;
7089       If l_blue_print_enabled = 'Y' AND l_autoaccounting_flag = 'N' then
7090         SELECT NVL(MAX(pjm_clearing_account),-999999)
7091         INTO O_cr_code_combination_id
7092         FROM pjm_org_parameters
7093         where organization_id = l_mta_organization_id;
7094 
7095         O_inv_cr_sub_ledger_id := NULL;
7096 
7097         If O_cr_code_combination_id = -999999 then
7098           raise CST_NO_ACCOUNT_FOUND;
7099         end If;
7100       End If;
7101     ELSIF (l_source_flag = 1) THEN /*Source Txn */
7102       IF (l_transaction_action_id = 3) THEN /* Dir IO */
7103         If (NVL(p_cost_element_id,-99) = 2 AND
7104             NVL(p_resource_id,-99) <> -99 ) then
7105           l_cost_element_id := 2;
7106           l_accounting_line_type := 3;
7107         else
7108           l_cost_element_id := -99;
7109           select (decode(sign(l_mmt_primary_quantity),1,10,9))
7110           into l_accounting_line_type
7111           from dual;
7112         End If;
7113                                 l_mta_primary_quantity:=l_mmt_primary_quantity;
7114       ELSIF ( l_transaction_action_id = 1 AND
7115               l_transaction_source_type_id = 8 ) THEN
7116         L_accounting_line_type := 1;
7117         l_cost_element_id := -99;
7118         l_mta_primary_quantity := l_mmt_primary_quantity;
7119       ELSIF (l_transaction_action_id = 17) THEN
7120         L_accounting_line_type := 9;
7121         l_cost_element_id := -99;
7122         l_mta_primary_quantity := l_mmt_primary_quantity;
7123       ELSE
7124         IF l_exp_flag = 0 THEN
7125           l_accounting_line_type := 1;
7126         ELSE
7127           l_accounting_line_type := 2;
7128         END IF;
7129         l_cost_element_id :=NVL(p_cost_element_id,-99);
7130         l_mta_primary_quantity:=l_mmt_primary_quantity;
7131       END IF;
7132     ELSE /* Neither CITW Nor Source_Flg */
7133       IF NVL(p_resource_id,-99) <> -99 THEN /* Res Not Null*/
7134         l_accounting_line_type := 3; --OHA
7135         l_cost_element_id :=2;       --MOH
7136         l_mta_primary_quantity := -1 * l_mmt_primary_quantity;
7137       ELSIF l_transaction_action_id = 3 and p_variance_flag =1 then
7138         /* handle PPV for direct interorgs receiving side */
7139         l_accounting_line_type := 9;
7140         /* the MTA primary Qty is always - against the Rcv */
7141         if l_mmt_primary_quantity < 0 then
7142           l_mta_primary_quantity := l_mmt_primary_quantity;
7143         else
7144           l_mta_primary_quantity := -1 * l_mmt_primary_quantity;
7145         end if;
7146       ELSE
7147         l_mta_primary_quantity := -1 * l_mmt_primary_quantity;
7148         SELECT
7149         decode(l_transaction_action_id, 3, --Direct IO Xfer
7150         decode(sign(l_mmt_primary_quantity), 1,9, --IO Pyble
7151         10), --IO Rcvble
7152         32, 7, --Assy Return
7153         31, 7, --Assy Complete
7154         33, 7, --Neg WIP Issue
7155         34, 7, --Neg WIP Return
7156         2, decode(l_xfer_exp_flag, 0,1, 2), --Sub Xfer
7157         4, 2, --Cycle Count
7158         8, 2, --Phy Inv
7159         12, --Intransit Receipt
7160         decode(nvl(l_fob_point,-99), 1,14, 2,9), 21, --Intransit Shipment
7161         decode(nvl(l_fob_point,-99), 1,10, 2,14), 29, decode(l_transaction_source_type_id, 1,5), --PO Adj
7162         1, decode(l_transaction_source_type_id, 1, 5, --RTV
7163         3, 2, --A/C Issue
7164         5, 7, --WIP/CFM
7165         6, 2, --Ali Issue
7166         13,2), --Misc Issue
7167         27, decode(l_transaction_source_type_id, 1, 5, --PO Receipt
7168         3, 2, --A/C Receipt
7169         5, 7, --WIP/CFM
7170         6, 2, --Ali Receipt
7171         13,2), --Misc Rcpt
7172         6,16, /*consigned ownership transfer */
7173         26,31, /* Logical PO receipt for BP org */
7174         28,1, /* staging Txfrs */
7175         17, 9, /* Logical Expense Requition Receipt */ -99)
7176         INTO l_accounting_line_type
7177         FROM DUAL;
7178 
7179         IF l_accounting_line_type IN (7,1,14) THEN
7180           l_cost_element_id := NVL(p_cost_element_id,-99);
7181         ELSIF l_accounting_line_type = 2 AND l_transaction_action_id =2 THEN
7182           l_cost_element_id := NVL(p_cost_element_id,-99);
7183         ELSIF l_accounting_line_type = 9 AND l_transaction_action_id = 17 THEN
7184           l_cost_element_id := NVL(p_cost_element_id,-99);
7185         ELSE
7186           l_cost_element_id := -99;
7187         END IF;
7188 
7189       END IF; /* Res Not Null */
7190     END IF; /*MAIN IF */
7191 
7192     IF (l_citw_flag <> 1) THEN
7193       IF l_debug = 'Y' THEN
7194         FND_FILE.put_line(fnd_file.log, 'CREDIT DATA: ');
7195         FND_FILE.put_line(fnd_file.log, 'Cost Element: '||to_char(l_cost_element_id) || ' Accounting Line Type: '||to_char(l_accounting_line_type)||' Primary Quantity: '||to_char(l_mta_primary_quantity));
7196       END IF;
7197 -- Bug 936641, for borrow payback variance, should NOT
7198 -- store both Cr and Dr with the same accounting information
7199 -- The line 'O_cr_code_combination_id := O_dr_code_combination_id;'
7200 -- was removed.
7201 -- The credit accounts for project transfer txns will be replaced
7202 -- with clearing accounts for blue print orgs
7203 -- Bug 3662806. Enclose Begin and end for non PJM orgs
7204       l_stmt_num := 60;
7205       BEGIN
7206       Select NVL(pa_posting_flag,'N'),
7207              NVL(pa_autoaccounting_flag,'N')
7208       into l_blue_print_enabled,
7209            l_autoaccounting_flag
7210       from pjm_org_parameters
7211       where organization_id = l_mta_organization_id;
7212       EXCEPTION
7213       WHEN NO_DATA_FOUND then
7214         l_blue_print_enabled := 'N';
7215         l_autoaccounting_flag := 'N';
7216       END;
7217       if ((l_transaction_action_id = 2) OR (l_transaction_action_id = 1
7218           AND l_transaction_source_type_id = 5) OR l_transaction_action_id = 28)
7219           AND l_blue_print_enabled= 'Y'
7220           AND l_autoaccounting_flag = 'N' then
7221         SELECT NVL(MAX(pjm_clearing_account),-999999)
7222         INTO O_cr_code_combination_id
7223         FROM pjm_org_parameters
7224         where organization_id = l_mta_organization_id;
7225 
7226         O_inv_cr_sub_ledger_id := NULL;
7227 
7228         If O_cr_code_combination_id = -999999 then
7229           raise CST_NO_ACCOUNT_FOUND;
7230         end If;
7231       else
7232       /* Non blue print or non project transfer txns
7233          changed the join condition on the cost element ID to get the correct account */
7234         SELECT  NVL(MAX(mta.reference_account), -999999)
7235         INTO    O_cr_code_combination_id
7236         FROM    mtl_transaction_accounts mta
7237         WHERE
7238         mta.transaction_id            =l_mta_transaction_id  AND
7239         mta.organization_id           =l_mta_organization_id AND
7240         mta.inventory_item_id         =l_inventory_item_id AND
7241         nvl(mta.cost_element_id, -99) =decode(l_cost_element_id,-99,nvl(mta.cost_element_id,-99),l_cost_element_id)   AND
7242         nvl(mta.resource_id, -99) = nvl(p_resource_id,-99)  AND
7243         ((mta.accounting_line_type   = l_accounting_line_type)
7244           OR (mta.accounting_line_type = 31 AND l_accounting_line_type = 5)) AND  /* Changes for Global Procurement PO clearing account*/
7245         mta.primary_quantity = l_mta_primary_quantity;
7246 
7247        /* Changes to get the INV sub ledger ID for the credit account from MTA */
7248 
7249        SELECT MAX(INV_SUB_LEDGER_ID)
7250          INTO O_inv_cr_sub_ledger_id
7251         FROM  mtl_transaction_accounts mta
7252         WHERE
7253         mta.reference_account         = O_cr_code_combination_id AND
7254         mta.transaction_id            =l_mta_transaction_id  AND
7255         mta.organization_id           =l_mta_organization_id AND
7256         mta.inventory_item_id         =l_inventory_item_id AND
7257         nvl(mta.cost_element_id, -99) =decode(l_cost_element_id,-99,nvl(mta.cost_element_id,-99),l_cost_element_id)   AND
7258         nvl(mta.resource_id, -99) = nvl(p_resource_id,-99)  AND
7259         ((mta.accounting_line_type   = l_accounting_line_type)
7260           OR (mta.accounting_line_type = 31 AND l_accounting_line_type = 5)) AND  /* Changes for Global Procurement PO clearing account*/
7261         mta.primary_quantity = l_mta_primary_quantity;
7262 
7263       end if;
7264     END IF; -- l_citw_flag <> 1
7265 
7266     IF O_cr_code_combination_id = -999999 THEN
7267       O_cr_code_combination_id := O_dr_code_combination_id;
7268       O_inv_cr_sub_ledger_id   := O_inv_dr_sub_ledger_id;
7269 
7270       IF l_debug = 'Y' THEN
7271         FND_FILE.put_line(FND_FILE.log, 'Credit Account not found. It will be set the same as Debit account');
7272       END IF;
7273 
7274     END IF;
7275 
7276   ELSE
7277     RAISE CST_NO_ACCOUNT_FOUND;
7278   END IF; /* Valid Dr A/C */
7279 
7280   EXCEPTION
7281     WHEN CST_NO_ACCOUNT_FOUND THEN
7282       O_err_num := -1;
7283       O_err_code := NULL;
7284       O_err_msg := SUBSTR('No Account Retrieved.'
7285                                         || '('
7286                                         ||to_char(l_stmt_num)
7287                                         ||') '
7288                                         ||' Txn_id: '
7289                                         ||to_char(p_transaction_id)
7290                                         ||' CE: '
7291                                         ||to_char(p_cost_element_id)
7292                                         ||' Rsrc: '
7293                                         ||to_char(p_resource_id)
7294                                         ||' Src_flag: '
7295                                         ||to_char(p_source_flag)
7296                                         ||' Borrow payback variance: '
7297                                         ||to_char(p_variance_flag),1,200);
7298     WHEN OTHERS THEN
7299       IF O_dr_code_combination_id <> -999999 THEN
7300         O_cr_code_combination_id := O_dr_code_combination_id;
7301         O_inv_cr_sub_ledger_id   := O_inv_dr_sub_ledger_id;
7302       ELSE
7303         O_dr_code_combination_id := -999999;
7304         O_cr_code_combination_id := -999999;
7305         O_inv_dr_sub_ledger_id   := NULL;
7306         O_inv_cr_sub_ledger_id   := NULL;
7307         O_err_num := SQLCODE;
7308         O_err_code := NULL;
7309         O_err_msg := SUBSTR('CSTPPCCB.pm_get_mta_accts('
7310                                         ||to_char(l_stmt_num)
7311                                         ||'): '
7312                                         ||SQLERRM,1,240);
7313       END IF;
7314   END;
7315 END pm_get_mta_accts;
7316 
7317  /*----------------------------------------------------------------------------*
7318  | PRIVATE FUNCTION                                                           |
7319  |    get_max_group_size                                                      |
7320  |                                                                            |
7321  | DESCRIPTION                                                                |
7322  |                                                                            |
7323  |    This function will return the maximum number of records that a single   |
7324  |    worker should process.                                                  |
7325  |                                                                            |
7326  | PARAMETERS                                                                 |
7327  |                                                                            |
7328  | RETURNS                                                                    |
7329  |      Records with in a Single Group                                        |
7330  |                                                                            |
7331  | CALLED FROM                                                                |
7332  |    pm_cc_manager                                                           |
7333  |                                                                            |
7334  | HISTORY                                                                    |
7335  |    07-SEP-96  Bhaskar Dasari Created.                                      |
7336  *----------------------------------------------------------------------------*/
7337 
7338   FUNCTION  get_group_size   RETURN NUMBER IS
7339 
7340   max_group_size_per_worker NUMBER := 3000;
7341   BEGIN
7342 
7343         return ( max_group_size_per_worker );
7344 
7345   END get_group_size;
7346 
7347 
7348  /*----------------------------------------------------------------------------*
7349  | PRIVATE PROCEDURE                                                          |
7350  |    pm_ins_pti_pvt                                                          |
7351  |                                                                            |
7352  | DESCRIPTION                                                                |
7353  |                                                                            |
7354  |    This procedure will call accounting  hook and cost collector hook andt  |
7355  |    it will manage the insertion of records in PA_TRANSACTIONS_INTERFACE    |
7356  |                                                                            |
7357  | PARAMETERS                                                                 |
7358  |                                                                            |
7359  |                                                                            |
7360  | CALLED FROM                                                                |
7361  |    pm_process_txn_mmt                                                      |
7362  |                                                                            |
7363  | HISTORY                                                                    |
7364  |    23-APR-01  Hemant Gosain Created.                                       |
7365  *----------------------------------------------------------------------------*/
7366 
7367   PROCEDURE  pm_insert_pti_pvt
7368                   (p_transaction_source                        VARCHAR2,
7369                       p_batch_name                                VARCHAR2,
7370                       p_expenditure_ending_date                DATE,
7371                       p_employee_number                        VARCHAR2,
7372                       p_organization_name                        VARCHAR2,
7373                       p_expenditure_item_date                DATE,
7374                       p_project_number                        VARCHAR2,
7375                       p_task_number                        VARCHAR2,
7376                       p_expenditure_type                        VARCHAR2,
7377                       p_pa_quantity                        NUMBER,
7378                       p_raw_cost                                NUMBER,
7379                       p_expenditure_comment                VARCHAR2,
7380                       p_orig_transaction_reference                VARCHAR2,
7381                       p_raw_cost_rate                        NUMBER,
7382                       p_unmatched_negative_txn_flag        VARCHAR2,
7383                       p_gl_date                                DATE,
7384                    p_org_id                                NUMBER,
7385                    p_burdened_cost                        NUMBER,
7386                    p_burdened_cost_rate                        NUMBER,
7387                    p_system_linkage                        VARCHAR2,
7388                    p_transaction_status_code                VARCHAR2,
7389                    p_denom_currency_code                VARCHAR2,
7390 
7391                    p_transaction_id                     NUMBER,
7392                    p_transaction_action_id              NUMBER,
7393                    p_transaction_source_type_id         NUMBER,
7394                    p_organization_id                    NUMBER,
7395                    p_inventory_item_id                  NUMBER,
7396                    p_cost_element_id                    NUMBER,
7397                    p_resource_id                        NUMBER,
7398                    p_source_flag                        NUMBER,
7399                    p_variance_flag                      NUMBER,
7400                    p_primary_quantity                   NUMBER,
7401                    p_transfer_organization_id           NUMBER,
7402                    p_fob_point                          NUMBER,
7403                    p_wip_entity_id                      NUMBER,
7404                    p_basis_resource                     NUMBER,
7405 
7406                    p_type_class                         NUMBER,
7407                    p_project_id                         NUMBER,
7408                    p_task_id                            NUMBER,
7409                    p_transaction_date                   DATE,
7410                    p_cost_group_id                      NUMBER,
7411                    p_transfer_cost_group_id             NUMBER,
7412                    p_transaction_source_id              NUMBER,
7413                    p_to_project_id                        NUMBER,
7414                    p_to_task_id                         NUMBER,
7415                    p_source_project_id                  NUMBER,
7416                    p_source_task_id                     NUMBER,
7417                    p_transfer_transaction_id            NUMBER,
7418                    p_primary_cost_method                NUMBER,
7419                    p_acct_period_id                     NUMBER,
7420                    p_exp_org_id                         NUMBER,
7421                    p_distribution_account_id            NUMBER,
7422                    p_proj_job_ind                       NUMBER,
7423                    p_first_matl_se_exp_type             VARCHAR2,
7424                    p_inv_txn_source_literal             VARCHAR2,
7425                    p_cap_txn_source_literal             VARCHAR2,
7426                    p_inv_syslink_literal                VARCHAR2,
7427                    p_bur_syslink_literal                VARCHAR2,
7428                    p_wip_syslink_literal                VARCHAR2,
7429                    p_user_def_exp_type                  VARCHAR2,
7430                    p_flow_schedule                      VARCHAR2,
7431                    p_si_asset_yes_no                    NUMBER,
7432                    p_transfer_si_asset_yes_no           NUMBER,
7433 
7434                    O_err_num                  OUT       NOCOPY  NUMBER,
7435                    O_err_code                 OUT       NOCOPY  VARCHAR2,
7436                    O_err_msg                  OUT       NOCOPY  VARCHAR2
7437                   )
7438 
7439   IS
7440 
7441     l_err_num                NUMBER := 0;
7442     l_err_code               VARCHAR2(240) ;
7443     l_err_msg                VARCHAR2(240) ;
7444     l_stmt_num               NUMBER;
7445     l_hook_used              NUMBER;
7446     l_cse_hook_used          NUMBER;
7447     l_cse_err_code           NUMBER := 0;
7448     l_cse_err_msg            NUMBER := 0;
7449     l_dr_code_combination_id NUMBER;
7450     l_cr_code_combination_id NUMBER;
7451     l_sql_stmt               VARCHAR2(8000);
7452     l_cse_installed          BOOLEAN;
7453     l_status                 VARCHAR2(1);
7454     l_industry               VARCHAR2(1);
7455     l_schema                 VARCHAR2(30);
7456     l_nl_trackable           VARCHAR2(1);
7457     l_asset_creation_code    VARCHAR2(30);
7458 
7459     l_raw_cost_rate          NUMBER := 0;
7460     l_raw_cost               NUMBER := 0;
7461     l_burdened_cost_rate     NUMBER := 0;
7462     l_burdened_cost          NUMBER := 0;
7463     l_systemlinkage          VARCHAR2(30);
7464 
7465     l_uom_code               VARCHAR2(30) ;
7466     l_cc_rate                NUMBER;
7467     l_cc_type                VARCHAR2(30);
7468     l_cc_date                DATE;
7469     l_cc_foreign_currency    VARCHAR2(10);
7470     l_cc_functional_currency VARCHAR2(10);
7471     l_inv_cr_sub_ledger_id   NUMBER;
7472     l_inv_dr_sub_ledger_id   NUMBER;
7473 
7474     CST_FAILED_GET_ACCOUNT   EXCEPTION;
7475     CST_FAILED_HOOK_ACCT     EXCEPTION;
7476     CST_FAILED_CST_CC_HOOK   EXCEPTION;
7477     CST_FAILED_CSE_CALL      EXCEPTION;
7478     CST_NL_NOT_INSTALLED     EXCEPTION;
7479 
7480     l_debug                  VARCHAR2(80);
7481     l_op_unit         NUMBER;
7482     l_cross_bg_profile	     VARCHAR2(1);  /* Added for bug 8398299 to pass business group name        */
7483     l_business_group_name    VARCHAR2(240);/* when the Cross Business Group profile has been activated */
7484 
7485   BEGIN
7486 
7487     l_stmt_num := 1;
7488     l_hook_used := 0;
7489     l_cse_hook_used := 0;
7490     l_cross_bg_profile := pa_utils.IsCrossBGProfile_WNPS; /*Added or bug 8398299, using wrapping function to get profile value*/
7491     l_business_group_name := NULL;
7492 
7493     l_debug := FND_PROFILE.VALUE('MRP_DEBUG');
7494     if (l_debug = 'Y') then
7495         FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Reached#:pm_insert_pti_pvt');
7496         FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Transaction_id :'||to_char(p_transaction_id));
7497         FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: HR:Cross Business Profile = '||l_cross_bg_profile);
7498     end if;
7499 
7500       l_stmt_num := 10;
7501 
7502       pm_get_mta_accts(
7503                         p_transaction_id         => p_transaction_id,
7504                         p_cost_element_id        => p_cost_element_id,
7505                         p_resource_id            => p_resource_id,
7506                         p_source_flag            => p_source_flag,
7507                         p_variance_flag          => p_variance_flag,
7508                         O_dr_code_combination_id => l_dr_code_combination_id,
7509                         O_cr_code_combination_id => l_cr_code_combination_id,
7510                         O_inv_cr_sub_ledger_id   => l_inv_cr_sub_ledger_id,
7511                         O_inv_dr_sub_ledger_id   => l_inv_dr_sub_ledger_id,
7512                         O_cc_rate                => l_cc_rate,
7513                         O_cc_type                => l_cc_type,
7514                         O_cc_date                => l_cc_date,
7515                         O_cc_foreign_currency    => l_cc_foreign_currency,
7516                         O_cc_functional_currency => l_cc_functional_currency,
7517                         O_err_num                => l_err_num,
7518                         O_err_code               => l_err_code,
7519                         O_err_msg                => l_err_msg);
7520 
7521        IF (l_err_num <> 0) THEN
7522 
7523          RAISE CST_FAILED_GET_ACCOUNT;
7524 
7525        END IF;
7526 
7527 
7528        l_stmt_num := 20;
7529 
7530        IF (NVL(l_dr_code_combination_id,-9) < 0) OR
7531                                 (NVL(l_cr_code_combination_id,-9) < 0)
7532        THEN
7533 
7534          RAISE CST_FAILED_HOOK_ACCT;
7535 
7536        END IF;
7537 
7538 
7539 
7540    l_stmt_num := 15;
7541 
7542 /* changes to support the PJM Blue Print organization stuff */
7543 
7544    If (p_transaction_source = 'PJM_CSTBP_INV_ACCOUNTS')
7545        OR (p_transaction_source = 'PJM_CSTBP_INV_NO_ACCOUNTS') then
7546 
7547        /* Blue print sources so pass everything as row cost */
7548 
7549          l_raw_cost_rate := p_burdened_cost_rate;
7550          l_raw_cost := p_burdened_cost;
7551          l_burdened_cost_rate := NULL;
7552          l_burdened_cost := NULL;
7553 
7554    Else /* non BP org */
7555 
7556          l_raw_cost_rate := p_raw_cost_rate;
7557          l_raw_cost := p_raw_cost;
7558          l_burdened_cost_rate := p_burdened_cost_rate;
7559          l_burdened_cost := p_burdened_cost;
7560 
7561    End If;
7562 
7563 
7564    l_stmt_num := 17;
7565 
7566       /* check if the transaction source is "Inventory with no accounts".If it  is, then do not send any accounts to PA.They will use auto accounting */
7567 
7568      If p_transaction_source = 'PJM_CSTBP_INV_NO_ACCOUNTS' then
7569 
7570        l_dr_code_combination_id := NULL;
7571        l_cr_code_combination_id := NULL;
7572 
7573        l_inv_cr_sub_ledger_id := NULL;
7574        l_inv_dr_sub_ledger_id := NULL;
7575 
7576      end if;
7577 
7578   -------------------------------------------------------------------------
7579     -- Network Logistics Support
7580     -------------------------------------------------------------------------
7581 
7582 /* Added Block - Network Logistics Changes */
7583 
7584     -------------------------------------------------------------------------
7585     -- Check Installation Status of Network Logistics (CSE)
7586     -------------------------------------------------------------------------
7587 
7588     l_stmt_num := 30;
7589 
7590      /* Bug 3742735. Pass the org_id of the organization on the txn always.This will enable cross project charging to also work. */
7591 
7592     select to_number(org_information3)
7593       into l_op_unit
7594       from hr_organization_information hoi,
7595            mtl_material_transactions mmt
7596       where hoi.organization_id = mmt.organization_id
7597       and mmt.transaction_id = to_number(p_orig_transaction_reference)
7598       and org_information_context ='Accounting Information';
7599 
7600     l_cse_installed := FND_INSTALLATION.GET_APP_INFO ( 'CSE',
7601                                                     l_status,
7602                                                     l_industry,
7603                                                     l_schema);
7604 
7605 /* Check if item is NL trackable  */
7606        SELECT   nvl(comms_nl_trackable_flag, 'N'), asset_creation_code
7607        INTO     l_nl_trackable, l_asset_creation_code
7608        FROM     mtl_system_items
7609        WHERE    inventory_item_id = p_inventory_item_id
7610        AND      organization_id =  p_organization_id;
7611 
7612 /* Bug 2907681 The code that checks for the item to be NL trackable and the NL module to be installed has been removed as it is not true always.We should not error out if the item is NL trackable but the NL module is not installed */
7613 
7614 
7615     IF (l_nl_trackable = 'Y' and l_status = 'I') THEN
7616 
7617       l_stmt_num := 35;
7618       l_cse_hook_used := 0;
7619 
7620       CSE_COST_COLLECTOR.eib_cost_collector_stub (
7621             p_transaction_id             =>  p_transaction_id,
7622             p_organization_id            =>  p_organization_id,
7623             p_transaction_action_id      =>  p_transaction_action_id,
7624             p_transaction_source_type_id =>  p_transaction_source_type_id,
7625             p_type_class                 =>  p_type_class,
7626             p_project_id                 =>  p_project_id,
7627             p_task_id                    =>  p_task_id,
7628             p_transaction_date           =>  p_transaction_date,
7629             p_primary_quantity           =>  p_primary_quantity,
7630             p_cost_group_id              =>  p_cost_group_id,
7631             p_transfer_cost_group_id     =>  p_transfer_cost_group_id,
7632             p_inventory_item_id          =>  p_inventory_item_id,
7633             p_transaction_source_id      =>  p_transaction_source_id,
7634             p_to_project_id              =>  p_to_project_id,
7635             p_to_task_id                 =>  p_to_task_id,
7636             p_source_project_id          =>  p_source_project_id,
7637             p_source_task_id             =>  p_source_task_id,
7638             p_transfer_transaction_id    =>  p_transfer_transaction_id,
7639             p_primary_cost_method        =>  p_primary_cost_method,
7640             p_acct_period_id             =>  p_acct_period_id,
7641             p_exp_org_id                 =>  p_exp_org_id,
7642             p_distribution_account_id    =>  p_distribution_account_id,
7643             p_proj_job_ind               =>  p_proj_job_ind,
7644             p_first_matl_se_exp_type     =>  p_first_matl_se_exp_type,
7645             p_inv_txn_source_literal     =>  p_inv_txn_source_literal,
7646             p_cap_txn_source_literal     =>  p_cap_txn_source_literal,
7647             p_inv_syslink_literal        =>  p_inv_syslink_literal,
7648             p_bur_syslink_literal        =>  p_bur_syslink_literal,
7649             p_wip_syslink_literal        =>  p_wip_syslink_literal,
7650             p_user_def_exp_type          =>  p_user_def_exp_type,
7651  	           p_transfer_organization_id   =>  p_transfer_organization_id,
7652             p_flow_schedule              =>  p_flow_schedule,
7653             p_si_asset_yes_no            =>  p_si_asset_yes_no,
7654             p_transfer_si_asset_yes_no   =>  p_transfer_si_asset_yes_no,
7655             p_denom_currency_code        =>  p_denom_currency_code,
7656             p_exp_type                   =>  p_expenditure_type,
7657             p_dr_code_combination_id     =>  l_dr_code_combination_id,
7658             p_cr_code_combination_id     =>  l_cr_code_combination_id,
7659             p_raw_cost                   =>  l_raw_cost,
7660             p_burden_cost                =>  l_burdened_cost,
7661             p_cr_sub_ledger_id           =>  l_inv_cr_sub_ledger_id,
7662             p_dr_sub_ledger_id           =>  l_inv_dr_sub_ledger_id,
7663             p_cost_element_id            =>  p_cost_element_id,
7664             O_hook_used                  =>  l_cse_hook_used,
7665             O_err_num                    =>  l_err_num,
7666             O_err_code                   =>  l_cse_err_code,
7667             O_err_msg                    =>  l_cse_err_msg);
7668 
7669       l_stmt_num := 40;
7670 
7671       FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: CSE hook usage flag :'||to_char(l_cse_hook_used));
7672       IF (l_err_num <> 0) THEN
7673 
7674         RAISE CST_FAILED_CSE_CALL;
7675 
7676       END IF;
7677 
7678     END IF;
7679 
7680 /* End: Added Block - Network Logistics Changes */
7681     l_stmt_num := 45;
7682 
7683     l_hook_used := 0;
7684 
7685     CST_COST_COLLECTOR_HOOK.pm_invtxn_hook(
7686                                  p_transaction_id                ,
7687                                  p_organization_id                ,
7688                                  p_transaction_action_id        ,
7689                                  p_transaction_source_type_id        ,
7690                                  p_type_class                        ,
7691                                  p_project_id                        ,
7692                                  p_task_id                        ,
7693                                  p_transaction_date                ,
7694                                  p_primary_quantity                ,
7695                                  p_cost_group_id                ,
7696                                  p_transfer_cost_group_id        ,
7697                                  p_inventory_item_id                ,
7698                                  p_transaction_source_id        ,
7699                                  p_to_project_id                ,
7700                                  p_to_task_id                        ,
7701                                  p_source_project_id                ,
7702                                  p_source_task_id                ,
7703                                  p_transfer_transaction_id        ,
7704                                  p_primary_cost_method                ,
7705                                  p_acct_period_id                ,
7706                                  p_exp_org_id                        ,
7707                                  p_distribution_account_id        ,
7708                                  p_proj_job_ind                        ,
7709                                  p_first_matl_se_exp_type        ,
7710                                  p_inv_txn_source_literal        ,
7711                                  p_cap_txn_source_literal        ,
7712                                  p_inv_syslink_literal                ,
7713                                  p_bur_syslink_literal                ,
7714                                  p_wip_syslink_literal                ,
7715                                  p_user_def_exp_type            ,
7716                                  p_transfer_organization_id     ,
7717                                  p_flow_schedule                ,
7718                                  p_si_asset_yes_no                ,
7719                                  p_transfer_si_asset_yes_no        ,
7720                                  p_denom_currency_code          ,
7721                                  p_expenditure_type             ,
7722                                  l_dr_code_combination_id       ,
7723                                     l_cr_code_combination_id       ,
7724                                  l_raw_cost                     ,
7725                                  l_burdened_cost,
7726                                  p_transaction_source           ,
7727                                  p_batch_name                   ,
7728                                  p_expenditure_ending_date      ,
7729                                  p_employee_number              ,
7730                                  p_organization_name            ,
7731                                  p_expenditure_item_date        ,
7732                                  p_project_number               ,
7733                                  p_task_number                  ,
7734                                  p_pa_quantity                  ,
7735                                  p_expenditure_comment          ,
7736                                  p_orig_transaction_reference   ,
7737                                  p_raw_cost_rate                ,
7738                                  p_unmatched_negative_txn_flag  ,
7739                                  p_gl_date                      ,
7740                                  l_op_unit                      ,
7741                                  p_burdened_cost_rate           ,
7742                                  p_system_linkage               ,
7743                                  p_transaction_status_code      ,
7744                                  l_hook_used                    ,
7745                                  l_err_num                      ,
7746                                  l_err_code                     ,
7747                                  l_err_msg);
7748     FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: INV hook usage flag :'||to_char(l_hook_used));
7749 
7750     IF (l_err_num <> 0) THEN
7751 
7752       RAISE CST_FAILED_CST_CC_HOOK;
7753 
7754     END IF;
7755 
7756 
7757 
7758 
7759 /* Added Block - Network Logistics Changes */
7760 
7761     IF(l_hook_used = 1 OR l_cse_hook_used = 1) THEN
7762       return;
7763     END IF;
7764 
7765 /* End: Added Block - Network Logistics Changes */
7766 
7767     l_stmt_num := 50;
7768 
7769 /* bug 3345746 .For Blue print orgs,the system linkage that should be pased in is INV  for burden as the
7770    transaction sources do not allow burdening in MFG ( allow_burden_flag = 'N') */
7771 
7772     l_systemlinkage := p_system_linkage;
7773 
7774     If (p_transaction_source = 'PJM_CSTBP_INV_ACCOUNTS') OR (p_transaction_source = 'PJM_CSTBP_INV_NO_ACCOUNTS') then
7775 
7776        If l_systemlinkage = p_bur_syslink_literal then
7777         l_systemlinkage := p_inv_syslink_literal;
7778        end If;
7779 
7780     End If;
7781 
7782    l_stmt_num := 51;
7783 /*Bug 8398299:  We will pass the business_group_name from hr_all_organization_units_tl from the business group
7784                 id that we will obtain from cst_organization_definitions for that particular organization_id to
7785                 prevent the error: PA_TOO_MANY_ORGN when they have activated the profile HR:Cross Business Group
7786 */
7787     IF (l_cross_bg_profile = 'Y') THEN
7788     	SELECT 	HAOUT.name
7789 	INTO   	l_business_group_name
7790         FROM   	hr_all_organization_units_tl HAOUT
7791         WHERE 	HAOUT.organization_id =
7792                                         (SELECT COD.business_group_id
7793                                          FROM	cst_organization_definitions COD
7794 				     	 WHERE  COD.organization_id = p_organization_id)
7795    	AND 	haout.language = USERENV('LANG');
7796      END IF;
7797 
7798    l_stmt_num := 52;
7799 
7800 
7801     IF (l_hook_used = 0 ) THEN
7802 
7803       if (l_debug = 'Y') then
7804           FND_FILE.PUT_LINE(FND_FILE.LOG,'DEBUG: Inserting transaction into PTI ');
7805       end if;
7806 
7807 
7808 /* The following insert statement into pa_transaction_interface will be changed to insert into pa_transaction_interface_all */
7809 
7810 
7811 /* The following insert statement into pa_transaction_interface will be changed to insert into pa_transaction_interface_all */
7812 
7813 /* Modified the insert to insert the Inventory_item_id and basic UOM code bug 3298023 */
7814 
7815    /*Get the primary UOM code of the item from mtl_system_items */
7816 
7817       select primary_uom_code into l_uom_code
7818       from mtl_system_items msi
7819       where msi.inventory_item_id = p_inventory_item_id
7820       AND msi.organization_id = p_organization_id;
7821 
7822         If l_debug = 'Y' then
7823          fnd_file.put_line(fnd_file.log,'UOM code for the INV txn : ' || l_uom_code);
7824         end If;
7825 
7826 
7827       /*++++++++++++++++++++++++++++++++++++++++++++++++++++++
7828       | Bug 9656218:                                         |
7829       | According to the feedback from Projects Development  |
7830       | 1. For foreign currency transactions we will pass:   |
7831       |     a) Currency conversion date, rate and type from  |
7832       |        the value returned from get_mta_accts         |
7833       |     b) Denom currency code as the foreign  currency  |
7834       |        code return from get_mta_accts                |
7835       |     c) Denom colums with transaction value           |
7836       |     d) Acc columns with base transaction value       |
7837       | 2. For non foreign currency transactions:            |
7838       |     a) Denom currency code as null                   |
7839       |     b) Acc columns with base transaction value       |
7840       |     c) Currency conversion date, rate, type and the  |
7841       |        denom currency code as null                   |
7842       +++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
7843 
7844 
7845 
7846       INSERT INTO pa_transaction_interface_all
7847                   (  transaction_source,
7848                       batch_name,
7849                       expenditure_ending_date,
7850                       employee_number,
7851                       organization_name,
7852                       expenditure_item_date,
7853                       project_number,
7854                       task_number,
7855                       expenditure_type,
7856                       quantity,
7857                       denom_raw_cost,
7858                       acct_raw_cost,
7859                       expenditure_comment,
7860                       orig_transaction_reference,
7861                       raw_cost_rate,
7862                       unmatched_negative_txn_flag,
7863                       dr_code_combination_id,
7864                       cr_code_combination_id,
7865                       cdl_system_reference1,
7866                       cdl_system_reference2,
7867                       cdl_system_reference3,
7868                       gl_date,
7869                       org_id,
7870                       denom_burdened_cost,
7871                       acct_burdened_cost,
7872                       burdened_cost_rate,
7873                       system_linkage,
7874                       transaction_status_code,
7875                       denom_currency_code,
7876                       acct_exchange_rate,
7877                       acct_rate_type,
7878                       acct_rate_date,
7879                       Inventory_item_id,
7880                       unit_of_measure,
7881                       cdl_system_reference4,    --Credit INV Sub ledger ID
7882                       cdl_system_reference5,     --Debit INV Subledger ID
7883                       person_business_group_name
7884                   )
7885       VALUES
7886                   (
7887                       p_transaction_source,
7888                       p_batch_name,
7889                       p_expenditure_ending_date,
7890                       p_employee_number,
7891                       p_organization_name,
7892                       p_expenditure_item_date,
7893                       p_project_number,
7894                       p_task_number,
7895                       p_expenditure_type,
7896                       p_pa_quantity,
7897                       decode(l_cc_rate, null, null, (l_raw_cost/l_cc_rate)), /*Foreign currency value*/
7898                       l_raw_cost,                                            /*Functional currency value*/
7899                       p_expenditure_comment,
7900                       p_orig_transaction_reference,
7901                       l_raw_cost_rate,
7902                       p_unmatched_negative_txn_flag,
7903                       l_dr_code_combination_id,
7904                       l_cr_code_combination_id,
7905                       NULL,
7906                       NULL,
7907                       NULL,
7908                       p_gl_date,
7909                       l_op_unit,
7910                       decode(l_cc_rate, null, null, (l_burdened_cost/l_cc_rate)), /*Foreign currency value*/
7911                       l_burdened_cost,                                            /*Functional currency value */
7912                       l_burdened_cost_rate,
7913                       l_systemlinkage,
7914                       p_transaction_status_code,
7915                       l_cc_foreign_currency, /*This should be null for non-foreign currency transactions*/
7916                       l_cc_rate,
7917                       l_cc_type,
7918                       l_cc_date,
7919                       p_inventory_item_id,
7920                       l_uom_code,
7921                       l_inv_cr_sub_ledger_id,
7922                       l_inv_dr_sub_ledger_id,
7923                       l_business_group_name
7924                   );
7925     END IF; --l_hook_used
7926 
7927   EXCEPTION
7928 
7929     WHEN CST_FAILED_CSE_CALL THEN
7930 
7931       O_err_num := 20005;
7932       O_err_code  := SUBSTR('CSTPPCCB.pm_insert_pti_pvt('
7933                             || to_char(l_stmt_num)
7934                             || '): '
7935                             || 'FAILED CSE Package Call. '
7936                             || l_cse_err_code,1,240);
7937       O_err_msg := substr(l_err_msg,1,240) ;
7938 
7939     WHEN CST_NL_NOT_INSTALLED THEN
7940       O_err_num := 20004;
7941       O_err_code  := SUBSTR('CSTPPCCB.pm_process_txn_mmt('
7942                             || to_char(l_stmt_num)
7943                             || '): '
7944                             || 'NL trackable/depreciable item will not be cost collected if Network Logistics is not installed',1,240);
7945       O_err_msg := substr(l_err_msg,1,240) ;
7946 
7947 
7948     WHEN CST_FAILED_GET_ACCOUNT THEN
7949 
7950       O_err_num := 20001;
7951       O_err_code  := SUBSTR('CSTPPCCB.pm_insert_pti_pvt('
7952                             || to_char(l_stmt_num)
7953                             || '): '
7954                             || 'FAILED TO GET ACCT. '
7955                             || l_err_msg,1,240);
7956       fnd_message.set_name('BOM','CST_FAILED_GET_ACCOUNT');
7957       l_err_msg := fnd_message.get ;
7958       O_err_msg := substr(l_err_msg,1,240) ;
7959 
7960      WHEN CST_FAILED_HOOK_ACCT THEN
7961 
7962        O_err_num := 20002;
7963 
7964        fnd_message.set_name('BOM','CST_FAILED_HOOK_ACCT');
7965        l_err_msg := fnd_message.get ;
7966        O_err_msg := substr(l_err_msg,1,240) ;
7967 
7968        O_err_code := 'CSTPPCCB.pm_insert_pti_pvt('
7969                             || to_char(l_stmt_num)
7970                             || '): ';
7971 
7972       WHEN CST_FAILED_CST_CC_HOOK THEN
7973 
7974         O_err_num := 20003;
7975         O_err_code := l_err_code;
7976         O_err_msg := SUBSTR('CSTPPCCB.pm_insert_pti_pvt('
7977                             ||to_char(l_stmt_num)
7978                             ||'), '
7979                             ||'CSTCCHKB.pm_invtxn_hook: '
7980                             ||'error at CSTCCHKB, line: '
7981                             ||to_char(l_err_num)
7982                             ||' CSTCCHKB.err_msg:'
7983                             ||l_err_msg,1,240);
7984 
7985 
7986       WHEN OTHERS THEN
7987 
7988          O_err_num := SQLCODE;
7989          O_err_code := NULL;
7990          O_err_msg := SUBSTR('CSTPPCCB.pm_insert_pti_pvt('
7991                             || to_char(l_stmt_num)
7992                             || '): '
7993                             ||SQLERRM,1,240);
7994 
7995 
7996   END pm_insert_pti_pvt;
7997 
7998 /*----------------------------------------------------------------------------*
7999 | PACKAGE CONSTRUCTOR                                                        |
8000 |                                                                            |
8001 | DESCRIPTION                                                                |
8002 |                                                                            |
8003 | HISTORY                                                                    |
8004 |      07-SEP-96   Bhaskar Dasari                                            |
8005 |                                                                            |
8006 *----------------------------------------------------------------------------*/
8007   --
8008   -- Constructor Code
8009   --
8010 BEGIN
8011         NULL;
8012 END CST_PRJMFG_COST_COLLECTOR ;