DBA Data[Home] [Help]

PACKAGE BODY: APPS.PA_ALLOC_RUN

Source


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