DBA Data[Home] [Help]

PACKAGE BODY: APPS.PA_ALLOC_RUN

Source


1 PACKAGE BODY PA_ALLOC_RUN AS
2 /* $Header: PAXALRNB.pls 120.12.12020000.3 2013/03/06 09:51:42 admarath ship $ */
3       P_DEBUG_MODE varchar2(1) := NVL(FND_PROFILE.value('PA_DEBUG_MODE'), 'N');
4       G_creation_date      DATE    ;
5       G_last_update_date   DATE    ;
6       G_created_by         NUMBER  ;
7       G_last_updated_by    NUMBER  ;
8       G_last_update_login  NUMBER  ;
9       G_sysdate            DATE := SYSDATE;
10       G_fatal_err_found    BOOLEAN := FALSE;
11       G_denom_currency_code      VARCHAR2(15);
12       G_lock_name VARCHAR2(20):= NULL;
13       G_rule_id     NUMBER ;
14       G_run_id      NUMBER ;
15       G_rule_name   PA_ALLOC_RULES_ALL.rule_name%TYPE ;
16       G_request_id  NUMBER ;
17       /* Bug No:- 2487147, UTF8 change : changed G_tgt_expnd_org to %TYPE */
18       /* G_tgt_expnd_org      VARCHAR2(60); */
19       G_tgt_expnd_org   hr_organization_units.name%TYPE;
20       G_tgt_expnd_type_class VARCHAR2(30);
21       G_tgt_expnd_type       VARCHAR2(30);
22       /* Bug No:- 2487147, UTF8 change : changed   G_offset_expnd_org to %TYPE */
23       /* G_offset_expnd_org      VARCHAR2(60); */
24       G_offset_expnd_org   hr_organization_units.name%TYPE;
25       G_offset_expnd_type_class VARCHAR2(30);
26       G_offset_expnd_type       VARCHAR2(30);
27       G_num_txns  NUMBER := 0 ;
28       G_basis_balance_category PA_ALLOC_RULES_ALL.basis_balance_category%TYPE;   /* added bug 2619977 */
29       G_basis_budget_type_code PA_ALLOC_RULES_ALL.basis_budget_type_code%TYPE;   /* added bug 2619977 */
30       G_basis_fin_plan_type_id PA_ALLOC_RULES_ALL.basis_fin_plan_type_id%TYPE;   /* added bug 2619977 */
31 	  G_Org_Id      Number; /* F
32       /* Cursor Declarations --------------------------------------- */
33       CURSOR C_proj_in_RL (p_project_id IN NUMBER, p_rl_id IN NUMBER , p_resource_struct_type in Varchar2, p_version_id In Number ) IS
34         SELECT 1
35           FROM pa_resource_list_assignments
36           WHERE project_id = p_project_id
37           AND resource_list_id = p_rl_id
38 		  AND P_resource_struct_type = 'RL'
39 		  UNION All
40 		Select 1
41 		  From PA_RBS_PRJ_ASSIGNMENTS
42 		 Where Project_Id = p_project_id
43 		   And RBS_VERSION_ID = p_version_id
44 		   AND P_resource_struct_type = 'RBS' ;
45         -- ------------------------------------------------------------
46         -- Init_who_cols
47         -- ------------------------------------------------------------
48         PROCEDURE Init_who_cols
49         IS
50         BEGIN
51            G_created_by        := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
52            G_last_update_login := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')), -1);
53            G_last_updated_by   := G_created_by;
54            G_creation_date     := G_sysdate;
55            G_last_update_date  := G_sysdate ;
56         END Init_who_cols ;
57         -- ------------------------------------------------------------
58         -- allocation_run: Main procedure for Allocation Run process
59         --                 Called through a report.
60         -- ------------------------------------------------------------
61 PROCEDURE allocation_run( p_process_mode    IN  VARCHAR2
62                                     , p_debug_mode      IN  VARCHAR2
63                                     , p_run_mode        IN  VARCHAR2 DEFAULT 'G'
64                                     , p_rule_id         IN  NUMBER
65                                     , p_run_period      IN  VARCHAR2 DEFAULT NULL
66                                     , p_expnd_item_date IN  DATE DEFAULT NULL
67                                     , x_run_id          OUT NOCOPY NUMBER
68                                     , x_retcode         OUT NOCOPY VARCHAR2
69                                     , x_errbuf          OUT NOCOPY VARCHAR2 ) IS
70            CURSOR C_get_rule IS
71              SELECT RULE_ID
72                , RULE_NAME
73                , ALLOCATION_METHOD
74                , TARGET_EXP_TYPE_CLASS
75                , TARGET_EXP_ORG_ID
76                , TARGET_EXP_TYPE
77                , TARGET_COST_TYPE
78                , POOL_PERCENT
79                , PERIOD_TYPE
80                , SOURCE_AMOUNT_TYPE
81                , SOURCE_BALANCE_CATEGORY
82                , SOURCE_BALANCE_TYPE
83                , ALLOC_RESOURCE_LIST_ID
84                , AUTO_RELEASE_FLAG
85                , IMP_WITH_EXCEPTION
86                , DUP_TARGETS_FLAG
87                , OFFSET_EXP_TYPE_CLASS
88                , OFFSET_EXP_ORG_ID
89                , OFFSET_EXP_TYPE
90                , OFFSET_COST_TYPE
91                , OFFSET_METHOD
92                , OFFSET_PROJECT_ID
93                , OFFSET_TASK_ID
94                , BASIS_METHOD
95                , BASIS_RELATIVE_PERIOD
96                , BASIS_AMOUNT_TYPE
97                , BASIS_BALANCE_CATEGORY
98                , BASIS_BUDGET_TYPE_CODE
99                , BASIS_FIN_PLAN_TYPE_ID   /* added bug 2619977 */
100                , BASIS_BUDGET_ENTRY_METHOD_CODE
101                , BASIS_BALANCE_TYPE
102                , BASIS_RESOURCE_LIST_ID
103                , SOURCE_EXTN_FLAG
104                , TARGET_EXTN_FLAG
105                , FIXED_AMOUNT
106                , NVL(START_DATE_ACTIVE, G_sysdate) START_DATE_ACTIVE
107                , NVL(END_DATE_ACTIVE, G_sysdate) END_DATE_ACTIVE
108                , ORG_ID
109                , LIMIT_TARGET_PROJECTS_CODE
110 			   /* FP.M : Allocation Impact : Bug # 3512552 */
111 			   , ALLOC_RESOURCE_STRUCT_TYPE
112 			   , BASIS_RESOURCE_STRUCT_TYPE
113 			   , ALLOC_RBS_VERSION
114 			   , BASIS_RBS_VERSION
115                FROM pa_alloc_rules_all
116                WHERE rule_id = p_rule_id ;
117              cursor c_target_det is
118                select '1'
119                from dual
120                  where exists ( select 'Y'
121                  from pa_alloc_run_targets
122                  where run_id = x_run_id
123                  and exclude_flag <> 'Y' ) ;
124                v_alloc_rule_rec C_get_rule%ROWTYPE;
125                v_status NUMBER:= NULL;
126                v_mode VARCHAR2(10) := NULL;
127                v_run_id NUMBER:=NULL;
128                v_prev_run_id NUMBER:= NULL;
129                v_old_stack VARCHAR2(630);
130                v_debug_mode VARCHAR2(2);
131                v_process_mode VARCHAR2(10);
132                v_src_amount_from_GL NUMBER;
133                v_src_amount_from_proj NUMBER;
134                v_bas_amount_from_proj NUMBER;
135                v_curr_alloc_amount NUMBER ;
136                v_period_type VARCHAR2(15);
137                v_period_set_name VARCHAR2(30);
138                v_period_year NUMBER;
139                v_quarter NUMBER;
140                v_period_num NUMBER;
141                v_run_period_end_date DATE;
142                v_request_id NUMBER;
143                v_err_message VARCHAR2(250);
144                v_basis_method VARCHAR2(2);
145                v_dummy  varchar2(1) := NULL ;
146                v_pazspa varchar2(1) := NULL ;
147 	       completion_status boolean;   -- Added for 2841843
148         BEGIN
149            pa_debug.Init_err_stack ( 'Allocation Run');
150            v_debug_mode := NVL(p_debug_mode, 'Y');
151            v_process_mode := NVL(p_process_mode, 'SQL');
152            pa_debug.set_process(v_process_mode, 'LOG', v_debug_mode) ;
153            pa_debug.G_err_code := '0';
154            x_retcode := pa_debug.G_err_code;
155            x_errbuf := NULL;
156            G_rule_id  := p_rule_id ;
157            pa_debug.G_err_stage := 'GETTING NEW RUN ID';
158            IF P_DEBUG_MODE = 'Y' THEN
159               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
160            END IF;
161            SELECT pa_alloc_runs_s.nextval
162              INTO v_run_id
163              FROM dual;
164            G_alloc_run_id := v_run_id;
165            x_run_id := v_run_id;
166            pa_debug.G_err_stage:= 'INITIALIZING WHO COLUMNS';
167            IF P_DEBUG_MODE = 'Y' THEN
168               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
169            END IF;
170            init_who_cols();
171            -- Initialize the global variables for currency information
172            pa_currency.SET_CURRENCY_INFO ;
173            pa_debug.G_err_stage:= 'GETTING CURENCY CODE';
174            IF P_DEBUG_MODE = 'Y' THEN
175               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
176            END IF;
177            G_denom_currency_code := pa_currency.get_currency_code();
178            --  G_lock_name := 'PA_AL-'||to_char(p_rule_id);
179            --  pa_debug.G_err_stage := 'ACQUIRING LOCK';
180            --  pa_debug.write_file( 'LOG', pa_debug.G_err_stage);
181            --  IF (pa_debug.Acquire_User_Lock(G_lock_name) <> 0 ) THEN
182            --    alloc_errors( p_rule_id, x_run_id, 'R', 'E',
183            --                  'PA_AL_CANT_ACQUIRE_LOCK', TRUE );
184            --  END IF;
185            lock_rule ( p_rule_id , x_run_id ) ;
186            pa_debug.G_err_stage := 'GETTING RULE DEFINITION';
187            IF P_DEBUG_MODE = 'Y' THEN
188               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
189            END IF;
190 		   OPEN C_get_rule;
191 		   FETCH C_get_rule INTO v_alloc_rule_rec;
192 		   IF C_get_rule%NOTFOUND THEN
193               alloc_errors( p_rule_id, x_run_id, 'R', 'E','PA_AL_RULE_NOT_FOUND', TRUE);
194            END IF;
195            G_basis_balance_category := v_alloc_rule_rec.basis_balance_category;   /* added bug 2619977 */
196            G_basis_budget_type_code := v_alloc_rule_rec.basis_budget_type_code;   /* added bug 2619977 */
197            G_basis_fin_plan_type_id := v_alloc_rule_rec.basis_fin_plan_type_id;   /* added bug 2619977 */
198 		   G_Org_Id := v_alloc_rule_rec.Org_Id ; /* FP.M : Allocation Impact */
199            CLOSE C_get_rule;
200            pa_client_extn_alloc.check_dependency(p_rule_id, v_status, v_err_message) ;
201            IF nvl(v_status,0) <> 0 then
202               v_err_message:=nvl(v_err_message,'PA_AL_CE_FAILED');
203               IF P_DEBUG_MODE = 'Y' THEN
204                  pa_debug.write_file('allocation_run: ' || 'LOG',v_err_message);
205               END IF;
206               alloc_errors(p_rule_id, x_run_id, 'R', 'E',v_err_message, TRUE) ;
207            END IF ;
208            pa_debug.G_err_stage := 'CHECKING LAST RUN STATUS';
209            IF P_DEBUG_MODE = 'Y' THEN
210               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
211            END IF;
212 		   check_last_run_status (p_rule_id, v_run_id, v_mode );
213 		   G_alloc_run_id := v_run_id;
214            x_run_id := v_run_id;
215            IF ( v_mode = 'RELEASE' AND p_run_mode = 'G' ) THEN
216               v_mode := 'INVALID' ;
217            END IF ;
218            IF(v_mode = 'INVALID') THEN
219               x_retcode:='-1';
220               alloc_errors( p_rule_id, x_run_id, 'R', 'E',
221                 'PA_AL_DRAFT_PENDING_FOR_DEL', TRUE );
222            ELSE
223               G_alloc_run_id := v_run_id;
224               x_run_id := v_run_id;
225            END IF;
226            pa_debug.G_err_stage := 'GET CONCURRENT REQUEST_ID FOR DRAFT';
227            IF P_DEBUG_MODE = 'Y' THEN
228               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
229            END IF;
230            v_request_id:= fnd_global.conc_request_id ;
231            -- 923184 : Getting rule name and request id into a global variable.
232            G_rule_name := v_alloc_rule_rec.rule_name ;
233            G_request_id := v_request_id ;
234            IF (v_mode = 'DRAFT' OR v_mode = 'DELETE') THEN    /* for 2176096 */
235               pa_debug.G_err_stage := 'GET FISCAL YEAR QUARTER for '||p_run_period;
236               IF P_DEBUG_MODE = 'Y' THEN
237                  pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
238               END IF;
239               get_fiscalyear_quarter(  v_alloc_rule_rec.period_type
240                 , p_run_period
241                 , v_period_type
242                 , v_period_set_name
243                 , v_period_year
244                 , v_quarter
245                 , v_period_num
246                 , v_run_period_end_date );
247               pa_debug.G_err_stage := 'INSERTING ALLOC RUNS';
248               IF P_DEBUG_MODE = 'Y' THEN
249                  pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
250               END IF;
251               insert_alloc_runs( x_run_id
252                 , p_rule_id
253                 , p_run_period
254                 , p_expnd_item_date
255                 , G_creation_date
256                 , G_created_by
257                 , G_last_update_date
258                 , G_last_updated_by
259                 , G_last_update_login
260                 , v_alloc_rule_rec.pool_percent
261                 , v_alloc_rule_rec.period_type
262                 , v_alloc_rule_rec.source_amount_type
263                 , v_alloc_rule_rec.source_balance_category
264                 , v_alloc_rule_rec.source_balance_type
265                 , v_alloc_rule_rec.alloc_resource_list_id
266                 , v_alloc_rule_rec.auto_release_flag
267                 , v_alloc_rule_rec.allocation_method
268                 , v_alloc_rule_rec.imp_with_exception
269                 , v_alloc_rule_rec.dup_targets_flag
270                 , v_alloc_rule_rec.target_exp_type_class
271                 , v_alloc_rule_rec.target_exp_org_id
272                 , v_alloc_rule_rec.target_exp_type
273                 , v_alloc_rule_rec.target_cost_type
274                 , v_alloc_rule_rec.offset_exp_type_class
275                 , v_alloc_rule_rec.offset_exp_org_id
276                 , v_alloc_rule_rec.offset_exp_type
277                 , v_alloc_rule_rec.offset_cost_type
278                 , v_alloc_rule_rec.offset_method
279                 , v_alloc_rule_rec.offset_project_id
280                 , v_alloc_rule_rec.offset_task_id
281                 , 'IP' /* In-Process as initial status */
282 				, v_alloc_rule_rec.basis_method
283 				, v_alloc_rule_rec.basis_relative_period
284 				, v_alloc_rule_rec.basis_amount_type
285 				, v_alloc_rule_rec.basis_balance_category
286 		        , v_alloc_rule_rec.basis_budget_type_code
287 			    , v_alloc_rule_rec.basis_balance_type
288 			    , v_alloc_rule_rec.basis_resource_list_id
289 				, v_period_year  /* fiscal_year */
290 				, v_quarter
291 				, v_period_num /* p_period_num  */
292 				, NULL /* p_target_exp_group */
293 				, NULL /* p_offset_exp_group */
294 				, NULL /* p_total_pool_amount  */
295 				, NULL  /* p_allocated_amount */
296 				, NULL /* p_reversal_date */
297 				, v_request_id
298 				, G_sysdate /*p_draft_request_date */
299 				, NULL /* p_release_request_id */
300 				, NULL /* p_release_request_date */
301 				, G_denom_currency_code
302 				, v_alloc_rule_rec.fixed_amount
303 				, NULL  /* rev_target_exp_group */
304 				, NULL  /* rev_offset_exp_group */
305 				, v_alloc_rule_rec.ORG_ID
306 				, v_alloc_rule_rec.limit_target_projects_code
307 			    /* FP.M : Allocation Impact : Bug # 3512552 */
308 				, Null /* p_CINT_RATE_NAME  */
309 				, v_alloc_rule_rec.ALLOC_RESOURCE_STRUCT_TYPE
310 				, v_alloc_rule_rec.BASIS_RESOURCE_STRUCT_TYPE
311 				, v_alloc_rule_rec.ALLOC_RBS_VERSION
312 				, v_alloc_rule_rec.BASIS_RBS_VERSION
313 				);
314            G_tgt_expnd_org:=pa_utils.GetOrgName( v_alloc_rule_rec.target_exp_org_id);
315            G_tgt_expnd_type_class:= v_alloc_rule_rec.target_exp_type_class;
316            G_tgt_expnd_type:=v_alloc_rule_rec.target_exp_type;
317            G_offset_expnd_org:=pa_utils.GetOrgName( v_alloc_rule_rec.offset_exp_org_id);
318            G_offset_expnd_type_class:= v_alloc_rule_rec.offset_exp_type_class;
319            G_offset_expnd_type:=v_alloc_rule_rec.offset_exp_type;
320            pa_debug.G_err_stage := 'VALIDATING RULE';
321            IF P_DEBUG_MODE = 'Y' THEN
322               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
323            END IF;
324            validate_rule( p_rule_id
325              , x_run_id
326              , v_alloc_rule_rec.start_date_active
327              , v_alloc_rule_rec.end_date_active
328              , v_alloc_rule_rec.source_extn_flag
329              , v_alloc_rule_rec.target_extn_flag
330              , v_alloc_rule_rec.target_exp_type_class
331              , v_alloc_rule_rec.target_exp_org_id
332              , v_alloc_rule_rec.target_exp_type
333              , v_alloc_rule_rec.offset_exp_type_class
334              , v_alloc_rule_rec.offset_exp_org_id
335              , v_alloc_rule_rec.offset_exp_type
336              , v_alloc_rule_rec.offset_method
337              , v_alloc_rule_rec.offset_project_id
338              , v_alloc_rule_rec.offset_task_id
339              , v_alloc_rule_rec.basis_method
340              , v_alloc_rule_rec.basis_amount_type
341              , v_alloc_rule_rec.basis_balance_category
342              , v_alloc_rule_rec.basis_budget_type_code
343              , v_alloc_rule_rec.basis_budget_entry_method_code
344              , v_alloc_rule_rec.basis_balance_type
345              , v_alloc_rule_rec.ORG_ID
346              , v_alloc_rule_rec.fixed_amount
347              , p_expnd_item_date );
348            pa_debug.G_err_stage := 'POPULATING RUN SOURCES';
349            IF P_DEBUG_MODE = 'Y' THEN
350               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
351            END IF;
352            populate_run_sources( p_rule_id
353              , x_run_id
354              , v_alloc_rule_rec.alloc_resource_list_id
355              , v_alloc_rule_rec.source_extn_flag
356 			 /* FP.M : Allocation Impact */
357 			 , v_alloc_rule_rec.alloc_resource_struct_type
358 			 , v_alloc_rule_rec.alloc_rbs_version
359 			 );
360            pa_debug.G_err_stage := 'POPULATING RUN TARGETS';
361            IF P_DEBUG_MODE = 'Y' THEN
362               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
363            END IF;
364            populate_run_targets( p_rule_id
365              , x_run_id
366              , v_alloc_rule_rec.basis_method
367              , v_alloc_rule_rec.basis_budget_type_code
368              , v_alloc_rule_rec.basis_budget_entry_method_code
369              , v_alloc_rule_rec.basis_resource_list_id
370              , v_alloc_rule_rec.target_extn_flag
371              , v_alloc_rule_rec.dup_targets_flag
372              , p_expnd_item_date
373              , v_alloc_rule_rec.limit_target_projects_code
374              , v_basis_method
375 			 /* FP.M : ALlocation Impact */
376 			 , v_alloc_rule_rec.basis_resource_struct_type
377 		     , v_alloc_rule_rec.basis_rbs_version
378 			 ) ;
379            /** Basis method of FS and FP may be overwritten by
380                 populate_run_targets.
381             When targets lines are defined by client extension and also
382                 there are lines in the form **/
383            open c_target_det ;
384            fetch c_target_det into  v_dummy ;
385            close c_target_det ;
386            if v_dummy is NULL then
387               alloc_errors( p_rule_id, x_run_id, 'T', 'E',
388                 'PA_AL_NO_TARGET_DETAILS', TRUE,'Y' );
389            end if ;
390            /* raise error NOW if validation failed at any point */
391            pa_debug.G_err_stage := 'CHECKING FOR FATAL ERRORS';
392            IF P_DEBUG_MODE = 'Y' THEN
393               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
394            END IF;
395            IF (G_fatal_err_found) THEN
396               G_fatal_err_found:= FALSE;
397               COMMIT;
398               alloc_errors( p_rule_id, x_run_id, 'R', 'E',
399                 'PA_AL_FATAL_ERROR_FOUND', TRUE,'N' );
400            END IF;
401            COMMIT ;  /* Introduced as part of commit cycle changes */
402            pa_debug.G_err_stage := 'CALCULATE SRC PROJECT AMOUNTS';
403            IF P_DEBUG_MODE = 'Y' THEN
404               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
405            END IF;
406            cal_amounts_from_projects(  p_rule_id
407              , x_run_id
408              , v_alloc_rule_rec.period_type
409              , v_alloc_rule_rec.source_amount_type
410              , p_run_period
411              , v_alloc_rule_rec.source_balance_type
412              , v_alloc_rule_rec.alloc_resource_list_id
413              , v_alloc_rule_rec.pool_percent
414              , v_alloc_rule_rec.fixed_amount
415              , v_src_amount_from_proj
416 			 /* FP.M : Allocation Impact : Bug # 3512552 */
417 			 , v_alloc_rule_rec.ALLOC_RESOURCE_STRUCT_TYPE
418 			 , v_alloc_rule_rec.ALLOC_RBS_VERSION
419 			 );
420            pa_debug.G_err_stage := 'CALCULATE SRC GL AMOUNTS';
421            IF P_DEBUG_MODE = 'Y' THEN
422               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
423            END IF;
424            calculate_src_GL_amounts( p_rule_id
425              , x_run_id
426              , p_run_period
427              , v_alloc_rule_rec.source_amount_type
428              , v_src_amount_from_GL );
429            IF P_DEBUG_MODE = 'Y' THEN
430               pa_debug.write_file('allocation_run: ' || 'LOG', 'v_src_amount_from_GL: '||to_char(v_src_amount_from_GL) );
431            END IF;
432            If v_alloc_rule_rec.allocation_method = 'F' then
433               If (nvl(v_src_amount_from_proj,0) + nvl(v_src_amount_from_GL,0)) = 0  then
434                   v_pazspa :='1' ;
435                  alloc_errors( p_rule_id, x_run_id, 'R', 'E',
436                    'PA_AL_ZERO_SOURCE_POOL_AMOUNT', TRUE );
437                    /*Return; reverting changes done in 5598267 for bug 9789612*/
438               End if ;
439            End If ;
440            pa_debug.G_err_stage := 'CALCULATE BASIS PROJECT AMOUNTS';
441            IF P_DEBUG_MODE = 'Y' THEN
442               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
443            END IF;
444            /* 2182563 - v_basis_method is a out variable now. If no records are found
445               for basis method 'P' or 'C' then it is changed to 'S'. */
446            cal_proj_basis_amounts( p_rule_id
447              , x_run_id
448              , v_alloc_rule_rec.period_type
449              , p_run_period
450              , v_basis_method
451              , v_alloc_rule_rec.basis_amount_type
452              , v_alloc_rule_rec.basis_balance_type
453              , v_alloc_rule_rec.basis_relative_period
454              , v_alloc_rule_rec.basis_balance_category
455              , v_alloc_rule_rec.basis_resource_list_id
456              , v_alloc_rule_rec.basis_budget_type_code
457              , v_bas_amount_from_proj
458 			 /* FP.M : Allocation Impact : Bug# 3512552 */
459             , v_alloc_rule_rec.BASIS_RESOURCE_STRUCT_TYPE
460 			, v_alloc_rule_rec.BASIS_RBS_VERSION
461 			 );
462            COMMIT ;  /* Introduced as part of commit cycle changes */
463            pa_debug.G_err_stage := 'CREATING TARGET TRANSACTIONS';
464            IF P_DEBUG_MODE = 'Y' THEN
465               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
466            END IF;
467            create_target_txns( p_rule_id
468              , x_run_id
469              , v_alloc_rule_rec.period_type
470              , v_period_year
471              , v_quarter
472              , v_period_num
473              , v_run_period_end_date
474              , v_alloc_rule_rec.target_exp_type
475              , v_alloc_rule_rec.allocation_method
476              , v_basis_method
477              , v_alloc_rule_rec.source_amount_type
478              , nvl(v_src_amount_from_proj,0) + nvl(v_src_amount_from_GL,0)
479              , v_curr_alloc_amount );
480            IF (v_alloc_rule_rec.offset_method <> 'N') THEN
481               pa_debug.G_err_stage := 'CREATING OFFSET TRANSACTIONS';
482               IF P_DEBUG_MODE = 'Y' THEN
483                  pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
484               END IF;
485               create_offset_txns( p_rule_id
486                 , x_run_id
487                 , v_alloc_rule_rec.period_type
488                 , v_period_year
489                 , v_quarter
490                 , v_period_num
491                 , v_run_period_end_date
492                 , v_alloc_rule_rec.offset_exp_type
493                 , v_alloc_rule_rec.allocation_method
494                 , v_alloc_rule_rec.offset_method
495                 , v_alloc_rule_rec.offset_project_id
496                 , v_alloc_rule_rec.offset_task_id
497                 , v_alloc_rule_rec.source_amount_type
498                 , nvl(v_src_amount_from_proj,0) + nvl(v_src_amount_from_GL,0)
499                 , v_curr_alloc_amount );
500            END IF;
501         END IF;    /* v_mode = draft */
502            COMMIT ;  /* Introduced as part of commit cycle changes */
503            IF (v_mode = 'RELEASE' OR v_alloc_rule_rec.auto_release_flag ='Y') THEN
504            v_mode := 'RELEASE' ;
505            pa_debug.G_err_stage := 'RELEASING THE RUN';
506            IF P_DEBUG_MODE = 'Y' THEN
507               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
508            END IF;
509            pa_alloc_run.release_alloc_txns( p_rule_id, x_run_id ,x_retcode,
510              x_errbuf );
511            END IF;    /* v_mode = release */
512            if x_errbuf is NOT NULL then
513            alloc_errors( p_rule_id, x_run_id, 'R', 'E',
514            x_errbuf, TRUE );
515            End if ;
516            --
517            --
518            --    Bug: 983057  Do not create txn with zero curren alloc amount
519            --   If no txns created set the allocation run status as release success
520            --
521            IF (G_num_txns = 0 )  THEN
522               v_mode := 'RELEASE' ;
523               IF P_DEBUG_MODE = 'Y' THEN
524                  pa_debug.write_file('allocation_run: ' ||  'LOG', 'All tasks received zero amounts. No transactions created');
525               END IF;
526               alloc_errors( p_rule_id, x_run_id,'R','W','PA_AL_ALL_TASKS_RECD_ZERO_AMT', FALSE) ;
527            END IF ;
528 
529         if x_errbuf is null then  /* added if clause for 6243121 */
530  	    UPDATE pa_alloc_runs
531  	       SET run_status = DECODE( v_mode, 'RELEASE', 'RS', 'DS'),
532  	           release_request_id = decode(v_mode, 'RELEASE',v_request_id, NULL),
533  	           release_request_date = decode(v_mode, 'RELEASE',sysdate, NULL)
534  	     WHERE run_id = x_run_id;
535 
536            pa_debug.G_err_stage := 'UPDATING RUN STATUS AS SUCCESS';
537 
538            IF P_DEBUG_MODE = 'Y' THEN
539               pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
540            END IF;
541 
542 	   /* added for 6243121 */
543  	 else         -- if any error/exception is encountered while releasing
544            UPDATE pa_alloc_runs
545              SET run_status = DECODE( v_mode, 'RELEASE', 'RF', 'DF'),
546              release_request_id = decode(v_mode, 'RELEASE',v_request_id, NULL),
547              release_request_date = decode(v_mode, 'RELEASE',sysdate, NULL)
548              WHERE run_id = x_run_id;
549 
550 	   pa_debug.G_err_stage := 'UPDATED RUN STATUS AS FAILURE';
551  	     IF P_DEBUG_MODE = 'Y' THEN
552  	        pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
553  	     END IF;
554  	  end if;
555  	      /* added for 6243121 */
556 
557            pa_debug.G_err_stage := 'RELEASING LOCK';
558            --  pa_debug.write_file( 'LOG', pa_debug.G_err_stage);
559            --  IF (pa_debug.Release_User_Lock(G_lock_name) <> 0 ) THEN
560            --    alloc_errors( p_rule_id, x_run_id, 'R', 'E',
561            --                  'PA_AL_LOCK_RELEASE_FAILED', TRUE );
562            --  END IF;
563            unlock_rule(p_rule_id, x_run_id) ;
564            COMMIT;
565            /* restore the old stack */
566            pa_debug.reset_err_stack;
567     EXCEPTION
568        WHEN OTHERS THEN
569          x_errbuf:= pa_debug.G_err_stage|| ': '||sqlerrm;
570          x_run_id:= G_alloc_run_id;
571          IF P_DEBUG_MODE = 'Y' THEN
572             pa_debug.write_file('allocation_run: ' ||  'LOG', x_errbuf);
573             pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stack);
574          END IF;
575          pa_debug.G_err_stage := 'UPDATING RUN STATUS AS FAILURE';
576          IF P_DEBUG_MODE = 'Y' THEN
577             pa_debug.write_file('allocation_run: ' ||  'LOG', pa_debug.G_err_stage);
578          END IF;
579          UPDATE pa_alloc_runs
580            SET run_status = DECODE( v_mode, 'RELEASE', 'RF',
581            'DRAFT',   'DF',
582            run_status ),
583            release_request_id = decode(v_mode, 'RELEASE',v_request_id, NULL),
584            release_request_date = decode(v_mode, 'RELEASE',sysdate, NULL)
585            WHERE run_id = x_run_id;
586          IF P_DEBUG_MODE = 'Y' THEN
587             pa_debug.write_file('allocation_run: ' ||  'LOG', 'x_run_id in when others of Main: '||to_char( x_run_id) );
588          END IF;
589          COMMIT;
590         /* 2841843 Marking Conc request as completed failure
591 	   Explicitely not raising the exception as all debug messages
592 	   are retrived in "After report" trigger of the report. */
593 
594       if v_pazspa is null then  /* added if condition for bug 12356188  */
595        completion_status  := fnd_concurrent.set_completion_status('ERROR', SQLERRM);
596       end if;
597 
598     END allocation_run;
599 -- ------------------------------------------------------------
600 -- check_last_run_status
601 -- ------------------------------------------------------------
602 PROCEDURE check_last_run_status( p_rule_id     IN  NUMBER
603                                , x_run_id      IN OUT NOCOPY NUMBER
604                                , x_mode        OUT NOCOPY VARCHAR2 ) IS
605 CURSOR C_run_id IS
606   SELECT max(run_id)
607   FROM pa_alloc_runs
608   WHERE rule_id = p_rule_id
609   AND   run_id <> x_run_id;
610 CURSOR C_prev_run( p_prev_run_id IN NUMBER) IS
611   SELECT run_status
612   FROM pa_alloc_runs
613   WHERE run_id = p_prev_run_id ;
614  v_run_status VARCHAR2(5) := NULL;
615  v_prev_run_id NUMBER;
616 BEGIN
617   pa_debug.set_err_stack('check_last_run_status');
618   pa_debug.G_err_code := 0;
619   OPEN C_run_id;
620   FETCH C_run_id INTO v_prev_run_id;
621   CLOSE C_run_id;
622   IF v_prev_run_id IS NULL THEN
623   /* a draft does not exists, so draft mode */
624     x_mode := 'DRAFT';
625   ELSE  /* a draft exists, check the status */
626     OPEN C_prev_run( v_prev_run_id);
627     FETCH C_prev_run INTO v_run_status;
628     CLOSE C_prev_run;
629       IF ( v_run_status = 'RS' OR v_run_status ='RV')  THEN
630         x_mode := 'DRAFT';
631       ELSIF (v_run_status = 'RF' OR v_run_status ='DS') THEN
632         x_mode := 'RELEASE';
633                x_run_id := v_prev_run_id;
634       /* added for bug 2176096 */
635       ELSIF (v_run_status = 'DL' ) THEN
636         x_mode := 'DELETE';
637 --      ELSIF (v_run_status = 'DF') THEN
638 --        x_mode := 'INVALID';
639 --             x_run_id := v_prev_run_id;
640       ELSE
641         x_mode := 'INVALID';
642       END IF;  /* end if v_run_status */
643   END IF; /* end if v_prev_run_id */
644 /*
645   IF (x_mode = 'RELEASE') THEN
646          x_run_id := v_prev_run_id;
647   END IF ;
648 */
649   /* restore the old stack */
650   pa_debug.reset_err_stack;
651 EXCEPTION
652   WHEN OTHERS THEN
653     pa_debug.G_err_code := SQLCODE;
654     RAISE;
655 END check_last_run_status;
656 -- ------------------------------------------------------------
657 -- ins_alloc_exceptions
658 -- ------------------------------------------------------------
659 PROCEDURE ins_alloc_exceptions (  p_rule_id           IN NUMBER
660                                 , p_run_id            IN NUMBER
661                                 , p_creation_date     IN DATE
662                                 , p_created_by        IN NUMBER
663                                 , p_last_updated_date IN DATE
664                                 , p_last_updated_by   IN NUMBER
665                                 , p_last_update_login IN NUMBER
666                                 , p_level_code        IN VARCHAR2
667                                 , p_exception_type    IN VARCHAR2
668                                 , p_project_id        IN NUMBER
669                                 , p_task_id           IN NUMBER
670                                 , p_exception_code    IN VARCHAR2 ) IS
671 BEGIN
672   pa_debug.set_err_stack('ins_alloc_exceptions');
673   INSERT INTO pa_alloc_exceptions (
674     RUN_ID
675   , RULE_ID
676   , LEVEL_CODE
677   , EXCEPTION_TYPE
678   , CREATION_DATE
679   , CREATED_BY
680   , LAST_UPDATE_DATE
681   , LAST_UPDATED_BY
682   , LAST_UPDATE_LOGIN
683   , PROJECT_ID
684   , TASK_ID
685   , EXCEPTION_CODE )
686   VALUES (
687     p_run_id
688   , p_rule_id
689   , p_level_code
690   , p_exception_type
691   , p_creation_date
692   , p_created_by
693   , p_last_updated_date
694   , p_last_updated_by
695   , p_last_update_login
696   , p_project_id
697   , p_task_id
698   , p_exception_code );
699   /*  restore the old stack */
700   pa_debug.reset_err_stack;
701 EXCEPTION
702   WHEN OTHERS THEN
703   pa_debug.G_err_code := SQLCODE;
704   RAISE;
705 END ins_alloc_exceptions;
706 -- ------------------------------------------------------------
707 -- alloc_errors:
708 -- ------------------------------------------------------------
709 PROCEDURE alloc_errors( p_rule_id IN NUMBER
710                       , p_run_id  IN NUMBER
711                       , p_level   IN VARCHAR2
712                       , p_type    IN VARCHAR2
713                       , p_mesg_code   IN VARCHAR2
714                       , p_fatal_err   IN BOOLEAN  DEFAULT FALSE
715                       , p_insert_flag IN VARCHAR2 DEFAULT 'Y'
716                       , p_project_id  IN NUMBER   DEFAULT NULL
717                       , p_task_id     IN NUMBER   DEFAULT NULL ) IS
718 v_mesg_code VARCHAR2(30);
719 BEGIN
720   v_mesg_code := SUBSTR( p_mesg_code,1,30);
721   pa_debug.set_err_stack('alloc_errors');
722   IF ( p_insert_flag = 'Y') THEN
723     ins_alloc_exceptions( p_rule_id, p_run_id, G_creation_date,
724                           G_created_by, G_last_update_date,
725                           G_last_updated_by, G_last_update_login,
726                           p_level, p_type, p_project_id, p_task_id, v_mesg_code );
727   END IF;
728   IF (p_fatal_err) THEN
729 	           /*Return; reverting changes done in 5598267 for bug 9789612 */
730  	           pa_debug.raise_error( -20010, p_mesg_code );
731   END IF;
732   pa_debug.reset_err_stack;
733 EXCEPTION
734   WHEN OTHERS THEN
735     pa_debug.G_err_code := SQLCODE;
736     RAISE;
737 END alloc_errors;
738 -- ----------------------------------------------------------------------
739 -- Validate_rule
740 -- ----------------------------------------------------------------------
741 PROCEDURE validate_rule( p_rule_id               IN NUMBER
742                        , p_run_id                IN NUMBER
743                        , p_start_date_active     IN DATE
744                        , p_end_date_active       IN DATE
745                        , p_source_extn_flag      IN VARCHAR2
746                        , p_target_extn_flag      IN VARCHAR2
747                        , p_target_exp_type_class IN VARCHAR2
748                        , p_target_exp_org_id     IN NUMBER
749                        , p_target_exp_type       IN VARCHAR2
750                        , p_offset_exp_type_class IN VARCHAR2
751                        , p_offset_exp_org_id     IN NUMBER
752                        , p_offset_exp_type       IN VARCHAR2
753                        , p_offset_method         IN VARCHAR2
754                        , p_offset_project_id     IN NUMBER
755                        , p_offset_task_id        IN NUMBER
756                        , p_basis_method             IN VARCHAR2
757                        , p_basis_amount_type        IN VARCHAR2
758                        , p_basis_balance_category   IN VARCHAR2
759                        , p_bas_budget_type_code     IN VARCHAR2
760                        , p_bas_bdgt_entry_mthd_code IN VARCHAR2
761                        , p_basis_balance_type       IN VARCHAR2
762                        , p_org_id                IN NUMBER
763                        , p_fixed_amount          IN NUMBER
764                        , p_expnd_item_date       IN DATE ) IS
765 CURSOR C_pa_src_exists IS
766   SELECT 1
767   FROM pa_alloc_source_lines
768   WHERE rule_id = p_rule_id ;
769 CURSOR C_gl_src_exists IS
770   SELECT 1
771   FROM pa_alloc_GL_lines
772   WHERE rule_id = p_rule_id ;
773 CURSOR C_target_lines IS
774   SELECT project_id, task_id, exclude_flag, billable_only_flag
775   FROM pa_alloc_target_lines
776   WHERE rule_id = p_rule_id;
777 CURSOR C_pa_trg_exists IS
778   SELECT 1
779   FROM pa_alloc_target_lines
780   WHERE rule_id = p_rule_id;
781 CURSOR C_check_task( p_project_id IN NUMBER
782                    , p_task_id    IN NUMBER
783                                                  , p_expnd_item_date IN DATE ) IS
784 SELECT 1
785   FROM pa_tasks pt
786   WHERE pt.task_id = p_task_id
787   AND nvl(pt.chargeable_flag, 'N') = 'Y'
788   AND ( trunc(p_expnd_item_date) BETWEEN  trunc(nvl(pt.start_date,p_expnd_item_date))
789          AND trunc(NVL(pt.completion_date, p_expnd_item_date)) )
790   AND ( pa_project_utils.check_project_action_allowed( p_project_id, 'NEW_TXNS') = 'Y')
791   AND (pa_project_stus_utils.is_project_closed( p_project_id )= 'N');
792 CURSOR C_exptype_exists(p_exp_type IN VARCHAR2) IS
793   SELECT 1
794   FROM pa_expenditure_types
795   WHERE expenditure_type = p_exp_type
796   AND TRUNC(G_sysdate) BETWEEN TRUNC(start_date_active)
797                 AND TRUNC(nvl(end_date_active, G_sysdate));
798 CURSOR C_fixed_percent IS
799   SELECT nvl(sum(NVL(line_percent,0)),0)
800   FROM pa_alloc_target_lines
801   WHERE rule_id = p_rule_id;
802 CURSOR C_billable_task( p_task_id            IN NUMBER
803                       , p_billable_only_flag IN VARCHAR2 ) IS
804   SELECT 1
805   FROM pa_tasks
806   WHERE task_id = p_task_id
807   AND billable_flag = p_billable_only_flag;
808 v_src_error_found BOOLEAN;
809 v_gl_error_found BOOLEAN;
810 v_dummy NUMBER;
811 -- v_task_id  NUMBER;
812 -- v_billable_only_flag VARCHAR2(1);
813 BEGIN
814   pa_debug.set_err_stack('validate_rule');
815   pa_debug.G_err_code:= 0;
816   pa_debug.G_err_stage:= 'CHECKING DATE EFFECTIVITY OF RULE';
817   IF P_DEBUG_MODE = 'Y' THEN
818      pa_debug.write_file('validate_rule: ' ||  'LOG', pa_debug.G_err_stage);
819   END IF;
820   IF ( TRUNC(p_start_date_active) > TRUNC(G_sysdate) OR
821       TRUNC(p_end_date_active) <TRUNC(G_sysdate) ) THEN
822     G_fatal_err_found := TRUE;
823     alloc_errors( p_rule_id, p_run_id, 'R', 'E',
824                   'PA_AL_RULE_INACTIVE');
825   END IF;
826   pa_debug.G_err_stage:= 'CHECKING SRC_LINES DEFINITON';
827   IF P_DEBUG_MODE = 'Y' THEN
828      pa_debug.write_file('validate_rule: ' ||  'LOG', pa_debug.G_err_stage);
829   END IF;
830   IF(p_fixed_amount IS NULL) THEN
831     IF (p_source_extn_flag = 'N') THEN
832       OPEN C_pa_src_exists;
833       FETCH C_pa_src_exists INTO v_dummy;
834        IF C_pa_src_exists%NOTFOUND THEN
835           v_src_error_found := TRUE;
836         END IF;
837       CLOSE C_pa_src_exists;
838       OPEN C_gl_src_exists;
839       FETCH C_gl_src_exists INTO v_dummy;
840         IF C_gl_src_exists%NOTFOUND THEN
841           v_GL_error_found := TRUE;
842         ELSE
843           v_GL_error_found := FALSE;
844         END IF;
845       CLOSE C_gl_src_exists ;
846       IF( v_GL_error_found AND v_src_error_found ) THEN
847         G_fatal_err_found := TRUE;
848         alloc_errors( p_rule_id, p_run_id, 'R', 'E',
849                       'PA_AL_NO_SOURCE_LINES');
850       END IF;  /* if v_gl_err AND v_src_err */
851     END IF;  /* if p_source_extn_flag */
852   END IF;  /* if p_fixed_amount */
853   pa_debug.G_err_stage:= 'CHECKING TARGET LINES';
854   IF P_DEBUG_MODE = 'Y' THEN
855      pa_debug.write_file('validate_rule: ' ||  'LOG', pa_debug.G_err_stage);
856   END IF;
857   IF( p_target_extn_flag = 'N') THEN
858     v_dummy:= 0;
859     OPEN C_pa_trg_exists ;
860     FETCH C_pa_trg_exists INTO v_dummy;
861     IF (C_pa_trg_exists%NOTFOUND) THEN
862           G_fatal_err_found := TRUE;
863           alloc_errors( p_rule_id, p_run_id, 'R', 'E',
864                         'PA_AL_NO_TARGET_LINES');
865     END IF;   /* if c_pa_trg_exists */
866     CLOSE C_pa_trg_exists;
867     /* if target lines exists and basis_method = Fixed_prorate
868                  or Fixed_SpreadEvenly */
869     IF (v_dummy <> 0 AND p_basis_method IN ( 'FP', 'FS') ) THEN
870       OPEN C_fixed_percent;
871       FETCH C_fixed_percent INTO v_dummy;
872         IF (v_dummy <>100) THEN
873           G_fatal_err_found := TRUE;
874           alloc_errors( p_rule_id, p_run_id, 'R', 'E',
875                         'PA_AL_LINE_PRCNT_NOT_100');
876         END IF;
877       CLOSE C_fixed_percent;
878     END IF; /* if basis_method */
879   END IF;   /* if p_target_extn_flag */
880   FOR target_line_rec IN C_target_lines LOOP
881     /* check if task is billable */
882     IF ( target_line_rec.task_id IS NOT NULL AND target_line_rec.exclude_flag = 'N') THEN
883       /* check that the proj-task combination is valid and chargeable */
884       OPEN C_check_task( target_line_rec.project_id
885                        , target_line_rec.task_id
886                                                           , p_expnd_item_date );
887       FETCH C_check_task INTO v_dummy;
888       IF C_check_task%NOTFOUND THEN
889         G_fatal_err_found := TRUE;
890         alloc_errors( p_rule_id, p_run_id, 'R', 'E',
891                       'PA_AL_NOT_A_CHARGEABLE_TASK',
892                        FALSE, 'Y',
893                        target_line_rec.project_id,
894                        target_line_rec.task_id );
895       END IF;
896       CLOSE C_check_task;
897       IF ( target_line_rec.billable_only_flag = 'Y' ) THEN
898         OPEN C_billable_task ( target_line_rec.task_id
899                              , target_line_rec.billable_only_flag );
900         FETCH C_billable_task INTO v_dummy;
901         IF C_billable_task%NOTFOUND THEN
902           G_fatal_err_found := TRUE;
903           alloc_errors( p_rule_id, p_run_id, 'R', 'E',
904                         'PA_AL_TASK_NON_BILLABLE',
905                                                                  FALSE, 'Y',
906                          target_line_rec.project_id,
907                          target_line_rec.task_id );
908         END IF;
909         CLOSE C_billable_task ;
910       END IF;  /* billable_flag */
911     END IF; /* task_id IS NOT NULL */
912   END LOOP;  /* C_target_lines */
913   pa_debug.G_err_stage:= 'VALIDATING OFFSET METHOD';
914   IF P_DEBUG_MODE = 'Y' THEN
915      pa_debug.write_file('validate_rule: ' ||  'LOG', pa_debug.G_err_stage);
916   END IF;
917   IF(p_offset_method IN ( 'A', 'B')) THEN
918     /* if GL_source_lines exists or p_fixed_amount exists
919     then error out */
920     IF (v_GL_error_found = FALSE OR p_fixed_amount IS NOT NULL) THEN
921         G_fatal_err_found := TRUE;
922         alloc_errors( p_rule_id, p_run_id, 'R', 'E',
923                       'PA_AL_INVALID_OFFSET_METHOD');
924     END IF;
925   END IF; /* if p_offset_method */
926   IF (p_offset_method <> 'N') THEN
927     pa_debug.G_err_stage:= 'CHECKING OFFSET LINES';
928     IF P_DEBUG_MODE = 'Y' THEN
929        pa_debug.write_file('validate_rule: ' ||  'LOG', pa_debug.G_err_stage);
930     END IF;
931     IF(p_offset_method = 'C') THEN      /* specific offset proj/task */
932       OPEN C_check_task(p_offset_project_id, p_offset_task_id, p_expnd_item_date);
933       FETCH C_check_task INTO v_dummy;
934         IF C_check_task%NOTFOUND THEN
935         G_fatal_err_found := TRUE;
936         alloc_errors( p_rule_id, p_run_id, 'R', 'E',
937                       'PA_AL_INVALID_OFFSET_DEFN');
938         END IF;
939       CLOSE C_check_task;
940     END IF;   /* endif offset_method */
941   END IF;  /* p_offset_method <> */
942   IF(p_basis_method NOT IN ( 'S', 'C')) THEN
943     pa_debug.G_err_stage:= 'CHECKING BASIS LINES';
944     IF P_DEBUG_MODE = 'Y' THEN
945        pa_debug.write_file('validate_rule: ' ||  'LOG', pa_debug.G_err_stage);
946     END IF;
947     IF (p_basis_method IN( 'P', 'FP')) THEN
948       IF (p_basis_balance_category IS NULL) THEN
949         G_fatal_err_found := TRUE;
950         alloc_errors( p_rule_id, p_run_id, 'R', 'E',
951                       'PA_AL_NO_BASIS_BAL_CATGRY');
952       END IF ;
953       IF (p_basis_amount_type IS NULL) THEN
954           G_fatal_err_found := TRUE;
955           alloc_errors( p_rule_id, p_run_id, 'R', 'E',
956                         'PA_AL_NO_BASIS_AMNT_TYPE');
957       END IF ;
958       IF ( p_basis_balance_type IS NULL) THEN
959           G_fatal_err_found := TRUE;
960           alloc_errors( p_rule_id, p_run_id, 'R', 'E',
961                         'PA_AL_NO_BASIS_BAL_TYPE');
962       END IF;
963       IF ( p_basis_balance_category = 'B') THEN
964         IF ( p_bas_budget_type_code IS NULL) THEN
965           G_fatal_err_found := TRUE;
966           alloc_errors( p_rule_id, p_run_id, 'R', 'E',
967                         'PA_AL_NO_BASIS_BDGT_TYP_CODE');
968         END IF;
969       END IF;  /* if p_basis_balance_category */
970       IF ( p_basis_balance_category = 'F') THEN  /* added validation for FP_type_id bug 2619977 */
971         IF ( G_basis_fin_plan_type_id IS NULL) THEN
972           G_fatal_err_found := TRUE;
973           alloc_errors( p_rule_id, p_run_id, 'R', 'E',
974                         'PA_AL_NO_BASIS_FP_TYPE_ID');
975         END IF;
976       END IF;  /* if p_basis_balance_category */
977     END IF; /* if p_basis_method = 'P' or 'FP' */
978   END IF; /* if basis_method NOT IN  */
979   pa_debug.G_err_stage:= 'VALIDATING TARGET EXP ORGANIZATION';
980   IF P_DEBUG_MODE = 'Y' THEN
981      pa_debug.write_file('validate_rule: ' ||  'LOG', pa_debug.G_err_stage);
982   END IF;
983   IF( p_target_exp_org_id IS NULL OR
984       pa_utils2.CheckExpOrg( p_target_exp_org_id) = 'N') THEN
985     G_fatal_err_found := TRUE;
986     alloc_errors( p_rule_id, p_run_id, 'R', 'E',
987                   'PA_AL_INVALID_TARGET_EXP_ORG') ;
988   END IF;
989   pa_debug.G_err_stage:= 'VALIDATING TARGET EXPENDITURE TYPE';
990   IF P_DEBUG_MODE = 'Y' THEN
991      pa_debug.write_file('validate_rule: ' ||  'LOG', pa_debug.G_err_stage);
992   END IF;
993   IF(p_target_exp_type IS NOT NULL) THEN
994     OPEN C_exptype_exists(p_target_exp_type);
995     FETCH C_exptype_exists INTO v_dummy;
996       IF C_exptype_exists%NOTFOUND THEN
997         G_fatal_err_found := TRUE;
998         alloc_errors( p_rule_id, p_run_id, 'R', 'E',
999                       'PA_AL_INVALID_TARGET_EXP_TYPE') ;
1000       END IF;
1001     CLOSE C_exptype_exists;
1002   ELSE
1003     G_fatal_err_found := TRUE;
1004     alloc_errors( p_rule_id, p_run_id, 'R', 'E',
1005                       'PA_AL_INVALID_TARGET_EXP_TYPE') ;
1006   END IF; /* if p_target_exp_type */
1007   IF (p_offset_method <> 'N') THEN
1008     pa_debug.G_err_stage:= 'VALIDATING OFFSET EXP ORGANIZATION';
1009     IF P_DEBUG_MODE = 'Y' THEN
1010        pa_debug.write_file('validate_rule: ' ||  'LOG', pa_debug.G_err_stage);
1011     END IF;
1012     IF( p_offset_exp_org_id IS NULL ) THEN
1013       G_fatal_err_found := TRUE;
1014       alloc_errors( p_rule_id, p_run_id, 'R', 'E',
1015                         'PA_AL_UNDEFINED_OFFSET_EXP_ORG') ;
1016     ELSIF(pa_utils2.CheckExpOrg( p_offset_exp_org_id) = 'N') THEN
1017       G_fatal_err_found := TRUE;
1018       alloc_errors( p_rule_id, p_run_id, 'R', 'E',
1019                         'PA_AL_INVALID_OFFSET_EXP_ORG') ;
1020     END IF;
1021     pa_debug.G_err_stage:= 'VALIDATING OFFSET EXPENDITURE TYPE';
1022     IF P_DEBUG_MODE = 'Y' THEN
1023        pa_debug.write_file('validate_rule: ' ||  'LOG', pa_debug.G_err_stage);
1024     END IF;
1025          IF ( p_offset_exp_type IS NOT NULL) THEN
1026       OPEN C_exptype_exists(p_offset_exp_type);
1027       FETCH C_exptype_exists INTO v_dummy;
1028         IF C_exptype_exists%NOTFOUND THEN
1029           G_fatal_err_found := TRUE;
1030           alloc_errors( p_rule_id, p_run_id, 'R', 'E',
1031                         'PA_AL_INVALID_OFFSET_EXP_TYPE') ;
1032         END IF ;
1033       CLOSE C_exptype_exists;
1034     ELSE
1035       G_fatal_err_found := TRUE;
1036       alloc_errors( p_rule_id, p_run_id, 'R', 'E',
1037                     'PA_AL_UNDEFINED_OFFSET_EXP_TYPE');
1038          END IF; /* if p_offset_exp_type */
1039   END IF; /* p_offset_method */
1040   /* restore the old G_err_stack */
1041   pa_debug.reset_err_stack;
1042 EXCEPTION
1043   WHEN OTHERS THEN
1044     pa_debug.G_err_code := SQLCODE;
1045     RAISE;
1046 END validate_rule;
1047 -- ------------------------------------------------------------
1048 -- insert_alloc_run_sources
1049 -- ------------------------------------------------------------
1050 PROCEDURE insert_alloc_run_sources( p_rule_id           IN NUMBER
1051                                   , p_run_id            IN NUMBER
1052                                   , p_line_num          IN NUMBER
1053                                   , p_project_id        IN NUMBER
1054                                   , p_task_id           IN NUMBER
1055                                   , p_exclude_flag      IN VARCHAR2
1056                                   , p_creation_date     IN DATE
1057                                   , p_created_by        IN NUMBER
1058                                   , p_last_update_date  IN DATE
1059                                   , p_last_updated_by   IN NUMBER
1060                                   , p_last_update_login IN NUMBER) IS
1061 CURSOR source_exists IS
1062   SELECT 1
1063   FROM pa_alloc_run_sources
1064   WHERE run_id = p_run_id
1065   AND project_id = p_project_id
1066   AND task_id = p_task_id;
1067 v_dummy NUMBER;
1068 allow_insert_flag VARCHAR2(1);
1069 BEGIN
1070   pa_debug.set_err_stack('ins_alloc_run_sources');
1071   pa_debug.G_err_code:= 0;
1072   OPEN source_exists;
1073   FETCH source_exists INTO v_dummy;
1074     IF source_exists%NOTFOUND THEN
1075                 /* allow insert if current proj-task not exists */
1076            allow_insert_flag := 'Y';
1077     ELSE
1078            allow_insert_flag := 'N';
1079                 alloc_errors( p_rule_id, p_run_id, 'S', 'W',
1080                     'PA_AL_DUP_SRC_PROJ_TASK', FALSE, 'Y',
1081                                                   p_project_id, p_task_id );
1082     END IF;
1083   CLOSE source_exists;
1084   IF (allow_insert_flag = 'Y') THEN
1085     INSERT INTO PA_ALLOC_RUN_SOURCES (
1086        RUN_ID
1087      , RULE_ID
1088      , LINE_NUM
1089      , PROJECT_ID
1090      , EXCLUDE_FLAG
1091      , CREATION_DATE
1092      , CREATED_BY
1093      , LAST_UPDATE_DATE
1094      , LAST_UPDATED_BY
1095      , LAST_UPDATE_LOGIN
1096      , TASK_ID )
1097     VALUES (
1098        p_run_id
1099      , p_rule_id
1100      , p_line_num
1101      , p_project_id
1102      , p_exclude_flag
1103      , p_creation_date
1104      , p_created_by
1105      , p_last_update_date
1106      , p_last_updated_by
1107      , p_last_update_login
1108      , p_task_id );
1109   END IF;
1110  /*  restore the old stack */
1111  pa_debug.reset_err_stack;
1112 EXCEPTION
1113    WHEN OTHERS THEN
1114      pa_debug.G_err_code := SQLCODE;
1115      RAISE;
1116 END insert_alloc_run_sources;
1117 -- ------------------------------------------------------------
1118 -- exclude_curr_proj_task
1119 -- ------------------------------------------------------------
1120 FUNCTION exclude_curr_proj_task ( p_run_id     IN NUMBER
1121                                 , p_type       IN VARCHAR2
1122                                 , p_project_id IN NUMBER
1123                                 , p_task_id    IN NUMBER ) RETURN NUMBER IS
1124 CURSOR C_excl_src_proj_task IS
1125   SELECT project_id, task_id
1126   FROM pa_alloc_run_sources
1127   WHERE run_id = p_run_id
1128   AND exclude_flag = 'Y'
1129   AND project_id = p_project_id
1130   AND NVL(task_id, NVL(p_task_id, -1)) = NVL(p_task_id, -1);
1131 CURSOR C_excl_trg_proj_task IS
1132   SELECT project_id, task_id
1133   FROM pa_alloc_run_targets
1134   WHERE run_id = p_run_id
1135   AND exclude_flag = 'Y'
1136   AND project_id = p_project_id
1137   AND NVL(task_id, NVL(p_task_id, -1)) = NVL(p_task_id, -1);
1138 v_dummy1 NUMBER;
1139 v_dummy2 NUMBER;
1140 v_status NUMBER:= 0;
1141 BEGIN
1142  pa_debug.set_err_stack('exclude_curr_proj_task');
1143  pa_debug.G_err_code:= 0;
1144  pa_debug.G_err_stage := 'DETERMINE EXCL_CURR_PROJ_TASK';
1145  IF P_DEBUG_MODE = 'Y' THEN
1146     pa_debug.write_file('exclude_curr_proj_task: ' ||  'LOG', pa_debug.G_err_stage);
1147  END IF;
1148  IF (p_type = 'SRC') THEN
1149    OPEN C_excl_src_proj_task;
1150    FETCH C_excl_src_proj_task INTO v_dummy1, v_dummy2;
1151      IF C_excl_src_proj_task%NOTFOUND THEN
1152        v_status := 0;
1153      ELSE
1154        v_status := 1;
1155      END IF;
1156    CLOSE C_excl_src_proj_task;
1157  ELSIF (p_type = 'TRG') THEN
1158    OPEN C_excl_trg_proj_task;
1159    FETCH C_excl_trg_proj_task INTO v_dummy1, v_dummy2;
1160      IF C_excl_trg_proj_task%NOTFOUND THEN
1161        v_status := 0;
1162      ELSE
1163        v_status := 1;
1164      END IF;
1165    CLOSE C_excl_trg_proj_task;
1166  END IF;
1167   /*  restore the old stack */
1168   pa_debug.reset_err_stack;
1169   return v_status;
1170 EXCEPTION
1171   WHEN OTHERS THEN
1172     return (SQLCODE);
1173 --    return 0;
1174 END exclude_curr_proj_task;
1175 -- ------------------------------------------------------------
1176 -- Build_src_sql
1177 -- ------------------------------------------------------------
1178 Procedure  Build_src_sql( p_project_org_id   IN NUMBER
1179           ,p_project_type     IN VARCHAR2
1180           ,p_task_org_id      IN NUMBER
1181           ,p_service_type     IN VARCHAR2
1182           ,p_class_category   IN VARCHAR2
1183           ,p_class_code       IN VARCHAR2
1184           ,p_project_id       IN NUMBER
1185           ,p_task_id          IN NUMBER
1186           ,x_sql_str          OUT NOCOPY VARCHAR2 ) IS
1187 l_proj_org_str  VARCHAR2(80) ;
1188 l_proj_type_str  VARCHAR2(80) ;
1189 l_task_org_str   VARCHAR2(80) ;
1190 l_serv_type_str  VARCHAR2(80) ;
1191 l_class_catg_str VARCHAR2(80) ;
1192 l_class_code_str VARCHAR2(80) ;
1193 l_proj_id_str    VARCHAR2(80) ;
1194 l_task_id_str    VARCHAR2(300) ;
1195 l_select_clause    VARCHAR2(80) ;
1196 l_from_clause     VARCHAR2(80) ;
1197 l_from_str1     VARCHAR2(80) ;
1198 l_from_str2     VARCHAR2(80) ;
1199 l_pc_pp_str     VARCHAR2(80) ;
1200 l_pc_pt_str     VARCHAR2(80) ;
1201 l_where_str0    VARCHAR2(500) ;
1202 --l_where_str1    VARCHAR2(80) ; commented as part of the bug 13949738
1203 l_where_str2    VARCHAR2(80) ;
1204 l_where_clause  VARCHAR2(1200) ;
1205 v_csr_id  INTEGER ;
1206 BEGIN
1207 l_proj_org_str  := ' pp.carrying_out_organization_id = :lp_project_org_id ' ;
1208 l_proj_type_str := ' pp.project_type = :lp_project_type '                   ;
1209 l_task_org_str  := ' pt.carrying_out_organization_id = :lp_task_org_id '    ;
1210 l_serv_type_str := ' pt.service_type_code = :lp_service_type_code '         ;
1211 l_class_catg_str:= ' pc.class_category = :lp_class_category '             ;
1212 l_class_code_str:= ' pc.class_code     = :lp_class_code    '             ;
1213 l_proj_id_str   := ' pp.project_id = :lp_project_id '                    ;
1214 l_task_id_str   := ' ( (pt.task_id = :lp_task_id AND '
1215                    || ' pa_task_utils.check_child_exists(:lp_task_id)=0) OR '
1216                    || '  (pt.top_task_id = :lp_task_id AND pt.task_id in '
1217                    || ' (select  pt1.task_id FROM pa_tasks pt1 '
1218                    || ' WHERE pt1.top_task_id = :lp_task_id  '
1219                    || ' AND pa_task_utils.check_child_exists(pt1.task_id)=0 )))'  ;
1220 l_select_clause   := 'Select pt.project_id, pt.task_id, pt.top_task_id ' ;
1221 l_from_str1    := ' From pa_project_classes pc, pa_tasks pt, pa_projects pp' ;
1222 l_from_str2    := ' From pa_tasks pt, pa_projects pp' ;
1223 l_pc_pp_str    := ' pp.project_id = pc.project_id ' ;
1224 l_where_str0   := ' where pp.project_id = pt.project_id  AND ' ;
1225 --l_where_str1   := ' pa_project_stus_utils.is_project_status_closed(pp.project_status_code)=''N''' ; commented as part of the bug 13949738
1226 l_where_str2   := ' pa_task_utils.check_child_exists(pt.task_id)=0'  ;
1227 IF p_project_org_id is NOT NULL THEN
1228  l_where_str0 := l_where_str0 || l_proj_org_str || ' AND ' ;
1229  --  l_where_str0 := l_where_str0 || l_proj_org_str  ;
1230 END IF ;
1231 IF p_project_type is NOT NULL THEN
1232    l_where_str0 := l_where_str0 || l_proj_type_str || ' AND ' ;
1233 END IF ;
1234 IF p_task_org_id is NOT NULL THEN
1235    l_where_str0 := l_where_str0 || l_task_org_str || ' AND ' ;
1236 END IF ;
1237 IF p_service_type is NOT NULL THEN
1238    l_where_str0 := l_where_str0 || l_serv_type_str || ' AND ' ;
1239 END IF ;
1240 IF p_class_category is NOT NULL OR p_class_code is NOT NULL THEN
1241    l_from_clause  := l_from_str1 ;
1242    l_where_str0 := l_where_str0 ||l_pc_pp_str|| ' AND ' ;
1243    IF p_class_category is NOT NULL THEN
1244       l_where_str0 := l_where_str0 || l_class_catg_str || ' AND ' ;
1245    END IF ;
1246    IF p_class_code is NOT NULL THEN
1247       l_where_str0 := l_where_str0 || l_class_code_str || ' AND ' ;
1248    END IF ;
1249 ELSE
1250    l_from_clause  := l_from_str2 ;
1251 END IF ;
1252 IF p_project_id is NOT NULL THEN
1253    l_where_str0 := l_where_str0 || l_proj_id_str || ' AND ' ;
1254 END IF ;
1255 IF p_task_id is NOT NULL THEN
1256 --   l_where_clause := l_where_str0 ||l_where_str1||' AND '||  l_task_id_str  ; commented as part of the bug 13949738
1257    l_where_clause := l_where_str0 ||  l_task_id_str  ; -- added as part of the bug 13949738
1258 ELSE
1259 -- l_where_clause := l_where_str0 || l_where_str1 || ' AND ' || l_where_str2  ; commented as part of the bug 13949738
1260  l_where_clause := l_where_str0 || l_where_str2  ; -- added as part of the bug 13949738
1261  --  l_where_clause := l_where_str0 || ';' ;
1262 END IF ;
1263 x_sql_str := l_select_clause || l_from_clause || l_where_clause  ;
1264 EXCEPTION
1265   WHEN OTHERS THEN
1266   RAISE ;
1267 END build_src_sql;
1268 -- ------------------------------------------------------------
1269 -- populate_run_sources
1270 -- ------------------------------------------------------------
1271 PROCEDURE populate_run_sources ( p_rule_id               IN NUMBER
1272                                , p_run_id                IN NUMBER
1273                                , p_resource_list_id      IN NUMBER
1274                                , p_source_clnt_extn_flag IN VARCHAR2
1275 							   /* FP.M : Allocation Impact */
1276 							   , p_alloc_resource_struct_type In Varchar2
1277 							   , p_rbs_version_id		 In Number
1278 							   ) IS
1279 CURSOR c_alloc_source_lines IS
1280   SELECT exclude_flag
1281   , line_num
1282   , project_org_id
1283   , task_org_id
1284   , project_type
1285   , class_category
1286   , class_code
1287   , service_type
1288   , project_id
1289   , task_id
1290   FROM pa_alloc_source_lines
1291   WHERE rule_id = p_rule_id
1292   ORDER BY exclude_flag, line_num;
1293 source_lines_rec c_alloc_source_lines%ROWTYPE;
1294 CURSOR c_leaf_tasks_under_task(p_tsk_id IN NUMBER) IS
1295   SELECT task_id
1296   FROM pa_tasks
1297   WHERE top_task_id = p_tsk_id
1298   AND pa_task_utils.check_child_exists(task_id) = 0;
1299 leaf_task_rec c_leaf_tasks_under_task%ROWTYPE;
1300 CURSOR c_leaf_tasks_under_proj(p_proj_id IN NUMBER) IS
1301   SELECT task_id
1302   FROM pa_tasks
1303   WHERE project_id = p_proj_id
1304   AND pa_task_utils.check_child_exists(task_id) = 0;
1305 CURSOR c_alloc_run_src_projects IS
1306   SELECT project_id, task_id
1307   FROM pa_alloc_run_sources
1308   WHERE rule_id = p_rule_id
1309   AND run_id = p_run_id;
1310 RUN_SRC_PROJ_REC c_alloc_run_src_projects%ROWTYPE;
1311 CURSOR C_top_task (p_tsk_id IN NUMBER)IS
1312   SELECT top_task_id
1313   FROM pa_tasks
1314   WHERE top_task_id = p_tsk_id;
1315 v_top_task_id NUMBER;
1316 v_retcode NUMBER;
1317 v_dummy NUMBER;
1318 v_src_extn_tabtype PA_CLIENT_EXTN_ALLOC.ALLOC_SOURCE_TABTYPE;
1319 v_cx_project_id NUMBER;
1320 v_cx_task_id NUMBER;
1321 v_cx_exclude_flag VARCHAR2(1);
1322 v_status NUMBER :=NULL;
1323 v_err_message VARCHAR2(250);
1324 v_src_sql_str       VARCHAR2 (2000) ;
1325 v_source_csr_id     INTEGER ;
1326 v_src_project_id   NUMBER ;
1327 v_src_task_id   NUMBER ;
1328 v_src_top_task_id NUMBER ;
1329 v_cx_err_flag VARCHAR2(1) ;
1330 BEGIN
1331   pa_debug.set_err_stack('populate_run_sources');
1332   pa_debug.G_err_code:= 0;
1333   IF p_source_clnt_extn_flag = 'Y' THEN
1334   pa_debug.G_err_stage:= 'READING SRC CLIENT EXTENSION FOR EXCLUDES';
1335   IF P_DEBUG_MODE = 'Y' THEN
1336      pa_debug.write_file('populate_run_sources: ' ||  'LOG', pa_debug.G_err_stage);
1337   END IF;
1338   /* reset the error_flag */
1339   G_fatal_err_found := FALSE;
1340   pa_client_extn_alloc.source_extn(p_rule_id, v_src_extn_tabtype,v_status,v_err_message);
1341   IF P_DEBUG_MODE = 'Y' THEN
1342      pa_debug.write_file('populate_run_sources: ' || 'LOG','Client return message: '|| v_err_message);
1343   END IF;
1344   IF nvl(v_status,0) <> 0 then
1345      v_err_message:=nvl(v_err_message,'PA_AL_CE_FAILED');
1346      alloc_errors(p_rule_id, p_run_id, 'S', 'E', v_err_message, TRUE) ;
1347   END IF ;
1348     IF ( v_src_extn_tabtype.count >0 ) THEN
1349       FOR I in 1..v_src_extn_tabtype.count LOOP
1350              v_cx_err_flag := 'N' ;
1351              v_cx_project_id := NVL(v_src_extn_tabtype(I).project_id, 0);
1352              v_cx_task_id := NVL(v_src_extn_tabtype(I).task_id,0);
1353              v_cx_exclude_flag := NVL(v_src_extn_tabtype(I).exclude_flag,'N');
1354                   /* capture all the invalid exclude flags now and error out later */
1355         IF  is_src_project_valid( v_cx_project_id) = 'N' THEN
1356             G_fatal_err_found:= TRUE;
1357             alloc_errors( p_rule_id, p_run_id, 'S', 'E',
1358                       'PA_AL_INV_PROJECT_TASK_IN_CE',FALSE,'Y',v_cx_project_id);
1359             IF P_DEBUG_MODE = 'Y' THEN
1360                pa_debug.write_file('populate_run_sources: ' || 'LOG','Client Extension returned an invalid source project:  '
1361                                || to_char(v_cx_project_id));
1362             END IF;
1363             v_cx_err_flag := 'Y' ;
1364         END IF;
1365         IF (v_cx_task_id <> 0 AND
1366             is_src_task_valid( v_cx_project_id, v_cx_task_id) = 'N') THEN
1367             G_fatal_err_found:= TRUE;
1368             alloc_errors( p_rule_id, p_run_id, 'S', 'E',
1369                         'PA_AL_INV_PROJECT_TASK_IN_CE',FALSE,'Y',v_cx_project_id, v_cx_task_id);
1370             IF P_DEBUG_MODE = 'Y' THEN
1371                pa_debug.write_file('populate_run_sources: ' || 'LOG','Client Extension returned an invalid source task:  '
1372                                 ||to_char( v_cx_task_id) );
1373             END IF;
1374             v_cx_err_flag := 'Y' ;
1375         END IF;
1376         IF( v_cx_exclude_flag NOT IN ( 'Y', 'N') ) THEN
1377           G_fatal_err_found:= TRUE;
1378           alloc_errors( p_rule_id, p_run_id, 'S', 'E',
1379                         'PA_AL_INVAL_CLNT_EXCL_FLG');
1380           v_cx_err_flag := 'Y' ;
1381         END IF;
1382                   /* capture all the NULL projects now and error out later */
1383         IF( v_cx_project_id = 0 ) THEN
1384           G_fatal_err_found:= TRUE;
1385           alloc_errors( p_rule_id, p_run_id, 'S', 'E', 'PA_AL_CLNT_RETURNED_NO_PROJ');
1386           v_cx_err_flag := 'Y' ;
1387         END IF;
1388         IF (v_cx_err_flag = 'N' AND v_cx_exclude_flag = 'Y') THEN
1389           IF v_cx_project_id <>0  THEN /* i.e. a valid project */
1390                                 IF v_cx_task_id IS NULL  THEN
1391                                   /* only valid project_id so insert */
1392               insert_alloc_run_sources( p_rule_id
1393                                       , p_run_id
1394                                       , ( I * -1) /* line_num */
1395                                       , v_cx_project_id
1396                                       , v_cx_task_id
1397                                       , v_cx_exclude_flag
1398                                       , G_creation_date
1399                                       , G_created_by
1400                                       , G_last_update_date
1401                                       , G_last_updated_by
1402                                       , G_last_update_login );
1403                            ELSE /* v_cx_task_id is NOT NULL i.e. a valid task_id */
1404               /* check if lowest level task */
1405               IF( pa_task_utils.check_child_exists(v_cx_task_id) = 0) THEN
1406               /* if lowest level task */
1407                 IF (exclude_curr_proj_task( p_run_id, 'SRC', v_cx_project_id
1408                                           , v_cx_task_id ) = 0) THEN
1409                   /* include current project/task, so insert... */
1410                   insert_alloc_run_sources( p_rule_id
1411                                           , p_run_id, 0
1412                                           , v_cx_project_id
1413                                           , v_cx_task_id
1414                                           , v_cx_exclude_flag
1415                                           , G_creation_date
1416                                           , G_created_by
1417                                           , G_last_update_date
1418                                           , G_last_updated_by
1419                                           , G_last_update_login );
1420                 END IF;
1421               ELSE  /* not a lowest level task */
1422                 OPEN C_top_task( v_cx_task_id);
1423                 FETCH C_top_task INTO v_top_task_id;
1424                 CLOSE C_top_task;
1425                 IF (v_top_task_id <> v_cx_task_id ) THEN
1426                   /* not a top task, mid-level task */
1427                   G_fatal_err_found := TRUE;
1428                   alloc_errors( p_rule_id, p_run_id, 'S', 'E',
1429                                 'PA_AL_MID_LEVEL_TASK', FALSE,'Y',
1430                                 v_cx_project_id, v_cx_task_id);
1431                 ELSE /* if top_task, explode to lowest level */
1432                   FOR leaf_task_rec IN c_leaf_tasks_under_task(v_cx_task_id) LOOP
1433                     IF (exclude_curr_proj_task( p_run_id, 'SRC', v_cx_project_id
1434                                               , leaf_task_rec.task_id) = 0) THEN
1435                       /* include current project/task, so insert... */
1436                       insert_alloc_run_sources( p_rule_id
1437                                                , p_run_id
1438                                                , (I * -1) /* line_num */
1439                                                , v_cx_project_id
1440                                                , leaf_task_rec.task_id
1441                                                , v_cx_exclude_flag
1442                                                , G_creation_date
1443                                                , G_created_by
1444                                                , G_last_update_date
1445                                                , G_last_updated_by
1446                                                , G_last_update_login );
1447                     END IF; /* exclude_curr_proj_task */
1448                   END LOOP;
1449                 END IF; /* top_task */
1450               END IF; /* pa_task_utils */
1451             END IF; /* end v_cx_task_id */
1452           END IF; /* if v_cx_project_id = 0 */
1453         END IF; /* end exclude_flag = 'Y' */
1454       END LOOP;
1455     END IF;  /* end cound>0 */
1456   END IF; /* end src_extn_flag = 'Y' */
1457   pa_debug.G_err_stage:= 'READING SRC LINES FOR EXCLUDES';
1458   IF P_DEBUG_MODE = 'Y' THEN
1459      pa_debug.write_file('populate_run_sources: ' ||  'LOG', pa_debug.G_err_stage);
1460   END IF;
1461   v_source_csr_id := DBMS_SQL.OPEN_CURSOR ; -- added for bug 3799389
1462 
1463   FOR source_lines_rec IN c_alloc_source_lines LOOP
1464     IF (source_lines_rec.exclude_flag = 'Y') THEN
1465       build_src_sql( source_lines_rec.project_org_id
1466                    , source_lines_rec.project_type
1467                    , source_lines_rec.task_org_id
1468                    , source_lines_rec.service_type
1469                    , source_lines_rec.class_category
1470                    , source_lines_rec.class_code
1471                    , source_lines_rec.project_id
1472                    , source_lines_rec.task_id
1473                    , v_src_sql_str ) ;
1474         IF P_DEBUG_MODE = 'Y' THEN
1475            pa_debug.write_file('populate_run_sources: ' || 'LOG',v_src_sql_str);
1476         END IF;
1477 /*        v_source_csr_id := DBMS_SQL.OPEN_CURSOR ;  commented for bug 3799389 */
1478         DBMS_SQL.PARSE(v_source_csr_id, v_src_sql_str, DBMS_SQL.V7) ;
1479         IF source_lines_rec.project_org_id is NOT NULL THEN
1480            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_project_org_id',source_lines_rec.project_org_id) ;
1481         END IF ;
1482         IF  source_lines_rec.project_type is NOT NULL THEN
1483            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_project_type',source_lines_rec.project_type) ;
1484         END IF ;
1485         IF source_lines_rec.task_org_id is NOT NULL THEN
1486            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_task_org_id',source_lines_rec.task_org_id) ;
1487         END IF ;
1488         IF source_lines_rec.service_type is NOT NULL THEN
1489            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_service_type_code',source_lines_rec.service_type) ;
1490         END IF ;
1491         IF source_lines_rec.class_category is NOT NULL THEN
1492            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_class_category',source_lines_rec.class_category) ;
1493         END IF ;
1494         IF source_lines_rec.class_code is NOT NULL THEN
1495            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_class_code',source_lines_rec.class_code) ;
1496         END IF ;
1497         IF source_lines_rec.project_id is NOT NULL THEN
1498            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_project_id',source_lines_rec.project_id) ;
1499         END IF ;
1500         IF source_lines_rec.task_id is NOT NULL THEN
1501            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_task_id',source_lines_rec.task_id) ;
1502         END IF ;
1503         DBMS_SQL.DEFINE_COLUMN(v_source_csr_id,1,v_src_project_id ) ;
1504         DBMS_SQL.DEFINE_COLUMN(v_source_csr_id,2,v_src_task_id ) ;
1505         DBMS_SQL.DEFINE_COLUMN(v_source_csr_id,3,v_src_top_task_id ) ;
1506         v_dummy := DBMS_SQL.execute(v_source_csr_id) ;
1507         LOOP
1508            IF DBMS_SQL.FETCH_ROWS(v_source_csr_id) = 0 THEN
1509                  EXIT ;
1510            END IF ;
1511            dbms_sql.column_value(v_source_csr_id,1,v_src_project_id) ;
1512            dbms_sql.column_value(v_source_csr_id,2,v_src_task_id ) ;
1513            dbms_sql.column_value(v_source_csr_id,3,v_src_top_task_id ) ;
1514            IF P_DEBUG_MODE = 'Y' THEN
1515               pa_debug.write_file('populate_run_sources: ' || 'LOG',to_char(v_src_project_id) || to_char(v_src_task_id) ||
1516                                   to_char(v_src_top_task_id));
1517            END IF;
1518            insert_alloc_run_sources( p_rule_id
1519                                 , p_run_id
1520                                 , source_lines_rec.line_num
1521                                 , v_src_project_id
1522                                 , v_src_task_id
1523                                 , source_lines_rec.exclude_flag
1524                                 , G_creation_date
1525                                 , G_created_by
1526                                 , G_last_update_date
1527                                 , G_last_updated_by
1528                                 , G_last_update_login );
1529         END LOOP;    /* endloop explode srcs  Dynamic sql */
1530     END IF;     /* End if of source excludes from table */
1531   END LOOP;    /* endloop read srcs */
1532   DBMS_SQL.CLOSE_CURSOR(v_source_csr_id ); --added for bug 3799389
1533   IF p_source_clnt_extn_flag = 'Y' THEN
1534   pa_debug.G_err_stage:= 'READING SRC CLIENT EXTENSION FOR INCLUDES';
1535   IF P_DEBUG_MODE = 'Y' THEN
1536      pa_debug.write_file('populate_run_sources: ' ||  'LOG', pa_debug.G_err_stage);
1537   END IF;
1538     pa_client_extn_alloc.source_extn( p_rule_id, v_src_extn_tabtype,v_status,v_err_message);
1539     IF P_DEBUG_MODE = 'Y' THEN
1540        pa_debug.write_file('populate_run_sources: ' || 'LOG','Client return message: '|| v_err_message);
1541     END IF;
1542     IF nvl(v_status,0) <> 0 then
1543       v_err_message:=nvl(v_err_message,'PA_AL_CE_FAILED');
1544       alloc_errors(p_rule_id, p_run_id, 'S', 'E', v_err_message, TRUE) ;
1545     END IF ;
1546     IF (v_src_extn_tabtype.count >0) THEN
1547       FOR I in 1..v_src_extn_tabtype.count LOOP
1548         v_cx_err_flag := 'N' ;
1549         v_cx_project_id :=  NVl(v_src_extn_tabtype(I).project_id, 0);
1550         v_cx_task_id :=  v_src_extn_tabtype(I).task_id ;
1551         v_cx_exclude_flag :=  NVL(v_src_extn_tabtype(I).exclude_flag,'N');
1552         IF  is_src_project_valid( v_cx_project_id) = 'N' THEN
1553             G_fatal_err_found:= TRUE;
1554             alloc_errors( p_rule_id, p_run_id, 'S', 'E',
1555                       'PA_AL_INV_PROJECT_TASK_IN_CE',FALSE,'Y',v_cx_project_id);
1556             IF P_DEBUG_MODE = 'Y' THEN
1557                pa_debug.write_file('populate_run_sources: ' || 'LOG','Client Extension returned an invalid source project:  '
1558                                || to_char(v_cx_project_id));
1559             END IF;
1560            v_cx_err_flag := 'Y' ;
1561         END IF;
1562         IF (v_cx_task_id <> 0 AND
1563              is_src_task_valid( v_cx_project_id, v_cx_task_id) = 'N') THEN
1564             G_fatal_err_found:= TRUE;
1565             alloc_errors( p_rule_id, p_run_id, 'S', 'E',
1566                         'PA_AL_INV_PROJECT_TASK_IN_CE',FALSE,'Y',v_cx_project_id, v_cx_task_id);
1567             IF P_DEBUG_MODE = 'Y' THEN
1568                pa_debug.write_file('populate_run_sources: ' || 'LOG','Client Extension returned an invalid source task:  '
1569                                 ||to_char( v_cx_task_id) );
1570             END IF;
1571            v_cx_err_flag := 'Y' ;
1572         END IF;
1573         IF( v_cx_exclude_flag NOT IN ( 'Y', 'N') ) THEN
1574           G_fatal_err_found:= TRUE;
1575           alloc_errors( p_rule_id, p_run_id, 'S', 'E',
1576                         'PA_AL_INVAL_CLNT_EXCL_FLG');
1577            v_cx_err_flag := 'Y' ;
1578         END IF;
1579         IF (v_cx_err_flag = 'N' AND v_cx_exclude_flag = 'N') THEN
1580           IF( v_cx_project_id <> 0 ) THEN
1581              IF v_cx_task_id IS NULL THEN
1582                FOR each_task in c_leaf_tasks_under_proj(v_cx_project_id) LOOP
1583                 IF (exclude_curr_proj_task( p_run_id, 'SRC', v_cx_project_id,
1584                                    each_task.task_id ) = 0) THEN
1585                   /* include current project/task, so insert */
1586                  insert_alloc_run_sources( p_rule_id
1587                                     , p_run_id
1588                                     , to_number(I*-1)
1589                                     , v_cx_project_id
1590                                     , each_task.task_id
1591                                     , 'N'
1592                                     , G_creation_date
1593                                     , G_created_by
1594                                     , G_last_update_date
1595                                     , G_last_updated_by
1596                                     , G_last_update_login);
1597                  END IF ;
1598                END LOOP ;
1599              ELSE /* v_cx_task_id is NOT NULL */
1600               -- check if lowest level task
1601               IF( pa_task_utils.check_child_exists(v_cx_task_id) = 0) THEN
1602                 IF (exclude_curr_proj_task( p_run_id, 'SRC', v_cx_project_id
1603                                            , v_cx_task_id ) = 0) THEN
1604                   /* include current project/task, so insert... */
1605                   insert_alloc_run_sources( p_rule_id
1606                                           , p_run_id
1607                                           , ( I * -1) /* line_num */
1608                                           , v_cx_project_id
1609                                           , v_cx_task_id
1610                                           , v_cx_exclude_flag
1611                                           , G_creation_date
1612                                           , G_created_by
1613                                           , G_last_update_date
1614                                           , G_last_updated_by
1615                                           , G_last_update_login );
1616                 END IF;
1617               ELSE   /* not a lowest level task */
1618                 OPEN C_top_task( v_cx_task_id);
1619                 FETCH C_top_task INTO v_top_task_id;
1620                 CLOSE C_top_task;
1621                 IF (v_top_task_id <> v_cx_task_id ) THEN
1622                   /* not a top task, mid-level task */
1623                   G_fatal_err_found := TRUE;
1624                   alloc_errors( p_rule_id, p_run_id, 'S', 'E',
1625                                 'PA_AL_MID_LEVEL_TASK',FALSE, 'Y',
1626                                 v_cx_project_id, v_cx_task_id );
1627                 ELSE /* if top_task, explode to lowest level */
1628                   FOR leaf_task_rec IN c_leaf_tasks_under_task(v_cx_task_id) LOOP
1629                     IF (exclude_curr_proj_task( p_run_id, 'SRC', v_cx_project_id
1630                                                , leaf_task_rec.task_id) = 0) THEN
1631                       /* include current project/task, so insert... */
1632                       insert_alloc_run_sources( p_rule_id
1633                                                , p_run_id
1634                                                , (I * -1) /* line_num */
1635                                                , v_cx_project_id
1636                                                , leaf_task_rec.task_id
1637                                                , v_cx_exclude_flag
1638                                                , G_creation_date
1639                                                , G_created_by
1640                                                , G_last_update_date
1641                                                , G_last_updated_by
1642                                                , G_last_update_login );
1643                     END IF; /* exclude_curr_proj */
1644                   END LOOP;
1645                 END IF; /* if v_top_task_id */
1646               END IF; /* end pa_task_utils  */
1647             END IF; /* v_cx_task_id = 0 */
1648           END IF; /* v_cx_project_id <> 0 */
1649         END IF; /* end exclude_flag = 'N' */
1650       END LOOP;
1651     END IF; /* if count>0 */
1652   END IF; /* end src_extn_flag = 'Y' */
1653   pa_debug.G_err_stage:= 'READING SRC LINES FOR INCLUDES';
1654   v_source_csr_id := DBMS_SQL.OPEN_CURSOR ; -- added for bug 3799389
1655   FOR source_lines_rec IN c_alloc_source_lines LOOP
1656     IF (source_lines_rec.exclude_flag = 'N') THEN
1657       pa_debug.G_err_stage:= 'EXPLODING SOURCE LINES FOR INCLUDES';
1658       IF P_DEBUG_MODE = 'Y' THEN
1659          pa_debug.write_file('populate_run_sources: ' ||  'LOG', pa_debug.G_err_stage);
1660       END IF;
1661       build_src_sql( source_lines_rec.project_org_id
1662                    , source_lines_rec.project_type
1663                    , source_lines_rec.task_org_id
1664                    , source_lines_rec.service_type
1665                    , source_lines_rec.class_category
1666                    , source_lines_rec.class_code
1667                    , source_lines_rec.project_id
1668                    , source_lines_rec.task_id
1669                    , v_src_sql_str ) ;
1670         IF P_DEBUG_MODE = 'Y' THEN
1671            pa_debug.write_file('populate_run_sources: ' || 'LOG',v_src_sql_str);
1672         END IF;
1673 /*        v_source_csr_id := DBMS_SQL.OPEN_CURSOR ;  commented for bug 3799389 */
1674         IF P_DEBUG_MODE = 'Y' THEN
1675            pa_debug.write_file('populate_run_sources: ' || 'LOG','Parsing the code' );
1676         END IF;
1677         DBMS_SQL.PARSE(v_source_csr_id, v_src_sql_str, DBMS_SQL.V7) ;
1678         IF P_DEBUG_MODE = 'Y' THEN
1679            pa_debug.write_file('populate_run_sources: ' || 'LOG','After Parsing the code' );
1680         END IF;
1681         IF source_lines_rec.project_org_id is NOT NULL THEN
1682            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_project_org_id',source_lines_rec.project_org_id) ;
1683         END IF ;
1684         IF  source_lines_rec.project_type is NOT NULL THEN
1685            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_project_type',source_lines_rec.project_type) ;
1686         END IF ;
1687         IF source_lines_rec.task_org_id is NOT NULL THEN
1688            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_task_org_id',source_lines_rec.task_org_id) ;
1689         END IF ;
1690         IF source_lines_rec.service_type is NOT NULL THEN
1691            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_service_type_code',source_lines_rec.service_type) ;
1692         END IF ;
1693         IF source_lines_rec.class_category is NOT NULL THEN
1694            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_class_category',source_lines_rec.class_category) ;
1695         END IF ;
1696         IF source_lines_rec.class_code is NOT NULL THEN
1697            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_class_code',source_lines_rec.class_code) ;
1698         END IF ;
1699         IF source_lines_rec.project_id is NOT NULL THEN
1700            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_project_id',source_lines_rec.project_id) ;
1701         END IF ;
1702         IF source_lines_rec.task_id is NOT NULL THEN
1703            DBMS_SQL.BIND_VARIABLE(v_source_csr_id,':lp_task_id',source_lines_rec.task_id) ;
1704         END IF ;
1705         DBMS_SQL.DEFINE_COLUMN(v_source_csr_id,1,v_src_project_id ) ;
1706         DBMS_SQL.DEFINE_COLUMN(v_source_csr_id,2,v_src_task_id ) ;
1707         DBMS_SQL.DEFINE_COLUMN(v_source_csr_id,3,v_src_top_task_id ) ;
1708         IF P_DEBUG_MODE = 'Y' THEN
1709            pa_debug.write_file('populate_run_sources: ' || 'LOG','Executing the code' );
1710         END IF;
1711         v_dummy := DBMS_SQL.execute(v_source_csr_id) ;
1712         IF P_DEBUG_MODE = 'Y' THEN
1713            pa_debug.write_file('populate_run_sources: ' || 'LOG','After Executing the code' );
1714         END IF;
1715         LOOP
1716            IF DBMS_SQL.FETCH_ROWS(v_source_csr_id) = 0 THEN
1717                  EXIT ;
1718            END IF ;
1719            dbms_sql.column_value(v_source_csr_id,1,v_src_project_id) ;
1720            dbms_sql.column_value(v_source_csr_id,2,v_src_task_id ) ;
1721            dbms_sql.column_value(v_source_csr_id,3,v_src_top_task_id ) ;
1722            IF P_DEBUG_MODE = 'Y' THEN
1723               pa_debug.write_file('populate_run_sources: ' || 'LOG',to_char(v_src_project_id) || to_char(v_src_task_id) ||
1724                                   to_char(v_src_top_task_id));
1725            END IF;
1726         -- check if lowest level task
1727         IF( pa_task_utils.check_child_exists(v_src_task_id)= 0) THEN
1728           /* if lowest level task */
1729           IF (exclude_curr_proj_task( p_run_id, 'SRC', v_src_project_id
1730                                      , v_src_task_id ) = 0) THEN
1731             /* include current project/task, so insert */
1732             insert_alloc_run_sources( p_rule_id
1733                                     , p_run_id
1734                                     , to_number(source_lines_rec.line_num)
1735                                     , v_src_project_id
1736                                     , v_src_task_id
1737                                     , source_lines_rec.exclude_flag
1738                                     , G_creation_date
1739                                     , G_created_by
1740                                     , G_last_update_date
1741                                     , G_last_updated_by
1742                                     , G_last_update_login);
1743           END IF;
1744         ELSE /* not a lowest level task */
1745           IF (v_src_top_task_id <> v_src_task_id ) THEN
1746             /* not a top task, mid-level task */
1747             G_fatal_err_found := TRUE;
1748             alloc_errors( p_rule_id, p_run_id, 'S', 'E',
1749                           'PA_AL_MID_LEVEL_TASK', FALSE, 'Y',
1750                           v_src_project_id, v_src_task_id);
1751           ELSE     /* if top_task, explode to lowest level */
1752             FOR leaf_task_rec IN c_leaf_tasks_under_task(v_src_task_id) LOOP
1753               IF (exclude_curr_proj_task( p_run_id, 'SRC', v_src_project_id
1754                                          , leaf_task_rec.task_id) = 0) THEN
1755                 /* include current project/task, so insert */
1756                 insert_alloc_run_sources( p_rule_id
1757                                         , p_run_id
1758                                         , source_lines_rec.line_num
1759                                         , v_src_project_id
1760                                         , leaf_task_rec.task_id
1761                                         , source_lines_rec.exclude_flag
1762                                         , G_creation_date
1763                                         , G_created_by
1764                                         , G_last_update_date
1765                                         , G_last_updated_by
1766                                         , G_last_update_login );
1767               END IF;  /* exclude_curr_proj */
1768             END LOOP;  /* End of leaf_task_rec  */
1769           END IF; /* if v_top_task */
1770         END IF;   /* end pa_task_utils */
1771       END LOOP;   /* end alloc_sources for loop - Dyn sql*/
1772     END IF;       /* end exclude_flag = 'N' */
1773   END LOOP;       /* alloc_source_lines */
1774   DBMS_SQL.CLOSE_CURSOR(v_source_csr_id );  --added for bug 3799389
1775   IF (p_resource_list_id IS NOT NULL ) THEN
1776     /* -- validate_srce_proj_for_RL ----------------------- */
1777     pa_debug.G_err_stage:= 'VALIDATING RSRCE ASSIGNMENTS TO SRC PROJECTS';
1778     IF P_DEBUG_MODE = 'Y' THEN
1779        pa_debug.write_file('populate_run_sources: ' ||  'LOG', pa_debug.G_err_stage);
1780     END IF;
1781 	FOR run_src_proj_rec IN c_alloc_run_src_projects LOOP
1782       OPEN C_proj_in_RL( run_src_proj_rec.project_id
1783                        , p_resource_list_id
1784 						/* FP.M : ALlocation Impact */
1785 					   , p_alloc_resource_struct_type
1786 					   , p_rbs_version_id);
1787       FETCH C_proj_in_RL INTO v_dummy;
1788         IF C_proj_in_RL%NOTFOUND THEN
1789           G_fatal_err_found := TRUE;
1790           alloc_errors( p_rule_id, p_run_id, 'S', 'E',
1791                         'PA_AL_UNASSIGNED_SRC_RL', FALSE, 'Y',
1792                                                                 run_src_proj_rec.project_id,
1793                                                                 run_src_proj_rec.task_id );
1794         END IF;
1795       CLOSE C_proj_in_RL;
1796     END LOOP;
1797   END IF;  /* p_resource_list_id */
1798   /*  restore the old stack */
1799   pa_debug.reset_err_stack;
1800 EXCEPTION
1801   WHEN OTHERS THEN
1802     pa_debug.G_err_code := SQLCODE;
1803     RAISE;
1804 END populate_run_sources;
1805 -- ------------------------------------------------------------
1806 -- insert_alloc_run_targets
1807 -- ------------------------------------------------------------
1808 PROCEDURE insert_alloc_run_targets( p_rule_id            IN NUMBER
1809                                   , p_run_id            IN NUMBER
1810                                   , p_line_num          IN NUMBER
1811                                   , p_project_id        IN NUMBER
1812                                   , p_task_id           IN NUMBER
1813                                   , p_line_percent      IN NUMBER
1814                                   , p_exclude_flag      IN VARCHAR2
1815                                   , p_creation_date     IN DATE
1816                                   , p_created_by        IN NUMBER
1817                                   , p_last_update_date  IN DATE
1818                                   , p_last_updated_by   IN NUMBER
1819                                   , p_last_update_login IN NUMBER
1820                                   , p_bas_method        IN VARCHAR2
1821                                   , p_dup_targets_flag  IN VARCHAR2 ) IS
1822 CURSOR target_exists IS
1823   SELECT 1
1824   FROM pa_alloc_run_targets
1825   WHERE run_id = p_run_id
1826   AND project_id = p_project_id
1827   AND task_id = p_task_id;
1828 v_dummy NUMBER;
1829 allow_insert_flag VARCHAR2(1):= 'Y';
1830 x_budget_version_id PA_BUDGET_VERSIONS.budget_version_id%TYPE := Null; /* added bug2619977 */
1831 x_return_status VARCHAR2(2000) := Null; /* added bug2619977 */
1832 x_msg_count     NUMBER         := 0   ; /* added bug2619977 */
1833 x_msg_data      VARCHAR2(2000) := Null; /* added bug2619977 */
1834 BEGIN
1835   pa_debug.set_err_stack('insert_alloc_run_targets');
1836   pa_debug.G_err_code:= 0;
1837   IF p_dup_targets_flag = 'N'THEN
1838   /* allow insert only if current proj-task does NOT exist */
1839     OPEN target_exists;
1840     FETCH target_exists INTO v_dummy;
1841       IF target_exists%NOTFOUND THEN
1842         allow_insert_flag := 'Y';
1843       ELSE
1844         allow_insert_flag := 'N';
1845       END IF;
1846     CLOSE target_exists;
1847   END IF;
1848   IF (allow_insert_flag = 'Y') THEN
1849       /* added if condition for bug 2619977 */
1850       /* Invoking FP API to get budget_version_id. Will populate the ID in
1851          pa_alloc_run_targets based on budget type or FP type selected. After
1852          this processing become same for both basis - budgets and FPs */
1853 	PA_FIN_PLAN_UTILS.GET_COST_BASE_VERSION_INFO
1854      (  p_project_id
1855        ,G_basis_fin_plan_Type_id
1856        ,G_basis_budget_type_code
1857        ,x_budget_version_id
1858        ,x_return_status
1859        ,x_msg_count
1860        ,x_msg_data
1861 	 );
1862  IF P_DEBUG_MODE = 'Y' THEN
1863     pa_debug.write_file('insert_alloc_run_targets: ' || 'LOG','Fetching budget version id for project ['|| to_char(p_project_id) ||
1864                            '] Fin plan type ['||to_char(G_basis_fin_plan_Type_id) ||
1865                            '] Budget type code ['||G_basis_budget_type_code ||
1866                            '] Budget version ['||to_char(x_budget_version_id) ||
1867                            '] Return status ['||x_return_status||']' );
1868  END IF;
1869  ---- Here needs a validation of
1870 /******** Need to handle the if error is generated in API *****/
1871   INSERT INTO PA_ALLOC_RUN_TARGETS (
1872       RUN_ID
1873     , RULE_ID
1874     , LINE_NUM
1875     , PROJECT_ID
1876     , EXCLUDE_FLAG
1877     , TASK_ID
1878     , LINE_PERCENT
1879     , BUDGET_VERSION_ID /* added bug 2619977 */
1880     , CREATION_DATE
1881     , CREATED_BY
1882     , LAST_UPDATE_DATE
1883     , LAST_UPDATED_BY
1884     , LAST_UPDATE_LOGIN )
1885     VALUES (
1886       p_run_id
1887     , p_rule_id
1888     , p_line_num
1889     , p_project_id
1890     , p_exclude_flag
1891     , p_task_id
1892     , DECODE( p_bas_method, 'S', NULL, 'P', NULL, p_line_percent )
1893     , x_budget_version_id  /* added bug 2619977 */
1894     , p_creation_date
1895     , p_created_by
1896     , p_last_update_date
1897     , p_last_updated_by
1898     , p_last_update_login );
1899 END IF;
1900   /* restore the old stack */
1901   pa_debug.reset_err_stack;
1902 EXCEPTION
1903   WHEN OTHERS THEN
1904     pa_debug.G_err_code := SQLCODE;
1905     RAISE;
1906 END insert_alloc_run_targets;
1907 -- ------------------------------------------------------------
1908 -- Build_tgt_sql
1909 -- ------------------------------------------------------------
1910 Procedure  Build_tgt_sql( p_project_org_id   IN NUMBER
1911           ,p_project_type     IN VARCHAR2
1912           ,p_task_org_id      IN NUMBER
1913           ,p_service_type     IN VARCHAR2
1914           ,p_class_category   IN VARCHAR2
1915           ,p_class_code       IN VARCHAR2
1916           ,p_project_id       IN NUMBER
1917           ,p_task_id          IN NUMBER
1918           ,p_billable_only_flag    IN VARCHAR2
1919           ,p_expnd_item_date  IN DATE
1920           , p_limit_target_projects_code IN VARCHAR2
1921           ,x_sql_str          OUT NOCOPY VARCHAR2 ) IS
1922 l_proj_org_str  VARCHAR2(80) ;
1923 l_proj_type_str  VARCHAR2(80) ;
1924 l_task_org_str   VARCHAR2(80) ;
1925 l_serv_type_str  VARCHAR2(80) ;
1926 l_class_catg_str VARCHAR2(80) ;
1927 l_class_code_str VARCHAR2(80) ;
1928 l_proj_id_str    VARCHAR2(80) ;
1929 l_task_id_str    VARCHAR2(255) ;
1930 l_billable_str   VARCHAR2(80)  ;
1931 l_expnd_item_str VARCHAR2(120) ;
1932 l_select_clause    VARCHAR2(80) ;
1933 l_from_clause     VARCHAR2(120) ;
1934 l_from_str1     VARCHAR2(100) ;
1935 l_from_str2     VARCHAR2(80) ;
1936 l_pc_pp_str     VARCHAR2(80) ;
1937 l_pc_pt_str     VARCHAR2(80) ;
1938 l_where_str0    VARCHAR2(1300) ;
1939 l_where_str1    VARCHAR2(200) ;
1940 l_where_str2    VARCHAR2(80) ;
1941 l_where_clause  VARCHAR2(1500) ;
1942 v_csr_id  INTEGER ;
1943 BEGIN
1944   pa_debug.set_err_stack('build_tgt_sql');
1945   pa_debug.G_err_code:= 0;
1946 l_proj_org_str  := ' pp.carrying_out_organization_id = :lp_project_org_id ' ;
1947 l_proj_type_str := ' pp.project_type = :lp_project_type '                   ;
1948 l_task_org_str  := ' pt.carrying_out_organization_id = :lp_task_org_id '    ;
1949 l_serv_type_str := ' pt.service_type_code = :lp_service_type_code '         ;
1950 l_class_catg_str:= ' pc.class_category = :lp_class_category '             ;
1951 l_class_code_str:= ' pc.class_code     = :lp_class_code    '             ;
1952 l_proj_id_str   := ' pp.project_id = :lp_project_id '                    ;
1953 l_task_id_str   := ' ( (pt.task_id =:lp_task_id AND pt.chargeable_flag=''Y'') OR '
1954                    || '  (pt.top_task_id = :lp_task_id AND pt.task_id in '
1955                    || ' (select  pt1.task_id FROM pa_tasks pt1 '
1956                    || ' WHERE pt1.top_task_id = :lp_task_id  '
1957                    || ' AND pt1.chargeable_flag = ''Y'' )))'  ;
1958 l_billable_str  := ' pt.billable_flag = :lp_billable_only_flag ' ;
1959 l_expnd_item_str:= ' :p_expnd_item_date BETWEEN  nvl(pt.start_date,:p_expnd_item_date) AND NVL(pt.completion_date, :p_expnd_item_date) ' ;
1960 l_select_clause   := 'Select pt.project_id, pt.task_id, pt.top_task_id ' ;
1961 if p_limit_target_projects_code = 'O' then
1962   l_from_str1    := ' From pa_project_classes pc, pa_tasks pt, pa_projects pp ' ;
1963   l_from_str2    := ' From pa_tasks pt, pa_projects pp ' ;
1964   l_where_str0   := ' where pp.project_id = pt.project_id AND ' ;
1965 else
1966   l_from_str1    := ' From pa_project_classes pc, pa_tasks pt, pa_cross_chargeable_ou_v pcou, pa_projects_all pp ' ;
1967   l_from_str2    := ' From pa_tasks pt, pa_cross_chargeable_ou_v pcou, pa_projects_all pp ' ;
1968   --l_where_str0   := ' where pp.project_id = pt.project_id AND nvl(pp.org_id,''0'') = nvl(pcou.recvr_org_id, ''0'')  AND ' ;
1969   l_where_str0   := ' where pp.project_id = pt.project_id AND  pp.org_id  = nvl(pcou.recvr_org_id, ''0'')  AND ' ;
1970   if p_limit_target_projects_code = 'L' then
1971    l_where_str0 := l_where_str0 || 'pcou.prvdr_legal_entity_id = pcou.recvr_legal_entity_id AND ' ;
1972   end if ;
1973   if p_limit_target_projects_code = 'B' then
1974    l_where_str0 := l_where_str0 || 'pcou.business_group_id = pcou.recvr_business_group_id AND ' ;
1975   end if ;
1976 end if ;
1977 l_pc_pp_str    := ' pp.project_id = pc.project_id ' ;
1978 l_where_str1   := ' pa_project_stus_utils.is_project_status_closed(pp.project_status_code)=''N'' '||
1979                   ' AND pa_project_utils.check_project_action_allowed( pp.project_id, ''NEW_TXNS'') = ''Y'' ' ;
1980 l_where_str2   := '  pt.chargeable_flag= ''Y'' '   ;
1981 IF p_project_org_id is NOT NULL THEN
1982  l_where_str0 := l_where_str0 || l_proj_org_str || ' AND ' ;
1983  --  l_where_str0 := l_where_str0 || l_proj_org_str  ;
1984 END IF ;
1985 IF p_project_type is NOT NULL THEN
1986    l_where_str0 := l_where_str0 || l_proj_type_str || ' AND ' ;
1987 END IF ;
1988 IF p_task_org_id is NOT NULL THEN
1989    l_where_str0 := l_where_str0 || l_task_org_str || ' AND ' ;
1990 END IF ;
1991 IF p_service_type is NOT NULL THEN
1992    l_where_str0 := l_where_str0 || l_serv_type_str || ' AND ' ;
1993 END IF ;
1994 IF p_class_category is NOT NULL OR p_class_code is NOT NULL THEN
1995    l_from_clause  := l_from_str1 ;
1996    l_where_str0 := l_where_str0 ||l_pc_pp_str|| ' AND ' ;
1997    IF p_class_category is NOT NULL THEN
1998       l_where_str0 := l_where_str0 || l_class_catg_str || ' AND ' ;
1999    END IF ;
2000    IF p_class_code is NOT NULL THEN
2001       l_where_str0 := l_where_str0 || l_class_code_str || ' AND ' ;
2002    END IF ;
2003 ELSE
2004    l_from_clause  := l_from_str2 ;
2005 END IF ;
2006 -- If billable is set select all billable tasks else should select all chargeable tasks.
2007 IF ( nvl(p_billable_only_flag,'N') = 'Y' )  THEN
2008    l_where_str0 := l_where_str0 || l_billable_str || ' AND ' ;
2009 END IF ;
2010 IF p_project_id is NOT NULL THEN
2011    l_where_str0 := l_where_str0 || l_proj_id_str || ' AND ' ;
2012 END IF ;
2013 IF p_task_id is NOT NULL THEN
2014    l_where_str0 := l_where_str0 ||l_task_id_str ||' AND '   ;
2015 END IF ;
2016  l_where_clause := l_where_str0 || l_where_str1 || ' AND ' || l_where_str2 || ' AND ' || l_expnd_item_str ;
2017    l_where_clause := l_where_clause || ' AND pp.template_flag = ''N''' ;
2018 x_sql_str := l_select_clause || l_from_clause || l_where_clause  ;
2019   /* restore the old stack */
2020   pa_debug.reset_err_stack;
2021 EXCEPTION
2022   WHEN OTHERS THEN
2023     pa_debug.G_err_code := SQLCODE;
2024   RAISE ;
2025 END build_tgt_sql;
2026 -- ------------------------------------------------------------
2027 -- populate_run_targets
2028 -- ------------------------------------------------------------
2029 PROCEDURE populate_run_targets( p_rule_id           IN NUMBER
2030                               , p_run_id           IN NUMBER
2031                               , p_basis_method     IN VARCHAR2
2032                               , p_bas_budget_type_code         IN VARCHAR2
2033                               , p_bas_budget_entry_method_code IN VARCHAR2
2034                               , p_resource_list_id IN NUMBER
2035                               , p_trgt_client_extn IN VARCHAR2
2036                               , p_dup_targets_flag IN VARCHAR2
2037                               , p_expnd_item_date IN DATE
2038                               , p_limit_target_projects_code  IN VARCHAR2
2039                               , x_basis_method OUT NOCOPY VARCHAR2
2040 							  /* FP.M : Allocation Impact */
2041 							  , p_basis_resource_struct_type in varchar2
2042 							  , p_rbs_version_id in Number
2043 							  ) IS
2044 CURSOR c_alloc_target_lines  IS
2045   SELECT exclude_flag
2046   , line_num
2047   , project_org_id
2048   , task_org_id
2049   , project_type
2050   , class_category
2051   , class_code
2052   , service_type
2053   , project_id
2054   , task_id
2055   , billable_only_flag
2056   , line_percent
2057   FROM pa_alloc_target_lines
2058   WHERE rule_id = p_rule_id
2059   ORDER BY 1, 2;
2060 target_lines_rec c_alloc_target_lines%ROWTYPE;
2061 Cursor c_chargeable_tasks_in_proj (x_project_id IN NUMBER,
2062                                    x_ei_date    IN DATE  ) is
2063     select project_id, task_id
2064       from pa_tasks pt
2065      where pt.project_id = x_project_id
2066        AND pt.chargeable_flag = 'Y'
2067        AND x_ei_date between nvl(pt.start_date,x_ei_date) and nvl(pt.completion_date,x_ei_date) ;
2068 CURSOR c_alloc_run_trg_projects IS
2069   SELECT project_id, task_id
2070   FROM pa_alloc_run_targets
2071   WHERE run_id = p_run_id;
2072 CURSOR c_rule_entry_level_code IS
2073   SELECT entry_level_code
2074   FROM pa_budget_entry_methods
2075   WHERE budget_entry_method_code = p_bas_budget_entry_method_code;
2076 CURSOR c_proj_entry_level_code(p_proj_id IN NUMBER) IS
2077   SELECT entry_level_code
2078   FROM pa_budget_entry_methods
2079   WHERE budget_entry_method_code =
2080         ( SELECT budget_entry_method_code
2081           FROM pa_budget_versions
2082           WHERE project_id = p_proj_id
2083           AND current_flag = 'Y'
2084           AND budget_type_code = p_bas_budget_type_code
2085           AND budget_entry_method_code = p_bas_budget_entry_method_code );
2086 CURSOR C_target_line_exists IS
2087   SELECT 1
2088   FROM pa_alloc_target_lines
2089   WHERE rule_id = p_rule_id;
2090 v_trg_extn_tabtype PA_CLIENT_EXTN_ALLOC.ALLOC_TARGET_TABTYPE;
2091 v_top_task_id NUMBER;
2092 v_retcode NUMBER;
2093 v_dummy NUMBER;
2094 v_dummy_target NUMBER;
2095 v_rule_level_code VARCHAR2(1);
2096 v_proj_level_code VARCHAR2(1);
2097 v_cx_project_id NUMBER;
2098 v_cx_task_id NUMBER;
2099 v_cx_percent NUMBER;
2100 v_cx_exclude_flag VARCHAR2(1);
2101 v_status NUMBER :=NULL;
2102 v_err_message VARCHAR2(250);
2103 v_tgt_sql_str       VARCHAR2 (2000) ;
2104 v_target_csr_id     INTEGER ;
2105 v_tgt_project_id   NUMBER ;
2106 v_tgt_task_id   NUMBER ;
2107 v_tgt_top_task_id NUMBER ;
2108 v_cx_err_flag VARCHAR2(1) ;
2109 FUNCTION check_line_percent RETURN NUMBER IS
2110 CURSOR c_check_line_percent IS
2111   SELECT distinct line_num, nvl(line_percent,0) line_percent
2112   FROM pa_alloc_run_targets
2113   WHERE rule_id = p_rule_id
2114   AND   run_id = p_run_id;
2115   v_percent NUMBER;
2116   v_tot_percent number ;
2117   v_line_no number ;
2118 BEGIN
2119   v_percent := 0 ;
2120   v_tot_percent := 0 ;
2121   For line_percent_rec in c_check_line_percent LOOP
2122   v_tot_percent := v_tot_percent + line_percent_rec.line_percent ;
2123   END LOOP ;
2124   IF ( v_tot_percent <>100 ) THEN
2125          v_tot_percent:= 0;
2126   END IF;
2127   RETURN v_tot_percent;
2128 EXCEPTION
2129   WHEN OTHERS THEN
2130   raise;
2131 END check_line_percent;
2132 BEGIN
2133   pa_debug.set_err_stack('populate_run_targets');
2134   pa_debug.G_err_code:= 0;
2135   x_basis_method := p_basis_method;
2136   IF ( p_basis_method = 'FS' OR p_basis_method ='FP') THEN
2137     IF (p_trgt_client_extn = 'Y') THEN
2138         open c_target_line_exists;
2139         fetch c_target_line_exists
2140         into v_dummy_target;
2141         IF c_target_line_exists%FOUND THEN
2142            alloc_errors( p_rule_id, p_run_id, 'T', 'W',
2143                     'PA_AL_LINE_PERCENT_IGNORED');
2144            IF ( p_basis_method = 'FS') THEN
2145                 x_basis_method := 'S';
2146            ELSE
2147                 x_basis_method := 'P';
2148            END IF;
2149         END IF; /** Target line exists **/
2150         close c_target_line_exists;
2151      END IF;/** client_extn = 'Y' **/
2152   END IF;    /* end p_basis_method */
2153   IF (p_trgt_client_extn = 'Y') THEN
2154     pa_debug.G_err_stage:= 'READING TRG CLIENT EXTENSION FOR EXCLUDES';
2155     IF P_DEBUG_MODE = 'Y' THEN
2156        pa_debug.write_file('populate_run_targets: ' ||  'LOG', pa_debug.G_err_stage);
2157     END IF;
2158     pa_client_extn_alloc.target_extn(p_rule_id, v_trg_extn_tabtype,v_status,v_err_message);
2159     IF nvl(v_status,0) <>0 THEN
2160        v_err_message:=nvl(v_err_message,'PA_AL_CE_FAILED');
2161        alloc_errors(p_rule_id,p_run_id,'T','E',v_err_message,TRUE);
2162     END IF;
2163     IF ( v_trg_extn_tabtype.count >0 ) THEN
2164       FOR I in 1..v_trg_extn_tabtype.count LOOP
2165         v_cx_err_flag := 'N' ;
2166         v_cx_project_id := NVl(v_trg_extn_tabtype(I).project_id, 0);
2167         v_cx_task_id:= NVL(v_trg_extn_tabtype(I).task_id,0);
2168         v_cx_percent:= v_trg_extn_tabtype(I).percent;   /* for bug 2013779 */
2169         v_cx_exclude_flag:= NVl(v_trg_extn_tabtype(I).exclude_flag,'N');
2170         /* capture all the invalid exclude flags now and error out later */
2171         IF  is_tgt_project_valid( v_cx_project_id) = 'N' THEN
2172             G_fatal_err_found:= TRUE;
2173             alloc_errors( p_rule_id, p_run_id, 'T', 'E',
2174                         'PA_AL_INV_PROJECT_TASK_IN_CE',FALSE,'Y',v_cx_project_id);
2175             IF P_DEBUG_MODE = 'Y' THEN
2176                pa_debug.write_file('populate_run_targets: ' || 'LOG','Client Extension returned an invalid target project:  '
2177                                         ||to_char(v_cx_project_id));
2178             END IF;
2179             v_cx_err_flag := 'Y' ;
2180         END IF;
2181         IF (v_cx_task_id <> 0 AND
2182            is_tgt_task_valid( v_cx_project_id, v_cx_task_id) = 'N' ) THEN
2183             G_fatal_err_found:= TRUE;
2184             alloc_errors( p_rule_id, p_run_id, 'T', 'E',
2185                         'PA_AL_INV_PROJECT_TASK_IN_CE',FALSE,'Y',v_cx_project_id, v_cx_task_id);
2186             IF P_DEBUG_MODE = 'Y' THEN
2187                pa_debug.write_file('populate_run_targets: ' || 'LOG','Client Extension returned an invalid target task:  '
2188                    || to_char(v_cx_task_id));
2189             END IF;
2190             v_cx_err_flag := 'Y' ;
2191         END IF;
2192         IF( v_cx_exclude_flag NOT IN ( 'Y', 'N') ) THEN
2193           G_fatal_err_found:= TRUE;
2194           alloc_errors( p_rule_id, p_run_id, 'S', 'E',
2195                         'PA_AL_INVAL_CLNT_EXCL_FLG');
2196             v_cx_err_flag := 'Y' ;
2197         END IF;
2198         /* capture all NULL projects and error out */
2199         IF (v_cx_project_id = 0) THEN
2200           G_fatal_err_found:= TRUE;
2201           alloc_errors( p_rule_id, p_run_id, 'T', 'E',
2202                         'PA_AL_CLNT_RETURNED_NO_PROJ');
2203           v_cx_err_flag := 'Y' ;
2204         END IF;
2205         IF (v_cx_err_flag = 'N' AND v_cx_exclude_flag = 'Y')  THEN
2206            IF (v_cx_task_id is NULL OR
2207               (pa_task_utils.check_child_exists(v_cx_task_id)= 0)) THEN
2208                  insert_alloc_run_targets( p_rule_id, p_run_id
2209                                     , (I * -1) /* line_num */
2210                                     , v_cx_project_id
2211                                     , v_cx_task_id
2212                                     , v_cx_percent
2213                                     , v_cx_exclude_flag
2214                                     , G_creation_date
2215                                     , G_created_by
2216                                     , G_last_update_date
2217                                     , G_last_updated_by
2218                                     , G_last_update_login
2219                                     , x_basis_method
2220                                     , p_dup_targets_flag );
2221            ELSE
2222              G_fatal_err_found := TRUE;
2223              alloc_errors( p_rule_id, p_run_id, 'T', 'E',
2224                               'PA_AL_NOT_A_CHARGEABLE_TASK',FALSE,'Y',
2225                               v_cx_project_id,
2226                               v_cx_task_id );
2227            END IF; /* exclude_curr_proj_task */
2228         END IF; /* exclude_flag */
2229       END LOOP;
2230     END IF; /* count */
2231   END IF; /* if p_trgt_client_extn */
2232   pa_debug.G_err_stage:= 'READING TRG LINES FOR EXCLUDES';
2233   IF P_DEBUG_MODE = 'Y' THEN
2234      pa_debug.write_file('populate_run_targets: ' ||  'LOG', pa_debug.G_err_stage);
2235   END IF;
2236   v_target_csr_id := DBMS_SQL.OPEN_CURSOR ; --added for bug 3799389
2237   FOR target_lines_rec IN c_alloc_target_lines LOOP
2238     IF (target_lines_rec.exclude_flag = 'Y') THEN
2239       build_tgt_sql( target_lines_rec.project_org_id
2240                    , target_lines_rec.project_type
2241                    , target_lines_rec.task_org_id
2242                    , target_lines_rec.service_type
2243                    , target_lines_rec.class_category
2244                    , target_lines_rec.class_code
2245                    , target_lines_rec.project_id
2246                    , target_lines_rec.task_id
2247                    , target_lines_rec.billable_only_flag
2248                    , p_expnd_item_date
2249                    , p_limit_target_projects_code
2250                    , v_tgt_sql_str ) ;
2251         IF P_DEBUG_MODE = 'Y' THEN
2252            pa_debug.write_file('populate_run_targets: ' || 'LOG',v_tgt_sql_str);
2253         END IF;
2254 /*        v_target_csr_id := DBMS_SQL.OPEN_CURSOR ; commented for bug 3799389 */
2255         DBMS_SQL.PARSE(v_target_csr_id, v_tgt_sql_str, DBMS_SQL.V7) ;
2256         IF target_lines_rec.project_org_id is NOT NULL THEN
2257            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_project_org_id',target_lines_rec.project_org_id) ;
2258         END IF ;
2259         IF  target_lines_rec.project_type is NOT NULL THEN
2260            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_project_type',target_lines_rec.project_type) ;
2261         END IF ;
2262         IF target_lines_rec.task_org_id is NOT NULL THEN
2263            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_task_org_id',target_lines_rec.task_org_id) ;
2264         END IF ;
2265         IF target_lines_rec.service_type is NOT NULL THEN
2266            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_service_type_code',target_lines_rec.service_type) ;
2267         END IF ;
2268         IF target_lines_rec.class_category is NOT NULL THEN
2269            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_class_category',target_lines_rec.class_category) ;
2270         END IF ;
2271         IF target_lines_rec.class_code is NOT NULL THEN
2272            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_class_code',target_lines_rec.class_code) ;
2273         END IF ;
2274         IF target_lines_rec.project_id is NOT NULL THEN
2275            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_project_id',target_lines_rec.project_id) ;
2276         END IF ;
2277         IF target_lines_rec.task_id is NOT NULL THEN
2278            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_task_id',target_lines_rec.task_id) ;
2279         END IF ;
2280         IF p_expnd_item_date is NOT NULL THEN
2281            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':p_expnd_item_date',p_expnd_item_date) ;
2282         END IF ;
2283         IF ( nvl(target_lines_rec.billable_only_flag,'N') = 'Y' )  THEN
2284            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_billable_only_flag',target_lines_rec.billable_only_flag) ;
2285         END IF ;
2286         DBMS_SQL.DEFINE_COLUMN(v_target_csr_id,1,v_tgt_project_id ) ;
2287         DBMS_SQL.DEFINE_COLUMN(v_target_csr_id,2,v_tgt_task_id ) ;
2288         DBMS_SQL.DEFINE_COLUMN(v_target_csr_id,3,v_tgt_top_task_id ) ;
2289         v_dummy := DBMS_SQL.execute(v_target_csr_id) ;
2290         LOOP
2291            IF DBMS_SQL.FETCH_ROWS(v_target_csr_id) = 0 THEN
2292                  EXIT ;
2293            END IF ;
2294            dbms_sql.column_value(v_target_csr_id,1,v_tgt_project_id) ;
2295            dbms_sql.column_value(v_target_csr_id,2,v_tgt_task_id ) ;
2296            dbms_sql.column_value(v_target_csr_id,3,v_tgt_top_task_id ) ;
2297            IF P_DEBUG_MODE = 'Y' THEN
2298               pa_debug.write_file('populate_run_targets: ' || 'LOG',to_char(v_tgt_project_id) || to_char(v_tgt_task_id) ||
2299                                   to_char(v_tgt_top_task_id));
2300            END IF;
2301         insert_alloc_run_targets( p_rule_id
2302                                 , p_run_id
2303                                 , target_lines_rec.line_num
2304                                 , v_tgt_project_id
2305                                 , v_tgt_task_id
2306                                 , target_lines_rec.line_percent
2307                                 , target_lines_rec.exclude_flag
2308                                 , G_creation_date
2309                                 , G_created_by
2310                                 , G_last_update_date
2311                                 , G_last_updated_by
2312                                 , G_last_update_login
2313                                 , x_basis_method
2314                                 , p_dup_targets_flag );
2315       END LOOP; /* for c_alloc_targets */
2316     END IF;
2317   END LOOP; /* for c_alloc_target_lines */
2318   DBMS_SQL.CLOSE_CURSOR(v_target_csr_id );  --added for bug 3799389
2319   IF (p_trgt_client_extn = 'Y') THEN
2320     pa_debug.G_err_stage:= 'READING TRG CLIENT EXTENSION FOR INCLUDES';
2321     IF P_DEBUG_MODE = 'Y' THEN
2322        pa_debug.write_file('populate_run_targets: ' ||  'LOG', pa_debug.G_err_stage);
2323     END IF;
2324     pa_client_extn_alloc.target_extn( p_rule_id, v_trg_extn_tabtype,v_status, v_err_message);
2325     IF nvl(v_status,0) <>0 THEN
2326        v_err_message:=nvl(v_err_message,'PA_AL_CE_FAILED');
2327        alloc_errors(p_rule_id,p_run_id,'T','E',v_err_message,TRUE);
2328     END IF;
2329     IF (v_trg_extn_tabtype.count >0) THEN
2330       FOR I in 1..v_trg_extn_tabtype.count LOOP
2331         v_cx_project_id:= NVL(v_trg_extn_tabtype(I).project_id, 0);
2332         v_cx_task_id := v_trg_extn_tabtype(I).task_id;
2333         v_cx_percent:= v_trg_extn_tabtype(I).percent; /* bug 2013779 */
2334         v_cx_exclude_flag := NVL(v_trg_extn_tabtype(I).exclude_flag, 'N');
2335         IF v_cx_exclude_flag = 'N' THEN
2336           IF (v_cx_project_id <> 0 ) THEN
2337           IF ( v_cx_task_id IS NOT NULL ) THEN /* if task_id is NOT NULL */
2338             /*  check if lowest level task */
2339             IF( pa_task_utils.check_child_exists(v_cx_task_id)= 0) THEN
2340               /* if lowest level chargeable task */
2341                IF (exclude_curr_proj_task( p_run_id, 'TRG', v_cx_project_id
2342                                         , v_cx_task_id ) = 0) THEN
2343                 /* include current project/task, so insert... */
2344                 insert_alloc_run_targets( p_rule_id, p_run_id
2345                                         , (I * -1) /* line_num */
2346                                         , v_cx_project_id
2347                                         , v_cx_task_id
2348                                         , v_cx_percent
2349                                         , v_cx_exclude_flag
2350                                         , G_creation_date
2351                                         , G_created_by
2352                                         , G_last_update_date
2353                                         , G_last_updated_by
2354                                         , G_last_update_login
2355                                         , x_basis_method
2356                                         , p_dup_targets_flag );
2357                END IF; /* exclude_curr_proj_task */
2358             ELSE
2359                 G_fatal_err_found := TRUE;
2360                 alloc_errors( p_rule_id, p_run_id, 'T', 'E',
2361                               'PA_AL_NOT_A_CHARGEABLE_TASK',FALSE,'Y',
2362                               v_cx_project_id,
2363                               v_cx_task_id );
2364             END IF; /* pa_task_utils */
2365           ELSE
2366              FOR chargeable_tasks in c_chargeable_tasks_in_proj(v_cx_project_id
2367                                                                 ,p_expnd_item_date)
2368              LOOP
2369                 insert_alloc_run_targets( p_rule_id, p_run_id
2370                                         , (I * -1) /* line_num */
2371                                         , v_cx_project_id
2372                                         , chargeable_tasks.task_id
2373                                         , v_cx_percent
2374                                         , v_cx_exclude_flag
2375                                         , G_creation_date
2376                                         , G_created_by
2377                                         , G_last_update_date
2378                                         , G_last_updated_by
2379                                         , G_last_update_login
2380                                         , x_basis_method
2381                                         , p_dup_targets_flag );
2382              END LOOP ;
2383           END IF; /* v_cx_task_id <> 0 */
2384          END IF ; /* v_cx_project_id <> 0 */
2385         END IF;  /* v_cx_exclude_flag = N */
2386       END LOOP;
2387     END IF;  /* if count>0 */
2388   END IF; /* trg_lnc_extn_flag */
2389   pa_debug.G_err_stage:= 'READING TRG LINES FOR INCLUDES';
2390   IF P_DEBUG_MODE = 'Y' THEN
2391      pa_debug.write_file('populate_run_targets: ' ||  'LOG', pa_debug.G_err_stage);
2392   END IF;
2393   v_target_csr_id := DBMS_SQL.OPEN_CURSOR ;  --added for bug 3799389
2394   FOR target_lines_rec IN c_alloc_target_lines LOOP
2395     IF (target_lines_rec.exclude_flag = 'N') THEN
2396       build_tgt_sql( target_lines_rec.project_org_id
2397                    , target_lines_rec.project_type
2398                    , target_lines_rec.task_org_id
2399                    , target_lines_rec.service_type
2400                    , target_lines_rec.class_category
2401                    , target_lines_rec.class_code
2402                    , target_lines_rec.project_id
2403                    , target_lines_rec.task_id
2404                    , target_lines_rec.billable_only_flag
2405                    , p_expnd_item_date
2406                    , p_limit_target_projects_code
2407                    , v_tgt_sql_str ) ;
2408         IF P_DEBUG_MODE = 'Y' THEN
2409            pa_debug.write_file('populate_run_targets: ' || 'LOG',v_tgt_sql_str);
2410         END IF;
2411 /*        v_target_csr_id := DBMS_SQL.OPEN_CURSOR ;  commented for bug 3799389 */
2412         DBMS_SQL.PARSE(v_target_csr_id, v_tgt_sql_str, DBMS_SQL.V7) ;
2413         IF target_lines_rec.project_org_id is NOT NULL THEN
2414            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_project_org_id',target_lines_rec.project_org_id) ;
2415         END IF ;
2416         IF  target_lines_rec.project_type is NOT NULL THEN
2417            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_project_type',target_lines_rec.project_type) ;
2418         END IF ;
2419         IF target_lines_rec.task_org_id is NOT NULL THEN
2420            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_task_org_id',target_lines_rec.task_org_id) ;
2421         END IF ;
2422         IF target_lines_rec.service_type is NOT NULL THEN
2423            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_service_type_code',target_lines_rec.service_type) ;
2424         END IF ;
2425         IF target_lines_rec.class_category is NOT NULL THEN
2426            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_class_category',target_lines_rec.class_category) ;
2427         END IF ;
2428         IF target_lines_rec.class_code is NOT NULL THEN
2429            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_class_code',target_lines_rec.class_code) ;
2430         END IF ;
2431         IF target_lines_rec.project_id is NOT NULL THEN
2432            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_project_id',target_lines_rec.project_id) ;
2433         END IF ;
2434         IF target_lines_rec.task_id is NOT NULL THEN
2435            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_task_id',target_lines_rec.task_id) ;
2436         END IF ;
2437         IF p_expnd_item_date is NOT NULL THEN
2438            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':p_expnd_item_date',p_expnd_item_date) ;
2439         END IF ;
2440         IF ( nvl(target_lines_rec.billable_only_flag,'N') = 'Y' )  THEN
2441            DBMS_SQL.BIND_VARIABLE(v_target_csr_id,':lp_billable_only_flag',target_lines_rec.billable_only_flag) ;
2442         END IF ;
2443         DBMS_SQL.DEFINE_COLUMN(v_target_csr_id,1,v_tgt_project_id ) ;
2444         DBMS_SQL.DEFINE_COLUMN(v_target_csr_id,2,v_tgt_task_id ) ;
2445         DBMS_SQL.DEFINE_COLUMN(v_target_csr_id,3,v_tgt_top_task_id ) ;
2446         v_dummy := DBMS_SQL.execute(v_target_csr_id) ;
2447         LOOP
2448            IF DBMS_SQL.FETCH_ROWS(v_target_csr_id) = 0 THEN
2449                  EXIT ;
2450            END IF ;
2451            dbms_sql.column_value(v_target_csr_id,1,v_tgt_project_id) ;
2452            dbms_sql.column_value(v_target_csr_id,2,v_tgt_task_id ) ;
2453            dbms_sql.column_value(v_target_csr_id,3,v_tgt_top_task_id ) ;
2454            IF P_DEBUG_MODE = 'Y' THEN
2455               pa_debug.write_file('populate_run_targets: ' || 'LOG','Exploded proj/tasks: '|| to_char(v_tgt_project_id) ||
2456                                   to_char(v_tgt_task_id) ||
2457                                   to_char(v_tgt_top_task_id));
2458            END IF;
2459         /* since always lowest chargeable task, check whether to exclude */
2460         IF (exclude_curr_proj_task(p_run_id, 'TRG',v_tgt_project_id
2461                                   , v_tgt_task_id ) = 0) THEN
2462           /* include current project/task, so insert */
2463           insert_alloc_run_targets( p_rule_id
2464                                   , p_run_id
2465                                   , target_lines_rec.line_num
2466                                   , v_tgt_project_id
2467                                   , v_tgt_task_id
2468                                   , target_lines_rec.line_percent
2469                                   , target_lines_rec.exclude_flag
2470                                   , G_creation_date
2471                                   , G_created_by
2472                                   , G_last_update_date
2473                                   , G_last_updated_by
2474                                   , G_last_update_login
2475                                   , x_basis_method
2476                                   , p_dup_targets_flag );
2477         END IF; /* exclude_curr_proj_task */
2478       END LOOP;
2479     END IF;
2480   END LOOP;
2481   DBMS_SQL.CLOSE_CURSOR(v_target_csr_id );  --added for bug 3799389
2482   /* -- validate budget_entry_level_code for trg_proj --------------- */
2483   IF( p_bas_budget_entry_method_code IS NOT NULL) THEN
2484     pa_debug.G_err_stage:= 'VALIDATING BASIS BUDGET ENTRY METHOD TO TRG PROJECTS';
2485     IF P_DEBUG_MODE = 'Y' THEN
2486        pa_debug.write_file('populate_run_targets: ' ||  'LOG', pa_debug.G_err_stage);
2487     END IF;
2488     OPEN c_rule_entry_level_code;
2489     FETCH c_rule_entry_level_code INTO v_rule_level_code;
2490     CLOSE c_rule_entry_level_code;
2491     FOR run_trg_proj_rec IN c_alloc_run_trg_projects LOOP
2492       OPEN c_proj_entry_level_code(run_trg_proj_rec.project_id);
2493       FETCH c_proj_entry_level_code INTO v_proj_level_code;
2494       IF (v_rule_level_code <> v_proj_level_code ) THEN
2495         G_fatal_err_found := TRUE;
2496         alloc_errors(p_rule_id, p_run_id, 'B', 'E',
2497                       'PA_AL_UNASSIGNED_BASIS_BEM', FALSE, 'Y',
2498                        run_trg_proj_rec.project_id );
2499       END IF;
2500       CLOSE c_proj_entry_level_code;
2501     END LOOP;
2502   END IF; /* p_bas_budget_entry_method_code  */
2503   /* -- validate_trg_proj_for_RL ----------------------- */
2504   IF( p_resource_list_id IS NOT NULL ) THEN
2505     pa_debug.G_err_stage:= 'VALIDATING RSRCE ASSIGNMENTS TO TRG PROJECTS';
2506     IF P_DEBUG_MODE = 'Y' THEN
2507        pa_debug.write_file('populate_run_targets: ' ||  'LOG', pa_debug.G_err_stage);
2508     END IF;
2509     FOR run_trg_proj_rec IN c_alloc_run_trg_projects LOOP
2510       OPEN C_proj_in_RL( run_trg_proj_rec.project_id
2511                        , p_resource_list_id
2512 					   /* FP.M : ALlocation Impact */
2513 					   , p_basis_resource_struct_type
2514 					   , p_rbs_version_id
2515 					   );
2516       FETCH C_proj_in_RL INTO v_dummy;
2517         IF C_proj_in_RL%NOTFOUND THEN
2518           G_fatal_err_found := TRUE;
2519           alloc_errors( p_rule_id, p_run_id,
2520                         'T', 'E',
2521                         'PA_AL_UNASSIGNED_TRG_RL', FALSE, 'Y',
2522                          run_trg_proj_rec.project_id,
2523                          run_trg_proj_rec.task_id );
2524         END IF;
2525       CLOSE C_proj_in_RL;
2526     END LOOP;
2527   END IF;
2528   IF ( x_basis_method IN ( 'FS', 'FP') AND check_line_percent <> 100 ) THEN
2529     pa_debug.G_err_stage:= 'VALIDATING TRG LINE PERCENT';
2530     IF P_DEBUG_MODE = 'Y' THEN
2531        pa_debug.write_file('populate_run_targets: ' ||  'LOG', pa_debug.G_err_stage);
2532     END IF;
2533     G_fatal_err_found := TRUE;
2534     alloc_errors( p_rule_id, p_run_id,
2535                   'T', 'E',
2536                   'PA_AL_LINE_PRCNT_NOT_100');
2537   END IF;
2538   /*  restore the old stack */
2539   pa_debug.reset_err_stack;
2540 EXCEPTION
2541   WHEN OTHERS THEN
2542     pa_debug.G_err_code := SQLCODE;
2543     RAISE;
2544 END populate_run_targets;
2545 -- ------------------------------------------------------------
2546 -- insert_alloc_run_GL_det
2547 -- ------------------------------------------------------------
2548 PROCEDURE insert_alloc_run_GL_det ( p_run_id            IN NUMBER
2549                                   , p_rule_id           IN NUMBER
2550                                   , p_line_num          IN NUMBER
2551                                   , p_source_ccid       IN NUMBER
2552                                   , p_subtract_flag     IN VARCHAR2
2553                                   , p_creation_date     IN DATE
2554                                   , p_created_by        IN NUMBER
2555                                   , p_last_update_date  IN DATE
2556                                   , p_last_updated_by   IN NUMBER
2557                                   , p_last_update_login IN NUMBER
2558                                   , p_source_percent    IN NUMBER
2559                                   , p_amount            IN NUMBER
2560                                   , p_eligible_amount   IN NUMBER ) IS
2561 BEGIN
2562   pa_debug.set_err_stack('insert_alloc_run_GL_det');
2563   INSERT INTO pa_alloc_run_gl_det(
2564     RUN_ID
2565   , RULE_ID
2566   , LINE_NUM
2567   , SOURCE_CCID
2568   , SUBTRACT_FLAG
2569   , CREATION_DATE
2570   , CREATED_BY
2571   , LAST_UPDATE_DATE
2572   , LAST_UPDATED_BY
2573   , LAST_UPDATE_LOGIN
2574   , SOURCE_PERCENT
2575   , AMOUNT
2576   , ELIGIBLE_AMOUNT )
2577   VALUES (
2578     p_run_id
2579   , p_rule_id
2580   , p_line_num
2581   , p_source_ccid
2582   , p_subtract_flag
2583   , p_creation_date
2584   , p_created_by
2585   , p_last_update_date
2586   , p_last_updated_by
2587   , p_last_update_login
2588   , p_source_percent
2589   , p_amount
2590   , p_eligible_amount);
2591   pa_debug.reset_err_stack;
2592 EXCEPTION
2593   WHEN OTHERS THEN
2594     pa_debug.G_err_code := SQLCODE;
2595     RAISE;
2596 END insert_alloc_run_GL_det;
2597 -- ------------------------------------------------------------
2598 -- calculate_src_GL_amounts
2599 -- ------------------------------------------------------------
2600 PROCEDURE calculate_src_GL_amounts( p_rule_id      IN  NUMBER
2601                                  , p_run_id        IN  NUMBER
2602                                  , p_run_period    IN  VARCHAR2
2603                                  , p_amount_type   IN  VARCHAR2
2604                                  , x_gl_src_amount OUT NOCOPY NUMBER ) IS
2605 CURSOR gl_sources IS
2606   SELECT source_ccid
2607        , nvl(source_percent,100) source_percent
2608        , subtract_flag
2609        , line_num
2610   FROM pa_alloc_gl_lines
2611   WHERE rule_id = p_rule_id;
2612 CURSOR gl_account_dets IS
2613   SELECT a.set_of_books_id
2614        , b.accounted_period_type
2615        , b.currency_code
2616   FROM pa_implementations a
2617      , gl_sets_of_books b
2618   WHERE a.set_of_books_id = b.set_of_books_id;
2619 CURSOR gl_eligible_amount IS
2620   SELECT nvl(sum( nvl(eligible_amount,0)*DECODE(subtract_flag,'Y',-1,1) ),0)
2621   FROM pa_alloc_run_GL_det
2622   WHERE run_id = p_run_id;
2623 CURSOR get_gl_amount( p_sob_id        IN NUMBER
2624                                                   , p_source_ccid   IN NUMBER
2625                                                   , p_currency_code IN VARCHAR2
2626                                                   , p_run_period    IN VARCHAR2
2627                                                   , p_amount_type   In VARCHAR2 ) IS
2628   SELECT NVL(period_net_dr,0) - NVL(period_net_cr, 0) +
2629            decode(p_amount_type, 'FYTD',NVL(begin_balance_dr, 0), 'QTD'
2630                   , NVL(quarter_to_date_dr, 0), 0) -
2631            decode(p_amount_type, 'FYTD', NVL(begin_balance_cr, 0), 'QTD'
2632                   , NVL(quarter_to_date_cr, 0), 0)
2633   FROM gl_balances
2634   WHERE ledger_id = p_sob_id
2635   AND code_combination_id = p_source_ccid   /** .source_ccid */
2636   AND currency_code = p_currency_code
2637   AND period_name = p_run_period
2638   AND actual_flag = 'A'
2639   AND translated_flag IS NULL;
2640 CURSOR get_pool_percent  IS
2641   SELECT nvl(pool_percent,100) pool_percent
2642   FROM   pa_alloc_runs
2643   WHERE  run_id = p_run_id;
2644 v_sob_id NUMBER;
2645 v_currency_code VARCHAR2(10);
2646 v_period_type VARCHAR2(10);
2647 v_amount NUMBER;
2648 v_pool_percent NUMBER;
2649 BEGIN
2650   pa_debug.set_err_stack('calculate_src_GL_amounts');
2651   pa_debug.G_err_stage:= 'Getting gl_account_details';
2652   IF P_DEBUG_MODE = 'Y' THEN
2653      pa_debug.write_file('calculate_src_GL_amounts: ' ||  'LOG', pa_debug.G_err_stage);
2654   END IF;
2655   OPEN gl_account_dets;
2656   FETCH gl_account_dets
2657   INTO v_sob_id, v_period_type, v_currency_code ;
2658   CLOSE gl_account_dets;
2659    IF P_DEBUG_MODE = 'Y' THEN
2660       pa_debug.write_file('calculate_src_GL_amounts: ' || 'LOG', 'v_SOB_ID is: '|| to_char(v_sob_id) );
2661       pa_debug.write_file('calculate_src_GL_amounts: ' || 'LOG', 'v_currency_code is: '|| v_currency_code );
2662       pa_debug.write_file('calculate_src_GL_amounts: ' || 'LOG', 'v_period_type is: '|| v_period_type );
2663    END IF;
2664   OPEN get_pool_percent;
2665   FETCH get_pool_percent
2666   INTO v_pool_percent;
2667   CLOSE get_pool_percent;
2668   IF P_DEBUG_MODE = 'Y' THEN
2669      pa_debug.write_file('calculate_src_GL_amounts: ' || 'LOG', 'v_pool_percent is: '|| to_char(v_pool_percent));
2670   END IF;
2671   FOR gl_source_rec IN gl_sources LOOP
2672     OPEN get_gl_amount( v_sob_id
2673                       , gl_source_rec.source_ccid
2674                       , v_currency_code
2675                       , p_run_period
2676                       , p_amount_type );
2677          FETCH get_gl_amount INTO v_amount;
2678          IF (get_gl_amount%NOTFOUND) THEN
2679                 IF P_DEBUG_MODE = 'Y' THEN
2680                    pa_debug.write_file('calculate_src_GL_amounts: ' || 'LOG', 'no amount found for the source');
2681                 END IF;
2682                 v_amount := 0;               /* for bug 2154559 */
2683                 alloc_errors( p_rule_id, p_run_id, 'S', 'W',
2684                                                   'PA_AL_NO_GL_BALANCES');
2685          END IF;
2686          CLOSE get_gl_amount;
2687 --  Commented the following line as a part of fixing rounding issues.
2688 --  The rounding is done for eligible amount
2689 --    v_amount:= pa_currency.round_currency_amt( NVl(v_amount,0) );
2690    IF P_DEBUG_MODE = 'Y' THEN
2691       pa_debug.write_file('calculate_src_GL_amounts: ' || 'LOG', 'v_amount is: '|| to_char(v_amount) );
2692    END IF;
2693     /* then insert into gl_source_det */
2694     insert_alloc_run_GL_det( p_run_id
2695                            , p_rule_id
2696                            , gl_source_rec.line_num
2697                            , gl_source_rec.source_ccid
2698                            , gl_source_rec.subtract_flag
2699                            , G_creation_date
2700                            , G_created_by
2701                            , G_last_update_date
2702                            , G_last_updated_by
2703                            , G_last_update_login
2704                            , gl_source_rec.source_percent
2705                            , v_amount
2706                            , pa_currency.round_currency_amt(v_amount*
2707                                    (gl_source_rec.source_percent/100)*
2708                                    ( v_pool_percent/100))
2709                             ) ;
2710    IF P_DEBUG_MODE = 'Y' THEN
2711       pa_debug.write_file('calculate_src_GL_amounts: ' || 'LOG', 'After insert into alloc_run_Gl' );
2712    END IF;
2713   END LOOP;
2714   /* calculate total GL pool amount */
2715   OPEN gl_eligible_amount ;
2716   FETCH gl_eligible_amount INTO v_amount;
2717   CLOSE gl_eligible_amount ;
2718   x_gl_src_amount:= NVL(v_amount, 0);
2719   pa_debug.reset_err_stack;
2720 EXCEPTION
2721   WHEN OTHERS THEN
2722   pa_debug.G_err_code:= SQLCODE;
2723   RAISE;
2724 END calculate_src_GL_amounts;
2725 -- ------------------------------------------------------------
2726 -- get_trg_line_proj_task_count
2727 -- ------------------------------------------------------------
2728 FUNCTION get_trg_line_proj_task_count( p_run_id IN NUMBER
2729                                      , p_line_num IN NUMBER ) RETURN NUMBER IS
2730 x_count NUMBER:=0;
2731 CURSOR C_get_count IS
2732   SELECT count(task_id)
2733   FROM pa_alloc_run_targets
2734   WHERE run_id = p_run_id
2735   AND line_num = p_line_num;
2736 BEGIN
2737   pa_debug.set_err_stack ('Get Proj_task Count For Each Target Line');
2738   OPEN C_get_count;
2739   FETCH C_get_count INTO x_count;
2740   CLOSE C_get_count;
2741   pa_debug.reset_err_stack ;
2742   return x_count;
2743 EXCEPTION
2744   WHEN OTHERS THEN
2745     return (SQLCODE);
2746 --    return 0;
2747 END get_trg_line_proj_task_count;
2748 -- ------------------------------------------------------------
2749 -- insert_missing_costs
2750 -- ------------------------------------------------------------
2751 PROCEDURE insert_missing_costs(     p_run_id              IN NUMBER
2752                                   , p_type_code           IN VARCHAR2
2753                                   , p_project_id          IN NUMBER
2754                                   , p_task_id             IN NUMBER
2755                                   , p_amount  IN NUMBER )  IS
2756 BEGIN
2757   pa_debug.set_err_stack('Insert missing project costs');
2758   INSERT INTO pa_alloc_missing_costs (
2759       RUN_ID
2760     , TYPE_CODE
2761     , CREATION_DATE
2762     , CREATED_BY
2763     , LAST_UPDATE_DATE
2764     , LAST_UPDATED_BY
2765     , LAST_UPDATE_LOGIN
2766     , PROJECT_ID
2767     , TASK_ID
2768     , AMOUNT)
2769   VALUES (
2770       p_run_id
2771     , p_type_code
2772     , G_creation_date
2773     , G_created_by
2774     , G_last_update_date
2775     , G_last_updated_by
2776     , G_last_update_login
2777     , p_project_id
2778     , p_task_id
2779     , p_amount);
2780   pa_debug.reset_err_stack;
2781 EXCEPTION
2782   WHEN OTHERS THEN
2783     pa_debug.G_err_code := SQLCODE;
2784     RAISE;
2785 END insert_missing_costs;
2786 ---------------------------------------------------------------
2787 -- get_sunk_cost
2788 -- ------------------------------------------------------------
2789 PROCEDURE get_sunk_cost( p_rule_id IN NUMBER
2790                       , p_run_id  IN NUMBER
2791                       , p_fiscal_year IN NUMBER
2792                       , p_quarter_num IN NUMBER
2793                       , p_period_num  IN NUMBER
2794                       , p_amount_type IN VARCHAR2
2795                       , x_src_sunk_cost OUT NOCOPY NUMBER
2796                       , x_tgt_sunk_cost OUT NOCOPY NUMBER
2797                       , p_src_proj_id  IN NUMBER    ) IS
2798 -- the parameter p_src_proj_id is added to get the sunk cost for given project.
2799 -- This will be used only when called from the create_offset_txns procedure.
2800 -- So if this parameter is not null then, the target sunk cost calculations and
2801 -- the insert operations into the missing cost table are not required
2802 /* The cursor definition was changed in bug 3157765
2803 CURSOR C_src_sunk_cost( p_fscl_year IN NUMBER
2804         , p_qrtr_num  IN NUMBER
2805         , p_prd_num   IN NUMBER ) IS
2806   SELECT a.run_id run_id
2807   , a.project_id  project_id
2808   , a.task_id     task_id
2809   , nvl(a.eligible_amount,0) eligible_amount
2810   FROM pa_alloc_run_source_det a,
2811        pa_alloc_runs ar
2812   WHERE a.run_id = ar.run_id
2813   AND  a.rule_id = p_rule_id
2814   AND  ar.fiscal_year = NVL(p_fscl_year, ar.fiscal_year)
2815   AND  ar.quarter = NVL(p_qrtr_num, ar.quarter)
2816   AND ar.period_num = NVL(p_prd_num, ar.period_num )
2817   AND  a.run_id < p_run_id
2818   AND NOT EXISTS ( SELECT 1
2819                   FROM pa_alloc_run_source_det b
2820                   WHERE a.project_id = b.project_id
2821                   AND a.task_id = b.task_id
2822                   AND b.rule_id = p_rule_id
2823                   AND b.run_id = p_run_id )
2824   AND a.run_id =( SELECT max(c.run_id)
2825                   FROM pa_alloc_run_source_det c,
2826                        pa_alloc_runs c_ar    -- added this table to exclude reversed runs.
2827                   WHERE c.project_id = a.project_id
2828                   AND c.task_id = a.task_id
2829                   AND c.run_id = c_ar.run_id
2830                   AND c_ar.rule_id = p_rule_id
2831                   AND c_ar.run_status = 'RS'
2832                   AND c.run_id < p_run_id )
2833  AND  a.project_id > 0   -- This is added to ignore the missing cost from fixed amount.
2834  AND  a.project_id = nvl(p_src_proj_id, a.project_id) ;
2835 *******/
2836 CURSOR C_Src_Sunk_Cost
2837     (  p_Fscl_Year    IN NUMBER
2838     ,  p_Qrtr_Num      IN NUMBER
2839     ,  p_Prd_Num      IN NUMBER
2840     )  IS
2841    SELECT  A.Run_Id                        Run_Id,
2842            A.Project_Id                    Project_Id,
2843            A.Task_Id                       Task_Id,
2844            NVL ( A.Eligible_Amount, 0 )    Eligible_Amount
2845    FROM    PA_ALLOC_RUN_SOURCE_DET    A ,
2846     (  --
2847        -- The purpose of this in-line view is to return
2848        --  the PA_ALLOC_RUNS (single) record for the largest Run_Id
2849        --  less than the input p_Run_Id
2850        --
2851        SELECT  MAX ( AR.Run_Id ) AS Run_Id
2852        FROM    PA_ALLOC_RUNS              AR
2853        WHERE  AR.Fiscal_Year  = NVL ( p_Fscl_Year , AR.Fiscal_Year )
2854        AND    AR.Quarter      = NVL ( p_Qrtr_Num  , AR.Quarter  )
2855        AND    AR.Period_Num  = NVL ( p_Prd_Num  , AR.Period_Num  )
2856        AND    AR.Run_Id      < p_Run_Id
2857        AND    AR.Rule_Id      = p_Rule_Id
2858        AND    AR.Run_Status  = 'RS'
2859        AND    NVL(AR.allocated_amount,0) <> 0 -- added this as part of the bug 13556282
2860      )  AR
2861     WHERE  A.Run_Id        = AR.Run_Id
2862     AND    A.Rule_Id      = p_Rule_Id
2863     AND NOT EXISTS
2864            (  SELECT  1
2865               FROM    PA_ALLOC_RUN_SOURCE_DET    B
2866               WHERE  A.Project_Id    = B.Project_Id
2867               AND    A.Task_Id      = B.Task_Id
2868               AND    B.Rule_Id      = p_Rule_Id
2869               AND    B.Run_Id        = p_Run_Id
2870            )
2871     AND  A.Project_Id > 0
2872     AND  A.Project_Id = NVL ( p_Src_Proj_Id, A.Project_Id ) ;
2873 /**** Changes for bug-3157765 ends here *****/
2874 CURSOR C_trg_sunk_cost( p_fscl_year IN NUMBER
2875         , p_qrtr_num  IN NUMBER
2876         , p_prd_num   IN NUMBER ) IS
2877   SELECT a.run_id  run_id
2878   , a.project_id  project_id
2879   , a.task_id     task_id
2880   , nvl(a.Total_allocation,0) Total_allocation
2881   FROM pa_alloc_txn_details a,
2882        pa_alloc_runs ar
2883   WHERE a.run_id = ar.run_id
2884   AND  a.rule_id = p_rule_id
2885   AND  ar.rule_id = p_rule_id /* Bug 10191044 */
2886   AND  ar.fiscal_year = NVL(p_fscl_year, ar.fiscal_year)
2887   AND  ar.quarter = NVL(p_qrtr_num, ar.quarter)
2888   AND ar.period_num = NVL(p_prd_num, ar.period_num )
2889   AND  a.run_id < p_run_id
2890   AND  a.transaction_type  = 'T'
2891   AND NOT EXISTS ( SELECT 1
2892                   FROM pa_alloc_run_targets b
2893                   WHERE b.project_id = a.project_id
2894                     AND b.task_id = a.task_id
2895                     AND b.exclude_flag    = 'N'
2896                     AND b.run_id = p_run_id )
2897   AND a.run_id =( SELECT max(c.run_id)
2898                   FROM pa_alloc_txn_details c,
2899                        pa_alloc_runs c_ar   -- added this table to exclude reversed runs.
2900                   WHERE c.project_id = a.project_id
2901                   AND c.task_id = a.task_id
2902                   AND c.transaction_type    = 'T'
2903                   AND c.run_id = c_ar.run_id
2904                   AND c_ar.rule_id = p_rule_id
2905                   AND c_ar.run_status = 'RS'
2906                   AND c.run_id < p_run_id );
2907 v_tot_sunk_cost NUMBER;
2908 v_src_sunk_cost NUMBER;
2909 v_trg_sunk_cost NUMBER;
2910 v_prev_amount NUMBER:=0;
2911 v_quarter_num NUMBER;
2912 v_fiscal_year NUMBER;
2913 v_period_num NUMBER;
2914 BEGIN
2915   pa_debug.set_err_stack('Get_Sunk_Cost');
2916   IF P_DEBUG_MODE = 'Y' THEN
2917      pa_debug.write_file('get_sunk_cost: ' || 'LOG', 'p_amount_type '||p_amount_type);
2918   END IF;
2919   IF( p_amount_type = 'ITD') THEN
2920     v_fiscal_year:= NULL;
2921     v_quarter_num:= NULL;
2922     v_period_num := NULL;
2923   ELSIF (p_amount_type = 'FYTD') THEN
2924     v_fiscal_year:= p_fiscal_year;
2925     v_quarter_num:= NULL;
2926     v_period_num := NULL;
2927   ELSIF (p_amount_type = 'QTD') THEN
2928     v_fiscal_year:= p_fiscal_year;
2929     v_quarter_num:= p_quarter_num;
2930     v_period_num := NULL;
2931   ELSE
2932     v_fiscal_year:= p_fiscal_year;
2933     v_quarter_num:= p_quarter_num;
2934     v_period_num := p_period_num;
2935   END IF;
2936 v_src_sunk_cost :=0;
2937 v_trg_sunk_cost :=0;
2938   /* calculate total src_sunk_cost */
2939   IF P_DEBUG_MODE = 'Y' THEN
2940      pa_debug.write_file('get_sunk_cost: ' || 'LOG',' calculate total src_sunk_cost' );
2941   END IF;
2942   FOR src_sunk_cost_rec IN C_src_sunk_cost( v_fiscal_year
2943                                           , v_quarter_num
2944                                           , v_period_num ) LOOP
2945     IF P_DEBUG_MODE = 'Y' THEN
2946        pa_debug.write_file('get_sunk_cost: ' || 'LOG','src_sunk_cost :'|| to_char(src_sunk_cost_rec.eligible_amount) );
2947     END IF;
2948     if p_src_proj_id is  NULL then
2949      insert_missing_costs( p_run_id
2950                           ,'S'
2951                           ,src_sunk_cost_rec.project_id
2952                           ,src_sunk_cost_rec.task_id
2953                           ,src_sunk_cost_rec.eligible_amount);
2954     end if ;
2955     v_src_sunk_cost := v_src_sunk_cost + src_sunk_cost_rec.eligible_amount;
2956   END LOOP;
2957   x_src_sunk_cost := nvl(v_src_sunk_cost, 0) ;
2958   IF P_DEBUG_MODE = 'Y' THEN
2959      pa_debug.write_file('get_sunk_cost: ' || 'LOG','v_src_sunk_cost =' ||to_char(v_src_sunk_cost) );
2960   END IF;
2961  /* calculate total trgt_sunk_cost */
2962 if p_src_proj_id is  NULL then
2963   IF P_DEBUG_MODE = 'Y' THEN
2964      pa_debug.write_file('get_sunk_cost: ' || 'LOG',' calculate total tgt_sunk_cost ' || to_char(v_fiscal_year)
2965           || ' ' || to_char(v_quarter_num) || ' '
2966           || to_char(v_period_num) );
2967   END IF;
2968   FOR trg_sunk_cost_rec IN C_trg_sunk_cost( v_fiscal_year
2969                                           , v_quarter_num
2970                                           , v_period_num ) LOOP
2971   IF P_DEBUG_MODE = 'Y' THEN
2972      pa_debug.write_file('get_sunk_cost: ' || 'LOG','tgt_sunk_cost = '|| to_char(trg_sunk_cost_rec.Total_allocation) );
2973   END IF;
2974      insert_missing_costs( p_run_id
2975                           ,'T'
2976                           ,trg_sunk_cost_rec.project_id
2977                           ,trg_sunk_cost_rec.task_id
2978                           ,trg_sunk_cost_rec.Total_allocation);
2979     v_trg_sunk_cost := v_trg_sunk_cost + trg_sunk_cost_rec.Total_allocation;
2980   END LOOP;
2981   x_tgt_sunk_cost := nvl(v_trg_sunk_cost,0) ;
2982  IF P_DEBUG_MODE = 'Y' THEN
2983     pa_debug.write_file('get_sunk_cost: ' || 'LOG','v_tgt_sunk_cost =' ||to_char(x_tgt_sunk_cost) );
2984  END IF;
2985 End if ;
2986   v_tot_sunk_cost := NVL(v_src_sunk_cost,0) - NVL(v_trg_sunk_cost,0);
2987   pa_debug.reset_err_stack;
2988 EXCEPTION
2989   WHEN OTHERS THEN
2990    null;
2991  --    return (SQLCODE);
2992 --       return 0;
2993 END get_sunk_cost;
2994 -- ------------------------------------------------------------
2995 -- get_previous_alloc_amnt
2996 -- ------------------------------------------------------------
2997 FUNCTION get_previous_alloc_amnt ( p_rule_id     IN NUMBER
2998                                  , p_run_id      IN NUMBER
2999                                  , p_project_id  IN NUMBER
3000                                  , p_task_id     IN NUMBER
3001                                  , p_quarter_num IN NUMBER
3002                                  , p_fiscal_year IN NUMBER
3003                                  , p_period_num  IN NUMBER
3004                                  , p_type        IN VARCHAR2
3005                                  , p_amount_type IN VARCHAR2 ) RETURN NUMBER IS
3006 CURSOR C_prev_amount( p_qrtr_num  IN NUMBER
3007         , p_fscl_year IN NUMBER
3008         , p_prd_num   IN NUMBER ) IS
3009   SELECT nvl(sum(nvl(a.current_allocation,0)),0)
3010   FROM pa_alloc_txn_details a,
3011        pa_alloc_runs b
3012   WHERE b.rule_id = p_rule_id
3013     AND b.run_id < p_run_id
3014     AND b.quarter = nvl(p_qrtr_num, b.quarter)
3015     AND b.fiscal_year = nvl(p_fscl_year, b.fiscal_year)
3016     AND b.period_num = nvl(p_prd_num, b.period_num)
3017     AND b.run_id = a.run_id
3018     AND a.transaction_type = p_type
3019     AND a.project_id = p_project_id
3020     AND a.task_id = p_task_id
3021     AND b.reversal_date is NULL
3022     AND b.run_status <> 'DL'; /* for bug 2176096 */
3023 v_prev_amount NUMBER:=0;
3024 v_quarter_num NUMBER;
3025 v_fiscal_year NUMBER;
3026 v_period_num NUMBER;
3027 BEGIN
3028   v_prev_amount :=0;
3029   pa_debug.set_err_stack('Get_Previous_Alloc_Amount');
3030   IF( p_amount_type = 'ITD') THEN
3031     v_fiscal_year:= NULL;
3032     v_quarter_num:= NULL;
3033     v_period_num := NULL;
3034   ELSIF (p_amount_type = 'FYTD') THEN
3035     v_fiscal_year:= p_fiscal_year;
3036     v_quarter_num:= NULL;
3037     v_period_num := NULL;
3038   ELSIF (p_amount_type = 'QTD') THEN
3039     v_fiscal_year:= p_fiscal_year;
3040     v_quarter_num:= p_quarter_num;
3041     v_period_num := NULL;
3042   ELSE
3043     v_fiscal_year:= p_fiscal_year;
3044     v_quarter_num:= p_quarter_num;
3045     v_period_num := p_period_num;
3046   END IF;
3047   /* returns the previous allocated amount for the
3048    passed rule_id, project_id and task_id */
3049   OPEN C_prev_amount ( v_quarter_num, v_fiscal_year, v_period_num );
3050   FETCH C_prev_amount INTO v_prev_amount;
3051   CLOSE C_prev_amount;
3052   IF P_DEBUG_MODE = 'Y' THEN
3053      pa_debug.write_file('get_previous_alloc_amnt: ' || 'LOG','rule_id:run_id:year:quarter:period:project:task:Amt='||
3054          to_char(p_rule_id)||': '|| to_char(p_run_id)||': '||
3055          to_char(v_fiscal_year)||': '|| to_char(v_quarter_num)||': '||
3056          to_char(v_period_num)||': '|| to_char(p_project_id)||': '||
3057          to_char(p_task_id)||':'||to_char(v_prev_amount));
3058   END IF;
3059   pa_debug.reset_err_stack;
3060   return nvl(v_prev_amount,0);
3061 EXCEPTION
3062   WHEN NO_DATA_FOUND THEN
3063          return 0;
3064   WHEN OTHERS THEN
3065          return (SQLCODE);
3066 END get_previous_alloc_amnt;
3067 -- ------------------------------------------------------------
3068 -- insert_alloc_txn_details
3069 -- ------------------------------------------------------------
3070 PROCEDURE insert_alloc_txn_details( x_alloc_txn_id        IN OUT NOCOPY NUMBER
3071                                   , p_run_id              IN NUMBER
3072                                   , p_rule_id             IN NUMBER
3073                                   , p_transaction_type    IN VARCHAR2
3074                                   , p_fiscal_year         IN NUMBER
3075                                   , p_quarter_num         IN NUMBER
3076                                   , p_period_num          IN NUMBER
3077                                   , p_run_period          IN VARCHAR2
3078                                   , p_line_num            IN NUMBER
3079                                   , p_project_id          IN NUMBER
3080                                   , p_task_id             IN NUMBER
3081                                   , p_expenditure_type    IN VARCHAR2
3082                                   , p_total_allocation    IN NUMBER
3083                                   , p_previous_allocation IN NUMBER
3084                                   , p_current_allocation  IN NUMBER
3085                                  /* PA.L:Added for Capitalized Interest */
3086                                   , p_EXPENDITURE_ID      IN NUMBER   DEFAULT NULL
3087                                   , p_EXPENDITURE_ITEM_ID IN NUMBER   DEFAULT NULL
3088                                   , p_CINT_SOURCE_TASK_ID IN NUMBER   DEFAULT NULL
3089                                   , p_CINT_EXP_ORG_ID     IN NUMBER   DEFAULT NULL
3090                                   , p_CINT_RATE_MULTIPLIER IN NUMBER   DEFAULT NULL
3091                                   , p_CINT_PRIOR_BASIS_AMT IN NUMBER   DEFAULT NULL
3092                                   , p_CINT_CURRENT_BASIS_AMT IN NUMBER   DEFAULT NULL
3093                                   , p_REJECTION_CODE      IN VARCHAR2 DEFAULT NULL
3094                                   , p_STATUS_CODE         IN VARCHAR2 DEFAULT NULL
3095                                   , p_ATTRIBUTE_CATEGORY  IN VARCHAR2 DEFAULT NULL
3096                                   , p_ATTRIBUTE1          IN VARCHAR2 DEFAULT NULL
3097                                   , p_ATTRIBUTE2          IN VARCHAR2 DEFAULT NULL
3098                                   , p_ATTRIBUTE3          IN VARCHAR2 DEFAULT NULL
3099                                   , p_ATTRIBUTE4          IN VARCHAR2 DEFAULT NULL
3100                                   , p_ATTRIBUTE5          IN VARCHAR2 DEFAULT NULL
3101                                   , p_ATTRIBUTE6          IN VARCHAR2 DEFAULT NULL
3102                                   , p_ATTRIBUTE7          IN VARCHAR2 DEFAULT NULL
3103                                   , p_ATTRIBUTE8          IN VARCHAR2 DEFAULT NULL
3104                                   , p_ATTRIBUTE9          IN VARCHAR2 DEFAULT NULL
3105                                   , p_ATTRIBUTE10         IN VARCHAR2 DEFAULT NULL
3106                                 /* PA.L : end */
3107 				 ) IS
3108 v_attribute_category VARCHAR2(30);
3109 v_attribute1  VARCHAR2(150);
3110 v_attribute2  VARCHAR2(150);
3111 v_attribute3  VARCHAR2(150);
3112 v_attribute4  VARCHAR2(150);
3113 v_attribute5  VARCHAR2(150);
3114 v_attribute6  VARCHAR2(150);
3115 v_attribute7  VARCHAR2(150);
3116 v_attribute8  VARCHAR2(150);
3117 v_attribute9  VARCHAR2(150);
3118 v_attribute10  VARCHAR2(150);
3119 v_status NUMBER ;
3120 v_err_message VARCHAR2(250);
3121 BEGIN
3122   pa_debug.set_err_stack('Insert Alloc Txn Details');
3123  /* Added if condition to avoid calling client extns for capint if p_rule_id =-1 */
3124  IF nvl(p_rule_id,0) <> -1 Then
3125    --For Capitalized Interest the DFF will be derived outside the
3126    --table handler by calling client extn
3127   If p_transaction_type='T' then
3128       PA_CLIENT_EXTN_ALLOC.txn_dff_extn( p_rule_id
3129                        ,p_run_id
3130                        ,p_transaction_type
3131                        ,p_project_id
3132                        ,P_task_id
3133                        ,G_tgt_expnd_org
3134                        ,G_tgt_expnd_type_class
3135                        ,G_tgt_expnd_type
3136                        ,v_attribute_category
3137                        ,v_attribute1
3138                        ,v_attribute2
3139                        ,v_attribute3
3140                        ,v_attribute4
3141                        ,v_attribute5
3142                        ,v_attribute6
3143                        ,v_attribute7
3144                        ,v_attribute8
3145                        ,v_attribute9
3146                        ,v_attribute10
3147                        , v_status
3148                        , v_err_message
3149                      ) ;
3150      IF nvl(v_status,0) <> 0 then
3151         v_err_message:=nvl(v_err_message,'PA_AL_CE_FAILED');
3152         IF P_DEBUG_MODE = 'Y' THEN
3153            pa_debug.write_file('insert_alloc_txn_details: ' || 'LOG',v_err_message);
3154         END IF;
3155         alloc_errors(p_rule_id, p_run_id, 'R', 'E',v_err_message, TRUE) ;
3156      END IF ;
3157    Elsif  p_transaction_type='O' then
3158      PA_CLIENT_EXTN_ALLOC.txn_dff_extn( p_rule_id
3159                        ,p_run_id
3160                        ,p_transaction_type
3161                        ,p_project_id
3162                        ,P_task_id
3163                        ,G_offset_expnd_org
3164                        ,G_offset_expnd_type_class
3165                        ,G_offset_expnd_type
3166                        ,v_attribute_category
3167                        ,v_attribute1
3168                        ,v_attribute2
3169                        ,v_attribute3
3170                        ,v_attribute4
3171                        ,v_attribute5
3172                        ,v_attribute6
3173                        ,v_attribute7
3174                        ,v_attribute8
3175                        ,v_attribute9
3176                        ,v_attribute10
3177                        , v_status
3178                        , v_err_message
3179                      ) ;
3180        IF nvl(v_status,0) <> 0 then
3181            v_err_message:=nvl(v_err_message,'PA_AL_CE_FAILED');
3182            IF P_DEBUG_MODE = 'Y' THEN
3183               pa_debug.write_file('insert_alloc_txn_details: ' || 'LOG',v_err_message);
3184            END IF;
3185            alloc_errors(p_rule_id, p_run_id, 'R', 'E',v_err_message, TRUE) ;
3186        END IF ;
3187     End if;
3188   END IF ;  -- end of p_rule_id <> -1
3189   If x_alloc_txn_id is Null Then
3190       Select pa_alloc_txn_details_s.nextval
3191       Into x_alloc_txn_id
3192       From Dual;
3193   End If;
3194   INSERT INTO pa_alloc_txn_details (
3195       RUN_ID
3196     , RULE_ID
3197     , TRANSACTION_TYPE
3198     , FISCAL_YEAR
3199     , QUARTER_NUM
3200     , PERIOD_NUM
3201     , RUN_PERIOD
3202     , LINE_NUM
3203     , CREATION_DATE
3204     , CREATED_BY
3205     , LAST_UPDATE_DATE
3206     , LAST_UPDATED_BY
3207     , LAST_UPDATE_LOGIN
3208     , PROJECT_ID
3209     , TASK_ID
3210     , EXPENDITURE_TYPE
3211     , TOTAL_ALLOCATION
3212     , PREVIOUS_ALLOCATION
3213     , CURRENT_ALLOCATION
3214     , ALLOC_TXN_ID
3215     , ATTRIBUTE_CATEGORY
3216     ,ATTRIBUTE1
3217     ,ATTRIBUTE2
3218     ,ATTRIBUTE3
3219     ,ATTRIBUTE4
3220     ,ATTRIBUTE5
3221     ,ATTRIBUTE6
3222     ,ATTRIBUTE7
3223     ,ATTRIBUTE8
3224     ,ATTRIBUTE9
3225     ,ATTRIBUTE10
3226     ,EXPENDITURE_ID
3227     ,EXPENDITURE_ITEM_ID
3228     ,CINT_SOURCE_TASK_ID
3229     ,CINT_EXP_ORG_ID
3230     ,CINT_RATE_MULTIPLIER
3231     ,CINT_PRIOR_BASIS_AMT
3232     ,CINT_CURRENT_BASIS_AMT
3233     ,REJECTION_CODE
3234     ,STATUS_CODE
3235 )
3236   VALUES (
3237       p_run_id
3238     , p_rule_id
3239     , p_transaction_type
3240     , p_fiscal_year
3241     , p_quarter_num
3242     , p_period_num
3243     , p_run_period
3244     , p_line_num
3245     , G_creation_date
3246     , G_created_by
3247     , G_last_update_date
3248     , G_last_updated_by
3249     , G_last_update_login
3250     , p_project_id
3251     , p_task_id
3252     , p_expenditure_type
3253     , p_total_allocation
3254     , p_previous_allocation
3255     , p_current_allocation
3256     , x_alloc_txn_id --, pa_alloc_txn_details_s.nextval
3257     ,decode(p_rule_id,-1,p_attribute_category,v_attribute_category)
3258     ,decode(p_rule_id,-1,p_attribute1,v_attribute1)
3259     ,decode(p_rule_id,-1,p_attribute2,v_attribute2)
3260     ,decode(p_rule_id,-1,p_attribute3,v_attribute3)
3261     ,decode(p_rule_id,-1,p_attribute4,v_attribute4)
3262     ,decode(p_rule_id,-1,p_attribute5,v_attribute5)
3263     ,decode(p_rule_id,-1,p_attribute6,v_attribute6)
3264     ,decode(p_rule_id,-1,p_attribute7,v_attribute7)
3265     ,decode(p_rule_id,-1,p_attribute8,v_attribute8)
3266     ,decode(p_rule_id,-1,p_attribute9,v_attribute9)
3267     ,decode(p_rule_id,-1,p_attribute10,v_attribute10)
3268     ,p_EXPENDITURE_ID
3269     ,p_EXPENDITURE_ITEM_ID
3270     ,p_CINT_SOURCE_TASK_ID
3271     ,p_CINT_EXP_ORG_ID
3272     ,p_CINT_RATE_MULTIPLIER
3273     ,p_CINT_PRIOR_BASIS_AMT
3274     ,p_CINT_CURRENT_BASIS_AMT
3275     ,p_REJECTION_CODE
3276     ,p_STATUS_CODE
3277  );
3278   pa_debug.reset_err_stack;
3279 EXCEPTION
3280   WHEN OTHERS THEN
3281     pa_debug.G_err_code := SQLCODE;
3282     RAISE;
3283 END insert_alloc_txn_details;
3284 -- ------------------------------------------------------------
3285 -- create_target_txns
3286 -- ------------------------------------------------------------
3287 PROCEDURE create_target_txns( p_rule_id           IN NUMBER
3288                             , p_run_id            IN NUMBER
3289                             , p_type              IN VARCHAR2
3290                             , p_fiscal_year       IN NUMBER
3291                             , p_quarter_num       IN NUMBER
3292                             , p_period_num        IN NUMBER
3293                             , p_run_period        IN VARCHAR2
3294                             , p_expenditure_type  IN VARCHAR2
3295                             , p_allocation_method IN VARCHAR2
3296                             , p_basis_method      IN VARCHAR2
3297                             , p_amount_type       IN VARCHAR2
3298                             , p_pool_amount       IN NUMBER
3299                             , x_curr_alloc_amount OUT NOCOPY NUMBER  ) IS
3300 CURSOR C_run_basis_det( p_run_id       IN NUMBER
3301                       , p_basis_method IN VARCHAR2
3302                       , p_line_num     IN NUMBER
3303                       , p_project_id   IN NUMBER
3304                       , p_task_id      IN NUMBER ) IS
3305   SELECT nvl(sum( nvl(basis_percent, 0)*nvl(line_percent,100)/10000),0) basis
3306   FROM pa_alloc_run_basis_det
3307   WHERE run_id = p_run_id
3308   AND line_num = p_line_num
3309   AND project_id = p_project_id
3310   AND task_id = p_task_id ;
3311 CURSOR C_run_targets IS
3312   SELECT line_num
3313   , project_id
3314   , task_id
3315   , line_percent
3316   , exclude_flag
3317   FROM pa_alloc_run_targets
3318   WHERE run_id = p_run_id
3319   AND exclude_flag = 'N';
3320 /* added p_run_id parameter for bug 1900331 */
3321 CURSOR C_all_proj_task_count (p_run_id IN NUMBER) IS
3322   SELECT count(task_id)
3323   FROM pa_alloc_run_targets
3324   WHERE run_id = p_run_id
3325   AND exclude_flag = 'N';
3326 CURSOR C_count_of_trg_lines IS
3327   SELECT COUNT(DISTINCT line_num)
3328   FROM pa_alloc_run_targets
3329   WHERE run_id = p_run_id
3330   AND exclude_flag = 'N';
3331 v_quarter_num NUMBER;
3332 v_fiscal_year NUMBER;
3333 v_period_num NUMBER;
3334 cursor c_sum_of_allocated_amts is
3335   select NVL(sum ( nvl(par1.allocated_amount,0)),0)
3336     from pa_alloc_runs par1
3337    where par1.run_id < p_run_id
3338      and par1.rule_id = p_rule_id
3339      and par1.fiscal_year = nvl(v_fiscal_year, par1.fiscal_year)
3340      and par1.quarter     = nvl(v_quarter_num, par1.quarter)
3341      and par1.period_num = nvl(v_period_num, par1.period_num)
3342      and par1.reversal_date is NULL
3343      and par1.run_status <> 'DL'; /* for bug 2176096 */
3344 v_sunk_cost NUMBER :=0;
3345 v_src_sunk_cost NUMBER :=0;
3346 v_tgt_sunk_cost NUMBER :=0;
3347 v_tot_pool_amount NUMBER :=0;
3348 v_curr_alloc_amount NUMBER :=0;
3349 v_prev_alloc_amount NUMBER :=0;
3350 v_tot_alloc_amount NUMBER :=0;
3351 v_count NUMBER :=0;
3352 v_count2 NUMBER :=0;
3353 v_factor NUMBER :=0;
3354 v_remnant_amount NUMBER;
3355 v_net_alloc_amount NUMBER;
3356 v_sum_alloc_amts  NUMBER ;
3357 v_sum_tot_alloc_amt NUMBER;
3358 l_alloc_txn_id      NUMBER := NULL; /** added for capint changes */
3359 Function get_basis_factor( p_run_id       IN NUMBER
3360                           ,p_basis_method IN VARCHAR2
3361                           ,p_line_num     IN NUMBER
3362                           ,p_project_id   IN NUMBER
3363                           ,p_task_id      IN NUMBER ) return NUMBER
3364 IS
3365  v_basis   number ;
3366 BEGIN
3367   OPEN C_run_basis_det(p_run_id,p_basis_method,p_line_num, p_project_id, p_task_id) ;
3368   FETCH C_run_basis_det INTO v_basis;
3369   IF C_run_basis_det%NOTFOUND then  /* Bug 2182563 */
3370     CLOSE C_run_basis_det ;
3371     return 0 ;
3372   END IF ;
3373   CLOSE C_run_basis_det ;
3374   return NVL(v_basis,0) ;
3375 EXCEPTION
3376   WHEN OTHERS THEN
3377     return (SQLCODE);
3378 --    return 0 ;
3379 END get_basis_factor ;
3380 BEGIN
3381   pa_debug.set_err_stack('Create_Target_txns');
3382   pa_debug.G_err_stage:= 'CREATING TARGET TRANSACTIONS';
3383   IF P_DEBUG_MODE = 'Y' THEN
3384      pa_debug.write_file('create_target_txns: ' || 'LOG', pa_debug.G_err_stage);
3385      pa_debug.write_file('create_target_txns: ' || 'LOG','p_pool_amount : '|| to_char(p_pool_amount));
3386   END IF;
3387   v_tot_pool_amount:= p_pool_amount;
3388   IF p_allocation_method = 'I' THEN
3389     /* if allocation method is Incremental then consider sunk cost */
3390     get_sunk_cost(p_rule_id
3391                   , p_run_id
3392                   , p_fiscal_year
3393                   , p_quarter_num
3394                   , p_period_num
3395                   , p_amount_type
3396                   , v_src_sunk_cost
3397                   , v_tgt_sunk_cost
3398                   , NULL );
3399      v_sunk_cost :=NVL( v_src_sunk_cost,0) - NVL( v_tgt_sunk_cost,0) ;
3400 IF P_DEBUG_MODE = 'Y' THEN
3401    pa_debug.write_file('create_target_txns: ' || 'LOG','v_src_sunk_cost is: '||to_char(v_src_sunk_cost) );
3402    pa_debug.write_file('create_target_txns: ' || 'LOG','v_tgt_sunk_cost is: '||to_char(v_tgt_sunk_cost) );
3403      pa_debug.write_file('create_target_txns: ' || 'LOG','v_sunk_cost is: '||to_char(v_sunk_cost) );
3404     pa_debug.write_file('create_target_txns: ' || 'LOG','v_tot_pool_amount_before_ is: '||to_char(v_tot_pool_amount) );
3405  END IF;
3406     v_tot_pool_amount := NVL(v_tot_pool_amount,0) + NVl(v_sunk_cost, 0);
3407  IF P_DEBUG_MODE = 'Y' THEN
3408     pa_debug.write_file('create_target_txns: ' || 'LOG','v_tot_pool_amount_after_ is: '||to_char(v_tot_pool_amount) );
3409  END IF;
3410   END IF;
3411  IF( p_basis_method = 'S') THEN
3412     /* get count of total number of project/tasks for targets */
3413     OPEN C_all_proj_task_count(p_run_id); /* 1900331 added p_run_id */
3414     FETCH C_all_proj_task_count
3415     INTO v_count;
3416     CLOSE C_all_proj_task_count;
3417     IF (v_count>0) THEN
3418       v_factor := 1/ v_count;
3419     END IF;
3420  END IF;
3421  FOR run_target_rec IN C_run_targets LOOP
3422     IF (p_basis_method = 'FS') THEN
3423       /* get the count of project/tasks for the current line */
3424       v_count2 := get_trg_line_proj_task_count( p_run_id,
3425                                                 run_target_rec.line_num );
3426       IF (v_count2 >0) THEN
3427         v_factor := (NVL(run_target_rec.line_percent, 0)/100 )/ v_count2;
3428       END IF; /* end v_count2 */
3429     END IF; /* if p_basis_method */
3430     IF ( p_basis_method in ( 'P','FP','C' )) THEN
3431       IF P_DEBUG_MODE = 'Y' THEN
3432          pa_debug.write_file('create_target_txns: ' || 'LOG','In basis_method'||p_basis_method);
3433       END IF;
3434       v_factor := get_basis_factor( p_run_id,
3435                                     p_basis_method,
3436                                     run_target_rec.line_num,
3437                                     run_target_rec.project_id,
3438                                     run_target_rec.task_id ) ;
3439       IF P_DEBUG_MODE = 'Y' THEN
3440          pa_debug.write_file('create_target_txns: ' || 'LOG','V_factor is :'|| to_char(v_factor));
3441       END IF;
3442     END IF ;
3443          /* if basis amount is zero for a particular proj-task then show warning */
3444          IF (v_factor = 0 ) THEN
3445                   alloc_errors( p_rule_id, p_run_id, 'B','W',
3446                     'PA_AL_ZERO_TXN_BASIS_AMT', FALSE, 'Y',
3447                     run_target_rec.project_id, run_target_rec.task_id );
3448          END IF;
3449     v_curr_alloc_amount :=     v_tot_pool_amount * v_factor ;
3450  IF P_DEBUG_MODE = 'Y' THEN
3451     pa_debug.write_file('create_target_txns: ' || 'LOG', 'v_current_alloc_amount :'||to_char(v_curr_alloc_amount) );
3452  END IF;
3453     v_tot_alloc_amount := v_curr_alloc_amount;
3454     v_sum_tot_alloc_amt := v_sum_tot_alloc_amt + v_tot_alloc_amount ;
3455     IF (p_allocation_method = 'F') THEN
3456         v_prev_alloc_amount := 0;
3457     ELSE   /* if p_allocation_method = 'I' */
3458         v_prev_alloc_amount := get_previous_alloc_amnt ( p_rule_id
3459                                                        , p_run_id
3460                                                        , run_target_rec.project_id
3461                                                        , run_target_rec.task_id
3462                                                        , p_quarter_num
3463                                                        , p_fiscal_year
3464                                                        , p_period_num
3465                                                        , 'T'
3466                                                        , p_amount_type );
3467 --        v_curr_alloc_amount := NVl(v_curr_alloc_amount,0) - NVl(v_prev_alloc_amount, 0); -- commented as part of the bug 13949738
3468 
3469     END IF;
3470 -- Start: added as part of the bug 13949738
3471    IF v_curr_alloc_amount <> 0 THEN
3472         v_curr_alloc_amount := NVl(v_curr_alloc_amount,0) - NVl(v_prev_alloc_amount, 0);
3473    END IF;
3474 -- End:added as part of the bug 13949738
3475 
3476       v_tot_alloc_amount:= pa_currency.round_currency_amt(v_tot_alloc_amount);
3477       v_prev_alloc_amount:= pa_currency.round_currency_amt(v_prev_alloc_amount);
3478       v_curr_alloc_amount:= pa_currency.round_currency_amt(v_curr_alloc_amount);
3479   IF P_DEBUG_MODE = 'Y' THEN
3480      pa_debug.write_file('create_target_txns: ' || 'LOG', 'Amounts after rounding' );
3481      pa_debug.write_file('create_target_txns: ' || 'LOG', 'v_tot_alloc_amount:'||to_char(v_tot_alloc_amount) );
3482      pa_debug.write_file('create_target_txns: ' || 'LOG', 'v_prev_alloc_amount:'||to_char(v_prev_alloc_amount) );
3483      pa_debug.write_file('create_target_txns: ' || 'LOG', 'v_curr_alloc_amount:'||to_char(v_curr_alloc_amount) );
3484   END IF;
3485       pa_debug.G_err_stage:= 'INSERTING INTO ALLOC TXN DETAILS';
3486       IF P_DEBUG_MODE = 'Y' THEN
3487          pa_debug.write_file('create_target_txns: ' || 'LOG', pa_debug.G_err_stage);
3488       END IF;
3489       /* insert into alloc_txn_details */
3490 --
3491 --    Bug: 983057  Do not create txn with zero curren alloc amount
3492 --
3493       IF (v_curr_alloc_amount <> 0 ) THEN
3494 	/** modified to call the api by reference **/
3495          ---insert_alloc_txn_details(  p_run_id
3496          ---, p_rule_id
3497          ---, 'T'
3498          ---, p_fiscal_year
3499          --- , p_quarter_num
3500          --- , p_period_num
3501          --- , p_run_period
3502          --- , run_target_rec.line_num
3503          --- , run_target_rec.project_id
3504          --- , run_target_rec.task_id
3505          --- , p_expenditure_type
3506          ---, v_tot_alloc_amount
3507          ---, v_prev_alloc_amount
3508          ---, v_curr_alloc_amount );
3509 	 l_alloc_txn_id := Null;
3510 	 insert_alloc_txn_details( x_alloc_txn_id         => l_alloc_txn_id
3511                                   , p_run_id              => p_run_id
3512                                   , p_rule_id             => p_rule_id
3513                                   , p_transaction_type    => 'T'
3514                                   , p_fiscal_year         => p_fiscal_year
3515                                   , p_quarter_num         => p_quarter_num
3516                                   , p_period_num          => p_period_num
3517                                   , p_run_period          => p_run_period
3518                                   , p_line_num            => run_target_rec.line_num
3519                                   , p_project_id          => run_target_rec.project_id
3520                                   , p_task_id             => run_target_rec.task_id
3521                                   , p_expenditure_type    => p_expenditure_type
3522                                   , p_total_allocation    => v_tot_alloc_amount
3523                                   , p_previous_allocation => v_prev_alloc_amount
3524                                   , p_current_allocation  => v_curr_alloc_amount
3525                                   , p_EXPENDITURE_ID      => NULL
3526                                   , p_EXPENDITURE_ITEM_ID => NULL
3527                                   , p_CINT_SOURCE_TASK_ID => NULL
3528                                   , p_CINT_EXP_ORG_ID     => NULL
3529                                   , p_CINT_RATE_MULTIPLIER => NULL
3530                                   , p_CINT_PRIOR_BASIS_AMT => NULL
3531                                   , p_CINT_CURRENT_BASIS_AMT => NULL
3532                                   , p_REJECTION_CODE      => NULL
3533                                   , p_STATUS_CODE         => NULL
3534                                   , p_ATTRIBUTE_CATEGORY  => NULL
3535                                   , p_ATTRIBUTE1          => NULL
3536                                   , p_ATTRIBUTE2          => NULL
3537                                   , p_ATTRIBUTE3          => NULL
3538                                   , p_ATTRIBUTE4          => NULL
3539                                   , p_ATTRIBUTE5          => NULL
3540                                   , p_ATTRIBUTE6          => NULL
3541                                   , p_ATTRIBUTE7          => NULL
3542                                   , p_ATTRIBUTE8          => NULL
3543                                   , p_ATTRIBUTE9          => NULL
3544                                   , p_ATTRIBUTE10         => NULL
3545 				);
3546 	 /* End of PA.L Capint changes */
3547          G_num_txns := G_num_txns + 1 ;
3548       END IF ;
3549       /* get the total allocated */
3550       v_net_alloc_amount := NVl(v_net_alloc_amount,0) + NVL(v_curr_alloc_amount,0) ;
3551       END LOOP;
3552        IF P_DEBUG_MODE = 'Y' THEN
3553           pa_debug.write_file('create_target_txns: ' || 'LOG', 'v_net_alloc_amount:'||to_char(v_net_alloc_amount) );
3554        END IF;
3555         pa_debug.G_err_stage:= 'ALLOCATING REMNANT';
3556         IF P_DEBUG_MODE = 'Y' THEN
3557            pa_debug.write_file('create_target_txns: ' || 'LOG', pa_debug.G_err_stage);
3558         END IF;
3559         If p_allocation_method = 'I' then
3560            IF( p_amount_type = 'ITD') THEN
3561              v_fiscal_year:= NULL;
3562              v_quarter_num:= NULL;
3563              v_period_num := NULL;
3564            ELSIF (p_amount_type = 'FYTD') THEN
3565              v_fiscal_year:= p_fiscal_year;
3566              v_quarter_num:= NULL;
3567              v_period_num := NULL;
3568            ELSIF (p_amount_type = 'QTD') THEN
3569              v_fiscal_year:= p_fiscal_year;
3570              v_quarter_num:= p_quarter_num;
3571              v_period_num := NULL;
3572            ELSE
3573              v_fiscal_year:= p_fiscal_year;
3574              v_quarter_num:= p_quarter_num;
3575              v_period_num := p_period_num;
3576            END IF;
3577            IF P_DEBUG_MODE = 'Y' THEN
3578               pa_debug.write_file('create_target_txns: ' || 'LOG', 'Fetching sum of allocated amounts until the current run' );
3579            END IF;
3580            Open c_sum_of_allocated_amts ;
3581            Fetch c_sum_of_allocated_amts into v_sum_alloc_amts ;
3582            if c_sum_of_allocated_amts%NOTFOUND then
3583              v_sum_alloc_amts := 0 ;
3584            end if ;
3585            Close c_sum_of_allocated_amts ;
3586         End If ;
3587        IF P_DEBUG_MODE = 'Y' THEN
3588           pa_debug.write_file('create_target_txns: ' || 'LOG', 'v_sum_alloc_amts:'||to_char(v_sum_alloc_amts) );
3589        END IF;
3590 
3591 	   IF nvl(v_net_alloc_amount,0) <> 0 THEN -- added this if condition as part of the bug 13949738
3592        allocate_remnant ( p_run_id, p_pool_amount + v_src_sunk_cost - nvl(v_sum_alloc_amts,0)
3593                             , v_remnant_amount);
3594        END IF;
3595 
3596 		pa_debug.G_err_stage:= 'UPDATING ALLOC RUNS WITH AMOUNTS';
3597         IF P_DEBUG_MODE = 'Y' THEN
3598            pa_debug.write_file('create_target_txns: ' || 'LOG', pa_debug.G_err_stage);
3599           pa_debug.write_file('create_target_txns: ' || 'LOG', 'v_sum_alloc_amts:'||to_char(v_sum_alloc_amts) );
3600           pa_debug.write_file('create_target_txns: ' || 'LOG', 'v_net_alloc_amount:'||to_char(v_net_alloc_amount) );
3601           pa_debug.write_file('create_target_txns: ' || 'LOG', 'v_remnant_amount:'||to_char(v_remnant_amount) );
3602           pa_debug.write_file('create_target_txns: ' || 'LOG', 'v_src_sunk_cost:'||to_char(v_src_sunk_cost) );
3603           pa_debug.write_file('create_target_txns: ' || 'LOG', 'v_tgt_sunk_cost:'||to_char(v_tgt_sunk_cost) );
3604        END IF;
3605         x_curr_alloc_amount :=  nvl(v_net_alloc_amount,0)+nvl(v_remnant_amount,0) ;
3606         UPDATE pa_alloc_runs
3607         SET total_pool_amount = nvl(p_pool_amount,0)
3608           , allocated_amount = nvl(v_net_alloc_amount,0)+nvl(v_remnant_amount,0)
3609 --        , Missing_source_proj_amt = v_src_sunk_cost -- commented as part of the bug 13949738
3610           , Missing_target_proj_amt = v_tgt_sunk_cost
3611           , Total_allocated_amount  = nvl(v_sum_alloc_amts,0) + nvl(v_net_alloc_amount,0)+nvl(v_remnant_amount,0)-nvl(v_tgt_sunk_cost,0)
3612         WHERE run_id = p_run_id;
3613   pa_debug.reset_err_stack;
3614 EXCEPTION
3615   WHEN OTHERS THEN
3616     pa_debug.G_err_code := SQLCODE;
3617     RAISE;
3618 END create_target_txns;
3619 -- ------------------------------------------------------------
3620 -- create_offset_txns
3621 -- ------------------------------------------------------------
3622 PROCEDURE create_offset_txns( p_rule_id           IN NUMBER
3623                             , p_run_id            IN NUMBER
3624                             , p_type              IN VARCHAR2
3625                             , p_fiscal_year       IN NUMBER
3626                             , p_quarter_num       IN NUMBER
3627                             , p_period_num        IN NUMBER
3628                             , p_run_period        IN VARCHAR2
3629                             , p_expenditure_type  IN VARCHAR2
3630                             , p_allocation_method IN VARCHAR2
3631                             , p_offset_method      IN VARCHAR2
3632                             , p_offset_project_id  IN NUMBER
3633                             , p_offset_task_id     IN NUMBER
3634                             , p_amount_type        IN VARCHAR2
3635                             , p_pool_amount        IN NUMBER
3636                             , p_allocated_amount   IN NUMBER ) IS
3637 CURSOR C_offset_det IS
3638   SELECT line_num, project_id, task_id, nvl(sum(nvl(eligible_amount,0)),0) eligible_amount
3639   FROM pa_alloc_run_source_det
3640   WHERE run_id=p_run_id
3641   GROUP BY line_num, project_id, task_id;
3642 CURSOR c_offset_proj_sum IS
3643   SELECT project_id, nvl(sum(nvl(eligible_amount,0)),0) eligible_amount
3644   FROM pa_alloc_run_source_det
3645   WHERE run_id = p_run_id
3646   GROUP BY project_id ;
3647 CURSOR c_pool_amount IS
3648   SELECT nvl(sum(nvl(eligible_amount,0)),0) pool_amount
3649   FROM pa_alloc_run_source_det
3650   WHERE run_id = p_run_id;
3651   v_project_id   NUMBER ;
3652   v_task_id      NUMBER ;
3653   v_proj_prev_offset_amt NUMBER ;
3654  -- This cursor will get the previously allocated offset amounts for a given source project
3655  -- on the tasks other than current task provided by client extension.
3656 CURSOR C_prev_offset_task_CE (v_project_id IN NUMBER
3657                             , v_task_id    IN NUMBER) is
3658    SELECT nvl(sum(nvl(current_allocation,0)),0)
3659      from pa_alloc_txn_details pat
3660          ,pa_alloc_runs par
3661     where pat.run_id = par.run_id
3662       and par.fiscal_year = nvl(p_fiscal_year, par.fiscal_year)
3663       and par.quarter     = nvl(p_quarter_num,par.quarter)
3664       and par.period_num  = nvl(p_period_num , par.period_num)
3665       and par.run_id < p_run_id
3666       and par.rule_id = p_rule_id
3667       and par.run_status = 'RS'
3668       and pat.transaction_type = 'O'
3669       and pat.project_id = v_project_id
3670       and pat.task_id   <> v_task_id ;
3671 CURSOR C_off_sunk_cost( p_fscl_year IN NUMBER
3672         , p_qrtr_num  IN NUMBER
3673         , p_prd_num   IN NUMBER ) IS
3674   SELECT a.run_id  run_id
3675   , a.project_id  project_id
3676   , a.task_id     task_id
3677   , nvl(a.Total_allocation,0) Total_allocation
3678   FROM pa_alloc_txn_details a,
3679        pa_alloc_runs ar
3680   WHERE a.run_id = ar.run_id
3681   AND  a.rule_id = p_rule_id
3682   AND  ar.fiscal_year = NVL(p_fscl_year, ar.fiscal_year)
3683   AND  ar.quarter = NVL(p_qrtr_num, ar.quarter)
3684   AND ar.period_num = NVL(p_prd_num, ar.period_num )
3685   AND  a.run_id < p_run_id
3686   AND  a.transaction_type  = 'O'
3687   AND NOT EXISTS ( SELECT 1
3688                   FROM pa_alloc_txn_details b
3689                   WHERE b.project_id = a.project_id
3690                     AND b.task_id = a.task_id
3691                     AND b.transaction_type    = 'O'
3692                     AND b.run_id = p_run_id )
3693   AND a.run_id =( SELECT max(c.run_id)
3694                   FROM pa_alloc_txn_details c,
3695                        pa_alloc_runs c_ar   -- added this table to exclude reversed runs.
3696                   WHERE c.project_id = a.project_id
3697                   AND c.task_id = a.task_id
3698                   AND c.transaction_type    = 'O'
3699                   AND c.run_id = c_ar.run_id
3700                   AND c_ar.rule_id = p_rule_id
3701                   AND c_ar.run_status = 'RS'
3702                   AND c.run_id < p_run_id );
3703 v_tot_pool_amount NUMBER := 0;
3704 v_curr_offset_amount NUMBER := 0;
3705 v_prev_offset_amount NUMBER := 0;
3706 v_tot_offset_amount NUMBER := 0;
3707 v_src_sunk_cost NUMBER :=0;
3708 v_tgt_sunk_cost NUMBER :=0;
3709 v_off_sunk_cost NUMBER := 0 ;
3710 v_sum_tot_offsets NUMBER := 0 ;
3711 v_cx_task_id NUMBER ;
3712 v_cx_project_id NUMBER ;
3713 l_alloc_txn_id      NUMBER := NULL; /** added for capint changes */
3714 v_quarter_num NUMBER;
3715 v_fiscal_year NUMBER;
3716 v_period_num NUMBER;
3717 v_status NUMBER :=NULL;
3718 v_err_message VARCHAR2(250);
3719 v_returned_amount  NUMBER := 0;
3720 v_offset_extn_tabtype PA_CLIENT_EXTN_ALLOC.ALLOC_OFFSET_TABTYPE;
3721 BEGIN
3722   pa_debug.set_err_stack('Create_Offset_txns');
3723   pa_debug.G_err_stage:= 'CREATING OFFSET TRANSACTIONS';
3724   IF P_DEBUG_MODE = 'Y' THEN
3725      pa_debug.write_file('create_offset_txns: ' || 'LOG', pa_debug.G_err_stage);
3726   END IF;
3727   IF (p_offset_method = 'A') THEN   -- Offset Method: Same as source
3728     FOR offset_det_rec IN c_offset_det LOOP
3729   v_tot_offset_amount := offset_det_rec.eligible_amount * (-1);
3730       IF p_allocation_method = 'I' THEN
3731         v_prev_offset_amount := get_previous_alloc_amnt( p_rule_id
3732                                                        , p_run_id
3733                                                        , offset_det_rec.project_id
3734                                                        , offset_det_rec.task_id
3735                                                        , p_quarter_num
3736                                                        , p_fiscal_year
3737                                                        , p_period_num
3738                                                        , 'O'
3739                                                        , p_amount_type );
3740       ELSE   /* if full allocation */
3741         v_prev_offset_amount:= 0;
3742       END IF;
3743       v_curr_offset_amount := NVl(v_tot_offset_amount,0) - NVL(v_prev_offset_amount,0);
3744       v_tot_offset_amount:= pa_currency.round_currency_amt(v_tot_offset_amount);
3745       v_prev_offset_amount:= pa_currency.round_currency_amt(v_prev_offset_amount);
3746       v_curr_offset_amount:= pa_currency.round_currency_amt(v_curr_offset_amount);
3747       v_sum_tot_offsets := v_sum_tot_offsets + v_tot_offset_amount ;
3748       /* insert into alloc_txn_details */
3749 --
3750 --    Bug: 983057  Do not create txn with zero curren alloc amount
3751 --
3752       IF (v_curr_offset_amount <> 0 ) THEN
3753 	/** Calling the api by reference changed for capint */
3754       	--insert_alloc_txn_details( p_run_id
3755         --                      , p_rule_id
3756         --                      , 'O'
3757         --                      , p_fiscal_year
3758         --                      , p_quarter_num
3759         --                      , p_period_num
3760         --                      , p_run_period
3761         --                      , offset_det_rec.line_num
3762         --                      , offset_det_rec.project_id
3763         --                      , offset_det_rec.task_id
3764         --                      , p_expenditure_type
3765         --                      , v_tot_offset_amount
3766         --                      , v_prev_offset_amount
3767         --                      , v_curr_offset_amount );
3768 	 l_alloc_txn_id :=  Null;
3769          insert_alloc_txn_details( x_alloc_txn_id         => l_alloc_txn_id
3770                                   , p_run_id              => p_run_id
3771                                   , p_rule_id             => p_rule_id
3772                                   , p_transaction_type    => 'O'
3773                                   , p_fiscal_year         => p_fiscal_year
3774                                   , p_quarter_num         => p_quarter_num
3775                                   , p_period_num          => p_period_num
3776                                   , p_run_period          => p_run_period
3777                                   , p_line_num            => offset_det_rec.line_num
3778                                   , p_project_id          => offset_det_rec.project_id
3779                                   , p_task_id             => offset_det_rec.task_id
3780                                   , p_expenditure_type    => p_expenditure_type
3781                                   , p_total_allocation    => v_tot_offset_amount
3782                                   , p_previous_allocation => v_prev_offset_amount
3783                                   , p_current_allocation  => v_curr_offset_amount
3784                                   , p_EXPENDITURE_ID      => NULL
3785                                   , p_EXPENDITURE_ITEM_ID => NULL
3786                                   , p_CINT_SOURCE_TASK_ID => NULL
3787                                   , p_CINT_EXP_ORG_ID     => NULL
3788                                   , p_CINT_RATE_MULTIPLIER => NULL
3789                                   , p_CINT_PRIOR_BASIS_AMT => NULL
3790                                   , p_CINT_CURRENT_BASIS_AMT => NULL
3791                                   , p_REJECTION_CODE      => NULL
3792                                   , p_STATUS_CODE         => NULL
3793                                   , p_ATTRIBUTE_CATEGORY  => NULL
3794                                   , p_ATTRIBUTE1          => NULL
3795                                   , p_ATTRIBUTE2          => NULL
3796                                   , p_ATTRIBUTE3          => NULL
3797                                   , p_ATTRIBUTE4          => NULL
3798                                   , p_ATTRIBUTE5          => NULL
3799                                   , p_ATTRIBUTE6          => NULL
3800                                   , p_ATTRIBUTE7          => NULL
3801                                   , p_ATTRIBUTE8          => NULL
3802                                   , p_ATTRIBUTE9          => NULL
3803                                   , p_ATTRIBUTE10         => NULL
3804                                 );
3805 	/* End of capint changes */
3806          G_num_txns := G_num_txns + 1 ;
3807       END IF ;
3808       END LOOP;
3809   ELSIF (p_offset_method = 'B') THEN -- Offset Method: same as source project, CE tasks
3810     IF P_DEBUG_MODE = 'Y' THEN
3811        pa_debug.write_file('create_offset_txns: ' || 'LOG', 'Test offset method B ');
3812     END IF;
3813     FOR offset_proj_sum_rec IN c_offset_proj_sum LOOP
3814       pa_client_extn_alloc.offset_task_extn(p_rule_id, offset_proj_sum_rec.project_id, v_task_id,v_status,v_err_message);
3815       IF nvl(v_status,0) <>0 THEN
3816          v_err_message:=nvl(v_err_message,'PA_AL_CE_FAILED');
3817          alloc_errors(p_rule_id, p_run_id,'O','E',v_err_message,TRUE);
3818       END IF;
3819       v_cx_task_id:=NVL(v_task_id,0);
3820       v_cx_project_id:=NVL(offset_proj_sum_rec.project_id,0);
3821       IF  is_offset_task_valid(v_cx_project_id, v_cx_task_id) = 'N' THEN
3822          G_fatal_err_found:= TRUE;
3823          alloc_errors( p_rule_id, p_run_id, 'O', 'E',
3824                         'PA_AL_INV_PROJECT_TASK_IN_CE',FALSE,'Y',NULL, v_task_id);
3825          IF P_DEBUG_MODE = 'Y' THEN
3826             pa_debug.write_file('create_offset_txns: ' || 'LOG','Client Extension returned an invalid offset task:  '
3827                           || to_char(v_cx_task_id));
3828          END IF;
3829        END IF;
3830       IF P_DEBUG_MODE = 'Y' THEN
3831          pa_debug.write_file('create_offset_txns: ' || 'LOG', 'v_task_id: '||to_char(v_task_id) );
3832       END IF;
3833       v_tot_offset_amount:= offset_proj_sum_rec.eligible_amount * (-1);
3834       IF (p_allocation_method = 'I') THEN  /* incremental */
3835         v_prev_offset_amount := get_previous_alloc_amnt( p_rule_id
3836                                                        , p_run_id
3837                                                        , offset_proj_sum_rec.project_id
3838                                                        , v_task_id
3839                                                        , p_quarter_num
3840                                                        , p_fiscal_year
3841                                                        , p_period_num
3842                                                        , 'O'
3843                                                        , p_amount_type );
3844          get_sunk_cost(p_rule_id
3845                   , p_run_id
3846                   , p_fiscal_year
3847                   , p_quarter_num
3848                   , p_period_num
3849                   , p_amount_type
3850                   , v_src_sunk_cost
3851                   , v_tgt_sunk_cost
3852                   , offset_proj_sum_rec.project_id);
3853            open C_prev_offset_task_CE( offset_proj_sum_rec.project_id,v_task_id ) ;
3854            Fetch C_prev_offset_task_CE into v_proj_prev_offset_amt;
3855            Close C_prev_offset_task_CE ;
3856            IF P_DEBUG_MODE = 'Y' THEN
3857               pa_debug.write_file('create_offset_txns: ' || 'LOG', 'offset project:Task:total_amount:src_sunk_cost:prev_offset '
3858                                 ||to_char(offset_proj_sum_rec.project_id)||':'
3859                                 ||to_char(v_task_id)|| ':'
3860                                 ||to_char(v_tot_offset_amount)|| ':'
3861                                 || to_char(v_src_sunk_cost)||':'
3862                                 || to_char(v_proj_prev_offset_amt) );
3863            END IF;
3864           v_tot_offset_amount:= v_tot_offset_amount +  NVL(v_src_sunk_cost,0) *(-1) - v_proj_prev_offset_amt ;
3865       ELSE   /* if full allocation */
3866         v_prev_offset_amount := 0;
3867       END IF;
3868       v_curr_offset_amount:= NVL(v_tot_offset_amount,0) - NVL(v_prev_offset_amount,0);
3869       v_tot_offset_amount:= pa_currency.round_currency_amt(v_tot_offset_amount);
3870       v_prev_offset_amount:= pa_currency.round_currency_amt(v_prev_offset_amount);
3871       v_curr_offset_amount:= pa_currency.round_currency_amt(v_curr_offset_amount);
3872       v_sum_tot_offsets := v_sum_tot_offsets + v_tot_offset_amount ;
3873       /* insert into alloc_txn_details */
3874 --
3875 --    Bug: 983057  Do not create txn with zero curren alloc amount
3876 --
3877       IF (v_curr_offset_amount <> 0 ) THEN
3878 		/* Start of capint changes Call by reference */
3879       		--insert_alloc_txn_details( p_run_id
3880                 --              , p_rule_id
3881                 --              , 'O'
3882                 --              , p_fiscal_year
3883                 --              , p_quarter_num
3884                 --              , p_period_num
3885                 --              , p_run_period
3886                 --              , 0 /* offset_proj_sum_rec.line_num */
3887                 --              , offset_proj_sum_rec.project_id
3888                 --              , v_task_id
3889                 --              , p_expenditure_type
3890                 --              , v_tot_offset_amount
3891                 --              , v_prev_offset_amount
3892                 --              , v_curr_offset_amount );
3893          	l_alloc_txn_id :=  Null;
3894          	insert_alloc_txn_details( x_alloc_txn_id  => l_alloc_txn_id
3895                                   , p_run_id              => p_run_id
3896                                   , p_rule_id             => p_rule_id
3897                                   , p_transaction_type    => 'O'
3898                                   , p_fiscal_year         => p_fiscal_year
3899                                   , p_quarter_num         => p_quarter_num
3900                                   , p_period_num          => p_period_num
3901                                   , p_run_period          => p_run_period
3902                                   , p_line_num            => 0 /* offset_proj_sum_rec.line_num */
3903                                   , p_project_id          => offset_proj_sum_rec.project_id
3904                                   , p_task_id             => v_task_id
3905                                   , p_expenditure_type    => p_expenditure_type
3906                                   , p_total_allocation    => v_tot_offset_amount
3907                                   , p_previous_allocation => v_prev_offset_amount
3908                                   , p_current_allocation  => v_curr_offset_amount
3909                                   , p_EXPENDITURE_ID      => NULL
3910                                   , p_EXPENDITURE_ITEM_ID => NULL
3911                                   , p_CINT_SOURCE_TASK_ID => NULL
3912                                   , p_CINT_EXP_ORG_ID     => NULL
3913                                   , p_CINT_RATE_MULTIPLIER => NULL
3914                                   , p_CINT_PRIOR_BASIS_AMT => NULL
3915                                   , p_CINT_CURRENT_BASIS_AMT => NULL
3916                                   , p_REJECTION_CODE      => NULL
3917                                   , p_STATUS_CODE         => NULL
3918                                   , p_ATTRIBUTE_CATEGORY  => NULL
3919                                   , p_ATTRIBUTE1          => NULL
3920                                   , p_ATTRIBUTE2          => NULL
3921                                   , p_ATTRIBUTE3          => NULL
3922                                   , p_ATTRIBUTE4          => NULL
3923                                   , p_ATTRIBUTE5          => NULL
3924                                   , p_ATTRIBUTE6          => NULL
3925                                   , p_ATTRIBUTE7          => NULL
3926                                   , p_ATTRIBUTE8          => NULL
3927                                   , p_ATTRIBUTE9          => NULL
3928                                   , p_ATTRIBUTE10         => NULL
3929                                 );
3930 		/* End of capint changes */
3931          G_num_txns := G_num_txns + 1 ;
3932        END IF ;
3933     END LOOP;
3934   ELSIF( p_offset_method= 'C' ) THEN  --Offset Method:  Specific project and Task
3935     IF (p_allocation_method = 'I') THEN
3936         v_prev_offset_amount := Get_previous_alloc_amnt( p_rule_id
3937                                                      , p_run_id
3938                                                      , p_offset_project_id
3939                                                      , p_offset_task_id
3940                                                      , p_quarter_num
3941                                                      , p_fiscal_year
3942                                                      , p_period_num
3943                                                      , 'O'
3944                                                      , p_amount_type );
3945        v_tot_offset_amount:= v_tot_offset_amount + v_src_sunk_cost * (-1);
3946     ELSE  /* full allocation */
3947       v_prev_offset_amount:= 0;
3948     END IF;
3949     v_tot_offset_amount := (p_allocated_amount*-1) + v_prev_offset_amount ;
3950     v_prev_offset_amount:= pa_currency.round_currency_amt(v_prev_offset_amount);
3951     v_curr_offset_amount:= pa_currency.round_currency_amt(p_allocated_amount*-1);
3952     v_tot_offset_amount:= pa_currency.round_currency_amt(v_tot_offset_amount);
3953       v_sum_tot_offsets := v_sum_tot_offsets + v_tot_offset_amount ;
3954     /* insert into alloc_txn_details */
3955 --
3956 --    Bug: 983057  Do not create txn with zero curren alloc amount
3957 --
3958       IF (v_curr_offset_amount <> 0 ) THEN
3959 		/** Start Capint changes  call by reference */
3960     		--insert_alloc_txn_details( p_run_id
3961                 --            , p_rule_id
3962                 --            , 'O'
3963                 --            , p_fiscal_year
3964                 --            , p_quarter_num
3965                 --            , p_period_num
3966                 --            , p_run_period
3967                 --            , 0
3968                 --            , p_offset_project_id
3969                 --            , p_offset_task_id
3970                 --            , p_expenditure_type
3971                 --            , v_tot_offset_amount
3972                 --            , v_prev_offset_amount
3973                 --            , v_curr_offset_amount );
3974                 l_alloc_txn_id :=  Null;
3975                 insert_alloc_txn_details( x_alloc_txn_id  => l_alloc_txn_id
3976                                   , p_run_id              => p_run_id
3977                                   , p_rule_id             => p_rule_id
3978                                   , p_transaction_type    => 'O'
3979                                   , p_fiscal_year         => p_fiscal_year
3980                                   , p_quarter_num         => p_quarter_num
3981                                   , p_period_num          => p_period_num
3982                                   , p_run_period          => p_run_period
3983                                   , p_line_num            => 0
3984                                   , p_project_id          => p_offset_project_id
3985                                   , p_task_id             => p_offset_task_id
3986                                   , p_expenditure_type    => p_expenditure_type
3987                                   , p_total_allocation    => v_tot_offset_amount
3988                                   , p_previous_allocation => v_prev_offset_amount
3989                                   , p_current_allocation  => v_curr_offset_amount
3990                                   , p_EXPENDITURE_ID      => NULL
3991                                   , p_EXPENDITURE_ITEM_ID => NULL
3992                                   , p_CINT_SOURCE_TASK_ID => NULL
3993                                   , p_CINT_EXP_ORG_ID     => NULL
3994                                   , p_CINT_RATE_MULTIPLIER => NULL
3995                                   , p_CINT_PRIOR_BASIS_AMT => NULL
3996                                   , p_CINT_CURRENT_BASIS_AMT => NULL
3997                                   , p_REJECTION_CODE      => NULL
3998                                   , p_STATUS_CODE         => NULL
3999                                   , p_ATTRIBUTE_CATEGORY  => NULL
4000                                   , p_ATTRIBUTE1          => NULL
4001                                   , p_ATTRIBUTE2          => NULL
4002                                   , p_ATTRIBUTE3          => NULL
4003                                   , p_ATTRIBUTE4          => NULL
4004                                   , p_ATTRIBUTE5          => NULL
4005                                   , p_ATTRIBUTE6          => NULL
4006                                   , p_ATTRIBUTE7          => NULL
4007                                   , p_ATTRIBUTE8          => NULL
4008                                   , p_ATTRIBUTE9          => NULL
4009                                   , p_ATTRIBUTE10         => NULL
4010 				);
4011 		/* End capint chagnes */
4012          G_num_txns := G_num_txns + 1 ;
4013       END IF ;
4014   ELSIF (p_offset_method = 'D') THEN  -- Offset Method: CLient Extension
4015     v_tot_offset_amount:= p_allocated_amount * (-1);
4016     pa_client_extn_alloc.offset_extn( p_rule_id, v_tot_offset_amount
4017                               , v_offset_extn_tabtype,v_status,v_err_message);
4018     IF nvl(v_status,0) <> 0 THEN
4019        v_err_message:=nvl(v_err_message,'PA_AL_CE_FAILED');
4020        alloc_errors(p_rule_id,p_run_id,'O','E',v_err_message,TRUE);
4021     END IF;
4022     /* check whether tot_offset_amount retuned is same as what was passed */
4023     FOR I in 1..v_offset_extn_tabtype.count LOOP
4024              IF P_DEBUG_MODE = 'Y' THEN
4025                 pa_debug.write_file('create_offset_txns: ' || 'LOG', 'offset project-Task: '||
4026               to_char(v_offset_extn_tabtype(I).project_id)||'-'||
4027               to_char(v_offset_extn_tabtype(I).task_id) || 'Index :'||to_char(I) );
4028              END IF;
4029               v_cx_project_id:=v_offset_extn_tabtype(I).project_id;
4030               v_cx_task_id:=v_offset_extn_tabtype(I).task_id;
4031              v_returned_amount := v_returned_amount + NVL(v_offset_extn_tabtype(I).offset_amount, 0);
4032         IF  is_offset_project_valid( v_cx_project_id) = 'N' THEN
4033               G_fatal_err_found:= TRUE;
4034               alloc_errors( p_rule_id, p_run_id, 'O', 'E',
4035                         'PA_AL_INV_PROJECT_TASK_IN_CE',FALSE,'Y',v_cx_project_id);
4036               IF P_DEBUG_MODE = 'Y' THEN
4037                  pa_debug.write_file('create_offset_txns: ' || 'LOG','Client Extension returned an invalid offset project:  '
4038                                         ||to_char(v_cx_project_id) );
4039               END IF;
4040         END IF;
4041         IF is_offset_task_valid(v_cx_project_id,v_cx_task_id ) = 'N' THEN
4042             G_fatal_err_found:= TRUE;
4043             alloc_errors( p_rule_id, p_run_id, 'O', 'E',
4044                         'PA_AL_INV_PROJECT_TASK_IN_CE',FALSE,'Y',v_cx_project_id,v_cx_task_id);
4045             IF P_DEBUG_MODE = 'Y' THEN
4046                pa_debug.write_file('create_offset_txns: ' || 'LOG','Client Extension returned an invalid offset task:  '
4047                                         || to_char(v_cx_task_id ));
4048             END IF;
4049         END IF;
4050     END LOOP;
4051     IF ( v_tot_offset_amount <> v_returned_amount ) THEN
4052             alloc_errors( p_rule_id, p_run_id, 'O', 'E',
4053                      'PA_AL_CLNT_INCORECT_OFFST_AMNT', TRUE );
4054     END IF;
4055   FOR I in 1..v_offset_extn_tabtype.count LOOP
4056       IF p_allocation_method = 'I' THEN
4057         v_prev_offset_amount := get_previous_alloc_amnt( p_rule_id
4058                                                        , p_run_id
4059                                                        , v_offset_extn_tabtype(I).project_id
4060                                                        , v_offset_extn_tabtype(I).task_id
4061                                                        , p_quarter_num
4062                                                        , p_fiscal_year
4063                                                        , p_period_num
4064                                                        , 'O'
4065                                                        , p_amount_type );
4066       ELSE   /* if full allocation */
4067         v_prev_offset_amount:= 0;
4068       END IF;
4069                     v_curr_offset_amount:=  v_offset_extn_tabtype(I).offset_amount;
4070                     v_tot_offset_amount:= NVl(v_curr_offset_amount,0) + NVl(v_prev_offset_amount,0);
4071       v_tot_offset_amount:= pa_currency.round_currency_amt(v_tot_offset_amount);
4072       v_prev_offset_amount:= pa_currency.round_currency_amt(v_prev_offset_amount);
4073       v_curr_offset_amount:= pa_currency.round_currency_amt(v_curr_offset_amount);
4074       v_sum_tot_offsets := v_sum_tot_offsets + v_tot_offset_amount ;
4075 --
4076 --    Bug: 983057  Do not create txn with zero curren alloc amount
4077 --
4078       IF (v_curr_offset_amount <> 0 ) THEN
4079 		/* Start Capint changes */
4080       		--insert_alloc_txn_details( p_run_id
4081                 --              , p_rule_id
4082                 --              , 'O'
4083                 --              , p_fiscal_year
4084                 --              , p_quarter_num
4085                 --              , p_period_num
4086                 --              , p_run_period
4087                 --              , 0
4088                 --              , v_offset_extn_tabtype(I).project_id
4089                 --              , v_offset_extn_tabtype(I).task_id
4090                 --              , p_expenditure_type
4091                 --              , v_tot_offset_amount
4092                 --              , v_prev_offset_amount
4093                 --              , v_curr_offset_amount );
4094                 l_alloc_txn_id :=  Null;
4095                 insert_alloc_txn_details( x_alloc_txn_id  => l_alloc_txn_id
4096                                   , p_run_id              => p_run_id
4097                                   , p_rule_id             => p_rule_id
4098                                   , p_transaction_type    => 'O'
4099                                   , p_fiscal_year         => p_fiscal_year
4100                                   , p_quarter_num         => p_quarter_num
4101                                   , p_period_num          => p_period_num
4102                                   , p_run_period          => p_run_period
4103                                   , p_line_num            => 0
4104                                   , p_project_id          => v_offset_extn_tabtype(I).project_id
4105                                   , p_task_id             => v_offset_extn_tabtype(I).task_id
4106                                   , p_expenditure_type    => p_expenditure_type
4107                                   , p_total_allocation    => v_tot_offset_amount
4108                                   , p_previous_allocation => v_prev_offset_amount
4109                                   , p_current_allocation  => v_curr_offset_amount
4110                                   , p_EXPENDITURE_ID      => NULL
4111                                   , p_EXPENDITURE_ITEM_ID => NULL
4112                                   , p_CINT_SOURCE_TASK_ID => NULL
4113                                   , p_CINT_EXP_ORG_ID     => NULL
4114                                   , p_CINT_RATE_MULTIPLIER => NULL
4115                                   , p_CINT_PRIOR_BASIS_AMT => NULL
4116                                   , p_CINT_CURRENT_BASIS_AMT => NULL
4117                                   , p_REJECTION_CODE      => NULL
4118                                   , p_STATUS_CODE         => NULL
4119                                   , p_ATTRIBUTE_CATEGORY  => NULL
4120                                   , p_ATTRIBUTE1          => NULL
4121                                   , p_ATTRIBUTE2          => NULL
4122                                   , p_ATTRIBUTE3          => NULL
4123                                   , p_ATTRIBUTE4          => NULL
4124                                   , p_ATTRIBUTE5          => NULL
4125                                   , p_ATTRIBUTE6          => NULL
4126                                   , p_ATTRIBUTE7          => NULL
4127                                   , p_ATTRIBUTE8          => NULL
4128                                   , p_ATTRIBUTE9          => NULL
4129                                   , p_ATTRIBUTE10         => NULL
4130 				);
4131 		/* End capint changes */
4132          G_num_txns := G_num_txns + 1 ;
4133        END IF ;
4134     END LOOP;
4135   END IF; /* end of p_allocation_method */
4136   IF p_allocation_method = 'I' THEN
4137      IF( p_amount_type = 'ITD') THEN
4138        v_fiscal_year:= NULL;
4139        v_quarter_num:= NULL;
4140        v_period_num := NULL;
4141      ELSIF (p_amount_type = 'FYTD') THEN
4142        v_fiscal_year:= p_fiscal_year;
4143        v_quarter_num:= NULL;
4144        v_period_num := NULL;
4145      ELSIF (p_amount_type = 'QTD') THEN
4146        v_fiscal_year:= p_fiscal_year;
4147        v_quarter_num:= p_quarter_num;
4148        v_period_num := NULL;
4149      ELSE
4150        v_fiscal_year:= p_fiscal_year;
4151        v_quarter_num:= p_quarter_num;
4152        v_period_num := p_period_num;
4153      END IF;
4154      IF P_DEBUG_MODE = 'Y' THEN
4155         pa_debug.write_file('create_offset_txns: ' || 'LOG',' calculate total offset missing cost'
4156                 ||to_char(p_fiscal_year) || ' ' || to_char(v_quarter_num)
4157                 || ' ' || to_char(v_period_num) );
4158      END IF;
4159      FOR off_sunk_cost_rec IN C_off_sunk_cost( v_fiscal_year
4160                                              , v_quarter_num
4161                                              , v_period_num ) LOOP
4162      IF P_DEBUG_MODE = 'Y' THEN
4163         pa_debug.write_file('create_offset_txns: ' || 'LOG','off_sunk_cost = '|| to_char(off_sunk_cost_rec.Total_allocation) );
4164      END IF;
4165         insert_missing_costs( p_run_id
4166                              ,'O'
4167                              ,off_sunk_cost_rec.project_id
4168                              ,off_sunk_cost_rec.task_id
4169                              ,off_sunk_cost_rec.Total_allocation);
4170        v_off_sunk_cost := v_off_sunk_cost + nvl(off_sunk_cost_rec.Total_allocation,0) ;
4171      END LOOP;
4172      IF P_DEBUG_MODE = 'Y' THEN
4173         pa_debug.write_file('create_offset_txns: ' || 'LOG','v_off_sunk_cost =' ||to_char(v_off_sunk_cost) );
4174      END IF;
4175   END IF ;
4176        update pa_alloc_runs
4177           set  Missing_offset_proj_amt = nvl(v_off_sunk_cost,0) ,
4178                TOTAL_OFFSETTED_AMOUNT  = v_sum_tot_offsets
4179         where run_id = p_run_id ;
4180   pa_debug.reset_err_stack;
4181 EXCEPTION
4182   WHEN OTHERS THEN
4183     pa_debug.G_err_code := SQLCODE;
4184     RAISE;
4185 END create_offset_txns;
4186 -- ------------------------------------------------------------
4187 -- allocate_remnant
4188 -- ------------------------------------------------------------
4189 PROCEDURE allocate_remnant( p_run_id IN NUMBER
4190                           , p_act_alloc_amount IN NUMBER
4191                           , x_remnant_amount OUT NOCOPY NUMBER ) IS
4192 CURSOR c_amnt_before_remnant IS
4193   SELECT nvl(SUM(current_allocation),0) sum_curr_alloc
4194        , MAX( ABS(current_allocation) ) max_curr_alloc
4195   FROM pa_alloc_txn_details
4196   WHERE run_id = p_run_id
4197   AND transaction_type = 'T';
4198 CURSOR c_remnant_proj_task( p_max_alloc IN NUMBER ) IS
4199   SELECT project_id, task_id
4200   FROM pa_alloc_txn_details
4201   WHERE run_id = p_run_id
4202   AND transaction_type = 'T'
4203   AND ABS(current_allocation) = p_max_alloc;
4204 v_tot_pool_amount NUMBER;
4205 v_sum_curr_alloc NUMBER;
4206 v_max_curr_alloc NUMBER;
4207 v_project_id NUMBER;
4208 v_task_id NUMBER;
4209 v_amount NUMBER;
4210 v_remnant NUMBER;
4211 BEGIN
4212   pa_debug.set_err_stack ('allocate_remnant');
4213   OPEN c_amnt_before_remnant;
4214   FETCH c_amnt_before_remnant INTO v_sum_curr_alloc, v_max_curr_alloc ;
4215   CLOSE c_amnt_before_remnant;
4216  IF P_DEBUG_MODE = 'Y' THEN
4217     pa_debug.write_file('allocate_remnant: ' || 'LOG', 'Actual allocated amount is:'|| to_char(p_act_alloc_amount));
4218  END IF;
4219   v_remnant:= p_act_alloc_amount - v_sum_curr_alloc;
4220   IF (v_remnant <>0 ) THEN
4221   OPEN c_remnant_proj_task( v_max_curr_alloc );
4222   FETCH c_remnant_proj_task INTO v_project_id, v_task_id;
4223   CLOSE c_remnant_proj_task;
4224   x_remnant_amount:= NVL(v_remnant,0);
4225   UPDATE pa_alloc_txn_details
4226   SET current_allocation = NVL(current_allocation,0) + NVL(v_remnant,0)
4227    ,  total_allocation = NVL(total_allocation, 0) + NVl(v_remnant, 0)
4228   WHERE run_id = p_run_id
4229   AND transaction_type='T'
4230   AND project_id = v_project_id
4231   AND task_id = v_task_id;
4232   END IF;
4233   pa_debug.reset_err_stack ;
4234 EXCEPTION
4235   WHEN OTHERS THEN
4236     pa_debug.G_err_code := SQLCODE;
4237     RAISE;
4238 END allocate_remnant;
4239 -- ------------------------------------------------------------
4240 -- insert_alloc_runs
4241 -- ------------------------------------------------------------
4242 PROCEDURE insert_alloc_runs( x_run_id                  IN OUT NOCOPY NUMBER /* modified as IN OUT for capint */
4243                            , p_rule_id                 IN NUMBER
4244                            , p_run_period              IN VARCHAR2
4245                            , p_expnd_item_date         IN DATE
4246                            , p_creation_date           IN DATE
4247                            , p_created_by              IN NUMBER
4248                            , p_last_update_date        IN DATE
4249                            , p_last_updated_by         IN NUMBER
4250                            , p_last_update_login       IN NUMBER
4251                            , p_pool_percent            IN NUMBER
4252                            , p_period_type             IN VARCHAR2
4253                            , p_source_amount_type      IN VARCHAR2
4254                            , p_source_balance_category IN VARCHAR2
4255                            , p_source_balance_type     IN VARCHAR2
4256                            , p_alloc_resource_list_id  IN NUMBER
4257                            , p_auto_release_flag       IN VARCHAR2
4258                            , p_allocation_method       IN VARCHAR2
4259                            , p_imp_with_exception      IN VARCHAR2
4260                            , p_dup_targets_flag        IN VARCHAR2
4261                            , p_target_exp_type_class   IN VARCHAR2
4262                            , p_target_exp_org_id       IN NUMBER
4263                            , p_target_exp_type         IN VARCHAR2
4264                            , p_target_cost_type        IN VARCHAR2
4265                            , p_offset_exp_type_class   IN VARCHAR2
4266                            , p_offset_exp_org_id       IN NUMBER
4267                            , p_offset_exp_type         IN VARCHAR2
4268                            , p_offset_cost_type        IN VARCHAR2
4269                            , p_offset_method           IN VARCHAR2
4270                            , p_offset_project_id       IN NUMBER
4271                            , p_offset_task_id          IN NUMBER
4272                            , p_run_status              IN VARCHAR2
4273                            , p_basis_method            IN VARCHAR2
4274                            , p_basis_relative_period   IN NUMBER
4275                            , p_basis_amount_type       IN VARCHAR2
4276                            , p_basis_balance_category  IN VARCHAR2
4277                            , p_basis_budget_type_code  IN VARCHAR2
4278                            , p_basis_balance_type      IN VARCHAR2
4279                            , p_basis_resource_list_id  IN NUMBER
4280                            , p_fiscal_year             IN NUMBER
4281                            , p_quarter                 IN NUMBER
4282                            , p_period_num              IN VARCHAR2
4283                            , p_target_exp_group        IN VARCHAR2
4284                            , p_offset_exp_group        IN VARCHAR2
4285                            , p_total_pool_amount       IN NUMBER
4286                            , p_allocated_amount        IN NUMBER
4287                            , p_reversal_date           IN DATE
4288                            , p_draft_request_id        IN NUMBER
4289                            , p_draft_request_date      IN DATE
4290                            , p_release_request_id      IN NUMBER
4291                            , p_release_request_date    IN DATE
4292                            , p_denom_currency_code     IN VARCHAR2
4293                            , p_fixed_amount            IN NUMBER
4294                            , p_rev_target_exp_group    IN VARCHAR2
4295                            , p_rev_offset_exp_group    IN VARCHAR2
4296                            , p_org_id                  IN NUMBER
4297                            , p_limit_target_projects_code IN VARCHAR2
4298 						   , p_CINT_RATE_NAME            IN VARCHAR2 default NULL
4299 						   /* FP.M : Allocation Impact : bug # 3512552 */
4300 						   , p_ALLOC_RESOURCE_STRUCT_TYPE In Varchar2 default NULL
4301 						   , p_BASIS_RESOURCE_STRUCT_TYPE In Varchar2 default NULL
4302 						   , p_ALLOC_RBS_VERSION          In Number default NULL
4303 						   , p_BASIS_RBS_VERSION          In Number default NULL
4304 						   ) IS
4305 BEGIN
4306   pa_debug.set_err_stack('insert_alloc_runs');
4307   If x_run_id is NULL then
4308 	Select pa_alloc_runs_s.nextval
4309 	Into x_run_id
4310 	From dual;
4311   End If;
4312   INSERT INTO pa_alloc_runs_all (
4313     RUN_ID
4314   , RULE_ID
4315   , RUN_PERIOD
4316   , EXPND_ITEM_DATE
4317   , CREATION_DATE
4318   , CREATED_BY
4319   , LAST_UPDATE_DATE
4320   , LAST_UPDATED_BY
4321   , LAST_UPDATE_LOGIN
4322   , POOL_PERCENT
4323   , PERIOD_TYPE
4324   , SOURCE_AMOUNT_TYPE
4325   , SOURCE_BALANCE_CATEGORY
4326   , SOURCE_BALANCE_TYPE
4327   , ALLOC_RESOURCE_LIST_ID
4328   , AUTO_RELEASE_FLAG
4329   , ALLOCATION_METHOD
4330   , IMP_WITH_EXCEPTION
4331   , DUP_TARGETS_FLAG
4332   , TARGET_EXP_TYPE_CLASS
4333   , TARGET_EXP_ORG_ID
4334   , TARGET_EXP_TYPE
4335   , TARGET_COST_TYPE
4336   , OFFSET_EXP_TYPE_CLASS
4337   , OFFSET_EXP_ORG_ID
4338   , OFFSET_EXP_TYPE
4339   , OFFSET_COST_TYPE
4340   , OFFSET_METHOD
4341   , OFFSET_PROJECT_ID
4342   , OFFSET_TASK_ID
4343   , RUN_STATUS
4344   , BASIS_METHOD
4345   , BASIS_RELATIVE_PERIOD
4346   , BASIS_AMOUNT_TYPE
4347   , BASIS_BALANCE_CATEGORY
4348   , BASIS_BUDGET_TYPE_CODE
4349   , BASIS_FIN_PLAN_TYPE_ID                   /* added bug 2619977 */
4350   , BASIS_BALANCE_TYPE
4351   , BASIS_RESOURCE_LIST_ID
4352   , FISCAL_YEAR
4353   , QUARTER
4354   , PERIOD_NUM
4355   , TARGET_EXP_GROUP
4356   , OFFSET_EXP_GROUP
4357   , TOTAL_POOL_AMOUNT
4358   , ALLOCATED_AMOUNT
4359   , REVERSAL_DATE
4360   , DRAFT_REQUEST_ID
4361   , DRAFT_REQUEST_DATE
4362   , RELEASE_REQUEST_ID
4363   , RELEASE_REQUEST_DATE
4364   , DENOM_CURRENCY_CODE
4365   , FIXED_AMOUNT
4366   , REV_TARGET_EXP_GROUP
4367   , REV_OFFSET_EXP_GROUP
4368   , org_id
4369   , limit_target_projects_code
4370   , cint_rate_name
4371   /* FP.M : Allocation Impact : Bug # 3512552 */
4372   , ALLOC_RESOURCE_STRUCT_TYPE
4373   , BASIS_RESOURCE_STRUCT_TYPE
4374   , ALLOC_RBS_VERSION
4375   , BASIS_RBS_VERSION
4376   )
4377   VALUES (
4378     x_run_id
4379     ---p_run_id
4380   , p_rule_id
4381   , p_run_period
4382   , p_expnd_item_date
4383   , p_creation_date
4384   , p_created_by
4385   , p_last_update_date
4386   , p_last_updated_by
4387   , p_last_update_login
4388   , p_pool_percent
4389   , p_period_type
4390   , p_source_amount_type
4391   , p_source_balance_category
4392   , p_source_balance_type
4393   , p_alloc_resource_list_id
4394   , p_auto_release_flag
4395   , p_allocation_method
4396   , p_imp_with_exception
4397   , p_dup_targets_flag
4398   , p_target_exp_type_class
4399   , p_target_exp_org_id
4400   , p_target_exp_type
4401   , p_target_cost_type
4402   , p_offset_exp_type_class
4403   , p_offset_exp_org_id
4404   , p_offset_exp_type
4405   , p_offset_cost_type
4406   , p_offset_method
4407   , p_offset_project_id
4408   , p_offset_task_id
4409   , p_run_status
4410   , p_basis_method
4411   , p_basis_relative_period
4412   , p_basis_amount_type
4413   , p_basis_balance_category
4414   , p_basis_budget_type_code
4415   , G_basis_fin_plan_type_id    /* added bug 2619977 */
4416   , p_basis_balance_type
4417   , p_basis_resource_list_id
4418   , p_fiscal_year
4419   , p_quarter
4420   , p_period_num
4421   , p_target_exp_group
4422   , p_offset_exp_group
4423   , p_total_pool_amount
4424   , p_allocated_amount
4425   , p_reversal_date
4426   , p_draft_request_id
4427   , p_draft_request_date
4428   , p_release_request_id
4429   , p_release_request_date
4430   , p_denom_currency_code
4431   , p_fixed_amount
4432   , p_rev_target_exp_group
4433   , p_rev_offset_exp_group
4434   , p_org_id
4435   , p_limit_target_projects_code
4436   , p_CINT_RATE_NAME
4437    /* FP.M : Allocation Impact : Bug # 3512552 */
4438   , p_ALLOC_RESOURCE_STRUCT_TYPE
4439   , p_BASIS_RESOURCE_STRUCT_TYPE
4440   , p_ALLOC_RBS_VERSION
4441   , p_BASIS_RBS_VERSION
4442   ) ;
4443   pa_debug.reset_err_stack;
4444 EXCEPTION
4445   WHEN OTHERS THEN
4446   pa_debug.G_err_code:= SQLCODE;
4447   RAISE;
4448 END insert_alloc_runs;
4449 -- ------------------------------------------------------------
4450 -- get_fiscalyear_quarter
4451 -- PROCEDURE : get_fiscalyear_quarter()
4452 --   Purpose : For a given run_period_type (PA/GL) and run_period,
4453 --             this PROCEDURE will get period_type, period_set_name(calender)
4454 --             ,period_year ( Fiscal Year), quarterperiod_num and
4455 --             end date of the run period.
4456 --   Created : 27-JUL-98   Sesivara
4457 -- ------------------------------------------------------------
4458 PROCEDURE get_fiscalyear_quarter(   p_run_period_type  IN VARCHAR2,
4459                                     p_run_period         IN  VARCHAR2 ,
4460                                     x_period_type      OUT NOCOPY VARCHAR2 ,
4461                                     x_period_set_name  OUT NOCOPY VARCHAR2 ,
4462                                     x_period_year      OUT NOCOPY NUMBER   ,
4463                                     x_quarter          OUT NOCOPY NUMBER   ,
4464                                     x_period_num       OUT NOCOPY NUMBER   ,
4465                                     x_run_period_end_date  OUT NOCOPY DATE )
4466 IS
4467   Cursor C_fy_qtr is
4468      select  decode(p_run_period_type,'PA', b.pa_period_type, a.accounted_period_type),
4469              a.period_set_name,glp.period_year,glp.quarter_num,glp.period_num,end_date
4470       from  gl_periods glp,
4471             gl_sets_of_books a,
4472             pa_implementations b
4473       where a.set_of_books_id    =  b.set_of_books_id
4474         and glp.period_set_name  =  a.period_set_name
4475         and glp.period_type      = decode(p_run_period_type,'PA', b.pa_period_type,
4476                                    a.accounted_period_type)
4477         and glp.period_name =  p_run_period ;
4478 BEGIN
4479       OPEN C_fy_qtr ;
4480       FETCH C_fy_qtr into  x_period_type, x_period_set_name, x_period_year,
4481                            x_quarter,x_period_num, x_run_period_end_date;
4482       IF C_fy_qtr%NOTFOUND then
4483           G_fatal_err_found:= TRUE;
4484          alloc_errors(G_rule_id, G_alloc_run_id, 'R', 'E','PA_AL_INVALID_RUN_PERIOD',TRUE) ;
4485       End If;
4486       Close C_fy_qtr    ;
4487 EXCEPTION
4488    WHEN OTHERS THEN
4489       x_period_type      := NULL ;
4490       x_period_set_name  := NULL ;
4491       x_period_year      := NULL ;
4492       x_quarter          := NULL ;
4493       x_period_num       := NULL ;
4494       x_run_period_end_date  := NULL ;
4495       RAISE ;
4496 END get_fiscalyear_quarter ;
4497 -- ==========================================================================
4498 -- PROCEDURE :  populate_RLM_table
4499 --   Purpose   :  Build the included  resource list members and resource percent of a given
4500 --              resource list.
4501 --  Rules: 1.If no members are defines for the RL,then all the defined RLMs are considered.
4502 --         2. If  RLMS are defined, the table is populated with all the  included RLMs.
4503 --         3. If a  RLM is a resource group, its child RLMs are placed in the array
4504 --            after checking for exclusion.
4505 -- Created :   28-JUL-98   Sesivara
4506 -- ==========================================================================
4507 /* removed the plsql table as output from this procedure */
4508 PROCEDURE populate_RLM_table( p_rule_id           IN  NUMBER,
4509                               p_run_id            IN  NUMBER, /* for bug 2211234 */
4510                               p_type              IN  VARCHAR2,
4511                               p_resource_list_id  IN  NUMBER ,
4512 							  /* FP.M : Allocation Impact Bug # 3512552 */
4513 							  p_resource_struct_type in Varchar2 ,
4514 							  p_rbs_version_id	  In Number ,
4515 							  p_basis_category    In Varchar2 /* Added to consider if it is 'A' then only leaf nodes should be considered for basis %. Else all nodes */
4516 							)
4517 IS
4518    v_rl_id              NUMBER ;
4519    v_rlm_id             NUMBER ;
4520    v_chd_rlm_id			NUMBER ;
4521    v_rlm_exists         VARCHAR2(1) := 'N' ;
4522    v_counter            BINARY_INTEGER  ;
4523    v_incld_exists       VARCHAR2(1) := 'N' ; /*2564418 Flagged if an include
4524    							is specified*/
4525    v_excld_exists       VARCHAR2(1) := 'N' ; /*2564418 Flagged if an exclude
4526    							is specified*/
4527    v_child_resource_excl_id pa_plsql_datatypes.IdTabTyp; /* 3567201 : Exclusion Of Childs when Parent Resource Is Excluded. */
4528    v_child_resource_excl_id_temp pa_plsql_datatypes.IdTabTyp; /* 3567201 : Exclusion Of Childs when Parent Resource Is Excluded. */
4529    v_cnt Number := 0;
4530 -- Cursor to get all the Resource list members of a rule
4531    Cursor C_RLM is
4532 	Select * From
4533 		(
4534           Select  par.resource_list_member_id ,
4535                   par.exclude_flag            ,
4536                   nvl(pbr.parent_member_id,0) parent_member_id,
4537                   par.resource_percentage
4538             from  pa_resource_list_members pbr,
4539             /**   pa_budget_resources_v pbr,    ** bug 2661889 */
4540                   pa_alloc_resources par
4541            where  par.rule_id = p_rule_id
4542              and  par.member_type = p_type
4543              and  pbr.resource_list_member_id = par.resource_list_member_id
4544 			 and  display_flag = 'Y'  /* FP.M : Allocation Impact Bug # 3512552 */
4545 			 and  enabled_flag = 'Y'  /* FP.M : Allocation Impact Bug # 3512552 */
4546 			 and  nvl(pbr.migration_code , 'M') = 'M'
4547 			 and  p_resource_struct_type = 'RL'
4548 		Union All
4549 		/* FP.M : Allocation Impact Bug # 3512552 */
4550 		 Select  par.resource_list_member_id					  ,
4551  				 par.exclude_flag							      ,
4552 				 nvl(prbs.parent_element_id , 0) parent_member_id ,
4553 				 par.resource_percentage
4554 		   From  pa_rbs_elements prbs,
4555 		 	     pa_alloc_resources par
4556 		  Where  par.rule_id = p_rule_id
4557 		    and  prbs.user_created_flag = 'N' /* To show only those elements created after summarization process */
4558 		    and  par.member_type = p_type
4559 		    and  prbs.rbs_version_id = p_rbs_version_id
4560 		    and  prbs.rbs_element_id = par.resource_list_member_id
4561 			and  p_resource_struct_type = 'RBS'
4562 		)
4563 	order by  exclude_flag, parent_member_id, resource_list_member_id ;
4564 -- Cursor to all defined resource list members of a resource list
4565 /******************    Bug 3149853 Starts   ****************************
4566  * Modified Cursor query for performance improvement.
4567  *  --2564418. Added the second condition in the where clause below
4568  *     cursor C_RL_RLM is
4569  *        Select resource_list_member_id
4570  *          from  pa_resource_list_members
4571  *		-- pa_budget_resources_v     -- bug 2661889
4572  *         where resource_list_id     = p_resource_list_id
4573  *		-- 2564418 changes start
4574  *            AND resource_list_member_id NOT IN
4575  *                              (select resource_list_member_id
4576  *                               from pa_alloc_resources
4577  *                               where exclude_flag='Y'
4578  *                                AND rule_id = p_rule_id);
4579  *		-- 2564418 changes end.
4580 ******************  End of comment  *****************************/
4581 	 Cursor C_RL_RLM_ALL is /* Cursor Created for performnace : Derived from C_RL_RLM and removed the exists call.
4582 							   This cursor will be used When no include and no exclude is defined. */
4583         Select  prlm.resource_list_member_id
4584           From  pa_resource_list_members prlm
4585          Where  prlm.resource_list_id    = p_resource_list_id
4586 		   And  display_flag = 'Y'  /* FP.M : Allocation Impact Bug # 3512552 */
4587 		   And  enabled_flag = 'Y'  /* FP.M : Allocation Impact Bug # 3512552 */
4588 		   And  nvl(prlm.migration_code , 'M') = 'M';
4589 	Cursor C_RL_RLM_RBS_ALL_ACT is /* Cursor Created for performnace : Derived from C_RL_RLM and removed the exists call.
4590 									  This cursor will be used When no include and no exclude is defined.
4591 									  This cursor is used only in source and basis ( if the basis_category is actuals )
4592 								   */
4593         Select prbs.rbs_element_id
4594 		  From pa_rbs_elements prbs
4595 		 Where prbs.rbs_version_id = p_rbs_version_id
4596 		   and prbs.user_created_flag = 'N' /* To show only those elements created after summarization process */
4597 		   and prbs.resource_type_id <> -1 /* To remove first record of Version Info*/
4598 		   and Not Exists (Select '1'
4599 		                    From pa_rbs_elements rbs_chd
4600 						   Where rbs_chd.rbs_version_id = p_rbs_version_id
4601 						     and rbs_chd.user_created_flag = 'N' /* To show only those elements created after summarization process */
4602 						     And rbs_chd.parent_element_id = prbs.rbs_element_id
4603 						  ); /* To select only Leaf nodes in case of Actuals */
4604 	Cursor C_RL_RLM_RBS_ALL_NON_ACT is /* Cursor Created for performnace : Derived from C_RL_RLM and removed the exists call.
4605 										   This cursor will be used When no include and no exclude is defined.
4606 										   This cursor is used only in basis and basis category is not actuals. Only
4607 										   If the basis Category is Finplan or Budgets
4608 										   */
4609         Select prbs.rbs_element_id
4610 		  From pa_rbs_elements prbs
4611 		 Where prbs.rbs_version_id = p_rbs_version_id
4612 		   and prbs.user_created_flag = 'N' /* To show only those elements created after summarization process */
4613 		   and prbs.resource_type_id <> -1 ; /* To remove first record of Version Info*/
4614 									  	     /* In this case, data can be there for intermediate nodes also. So to insert all the records */
4615 	 Cursor C_RL_RLM is
4616         Select  prlm.resource_list_member_id
4617           from  pa_resource_list_members prlm
4618          where  prlm.resource_list_id    = p_resource_list_id
4619 		   And  display_flag = 'Y'  /* FP.M : Allocation Impact Bug # 3512552 */
4620 		   And  enabled_flag = 'Y'  /* FP.M : Allocation Impact Bug # 3512552 */
4621 		   And  nvl(prlm.migration_code , 'M') = 'M'
4622            AND  NOT exists
4623                 (select par.resource_list_member_id
4624                  from pa_alloc_resources par
4625                  where par.exclude_flag = 'Y'
4626 				 And par.member_type = p_type /* Bug 3819804 */
4627                  AND par.rule_id = p_rule_id
4628                  and prlm.resource_list_member_id = par.resource_list_member_id
4629 				)
4630 		   AND p_resource_struct_type = 'RL';
4631    /* FP.M : Allocation Impact Bug # 3512552 */
4632 	Cursor C_RL_RLM_RBS_ACT is
4633         Select prbs.rbs_element_id
4634 		  From pa_rbs_elements prbs
4635 		 Where prbs.rbs_version_id = p_rbs_version_id
4636 		   and prbs.user_created_flag = 'N' /* To show only those elements created after summarization process */
4637 		   and NOT exists
4638                 (Select par.resource_list_member_id
4639                    From pa_alloc_resources par
4640                   Where par.exclude_flag = 'Y'
4641 				    And par.member_type = p_type /* Bug 3819804 */
4642                     AND par.rule_id = p_rule_id
4643                     AND prbs.rbs_element_id = par.resource_list_member_id
4644 				)
4645 		   and Not Exists ( Select '1'
4646 		                      From pa_rbs_elements chd_prbs
4647 							 where chd_prbs.rbs_version_id    = p_rbs_Version_id
4648 							   and chd_prbs.user_created_flag = 'N' /* To show only those elements created after summarization process */
4649 							   And chd_prbs.parent_element_id = prbs.rbs_element_id
4650 						  ) /* To fetch only leaf nodes */
4651            and p_resource_struct_type = 'RBS';
4652 	Cursor C_RL_RLM_RBS_NON_ACT is
4653         Select prbs.rbs_element_id
4654 		  From pa_rbs_elements prbs
4655 		 Where prbs.rbs_version_id = p_rbs_version_id
4656    		   and prbs.user_created_flag = 'N' /* To show only those elements created after summarization process */
4657 		   and NOT exists
4658                 (Select par.resource_list_member_id
4659                    From pa_alloc_resources par
4660                   Where par.exclude_flag = 'Y'
4661 				    And par.member_type = p_type /* Bug 3819804 */
4662                     AND par.rule_id = p_rule_id
4663                     AND prbs.rbs_element_id = par.resource_list_member_id
4664 				)
4665            and p_resource_struct_type = 'RBS';
4666    resource_list_member_tab pa_plsql_datatypes.IdTabTyp;
4667 /******************    Bug 3149853 Ends   ***************************/
4668 -- Cursor to all defined resource list members of a resource group
4669    Cursor C_RG_RLM is
4670          Select resource_list_member_id
4671            from  pa_resource_list_members
4672            /**   pa_budget_resources_v  ** bug 2661889 */
4673           where nvl(parent_member_id,0) = v_rlm_id
4674             and  resource_list_id     = p_resource_list_id
4675 			and  display_flag = 'Y'  /* FP.M : Allocation Impact Bug # 3512552 */
4676 			and  enabled_flag = 'Y'  /* FP.M : Allocation Impact Bug # 3512552 */
4677 			And  nvl(migration_code , 'M') = 'M'
4678             and  p_resource_struct_type = 'RL';
4679 	/* FP.M : Allocation Impact Bug # 3512552 */
4680    Cursor C_RG_RLM_RBS_ACT Is
4681 		Select Rbs_Element_Id  resource_list_member_id
4682 		  From pa_rbs_elements prbs
4683 		 Where Rbs_Version_Id = p_rbs_version_id
4684 		   and prbs.user_created_flag = 'N' /* To show only those elements created after summarization process */
4685 		   AND p_resource_struct_type = 'RBS'
4686 		   AND Not Exists (
4687 							Select '1'
4688 							  From pa_rbs_elements chd_prbs
4689 							 Where chd_prbs.rbs_version_id = p_rbs_version_id
4690 							   and chd_prbs.user_created_flag = 'N' /* To show only those elements created after summarization process */
4691 							   and chd_prbs.parent_element_id = prbs.rbs_element_id
4692 						   )
4693 		  Start with Rbs_Element_Id = v_rlm_id
4694 		Connect By Prior rbs_element_id = parent_element_id;
4695      Cursor C_RG_RLM_RBS_NON_ACT Is
4696 		Select Rbs_Element_Id  resource_list_member_id
4697 		  From pa_rbs_elements prbs
4698 		 Where Rbs_Version_Id = p_rbs_version_id
4699 		   and prbs.user_created_flag = 'N' /* To show only those elements created after summarization process */
4700 		   AND p_resource_struct_type = 'RBS'
4701 		  Start with Rbs_Element_Id = v_rlm_id
4702 		Connect By Prior rbs_element_id = parent_element_id;
4703   /* FP.M : Allocation Impact Bug # 3512552
4704      Bug # 3567201 : Child Exlcusion When Parent Exlcluded
4705   */
4706    Cursor C_Exc_RLM Is
4707 		 Select Resource_List_Member_Id
4708 		   From pa_alloc_resources
4709 		  Where Rule_Id = P_Rule_Id
4710 		    And Member_Type = P_type
4711 			And Exclude_Flag = 'Y';
4712   /* FP.M : Allocation Impact Bug # 3512552
4713      Bug # 3567201 : Child Exlcusion When Parent Exlcluded
4714   */
4715 Function check_rlm_excluded( p_rule_id IN NUMBER  ,
4716                              p_type    IN VARCHAR2,
4717                              p_rlm_id  IN NUMBER  ) RETURN VARCHAR2
4718 IS
4719     v_rlm_excluded  VARCHAR2(1) ;
4720     cursor c_rlm_excluded is
4721        Select  'Y'
4722          from  pa_alloc_resources
4723         where  rule_id  = p_rule_id
4724           and  member_type = p_type
4725           and  resource_list_member_id = p_rlm_id
4726           and  exclude_flag = 'Y'  ;
4727 BEGIN
4728        v_rlm_excluded := 1 ;
4729        OPEN c_rlm_excluded ;
4730        FETCH c_rlm_excluded INTO v_rlm_excluded ;
4731        If c_rlm_excluded%NOTFOUND then
4732           v_rlm_excluded :='N';
4733        End If ;
4734        CLOSE c_rlm_excluded ;
4735        return ( v_rlm_excluded) ;
4736 EXCEPTION
4737        WHEN OTHERS THEN
4738           RAISE ;
4739 END Check_rlm_excluded ;
4740 Function check_rlm_exists( p_rule_id IN NUMBER,
4741                            p_type    IN VARCHAR2  ) RETURN VARCHAR2
4742 IS
4743 	v_rlm_exists  VARCHAR2(1);
4744     cursor c_rlm_exists is
4745        select 'Y'
4746          from  pa_alloc_resources
4747         where  rule_id  = p_rule_id
4748           and  member_type = p_type  ;
4749 BEGIN
4750 	   v_rlm_exists  := 'N' ;
4751        OPEN c_rlm_exists ;
4752        FETCH c_rlm_exists INTO v_rlm_exists ;
4753        If c_rlm_exists%NOTFOUND then
4754           v_rlm_exists :='N';
4755        End If ;
4756        CLOSE c_rlm_exists ;
4757        return ( v_rlm_exists) ;
4758 EXCEPTION
4759        WHEN OTHERS then
4760           RAISE ;
4761 END Check_Rlm_Exists ;
4762 Function check_child_rlm_exists( p_rlm_id IN NUMBER ) RETURN VARCHAR2
4763 IS
4764        v_rlm_exists  VARCHAR2(1) ;
4765        cursor c_child_rlm is
4766            select 'Y'
4767            from  pa_resource_list_members
4768            /**   pa_budget_resources_v     ** bug 2661889 */
4769             where  nvl(parent_member_id,0)  = p_rlm_id
4770 			  and  display_flag = 'Y'  /* FP.M : Allocation Impact Bug # 3512552 */
4771 			  and  enabled_flag = 'Y'  /* FP.M : Allocation Impact Bug # 3512552 */
4772 			  And  nvl(migration_code , 'M') = 'M';
4773 	/* FP.M : Allocation Impact : Bug # 3512552 */
4774 	   cursor c_child_rbs_rlm Is
4775 	     Select 'Y'
4776 		   From pa_rbs_elements
4777 		  Where rbs_version_id = p_rbs_version_id
4778 		    and user_created_flag = 'N' /* To show only those elements created after summarization process */
4779 		    And Nvl(parent_element_id,0) = p_rlm_id
4780 			And RowNum = 1;
4781 BEGIN
4782       v_rlm_exists := 'N' ;
4783 	  If nvl(p_resource_struct_type,'RL') = 'RL' then
4784 		  OPEN c_child_rlm ;
4785 		  FETCH c_child_rlm INTO v_rlm_exists ;
4786 	      If c_child_rlm%NOTFOUND then
4787 		      v_rlm_exists :='N';
4788 	      End If ;
4789 		  CLOSE c_child_rlm ;
4790 	  Else
4791 		  OPEN c_child_rbs_rlm ;
4792 		  FETCH c_child_rbs_rlm INTO v_rlm_exists ;
4793 	      If c_child_rbs_rlm%NOTFOUND then
4794 		      v_rlm_exists :='N';
4795 	      End If ;
4796 		  CLOSE c_child_rbs_rlm ;
4797 	  End If;
4798 	  Return ( v_rlm_exists) ;
4799 EXCEPTION
4800        WHEN OTHERS THEN
4801           RAISE ;
4802 END Check_child_rlm_exists;
4803 /* added for 2211234 */
4804 Procedure insert_alloc_run_resources(p_run_id IN NUMBER,
4805                                      p_rule_id IN NUMBER,
4806                                      p_member_type IN VARCHAR2,
4807                                      p_res_list_member_id IN NUMBER,
4808                                      p_resource_percent  IN NUMBER)
4809 IS
4810 BEGIN
4811      insert into pa_alloc_run_resource_det (
4812             rule_id,
4813             run_id,
4814             member_type,
4815             resource_list_member_id,
4816             resource_percent,
4817             creation_date,
4818             created_by,
4819             last_update_date,
4820             last_updated_by,
4821             last_update_login)
4822      values(p_rule_id,
4823             p_run_id,
4824             p_member_type,
4825             p_res_list_member_id ,
4826             p_resource_percent,
4827             G_creation_date,
4828             G_created_by,
4829             G_last_update_date,
4830             G_last_updated_by,
4831             G_last_update_login) ;
4832 EXCEPTION
4833      WHEN OTHERS THEN
4834      RAISE;
4835 END;
4836  -- Bug3149853 .Changes done to enhance performance .
4837  -- Created procedure to perform BULK INSERT into pa_alloc_run_resource_det.
4838 Procedure bulk_ins_alloc_run_res(p_run_id 			IN NUMBER,
4839 				  p_rule_id 			IN NUMBER,
4840 				  p_member_type 		IN VARCHAR2,
4841 				  p_res_list_member_id_tab	IN pa_plsql_datatypes.IdTabTyp,
4842 				  p_resource_percent  		IN NUMBER)
4843 IS
4844 BEGIN
4845         FORALL i IN p_res_list_member_id_tab.first..p_res_list_member_id_tab.last
4846         insert into pa_alloc_run_resource_det (
4847                     rule_id,
4848                     run_id,
4849                     member_type,
4850                     resource_list_member_id,
4851                     resource_percent,
4852                     creation_date,
4853                     created_by,
4854                     last_update_date,
4855                     last_updated_by,
4856                     last_update_login)
4857              values(p_rule_id,
4858                     p_run_id,
4859                     p_type,
4860                     resource_list_member_tab(i),
4861                     100,
4862                     G_creation_date,
4863                     G_created_by,
4864                     G_last_update_date,
4865                     G_last_updated_by,
4866                     G_last_update_login) ;
4867 EXCEPTION
4868      WHEN OTHERS THEN
4869      RAISE;
4870 END;
4871 /*
4872   Created By : Vthakkar
4873   Created Date : 26-Apr-2004
4874   Desc : to store all the excluded resources of Resource List or RBS in Allocation Rule for Basis or Source.
4875 */
4876 procedure fetch_all_excludes
4877 Is
4878 Begin
4879 	------------------------------------------------------------------
4880 	---- Filling All Excludes And All its childs to The PLSQL table ----
4881 	IF P_DEBUG_MODE = 'Y' THEN
4882 		pa_debug.write_file('fetch_all_excludes: ' ||  'LOG',' Inside fetch_all_excludes');
4883 	END IF;
4884 	v_cnt := 0;
4885 	v_child_resource_excl_id.delete;
4886 	If Nvl(p_resource_struct_type,'RL') = 'RL' Then
4887 			IF P_DEBUG_MODE = 'Y' THEN
4888 				pa_debug.write_file('fetch_all_excludes: ' ||  'LOG',' In case of RL');
4889 			END IF;
4890 			Declare
4891 				Cursor C_Exc_Chd_Rlm (V_parent_rlm in Number)
4892 				Is
4893 				  Select resource_list_member_id
4894 				    From pa_resource_list_members
4895 				   Where Resource_List_Id = p_resource_list_Id
4896 				     And Parent_member_Id = v_parent_rlm
4897 					 And display_flag = 'Y'  /* FP.M : Allocation Impact Bug # 3512552 */
4898 		 			 And enabled_flag = 'Y'  /* FP.M : Allocation Impact Bug # 3512552 */
4899 					 And nvl(migration_code , 'M') = 'M'
4900 					 And p_resource_struct_type = 'RL';
4901 			Begin
4902 				Open C_Exc_RLM;
4903 				Loop
4904 					IF P_DEBUG_MODE = 'Y' THEN
4905 						pa_debug.write_file('fetch_all_excludes: ' ||  'LOG',' In case of RL 1 ');
4906 					END IF;
4907 					Resource_List_Member_tab.Delete;
4908 					Fetch C_Exc_RLM Bulk Collect Into Resource_List_Member_tab Limit 500;
4909 					If Not Resource_List_Member_Tab.Exists(1) Then
4910 						Exit;
4911 					End If;
4912 					For k in 1..Resource_List_Member_tab.count
4913 					Loop
4914 						Open  C_Exc_Chd_Rlm(Resource_List_Member_tab(k));
4915 						IF P_DEBUG_MODE = 'Y' THEN
4916 							pa_debug.write_file('fetch_all_excludes: ' ||  'LOG',' RL : Child Member ' || Resource_List_Member_tab(k) );
4917 						END IF;
4918 						Loop
4919 							v_child_resource_excl_id_temp.Delete;
4920 							Fetch C_Exc_Chd_Rlm Bulk Collect
4921 							 Into v_child_resource_excl_id_temp
4922 							Limit 500;
4923 							If Not v_child_resource_excl_id_temp.Exists(1) Then
4924 								Exit;
4925 							End If;
4926 							For i in 1..v_child_resource_excl_id_temp.count
4927 							Loop
4928 								v_cnt := v_cnt + 1;
4929 								v_child_resource_excl_id (v_cnt) := v_child_resource_excl_id_temp(i);
4930 							End Loop;
4931 						  End Loop;
4932 						/* Include The Node Itself also */
4933 						v_cnt := v_cnt + 1;
4934 						v_child_resource_excl_id (v_cnt) := Resource_List_Member_tab(k);
4935 						Close C_Exc_Chd_Rlm;
4936 					 End Loop;
4937 				End Loop;
4938 				Close C_Exc_RLM;
4939 			 End;
4940 	ElsIf Nvl(p_resource_struct_type,'RL') = 'RBS' Then
4941 			IF P_DEBUG_MODE = 'Y' THEN
4942 				pa_debug.write_file('fetch_all_excludes: ' ||  'LOG',' In case of RBS');
4943 			END IF;
4944 			Declare
4945 				Cursor C_Exc_Chd_Rlm_RBS (V_parent_rlm in Number)
4946 				    Is
4947 				  Select Rbs_Element_Id  resource_list_member_id
4948 					From pa_rbs_elements
4949 				   Where Rbs_Version_Id = p_rbs_version_id
4950 				     and user_created_flag = 'N' /* To show only those elements created after summarization process */
4951 					 And p_resource_struct_type = 'RBS'
4952 				   Start With Parent_element_Id = V_parent_Rlm
4953 				  Connect By Prior Rbs_Element_Id = Parent_Element_Id;
4954 			Begin
4955 				Open C_Exc_RLM;
4956 				Loop
4957 					IF P_DEBUG_MODE = 'Y' THEN
4958 						pa_debug.write_file('fetch_all_excludes: ' ||  'LOG',' In case of RBS 1 ');
4959 					END IF;
4960 					Resource_List_Member_tab.Delete;
4961 					Fetch C_Exc_RLM Bulk Collect Into Resource_List_Member_tab Limit 500;
4962 					If Not Resource_List_Member_Tab.Exists(1) Then
4963 						Exit;
4964 					End If;
4965 					For k in 1..Resource_List_Member_tab.count
4966 					Loop
4967 						Open  C_Exc_Chd_Rlm_RBS(Resource_List_Member_tab(k));
4968 						IF P_DEBUG_MODE = 'Y' THEN
4969 							pa_debug.write_file('fetch_all_excludes: ' ||  'LOG',' RBS : Child Member ' || Resource_List_Member_tab(k) );
4970 						END IF;
4971 						Loop
4972 							v_child_resource_excl_id_temp.Delete;
4973 							Fetch C_Exc_Chd_Rlm_RBS Bulk Collect
4974 							 Into v_child_resource_excl_id_temp
4975 							Limit 500;
4976 							If Not v_child_resource_excl_id_temp.Exists(1) Then
4977 								Exit;
4978 							End If;
4979 							For i in 1..v_child_resource_excl_id_temp.count
4980 							Loop
4981 								v_cnt := v_cnt + 1;
4982 								v_child_resource_excl_id (v_cnt) := v_child_resource_excl_id_temp(i);
4983 							End Loop;
4984 						 End Loop;
4985 						 /* Include The Node Itself also */
4986 						v_cnt := v_cnt + 1;
4987 						v_child_resource_excl_id (v_cnt) := Resource_List_Member_tab(k);
4988 						Close C_Exc_Chd_Rlm_RBS;
4989 					 End Loop;
4990 				End Loop;
4991 				Close C_Exc_RLM;
4992 			End;
4993 	End If;
4994 	------------------------------------------------------------------
4995 EXCEPTION
4996        WHEN OTHERS then
4997           RAISE ;
4998 End fetch_all_excludes;
4999 /*
5000   Created By : Vthakkar
5001   Created Date : 26-Apr-2004
5002   Desc : to validate a resource list member whether it's included or excluded.
5003 */
5004 Function Is_Excluded_Rlm (v_rlm_id in Number) return Boolean
5005 Is
5006 	l_ret_flag Boolean := False;
5007 Begin
5008 	For i in 1..v_child_resource_excl_id.count
5009 		Loop
5010 			If v_child_resource_excl_id(i) = v_rlm_Id Then
5011 				l_ret_flag := True;
5012 				Return l_ret_flag;
5013 			End If;
5014 		End Loop;
5015 	Return l_ret_flag;
5016 EXCEPTION
5017        WHEN OTHERS then
5018 		Return l_ret_flag;
5019 End Is_Excluded_Rlm;
5020 BEGIN
5021 v_counter := 1 ;
5022 IF P_DEBUG_MODE = 'Y' THEN
5023 	pa_debug.write_file('populate_RLM_table: ' ||  'LOG','Inside populate_RLM');
5024 END IF;/*2564418*/
5025 IF check_rlm_exists(p_rule_id, p_type) = 'N' then
5026     IF P_DEBUG_MODE = 'Y' THEN
5027        pa_debug.write_file('populate_RLM_table: ' || 'LOG','If check_rlm_exists is N');
5028     END IF;/*2564418*/
5029 --   populate inc_rlm_tbl using pa_budget_resources_v for
5030 --   given alloc_rule.alloc_resource_list ;
5031 /***********************   Bug 3149853 starts  *****************************
5032     For RL_RLM_REC in C_RL_RLM LOOP
5033     --  commented for 2211234
5034     --  x_inc_rlm_tbl(v_counter).resource_list_member_id := RL_RLM_REC.resource_list_member_id ;
5035     --  x_inc_rlm_tbl(v_counter).resource_percent := 100 ;
5036     --  v_counter := v_counter + 1 ;
5037         insert_alloc_run_resources(p_run_id             => p_run_id
5038                                   ,p_rule_id            => p_rule_id
5039                                   ,p_member_type        => p_type
5040                                   ,p_res_list_member_id => RL_RLM_REC.resource_list_member_id
5041                                   ,p_resource_percent   => 100);
5042     END LOOP ;
5043 ********************   Commented for bug 3149853   ***********************/
5044 	If NVL(p_resource_struct_type,'RL') = 'RL' Then
5045 		Open C_RL_RLM_ALL;
5046 	Elsif NVL(p_resource_struct_type,'RL') = 'RBS' Then
5047 		If p_type = 'S' Then
5048 			Open C_RL_RLM_RBS_ALL_ACT;
5049 		Elsif P_type = 'B' Then
5050 			If p_basis_category = 'A' Then
5051 				Open C_RL_RLM_RBS_ALL_ACT;
5052 			Else
5053 				Open C_RL_RLM_RBS_ALL_NON_ACT;
5054 			End If;
5055 		End If;
5056 	End IF;
5057     LOOP
5058 		Resource_list_member_tab.delete;
5059 		If NVL(p_resource_struct_type,'RL') = 'RL' Then
5060 			Fetch C_RL_RLM_ALL BULK COLLECT INTO resource_list_member_tab LIMIT 500;
5061 		ElsIf NVL(p_resource_struct_type,'RL') = 'RBS' Then
5062 			If p_type = 'S' Then
5063 				Fetch C_RL_RLM_RBS_ALL_ACT BULK COLLECT INTO resource_list_member_tab LIMIT 500;
5064 			ElsIf p_type = 'B' Then
5065 				If P_basis_category = 'A' Then
5066 					Fetch C_RL_RLM_RBS_ALL_ACT BULK COLLECT INTO resource_list_member_tab LIMIT 500;
5067 				Else
5068 					Fetch C_RL_RLM_RBS_ALL_NON_ACT BULK COLLECT INTO resource_list_member_tab LIMIT 500;
5069 				End If;
5070 			End If;
5071 		End If;
5072 		If NOT resource_list_member_tab.exists(1) Then
5073                 EXIT;
5074         End If;
5075         bulk_ins_alloc_run_res(     p_run_id             	 => p_run_id
5076                                   , p_rule_id            	 => p_rule_id
5077                                   , p_member_type        	 => p_type
5078                                   , p_res_list_member_id_tab => resource_list_member_tab
5079                                   , p_resource_percent   	 => 100
5080 							   );
5081     END LOOP;
5082 	If NVL(p_resource_struct_type,'RL') = 'RL' Then
5083 	    CLOSE C_RL_RLM_ALL;
5084 	ElsIf NVL(p_resource_struct_type,'RL') = 'RBS' Then
5085 		If p_type = 'S' Then
5086 			CLOSE C_RL_RLM_RBS_ALL_ACT;
5087 		Elsif p_type = 'B' Then
5088 			if P_basis_category = 'A' Then
5089 				CLOSE C_RL_RLM_RBS_ALL_ACT;
5090 			Else
5091 				CLOSE C_RL_RLM_RBS_ALL_NON_ACT;
5092 			End If;
5093 		End If;
5094 	End IF;
5095 /***********************   Bug 3149853 ends  *****************************/
5096  Else
5097 	Fetch_all_excludes; /* Added For 3567201 */
5098 	IF P_DEBUG_MODE = 'Y' THEN
5099        pa_debug.write_file('populate_RLM_table: ' || 'LOG','If some RLM exists....');
5100     END IF;/*2564418*/
5101 	For RLM_REC in C_RLM LOOP
5102 		v_rlm_id := RLM_REC.resource_list_member_id ;
5103 		If RLM_REC.exclude_flag <> 'Y' then
5104 		   IF P_DEBUG_MODE = 'Y' THEN
5105      	      pa_debug.write_file('populate_RLM_table: ' || 'LOG','Some include exists....');
5106      	   END IF; /*2564418*/
5107 		   v_incld_exists := 'Y'; /*2564418 Hence some include is specified*/
5108 		   If (
5109 				 (
5110 				    nvl(RLM_REC.parent_member_id,0) = 0
5111 		         AND check_child_rlm_exists(v_rlm_id) = 'Y'
5112 				 AND NVL(p_resource_struct_type,'RL') = 'RL'
5113 				 )
5114 				 OR
5115 				 (
5116 					NVL(p_resource_struct_type,'RL') = 'RBS'
5117 				 AND check_child_rlm_exists(v_rlm_id) = 'Y'
5118 				 )
5119 			 ) then
5120 			  IF P_DEBUG_MODE = 'Y' THEN
5121 				   pa_debug.write_file('populate_RLM_table: ' || 'LOG','A RG is included specifically');
5122 			  END IF;/*2564418*/
5123 			  If NVL(p_resource_struct_type,'RL') = 'RL' Then
5124 				Open C_RG_RLM;
5125 			  Elsif NVL(p_resource_struct_type,'RL') = 'RBS' Then
5126 				If P_Type = 'S' Then
5127 					Open C_RG_RLM_RBS_ACT;
5128 				Elsif p_type = 'B' Then
5129 					If p_basis_Category = 'A' Then
5130 						Open C_RG_RLM_RBS_ACT;
5131 					Else
5132 						Open C_RG_RLM_RBS_NON_ACT;
5133 					End If;
5134 				End If;
5135 			  End If;
5136 			  Loop
5137 				 If NVL(p_resource_struct_type,'RL') = 'RL' Then
5138 					Fetch C_RG_RLM INTO v_chd_rlm_id ;
5139 					If C_RG_RLM%NOTFOUND Then
5140 						Exit;
5141 					End If;
5142 			     Else
5143 					If p_type = 'S' Then
5144 						Fetch C_RG_RLM_RBS_ACT INTO v_chd_rlm_id ;
5145 						If C_RG_RLM_RBS_ACT%NOTFOUND Then
5146 							Exit;
5147 						End If;
5148 					Elsif P_Type = 'B' Then
5149 						If P_basis_category = 'A' Then
5150 							Fetch C_RG_RLM_RBS_ACT INTO v_chd_rlm_id ;
5151 							If C_RG_RLM_RBS_ACT%NOTFOUND Then
5152 								Exit;
5153 							End If;
5154 						Else
5155 							Fetch C_RG_RLM_RBS_NON_ACT INTO v_chd_rlm_id ;
5156 							If C_RG_RLM_RBS_NON_ACT%NOTFOUND Then
5157 								Exit;
5158 							End If;
5159 						End If;
5160 					End If;
5161 				 End If;
5162 				 IF P_DEBUG_MODE = 'Y' THEN
5163                     pa_debug.write_file('populate_RLM_table: ' || 'LOG','v_counter: '|| to_char(v_counter)||' RLM ID :' || to_char(v_rlm_id ));
5164                  END IF;
5165 				 --- If check_rlm_excluded(p_rule_id, p_type, v_chd_rlm_id ) = 'N' then
5166 				 /* Check is thru now from PLSQL tables For 3567201 */
5167 				   /* commented for bug 2211234
5168                     x_inc_rlm_tbl(v_counter).resource_list_member_id := v_chd_rlm_id ;
5169                     x_inc_rlm_tbl(v_counter).resource_percent :=  nvl(v_chd_rlm_id,100) ;
5170                     v_counter := v_counter+1 ;
5171                    */
5172                    /* added for 2211234 */
5173 					If Not Is_Excluded_Rlm (v_chd_rlm_id) Then /* Added For 3567201 */
5174 						insert_alloc_run_resources(
5175 						                           p_run_id             => p_run_id
5176 												  ,p_rule_id            => p_rule_id
5177                                                   ,p_member_type        => p_type
5178                                                   ,p_res_list_member_id => v_chd_rlm_id
5179                                                   ,p_resource_percent   => nvl(RLM_REC.resource_percentage,100)
5180 												  );
5181 					End If;
5182                  ---- End if;
5183 				END LOOP ;
5184 				If NVL(p_resource_struct_type,'RL') = 'RL' Then
5185 					Close C_RG_RLM;
5186 			    Elsif NVL(p_resource_struct_type,'RL') = 'RBS' Then
5187 				    If P_Type = 'S' Then
5188 					    Close C_RG_RLM_RBS_ACT;
5189 					Elsif P_type = 'B' Then
5190 						If p_basis_category = 'A' Then
5191 							Close C_RG_RLM_RBS_ACT;
5192 						Else
5193 							Close C_RG_RLM_RBS_NON_ACT;
5194 						End If;
5195 					End If;
5196 			    End If;
5197 -- Bug 914304: If Budget is at resource group level it is ignored for basis
5198 -- Fix: Added the resource group into the RLM table if the p_type = 'B'
5199 			   If p_type = 'B' And NVL(p_resource_struct_type,'RL') = 'RL' then
5200 				 /* commented for 2211234
5201 					x_inc_rlm_tbl(v_counter).resource_list_member_id :=  RLM_REC.resource_list_member_id ;
5202 	                x_inc_rlm_tbl(v_counter).resource_percent := RLM_REC.resource_percentage ;
5203 		            v_counter := v_counter+1 ;
5204 			     */
5205 				 /* added for 2211234 */
5206 				 insert_alloc_run_resources(
5207 										   p_run_id             => p_run_id
5208                                           ,p_rule_id            => p_rule_id
5209                                           ,p_member_type        => p_type
5210                                           ,p_res_list_member_id => RLM_REC.resource_list_member_id
5211                                           ,p_resource_percent   => RLM_REC.resource_percentage
5212 										   );
5213               End if ;
5214 		  Else
5215 			  IF P_DEBUG_MODE = 'Y' THEN
5216                  pa_debug.write_file('populate_RLM_table: ' || 'LOG','v_counter : ' || to_char(v_counter) );
5217               END IF;
5218 			  --- If check_rlm_excluded (p_rule_id, p_type, RLM_REC.resource_list_member_id) = 'N' then
5219 			   /* Check is thru now from PLSQL tables For 3567201 */
5220 				/* commented for bug 2211234
5221                  x_inc_rlm_tbl(v_counter).resource_list_member_id := RLM_REC.resource_list_member_id ;
5222                  x_inc_rlm_tbl(v_counter).resource_percent := nvl(RLM_REC.resource_percentage,100) ;
5223                  v_counter := v_counter+1 ;
5224                 */
5225 				/* added for 2211234 */
5226 				If Not Is_Excluded_Rlm (RLM_REC.resource_list_member_id) Then /* Added For 3567201 */
5227 					insert_alloc_run_resources(p_run_id             => p_run_id
5228                                           ,p_rule_id            => p_rule_id
5229                                           ,p_member_type        => p_type
5230                                           ,p_res_list_member_id => RLM_REC.resource_list_member_id
5231                                           ,p_resource_percent   => nvl(RLM_REC.resource_percentage,100));
5232 				End If;
5233               --- End if ;
5234 		   End if ;
5235 	Else /* If the RLM is an exclude. 2564418 changes start here */
5236   	   IF P_DEBUG_MODE = 'Y' THEN
5237   	      pa_debug.write_file('populate_RLM_table: ' || 'LOG','If an exclude is specified...');
5238   	   END IF;
5239 	   If (v_incld_exists = 'N') then /* no include has been specified */
5240 		IF P_DEBUG_MODE = 'Y' THEN
5241 		   pa_debug.write_file('populate_RLM_table: ' || 'LOG','No include has been specified...');
5242 		END IF;
5243 		/*include all RLM but this one */
5244 /***********************   Bug 3149853 starts  *****************************
5245                 For RL_RLM_REC in C_RL_RLM LOOP
5246                   if(v_excld_exists <> 'Y') then
5247                         IF P_DEBUG_MODE = 'Y' THEN
5248                            pa_debug.write_file('populate_RLM_table: ' || 'LOG','Inserting the other members-- '|| RLM_REC.resource_list_member_id);
5249                         END IF;
5250                         insert_alloc_run_resources(p_run_id     => p_run_id
5251 												  ,p_rule_id            => p_rule_id
5252 												  ,p_member_type        => p_type
5253                                                   ,p_res_list_member_id => RL_RLM_REC.resource_list_member_id
5254 												  ,p_resource_percent   => nvl(RLM_REC.resource_percentage,100));
5255                   end if;
5256                 END LOOP;
5257 ********************   Commented for bug 3149853   ***********************/
5258 				if ( v_excld_exists <> 'Y') then	-- Added in version 115.69.40.3.
5259 					If NVL(p_resource_struct_type,'RL') = 'RL' Then
5260 						Open C_RL_RLM;
5261 					Elsif NVL(p_resource_struct_type,'RL') = 'RBS' Then
5262 						If p_Type = 'S' Then
5263 							Open C_RL_RLM_RBS_ACT;
5264 						ElsIf P_Type = 'B' Then
5265 							If P_basis_category = 'A' Then
5266 								Open C_RL_RLM_RBS_ACT;
5267 							Else
5268 								Open C_RL_RLM_RBS_NON_ACT;
5269 							End If;
5270 						End If;
5271 					End IF;
5272 					LOOP
5273 						Resource_list_member_tab.delete;
5274 						If NVL(p_resource_struct_type,'RL') = 'RL' Then
5275 							Fetch C_RL_RLM BULK COLLECT INTO resource_list_member_tab LIMIT 500;
5276 						ElsIf NVL(p_resource_struct_type,'RL') = 'RBS' Then
5277 							If p_type = 'S' Then
5278 								Fetch C_RL_RLM_RBS_ACT BULK COLLECT INTO resource_list_member_tab LIMIT 500;
5279 							ElsIf P_TYpe = 'B' Then
5280 								If P_basis_category = 'A' Then
5281 									Fetch C_RL_RLM_RBS_ACT BULK COLLECT INTO resource_list_member_tab LIMIT 500;
5282 								Else
5283 									Fetch C_RL_RLM_RBS_NON_ACT BULK COLLECT INTO resource_list_member_tab LIMIT 500;
5284 								End If;
5285 							End If;
5286 						End If;
5287 						If NOT resource_list_member_tab.exists(1) Then
5288                            EXIT;
5289                         End If;
5290 						FOR i IN resource_list_member_tab.first..resource_list_member_tab.last
5291 						LOOP
5292 							IF P_DEBUG_MODE = 'Y' THEN
5293 								pa_debug.write_file('populate_RLM_table:'||'LOG','Inserting member-'|| resource_list_member_tab(i));
5294 	                        END IF;
5295 							If Not Is_Excluded_Rlm (resource_list_member_tab(i)) Then /* Added For 3567201 */
5296 								insert_alloc_run_resources(p_run_id             => p_run_id
5297 									                      ,p_rule_id            => p_rule_id
5298 										                  ,p_member_type        => p_type
5299 											              ,p_res_list_member_id => resource_list_member_tab(i)
5300 												          ,p_resource_percent   => 100 /* nvl(RLM_REC.resource_percentage,100) */
5301 														  /*
5302 														    Bug 3741132 : When only excludes are defined. Then all other resources should
5303 														    be considered with 100 percentage instead of exclude's percentage
5304 														  */
5305 														  );
5306 							End If; /* Added For 3567201 */
5307                         END LOOP;
5308                       END LOOP;
5309 					  If NVL(p_resource_struct_type,'RL') = 'RL' Then
5310 					    CLOSE C_RL_RLM;
5311 					  ElsIf NVL(p_resource_struct_type,'RL') = 'RBS' Then
5312 						If P_Type = 'S' Then
5313 							CLOSE C_RL_RLM_RBS_ACT;
5314 						Elsif P_type = 'B' Then
5315 							If p_basis_category = 'A' Then
5316 								CLOSE C_RL_RLM_RBS_ACT;
5317 							Else
5318 								CLOSE C_RL_RLM_RBS_NON_ACT;
5319 							End If;
5320 						End If;
5321 					  End IF;
5322 				End if;		-- Added in version 115.69.40.3.
5323 /***********************   Bug 3149853 ends  *****************************/
5324 			v_excld_exists := 'Y';
5325 	   /*
5326 	    All the RLMs are arranged as includes first followed by excludes
5327 		hence, once we see an exclude, we will not encounter any more
5328 		includes. The above insert inserts all members which can be
5329 		included. Hence there is no need to process further excludes.
5330 		Hence, for every exclude except the first, we do not insert.
5331 	   */
5332 	   End if; /* v_incld_exists='N' 2564418 changes end here*/
5333         End if ;
5334      END LOOP ;
5335  End If ;
5336 END populate_RLM_table ;
5337 -- ==========================================================================
5338 /* PROCEDURE :   get_amttype_start_date
5339    Purpose   :  To get the start date of the given amount type ( FYTD/QTD)
5340                 based on a given period type( PA/GL)
5341      Created :   30-JUL-98   Sesivara
5342 */
5343 -- ==========================================================================
5344 PROCEDURE get_amttype_start_date( p_amt_type                  IN  VARCHAR2,
5345                                   p_period_type               IN  VARCHAR2 ,
5346                                   p_period_set_name           IN  VARCHAR2 ,
5347                                   p_run_period_end_date       IN  DATE,
5348                                   p_quarter_num               IN  NUMBER,
5349                                   p_period_year               IN  NUMBER,
5350                                   p_period                    IN  VARCHAR2 ,
5351                                   x_start_date                OUT NOCOPY DATE    )
5352 IS
5353    v_quarter_num   NUMBER ;
5354    cursor c_amttype_start_date is
5355       select  min (start_date)
5356         from  gl_periods glp
5357        where  glp.period_set_name =  p_period_set_name
5358          and  glp.period_type     =  p_period_type
5359          and  glp.end_date       <=  p_run_period_end_date
5360          and  glp.period_year     =  p_period_year
5361          and  glp.quarter_num     =  nvl(v_quarter_num, glp.quarter_num);
5362    cursor c_period_start_date is
5363       select  start_date
5364         from  gl_periods glp
5365        where  glp.period_set_name =  p_period_set_name
5366          and  glp.period_type     =  p_period_type
5367          and  glp.period_name     =  p_period ;
5368 BEGIN
5369     If p_amt_type in ('FYTD' ,'QTD' ) then
5370          IF p_amt_type = 'FYTD' then
5371             v_quarter_num := NULL ;
5372          ELSE
5373             v_quarter_num := p_quarter_num ;
5374          END IF ;
5375          OPEN  c_amttype_start_date ;
5376          FETCH c_amttype_start_date INTO x_start_date ;
5377          IF c_amttype_start_date%NOTFOUND THEN
5378            x_start_date := NULL ;
5379               alloc_errors(G_rule_id, G_alloc_run_id, 'R', 'E','PA_AL_NO_AMT_TYPE_START_DATE',TRUE) ;
5380          END IF ;
5381          CLOSE c_amttype_start_date ;
5382     elsif p_amt_type ='PTD' then
5383          OPEN c_period_start_date ;
5384          FETCH c_period_start_date into x_start_date ;
5385          IF c_period_start_date%NOTFOUND THEN
5386            x_start_date := NULL ;
5387            alloc_errors(G_rule_id, G_alloc_run_id, 'R', 'E','PA_AL_NO_AMT_TYPE_START_DATE',TRUE) ;
5388          END IF ;
5389     else
5390            x_start_date := NULL ;
5391     end if ;
5392 EXCEPTION
5393       WHEN OTHERS THEN
5394       RAISE ;
5395 END get_amttype_start_date  ;
5396 -- ==========================================================================
5397 /* PROCEDURE :  insert_alloc_basis_resource
5398    Purpose   :  To insert data into pa_alloc_run_basis_det table for each resource
5399                 for each task which has some data available in summarization.
5400                 Separate inserts are written for each type of amt_type
5401                 (FYTD,qtd,itd and ptd).
5402    Created :    16-JAN-02   Manokuma
5403    Modified:	 24-JAN-03   Tarun   for bug 2757875
5404 */
5405 -- ==========================================================================
5406 PROCEDURE insert_alloc_basis_resource(
5407                             p_run_id          IN NUMBER,
5408                             p_rule_id         IN NUMBER,
5409                             p_resource_list_id IN NUMBER,
5410                             p_amt_type        IN VARCHAR2,
5411                             p_bal_type        IN VARCHAR2,
5412                             p_run_period_type IN VARCHAR2,
5413                             p_period          IN VARCHAR2,
5414                             p_run_period_end_date IN DATE ,
5415                             p_amttype_start_date  IN DATE ,
5416 							/* FP.M : Allocation Impact */
5417 							p_resource_struct_type in Varchar2,
5418 							p_rbs_version_id In Varchar2
5419                             )
5420 IS
5421      cursor c_projects is
5422      select distinct part.project_id project_id
5423        from pa_alloc_run_targets part,
5424             pa_resource_list_assignments prla
5425       where part.project_id = prla.project_id
5426         and prla.resource_list_id = p_resource_list_id
5427         and prla.resource_list_accumulated_flag = 'Y'
5428         and part.run_id = p_run_id
5429 		and Nvl(p_resource_struct_type,'RL') = 'RL'
5430 	Union All
5431 	 select distinct part.project_id project_id
5432        from pa_alloc_run_targets part,
5433             pa_rbs_prj_assignments prpa
5434       where part.project_id = prpa.project_id
5435         and prpa.rbs_header_id = p_resource_list_id
5436 		and prpa.rbs_version_id = p_rbs_version_id
5437         and part.run_id = p_run_id
5438 		and Nvl(p_resource_struct_type,'RL') = 'RBS'
5439 		;
5440 /****cursor c_proj_start_date(p_proj_id IN NUMBER) is
5441      select start_date
5442        from pa_projects
5443       where project_id = p_proj_id;
5444      v_project_start_date  PA_PROJECTS.START_DATE%TYPE ; Commented for bug 2757875 ****/
5445      v_commit_count        NUMBER;
5446 BEGIN
5447 --    project and task amount are inserted based on the amount type (FTYD/QTD/PTD/ITD)
5448 --    and run period and run period type
5449      pa_debug.G_err_stage:= 'INSIDE INSERT_ALLOC_BASIS_RESOURCE procedure';
5450      IF P_DEBUG_MODE = 'Y' THEN
5451         pa_debug.write_file('insert_alloc_basis_resource: ' ||  'LOG', pa_debug.G_err_stage);
5452      END IF;
5453      If p_amt_type in ( 'FYTD', 'QTD') then
5454        pa_debug.G_err_stage:= 'inserting for FYTD or QTD';
5455        IF P_DEBUG_MODE = 'Y' THEN
5456           pa_debug.write_file('insert_alloc_basis_resource: ' ||  'LOG', pa_debug.G_err_stage);
5457        END IF;
5458        v_commit_count := 0;
5459        FOR c_projects_rec in c_projects LOOP
5460 	   IF P_DEBUG_MODE = 'Y' THEN
5461             pa_debug.write_file('insert_alloc_basis_resource: ' ||  'LOG', 'inserting data for project ' || c_projects_rec.project_id );
5462            END IF;
5463 		   If Nvl(p_resource_struct_type,'RL') = 'RL' Then	-------------- {
5464 				   INSERT INTO PA_ALLOC_RUN_BASIS_DET (
5465 					  RUN_ID
5466 					, RULE_ID
5467 					, LINE_NUM
5468 					, PROJECT_ID
5469 					, TASK_ID
5470 					, RESOURCE_LIST_MEMBER_ID
5471 					, AMOUNT
5472 					, LINE_PERCENT
5473 					, CREATION_DATE
5474 					, CREATED_BY
5475 					, LAST_UPDATE_DATE
5476 					, LAST_UPDATED_BY
5477 					, LAST_UPDATE_LOGIN )
5478 				   (  select --/*+ ORDERED INDEX (prad, PA_RESOURCE_ACCUM_DETAILS_N2) */ --Bug Fix: 3634912 added hint Bug 9054701 removed hint
5479 						part.run_id
5480 						,part.rule_id
5481 						,part.line_num
5482 						,part.project_id
5483 						,part.task_id
5484 						,parr.resource_list_member_id
5485 						,NVL(sum( decode (p_bal_type,
5486 							'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
5487 							'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
5488 													 +nvl( pta.i_tot_billable_raw_cost,0),
5489 							'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
5490 													 +nvl( pta.i_tot_billable_burdened_cost,0),
5491 							'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
5492 													 +nvl(pta.i_tot_burdened_cost,0),
5493 							'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
5494 													 +nvl(pta.i_tot_labor_hours,0),
5495 							'TOT_QUANTITY', nvl(pta.tot_quantity,0)
5496 													 +nvl(pta.i_tot_quantity,0),
5497 							0
5498 						  )),0) AMOUNT
5499 						,part.line_percent
5500 						,G_creation_date
5501 						,G_created_by
5502 						,G_last_update_date
5503 						,G_last_updated_by
5504 						,G_last_update_login
5505 					 from   ----Bug Fix: 3634912 :Changed the order of the tables
5506 						pa_alloc_run_targets part,
5507 						pa_alloc_run_resource_det parr,
5508 						pa_resource_accum_details prad,
5509 						pa_txn_accum  pta
5510 					where   pta.txn_accum_id              = prad.txn_accum_id
5511 					   and  prad.Resource_list_member_id  = parr.resource_list_member_id
5512 					   and  prad.Project_id               = part.project_id
5513 					   and  prad.task_id                  = part.task_id
5514 					   and  part.run_id                   = p_run_id
5515 					   and  parr.run_id                   = part.run_id
5516 					   and  parr.member_type              = 'B'
5517 					   and  part.project_id               = c_projects_rec.project_id
5518 					   and  part.exclude_flag             = 'N'
5519 					   and  exists
5520 		 /* Using gl_period_statuses instead of pa_periods for Bug 2757875 */
5521 					  (select /*+ NO_UNNEST */  -- Bug Fix: 3634912 added hint
5522 						  gl.period_name
5523 					   from   gl_period_statuses gl,
5524 						  pa_implementations imp
5525 					   where  pta.gl_period	    = gl.period_name
5526 					   and    gl.set_of_books_id = imp.set_of_books_id
5527 					   and    gl.application_id  = pa_period_process_pkg.application_id
5528 					   and    gl.adjustment_period_flag = 'N'
5529 					   and    gl.closing_status in ('C','F','O','P')
5530 					   and    gl.end_date	between  p_amttype_start_date
5531 								and      p_run_period_end_date)
5532 		/****                     (select 1
5533 					   from pa_periods pp
5534 					  where  pta.pa_period        = pp.period_name
5535 						and  pp.end_date between p_amttype_start_date
5536 						and  p_run_period_end_date ) **** Commented for Bug 2757875 ****/
5537 					 group by part.run_id
5538 						,part.rule_id
5539 						,part.line_num
5540 						,part.project_id
5541 						,part.task_id
5542 						,parr.resource_list_member_id
5543 						,part.line_percent
5544 						,G_creation_date
5545 						,G_created_by
5546 						,G_last_update_date
5547 						,G_last_updated_by
5548 						,G_last_update_login);
5549 			ElsIf Nvl(p_resource_struct_type,'RL') = 'RBS' Then
5550 					 INSERT INTO PA_ALLOC_RUN_BASIS_DET (
5551 						  RUN_ID
5552 						, RULE_ID
5553 						, LINE_NUM
5554 						, PROJECT_ID
5555 						, TASK_ID
5556 						, RESOURCE_LIST_MEMBER_ID
5557 						, AMOUNT
5558 						, LINE_PERCENT
5559 						, CREATION_DATE
5560 						, CREATED_BY
5561 						, LAST_UPDATE_DATE
5562 						, LAST_UPDATED_BY
5563 						, LAST_UPDATE_LOGIN )
5564 					   (  select
5565 					         part.run_id
5566 						,part.rule_id
5567 						,part.line_num
5568 						,part.project_id
5569 						,part.task_id
5570 						,parr.resource_list_member_id
5571 						,NVL(sum( decode (p_bal_type,
5572 						'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
5573 						'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
5574 												 +nvl( pta.i_tot_billable_raw_cost,0),
5575 						'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
5576 												 +nvl( pta.i_tot_billable_burdened_cost,0),
5577 						'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
5578 												 +nvl(pta.i_tot_burdened_cost,0),
5579 						'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
5580 												 +nvl(pta.i_tot_labor_hours,0),
5581 						'TOT_QUANTITY', nvl(pta.tot_quantity,0)
5582 													 +nvl(pta.i_tot_quantity,0),
5583 							0
5584 						  )),0) AMOUNT
5585 						,part.line_percent
5586 						,G_creation_date
5587 						,G_created_by
5588 						,G_last_update_date
5589 						,G_last_updated_by
5590 						,G_last_update_login
5591 					       from   pa_alloc_run_resource_det parr,
5592 							pa_alloc_run_targets part,
5593 							PA_ALLOC_TXN_ACCUM_RBS_V pta
5594 						where   pta.Rbs_Element_Id		= parr.resource_list_member_id
5595 						   and  pta.Project_id          = part.project_id
5596 						   and  pta.task_id             = part.task_id
5597 						   and  pta.RBS_STRUCT_VER_ID   = p_rbs_version_id
5598 						   and  part.run_id             = p_run_id
5599 						   and  parr.run_id             = part.run_id
5600 						   and  parr.member_type        = 'B'
5601 						   and  part.project_id         = c_projects_rec.project_id
5602 						   and  part.exclude_flag       = 'N'
5603 						   and  exists
5604 		 					(   select gl.period_name
5605 							      from gl_period_statuses gl,
5606 									   pa_implementations imp
5607 							     where pta.gl_period	    = gl.period_name
5608 								   and gl.set_of_books_id = imp.set_of_books_id
5609 								   and gl.application_id  = pa_period_process_pkg.application_id
5610 								   and gl.adjustment_period_flag = 'N'
5611 								   and gl.closing_status in ('C','F','O','P')
5612 								   and gl.end_date between p_amttype_start_date
5613 													   and p_run_period_end_date
5614 							)
5615 						 group by part.run_id
5616 							,part.rule_id
5617 							,part.line_num
5618 							,part.project_id
5619 							,part.task_id
5620 							,parr.resource_list_member_id
5621 							,part.line_percent
5622 							,G_creation_date
5623 							,G_created_by
5624 							,G_last_update_date
5625 							,G_last_updated_by
5626 							,G_last_update_login);
5627 			End If;		----------------- }
5628                v_commit_count := v_commit_count + sql%rowcount;
5629                 IF v_commit_count > 5000 then
5630                    IF P_DEBUG_MODE = 'Y' THEN
5631                       pa_debug.write_file('insert_alloc_basis_resource: ' || 'LOG','commiting the changes after 5000 records');
5632                    END IF;
5633                    COMMIT;
5634                    v_commit_count := 0;
5635                 END IF;
5636         END LOOP;
5637         COMMIT;
5638 	 Elsif  p_amt_type = 'PTD' then
5639        pa_debug.G_err_stage:= 'inserting for PTD';
5640        IF P_DEBUG_MODE = 'Y' THEN
5641           pa_debug.write_file('insert_alloc_basis_resource: ' ||  'LOG', pa_debug.G_err_stage);
5642        END IF;
5643 	   v_commit_count := 0;
5644 	   FOR c_projects_rec in c_projects LOOP
5645 			   IF p_run_period_type = 'PA' THEN
5646                     IF P_DEBUG_MODE = 'Y' THEN
5647                        pa_debug.write_file('insert_alloc_basis_resource: ' ||  'LOG', 'inserting data for project ' || c_projects_rec.project_id);
5648                     END IF;
5649 			If Nvl(p_resource_struct_type,'RL') = 'RL' Then		-------------{
5650 					INSERT INTO PA_ALLOC_RUN_BASIS_DET  (
5651 					   RUN_ID
5652 					 , RULE_ID
5653 					 , LINE_NUM
5654 					 , PROJECT_ID
5655 					 , TASK_ID
5656 					 , RESOURCE_LIST_MEMBER_ID
5657 					 , AMOUNT
5658 					 , LINE_PERCENT
5659 					 , CREATION_DATE
5660 					 , CREATED_BY
5661 					 , LAST_UPDATE_DATE
5662 					 , LAST_UPDATED_BY
5663 					 , LAST_UPDATE_LOGIN )
5664 					(  select --/*+ ORDERED INDEX (prad, PA_RESOURCE_ACCUM_DETAILS_N2) */  --Bug Fix: 3634912 added hint Bug 9054701 removed hint
5665 					          part.run_id
5666 						 ,part.rule_id
5667 						 ,part.line_num
5668 						 ,part.project_id
5669 						 ,part.task_id
5670 						 ,parr.resource_list_member_id
5671 						 ,NVL(sum( decode (p_bal_type,
5672 						 'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
5673 						 'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
5674 												 +nvl( pta.i_tot_billable_raw_cost,0),
5675 						 'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
5676 													  +nvl( pta.i_tot_billable_burdened_cost,0),
5677 						 'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
5678 											  +nvl(pta.i_tot_burdened_cost,0),
5679 						 'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
5680 											+nvl(pta.i_tot_labor_hours,0),
5681 						 'TOT_QUANTITY', nvl(pta.tot_quantity,0)
5682 										+nvl(pta.i_tot_quantity,0),
5683 						  0)),0)AMOUNT
5684 						 ,part.line_percent
5685 						 ,G_creation_date
5686 						 ,G_created_by
5687 						 ,G_last_update_date
5688 						 ,G_last_updated_by
5689 						 ,G_last_update_login
5690 					 from --Bug Fix: 3634912 : Changed the order of the tables.
5691 		                                pa_alloc_run_targets part,
5692 				                pa_alloc_run_resource_det parr,
5693 						pa_resource_accum_details prad,
5694 						pa_txn_accum  pta
5695 					  where  pta.txn_accum_id              = prad.txn_accum_id
5696 						and  prad.Resource_list_member_id  = parr.resource_list_member_id
5697 						and  pta.Project_id                = part.project_id
5698 						and  pta.task_id                   = part.task_id
5699 						and  part.run_id                   = p_run_id
5700 						and  parr.run_id                   = part.run_id
5701 						and  parr.member_type              = 'B'
5702 						and  part.project_id               = c_projects_rec.project_id
5703 						and  part.exclude_flag             = 'N'
5704 						and  pta.pa_period = p_period
5705 					  group by part.run_id
5706 						 ,part.rule_id
5707 						 ,part.line_num
5708 						 ,part.project_id
5709 						 ,part.task_id
5710 						 ,parr.resource_list_member_id
5711 						 ,part.line_percent
5712 						 ,G_creation_date
5713 						 ,G_created_by
5714 						 ,G_last_update_date
5715 						 ,G_last_updated_by
5716 						 ,G_last_update_login);
5717 			ElsIf Nvl(p_resource_struct_type,'RL') = 'RBS' Then
5718 					INSERT INTO PA_ALLOC_RUN_BASIS_DET  (
5719 						   RUN_ID
5720 						 , RULE_ID
5721 						 , LINE_NUM
5722 						 , PROJECT_ID
5723 						 , TASK_ID
5724 						 , RESOURCE_LIST_MEMBER_ID
5725 						 , AMOUNT
5726 						 , LINE_PERCENT
5727 						 , CREATION_DATE
5728 						 , CREATED_BY
5729 						 , LAST_UPDATE_DATE
5730 						 , LAST_UPDATED_BY
5731 						 , LAST_UPDATE_LOGIN )
5732 						(  select part.run_id
5733 							 ,part.rule_id
5734 							 ,part.line_num
5735 							 ,part.project_id
5736 							 ,part.task_id
5737 							 ,parr.resource_list_member_id
5738 							 ,NVL(sum( decode (p_bal_type,
5739 							 'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
5740 							 'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
5741 													 +nvl( pta.i_tot_billable_raw_cost,0),
5742 							 'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
5743 														  +nvl( pta.i_tot_billable_burdened_cost,0),
5744 							 'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
5745 												  +nvl(pta.i_tot_burdened_cost,0),
5746 							 'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
5747 												+nvl(pta.i_tot_labor_hours,0),
5748 							 'TOT_QUANTITY', nvl(pta.tot_quantity,0)
5749 											+nvl(pta.i_tot_quantity,0),
5750 							  0)),0)AMOUNT
5751 							 ,part.line_percent
5752 							 ,G_creation_date
5753 							 ,G_created_by
5754 							 ,G_last_update_date
5755 							 ,G_last_updated_by
5756 							 ,G_last_update_login
5757 						 from    pa_alloc_run_resource_det parr,
5758 							 pa_alloc_run_targets part,
5759 							 PA_ALLOC_TXN_ACCUM_RBS_V  pta
5760 						  where  pta.Rbs_Element_ID  = parr.resource_list_member_id
5761 							and  pta.Project_id                = part.project_id
5762 							and  pta.task_id                   = part.task_id
5763 							and  pta.RBS_STRUCT_VER_ID		   = p_rbs_version_id
5764 							and  part.run_id                   = p_run_id
5765 							and  parr.run_id                   = part.run_id
5766 							and  parr.member_type              = 'B'
5767 							and  part.project_id               = c_projects_rec.project_id
5768 							and  part.exclude_flag             = 'N'
5769 							and  pta.pa_period = p_period
5770 						  group by part.run_id
5771 							 ,part.rule_id
5772 							 ,part.line_num
5773 							 ,part.project_id
5774 							 ,part.task_id
5775 							 ,parr.resource_list_member_id
5776 							 ,part.line_percent
5777 							 ,G_creation_date
5778 							 ,G_created_by
5779 							 ,G_last_update_date
5780 							 ,G_last_updated_by
5781 							 ,G_last_update_login);
5782 			End If;		---------------- }
5783               ELSE /* p_run_period_type = 'GL' */
5784 			If Nvl(p_resource_struct_type,'RL') = 'RL' Then		--------------{
5785 					INSERT INTO PA_ALLOC_RUN_BASIS_DET  (
5786 					   RUN_ID
5787 					 , RULE_ID
5788 					 , LINE_NUM
5789 					 , PROJECT_ID
5790 					 , TASK_ID
5791 					 , RESOURCE_LIST_MEMBER_ID
5792 					 , AMOUNT
5793 					 , LINE_PERCENT
5794 					 , CREATION_DATE
5795 					 , CREATED_BY
5796 					 , LAST_UPDATE_DATE
5797 					 , LAST_UPDATED_BY
5798 					 , LAST_UPDATE_LOGIN )
5799 					(  select --/*+ ORDERED INDEX (prad, PA_RESOURCE_ACCUM_DETAILS_N2) */  --Bug Fix: 3634912 added hint Bug 9054701 removed hint
5800 					          part.run_id
5801 						 ,part.rule_id
5802 						 ,part.line_num
5803 						 ,part.project_id
5804 						 ,part.task_id
5805 						 ,parr.resource_list_member_id
5806 						 ,NVL(sum( decode (p_bal_type,
5807 						 'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
5808 						 'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
5809 												 +nvl( pta.i_tot_billable_raw_cost,0),
5810 						 'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
5811 													  +nvl( pta.i_tot_billable_burdened_cost,0),
5812 						 'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
5813 											  +nvl(pta.i_tot_burdened_cost,0),
5814 						 'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
5815 											+nvl(pta.i_tot_labor_hours,0),
5816 						 'TOT_QUANTITY', nvl(pta.tot_quantity,0)
5817 										+nvl(pta.i_tot_quantity,0),
5818 						  0)),0)AMOUNT
5819 						 ,part.line_percent
5820 						 ,G_creation_date
5821 						 ,G_created_by
5822 						 ,G_last_update_date
5823 						 ,G_last_updated_by
5824 						 ,G_last_update_login
5825 				         from    --Bug Fix: 3634912 : Changed the order of tables
5826 						 pa_alloc_run_targets part,
5827 					         pa_alloc_run_resource_det parr,
5828 						 pa_resource_accum_details prad,
5829 						 pa_txn_accum  pta
5830 					  where  pta.txn_accum_id              = prad.txn_accum_id
5831 						and  prad.Resource_list_member_id  = parr.resource_list_member_id
5832 						and  pta.Project_id                = part.project_id
5833 						and  pta.task_id                   = part.task_id
5834 						and  part.run_id                   = p_run_id
5835 						and  parr.run_id                   = part.run_id
5836 						and  parr.member_type              = 'B'
5837 						and  part.project_id               = c_projects_rec.project_id
5838 						and  part.exclude_flag             = 'N'
5839 						and  pta.gl_period		   = p_period  /*Using gl_period on pta directly for bug 2757875 */
5840 			/****                   and  pta.pa_period IN
5841 							 (SELECT period_name
5842 								FROM pa_periods pp
5843 							   WHERE pp.gl_period_name = p_period) **** Commented for bug 2757875 ****/
5844 					  group by part.run_id
5845 						 ,part.rule_id
5846 						 ,part.line_num
5847 						 ,part.project_id
5848 						 ,part.task_id
5849 						 ,parr.resource_list_member_id
5850 						 ,part.line_percent
5851 						 ,G_creation_date
5852 						 ,G_created_by
5853 						 ,G_last_update_date
5854 						 ,G_last_updated_by
5855 						 ,G_last_update_login);
5856 				ElsIf Nvl(p_resource_struct_type,'RL') = 'RBS' Then
5857 					INSERT INTO PA_ALLOC_RUN_BASIS_DET  (
5858 					   RUN_ID
5859 					 , RULE_ID
5860 					 , LINE_NUM
5861 					 , PROJECT_ID
5862 					 , TASK_ID
5863 					 , RESOURCE_LIST_MEMBER_ID
5864 					 , AMOUNT
5865 					 , LINE_PERCENT
5866 					 , CREATION_DATE
5867 					 , CREATED_BY
5868 					 , LAST_UPDATE_DATE
5869 					 , LAST_UPDATED_BY
5870 					 , LAST_UPDATE_LOGIN )
5871 					(  select part.run_id
5872 						 ,part.rule_id
5873 						 ,part.line_num
5874 						 ,part.project_id
5875 						 ,part.task_id
5876 						 ,parr.resource_list_member_id
5877 						 ,NVL(sum( decode (p_bal_type,
5878 						 'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
5879 						 'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
5880 												 +nvl( pta.i_tot_billable_raw_cost,0),
5881 						 'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
5882 												  +nvl( pta.i_tot_billable_burdened_cost,0),
5883 						 'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
5884 											  +nvl(pta.i_tot_burdened_cost,0),
5885 						 'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
5886 										+nvl(pta.i_tot_labor_hours,0),
5887 						 'TOT_QUANTITY', nvl(pta.tot_quantity,0)
5888 										+nvl(pta.i_tot_quantity,0),
5889 						  0)),0)AMOUNT
5890 						 ,part.line_percent
5891 						 ,G_creation_date
5892 						 ,G_created_by
5893 						 ,G_last_update_date
5894 						 ,G_last_updated_by
5895 						 ,G_last_update_login
5896 					 from    pa_alloc_run_resource_det parr,
5897 						 pa_alloc_run_targets part,
5898 						 PA_ALLOC_TXN_ACCUM_RBS_V  pta
5899 					  where  pta.Rbs_Element_ID		  = parr.resource_list_member_id
5900 						and  pta.Project_id                = part.project_id
5901 						and  pta.task_id                   = part.task_id
5902 						and  pta.RBS_STRUCT_VER_ID   = p_rbs_version_id
5903 						and  part.run_id                   = p_run_id
5904 						and  parr.run_id                   = part.run_id
5905 						and  parr.member_type              = 'B'
5906 						and  part.project_id               = c_projects_rec.project_id
5907 						and  part.exclude_flag             = 'N'
5908 						and  pta.gl_period				   = p_period
5909 					  group by part.run_id
5910 						 ,part.rule_id
5911 						 ,part.line_num
5912 						 ,part.project_id
5913 						 ,part.task_id
5914 						 ,parr.resource_list_member_id
5915 						 ,part.line_percent
5916 						 ,G_creation_date
5917 						 ,G_created_by
5918 						 ,G_last_update_date
5919 						 ,G_last_updated_by
5920 						 ,G_last_update_login);
5921 			End If;		--------------- }
5922                END IF;
5923                v_commit_count := v_commit_count + sql%rowcount;
5924                 IF v_commit_count > 5000 then
5925                    IF P_DEBUG_MODE = 'Y' THEN
5926                       pa_debug.write_file('insert_alloc_basis_resource: ' || 'LOG','commiting the changes after 5000 records');
5927                    END IF;
5928                    COMMIT;
5929                    v_commit_count := 0;
5930                 END IF;
5931        END LOOP;
5932        COMMIT;
5933     Elsif p_amt_type = 'ITD' then
5934        pa_debug.G_err_stage:= 'inserting for ITD';
5935        IF P_DEBUG_MODE = 'Y' THEN
5936           pa_debug.write_file('insert_alloc_basis_resource: ' ||  'LOG', pa_debug.G_err_stage);
5937        END IF;
5938        v_commit_count := 0;
5939        FOR c_projects_rec in c_projects LOOP
5940 /****      OPEN C_proj_start_date(c_projects_rec.project_id) ;
5941            FETCH C_proj_start_date into v_project_start_date ;
5942            If C_proj_start_date%NOTFOUND  then
5943                v_project_start_date := NULL ;
5944            End if ;
5945            CLOSE C_proj_start_date ;
5946                IF v_project_start_date is NOT NULL then **** Commented for bug 2757875 ****/
5947                IF P_DEBUG_MODE = 'Y' THEN
5948                   pa_debug.write_file('insert_alloc_basis_resource: ' ||  'LOG', 'inserting data for project ' || c_projects_rec.project_id);
5949                END IF;
5950 		If Nvl(p_resource_struct_type,'RL') = 'RL' Then		---------------{
5951                        INSERT INTO PA_ALLOC_RUN_BASIS_DET (
5952                           RUN_ID
5953                         , RULE_ID
5954                         , LINE_NUM
5955                         , PROJECT_ID
5956                         , TASK_ID
5957                         , RESOURCE_LIST_MEMBER_ID
5958                         , AMOUNT
5959                         , LINE_PERCENT
5960                         , CREATION_DATE
5961                         , CREATED_BY
5962                         , LAST_UPDATE_DATE
5963                         , LAST_UPDATED_BY
5964                         , LAST_UPDATE_LOGIN )
5965                        (  select --/*+ ORDERED INDEX (prad, PA_RESOURCE_ACCUM_DETAILS_N2) */ -- Bug Fix: 3634912 added hint Bug 9054701 removed hint
5966 		                 part.run_id
5967                                 ,part.rule_id
5968                                 ,part.line_num
5969                                 ,part.project_id
5970                                 ,part.task_id
5971                                 ,parr.resource_list_member_id
5972                                 ,NVL(sum( decode (p_bal_type,
5973                                     'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
5974                                     'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
5975                                                              +nvl( pta.i_tot_billable_raw_cost,0),
5976                                     'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
5977                                                              +nvl( pta.i_tot_billable_burdened_cost,0),
5978                                     'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
5979                                                              +nvl(pta.i_tot_burdened_cost,0),
5980                                     'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
5981                                                              +nvl(pta.i_tot_labor_hours,0),
5982                                     'TOT_QUANTITY', nvl(pta.tot_quantity,0)
5983                                                              +nvl(pta.i_tot_quantity,0),
5984                                     0
5985                                   )),0) AMOUNT
5986                                 ,part.line_percent
5987                                 ,G_creation_date
5988                                 ,G_created_by
5989                                 ,G_last_update_date
5990                                 ,G_last_updated_by
5991                                 ,G_last_update_login
5992                         from    -- Bug Fix: 3634912 : Changed the order of tables.
5993                                 pa_alloc_run_targets part,
5994 				pa_alloc_run_resource_det parr,
5995                                 pa_resource_accum_details prad,
5996                                 pa_txn_accum  pta
5997                          where  pta.txn_accum_id              = prad.txn_accum_id
5998                            and  prad.Resource_list_member_id  = parr.resource_list_member_id
5999                            and  prad.Project_id               = part.project_id
6000                            and  prad.task_id                  = part.task_id
6001                            and  part.run_id                   = p_run_id
6002                            and  parr.run_id                   = part.run_id
6003                            and  parr.member_type              = 'B'
6004                            and  part.project_id               = c_projects_rec.project_id
6005                            and  part.exclude_flag             = 'N'
6006                            and  exists
6007                                 (select  /*+ NO_UNNEST */  -- Bug Fix: 3634912 added hint
6008 				       1
6009                                   from pa_periods pp
6010                                  where  pta.pa_period = pp.period_name
6011 				   and  pp.end_date  <= p_run_period_end_date) /* Added for bug 2757875 */
6012 			/****    and  pp.end_date   between v_project_start_date
6013 						  and  p_run_period_end_date ) ****  Commented for bug 2757875 ****/
6014                          group by part.run_id
6015                                 ,part.rule_id
6016                                 ,part.line_num
6017                                 ,part.project_id
6018                                 ,part.task_id
6019                                 ,parr.resource_list_member_id
6020                                 ,part.line_percent
6021                                 ,G_creation_date
6022                                 ,G_created_by
6023                                 ,G_last_update_date
6024                                 ,G_last_updated_by
6025                                 ,G_last_update_login);
6026 		ElsIf Nvl(p_resource_struct_type,'RL') = 'RBS' Then
6027 			INSERT INTO PA_ALLOC_RUN_BASIS_DET (
6028                           RUN_ID
6029                         , RULE_ID
6030                         , LINE_NUM
6031                         , PROJECT_ID
6032                         , TASK_ID
6033                         , RESOURCE_LIST_MEMBER_ID
6034                         , AMOUNT
6035                         , LINE_PERCENT
6036                         , CREATION_DATE
6037                         , CREATED_BY
6038                         , LAST_UPDATE_DATE
6039                         , LAST_UPDATED_BY
6040                         , LAST_UPDATE_LOGIN )
6041                        (  select part.run_id
6042                                 ,part.rule_id
6043                                 ,part.line_num
6044                                 ,part.project_id
6045                                 ,part.task_id
6046                                 ,parr.resource_list_member_id
6047                                 ,NVL(sum( decode (p_bal_type,
6048                                     'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
6049                                     'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6050                                                              +nvl( pta.i_tot_billable_raw_cost,0),
6051                                     'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6052                                                              +nvl( pta.i_tot_billable_burdened_cost,0),
6053                                     'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6054                                                              +nvl(pta.i_tot_burdened_cost,0),
6055                                     'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6056                                                              +nvl(pta.i_tot_labor_hours,0),
6057                                     'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6058                                                              +nvl(pta.i_tot_quantity,0),
6059                                     0
6060                                   )),0) AMOUNT
6061                                 ,part.line_percent
6062                                 ,G_creation_date
6063                                 ,G_created_by
6064                                 ,G_last_update_date
6065                                 ,G_last_updated_by
6066                                 ,G_last_update_login
6067                         from    pa_alloc_run_resource_det parr,
6068                                 pa_alloc_run_targets part,
6069                                 PA_ALLOC_TXN_ACCUM_RBS_V  pta
6070                          where  pta.Rbs_Element_Id           = parr.resource_list_member_id
6071                            and  pta.Project_id                = part.project_id
6072                            and  pta.task_id                   = part.task_id
6073 						   and  pta.RBS_STRUCT_VER_ID   = p_rbs_version_id
6074                            and  part.run_id                   = p_run_id
6075                            and  parr.run_id                   = part.run_id
6076                            and  parr.member_type              = 'B'
6077                            and  part.project_id               = c_projects_rec.project_id
6078                            and  part.exclude_flag             = 'N'
6079                            and  exists
6080                                 (
6081 				  select /*+ NO_UNNEST */  -- Bug Fix: 3634912 added hint
6082 				        1
6083                                    from pa_periods pp
6084 				  where pta.pa_period = pp.period_name
6085 				    and pp.end_date  <= p_run_period_end_date
6086 				 )
6087                          group by part.run_id
6088                                 ,part.rule_id
6089                                 ,part.line_num
6090                                 ,part.project_id
6091                                 ,part.task_id
6092                                 ,parr.resource_list_member_id
6093                                 ,part.line_percent
6094                                 ,G_creation_date
6095                                 ,G_created_by
6096                                 ,G_last_update_date
6097                                 ,G_last_updated_by
6098                                 ,G_last_update_login);
6099 		   End If;		--------------- }
6100 					 v_commit_count := v_commit_count + sql%rowcount;
6101 --             END IF;    **** Commetned for bug 2757875
6102                IF v_commit_count > 5000 then
6103                   IF P_DEBUG_MODE = 'Y' THEN
6104                      pa_debug.write_file('insert_alloc_basis_resource: ' || 'LOG','commiting the changes after 5000 records');
6105                   END IF;
6106                   COMMIT;
6107                   v_commit_count := 0;
6108                END IF;
6109        END LOOP;
6110        COMMIT;
6111     End If ;
6112     pa_debug.G_err_stage:= 'exiting insert_alloc_basis_resource';
6113     IF P_DEBUG_MODE = 'Y' THEN
6114        pa_debug.write_file('insert_alloc_basis_resource: ' ||  'LOG', pa_debug.G_err_stage);
6115     END IF;
6116 EXCEPTION
6117     WHEN OTHERS THEN
6118        RAISE ;
6119 END insert_alloc_basis_resource;
6120 -- ==========================================================================
6121 /* PROCEDURE :  insert_alloc_source_resource
6122    Purpose   :  To insert data into pa_alloc_run_source_det table for each resource
6123                 for each task which has some data available in summarization.
6124                 Separate inserts are written for each type of amt_type
6125                 (FYTD,qtd,itd and ptd).
6126    Created :    16-JAN-02   Manokuma
6127    Modified:	 24-JAN-03   Tarun    for bug 2757875
6128 */
6129 -- ==========================================================================
6130 PROCEDURE insert_alloc_source_resource(
6131                             p_run_id          IN NUMBER,
6132                             p_rule_id         IN NUMBER,
6133                             p_resource_list_id IN NUMBER,
6134                             p_amt_type        IN VARCHAR2,
6135                             p_bal_type        IN VARCHAR2,
6136                             p_run_period_type IN VARCHAR2,
6137                             p_period          IN VARCHAR2,
6138                             p_run_period_end_date IN DATE ,
6139                             p_amttype_start_date  IN DATE ,
6140 							/* FP.M : Allocation Impact */
6141 							p_resource_struct_type in Varchar2,
6142 							p_rbs_version_id in Number
6143                             )
6144 IS
6145      cursor c_projects is
6146      select distinct pars.project_id project_id
6147        from pa_alloc_run_sources pars,
6148             pa_resource_list_assignments prla
6149       where pars.project_id = prla.project_id
6150         and prla.resource_list_id = p_resource_list_id
6151         and prla.resource_list_accumulated_flag = 'Y'
6152         and pars.run_id = p_run_id
6153 		and NVL(p_resource_struct_type,'RL') = 'RL'
6154 	UNION All
6155 	 select distinct pars.project_id project_id
6156        from pa_alloc_run_sources pars,
6157             pa_rbs_prj_assignments prpa
6158       where pars.project_id = prpa.project_id
6159         and prpa.rbs_header_id = p_resource_list_id
6160 		and prpa.rbs_version_id = p_rbs_version_id
6161         and pars.run_id = p_run_id
6162 		and NVL(p_resource_struct_type,'RL') = 'RBS'
6163 		;
6164      /* Added for bug 3227783 */
6165      cursor c_get_rule_pool_percent is
6166         Select nvl(pool_percent,100)/100
6167         from   pa_alloc_rules_all
6168         where  rule_id = p_rule_id;
6169 /****cursor c_proj_start_date(p_proj_id IN NUMBER) is
6170      select start_date
6171        from pa_projects
6172       where project_id = p_proj_id;
6173      v_project_start_date  PA_PROJECTS.START_DATE%TYPE ;
6174 ****  Commented for bug 2757875 ****/
6175      v_commit_count        NUMBER;
6176      v_rule_pool_percent   NUMBER;      /* Added for bug 3227783 */
6177 BEGIN
6178 --    project and task amount are inserted based on the amount type (FTYD/QTD/PTD/ITD)
6179 --    and run period and run period type
6180      pa_debug.G_err_stage:= 'INSIDE INSERT_ALLOC_source_RESOURCE procedure';
6181      IF P_DEBUG_MODE = 'Y' THEN
6182         pa_debug.write_file('insert_alloc_source_resource: ' ||  'LOG', pa_debug.G_err_stage);
6183      END IF;
6184      pa_debug.G_err_stage:= 'getting the pool_percent set for the rule.';
6185      IF P_DEBUG_MODE = 'Y' THEN
6186         pa_debug.write_file('insert_alloc_source_resource: ' ||  'LOG', pa_debug.G_err_stage);
6187      END IF;
6188      /* Added for bug 3227783 */
6189      Open c_get_rule_pool_percent ;
6190      FETCH c_get_rule_pool_percent into v_rule_pool_percent;
6191      CLOSE c_get_rule_pool_percent;
6192      If p_amt_type in ( 'FYTD', 'QTD') then
6193 	   pa_debug.G_err_stage:= 'inserting for FYTD or QTD';
6194 	   IF P_DEBUG_MODE = 'Y' THEN
6195           pa_debug.write_file('insert_alloc_source_resource: ' ||  'LOG', pa_debug.G_err_stage);
6196        END IF;
6197        v_commit_count := 0;
6198        FOR c_projects_rec in c_projects LOOP
6199 	  IF P_DEBUG_MODE = 'Y' THEN
6200              pa_debug.write_file('insert_alloc_source_resource: ' ||  'LOG', 'inserting data for project ' || c_projects_rec.project_id );
6201           END IF;
6202 	   IF Nvl(p_resource_struct_type,'RL') = 'RL' Then		-------------------{
6203 			INSERT INTO PA_ALLOC_RUN_SOURCE_DET (
6204 				  RUN_ID
6205 				, RULE_ID
6206 				, LINE_NUM
6207 				, PROJECT_ID
6208 				, TASK_ID
6209 				, RESOURCE_LIST_MEMBER_ID
6210 				, AMOUNT
6211 				, ELIGIBLE_AMOUNT
6212 				, RESOURCE_PERCENT
6213 				, CREATION_DATE
6214 				, CREATED_BY
6215 				, LAST_UPDATE_DATE
6216 				, LAST_UPDATED_BY
6217 				, LAST_UPDATE_LOGIN )
6218 				(  select --/*+ ORDERED INDEX (prad, PA_RESOURCE_ACCUM_DETAILS_N2) */ --Bug Fix: 3634912 added hint Bug 9054701 removed hint
6219 				         pars.run_id
6220 					,pars.rule_id
6221 					,pars.line_num
6222 					,pars.project_id
6223 					,pars.task_id
6224 					,parr.resource_list_member_id
6225 					,NVL(sum( decode (p_bal_type,
6226 						'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) + nvl(pta.i_tot_raw_cost,0),
6227 						'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6228 												 + nvl(pta.i_tot_billable_raw_cost,0),
6229 						'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6230 												 + nvl(pta.i_tot_billable_burdened_cost,0),
6231 						'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6232 												 + nvl(pta.i_tot_burdened_cost,0),
6233 						'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6234 												 + nvl(pta.i_tot_labor_hours,0),
6235 						'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6236 												 + nvl(pta.i_tot_quantity,0),
6237 						0
6238 					  )),0) AMOUNT
6239 					, pa_currency.round_currency_amt(NVL(sum( decode (p_bal_type,	 --Bug 3590551:Introduced rounding
6240 						'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
6241 						'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6242 												 +nvl( pta.i_tot_billable_raw_cost,0),
6243 						'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6244 												 +nvl( pta.i_tot_billable_burdened_cost,0),
6245 						'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6246 												 +nvl(pta.i_tot_burdened_cost,0),
6247 						'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6248 												 +nvl(pta.i_tot_labor_hours,0),
6249 						'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6250 												 +nvl(pta.i_tot_quantity,0),
6251 					   0
6252 					  )),0)*(parr.resource_percent/100)*v_rule_pool_percent) ELIGIBLE_AMOUNT /* bug 3227783 */
6253 					,parr.resource_percent
6254 					,G_creation_date
6255 					,G_created_by
6256 					,G_last_update_date
6257 					,G_last_updated_by
6258 					,G_last_update_login
6259 				 from   --Bug Fix: 3634912 : Changed the order of the tables.
6260 					pa_alloc_run_sources pars,
6261 					pa_alloc_run_resource_det parr,
6262 					pa_resource_accum_details prad,
6263 					pa_txn_accum  pta
6264 				where   pta.txn_accum_id              = prad.txn_accum_id
6265 				   and  prad.Resource_list_member_id  = parr.resource_list_member_id
6266 				   and  prad.Project_id               = pars.project_id
6267 				   and  prad.task_id                  = pars.task_id
6268 				   and  pars.run_id                   = p_run_id
6269 				   and  parr.run_id                   = pars.run_id
6270 				   and  parr.member_type              = 'S'
6271 				   and  pars.project_id               = c_projects_rec.project_id
6272 				   and  pars.exclude_flag             = 'N'
6273 				   and  exists
6274 		/* Using gl_period_statuses instead of pa_periods for bug 2757875 */
6275 				(select /*+ NO_UNNEST */  -- Bug Fix: 3634912 added hint
6276 					gl.period_name
6277 				   From gl_period_statuses gl,
6278 					pa_implementations imp
6279 				  where pta.gl_period		= gl.period_name
6280 					and gl.set_of_books_id = imp.set_of_books_id
6281 					and gl.application_id  = pa_period_process_pkg.application_id
6282 					and gl.adjustment_period_flag = 'N'
6283 					and gl.closing_status in ('C','F','O','P')
6284 					and gl.end_date between p_amttype_start_date
6285 					and p_run_period_end_date
6286 					 )
6287 		/*                      (select 1
6288 					 from pa_periods pp
6289 					 where pta.pa_period = pp.period_name
6290 					 and  pp.end_date  between p_amttype_start_date
6291 					 and p_run_period_end_date) **** 2757875 */
6292 				 group by pars.run_id
6293 					,pars.rule_id
6294 					,pars.line_num
6295 					,pars.project_id
6296 					,pars.task_id
6297 					,parr.resource_list_member_id
6298 					,parr.resource_percent
6299 					,G_creation_date
6300 					,G_created_by
6301 					,G_last_update_date
6302 					,G_last_updated_by
6303 					,G_last_update_login);
6304 		ElsIf Nvl(p_resource_struct_type,'RL') = 'RBS' Then
6305 				/* FP.M : Allocation Impact */
6306 					INSERT INTO PA_ALLOC_RUN_SOURCE_DET (
6307 						  RUN_ID
6308 						, RULE_ID
6309 						, LINE_NUM
6310 						, PROJECT_ID
6311 						, TASK_ID
6312 						, RESOURCE_LIST_MEMBER_ID
6313 						, AMOUNT
6314 						, ELIGIBLE_AMOUNT
6315 						, RESOURCE_PERCENT
6316 						, CREATION_DATE
6317 						, CREATED_BY
6318 						, LAST_UPDATE_DATE
6319 						, LAST_UPDATED_BY
6320 						, LAST_UPDATE_LOGIN
6321 						)
6322 						(  select pars.run_id
6323 							,pars.rule_id
6324 							,pars.line_num
6325 							,pars.project_id
6326 							,pars.task_id
6327 							,parr.resource_list_member_id
6328 							,NVL(sum( decode (p_bal_type,
6329 								'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) + nvl(pta.i_tot_raw_cost,0),
6330 								'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6331 														 + nvl(pta.i_tot_billable_raw_cost,0),
6332 								'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6333 														 + nvl(pta.i_tot_billable_burdened_cost,0),
6334 								'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6335 														 + nvl(pta.i_tot_burdened_cost,0),
6336 								'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6337 														 + nvl(pta.i_tot_labor_hours,0),
6338 								'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6339 														 + nvl(pta.i_tot_quantity,0),
6340 								0
6341 							  )),0) AMOUNT
6342 							,pa_currency.round_currency_amt(NVL(sum( decode (p_bal_type,   --Bug 3590551:Introduced rounding
6343 								'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
6344 								'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6345 														 +nvl( pta.i_tot_billable_raw_cost,0),
6346 								'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6347 														 +nvl( pta.i_tot_billable_burdened_cost,0),
6348 								'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6349 														 +nvl(pta.i_tot_burdened_cost,0),
6350 								'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6351 														 +nvl(pta.i_tot_labor_hours,0),
6352 								'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6353 														 +nvl(pta.i_tot_quantity,0),
6354 								0
6355 							  )),0)*(parr.resource_percent/100) * v_rule_pool_percent) ELIGIBLE_AMOUNT
6356 							,parr.resource_percent
6357 							,G_creation_date
6358 							,G_created_by
6359 							,G_last_update_date
6360 							,G_last_updated_by
6361 							,G_last_update_login
6362 						 from   pa_alloc_run_resource_det parr,
6363 							pa_alloc_run_sources pars,
6364 							PA_ALLOC_TXN_ACCUM_RBS_V  pta
6365 						where   pta.Rbs_Element_Id			  = parr.resource_list_member_id
6366 						   and  pta.Project_id                = pars.project_id
6367 						   and  pta.task_id                   = pars.task_id
6368 						   and  pta.RBS_STRUCT_VER_ID   = p_rbs_version_id
6369 						   and  pars.run_id                   = p_run_id
6370 						   and  parr.run_id                   = pars.run_id
6371 						   and  parr.member_type              = 'S'
6372 						   and  pars.project_id               = C_projects_rec.project_id
6373 						   and  pars.exclude_flag             = 'N'
6374 						   and  Exists
6375 								(select gl.period_name
6376 								   From gl_period_statuses gl,
6377 										pa_implementations imp
6378 								  where pta.gl_period		= gl.period_name
6379 									and gl.set_of_books_id	= imp.set_of_books_id
6380 									and gl.application_id	= pa_period_process_pkg.application_id
6381 									and gl.adjustment_period_flag = 'N'
6382 									and	gl.closing_status in ('C','F','O','P')
6383 									and gl.end_date between p_amttype_start_date
6384 														and p_run_period_end_date
6385 								)
6386 						 group by pars.run_id
6387 								,pars.rule_id
6388 								,pars.line_num
6389 								,pars.project_id
6390 								,pars.task_id
6391 								,parr.resource_list_member_id
6392 								,parr.resource_percent
6393 								,G_creation_date
6394 								,G_created_by
6395 								,G_last_update_date
6396 								,G_last_updated_by
6397 								,G_last_update_login);
6398 			End If;		------------}
6399                 v_commit_count := v_commit_count + sql%rowcount;
6400                 IF v_commit_count > 5000 then
6401                    IF P_DEBUG_MODE = 'Y' THEN
6402                       pa_debug.write_file('insert_alloc_source_resource: ' || 'LOG','commiting the changes after 5000 records');
6403                    END IF;
6404                    COMMIT;
6405                    v_commit_count := 0;
6406                 END IF;
6407         END LOOP;
6408         COMMIT;
6409      Elsif  p_amt_type = 'PTD' then
6410 	   pa_debug.G_err_stage:= 'inserting for PTD';
6411         IF P_DEBUG_MODE = 'Y' THEN
6412            pa_debug.write_file('insert_alloc_source_resource: ' ||  'LOG', pa_debug.G_err_stage);
6413         END IF;
6414           v_commit_count := 0;
6415 	   FOR c_projects_rec in c_projects LOOP
6416 	      IF p_run_period_type = 'PA' THEN
6417                     IF P_DEBUG_MODE = 'Y' THEN
6418                        pa_debug.write_file('insert_alloc_source_resource: ' ||  'LOG', 'inserting data for project ' || c_projects_rec.project_id);
6419                     END IF;
6420 			IF Nvl(p_resource_struct_type,'RL') = 'RL' Then		------------{
6421 					INSERT INTO PA_ALLOC_RUN_source_DET  (
6422 					   RUN_ID
6423 					 , RULE_ID
6424 					 , LINE_NUM
6425 					 , PROJECT_ID
6426 					 , TASK_ID
6427 					 , RESOURCE_LIST_MEMBER_ID
6428 					 , AMOUNT
6429 					 , ELIGIBLE_AMOUNT
6430 					 , RESOURCE_PERCENT
6431 					 , CREATION_DATE
6432 					 , CREATED_BY
6433 					 , LAST_UPDATE_DATE
6434 					 , LAST_UPDATED_BY
6435 					 , LAST_UPDATE_LOGIN )
6436 				(  select --/*+ ORDERED INDEX (prad, PA_RESOURCE_ACCUM_DETAILS_N2) */ -- Bug Fix: 3634912 added hint Bug 9054701 removed hint
6437 					 pars.run_id
6438 					,pars.rule_id
6439 					,pars.line_num
6440 					,pars.project_id
6441 					,pars.task_id
6442 					,parr.resource_list_member_id
6443 					,NVL(sum( decode (p_bal_type,
6444 						'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) + nvl( pta.i_tot_raw_cost,0),
6445 						'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6446 												 +nvl( pta.i_tot_billable_raw_cost,0),
6447 						'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6448 													  +nvl( pta.i_tot_billable_burdened_cost,0),
6449 						'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6450 											  +nvl(pta.i_tot_burdened_cost,0),
6451 						'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6452 											+nvl(pta.i_tot_labor_hours,0),
6453 						'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6454 										+nvl(pta.i_tot_quantity,0),
6455 						 0)),0)AMOUNT
6456 					 ,pa_currency.round_currency_amt(NVL(sum( decode (p_bal_type,	 --Bug 3590551:Introduced rounding
6457 						'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
6458 						'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6459 												 +nvl( pta.i_tot_billable_raw_cost,0),
6460 						'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6461 													  +nvl( pta.i_tot_billable_burdened_cost,0),
6462 						'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6463 											  +nvl(pta.i_tot_burdened_cost,0),
6464 						'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6465 											+nvl(pta.i_tot_labor_hours,0),
6466 						'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6467 										+nvl(pta.i_tot_quantity,0),
6468 						 0)),0)*(parr.resource_percent/100)*v_rule_pool_percent) ELIGIBLE_AMOUNT /* bug 3227783 */
6469 					,parr.resource_percent
6470 					,G_creation_date
6471 					,G_created_by
6472 					,G_last_update_date
6473 					,G_last_updated_by
6474 					,G_last_update_login
6475 				from    -- Bug Fix: 3634912 :Changed the order of the tables.
6476 					pa_alloc_run_sources pars,
6477 					pa_alloc_run_resource_det parr,
6478 					pa_resource_accum_details prad,
6479 					pa_txn_accum  pta
6480 				  where  pta.txn_accum_id              = prad.txn_accum_id
6481 					and  prad.Resource_list_member_id  = parr.resource_list_member_id
6482 					and  pta.Project_id                = pars.project_id
6483 					and  pta.task_id                   = pars.task_id
6484 					and  pars.run_id                   = p_run_id
6485 					and  parr.run_id                   = pars.run_id
6486 					and  parr.member_type              = 'S'
6487 					and  pars.exclude_flag             = 'N'
6488 					and  pars.project_id               = c_projects_rec.project_id
6489 					and  pta.pa_period                 = p_period
6490 				  group by pars.run_id
6491 					 ,pars.rule_id
6492 					 ,pars.line_num
6493 					 ,pars.project_id
6494 					 ,pars.task_id
6495 					 ,parr.resource_list_member_id
6496 					 ,parr.resource_percent
6497 					 ,G_creation_date
6498 					 ,G_created_by
6499 					 ,G_last_update_date
6500 					 ,G_last_updated_by
6501 					 ,G_last_update_login);
6502 			ElsiF Nvl(p_resource_struct_type,'RL') = 'RBS' Then
6503 					INSERT INTO PA_ALLOC_RUN_source_DET  (
6504 						   RUN_ID
6505 						 , RULE_ID
6506 						 , LINE_NUM
6507 						 , PROJECT_ID
6508 						 , TASK_ID
6509 						 , RESOURCE_LIST_MEMBER_ID
6510 						 , AMOUNT
6511 						 , ELIGIBLE_AMOUNT
6512 						 , RESOURCE_PERCENT
6513 						 , CREATION_DATE
6514 						 , CREATED_BY
6515 						 , LAST_UPDATE_DATE
6516 						 , LAST_UPDATED_BY
6517 						 , LAST_UPDATE_LOGIN )
6518 					(  select pars.run_id
6519 						 ,pars.rule_id
6520 						 ,pars.line_num
6521 						 ,pars.project_id
6522 						 ,pars.task_id
6523 						 ,parr.resource_list_member_id
6524 						 ,NVL(sum( decode (p_bal_type,
6525 							 'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) + nvl( pta.i_tot_raw_cost,0),
6526 							 'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6527 													 +nvl( pta.i_tot_billable_raw_cost,0),
6528 							 'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6529 														  +nvl( pta.i_tot_billable_burdened_cost,0),
6530 							 'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6531 											  +nvl(pta.i_tot_burdened_cost,0),
6532 							'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6533 												+nvl(pta.i_tot_labor_hours,0),
6534 							'TOT_QUANTITY',  nvl(pta.tot_quantity,0)
6535 											+ nvl(pta.i_tot_quantity,0),
6536 							 0)),0)AMOUNT
6537 						,pa_currency.round_currency_amt(NVL(sum( decode (p_bal_type,    --Bug 3590551:Introduced rounding
6538 							'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
6539 							'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6540 													 +nvl( pta.i_tot_billable_raw_cost,0),
6541 							'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6542 														  +nvl( pta.i_tot_billable_burdened_cost,0),
6543 							'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6544 												  +nvl(pta.i_tot_burdened_cost,0),
6545 							'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6546 												+nvl(pta.i_tot_labor_hours,0),
6547 							'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6548 											+nvl(pta.i_tot_quantity,0),
6549 							 0)),0)*(parr.resource_percent/100)*v_rule_pool_percent) ELIGIBLE_AMOUNT /* bug 3227783 */
6550 						,parr.resource_percent
6551 						,G_creation_date
6552 						,G_created_by
6553 						,G_last_update_date
6554 						,G_last_updated_by
6555 						,G_last_update_login
6556 					 from    pa_alloc_run_resource_det parr,
6557 						 pa_alloc_run_sources pars,
6558 						 PA_ALLOC_TXN_ACCUM_RBS_V pta
6559 					  where  pta.Rbs_Element_ID		   = parr.resource_list_member_id
6560 					    and  pta.Project_id                = pars.project_id
6561 					    and  pta.task_id                   = pars.task_id
6562 						and  pta.RBS_STRUCT_VER_ID   = p_rbs_version_id
6563 					    and  pars.run_id                   = p_run_id
6564 					    and  parr.run_id                   = pars.run_id
6565 					    and  parr.member_type              = 'S'
6566 					    and  pars.exclude_flag             = 'N'
6567 					    and  pars.project_id               = c_projects_rec.project_id
6568 					    and  pta.pa_period                 = p_period
6569 					  group by pars.run_id
6570 						 ,pars.rule_id
6571 						 ,pars.line_num
6572 						 ,pars.project_id
6573 						 ,pars.task_id
6574 						 ,parr.resource_list_member_id
6575 						 ,parr.resource_percent
6576 						 ,G_creation_date
6577 						 ,G_created_by
6578 						 ,G_last_update_date
6579 						 ,G_last_updated_by
6580 						 ,G_last_update_login);
6581 				End If;		-------------}
6582 		ELSE /* p_run_period_type = 'GL' */
6583 				IF Nvl(p_resource_struct_type,'RL') = 'RL' Then		----------- {
6584 						INSERT INTO PA_ALLOC_RUN_source_DET  (
6585 						   RUN_ID
6586 						 , RULE_ID
6587 						 , LINE_NUM
6588 						 , PROJECT_ID
6589 						 , TASK_ID
6590 						 , RESOURCE_LIST_MEMBER_ID
6591 						 , AMOUNT
6592 						 , ELIGIBLE_AMOUNT
6593 						 , RESOURCE_PERCENT
6594 						 , CREATION_DATE
6595 						 , CREATED_BY
6596 						 , LAST_UPDATE_DATE
6597 						 , LAST_UPDATED_BY
6598 						 , LAST_UPDATE_LOGIN )
6599 						(  select --/*+ ORDERED INDEX (prad, PA_RESOURCE_ACCUM_DETAILS_N2) */ --Bug Fix: 3634912 added hint Bug 9054701 removed hint
6600 						          pars.run_id
6601 							  ,pars.rule_id
6602 							,pars.line_num
6603 							,pars.project_id
6604 							,pars.task_id
6605 							,parr.resource_list_member_id
6606 							,NVL(sum( decode (p_bal_type,
6607 								'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
6608 								'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6609 													 +nvl( pta.i_tot_billable_raw_cost,0),
6610 								'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6611 													  +nvl( pta.i_tot_billable_burdened_cost,0),
6612 								'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6613 													  +nvl(pta.i_tot_burdened_cost,0),
6614 								'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6615 													  +nvl(pta.i_tot_labor_hours,0),
6616 								'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6617 													  +nvl(pta.i_tot_quantity,0),
6618 								 0)),0)AMOUNT
6619 							,pa_currency.round_currency_amt(NVL(sum( decode (p_bal_type,	  --Bug 3590551:Introduced rounding
6620 								'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
6621 								'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6622 													 +nvl( pta.i_tot_billable_raw_cost,0),
6623 								'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6624 													  +nvl( pta.i_tot_billable_burdened_cost,0),
6625 								'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6626 													  +nvl(pta.i_tot_burdened_cost,0),
6627 								'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6628 													  +nvl(pta.i_tot_labor_hours,0),
6629 								'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6630 													  +nvl(pta.i_tot_quantity,0),
6631 								 0)),0)*(parr.resource_percent/100)*v_rule_pool_percent) ELIGIBLE_AMOUNT /* bug 3227783 */
6632 							,parr.resource_percent
6633 							,G_creation_date
6634 							,G_created_by
6635 							,G_last_update_date
6636 							,G_last_updated_by
6637 							,G_last_update_login
6638 			                        from    --Bug Fix: 3634912 : Changed the order of the tables.
6639 							pa_alloc_run_sources pars,
6640 						        pa_alloc_run_resource_det parr,
6641 							pa_resource_accum_details prad,
6642 							pa_txn_accum  pta
6643 						  where  pta.txn_accum_id              = prad.txn_accum_id
6644 							and  prad.Resource_list_member_id  = parr.resource_list_member_id
6645 							and  pta.Project_id                = pars.project_id
6646 							and  pta.task_id                   = pars.task_id
6647 							and  pars.run_id                   = p_run_id
6648 							and  parr.run_id                   = pars.run_id
6649 							and  parr.member_type              = 'S'
6650 							and  pars.exclude_flag             = 'N'
6651 							and  pars.project_id               = c_projects_rec.project_id
6652 							and  pta.gl_period		   = p_period /* Using gl_period on pta directly :bug 2757875 */
6653 				/****                   and  pta.pa_period IN
6654 						 (SELECT period_name
6655 							FROM pa_periods pp
6656 						   WHERE pp.gl_period_name = p_period) **** Commented for bug 2757875 ****/
6657 						  group by pars.run_id
6658 								 ,pars.rule_id
6659 								 ,pars.line_num
6660 								 ,pars.project_id
6661 								 ,pars.task_id
6662 								 ,parr.resource_list_member_id
6663 								 ,parr.resource_percent
6664 								 ,G_creation_date
6665 								 ,G_created_by
6666 								 ,G_last_update_date
6667 								 ,G_last_updated_by
6668 								 ,G_last_update_login);
6669 				ElsIF Nvl(p_resource_struct_type,'RL') = 'RBS' Then
6670 				INSERT INTO PA_ALLOC_RUN_source_DET  (
6671 						   RUN_ID
6672 						 , RULE_ID
6673 						 , LINE_NUM
6674 						 , PROJECT_ID
6675 						 , TASK_ID
6676 						 , RESOURCE_LIST_MEMBER_ID
6677 						 , AMOUNT
6678 						 , ELIGIBLE_AMOUNT
6679 						 , RESOURCE_PERCENT
6680 						 , CREATION_DATE
6681 						 , CREATED_BY
6682 						 , LAST_UPDATE_DATE
6683 						 , LAST_UPDATED_BY
6684 						 , LAST_UPDATE_LOGIN )
6685 					(  select pars.run_id
6686 						 ,pars.rule_id
6687 						 ,pars.line_num
6688 						 ,pars.project_id
6689 						 ,pars.task_id
6690 						 ,parr.resource_list_member_id
6691 						 ,NVL(sum( decode (p_bal_type,
6692 							 'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
6693 							 'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6694 													 +nvl( pta.i_tot_billable_raw_cost,0),
6695 							 'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6696 													  +nvl( pta.i_tot_billable_burdened_cost,0),
6697 							 'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6698 													  +nvl(pta.i_tot_burdened_cost,0),
6699 							 'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6700 													  +nvl(pta.i_tot_labor_hours,0),
6701 							 'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6702 													  +nvl(pta.i_tot_quantity,0),
6703 							  0)),0)AMOUNT
6704 						 ,pa_currency.round_currency_amt(NVL(sum( decode (p_bal_type,	 --Bug 3590551:Introduced rounding
6705 							 'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
6706 							 'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6707 													  +nvl( pta.i_tot_billable_raw_cost,0),
6708 							 'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6709 													  +nvl( pta.i_tot_billable_burdened_cost,0),
6710 							 'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6711 													  +nvl(pta.i_tot_burdened_cost,0),
6712 							 'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6713 													  +nvl(pta.i_tot_labor_hours,0),
6714 							 'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6715 													  +nvl(pta.i_tot_quantity,0),
6716 							  0)),0)*(parr.resource_percent/100)*v_rule_pool_percent) ELIGIBLE_AMOUNT /* bug 3227783 */
6717 						 ,parr.resource_percent
6718 						 ,G_creation_date
6719 						 ,G_created_by
6720 						 ,G_last_update_date
6721 						 ,G_last_updated_by
6722 						 ,G_last_update_login
6723 					 from    pa_alloc_run_resource_det parr,
6724 						 pa_alloc_run_sources pars,
6725 						 PA_ALLOC_TXN_ACCUM_RBS_V pta
6726 					  where  pta.Rbs_Element_Id			   = parr.resource_list_member_id
6727 						and  pta.Project_id                = pars.project_id
6728 						and  pta.task_id                   = pars.task_id
6729 						and  pta.RBS_STRUCT_VER_ID   = p_rbs_version_id
6730 						and  pars.run_id                   = p_run_id
6731 						and  parr.run_id                   = pars.run_id
6732 						and  parr.member_type              = 'S'
6733 						and  pars.exclude_flag             = 'N'
6734 						and  pars.project_id               = c_projects_rec.project_id
6735 						and  pta.gl_period		   = p_period
6736 					  group by pars.run_id
6737 						 ,pars.rule_id
6738 						 ,pars.line_num
6739 						 ,pars.project_id
6740 						 ,pars.task_id
6741 						 ,parr.resource_list_member_id
6742 						 ,parr.resource_percent
6743 						 ,G_creation_date
6744 						 ,G_created_by
6745 						 ,G_last_update_date
6746 						 ,G_last_updated_by
6747 						 ,G_last_update_login);
6748 			End If;		------------- }
6749            END IF;
6750                v_commit_count := v_commit_count + sql%rowcount;
6751                 IF v_commit_count > 5000 then
6752                    IF P_DEBUG_MODE = 'Y' THEN
6753                       pa_debug.write_file('insert_alloc_source_resource: ' || 'LOG','commiting the changes after 5000 records');
6754                    END IF;
6755                    COMMIT;
6756                    v_commit_count := 0;
6757                 END IF;
6758        END LOOP;
6759        COMMIT;
6760     Elsif p_amt_type = 'ITD' then
6761        pa_debug.G_err_stage:= 'inserting for ITD';
6762        IF P_DEBUG_MODE = 'Y' THEN
6763           pa_debug.write_file('insert_alloc_source_resource: ' ||  'LOG', pa_debug.G_err_stage);
6764        END IF;
6765        v_commit_count := 0;
6766        FOR c_projects_rec in c_projects LOOP
6767 /****      OPEN C_proj_start_date(c_projects_rec.project_id) ;
6768            FETCH C_proj_start_date into v_project_start_date ;
6769            If C_proj_start_date%NOTFOUND  then
6770                v_project_start_date := NULL ;
6771            End if ;
6772            CLOSE C_proj_start_date ;
6773                IF v_project_start_date is NOT NULL then
6774 **** Commented for bug 2757875****/
6775 	      IF P_DEBUG_MODE = 'Y' THEN
6776                   pa_debug.write_file('insert_alloc_source_resource: ' ||  'LOG', 'inserting data for project ' || c_projects_rec.project_id);
6777               END IF;
6778 		   IF Nvl(p_resource_struct_type,'RL') = 'RL' Then	---------{
6779 			INSERT INTO PA_ALLOC_RUN_source_DET (
6780 						  RUN_ID
6781 						, RULE_ID
6782 						, LINE_NUM
6783 						, PROJECT_ID
6784 						, TASK_ID
6785 						, RESOURCE_LIST_MEMBER_ID
6786 						, AMOUNT
6787 						, ELIGIBLE_AMOUNT
6788 						, RESOURCE_PERCENT
6789 						, CREATION_DATE
6790 						, CREATED_BY
6791 						, LAST_UPDATE_DATE
6792 						, LAST_UPDATED_BY
6793 						, LAST_UPDATE_LOGIN )
6794 				   (  select --/*+ ORDERED INDEX (prad, PA_RESOURCE_ACCUM_DETAILS_N2) */ --Bug Fix: 3634912 added hint Bug 9054701 removed hint
6795 					      pars.run_id
6796 					     ,pars.rule_id
6797 					     ,pars.line_num
6798 					     ,pars.project_id
6799 					     ,pars.task_id
6800 					     ,parr.resource_list_member_id
6801 					     ,NVL(sum( decode (p_bal_type,
6802 					     'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
6803 						'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6804 												 +nvl( pta.i_tot_billable_raw_cost,0),
6805 						'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6806 												 +nvl( pta.i_tot_billable_burdened_cost,0),
6807 						'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6808 												 +nvl(pta.i_tot_burdened_cost,0),
6809 						'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6810 												 +nvl(pta.i_tot_labor_hours,0),
6811 						'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6812 											 +nvl(pta.i_tot_quantity,0),
6813 							0
6814 						  )),0) AMOUNT
6815 						,pa_currency.round_currency_amt(NVL(sum( decode (p_bal_type,	 --Bug 3590551:Introduced rounding
6816 							'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
6817 							'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6818 													 +nvl( pta.i_tot_billable_raw_cost,0),
6819 							'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6820 													 +nvl( pta.i_tot_billable_burdened_cost,0),
6821 							'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6822 													 +nvl(pta.i_tot_burdened_cost,0),
6823 							'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6824 														 +nvl(pta.i_tot_labor_hours,0),
6825 							'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6826 													 +nvl(pta.i_tot_quantity,0),
6827 							0
6828 						  )),0)*(parr.resource_percent/100)*v_rule_pool_percent) ELIGIBLE_AMOUNT /* bug 3227783 */
6829 						,parr.resource_percent
6830 						,G_creation_date
6831 						,G_created_by
6832 						,G_last_update_date
6833 						,G_last_updated_by
6834 						,G_last_update_login
6835 					from  --Bug Fix: 3634912 : Changed the order of the tables
6836 						pa_alloc_run_sources pars,
6837 						pa_alloc_run_resource_det parr,
6838 						pa_resource_accum_details prad,
6839 						pa_txn_accum  pta
6840 					 where  pta.txn_accum_id              = prad.txn_accum_id
6841 					   and  prad.Resource_list_member_id  = parr.resource_list_member_id
6842 					   and  prad.Project_id               = pars.project_id
6843 					   and  prad.task_id                  = pars.task_id
6844 					   and  pars.run_id                   = p_run_id
6845 					   and  parr.run_id                   = pars.run_id
6846 					   and  parr.member_type              = 'S'
6847 					   and  pars.exclude_flag             = 'N'
6848 					   and  pars.project_id               = c_projects_rec.project_id
6849 					   and  exists
6850 							(select /*+ NO_UNNEST */	--Bug 3634912 : Added Hint.
6851 								1
6852 							  from  pa_periods pp
6853 							 where  pta.pa_period = pp.period_name
6854 							   and  pp.end_date  <= p_run_period_end_date) /* Added for bug 2757875 */
6855 			/****                              and  pp.end_date between v_project_start_date
6856 						and p_run_period_end_date) **** Commented for bug 2757875****/
6857 					 group by pars.run_id
6858 						,pars.rule_id
6859 						,pars.line_num
6860 						,pars.project_id
6861 						,pars.task_id
6862 						,parr.resource_list_member_id
6863 						,parr.resource_percent
6864 						,G_creation_date
6865 						,G_created_by
6866 						,G_last_update_date
6867 						,G_last_updated_by
6868 						,G_last_update_login);
6869 			  ElsIf Nvl(p_resource_struct_type,'RL') = 'RBS' Then
6870 					INSERT INTO PA_ALLOC_RUN_source_DET (
6871 							  RUN_ID
6872 							, RULE_ID
6873 							, LINE_NUM
6874 							, PROJECT_ID
6875 							, TASK_ID
6876 							, RESOURCE_LIST_MEMBER_ID
6877 							, AMOUNT
6878 							, ELIGIBLE_AMOUNT
6879 							, RESOURCE_PERCENT
6880 							, CREATION_DATE
6881 							, CREATED_BY
6882 							, LAST_UPDATE_DATE
6883 							, LAST_UPDATED_BY
6884 							, LAST_UPDATE_LOGIN )
6885 						   (  select pars.run_id
6886 							,pars.rule_id
6887 							,pars.line_num
6888 							,pars.project_id
6889 							,pars.task_id
6890 							,parr.resource_list_member_id
6891 							,NVL(sum( decode (p_bal_type,
6892 								'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
6893 								'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6894 													 +nvl( pta.i_tot_billable_raw_cost,0),
6895 								'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6896 													 +nvl( pta.i_tot_billable_burdened_cost,0),
6897 								'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6898 													 +nvl(pta.i_tot_burdened_cost,0),
6899 								'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6900 													 +nvl(pta.i_tot_labor_hours,0),
6901 								'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6902 													 +nvl(pta.i_tot_quantity,0),
6903 								0
6904 							  )),0) AMOUNT
6905 							,pa_currency.round_currency_amt(NVL(sum( decode (p_bal_type,  --Bug 3590551:Introduced rounding
6906 								'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
6907 								'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
6908 													 +nvl( pta.i_tot_billable_raw_cost,0),
6909 								'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
6910 													 +nvl( pta.i_tot_billable_burdened_cost,0),
6911 								'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
6912 													 +nvl(pta.i_tot_burdened_cost,0),
6913 								'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
6914 													 +nvl(pta.i_tot_labor_hours,0),
6915 								'TOT_QUANTITY', nvl(pta.tot_quantity,0)
6916 													 +nvl(pta.i_tot_quantity,0),
6917 								0
6918 							  )),0)*(parr.resource_percent/100)*v_rule_pool_percent) ELIGIBLE_AMOUNT /* bug 3227783 */
6919 							,parr.resource_percent
6920 							,G_creation_date
6921 							,G_created_by
6922 							,G_last_update_date
6923 							,G_last_updated_by
6924 							,G_last_update_login
6925 						from    pa_alloc_run_resource_det parr,
6926 							pa_alloc_run_sources	  pars,
6927 							PA_ALLOC_TXN_ACCUM_RBS_V  pta
6928 						 where  pta.Rbs_Element_Id			  = parr.resource_list_member_id
6929 						   and  pta.Project_id                = pars.project_id
6930 						   and  pta.task_id                   = pars.task_id
6931 						   and  pta.RBS_STRUCT_VER_ID   = p_rbs_version_id
6932 						   and  pars.run_id                   = p_run_id
6933 						   and  parr.run_id                   = pars.run_id
6934 						   and  parr.member_type              = 'S'
6935 						   and  pars.exclude_flag             = 'N'
6936 						   and  pars.project_id               = C_projects_rec.project_id
6937 						   and  exists
6938 							(select /*+ NO_UNNEST */  -- Bug3634912 : Added hint .
6939 								1
6940 							   from  pa_periods pp
6941 								  where  pta.pa_period = pp.period_name
6942 									    and  pp.end_date  <= p_run_period_end_date
6943 									)
6944 						 group by pars.run_id
6945 							,pars.rule_id
6946 							,pars.line_num
6947 							,pars.project_id
6948 							,pars.task_id
6949 							,parr.resource_list_member_id
6950 							,parr.resource_percent
6951 							,G_creation_date
6952 							,G_created_by
6953 							,G_last_update_date
6954 							,G_last_updated_by
6955 							,G_last_update_login);
6956 		  End If;
6957 	      v_commit_count := v_commit_count + sql%rowcount;
6958 --             END IF; ****  Commented for bug 2757875
6959                IF v_commit_count > 5000 then
6960                   IF P_DEBUG_MODE = 'Y' THEN
6961                      pa_debug.write_file('insert_alloc_source_resource: ' || 'LOG','commiting the changes after 5000 records');
6962                   END IF;
6963                   COMMIT;
6964                   v_commit_count := 0;
6965                END IF;
6966        END LOOP;
6967        COMMIT;
6968     End If ;
6969     pa_debug.G_err_stage:= 'exiting insert_alloc_source_resource';
6970     IF P_DEBUG_MODE = 'Y' THEN
6971        pa_debug.write_file('insert_alloc_source_resource: ' ||  'LOG', pa_debug.G_err_stage);
6972     END IF;
6973 EXCEPTION
6974     WHEN OTHERS THEN
6975        RAISE ;
6976 END insert_alloc_source_resource;
6977 -- ==========================================================================
6978 /* PROCEDURE :  insert_budget_basis_resource
6979    Purpose   :  inserts records into pa_alloc_run_basis_det when budgets are used
6980                 for basis and resource lists are used to calculate basis amounts.
6981    Created   :  02-feb-01 Manoj.
6982    Modified  :  24-JAN-03 Tarun for bug 2757875
6983                 06-Apr-04 vthakkar FP.M : ALlocation Impact
6984 */
6985 -- ==========================================================================
6986 PROCEDURE insert_budget_basis_resource(p_run_id              IN NUMBER,
6987                                        p_rule_id             IN NUMBER,
6988                                        p_run_period_type     IN VARCHAR2,
6989                                        p_bal_type            IN VARCHAR2,
6990                                        p_budget_type_code    IN VARCHAR2,
6991                                        p_start_date          IN DATE ,
6992                                        p_end_date            IN DATE ,
6993 									   /* FP.M : Allocation Impact */
6994 									   p_basis_resource_struct_Type in Varchar2
6995 									   )
6996 IS
6997      cursor c_projects is
6998      select distinct project_id
6999        from pa_alloc_run_targets part
7000       where part.run_id = p_run_id;
7001     v_commit NUMBER := 0;
7002 BEGIN
7003   FOR c_projects_rec IN c_projects LOOP
7004      If p_run_period_type = 'PA' then
7005           INSERT INTO PA_ALLOC_RUN_BASIS_DET (
7006              RUN_ID
7007            , RULE_ID
7008            , LINE_NUM
7009            , PROJECT_ID
7010            , TASK_ID
7011            , RESOURCE_LIST_MEMBER_ID
7012            , AMOUNT
7013            , LINE_PERCENT
7014            , CREATION_DATE
7015            , CREATED_BY
7016            , LAST_UPDATE_DATE
7017            , LAST_UPDATED_BY
7018            , LAST_UPDATE_LOGIN )
7019           (  select part.run_id
7020                    ,part.rule_id
7021                    ,part.line_num
7022                    ,part.project_id
7023                    ,part.task_id
7024                    ,parr.resource_list_member_id
7025                    ,nvl(sum(decode (p_bal_type,
7026                     'BASE_RAW_COST',       nvl(pfpp.raw_cost,0),
7027                     'BASE_BURDENED_COST',  nvl(pfpp.burdened_cost,0),
7028                     'BASE_QUANTITY',       nvl(pfpp.quantity,0),
7029                     'BASE_LABOR_QUANTITY', nvl(pfpp.labor_quantity,0),
7030                     0)),0) AMOUNT
7031                    ,part.line_percent
7032                    ,G_creation_date
7033                    ,G_created_by
7034                    ,G_last_update_date
7035                    ,G_last_updated_by
7036                    ,G_last_update_login
7037             from   pa_alloc_run_resource_det parr,
7038                    pa_alloc_run_targets part,
7039     /***           pa_base_budget_by_pa_period_v  pbpp *** commented bug 2619977 */
7040                    pa_base_finplan_by_pa_period_v  pfpp   /* added bug 2619977 */
7041            where    Decode (
7042 							Nvl(p_basis_resource_struct_Type,'RL') ,
7043 							'RL' , pfpp.resource_list_member_id ,
7044 							'RBS' , pfpp.RBS_ELEMENT_ID
7045 					        ) = parr.resource_list_member_id
7046              and   pfpp.Project_id              = part.project_id
7047              and   pfpp.task_id                 = part.task_id
7048              and   part.project_id              = c_projects_rec.project_id
7049              and   part.exclude_flag            = 'N'
7050    /***      and   pbpp.budget_type_code        = p_budget_type_code *** commented bug 2619977 */
7051              and   pfpp.budget_version_id       = part.budget_version_id /* added bug 2619977 */
7052              and   pfpp.period_start_date      >= nvl(p_start_date,pfpp.period_start_date)
7053              and   pfpp.period_end_date        <= p_end_date
7054              and   parr.run_id                  = p_run_id
7055 			 and   parr.run_id                  = part.run_id  /* Bug #  3850611 */
7056              and   parr.member_type             = 'B'
7057             group by part.run_id
7058                    ,part.rule_id
7059                    ,part.line_num
7060                    ,part.project_id
7061                    ,part.task_id
7062                    ,parr.resource_list_member_id
7063                    ,part.line_percent
7064                    ,G_creation_date
7065                    ,G_created_by
7066                    ,G_last_update_date
7067                    ,G_last_updated_by
7068                    ,G_last_update_login);
7069      Elsif  p_run_period_type = 'GL' then
7070           INSERT INTO PA_ALLOC_RUN_BASIS_DET (
7071              RUN_ID
7072            , RULE_ID
7073            , LINE_NUM
7074            , PROJECT_ID
7075            , TASK_ID
7076            , RESOURCE_LIST_MEMBER_ID
7077            , AMOUNT
7078            , LINE_PERCENT
7079            , CREATION_DATE
7080            , CREATED_BY
7081            , LAST_UPDATE_DATE
7082            , LAST_UPDATED_BY
7083            , LAST_UPDATE_LOGIN )
7084           (  select part.run_id
7085                    ,part.rule_id
7086                    ,part.line_num
7087                    ,part.project_id
7088                    ,part.task_id
7089                    ,parr.resource_list_member_id
7090                    ,nvl(sum( decode (p_bal_type,
7091                     'BASE_RAW_COST',       nvl(pfpg.raw_cost,0),
7092                     'BASE_BURDENED_COST',  nvl(pfpg.burdened_cost,0),
7093                     'BASE_QUANTITY',       nvl(pfpg.quantity,0),
7094                     'BASE_LABOR_QUANTITY', nvl(pfpg.labor_quantity,0),
7095                     0
7096                     )),0)  amount
7097                    ,part.line_percent
7098                    ,G_creation_date
7099                    ,G_created_by
7100                    ,G_last_update_date
7101                    ,G_last_updated_by
7102                    ,G_last_update_login
7103             from   pa_alloc_run_resource_det parr,
7104                    pa_alloc_run_targets part,
7105     /***           pa_base_budget_by_gl_period_v  pbpg *** commented bug 2619977 */
7106 	           /* pa_base_finplan_by_pa_period_v  pfpg     added bug 2619977 commented bug 2757875 */
7107 			    pa_base_finplan_by_gl_period_v  pfpg     /*  added bug 2757875 */
7108            where   Decode (
7109 							NVL(p_basis_resource_struct_Type,'RL') ,
7110 							'RL' , pfpg.resource_list_member_id ,
7111 							'RBS' , pfpg.RBS_ELEMENT_ID
7112 					        )  = parr.resource_list_member_id
7113              and   pfpg.Project_id              = part.project_id
7114              and   pfpg.task_id                 = part.task_id
7115     /***     and   pfpg.budget_type_code        = p_budget_type_code ** commented bug 2619977 */
7116              and   pfpg.budget_version_id       = part.budget_version_id /* added bug 2619977 */
7117              and   pfpg.period_start_date      >= nvl(p_start_date,pfpg.period_start_date)
7118              and   pfpg.period_end_date        <= p_end_date
7119              and   part.project_id              = c_projects_rec.project_id
7120              and   part.exclude_flag            = 'N'
7121              and   parr.run_id                  = p_run_id
7122 			 and   parr.run_id                  = part.run_id /* Bug #  3850611 */
7123              and   parr.member_type             = 'B'
7124             group by part.run_id
7125                    ,part.rule_id
7126                    ,part.line_num
7127                    ,part.project_id
7128                    ,part.task_id
7129                    ,parr.resource_list_member_id
7130                    ,part.line_percent
7131                    ,G_creation_date
7132                    ,G_created_by
7133                    ,G_last_update_date
7134                    ,G_last_updated_by
7135                    ,G_last_update_login);
7136      End if ;
7137      v_commit := v_commit + sql%rowcount;
7138      if v_commit > 5000 then
7139         commit;
7140         v_commit := 0;
7141      end if;
7142   END LOOP;
7143 EXCEPTION
7144     WHEN OTHERS THEN
7145        RAISE ;
7146 END insert_budget_basis_resource;
7147 /* following procedure is obsoleted (not in use after fix for 2211234 */
7148 -- ==========================================================================
7149 /* PROCEDURE :  get_alloc_amount
7150    Purpose   :  To get the accumulated amount for given project/task/Resource.
7151                 The amount type ( FYTD/QTD/ITD/PTD) determines the start date from
7152                 which the amounts need to be considered.
7153                 For FYTD and QTD the period type is always GL.
7154                 For PTD the period type can be PA or GL
7155                 For ITD the project start date is the start date.
7156      Created :   30-JUL-98   Sesivara
7157 */
7158 -- ==========================================================================
7159 PROCEDURE get_alloc_amount( p_amt_type        IN VARCHAR2,
7160                             p_bal_type        IN VARCHAR2,
7161                             p_run_period_type IN VARCHAR2,
7162                             p_project_id      IN NUMBER  ,
7163                             p_task_id         IN NUMBER  ,
7164                             p_rlm_id          IN NUMBER  ,
7165                             p_period          IN VARCHAR2,
7166                             p_period_type     IN VARCHAR2 ,
7167                             p_peiod_set_name  IN VARCHAR2 ,
7168                             p_period_year     IN NUMBER   ,
7169                             p_quarter         IN NUMBER   ,
7170                             p_run_period_end_date IN DATE ,
7171                             p_amttype_start_date  IN DATE ,
7172                             x_amount          OUT NOCOPY NUMBER  )
7173 IS
7174      v_project_start_date  DATE ;
7175      cursor C_fytd_qtd_amt is
7176        select NVL(sum( decode (p_bal_type,
7177                     'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
7178                     'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
7179                                              +nvl( pta.i_tot_billable_raw_cost,0),
7180                     'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
7181                                              +nvl( pta.i_tot_billable_burdened_cost,0),
7182                     'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
7183                                              +nvl(pta.i_tot_burdened_cost,0),
7184                     'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
7185                                              +nvl(pta.i_tot_labor_hours,0),
7186                     'TOT_QUANTITY', nvl(pta.tot_quantity,0)
7187                                              +nvl(pta.i_tot_quantity,0),
7188                     0
7189                   )),0)
7190          from pa_txn_accum  pta,
7191               pa_periods pp,
7192               pa_resource_accum_details prad
7193         where pta.txn_accum_id = prad.txn_accum_id
7194            and  prad. Resource_list_member_id = p_rlm_id
7195            and  prad. Project_id              = p_project_id
7196            and  prad.task_id                  = p_task_id
7197            and  pta.pa_period                 = pp.period_name
7198            and  pp.end_date                  >= p_amttype_start_date
7199            and  pp.end_date                  <= p_run_period_end_date ;
7200      cursor C_ptd_amt is
7201        select NVL(sum( decode (p_bal_type,
7202                     'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
7203                     'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
7204                                              +nvl( pta.i_tot_billable_raw_cost,0),
7205                     'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
7206                                              +nvl( pta.i_tot_billable_burdened_cost,0),
7207                     'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
7208                                              +nvl(pta.i_tot_burdened_cost,0),
7209                     'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
7210                                              +nvl(pta.i_tot_labor_hours,0),
7211                     'TOT_QUANTITY', nvl(pta.tot_quantity,0)
7212                                              +nvl(pta.i_tot_quantity,0),
7213                     0
7214                   )),0)
7215          from pa_txn_accum  pta,
7216               pa_resource_accum_details prad
7217          where pta.txn_accum_id = prad.txn_accum_id
7218            and  prad. Resource_list_member_id = p_rlm_id
7219            and  prad. Project_id              = p_project_id
7220            and  prad.task_id                  = p_task_id
7221            and  decode ( p_run_period_type, 'GL', pta.gl_period, pta.pa_period) = p_period ;
7222      cursor C_itd_amt is
7223        select NVL(sum( decode (p_bal_type,
7224                     'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
7225                     'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
7226                                              +nvl( pta.i_tot_billable_raw_cost,0),
7227                     'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
7228                                              +nvl( pta.i_tot_billable_burdened_cost,0),
7229                     'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
7230                                              +nvl(pta.i_tot_burdened_cost,0),
7231                     'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
7232                                              +nvl(pta.i_tot_labor_hours,0),
7233                     'TOT_QUANTITY', nvl(pta.tot_quantity,0)
7234                                              +nvl(pta.i_tot_quantity,0),
7235                     0
7236                   )),0)
7237          from pa_txn_accum  pta,
7238               pa_periods    pp ,
7239               pa_resource_accum_details prad
7240        where  pta.txn_accum_id = prad.txn_accum_id
7241          and  prad. Resource_list_member_id = p_rlm_id
7242          and  prad. Project_id              = p_project_id
7243          and  prad.task_id                  = p_task_id
7244          and  pta.pa_period                 = pp.period_name
7245          and  pp.end_date                  >= v_project_start_date
7246          and  pp.end_date                  <= p_run_period_end_date ;
7247    Cursor C_proj_start_date is
7248         select start_date
7249           from  pa_projects
7250          where project_id =  p_project_id ;
7251 BEGIN
7252 --    project and task amount is returned  based on the amount type (FTYD/QTD/PTD/ITD)
7253 --    and run period and run period type
7254      If p_amt_type in ( 'FYTD', 'QTD') then
7255         OPEN C_fytd_qtd_amt ;
7256         FETCH C_fytd_qtd_amt into x_amount ;
7257         If C_fytd_qtd_amt%NOTFOUND  then
7258             x_amount := 0 ;
7259         End if ;
7260         CLOSE C_fytd_qtd_amt ;
7261      Elsif  p_amt_type = 'PTD' then
7262         OPEN C_ptd_amt ;
7263         FETCH C_ptd_amt into x_amount ;
7264         If C_ptd_amt%NOTFOUND  then
7265             x_amount := 0 ;
7266         End if ;
7267         CLOSE C_ptd_amt ;
7268     Elsif p_amt_type = 'ITD' then
7269         OPEN C_proj_start_date ;
7270         FETCH C_proj_start_date into v_project_start_date ;
7271         If C_proj_start_date%NOTFOUND  then
7272             v_project_start_date := NULL ;
7273         End if ;
7274         CLOSE C_proj_start_date ;
7275         IF v_project_start_date is NOT NULL then
7276            OPEN C_itd_amt ;
7277            FETCH C_itd_amt into x_amount ;
7278            If C_itd_amt%NOTFOUND  then
7279                x_amount := 0 ;
7280            End if ;
7281            CLOSE C_itd_amt ;
7282         End if ;
7283     End If ;
7284 EXCEPTION
7285     WHEN OTHERS THEN
7286        RAISE ;
7287 END get_alloc_amount ;
7288 -- ============================================================
7289 -- insert_alloc_run_src_det
7290 -- ============================================================
7291 PROCEDURE insert_alloc_run_src_det( p_rule_id            IN NUMBER
7292                                   , p_run_id             IN NUMBER
7293                                   , p_line_num           IN NUMBER
7294                                   , p_project_id         IN NUMBER
7295                                   , p_task_id            IN NUMBER
7296                                   , p_rlm_id             IN NUMBER
7297                                   , p_amount             IN NUMBER
7298                                   , p_resource_percent   IN NUMBER
7299                                   , p_eligible_amount    IN NUMBER
7300                                   , p_creation_date      IN DATE
7301                                   , p_created_by         IN NUMBER
7302                                   , p_last_update_date   IN DATE
7303                                   , p_last_updated_by    IN NUMBER
7304                                   , p_last_update_login  IN NUMBER)
7305 IS
7306 BEGIN
7307   pa_debug.set_err_stack('insert_alloc_run_source_det') ;
7308   pa_debug.G_err_stage := 'INSERTING PA_ALLOC_RUN_SOURCE_DET' ;
7309   INSERT INTO PA_ALLOC_RUN_SOURCE_DET (
7310      RUN_ID
7311    , RULE_ID
7312    , LINE_NUM
7313    , PROJECT_ID
7314    , TASK_ID
7315    , RESOURCE_LIST_MEMBER_ID
7316    , AMOUNT
7317    , RESOURCE_PERCENT
7318    , ELIGIBLE_AMOUNT
7319    , CREATION_DATE
7320    , CREATED_BY
7321    , LAST_UPDATE_DATE
7322    , LAST_UPDATED_BY
7323    , LAST_UPDATE_LOGIN )
7324   VALUES (
7325       p_run_id
7326     , p_rule_id
7327     , p_line_num
7328     , p_project_id
7329     , p_task_id
7330     , p_rlm_id
7331     , p_amount
7332     , p_resource_percent
7333     , p_eligible_amount
7334     , p_creation_date
7335     , p_created_by
7336     , p_last_update_date
7337     , p_last_updated_by
7338     , p_last_update_login ) ;
7339    /*  restore the old stack */
7340     pa_debug.reset_err_stack;
7341 EXCEPTION
7342    WHEN OTHERS THEN
7343    RAISE;
7344 END insert_alloc_run_src_det;
7345 -- ==========================================================================
7346 /* PROCEDURE :  calculate_amount_from_projects
7347    Purpose   :  For a given allocation run, this function will find the amount
7348                 for all defined projects , tasks on the given resources
7349      Created :   03-AUG-98   Sesivara
7350 	Modified:	  24-JAN-03   Tarun for bug 2757875
7351 */
7352 -- ==========================================================================
7353 PROCEDURE cal_amounts_from_projects(p_rule_id          IN NUMBER,
7354                                     p_run_id           IN NUMBER,
7355                                     p_run_period_type  IN VARCHAR2,
7356                                     p_run_amount_type  IN VARCHAR2,
7357                                     p_run_period       IN VARCHAR2,
7358                                     p_bal_type         IN VARCHAR2,
7359                                     p_resource_list_id IN NUMBER  ,
7360 									p_pool_percent     IN NUMBER  ,
7361                                     p_fixed_amount     IN NUMBER  ,
7362                                     x_proj_pool_amount OUT NOCOPY NUMBER ,
7363 									/* FP.M : Allocation Impact  Bug # 3512552 */
7364 									p_source_resource_struct_type in Varchar2,
7365 									p_source_rbs_version_id In Number
7366 )
7367 IS
7368 /*
7369   Function:  To calculate the source amount from each project and task for a
7370              given rule, run, and period type , amount type and run_period.
7371    Steps: 1.  Get the  Period Year and Quarter Num values for the given run_period.
7372           2. If source_resource_lists is not null then
7373                Populate  Include_RLM_tbl.
7374                For each resource list member in the Include_RLM_tbl then
7375                    Get source amount for each project and task in pa_alloc_run_sources
7376                    tables for given rule_id and run_id
7377              Else
7378                For each project and task in  pa_alloc_run_targets
7379                find the basis amount at project and task level.
7380              End if.
7381 */
7382     v_period_type           VARCHAR2(15)  ;
7383     v_period_set_name       VARCHAR2 (15) ;
7384     v_period_year           NUMBER    ;
7385     v_quarter               NUMBER    ;
7386     v_period_num            NUMBER    ;
7387     v_run_period_end_date   DATE      ;
7388     v_amttype_start_date    DATE      ;
7389     v_resource_list_id      NUMBER    ;
7390     v_rlm_id                NUMBER ;
7391     v_rlm_percent           NUMBER ;
7392 --  v_src_rlm_tab           SRC_RLM_TABTYPE ;  /* after fix 2211234 this table is not required */
7393     v_resource_count        NUMBER;  /* added for 2211234 */
7394     v_amount                NUMBER ;
7395     v_pool_amount           NUMBER ;
7396 -- Start: added as part of the bug 13949738
7397 	v_total_missing_cost    NUMBER := 0;
7398 	v_missing_amount        NUMBER;
7399 	v_previous_amount       NUMBER;
7400 -- End: added as part of the bug 13949738
7401     v_pool_percent          NUMBER ;
7402          v_net_fixed_amount      NUMBER ;
7403     cursor C_run_source_details is
7404            Select  line_num, project_id , task_id
7405              from  pa_alloc_run_sources
7406             where  rule_id   =  p_rule_id
7407              and   run_id    =  p_run_id
7408              and   exclude_flag  <> 'Y' ;
7409     cursor C_get_pool_amount is
7410            select nvl(sum(nvl(eligible_amount,0)),0)
7411              from pa_alloc_run_source_det
7412             where run_id = p_run_id
7413            AND (pa_project_stus_utils.is_project_closed(project_id ))= 'N';	 -- added as part of the bug 13949738
7414 
7415 -- Start: added as part of the bug 13949738
7416 		   cursor C_get_missing_source_amount is
7417            Select * from pa_alloc_run_source_det
7418            where run_id = p_run_id
7419            AND (pa_project_stus_utils.is_project_closed(project_id ))= 'Y';
7420 
7421 CURSOR C_get_previous_run_amount
7422     (  p_Fscl_Year    IN NUMBER
7423     ,  p_Qrtr_Num      IN NUMBER
7424     ,  p_Prd_Num      IN NUMBER
7425 	,  p_Project_Id   IN NUMBER
7426 	,  p_Task_Id      IN NUMBER
7427     )  IS
7428    SELECT  NVL ( A.Eligible_Amount, 0 )    Eligible_Amount
7429    FROM    PA_ALLOC_RUN_SOURCE_DET    A ,
7430     (  SELECT  MAX ( AR.Run_Id ) AS Run_Id
7431        FROM    PA_ALLOC_RUNS              AR
7432        WHERE  AR.Fiscal_Year  = NVL ( p_Fscl_Year , AR.Fiscal_Year )
7433        AND    AR.Quarter      = NVL ( p_Qrtr_Num  , AR.Quarter  )
7434        AND    AR.Period_Num  = NVL ( p_Prd_Num  , AR.Period_Num  )
7435        AND    AR.Run_Id      < p_Run_Id
7436        AND    AR.Rule_Id      = p_Rule_Id
7437        AND    AR.Run_Status  = 'RS'
7438        AND    NVL(AR.allocated_amount,0) <> 0
7439      )  AR
7440     WHERE  A.Run_Id        = AR.Run_Id
7441     AND    A.Rule_Id      = p_Rule_Id
7442     AND  A.Project_Id = p_Project_Id
7443 	AND  A.Task_Id = p_Task_Id ;
7444 -- End: added as part of the bug 13949738
7445 
7446 		   Cursor c_get_proj is
7447 		   Select RUN_ID              ,
7448 				  RULE_ID             ,
7449 				  LINE_NUM            ,
7450 				  PROJECT_ID          ,
7451 				  TASK_ID
7452 		     From Pa_Alloc_Run_Sources
7453 			Where Rule_Id = P_Rule_Id
7454 			  And Run_Id = P_Run_Id
7455 			  And Nvl(Exclude_Flag,'N') = 'N';
7456 BEGIN
7457     pa_debug.set_err_stack('Cal_amounts_from_projects') ;
7458     pa_debug.G_err_stage := 'Get Fiscal Year and quarter for run period and amounttype' ;
7459     IF P_DEBUG_MODE = 'Y' THEN
7460        pa_debug.write_file('cal_amounts_from_projects: ' || 'LOG', pa_debug.G_Err_Stage);
7461     END IF;
7462     get_fiscalyear_quarter(p_run_period_type, p_run_period, v_period_type,
7463                            v_period_set_name, v_period_year, v_quarter, v_period_num,
7464                            v_run_period_end_date) ;
7465     pa_debug.G_err_stage := 'Getting start date for given amount type(FYTD/QTD)' ;
7466     IF P_DEBUG_MODE = 'Y' THEN
7467        pa_debug.write_file('cal_amounts_from_projects: ' || 'LOG', pa_debug.G_Err_Stage);
7468     END IF;
7469     get_amttype_start_date( p_run_amount_type, v_period_type, v_period_set_name,
7470                             v_run_period_end_date, v_quarter, v_period_year,
7471                             p_run_period, v_amttype_start_date) ;
7472 --  v_resource_list_id := alloc_rule.alloc_resource_list_id ;
7473 --  v_pool_percent     := nvl(alloc_rule.pool_percent,100)/100     ;
7474     v_pool_percent     :=  nvl(p_pool_percent,100)/100  ;
7475     v_resource_list_id :=  p_resource_list_id ;
7476 --  Create source details record for fixed amount defined at rule level
7477     If nvl(p_fixed_amount, 0)  <> 0 then
7478 	   v_net_fixed_amount := p_fixed_amount*v_pool_percent;
7479        v_net_fixed_amount := pa_currency.round_currency_amt( v_net_fixed_amount );
7480 	   insert_alloc_run_src_det(p_rule_id, p_run_id, 0,
7481                                 0, 0, NULL, p_fixed_amount,
7482                                 NULL , v_net_fixed_amount,
7483                                 G_creation_date, G_created_by,
7484                                 G_last_update_date,
7485                                 G_last_updated_by, G_last_update_login);
7486     End if ;
7487     If v_resource_list_id is NOT NULL then
7488        pa_debug.G_err_stage := 'Populating the Resource List Member array         ' ;
7489 	   IF P_DEBUG_MODE = 'Y' THEN
7490           pa_debug.write_file('cal_amounts_from_projects: ' || 'LOG', pa_debug.G_Err_Stage);
7491        END IF;
7492        /* bug 2211234 - added p_run_id.
7493                       - removed v_src_rlm_tab
7494           This procedure will now populate records into pa_alloc_run_resources_det */
7495        populate_RLM_table( p_rule_id, p_run_id,
7496 						   'S',
7497 						   v_resource_list_id ,
7498 						   p_source_resource_struct_type ,
7499 						   p_source_rbs_version_id ,
7500 						   'A'  /* value of p_basis_category  as 'A' in case of Source */
7501 						   ) ;
7502        BEGIN
7503             select count(*)
7504               into v_resource_count
7505               from pa_alloc_run_resource_det
7506              where rule_id = p_rule_id
7507                and run_id = p_run_id
7508                and member_type = 'S';
7509             if v_resource_count = 0 then
7510                alloc_errors(p_rule_id, p_run_id, 'S','E', 'PA_AL_NO_INCL_SRC_RESRC',TRUE) ;
7511             end if ;
7512        EXCEPTION
7513        WHEN OTHERS THEN
7514           pa_debug.G_err_stage := 'error during selecting count from pa_alloc_run_resources_det' ;
7515           IF P_DEBUG_MODE = 'Y' THEN
7516              pa_debug.write_file('cal_amounts_from_projects: ' || 'LOG', pa_debug.G_Err_Stage);
7517           END IF;
7518           RAISE;
7519        END;
7520        pa_debug.G_err_stage := 'Processing project/Tasks to get resource level amounts ' ;
7521        IF P_DEBUG_MODE = 'Y' THEN
7522           pa_debug.write_file('cal_amounts_from_projects: ' || 'LOG', pa_debug.G_Err_Stage);
7523        END IF;
7524 /*     added for 2211234 */
7525        insert_alloc_source_resource(p_run_id               =>   p_run_id
7526                                    ,p_rule_id              =>   p_rule_id
7527                                    ,p_resource_list_id     =>   v_resource_list_id
7528                                    ,p_amt_type             =>   p_run_amount_type
7529                                    ,p_bal_type             =>   p_bal_type
7530                                    ,p_run_period_type      =>   p_run_period_type
7531                                    ,p_period               =>   p_run_period
7532                                    ,p_run_period_end_date  =>   v_run_period_end_date
7533                                    ,p_amttype_start_date   =>   v_amttype_start_date
7534 								   /* FP.M : Allocation Impact */
7535 								   ,p_resource_struct_type =>   p_source_resource_struct_type
7536 								   ,p_rbs_version_id       =>   p_source_rbs_version_id
7537 								   );
7538 /*     insert_alloc_source_resource will do whatever used to be done by this block
7539        For  I in 1.. v_src_rlm_tab.count   LOOP
7540              v_rlm_id            :=  v_src_rlm_tab(I).resource_list_member_id ;
7541              v_rlm_percent       := v_src_rlm_tab(I).resource_percent ;
7542             For src_det_rec in C_run_source_details  LOOP
7543                 get_alloc_amount( p_run_amount_type, p_bal_type, p_run_period_type,
7544                                   src_det_rec.project_id, src_det_rec.task_id,
7545                                   v_rlm_id, p_run_period, v_period_type,
7546                                   v_period_set_name, v_period_year, v_quarter,
7547                                   v_run_period_end_date,
7548                                   v_amttype_start_date, v_amount )  ;
7549                 IF P_DEBUG_MODE = 'Y' THEN
7550                    pa_debug.write_file('cal_amounts_from_projects: ' || 'LOG', 'v_rlm_percent '||to_char(v_rlm_percent));
7551                 END IF;
7552                 v_pool_amount := v_amount * (nvl(v_rlm_percent,100)/100) *
7553                                  (nvl(p_pool_percent,100)/100) ;
7554                 IF P_DEBUG_MODE = 'Y' THEN
7555                    pa_debug.write_file('cal_amounts_from_projects: ' || 'LOG', 'v_amount '||to_char(v_amount));
7556                    pa_debug.write_file('cal_amounts_from_projects: ' || 'LOG', 'v_pool_amount '||to_char(v_pool_amount));
7557                 END IF;
7558                 v_pool_amount := pa_currency.round_currency_amt( v_pool_amount );
7559                 v_amount := pa_currency.round_currency_amt( v_amount );
7560                 insert_alloc_run_src_det(p_rule_id, p_run_id, src_det_rec.line_num,
7561                                          src_det_rec.project_id,
7562                                          src_det_rec.task_id, v_rlm_id, v_amount,
7563                                          v_rlm_percent , v_pool_amount,
7564                                          G_creation_date, G_created_by,
7565                                          G_last_update_date,
7566                                          G_last_updated_by, G_last_update_login) ;
7567             END LOOP ;
7568        END LOOP ;
7569 */
7570 	Else
7571 	   pa_debug.G_err_stage := 'Processing project/Tasks to get amounts         ' ;
7572 	   IF P_DEBUG_MODE = 'Y' THEN
7573           pa_debug.write_file('cal_amounts_from_projects: ' || 'LOG', pa_debug.G_Err_Stage);
7574        END IF;
7575 	   If  p_run_amount_type in ( 'FYTD','QTD')  then
7576 		 For l_get_proj in c_get_proj
7577 		 Loop
7578 			 Insert into pa_alloc_run_source_det( rule_id, run_id, line_num, project_id,
7579 												  task_id, creation_date, created_by,
7580 												  last_update_date,
7581 												  last_updated_by, last_update_login,
7582 												  amount, eligible_amount)
7583 						 select  l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
7584 								 l_get_proj.task_id,
7585 								 /*  Bug 3749469
7586 								 pars.rule_id, pars.run_id,  pars.line_num, pars.project_id,
7587 								 pars.task_id,
7588 								 */
7589 								 G_creation_date, G_created_by, G_last_update_date,
7590 								 G_last_updated_by, G_last_update_login ,
7591 								 sum( decode (p_bal_type,
7592 						'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
7593 						'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
7594 												 +nvl( pta.i_tot_billable_raw_cost,0),
7595 						'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
7596 												 +nvl( pta.i_tot_billable_burdened_cost,0),
7597 						'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)+
7598 												 +nvl(pta.i_tot_burdened_cost,0),
7599 									  0 )),
7600 								 pa_currency.round_currency_amt(sum( decode (p_bal_type,
7601 						'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
7602 						'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
7603 												 +nvl( pta.i_tot_billable_raw_cost,0),
7604 						'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
7605 												 +nvl( pta.i_tot_billable_burdened_cost,0),
7606 						'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)+
7607 												 +nvl(pta.i_tot_burdened_cost,0),
7608 										0) * v_pool_percent ) )
7609 						   from  pa_alloc_txn_accum_v pta,
7610 														   /* FP.M : Allocation Impact : pa_txn_accum pta */
7611 														   /* Commenting out pa_periods for bug 2757875 and using gl_period_statuses instead */
7612 	--                           pa_periods   pp ,
7613 								 gl_period_statuses   gl ,
7614 								 pa_implementations imp
7615 								 /* pa_alloc_run_sources pars */ /* Loop thru pa_alloc_run_sources. Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id and Task_Id */
7616 						   where  /*  Bug 3749469
7617 						                 pars.rule_id       = p_rule_id
7618 									and  pars.run_id        = p_run_id
7619 								    and  pars.exclude_flag  = 'N'
7620 								    and  pta.project_id     = pars.project_id
7621 									and  pta.task_id        = pars.task_id
7622 								 */
7623 							      pta.project_id     = l_get_proj.project_id /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id */
7624 						     and  pta.task_id        = l_get_proj.task_id /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id */
7625 							 and  pta.gl_period      = gl.period_name
7626 							 and  gl.set_of_books_id = imp.set_of_books_id
7627 							 and  gl.application_id = pa_period_process_pkg.application_id
7628 							 and  gl.adjustment_period_flag = 'N'
7629 							 and  gl.closing_status in ('C','F','O','P')
7630 							 and  gl.end_date       >= v_amttype_start_date
7631 							 and  gl.end_date       <= v_run_period_end_date
7632 	--                       and  pta.pa_period      = pp.period_name
7633 	--                       and  pp.end_date       >= v_amttype_start_date
7634 	--                       and  pp.end_date       <= v_run_period_end_date
7635 						group by  /*
7636 								  pars.rule_id, pars.run_id,  pars.line_num, pars.project_id,
7637 								  pars.task_id,
7638 								  */
7639 								  l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
7640 								  l_get_proj.task_id,
7641 								  G_creation_date, G_created_by, G_last_update_date,
7642 								  G_last_updated_by, G_last_update_login ;
7643 		 End Loop;
7644        elsif p_run_amount_type = 'PTD' then
7645 		   If p_run_period_type = 'GL' then
7646 			For l_get_proj in c_get_proj
7647 			Loop
7648 			  Insert into pa_alloc_run_source_det( rule_id, run_id, line_num, project_id,                                              task_id,
7649                                               creation_date, created_by,
7650                                               last_update_date,
7651                                               last_updated_by, last_update_login,
7652                                               amount, eligible_amount)
7653                  select  /* Bug 3749469
7654 						 pars.rule_id, pars.run_id, pars.line_num,  pars.project_id,
7655                          pars.task_id,
7656 						 */
7657 						 l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
7658 						 l_get_proj.task_id,
7659                          G_creation_date, G_created_by, G_last_update_date,
7660                          G_last_updated_by, G_last_update_login ,
7661                              sum( decode (p_bal_type,
7662                     'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
7663                     'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
7664                                              +nvl( pta.i_tot_billable_raw_cost,0),
7665                     'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
7666                                              +nvl( pta.i_tot_billable_burdened_cost,0),
7667                     'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)+
7668                                              +nvl(pta.i_tot_burdened_cost,0),
7669                                   0 )),
7670                             pa_currency.round_currency_amt( sum( decode (p_bal_type,
7671                     'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
7672                     'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
7673                                              +nvl( pta.i_tot_billable_raw_cost,0),
7674                     'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
7675                                              +nvl( pta.i_tot_billable_burdened_cost,0),
7676                     'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)+
7677                                              +nvl(pta.i_tot_burdened_cost,0),
7678                                     0) * v_pool_percent ) )
7679                  from  pa_alloc_txn_accum_v pta /* FP.M : Allocation Impact : pa_txn_accum pta */
7680 -- Commented out pa_periods. Used the gl_periods column in pa_txn_accum table
7681 --                     pa_periods   pp
7682                        /* pa_alloc_run_sources pars */ /* Loop thru pa_alloc_run_sources. Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id and Task_Id */
7683                 where  /* pars.rule_id       = p_rule_id
7684                   and  pars.run_id        = p_run_id
7685                   and  pars.exclude_flag  = 'N'
7686 				  and  pta.project_id     = pars.project_id
7687                   and  pta.task_id        = pars.task_id
7688                   */
7689 				       pta.project_id     = l_get_proj.project_id /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id */
7690 				  and  pta.task_id		  = l_get_proj.task_id /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Task_Id */
7691                   and  pta.gl_period      = p_run_period
7692 --                and  pta.pa_period      = pp.period_name
7693 --                and  pp.gl_period_name  = p_run_period
7694              group by
7695 			           /* Bug 3749469
7696 					   pars.rule_id, pars.run_id, pars.line_num,pars.project_id,
7697                        pars.task_id,
7698 					   */
7699 					   l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
7700 					   l_get_proj.task_id,
7701                        G_creation_date, G_created_by, G_last_update_date,
7702                        G_last_updated_by, G_last_update_login ;
7703 			End Loop;
7704 		elsif p_run_period_type = 'PA' then
7705 			For l_get_proj in c_get_proj
7706 			Loop
7707 				 Insert into pa_alloc_run_source_det(
7708                       rule_id, run_id, line_num, project_id, task_id,
7709                       creation_date, created_by, last_update_date,
7710                       last_updated_by, last_update_login,
7711                       amount, eligible_amount)
7712                   select
7713 					   /* Bug 3749469
7714 					   pars.rule_id, pars.run_id, pars.line_num,pars.project_id,
7715                        pars.task_id,
7716 					   */
7717 					   l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
7718 					   l_get_proj.task_id,
7719                           G_creation_date, G_created_by, G_last_update_date,
7720                           G_last_updated_by, G_last_update_login ,
7721                              sum( decode (p_bal_type,
7722                     'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
7723                     'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
7724                                              +nvl( pta.i_tot_billable_raw_cost,0),
7725                     'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
7726                                              +nvl( pta.i_tot_billable_burdened_cost,0),
7727                     'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)+
7728                                              +nvl(pta.i_tot_burdened_cost,0),
7729                                   0 )),
7730                              pa_currency.round_currency_amt(sum( decode (p_bal_type,
7731                     'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
7732                     'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
7733                                              +nvl( pta.i_tot_billable_raw_cost,0),
7734                     'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
7735                                              +nvl( pta.i_tot_billable_burdened_cost,0),
7736                     'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)+
7737                                              +nvl(pta.i_tot_burdened_cost,0),
7738                                     0) * v_pool_percent ) )
7739                   from  pa_alloc_txn_accum_v pta  /* FP.M : Allocation Impact : pa_txn_accum pta */
7740                         /* pa_alloc_run_sources pars */ /* Loop thru pa_alloc_run_sources. Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id and Task_Id */
7741                  where  /* pars.rule_id       = p_rule_id
7742                    and  pars.run_id        = p_run_id
7743                    and  pars.exclude_flag  = 'N'
7744 				   and  pta.project_id     = pars.project_id
7745                    and  pta.task_id        = pars.task_id
7746 				   */   pta.project_id     = l_get_proj.project_id  /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id */
7747                    and  pta.task_id		   = l_get_proj.task_id  /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Task_Id */
7748                    and  pta.pa_period      = p_run_period
7749               group by
7750 						/* Bug 3749469
7751 					   pars.rule_id, pars.run_id, pars.line_num,pars.project_id,
7752                        pars.task_id,
7753 					   */
7754 					   l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
7755 					   l_get_proj.task_id,
7756                         G_creation_date, G_created_by, G_last_update_date,
7757                         G_last_updated_by, G_last_update_login ;
7758 				End Loop;
7759 			   end if ;
7760            else
7761 			  For l_get_proj in c_get_proj
7762 				Loop
7763 				  Insert into pa_alloc_run_source_det(
7764 						 rule_id, run_id, line_num, project_id, task_id,
7765 						 creation_date, created_by, last_update_date,
7766 						 last_updated_by, last_update_login,
7767 						 amount, eligible_amount)
7768 				  select /* Bug 3749469
7769 					   pars.rule_id, pars.run_id, pars.line_num,pars.project_id,
7770                        pars.task_id,
7771 					   */
7772 					   l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
7773 					   l_get_proj.task_id,
7774 						 G_creation_date, G_created_by, G_last_update_date,
7775 						 G_last_updated_by, G_last_update_login ,
7776 								 sum( decode (p_bal_type,
7777 						'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
7778 						'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
7779 												 +nvl( pta.i_tot_billable_raw_cost,0),
7780 						'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
7781 												 +nvl( pta.i_tot_billable_burdened_cost,0),
7782 						'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)+
7783 												 +nvl(pta.i_tot_burdened_cost,0),
7784 									  0 )),
7785 								 pa_currency.round_currency_amt(sum( decode (p_bal_type,
7786 						'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
7787 						'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
7788 												 +nvl( pta.i_tot_billable_raw_cost,0),
7789 						'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
7790 												 +nvl( pta.i_tot_billable_burdened_cost,0),
7791 						'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)+
7792 												 +nvl(pta.i_tot_burdened_cost,0),
7793 										0) * v_pool_percent ) )
7794 				   from  pa_alloc_txn_accum_v pta, /* FP.M : Allocation Impact : pa_txn_accum pta */
7795 						 pa_periods   pp ,
7796 						 pa_projects  P
7797 						 /* pa_alloc_run_sources pars */ /* Loop thru pa_alloc_run_sources. Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id and Task_Id */
7798 				  where  /* pars.rule_id       = p_rule_id
7799 					and  pars.run_id        = p_run_id
7800 					and  pars.exclude_flag  = 'N'
7801 					and  pars.project_id    = p.project_id
7802 					and  pta.project_id     = pars.project_id
7803 					and  pta.task_id        = pars.task_id
7804 						*/
7805 					     pta.project_id     = l_get_proj.project_id /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id */
7806 				    and  pta.task_id		= l_get_proj.task_id /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Task_Id */
7807 					and  p.project_id       = l_get_proj.project_id
7808 					and  pta.pa_period      = pp.period_name
7809 	--                                Removed check for Project start date bug 1063600
7810 	--              and  pp.end_date        >= p.start_date
7811 					and  pp.end_date        <= v_run_period_end_date
7812 			   group by /* Bug 3749469
7813 					   pars.rule_id, pars.run_id, pars.line_num,pars.project_id,
7814                        pars.task_id,
7815 					   */
7816 					   l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
7817 					   l_get_proj.task_id,
7818 						 G_creation_date, G_created_by, G_last_update_date,
7819 						 G_last_updated_by, G_last_update_login ;
7820 				End Loop;
7821 			End if ;
7822    End If ;
7823    IF P_DEBUG_MODE = 'Y' THEN
7824       pa_debug.write_file('cal_amounts_from_projects: ' || 'LOG', 'Amount type is '|| p_run_amount_type ||'--' || to_char(SQL%ROWCOUNT));
7825    END IF;
7826    pa_debug.G_err_stage := 'Getting pool amount from projects for run'||to_char(p_run_id) ;
7827    IF P_DEBUG_MODE = 'Y' THEN
7828       pa_debug.write_file('cal_amounts_from_projects: ' || 'LOG', pa_debug.G_Err_Stage);
7829    END IF;
7830    commit ;
7831 
7832 -- Start: added as part of the bug 13949738
7833 
7834   IF( p_run_amount_type = 'ITD') THEN
7835     v_period_year:= NULL;
7836     v_quarter:= NULL;
7837     v_period_num := NULL;
7838   ELSIF (p_run_amount_type = 'FYTD') THEN
7839     v_quarter:= NULL;
7840     v_period_num := NULL;
7841   ELSIF (p_run_amount_type = 'QTD') THEN
7842     v_period_num := NULL;
7843   END IF;
7844 
7845   FOR src_missing_cost_rec IN C_get_missing_source_amount
7846   LOOP
7847 
7848      open C_get_previous_run_amount(v_period_year,v_quarter,v_period_num,src_missing_cost_rec.project_id,src_missing_cost_rec.task_id);
7849      fetch C_get_previous_run_amount into v_previous_amount;
7850 	 close C_get_previous_run_amount;
7851 
7852 	 v_missing_amount := src_missing_cost_rec.eligible_amount - v_previous_amount;
7853 	    IF v_missing_amount <> 0 THEN
7854 
7855 		IF P_DEBUG_MODE = 'Y' THEN
7856          pa_debug.write_file('cal_amounts_from_projects: ' || 'LOG','src_missing_cost for project: '||src_missing_cost_rec.project_id ||
7857                     	   ' and task: '||src_missing_cost_rec.task_id ||' is :'|| to_char(v_missing_amount) );
7858         END IF;
7859 
7860  	    insert_missing_costs( p_run_id
7861                              ,'S'
7862                              ,src_missing_cost_rec.project_id
7863                              ,src_missing_cost_rec.task_id
7864                              ,v_missing_amount);
7865         END IF;
7866         v_total_missing_cost := v_total_missing_cost + v_missing_amount;
7867   END LOOP;
7868 
7869         UPDATE pa_alloc_runs
7870         SET Missing_source_proj_amt = v_total_missing_cost
7871         WHERE run_id = p_run_id;
7872 -- End: added as part of the bug 13949738
7873 
7874    OPEN C_get_pool_amount ;
7875    Fetch C_get_pool_amount into x_proj_pool_amount ;
7876    close C_get_pool_amount ;
7877 
7878    pa_debug.reset_err_stack;
7879 EXCEPTION
7880    WHEN OTHERS THEN
7881    RAISE;
7882 END cal_amounts_from_projects ;
7883 -- ============================================================
7884 -- get_relative_period_name
7885 -- ============================================================
7886 PROCEDURE get_relative_period_name( p_period_set_name      IN VARCHAR2,
7887                                     p_period_type          IN VARCHAR2,
7888                                     p_run_period_end_date  IN DATE,
7889                                     p_run_period           IN VARCHAR2,
7890                                     p_relative_period      IN NUMBER,
7891                                     x_rel_period_name     OUT NOCOPY VARCHAR2 )
7892 IS
7893    v_rel_period  NUMBER ;
7894    v_counter     NUMBER ;
7895    Cursor C_rel_period is
7896        select  period_name
7897          from  gl_periods glp
7898         where  glp.period_set_name  =  p_period_set_name
7899           and glp.period_type       =  p_period_type
7900           and glp.end_date         <=  p_run_period_end_date
7901           and glp.adjustment_period_flag <> 'Y' /* Added for Bug#2409474 */
7902      order by start_date desc ;
7903 BEGIN
7904      pa_debug.set_err_stack('get_relative_period_name') ;
7905      pa_debug.G_err_stage := 'Fetching the Relative period name' ;
7906      IF P_DEBUG_MODE = 'Y' THEN
7907         pa_debug.write_file('get_relative_period_name: ' || 'LOG', pa_debug.G_err_stage);
7908      END IF;
7909       v_rel_period := 1 + nvl(p_relative_period,0)* -1 ;
7910       v_counter    := 1 ;
7911      If v_rel_period > 0 then
7912         OPEN C_rel_period ;
7913         For V_counter in 1..v_rel_period LOOP
7914           FETCH C_rel_period INTO x_rel_period_name ;
7915           EXIT WHEN C_rel_period%NOTFOUND ;
7916         END LOOP ;
7917         If C_rel_period%NOTFOUND  then
7918            CLOSE C_rel_period ;
7919            alloc_errors(G_rule_id, G_alloc_run_id, 'R', 'E','PA_AL_NO_BASIS_RELATIVE_PERIOD',TRUE) ;
7920         End if ;
7921         CLOSE C_rel_period ;
7922       else
7923          x_rel_period_name := p_run_period;
7924       End if ;
7925         IF P_DEBUG_MODE = 'Y' THEN
7926            pa_debug.write_file('get_relative_period_name: ' || 'LOG', 'Relative Period is '||x_rel_period_name);
7927         END IF;
7928         pa_debug.reset_err_stack;
7929 EXCEPTION
7930    WHEN OTHERS THEN
7931      RAISE ;
7932 END get_relative_period_name ;
7933 -- ============================================================
7934 -- insert_alloc_run_basis_det
7935 -- ============================================================
7936 PROCEDURE insert_alloc_run_basis_det( p_rule_id          IN NUMBER
7937                                   , p_run_id             IN NUMBER
7938                                   , p_line_num           IN NUMBER
7939                                   , p_project_id         IN NUMBER
7940                                   , p_task_id            IN NUMBER
7941                                   , p_rlm_id             IN NUMBER
7942                                   , p_amount             IN NUMBER
7943                                   , p_basis_percent      IN NUMBER
7944                                   , p_line_percent       IN NUMBER
7945                                   , p_creation_date      IN DATE
7946                                   , p_created_by         IN NUMBER
7947                                   , p_last_update_date   IN DATE
7948                                   , p_last_updated_by    IN NUMBER
7949                                   , p_last_update_login  IN NUMBER)
7950 IS
7951 BEGIN
7952   pa_debug.set_err_stack('insert_alloc_run_basis_det') ;
7953   IF P_DEBUG_MODE = 'Y' THEN
7954      pa_debug.write_file('insert_alloc_run_basis_det: ' || 'insert Basis record for task '||to_char(p_task_id)||':'||to_char(p_amount));
7955      pa_debug.write_file('insert_alloc_run_basis_det: ' || 'LOG', pa_debug.G_Err_Stage);
7956   END IF;
7957   pa_debug.G_err_stage := 'INSERTING PA_ALLOC_RUN_BASIS_DET' ;
7958   INSERT INTO PA_ALLOC_RUN_BASIS_DET (
7959      RUN_ID
7960    , RULE_ID
7961    , LINE_NUM
7962    , PROJECT_ID
7963    , TASK_ID
7964    , RESOURCE_LIST_MEMBER_ID
7965    , AMOUNT
7966    , BASIS_PERCENT
7967    , LINE_PERCENT
7968    , CREATION_DATE
7969    , CREATED_BY
7970    , LAST_UPDATE_DATE
7971    , LAST_UPDATED_BY
7972    , LAST_UPDATE_LOGIN )
7973   VALUES (
7974       p_run_id
7975     , p_rule_id
7976     , p_line_num
7977     , p_project_id
7978     , p_task_id
7979     , p_rlm_id
7980     , p_amount
7981     , p_basis_percent
7982     , p_line_percent
7983     , p_creation_date
7984     , p_created_by
7985     , p_last_update_date
7986     , p_last_updated_by
7987     , p_last_update_login ) ;
7988    /*  restore the old stack */
7989     pa_debug.reset_err_stack;
7990 EXCEPTION
7991    WHEN OTHERS THEN
7992    pa_debug.reset_err_stack;
7993    RAISE;
7994 END insert_alloc_run_basis_det ;
7995 -- ============================================================
7996 -- cal_proj_basis_amounts
7997 -- ============================================================
7998 PROCEDURE cal_proj_basis_amounts(p_rule_id          IN NUMBER,
7999                                  p_run_id           IN NUMBER,
8000                                  p_run_period_type  IN VARCHAR2,
8001                                  p_run_period       IN VARCHAR2,
8002                                  p_basis_method     IN OUT NOCOPY VARCHAR2, /* for bug 2182563 */
8003                                  p_basis_amt_type   IN VARCHAR2,
8004                                  P_basis_bal_type   IN VARCHAR2,
8005                                  P_basis_rel_period IN NUMBER,
8006                                  p_basis_category   IN VARCHAR2,
8007                                  p_basis_RL_id      IN NUMBER  ,
8008 								 p_budget_type_code IN VARCHAR2,
8009                          		 x_proj_pool_amount OUT NOCOPY NUMBER ,
8010 								 /* FP.M : Allocation Impact : Bug# 3512552 */
8011                                  p_basis_resource_struct_type in Varchar2 ,
8012 								 p_basis_rbs_version_id in number
8013 								 )
8014 IS
8015 /*---------- Logic--------------------------------------------------------------
8016  If basis_method  is Client extension
8017     get the basis amounts for target project and tasks using extension.
8018  Else
8019     Get  the  Period Year and Quarter Num values for the given Relative_period.
8020     If basis_resource_lists is not null then
8021        Populate  Include_RLM_tbl.
8022        For each resource list member in the Include_RLM_tbl Loop
8023           For Each project and task in pa_alloc_run_targets table Loop
8024         If Basis_category is 'ACTUALS' then
8025            Get the  basis amount using get_alloc_amounts( )
8026             Else
8027           Get the basis amount from get_budget_amounts()
8028             End if ;
8029           End Loop ;
8030        End Loop ;
8031      Else
8032       If basis_category is ACTUALS then
8033            For each project and task in  pa_alloc_run_targets
8034               find the basis amount at project and task level from pa_txn_accum
8035           and insert that into pa_alloc_run_basis_det.
8036          Else
8037            For each project and task in  pa_alloc_run_targets
8038               find the basis amount at project and task level from
8039               pa_base_budget_by_pa_period_v
8040               and insert that into pa_alloc_run_basis_det.
8041          End if.
8042      End if;
8043   End if
8044   Determine the basis_percent ;
8045 ---------------Logic ----------------------------------------------------------------- */
8046     v_period_type           VARCHAR2(15)  ;
8047     v_period_set_name       VARCHAR2 (15) ;
8048     v_period_year           NUMBER    ;
8049     v_quarter               NUMBER    ;
8050     v_period_num            NUMBER    ;
8051     v_run_period_end_date   DATE      ;
8052     v_amttype_start_date    DATE      ;
8053     v_resource_list_id      NUMBER    ;
8054     v_rlm_id                NUMBER ;
8055     v_rlm_percent           NUMBER ;
8056 --  v_basis_rlm_tab         SRC_RLM_TABTYPE ; /* after fix for 2211234 this table is not required */
8057     v_line_num              NUMBER ;
8058     v_amount                NUMBER ;
8059     v_pool_amount           NUMBER ;
8060 --  v_pool_percent          NUMBER ;
8061     v_basis_method         VARCHAR2(2) ;
8062     v_basis_amt_type       VARCHAR2(4) ;
8063     v_basis_bal_type       VARCHAR2(15) ;
8064     v_basis_rel_period     NUMBER     ;
8065     v_rel_period_name      VARCHAR2(15) ;
8066     v_rel_period_end_date  DATE      ;
8067    v_tot_basis_amt        NUMBER  ;
8068    v_max_basis_amt        NUMBER  ;    /* added for bug 1900331 */
8069    v_tot_basis_rec        NUMBER  ;    /* added for bug 1900331 */
8070    v_line_basis_amt       NUMBER  ;
8071    v_line_percent         NUMBER  ;
8072    v_line_max_amt         NUMBER  ;   /* added for bug 1900331 */
8073    v_line_count           NUMBER  ;   /* added for bug 1900331 */
8074    v_commit_count         NUMBER  ;   /* added for bug 2182563 */
8075    v_sum_tgt_pct          NUMBER ;
8076    v_status               NUMBER := NULL;
8077   v_err_message           VARCHAR2(250);
8078     cursor C_run_targets is
8079            Select  line_num, project_id , task_id, line_percent
8080              from  pa_alloc_run_targets
8081             where  rule_id   =  p_rule_id
8082              and   run_id    =  p_run_id
8083              and   exclude_flag  <> 'Y' ;
8084     cursor C_tot_basis_amt is
8085         select nvl(sum(nvl(amount,0)),0), nvl(max(nvl(amount,0)),0), count(1) /* 1900331 */
8086          from pa_alloc_run_basis_det
8087             where run_id = p_run_id ;
8088     cursor C_line_basis_amt is
8089         select line_num, line_percent, nvl(sum(nvl(amount,0)),0),  nvl(max(nvl(amount,0)),0), count(1) /* 1900331 */
8090          from pa_alloc_run_basis_det
8091             where run_id = p_run_id
8092         group by line_num, line_percent ;
8093     cursor C_tgt_line_pct is
8094         select sum((nvl(line_percent,0)))
8095           from pa_alloc_run_basis_det
8096          where run_id = p_run_id ;
8097 	cursor c_get_proj is
8098 		Select RUN_ID                 ,
8099 			   RULE_ID                ,
8100 			   LINE_NUM               ,
8101 			   PROJECT_ID             ,
8102 			   TASK_ID                ,
8103 			   EXCLUDE_FLAG           ,
8104 			   LINE_PERCENT           ,
8105 			   BUDGET_VERSION_ID
8106 		  From Pa_Alloc_Run_Targets
8107 		 Where rule_id = p_rule_id
8108 		   And run_id  = p_run_id
8109 		   And Nvl(exclude_flag,'N') = 'N' ;
8110 BEGIN
8111     v_basis_method     := p_basis_method ;
8112     v_resource_list_id := p_basis_RL_id  ;
8113     v_sum_tgt_pct      := 0 ;
8114        pa_debug.set_err_stack('Cal_proj_basis_amounts') ;
8115     If p_basis_method = 'C' then
8116        pa_debug.G_err_stage := 'Call basis client extension';
8117        IF P_DEBUG_MODE = 'Y' THEN
8118           pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG', pa_debug.G_Err_Stage);
8119        END IF;
8120        For run_targets_rec in C_run_targets LOOP
8121           pa_client_extn_alloc.basis_extn(p_rule_id, run_targets_rec.project_id,
8122                                     run_targets_rec.task_id, v_amount,v_status,v_err_message) ;
8123           IF nvl(v_status,0) <> 0 THEN
8124               v_err_message:=nvl(v_err_message,'PA_AL_CE_FAILED');
8125               alloc_errors(p_rule_id,p_run_id,'B','E',v_err_message,TRUE);
8126           END IF;
8127           insert_alloc_run_basis_det(
8128                     p_rule_id, p_run_id, run_targets_rec.line_num,
8129                     run_targets_rec.project_id, run_targets_rec.task_id,
8130                     v_rlm_id, nvl(v_amount,0), NULL, run_targets_rec.line_percent,
8131                     G_creation_date, G_created_by, G_last_update_date,
8132                     G_last_updated_by, G_last_update_login) ;
8133        End Loop ;
8134        Open C_tgt_line_pct ;
8135        Fetch C_tgt_line_pct into v_sum_tgt_pct  ;
8136        Close C_tgt_line_pct;
8137     elsif p_basis_method  in ( 'P', 'FP') then
8138        pa_debug.G_err_stage := 'Call get_fiscalyear_quarter';
8139        IF P_DEBUG_MODE = 'Y' THEN
8140           pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG', pa_debug.G_Err_Stage);
8141        END IF;
8142        get_fiscalyear_quarter(p_run_period_type, p_run_period, v_period_type,
8143                               v_period_set_name,
8144                               v_period_year, v_quarter, v_period_num,
8145                               v_run_period_end_date) ;
8146        pa_debug.G_err_stage := 'Calling get_relative_period_name' ;
8147        IF P_DEBUG_MODE = 'Y' THEN
8148           pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG', pa_debug.G_Err_Stage);
8149        END IF;
8150        get_relative_period_name(v_period_set_name, v_period_type, v_run_period_end_date,
8151                                 p_run_period, p_basis_rel_period, v_rel_period_name ) ;
8152        pa_debug.G_err_stage := 'calling get_fiscalyear_quarter for relative period' ;
8153        IF P_DEBUG_MODE = 'Y' THEN
8154           pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG', pa_debug.G_Err_Stage);
8155        END IF;
8156        get_fiscalyear_quarter(p_run_period_type, v_rel_period_name, v_period_type,
8157                               v_period_set_name,
8158                               v_period_year, v_quarter, v_period_num,
8159                               v_rel_period_end_date) ;
8160        pa_debug.G_err_stage := 'calling get_amttype_start_date';
8161        IF P_DEBUG_MODE = 'Y' THEN
8162           pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG', pa_debug.G_Err_Stage);
8163        END IF;
8164        get_amttype_start_date( p_basis_amt_type, v_period_type,
8165                                v_period_set_name, v_rel_period_end_date,
8166                                v_quarter, v_period_year, p_run_period, v_amttype_start_date) ;
8167        IF P_DEBUG_MODE = 'Y' THEN
8168           pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG','amttype start date = '|| to_char(v_amttype_start_date));
8169           pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG','rel_period_end_date = '|| to_char(v_rel_period_end_date));
8170        END IF;
8171        If v_resource_list_id is NOT NULL then
8172           pa_debug.G_err_stage := 'Populating the Resource List Member array for basis  ' ;
8173           IF P_DEBUG_MODE = 'Y' THEN
8174              pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG', pa_debug.G_Err_Stage);
8175           END IF;
8176           /* Bug 2211234 - added p_run_id.
8177                          - removed v_basis_rlm_tab
8178           this procedure will now insert into pa_alloc_run_resources_det */
8179           populate_RLM_table( p_rule_id,
8180 							  p_run_id,
8181 							  'B',
8182 							  v_resource_list_id,
8183 							  p_basis_resource_struct_type ,
8184 						      p_basis_rbs_version_id ,
8185 							  p_basis_category
8186 							  ) ;
8187           pa_debug.G_err_stage := 'Processing project/Tasks to get resource level amounts';
8188           IF P_DEBUG_MODE = 'Y' THEN
8189              pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG', pa_debug.G_Err_Stage);
8190           END IF;
8191 /*   major changes done for actuals for performance improvements. No more a call to get_alloc_amount
8192      is made and bulk inserts are done in new procedure added insert_alloc_basis_resource
8193      These changes are not done for budgets as in budgets the volume of data should not be that high.
8194 */
8195           IF p_basis_category = 'A' THEN
8196                    /* Currenly we insert all project/tasks into pa_alloc_run_targets.
8197                       These do not get converted into final txns in case there are no basis existing
8198                       for these targets in summarization. We want to intelligently delete those data
8199                       from pa_alloc_run_targets table which do not contain any basis
8200                       amount. */
8201                    clean_up_targets_for_actuals(p_run_id               =>   p_run_id
8202                                                ,p_rule_id              =>   p_rule_id
8203                                                ,p_amt_type             =>   p_basis_amt_type
8204                                                ,p_run_period_type      =>   p_run_period_type
8205                                                ,p_period               =>   v_rel_period_name
8206                                                ,p_run_period_end_date  =>   v_rel_period_end_date
8207                                                ,p_amttype_start_date   =>   v_amttype_start_date
8208                                                ,p_basis_method         =>   v_basis_method
8209 											   );
8210                    /* if basis method is not switched by the clean up targets process */
8211                    IF p_basis_method <> 'S' THEN
8212                       insert_alloc_basis_resource( p_run_id               =>   p_run_id
8213                                                   ,p_rule_id              =>   p_rule_id
8214                                                   ,p_resource_list_id     =>   v_resource_list_id
8215                                                   ,p_amt_type             =>   p_basis_amt_type
8216                                                   ,p_bal_type             =>   p_basis_bal_type
8217                                                   ,p_run_period_type      =>   p_run_period_type
8218                                                   ,p_period               =>   v_rel_period_name
8219                                                   ,p_run_period_end_date  =>   v_rel_period_end_date
8220                                                   ,p_amttype_start_date   =>   v_amttype_start_date
8221 												  /* FP.M : Allocation Impact : Bug# 3512552 */
8222 				                                  ,p_resource_struct_type => p_basis_resource_struct_type
8223 												  ,p_rbs_version_id       => p_basis_rbs_version_id
8224 												  );
8225                    END IF;
8226           ELSE /* for budgets */
8227           /* added for 2211234 */
8228                     insert_budget_basis_resource(p_run_id              =>   p_run_id
8229                                                 ,p_rule_id             =>   p_rule_id
8230                                                 ,p_run_period_type     =>   p_run_period_type
8231                                                 ,p_bal_type            =>   p_basis_bal_type
8232                                                 ,p_budget_type_code    =>   p_budget_type_code
8233                                                 ,p_start_date          =>   v_amttype_start_date
8234                                                 ,p_end_date            =>   v_rel_period_end_date
8235 												,p_basis_resource_struct_Type => p_basis_resource_struct_type);
8236           /***2211234 - insert_budget_basis_resource will take care of this.
8237            ***    FOR  I in 1.. v_basis_rlm_tab.count   LOOP
8238            ***         v_rlm_id            :=  v_basis_rlm_tab (I).resource_list_member_id ;
8239            ***         pa_debug.write_file('LOG','Resource list member : '|| to_char(v_rlm_id) );
8240            ***
8241            ***         FOR run_targets_rec in C_run_targets  LOOP
8242            ***             get_budget_amounts( p_run_period_type, p_basis_bal_type,
8243            ***                                   run_targets_rec.project_id,
8244            ***                                  run_targets_rec.task_id,
8245            ***                                  v_resource_list_id, v_rlm_id,
8246            ***                                  p_budget_type_code, v_amttype_start_date,
8247            ***                                  v_rel_period_end_date, v_amount) ;
8248            ***             pa_debug.write_file('LOG','get_budget_amounts  '||'project: '
8249            ***                   ||to_char( run_targets_rec.project_id)||'
8250            ***                   task: '|| to_char( run_targets_rec.task_id)|| '
8251            ***                   Amt:'|| to_char(v_amount) );
8252            ***
8253            ***             IF (p_basis_method in ('P','FP') and nvl(v_amount,0) <> 0) then
8254            ***                     insert_alloc_run_basis_det(
8255            ***                         p_rule_id, p_run_id, run_targets_rec.line_num,
8256            ***                         run_targets_rec.project_id, run_targets_rec.task_id,
8257            ***                         v_rlm_id, nvl(v_amount,0), NULL, run_targets_rec.line_percent,
8258            ***                         G_creation_date, G_created_by, G_last_update_date,
8259            ***                         G_last_updated_by, G_last_update_login) ;
8260            ***             END IF;
8261            ***         END LOOP ;
8262            ***    END LOOP;
8263            ***/
8264           END IF ;
8265           COMMIT;
8266        Else
8267           If p_basis_category = 'A' then
8268              pa_debug.G_err_stage := 'Processing project/Tasks to get basis amounts' ;
8269              IF P_DEBUG_MODE = 'Y' THEN
8270                 pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG', pa_debug.G_Err_Stage);
8271              END IF;
8272              If  p_basis_amt_type in ( 'FYTD','QTD')  then
8273                  IF P_DEBUG_MODE = 'Y' THEN
8274                     pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG','p_basis_amt_type' ||p_basis_amt_type);
8275                  END IF;
8276 				For l_get_proj in c_get_proj
8277 				Loop
8278 							Insert into pa_alloc_run_basis_det (
8279 							  rule_id, run_id, line_num, project_id, task_id,
8280 							  line_percent, creation_date, created_by,
8281 							  last_update_date, last_updated_by,
8282 							  last_update_login, amount)
8283 							select  /* Bug 3749469
8284 									part.rule_id, part.run_id,  part.line_num, part.project_id,
8285 									part.task_id, part.line_percent,
8286 									*/
8287 									l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
8288 									l_get_proj.task_id, l_get_proj.line_percent,
8289 									G_creation_date, G_created_by,
8290 									G_last_update_date, G_last_updated_by, G_last_update_login ,
8291 									sum( decode (p_basis_bal_type,
8292 								'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
8293 								'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
8294 														 +nvl( pta.i_tot_billable_raw_cost,0),
8295 								'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
8296 														 +nvl( pta.i_tot_billable_burdened_cost,0),
8297 								'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
8298 														 +nvl(pta.i_tot_burdened_cost,0),
8299 								'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
8300 														 +nvl(pta.i_tot_labor_hours,0),
8301 								'TOT_QUANTITY', nvl(pta.tot_quantity,0)
8302 														 +nvl(pta.i_tot_quantity,0),
8303 											  0 )) AMOUNT
8304 							  from  pa_alloc_txn_accum_v  pta, /* FP.M : Allocation Impact : pa_txn_accum pta, */
8305 			/* Commenting out pa_periods and using gl_period_statuses instead for bug 2757875 */
8306 			--                      pa_periods   pp ,
8307 									gl_period_statuses   gl,
8308 									pa_implementations imp
8309 									/* pa_alloc_run_targets part */ /* Loop thru pa_alloc_run_sources. Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id and Task_Id */
8310 							 where  /* Bug 3749469 part.rule_id       = p_rule_id
8311 							   and  part.run_id        = p_run_id
8312 							   and  part.exclude_flag  = 'N'
8313 							   and  pta.project_id     = part.project_id
8314 							   and  pta.task_id        = part.task_id
8315 							   */												/* added outer join for bug 1900331 */
8316 																			  /* Removed Outer join for bug 2182563 */
8317 							        pta.project_id     = l_get_proj.project_id /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id */
8318 							   and  pta.task_id		   = l_get_proj.task_id /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id */
8319 							   and  pta.gl_period	   = gl.period_name
8320 							   and  gl.set_of_books_id  = imp.set_of_books_id
8321 							   and  gl.application_id   = pa_period_process_pkg.application_id
8322 							   and  gl.adjustment_period_flag = 'N'
8323 							   and  gl.closing_status in ('C','F','O','P')
8324 							   and  gl.end_date	  >= v_amttype_start_date
8325 							   and  gl.end_date   <= v_rel_period_end_date
8326 			--                 and  nvl(pta.pa_period,pp.period_name)   = pp.period_name /* bug 2121598 */
8327 			--                 and  pp.end_date        >= v_amttype_start_date
8328 			--                 and  pp.end_date        <= v_rel_period_end_date
8329 						  group by
8330 								    /* Bug 3749469
8331 									part.rule_id, part.run_id,  part.line_num, part.project_id,
8332 									part.task_id, part.line_percent,
8333 									*/
8334 									l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
8335 									l_get_proj.task_id, l_get_proj.line_percent,
8336 									G_creation_date, G_created_by,
8337 									G_last_update_date,
8338 									G_last_updated_by, G_last_update_login ;
8339 				End Loop;
8340              elsif p_basis_amt_type = 'PTD' then
8341                  If p_run_period_type = 'GL' then
8342 			   For l_get_proj in c_get_proj
8343 				Loop
8344 						   Insert into pa_alloc_run_basis_det (
8345 							  rule_id, run_id, line_num, project_id, task_id,
8346 							  line_percent, creation_date, created_by,
8347 							  last_update_date, last_updated_by,
8348 							  last_update_login, amount)
8349 						   select  /* Bug 3749469
8350 									part.rule_id, part.run_id,  part.line_num, part.project_id,
8351 									part.task_id, part.line_percent,
8352 									*/
8353 									l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
8354 									l_get_proj.task_id, l_get_proj.line_percent,
8355 								   G_creation_date, G_created_by,
8356 								   G_last_update_date, G_last_updated_by, G_last_update_login ,
8357 								   sum( decode (p_basis_bal_type,
8358 							'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
8359 							'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
8360 													 +nvl( pta.i_tot_billable_raw_cost,0),
8361 							'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
8362 													 +nvl( pta.i_tot_billable_burdened_cost,0),
8363 							'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
8364 													 +nvl(pta.i_tot_burdened_cost,0),
8365 							'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
8366 													 +nvl(pta.i_tot_labor_hours,0),
8367 							'TOT_QUANTITY', nvl(pta.tot_quantity,0)
8368 													 +nvl(pta.i_tot_quantity,0),
8369 										  0 )) AMOUNT
8370 							 from  pa_alloc_txn_accum_v  pta /* FP.M : Allocation Impact : pa_txn_accum pta, */
8371 		-- Commented out pa_periods. Used the gl_periods column in pa_txn_accum table
8372 		--                         pa_periods   pp
8373 								   /* pa_alloc_run_targets part */ /* Loop thru pa_alloc_run_sources. Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id and Task_Id */
8374 							where  /* Bug 3749469
8375 							       part.rule_id       = p_rule_id
8376 							  and  part.run_id        = p_run_id
8377 							  and  part.exclude_flag  = 'N'
8378 							  and  pta.project_id     = part.project_id
8379 							  and  pta.task_id        = part.task_id
8380 							  */
8381 							  /* added outer join for bug 1900331 */
8382 																		  /* Removed Outer join for bug 2182563 */
8383 							       pta.project_id     = l_get_proj.project_id /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id */
8384 							  and  pta.task_id        = l_get_proj.task_id /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Task_Id */
8385 							  and  pta.gl_period      = v_rel_period_name
8386 		--                    and  pta.pa_period      = pp.period_name
8387 		--                    and  pp.gl_period_name  = v_rel_period_name
8388 					 group by  /* Bug 3749469
8389 									part.rule_id, part.run_id,  part.line_num, part.project_id,
8390 									part.task_id, part.line_percent,
8391 								*/
8392 									l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
8393 									l_get_proj.task_id, l_get_proj.line_percent,
8394 							   G_creation_date, G_created_by,
8395 							   G_last_update_date,
8396 							   G_last_updated_by, G_last_update_login ;
8397 				  End Loop;
8398 				 Elsif p_run_period_type = 'PA' then
8399 				For l_get_proj in c_get_proj
8400 				Loop
8401 						   Insert into pa_alloc_run_basis_det (
8402 							   rule_id, run_id, line_num, project_id, task_id,
8403 							   line_percent, creation_date, created_by,
8404 							   last_update_date, last_updated_by,
8405 							   last_update_login, amount)
8406 						   select  /* Bug 3749469
8407 									part.rule_id, part.run_id,  part.line_num, part.project_id,
8408 									part.task_id, part.line_percent,
8409 								*/
8410 									l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
8411 									l_get_proj.task_id, l_get_proj.line_percent,
8412 								  G_creation_date, G_created_by,
8413 								  G_last_update_date, G_last_updated_by,G_last_update_login,
8414 								  sum( decode (p_basis_bal_type,
8415 						'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
8416 						'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
8417 												 +nvl( pta.i_tot_billable_raw_cost,0),
8418 						'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
8419 												 +nvl( pta.i_tot_billable_burdened_cost,0),
8420 						'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
8421 												 +nvl(pta.i_tot_burdened_cost,0),
8422 						'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
8423 												 +nvl(pta.i_tot_labor_hours,0),
8424 						'TOT_QUANTITY', nvl(pta.tot_quantity,0)
8425 												 +nvl(pta.i_tot_quantity,0),
8426 									  0 )) AMOUNT
8427 							from  pa_alloc_txn_accum_v  pta /* FP.M : Allocation Impact : pa_txn_accum pta, */
8428 								  /* pa_alloc_run_targets part */ /* Loop thru pa_alloc_run_sources. Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id and Task_Id */
8429 						   where  /* Bug 3749469  part.rule_id       = p_rule_id
8430 							 and  part.run_id        = p_run_id
8431 							 and  part.exclude_flag  = 'N'
8432 							 and  pta.project_id     = part.project_id
8433 							 and  pta.task_id        = part.task_id
8434 							  */ 							 /* added outer join for bug 1900331 */
8435 																	  /* Removed Outer join for bug 2182563 */
8436 							      pta.project_id     = l_get_proj.project_id /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id */
8437 							 and  pta.task_id        = l_get_proj.task_id /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Task_Id */
8438 							 and  pta.pa_period      = v_rel_period_name
8439 						group by   /* Bug 3749469
8440 									part.rule_id, part.run_id,  part.line_num, part.project_id,
8441 									part.task_id, part.line_percent,
8442 								*/
8443 									l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
8444 									l_get_proj.task_id, l_get_proj.line_percent,
8445 								  G_creation_date, G_created_by,
8446 								  G_last_update_date,
8447 								  G_last_updated_by, G_last_update_login ;
8448 				 End Loop;
8449                 end if ;
8450 			 Else   -- process for ITD amounts
8451 				For l_get_proj in c_get_proj
8452 				Loop
8453 						Insert into pa_alloc_run_basis_det (
8454 							 rule_id, run_id, line_num, project_id, task_id,
8455 							 line_percent, creation_date, created_by,
8456 							 last_update_date, last_updated_by,
8457 							 last_update_login, amount)
8458 						select /*+ORDERED*/ -- added ORDERED hint for bug 2751178
8459 							    /* Bug 3749469
8460 									part.rule_id, part.run_id,  part.line_num, part.project_id,
8461 									part.task_id, part.line_percent,
8462 								*/
8463 								l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
8464 								l_get_proj.task_id, l_get_proj.line_percent,
8465 							   G_creation_date, G_created_by,
8466 							   G_last_update_date, G_last_updated_by, G_last_update_login ,
8467 							   sum( decode (p_basis_bal_type,
8468 							'TOT_RAW_COST', nvl(pta.tot_raw_cost,0) +nvl( pta.i_tot_raw_cost,0),
8469 							'TOT_BILLABLE_RAW_COST', nvl(pta.tot_billable_raw_cost,0)
8470 													 +nvl( pta.i_tot_billable_raw_cost,0),
8471 							'TOT_BILLABLE_BURDENED_COST', nvl(pta.tot_billable_burdened_cost,0)
8472 													 +nvl( pta.i_tot_billable_burdened_cost,0),
8473 							'TOT_BURDENED_COST', nvl(pta.tot_burdened_cost,0)
8474 													 +nvl(pta.i_tot_burdened_cost,0),
8475 							'TOT_LABOR_HOURS', nvl(pta.tot_labor_hours,0)
8476 													 +nvl(pta.i_tot_labor_hours,0),
8477 							'TOT_QUANTITY', nvl(pta.tot_quantity,0)
8478 													 +nvl(pta.i_tot_quantity,0),
8479 										  0 )) AMOUNT
8480 				/* Rearranged the tables in the FROM clause and commented out pa_projects_all for bug 2751178 */
8481 						 from  /* pa_alloc_run_targets part, */ /* Loop thru pa_alloc_run_sources. Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id and Task_Id */
8482 							   pa_alloc_txn_accum_v  pta, /* FP.M : Allocation Impact : pa_txn_accum pta, */
8483 							   pa_periods  pp
8484 		--                     pa_projects_all   p  ,
8485 						where  /* Bug 3749469
8486 							   part.rule_id       = p_rule_id
8487 						  and  part.run_id        = p_run_id
8488 						  and  part.exclude_flag  = 'N'
8489 		--                and  part.project_id    = p.project_id  --- Commented for bug 2751178
8490 		                  and  pta.project_id     = part.project_id
8491 						  and  pta.task_id        = part.task_id
8492 						      */
8493 						    /* added for bug 1900331 */
8494 																   /* Removed Outer join for bug 2182563 */
8495 						       pta.project_id     = l_get_proj.project_id /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id */
8496 					      and  pta.task_id		  = l_get_proj.task_id /* Bug 3749469 : Performance with pa_alloc_txn_accum_v. Filter for Project_Id */
8497 					/* Removed nvl for bug 2751178 */
8498 		--                and  nvl(pta.pa_period,pp.period_name)   = pp.period_name /* bug 2121598 */
8499 				  and  pta.pa_period = pp.period_name
8500 		--                                Removed check for Project start date bug 1063600
8501 		--                and  pp.end_date        >= p.start_date
8502 						  and  pp.end_date        <= v_rel_period_end_date
8503 					 group by  /* Bug 3749469
8504 									part.rule_id, part.run_id,  part.line_num, part.project_id,
8505 									part.task_id, part.line_percent,
8506 							   */
8507 							   l_get_proj.rule_id, l_get_proj.run_id,  l_get_proj.line_num, l_get_proj.project_id,
8508 							   l_get_proj.task_id, l_get_proj.line_percent,
8509 							   G_creation_date, G_created_by,
8510 							   G_last_update_date,
8511 							   G_last_updated_by, G_last_update_login ;
8512 				End Loop;
8513              End if ;
8514           Else -- Processing the Budget amounts ...
8515        IF P_DEBUG_MODE = 'Y' THEN
8516           pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG','Inserting in basis_det from budgets');
8517        END IF;
8518              if p_basis_amt_type  = 'ITD' then
8519                 Insert into pa_alloc_run_basis_det(
8520                        rule_id, run_id, line_num, project_id, task_id,
8521                        line_percent, creation_date, created_by, last_update_date,
8522                        last_updated_by, last_update_login, amount)
8523                 select  part.rule_id, part.run_id,  part.line_num,
8524                         part.project_id, part.task_id,
8525                         part.line_percent, G_creation_date,
8526                         G_created_by, G_last_update_date,
8527                         G_last_updated_by, G_last_update_login ,
8528                         sum( decode (p_basis_bal_type,
8529                              'BASE_RAW_COST',       nvl(pfpp.raw_cost,0),
8530                              'BASE_BURDENED_COST',  nvl(pfpp.burdened_cost,0),
8531                              'BASE_QUANTITY',       nvl(pfpp.quantity,0),
8532                              'BASE_LABOR_QUANTITY', nvl(pfpp.labor_quantity,0),
8533                              0
8534                              ))
8535           /***    from  pa_base_budget_by_pa_period_v pbpp, *** commented bug 2619977 */
8536                   from  pa_base_finplan_by_pa_period_v pfpp, /* added bug 2619977 */
8537                         pa_projects_all   p  ,
8538                         pa_alloc_run_targets part
8539                  where  part.rule_id            = p_rule_id
8540                    and  part.run_id             = p_run_id
8541                    and  part.exclude_flag       = 'N'
8542                    and  part.project_id         = p.project_id
8543                    and  pfpp.project_id         = part.project_id
8544                    and  pfpp.task_id            = part.task_id
8545                    and  pfpp.budget_version_id  = part.budget_version_id /* added bug 2619977 */
8546          /***      and  pbpp.budget_type_code   = p_budget_type_code  *** commented bug 2619977 */
8547 --                                Removed check for Project start date bug 1063600
8548 --                 and  pfpp.Period_start_date >= p.start_date
8549                    and  pfpp.period_end_date   <= v_rel_period_end_date
8550               group by  part.rule_id, part.run_id, part.line_num,
8551                         part.project_id, part.task_id,
8552                         part.line_percent, G_creation_date, G_created_by,
8553                         G_last_update_date,
8554                         G_last_updated_by, G_last_update_login ;
8555              elsif p_basis_amt_type  in ('FYTD','QTD') then /* Added bug 2757875 */
8556                 Insert into pa_alloc_run_basis_det(
8557                        rule_id, run_id, line_num, project_id, task_id,
8558                        line_percent, creation_date, created_by, last_update_date,
8559                        last_updated_by, last_update_login, amount)
8560                 select  part.rule_id, part.run_id,  part.line_num,
8561                         part.project_id, part.task_id,
8562                         part.line_percent, G_creation_date,
8563                         G_created_by, G_last_update_date,
8564                         G_last_updated_by, G_last_update_login ,
8565                         sum( decode (p_basis_bal_type,
8566                              'BASE_RAW_COST',       nvl(pfpp.raw_cost,0),
8567                              'BASE_BURDENED_COST',  nvl(pfpp.burdened_cost,0),
8568                              'BASE_QUANTITY',       nvl(pfpp.quantity,0),
8569                              'BASE_LABOR_QUANTITY', nvl(pfpp.labor_quantity,0),
8570                              0
8571                              ))
8572           /***    from  pa_base_budget_by_pa_period_v pbpp, *** commented bug 2619977 */
8573           /****   from  pa_base_finplan_by_pa_period_v pfpp,  added bug 2619977. **** Commented bug 2757875*/
8574 		        from  pa_base_finplan_by_gl_period_v pfpp, /* Added bug 2757875 */
8575                         pa_alloc_run_targets part
8576                  where  part.rule_id            = p_rule_id
8577                    and  part.run_id             = p_run_id
8578                    and  part.exclude_flag       = 'N'
8579                    and  pfpp.project_id         = part.project_id
8580                    and  pfpp.task_id            = part.task_id
8581                    and  pfpp.budget_version_id  = part.budget_version_id /* added bug 2619977 */
8582          /***      and  pbpp.budget_type_code   = p_budget_type_code  *** commented bug 2619977 */
8583          /****     and  pfpp.Period_start_date >= v_amttype_start_date *** Commented bug 2757875 */
8584 	           and  pfpp.period_end_date   >= v_amttype_start_date /* Added bug 2757875 */
8585                    and  pfpp.period_end_date   <= v_rel_period_end_date
8586               group by  part.rule_id, part.run_id,  part.line_num,
8587                         part.project_id, part.task_id,
8588                         part.line_percent, G_creation_date, G_created_by,
8589                         G_last_update_date,
8590                         G_last_updated_by, G_last_update_login ;
8591 /* Added the following code for bug 2757875 */
8592 		   else -- process for PTD amounts
8593 		      If p_run_period_type = 'GL' then
8594 			    Insert into pa_alloc_run_basis_det(
8595                        rule_id, run_id, line_num, project_id, task_id,
8596                        line_percent, creation_date, created_by, last_update_date,
8597                        last_updated_by, last_update_login, amount)
8598                 select  part.rule_id, part.run_id,  part.line_num,
8599                         part.project_id, part.task_id,
8600                         part.line_percent, G_creation_date,
8601                         G_created_by, G_last_update_date,
8602                         G_last_updated_by, G_last_update_login ,
8603                         sum( decode (p_basis_bal_type,
8604                              'BASE_RAW_COST',       nvl(pfgp.raw_cost,0),
8605                              'BASE_BURDENED_COST',  nvl(pfgp.burdened_cost,0),
8606                              'BASE_QUANTITY',       nvl(pfgp.quantity,0),
8607                              'BASE_LABOR_QUANTITY', nvl(pfgp.labor_quantity,0),
8608                              0
8609                              ))
8610                   from  pa_base_finplan_by_gl_period_v pfgp,
8611                         pa_alloc_run_targets part
8612                  where  part.rule_id            = p_rule_id
8613                    and  part.run_id             = p_run_id
8614                    and  part.exclude_flag       = 'N'
8615                    and  pfgp.project_id         = part.project_id
8616                    and  pfgp.task_id            = part.task_id
8617                    and  pfgp.budget_version_id  = part.budget_version_id
8618                    and  pfgp.gl_period_name	   = v_rel_period_name
8619               group by  part.rule_id, part.run_id,  part.line_num,
8620                         part.project_id, part.task_id,
8621                         part.line_percent, G_creation_date, G_created_by,
8622                         G_last_update_date,
8623                         G_last_updated_by, G_last_update_login ;
8624 		      elsif p_run_period_type = 'PA' then
8625 			       Insert into pa_alloc_run_basis_det(
8626                        rule_id, run_id, line_num, project_id, task_id,
8627                        line_percent, creation_date, created_by, last_update_date,
8628                        last_updated_by, last_update_login, amount)
8629                 select  part.rule_id, part.run_id,  part.line_num,
8630                         part.project_id, part.task_id,
8631                         part.line_percent, G_creation_date,
8632                         G_created_by, G_last_update_date,
8633                         G_last_updated_by, G_last_update_login ,
8634                         sum( decode (p_basis_bal_type,
8635                              'BASE_RAW_COST',       nvl(pfpp.raw_cost,0),
8636                              'BASE_BURDENED_COST',  nvl(pfpp.burdened_cost,0),
8637                              'BASE_QUANTITY',       nvl(pfpp.quantity,0),
8638                              'BASE_LABOR_QUANTITY', nvl(pfpp.labor_quantity,0),
8639                              0
8640                              ))
8641                   from  pa_base_finplan_by_pa_period_v pfpp,
8642                         pa_alloc_run_targets part
8643                  where  part.rule_id            = p_rule_id
8644                    and  part.run_id             = p_run_id
8645                    and  part.exclude_flag       = 'N'
8646                    and  pfpp.project_id         = part.project_id
8647                    and  pfpp.task_id            = part.task_id
8648                    and  pfpp.budget_version_id  = part.budget_version_id
8649                    and  pfpp.pa_period          = v_rel_period_name
8650               group by  part.rule_id, part.run_id,  part.line_num,
8651                         part.project_id, part.task_id,
8652                         part.line_percent, G_creation_date, G_created_by,
8653                         G_last_update_date,
8654                         G_last_updated_by, G_last_update_login ;
8655 		      end if; -- p_run_period_type
8656 /* Code changes end for bug 2757875 */
8657              End if ;
8658 ---   Create Zero amount records for tasks that did not have budgets..
8659                 Insert into pa_alloc_run_basis_det(
8660                       rule_id, run_id, line_num, project_id, task_id,
8661                       line_percent, creation_date, created_by, last_update_date,
8662                       last_updated_by, last_update_login, amount)
8663                 select  part.rule_id, part.run_id,  part.line_num,
8664                         part.project_id, part.task_id,
8665                         part.line_percent, G_creation_date,
8666                         G_created_by, G_last_update_date,
8667                         G_last_updated_by, G_last_update_login ,
8668                         0
8669                   from  pa_alloc_run_targets part
8670                  where  part.rule_id            = p_rule_id
8671                    and  part.run_id             = p_run_id
8672                    and  part.exclude_flag       = 'N'
8673                    and  NOT EXISTS ( Select 'Exists'
8674                                        from  pa_alloc_run_basis_det parbd
8675                                       where  parbd.run_id = part.run_id
8676                                         and  parbd.project_id = part.project_id
8677                                         and  parbd.task_id    = part.task_id ) ;
8678           End if ; -- basis category
8679        End if ; -- For Resource list
8680     End if ; --- For basis_method
8681     /* added for bug 2182563. For FP insert one row per target with zero amount */
8682     IF p_basis_method  = 'FP' then
8683        /* First delete any records that are existing in the table 'pa_alloc_run_basis_det'
8684           with amount = 0. so that the following insert does not insert any duplicate
8685           records into the table if there is a record already existing with amount = 0 */
8686                DELETE FROM PA_ALLOC_RUN_BASIS_DET
8687                 WHERE rule_id = p_rule_id
8688                   AND run_id  = p_run_id
8689                   AND nvl(amount,0)  = 0;
8690        /* we need to insert rows for a run_id/line_num only if there are no records in
8691           in basis table for that run_id and line number with amount <> 0. Because in this
8692           case the program has function like basis_method = 'FS' for that target line. */
8693                 Insert into pa_alloc_run_basis_det (
8694                   rule_id, run_id, line_num, project_id, task_id,
8695                   line_percent, creation_date, created_by,
8696                   last_update_date, last_updated_by,
8697                   last_update_login, amount)
8698                 select  part.rule_id, part.run_id,  part.line_num, part.project_id,
8699                         part.task_id, part.line_percent, G_creation_date, G_created_by,
8700                         G_last_update_date, G_last_updated_by, G_last_update_login ,
8701                         0 AMOUNT
8702                   from  pa_alloc_run_targets part
8703                  where  part.rule_id       = p_rule_id
8704                    and  part.run_id        = p_run_id
8705                    and  part.exclude_flag  = 'N'
8706                    and  not exists
8707                         (select null
8708                            from pa_alloc_run_basis_det parb
8709                           where parb.run_id = part.run_id
8710                             and parb.line_num = part.line_num
8711                             and parb.amount <> 0);
8712     END IF;
8713    IF P_DEBUG_MODE = 'Y' THEN
8714       pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG', 'No of Records Inserted in basis details'||
8715                       to_char(SQL%ROWCOUNT));
8716    END IF;
8717    pa_debug.G_err_stage := 'Calculating basis percent' ;
8718    IF P_DEBUG_MODE = 'Y' THEN
8719       pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG', pa_debug.G_Err_Stage);
8720    END IF;
8721    pa_debug.G_err_stage := 'Getting Basis total for Method:'||p_basis_method ;
8722    IF P_DEBUG_MODE = 'Y' THEN
8723       pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG', pa_debug.G_Err_Stage);
8724    END IF;
8725   If ((p_basis_method = 'P') or (p_basis_method ='C' and v_sum_tgt_pct = 0) ) then
8726      OPEN C_tot_basis_amt ;
8727      FETCH C_tot_basis_Amt INTO v_tot_basis_amt , v_max_basis_amt, v_tot_basis_rec; /* 1900331 */
8728      IF C_tot_basis_amt%NOTFOUND then
8729          /* 2182563 - this should be a warning and not an error. A different warning needs to be
8730             created */
8731          alloc_errors(p_rule_id, p_run_id, 'B', 'E','PA_AL_NO_BASIS_FOUND',FALSE) ;
8732          /* add different warning to say that it will be spread evenly */
8733      END IF ;
8734      If nvl(v_tot_basis_amt,0) = 0 and nvl(v_max_basis_amt,0) <> 0 then /* 1900331 */
8735         alloc_errors(p_rule_id, p_run_id, 'B', 'E','PA_AL_ZERO_BASIS',TRUE) ;
8736      Else
8737        IF P_DEBUG_MODE = 'Y' THEN
8738           pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG','Basis_amount :'||to_char(v_tot_basis_amt) );
8739        END IF;
8740      End if;
8741      Close C_tot_basis_amt ;
8742     if nvl(v_tot_basis_amt,0) <> 0 then   /* added for 1900331 */
8743        UPDATE pa_alloc_run_basis_det
8744        SET basis_percent = decode(nvl(amount,0), 0, 0, amount*100/v_tot_basis_amt)
8745        WHERE run_id = p_run_id ;
8746     else
8747       /* for bug 2182563. Change the basis method to 'S' and return to main program */
8748        p_basis_method := 'S';
8749       /* bug 1900331 prorate equally */
8750       /* commented for 2182563
8751        UPDATE pa_alloc_run_basis_det
8752        SET basis_percent = 100/v_tot_basis_rec
8753        WHERE run_id = p_run_id ;
8754       */
8755     end if;
8756     IF P_DEBUG_MODE = 'Y' THEN
8757        pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG','No of Records Updated : '|| to_char(SQL%ROWCOUNT));
8758     END IF;
8759   Elsif ((p_basis_method = 'FP') or (p_basis_method ='C' and v_sum_tgt_pct = 100)) then
8760     OPEN C_line_basis_amt;
8761     LOOP
8762      Fetch C_line_basis_amt into v_line_num, v_line_percent, v_line_basis_amt, v_line_max_amt, v_line_count; /* 1900331 */
8763     EXIT when c_line_basis_amt%NOTFOUND ;
8764      If ( nvl(v_line_basis_amt,0) = 0 and nvl(v_line_percent,0) > 0 and nvl(v_line_max_amt,0) <> 0 ) then /* 1900331 */
8765       IF P_DEBUG_MODE = 'Y' THEN
8766          pa_debug.write_file('cal_proj_basis_amounts: ' || 'LOG','basis amount  is zero for target line '||
8767                          to_char(v_line_num) );
8768       END IF;
8769          alloc_errors(p_rule_id, p_run_id, 'B', 'E','PA_AL_LINE_BASIS_AMT_IS_ZERO',TRUE) ;
8770       End if;
8771     if nvl(v_line_basis_amt,0) <> 0 then /* added for bug 1900331 */
8772     UPDATE pa_alloc_run_basis_det
8773       SET basis_percent = decode(nvl(amount,0), 0, 0, amount*100/v_line_basis_amt)
8774        WHERE run_id = p_run_id
8775       AND line_num = v_line_num ;
8776     else
8777       /* 1900331 - prorate equaly within the same target line */
8778       UPDATE pa_alloc_run_basis_det
8779       SET basis_percent = 100/v_line_count
8780        WHERE run_id = p_run_id
8781       AND line_num = v_line_num ;
8782     end if;
8783     END LOOP ;
8784      IF C_line_basis_amt%ROWCOUNT = 0 then
8785        alloc_errors(p_rule_id, p_run_id, 'B', 'E','PA_AL_NO_BASIS_FOUND',TRUE) ;
8786      End If ;
8787      Close C_line_basis_amt ;
8788    End If ;
8789    pa_debug.reset_err_stack;
8790 EXCEPTION
8791    WHEN OTHERS THEN
8792 --    pa_debug.reset_err_stack;
8793    RAISE;
8794 END cal_proj_basis_amounts ;
8795 /* following procedure is obsoleted (not in use after fix for 2211234 */
8796 /***-- ============================================================
8797  ***-- get_budget_amounts
8798  ***-- ============================================================
8799  ***PROCEDURE get_budget_amounts( p_run_period_type     IN VARCHAR2,
8800  ***                              p_bal_type            IN VARCHAR2,
8801  ***                              p_project_id          IN NUMBER  ,
8802  ***                              p_task_id             IN NUMBER  ,
8803  ***                              p_rl_id               IN NUMBER  ,
8804  ***                              p_rlm_id              IN NUMBER  ,
8805  ***                              p_budget_type_code    IN VARCHAR2,
8806  ***                              p_start_date          IN DATE ,
8807  ***                              p_end_date            IN DATE ,
8808  ***                              x_amount              OUT NOCOPY NUMBER  )
8809  ***IS
8810  ***     v_project_start_date  DATE ;
8811  ***
8812  ***     cursor C_budget_amt_by_pa_period is
8813  ***        select nvl(sum( decode (p_bal_type,
8814  ***                    'BASE_RAW_COST',       nvl(pbpp.raw_cost,0),
8815  ***                    'BASE_BURDENED_COST',  nvl(pbpp.burdened_cost,0),
8816  ***                    'BASE_QUANTITY',       nvl(pbpp.quantity,0),
8817  ***                    'BASE_LABOR_QUANTITY', nvl(pbpp.labor_quantity,0),
8818  ***                    0
8819  ***                    )),0)
8820  ***         from pa_base_budget_by_pa_period_v  pbpp
8821  ***        where pbpp.Resource_list_id        = p_rl_id
8822  ***          and pbpp.Resource_list_member_id = p_rlm_id
8823  ***          and pbpp.Project_id              = p_project_id
8824  ***          and pbpp.task_id                 = p_task_id
8825  ***          and pbpp.budget_type_code        = p_budget_type_code
8826  ***          and pbpp.period_start_date      >= nvl(p_start_date,pbpp.period_start_date)
8827  ***          and pbpp.period_end_date        <= p_end_date ;
8828  ***
8829  ***     cursor C_budget_amt_by_gl_period is
8830  ***        select nvl(sum( decode (p_bal_type,
8831  ***                    'BASE_RAW_COST',       nvl(pbpg.raw_cost,0),
8832  ***                    'BASE_BURDENED_COST',  nvl(pbpg.burdened_cost,0),
8833  ***                    'BASE_QUANTITY',       nvl(pbpg.quantity,0),
8834  ***                    'BASE_LABOR_QUANTITY', nvl(pbpg.labor_quantity,0),
8835  ***                    0
8836  ***                    )),0)
8837  ***         from pa_base_budget_by_gl_period_v  pbpg
8838  ***        where pbpg.Resource_list_id        = p_rl_id
8839  ***          and pbpg.Resource_list_member_id = p_rlm_id
8840  ***          and pbpg.Project_id              = p_project_id
8841  ***          and pbpg.task_id                 = p_task_id
8842  ***          and pbpg.budget_type_code        = p_budget_type_code
8843  ***          and pbpg.period_start_date      >= nvl(p_start_date, pbpg.period_start_date)
8844  ***          and pbpg.period_end_date        <= p_end_date ;
8845  ***BEGIN
8846  ***     If p_run_period_type = 'PA' then
8847  ***        OPEN C_budget_amt_by_pa_period ;
8848  ***        FETCH C_budget_amt_by_pa_period into x_amount ;
8849  ***        If C_budget_amt_by_pa_period%NOTFOUND  then
8850  ***        pa_debug.write_file('LOG','No data found');
8851  ***            x_amount := 0 ;
8852  ***        End if ;
8853  ***        CLOSE C_budget_amt_by_pa_period ;
8854  ***
8855  ***     Elsif  p_run_period_type = 'GL' then
8856  ***        OPEN C_budget_amt_by_gl_period ;
8857  ***        FETCH C_budget_amt_by_gl_period into x_amount ;
8858  ***        If C_budget_amt_by_gl_period%NOTFOUND  then
8859  ***            x_amount := 0 ;
8860  ***        End if ;
8861  ***        CLOSE C_budget_amt_by_gl_period ;
8862  ***     End if ;
8863  ***EXCEPTION
8864  ***    WHEN OTHERS THEN
8865  ***       RAISE ;
8866  ***END get_budget_amounts ;
8867  ***/
8868 -- ==========================================================================
8869 /* PROCEDURE :  clean_up_targets_for_actuals
8870    Purpose   :  Deletes records from pa_alloc_run_targets which do not contain
8871                 any Basis amount. The addition of this procedure does not
8872                 modify the existing flow. This procedure has been added to
8873                 delete unnecessary records from pa_alloc_run_targets for
8874                 performance reasons. So, removing this procedure will not impact
8875                 the existing flow of Allocations.
8876    Created   :  18-feb-02 Praveen for Bug #2222280
8877 */
8878 -- ==========================================================================
8879 PROCEDURE clean_up_targets_for_actuals(
8880                             p_run_id              IN NUMBER,
8881                             p_rule_id             IN NUMBER,
8882                             p_amt_type            IN VARCHAR2,
8883                             p_run_period_type     IN VARCHAR2,
8884                             p_period              IN VARCHAR2,
8885                             p_run_period_end_date IN DATE ,
8886                             p_amttype_start_date  IN DATE,
8887                             p_basis_method        IN OUT NOCOPY VARCHAR2
8888                             )
8889 IS
8890      cursor c_target_lines is
8891      select line_num
8892        from pa_alloc_target_lines patl
8893       where rule_id = p_rule_id;
8894      v_commit_count NUMBER ;
8895      v_do_commit    VARCHAR2(1);
8896 BEGIN
8897      pa_debug.G_err_stage:= 'INSIDE CLEAN_UP_TARGETS_FOR_ACTUALS procedure';
8898      IF P_DEBUG_MODE = 'Y' THEN
8899         pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', pa_debug.G_err_stage);
8900      END IF;
8901      IF p_amt_type in ( 'FYTD', 'QTD') THEN
8902           pa_debug.G_err_stage:= 'Deleting data from PA_Alloc_Run_Targets for FYTD or QTD';
8903           IF P_DEBUG_MODE = 'Y' THEN
8904              pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', pa_debug.G_err_stage);
8905           END IF;
8906           v_commit_count := 0;
8907           IF p_basis_method = 'P' THEN
8908                SAVEPOINT delete_unwanted_targets;
8909 	       /*For Bug 5403833*/
8910                DELETE FROM pa_alloc_run_targets part
8911                 WHERE part.run_id = p_run_id
8912                   AND part.exclude_flag = 'N' /* we want to delete only exclude flag 'N' targets */
8913                   AND not exists
8914                       (select null
8915                          from pa_txn_accum pta,pa_periods pp
8916                         where pta.project_id = part.project_id
8917                           and pta.task_id = part.task_id
8918                             and pp.period_name = pta.pa_period
8919                             and pp.end_date between p_amttype_start_date
8920                                                 and p_run_period_end_date);
8921                IF P_DEBUG_MODE = 'Y' THEN
8922                   pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Deleted '||sql%rowcount||' records');
8923                END IF;
8924                BEGIN
8925                        select 'Y'
8926                          into v_do_commit
8927                          from dual
8928                         where exists
8929                              (select null
8930                                 from pa_alloc_run_targets part
8931                                where part.run_id = p_run_id
8932                                  and part.exclude_flag = 'N'
8933                                  and rownum = 1);
8934                        /* We will commit if any records are still there in targets table.
8935                           If all the records are deleted then we will need these records
8936                           to do spread evenly.
8937                        */
8938                           COMMIT;
8939                           IF P_DEBUG_MODE = 'Y' THEN
8940                              pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Committed the deletion');
8941                           END IF;
8942                EXCEPTION
8943                         WHEN NO_DATA_FOUND THEN
8944                           /* switch the basis method to 'S' in case all records are deleted.
8945                              We need to do rollback also
8946                           */
8947                           ROLLBACK TO delete_unwanted_targets;
8948                           IF P_DEBUG_MODE = 'Y' THEN
8949                              pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Rolled back the deletion');
8950                           END IF;
8951                           p_basis_method := 'S';
8952                END;
8953           elsif p_basis_method = 'FP' then
8954                FOR c_target_lines_rec in c_target_lines LOOP
8955                     SAVEPOINT delete_unwanted_targets;
8956 		    /* For Bug 5403833 */
8957                     DELETE FROM pa_alloc_run_targets part
8958                      WHERE part.run_id = p_run_id
8959                        AND part.exclude_flag = 'N' /* we want to delete only exclude flag 'N' targets */
8960                        AND part.line_num = c_target_lines_rec.line_num
8961                        AND not exists
8962                            (select null
8963                               from pa_txn_accum pta,pa_periods pp
8964                              where pta.project_id = part.project_id
8965                                and pta.task_id = part.task_id
8966                                and pp.period_name = pta.pa_period
8967                                and pp.end_date between p_amttype_start_date
8968                                and p_run_period_end_date);
8969 
8970                     IF P_DEBUG_MODE = 'Y' THEN
8971                        pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Deleted '||sql%rowcount||' records');
8972                     END IF;
8973               BEGIN
8974                        select 'Y'
8975                          into  v_do_commit
8976                          from dual
8977                         where exists
8978                              (select null
8979                                 from pa_alloc_run_targets part
8980                                where part.run_id = p_run_id
8981                                  and part.exclude_flag = 'N'
8982                                  and line_num = c_target_lines_rec.line_num
8983                                  and rownum = 1);
8984                        /* We will commit if any records are still there in targets table.
8985                           If all the records are deleted then we will need these records
8986                           to do spread evenly.
8987                           No switch of basis method can be done in case of FP.
8988                        */
8989                           COMMIT;
8990                           IF P_DEBUG_MODE = 'Y' THEN
8991                              pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Committed the deletion');
8992                           END IF;
8993                EXCEPTION
8994                         WHEN NO_DATA_FOUND THEN
8995                           ROLLBACK TO delete_unwanted_targets;
8996                           IF P_DEBUG_MODE = 'Y' THEN
8997                              pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Rolled back the deletion');
8998                           END IF;
8999                END;
9000                end loop;
9001           END IF;
9002      ELSIF  p_amt_type = 'PTD' THEN
9003           pa_debug.G_err_stage:= 'Deleting data from PA_Alloc_Run_Targets for PTD';
9004           IF P_DEBUG_MODE = 'Y' THEN
9005              pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', pa_debug.G_err_stage);
9006           END IF;
9007           v_commit_count := 0;
9008           IF p_basis_method = 'P' THEN
9009              IF p_run_period_type = 'PA' THEN
9010                IF P_DEBUG_MODE = 'Y' THEN
9011                   pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Deleting for Period_Type = PA');
9012                END IF;
9013                SAVEPOINT delete_unwanted_targets;
9014                DELETE FROM pa_alloc_run_targets part
9015                 WHERE part.run_id = p_run_id
9016                   AND part.exclude_flag = 'N' /* we want to delete only exclude flag 'N' targets */
9017                   AND not exists
9018                       (select null
9019                          from pa_txn_accum pta
9020                         where pta.project_id = part.project_id
9021                           and pta.task_id = part.task_id
9022                           and pta.pa_period = p_period
9023                           and rownum = 1
9024                       );
9025                IF P_DEBUG_MODE = 'Y' THEN
9026                   pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Deleted '||sql%rowcount||' records');
9027                END IF;
9028                BEGIN
9029                        select 'Y'
9030                          into v_do_commit
9031                          from dual
9032                         where exists
9033                              (select null
9034                                 from pa_alloc_run_targets part
9035                                where part.run_id = p_run_id
9036                                  and part.exclude_flag = 'N'
9037                                  and rownum = 1);
9038                        /* We will commit if any records are still there in targets table.
9039                           If all the records are deleted then we will need these records
9040                           to do spread evenly.
9041                        */
9042                           COMMIT;
9043                           IF P_DEBUG_MODE = 'Y' THEN
9044                              pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Committed the deletion');
9045                           END IF;
9046                EXCEPTION
9047                       WHEN NO_DATA_FOUND THEN
9048                           /* switch the basis method to 'S' in case all records are deleted.
9049                              We need to do rollback also
9050                           */
9051                           ROLLBACK TO delete_unwanted_targets;
9052                           IF P_DEBUG_MODE = 'Y' THEN
9053                              pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Rolled back the deletion');
9054                           END IF;
9055                           p_basis_method := 'S';
9056                END;
9057              ELSE /* if p_run_period_type = GL */
9058                IF P_DEBUG_MODE = 'Y' THEN
9059                   pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Deleting for Period_Type = GL');
9060                END IF;
9061                SAVEPOINT delete_unwanted_targets;
9062 
9063               /*For Bug 5403833*/
9064 
9065                DELETE FROM pa_alloc_run_targets part
9066                 WHERE part.run_id = p_run_id
9067                   AND part.exclude_flag = 'N' /* we want to delete only exclude flag 'N' targets */
9068                   AND not exists
9069                       (select null
9070                          from pa_txn_accum pta,pa_periods pp
9071                         where pta.project_id = part.project_id
9072                           and pta.task_id = part.task_id
9073                           and pp.period_name = pta.pa_period
9074                           and pp.gl_period_name = p_period);
9075 
9076                IF P_DEBUG_MODE = 'Y' THEN
9077                   pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Deleted '||sql%rowcount||' records');
9078                END IF;
9079                BEGIN
9080                        select 'Y'
9081                          into v_do_commit
9082                          from dual
9083                         where exists
9084                              (select null
9085                                 from pa_alloc_run_targets part
9086                                where part.run_id = p_run_id
9087                                  and part.exclude_flag = 'N'
9088                                  and rownum = 1);
9089                        /* We will commit if any records are still there in targets table.
9090                           If all the records are deleted then we will need these records
9091                           to do spread evenly.
9092                        */
9093                           IF P_DEBUG_MODE = 'Y' THEN
9094                              pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Committed the deletion');
9095                           END IF;
9096                           COMMIT;
9097                EXCEPTION
9098                      WHEN NO_DATA_FOUND THEN
9099                           /* swith the basis method to 'S' in case all records are deleted
9100                              we need to do rollback also
9101                           */
9102                           ROLLBACK TO delete_unwanted_targets;
9103                           IF P_DEBUG_MODE = 'Y' THEN
9104                              pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Rolled back the deletion');
9105                           END IF;
9106                           p_basis_method := 'S';
9107                END;
9108              END IF; /* p_run_period_type = 'PA' */
9109           elsif p_basis_method = 'FP' then
9110              IF p_run_period_type = 'PA' THEN
9111                IF P_DEBUG_MODE = 'Y' THEN
9112                   pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Deleting for Period_Type = PA');
9113                END IF;
9114                FOR c_target_lines_rec in c_target_lines LOOP
9115                     SAVEPOINT delete_unwanted_targets;
9116                     DELETE FROM pa_alloc_run_targets part
9117                      WHERE part.run_id = p_run_id
9118                        AND part.exclude_flag = 'N' /* we want to delete only exclude flag 'N' targets */
9119                        AND part.line_num = c_target_lines_rec.line_num
9120                        AND not exists
9121                            (select null
9122                               from pa_txn_accum pta
9123                              where pta.project_id = part.project_id
9124                                and pta.task_id = part.task_id
9125                                and pta.pa_period = p_period
9126                                and rownum = 1
9127                            );
9128                     IF P_DEBUG_MODE = 'Y' THEN
9129                        pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Deleted '||sql%rowcount||' records');
9130                     END IF;
9131                     BEGIN
9132                             select 'Y'
9133                              into  v_do_commit
9134                               from dual
9135                              where exists
9136                                   (select null
9137                                      from pa_alloc_run_targets part
9138                                     where part.run_id = p_run_id
9139                                       and part.exclude_flag = 'N'
9140                                       and line_num = c_target_lines_rec.line_num
9141                                       and rownum = 1);
9142                             /* We will commit if any records are still there in targets table.
9143                                If all the records for the line are deleted then we will need
9144                                these records to do spread evenly.
9145                                No switch of basis method can be done in case of FP.
9146                             */
9147                                COMMIT;
9148                                IF P_DEBUG_MODE = 'Y' THEN
9149                                   pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Committed the deletion');
9150                                END IF;
9151                     EXCEPTION
9152                           WHEN NO_DATA_FOUND THEN
9153                                ROLLBACK TO delete_unwanted_targets;
9154                                IF P_DEBUG_MODE = 'Y' THEN
9155                                   pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Rolled back the deletion');
9156                                END IF;
9157                     END;
9158                end loop;
9159              ELSE /* p_run_period_type = 'GL' */
9160                IF P_DEBUG_MODE = 'Y' THEN
9161                   pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Deleting for Period_Type = GL');
9162                END IF;
9163                FOR c_target_lines_rec in c_target_lines LOOP
9164                     SAVEPOINT delete_unwanted_targets;
9165 		    /*For Bug 5403833 */
9166                     DELETE FROM pa_alloc_run_targets part
9167                      WHERE part.run_id = p_run_id
9168                        AND part.exclude_flag = 'N' /* we want to delete only exclude flag 'N' targets */
9169                        AND part.line_num = c_target_lines_rec.line_num
9170                        AND not exists
9171                            (select null
9172                               from pa_txn_accum pta,pa_periods pp
9173                              where pta.project_id = part.project_id
9174                                and pta.task_id = part.task_id
9175                                and pp.period_name = pta.pa_period
9176                                and pp.gl_period_name = p_period
9177                            );
9178                     IF P_DEBUG_MODE = 'Y' THEN
9179                        pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Deleted '||sql%rowcount||' records');
9180                     END IF;
9181                     BEGIN
9182                             select 'Y'
9183                              into  v_do_commit
9184                               from dual
9185                              where exists
9186                                   (select null
9187                                      from pa_alloc_run_targets part
9188                                     where part.run_id = p_run_id
9189                                       and part.exclude_flag = 'N'
9190                                       and line_num = c_target_lines_rec.line_num
9191                                       and rownum = 1);
9192                             /* We will commit if any records are still there in targets table.
9193                                If all the records for the line are deleted then we will need
9194                                these records to do spread evenly.
9195                                No switch of basis method can be done in case of FP.
9196                             */
9197                                COMMIT;
9198                                IF P_DEBUG_MODE = 'Y' THEN
9199                                   pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Committed the deletion');
9200                                END IF;
9201                     EXCEPTION
9202                           WHEN NO_DATA_FOUND THEN
9203                                ROLLBACK TO delete_unwanted_targets;
9204                                IF P_DEBUG_MODE = 'Y' THEN
9205                                   pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Rolled back the deletion');
9206                                END IF;
9207                     END;
9208                end loop;
9209              END IF; /* p_run_period_type = 'GL' */
9210           END IF;
9211      ELSIF p_amt_type = 'ITD' THEN
9212          /* For ITD let's consider all periods in pa_periods table rather than
9213             doing this delete for each project id in the targets table. Hence do
9214             not use pa_periods table in this case. */
9215           pa_debug.G_err_stage:= 'Deleting data from PA_Alloc_Run_Targets for ITD';
9216           IF P_DEBUG_MODE = 'Y' THEN
9217              pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', pa_debug.G_err_stage);
9218           END IF;
9219           v_commit_count := 0;
9220           IF p_basis_method = 'P' THEN
9221                SAVEPOINT delete_unwanted_targets;
9222                DELETE FROM pa_alloc_run_targets part
9223                 WHERE part.run_id = p_run_id
9224                   AND part.exclude_flag = 'N' /* we want to delete only exclude flag 'N' targets */
9225                   AND not exists
9226                       (select null
9227                          from pa_txn_accum pta
9228                         where pta.project_id = part.project_id
9229                           and pta.task_id = part.task_id
9230                           and rownum = 1
9231                       );
9232                IF P_DEBUG_MODE = 'Y' THEN
9233                   pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Deleted '||sql%rowcount||' records');
9234                END IF;
9235                BEGIN
9236                        select 'Y'
9237                          into v_do_commit
9238                          from dual
9239                         where exists
9240                              (select null
9241                                 from pa_alloc_run_targets part
9242                                where part.run_id = p_run_id
9243                                  and part.exclude_flag = 'N'
9244                                  and rownum = 1);
9245                        /* We will commit if any records are still there in targets table.
9246                           If all the records are deleted then we will need these records
9247                           to do spread evenly.
9248                        */
9249                           IF P_DEBUG_MODE = 'Y' THEN
9250                              pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Committed the deletion');
9251                           END IF;
9252                           COMMIT;
9253                EXCEPTION
9254                      WHEN NO_DATA_FOUND THEN
9255                           /* swith the basis method to 'S' in case all records are deleted
9256                              we need to do rollback also
9257                           */
9258                           ROLLBACK TO delete_unwanted_targets;
9259                           IF P_DEBUG_MODE = 'Y' THEN
9260                              pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', 'Rolled back the deletion');
9261                           END IF;
9262                           p_basis_method := 'S';
9263                END;
9264           elsif p_basis_method = 'FP' then
9265                FOR c_target_lines_rec in c_target_lines LOOP
9266                     SAVEPOINT delete_unwanted_targets;
9267                     DELETE FROM pa_alloc_run_targets part
9268                      WHERE part.run_id = p_run_id
9269                        AND part.exclude_flag = 'N' /* we want to delete only exclude flag 'N' targets */
9270                        AND part.line_num = c_target_lines_rec.line_num
9271                        AND not exists
9272                            (select null
9273                               from pa_txn_accum pta
9274                              where pta.project_id = part.project_id
9275                                and pta.task_id = part.task_id
9276                                and rownum = 1
9277                            );
9278                     BEGIN
9279                             select 'Y'
9280                              into  v_do_commit
9281                               from dual
9282                              where exists
9283                                   (select null
9284                                      from pa_alloc_run_targets part
9285                                     where part.run_id = p_run_id
9286                                       and part.exclude_flag = 'N'
9287                                       and line_num = c_target_lines_rec.line_num
9288                                       and rownum = 1);
9289                             /* We will commit if any records are still there in targets table.
9290                                If all the records for the line are deleted then we will need
9291                                these records to do spread evenly.
9292                                No switch of basis method can be done in case of FP.
9293                             */
9294                                COMMIT;
9295                     EXCEPTION
9296                           WHEN NO_DATA_FOUND THEN
9297                                ROLLBACK TO delete_unwanted_targets;
9298                     END;
9299                end loop;
9300           END IF;
9301      END IF ;
9302      pa_debug.G_err_stage:= 'exiting clean_up_targets_for_actuals';
9303      IF P_DEBUG_MODE = 'Y' THEN
9304         pa_debug.write_file('clean_up_targets_for_actuals: ' ||  'LOG', pa_debug.G_err_stage);
9305      END IF;
9306 EXCEPTION
9307     WHEN OTHERS THEN
9308        RAISE ;
9309 END clean_up_targets_for_actuals;
9310 -- ------------------------------------------------------------
9311 -- lock_rule
9312 -- ------------------------------------------------------------
9313 PROCEDURE lock_rule( p_rule_id  IN NUMBER
9314                     ,p_run_id   IN NUMBER )
9315 IS
9316 BEGIN
9317    pa_debug.G_err_stage := 'Acquiring lock  on the rule ' ;
9318    IF P_DEBUG_MODE = 'Y' THEN
9319       pa_debug.write_file('lock_rule: ' || 'LOG',pa_debug.G_err_stage);
9320    END IF;
9321    --Added this If and the else part for capital project changes
9322    IF p_rule_id <> -1 THEN
9323          If pa_debug.Acquire_user_lock( 'PA_AL_'||to_char(p_rule_id)) <> 0  then
9324           G_fatal_err_found:= TRUE;
9325           pa_alloc_run.ins_alloc_exceptions( p_rule_id, p_run_id, G_creation_date,
9326                                 G_created_by, G_last_update_date,
9327                                 G_last_updated_by, G_last_update_login,
9328                                 'R', 'E', NULL, NULL, 'PA_AL_CANT_ACQUIRE_LOCK');
9329           end if;
9330    ELSE
9331          If pa_debug.Acquire_user_lock( 'PA_CINT_'||to_char(p_run_id)) <> 0  then
9332           G_fatal_err_found:= TRUE;
9333           pa_alloc_run.ins_alloc_exceptions( p_rule_id, p_run_id, G_creation_date,
9334                                 G_created_by, G_last_update_date,
9335                                 G_last_updated_by, G_last_update_login,
9336                                 'R', 'E', NULL, NULL, 'PA_CINT_CANT_ACQUIRE_LOCK');
9337          END IF;
9338    END IF ;
9339 EXCEPTION
9340     WHEN OTHERS THEN
9341     RAISE ;
9342 END lock_rule ;
9343 -- ------------------------------------------------------------
9344 -- unlock_rule
9345 -- ------------------------------------------------------------
9346 PROCEDURE unlock_rule( p_rule_id  IN NUMBER
9347                     ,p_run_id   IN NUMBER  )
9348 IS
9349 BEGIN
9350    pa_debug.G_err_stage := 'unlock the  rule ' ;
9351    IF P_DEBUG_MODE = 'Y' THEN
9352       pa_debug.write_file('unlock_rule: ' || 'LOG',pa_debug.G_err_stage);
9353    END IF;
9354    IF p_rule_id <> -1 THEN
9355          If pa_debug.Release_user_lock(  'PA_AL_'||to_char(p_rule_id)) <> 0  then
9356           G_fatal_err_found:= TRUE;
9357           pa_alloc_run.ins_alloc_exceptions( p_rule_id, p_run_id, G_creation_date,
9358                                 G_created_by, G_last_update_date,
9359                                 G_last_updated_by, G_last_update_login,
9360                                 'R', 'E', NULL, NULL, 'PA_AL_LOCK_RELEASE_FAILED');
9361           end if;
9362    ELSE
9363          If pa_debug.Release_user_lock(  'PA_CINT_'||to_char(p_run_id)) <> 0  then
9364           G_fatal_err_found:= TRUE;
9365           pa_alloc_run.ins_alloc_exceptions( p_rule_id, p_run_id, G_creation_date,
9366                                 G_created_by, G_last_update_date,
9367                                 G_last_updated_by, G_last_update_login,
9368                                 'R', 'E', NULL, NULL, 'PA_CINT_LOCK_RELEASE_FAILED');
9369          End If;
9370    End If ;
9371 EXCEPTION
9372     WHEN OTHERS THEN
9373     RAISE ;
9374 END unlock_rule ;
9375 -- ------------------------------------------------------------
9376 -- Release_alloc_txns
9377 -- ------------------------------------------------------------
9378 PROCEDURE Release_alloc_txns( p_rule_id  IN NUMBER
9379                              ,p_run_id   IN  NUMBER
9380                              , x_retcode OUT NOCOPY VARCHAR2
9381                              , x_errbuf  OUT NOCOPY VARCHAR2
9382                             )
9383 IS
9384    Cursor C_run is
9385     Select  denom_currency_code
9386           , target_exp_type_class
9387           , offset_exp_type_class
9388           , target_exp_org_id
9389           , offset_exp_org_id
9390           , target_exp_type
9391           , offset_exp_type
9392           , trunc(expnd_item_date)  expnd_item_date
9393           , offset_method
9394       from  pa_alloc_runs
9395      where  run_id = p_run_id ;
9396    cursor C_org_id is
9397     select org_id
9398       from pa_implementations ;
9399    run_rec   C_run%ROWTYPE ;
9400    v_import_failed    VARCHAR2(1) ;
9401    v_org_id           NUMBER ;
9402    /* Bug No:- 2487147, UTF8 change : changed  v_target_expnd_org to %TYPE */
9403    /* v_target_expnd_org VARCHAR2(60) ; */
9404    v_target_expnd_org   hr_organization_units.name%TYPE;
9405    /* Bug No:- 2487147, UTF8 change : changed v_offset_expnd_org to %TYPE */
9406    /* v_offset_expnd_org VARCHAR2(60) ; */
9407    v_offset_expnd_org   hr_organization_units.name%TYPE;
9408    v_target_exp_type_class VARCHAR2(30) ;
9409    v_offset_exp_type_class VARCHAR2(30) ;
9410    v_expnd_end_date   DATE ;
9411    v_interface_id     NUMBER ;
9412    v_batch_name       PA_TRANSACTION_INTERFACE_ALL.BATCH_NAME%TYPE ;
9413    v_tgt_exp_group    PA_EXPENDITURE_GROUPS.EXPENDITURE_GROUP%TYPE ;
9414    v_off_exp_group    PA_EXPENDITURE_GROUPS.EXPENDITURE_GROUP%TYPE ;
9415    v_expnd_comment    PA_TRANSACTION_INTERFACE_ALL.EXPENDITURE_COMMENT%TYPE;
9416 
9417    l_rej_code         PA_ALLOC_TXN_DETAILS.rejection_code%type;  /* added for bug 6243121 */
9418    l_cnt_ex           number:=0; /* added for bug 6243121 */
9419 
9420    Cursor  C_expenditure is
9421      Select expenditure_id
9422        from pa_expenditures
9423       where expenditure_group in (v_tgt_exp_group,v_off_exp_group) ;
9424       --Get the the rule name for capitalized interest
9425       Cursor C_cint_rule_name IS
9426       SELECT  meaning
9427         FROM  pa_lookups
9428        WHERE  lookup_type='PROJECT_STATUS_ACTIONS'
9429           AND lookup_code='CAPITALIZED_INTEREST';
9430 
9431 	/* added cursor for bug 6243121 */
9432        cursor c_alloc_txn_err(runid IN NUMBER) is
9433        select distinct rejection_code
9434          from pa_alloc_txn_details
9435         where run_id=runid
9436 	  and status_code='R'
9437           and rejection_code is not null;
9438 
9439    --Declared this variable for Capital project Changes
9440    l_transaction_source       pa_lookups.meaning%TYPE;
9441 BEGIN
9442    pa_debug.set_err_stack('Release_alloc_txns') ;
9443    pa_debug.G_err_stage := 'Release_alloc_txns' ;
9444    IF P_DEBUG_MODE = 'Y' THEN
9445       pa_debug.write_file('Release_alloc_txns: ' || 'LOG',pa_debug.G_err_stage);
9446    END IF;
9447 -- Init_who_cols ;
9448    x_errbuf := NULL ;
9449    --Included this block for Capital project changes
9450    IF p_rule_id = -1 THEN
9451          l_transaction_source := 'Capitalized Interest' ;
9452    ELSE
9453          l_transaction_source := 'ALLOCATIONS';
9454    END IF;
9455 -- No  need lock the rule here. Allocation_run procedure is called in the report (PAXALRUN)
9456 -- and this procedure locks the rule.
9457 --    lock_rule(p_rule_id, p_run_id) ;
9458    pa_debug.G_err_stage := 'Inserting records in Interface table ' ;
9459    IF P_DEBUG_MODE = 'Y' THEN
9460       pa_debug.write_file('Release_alloc_txns: ' || 'LOG',pa_debug.G_err_stage);
9461    END IF;
9462 ---   Get info about the run   -----------------------
9463    OPEN C_run;
9464    FETCH C_run INTO run_rec ;
9465    IF C_run%NOTFOUND THEN
9466       G_fatal_err_found := TRUE;
9467       pa_alloc_run.ins_alloc_exceptions( p_rule_id, p_run_id, G_creation_date,
9468                           G_created_by, G_last_update_date,
9469                           G_last_updated_by, G_last_update_login,
9470                           'R', 'E', NULL, NULL, 'PA_AL_RUN_NOT_EXISTS');
9471      x_errbuf := 'PA_AL_RUN_NOT_EXISTS' ;
9472    END IF;
9473    CLOSE C_run;
9474    v_target_expnd_org := pa_utils.GetOrgName(run_rec.target_exp_org_id ) ;
9475    v_offset_expnd_org := pa_utils.GetOrgName(run_rec.offset_exp_org_id ) ;
9476    If (nvl(p_rule_id,-99) <> -1) AND --Added this condition for capital project changes
9477       v_target_expnd_org is NULL THEN
9478       G_fatal_err_found := TRUE;
9479       pa_alloc_run.ins_alloc_exceptions( p_rule_id, p_run_id, G_creation_date,
9480                           G_created_by, G_last_update_date,
9481                           G_last_updated_by, G_last_update_login,
9482                           'R', 'E', NULL, NULL, 'PA_AL_INVALID_TARGET_EXP_ORG');
9483      x_errbuf := 'PA_AL_INVALID_TARGET_EXP_ORG' ;
9484    End if ;
9485    If (nvl(p_rule_id,-99) <> -1) AND --Added this condition for capital project changes
9486       (run_rec.offset_method <> 'N') AND
9487       (v_offset_expnd_org is NULL) then
9488       G_fatal_err_found := TRUE;
9489       pa_alloc_run.ins_alloc_exceptions( p_rule_id, p_run_id, G_creation_date,
9490                           G_created_by, G_last_update_date,
9491                           G_last_updated_by, G_last_update_login,
9492                           'R', 'E', NULL, NULL, 'PA_AL_INVALID_OFFSET_EXP_ORG');
9493      x_errbuf := 'PA_AL_INVALID_OFFSET_EXP_ORG' ;
9494    End if ;
9495    v_expnd_end_date := pa_utils.GetWeekEnding(run_rec.expnd_item_date) ;
9496    If v_expnd_end_date is NULL then
9497       G_fatal_err_found := TRUE;
9498      alloc_errors(p_rule_id, p_run_id, 'R', 'E','PA_AL_INVALID_EXP_ITEM_DATE',TRUE) ;
9499      x_errbuf := 'PA_AL_INVALID_EXP_ITEM_DATE' ;
9500    End if ;
9501    if G_fatal_err_found  then
9502       --Added the if block for capital project changes
9503       IF p_rule_id = -1 THEN
9504             alloc_errors(p_rule_id, p_run_id, 'R', 'E','PA_CINT_RELEASE_FAILED',TRUE,'N') ;
9505       ELSE
9506             alloc_errors(p_rule_id, p_run_id, 'R', 'E','PA_AL_RELEASE_FAILED',TRUE,'N') ;
9507       END IF;
9508    end if ;
9509    IF P_DEBUG_MODE = 'Y' THEN
9510       pa_debug.write_file('Release_alloc_txns: ' || 'LOG', 'Inserting records in pa_transaction_interface_all');
9511    END IF;
9512    v_batch_name := substr( to_char(p_run_id), 1, 10) ;
9513    Open C_org_id ;
9514    Fetch C_org_id into v_org_id ;
9515    Close C_org_id ;
9516    --Added this check for cap int changes
9517    IF (nvl(p_rule_id,-99) <> -1 )THEN
9518 -- 923184: Adding rulename and release req id as comment.
9519          v_expnd_comment := G_rule_name || ' - '|| to_number(G_request_id) ;
9520    ELSE
9521     /* bug 3041022 transalation issue , commented out the hardcoded value*/
9522       OPEN C_cint_rule_name;
9523       FETCH C_cint_rule_name INTO v_expnd_comment;
9524       CLOSE C_cint_rule_name;
9525        --  v_expnd_comment :='CAPITALIZED INTEREST ';
9526        /* end of  bug 3041022 transalation issue */
9527    END IF;
9528    IF P_DEBUG_MODE = 'Y' THEN
9529       pa_debug.write_file('Release_alloc_txns: ' || 'LOG', 'Exp item Description : '||v_expnd_comment );
9530    END IF;
9531 ---- Validate the Items inserted in the Interface table  --------
9532 --- We don't do this since transaction import will take care of this
9533 --- If we call validate item then we have to maintain that.
9534 ----  Items inserted in to the Interface table  --------
9535    insert into pa_transaction_interface_all
9536            (       transaction_source
9537            ,       system_linkage
9538            ,       batch_name
9539            ,       organization_name
9540            ,       expenditure_ending_date
9541            ,       expenditure_item_date
9542            ,       project_number
9543            ,       task_number
9544            ,       expenditure_type
9545            ,       quantity
9546            ,       denom_currency_code
9547            ,       denom_raw_cost
9548            ,       denom_burdened_cost
9549            ,       transaction_status_code
9550            ,       orig_transaction_reference
9551            ,       unmatched_negative_txn_flag
9552            ,       attribute_category
9553            ,       attribute1
9554            ,       attribute2
9555            ,       attribute3
9556            ,       attribute4
9557            ,       attribute5
9558            ,       attribute6
9559            ,       attribute7
9560            ,       attribute8
9561            ,       attribute9
9562            ,       attribute10
9563            ,       created_by
9564            ,       creation_date
9565            ,       last_updated_by
9566            ,       last_update_date
9567            ,       org_id
9568            ,       expenditure_comment
9569 	   ,       billable_flag  -- added for Capitalized Interest Functionality
9570 		/* Passing the following columns values to increase the performance*/
9571            ,       project_id
9572 	   ,       task_id
9573 	   ,       organization_id
9574 	   ,       PERSON_BUSINESS_GROUP_ID
9575 		/* end of performance changes */
9576            )
9577    Select
9578                    l_transaction_source -- Changed this for capital project changes
9579            ,       decode(patd.transaction_type,'T', run_rec.target_exp_type_class,
9580                           run_rec.offset_exp_type_class)
9581            ,       v_batch_name
9582            ,       decode(p_rule_id,    --Changed for capital project changes
9583                           -1,pa_utils.GetOrgName(patd.cint_exp_org_id ),
9584                           decode(patd.transaction_type,
9585                                  'T',v_target_expnd_org,
9586                                   v_offset_expnd_org))
9587            ,       v_expnd_end_date
9588            ,       run_rec.expnd_item_date
9589            ,       pp.segment1
9590            ,       pt.task_number
9591            ,       patd.expenditure_type
9592            ,       decode(patd.transaction_type, 'T',
9593                           decode(run_rec.target_exp_type_class,
9594                                  'PJ', patd.current_allocation,0),
9595                           decode(run_rec.offset_exp_type_class,
9596                                  'PJ', patd.current_allocation,0))
9597            ,       run_rec.denom_currency_code
9598            ,       decode(patd.transaction_type, 'T',
9599                           decode(run_rec.target_exp_type_class,
9600                                  'PJ', patd.current_allocation,0),
9601                           decode(run_rec.offset_exp_type_class,
9602                                  'PJ', patd.current_allocation,0))
9603 /* In the decode below changed the 0 in default to null for bug 1524669 */
9604            ,       decode(patd.transaction_type, 'T',
9605                           decode(run_rec.target_exp_type_class,
9606                                  'BTC', patd.current_allocation,null),
9607                           decode(run_rec.offset_exp_type_class,
9608                                  'BTC', patd.current_allocation,null))
9609            ,       'P'
9610            ,       to_char(patd.alloc_txn_id)
9611            ,       'Y'
9612            ,       patd.attribute_category
9613            ,       patd.attribute1
9614            ,       patd.attribute2
9615            ,       patd.attribute3
9616            ,       patd.attribute4
9617            ,       patd.attribute5
9618            ,       patd.attribute6
9619            ,       patd.attribute7
9620            ,       patd.attribute8
9621            ,       patd.attribute9
9622            ,       patd.attribute10
9623            ,       G_created_by
9624            ,       G_creation_date
9625            ,       G_last_updated_by
9626            ,       G_last_update_date
9627            ,       v_org_id
9628            ,       v_expnd_comment
9629 	   ,       decode(l_transaction_source,'Capitalized Interest','Y',null)
9630                 /* Passing the following columns values to increase the performance*/
9631            ,       pp.project_id
9632            ,       pt.task_id
9633            ,       decode(p_rule_id,-1,patd.cint_exp_org_id,
9634 			decode(patd.transaction_type,'T',run_rec.target_exp_org_id,
9635 				run_rec.offset_exp_org_id))
9636            ,       decode(p_rule_id,
9637                           -1,pa_utils4.GetOrgBusinessGrpId(patd.cint_exp_org_id ),
9638                           decode(patd.transaction_type,
9639                                  'T',pa_utils4.GetOrgBusinessGrpId(run_rec.target_exp_org_id)
9640                                   ,pa_utils4.GetOrgBusinessGrpId(run_rec.offset_exp_org_id)))
9641                 /* end of performance changes */
9642      from  pa_alloc_txn_details patd
9643          , pa_projects_all pp
9644          , pa_tasks        pt
9645     where  patd.run_id = p_run_id
9646       and  patd.project_id = pp.project_id
9647       and  patd.task_id    = pt.task_id    ;
9648     IF P_DEBUG_MODE = 'Y' THEN
9649        pa_debug.write_file('Release_alloc_txns: ' || 'LOG', to_char(SQL%ROWCOUNT)||' Records inserted');
9650     END IF;
9651     Commit ;
9652 ---  Call import procedure to import the records ------
9653      Select pa_interface_id_s.nextval
9654        into v_interface_id
9655        from  dual ;
9656     IF P_DEBUG_MODE = 'Y' THEN
9657        pa_debug.write_file('Release_alloc_txns: ' || 'LOG', 'Calling Transaction Import ..' );
9658     END IF;
9659 /* Adding exception handling for bug 2749043 */
9660      BEGIN
9661      pa_trx_import.import1( l_transaction_source -- Changed this for capital project changes
9662                            , v_batch_name
9663                            , v_interface_id
9664                            , G_created_by
9665                            , NULL ) ;
9666      EXCEPTION
9667               WHEN OTHERS THEN
9668                 IF P_DEBUG_MODE = 'Y' THEN
9669        		pa_debug.write_file('Release_alloc_txns: ' || 'LOG', 'Error occurred in Transaction Import' );
9670     		END IF;
9671      END;
9672 --- Update txn_details table with import info ---------
9673     IF P_DEBUG_MODE = 'Y' THEN
9674        pa_debug.write_file('Release_alloc_txns: ' || 'LOG', 'Updating the transactions with import results');
9675     END IF;
9676     update pa_alloc_txn_details patd
9677        set (   status_code
9678              , rejection_code
9679              , expenditure_id
9680              , expenditure_item_id) =
9681                ( select transaction_status_code
9682                         ,transaction_rejection_code
9683                         , expenditure_id
9684                         , expenditure_item_id
9685                    from pa_transaction_interface_all pti
9686                   where pti.orig_transaction_reference = to_char(alloc_txn_id)
9687                     and pti.transaction_source = l_transaction_source -- Changed this for capital project changes
9688                     and pti.batch_name         = v_batch_name )
9689      where run_id = p_run_id ;
9690     IF P_DEBUG_MODE = 'Y' THEN
9691        pa_debug.write_file('Release_alloc_txns: ' || 'LOG', to_char(SQL%ROWCOUNT)||' Records updated');
9692     END IF;
9693 
9694  	   /**** added for bug 6243121 ***/
9695 
9696  	              ---    Alloc Exceptions status is used in report to populate 'Release status' of the release allocations run.
9697  	              ---    If generate allocations completes well, pa_alloc_exceptions will have no errors for the run and
9698  	              ---    run status is updated to 'RS' even if transaction import fails. Hence adding this code to reflect the
9699  	              ---    transaction import result into pa_alloc_exceptions. Also added code in allocation_run to
9700  	              ---    update the run_status_code conditionally based on whether any errors exist or not.
9701 
9702  	    begin
9703  	            for r1 in c_alloc_txn_err(p_run_id) loop
9704 
9705  	                pa_alloc_run.ins_alloc_exceptions( p_rule_id, p_run_id, G_creation_date,
9706  	                                                  G_created_by, G_last_update_date,
9707  	                                                  G_last_updated_by, G_last_update_login,
9708  	                                                 'T', 'E', NULL, NULL, r1.rejection_code);
9709  	            end loop;
9710 
9711  	    end;
9712  	    /* added for bug 6243121 */
9713 
9714      BEGIN
9715           select 'Y'
9716             into  v_import_failed
9717             from  dual
9718            where  EXISTS ( select 'exists'
9719                        from  pa_alloc_txn_details
9720                       where run_id = p_run_id
9721                         and status_code = 'R' ) ;
9722      EXCEPTION
9723                                  When NO_DATA_FOUND then
9724                                    v_import_failed := 'N' ;
9725                    END;
9726      IF P_DEBUG_MODE = 'Y' THEN
9727         pa_debug.write_file('Release_alloc_txns: ' || 'LOG', 'v_import_failed :' ||v_import_failed );
9728      END IF;
9729      v_tgt_exp_group :=  v_batch_name||run_rec.target_exp_type_class||to_char(v_interface_id);
9730      v_off_exp_group :=  v_batch_name||run_rec.offset_exp_type_class||to_char(v_interface_id);
9731       If v_import_failed ='Y' then
9732          For exp_rec in C_expenditure LOOP
9733              delete from pa_expenditure_items
9734              where  expenditure_id = exp_rec.expenditure_id ;
9735              delete from pa_expenditures
9736              where  expenditure_id = exp_rec.expenditure_id ;
9737          END LOOP ;
9738          Delete from pa_expenditure_groups
9739          where expenditure_group in ( v_tgt_exp_group, v_off_exp_group) ;
9740       else
9741         --Modifed this for capital project changes.
9742         update pa_alloc_runs
9743            set target_exp_group = v_tgt_exp_group ,
9744                offset_exp_group = decode(p_rule_id,
9745                                          -1,null,
9746                                          decode(run_rec.offset_method,'N',NULL,v_off_exp_group) )
9747         where run_id = p_run_id ;
9748       End if ;
9749      if v_import_failed = 'Y' then
9750         --Added the code for capital project changes.
9751         IF p_rule_id = -1 THEN
9752               x_errbuf := 'PA_CINT_RELEASE_FAILED' ;
9753         ELSE
9754               x_errbuf := 'PA_AL_RELEASE_FAILED' ;
9755         END IF;
9756      end if ;
9757 ----  Release Lock on the rule_name   ---------------------
9758 -- No  need unlock the rule here. Allocation_run procedure is called in the report (PAXALRUN)
9759 -- and this procedure locks and unlocks the rule.
9760 --    unlock_rule(p_rule_id, p_run_id) ;
9761    pa_debug.reset_err_stack ;
9762 EXCEPTION
9763     When OTHERS then
9764         RAISE ;
9765 END Release_alloc_txns ;
9766 -- ------------------------------------------------------------
9767 -- Reverse_alloc_txns
9768 -- ------------------------------------------------------------
9769 PROCEDURE Reverse_alloc_txns( p_rule_id          IN NUMBER
9770                              ,p_run_id           IN  NUMBER
9771                              ,p_tgt_exp_group    IN VARCHAR2
9772                              ,p_off_exp_group    IN VARCHAR2
9773                              ,x_retcode          OUT NOCOPY NUMBER
9774                              ,x_errbuf           OUT NOCOPY VARCHAR2
9775                             )
9776 IS
9777    Cursor C_run is
9778     Select  target_exp_group
9779           , offset_exp_group
9780       from  pa_alloc_runs
9781      where  run_id = p_run_id ;
9782    run_rec   C_run%ROWTYPE ;
9783    v_num_reversed     NUMBER ;
9784    v_num_rejected     NUMBER ;
9785    v_return_code      VARCHAR2(30) ;
9786 BEGIN
9787    -- pa_debug.G_process := 'SQL' ;
9788    pa_debug.Init_err_stack('Start') ;
9789    pa_debug.set_err_stack('Reverse_alloc_txns') ;
9790    pa_debug.G_err_stage := 'Reverse_alloc_txns' ;
9791    IF P_DEBUG_MODE = 'Y' THEN
9792       pa_debug.write_file('Reverse_alloc_txns: ' || 'LOG',pa_debug.G_err_stage);
9793    END IF;
9794    Init_who_cols ;
9795    lock_rule(p_rule_id, p_run_id) ;
9796 /*added for bug#2357646 */
9797    if (G_fatal_err_found) then
9798       G_fatal_err_found := FALSE;
9799        v_return_code := 'PA_AL_CANT_ACQUIRE_LOCK';
9800        COMMIT;
9801        x_retcode := -1 ;
9802        x_errbuf  := v_return_code ;
9803        return;
9804     end if;
9805 /*end of fix for bug#2357646 */
9806 ---- Check for the group name and call reverse group ------
9807    pa_debug.G_err_stage := 'Checking for expenditure groups' ;
9808    IF P_DEBUG_MODE = 'Y' THEN
9809       pa_debug.write_file('Reverse_alloc_txns: ' || 'LOG', pa_debug.G_err_stage);
9810    END IF;
9811    OPEN  C_run ;
9812    Fetch C_run into  run_rec ;
9813    Close C_run ;
9814    pa_exp_copy.reverseExpGroup(run_rec.target_exp_group
9815                                   ,p_tgt_exp_group
9816                                   ,G_created_by
9817                                   ,'PAXALRN'
9818                                   , v_num_reversed
9819                                   , v_num_rejected
9820                                   , v_return_code
9821                                   , 'RELEASED' ) ;
9822    If nvl(v_return_code,'0') <> '0' then
9823          IF P_DEBUG_MODE = 'Y' THEN
9824             pa_debug.write_file('Reverse_alloc_txns: ' || 'LOG', 'Reversing Error :'||v_return_code);
9825             pa_debug.write_file('Reverse_alloc_txns: ' || 'LOG','Records Reversed in Rejection :'||
9826                                  to_char(v_num_rejected) );
9827          END IF;
9828 --       pa_debug.raise_error('-20010',v_return_code) ;
9829    else
9830          IF P_DEBUG_MODE = 'Y' THEN
9831             pa_debug.write_file('Reverse_alloc_txns: ' || 'LOG','Records Reversed :'||
9832                                  to_char(v_num_reversed) );
9833          END IF;
9834    end if ;
9835    If run_rec.offset_exp_group is NOT NULL then
9836       If run_rec.offset_exp_group <> run_rec.target_exp_group then
9837          pa_exp_copy.reverseExpGroup(run_rec.offset_exp_group
9838                                      ,p_off_exp_group
9839                                      ,G_created_by
9840                                      ,'PAXALRN'
9841                                      , v_num_reversed
9842                                      , v_num_rejected
9843                                      , v_return_code
9844                                      , 'RELEASED' ) ;
9845           if nvl(v_return_code,'0') <> '0' then
9846              IF P_DEBUG_MODE = 'Y' THEN
9847                 pa_debug.write_file('Reverse_alloc_txns: ' || 'LOG', 'Reversing Error :'||v_return_code);
9848                 pa_debug.write_file('Reverse_alloc_txns: ' || 'LOG','Records Reversed in Rejection :'||
9849                                  to_char(v_num_rejected) );
9850              END IF;
9851              pa_debug.raise_error('-20010',v_return_code) ;
9852           else
9853              IF P_DEBUG_MODE = 'Y' THEN
9854                 pa_debug.write_file('Reverse_alloc_txns: ' || 'LOG','Records Reversed :'||
9855                                  to_char(v_num_reversed) );
9856              END IF;
9857           end if ;
9858        End if;
9859    End If;
9860 --  Update the alloc runs table with the reversal info --
9861   If nvl(v_return_code,'0') = '0' then
9862     update pa_alloc_runs
9863       set  run_status = 'RV'
9864           ,reversal_date = trunc(sysdate)
9865           ,rev_target_exp_group = p_tgt_exp_group
9866           ,rev_offset_exp_group = p_off_exp_group
9867      where run_id = p_run_id ;
9868   else
9869      x_retcode := -1 ;
9870      x_errbuf  := v_return_code ;
9871   end if ;
9872 ----  Release Lock on the rule_name   ---------------------
9873    unlock_rule(p_rule_id, p_run_id) ;
9874    pa_debug.reset_err_stack ;
9875 EXCEPTION
9876     WHEN OTHERS THEN
9877        --Added this code for capital project changes
9878        BEGIN
9879             IF p_rule_id = -1 THEN
9880                   unlock_rule(p_rule_id,p_run_id);
9881             END IF;
9882        EXCEPTION
9883             WHEN OTHERS THEN
9884                   NULL;
9885        END;
9886        RAISE ;
9887 END Reverse_alloc_txns ;
9888 -- ------------------------------------------------------------
9889 -- Delete_alloc_txns
9890 -- ------------------------------------------------------------
9891 PROCEDURE Delete_alloc_txns( p_rule_id  IN NUMBER
9892                              ,p_run_id   IN  NUMBER)
9893 IS
9894 BEGIN
9895 -- pa_debug.G_process := 'SQL' ;
9896    pa_debug.Init_err_stack('Start') ;
9897    pa_debug.set_err_stack('Delete_alloc_txns') ;
9898    pa_debug.G_err_stage := 'Delete_alloc_txns' ;
9899    IF P_DEBUG_MODE = 'Y' THEN
9900       pa_debug.write_file('Delete_alloc_txns: ' || 'LOG',pa_debug.G_err_stage);
9901    END IF;
9902    Init_who_cols ;
9903    lock_rule(p_rule_id, p_run_id) ;
9904 --  Delete Transactions from pa_alloc_txn_details
9905     While 1=1 Loop /* Bug 2176096 */
9906     Delete from pa_alloc_txn_details
9907     where run_id = p_run_id and rownum < 10001;
9908 /* Commented for Bug 2984871 Commit; */
9909     If Sql%Rowcount = 0 THen
9910       Exit;
9911     End If;
9912    /*Code Changes for Bug No.2984871 start */
9913    Commit;
9914    /*Code Changes for Bug No.2984871 end */
9915     End Loop;
9916 --- Delete Transactions from source_details
9917     While 1=1 Loop /* Bug 2176096 */
9918     Delete from pa_alloc_run_source_det
9919     where  run_id = p_run_id and rownum < 10001;
9920 /* Commented for Bug 2984871 Commit; */
9921     If Sql%Rowcount = 0 THen
9922       Exit;
9923     End If;
9924    /*Code Changes for Bug No.2984871 start */
9925    Commit;
9926    /*Code Changes for Bug No.2984871 end */
9927     End Loop;
9928 --- Delete Transactions from  basis_details
9929     While 1=1 Loop /* Bug 2176096 */
9930     Delete from pa_alloc_run_basis_det
9931     where run_id = p_run_id and rownum < 10001;
9932 /* Commented for Bug 2984871 Commit; */
9933     If Sql%Rowcount = 0 THen
9934       Exit;
9935     End If;
9936    /*Code Changes for Bug No.2984871 start */
9937    Commit;
9938    /*Code Changes for Bug No.2984871 end */
9939     End Loop;
9940 --  Delete Transactions from pa_alloc_run_sources
9941     While 1=1 Loop /* Bug 2176096 */
9942     Delete from pa_alloc_run_sources
9943     where run_id = p_run_id and rownum < 10001;
9944 /* Commented for Bug 2984871 Commit; */
9945     If Sql%Rowcount = 0 THen
9946       Exit;
9947     End If;
9948    /*Code Changes for Bug No.2984871 start */
9949    Commit;
9950    /*Code Changes for Bug No.2984871 end */
9951     End Loop;
9952 --  Delete Transactions from pa_alloc_run_targets
9953     While 1=1 Loop /* Bug 2176096 */
9954     Delete from pa_alloc_run_targets
9955      where run_id = p_run_id and rownum < 10001;
9956 /* Commented for Bug 2984871 Commit; */
9957     If Sql%Rowcount = 0 THen
9958       Exit;
9959     End If;
9960    /*Code Changes for Bug No.2984871 start */
9961    Commit;
9962    /*Code Changes for Bug No.2984871 end */
9963     End Loop;
9964 --  Delete Transactions from pa_alloc_run_resource_det
9965     While 1=1 Loop /* Bug 2182563 */
9966     Delete from pa_alloc_run_resource_det
9967      where run_id = p_run_id and rownum < 10001;
9968 /* Commented for Bug 2984871 Commit; */
9969     If Sql%Rowcount = 0 Then
9970       Exit;
9971     End If;
9972    /*Code Changes for Bug No.2984871 start */
9973    Commit;
9974    /*Code Changes for Bug No.2984871 end */
9975     End Loop;
9976 --  Delete Transactions from pa_alloc_missing_costs
9977     Delete from pa_alloc_missing_costs
9978     where run_id = p_run_id ;
9979 --  Delete Transactions from pa_alloc_exceptions
9980     Delete from pa_alloc_exceptions
9981     where run_id = p_run_id ;
9982 --  Delete Transactions from pa_alloc_runs
9983     Delete from pa_alloc_runs
9984      where run_id = p_run_id ;
9985 ----  Release Lock on the rule_name   ---------------------
9986    unlock_rule(p_rule_id , p_run_id) ;
9987    pa_debug.reset_err_stack ;
9988 EXCEPTION
9989      WHEN OTHERS THEN
9990      RAISE ;
9991 END Delete_alloc_txns ;
9992 -- ------------------------------------------------------------
9993 -- Delete_alloc_run
9994 -- ------------------------------------------------------------
9995 PROCEDURE Delete_alloc_run(
9996                            errbuf                  OUT NOCOPY VARCHAR2,
9997                            retcode                 OUT NOCOPY VARCHAR2,
9998                            p_rule_id  IN NUMBER
9999                            )
10000 IS
10001    CURSOR get_run_id
10002      IS
10003         select run_id from pa_alloc_runs_all
10004           where rule_id = p_rule_id
10005           and  run_status = 'DL';
10006    l_run_id NUMBER;
10007     v_debug_mode VARCHAR2(2);
10008     v_process_mode VARCHAR2(10);
10009     --Declared these variables for capital project changes
10010     l_return_status      VARCHAR2(1);
10011     l_msg_data           VARCHAR2(1000);
10012     l_msg_count          NUMBER :=0;
10013 BEGIN
10014    -- pa_debug.G_process := 'SQL' ;
10015    -- v_debug_mode := NVL(p_debug_mode, 'Y');
10016    --v_process_mode := NVL(p_process_mode, 'SQL');
10017    pa_debug.Init_err_stack('Start') ;
10018    v_debug_mode := 'Y';
10019    v_process_mode := 'PLSQL';
10020    pa_debug.set_process(v_process_mode, 'LOG', v_debug_mode) ;
10021    pa_debug.set_err_stack('Delete_alloc_run') ;
10022    pa_debug.G_err_stage := 'Delete_alloc_run' || To_char(p_rule_id) ;
10023    IF P_DEBUG_MODE = 'Y' THEN
10024       pa_debug.write_file('Delete_alloc_run: ' || 'LOG',pa_debug.G_err_stage);
10025    END IF;
10026    Init_who_cols ;
10027    FOR run_rec IN get_run_id LOOP
10028       l_run_id := run_rec.run_id;
10029       pa_debug.G_err_stage := 'Delete Rule Id' || To_char(p_rule_id) ;
10030       IF P_DEBUG_MODE = 'Y' THEN
10031          pa_debug.write_file('Delete_alloc_run: ' || 'LOG',pa_debug.G_err_stage);
10032       END IF;
10033       pa_debug.G_err_stage := 'Delete Run Id' || To_char(l_run_id) ;
10034       IF P_DEBUG_MODE = 'Y' THEN
10035          pa_debug.write_file('Delete_alloc_run: ' || 'LOG',pa_debug.G_err_stage);
10036       END IF;
10037       --Delete all the txn source details when this api is called in
10038       --the context of Capitalized Interest
10039       IF p_rule_id = -1 THEN
10040             pa_alloc_run.delete_cint_source_dets
10041            ( p_run_id         => l_run_id
10042             ,x_return_status  => l_return_status
10043             ,x_msg_data       => l_msg_data
10044             ,x_msg_count      => l_msg_count);
10045             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10046                  IF P_DEBUG_MODE = 'Y' THEN
10047                       pa_debug.G_err_stage := 'delte cint sources errored out' ;
10048                       pa_debug.write_file('Delete_alloc_run: ' || 'LOG',pa_debug.G_err_stage);
10049                  END IF;
10050                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10051             END IF;
10052      END IF;
10053 --   lock_rule(p_rule_id, l_run_id) ;
10054 --  Delete Transactions from pa_alloc_txn_details
10055     While 1=1 Loop /* Bug 2176096 */
10056     Delete from pa_alloc_txn_details
10057     where run_id = l_run_id and rownum < 10001;
10058 /* Commented for Bug 2984871 Commit; */
10059     If Sql%Rowcount = 0 THen
10060       Exit;
10061     End If;
10062    /*Code Changes for Bug No.2984871 start */
10063    Commit;
10064    /*Code Changes for Bug No.2984871 end */
10065     End Loop;
10066 --- Delete Transactions from source_details
10067     While 1=1 Loop /* Bug 2176096 */
10068     Delete from pa_alloc_run_source_det
10069     where  run_id = l_run_id and rownum < 10001;
10070 /* Commented for Bug 2984871 Commit; */
10071     If Sql%Rowcount = 0 THen
10072       Exit;
10073     End If;
10074    /*Code Changes for Bug No.2984871 start */
10075    Commit;
10076    /*Code Changes for Bug No.2984871 end */
10077     End Loop;
10078 --- Delete Transactions from  basis_details
10079     While 1=1 Loop /* Bug 2176096 */
10080     Delete from pa_alloc_run_basis_det
10081     where run_id = l_run_id and rownum < 10001;
10082 /* Commented for Bug 2984871 Commit; */
10083     If Sql%Rowcount = 0 THen
10084       Exit;
10085     End If;
10086    /*Code Changes for Bug No.2984871 start */
10087    Commit;
10088    /*Code Changes for Bug No.2984871 end */
10089     End Loop;
10090 --  Delete Transactions from pa_alloc_run_sources
10091     While 1=1 Loop /* Bug 2176096 */
10092     Delete from pa_alloc_run_sources
10093     where run_id = l_run_id and rownum < 10001;
10094 /* Commented for Bug 2984871 Commit; */
10095     If Sql%Rowcount = 0 THen
10096       Exit;
10097     End If;
10098    /*Code Changes for Bug No.2984871 start */
10099    Commit;
10100    /*Code Changes for Bug No.2984871 end */
10101     End Loop;
10102 --  Delete Transactions from pa_alloc_run_targets
10103     While 1=1 Loop /* Bug 2176096 */
10104     Delete from pa_alloc_run_targets
10105      where run_id = l_run_id and rownum < 10001;
10106 /* Commented for Bug 2984871 Commit; */
10107     If Sql%Rowcount = 0 THen
10108       Exit;
10109     End If;
10110    /*Code Changes for Bug No.2984871 start */
10111    Commit;
10112    /*Code Changes for Bug No.2984871 end */
10113     End Loop;
10114 --  Delete Transactions from pa_alloc_run_resource_det
10115     While 1=1 Loop /* Bug 2182563 */
10116     Delete from pa_alloc_run_resource_det
10117      where run_id = l_run_id and rownum < 10001;
10118 /* Commented for Bug 2984871 Commit; */
10119     If Sql%Rowcount = 0 Then
10120       Exit;
10121     End If;
10122    /*Code Changes for Bug No.2984871 start */
10123    Commit;
10124    /*Code Changes for Bug No.2984871 end */
10125     End Loop;
10126 --  Delete Transactions from pa_alloc_missing_costs
10127     Delete from pa_alloc_missing_costs
10128     where run_id = l_run_id ;
10129 --  Delete Transactions from pa_alloc_exceptions
10130     Delete from pa_alloc_exceptions
10131     where run_id = l_run_id ;
10132 --  Delete Transactions from pa_alloc_runs
10133     Delete from pa_alloc_runs
10134      where run_id = l_run_id ;
10135 ----  Release Lock on the rule_name   ---------------------
10136     --unlock_rule(p_rule_id , l_run_id) ;
10137    END LOOP;
10138    pa_debug.reset_err_stack ;
10139 EXCEPTION
10140      WHEN OTHERS THEN
10141      RAISE ;
10142 END Delete_alloc_run ;
10143 --------------------------------------------------------------------------
10144 --Function:  Is_src_project_valid
10145 --Purpose: validating source project_id returned from source client extension
10146 ----------------------------------------------------------------------------
10147 FUNCTION Is_src_project_valid(p_project_id IN NUMBER)
10148                                            RETURN VARCHAR2
10149 IS
10150 CURSOR C_valid_project is
10151  SELECT '1'
10152  FROM dual
10153  WHERE EXISTS (SELECT 'Y'
10154                FROM  pa_alloc_src_projects_v ps
10155                WHERE ps.project_id=p_project_id);
10156 v_ret_code varchar2(1) ;
10157 v_dummy  varchar2(1);
10158 BEGIN
10159   v_ret_code := 'N';
10160   OPEN  C_valid_project;
10161   FETCH C_valid_project INTO v_dummy;
10162   IF C_valid_project%FOUND THEN
10163      v_ret_code := 'Y' ;
10164   END IF;
10165   CLOSE C_valid_project;
10166   RETURN v_ret_code;
10167 EXCEPTION
10168   WHEN NO_DATA_FOUND THEN
10169      v_ret_code := 'N' ;
10170      Return v_ret_code ;
10171   WHEN OTHERS THEN
10172   RAISE;
10173 END  is_src_project_valid;
10174 --------------------------------------------------------------------------
10175 --Function:  Is_src_task_valid
10176 --Purpose: validating source task_id returned from source client extension
10177 ----------------------------------------------------------------------------
10178 FUNCTION Is_src_task_valid(p_project_id IN NUMBER,p_task_id IN NUMBER)
10179                                            RETURN VARCHAR2
10180 IS
10181 CURSOR C_valid_task is
10182  SELECT '1'
10183  FROM dual
10184  WHERE EXISTS (SELECT 'Y'
10185                FROM  pa_alloc_src_tasks_v pt
10186                WHERE pt.project_id=p_project_id
10187                and pt.task_id=p_task_id);
10188 v_ret_code varchar2(1) ;
10189 v_dummy  varchar2(1);
10190 BEGIN
10191   v_ret_code := 'N';
10192   OPEN  C_valid_task;
10193   FETCH C_valid_task INTO v_dummy;
10194   IF C_valid_task%FOUND THEN
10195      v_ret_code := 'Y' ;
10196   END IF;
10197   CLOSE C_valid_task;
10198   RETURN v_ret_code;
10199 EXCEPTION
10200   WHEN NO_DATA_FOUND THEN
10201      v_ret_code := 'N' ;
10202      Return v_ret_code ;
10203   WHEN OTHERS THEN
10204   RAISE;
10205 END  is_src_task_valid;
10206 --------------------------------------------------------------------------
10207 --Function:  Is_tgt_project_valid
10208 --Purpose: validating target project_id returned from target client extension
10209 ----------------------------------------------------------------------------
10210 FUNCTION Is_tgt_project_valid(p_project_id IN NUMBER)
10211                                            RETURN VARCHAR2
10212 IS
10213 CURSOR C_valid_project is
10214  SELECT '1'
10215  FROM dual
10216  WHERE EXISTS (SELECT 'Y'
10217                FROM  pa_alloc_target_proj_v pap
10218                WHERE pap.project_id=p_project_id);
10219 v_ret_code varchar2(1) ;
10220 v_dummy  varchar2(1);
10221 BEGIN
10222   v_ret_code := 'N';
10223   OPEN  C_valid_project;
10224   FETCH C_valid_project INTO v_dummy;
10225   IF C_valid_project%FOUND THEN
10226      v_ret_code := 'Y' ;
10227   END IF;
10228   CLOSE C_valid_project;
10229   RETURN v_ret_code;
10230 EXCEPTION
10231   WHEN NO_DATA_FOUND THEN
10232      v_ret_code := 'N' ;
10233      Return v_ret_code ;
10234   WHEN OTHERS THEN
10235   RAISE;
10236 END  is_tgt_project_valid;
10237 --------------------------------------------------------------------------
10238 --Function:  Is_tgt_task_valid
10239 --Purpose: validating target task_id returned from target client extension
10240 ----------------------------------------------------------------------------
10241 FUNCTION Is_tgt_task_valid(p_project_id IN NUMBER,p_task_id IN NUMBER)
10242                                            RETURN VARCHAR2
10243 IS
10244 CURSOR C_valid_task is
10245  SELECT '1'
10246  FROM dual
10247  WHERE EXISTS (SELECT 'Y'
10248                FROM  pa_alloc_tgt_tasks_v pt
10249                WHERE pt.project_id=p_project_id
10250                and pt.task_id=p_task_id);
10251 v_ret_code varchar2(1) ;
10252 v_dummy  varchar2(1);
10253 BEGIN
10254   v_ret_code := 'N';
10255   OPEN  C_valid_task;
10256   FETCH C_valid_task INTO v_dummy;
10257   IF C_valid_task%FOUND THEN
10258      v_ret_code := 'Y' ;
10259   END IF;
10260   CLOSE C_valid_task;
10261   RETURN v_ret_code;
10262 EXCEPTION
10263   WHEN NO_DATA_FOUND THEN
10264      v_ret_code := 'N' ;
10265      Return v_ret_code ;
10266   WHEN OTHERS THEN
10267   RAISE;
10268 END  is_tgt_task_valid;
10269 --------------------------------------------------------------------------
10270 --Function:  Is_offset_project_valid
10271 --Purpose: validating offset project_id returned from offset client extension
10272 ----------------------------------------------------------------------------
10273 FUNCTION Is_offset_project_valid(p_project_id IN NUMBER)
10274                                            RETURN VARCHAR2
10275 IS
10276 CURSOR C_valid_project is
10277  SELECT '1'
10278  FROM dual
10279  WHERE EXISTS (SELECT 'Y'
10280                FROM  pa_alloc_offset_projects_v pap
10281                WHERE pap.project_id=p_project_id);
10282 v_ret_code varchar2(1) ;
10283 v_dummy  varchar2(1);
10284 BEGIN
10285   v_ret_code := 'N';
10286   OPEN  C_valid_project;
10287   FETCH C_valid_project INTO v_dummy;
10288   IF C_valid_project%FOUND THEN
10289      v_ret_code := 'Y' ;
10290   END IF;
10291   CLOSE C_valid_project;
10292   RETURN v_ret_code;
10293 EXCEPTION
10294   WHEN NO_DATA_FOUND THEN
10295      v_ret_code := 'N' ;
10296      Return v_ret_code ;
10297   WHEN OTHERS THEN
10298   RAISE;
10299 END  is_offset_project_valid;
10300 --------------------------------------------------------------------------
10301 --Function:  Is_offset_task_valid
10302 --Purpose: validating offset task_id returned from offset client extension
10303 ----------------------------------------------------------------------------
10304 FUNCTION Is_offset_task_valid(p_project_id IN NUMBER,p_task_id IN NUMBER)
10305                                            RETURN VARCHAR2
10306 IS
10307 CURSOR C_valid_task is
10308  SELECT '1'
10309  FROM dual
10310  WHERE EXISTS (SELECT 'Y'
10311                FROM  pa_alloc_tgt_tasks_v pt
10312                WHERE pt.project_id=p_project_id
10313                and pt.task_id=p_task_id);
10314 v_ret_code varchar2(1) ;
10315 v_dummy  varchar2(1);
10316 BEGIN
10317   v_ret_code := 'N';
10318   OPEN  C_valid_task;
10319   FETCH C_valid_task INTO v_dummy;
10320   IF C_valid_task%FOUND THEN
10321      v_ret_code := 'Y' ;
10322   END IF;
10323   CLOSE C_valid_task;
10324   RETURN v_ret_code;
10325 EXCEPTION
10326   WHEN NO_DATA_FOUND THEN
10327      v_ret_code := 'N' ;
10328      Return v_ret_code ;
10329   WHEN OTHERS THEN
10330   RAISE;
10331 END  is_offset_task_valid;
10332 --Added this procedure for Capital Project Enhancement. This procedure releases a capitalized interest run
10333 --This procedure is called from PA_CAP_INT_PVT. Generate_cap_interest when release button is pressed on the
10334 --Allocation form(when form is accessed in the context of capitalized interest) or when auto release flag is
10335 --passed Y
10336 PROCEDURE release_capint_txns
10337 ( p_run_id           IN   pa_alloc_runs_all.run_id%TYPE
10338  ,x_return_status    OUT  NOCOPY VARCHAR2
10339  ,x_msg_count        OUT  NOCOPY NUMBER
10340  ,x_msg_data         OUT  NOCOPY VARCHAR2
10341 )
10342 IS
10343 l_msg_count                     NUMBER := 0;
10344 l_data                          VARCHAR2(2000);
10345 l_msg_data                      VARCHAR2(2000);
10346 l_msg_index_out                 NUMBER;
10347 l_debug_mode                    VARCHAR2(1);
10348 l_debug_level3                  CONSTANT NUMBER := 3;
10349 l_debug_level5                  CONSTANT NUMBER := 5;
10350 l_rel_request_id                CONSTANT NUMBER := fnd_global.conc_request_id;
10351 completion_status               BOOLEAN;
10352 l_module_name                   VARCHAR2(100);
10353 BEGIN
10354       x_msg_count := 0;
10355       x_return_status := FND_API.G_RET_STS_SUCCESS;
10356       l_debug_mode  := NVL(FND_PROFILE.value('PA_DEBUG_MODE'),'N');
10357       l_module_name := 'release_capint_txns';
10358       pa_debug.set_curr_function( p_function   => 'release_capint_txns',
10359                                   p_debug_mode => l_debug_mode );
10360       IF l_debug_mode = 'Y' THEN
10361             pa_debug.G_err_stage := 'About to call release_alloc_txns';
10362             pa_debug.write_file('LOG',pa_debug.g_err_stage);
10363       END IF;
10364       --Initialise the who columns
10365       pa_alloc_run.init_who_cols;
10366       --Call the api that releases the allocation run
10367       pa_alloc_run.release_alloc_txns
10368                    ( p_rule_id  => -1
10369                     ,p_run_id   => p_run_id
10370                     ,x_retcode  => x_return_status
10371                     ,x_errbuf   => x_msg_data);
10372       IF l_debug_mode = 'Y' THEN
10373             pa_debug.G_err_stage := 'Returned after releasing with msg '||x_msg_data;
10374             pa_debug.write_file('LOG',pa_debug.g_err_stage);
10375       END IF;
10376       --If failed insert errors into alloc_exceptions table and raise error
10377       IF x_msg_data is NOT NULL then
10378             x_return_status := FND_API.G_RET_STS_ERROR;
10379             alloc_errors( -1, p_run_id, 'R', 'E',x_msg_data, FALSE );
10380       Else
10381 	    x_return_status := 'S';
10382       End if ;
10383       --Update the capital interest run status
10384       IF l_debug_mode = 'Y' THEN
10385             pa_debug.G_err_stage := 'About to update the Release status to ['||x_return_status||']';
10386             pa_debug.write_file('LOG',pa_debug.g_err_stage);
10387       END IF;
10388 	/* bug fix: 3123539  based on the return status update the run staus and avoid
10389          * setting the concurrent process to raise error
10390          */
10391 	IF x_return_status = 'S' Then
10392       		UPDATE pa_alloc_runs
10393       		SET    run_status = 'RS'
10394             		,release_request_id = l_rel_request_id
10395             		,release_request_date =sysdate
10396       		WHERE run_id = p_run_id;
10397        Else
10398             	-- Update the status to release failure
10399             	UPDATE pa_alloc_runs
10400             	SET    run_status = 'RF'
10401                   ,release_request_id = null
10402                   ,release_request_date =sysdate
10403             	WHERE  run_id = p_run_id;
10404 	End If;
10405 	/* bug fix:3123539 Setting these values as success
10406            the concurrent process should error out only for the unexpected errors
10407          */
10408       x_msg_count := 0;
10409       x_return_status := FND_API.G_RET_STS_SUCCESS;
10410       x_msg_data := Null;
10411       IF l_debug_mode = 'Y' THEN
10412            pa_debug.g_err_stage:= 'Leaving release_capint_txns return status ['||x_return_status||']';
10413            pa_debug.write_file('LOG',pa_debug.g_err_stage);
10414       END IF;
10415       pa_debug.reset_curr_function;
10416 EXCEPTION
10417       WHEN OTHERS THEN
10418             x_return_status := FND_API.G_RET_STS_ERROR;
10419             x_msg_count  := FND_MSG_PUB.count_msg;
10420             IF l_debug_mode = 'Y' THEN
10421                   x_msg_data:=sqlerrm;
10422                   pa_debug.write_file('cap_int run: ' ||  'LOG', x_msg_data);
10423                   pa_debug.write_file('cap_int run: ' ||  'LOG', pa_debug.G_err_stack);
10424                   pa_debug.G_err_stage := 'UPDATING RUN STATUS AS FAILURE';
10425                   pa_debug.write_file('cap_int: ' ||  'LOG', pa_debug.G_err_stage);
10426             END IF;
10427             -- Update the status to release failure
10428             UPDATE pa_alloc_runs
10429             SET    run_status = 'RF'
10430                   ,release_request_id = l_rel_request_id
10431                   ,release_request_date =sysdate
10432             WHERE  run_id = p_run_id;
10433             IF l_debug_mode = 'Y' THEN
10434                   pa_debug.write_file('LOG','p_run_id in when others of Main: '||to_char( p_run_id) );
10435             END IF;
10436             COMMIT;
10437             pa_debug.reset_curr_function;
10438             completion_status  := fnd_concurrent.set_completion_status('ERROR', SQLERRM);
10439 END release_capint_txns;
10440 --This procedure deletes the source details for each capital interest transaction. This procedure will
10441 --be called from delete_alloc_run api when the DELETE button is pressed to delete a capital interest
10442 --batch
10443 PROCEDURE delete_cint_source_dets
10444 ( p_run_id              IN  pa_alloc_runs_all.run_id%TYPE
10445  ,x_return_status       OUT NOCOPY VARCHAR2
10446  ,x_msg_count           OUT NOCOPY NUMBER
10447  ,x_msg_data            OUT NOCOPY VARCHAR2
10448 )
10449 IS
10450 l_msg_count                     NUMBER := 0;
10451 l_data                          VARCHAR2(2000);
10452 l_msg_data                      VARCHAR2(2000);
10453 l_msg_index_out                 NUMBER;
10454 l_debug_mode                    VARCHAR2(1);
10455 l_debug_level3                  CONSTANT NUMBER := 3;
10456 l_debug_level5                  CONSTANT NUMBER := 5;
10457 l_rel_request_id                CONSTANT NUMBER := fnd_global.conc_request_id;
10458 completion_status               BOOLEAN;
10459 l_module_name                   VARCHAR2(100);
10460 CURSOR c_get_cint_txns
10461 IS
10462 SELECT alloc_txn_id
10463 FROM   pa_alloc_txn_details
10464 WHERE  run_id=p_run_id;
10465 BEGIN
10466       x_msg_count := 0;
10467       x_return_status := FND_API.G_RET_STS_SUCCESS;
10468       l_debug_mode  := NVL(FND_PROFILE.value('PA_DEBUG_MODE'),'N');
10469       l_module_name := 'delete_cint_source_dets';
10470       pa_debug.set_curr_function( p_function   => l_module_name,
10471                                   p_debug_mode => l_debug_mode );
10472       IF l_debug_mode = 'Y' THEN
10473            pa_debug.g_err_stage:= 'About to delete the source txn details';
10474            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10475       END IF;
10476       FOR c_cint_txn_rec IN  c_get_cint_txns LOOP
10477             LOOP
10478                   DELETE
10479                   FROM   pa_cint_source_details
10480                   WHERE  alloc_txn_id = c_cint_txn_rec.alloc_txn_id
10481                   AND ROWNUM <1000;
10482                   IF SQL%ROWCOUNT <1000 THEN
10483                       EXIT;
10484                   END IF;
10485             END LOOP;
10486       END LOOP;
10487       IF l_debug_mode = 'Y' THEN
10488            pa_debug.g_err_stage:= 'Leaving delete_cint_source_dets';
10489            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10490       END IF;
10491       pa_debug.reset_curr_function;
10492 EXCEPTION
10493 WHEN others THEN
10494      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10495      x_msg_count     := 1;
10496      x_msg_data      := SQLERRM;
10497      FND_MSG_PUB.add_exc_msg
10498                    ( p_pkg_name        => 'pa_alloc_run'
10499                     ,p_procedure_name  => 'delete_cint_source_dets'
10500                     ,p_error_text      => x_msg_data);
10501      IF l_debug_mode = 'Y' THEN
10502           pa_debug.g_err_stage:= 'Unexpected Error'||x_msg_data;
10503           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
10504      END IF;
10505      pa_debug.reset_curr_function;
10506      RAISE;
10507 END delete_cint_source_dets;
10508 END PA_ALLOC_run;