DBA Data[Home] [Help]

PACKAGE BODY: APPS.GMS_FUNDS_CONTROL_PKG

Source


1 PACKAGE BODY gms_funds_control_pkg AS
2 -- $Header: gmsfcfcb.pls 120.62.12020000.4 2013/03/14 10:24:44 utdas ship $
3 
4   -- Private Global Variables :
5 
6     -- Variables initialized in gms_fck:
7         g_packet_id              gms_bc_packets.packet_id%type;
8         g_mode                   Varchar2(1);
9         g_debug                  Varchar2(1); -- To check on, whether to print debug messages in log file or not
10         g_error_program_name     Varchar2 (30);
11         g_error_procedure_name   Varchar2 (30);
12         g_error_stage            Varchar2 (50); -- Bug 5344693 : Increased the size of this variable.
13 
14     -- Variables initialized in gms_fck_init:
15         g_derived_mode              Varchar2(1);
16         g_partial_flag              Varchar2(1);
17         g_doc_type                  gms_bc_packets.document_type%type;
18 	g_non_gms_txn 	            BOOLEAN;
19         g_bc_packet_has_P82_records Varchar2(1); -- Used in handle_net_zero_txn/update_fc_sequence
20         g_ip_fc_flag	            VARCHAR2(1); -- If FC called for IP or not ..
21         g_gl_bc_pkt_sponsored_count NUMBER;      -- Count of sponsored transactions in gl_bc_packets(AP/PO/REQ)
22         g_pa_addition_flag_t_count  NUMBER;      -- Count of sponsored AP records with pa_addition_flag = 'T'
23 
24   -- Funds Check Return Code for the Packet processed.
25 	g_return_code		gms_bc_packets.result_code%TYPE;
26 
27       -- R12 Funds Management uptake : Defining global variables which are reffered by
28       -- copy_gl_pkt_to_gms_pkt and misc_sync_adls procedures
29       -- PLSQL type of variables for storing transaction data
30 
31       g_set_of_books_id_tab       t_set_of_books_id_type;
32       g_je_source_name_tab        t_je_source_name_type;
33       g_je_category_name_tab      t_je_category_name_type;
34       g_actual_flag_tab           t_actual_flag_type;
35       g_project_id_tab            t_project_id_type;
36       g_task_id_tab               t_task_id_type;
37       g_award_id_tab              t_award_id_type;
38       g_result_code_tab           t_result_code_type;
39       /* Bug 5614467 : g_entered_dr_tab and g_entered_cr_tab are populated with accounted amounts.
40                        g_txn_dr_tab and g_txn_cr_tab are populated with entered amounts. */
41       g_entered_dr_tab            t_entered_dr_type;
42       g_entered_cr_tab            t_entered_cr_type;
43       g_txn_dr_tab                t_entered_dr_type;
44       g_txn_cr_tab                t_entered_cr_type;
45       g_po_rate_tab               t_po_rate_type; -- Bug 5614467
46       g_etype_tab                 t_etype_type;
47       g_exp_org_id_tab            t_exp_org_id_type;
48       g_exp_item_date_tab         t_exp_item_date_type;
49       g_document_type_tab         t_document_type_type;
50       g_doc_header_id_tab         t_doc_header_id_type;
51       g_doc_dist_id_tab           t_doc_dist_id_type;
52       g_vendor_id_tab             t_vendor_id_type;
53       g_exp_category_tab          t_exp_category_type;
54       g_revenue_category_tab      t_revenue_category_type;
55       g_ind_cmp_set_id_tab        t_ind_cmp_set_id_type;
56       g_burdenable_raw_cost_tab   t_brc_type; --R12 AP Lines Uptake enhancement : Forward porting bug 4450291
57       g_parent_reversal_id_tab    t_parent_reversal_id_type; -- Bug 5369296
58       g_doc_dist_line_num_tab     t_doc_dist_line_num_type;
59       g_invoice_type_code_tab     t_invoice_type_code_type;
60       g_inv_source_tab            t_inv_source_type;
61       g_inv_dist_reference_1_tab  t_doc_dist_id_type;
62       g_inv_dist_reference_2_tab  t_doc_dist_line_num_type;
63       g_source_event_id_tab       t_source_event_id_type;
64       g_entered_amount_tab        t_entered_dr_type;
65       g_event_type_code_tab       t_event_type_code_type;
66       g_main_or_backing_tab       t_main_or_backing_type;
67       g_reference6_tab            t_reference6_type;
68       g_reference13_tab           t_reference13_type;
69 
70       g_ap_inv_dist_id            T_PROJ_BC_AP_DIST := T_PROJ_BC_AP_DIST(); -- variable of nested table type
71       g_ap_line_type_lkup         t_invoice_type_code_type;
72       g_prepay_std_inv_dist_id    t_doc_dist_id_type;
73       g_quantity_variance_tab     t_entered_dr_type;
74       g_amount_variance_tab       t_entered_dr_type;
75       g_po_distribution_id_tab    t_doc_dist_id_type;
76       g_po_header_id_tab          t_doc_dist_id_type;
77       g_po_release_id_tab         t_doc_dist_id_type;
78 
79       g_ap_prepay_app_dist_id     t_doc_dist_line_num_type;
80 
81 ----------------------------------------------------------------------------------------------------------
82 -- Variables for error handling .
83 
84    g_budget_version_id		NUMBER; -- Bug 2092791 (This is used in setup_start_end_date Logic)
85    g_gb_end_date		DATE ;	-- Bug 2092791 (This is used in setup_start_end_date Logic)
86    g_exp_date		        DATE ;  -- Bug 2092791 (This is used in setup_start_end_date Logic)
87 
88    -- Bug : 2557041 - Added for IP check funds Enhancement
89    -- gms_fck_init derives values for the following variables ...
90 
91 /* -----------------------------------------------------------------------------------------------
92    Function  : sponsored_project
93    Purpose   : Returns 'Y' if project parameter passed is sponsored, else returns 'N'
94 -------------------------------------------------------------------------------------------------- */
95 FUNCTION sponsored_project(p_project_id IN NUMBER)
96 RETURN VARCHAR2 IS
97 x_sponsored_flag varchar2(1);
98 BEGIN
99  Select nvl(gpt.sponsored_flag,'N')
100  into   x_sponsored_flag
101  from   gms_project_types gpt,
102         pa_projects_all   pp
103  where  pp.project_id    = p_project_id
104  and    gpt.project_type = pp.project_type;
105  RETURN x_sponsored_flag;
106 END sponsored_project;
107 
108 /* -----------------------------------------------------------------------------------------------
109    Procedure : lock_budget_versions (Bug 4053891)
110    Purpose   : - This procedure will lock the budget version records for the budget versions
111                  in the packet being funds checked.
112                - Fired only for REQ/PO/AP/FAB/Interface.
113                - This was reqd. to enforce incompatibility between sweeper and FC
114                  for REQ/PO/AP/FAB/Interface.
115 -------------------------------------------------------------------------------------------------- */
116 Procedure Lock_budget_versions(p_packet_id number) is
117 Cursor c_lock_bvid is
118        select budget_version_id
119        from  gms_budget_versions
120        where budget_version_id in
121        (select budget_version_id from  gms_bc_packets_bvid)
122        for update;
123 Begin
124 for x in  c_lock_bvid loop
125     null; -- Dummy code to lock gms_budget_versions
126 end loop;
127 End Lock_budget_versions;
128 
129 /* -----------------------------------------------------------------------------------------------
130    Procedure : update_status_on_failed_txns
131    Purpose   : Update status code on failed transactions. Used for expenditure items.
132 
133 -------------------------------------------------------------------------------------------------- */
134 
135   Procedure update_status_on_failed_txns(p_packet_id IN Number)
136   IS
137   BEGIN
138 
139    g_error_procedure_name := 'update_status_on_failed_txns';
140 
141    UPDATE gms_bc_packets
142       SET status_code = 'R'
143     WHERE packet_id = p_packet_id
144       AND result_code like 'F%';
145 
146   END update_status_on_failed_txns;
147 
148 /* -----------------------------------------------------------------------------------------------
149    Procedure : delete_pending_txns
150    Purpose   : This procedure will delete pending records in gms_bc_packets associated with a
151                request that has been terminated.
152                After deleting the records from gms_bc_packets, corresponding request_id entry will
153                be deleted from gms_concurrency_control table.
154 -------------------------------------------------------------------------------------------------- */
155 
156 Procedure delete_pending_txns
157 (x_err_code                 OUT NOCOPY      NUMBER,
158  x_err_buff                 OUT NOCOPY      VARCHAR2 ) IS
159 
160  RESOURCE_BUSY EXCEPTION;
161  PRAGMA EXCEPTION_INIT(RESOURCE_BUSY, -00054);
162 
163  l_request_id    	NUMBER;
164  l_phase_code       fnd_concurrent_requests.phase_code%type;
165 
166  Cursor c_pending_request_id is
167         select request_id
168         from   gms_concurrency_control
169         where  request_id <> nvl(l_request_id,-1)
170         and    process_name = 'GMSFCSYS';
171 Begin
172 
173   g_error_procedure_name := 'delete_pending_txns';
174   g_error_stage := ':Start';
175 
176   l_request_id  := fnd_global.conc_request_id;
177 
178   IF g_debug = 'Y' THEN
179      gms_error_pkg.gms_debug (g_error_procedure_name||g_error_stage,'C');
180   END IF;
181 
182  For c_request_id in c_pending_request_id
183  Loop
184     -- A. Get the phase code of the concurrent request
185     g_error_stage := ':Derive phase';
186 
187     Begin
188        select phase_code
189        into   l_phase_code
190        from   fnd_concurrent_requests
191        where  request_id =  c_request_id.request_id;
192     Exception
193        When no_data_found then
194             -- requests were purged ..
195             l_phase_code := 'C';
196      End;
197 
198     IF g_debug = 'Y' THEN
199      gms_error_pkg.gms_debug (g_error_procedure_name||g_error_stage||' - request_id,phase_code:'||
200                                 c_request_id.request_id||','||l_phase_code,'C');
201     END IF;
202 
203      -- Phase Code 'C' indicates that the process has been completed
204     If l_phase_code = 'C' then
205 
206          -- C. Update gms_bc_packet status to 'T'
207          Update gms_bc_packets
208          set    status_code = 'T',
209                 fc_error_message = 'Packet had terminated,status updated to (T) by document_type,request_id:'||l_request_id||g_doc_type
210          where  request_id  = c_request_id.request_id
211          and    status_code = 'P';
212 
213          -- D. Delete concurrency record ..
214          Delete
215          from  gms_concurrency_control
216          where  request_id  = c_request_id.request_id;
217 
218    End If; --   If l_phase_code = 'C' then
219 
220       l_phase_code := null;
221       COMMIT;
222  End loop;
223 
224   g_error_stage := ':End';
225 
226   IF g_debug = 'Y' THEN
227      gms_error_pkg.gms_debug (g_error_procedure_name||g_error_stage,'C');
228   END IF;
229 
230 Exception
231   -- Handling exception, as any failure to this procedure need not fail the calling process
232   -- It's upto the calling process to pass or fail the Main code flow ..
233   When Others then
234       x_err_code := SQLCODE;
235       x_err_buff := SQLERRM;
236 End delete_pending_txns;
237 
238 /* ---------------------------------------------------------------------------------------
239    PROCEDURE RAW_BURDEN_FAILURE : This procedure will fail
240                                   raw transaction if burden failed: F75
241                                   burden transaction if raw failed: F63
242    !!!! Note: Burden_adjustment entries will be treated as burden of original transaction.
243 
244    Parameters: p_packet_id : packet_id of the batch being FC'ed
245                p_mode      : Mode in which FC is being carried out (Use g_derived_mode)
246                p_level     : Funds Check level from which it is being called
247                              'AWD'  : Award,
248                              'TTSK' : Top Task,
249 			     'TSK'  : Task,
250                              'RESG' : Resource Group,
251 			     'RES'  : Resource and
252                              'ALL'  : From Check funds etc ..
253 -------------------------------------------------------------------------------------------*/
254 PROCEDURE Raw_burden_failure(p_packet_id IN NUMBER, p_mode in VARCHAR2,p_level in VARCHAR2) IS
255 BEGIN
256  g_error_procedure_name := 'RAW_BURDEN_FAILURE';
257  -- F75 : Transaction Failed because of Raw
258  /*    Update gms_bc_packets bp  Commented for bug 5726575; Moved below the update below next update
259        set bp.status_code          = decode(p_mode,'C','F','R'),
260            bp.result_code          = decode(substr(bp.result_code,1,1),'P','F75',null,'F75',bp.result_code),
261 	   bp.fc_error_message     = decode(bp.fc_error_message,NULL,'RAW_BURDEN_FAILURE at stage:'||p_level,bp.fc_error_message)
262      where bp.packet_id            = p_packet_id
263        and bp.status_code          = 'P'
264        and ((p_level = 'RES'  and substr(bp.res_result_code,1,1)     = 'P') or
265             (p_level = 'RESG' and substr(bp.res_grp_result_code,1,1) = 'P') or
266             (p_level = 'TSK'  and substr(bp.task_result_code,1,1)    = 'P') or
267             (p_level = 'TTSK' and substr(bp.top_task_result_code,1,1)= 'P') or
268             (p_level = 'AWD'  and substr(bp.award_result_code,1,1)   = 'P') or
269             (p_level = 'ALL'  and substr(nvl(bp.result_code,'P'),1,1)         = 'P')
270             )
271        and bp.parent_bc_packet_id IS NOT NULL
272        and exists
273            (select 1
274             from   gms_bc_packets bp1
275             where  bp1.packet_id    = bp.packet_id
276             and    bp1.bc_packet_id = bp.parent_bc_packet_id   -- GMS_BC_PACKETS_U1
277             and ((p_level = 'RES'  and substr(bp1.res_result_code,1,1)     = 'F') or
278                  (p_level = 'RESG' and substr(bp1.res_grp_result_code,1,1) = 'F') or
279                  (p_level = 'TSK'  and substr(bp1.task_result_code,1,1)    = 'F') or
280                  (p_level = 'TTSK' and substr(bp1.top_task_result_code,1,1)= 'F') or
281                  (p_level = 'AWD'  and substr(bp1.award_result_code,1,1)   = 'F') or
282                  (p_level = 'ALL'  and substr(nvl(bp1.result_code,'P'),1,1)         = 'F')
283                  )
284             );*/
285         -- Records that has failed will not have status_code update to 'R' at this stage ..
286         -- Only way to check in main cursor is result_code 'P' ..
287 
288     -- F63 : Transaction Failed because of Burden
289      Update gms_bc_packets bp
290        set bp.status_code          = decode(p_mode,'C','F','R'),
291            bp.result_code          = decode(substr(bp.result_code,1,1),'P','F63',null,'F63',bp.result_code),
292 	   bp.fc_error_message     = decode(bp.fc_error_message,NULL,'RAW_BURDEN_FAILURE at stage:'||p_level,bp.fc_error_message)
293      where bp.packet_id            = p_packet_id
294        and bp.status_code          = 'P'
295        and ((p_level = 'RES'  and substr(bp.res_result_code,1,1)     = 'P') or
296             (p_level = 'RESG' and substr(bp.res_grp_result_code,1,1) = 'P') or
297             (p_level = 'TSK'  and substr(bp.task_result_code,1,1)    = 'P') or
298             (p_level = 'TTSK' and substr(bp.top_task_result_code,1,1)= 'P') or
299             (p_level = 'AWD'  and substr(bp.award_result_code,1,1)   = 'P') or
300             (p_level = 'ALL'  and substr(nvl(bp.result_code,'P'),1,1)         = 'P')
301             )
302        and bp.parent_bc_packet_id IS NULL
303        and exists
304            (select /*+ push_subq no_unnest */ 1 /* Hint added for bug 14685623 */
305             from   gms_bc_packets bp1
306             where  bp1.packet_id           = bp.packet_id
307             and    bp1.parent_bc_packet_id = bp.bc_packet_id   -- GMS_BC_PACKETS_N3
308             and ((p_level = 'RES'  and substr(bp1.res_result_code,1,1)     = 'F') or
309                  (p_level = 'RESG' and substr(bp1.res_grp_result_code,1,1) = 'F') or
310                  (p_level = 'TSK'  and substr(bp1.task_result_code,1,1)    = 'F') or
311                  (p_level = 'TTSK' and substr(bp1.top_task_result_code,1,1)= 'F') or
312                  (p_level = 'AWD'  and substr(bp1.award_result_code,1,1)   = 'F') or
313                  (p_level = 'ALL'  and substr(nvl(bp1.result_code,'P'),1,1)         = 'F')
314                  )
315             );
316         -- Records that has failed will not have status_code update to 'R' at this stage ..
317         -- Only way to check in main cursor is result_code 'P' ..
318      --Bug 5726575
319      Update gms_bc_packets bp
320        set bp.status_code          = decode(p_mode,'C','F','R'),
321            bp.result_code          = decode(substr(bp.result_code,1,1),'P','F75',null,'F75',bp.result_code),
322 	   bp.fc_error_message     = decode(bp.fc_error_message,NULL,'RAW_BURDEN_FAILURE at stage:'||p_level,bp.fc_error_message)
323      where bp.packet_id            = p_packet_id
324        and bp.status_code          = 'P'
325        and ((p_level = 'RES'  and substr(bp.res_result_code,1,1)     = 'P') or
326             (p_level = 'RESG' and substr(bp.res_grp_result_code,1,1) = 'P') or
327             (p_level = 'TSK'  and substr(bp.task_result_code,1,1)    = 'P') or
328             (p_level = 'TTSK' and substr(bp.top_task_result_code,1,1)= 'P') or
329             (p_level = 'AWD'  and substr(bp.award_result_code,1,1)   = 'P') or
330             (p_level = 'ALL'  and substr(bp.result_code,1,1)         = 'P')
331             )
332        and bp.parent_bc_packet_id IS NOT NULL
333        and exists
334            (select /*+ push_subq no_unnest */ 1 /* Hint added for bug 14685623 */
335             from   gms_bc_packets bp1
336             where  bp1.packet_id    = bp.packet_id
337             and    bp1.bc_packet_id = bp.parent_bc_packet_id   -- GMS_BC_PACKETS_U1
338             and    substr(bp1.result_code,1,1) = 'F'
339             /*and ((p_level = 'RES'  and substr(bp1.res_result_code,1,1)     = 'F') or
340                  (p_level = 'RESG' and substr(bp1.res_grp_result_code,1,1) = 'F') or
341                  (p_level = 'TSK'  and substr(bp1.task_result_code,1,1)    = 'F') or
342                  (p_level = 'TTSK' and substr(bp1.top_task_result_code,1,1)= 'F') or
343                  (p_level = 'AWD'  and substr(bp1.award_result_code,1,1)   = 'F') or
344                  (p_level = 'ALL'  and substr(bp1.result_code,1,1)         = 'F')
345                  )*/
346             );
347 
348 END Raw_burden_failure;
349 
350 /* ===========================================================================
351    FUNCTION FULL_MODE_FAILURE:
352    This function will take of following failures (F65):
353    1. Full mode failures
354    2. Fail all cdl if one cdl failed for EXP
355    3. Fail all burden if one burden failed
356    Parameters: p_packet_id : packet_id of the batch being FC'ed
357                p_mode      : Mode in which FC is being carried out (use g_mode)
358                p_level     : Funds Check level from which it is being called
359                              'AWD'  : Award,
360                              'TTSK' : Top Task,
361                              'TSK'  : Task,
362                              'RESG' : Resource Group,
363                              'RES'  : Resource and
364                              'ALL'  : From Check funds etc ..
365   ============================================================================ */
366 FUNCTION Full_mode_failure(p_packet_id in NUMBER, p_mode in VARCHAR2,p_level in VARCHAR2)
367 RETURN BOOLEAN IS
368  l_dummy number;
369 Begin
370   g_error_procedure_name := 'FULL_MODE_FAILURE';
371 
372   If g_partial_flag = 'N' then
373 
374   -- If p_mode in ('R','U','C','I') then
375     -- Full mode
376   Begin
377    Select 1
378      into l_dummy
379      from dual
380     where exists
381     (select 1 from gms_bc_packets bp1
382       where bp1.packet_id = p_packet_id
383         and (bp1.status_code in ('R','F') or
384              (p_level = 'RES'  and substr(bp1.res_result_code,1,1)     = 'F') or
385              (p_level = 'RESG' and substr(bp1.res_grp_result_code,1,1) = 'F') or
386              (p_level = 'TSK'  and substr(bp1.task_result_code,1,1)    = 'F') or
387              (p_level = 'TTSK' and substr(bp1.top_task_result_code,1,1)= 'F') or
388              (p_level = 'AWD'  and substr(bp1.award_result_code,1,1)   = 'F') --or
389              --(p_level = 'ALL'  and substr(bp1.result_code,1,1)         = 'F')
390              )
391      );
392 
393      Update gms_bc_packets bp
394        set bp.status_code       = decode(p_mode,'C','F','R'),
395            bp.result_code       = decode(substr(bp.result_code,1,1),'P','F65',null,'F65',bp.result_code),
396 	   bp.fc_error_message  = decode(bp.fc_error_message,NULL,'FULL_MODE_FAILURE (R/U/C/I mode) at stage:'||p_level,bp.fc_error_message)
397      where packet_id            = p_packet_id
398      and   status_code          = 'P';
399 
400      RETURN TRUE;
401   Exception
402      When no_data_found then
403           RETURN FALSE;
404   End;
405 
406  Else  -- (Partial mode)
407 -- Related invoice failure
408  -- If any distribution ITEM/TAX/Variance fails .. all related records should
409  -- fail. So, what about related PO?? That is handled below in the partial mode
410  -- if..end if. code
411 
412  If p_level in ('AWD','ALL') then
413 
414      IF g_debug = 'Y' THEN
415         gms_error_pkg.gms_debug ('Check for related invoice failure', 'C');
416       END IF;
417 
418     l_dummy := 0;
419     Begin
420      select 1
421      into   l_dummy
422      from   dual
423      where  exists (select 1
424                     from   gms_bc_packets
425                     where  packet_id     = p_packet_id
426                     and    document_type = 'AP'
427                     and    substr(result_code,1,1) = 'F');
428     Exception
429       When no_data_found then
430            null;
431     End;
432 
433      IF g_debug = 'Y' THEN
434         gms_error_pkg.gms_debug ('Check for related invoice failure, if exists then l_dummy = 1): l_dummy::'||l_dummy, 'C');
435       END IF;
436 
437 
438     If l_dummy = 1 then
439         update gms_bc_packets
440         set    result_code = 'F65',
441                fc_error_message = decode(fc_error_message,NULL,'FULL_MODE_FAILURE (Related invoice distribution failed) '||p_level,fc_error_message)
442         where  packet_id = p_packet_id
443         and    substr(result_code,1,1) = 'P'
444         and    (document_header_id,document_distribution_id) in
445                ( select distinct b.invoice_id,b.invoice_distribution_id
446                   from ap_invoice_distributions_all  a,
447                        ap_invoice_distributions_all  b
448                   where (a.invoice_id,a.invoice_distribution_id) in
449                         (select document_header_id,
450                                 document_distribution_id
451                            from gms_bc_packets gbc
452                           where gbc.packet_id = p_packet_id
453                             and  substr(gbc.result_code,1,1) = 'F'
454                             and  gbc.document_type = 'AP'
455                             and  gbc.parent_bc_packet_id is null)
456                             and  b.invoice_id = a.invoice_id
457                    and  COALESCE(b.charge_applicable_to_dist_id,b.related_id,b.invoice_distribution_id) =
458                         COALESCE(a.charge_applicable_to_dist_id,a.related_id,a.invoice_distribution_id)
459 		 union /* BUG 14216205 : Added the union for SAT */
460                  select distinct D.INVOICE_ID,D.INVOICE_DISTRIBUTION_ID
461                   from AP_SELF_ASSESSED_TAX_DIST_ALL  C,
462                        AP_SELF_ASSESSED_TAX_DIST_ALL  D
463                   where (c.invoice_id,c.invoice_distribution_id) in
464                         (select document_header_id,
465                                 document_distribution_id
466                            from gms_bc_packets gbc
467                           where gbc.packet_id = p_packet_id
468                             and  substr(gbc.result_code,1,1) = 'F'
469                             and  gbc.document_type = 'AP'
470                             and  GBC.PARENT_BC_PACKET_ID is null)
471                             and  D.INVOICE_ID = C.INVOICE_ID
472                    and  coalesce(D.CHARGE_APPLICABLE_TO_DIST_ID,D.RELATED_ID,D.INVOICE_DISTRIBUTION_ID) =
473                         coalesce(c.CHARGE_APPLICABLE_TO_DIST_ID,c.RELATED_ID,c.INVOICE_DISTRIBUTION_ID)
474                         );
475 
476 
477      IF g_debug = 'Y' THEN
478         gms_error_pkg.gms_debug (SQL%rowcount||' records updated','C');
479       END IF;
480 
481     End If;
482 
483  End If; --related invoice failure ..
484 
485    -- This section will deal with 2 checks:
486    -- 1. Fail all burden if one burden failed (EXP/ENC) and also (AP/PO/REQ in partial mode)
487    --    This also takes care of burden adjsutment records failing ..
488    -- 2. Fail all cdls if one cdl failed for an EXP
489 
490 
491      -- 1. Fail all burden if one burden failed (EXP/ENC) and also (AP/PO/REQ in partial mode)
492      --    This also takes care of burden adjsutment records failing ..
493      -- e.g.:  bc_packet parent_bc_packet exp.type  status
494      --            2         1            Overhead   P
495      --            3         1            Fringe     F
496      -- In this case, we need to fail 2..
497 
498    --If p_mode in ('X','E') then
499 
500     /* Bug 5250793 : Added code such that if an AP Invoice distribution fails fundscheck then the PO matched to that AP also fails
501                      with full mode failure. */
502      Update gms_bc_packets bp
503      set bp.status_code = decode(p_mode,'C','F','R'),
504          bp.result_code = decode(substr(bp.result_code,1,1),'P','F65',null,'F65',bp.result_code),
505          bp.fc_error_message = decode(bp.fc_error_message,NULL,'FULL_MODE_FAILURE (X/E/R/U/C mode, INV matched to PO has failed) at stage:'||p_level,bp.fc_error_message)
506      where bp.document_type  = 'PO'
507      and   bp.packet_id      = p_packet_id
508      and   bp.status_code    = 'P'
509      and   bp.document_distribution_id in ( select distinct apid.po_distribution_id
510 						 from  gms_bc_packets bp1,
511 						       ap_invoice_distributions_all apid
512 						 where bp1.packet_id = p_packet_id
513 						 and  bp1.document_type = 'AP'
514 						 and  bp1.document_distribution_id = apid.invoice_distribution_id
515 						 and  substr(bp1.result_code,1,1) = 'F'
516 						 and  apid.po_distribution_id IS NOT NULL ) ;
517 
518 
519      Update gms_bc_packets bp
520        set bp.status_code          = decode(p_mode,'C','F','R'),
521            bp.result_code          = decode(substr(bp.result_code,1,1),'P','F65',null,'F65',bp.result_code),
522 	   bp.fc_error_message     = decode(bp.fc_error_message,NULL,'FULL_MODE_FAILURE (X/E/R/U/C mode, one of the burden failed) at stage:'||p_level,bp.fc_error_message)
523      where bp.packet_id            = p_packet_id
524        and bp.status_code          = 'P'
525        and bp.document_type        in ('EXP','ENC','AP','PO','REQ')
526        and bp.parent_bc_packet_id is NOT NULL
527        and exists (select /*+ push_subq no_unnest */ 1 /* Hint added for bug 14685623 */
528                    from   gms_bc_packets bp1
529                    where  bp1.packet_id = bp.packet_id				/* Changed the order for Bug 6043224 */
530                    and    bp1.parent_bc_packet_id = bp.parent_bc_packet_id
531                    and    bp1.bc_packet_id <> bp.bc_packet_id			/* Uncommented for Bug 6043224 */
532                    and    bp1.document_type = bp.document_type			/* Uncommented for Bug 6043224 */
533 		   and    bp1.parent_bc_packet_id is NOT NULL
534                    and    bp1.document_header_id = bp.document_header_id 	/* Added for Bug 6043224 */
535                    and    bp1.document_distribution_id = bp.document_distribution_id /* Added for Bug 6043224 */
536                    and ((p_level = 'RES'  and substr(bp1.res_result_code,1,1)     = 'F') or
537                         (p_level = 'RESG' and substr(bp1.res_grp_result_code,1,1) = 'F') or
538                         (p_level = 'TSK'  and substr(bp1.task_result_code,1,1)    = 'F') or
539                         (p_level = 'TTSK' and substr(bp1.top_task_result_code,1,1)= 'F') or
540                         (p_level = 'AWD'  and substr(bp1.award_result_code,1,1)   = 'F') or
541                         (p_level = 'ALL'  and substr(bp1.result_code,1,1)         = 'F')
542                        )
543                  );
544 
545    -- End If;
546 
547    -- 2. Fail all cdls if one cdl failed for an EXP
548 
549    If p_mode = 'X' then
550 
551      Update gms_bc_packets bp
552        set bp.status_code          = decode(p_mode,'C','F','R'),
553            bp.result_code          = decode(substr(bp.result_code,1,1),'P','F65',null,'F65',bp.result_code),
554 	   bp.fc_error_message     = decode(bp.fc_error_message,NULL,'FULL_MODE_FAILURE (X mode - one of the CDL failed) at stage:'||p_level,bp.fc_error_message)
555      where bp.packet_id            = p_packet_id
556        and bp.status_code          = 'P'
557        and bp.document_type        = 'EXP'
558        and exists (select /*+ push_subq no_unnest */ 1 /* Hint added for bug 14685623 */
559                    from   gms_bc_packets bp1
560                    where  bp1.packet_id = bp.packet_id
561                    and    bp1.document_header_id = bp.document_header_id
562                    and    bp1.document_distribution_id <> bp.document_distribution_id
563                    and    bp1.document_type = bp.document_type
564                    and ((p_level = 'RES'  and substr(bp1.res_result_code,1,1)     = 'F') or
565                         (p_level = 'RESG' and substr(bp1.res_grp_result_code,1,1) = 'F') or
566                         (p_level = 'TSK'  and substr(bp1.task_result_code,1,1)    = 'F') or
567                         (p_level = 'TTSK' and substr(bp1.top_task_result_code,1,1)= 'F') or
568                         (p_level = 'AWD'  and substr(bp1.award_result_code,1,1)   = 'F') or
569                         (p_level = 'ALL'  and substr(bp1.result_code,1,1)         = 'F')
570                        )
571                  );
572    End if; --If p_mode = 'X'
573 
574      RETURN TRUE;
575 
576  End If; -- partial flag check
577 
578      RETURN TRUE; -- This has been put if the mode comes as something unexpected ..
579 
580 End Full_mode_failure;
581 
582 /* -------------------------------------------------------------------------------
583    This procedure will be used to validate adjusting/adjusted transactions. Procedure has
584     a parameter p_mode. This parameter can have 2 values: 'Check_Adjusted' and 'Net_Zero'
585 
586    Check_Adjusted Mode : In this mode, we will check for adjusting transactions in the
587                          packet whose adjusted transaction has not been funds checked.
588                          We will fail such transactions with   'F08'.
589 
590    Net_Zero_mode       : In this mode, we will check whether adjsuted and adjusting
591                          transactions are present in the same packet. If so, update the
592                          transactions result_code with 'P82' and effect_on_funds_code to
593                          'I'. This will ensure that 'Funds Available' calculations are
594                          not carried out for these transactions.
595 -------------------------------------------------------------------------------------------*/
596 
597 Procedure Handle_net_zero_txn(p_packetid IN number, p_mode IN varchar2 ) is
598 
599  -- R12 Funds management uptake
600  PRAGMA AUTONOMOUS_TRANSACTION;
601 
602 Cursor c_txn is
603     select adjusted_document_header_id,
604            nvl(ind_compiled_set_id,-1) ind_compiled_set_id
605     from   gms_bc_packets
606     where  packet_id = p_packetid
607      and   document_type = 'ENC'
608     having sum(entered_dr-entered_cr) = 0
609     group by  adjusted_document_header_id,
610                     nvl(ind_compiled_set_id,-1);
611 Begin
612       g_error_procedure_name := 'Handle_net_zero_txn' ;
613       g_error_stage := 'Net Zero - ENC';
614 
615       	gms_error_pkg.gms_debug ( 'Handle_net_zero_txn : Start','C');
616       	gms_error_pkg.gms_debug ( 'p_mode : '||p_mode,'C');
617 
618 If p_mode = 'Check_Adjusted' then
619    -- Fail adjusting txn. If adjusted has not been funds checked -F08
620    update gms_bc_packets gbc
621      set    gbc.result_code = 'F08',
622 	    gbc.award_result_code = 'F08',
623 	    gbc.top_task_result_code = 'F08',
624 	    gbc.task_result_code = 'F08',
625 	    gbc.res_grp_result_code = 'F08',
626 	    gbc.res_result_code = 'F08',
627             gbc.status_code = 'R'
628    where  gbc.packet_id = p_packetid
629      and  gbc.document_type = 'ENC'
630      and  nvl(gbc.result_code,'XX') <> 'P82'
631      and  gbc.adjusted_document_header_id is NOT NULL
632      and  gbc. adjusted_document_header_id <> gbc.document_header_id
633      and  exists
634             (select 1
635              from   gms_encumbrance_items gei
636              where  gei.encumbrance_item_id =   gbc.adjusted_document_header_id
637              and    nvl(gei.enc_distributed_flag,'N') = 'N'
638              and    nvl(request_id,-1)  <>  gbc.request_id
639              ) ;
640 Elsif p_mode = 'Net_Zero' then
641  -- Adjusted and adjusting in same packet
642 for recs in c_txn
643  loop
644 
645      update gms_bc_packets gbc
646      set    gbc.result_code = 'P82',
647  	    gbc.award_result_code = 'P82',
648 	    gbc.top_task_result_code = 'P82',
649 	    gbc.task_result_code = 'P82',
650 	    gbc.res_grp_result_code = 'P82',
651 	    gbc.res_result_code = 'P82',
652             gbc.effect_on_funds_code = 'I'
653      where  gbc.packet_id = p_packetid
654      and    gbc.adjusted_document_header_id = recs.adjusted_document_header_id
655      and    nvl(ind_compiled_set_id,-1) =  recs.ind_compiled_set_id;
656 
657     g_bc_packet_has_P82_records := 'Y' ;
658 end loop;
659       	gms_error_pkg.gms_debug ( 'Handle_net_zero_txn : End','C');
660 
661 End If;
662 
663 COMMIT;
664 
665 End Handle_net_zero_txn;
666 
667 -- This procedure will calculate the ind_compiled_set_id for encumbrances
668 Procedure Calc_enc_ind_compiled_set_id(p_packet_id IN number) IS
669 
670  -- R12 Funds management uptake
671  PRAGMA AUTONOMOUS_TRANSACTION;
672 
673  TYPE t_project_id_type IS TABLE OF gms_bc_packets.project_id%type;
674  TYPE t_award_id_type   IS TABLE OF gms_bc_packets.award_id%type;
675  TYPE t_task_id_type    IS TABLE OF gms_bc_packets.task_id%type;
676  TYPE t_exp_date_type   IS TABLE OF gms_bc_packets.expenditure_item_date%type;
677  TYPE t_exp_type_type   IS TABLE OF gms_bc_packets.expenditure_type%type;
678  TYPE t_exp_org_type    IS TABLE OF gms_bc_packets.expenditure_organization_id%type;
679  TYPE t_ind_set_type    IS TABLE OF gms_bc_packets.ind_compiled_set_id%type;
680 
681  t_project_id t_project_id_type;
682  t_award_id   t_award_id_type;
683  t_task_id    t_task_id_type;
684  t_exp_date   t_exp_date_type;
685  t_exp_type   t_exp_type_type;
686  t_exp_org    t_exp_org_type;
687  t_ind_set    t_ind_set_type;
688 
689 Begin
690       	gms_error_pkg.gms_debug ( 'Calc_enc_ind_compiled_set_id : Start','C');
691 
692   -- This part of the code will take care of non-net zero encumbrance items or
693   -- the original (adjusted) net zero item
694 
695       g_error_procedure_name := 'calc_enc_ind_compiled_set_id' ;
696       g_error_stage := 'UPD IND SET: BLK COLL';
697 
698  select distinct  gbc.project_id,
699                   gbc.award_id,
700                   gbc.task_id,
701                   gbc.expenditure_item_date,
702                   gbc.expenditure_type,
703                   gbc.expenditure_organization_id,
704                   null
705  BULK COLLECT into t_project_id,
706                    t_award_id,
707                    t_task_id,
708                    t_exp_date,
709                    t_exp_type,
710                    t_exp_org,
711                    t_ind_set
712   from  gms_bc_packets gbc
713   where gbc.packet_id   = p_packet_id
714   and   gbc.status_code = 'P'
715   and   gbc.ind_compiled_set_id is null
716   and   nvl(gbc.burden_adjustment_flag,'N') = 'N' -- 3389292
717   and   (gbc.adjusted_document_header_id is NULL OR
718          gbc.adjusted_document_header_id = gbc.document_header_id);
719 
720       If t_project_id.COUNT > 0 then
721 
722       g_error_stage := 'UPD IND SET: COMPUTE';
723 
724     	FOR i IN t_project_id.FIRST .. t_project_id.LAST
725 	LOOP
726              t_ind_set(i) := gms_cost_plus_extn.get_award_cmt_compiled_set_id (
727                                          t_task_id(i),
728                                          t_exp_date(i),
729                                          t_exp_type(i),
730                                          t_exp_org(i),
731                                          'C',
732                                          t_award_id(i));
733 			END LOOP;
734 
735       g_error_stage := 'UPD FC SEQ: FOR ALL';
736       /* Bug#7034365 :Modified this update to pick up ind_compiled_set_id from gms_award_distributions
737  	 so as to ensure that we relieve burden component same as it was reserved for a PO/AP/REQ transaction
738       */
739 
740          FORALL j IN t_project_id.FIRST .. t_project_id.LAST
741            Update /*+ index(gbc GMS_BC_PACKETS_N1) */ gms_bc_packets gbc  /*Added hint for bug 5683910 */
742            set   ind_compiled_Set_id         = (nvl((select ind_compiled_set_id  from gms_award_distributions
743                                       where document_type = gbc.document_type
744                                       and ((document_type = 'AP' and
745                                       invoice_id = gbc.document_header_id and
746                                       --distribution_line_number = gbc.document_distribution_id) OR  -- Bug 10082739: Joins were not proper
747                                       invoice_distribution_id = gbc.document_distribution_id) OR
748                                       (document_type = 'PO' and
749                                       po_distribution_id = gbc.document_distribution_id) OR
750      	                              (document_type = 'REQ' and
751  	                              distribution_id = gbc.document_distribution_id))
752  	                              and burdenable_raw_cost<>0
753  	                              ),t_ind_set(j))
754  	                              )
755            where packet_id                   = p_packet_id
756            and   project_id                  = t_project_id(j)
757            and   award_id                    = t_award_id(j)
758            and   task_id                     = t_task_id(j)
759            and   expenditure_item_date       = t_exp_date(j)
760            and   expenditure_type            = t_exp_type(j)
761            and   expenditure_organization_id = t_exp_org(j)
762            and   status_code = 'P' --Bug 5726575
763            and   ind_compiled_set_id is null --Bug 5726575
764            and   nvl(burden_adjustment_flag,'N') = 'N' --Bug 5726575
765            and   (adjusted_document_header_id is NULL OR  --Bug 5122879
766                   adjusted_document_header_id = document_header_id);
767 
768       End If; --If t_project_id.COUNT > 0 then
769 
770       	gms_error_pkg.gms_debug ( 'After Bulk processing','C');
771   -- =============================================================
772   -- This part of the code will take care of the reversing (adjusting) net zero item
773   -- =============================================================
774 
775   -- Update 1 : For Adjusting (Reversing) transactions whose  original transaction
776   --            is in the same packet
777 
778   Update gms_bc_packets gbc
779   set    gbc.ind_compiled_Set_id = (Select gbc1.ind_compiled_set_id
780                                     from   gms_bc_packets gbc1
781                                     where  gbc1.packet_id          =  p_packet_id
782                                     and    gbc1.document_header_id =  gbc.adjusted_document_header_id
783                                     /* bug 6414366 start */
784                                     and gbc1.document_distribution_id =
785                                             (select max(gbc2.document_distribution_id)
786                                              from gms_bc_packets gbc2
787                                              where gbc2.packet_id = p_packet_id
788                                              and   gbc2.document_header_id = gbc.adjusted_document_header_id))
789                                     /* bug 6414366 end */
790    where  gbc.packet_id   = p_packet_id
791    and    gbc.status_code = 'P'
792    --and    gbc.result_code = 'P82'
793    and    gbc.ind_compiled_set_id is null
794    and    gbc.adjusted_document_header_id is not NULL
795   and   nvl(gbc.burden_adjustment_flag,'N') = 'N' -- 3389292
796    and    gbc.adjusted_document_header_id <> gbc.document_header_id;
797 
798       	gms_error_pkg.gms_debug ( 'After Update 1','C');
799 -- Bug#6075039 Modified the sub query for performance issue.
800   -- Update 2 : For Adjusting (Reversing) transactions whose  original transaction
801   --            was funds checked earlier
802   Update gms_bc_packets gbc
803   set    gbc.ind_compiled_Set_id = (Select nvl(gei.ind_compiled_set_id, adl.ind_compiled_set_id) --Bug 5122879
804                                     from   gms_encumbrance_items gei,
805                                            gms_award_distributions adl
806                                     where adl.expenditure_item_id =gbc.adjusted_document_header_id
807                                       and adl.adl_status = 'A'
808 				      and adl.fc_status = 'A'
809 				      and nvl(adl.reversed_flag, 'N') = 'N'
810 				      and adl.line_num_reversed is null
811                                       and adl.document_type = 'ENC'
812                                       and gei.encumbrance_item_id = adl.expenditure_item_id)
813    where  gbc.packet_id   = p_packet_id
814    and    gbc.status_code = 'P'
815    and    gbc.result_code is NULL
816    and    gbc.ind_compiled_set_id is null
817    and    gbc.adjusted_document_header_id is not NULL
818    and   nvl(gbc.burden_adjustment_flag,'N') = 'N' -- 3389292
819    and    gbc.adjusted_document_header_id <> gbc.document_header_id;
820 
821    gms_error_pkg.gms_debug ( 'Calc_enc_ind_compiled_set_id : End','C');
822 
823    COMMIT;
824 
825 End Calc_enc_ind_compiled_set_id;
826 
827 /*--------------------------------------------------------------------------------------------------------
828 -- This procedure updates table values
829 -- TABLE          Columns
830 -- GMS_BC_PACKETS       STATUS_CODE
831 -- GMS_AWARD_DISTRIBUTIONS    FC_STATUS
832 --             RESOURCE_LIST_MEMBER_ID
833 --             BUD_TASK_ID(Budgeted Task)
834 --             BUD_RES_LIST_MEMBER_ID(Budgeted rlmi)
835 --             RAW_COST
836 --             IND_COMPILED_SET_ID
837 -- GMS_ENCUMBRANCE_ITEMS_ALL     ENC_DISTRIBUTED_FLAG
838 --
839 -- DIFFERENT MODE USED FOR UPDATE
840 -- S - Submit
841 -- B - Baseline
842 -- R - Reserve
843 -- U - Unreserve
844 -- C - Check Funds
845 -- E - Encumrance
846 --
847 -- DIFFERENT DOCUMENT TYPES FOR UPDATE
848 -- REQ - Requisitions
849 -- PO  - Purchase Orders
850 -- AP  - Payables
851 -- ENC - Encumbrances
852 -- -----------------------------------------------------------------*/
853 
854    PROCEDURE status_code_update (p_packet_id NUMBER, p_mode VARCHAR2, p_partial VARCHAR2 DEFAULT 'N') IS
855       x_err_code   NUMBER;
856       x_err_buff   VARCHAR2 (2000);
857 
858       x_dummy      NUMBER;          -- Bug 2181546, Added
859          CURSOR c_failed_packet IS  -- Bug 2181546, Added
860          SELECT 1
861            FROM gms_bc_packets
862           WHERE packet_id = p_packet_id
863             AND SUBSTR (nvl(result_code,'F65'), 1, 1) = 'F' ;
864 
865       /* Introduced for Bug# 4159238 (BaseBug#4292763)*/
866 
867       TYPE tab_doc_head_id     IS TABLE OF gms_bc_packets.document_header_id%TYPE;
868       TYPE tab_doc_type        IS TABLE OF gms_bc_packets.document_type%TYPE;
869       TYPE tab_res_code        IS TABLE OF gms_bc_packets.result_code%TYPE;
870       TYPE tab_sta_code        IS TABLE OF gms_bc_packets.status_code%TYPE;
871       TYPE tab_dr_code         IS TABLE OF gms_bc_packets.entered_dr%TYPE;
872       TYPE tab_cr_code         IS TABLE OF gms_bc_packets.entered_cr%TYPE;
873       TYPE tab_bud_task_id     IS TABLE OF gms_bc_packets.bud_task_id%TYPE;
874       TYPE tab_proj_id         IS TABLE OF gms_bc_packets.project_id%TYPE;
875       TYPE tab_res_list_mem_id IS TABLE OF gms_bc_packets.resource_list_member_id%TYPE;
876       TYPE tab_doc_dist_id     IS TABLE OF gms_bc_packets.document_distribution_id%TYPE;
877       TYPE tab_task_id         IS TABLE OF gms_bc_packets.task_id%TYPE;
878       TYPE tab_exp_item_date   IS TABLE OF gms_bc_packets.expenditure_item_date%TYPE;
879       TYPE tab_award_id        IS TABLE OF gms_bc_packets.award_id%TYPE;
880       TYPE tab_exp_orgnzt_id   IS TABLE OF gms_bc_packets.expenditure_organization_id%TYPE;
881       TYPE tab_packet_id       IS TABLE OF gms_bc_packets.packet_id%TYPE;
882       TYPE tab_bc_packet_id    IS TABLE OF gms_bc_packets.bc_packet_id%TYPE;
883       TYPE tab_exp_type        IS TABLE OF gms_bc_packets.expenditure_type%TYPE;
884       TYPE tab_ind_comp_setid  IS TABLE OF gms_bc_packets.ind_compiled_set_id%TYPE;
885       TYPE tab_set_of_books_id IS TABLE OF gms_bc_packets.set_of_books_id%TYPE; --Bug 5845974
886 
887       tdocument_header_id             tab_doc_head_id;
888       tdocument_type                  tab_doc_type;
889       tresult_code                    tab_res_code;
890       tstatus_code                    tab_sta_code;
891       tentered_dr                     tab_dr_code;
892       tentered_cr                     tab_cr_code;
893       tbud_task_id                    tab_bud_task_id;
894       tproject_id                     tab_proj_id;
895       tresource_list_member_id        tab_res_list_mem_id;
896       tdocument_distribution_id       tab_doc_dist_id;
897       ttask_id                        tab_task_id;
898       texpenditure_item_date          tab_exp_item_date;
899       taward_id                       tab_award_id;
900       texpenditure_organization_id    tab_exp_orgnzt_id;
901       tpacket_id                      tab_packet_id;
902       tbc_packet_id                   tab_bc_packet_id;
903       texpenditure_type               tab_exp_type;
904       tind_compiled_set_id            tab_ind_comp_setid;
905       tset_of_books_id                tab_set_of_books_id; --Bug 5845974
906 
907       l_batch_size                    number := 10000;
908 
909       /* End of variables introduced for Bug# 4159238 (BaseBug#4292763)*/
910 
911 
912       CURSOR update_status IS
913 	 SELECT document_header_id,                 document_type,                result_code,
914                 status_code,                        entered_dr,                   entered_cr,
915                 bud_task_id,                        project_id,                   resource_list_member_id,
916                 document_distribution_id,           task_id,                      expenditure_item_date,
917 		expenditure_type , -- Bug 3003584
918                 award_id,                           expenditure_organization_id,  packet_id,
919                 bc_packet_id,                       ind_compiled_set_id  -- Added for bug : 2927485
920            FROM gms_bc_packets
921           WHERE packet_id = p_packet_id
922             AND parent_bc_packet_id IS NULL
923             AND nvl(burden_adjustment_flag,'N') = 'N'
924             AND status_code in ('A','B') --Added to fix bug 2138376 from 'B'*/
925 	    AND document_type IN ('REQ','PO','AP');
926 
927       CURSOR update_status1 IS
928 	 SELECT document_header_id,                 document_type,                result_code,
929                 status_code,                        entered_dr,                   entered_cr,
930                 bud_task_id,                        project_id,                   resource_list_member_id,
931                 document_distribution_id,           task_id,                      expenditure_item_date,
932 		expenditure_type , -- Bug 3003584
933                 award_id,                           expenditure_organization_id,  packet_id,
934                 bc_packet_id,                       ind_compiled_set_id,  -- Added for bug : 2927485
935                 set_of_books_id --Bug 5845974
936            FROM gms_bc_packets
937           WHERE packet_id = p_packet_id
938             AND parent_bc_packet_id IS NULL
939             AND nvl(burden_adjustment_flag,'N') = 'N'
940             AND status_code in ('A','B') --Added to fix bug 2138376 from 'B'*/
941 	    AND document_type in ('EXP','ENC');
942 	    --AND document_type = 'ENC';
943 
944       CURSOR update_status_enc IS --Bug 5726575
945          SELECT gbp.document_header_id,
946                 gbp.document_type,
947                 gbp.result_code,
948                 gbp.document_distribution_id,
949                 adl.ind_compiled_set_id,
950                 gbp.packet_id
951            FROM gms_bc_packets gbp,
952                 gms_award_distributions adl
953           WHERE gbp.document_header_id = adl.expenditure_item_id
954             and gbp.document_distribution_id = adl.adl_line_num
955             and gbp.packet_id = p_packet_id
956             AND gbp.parent_bc_packet_id IS NULL
957             AND nvl(gbp.burden_adjustment_flag,'N') = 'N'
958             AND gbp.status_code in ('A','B')
959             AND gbp.document_type in ('ENC')
960             and adl.document_type = 'ENC'
961             and adl.adl_status = 'A'
962             and nvl(adl.reversed_flag, 'N') <> 'Y'
963             and adl.line_num_reversed is null;
964 
965    BEGIN
966       g_error_procedure_name := 'status_code_update';
967       g_error_stage := 'SCU : START';
968       IF g_debug = 'Y' THEN
969       	gms_error_pkg.gms_debug ( 'STATUS_CODE_UPDATE - START ','C');
970       END IF;
971 
972      If g_partial_flag = 'N' then
973 
974         -- -------------------------------- FULL MODE  START ---------------------------------------------+
975 	--  IF (    ( NVL(p_mode,'R') in ('R') and NVL(p_partial,'N') = 'N')
976 	--  	   OR ( NVL(p_mode,'R') in ('U','S','B','C'))) 			  	    THEN
977 
978         g_error_stage := 'SCU : PARTIAL NO RES';
979 
980         --Bug 2181546, Added the cursor and failing packet if atleast one failed record exists in packet
981            OPEN c_failed_packet;
982            FETCH c_failed_packet INTO x_dummy;
983 
984            IF c_failed_packet%FOUND THEN
985 
986              -- ---------------------------------------------+
987              -- FULL MODE: FAILURE
988              -- ---------------------------------------------+
989              UPDATE gms_bc_packets
990                 SET status_code = decode(p_mode,'S','E','C','F','R'),
991 	            result_code =
992                   DECODE (SUBSTR (NVL (result_code, 'F65'), 1, 1), 'P','F65', NVL(result_code,'F65')),  --Bug 2092791 Added NVL Clause
993 	            fc_error_message     = decode(fc_error_message,NULL,g_error_procedure_name,fc_error_message)
994               WHERE packet_id = p_packet_id;
995 
996            ELSE
997 
998              -- ---------------------------------------------+
999              -- FULL MODE: PASS
1000              -- ---------------------------------------------+
1001             UPDATE gms_bc_packets
1002                SET status_code = decode(p_mode,'S','S','B','B','C','C','A')
1003              WHERE packet_id = p_packet_id;
1004 			IF g_debug = 'Y' THEN
1005 				gms_error_pkg.gms_debug ('STATUS_CODE_UPDATE - SUBMIT UPDATE FOR PASS TRANSACTIONS', 'C');
1006 			END IF;
1007            END IF;
1008            CLOSE c_failed_packet;
1009         -- -------------------------------- PARTIAL MODE  START ---------------------------------------------+
1010 	ELSIF g_partial_flag ='Y' then
1011 
1012 	-- ELSIF (     ( NVL(p_mode,'R') in ('R') and NVL(p_partial,'N') = 'Y' )
1013 	--  	      OR ( NVL(p_mode,'R') in ('E'))) THEN
1014 
1015             UPDATE gms_bc_packets
1016                SET status_code = DECODE (SUBSTR (nvl(result_code,'F65'), 1, 1), 'P', 'A', 'R'),
1017 		   fc_error_message     = decode(fc_error_message,NULL,g_error_procedure_name,fc_error_message)
1018              WHERE packet_id = p_packet_id;
1019 
1020             IF SQL%NOTFOUND THEN
1021                IF g_debug = 'Y' THEN
1022                	gms_error_pkg.gms_debug ('STATUS_CODE_UPDATE - NO RECORDS UPDATED IN PARTIAL MODE', 'C');
1023                END IF;
1024             END IF;
1025             g_error_stage := 'SCU : PARTIAL YES RES';
1026 
1027 	 ELSE
1028             IF g_debug = 'Y' THEN
1029             	gms_error_pkg.gms_debug ('STATUS_CODE_UPDATE - NO RECORDS UPDATED ', 'C');
1030             END IF;
1031 	 END IF;
1032 
1033       -- --------------------------------------------------------------------------------------------------+
1034       -- If g_doc_type <> 'EXP' then
1035 
1036        /* Changes for Bug#4159238  (BaseBug#4292763): Implemented bulk collect logic*/
1037 
1038        IF p_mode IN ('R','U','B','E') THEN
1039 
1040 
1041         OPEN update_status;
1042 
1043 	LOOP
1044 
1045 	 FETCH update_status
1046          BULK COLLECT INTO
1047                 tdocument_header_id,                tdocument_type,               tresult_code,
1048                 tstatus_code,                       tentered_dr,                  tentered_cr,
1049                 tbud_task_id,                       tproject_id,                  tresource_list_member_id,
1050                 tdocument_distribution_id,          ttask_id,                     texpenditure_item_date,
1051 		texpenditure_type,
1052                 taward_id,                          texpenditure_organization_id, tpacket_id,
1053                 tbc_packet_id,                      tind_compiled_set_id
1054 	 LIMIT  l_batch_size;
1055 
1056 	 IF tpacket_id.COUNT > 0 THEN
1057 
1058   	      FORALL I in tpacket_id.FIRST..tpacket_id.LAST
1059                 UPDATE gms_award_distributions
1060                 SET     resource_list_member_id = tresource_list_member_id(i),
1061                       bud_task_id             = tbud_task_id(i),
1062                       fc_status               = DECODE(p_mode,'B',fc_status,
1063                                                 DECODE (SUBSTR (tresult_code(i), 1, 1), 'P', 'A', 'R'))
1064                 WHERE  DECODE(tdocument_type(i), 'AP', invoice_id, tdocument_header_id(i) ) = tdocument_header_id(i)
1065                 AND    DECODE(tdocument_type(i), 'REQ', distribution_id,
1066                                                  'PO',  po_distribution_id,
1067                                                  'AP', invoice_distribution_id) = tdocument_distribution_id(i)
1068                 /* Bug 5344693 : tdocument_distribution_id(i) stores the invoice_distribution_id for an AP invoice.
1069 		   So for an AP invoice , tdocument_distribution_id(i) should be compared with invoice_distribution_id. */
1070                 AND    adl_status    = 'A'
1071                 AND    document_type = tdocument_type(i)
1072                 AND    project_id    = tproject_id(i)
1073                 AND    task_id       = ttask_id(i)
1074                 AND    award_id      = taward_id(i);
1075 
1076 	  /* Used .delete instead of assigning null table to these tables.*/
1077 	  tdocument_header_id.delete;
1078           tdocument_type.delete;
1079           tresult_code.delete;
1080           tstatus_code.delete;
1081           tentered_dr.delete;
1082           tentered_cr.delete;
1083           tbud_task_id.delete;
1084           tproject_id.delete;
1085           tresource_list_member_id.delete;
1086           tdocument_distribution_id.delete;
1087           ttask_id.delete;
1088           texpenditure_item_date.delete;
1089           taward_id.delete;
1090           texpenditure_organization_id.delete;
1091           tpacket_id.delete;
1092           tbc_packet_id.delete;
1093           texpenditure_type.delete;
1094 	  tind_compiled_set_id.delete;
1095 
1096 	 END IF;
1097 
1098 	 EXIT WHEN update_status%NOTFOUND;
1099 
1100 	END LOOP;
1101         CLOSE update_status;
1102 
1103       open update_status1;
1104 
1105       LOOP
1106 
1107 	FETCH update_status1
1108 	BULK COLLECT INTO
1109                 tdocument_header_id,                tdocument_type,               tresult_code,
1110                 tstatus_code,                       tentered_dr,                  tentered_cr,
1111                 tbud_task_id,                       tproject_id,                  tresource_list_member_id,
1112                 tdocument_distribution_id,          ttask_id,                     texpenditure_item_date,
1113 		texpenditure_type,
1114                 taward_id,                          texpenditure_organization_id, tpacket_id,
1115                 tbc_packet_id,                      tind_compiled_set_id,         tset_of_books_id --Bug 5845974
1116 	 LIMIT  l_batch_size;
1117 
1118        IF tpacket_id.COUNT > 0 THEN
1119 
1120 	 FORALL I in tpacket_id.FIRST..tpacket_id.LAST
1121          UPDATE gms_award_distributions
1122          SET    cost_distributed_flag =
1123                 DECODE(P_MODE,'B',cost_distributed_flag,DECODE (SUBSTR (tresult_code(i), 1, 1), 'P', 'Y', 'N')),
1124                 fc_status  = DECODE(P_MODE,'B',FC_STATUS,DECODE (SUBSTR (tresult_code(i), 1, 1), 'P', 'A', 'R')),
1125                 raw_cost   = DECODE(P_MODE,'B',RAW_COST,NVL (tentered_dr(i), 0) - NVL (tentered_cr(i), 0)),
1126                 bud_task_id = tbud_task_id(i),
1127                 resource_list_member_id = tresource_list_member_id(i),
1128                 ind_compiled_set_id = DECODE(P_MODE,'B',ind_compiled_set_id, tind_compiled_set_id(i))
1129          WHERE  expenditure_item_id = tdocument_header_id(i)
1130          AND    adl_line_num = decode(tdocument_type(i), 'ENC', tdocument_distribution_id(i), adl_line_num)--Bug 5726575
1131          AND    cdl_line_num = decode(tdocument_type(i), 'ENC', 1, tdocument_distribution_id(i)) /* Bug 6066845 */
1132          AND    document_type = tdocument_type(i)
1133          AND    adl_status = 'A';
1134 
1135          /* Commented for bug 5726575
1136          IF p_mode <> 'B' THEN
1137               g_error_stage := 'UPDATE_ENC_ITEM';
1138 	       FORALL I in tpacket_id.FIRST..tpacket_id.LAST
1139                UPDATE gms_encumbrance_items_all
1140                SET    enc_distributed_flag = DECODE (SUBSTR (tresult_code(i), 1, 1), 'P', 'Y', 'N'),
1141 	              ind_compiled_set_id  = tind_compiled_set_id(i)
1142                WHERE  encumbrance_item_id  = tdocument_header_id(i)
1143                AND    tdocument_type(i)    = 'ENC';
1144          END IF;*/
1145 
1146 	 --Bug 5845974
1147 	 IF p_mode <> 'B' THEN
1148              g_error_stage := 'UPDATE_ADL_WITH_GL_DATE';
1149              FORALL I in tpacket_id.FIRST..tpacket_id.LAST
1150                update gms_award_distributions
1151                set gl_date = pa_utils2.get_prvdr_gl_date(texpenditure_item_date(i), 101, tset_of_books_id(i))
1152                where document_type = 'ENC'
1153                  and adl_status = 'A'
1154                  and expenditure_item_id = tdocument_header_id(i)
1155                  and adl_line_num = tdocument_distribution_id(i);
1156 	 END IF;
1157 
1158           /* Used .delete instead of assigning null table to these tables. Bug# 4337250*/
1159 
1160 	  tdocument_header_id.delete;
1161           tdocument_type.delete;
1162           tresult_code.delete;
1163           tstatus_code.delete;
1164           tentered_dr.delete;
1165           tentered_cr.delete;
1166           tbud_task_id.delete;
1167           tproject_id.delete;
1168           tresource_list_member_id.delete;
1169           tdocument_distribution_id.delete;
1170           ttask_id.delete;
1171           texpenditure_item_date.delete;
1172           taward_id.delete;
1173           texpenditure_organization_id.delete;
1174           tpacket_id.delete;
1175           tbc_packet_id.delete;
1176           texpenditure_type.delete;
1177     	  tind_compiled_set_id.delete;
1178 
1179 	END IF;
1180 
1181         EXIT WHEN update_status1%NOTFOUND;
1182 
1183         END LOOP;
1184         close update_status1;
1185 
1186         --Bug 5726575 Start
1187         open update_status_enc;
1188         LOOP
1189           FETCH update_status_enc
1190           BULK COLLECT INTO tdocument_header_id,
1191                             tdocument_type,
1192                             tresult_code,
1193                             tdocument_distribution_id,
1194                             tind_compiled_set_id,
1195                             tpacket_id
1196           LIMIT  l_batch_size;
1197          IF tpacket_id.COUNT > 0 THEN
1198            IF p_mode <> 'B' THEN
1199              FORALL I in tpacket_id.FIRST..tpacket_id.LAST
1200                UPDATE gms_encumbrance_items_all
1201                SET    enc_distributed_flag = DECODE (SUBSTR (tresult_code(i), 1, 1), 'P', 'Y', 'N'),
1202                       ind_compiled_set_id  = tind_compiled_set_id(i)
1203                WHERE  encumbrance_item_id  = tdocument_header_id(i);
1204            END IF;
1205            tdocument_header_id.delete;
1206            tdocument_type.delete;
1207            tresult_code.delete;
1208            tdocument_distribution_id.delete;
1209            tpacket_id.delete;
1210            tind_compiled_set_id.delete;
1211          END IF;
1212          EXIT WHEN update_status_enc%NOTFOUND;  /*bug 5840237 */
1213         END LOOP;
1214         close update_status_enc;
1215         --Bug 5726575 End
1216 
1217      --  END IF; --  If g_doc_type <> 'EXP' then
1218 
1219     /* End changes for Bug#4159238 (BaseBug#4292763): Implemented bulk collect logic*/
1220     END IF;
1221 
1222    EXCEPTION
1223      WHEN OTHERS THEN
1224 	 IF update_status%ISOPEN THEN
1225 	   CLOSE update_status;
1226 	 END IF;
1227 
1228 	 IF update_status1%ISOPEN THEN
1229 	   CLOSE update_status1;
1230 	 END IF;
1231 
1232          -- Bug 5726575 Start
1233          IF c_failed_packet%ISOPEN THEN
1234            CLOSE c_failed_packet;
1235  	 END IF;
1236 
1237          IF update_status_enc%ISOPEN THEN
1238            CLOSE update_status_enc;
1239          END IF;
1240          -- Bug 5726575 End
1241 
1242 	 RAISE;    -- Bug 2181546, Added
1243    END status_code_update;
1244 
1245 ----------------------------------------------------------------------------------------------------------
1246 -- Procedure to update gms_bc_packets when there is a failure ..
1247 -- This Procedure updates
1248 --                    status_code ,
1249 --					  result_code at Award Level,Task Level,Resource Group Level
1250 --					  fc_error_message
1251 -- 	All the above parameters are optional
1252 --
1253 ----------------------------------------------------------------------------------------------------------
1254 
1255    PROCEDURE result_status_code_update (
1256       p_packet_id                  IN   NUMBER,
1257       p_status_code                IN   VARCHAR2,
1258       p_result_code                IN   VARCHAR2,
1259       p_bc_packet_id               IN   NUMBER DEFAULT NULL,
1260       p_fc_error_message	   IN	VARCHAR2 DEFAULT NULL ) IS
1261       x_err_code   NUMBER;
1262       x_err_buff   VARCHAR2 (2000);
1263    BEGIN
1264       IF g_debug = 'Y' THEN
1265       	gms_error_pkg.gms_debug ('BEGIN result_status_code_update', 'C');
1266       END IF;
1267 	  g_error_procedure_name  :=  'result_status_code_update' ;
1268 
1269     IF p_bc_packet_id is NULL THEN
1270        g_error_stage := 'RESULT_CODE:PACK_ID';
1271 
1272        UPDATE gms_bc_packets
1273        SET    status_code          = decode(status_code,'P',p_status_code,'I',p_status_code,status_code),
1274               result_code          = decode(substr(result_code,1,1),'F',result_code,p_result_code),
1275               fc_error_message     = decode(fc_error_message,null,p_fc_error_message,fc_error_message)
1276        WHERE  packet_id            = p_packet_id;
1277 	ELSE
1278        g_error_stage := 'RESULT_CODE:BC_PACK_ID';
1279 
1280        UPDATE gms_bc_packets
1281        SET    status_code          = decode(status_code,'P',p_status_code,'I',p_status_code,status_code),
1282               result_code          = decode(substr(result_code,1,1),'F',result_code,p_result_code),
1283               fc_error_message     = decode(fc_error_message,null,p_fc_error_message,fc_error_message)
1284        WHERE packet_id             = p_packet_id
1285          AND bc_packet_id          = p_bc_packet_id;
1286 
1287 	END IF;
1288 
1289       IF g_debug = 'Y' THEN
1290       	gms_error_pkg.gms_debug ('END result_status_code_update', 'C');
1291       END IF;
1292       x_err_code := 0;
1293    END result_status_code_update;
1294 
1295 ----------------------------------------------------------------------------------------------------------
1296 --  Function to create ADL's for REQ,PO,AP.
1297 --  This Function is used to SYNC up ADL's in case of REQ/PO/AP
1298 --  This function will create ADL's in following scenarios
1299 --                  Auto Create a PO
1300 --		    Copy a PO
1301 --                  Duplicate Distribution lines in REQ/PO
1302 --                  Match a PO with Invoice
1303 ----------------------------------------------------------------------------------------------------------
1304 
1305 -- R12 Funds management uptake : With R12 this API will get fired before inserting records into gl_bc_packets,
1306 -- hence modified the whole logic of this procedure .
1307 -- Logic is now based on global PLSQL variables populated by copy_gl_pkt_to_gms_pkt procedure
1308 
1309 ----------------------------------------------------------------------------------------------------------
1310 
1311    FUNCTION misc_synch_adls (p_application_id   IN  NUMBER) RETURN BOOLEAN IS
1312 
1313       x_stage              NUMBER;
1314       x_award_set_id       NUMBER;
1315       x_flip_adl_status    varchar2(1); -- Bug 2155774
1316       l_adl_status         gms_award_distributions.adl_status%TYPE;
1317       l_adl_document_type  gms_award_distributions.document_type%TYPE;
1318       l_adl_invoice_id     NUMBER;
1319       l_adl_dist_id        gms_award_distributions.invoice_distribution_id%TYPE;
1320       l_award_set_id       gms_award_distributions.award_set_id%TYPE;
1321 
1322 
1323       CURSOR c_ap (p_inv_dist_id NUMBER) IS
1324          SELECT DISTINCT adl.award_set_id,
1325 	                 adl.document_type,		-- Bug 2433889
1326 			 adl.invoice_id,		-- Bug 2433889
1327 			 adl.invoice_distribution_id	-- Bug 2433889
1328                     FROM ap_invoice_distributions_all ap,
1329                          gms_award_distributions adl
1330                    WHERE ap.invoice_distribution_id = p_inv_dist_id
1331                      AND ap.award_id IS NOT NULL
1332                      AND ap.award_id = adl.award_set_id
1333 		     AND adl.adl_line_num = 1
1334 		     UNION /* BUG 14216205 : Added the union for SAT */
1335 	 SELECT DISTINCT adl.award_set_id,
1336 			 adl.document_type,
1337 			 adl.invoice_id,
1338 			 adl.invoice_distribution_id
1339 		    FROM AP_SELF_ASSESSED_TAX_DIST_ALL apsat,
1340 			 GMS_AWARD_DISTRIBUTIONS ADL
1341 		   WHERE APSAT.INVOICE_DISTRIBUTION_ID = P_INV_DIST_ID
1342 		     AND APSAT.AWARD_ID                 IS NOT NULL
1343 		     AND APSAT.AWARD_ID                  = ADL.AWARD_SET_ID
1344                      AND adl.adl_line_num                = 1;
1345 
1346 
1347       CURSOR c_exp_adl (p_expenditure_item_id NUMBER,
1348                         p_cdl_line_num        NUMBER) IS
1349          SELECT *
1350            FROM gms_award_distributions
1351           WHERE document_type = 'EXP'
1352             AND adl_status = 'A'
1353             AND expenditure_item_id = NVL (p_expenditure_item_id, -1)
1354             AND cdl_line_num = NVL (p_cdl_line_num, -1);
1355 
1356       x_rec            c_exp_adl%ROWTYPE;
1357 
1358        	-- Bug 2155774, Cursor to pick all the REQ distributions not having adls
1359       	CURSOR c_REQ_miss_adls (p_distribution_id NUMBER) IS
1360       	SELECT adl.award_set_id, adl.adl_status
1361           FROM gms_award_distributions adl,
1362                po_req_distributions pd
1363        	 WHERE pd.distribution_id = p_distribution_id
1364            AND pd.award_id = adl.award_set_id
1365       	   AND adl.adl_line_num = 1
1366            AND not exists (select 1 from gms_award_distributions gad
1367 			    where gad.award_set_id = pd.award_id
1368 			     and  gad.document_type = 'REQ'
1369       			     and  gad.distribution_id = pd.distribution_id
1370       			     and  gad.adl_status = 'A');
1371 
1372       	CURSOR c_po_miss_adls (p_po_distribution_id NUMBER) IS
1373          SELECT pod.award_id award_set_id,
1374                 adl.adl_status  -- Bug 2155774
1375            FROM po_distributions_all pod,
1376                 gms_award_distributions adl
1377           WHERE pod.po_distribution_id = p_po_distribution_id
1378             AND pod.award_id IS NOT NULL
1379             AND pod.award_id = adl.award_set_id
1380 	    AND adl.adl_line_num = 1   -- Bug 2155774
1381             AND (adl.document_type = 'REQ'
1382 		 OR NOT EXISTS (SELECT 1
1383 		                  FROM gms_award_distributions gad
1384 				 WHERE gad.award_set_id = pod.award_id
1385 				   AND gad.po_distribution_id = pod.po_distribution_id
1386 				   AND gad.adl_status = 'A'));  -- Bug 2155774, added to pick distribution lines
1387 								-- created by copy of PO and distribution lines
1388 
1389 
1390 BEGIN
1391 
1392       x_flip_adl_status := 'N';  -- Bug 2155774
1393       g_error_procedure_name := 'misc_synch_adls';
1394 
1395 	-- ---------------------------------
1396 	--  x_stage := 10 - REQUISITION
1397 	--  x_stage := 20 - PO
1398 	--  x_stage := 30 - AP
1399 	-- ---------------------------------
1400 
1401       If p_application_id = 201 THEN
1402 
1403 	   g_error_stage := 'Requisitions and Purchase orders';
1404 
1405 	    FOR i in 1..g_set_of_books_id_tab.count LOOP
1406 
1407 	      -- ========================================================
1408 	      -- For REQ and PO records : Inactivate multiple active adls found at OHSU. This is
1409 	      -- due to data entry combinations.
1410 	      -- ========================================================
1411 	      -- bug : 2308005
1412 
1413 		UPDATE gms_award_distributions adl
1414 		   SET adl_status = 'I'
1415 		 WHERE adl.adl_status = 'A'
1416 		   AND (adl.award_set_id,adl.document_type) IN ( SELECT adl2.award_set_id,adl2.document_type
1417 								   FROM gms_award_distributions adl2,
1418                                                                         po_req_distributions_all pd
1419                                                                   WHERE g_document_type_tab(i) = 'REQ'
1420                                                                     AND adl2.document_type     = 'REQ'
1421                                                                     AND adl2.adl_line_num      = 1
1422                                                                     AND adl2.distribution_id   = pd.distribution_id
1423                                                                     AND pd.distribution_id     = g_doc_dist_id_tab(i)
1424                                                                     AND pd.award_id            <> adl2.award_set_id
1425                          					  UNION ALL
1426 						                 SELECT adl2.award_set_id,adl2.document_type
1427                         					   FROM gms_award_distributions adl2,
1428                                                                         po_distributions_all pd
1429                                                                   WHERE g_document_type_tab(i)    = 'PO'
1430                                                                     AND adl2.document_type        = 'PO'
1431                                                                     AND adl2.adl_line_num         = 1
1432                                                                     AND adl2.po_distribution_id   = pd.po_distribution_id
1433                                                                     AND pd.po_distribution_id     = g_doc_dist_id_tab(i)
1434                                                                     AND pd.award_id               <> adl2.award_set_id);
1435 
1436 	   END LOOP;
1437 
1438 	   FOR i in 1..g_set_of_books_id_tab.count LOOP
1439 
1440 	     -- ---------------------------------------------------------
1441 	     -- STAGE:10 - We need to create missing ADLs for Requistitions
1442 	     -- created using key Duplicate Record(Shift+F6)
1443 	     -- So synch processsing required here.
1444 	     -- ---------------------------------------------------------
1445 
1446 	    -- --------------------------------------------------------
1447 	    -- STAGE:20 - Purchasing.
1448 	    -- Standard purchasing and Releases are okay as they are
1449 	    -- Entered.
1450 	    -- REQ - Becomes PO or release will have AWARD SET ID
1451 	    -- copied from REQ. We need to create ADLS for that PO
1452 	    -- So that ADLS are in SYNCH with PO.
1453 	    -- ---------------------------------------------------------
1454 
1455 	    -- --------------------------------------------------------------------
1456 	    -- CALL COPY ADLS COMMON PACKAGE to copy the new ADLS
1457 	    -- and also update the distribution line to create a
1458 	    -- link between distribution line and ADLS.
1459 	    -- gms_awards_dist_pkg.copy_adls( p_award_set_id      IN  NUMBER ,
1460 	    --                P_NEW_AWARD_SET_ID  OUT NOCOPY NUMBER,
1461 	    --                p_doc_type          IN  varchar2,
1462 	    --                p_dist_id           IN  NUMBER,
1463 	    --                P_INVOICE_ID        IN  NUMBER DEFAULT NULL,
1464 	    --                p_dist_line_num     IN  NUMBER DEFAULT NULL   )
1465 	    -- --------------------------------------------------------------------
1466 
1467 	    g_error_stage := 'MISC_ADL: REQ and PO ';
1468 
1469             l_award_set_id := 0;
1470 
1471 	    IF g_document_type_tab(i) = 'REQ' THEN
1472 
1473 	       OPEN  c_REQ_miss_adls (g_doc_dist_id_tab(i));
1474 	       FETCH c_REQ_miss_adls INTO l_award_set_id ,l_adl_status;
1475                CLOSE c_REQ_miss_adls;
1476 
1477 	    ELSIF g_document_type_tab(i) = 'PO' THEN
1478 
1479 	       OPEN  c_po_miss_adls (g_doc_dist_id_tab(i));
1480 	       FETCH c_po_miss_adls INTO l_award_set_id ,l_adl_status;
1481                CLOSE c_po_miss_adls;
1482 
1483 	    END IF;
1484 
1485 	    IF NVL(l_award_set_id ,0) <>0  THEN
1486 
1487 	      If l_adl_status = 'I' Then
1488 			update gms_award_distributions
1489 			set adl_status = 'A'
1490 			where award_set_id  = l_award_set_id ;
1491 			x_flip_adl_status := 'Y';
1492 	      End If;
1493 
1494 	      gms_awards_dist_pkg.copy_adls(p_award_set_id =>  l_award_set_id,
1495 					    P_NEW_AWARD_SET_ID => x_award_set_id,          --OUT variable
1496 					    p_doc_type => g_document_type_tab(i),
1497 					    p_dist_id => g_doc_dist_id_tab(i),
1498 					    p_called_from => 'MISC_SYNCH_ADLS'
1499 					    );
1500 
1501 	      If (x_flip_adl_status = 'Y') Then
1502 			update gms_award_distributions
1503 			set adl_status = 'I'
1504 			where award_set_id  = l_award_set_id ;
1505 			x_flip_adl_status := 'N';
1506 	      End If;
1507 
1508             END IF; --IF NVL(l_award_set_id ,0) <>0  THEN
1509 
1510 	   END LOOP;
1511 
1512       ELSIF p_application_id = 200 THEN
1513 
1514 	    g_error_stage := 'AP INVOICE';
1515 	    -- =============================================================================
1516 	    -- AP invoice distribution lines having ADls inactive. This occurs due to update
1517 	    -- in when validate item. We are changing the status to 'I' when award change.
1518 	    -- Now if someone clears record then ADLs stays at inactive status.
1519 	    -- =============================================================================
1520 	    -- 2308005 ( CLEARING INVOICE DIST. LINE AFTER CHANGING AWARD MAKES ADL STATUS 'I' ).
1521 	    -- ===================
1522 
1523 	    FOR i in 1..g_set_of_books_id_tab.count LOOP
1524 
1525               update gms_award_distributions  adl
1526                  set adl.adl_status = 'A'
1527                where adl.document_type = 'AP'
1528                  and adl.adl_status    = 'I'
1529                  and adl.award_set_id in ( select adl2.award_set_id
1530                                              from gms_award_distributions adl2,
1531                                                   ap_invoice_distributions_all apd
1532                                             where apd.invoice_id        = g_doc_header_id_tab(i)
1533                                               AND apd.invoice_distribution_id = g_doc_dist_id_tab(i)
1534                                               and apd.award_id          is not null
1535                                               and adl2.award_set_id     = apd.award_id
1536                                               and adl2.invoice_id       = apd.invoice_id
1537                                               and adl2.document_type    = 'AP'
1538                                               and adl2.invoice_distribution_id = apd.invoice_distribution_id
1539                                               and adl2.adl_status       = 'I'
1540 					    union /* BUG 14216205 : Added the union for SAT */
1541                                             select ADL3.AWARD_SET_ID
1542                                               from GMS_AWARD_DISTRIBUTIONS ADL3,
1543                                                     AP_SELF_ASSESSED_TAX_DIST_ALL APSAT
1544                                               where APSAT.INVOICE_ID             = g_doc_header_id_tab(i)
1545                                                 AND apsat.invoice_distribution_id  = g_doc_dist_id_tab(i)
1546                                                 AND apsat.award_id                IS NOT NULL
1547                                                 and ADL3.AWARD_SET_ID            = APSAT.AWARD_ID
1548                                                 AND adl3.invoice_id              = apsat.invoice_id
1549                                                 AND adl3.document_type           = 'AP'
1550                                                 and ADL3.INVOICE_DISTRIBUTION_ID = APSAT.INVOICE_DISTRIBUTION_ID
1551                                                 AND adl3.adl_status              = 'I'
1552 					      ) ;
1553 
1554 	    END LOOP;
1555 
1556 	    FOR i in 1..g_set_of_books_id_tab.count LOOP
1557 
1558 		-- ---------------------------------------------------------------
1559 		-- STAGE:30 - AP Invoice.
1560 		-- Standard AP invoice/credit memo/debit memo are okay as they are
1561 		-- Entered.
1562 		-- CASE 1 - PO - Becomes AP will have AWARD SET ID
1563 		--          copied from PO. We need to create ADLS for that AP
1564 		--          So that ADLS are in SYNCH with AP.
1565 		-- CASE 2 - PRORATE
1566 		-- CASE 3 - REVERSAL
1567 		-- CASE 4 - Credit memo matching to AP invoice/ PO
1568 		-- CASE 5 - Debit memo matching to AP invoice/ PO
1569 		-- ---------------------------------------------------------
1570 		-- --------------------------------------------------------------------
1571 		-- CALL COPY ADLS COMMON PACKAGE to copy the new ADLS
1572 		-- and also update the distribution line to create a
1573 		-- link between distribution line and ADLS.
1574 		-- gms_awards_dist_pkg.copy_adls( p_award_set_id      IN  NUMBER ,
1575 		--                P_NEW_AWARD_SET_ID  OUT NOCOPY NUMBER,
1576 		--                p_doc_type          IN  varchar2,
1577 		--                p_dist_id           IN  NUMBER,
1578 		--                P_INVOICE_ID        IN  NUMBER DEFAULT NULL,
1579 		--                p_dist_line_num     IN  NUMBER DEFAULT NULL   )
1580 		-- --------------------------------------------------------------------
1581 		-- Find out NOCOPY do we need to create ADLS
1582 		-- ----------------------------------
1583 
1584 		-- Bug 2433889 : Added following If statement to incorporate cursor logic in the
1585 		--		 If statement
1586 		-- R12 Funds Managment Uptake : Unique identifier of invoice distribution is invoice_distribution_id
1587 
1588 
1589 		OPEN c_ap(g_doc_dist_id_tab(i));
1590 		FETCH c_ap INTO l_award_set_id,
1591 		                l_adl_document_type,
1592 				l_adl_invoice_id,
1593 				l_adl_dist_id;
1594 
1595 
1596 		IF (C_AP%FOUND AND
1597 		    ( (l_adl_document_type  = 'PO')
1598 		      OR
1599 		      ( /* Bug 5344693 : Removed the document_header_id condition as the document_distribution_id is an unique identifier */
1600 		       g_doc_dist_id_tab(i) <> NVL(l_adl_dist_id,0) AND
1601 		       l_adl_document_type = 'AP'
1602 		     )
1603 		    OR
1604 		     -- Forward port 3598982
1605 		     --
1606 		     ( l_adl_document_type  = 'APD')
1607 		   ))
1608 		 THEN
1609 
1610 			 gms_awards_dist_pkg.copy_adls (
1611 			    p_award_set_id => l_award_set_id,
1612 			    P_NEW_AWARD_SET_ID => x_award_set_id,
1613 			    p_doc_type => 'AP',
1614 			    p_dist_id => g_doc_dist_id_tab(i),
1615 			    P_INVOICE_ID => g_doc_header_id_tab(i),
1616 			    p_dist_line_num => g_doc_dist_line_num_tab(i),
1617 			    p_called_from => 'MISC_SYNCH_ADLS');
1618 		END IF;
1619 
1620 		CLOSE c_ap;
1621 
1622 	     END LOOP;
1623 
1624 	     FOR i in 1..g_set_of_books_id_tab.count LOOP
1625 
1626 		IF g_inv_source_tab(i) = 'Oracle Project Accounting' AND g_invoice_type_code_tab(i) = 'EXPENSE REPORT' THEN
1627 
1628 		     -- ------------------------------------------------------------------------------
1629 		     -- BUG:1361978 - Payable invoice import process for exp report is not bringing
1630 		     -- award.
1631 		     -- ------------------------------------------------------------------------------
1632 		      g_error_stage := 'MISC_ADL: EXP. REP';
1633 
1634 			 OPEN c_exp_adl (g_inv_dist_reference_1_tab(i),
1635 				         g_inv_dist_reference_2_tab(i));
1636 			 FETCH c_exp_adl INTO x_rec;
1637 			 IF c_exp_adl%NOTFOUND THEN
1638 			    CLOSE c_exp_adl;
1639 			    RAISE NO_DATA_FOUND;
1640 			 END IF;
1641 			 CLOSE c_exp_adl;
1642 
1643 			 x_rec.document_type := 'AP';
1644 			 x_rec.cdl_line_num := NULL;
1645 			 x_rec.expenditure_item_id := NULL;
1646 			 x_rec.invoice_id := g_doc_header_id_tab(i);
1647 			 x_rec.invoice_distribution_id := g_doc_dist_id_tab(i);
1648 			 x_rec.distribution_line_number := g_doc_dist_line_num_tab(i);
1649 			 x_rec.adl_line_num := 1;
1650 			 x_rec.award_set_id := gms_awards_dist_pkg.get_award_set_id;
1651 
1652 			 gms_awards_dist_pkg.create_adls (x_rec);
1653 
1654 			 UPDATE ap_invoice_distributions_all
1655 			    SET award_id = x_rec.award_set_id
1656 			  WHERE invoice_id = g_doc_header_id_tab(i)
1657 			    AND invoice_distribution_id = g_doc_dist_id_tab(i);
1658 /* BUG 14216205 : Added the following Code for SAT : Starts */
1659 			  UPDATE AP_SELF_ASSESSED_TAX_DIST_ALL
1660 			    SET award_id = x_rec.award_set_id
1661 			  where INVOICE_ID = G_DOC_HEADER_ID_TAB(I)
1662 			    AND invoice_distribution_id = g_doc_dist_id_tab(i);
1663 /* BUG 14216205 : Added the following Code for SAT : Ends */
1664 		END IF;
1665 
1666 	      END LOOP;
1667 
1668 	-- ------------------------------------------------------------------------------
1669 	-- BUG:1361978 - Payable invoice import process for exp report is not bringing
1670 	-- award. End of change.
1671 	-- ------------------------------------------------------------------------------
1672       End If;   --If p_application_id = 201 THEN
1673 
1674       RETURN TRUE;
1675 
1676 EXCEPTION
1677       WHEN OTHERS THEN
1678          IF c_ap%ISOPEN THEN
1679             CLOSE c_ap;
1680          END IF;
1681          IF c_exp_adl%ISOPEN THEN
1682             CLOSE c_exp_adl;
1683          END IF;
1684          RAISE;
1685 END misc_synch_adls;
1686 
1687    -- R12 Funds Management Uptake : Obsolete data_transfer_failure procedure as this logic is handled in copy_gl_pkt_to_gms_pkt
1688    --============================================================================================
1689    -- Bug 2899151 :  This procedure will fail gl_bc_packets and gms_bc_packets
1690    -- This procedure will be used when all records in gl_bc_packets (corresponding to sponsored)
1691    -- and not existing in gms_bc_packets.
1692    --============================================================================================
1693 
1694 
1695 -- =====================================================================================================
1696 -- R12 Funds Managment Uptake : New autonomous procedure to update failed result and status codes on
1697 -- gms and gl bc packets if burdenable raw cost calculation failed for atleast one record in packet.
1698 -- This called when calling_mode ='R' (Reserve)/'C' (Checkfunds)/'U' (unreserve) for AP/PO/REQ transactions.
1699 -- This procedure will be fired from main session procedure copy_gl_pkt_to_gms_pkt which in turn is
1700 -- called from GL main budgetory control API.
1701 -- =====================================================================================================
1702 
1703  PROCEDURE UPDATE_BC_PKT_BRC_STATUS  ( p_packet_id   IN NUMBER,
1704                                        p_result_code IN VARCHAR2,
1705 				       p_partial_flag IN VARCHAR2,
1706                                        p_mode         IN VARCHAR2 ) IS
1707 
1708  PRAGMA AUTONOMOUS_TRANSACTION;
1709  l_count NUMBER;
1710 
1711  CURSOR C_count_rejected_rec IS
1712  SELECT count(*)
1713    FROM gms_bc_packets
1714   WHERE packet_id = p_packet_id
1715     AND status_code in ( 'I' ,'P')
1716     AND substr(result_code,1,1) = 'F' ;
1717 
1718  BEGIN
1719 
1720   IF g_debug = 'Y' THEN
1721      gms_error_pkg.gms_debug ('UPDATE_BC_PKT_BRC_STATUS'||':'|| 'Start','C');
1722   END IF;
1723 
1724   -- Burdenable raw cost calculation procedure returned FALSE due to exception
1725 
1726   IF p_result_code  = 'F' THEN
1727 
1728      IF g_debug = 'Y' THEN
1729         gms_error_pkg.gms_debug ('UPDATE_BC_PKT_BRC_STATUS'||':'|| 'Updating gms and gl packets to failed status F76/F67','C');
1730      END IF;
1731 
1732      result_status_code_update ( p_packet_id=> p_packet_id,
1733                                  p_status_code=> 'T',
1734                                  p_result_code=> 'F76');
1735 
1736     -- Bug : 2557041 - Added for IP check funds Enhancement
1737     -- Update gl_bc_packets result_code to F67 if update Burdenable Raw Cost
1738     -- failed.
1739 
1740     -- R12 funds management Uptake : no update on gl_bc_packets required
1741     /*UPDATE gl_bc_packets
1742        SET result_code = DECODE (NVL (SUBSTR (result_code, 1, 1), 'P'),'P', 'F67',result_code)
1743      WHERE packet_id = P_packet_id;   */
1744 
1745   END IF;
1746 
1747   IF p_partial_flag = 'N' THEN -- Full mode
1748 	  -- Burdenable raw cost calculation failed on atleast one record in packet
1749 	  -- If any transaction has failed burdenable_raw_cost calculation and
1750 	  -- the mode is 'R' or 'U' or 'C' .. fail the packet with F65
1751 
1752 	  -- Check for failure
1753 	  OPEN  C_count_rejected_rec;
1754 	  FETCH C_count_rejected_rec INTO l_count;
1755 	  CLOSE C_count_rejected_rec;
1756 
1757 
1758 	  IF l_count <> 0 THEN
1759 
1760 	     IF g_debug = 'Y' THEN
1761 		gms_error_pkg.gms_debug ('UPDATE_BC_PKT_BRC_STATUS'||':'|| 'Updating gms packets to Full mode failure','C');
1762 	     END IF;
1763 
1764 	     --  If failure, update result/status code
1765 	     Update gms_bc_packets
1766 		set status_code = decode(p_mode,'C','F','R'),
1767 		    result_code =decode(result_code,null,'F65',
1768 					decode(substr(result_code,1,1),'P','F65',result_code)),
1769 		    fc_error_message = decode(fc_error_message,NULL,
1770 					      'COPY_GL_PKT_TO_GMS_PKT: Post burden calculation Check',fc_error_message)
1771 	      where packet_id = p_packet_id
1772 		AND status_code in ( 'I' ,'P');
1773 
1774 	  END If;
1775   END IF; -- If p_partial_flag = 'N' THEN -- Full mode
1776 
1777   COMMIT;
1778 
1779 END UPDATE_BC_PKT_BRC_STATUS;
1780 
1781 -- =====================================================================================================
1782 -- R12 Funds Managment Uptake : New autonomous procedure to insert records into gms_bc_packets and also
1783 -- updates failed result and status codes on gms packets in case of any failures during loding.
1784 -- This procedure will be fired from main session procedure copy_gl_pkt_to_gms_pkt which in turn is
1785 -- called from GL main budgetory control API.
1786 -- Input parameters : PLSQL tables storing data associated with gl pkts and AP/PO/REQ tables as the
1787 --                    uncommited data in these transaction tables will not be accessible from
1788 --                    autonomous session.
1789 --                    p_gl_bc_pkt_spon_count -  This parameter stores number of eligible gms records
1790 --                                              in gl_bc_packets
1791 --                    p_return_code          -  'F' - Insertion Failed / Failed status records inserted
1792 --                                              'P' - Insertion passed and no failed status records inserted
1793 -- =====================================================================================================
1794 
1795 PROCEDURE Load_gms_pkts(
1796                  p_packet_id             IN NUMBER,
1797 		 p_partial_flag          IN VARCHAR2,
1798                  p_set_of_books_id_tab   IN t_set_of_books_id_type,
1799                  p_je_source_name_tab    IN t_je_source_name_type,
1800 		 p_je_category_name_tab  IN t_je_category_name_type,
1801 		 p_actual_flag_tab       IN t_actual_flag_type,
1802 		 p_project_id_tab        IN t_project_id_type,
1803 		 p_task_id_tab           IN t_task_id_type,
1804 		 p_award_id_tab          IN t_award_id_type,
1805 		 p_result_code_tab       IN t_result_code_type,
1806 		 p_entered_dr_tab        IN t_entered_dr_type,
1807 		 p_entered_cr_tab        IN t_entered_cr_type,
1808 		 p_etype_tab             IN t_etype_type,
1809 		 p_exp_org_id_tab        IN t_exp_org_id_type,
1810 		 p_exp_item_date_tab     IN t_exp_item_date_type,
1811 		 p_document_type_tab     IN t_document_type_type,
1812                  p_doc_header_id_tab     IN t_doc_header_id_type,
1813                  p_doc_dist_id_tab       IN t_doc_dist_id_type,
1814 		 p_vendor_id_tab         IN t_vendor_id_type,
1815 		 p_exp_category_tab      IN t_exp_category_type,
1816 		 p_revenue_category_tab  IN t_revenue_category_type,
1817 		 p_ind_cmp_set_id_tab    IN t_ind_cmp_set_id_type,
1818 		 p_burdenable_raw_cost_tab IN t_brc_type, --R12 AP Lines Uptake
1819 		 p_source_event_id_tab     IN t_source_event_id_type,
1820                  p_return_code           OUT NOCOPY VARCHAR2
1821                ) IS
1822 
1823 	PRAGMA AUTONOMOUS_TRANSACTION;
1824 
1825 	l_rec_count                NUMBER;
1826 	l_accrue_at_receipt_flag   VARCHAR2(1);
1827 	l_request_id               NUMBER;
1828 	l_sysdate                  DATE;
1829         l_user_id                  NUMBER;
1830         l_login_id                 NUMBER;
1831 
1832    BEGIN
1833 
1834         IF g_debug = 'Y' THEN
1835           gms_error_pkg.gms_debug ('Load_gms_pkts'||':'|| 'Start','C');
1836         END IF;
1837 
1838 
1839 	l_rec_count := p_set_of_books_id_tab.count();
1840 
1841         IF g_debug = 'Y' THEN
1842           gms_error_pkg.gms_debug ('Load_gms_pkts'||':'|| 'Intializing local variables','C');
1843         END IF;
1844 
1845 	p_return_code    := 'P';
1846 	l_request_id     := fnd_global.conc_request_id;
1847         l_user_id        := fnd_global.user_id;
1848         l_login_id       := fnd_global.login_id;
1849 
1850 	IF l_request_id = -1 Then
1851 	  l_request_id := NULL;
1852         end if;
1853 
1854  	SELECT SYSDATE
1855 	  INTO l_sysdate
1856 	  FROM DUAL;
1857 
1858 	IF l_rec_count > 0 Then
1859 
1860            IF g_debug = 'Y' THEN
1861               gms_error_pkg.gms_debug ('Load_gms_pkts'||':'|| 'Starting loop to insert '||l_rec_count||'into gms_bc_packets','C');
1862            END IF;
1863 
1864    	   FORALL i IN 1 .. l_rec_count
1865 	      INSERT INTO gms_bc_packets
1866 			  (packet_id,
1867 			   set_of_books_id,
1868 			   je_source_name,
1869 			   je_category_name,
1870 			   actual_flag,
1871 			   project_id,
1872 			   task_id,
1873 			   award_id,
1874 			   result_code,
1875 			   status_code,
1876 			   last_update_date,
1877 			   last_updated_by,
1878 			   created_by,
1879 			   creation_date,
1880 			   last_update_login,
1881 			   entered_dr,
1882 			   entered_cr,
1883 			   expenditure_type,
1884 			   burdenable_raw_cost,
1885 			   expenditure_organization_id,
1886 			   expenditure_item_date,
1887 			   document_type,
1888 			   document_header_id,
1889 			   document_distribution_id,
1890 			   transfered_flag,
1891 			   account_type,
1892 			   bc_packet_id,
1893 			   vendor_id,
1894 			   expenditure_category,
1895 			   revenue_category,
1896 			   request_id,
1897 			   ind_compiled_set_id,
1898                            source_event_id)
1899 	          SELECT   p_packet_id
1900 		          ,p_set_of_books_id_tab(i)
1901 			  ,p_je_source_name_tab(i)
1902 			  ,p_je_category_name_tab(i)
1903  			  ,p_actual_flag_tab(i)
1904 			  ,p_project_id_tab(i)
1905 			  ,p_task_id_tab(i)
1906 			  ,p_award_id_tab(i)
1907 			  ,p_result_code_tab(i)
1908  			  ,DECODE (p_award_id_tab(i), NULL, 'R', DECODE(p_document_type_tab(i),'PO','I'
1909 			                                                                      ,'REQ','I'
1910 											      ,'AP','I')) --Check for GMSIP impact as it was always 'P'
1911 			  ,l_sysdate
1912 			  ,l_user_id
1913 			  ,l_user_id
1914 			  ,l_sysdate
1915 			  ,l_login_id
1916 			  ,p_entered_dr_tab(i)
1917 			  ,p_entered_cr_tab(i)
1918 			  ,p_etype_tab(i)
1919 			  ,DECODE(p_document_type_tab(i),'AP',p_burdenable_raw_cost_tab(i),NULL) --R12 AP Lines Uptake
1920 			  ,p_exp_org_id_tab(i)
1921 			  ,p_exp_item_date_tab(i)
1922 			  ,p_document_type_tab(i)
1923 			  ,p_doc_header_id_tab(i)
1924 			  ,p_doc_dist_id_tab(i)
1925 			  ,'N'  -- For GMSIP 'N' should not cause an issue
1926                           ,NULL
1927 			  ,gms_bc_packets_s.NEXTVAL
1928 			  ,p_vendor_id_tab(i)
1929 			  ,p_exp_category_tab(i)
1930 			  ,p_revenue_category_tab(i)
1931 			  ,l_request_id
1932 			  ,p_ind_cmp_set_id_tab(i)
1933 			  ,p_source_event_id_tab(i)
1934                    FROM dual;
1935         END If;
1936 
1937 
1938         IF p_partial_flag = 'N'  THEN -- If full mode
1939 
1940            FOR  i IN 1 .. l_rec_count LOOP
1941 
1942 		IF SUBSTR(p_result_code_tab(i),1,1) = 'F' THEN
1943 
1944 		 UPDATE gms_bc_packets
1945 		    SET result_code = 'F65',
1946 			fc_error_message = decode(fc_error_message,NULL,'Load_gms_pkts:Full mode failure',fc_error_message)
1947 		    WHERE packet_id   = p_packet_id
1948 		      and SUBSTR(result_code,1,1) <> 'F' ;
1949 
1950 		 p_return_code := 'F';
1951 		 EXIT;
1952 		END If;
1953             END LOOP;
1954 
1955          END If;
1956 
1957 COMMIT;
1958 
1959 EXCEPTION
1960       WHEN OTHERS THEN
1961           IF g_debug = 'Y' THEN
1962 	    gms_error_pkg.gms_debug ('Load_gms_pkts - Exception '||' SQLCODE:'||SQLCODE||' SQLERRM:'||SQLERRM, 'C');
1963 	  END IF;
1964 	  p_return_code := 'F';
1965 END Load_gms_pkts;
1966 
1967 -----------------------------------------------------------------------------------
1968 -- R12 Funds management Uptake : New procedure create new record in plsql tabs for
1969 -- backing PO/Release/variance
1970 -----------------------------------------------------------------------------------
1971 
1972 PROCEDURE COPY_AP_RECORD (p_copy_from_index    IN NUMBER,
1973                           p_new_rec_index      IN NUMBER,
1974 			  p_document_type      IN VARCHAR2,
1975 			  p_po_vendor_id       IN NUMBER,
1976  		          p_po_ind_com_set_id  IN NUMBER,
1977 			  p_entered_dr         IN NUMBER,
1978 			  p_entered_cr         IN NUMBER) IS
1979 BEGIN
1980 
1981        IF g_debug = 'Y' THEN
1982          gms_error_pkg.gms_debug (g_error_procedure_name||':'||'COPY_AP_RECORD - Start','C');
1983          gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of parameter p_copy_from_index ='||p_copy_from_index,'C');
1984          gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of parameter p_new_rec_index ='||p_new_rec_index,'C');
1985          gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of parameter p_document_type ='||p_document_type,'C');
1986          gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of parameter p_po_vendor_id ='||p_po_vendor_id,'C');
1987          gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of parameter p_po_ind_com_set_id ='||p_po_ind_com_set_id,'C');
1988          gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of parameter p_entered_dr ='||p_entered_dr,'C');
1989          gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of parameter p_entered_cr ='||p_entered_cr,'C');
1990        END IF;
1991 
1992        g_je_source_name_tab(p_new_rec_index)     := g_je_source_name_tab(p_copy_from_index);
1993        g_je_category_name_tab(p_new_rec_index)   := g_je_category_name_tab(p_copy_from_index);
1994        g_actual_flag_tab(p_new_rec_index)        := g_actual_flag_tab(p_copy_from_index);
1995        g_project_id_tab(p_new_rec_index)         := g_project_id_tab(p_copy_from_index);
1996        g_task_id_tab(p_new_rec_index)            := g_task_id_tab(p_copy_from_index);
1997        g_award_id_tab(p_new_rec_index)           := g_award_id_tab(p_copy_from_index);
1998        g_entered_dr_tab(p_new_rec_index)         := p_entered_dr;
1999        g_entered_cr_tab(p_new_rec_index)         := p_entered_cr;
2000        g_etype_tab(p_new_rec_index)              := g_etype_tab(p_copy_from_index);
2001        g_exp_org_id_tab(p_new_rec_index)         := g_exp_org_id_tab(p_copy_from_index);
2002        g_exp_item_date_tab(p_new_rec_index)      := g_exp_item_date_tab(p_copy_from_index);
2003        g_document_type_tab(p_new_rec_index)      := g_document_type_tab(p_copy_from_index);
2004        g_doc_header_id_tab(p_new_rec_index)      := g_doc_header_id_tab(p_copy_from_index);
2005        g_doc_dist_id_tab(p_new_rec_index)        := g_doc_dist_id_tab(p_copy_from_index);
2006        g_prepay_std_inv_dist_id(p_new_rec_index) := g_prepay_std_inv_dist_id(p_copy_from_index);
2007        g_source_event_id_tab(p_new_rec_index)    := g_source_event_id_tab(p_copy_from_index);
2008        g_result_code_tab(p_new_rec_index)        := g_result_code_tab(p_copy_from_index);
2009        g_vendor_id_tab(p_new_rec_index)          := g_vendor_id_tab(p_copy_from_index);
2010        g_ind_cmp_set_id_tab(p_new_rec_index)     := g_ind_cmp_set_id_tab(p_copy_from_index);
2011        g_burdenable_raw_cost_tab(p_new_rec_index):= NULL;
2012        g_quantity_variance_tab(p_new_rec_index)  := NULL;
2013        g_amount_variance_tab(p_new_rec_index)    := NULL;
2014        g_po_distribution_id_tab(p_new_rec_index) := g_po_distribution_id_tab(p_copy_from_index);
2015        g_po_header_id_tab(p_new_rec_index)       := g_po_header_id_tab(p_copy_from_index);
2016        g_po_release_id_tab(p_new_rec_index)      := g_po_release_id_tab(p_copy_from_index);
2017        g_set_of_books_id_tab(p_new_rec_index)    := g_set_of_books_id_tab(p_copy_from_index);
2018        g_exp_category_tab(p_new_rec_index)       := g_exp_category_tab(p_copy_from_index);
2019        g_revenue_category_tab(p_new_rec_index)   := g_revenue_category_tab(p_copy_from_index);
2020        g_doc_dist_line_num_tab(p_new_rec_index)  := g_doc_dist_line_num_tab(p_copy_from_index);
2021        g_invoice_type_code_tab(p_new_rec_index)  := g_invoice_type_code_tab(p_copy_from_index);
2022        g_inv_source_tab(p_new_rec_index)         := g_inv_source_tab(p_copy_from_index);
2023        g_inv_dist_reference_1_tab(p_new_rec_index):= g_inv_dist_reference_1_tab(p_copy_from_index);
2024        g_inv_dist_reference_2_tab(p_new_rec_index):= g_inv_dist_reference_2_tab(p_copy_from_index);
2025 
2026        IF p_document_type = 'PO' THEN
2027 
2028 	       g_je_source_name_tab(p_new_rec_index)     := 'Purchasing';
2029 	       g_je_category_name_tab(p_new_rec_index)   := 'Purchases';
2030                g_document_type_tab(p_new_rec_index)      := 'PO';
2031                g_doc_header_id_tab(p_new_rec_index)      := NVL(g_po_release_id_tab(p_copy_from_index),g_po_header_id_tab(p_copy_from_index));
2032                g_doc_dist_id_tab(p_new_rec_index)        := g_po_distribution_id_tab(p_copy_from_index);
2033 	       g_vendor_id_tab(p_new_rec_index)          := p_po_vendor_id;
2034 	       g_ind_cmp_set_id_tab(p_new_rec_index)     := p_po_ind_com_set_id;
2035 	       g_po_distribution_id_tab(p_new_rec_index) := NULL;
2036 	       g_doc_dist_line_num_tab(p_new_rec_index)  := NULL;
2037 	       g_invoice_type_code_tab(p_new_rec_index)  := NULL;
2038 	       g_inv_source_tab(p_new_rec_index)         := NULL;
2039 	       g_inv_dist_reference_1_tab(p_new_rec_index):= NULL;
2040 	       g_inv_dist_reference_2_tab(p_new_rec_index):= NULL;
2041 
2042        END IF;
2043 
2044        IF g_debug = 'Y' THEN
2045          gms_error_pkg.gms_debug (g_error_procedure_name||':'||'COPY_AP_RECORD - End','C');
2046        END IF;
2047 
2048 EXCEPTION
2049 WHEN OTHERS THEN
2050   IF g_debug = 'Y' THEN
2051      gms_error_pkg.gms_debug (g_error_procedure_name||':'||'COPY_AP_RECORD - exception'||SQLERRM,'C');
2052   END IF;
2053   RAISE;
2054 END COPY_AP_RECORD;
2055 
2056 -----------------------------------------------------------------------------------
2057 -- R12 Funds management Uptake : New procedure to create additional records
2058 -- for associated PO/RELEASE/AMOUNT variance/Quantity Variance while
2059 -- fundschecking AP records.
2060 -----------------------------------------------------------------------------------
2061 
2062 PROCEDURE CREATE_BACKING_PO_APVAR_REC (p_copy_from_index    IN NUMBER,
2063                                        p_new_rec_index      IN OUT NOCOPY NUMBER,
2064 				       p_po_vendor_id       IN NUMBER,
2065 				       p_po_ind_com_set_id  IN NUMBER )  IS
2066 
2067 l_new_rec_index             NUMBER; /*modified for bug:	7203553*/
2068 l_entered_dr                NUMBER;
2069 l_entered_cr                NUMBER;
2070 
2071 BEGIN
2072 
2073        IF g_debug = 'Y' THEN
2074          gms_error_pkg.gms_debug (g_error_procedure_name||':'||'CREATE_BACKING_PO_APVAR_REC - Start','C');
2075        END IF;
2076 
2077        -- Below is the logic of splitting AP line into multiple bc records
2078        -- 1.IF po_distribution id populated then create PO relieving record
2079        --   a. IF amount variance exists then
2080        --           Create new BC record for variance line since both AP line and variance
2081        --           are eligible for FC
2082        --   b. IF quantity varaince exists then
2083        --           Create new BC record for variance line since both main AP line and variance
2084        --           are eligible for FC .-- Functionality to be verified.
2085 
2086        l_new_rec_index  := p_new_rec_index  ;
2087 
2088        /* Bug 5614467 :
2089 	    If the rate on the PO distribution is not null then
2090 	      Calculate accounted amounts for PO relieving record from its entered amounts and rate
2091             Else
2092 	      Copy accounted amounts for PO relieving record from those on the invoice */
2093        If g_po_rate_tab(p_copy_from_index) is NOT NULL then
2094          IF g_debug = 'Y' THEN
2095            gms_error_pkg.gms_debug (g_error_procedure_name||':'||'CREATE_BACKING_PO_APVAR_REC - Calculating accounted amounts for PO relieving record from its entered amounts and rate','C');
2096            gms_error_pkg.gms_debug (g_error_procedure_name||':'||'CREATE_BACKING_PO_APVAR_REC - Rate : '||g_po_rate_tab(p_copy_from_index),'C');
2097 	 END IF;
2098          l_entered_dr := g_txn_cr_tab(p_copy_from_index) * g_po_rate_tab(p_copy_from_index);
2099          l_entered_cr := g_txn_dr_tab(p_copy_from_index) * g_po_rate_tab(p_copy_from_index);
2100        else
2101          IF g_debug = 'Y' THEN
2102            gms_error_pkg.gms_debug (g_error_procedure_name||':'||'CREATE_BACKING_PO_APVAR_REC - Copying accounted amounts for PO relieving record from those on the invoice','C');
2103 	 END IF;
2104 	 l_entered_dr := g_entered_cr_tab(p_copy_from_index);
2105          l_entered_cr := g_entered_dr_tab(p_copy_from_index);
2106        end if;
2107        IF g_debug = 'Y' THEN
2108          gms_error_pkg.gms_debug (g_error_procedure_name||':'||'CREATE_BACKING_PO_APVAR_REC - PO relieving record - l_entered_dr : '||l_entered_dr,'C');
2109 	 gms_error_pkg.gms_debug (g_error_procedure_name||':'||'CREATE_BACKING_PO_APVAR_REC - PO relieving record - l_entered_cr : '||l_entered_cr,'C');
2110        END IF;
2111 
2112        COPY_AP_RECORD   (p_copy_from_index    => p_copy_from_index,
2113                           p_new_rec_index     => l_new_rec_index,
2114 			  p_document_type     => 'PO',
2115 			  p_po_vendor_id      => p_po_vendor_id,
2116  		          p_po_ind_com_set_id => p_po_ind_com_set_id,
2117 			  p_entered_dr        => l_entered_dr,
2118 			  p_entered_cr        => l_entered_cr);
2119 
2120        IF NVL(g_amount_variance_tab(p_copy_from_index),0) <> 0 THEN
2121 
2122                IF g_debug = 'Y' THEN
2123                  gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Calling CREATE_APVAR_RECORD for amount varaince','C');
2124                END IF;
2125 
2126                l_new_rec_index := l_new_rec_index + 1;
2127 
2128 	       IF g_amount_variance_tab(p_copy_from_index) <0 THEN
2129 		       l_entered_dr := 0;
2130 		       l_entered_cr := ABS(g_amount_variance_tab(p_copy_from_index));
2131                ELSE
2132 		       l_entered_dr := ABS(g_amount_variance_tab(p_copy_from_index));
2133 		       l_entered_cr := 0;
2134 	       END IF;
2135 
2136 	       COPY_AP_RECORD   (p_copy_from_index    => p_copy_from_index,
2137 				  p_new_rec_index     => l_new_rec_index,
2138 				  p_document_type     => 'AP',
2139 				  p_po_vendor_id      => NULL,
2140 				  p_po_ind_com_set_id => NULL,
2141 				  p_entered_dr        => l_entered_dr,
2142 				  p_entered_cr        => l_entered_cr);
2143 
2144 	       /* Bug 5369296 : If the AP distribution is a reversing distribution (i.e parent_reversal_id is not  null) ,
2145 	          then the burdenable raw cost on the amount/quantity variance records in gms_bc_packets is stamped as 0.
2146 		  This is because after cancelling an invoice matched to a PO with quantity/amount variance the
2147 		  burdenable raw cost for the reversing distribution is populated on the basis of that populated in
2148 		  gms_award_distributions for the original distribution . The burdenable raw cost populated in
2149 		  gms_award_distributions for the original distribution includes the burdenable raw cost for both the normal
2150 		  distribution  amount and the quantity/amount variance amount. So during invoice cancel we should not
2151 		  create the burden for the amount/quantity variance record in gms_bc_packets i.e the burdenable raw cost on the
2152 		  quantity/amount variance record in gms_bc_packets for the reversing distribution should be zero. */
2153 
2154 	       IF g_parent_reversal_id_tab(p_copy_from_index) is NOT NULL then
2155 	               g_burdenable_raw_cost_tab(l_new_rec_index):= 0;
2156                END IF;
2157 
2158                IF g_debug = 'Y' THEN
2159                  gms_error_pkg.gms_debug (g_error_procedure_name||':'||'After call to CREATE_APVAR_RECORD for amount varaince','C');
2160                END IF;
2161 
2162        END IF; --IF NVL(g_tab_ap_amount_variance(p_copy_from_index),0) <> 0 THEN
2163 
2164 
2165        IF NVL(g_quantity_variance_tab(p_copy_from_index),0) <> 0 THEN
2166 
2167                IF g_debug = 'Y' THEN
2168                  gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Calling CREATE_APVAR_RECORD for qty varaince','C');
2169                END IF;
2170 
2171                l_new_rec_index := l_new_rec_index + 1;
2172 
2173 	       IF g_quantity_variance_tab(p_copy_from_index) <0 THEN
2174 		       l_entered_dr := 0;
2175 		       l_entered_cr := ABS(g_quantity_variance_tab(p_copy_from_index));
2176                ELSE
2177 		       l_entered_dr := ABS(g_quantity_variance_tab(p_copy_from_index));
2178 		       l_entered_cr := 0;
2179 	       END IF;
2180 
2181 	       COPY_AP_RECORD   (p_copy_from_index    => p_copy_from_index,
2182 				  p_new_rec_index     => l_new_rec_index,
2183 				  p_document_type     => 'AP',
2184 				  p_po_vendor_id      => NULL,
2185 				  p_po_ind_com_set_id => NULL,
2186 				  p_entered_dr        => l_entered_dr,
2187 				  p_entered_cr        => l_entered_cr);
2188 
2189                -- Bug 5369296
2190 	       IF g_parent_reversal_id_tab(p_copy_from_index) is NOT NULL then
2191 	               g_burdenable_raw_cost_tab(l_new_rec_index):= 0;
2192                END IF;
2193 
2194                IF g_debug = 'Y' THEN
2195                  gms_error_pkg.gms_debug (g_error_procedure_name||':'||'After call to CREATE_APVAR_RECORD for qty varaince','C');
2196                END IF;
2197 
2198        END IF; --IF NVL(g_quantity_variance_tab(p_copy_from_index),0) <> 0 THEN
2199 
2200        p_new_rec_index := l_new_rec_index;
2201 
2202        IF g_debug = 'Y' THEN
2203          gms_error_pkg.gms_debug (g_error_procedure_name||':'||'CREATE_BACKING_PO_APVAR_REC - End','C');
2204        END IF;
2205 
2206 EXCEPTION
2207 WHEN OTHERS THEN
2208 
2209        IF g_debug = 'Y' THEN
2210          gms_error_pkg.gms_debug (g_error_procedure_name||':'||'CREATE_BACKING_PO_APVAR_REC - EXception','C');
2211        END IF;
2212 
2213   RAISE;
2214 
2215 END CREATE_BACKING_PO_APVAR_REC;
2216 
2217 -- =====================================================================================================
2218 -- R12 Fundscheck Management uptake: AP/PO/REQ will no longer be saving data before firing fundscheck
2219 -- hence existing logic in misc_gms_insert is modified such that code which needs access
2220 -- to AP/PO/REQ tables uncommited gets fired in this main session procedure and insert/update code
2221 -- gets fired in new autonomous procedures.
2222 --
2223 -- This Function fetches required data from PO/AP/REQ and stores in PLSQL tables.Later fires autonomous
2224 -- procedure to insert into gms_bc_packets and update result codes.This is fired from main GL budgetory
2225 -- control API.
2226 -- for EXP and AP -Interface, insert is through gms_pa_costing_pkg
2227 -- for ENC insert into gms_bc_packets takes place through GMS_FC_SYS Package
2228 -- for Budget Submit/Baseline insert into gms_bc_packets takes place through GMS_BUDGET_BALANCE Package
2229 -- =====================================================================================================
2230 
2231 PROCEDURE copy_gl_pkt_to_gms_pkt (p_application_id      IN NUMBER,
2232                                   p_mode                IN  VARCHAR2 DEFAULT 'C',
2233 				  p_partial_flag        IN  VARCHAR2 DEFAULT 'N',
2234                                   x_return_code         OUT NOCOPY VARCHAR2 ) IS
2235 
2236       -- -----------------------------------------------------------------------------------
2237       -- If the default award is not distributed and the document is sent for funds check
2238       -- the packet should fail with the correct failure result code.
2239       -- ----------------------------------------------------------------------------------
2240       l_dist_award_id           NUMBER;
2241       l_award_dist_option       VARCHAR2 (1);
2242       x_adl_rec                 gms_award_distributions%ROWTYPE;
2243       l_packet_id               gms_bc_packets.packet_id%TYPE;
2244 
2245 
2246       -- IP records are fetched by c_req_po_pkt_rec cursor as IP records will
2247       -- also be stored in po_bc_distributions
2248 
2249      -- Cursor to fetch REQ and PO data from po_bc_distributions
2250      -- This cursor fetches all data which has award_set_id populated
2251      -- Further ADL validations are done at later point in code
2252 
2253      CURSOR c_req_po_pkt_rec  IS
2254          SELECT pobc.ledger_id,
2255                 'Purchasing'  je_source_name,
2256    		DECODE(pobc.distribution_type,
2257 			    'REQUISTION','Requisitions',
2258                             'BLANKET'   ,'Release',
2259         		    'SCHEDULED' ,'Release',
2260 			    'Purchases') je_category_name,
2261                 'E' actual_flag,
2262                 pobc.pa_project_id,
2263                 pobc.pa_task_id,
2264                 pobc.pa_award_id,
2265                 pobc.accounted_amt, -- Bug 5614467
2266                 pobc.event_type_code,
2267                 pobc.main_or_backing_code,
2268                 pobc.pa_exp_type,
2269                 pobc.pa_exp_org_id,
2270                 TRUNC (pobc.pa_exp_item_date),
2271                 DECODE(pobc.distribution_type,'REQUISITION','REQ','PO'),
2272                 pobc.header_id,
2273                 pobc.distribution_id,
2274 		et.expenditure_category,
2275 		et.revenue_category_code,
2276 		pobc.ae_event_id source_event_id,
2277                 NULL,   -- result_code
2278 		NULL, 	-- vendor_id
2279 		NULL,   -- ind_cmp_set_id
2280 		NULL,    -- burdenable_raw_cost
2281 		pobc.reference6, -- For GMSIP
2282 		pobc.reference13 --Vendor id for GMSIP transactions
2283            FROM po_bc_distributions pobc ,
2284                 psa_bc_xla_events_gt xlaevt,
2285 		pa_expenditure_types et,
2286                 gms_project_types gpt,
2287                 pa_projects_all pp
2288           WHERE pobc.ae_event_id = xlaevt.event_id
2289     	    AND pobc.pa_project_id IS NOT NULL
2290             AND pobc.pa_project_id = pp.project_id
2291             AND pp.project_type = gpt.project_type
2292             AND gpt.sponsored_flag = 'Y'
2293 	    AND pobc.pa_exp_type = et.expenditure_type;
2294 
2295 
2296         CURSOR c_awrd_ap_dist IS
2297          SELECT apd.invoice_distribution_id,
2298 /* Commenting for Bug 5645290
2299 	        apd.line_type_lookup_code */
2300 /* Added for Bug 5645290 */
2301                 decode (apd.prepay_distribution_id ,NULL,line_type_lookup_code,'PREPAY')
2302 /* Bug 5645290 - End */
2303            FROM psa_bc_xla_events_gt xlaevt,
2304                 ap_invoice_distributions_all apd,
2305                 gms_project_types gpt,
2306                 pa_projects_all pp,
2307 		ap_invoices_all apinv
2308           WHERE apd.bc_event_id = xlaevt.event_id
2309 	    AND apd.project_id IS NOT NULL
2310             AND  apinv.invoice_id = apd.invoice_id
2311             AND apd.project_id = pp.project_id
2312             AND pp.project_type = gpt.project_type
2313             AND gpt.sponsored_flag = 'Y'
2314 	    AND NVL (apd.pa_addition_flag, 'X' ) <> 'T'
2315             --AND  apinv.invoice_type_lookup_code <> 'EXPENSE REPORT' -- need to check if this check is required for Grants in R12
2316 	    -- R12 : Prepayments mathed to PO will not be fundschecked
2317 	    AND  ((apinv.invoice_type_lookup_code = 'PREPAYMENT'
2318 	           AND apd.po_distribution_id IS NULL )
2319 	           OR apinv.invoice_type_lookup_code <> 'PREPAYMENT')
2320 	    --R12 : Application of Prepayment matched to PO will not be fundschecked
2321 	    AND  ((apd.line_type_lookup_code ='PREPAY' AND
2322 	             apd.po_distribution_id IS NULL) OR
2323                      apd.line_type_lookup_code <> 'PREPAY' )
2324 	 UNION /* BUG 14216205 : Added the union for SAT */
2325 	 SELECT apsat.invoice_distribution_id,
2326 		DECODE (apsat.prepay_distribution_id ,NULL,line_type_lookup_code,'PREPAY')
2327 	   from PSA_BC_XLA_EVENTS_GT XLAEVT,
2328 		AP_SELF_ASSESSED_TAX_DIST_ALL apsat,
2329 		gms_project_types gpt,
2330 		pa_projects_all pp,
2331 		ap_invoices_all apinv
2332 	  WHERE apsat.bc_event_id                 = xlaevt.event_id
2333 	    AND apsat.project_id                   IS NOT NULL
2334 	    AND apinv.invoice_id                  = apsat.invoice_id
2335 	    AND apsat.project_id                    = pp.project_id
2336 	    AND pp.project_type                   = gpt.project_type
2337 	    AND gpt.sponsored_flag                = 'Y'
2338 	    AND NVL (apsat.pa_addition_flag, 'X' ) <> 'T'
2339 	    AND ((apinv.invoice_type_lookup_code  = 'PREPAYMENT'
2340 	    AND apsat.po_distribution_id           IS NULL ) or
2341 	        APINV.INVOICE_TYPE_LOOKUP_CODE    <> 'PREPAYMENT')
2342 	    AND apsat.line_type_lookup_code       ='NONREC_TAX';
2343 
2344         -- R12 Funds Management Uptake : This is the main cursor to fetch records from ap extracts
2345 	-- for all eligible invoice distribution id's. This cursor fetches data for Standard Invoices
2346 	-- and prepayments. Note : For prepayments there will be multiple lines for each invoice
2347 	-- distribution as data is fetched from AP_PREPAY_APP_DISTS.
2348 	-- Note : This cursor fetches only the AP dist record ,it doesnt fetch associated
2349 	-- PO/RELEASE that has to be unreserved.
2350 
2351         /* AP's amount calculation logic for all type of invoices
2352 	  (except prepay application) for populating entered amounts
2353 
2354            Its encumbrance_amount column for 'ERV',TERV ,ITEM ,IPV ,MISCELLANEOUS
2355            FREIGHT , NONREC_TAX,TIPV,TRV,FREIGHT,backing PO
2356 
2357            And ENCUMBRANCE_AMOUNT =NVL(AID.amount,0) - NVL(AID.amount_variance,0) -
2358            NVL(AID.quantity_variance,0)
2359 
2360            For amount variance extract column = AID_AMOUNT_VARIANCE
2361            For qty variance extract column = AID_QUANTITY_VARIANCE*/
2362 
2363 	CURSOR cur_ap_bc_dist (p_stdinvoice_exists VARCHAR2,
2364 	                       p_prepay_exists     VARCHAR2) IS
2365 	 SELECT /*+ leading(PBXEG, APEXT.AID ) USE_NL(APEXT.AID) cardinality(PBXEG,1) */  /* 9694907 */
2366                 'Payables'                                                                je_source_name,
2367    		'Purchase Invoices'                                                       je_category_name,
2368                 'E'                                                                       actual_flag,
2369                 apext.aid_project_id                                                      project_id,
2370                 apext.aid_task_id                                                         task_id,
2371                 apext.aid_award_id                                                          award_id,
2372 	        DECODE(SIGN(apext.ENCUMBRANCE_BASE_AMOUNT),-1,0,apext.ENCUMBRANCE_BASE_AMOUNT)      entered_dr, -- Bug 5614467
2373 		DECODE(SIGN(apext.ENCUMBRANCE_BASE_AMOUNT),-1,ABS(apext.ENCUMBRANCE_BASE_AMOUNT),0) entered_cr, -- Bug 5231395 -- Bug 5614467
2374 	        DECODE(SIGN(apext.ENCUMBRANCE_AMOUNT),-1,0,apext.ENCUMBRANCE_AMOUNT)      txn_dr, -- Bug 5614467
2375 		DECODE(SIGN(apext.ENCUMBRANCE_AMOUNT),-1,ABS(apext.ENCUMBRANCE_AMOUNT),0) txn_cr, -- Bug 5614467
2376                 apext.aid_expenditure_type                                                expenditure_type,
2377                 apext.aid_expenditure_org_id                                              org_id,
2378                 NULL                                                                      expenditure_item_date,--populated in later code
2379                 'AP'                                                                      document_type,
2380                 apext.bus_flow_inv_id                                                     invoice_id,
2381                 apext.aid_invoice_dist_id                                                 invoice_distribution_id,
2382 		NULL                                                                      prepay_source_inv_id,
2383                 apext.event_id                                                            source_event_id,
2384                 NULL                                                                      result_code,
2385 		NULL                                                                      vendor_id,
2386 		NULL                                                                      ind_cmp_set_id,
2387 		NULL                                                                      burdenable_raw_cost,
2388  	        apext.aid_base_quantity_variance                                          ap_quantity_variance, -- Bug 5614467
2389 		apext.aid_base_amount_variance                                            ap_amount_variance, -- Bug 5614467
2390 		/* Bug 5344693 : In the scenario where an Invoice is matched to a PO with variance , the ap_po_distribution_id,
2391 		   ap_po_header_id and ap_po_release_id for the variance distribution should be NULL. */
2392                 DECODE(apext.AID_LINE_TYPE_LOOKUP_CODE,'ITEM',apext.po_distribution_id
2393                                                       ,'ACCRUAL',apext.po_distribution_id
2394                                                       ,'NONREC_TAX',apext.po_distribution_id
2395                                                       ,NULL)                              ap_po_distribution_id,
2396                 DECODE(apext.AID_LINE_TYPE_LOOKUP_CODE,'ITEM',DECODE(apext.po_distribution_id,NULL,NULL,apext.bus_flow_po_doc_id)
2397                                                       ,'ACCRUAL',DECODE(apext.po_distribution_id,NULL,NULL,apext.bus_flow_po_doc_id)
2398                                                       ,'NONREC_TAX',DECODE(apext.po_distribution_id,NULL,NULL,apext.bus_flow_po_doc_id)
2399                                                       , NULL )                            ap_po_header_id,
2400                 DECODE(apext.AID_LINE_TYPE_LOOKUP_CODE,'ITEM',  DECODE(apext.bus_flow_po_dist_type,'RELEASE',apext.bus_flow_po_doc_id,NULL)
2401                                                       ,'ACCRUAL',DECODE(apext.bus_flow_po_dist_type,'RELEASE',apext.bus_flow_po_doc_id,NULL)
2402                                                       ,'NONREC_TAX',DECODE(apext.bus_flow_po_dist_type,'RELEASE',apext.bus_flow_po_doc_id,NULL)
2403                                                       ,NULL)                              ap_po_release_id,
2404 		-- Below columns will be populated later in code
2405 		NULL                                            			  set_of_books_id,
2406                 NULL                                                                      exp_category,
2407                 NULL                                         				  revenue_category,
2408                 NULL                                       				  doc_dist_line_num,
2409 		NULL                                         				  invoice_type_code,
2410                 NULL                                                            	  inv_source,
2411                 NULL                                      				  inv_dist_reference_1,
2412                 NULL                                				          inv_dist_reference_2,
2413                 NULL                                                                      ap_prepay_app_dist_id
2414           FROM
2415 		(    SELECT  AID.bc_event_id                           event_id
2416            ,AID.accounting_date                       aid_accounting_date
2417            ,AID.award_id                              aid_award_id
2418            ,AID.dist_code_combination_id              aid_dist_ccid
2419            ,AID.expenditure_type                      aid_expenditure_type
2420            ,AID.period_name                           aid_period_name
2421            ,AID.line_type_lookup_code                 aid_line_type_lookup_code
2422            ,AID.invoice_line_number                   aid_invoice_line_number
2423            ,AID.invoice_distribution_id               aid_invoice_dist_id
2424            ,AID.parent_reversal_id                    aid_parent_reversal_id
2425            ,AID.po_distribution_id                    po_distribution_id
2426            ,AID.invoice_id                            bus_flow_inv_id
2427            ,Decode (IGC.CC_LINE, null, 'PO_DISTRIBUTIONS_ALL', IGC.CC_DIST_TYPE)   Bus_Flow_PO_Dist_Type
2428            ,Decode (IGC.CC_LINE, null, Decode (pod.po_release_id, NULL, 'PURCHASE_ORDER', 'RELEASE') , IGC.CC_ENTITY_CODE)  Bus_Flow_PO_Entity_Code
2429            ,Decode (IGC.CC_LINE, null, Decode (pod.po_release_id, NULL, pod.po_header_id,pod.po_release_id)
2430                                       ,IGC.cc_doc_id)   Bus_Flow_PO_Doc_Id
2431            ,AID.project_id                              aid_project_id
2432            ,AID.task_id                                 aid_task_id
2433            ,AID.Expenditure_Organization_ID             aid_expenditure_org_id
2434            ,AID.quantity_variance                       aid_quantity_variance
2435            ,nvl(AID.base_quantity_variance, AID.quantity_variance)           aid_base_quantity_variance
2436            ,AID.amount_variance                                              aid_amount_variance
2437            ,nvl(AID.base_amount_variance, AID.amount_variance)               aid_base_amount_variance
2438 	         ,NVL(AID.base_amount,AID.amount)
2439              - NVL(AID.base_amount_variance,nvl(AID.amount_variance,0))
2440              - NVL(AID.base_quantity_variance,NVL(AID.quantity_variance,0))  encumbrance_base_amount
2441            ,NVL(AID.amount,0)  - NVL(AID.amount_variance,0)
2442                 - NVL(AID.quantity_variance,0)                               encumbrance_amount
2443            ,AID.CHARGE_APPLICABLE_TO_DIST_ID                                 charge_applicable_to_dist_id
2444      FROM   AP_INVOICE_DISTRIBUTIONS_ALL AID,
2445             PO_DISTRIBUTIONS_ALL POD,
2446             FINANCIALS_SYSTEM_PARAMS_ALL FSP,
2447 	          IGC_CC_SLA_BFLOW_PRIOR_V IGC,
2448 	          ap_system_parameters_all ASP
2449      WHERE  nvl(AID.posted_flag,'N') <> 'Y'
2450      AND    AID.po_distribution_id = POD.po_distribution_id(+)
2451      AND    AID.po_distribution_id = IGC.po_distribution_id(+)
2452      AND    AID.ORG_ID = FSP.ORG_ID
2453      AND    aid.set_of_books_id = asp.set_of_books_id
2454      AND    aid.org_id = asp.org_id
2455      		    ) apext, -- Bug 5500126
2456                 PSA_BC_XLA_EVENTS_GT PBXEG
2457          WHERE  apext.aid_invoice_dist_id IN (select Column_Value from Table(g_ap_inv_dist_id))
2458 	   /* AND  apext.event_id in ( SELECT event_id FROM psa_bc_xla_events_gt) */
2459 	      AND   apext.event_id = PBXEG.EVENT_ID
2460             	-- Bug 5238282 : Prepayment application will be treated as standard invoice line for check funds
2461 		-- as there will be no data in ap_prepay_app_dists table.This table is populated during invoice
2462 		-- validation.
2463 /* Commenting the following condition for Bug 5645290
2464   	   AND  (p_mode ='C' OR (apext.aid_line_type_lookup_code <> 'PREPAY' AND p_mode <>'C')) */
2465 /* Adding for Bug 5645290*/
2466            AND  exists (
2467                   select 1
2468                   from ap_invoice_distributions_all apd
2469                   where apd.invoice_distribution_id = apext.aid_invoice_dist_id
2470                   and ((apd.prepay_distribution_id is NULL AND p_mode <>'C') OR p_mode ='C' ))
2471 /* Bug 5645290 - End */
2472 	   AND  p_stdinvoice_exists = 'Y'
2473 	   UNION ALL
2474 	 SELECT /*+ leading(PBXEG, APEXT.ASAT ) USE_NL(APEXT.ASAT) cardinality(PBXEG,1) */  /* 9694907 */
2475                 'Payables'                                            je_source_name,
2476    		'Purchase Invoices'                                                       je_category_name,
2477                 'E'                                                                       actual_flag,
2478                 apext.aid_project_id                                                      project_id,
2479                 apext.aid_task_id                                                         task_id,
2480                 apext.aid_award_id                                                          award_id,
2481 	        DECODE(SIGN(apext.ENCUMBRANCE_BASE_AMOUNT),-1,0,apext.ENCUMBRANCE_BASE_AMOUNT)      entered_dr, -- Bug 5614467
2482 		DECODE(SIGN(apext.ENCUMBRANCE_BASE_AMOUNT),-1,ABS(apext.ENCUMBRANCE_BASE_AMOUNT),0) entered_cr, -- Bug 5231395 -- Bug 5614467
2483 	        DECODE(SIGN(apext.ENCUMBRANCE_AMOUNT),-1,0,apext.ENCUMBRANCE_AMOUNT)      txn_dr, -- Bug 5614467
2484 		DECODE(SIGN(apext.ENCUMBRANCE_AMOUNT),-1,ABS(apext.ENCUMBRANCE_AMOUNT),0) txn_cr, -- Bug 5614467
2485                 apext.aid_expenditure_type                                                expenditure_type,
2486                 apext.aid_expenditure_org_id                                              org_id,
2487                 NULL                                                                      expenditure_item_date,--populated in later code
2488                 'AP'                                                                      document_type,
2489                 apext.bus_flow_inv_id                                                     invoice_id,
2490                 apext.aid_invoice_dist_id                                                 invoice_distribution_id,
2491 		NULL                                                                      prepay_source_inv_id,
2492                 apext.event_id                                                            source_event_id,
2493                 NULL                                                                      result_code,
2494 		NULL                                                                      vendor_id,
2495 		NULL                                                                      ind_cmp_set_id,
2496 		NULL                                                                      burdenable_raw_cost,
2497  	        apext.aid_base_quantity_variance                                          ap_quantity_variance, -- Bug 5614467
2498 		apext.aid_base_amount_variance                                            ap_amount_variance, -- Bug 5614467
2499 		/* Bug 5344693 : In the scenario where an Invoice is matched to a PO with variance , the ap_po_distribution_id,
2500 		   ap_po_header_id and ap_po_release_id for the variance distribution should be NULL. */
2501                 /*DECODE(apext.AID_LINE_TYPE_LOOKUP_CODE,'ITEM',apext.po_distribution_id
2502                                                       ,'ACCRUAL',apext.po_distribution_id
2503                                                       ,'NONREC_TAX',apext.po_distribution_id
2504                                                       ,NULL)                              ap_po_distribution_id,*/
2505 /* MAKING THE PO_DISTRIBUTION_ID TO NULL TO HANDLE THE ISSUE OF MISMATCH OF COMMITMENT AND ASI HEADER COMMITMENT BUG#15970881
2506    The po commitment relieveing logic depends upon the po distribution id.if the EI has PO distribution id stamped on it then the
2507    item should be relieved and it should creates the line in CR header in the gms_bc_packets table*/
2508 		NULL ap_po_distribution_id,
2509                 DECODE(apext.AID_LINE_TYPE_LOOKUP_CODE,'ITEM',DECODE(apext.po_distribution_id,NULL,NULL,apext.bus_flow_po_doc_id)
2510                                                       ,'ACCRUAL',DECODE(apext.po_distribution_id,NULL,NULL,apext.bus_flow_po_doc_id)
2511                                                       ,'NONREC_TAX',DECODE(apext.po_distribution_id,NULL,NULL,apext.bus_flow_po_doc_id)
2512                                                       , NULL )                            ap_po_header_id,
2513                 DECODE(apext.AID_LINE_TYPE_LOOKUP_CODE,'ITEM',  DECODE(apext.bus_flow_po_dist_type,'RELEASE',apext.bus_flow_po_doc_id,NULL)
2514                                                       ,'ACCRUAL',DECODE(apext.bus_flow_po_dist_type,'RELEASE',apext.bus_flow_po_doc_id,NULL)
2515                                                       ,'NONREC_TAX',DECODE(apext.bus_flow_po_dist_type,'RELEASE',apext.bus_flow_po_doc_id,NULL)
2516                                                       ,NULL)                              ap_po_release_id,
2517 		-- Below columns will be populated later in code
2518 		NULL                                            			  set_of_books_id,
2519                 NULL                                                                      exp_category,
2520                 NULL                                         				  revenue_category,
2521                 NULL                                       				  doc_dist_line_num,
2522 		NULL                                         				  invoice_type_code,
2523                 NULL                                                            	  inv_source,
2524                 NULL                                      				  inv_dist_reference_1,
2525                 NULL                                				          inv_dist_reference_2,
2526                 NULL                                                                      ap_prepay_app_dist_id
2527           FROM  (
2528                         SELECT  ASAT.bc_event_id event_id
2529                ,ASAT.accounting_date aid_accounting_date
2530                ,ASAT.award_id aid_award_id
2531                ,ASAT.dist_code_combination_id aid_dist_ccid
2532                ,ASAT.expenditure_type aid_expenditure_type
2533                ,ASAT.period_name aid_period_name
2534                ,ASAT.line_type_lookup_code aid_line_type_lookup_code
2535                ,ASAT.invoice_line_number aid_invoice_line_number
2536                ,ASAT.invoice_distribution_id aid_invoice_dist_id
2537                ,ASAT.parent_reversal_id aid_parent_reversal_id
2538                ,ASAT.po_distribution_id po_distribution_id
2539                ,ASAT.invoice_id bus_flow_inv_id
2540                ,Decode (IGC.CC_LINE, null, 'PO_DISTRIBUTIONS_ALL', IGC.CC_DIST_TYPE)  Bus_Flow_PO_Dist_Type
2541                ,Decode (IGC.CC_LINE, null, Decode (pod.po_release_id, NULL, 'PURCHASE_ORDER', 'RELEASE'), IGC.CC_ENTITY_CODE)  Bus_Flow_PO_Entity_Code
2542                ,Decode (IGC.CC_LINE, null, Decode (pod.po_release_id, NULL, pod.po_header_id,pod.po_release_id),IGC.cc_doc_id)  Bus_Flow_PO_Doc_Id
2543 	             ,ASAT.project_id aid_project_id
2544                ,ASAT.task_id aid_task_id
2545                ,ASAT.Expenditure_Organization_ID aid_expenditure_org_id
2546                ,ASAT.quantity_variance aid_quantity_variance
2547                ,nvl(ASAT.base_quantity_variance, ASAT.quantity_variance) aid_base_quantity_variance
2548                ,ASAT.amount_variance aid_amount_variance
2549                ,nvl(ASAT.base_amount_variance, ASAT.amount_variance)  aid_base_amount_variance
2550 	             ,NVL(ASAT.base_amount,ASAT.amount)
2551                 - NVL(ASAT.base_amount_variance,nvl(ASAT.amount_variance,0))
2552                 - NVL(ASAT.base_quantity_variance,NVL(ASAT.quantity_variance,0)) encumbrance_base_amount
2553                ,NVL(ASAT.amount,0)  - NVL(ASAT.amount_variance,0)
2554                 - NVL(ASAT.quantity_variance,0) encumbrance_amount
2555                ,ASAT.CHARGE_APPLICABLE_TO_DIST_ID charge_applicable_to_dist_id
2556           FROM  AP_SELF_ASSESSED_TAX_DIST_ALL ASAT,
2557                 PO_DISTRIBUTIONS_ALL  POD,
2558 	            	IGC_CC_SLA_BFLOW_PRIOR_V IGC
2559          WHERE  nvl(ASAT.posted_flag,'N') <> 'Y'
2560          AND    ASAT.po_distribution_id = POD.po_distribution_id(+)
2561          AND	  ASAT.po_distribution_id = IGC.po_distribution_id(+)
2562      			) apext, -- Bug 5500126
2563                 PSA_BC_XLA_EVENTS_GT PBXEG
2564          WHERE  apext.aid_invoice_dist_id IN (select Column_Value from Table(g_ap_inv_dist_id))
2565 	   /* AND  apext.event_id in ( SELECT event_id FROM psa_bc_xla_events_gt) */
2566 	      AND   apext.event_id = PBXEG.EVENT_ID
2567             	-- Bug 5238282 : Prepayment application will be treated as standard invoice line for check funds
2568 		-- as there will be no data in ap_prepay_app_dists table.This table is populated during invoice
2569 		-- validation.
2570 /* Commenting the following condition for Bug 5645290
2571   	   AND  (p_mode ='C' OR (apext.aid_line_type_lookup_code <> 'PREPAY' AND p_mode <>'C')) */
2572 /* Adding for Bug 5645290*/
2573            AND  (exists (
2574                   select 1
2575                   from ap_invoice_distributions_all apd
2576                   where apd.invoice_distribution_id = apext.aid_invoice_dist_id
2577                   and ((APD.PREPAY_DISTRIBUTION_ID is null and P_MODE <>'C') or P_MODE ='C' ))
2578            OR     exists ( /* BUG 14216205 : Added the following Code for SAT : Starts */
2579                   select 1
2580                   from AP_SELF_ASSESSED_TAX_DIST_ALL APSAT
2581                   where APSAT.INVOICE_DISTRIBUTION_ID = APEXT.AID_INVOICE_DIST_ID
2582                   and ((APSAT.PREPAY_DISTRIBUTION_ID is null and P_MODE <>'C') or P_MODE ='C' ))
2583                   ) /* BUG 14216205 : Added the following Code for SAT : Ends */
2584 /* Bug 5645290 - End */
2585 	   AND  p_stdinvoice_exists = 'Y'
2586       UNION ALL
2587         SELECT  /*+ leading(PSA_BC_XLA_EVENTS_GT)  cardinality(PSA_BC_XLA_EVENTS_GT,1) */ /* 9694907 */
2588                 'Payables'                                                                je_source_name,
2589    		'Purchase Invoices'                                                       je_category_name,
2590                 'E'                                                                       actual_flag,
2591                 AID.project_id                                                            project_id,
2592                 AID.task_id                                                               task_id,
2593                 AID.award_id                                                              award_id,
2594 	        DECODE(SIGN(APAD.BASE_AMOUNT),-1,0,APAD.BASE_AMOUNT)                      entered_dr, -- Bug 5614467
2595 		DECODE(SIGN(APAD.BASE_AMOUNT),-1,ABS(APAD.BASE_AMOUNT),0)                 entered_cr, -- Bug 5231395 -- Bug 5614467
2596 	        DECODE(SIGN(APAD.AMOUNT),-1,0,APAD.AMOUNT)                                txn_dr, -- Bug 5614467
2597 		DECODE(SIGN(APAD.AMOUNT),-1,ABS(APAD.AMOUNT),0)                           txn_cr, -- Bug 5614467
2598                 AID.expenditure_type                                                      expenditure_type,
2599                 AID.expenditure_organization_id                                           org_id,
2600                 NULL                                                                      expenditure_item_date, --populated later in code
2601                 'AP'                                                                      document_type,
2602                 AID.invoice_id                                                            invoice_id,
2603                 APAD.Prepay_App_Distribution_ID                                           invoice_distribution_id,
2604 		AID.invoice_distribution_id                                               prepay_source_inv_id,
2605                 APPH.bc_event_id                                                          source_event_id,
2606                 NULL                                                                      result_code,
2607 		NULL                                                                      vendor_id,
2608 		NULL                                                                      ind_cmp_set_id,
2609 		NULL                                                                      burdenable_raw_cost,
2610  	        NULL                                                                      ap_quantity_variance,
2611 		NULL                                                                      ap_amount_variance,
2612 		AID.po_distribution_id                                                    ap_po_distribution_id,
2613                 NULL                                                                      ap_po_header_id,
2614                 NULL                                                                      ap_po_release_id,
2615 		-- Below columns will be populated later in code
2616 		NULL                                            			  set_of_books_id,
2617                 NULL                                                                      exp_category,
2618                 NULL                                         				  revenue_category,
2619                 NULL                                       				  doc_dist_line_num,
2620 		NULL                                         				  invoice_type_code,
2621                 NULL                                                            	  inv_source,
2622                 NULL                                      				  inv_dist_reference_1,
2623                 NULL                                				          inv_dist_reference_2,
2624                 APAD.prepay_app_dist_id                                                   ap_prepay_app_dist_id
2625                  -- Last col. will be used in Synch_gms_gl_packets ...
2626               FROM AP_PREPAY_HISTORY_ALL APPH,
2627                    PSA_BC_XLA_EVENTS_GT PBXEG,
2628                    AP_PREPAY_APP_DISTS APAD,
2629                    AP_INVOICE_LINES_ALL AIL,
2630                    AP_INVOICE_DISTRIBUTIONS_ALL AID
2631              WHERE AID.bc_event_id = APPH.bc_Event_id
2632                AND APPH.prepay_history_id = APAD.prepay_history_id
2633                AND AID.invoice_line_number = AIL.line_number
2634                AND AID.invoice_id = AIL.invoice_id
2635                AND AID.line_type_lookup_code IN ( 'PREPAY' ,'NONREC_TAX' ) --Bug 5490378
2636                /* and APPH.bc_Event_id IN ( SELECT event_id FROM psa_bc_xla_events_gt) */
2637                AND APPH.bc_Event_id = PBXEG.event_id
2638                and AID.invoice_distribution_id IN (select Column_Value from Table(g_ap_inv_dist_id))
2639                AND p_prepay_exists = 'Y'
2640                and aid.invoice_distribution_id = apad.prepay_app_distribution_id
2641 	       and APAD.PREPAY_DIST_LOOKUP_CODE <> 'AWT';
2642 
2643      CURSOR c_req_adl_details (p_req_dist_id NUMBER) IS
2644        SELECT   adl.award_id,
2645                 DECODE (
2646                    adl.award_id,
2647                    l_dist_award_id, 'F21',
2648                    DECODE (adl.award_id, NULL, 'F62', NULL)), --Bug Fix 1599750(2)-- RESULT CODE for missing ADLS F62
2649 		pov.vendor_id,
2650     	        adl.ind_compiled_set_id
2651            FROM gms_award_distributions adl,
2652 		po_requisition_lines_all porl,
2653                 po_req_distributions_all pord,
2654 		po_vendors pov
2655           WHERE pord.distribution_id = p_req_dist_id
2656             AND pord.project_id IS NOT NULL
2657             AND NVL (pord.award_id, l_dist_award_id) = adl.award_set_id
2658 	    AND pord.requisition_line_id = porl.requisition_line_id
2659 	    AND pord.distribution_id = NVL (adl.distribution_id, pord.distribution_id)
2660             AND pord.project_id = NVL (adl.project_id, pord.project_id)
2661             AND pord.task_id = NVL (adl.task_id, pord.task_id)
2662 	    AND porl.suggested_vendor_name = pov.vendor_name (+)
2663             AND NVL (adl.adl_status, 'I') = 'A'
2664             AND NVL (adl.document_type, 'REQ') IN ('REQ', 'DST');
2665 
2666 
2667       CURSOR c_po_adl_details (p_po_dist_id NUMBER) IS
2668          SELECT adl.award_id,
2669                 DECODE (
2670                    adl.award_id,
2671                    l_dist_award_id, 'F21',
2672                    NULL, 'F62',
2673 		   decode(pll.accrue_on_receipt_flag, 'Y', 'F07',NULL)),
2674 		poh.vendor_id,
2675 	        adl.ind_compiled_set_id,
2676 		pod.rate -- Bug 5614467
2677            FROM po_distributions_all pod,
2678 		po_headers_all poh,
2679 		po_lines_all   pol, --BUG 3022249
2680 		po_line_locations_all pll, -- BUG 3022249
2681                 gms_award_distributions adl
2682           WHERE pod.po_distribution_id = p_po_dist_id
2683             AND pod.project_id IS NOT NULL
2684 	    AND pod.po_header_id = poh.po_header_id
2685 	    and pol.po_header_id = poh.po_header_id
2686 	    and pol.po_line_id   = pod.po_line_id
2687 	    and pll.line_location_id = pod.line_location_id
2688 	    and pll.po_line_id       = pol.po_line_id
2689             AND NVL (pod.award_id, l_dist_award_id) = adl.award_set_id
2690             AND pod.po_distribution_id = NVL (adl.po_distribution_id, pod.po_distribution_id)
2691             AND pod.project_id = NVL (adl.project_id, pod.project_id)
2692             AND pod.task_id = NVL (adl.task_id, pod.task_id)
2693             AND NVL (adl.adl_status, 'I') = 'A'	 			  	   -- Bug 2092791
2694             AND NVL (adl.document_type, 'PO') IN ('PO', 'DST');
2695 
2696       -- Bug 5231395
2697       -- Cursor to fetch AP data
2698       CURSOR c_ap_adl_details (p_ap_dist_id  NUMBER) IS
2699          SELECT apd.set_of_books_id,
2700                 apd.expenditure_item_date,
2701 		et.expenditure_category,
2702 		et.revenue_category_code,
2703 		apd.distribution_line_number,
2704 		api.invoice_type_lookup_code,
2705                 api.source,
2706 		apd.reference_1,        --expenditure_item_id for ER imported to Payables from projects
2707 		apd.reference_2,	--cdl_line_num for ER imported to Payables from projects
2708 	        adl.award_id,
2709                 DECODE (
2710                    adl.award_id,
2711                    l_dist_award_id, 'F21',
2712                    DECODE (adl.award_id, NULL, 'F62', NULL)), --Bug Fix 1599750(2)-- RESULT CODE for missing ADLS F62
2713 		api.vendor_id,
2714  	        adl.ind_compiled_set_id ,                     --Bug 2456878
2715 		/* Bug 5519731 : The following code is modified such that for a reversing invoice distribution if the parent invoice
2716 		   distribution is interfaced to Grants then calculate the BRC else the burdenable raw cost for the reversing
2717 		   distribution is the negative of the BRC for the parent distribution. */
2718 		( SELECT decode(ap1.pa_addition_flag,'Y',NULL,-1 * nvl(adl1.burdenable_raw_cost,0)) --R12 AP Lines Uptake enhancement : Forward porting bug 4450291
2719 		                                             -- Reversing AP distributions should copy the BRC from reversed Distribution
2720 		    FROM gms_award_distributions adl1,
2721 		         ap_invoice_distributions ap1
2722                    WHERE adl1.document_type = 'AP'
2723 		     AND adl1.adl_status = 'A' -- Bug 5654186
2724 		     AND adl1.fc_status = 'A'  -- Bug 5654186
2725 		     AND ap1.invoice_id = apd.invoice_id
2726 		     AND ap1.invoice_distribution_id = apd.parent_reversal_id
2727 		     AND apd.reversal_flag = 'Y'
2728 		     AND ap1.reversal_flag ='Y'
2729 		     AND apd.parent_reversal_id IS NOT NULL
2730 		     AND adl1.award_set_id = ap1.award_id
2731 		     AND adl1.adl_line_num =1 ) burdenable_raw_cost ,
2732                 apd.parent_reversal_id  parent_reversal_id  -- Bug 5369296
2733            FROM ap_invoice_distributions_all apd,
2734 		ap_invoices_all api,
2735                 gms_award_distributions adl,
2736    	        pa_expenditure_types et
2737           WHERE apd.invoice_distribution_id = p_ap_dist_id
2738             AND apd.project_id IS NOT NULL
2739             AND (NVL (apd.pa_addition_flag, 'X') <> 'T')
2740             AND NVL (apd.award_id, l_dist_award_id) = adl.award_set_id
2741             AND apd.invoice_id = NVL (adl.invoice_id, apd.invoice_id)
2742             AND apd.distribution_line_number =
2743                                      NVL (adl.distribution_line_number, apd.distribution_line_number)
2744             AND apd.invoice_distribution_id =
2745                                        NVL (adl.invoice_distribution_id, apd.invoice_distribution_id)
2746             AND apd.project_id = NVL (adl.project_id, apd.project_id)
2747             AND apd.task_id = NVL (adl.task_id, apd.task_id)
2748       	    AND apd.invoice_id = api.invoice_id
2749             AND NVL (adl.adl_status, 'I') = 'A'	 			  	   -- Bug 2092791
2750             AND NVL (adl.document_type, 'AP') IN ('AP', 'DST')
2751            -- AND NVL (adl.fc_status, 'X') <> 'A' --bug8771286
2752 	    AND apd.expenditure_type = et.expenditure_type
2753 	  union /* BUG 14216205 : Added the union for SAT */
2754 	 SELECT apsat.set_of_books_id,
2755 		apsat.expenditure_item_date,
2756 		et.expenditure_category,
2757 		et.revenue_category_code,
2758 		apsat.distribution_line_number,
2759 		api.invoice_type_lookup_code,
2760 		api.source,
2761 		apsat.reference_1,
2762 		apsat.reference_2,
2763 		ADL.AWARD_ID,
2764 		DECODE ( adl.award_id,l_dist_award_id, 'F21', DECODE (adl.award_id, NULL, 'F62', NULL)),
2765 		api.vendor_id,
2766 		ADL.IND_COMPILED_SET_ID ,
2767 		(SELECT DECODE(apt.pa_addition_flag,'Y',NULL,-1 * NVL(adl1.burdenable_raw_cost,0))
2768 		from GMS_AWARD_DISTRIBUTIONS ADL1,
2769 			AP_SELF_ASSESSED_TAX_DIST apt
2770 		WHERE adl1.document_type        = 'AP'
2771 		AND adl1.adl_status             = 'A'
2772 		and ADL1.FC_STATUS              = 'A'
2773 		AND apt.invoice_id              = apsat.invoice_id
2774 		AND apt.invoice_distribution_id = apsat.parent_reversal_id
2775 		and APSAT.REVERSAL_FLAG           = 'Y'
2776 		AND apt.reversal_flag           ='Y'
2777 		and APSAT.PARENT_REVERSAL_ID     is not null
2778 		AND adl1.award_set_id           = apt.award_id
2779 		AND adl1.adl_line_num           =1 ) burdenable_raw_cost ,
2780 		apsat.PARENT_REVERSAL_ID PARENT_REVERSAL_ID
2781 	 FROM AP_SELF_ASSESSED_TAX_DIST_ALL apsat,
2782 		ap_invoices_all api,
2783 		gms_award_distributions adl,
2784 		PA_EXPENDITURE_TYPES ET
2785 	 WHERE apsat.invoice_distribution_id       = p_ap_dist_id
2786 	 AND apsat.project_id                     IS NOT NULL
2787 	 and (NVL (APSAT.PA_ADDITION_FLAG, 'X')   <> 'T')
2788 	 AND NVL (apsat.award_id, l_dist_award_id) = adl.award_set_id
2789 	 AND apsat.invoice_id                      = NVL (adl.invoice_id, apsat.invoice_id)
2790 	 AND apsat.distribution_line_number        = NVL (adl.distribution_line_number, apsat.distribution_line_number)
2791 	 AND apsat.invoice_distribution_id         = NVL (adl.invoice_distribution_id, apsat.invoice_distribution_id)
2792 	 AND apsat.project_id                      = NVL (adl.project_id, apsat.project_id)
2793 	 AND apsat.task_id                         = NVL (adl.task_id, apsat.task_id)
2794 	 AND apsat.invoice_id                      = api.invoice_id
2795 	 AND NVL (adl.adl_status, 'I')           = 'A'
2796 	 and NVL (ADL.DOCUMENT_TYPE, 'AP')      in ('AP', 'DST')
2797 	 AND apsat.expenditure_type                = et.expenditure_type;
2798 
2799 
2800       -- Local variables
2801       l_document_type             gl_bc_packets.source_distribution_type%TYPE;
2802       l_gl_bc_pkt_spon_count      NUMBER;
2803       L_RETURN_CODE               VARCHAR2(1);
2804       l_mode                      VARCHAR2(1);
2805       l_prepay_exists             VARCHAR2(1);
2806       l_stdinvoice_exists         VARCHAR2(1);
2807       l_counter                   NUMBER;
2808       l_debug_start_counter       NUMBER;
2809       l_po_award_id               po_distributions_all.award_id%TYPE;
2810       l_po_result_code            gms_bc_packets.result_code%TYPE;
2811       l_po_vendor_id              po_headers_all.vendor_id%TYPE;
2812       l_po_ind_com_set_id         gms_award_distributions.ind_compiled_set_id%TYPE;
2813 
2814   -- Procedure to intialize PLSQL type variables
2815   PROCEDURE Intialize_tabs IS
2816   BEGIN
2817 	  g_set_of_books_id_tab.delete;
2818 	  g_je_source_name_tab.delete;
2819 	  g_je_category_name_tab.delete;
2820 	  g_actual_flag_tab.delete;
2821 	  g_project_id_tab.delete;
2822 	  g_task_id_tab.delete;
2823 	  g_award_id_tab.delete;
2824 	  g_result_code_tab.delete;
2825 	  g_entered_dr_tab.delete;
2826 	  g_entered_cr_tab.delete;
2827 	  g_txn_dr_tab.delete; -- Bug 5614467
2828 	  g_txn_cr_tab.delete; -- Bug 5614467
2829 	  g_po_rate_tab.delete; -- Bug 5614467
2830 	  g_etype_tab.delete;
2831 	  g_exp_org_id_tab.delete;
2832 	  g_exp_item_date_tab.delete;
2833 	  g_document_type_tab.delete;
2834 	  g_doc_header_id_tab.delete;
2835 	  g_doc_dist_id_tab.delete;
2836 	  g_vendor_id_tab.delete;
2837 	  g_exp_category_tab.delete;
2838 	  g_revenue_category_tab.delete;
2839 	  g_ind_cmp_set_id_tab.delete;
2840 	  g_burdenable_raw_cost_tab.delete; --R12 AP Lines Uptake enhancement : Forward porting bug 4450291
2841 	  g_parent_reversal_id_tab.delete; -- Bug 5369296
2842 	  g_doc_dist_line_num_tab.delete;
2843           g_invoice_type_code_tab.delete;
2844           g_inv_source_tab.delete;
2845           g_inv_dist_reference_1_tab.delete;
2846           g_inv_dist_reference_2_tab.delete;
2847 	  g_source_event_id_tab.delete;
2848           g_entered_amount_tab.delete;
2849           g_event_type_code_tab.delete;
2850           g_main_or_backing_tab.delete;
2851 	  g_ap_line_type_lkup.delete;
2852           g_prepay_std_inv_dist_id.delete;
2853           g_quantity_variance_tab.delete;
2854           g_amount_variance_tab.delete;
2855           g_po_distribution_id_tab.delete;
2856 	  g_po_header_id_tab.delete;
2857 	  g_po_release_id_tab.delete;
2858    END;
2859 
2860 PROCEDURE DERIVE_DR_CR IS
2861 BEGIN
2862 
2863    IF g_debug = 'Y' THEN
2864       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Start of DERIVE_DR_CR ','C');
2865    END IF;
2866 
2867    FOR l_index IN 1..g_set_of_books_id_tab.Last LOOP
2868 
2869      g_entered_dr_tab(l_index)   := 0;
2870      g_entered_cr_tab(l_index)   := 0 ;
2871 
2872      IF g_event_type_code_tab(l_index) IN ( 'PO_PA_RESERVED' ,
2873                                             'PO_PA_CR_MEMO_CANCELLED',
2874                                             'RELEASE_REOPEN_FINAL_CLOSED',
2875                                             'RELEASE_CR_MEMO_CANCELLED',
2876                                             'RELEASE_RESERVED',
2877                                             'REQ_RESERVED',
2878                                             'PO_REOPEN_FINAL_MATCH',
2879                                             -- g_tab_entered_amount and g_tab_accted_amount will be negative for below events
2880                                             'PO_PA_CANCELLED',
2881                                             'RELEASE_CANCELLED',
2882                                             'REQ_CANCELLED'
2883                                             ) THEN
2884 
2885        IF g_main_or_backing_tab(l_index) = 'M' THEN
2886           	g_entered_dr_tab(l_index)   := g_entered_amount_tab(l_index);
2887        ELSE
2888           	g_entered_cr_tab(l_index)   := g_entered_amount_tab(l_index);
2889        END IF;
2890 
2891     ELSIF g_event_type_code_tab(l_index) IN ('PO_PA_UNRESERVED' ,
2892                                              'PO_PA_FINAL_CLOSED',
2893                                              'PO_PA_REJECTED',
2894                                              'PO_PA_INV_CANCELLED',
2895                                              'RELEASE_FINAL_CLOSED',
2896                                              'RELEASE_INV_CANCELLED',
2897                                              'RELEASE_REJECTED',
2898                                              'RELEASE_UNRESERVED',
2899                                              'REQ_FINAL_CLOSED',
2900                                              'REQ_REJECTED',
2901                                              'REQ_RETURNED',
2902                                              'REQ_UNRESERVED',
2903                                              'REQ_ADJUSTED') THEN
2904 
2905        IF g_main_or_backing_tab(l_index) = 'M' THEN
2906           	g_entered_cr_tab(l_index)   := g_entered_amount_tab(l_index);
2907        ELSE
2908           	g_entered_dr_tab(l_index)   := g_entered_amount_tab(l_index);
2909        END IF;
2910 
2911     END IF;
2912 
2913     IF g_debug = 'Y' THEN
2914       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'value of  g_main_or_backing_tab '||g_main_or_backing_tab(l_index),'C');
2915       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'value of  g_event_type_code_tab '||g_event_type_code_tab(l_index),'C');
2916       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'value of  g_entered_cr_tab '||g_entered_cr_tab(l_index),'C');
2917       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'value of  g_entered_dr_tab '||g_entered_dr_tab(l_index),'C');
2918     END IF;
2919 
2920 
2921   END LOOP;
2922 
2923    IF g_debug = 'Y' THEN
2924       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'End of DERIVE_DR_CR ','C');
2925    END IF;
2926 
2927 END DERIVE_DR_CR;
2928 
2929 -- Main begin
2930 BEGIN
2931 
2932   g_error_procedure_name := 'COPY_GL_PKT_TO_GMS_PKT';
2933   g_debug                := NVL(FND_PROFILE.value('GMS_ENABLE_DEBUG_MODE'), 'N');
2934   l_mode                 := p_mode;
2935 
2936   -- 'A' mode passed for REQ/PO adjustment scenarios .This should be considered as reserve
2937   IF l_mode ='A' THEN
2938      l_mode := 'R';
2939   END IF;
2940 
2941   IF g_debug = 'Y' THEN
2942     gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Start','C');
2943   END IF;
2944 
2945   -- Initializing OUT variables
2946   x_return_code := 'P';
2947   l_award_dist_option := 'N';
2948 
2949   SELECT gms_bc_packets_s.nextval
2950     INTO l_packet_id
2951     FROM dual;
2952 
2953   IF g_debug = 'Y' THEN
2954       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Calling Intialize_tabs','C');
2955   END IF;
2956 
2957   Intialize_tabs;
2958 
2959   -- For AP/PO/REQ/IP records fire misc_synch_adls
2960   IF p_application_id in (200,201) THEN
2961 
2962                -- Fetch AP/PO/REQ data associted with sponsored Project.No other validations performed
2963 	     IF p_application_id = 200 THEN -- Payables is the Calling application
2964 
2965                 IF g_debug = 'Y' THEN
2966                     gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Inside p_application_id = 200 ','C');
2967                 END IF;
2968 
2969                 OPEN  c_awrd_ap_dist;
2970  	        FETCH c_awrd_ap_dist BULK COLLECT INTO  g_ap_inv_dist_id,g_ap_line_type_lkup;
2971                 CLOSE c_awrd_ap_dist;
2972 
2973                 IF g_debug = 'Y' THEN
2974                     gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Number of AP distribuions fetched ='||g_ap_inv_dist_id.count,'C');
2975                 END IF;
2976 
2977 		IF g_ap_inv_dist_id.count <> 0 THEN
2978 
2979 		   l_prepay_exists := 'N';
2980                    l_stdinvoice_exists := 'N';
2981 
2982                    FOR i in 1..g_ap_line_type_lkup.count LOOP
2983 		     -- Prepayment application will be treated as standard invoice line for check funds
2984 		     -- as there will be no data in ap_prepay_app_dists table.This table is populated during invoice
2985 		     -- validation.
2986 		     IF g_ap_line_type_lkup(i) = 'PREPAY' AND p_mode <> 'C' THEN
2987 		        l_prepay_exists := 'Y';
2988                      ELSE
2989 		        l_stdinvoice_exists := 'Y';
2990 		     END IF;
2991                      -- Exit the loop if both prepay and standard invoices exists.
2992 		     IF l_prepay_exists= 'Y' AND l_stdinvoice_exists = 'Y' THEN
2993 		        EXIT;
2994                      END IF;
2995 
2996 		   END LOOP;
2997 
2998                    IF g_debug = 'Y' THEN
2999                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'For current run there exists PREPAY distribution ? '||l_prepay_exists,'C');
3000                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'For current run there exists Std Invoice distribution ? '||l_stdinvoice_exists,'C');
3001                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Fetching required data from AP extract into plsql tables','C');
3002                    End if;
3003 
3004 		   OPEN  cur_ap_bc_dist(l_stdinvoice_exists,l_prepay_exists);
3005 		   FETCH cur_ap_bc_dist BULK COLLECT INTO
3006 				  g_je_source_name_tab,
3007 				  g_je_category_name_tab,
3008 				  g_actual_flag_tab,
3009 				  g_project_id_tab,
3010 				  g_task_id_tab,
3011 				  g_award_id_tab,
3012 				  g_entered_dr_tab,
3013 				  g_entered_cr_tab,
3014 				  g_txn_dr_tab, -- Bug 5614467
3015 				  g_txn_cr_tab, -- Bug 5614467
3016 				  g_etype_tab,
3017 				  g_exp_org_id_tab,
3018 				  g_exp_item_date_tab,
3019 				  g_document_type_tab,
3020 				  g_doc_header_id_tab,
3021 				  g_doc_dist_id_tab,
3022 				  g_prepay_std_inv_dist_id,
3023 				  g_source_event_id_tab,
3024 			          g_result_code_tab,
3025 		                  g_vendor_id_tab,
3026 				  g_ind_cmp_set_id_tab,
3027 				  g_burdenable_raw_cost_tab,
3028 				  g_quantity_variance_tab,
3029 				  g_amount_variance_tab,
3030                                   g_po_distribution_id_tab,
3031                                   g_po_header_id_tab,
3032 				  g_po_release_id_tab,
3033                			  g_set_of_books_id_tab,
3034 	                          g_exp_category_tab,
3035 				  g_revenue_category_tab,
3036 				  g_doc_dist_line_num_tab,
3037 				  g_invoice_type_code_tab,
3038 				  g_inv_source_tab,
3039 				  g_inv_dist_reference_1_tab,
3040 				  g_inv_dist_reference_2_tab,
3041                                   g_ap_prepay_app_dist_id;
3042                                   -- If you ever use limit, then there will be an issue as g_ap_prepay_app_dist_id
3043                                   -- is used in Synch_gms_gl_packets
3044                    CLOSE cur_ap_bc_dist;
3045 
3046                    IF g_debug = 'Y' AND g_doc_dist_id_tab.count <> 0 THEN
3047 		     FOR i in 1..g_doc_dist_id_tab.count LOOP
3048                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Start-Records fetched from AP extract'||-i,'C');
3049                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_je_source_name_tab ='||g_je_source_name_tab(i),'C');
3050                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_je_category_name_tab ='||g_je_category_name_tab(i),'C');
3051                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_actual_flag_tab ='||g_actual_flag_tab(i),'C');
3052                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_project_id_tab ='||g_project_id_tab(i),'C');
3053                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_task_id_tab ='||g_task_id_tab(i),'C');
3054                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_award_id_tab ='||g_award_id_tab(i),'C');
3055                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_entered_dr_tab ='||g_entered_dr_tab(i),'C');
3056                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_entered_cr_tab ='||g_entered_cr_tab(i),'C');
3057                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_etype_tab ='||g_etype_tab(i),'C');
3058                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_exp_org_id_tab ='||g_exp_org_id_tab(i),'C');
3059                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_exp_item_date_tab ='||g_exp_item_date_tab(i),'C');
3060                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_document_type_tab ='||g_document_type_tab(i),'C');
3061                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_doc_header_id_tab ='||g_doc_header_id_tab(i),'C');
3062                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_doc_dist_id_tab ='||g_doc_dist_id_tab(i),'C');
3063                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_prepay_std_inv_dist_id ='||g_prepay_std_inv_dist_id(i),'C');
3064                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_source_event_id_tab ='||g_source_event_id_tab(i),'C');
3065                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_result_code_tab ='||g_result_code_tab(i),'C');
3066                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_vendor_id_tab ='||g_vendor_id_tab(i),'C');
3067                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_ind_cmp_set_id_tab ='||g_ind_cmp_set_id_tab(i),'C');
3068                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_burdenable_raw_cost_tab ='||g_burdenable_raw_cost_tab(i),'C');
3069                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_quantity_variance_tab ='||g_quantity_variance_tab(i),'C');
3070                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_amount_variance_tab ='||g_amount_variance_tab(i),'C');
3071                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_po_distribution_id_tab ='||g_po_distribution_id_tab(i),'C');
3072                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'End-Records fetched from AP extract'||-i,'C');
3073                      END LOOP;
3074                    End if;
3075 
3076 		END IF;--IF g_ap_inv_dist_id.count <> 0 THEN
3077 
3078 	     ELSIF p_application_id = 201 THEN  -- Purchasing is the Calling application
3079 
3080                    IF g_debug = 'Y' THEN
3081                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Inside p_application_id = 201 ','C');
3082                    END IF;
3083 
3084 		   OPEN c_REQ_PO_pkt_rec;
3085 		   FETCH c_REQ_PO_pkt_rec BULK COLLECT INTO
3086 				  g_set_of_books_id_tab,
3087 				  g_je_source_name_tab,
3088 				  g_je_category_name_tab,
3089 				  g_actual_flag_tab,
3090 				  g_project_id_tab,
3091 				  g_task_id_tab,
3092 				  g_award_id_tab,
3093                                   g_entered_amount_tab,
3094                                   g_event_type_code_tab,
3095                                   g_main_or_backing_tab,
3096 				  g_etype_tab,
3097 				  g_exp_org_id_tab,
3098 				  g_exp_item_date_tab,
3099 				  g_document_type_tab,
3100 				  g_doc_header_id_tab,
3101 				  g_doc_dist_id_tab,
3102 				  g_exp_category_tab,
3103 				  g_revenue_category_tab,
3104 				  g_source_event_id_tab,
3105 			          g_result_code_tab,
3106 		                  g_vendor_id_tab,
3107 				  g_ind_cmp_set_id_tab,
3108 				  g_burdenable_raw_cost_tab,
3109 				  g_reference6_tab,
3110 				  g_reference13_tab;
3111 		   CLOSE c_REQ_PO_pkt_rec;
3112 
3113                    IF g_doc_dist_id_tab.count <> 0 THEN
3114                          IF g_debug = 'Y' THEN
3115                               gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Calling DERIVE_DR_CR ','C');
3116                          END IF;
3117                          DERIVE_DR_CR;
3118                    END IF;
3119 
3120                    IF g_debug = 'Y' AND g_doc_dist_id_tab.count <> 0 THEN
3121 		     FOR i in 1..g_doc_dist_id_tab.count LOOP
3122                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Start-Records fetched from PO GT table'||-i,'C');
3123                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_set_of_books_id_tab ='||g_set_of_books_id_tab(i),'C');
3124                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_je_source_name_tab ='||g_je_source_name_tab(i),'C');
3125                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_je_category_name_tab ='||g_je_category_name_tab(i),'C');
3126                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_actual_flag_tab ='||g_actual_flag_tab(i),'C');
3127                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_project_id_tab ='||g_project_id_tab(i),'C');
3128                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_task_id_tab ='||g_task_id_tab(i),'C');
3129                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_award_id_tab ='||g_award_id_tab(i),'C');
3130                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_entered_amount_tab ='||g_entered_amount_tab(i),'C');
3131                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_entered_dr_tab ='||g_entered_dr_tab(i),'C');
3132                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_entered_cr_tab ='||g_entered_cr_tab(i),'C');
3133 		      gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_event_type_code_tab ='||g_event_type_code_tab(i),'C');
3134                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_main_or_backing_tab ='||g_main_or_backing_tab(i),'C');
3135                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_etype_tab ='||g_etype_tab(i),'C');
3136                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_exp_org_id_tab ='||g_exp_org_id_tab(i),'C');
3137                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_exp_item_date_tab ='||g_exp_item_date_tab(i),'C');
3138                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_document_type_tab ='||g_document_type_tab(i),'C');
3139                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_doc_header_id_tab ='||g_doc_header_id_tab(i),'C');
3140                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_doc_dist_id_tab ='||g_doc_dist_id_tab(i),'C');
3141                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_exp_category_tab ='||g_exp_category_tab(i),'C');
3142                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_revenue_category_tab ='||g_revenue_category_tab(i),'C');
3143                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_source_event_id_tab ='||g_source_event_id_tab(i),'C');
3144                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_result_code_tab ='||g_result_code_tab(i),'C');
3145                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_vendor_id_tab ='||g_vendor_id_tab(i),'C');
3146                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_ind_cmp_set_id_tab ='||g_ind_cmp_set_id_tab(i),'C');
3147                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_burdenable_raw_cost_tab ='||g_burdenable_raw_cost_tab(i),'C');
3148                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_reference6_tab ='||g_reference6_tab(i),'C');
3149                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_reference13_tab ='||g_reference13_tab(i),'C');
3150                       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'End-Records fetched from PO GT table '||-i,'C');
3151                      END LOOP;
3152                    End if;
3153 
3154 	      END IF;
3155 
3156              IF g_debug = 'Y' THEN
3157                 gms_error_pkg.gms_debug (g_error_procedure_name||':'||'After fetching data from AP/PO ','C');
3158              END IF;
3159 
3160    	     -- If no eligible GMS records to process then return with x_return_code as success ('P')
3161 	     IF g_doc_dist_id_tab.count = 0 THEN
3162                  IF g_debug = 'Y' THEN
3163                     gms_error_pkg.gms_debug (g_error_procedure_name||':'||'NO records fetched from AP/PO extract,GOTO END_OF_PROCESS ','C');
3164                  END IF;
3165                 GOTO END_OF_PROCESS;
3166              END IF;
3167 
3168 	     IF g_debug = 'Y' THEN
3169 		 gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Calling misc_synch_adls','C');
3170 	     END IF;
3171 
3172 	     IF NOT misc_synch_adls (p_application_id) THEN -- Bug 5344693 : misc_synch_adls is called with correct application_id.
3173 		 x_return_code := 'F';
3174 		 IF g_debug = 'Y' THEN
3175 		    gms_error_pkg.gms_debug (g_error_procedure_name||':'||'misc_synch_adls returned false','C');
3176 		 END IF;
3177 		 -- misc_synch_adls raises if any exceptions so no need to handle exception
3178 		 GOTO END_OF_PROCESS;
3179 	     END IF;
3180 
3181              -- Start of Code to fire GMS specific validations
3182 
3183              IF g_debug = 'Y' THEN
3184                 gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Fetching default award id ','C');
3185              END IF;
3186 
3187 	     SELECT NVL (default_dist_award_id, 0),
3188 		    NVL (award_distribution_option, 'N')
3189 	       INTO l_dist_award_id,
3190 		    l_award_dist_option
3191 	       FROM gms_implementations;
3192 
3193              IF g_debug = 'Y' THEN
3194                 gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Fetching default award id l_award_dist_option = '||l_award_dist_option,'C');
3195                 gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Fetching default award id l_dist_award_id = '||l_dist_award_id,'C');
3196              END IF;
3197 
3198 	     IF l_award_dist_option = 'Y' THEN
3199 
3200 		      -- --------------------------------------------------------------------------+
3201 		      -- Insert a dummy record into gms_award_distributions for the default award id
3202 		      -- to remove the outer joints on gms_award_distributions.
3203 		      -- --------------------------------------------------------------------------+
3204 		      x_adl_rec.award_set_id := l_dist_award_id;
3205 		      x_adl_rec.adl_line_num := 1;
3206 		      x_adl_rec.document_type := 'DST';
3207 		      x_adl_rec.award_id := l_dist_award_id;
3208 		      x_adl_rec.adl_status := 'A';
3209 		      x_adl_rec.fc_status := 'N';
3210 		      x_adl_rec.last_update_date := SYSDATE;
3211 		      x_adl_rec.last_updated_by := 0;
3212 		      x_adl_rec.created_by := 0;
3213 		      x_adl_rec.creation_date := SYSDATE ;
3214 		      x_adl_rec.last_update_login := 0;
3215 		      x_adl_rec.request_id := -9999;
3216 
3217 		      IF g_debug = 'Y' THEN
3218 			gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Calling gms_awards_dist_pkg.create_adls to create dummy ADL','C');
3219 		      END IF;
3220 
3221 		      gms_awards_dist_pkg.create_adls (x_adl_rec);
3222 
3223 	     END IF;
3224 	     l_counter := g_doc_dist_id_tab.count;
3225              --Below loop is to fetch additional information for each distribution
3226 	     FOR i in 1..g_doc_dist_id_tab.count LOOP
3227 
3228 		  IF g_document_type_tab(i) ='REQ' THEN
3229 
3230                       IF g_debug = 'Y' THEN
3231                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Fetching REQ data for distribution id '||g_doc_dist_id_tab(i),'C');
3232                       END IF;
3233 
3234                       OPEN c_req_adl_details(g_doc_dist_id_tab(i));
3235 		      FETCH c_req_adl_details INTO g_award_id_tab(i),
3236 		                                   g_result_code_tab(i),
3237 		                                   g_vendor_id_tab(i),
3238 						   g_ind_cmp_set_id_tab(i);
3239                       IF c_req_adl_details%NOTFOUND THEN
3240 
3241                        IF g_reference6_tab(i) = 'GMSIP' THEN -- If its an unsaved Iprocurement
3242                           IF g_debug = 'Y' THEN
3243                              gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Its an unsaved GMSIP transaction' ,'C');
3244                           END IF;
3245                           IF g_award_id_tab(i)=l_dist_award_id THEN
3246                              g_result_code_tab(i) :='F21';
3247                           ELSIF g_award_id_tab(i) IS NULL THEN
3248                              g_result_code_tab(i) :='F62';
3249                           END IF;
3250                           g_vendor_id_tab(i) :=g_reference13_tab(i);
3251                        ELSE
3252                           IF g_debug = 'Y' THEN
3253                              gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Raising REQ Data transfer failure F09' ,'C');
3254                           END IF;
3255 		          g_result_code_tab(i) := 'F09'; -- Data transfer failure
3256                        END IF; --F g_reference6_tab(i) = 'GMSIP' THEN
3257 
3258                       END IF;
3259 		      CLOSE c_req_adl_details;
3260 
3261                       IF g_debug = 'Y' THEN
3262                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_award_id_tab ='||g_award_id_tab(i),'C');
3263                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_result_code_tab ='||g_result_code_tab(i),'C');
3264                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_vendor_id_tab ='||g_vendor_id_tab(i),'C');
3265                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_ind_cmp_set_id_tab ='||g_ind_cmp_set_id_tab(i),'C');
3266                       END IF;
3267 
3268                   ELSIF g_document_type_tab(i) ='PO' THEN
3269 
3270                       IF g_debug = 'Y' THEN
3271                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Fetching PO data for distribution id '||g_doc_dist_id_tab(i),'C');
3272                       END IF;
3273 
3274                       OPEN c_po_adl_details(g_doc_dist_id_tab(i));
3275 		      FETCH c_po_adl_details INTO  g_award_id_tab(i),
3276 		                                   g_result_code_tab(i),
3277 		                                   g_vendor_id_tab(i),
3278 						   g_ind_cmp_set_id_tab(i),
3279 						   g_po_rate_tab(i); -- Bug 5614467
3280                       IF c_po_adl_details%NOTFOUND THEN
3281                           IF g_debug = 'Y' THEN
3282                              gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Raising PO Data transfer failure F09' ,'C');
3283                          END IF;
3284 		         g_result_code_tab(i) := 'F09'; -- Data transfer failure
3285                       END IF;
3286 		      CLOSE c_po_adl_details;
3287 
3288                       IF g_debug = 'Y' THEN
3289                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_award_id_tab ='||g_award_id_tab(i),'C');
3290                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_result_code_tab ='||g_result_code_tab(i),'C');
3291                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_vendor_id_tab ='||g_vendor_id_tab(i),'C');
3292                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_ind_cmp_set_id_tab ='||g_ind_cmp_set_id_tab(i),'C');
3293                       END IF;
3294 
3295 
3296                   ELSIF g_document_type_tab(i) ='AP' THEN
3297 
3298                       IF g_debug = 'Y' THEN
3299                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Fetching AP data for distribution id '||g_doc_dist_id_tab(i),'C');
3300                       END IF;
3301 
3302                       OPEN c_ap_adl_details(g_doc_dist_id_tab(i));
3303 		      FETCH c_ap_adl_details INTO  g_set_of_books_id_tab(i),
3304 		      				   g_exp_item_date_tab(i),
3305 		                                   g_exp_category_tab(i),
3306 						   g_revenue_category_tab(i),
3307 						   g_doc_dist_line_num_tab(i),
3308 						   g_invoice_type_code_tab(i),
3309 						   g_inv_source_tab(i),
3310 						   g_inv_dist_reference_1_tab(i),
3311 						   g_inv_dist_reference_2_tab(i),
3312 						   g_award_id_tab(i),
3313 		                                   g_result_code_tab(i),
3314 		                                   g_vendor_id_tab(i),
3315 						   g_ind_cmp_set_id_tab(i),
3316 						   g_burdenable_raw_cost_tab(i),
3317 						   g_parent_reversal_id_tab(i); -- Bug 5369296
3318                       IF c_ap_adl_details%NOTFOUND THEN
3319                           IF g_debug = 'Y' THEN
3320                              gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Raising AP Data transfer failure F09' ,'C');
3321                          END IF;
3322 		         g_result_code_tab(i) := 'F09'; -- Data transfer failure
3323                       END IF;
3324 		      CLOSE c_ap_adl_details;
3325 
3326                       IF g_debug = 'Y' THEN
3327                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_set_of_books_id_tab ='||g_set_of_books_id_tab(i),'C');
3328                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_exp_item_date_tab ='||g_exp_item_date_tab(i),'C');
3329                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_exp_category_tab ='||g_exp_category_tab(i),'C');
3330                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_revenue_category_tab ='||g_revenue_category_tab(i),'C');
3331                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_doc_dist_line_num_tab ='||g_doc_dist_line_num_tab(i),'C');
3332                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_invoice_type_code_tab ='||g_invoice_type_code_tab(i),'C');
3333                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_inv_source_tab ='||g_inv_source_tab(i),'C');
3334                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_inv_dist_reference_1_tab ='||g_inv_dist_reference_1_tab(i),'C');
3335                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_inv_dist_reference_2_tab( ='||g_inv_dist_reference_2_tab(i),'C');
3336                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_award_id_tab ='||g_award_id_tab(i),'C');
3337                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_result_code_tab ='||g_result_code_tab(i),'C');
3338                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_vendor_id_tab ='||g_vendor_id_tab(i),'C');
3339                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_ind_cmp_set_id_tab ='||g_ind_cmp_set_id_tab(i),'C');
3340                           gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_burdenable_raw_cost_tab ='||g_burdenable_raw_cost_tab(i),'C');
3341 			  gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_parent_reversal_id_tab ='||g_parent_reversal_id_tab(i),'C');
3342                       END IF;
3343 
3344                       -- Code to check if there exists associated PO/Release and if exists create records
3345     		      -- to relieve amount variance/quantitiy variance AND PO
3346 		      -- Note: Variance exists only if its PO matched distribution
3347 
3348     	              IF NVL(g_po_distribution_id_tab(i),0) <> 0 THEN
3349 
3350 
3351                          OPEN c_po_adl_details(g_po_distribution_id_tab(i));
3352 		         FETCH c_po_adl_details INTO  l_po_award_id,
3353 		                                      l_po_result_code ,
3354 		                                      l_po_vendor_id,
3355 						      l_po_ind_com_set_id,
3356 						      g_po_rate_tab(i); -- Bug 5614467
3357                          IF c_po_adl_details%NOTFOUND THEN
3358                              IF g_debug = 'Y' THEN
3359                                 gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Raising PO Data transfer failure F09' ,'C');
3360                              END IF;
3361 		             g_result_code_tab(i) := 'F09'; -- Data transfer failure
3362                          ELSE
3363                              IF g_debug = 'Y' THEN
3364 	                        pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling CREATE_BACKING_PO_APVAR_REC ');
3365                              End if;
3366 
3367                              -- Creating PO relieving record by copying AP line record and overwriting required column values
3368                              l_counter := l_counter+1;
3369 			     l_debug_start_counter:= l_counter;
3370 
3371 		             CREATE_BACKING_PO_APVAR_REC(p_copy_from_index    => i,
3372 		                                         p_new_rec_index      => l_counter, -- IN OUT VARIABLE
3373 							 p_po_vendor_id       => l_po_vendor_id,
3374 							 p_po_ind_com_set_id  => l_po_ind_com_set_id);
3375 
3376 			     IF g_debug = 'Y' THEN
3377 			       FOR i in l_debug_start_counter..g_doc_dist_id_tab.count LOOP
3378 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Start-Records created by CREATE_BACKING_PO_APVAR_REC'||-i,'C');
3379 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_je_source_name_tab ='||g_je_source_name_tab(i),'C');
3380 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_je_category_name_tab ='||g_je_category_name_tab(i),'C');
3381 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_actual_flag_tab ='||g_actual_flag_tab(i),'C');
3382 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_project_id_tab ='||g_project_id_tab(i),'C');
3383 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_task_id_tab ='||g_task_id_tab(i),'C');
3384 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_award_id_tab ='||g_award_id_tab(i),'C');
3385 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_entered_dr_tab ='||g_entered_dr_tab(i),'C');
3386 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_entered_cr_tab ='||g_entered_cr_tab(i),'C');
3387 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_etype_tab ='||g_etype_tab(i),'C');
3388 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_exp_org_id_tab ='||g_exp_org_id_tab(i),'C');
3389 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_exp_item_date_tab ='||g_exp_item_date_tab(i),'C');
3390 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_document_type_tab ='||g_document_type_tab(i),'C');
3391 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_doc_header_id_tab ='||g_doc_header_id_tab(i),'C');
3392 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_doc_dist_id_tab ='||g_doc_dist_id_tab(i),'C');
3393 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_prepay_std_inv_dist_id ='||g_prepay_std_inv_dist_id(i),'C');
3394 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_source_event_id_tab ='||g_source_event_id_tab(i),'C');
3395 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_result_code_tab ='||g_result_code_tab(i),'C');
3396 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_vendor_id_tab ='||g_vendor_id_tab(i),'C');
3397 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_ind_cmp_set_id_tab ='||g_ind_cmp_set_id_tab(i),'C');
3398 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_burdenable_raw_cost_tab ='||g_burdenable_raw_cost_tab(i),'C');
3399 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_quantity_variance_tab ='||g_quantity_variance_tab(i),'C');
3400 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_amount_variance_tab ='||g_amount_variance_tab(i),'C');
3401 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Value of g_po_distribution_id_tab ='||g_po_distribution_id_tab(i),'C');
3402 			         gms_error_pkg.gms_debug (g_error_procedure_name||':'||'End-Records created by CREATE_BACKING_PO_APVAR_REC'||-i,'C');
3403 			     END LOOP;
3404 			   End if;
3405 
3406                           END IF;
3407 		          CLOSE c_po_adl_details;
3408 
3409   	              END IF; --IF g_po_distribution_id_tab(i) IS NOT NULL THEN
3410 
3411                   END IF; --ELSIF g_document_type_tab(i) ='AP' THEN
3412 
3413               END LOOP;
3414 
3415 	      IF NVL (l_award_dist_option, 'N') = 'Y' THEN
3416 
3417                   IF g_debug = 'Y' THEN
3418                      gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Deleting dummy ADL','C');
3419                   END IF;
3420 
3421 		  DELETE gms_award_distributions
3422 		   WHERE award_set_id = NVL (l_dist_award_id, 0)
3423 		     AND document_type = 'DST'
3424 		     AND adl_line_num = 1
3425 	      	     AND adl_status = 'A'
3426 		     AND request_id = -9999;
3427 	     END IF;
3428    END IF; --IF p_application_id in (200,201) THEN
3429 
3430    IF g_debug = 'Y' THEN
3431       gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Calling Load_gms_pkts ','C');
3432    END IF;
3433 
3434    Load_gms_pkts (l_packet_id,
3435 		  p_partial_flag,
3436 		  g_set_of_books_id_tab,
3437 		  g_je_source_name_tab,
3438 		  g_je_category_name_tab,
3439 		  g_actual_flag_tab,
3440 		  g_project_id_tab,
3441 		  g_task_id_tab,
3442 		  g_award_id_tab,
3443 		  g_result_code_tab,
3444 		  g_entered_dr_tab,
3445 		  g_entered_cr_tab,
3446 		  g_etype_tab,
3447 		  g_exp_org_id_tab,
3448 		  g_exp_item_date_tab,
3449 		  g_document_type_tab,
3450 		  g_doc_header_id_tab,
3451 		  g_doc_dist_id_tab,
3452 		  g_vendor_id_tab,
3453 		  g_exp_category_tab,
3454 		  g_revenue_category_tab,
3455 		  g_ind_cmp_set_id_tab,
3456 		  g_burdenable_raw_cost_tab,--R12 AP Lines Uptake enhancement : Forward porting bug 4450291
3457 		  g_source_event_id_tab,
3458 		  l_return_code) ;
3459 
3460 
3461    x_return_code := l_return_code;
3462 
3463    -- R12 Funds Management uptake : Calling burdenable raw cost calculation logic for AP/PO/REQ from main session
3464    -- as access to AP/PO/REQ tables is required
3465 
3466    IF l_mode IN ('R','U','C') AND x_return_code = 'P' THEN
3467 
3468       -- Calling burdenable raw cost calculation
3469       IF NOT gms_cost_plus_extn.update_bc_pkt_burden_raw_cost (l_packet_id,l_mode,p_partial_flag) THEN
3470          x_return_code := 'F';
3471       END IF;
3472 
3473       -- Calling procedure to update failed status because of BRC calculation error
3474       update_bc_pkt_brc_status  ( l_packet_id,
3475                                   x_return_code,
3476 				  p_partial_flag,
3477 				  l_mode);
3478    END IF;
3479 
3480   -- If code reaches this point means that data has been correctly transferred from
3481   -- gl_bc_packets to gms_bc_packets ..
3482 
3483   <<END_OF_PROCESS>>
3484   NULL;
3485 
3486 EXCEPTION
3487       WHEN OTHERS THEN
3488          IF l_award_dist_option = 'Y' THEN
3489             DELETE      gms_award_distributions
3490                   WHERE award_set_id = l_dist_award_id
3491                     AND document_type = 'DST'
3492                     AND adl_line_num = 1
3493                     AND adl_status = 'A'
3494                     AND request_id = -9999;
3495          END IF;
3496 
3497          IF g_debug = 'Y' THEN
3498 	  	gms_error_pkg.gms_debug ('misc_gms_insert - Exception '||' SQLCODE:'||SQLCODE||' SQLERRM:'||SQLERRM, 'C');
3499 	 END IF;
3500          x_return_code := 'F';
3501 END copy_gl_pkt_to_gms_pkt;
3502 
3503 
3504 ----------------------------------------------------------------------------------------------------------
3505 -- This Module creates records for indirect cost for each resource record in a
3506 -- packet.
3507 ----------------------------------------------------------------------------------------------------------
3508 
3509    FUNCTION misc_gms_idc (x_packet_id IN NUMBER)
3510       RETURN BOOLEAN IS
3511       doc_type   VARCHAR2 (10);
3512    BEGIN
3513       g_error_procedure_name := 'misc_gms_idc';
3514       SELECT document_type
3515         INTO doc_type
3516         FROM gms_bc_packets
3517        WHERE packet_id = x_packet_id
3518          AND nvl(burden_adjustment_flag,'N') = 'N'
3519          AND ROWNUM = 1;
3520 
3521 -- ------------------------------------------------------------------
3522 -- Indirect Cost should also be created for FAB to get funds checked.
3523 -- ------------------------------------------------------------------
3524 --  Begin Bug 2456878; three insert statements for doc_type 'EXP'/('AP', 'PO', 'REQ', 'FAB') have
3525 --  been combined into one insert statement for perormance reasons. Also, redundant joins to table
3526 --  have been removed
3527 
3528       IF doc_type IN ('AP', 'PO', 'REQ', 'FAB') THEN
3529          g_error_stage := 'MISC_IDC : AP,PO,REQ';
3530       ELSIF doc_type = 'EXP' THEN -- Expenditures
3531          g_error_stage := 'MISC_IDC : EXP';
3532       ELSIF doc_type = 'ENC' THEN -- Encumbrances
3533          g_error_stage := 'MISC_IDC : ENC';
3534       END IF;
3535 
3536       -- get the compile set id for 'AP', 'PO', 'REQ', 'FAB', 'ENC'.
3537       -- no need to get it for 'EXP'
3538       -- Fix for bug : 2927485 , Removed 'ENC'
3539       IF doc_type IN ('AP', 'PO', 'REQ', 'FAB' ) THEN
3540 
3541 /* Bug 10082739: Reverted the fixes done in previous couple of versions to the below update statement */
3542 
3543         -- populating compiled set id where ever it is null
3544         UPDATE gms_bc_packets gbc
3545         SET ind_compiled_set_id =  gms_cost_plus_extn.get_award_cmt_compiled_set_id (
3546                             gbc.task_id,
3547                             gbc.expenditure_item_date,
3548                             gbc.expenditure_type, --Bug 3003584
3549                             gbc.expenditure_organization_id,
3550                             'C',
3551                             gbc.award_id)
3552         WHERE gbc.packet_id = x_packet_id
3553         AND   gbc.status_code = 'P'
3554         AND   gbc.ind_compiled_set_id is null
3555         AND   nvl(gbc.burden_adjustment_flag,'N') = 'N'; -- 3389292
3556 
3557 
3558 /* Bug 10082739: Added the below update statement to specifically handle the case of AP distributions where ind_compiled_set_id
3559    was derived as NULL during validation of the invoice, and so should again be derived as NULL when
3560    trying to relieve funds from AP bucket*/
3561 
3562         UPDATE gms_bc_packets gbc
3563         SET ind_compiled_set_id =
3564                  (select adl.ind_compiled_set_id from gms_award_distributions adl
3565                   where adl.invoice_id = gbc.document_header_id
3566                   and   adl.invoice_distribution_id = gbc.document_distribution_id
3567                   and   adl.adl_status = 'A'
3568                   and   adl.fc_status = 'A'
3569                   and   adl.document_type = 'AP')
3570         WHERE gbc.packet_id = x_packet_id
3571         AND   gbc.status_code = 'P'
3572         AND   gbc.document_type = 'AP'
3573 		and   gbc.actual_flag = 'E'
3574         AND   nvl(gbc.burden_adjustment_flag,'N') = 'N'
3575         AND exists (select 1 from gms_award_distributions adl2
3576                     where adl2.invoice_id = gbc.document_header_id
3577                     and   adl2.invoice_distribution_id = gbc.document_distribution_id
3578                     and   adl2.adl_status = 'A'
3579                     and   adl2.fc_status = 'A'
3580                     and   adl2.document_type = 'AP'
3581                     and   nvl(adl2.ind_compiled_set_id,-99) <>  nvl(gbc.ind_compiled_set_id,-99));
3582 
3583 
3584 
3585 /* ====================================================================================
3586     Commented out for bug 3810247 : This code will move to stage 701 of gms_fck ..
3587 
3588      ELSIF doc_type = 'ENC' THEN
3589 
3590   	gms_error_pkg.gms_debug ('ENC1: Call calc_enc_ind_compiled_set_id ', 'C');
3591          -- Fix for bug : 2927485 : calculate ind_compiled_set_id for ENC
3592           CALC_ENC_IND_COMPILED_SET_ID (x_packet_id);
3593 
3594   	gms_error_pkg.gms_debug ('ENC2: Call Handle_net_zero_txn:Net_Zero ', 'C');
3595         -- Check if  adjusted and adjusting transactions are present in the same packet
3596         -- If so, update them with result_code 'P82' and update effect_on_funds_code
3597         -- to 'I' so that 'funds avilable' calculation ignores them.
3598 
3599             HANDLE_NET_ZERO_TXN(x_packet_id,'Net_Zero');
3600 
3601   	gms_error_pkg.gms_debug ('ENC3: Call Handle_net_zero_txn:Check_Adjusted ', 'C');
3602         --  Fail adjusting transaction, if original transaction has not been  FC'ed(F08)
3603 
3604             HANDLE_NET_ZERO_TXN(x_packet_id, 'Check_Adjusted');
3605  ======================================================================================== */
3606 
3607      END IF;
3608 
3609 
3610 	             INSERT INTO gms_bc_packets
3611                      (packet_id,
3612                       project_id,
3613                       award_id,
3614                       task_id,
3615                       expenditure_type,
3616                       expenditure_item_date,
3617                       actual_flag,
3618                       status_code,
3619                       last_update_date,
3620                       last_updated_by,
3621                       created_by,
3622                       creation_date,
3623                       last_update_login,
3624                       set_of_books_id,
3625                       je_category_name,
3626                       je_source_name,
3627                       transfered_flag,
3628                       document_type,
3629                       expenditure_organization_id,
3630                       period_name,
3631                       period_year,
3632                       period_num,
3633                       document_header_id,
3634                       document_distribution_id,
3635                       top_task_id,
3636                       budget_version_id,
3637                       resource_list_member_id,
3638                       account_type,
3639                       entered_dr,
3640                       entered_cr,
3641                       tolerance_amount,
3642                       tolerance_percentage,
3643                       override_amount,
3644                       effect_on_funds_code,
3645                       result_code,
3646                       amount_type,
3647                       boundary_code,
3648                       time_phased_type_code,
3649                       categorization_code,
3650                       request_id,
3651                       gl_bc_packets_rowid,
3652                       bc_packet_id,
3653                       parent_bc_packet_id,
3654 		      VENDOR_ID,  --ADDED FOR BUG 16009601
3655 		      person_id,
3656 		      job_id,
3657 		      expenditure_category,
3658 		      revenue_category,
3659 		      adjusted_document_header_id,
3660 		      award_set_id,
3661 		      transaction_source,
3662 		      burden_adjustment_flag,
3663 		      burden_adj_bc_packet_id,
3664                       source_event_id,
3665                       session_id,
3666                       serial_id,
3667                       txn_interface_id)    -- Bug 9929155: Added txn_interface_id
3668             SELECT /*+ index(gbc GMS_BC_PACKETS_N1) */ gbc.packet_id, /* Added the index hint for performance - Bug 5656276 */
3669                    gbc.project_id,
3670                    gbc.award_id,
3671                    gbc.task_id,
3672                    icc.expenditure_type,
3673                    TRUNC (gbc.expenditure_item_date),
3674                    gbc.actual_flag,
3675                    gbc.status_code,
3676                    gbc.last_update_date,
3677                    gbc.last_updated_by,
3678                    gbc.created_by,
3679                    gbc.creation_date,
3680                    gbc.last_update_login,
3681                    gbc.set_of_books_id,
3682                    gbc.je_category_name,
3683                    gbc.je_source_name,
3684                    gbc.transfered_flag,
3685                    gbc.document_type,
3686                    gbc.expenditure_organization_id,
3687                    gbc.period_name,
3688                    gbc.period_year,
3689                    gbc.period_num,
3690                    gbc.document_header_id,
3691                    gbc.document_distribution_id,
3692                    gbc.top_task_id,
3693                    gbc.budget_version_id,
3694                    gbc.resource_list_member_id,
3695                    gbc.account_type,
3696                    pa_currency.round_currency_amt(
3697                            decode(sign(gbc.BURDENABLE_RAW_COST * nvl(cm.compiled_multiplier,0)),
3698                                   1, gbc.burdenable_raw_cost * nvl(cm.compiled_multiplier, 0),
3699                                   0)), /* Bug 3620801 --entered_dr*/
3700                    pa_currency.round_currency_amt(
3701                            decode(sign(gbc.BURDENABLE_RAW_COST * nvl(cm.compiled_multiplier,0)),
3702                                   -1, abs(gbc.burdenable_raw_cost * nvl(cm.compiled_multiplier, 0)),
3703                                   0)), /* Bug 3620801 entered_cr*/
3704                    gbc.tolerance_amount,
3705                    gbc.tolerance_percentage,
3706                    gbc.override_amount,
3707                    gbc.effect_on_funds_code,
3708                    gbc.result_code,
3709                    gbc.amount_type,
3710                    gbc.boundary_code,
3711                    gbc.time_phased_type_code,
3712                    gbc.categorization_code,
3713                    gbc.request_id,
3714                    gbc.gl_bc_packets_rowid,
3715                    gms_bc_packets_s.NEXTVAL,
3716                    decode(gbc.burden_adjustment_flag,'Y',gbc.parent_bc_packet_id,gbc.bc_packet_id),
3717                     -- In case of burden adjustment flag, use parent_bc_packet_id on raw adjsutment line
3718 		   gbc.vendor_id, --ADDED FOR BUG 16009601
3719 		   gbc.person_id,
3720 		   gbc.job_id,
3721 		   et.expenditure_category,
3722 		   et.revenue_category_code,
3723 		   gbc.adjusted_document_header_id,
3724 		   gbc.award_set_id,
3725 		   gbc.transaction_source,
3726                    gbc.burden_adjustment_flag,
3727                    gbc.burden_adj_bc_packet_id,
3728                    gbc.source_event_id,
3729                    gbc.session_id,
3730                    gbc.serial_id,
3731                    gbc.txn_interface_id   -- Bug 9929155
3732               FROM /*pa_ind_rate_sch_revisions irsr, Bug 5656276 */
3733                    pa_expenditure_types et,
3734                    pa_ind_cost_codes icc,
3735 		   pa_cost_base_cost_codes cbcc, -- Bug 5656276
3736                    pa_cost_base_exp_types cbet,
3737                    /*pa_ind_compiled_sets ics, Bug 5656276 */
3738                    pa_compiled_multipliers cm,
3739                    gms_bc_packets gbc
3740              WHERE /*irsr.cost_plus_structure = cbet.cost_plus_structure Bug 5656276 */
3741                    et.expenditure_type = icc.expenditure_type -- 2092791 ( RLMI Change)
3742                AND icc.ind_cost_code = cm.ind_cost_code
3743                AND cbet.cost_base = cm.cost_base
3744 	       AND cbcc.cost_plus_structure = cbet.cost_plus_structure
3745                AND cbet.cost_base_type = 'INDIRECT COST'
3746                /*AND ics.cost_base = cbet.cost_base -- Bug 3003584 Bug 5656276 */
3747                AND cbet.expenditure_type = gbc.expenditure_type
3748 	       AND cbcc.cost_base = cbet.cost_base /* Bug 5656276 start */
3749 	       AND cm.cost_base_cost_code_id = cbcc.cost_base_cost_code_id
3750 	       AND cm.ind_cost_code = cbcc.ind_cost_code /* Bug 5656276 end */
3751                /*AND ics.ind_rate_sch_revision_id = irsr.ind_rate_sch_revision_id
3752                AND ics.organization_id = gbc.expenditure_organization_id
3753                AND ics.ind_compiled_set_id = gbc.ind_compiled_set_id  Bug 5656276 */
3754                AND cm.ind_compiled_set_id = gbc.ind_compiled_set_id /* Bug 5656276 */
3755                AND cm.compiled_multiplier <> 0
3756                AND NVL (gbc.burdenable_raw_cost, 0) <> 0
3757                AND gbc.packet_id = x_packet_id
3758     	       AND status_code in ('I', 'P');
3759 
3760 
3761 --  BUG  2456878
3762 --  Three insert statements for doc_type 'EXP'/'ENC'/('AP', 'PO', 'REQ', 'FAB') have
3763 --  been combined into one insert statement as coded above for perormance reasons.
3764 
3765       RETURN TRUE;
3766    END misc_gms_idc;
3767 
3768 --===============================================================================================
3769 /*             This Function updates following setup columns of gms_bc_packets
3770    	  		   budget_version_id
3771                amount_type
3772                boundary_code
3773                time_phased_type_code
3774                categorization_code
3775                resource_list_id
3776 			   effect_on_funds_code
3777 			   Note : Budget Version Id is updated only if it is null , in case of
3778 			   		  Award budget submit/Baseline Process. Budget_version_id is inserted during
3779 					  insertion of records in gms_bc_packets. So if budget_version_id is
3780 					  alreay present this procedure will not update budget_version_id.
3781 
3782 					  The earlier logic of calculating budget_version_id in case of
3783 					  mode ('S'/'B') is removed, as budget_version_id logic is already
3784 					  present while inserting records in gms_bc_packets.
3785 */
3786 --===============================================================================================
3787 
3788    FUNCTION initialize_setup (x_packet_id IN NUMBER,
3789                               p_mode      IN VARCHAR2)
3790       RETURN BOOLEAN IS
3791       x_budget_version_id           gms_bc_packets.budget_version_id%TYPE;
3792       x_amount_type                 gms_bc_packets.amount_type%TYPE;
3793       x_boundary_code               gms_bc_packets.boundary_code%TYPE;
3794       x_time_phased_type_code       gms_bc_packets.time_phased_type_code%TYPE;
3795       x_categorization_code         gms_bc_packets.categorization_code%TYPE;
3796       x_project_id                  gms_bc_packets.project_id%TYPE;
3797       x_award_id                    gms_bc_packets.award_id%TYPE;
3798       x_dist_award_id               gms_bc_packets.award_id%TYPE;
3799       x_resource_list_id            gms_bc_packets.resource_list_id%TYPE;
3800       x_award_distribution_option   VARCHAR2 (10);
3801       CURSOR cur_init_setup IS
3802          SELECT DISTINCT project_id,
3803                          award_id,
3804                          budget_version_id
3805                     FROM gms_bc_packets
3806                    WHERE packet_id = x_packet_id
3807 				     AND status_code in ('P','A')	--Bug 2143160
3808 				   ;
3809    BEGIN
3810    	  g_error_procedure_name  :=  'initialize_setup';
3811       IF g_debug = 'Y' THEN
3812       	gms_error_pkg.gms_debug ('Initilaze Setup - Start ', 'C');
3813       END IF;
3814       g_error_stage := 'INIT_SETUP : START';
3815       SELECT NVL (default_dist_award_id, 0),
3816              NVL (award_distribution_option, 'N')
3817         INTO x_dist_award_id,
3818              x_award_distribution_option
3819         FROM gms_implementations;
3820       OPEN cur_init_setup;
3821       LOOP
3822          FETCH cur_init_setup INTO x_project_id, x_award_id, x_budget_version_id;
3823          EXIT WHEN cur_init_setup%NOTFOUND
3824                 OR x_award_id = NVL (x_dist_award_id, -1111);
3825 		 BEGIN
3826          SELECT pb.budget_version_id,
3827                 ga.amount_type,
3828                 ga.boundary_code,
3829                 pbm.time_phased_type_code,
3830                 pbm.categorization_code,
3831                 pb.resource_list_id
3832            INTO x_budget_version_id,
3833                 x_amount_type,
3834                 x_boundary_code,
3835                 x_time_phased_type_code,
3836                 x_categorization_code,
3837                 x_resource_list_id
3838            FROM gms_budget_versions pb, pa_budget_entry_methods pbm, gms_awards ga
3839           WHERE ga.award_id = pb.award_id
3840             AND pb.project_id = x_project_id
3841             AND pb.award_id = x_award_id
3842             AND pb.budget_entry_method_code = pbm.budget_entry_method_code
3843             AND pb.award_id = ga.award_id
3844             AND pb.budget_version_id =
3845                       DECODE (x_budget_version_id, NULL, pb.budget_version_id, x_budget_version_id)
3846             AND pb.current_flag = DECODE (x_budget_version_id, NULL, 'Y', pb.current_flag);
3847        --   AND pb.budget_status_code = 'B'; -- (This code is commented because in 11I Funds Check is done in
3848                                                --  Budget Submit mode also )
3849          UPDATE gms_bc_packets
3850             SET budget_version_id = x_budget_version_id,
3851                 amount_type = x_amount_type,
3852                 boundary_code = x_boundary_code,
3853                 time_phased_type_code = x_time_phased_type_code,
3854                 categorization_code = x_categorization_code,
3855                 resource_list_id = x_resource_list_id,
3856                 -- Bug 2927485 : Added decode in following statement, we shouldn't
3857                 -- override effect_on_funds_code if it is already populated
3858 		effect_on_funds_code = DECODE(effect_on_funds_code,NULL,DECODE (SIGN (NVL (entered_dr, 0) - NVL (entered_cr, 0)), 1, 'D', 'I'),effect_on_funds_code) --Bug 2069132 ( code Transferred from setup_rlmi )
3859           WHERE packet_id = x_packet_id
3860             AND project_id = x_project_id
3861             AND award_id = x_award_id
3862  	        AND status_code in ('P','A')	--Bug 2143160
3863 			;
3864 	  EXCEPTION
3865 	  	WHEN NO_DATA_FOUND THEN
3866          UPDATE gms_bc_packets
3867             SET result_code = 'F12',
3868 		status_code = decode(p_mode,'S','E','C','F','R')
3869           WHERE packet_id = x_packet_id
3870             AND project_id = x_project_id
3871             AND award_id = x_award_id
3872  	        AND status_code in ('P','A');	--Bug 2143160
3873 	  END;
3874 
3875       END LOOP;
3876       CLOSE cur_init_setup;
3877       g_error_stage := 'INIT_SETUP : END';
3878       IF g_debug = 'Y' THEN
3879       	gms_error_pkg.gms_debug ('Initilize_Setup - End ', 'C');
3880       END IF;
3881       IF x_award_id = NVL (x_dist_award_id, -1111) THEN
3882          UPDATE gms_bc_packets
3883             SET status_code = decode(p_mode,'S','E','C','F','R'),
3884 		result_code = 'F21',
3885                 res_result_code = 'F21',
3886                 res_grp_result_code = 'F21',
3887                 task_result_code = 'F21',
3888                 top_task_result_code = 'F21',
3889                 award_result_code = 'F21'
3890           WHERE packet_id = x_packet_id;
3891 		 RETURN FALSE;
3892       END IF;
3893       RETURN TRUE;
3894    EXCEPTION
3895       WHEN OTHERS THEN
3896          IF cur_init_setup%ISOPEN THEN
3897             CLOSE cur_init_setup;
3898          END IF;
3899          RAISE;
3900    END initialize_setup;
3901 
3902 ----------------------------------------------------------------------------------------------------------
3903 -- Bug 2092791 : RLMI BULK Update Changes
3904 -- 	   		   	 commented out NOCOPY old setup_rlmi procedure and written new setup_rlmi Procedure
3905 --				 This change is made because of performance issue. The BULK Update feature will update
3906 --				 This Procedure uses BULK UPDATE Feature which will update resource List Member Id
3907 --				 in BULK using PL/SQL Table
3908 -- Purpose :     Generating resource_list_member_id for the packet based on the resource list
3909 --               and expenditure_type
3910 
3911 --  --------------------*****-NOTE-******-------------------
3912 --  SETUP_RLMI - Please don't use commit in this API.
3913 --  THIS API IS SHARED WITH VERTICAL APPLICATION INTERFACE.
3914 --  --------------------------------------------------------
3915 ----------------------------------------------------------------------------------------------------------
3916 
3917       PROCEDURE setup_rlmi (
3918       x_packet_id   IN       NUMBER,
3919       x_mode        IN       VARCHAR2,
3920       x_err_code    OUT NOCOPY      NUMBER,
3921       x_err_buff    OUT NOCOPY      VARCHAR2) IS
3922       TYPE t_doctype IS TABLE OF gms_bc_packets.document_type%TYPE;
3923 
3924       TYPE t_exptype IS TABLE OF gms_bc_packets.expenditure_type%TYPE;
3925 
3926       TYPE t_orgid IS TABLE OF gms_bc_packets.expenditure_organization_id%TYPE;
3927 
3928       TYPE t_personid IS TABLE OF gms_bc_packets.person_id%TYPE;
3929 
3930       TYPE t_jobid IS TABLE OF gms_bc_packets.job_id%TYPE;
3931 
3932       TYPE t_vendorid IS TABLE OF gms_bc_packets.vendor_id%TYPE;
3933 
3934       TYPE t_expcat IS TABLE OF gms_bc_packets.expenditure_category%TYPE;
3935 
3936       TYPE t_revcat IS TABLE OF gms_bc_packets.revenue_category%TYPE;
3937 
3938       TYPE t_catcode IS TABLE OF gms_bc_packets.categorization_code%TYPE;
3939 
3940       TYPE t_reslist IS TABLE OF gms_bc_packets.resource_list_id%TYPE;
3941 
3942       TYPE t_rowid IS TABLE OF VARCHAR2 (50);
3943 
3944       TYPE t_rlmi IS TABLE OF gms_bc_packets.resource_list_member_id%TYPE;
3945 
3946 --      TYPE t_upg_err IS TABLE OF gms_award_distributions.upg_error%TYPE;  -- Bug 2178694
3947 
3948       TYPE t_fc_err IS TABLE OF gms_bc_packets.fc_error_message%TYPE;
3949 
3950       t_doc_type                   t_doctype;
3951       t_exp_type                   t_exptype;
3952       t_person_id                  t_personid;
3953       t_job_id                     t_jobid;
3954       t_vendor_id                  t_vendorid;
3955       t_org_id                     t_orgid;
3956       t_exp_cat                    t_expcat;
3957       t_rev_cat                    t_revcat;
3958       t_cat_code                   t_catcode;
3959       t_res_list                   t_reslist;
3960       t_row_id                     t_rowid;
3961       t_rlmi_value                 t_rlmi;
3962       t_fc_error                   t_fc_err;			-- Bug 2178694
3963       x_prev_list_processed        NUMBER (30);
3964       x_group_resource_type_id     NUMBER (15);
3965       x_group_resource_type_name   VARCHAR2 (60);
3966       x_resource_type_tab          gms_res_map.resource_type_table;
3967       x_budget_version_id          NUMBER;
3968       x_res_list_id                NUMBER;
3969       x_categorization_code        VARCHAR2 (1);
3970       --   x_err_code                   NUMBER;
3971       --   x_err_buff                   VARCHAR2 (2000);
3972 
3973 
3974       BEGIN
3975 
3976       IF initialize_setup (x_packet_id,x_mode) THEN
3977 
3978       --  ########### =========================================
3979       --  SETUP_RLMI - Please don't use commit in this API.
3980       --  THIS API IS SHARED WITH VERTICAL APPLICATION INTERFACE.
3981       --  ########### =========================================-
3982 
3983          g_error_stage := 'SETUP_RLMI : START';
3984          IF g_debug = 'Y' THEN
3985          	gms_error_pkg.gms_debug ('SETUP_RLMI - Start  ', 'C');
3986          END IF;
3987          g_error_procedure_name := 'setup_rlmi';
3988 
3989          IF x_mode <> 'U' THEN
3990             -- 1. Bulk Collect records
3991 
3992 		-- --------------------------------------------------------
3993      		-- Bug 2069132 : Removed nvl for vendor_id,person_id,job_id
3994 		-- --------------------------------------------------------
3995             SELECT   resource_list_id,
3996                      categorization_code,
3997                      document_type,
3998                      expenditure_type,
3999                      expenditure_organization_id,
4000                      expenditure_category,
4001                      revenue_category,
4002                      person_id,
4003                      job_id,
4004                      vendor_id,
4005                      ROWID,
4006                      resource_list_member_id,
4007                      fc_error_message
4008                 BULK COLLECT INTO t_res_list,
4009                                   t_cat_code,
4010                                   t_doc_type,
4011                                   t_exp_type,
4012                                   t_org_id,
4013                                   t_exp_cat,
4014                                   t_rev_cat,
4015                                   t_person_id,
4016                                   t_job_id,
4017                                   t_vendor_id,
4018                                   t_row_id,
4019                                   t_rlmi_value,
4020                                   t_fc_error		-- Bug 2178694
4021                 FROM gms_bc_packets
4022                WHERE packet_id = x_packet_id
4023                  AND status_code NOT IN ('F','R')                 -- Bug 2927485
4024 		 AND resource_list_member_id is NULL
4025 		 AND nvl(burden_adjustment_flag ,'N') = 'N' -- 3389292
4026             ORDER BY resource_list_id,
4027                      categorization_code,
4028                      document_type,
4029                      expenditure_type,
4030                      expenditure_organization_id,
4031                      expenditure_category,
4032                      person_id,
4033                      vendor_id,
4034                      job_id,
4035                      revenue_category ;
4036 
4037                g_error_stage := 'SETUP_RLMI : BULK COLLECT';
4038       -- 2. Bulk Processing
4039             IF t_row_id.COUNT > 0 THEN
4040                FOR bcpkt_records IN t_row_id.FIRST .. t_row_id.LAST
4041                LOOP
4042 			   --  If The Value of following Variable is same as that of previous then don't calculate
4043 			   --  resource_list_member_id again use the same resource list member_id
4044                   IF    t_row_id.PRIOR (bcpkt_records) IS NULL
4045                      OR (   (t_res_list (t_row_id.PRIOR (bcpkt_records)) <>  t_res_list (bcpkt_records))
4046                          OR (t_cat_code (t_row_id.PRIOR (bcpkt_records)) <>  t_cat_code (bcpkt_records))
4047                          OR (t_doc_type (t_row_id.PRIOR (bcpkt_records)) <>  t_doc_type (bcpkt_records))
4048                          OR (t_exp_type (t_row_id.PRIOR (bcpkt_records)) <>  t_exp_type (bcpkt_records))
4049                          OR (t_org_id (t_row_id.PRIOR (bcpkt_records))   <>  t_org_id (bcpkt_records))
4050                          OR (t_exp_cat (t_row_id.PRIOR (bcpkt_records))  <>  t_exp_cat (bcpkt_records))
4051                          OR (t_rev_cat (t_row_id.PRIOR (bcpkt_records))  <>  t_rev_cat (bcpkt_records))
4052                          OR (t_person_id (t_row_id.PRIOR (bcpkt_records))<>  t_person_id (bcpkt_records))
4053                          OR (t_job_id (t_row_id.PRIOR (bcpkt_records))   <>  t_job_id (bcpkt_records))
4054                          OR (t_vendor_id (t_row_id.PRIOR (bcpkt_records))<>  t_vendor_id (bcpkt_records))) THEN
4055 
4056       --               gms_error_pkg.gms_debug (
4057       --                        'Setup_rlmi - Debug  '|| t_exp_type (bcpkt_records),
4058       --                  'C');
4059 
4060                      gms_res_map.map_resources_group (
4061                         t_doc_type (bcpkt_records),
4062                         t_exp_type (bcpkt_records),
4063                         t_org_id (bcpkt_records),
4064                         t_person_id (bcpkt_records),
4065                         t_job_id (bcpkt_records),
4066                         t_vendor_id (bcpkt_records),
4067                         t_exp_cat (bcpkt_records),
4068                         t_rev_cat (bcpkt_records),
4069                         t_cat_code (bcpkt_records),
4070                         t_res_list (bcpkt_records),
4071                         NULL,
4072                         x_prev_list_processed,
4073                         x_group_resource_type_id,
4074                         x_group_resource_type_name,
4075                         x_resource_type_tab,
4076                         t_rlmi_value (bcpkt_records),
4077                         t_fc_error (bcpkt_records),
4078                         x_err_buff);
4079 
4080       --         gms_error_pkg.gms_debug ('Setup_rlmi - Resource List '||to_char(t_res_list(bcpkt_records)), 'C');
4081       --         gms_error_pkg.gms_debug ('Setup_rlmi - Cat Code '||t_cat_code(bcpkt_records), 'C');
4082       --         gms_error_pkg.gms_debug ('Setup_rlmi - Doc Type '||t_doc_type(bcpkt_records), 'C');
4083       --         gms_error_pkg.gms_debug ('Setup_rlmi - Exp Type '||t_exp_type(bcpkt_records), 'C');
4084       --         gms_error_pkg.gms_debug ('Setup_rlmi - Org Id '||t_org_id(bcpkt_records), 'C');
4085       --         gms_error_pkg.gms_debug ('Setup_rlmi - Exp Cat '||t_exp_cat(bcpkt_records), 'C');
4086       --         gms_error_pkg.gms_debug ('Setup_rlmi - Rev Cat '||t_rev_cat(bcpkt_records), 'C');
4087       --         gms_error_pkg.gms_debug ('Setup_rlmi - Person Id '||t_person_id(bcpkt_records), 'C');
4088       --         gms_error_pkg.gms_debug ('Setup_rlmi - Job Id '||t_job_id(bcpkt_records), 'C');
4089       --         gms_error_pkg.gms_debug ('Setup_rlmi - Vendor Id '||t_vendor_id(bcpkt_records), 'C');
4090       --         gms_error_pkg.gms_debug ('Setup_rlmi - RLMId '||t_rlmi_value(bcpkt_records), 'C');
4091 
4092                   ELSE
4093                      t_rlmi_value (bcpkt_records) :=
4094                                      t_rlmi_value (t_row_id.PRIOR (bcpkt_records));
4095                   END IF;
4096                END LOOP;
4097       --4. Bulk Update
4098                g_error_stage := 'SETUP_RLMI : BULK COLLECT';
4099                FORALL bcpkt_txns IN t_row_id.FIRST .. t_row_id.LAST
4100                   UPDATE gms_bc_packets
4101                      SET status_code = decode(t_rlmi_value (bcpkt_txns),NULL,decode(x_mode,'S','E','C','F','R'),status_code),
4102 					 	 result_code = decode(t_rlmi_value (bcpkt_txns),NULL,'F94',result_code),
4103 					 	 resource_list_member_id = t_rlmi_value (bcpkt_txns),
4104                          fc_error_message = t_fc_error (bcpkt_txns)		-- Bug 2178694
4105                    WHERE ROWID = t_row_id (bcpkt_txns);
4106             END IF;
4107          END IF;
4108       END IF;
4109       END setup_rlmi;
4110 
4111 
4112 ----------------------------------------------------------------------------------------------------------
4113 --Procedure to calulate budgeted task id in packet for a budget version, entry level code and budget entry
4114 --method and update gms_bc_packets for the same set of records having the same combinations.
4115 --A single Update Statment will take care of Updating Budget task id for Following Budget Entry Methods
4116 --      Budget Entry Method
4117 --              P               By Project
4118 --              T               By Top Task
4119 --              L               By Lowest Task
4120 --              M               By Top or Lowest Task
4121 ----------------------------------------------------------------------------------------------------------
4122    PROCEDURE budget_task_id_update (
4123       x_packetid   IN       NUMBER) IS
4124    BEGIN
4125       g_error_procedure_name := 'budget_task_id_update';
4126       -- if the budget entry level in 'L','T','P' -- update directly.
4127       g_error_stage := 'BUD_TASK_UPD :L,P,T';
4128       UPDATE gms_bc_packets bc
4129          SET (bc.bud_task_id, bc.top_task_id) =
4130                 (SELECT DECODE (bem.entry_level_code, 'P', 0, 'L', bc.task_id, t.top_task_id),
4131                         DECODE (bem.entry_level_code, 'P', 0, t.top_task_id)
4132                    FROM pa_budget_entry_methods bem, gms_budget_versions bv,
4133                                                                             pa_tasks t
4134                   WHERE bv.budget_version_id = bc.budget_version_id
4135                     AND bv.budget_entry_method_code = bem.budget_entry_method_code
4136                     AND bem.entry_level_code IN ('P', 'L', 'T')
4137                     AND t.task_id = bc.task_id)
4138        WHERE bc.packet_id = x_packetid
4139         AND  bc.status_code = 'P'
4140 	AND  bc.bud_task_id IS NULL
4141 	AND  nvl(bc.burden_adjustment_flag,'N') = 'N'  -- 3389292
4142         AND EXISTS ( SELECT 1
4143                         FROM pa_budget_entry_methods bem1, gms_budget_versions bv1
4144                        WHERE bv1.budget_version_id = bc.budget_version_id
4145                          AND bv1.budget_entry_method_code = bem1.budget_entry_method_code
4146                          AND bem1.entry_level_code IN ('P', 'L', 'T'));
4147 
4148         IF g_debug = 'Y' THEN
4149         	gms_error_pkg.gms_debug('BUDGET_TASK_ID_UPDATE - Update for Entry Level code P,L,T Complete ','C');
4150         END IF;
4151 
4152        -- Added commit for the base bug 3848201
4153        commit;
4154 
4155       -- if the budget entry level = 'M' and budget at LOWEST TASK
4156         g_error_stage := 'BUD_TASK_UPD :M';
4157       UPDATE gms_bc_packets bc
4158          SET (bc.bud_task_id, bc.top_task_id) =
4159                 (SELECT t.task_id,
4160                         t.top_task_id
4161                    FROM pa_budget_entry_methods bem, gms_budget_versions bv,
4162                                                                             pa_tasks t
4163                   WHERE bv.budget_version_id = bc.budget_version_id
4164                     AND bv.budget_entry_method_code = bem.budget_entry_method_code
4165                     AND bem.entry_level_code = 'M'
4166                     AND t.task_id = (SELECT task_id
4167                                        FROM gms_balances
4168                                       WHERE budget_version_id = bc.budget_version_id
4169                                         AND project_id = bc.project_id
4170                                         AND award_id = bc.award_id
4171                                         AND task_id = bc.task_id
4172                                         AND balance_type = 'BGT'
4173                                         AND ROWNUM = 1))
4174        WHERE bc.packet_id = x_packetid
4175          AND bud_task_id IS NULL
4176 	     AND status_code = 'P';
4177 
4178        -- Added commit for the base bug 3848201
4179        commit;
4180 
4181       -- if the budget entry level = 'M' and budget at TOP TASK
4182       UPDATE gms_bc_packets bc
4183          SET (bc.bud_task_id, bc.top_task_id) =
4184                 (SELECT t.task_id,
4185                         t.top_task_id
4186                    FROM pa_budget_entry_methods bem, gms_budget_versions bv,pa_tasks t
4187                   WHERE bv.budget_version_id = bc.budget_version_id
4188                     AND bv.budget_entry_method_code = bem.budget_entry_method_code
4189                     AND bem.entry_level_code = 'M'
4190                     AND t.task_id = (SELECT task_id
4191                                        FROM gms_balances
4192                                       WHERE task_id = (SELECT top_task_id
4193                                                          FROM pa_tasks
4194                                                         WHERE task_id = bc.task_id)
4195                                         AND budget_version_id = bc.budget_version_id
4196                                         AND project_id = bc.project_id
4197                                         AND award_id = bc.award_id
4198                                         AND balance_type = 'BGT'
4199                                         AND ROWNUM = 1))
4200        WHERE bc.packet_id = x_packetid
4201          AND bud_task_id IS NULL
4202 	     AND status_code = 'P';
4203 
4204        -- Added commit for the base bug 3848201
4205        commit;
4206 
4207 	IF g_debug = 'Y' THEN
4208         	gms_error_pkg.gms_debug('BUDGET_TASK_ID_UPDATE - Update for Entry Level code M Complete ','C');
4209         END IF;
4210 
4211 -- If Bud Task Id is not updated till this point , then update bud_task_id with
4212 -- task_id of expenditure
4213       g_error_stage := 'BUD_TASK_UPD :ELSE';
4214       UPDATE gms_bc_packets bc
4215          SET (bc.bud_task_id, bc.top_task_id) =
4216                 (SELECT t.task_id,
4217                         t.top_task_id
4218                    FROM pa_tasks t
4219                   WHERE t.task_id = bc.task_id)
4220        WHERE bc.packet_id = x_packetid
4221          AND bc.bud_task_id IS NULL
4222 	     AND status_code = 'P'
4223 		 ;
4224 
4225        -- Added commit for the base bug 3848201
4226        commit;
4227 
4228    END budget_task_id_update;
4229 
4230 ----------------------------------------------------------------------------------------------------------
4231 -- Procedure to calulate budgeted resource list id in packet for a budget version, entry level code and
4232 -- budget entry method and update gms_bc_packets for the set of records having the same combinations.
4233 ----------------------------------------------------------------------------------------------------------
4234 
4235    PROCEDURE bud_res_list_id_update (
4236       x_packetid   IN       NUMBER) IS
4237    BEGIN
4238       g_error_procedure_name := 'bud_res_list_id_update';
4239       -- At Resource/Resource Group Level
4240       g_error_stage := 'BUD_RES_UPD :START';
4241 
4242       -- Bug 2605070, Only one stmt is needed to update the parent_resource_id
4243       UPDATE gms_bc_packets gms
4244          SET (parent_resource_id) =
4245                 (SELECT pr.parent_member_id
4246                    FROM pa_resource_list_members pr
4247                   WHERE pr.resource_list_member_id = gms.resource_list_member_id
4248                     AND ROWNUM = 1)
4249        WHERE packet_id = x_packetid
4250        AND   status_code = 'P'
4251        AND   parent_resource_id is NULL
4252        AND   nvl(burden_adjustment_flag,'N') = 'N'; -- 3389292
4253    END bud_res_list_id_update;
4254 
4255 ----------------------------------------------------------------------------------------------------------
4256 -- Procedure to update the funds control level code in a packet for a project, award, budget version,
4257 -- budget entry method.
4258 -- update gms_bc_packets for the set of records having the same combinations.
4259 ----------------------------------------------------------------------------------------------------------
4260    PROCEDURE funds_ctrl_level_code (
4261       x_packet_id   IN       NUMBER) IS
4262 
4263 	fcl_a 		gms_awards_all.fund_control_level_award%type;
4264     fcl_t 		gms_awards_all.fund_control_level_task%type;
4265     fcl_r_grp 	gms_awards_all.fund_control_level_res_grp%type;
4266     fcl_r  		gms_awards_all.fund_control_level_res%type;
4267 
4268     /* 12649316 */
4269     /* 13410272 added distinct to the select query */
4270     CURSOR award_csr (p_packet_id number) is
4271        select distinct award_id
4272        from   gms_bc_packets
4273        where  packet_id = p_packet_id;
4274 
4275    BEGIN
4276       g_error_procedure_name := 'funds_ctrl_level_code';
4277       g_error_stage := 'FUND_CTRL_LEVEL_CODE : A';
4278 -- Award Level
4279       UPDATE gms_bc_packets gms
4280          SET a_funds_control_level_code = (SELECT funds_control_level_code
4281                                              FROM gms_budgetary_controls gbc
4282                                             WHERE gbc.project_id = gms.project_id
4283                                               AND gbc.award_id = gms.award_id
4284                                               AND gbc.task_id IS NULL
4285                                               AND gbc.parent_member_id IS NULL
4286                                               AND gbc.resource_list_member_id IS NULL)
4287        WHERE packet_id = x_packet_id
4288 	     AND status_code = 'P'
4289 	   ;
4290         IF g_debug = 'Y' THEN
4291         	gms_error_pkg.gms_debug('FUNDS_CTRL_LEVEL_CODE - Update for Award Result code Complete ','C');
4292         END IF;
4293         g_error_stage := 'FUND_CTRL_LEVEL_CODE : TT';
4294 -- Top Task Level
4295       UPDATE gms_bc_packets gms
4296          SET tt_funds_control_level_code = (SELECT funds_control_level_code
4297                                               FROM gms_budgetary_controls gbc
4298                                              WHERE gbc.project_id = gms.project_id
4299                                                AND gbc.award_id = gms.award_id
4300                                                AND gbc.task_id = gms.top_task_id
4301                                                AND gbc.parent_member_id IS NULL
4302                                                AND gbc.resource_list_member_id IS NULL)
4303        WHERE packet_id = x_packet_id
4304          AND bud_task_id <> 0
4305 	     AND status_code = 'P'
4306 		 ;
4307 
4308        -- Added commit for the base bug 3848201
4309        commit;
4310 
4311 	IF g_debug = 'Y' THEN
4312         	gms_error_pkg.gms_debug('FUNDS_CTRL_LEVEL_CODE - Update for Top Task Result code Complete ','C');
4313         END IF;
4314         g_error_stage := 'FUND_CTRL_LEVEL_CODE : T';
4315 
4316 -- Task Level
4317       UPDATE gms_bc_packets gms
4318          SET t_funds_control_level_code = (SELECT funds_control_level_code
4319                                              FROM gms_budgetary_controls gbc
4320                                             WHERE gbc.project_id = gms.project_id
4321                                               AND gbc.award_id = gms.award_id
4322                                               AND gbc.task_id = gms.task_id   -- bug 2579619 : gms.bud_task_id
4323                                               AND gbc.parent_member_id IS NULL
4324                                               AND gbc.resource_list_member_id IS NULL)
4325        WHERE packet_id = x_packet_id
4326          AND bud_task_id <> 0
4327 	     AND status_code = 'P'
4328 		 ;
4329 
4330        -- Added commit for the base bug 3848201
4331        commit;
4332 
4333 	IF g_debug = 'Y' THEN
4334         	gms_error_pkg.gms_debug('FUNDS_CTRL_LEVEL_CODE - Update for Task Result code Complete ','C');
4335         END IF;
4336       --Task level funds control level code should set up only if budget entry method is by task
4337       --For project with resource level budget entry method task_id =0
4338 -- Resource Group Level
4339       g_error_stage := 'FUND_CTRL_LEVEL_CODE : RG';
4340       UPDATE gms_bc_packets gms
4341          SET rg_funds_control_level_code = (SELECT funds_control_level_code
4342                                               FROM gms_budgetary_controls gbc
4343                                              WHERE gbc.project_id = gms.project_id
4344                                                AND gbc.award_id = gms.award_id
4345                                                AND gbc.task_id = gms.bud_task_id
4346                                                AND gbc.resource_list_member_id =
4347                                                                              gms.parent_resource_id
4348                                                AND gbc.parent_member_id = 0)
4349        WHERE packet_id = x_packet_id
4350          AND categorization_code <> 'N'
4351 	     AND status_code = 'P'
4352 		 ;
4353 
4354        -- Added commit for the base bug 3848201
4355        commit;
4356 
4357 	IF g_debug = 'Y' THEN
4358         	gms_error_pkg.gms_debug('FUNDS_CTRL_LEVEL_CODE - Update for resource Group Result code Complete ','C');
4359         END IF;
4360 
4361 -- Resource Level
4362       g_error_stage := 'FUND_CTRL_LEVEL_CODE : R';
4363       UPDATE gms_bc_packets gms
4364          SET r_funds_control_level_code = (SELECT funds_control_level_code
4365                                              FROM gms_budgetary_controls gbc
4366                                             WHERE gbc.project_id = gms.project_id
4367                                               AND gbc.award_id = gms.award_id
4368                                               AND gbc.task_id = gms.bud_task_id
4369                                               AND gbc.resource_list_member_id =
4370                                                                         gms.resource_list_member_id)
4371        WHERE packet_id = x_packet_id
4372          AND categorization_code <> 'N'
4373 	     AND status_code = 'P'
4374 		 ;
4375 
4376        -- Added commit for the base bug 3848201
4377        commit;
4378 
4379        IF g_debug = 'Y' THEN
4380        	gms_error_pkg.gms_debug('FUNDS_CTRL_LEVEL_CODE - Update for resource Level Result code Complete ','C');
4381        END IF;
4382 
4383 -- If Funds control level code at any level is null
4384 -- The update it to 'None'
4385        FOR award_rec in award_csr(x_packet_id)
4386        LOOP
4387 		select fund_control_level_award,
4388 	 		fund_control_level_task,
4389 	 		fund_control_level_res_grp,
4390 	 		fund_control_level_res
4391 			into
4392 			fcl_a,
4393             fcl_t,
4394             fcl_r_grp,
4395             fcl_r
4396 		from gms_awards_all
4397 		where award_id = award_rec.award_id;
4398 		/* 12649316 (select award_id
4399 		from gms_bc_packets
4400 		WHERE packet_id = x_packet_id ); */  /* added for bug 12432376 for debugging*/
4401 
4402       g_error_stage := 'FUND_CTRL_LEVEL_CODE : NONE';
4403       UPDATE gms_bc_packets gms
4404          SET r_funds_control_level_code =
4405                           DECODE (r_funds_control_level_code, NULL, nvl(fcl_r,'N'), r_funds_control_level_code), /* added  nvl(fcl_r,'N')for bug 12432376 for debugging*/
4406              rg_funds_control_level_code =
4407                         DECODE (rg_funds_control_level_code, NULL, nvl(fcl_r_grp,'N'), rg_funds_control_level_code),/* added  nvl(fcl_r_grp,'N') for bug 12432376 for debugging*/
4408              t_funds_control_level_code =
4409                           DECODE (t_funds_control_level_code, NULL, nvl(fcl_t,'N'), t_funds_control_level_code),/* added  nvl(fcl_t,'N') for bug 12432376 for debugging*/
4410              tt_funds_control_level_code =
4411                    DECODE (tt_funds_control_level_code, NULL, nvl(fcl_t,'N'), tt_funds_control_level_code),/* added  nvl(fcl_t,'N') for bug 12432376 for debugging*/
4412              a_funds_control_level_code =
4413                           DECODE (a_funds_control_level_code, NULL, nvl(fcl_a,'N'), a_funds_control_level_code)/* added nvl(fcl_a,'N') for bug 12432376 for debugging*/
4414        WHERE packet_id = x_packet_id
4415 	     AND award_id = award_rec.award_id /* 12649316 */
4416 	     AND status_code = 'P'
4417 	   ;
4418       g_error_stage := 'FUND_CTRL_LEVEL_CODE : NONE';
4419       UPDATE gms_bc_packets gms
4420          SET r_funds_control_level_code =
4421                           DECODE (r_funds_control_level_code, NULL, 'N', r_funds_control_level_code),
4422              rg_funds_control_level_code =
4423                         DECODE (rg_funds_control_level_code, NULL, 'N', rg_funds_control_level_code),
4424              t_funds_control_level_code =
4425                           DECODE (t_funds_control_level_code, NULL, 'N', t_funds_control_level_code),
4426              tt_funds_control_level_code =
4427                    DECODE (tt_funds_control_level_code, NULL, 'N', tt_funds_control_level_code),
4428              a_funds_control_level_code =
4429                           DECODE (a_funds_control_level_code, NULL, 'N', a_funds_control_level_code)
4430        WHERE packet_id = x_packet_id
4431 	     AND award_id = award_rec.award_id /* 12649316 */
4432 	     AND status_code = 'P'
4433 	   ;
4434     END LOOP; /* 12649316 */
4435 
4436        -- Added commit for the base bug 3848201
4437        commit;
4438 
4439    END funds_ctrl_level_code;
4440 
4441 ----------------------------------------------------------------------------------------------------------
4442 --Procedure to calculate start and end date for all amount type and boudary code combinations
4443 ----------------------------------------------------------------------------------------------------------
4444    PROCEDURE setup_start_end_date (
4445       x_packetid                IN       NUMBER,
4446       x_bc_packet_id            IN       NUMBER,
4447       x_project_id              IN       gms_bc_packets.project_id%TYPE,
4448       x_award_id                IN       gms_bc_packets.award_id%TYPE,
4449       x_budget_version_id       IN       gms_bc_packets.budget_version_id%TYPE,
4450       x_time_phased_type_code   IN       pa_budget_entry_methods.time_phased_type_code%TYPE,
4451       x_expenditure_item_date   IN       DATE,
4452       x_amount_type             IN       gms_awards.amount_type%TYPE,
4453       x_boundary_code           IN       gms_awards.boundary_code%TYPE,
4454       x_set_of_books_id         IN       gms_bc_packets.set_of_books_id%TYPE,
4455 
4456 --      x_budgeted_task_id         IN       gms_bc_packets.bud_task_id%TYPE,
4457 --      x_bud_res_list_member_id   IN       NUMBER,
4458       x_start_date              OUT NOCOPY      DATE,
4459       x_end_date                OUT NOCOPY      DATE) IS
4460 
4461 -- Variables added to get the budgeted task,budgeted resource list member,start and end date for all amount type
4462 -- and boundary code combinations.
4463       project_start_date     DATE;
4464       project_end_date       DATE;
4465       year_start_date        DATE;
4466       year_end_date          DATE;
4467       pa_period_start_date   DATE;
4468       pa_period_end_date     DATE;
4469       gl_period_start_date   DATE;
4470       gl_period_end_date     DATE;
4471       dr_period_start_date   DATE;
4472       dr_period_end_date     DATE;
4473       gs_start_date          DATE;
4474       gs_end_date            DATE;
4475       gb_end_date            DATE;
4476       exp_date               DATE;
4477 	  x_err_code			 NUMBER;
4478 	  x_err_buff			 VARCHAR2(500);
4479 
4480       x_error_code          VARCHAR2(1);
4481       -- BUG 5529930 11I.GMS:QA:PJMRP3B4: FC FAILURE WHEN PROJECT START PRIOR TO BUDGET STAR
4482       --
4483       l_gb_start_date        DATE ;
4484       l_gb_end_date          DATE ;
4485 
4486    BEGIN
4487       g_error_procedure_name := 'setup_start_end_date';
4488       --  Find the budget start date and budget end date (X_start_date, x_end_date)
4489       -- get project start date and end date
4490       x_err_code := 0; -- initialize error code
4491       IF (   x_time_phased_type_code = 'N'
4492           OR x_amount_type = 'PJTD'
4493           OR x_boundary_code = 'J') THEN
4494          g_error_stage := 'Project Start and End Date';
4495 
4496          SELECT start_date,
4497                 completion_date
4498            INTO project_start_date,
4499                 project_end_date
4500            FROM pa_projects_all
4501           WHERE project_id = x_project_id;
4502 
4503            -- Added for GMS enhancements : Bug : 5583170
4504            -- If time pjhase code is date range then
4505           if x_time_phased_type_code = 'R' then
4506 
4507             SELECT MIN (gb.start_date)
4508               INTO l_gb_start_date
4509               FROM gms_balances gb
4510              WHERE gb.budget_version_id = x_budget_version_id ;
4511 
4512             SELECT MAX (gb.END_date)
4513               INTO l_gb_end_date
4514               FROM gms_balances gb
4515              WHERE gb.budget_version_id = x_budget_version_id  ;
4516 
4517             --
4518             -- BUG 5529930 11I.GMS:QA:PJMRP3B4: FC FAILURE WHEN PROJECT START PRIOR TO BUDGET STAR
4519             --
4520 	    IF project_start_date > l_gb_start_date  then
4521 	       project_start_date := l_gb_start_date ;
4522 	    END IF ;
4523 
4524 	    IF project_end_date < l_gb_end_date  then
4525 	       project_end_date := l_gb_end_date ;
4526 	    END IF ;
4527 	    --
4528 	    -- End of Fix 5529930.
4529 
4530           end if;
4531          -- End of GMS enhancement changes.
4532 
4533          IF (project_end_date IS NULL) THEN
4534 
4535 		  IF nvl(g_budget_version_id,0) <> x_budget_version_id THEN	-- Bug 2092791
4536 
4537             SELECT MAX (end_date) --Bug Fix 1828613 From
4538               INTO gb_end_date
4539               FROM gms_balances
4540              WHERE budget_version_id = x_budget_version_id;
4541             SELECT MAX (expenditure_item_date)
4542               INTO exp_date
4543               FROM gms_bc_packets
4544              WHERE budget_version_id = x_budget_version_id;
4545 
4546 			 g_budget_version_id := x_budget_version_id;
4547 			 g_gb_end_date		 := gb_end_date;
4548 			 g_exp_date			 := exp_date;
4549 
4550 		  ELSE
4551 
4552  		     gb_end_date := g_gb_end_date;	-- Bug 2092791
4553 			 exp_date := g_exp_date;   -- Bug 2092791
4554 		  END IF;	 -- Bug 2092791
4555             IF x_time_phased_type_code IN ('N', 'G', 'R') THEN
4556                SELECT TRUNC (gps.end_date)
4557                  INTO gl_period_end_date
4558                  FROM gl_period_statuses gps
4559                 WHERE gps.application_id = 101
4560                   AND gps.set_of_books_id = x_set_of_books_id
4561                   AND TRUNC (exp_date) BETWEEN gps.start_date AND gps.end_date
4562                   AND gps.adjustment_period_flag = 'N';
4563                IF gl_period_end_date > gb_end_date THEN
4564                   project_end_date := gl_period_end_date;
4565                ELSE
4566                   project_end_date := gb_end_date;
4567                END IF;
4568             ELSIF x_time_phased_type_code = 'P' THEN
4569                SELECT TRUNC (end_date)
4570                  INTO pa_period_end_date
4571                  FROM pa_periods gpa
4572                 WHERE TRUNC (exp_date) BETWEEN gpa.start_date AND gpa.end_date;
4573                IF pa_period_end_date > gb_end_date THEN
4574                   project_end_date := pa_period_end_date;
4575                ELSE
4576                   project_end_date := gb_end_date;
4577                END IF;
4578             END IF;
4579          ELSE
4580             IF x_time_phased_type_code = 'P' THEN
4581                g_error_stage := 'PJTD_J-P';
4582                BEGIN
4583                   SELECT TRUNC (end_date)
4584                     INTO gs_end_date
4585                     FROM pa_periods gpa
4586                    WHERE project_end_date BETWEEN gpa.start_date AND gpa.end_date;
4587                EXCEPTION
4588                   WHEN NO_DATA_FOUND THEN
4589                      SELECT TRUNC (MAX (end_date))
4590                        INTO gs_end_date
4591                        FROM gms_balances
4592                       WHERE budget_version_id = x_budget_version_id;
4593                END;
4594                IF gs_end_date > project_end_date THEN
4595                   project_end_date := gs_end_date;
4596                END IF;
4597             ELSIF x_time_phased_type_code = 'G' THEN
4598                g_error_stage := 'PJTD_J-G';
4599                BEGIN
4600                   SELECT TRUNC (gps.end_date)
4601                     INTO gs_end_date
4602                     FROM gl_period_statuses gps
4603                    WHERE gps.application_id = 101
4604                      AND gps.set_of_books_id = x_set_of_books_id
4605                      AND project_end_date BETWEEN gps.start_date AND gps.end_date
4606                      AND gps.adjustment_period_flag = 'N';
4607                EXCEPTION
4608                   WHEN NO_DATA_FOUND THEN
4609                      SELECT TRUNC (MAX (end_date))
4610                        INTO gs_end_date
4611                        FROM gms_balances
4612                       WHERE budget_version_id = x_budget_version_id;
4613                END;
4614                IF gs_end_date > project_end_date THEN
4615                   project_end_date := gs_end_date;
4616                END IF;
4617             END IF;
4618          END IF; --Bug Fix 1828613 From
4619       END IF;
4620       -- get Financial year start and end dates
4621       IF (   x_amount_type = 'YTD'
4622           OR x_boundary_code = 'Y') THEN
4623          g_error_stage := 'Year Start and End Date';
4624          SELECT gps.year_start_date
4625            INTO year_start_date
4626            FROM gl_period_statuses gps
4627           WHERE gps.application_id = 101
4628             AND gps.set_of_books_id = x_set_of_books_id
4629             AND TRUNC (x_expenditure_item_date) BETWEEN gps.start_date AND gps.end_date
4630             AND gps.adjustment_period_flag = 'N';
4631          year_end_date := ADD_MONTHS (year_start_date, 12) - 1;
4632       END IF;
4633       -- get period start and end dates
4634       IF x_time_phased_type_code = 'G' THEN -- FOR GL period
4635         BEGIN --Added for bug#5474922
4636          g_error_stage := 'Time Phase = G';
4637          SELECT TRUNC (gps.start_date),
4638                 TRUNC (gps.end_date)
4639            INTO gl_period_start_date,
4640                 gl_period_end_date
4641            FROM gl_period_statuses gps
4642           WHERE gps.application_id = 101
4643             AND gps.set_of_books_id = x_set_of_books_id
4644             AND TRUNC (x_expenditure_item_date) BETWEEN gps.start_date AND gps.end_date
4645             AND gps.adjustment_period_flag = 'N';
4646        EXCEPTION --Added for bug#5474922
4647          WHEN NO_DATA_FOUND THEN
4648             gl_period_start_date := NULL;
4649             gl_period_end_date := NULL;
4650         END;
4651       ELSIF x_time_phased_type_code = 'P' THEN -- FOR PA period
4652          g_error_stage := 'Time Phase = P';
4653          SELECT TRUNC (start_date),
4654                 TRUNC (end_date)
4655            INTO pa_period_start_date,
4656                 pa_period_end_date
4657            FROM pa_periods gpa
4658           WHERE TRUNC (x_expenditure_item_date) BETWEEN gpa.start_date AND gpa.end_date;
4659       ELSIF x_time_phased_type_code = 'R' THEN -- FOR DATE RANGE
4660          g_error_stage := 'Time Phase R';
4661          /* ====================================================
4662          || The following code is the new logic for end_date
4663          || calc for date range and having boundary_code of
4664          || Period Bug 1622190
4665          ====================================================*/
4666          SELECT TRUNC (MAX (start_date)),
4667                 TRUNC (MIN (end_date))
4668            INTO dr_period_start_date,
4669                 dr_period_end_date
4670            FROM gms_balances
4671           WHERE project_id = x_project_id
4672             AND budget_version_id = x_budget_version_id
4673             AND award_id = x_award_id
4674             AND balance_type <> 'BGT'
4675             AND TRUNC (x_expenditure_item_date) BETWEEN start_date AND end_date;
4676          IF dr_period_start_date IS NULL THEN
4677             SELECT TRUNC (MAX (start_date)),
4678                    TRUNC (MIN (end_date))
4679               INTO dr_period_start_date,
4680                    dr_period_end_date
4681               FROM gms_balances
4682              WHERE project_id = x_project_id
4683                AND budget_version_id = x_budget_version_id
4684                AND award_id = x_award_id
4685                AND balance_type = 'BGT'
4686                AND TRUNC (x_expenditure_item_date) BETWEEN start_date AND end_date;
4687          END IF;
4688          IF dr_period_start_date IS NULL THEN
4689             SELECT TRUNC (gps.start_date),
4690                    TRUNC (gps.end_date)
4691               INTO dr_period_start_date,
4692                    dr_period_end_date
4693               FROM gl_period_statuses gps
4694              WHERE gps.application_id = 101
4695                AND gps.set_of_books_id = x_set_of_books_id
4696                AND TRUNC (x_expenditure_item_date) BETWEEN gps.start_date AND gps.end_date
4697                AND gps.adjustment_period_flag = 'N';
4698          END IF; --Bug 1622190
4699       END IF;
4700       -- Find the x_start_date and x_end_date
4701       IF x_time_phased_type_code = 'N' THEN -- for no time phase
4702          IF (   x_amount_type <> 'PJTD'
4703              OR x_boundary_code <> 'J') THEN
4704             gms_error_pkg.gms_message (
4705                'GMS_INVALID_AMT_TYPE_BND_CODE',
4706                'TIME_PHASED_CODE',
4707                x_time_phased_type_code,
4708                'AMOUNT_TYPE',
4709                x_amount_type,
4710                'BOUNDARY_CODE',
4711                x_boundary_code,
4712                x_exec_type=> 'C',
4713                x_err_code=> x_err_code,
4714                x_err_buff=> x_err_buff);
4715 
4716             Select decode(g_mode,'C','F','R') into x_error_code from dual;
4717 
4718             result_status_code_update (
4719                p_status_code=>x_error_code,
4720                p_result_code=> 'F78',
4721                p_packet_id=> x_packetid,
4722                p_bc_packet_id=> x_bc_packet_id);
4723 
4724             IF g_debug = 'Y' THEN
4725             	gms_error_pkg.gms_debug ('For time phase = N it should be project to date project', 'C');
4726             END IF;
4727          ELSE
4728             x_start_date := project_start_date;
4729             x_end_date := project_end_date;
4730          END IF;
4731       ELSIF x_time_phased_type_code IN ('P', 'G', 'R') THEN
4732          --Project to Date Start and End Date Calculations
4733            -- start date calc - PJTD
4734          IF x_amount_type = 'PJTD' THEN
4735             IF x_time_phased_type_code = 'P' THEN
4736                g_error_stage := 'PJTD1';
4737                BEGIN
4738                   SELECT TRUNC (start_date)
4739                     INTO gs_start_date
4740                     FROM pa_periods gpa
4741                    WHERE project_start_date BETWEEN gpa.start_date AND gpa.end_date;
4742                EXCEPTION
4743                   WHEN NO_DATA_FOUND THEN
4744                      SELECT TRUNC (MIN (start_date))
4745                        INTO gs_start_date
4746                        FROM gms_balances
4747                       WHERE project_id = x_project_id
4748                         AND award_id = x_award_id
4749                         AND budget_version_id = x_budget_version_id
4750                         AND balance_type = 'BGT';
4751                END;
4752             ELSIF x_time_phased_type_code = 'G' THEN
4753                g_error_stage := 'PJTD2';
4754                BEGIN
4755                   SELECT TRUNC (gps.start_date)
4756                     INTO gs_start_date
4757                     FROM gl_period_statuses gps
4758                    WHERE gps.application_id = 101
4759                      AND gps.set_of_books_id = x_set_of_books_id
4760                      AND project_start_date BETWEEN gps.start_date AND gps.end_date
4761                      AND gps.adjustment_period_flag = 'N';
4762                EXCEPTION
4763                   WHEN NO_DATA_FOUND THEN
4764                      SELECT TRUNC (MIN (start_date))
4765                        INTO gs_start_date
4766                        FROM gms_balances
4767                       WHERE project_id = x_project_id
4768                         AND award_id = x_award_id
4769                         AND budget_version_id = x_budget_version_id
4770                         AND balance_type = 'BGT';
4771                END;
4772             ELSIF x_time_phased_type_code = 'R' THEN
4773                g_error_stage := 'PJTD3';
4774                SELECT TRUNC (MIN (start_date))
4775                  INTO gs_start_date
4776                  FROM gms_balances
4777                 WHERE project_id = x_project_id
4778                   AND award_id = x_award_id
4779                   AND budget_version_id = x_budget_version_id
4780                   AND balance_type = 'BGT';
4781             END IF;
4782             IF gs_start_date < project_start_date THEN
4783                x_start_date := gs_start_date;
4784             ELSE
4785                x_start_date := project_start_date;
4786             END IF;
4787             -- end date calc for PJTD - Project
4788             IF x_boundary_code = 'J' THEN
4789 
4790 -- ------------------------------------------------------------------------------------
4791 -- Bug Fix 1828613 the above portion has been commented out NOCOPY replaced by the code below.
4792 -- ------------------------------------------------------------------------------------
4793                IF x_time_phased_type_code IN ('P', 'G', 'R') THEN
4794                   x_end_date := project_end_date;
4795                END IF;
4796             -- end date calc for PJTD - Year
4797             ELSIF x_boundary_code = 'Y' THEN
4798                IF x_time_phased_type_code = 'P' THEN
4799                   g_error_stage := 'PJTD-Y-P';
4800                   BEGIN
4801                      SELECT p.end_date
4802                        INTO gs_end_date
4803                        FROM pa_periods p
4804                       WHERE year_end_date BETWEEN p.start_date AND p.end_date;
4805                      x_end_date := gs_end_date;
4806                   EXCEPTION
4807                      WHEN NO_DATA_FOUND THEN
4808                         x_end_date := year_end_date;
4809                   END;
4810                ELSIF x_time_phased_type_code = 'G' THEN
4811                   g_error_stage := 'PJTD-Y-G';
4812                   x_end_date := year_end_date;
4813                ELSIF x_time_phased_type_code = 'R' THEN
4814                   g_error_stage := 'PJTD-Y-R';
4815                   x_end_date := year_end_date;
4816                END IF;
4817             -- end date calc for PJTD - period
4818             ELSIF x_boundary_code = 'P' THEN
4819                g_error_stage := 'PJTD-P';
4820                IF x_time_phased_type_code = 'P' THEN
4821                   x_end_date := pa_period_end_date;
4822                ELSIF x_time_phased_type_code = 'G' THEN
4823                   x_end_date := gl_period_end_date;
4824                ELSIF x_time_phased_type_code = 'R' THEN
4825                   x_end_date := dr_period_end_date;
4826                END IF;
4827             -- end date calc for PJTD - period
4828             ELSE
4829                gms_error_pkg.gms_message (
4830                   'GMS_INVALID_AMT_TYPE_BND_CODE',
4831                   'TIME_PHASED_CODE',
4832                   x_time_phased_type_code,
4833                   'AMOUNT_TYPE',
4834                   x_amount_type,
4835                   'BOUNDARY_CODE',
4836                   x_boundary_code,
4837                   x_exec_type=> 'C',
4838                   x_err_code=> x_err_code,
4839                   x_err_buff=> x_err_buff);
4840                IF g_debug = 'Y' THEN
4841                	gms_error_pkg.gms_debug ('invalid end date for PJTD ', 'C');
4842                END IF;
4843             END IF;
4844          -- Year to Date - start and End date calculation
4845             -- start date calc - YTD
4846          ELSIF x_amount_type = 'YTD' THEN
4847             IF x_time_phased_type_code = 'P' THEN
4848                g_error_stage := 'YTD1';
4849                BEGIN
4850                   SELECT p.start_date
4851                     INTO gs_start_date
4852                     FROM pa_periods p
4853                    WHERE year_start_date BETWEEN p.start_date AND p.end_date;
4854                   x_start_date := gs_start_date;
4855                EXCEPTION
4856                   WHEN NO_DATA_FOUND THEN
4857                      x_start_date := year_start_date;
4858                END;
4859             ELSIF x_time_phased_type_code = 'G' THEN
4860                g_error_stage := 'YTD2';
4861                x_start_date := year_start_date;
4862             ELSIF x_time_phased_type_code = 'R' THEN
4863                g_error_stage := 'YTD3';
4864                x_start_date := year_start_date;
4865             END IF;
4866             -- end date calc for YTD - year
4867             IF x_boundary_code = 'Y' THEN
4868                IF x_time_phased_type_code = 'P' THEN
4869                   g_error_stage := 'YTD-Y-P';
4870                   BEGIN
4871                      SELECT p.end_date
4872                        INTO gs_end_date
4873                        FROM pa_periods p
4874                       WHERE year_end_date BETWEEN p.start_date AND p.end_date;
4875                      x_end_date := gs_end_date;
4876                   EXCEPTION
4877                      WHEN NO_DATA_FOUND THEN
4878                         x_end_date := year_end_date;
4879                   END;
4880                ELSIF x_time_phased_type_code = 'G' THEN
4881                   g_error_stage := 'YTD-Y-G';
4882                   x_end_date := year_end_date;
4883                ELSIF x_time_phased_type_code = 'R' THEN
4884                   g_error_stage := 'YTD-Y-R';
4885                   x_end_date := year_end_date;
4886                END IF;
4887             -- end date calc for YTD - period
4888             ELSIF x_boundary_code = 'P' THEN
4889                g_error_stage := 'YTD-P';
4890                IF x_time_phased_type_code = 'P' THEN
4891                   x_end_date := pa_period_end_date;
4892                ELSIF x_time_phased_type_code = 'G' THEN
4893                   x_end_date := gl_period_end_date;
4894                ELSIF x_time_phased_type_code = 'R' THEN
4895                   x_end_date := dr_period_end_date;
4896                END IF;
4897             ELSE
4898                gms_error_pkg.gms_message (
4899                   'GMS_INVALID_AMT_TYPE_BND_CODE',
4900                   'TIME_PHASED_CODE',
4901                   x_time_phased_type_code,
4902                   'AMOUNT_TYPE',
4903                   x_amount_type,
4904                   'BOUNDARY_CODE',
4905                   x_boundary_code,
4906                   x_exec_type=> 'C',
4907                   x_err_code=> x_err_code,
4908                   x_err_buff=> x_err_buff);
4909                IF g_debug = 'Y' THEN
4910                	gms_error_pkg.gms_debug ('invalid end date for year to date year', 'C');
4911                END IF;
4912             END IF;
4913          --For Period to Date Period
4914          ELSIF x_amount_type = 'PTD' THEN
4915             IF x_boundary_code = 'P' THEN
4916                g_error_stage := 'PTD-P';
4917                IF x_time_phased_type_code = 'P' THEN
4918                   x_start_date := pa_period_start_date;
4919                   x_end_date := pa_period_end_date;
4920                ELSIF x_time_phased_type_code = 'G' THEN
4921                   x_start_date := gl_period_start_date;
4922                   x_end_date := gl_period_end_date;
4923                ELSIF x_time_phased_type_code = 'R' THEN
4924                   x_start_date := dr_period_start_date;
4925                   x_end_date := dr_period_end_date;
4926                END IF;
4927             ELSE
4928                gms_error_pkg.gms_message (
4929                   'GMS_INVALID_AMT_TYPE_BND_CODE',
4930                   'TIME_PHASED_CODE',
4931                   x_time_phased_type_code,
4932                   'AMOUNT_TYPE',
4933                   x_amount_type,
4934                   'BOUNDARY_CODE',
4935                   x_boundary_code,
4936                   x_exec_type=> 'C',
4937                   x_err_code=> x_err_code,
4938                   x_err_buff=> x_err_buff);
4939                IF g_debug = 'Y' THEN
4940                	gms_error_pkg.gms_debug ('invalid end date for period to date', 'C');
4941                END IF;
4942             END IF;
4943          ELSE
4944             gms_error_pkg.gms_message (
4945                'GMS_INVALID_AMT_TYPE_BND_CODE',
4946                'TIME_PHASED_CODE',
4947                x_time_phased_type_code,
4948                'AMOUNT_TYPE',
4949                x_amount_type,
4950                'BOUNDARY_CODE',
4951                x_boundary_code,
4952                x_exec_type=> 'C',
4953                x_err_code=> x_err_code,
4954                x_err_buff=> x_err_buff);
4955             IF g_debug = 'Y' THEN
4956             	gms_error_pkg.gms_debug ('invalid start date for any combination', 'C');
4957             END IF;
4958          END IF;
4959       ELSE
4960          gms_error_pkg.gms_message (
4961             'GMS_INVALID_AMT_TYPE_BND_CODE',
4962             'TIME_PHASED_CODE',
4963             x_time_phased_type_code,
4964             'AMOUNT_TYPE',
4965             x_amount_type,
4966             'BOUNDARY_CODE',
4967             x_boundary_code,
4968             x_exec_type=> 'C',
4969             x_err_code=> x_err_code,
4970             x_err_buff=> x_err_buff);
4971          IF g_debug = 'Y' THEN
4972          	gms_error_pkg.gms_debug ('invalid time phased type for any combination', 'C');
4973          END IF;
4974       END IF;
4975 --      gms_error_pkg.gms_debug ('start start end date cal-start date'|| x_start_date, 'C');
4976 --      gms_error_pkg.gms_debug ('start start end date cal-end date'|| x_end_date, 'C');
4977       --('After Date Check Process');
4978    END setup_start_end_date;
4979 
4980 ----------------------------------------------------------------------------------------------------------
4981 -- This Procedure calls setup_start_end_date in a Cursor, For each Record in the Packet
4982 -- The x_Start_date and x_end_date returned by the procedure will be assigned to
4983 -- budget_period_start_date and budget_period_end_date
4984 -- Due to performance issues the Procedure setup_start_end_date is called for only Raw Transactions only
4985 -- The same start_date and end_date is used to update the Burden Lines also.
4986 -- Here BULK Update of Oracle 8i is Used for Performance issues
4987 ----------------------------------------------------------------------------------------------------------
4988 
4989    PROCEDURE call_start_end_date_update (
4990       x_packetid   IN       NUMBER ,
4991       p_mode       IN       VARCHAR2) IS
4992       TYPE t_packetid IS TABLE OF gms_bc_packets.packet_id%TYPE;
4993       TYPE t_bcpktid  IS TABLE OF gms_bc_packets.bc_packet_id%TYPE;
4994       TYPE t_projid	  IS TABLE OF gms_bc_packets.project_id%TYPE;
4995       TYPE t_awardid  IS TABLE OF gms_bc_packets.award_id%TYPE;
4996       TYPE t_bvid 	  IS TABLE OF gms_bc_packets.budget_version_id%TYPE;
4997       TYPE t_tptypecd IS TABLE OF gms_bc_packets.time_phased_type_code%TYPE;
4998       TYPE t_expdate  IS TABLE OF gms_bc_packets.expenditure_item_date%TYPE;
4999       TYPE t_amttype  IS TABLE OF gms_bc_packets.amount_type%TYPE;
5000       TYPE t_boudrcd  IS TABLE OF gms_bc_packets.boundary_code%TYPE;
5001       TYPE t_sobid	  IS TABLE OF gms_bc_packets.set_of_books_id%TYPE;
5002       TYPE t_startdt  IS TABLE OF DATE;
5003       TYPE t_enddt	  IS TABLE OF DATE;
5004       TYPE t_errcode  IS TABLE OF NUMBER;
5005       TYPE t_errcbuff IS TABLE OF VARCHAR2(500);
5006       TYPE t_rowid IS TABLE OF VARCHAR2 (50);
5007 	  t_packet_id     		   t_packetid ;
5008       t_bc_packet_id  		   t_bcpktid ;
5009       t_project_id	  		   t_projid ;
5010       t_award_id	  		   t_awardid ;
5011       t_budget_version_id	   t_bvid ;
5012       t_time_phased_type_code  t_tptypecd ;
5013       t_expenditure_item_date  t_expdate ;
5014       t_amount_type 		   t_amttype ;
5015       t_boundary_code		   t_boudrcd ;
5016       t_set_of_books_id		   t_sobid ;
5017       t_start_date			   t_startdt ;
5018       t_end_date			   t_enddt ;
5019 
5020 	  x_err_code			   NUMBER;
5021 	  x_err_buff			   VARCHAR2(500);
5022 
5023    BEGIN
5024       g_error_procedure_name := 'call_start_end_date_update';
5025       g_error_stage := 'CL_STEND_DATE:BLK COL';
5026          SELECT project_id,
5027                 award_id,
5028                 budget_version_id,
5029                 time_phased_type_code,
5030                 expenditure_item_date,
5031                 amount_type,
5032                 boundary_code,
5033                 set_of_books_id,
5034                 bc_packet_id,
5035 				budget_period_start_date,
5036 				budget_period_end_date
5037                 BULK COLLECT INTO
5038 					       t_project_id,
5039 						   t_award_id,
5040 						   t_budget_version_id,
5041 						   t_time_phased_type_code,
5042 						   t_expenditure_item_date,
5043 						   t_amount_type,
5044 						   t_boundary_code,
5045 						   t_set_of_books_id,
5046 						   t_bc_packet_id,
5047 						   t_start_date,
5048 						   t_end_date
5049                 FROM gms_bc_packets
5050                WHERE packet_id = x_packetid
5051 		     AND status_code = 'P'
5052 				 AND parent_bc_packet_id IS NULL ;
5053 
5054 	  --  Added following variables for Bug 2092791
5055 
5056 	  /*  The Below variables are used in a scenario where project_end_date is NULL
5057 	      instead of recalculating the end_date , calculate it only once for a packet
5058 	  */
5059 
5060 	  	  g_budget_version_id := NULL;
5061 	  	  g_gb_end_date	 := NULL;
5062 		  g_exp_date	:= NULL;
5063 
5064            IF t_bc_packet_id.COUNT > 0 THEN
5065                FOR bcpkt_records IN t_bc_packet_id.FIRST .. t_bc_packet_id.LAST
5066                LOOP
5067             setup_start_end_date (
5068                x_packetid,
5069                t_bc_packet_id (bcpkt_records ),
5070                t_project_id  (bcpkt_records ),
5071                t_award_id  (bcpkt_records ),
5072                t_budget_version_id  (bcpkt_records ),
5073                t_time_phased_type_code  (bcpkt_records ),
5074                t_expenditure_item_date  (bcpkt_records ),
5075                t_amount_type  (bcpkt_records ),
5076                t_boundary_code  (bcpkt_records ),
5077                t_set_of_books_id  (bcpkt_records ),
5078                t_start_date  (bcpkt_records ),
5079                t_end_date  (bcpkt_records ));
5080 			 END LOOP;
5081 
5082 	       -- Added commit for the base bug 3848201
5083                commit;
5084 
5085 		--	END IF; -- Bug 2683607 : Commented , moved the END IF statement in the end
5086 				-- so that all the FOR ALL statements are included in "IF t_bc_packet_id.COUNT > 0" check
5087             g_error_stage := 'CL_STEND_DATE:FORALL R';
5088             FORALL bcpkt_txns IN t_bc_packet_id.FIRST .. t_bc_packet_id.LAST
5089             UPDATE gms_bc_packets
5090                SET status_code = DECODE (
5091                                     t_start_date (bcpkt_txns),
5092                                     NULL, decode(p_mode,'S','E','C','F','R'),
5093                                     DECODE (t_end_date (bcpkt_txns), NULL, decode(p_mode,'S','E','C','F','R'), status_code)),
5094                    result_code = DECODE (
5095                                     t_start_date (bcpkt_txns),
5096                                     NULL, DECODE (
5097                                              t_time_phased_type_code (bcpkt_txns),
5098                                              'R', 'F95',
5099                                              'G', 'F79',
5100                                              'P', 'F73',
5101                                              'F78'),
5102                                     DECODE (
5103                                        t_end_date (bcpkt_txns),
5104                                        NULL, DECODE (
5105                                                 t_time_phased_type_code (bcpkt_txns),
5106                                                 'R', 'F95',
5107                                                 'G', 'F79',
5108                                                 'P', 'F73',
5109                                                 'F78'),
5110                                        result_code)),
5111                    budget_period_start_date = t_start_date (bcpkt_txns),
5112                    budget_period_end_date = t_end_date (bcpkt_txns)
5113              WHERE bc_packet_id = t_bc_packet_id (bcpkt_txns);
5114 
5115            -- Added commit for the base bug 3848201
5116            commit;
5117 
5118 -- Bug 2092791
5119 -- This update statement is written to update the burden line with same budget_period_start_date and
5120 -- budget_period_end_date  as that of raw line . This will restrict the call of date calculation
5121 -- program once for each raw/burden transaction
5122             g_error_stage := 'CL_STEND_DATE:FORALL B';
5123             FORALL bcpkt_txns IN t_bc_packet_id.FIRST .. t_bc_packet_id.LAST
5124             UPDATE gms_bc_packets
5125                SET status_code = DECODE (
5126                                     t_start_date (bcpkt_txns),
5127                                     NULL, decode(p_mode,'S','E','C','F','R'),
5128                                     DECODE (t_end_date (bcpkt_txns), NULL, decode(p_mode,'S','E','C','F','R'), status_code)),
5129                    result_code = DECODE (
5130                                     t_start_date (bcpkt_txns),
5131                                     NULL, DECODE (
5132                                              t_time_phased_type_code (bcpkt_txns),
5133                                              'R', 'F95',
5134                                              'G', 'F79',
5135                                              'P', 'F73',
5136                                              'F78'),
5137                                     DECODE (
5138                                        t_end_date (bcpkt_txns),
5139                                        NULL, DECODE (
5140                                                 t_time_phased_type_code (bcpkt_txns),
5141                                                 'R', 'F95',
5142                                                 'G', 'F79',
5143                                                 'P', 'F73',
5144                                                 'F78'),
5145                                        result_code)),
5146                    budget_period_start_date = t_start_date (bcpkt_txns),
5147                    budget_period_end_date = t_end_date (bcpkt_txns)
5148    		   WHERE parent_bc_packet_id = t_bc_packet_id (bcpkt_txns);
5149 	   END IF; -- Bug 2683607 : Added
5150 
5151        -- Added commit for the base bug 3848201
5152        commit;
5153 
5154    END call_start_end_date_update;
5155    -- Bug 2039271
5156 
5157 ----------------------------------------------------------------------------------------------------------
5158 -- This Procedure locks the gms_concurrency_control table before inserting record into
5159 -- gms_bc_packet_arrival_order, once record is inserted lock is released.
5160 ----------------------------------------------------------------------------------------------------------
5161 
5162    PROCEDURE set_concurrency_tab IS
5163       l_sys_date   DATE;
5164       x_err_code   NUMBER;
5165    BEGIN
5166       g_error_procedure_name := 'set_concurrency_tab';
5167       g_error_stage := 'SET_CONCURR_TAB: START';
5168       SELECT SYSDATE
5169         INTO l_sys_date
5170         FROM DUAL;
5171       SELECT     0
5172             INTO x_err_code
5173             FROM gms_concurrency_control
5174            WHERE process_name = 'GMSFCTRL'
5175       FOR UPDATE;
5176    EXCEPTION
5177       WHEN NO_DATA_FOUND THEN
5178          g_error_stage := 'SET_CONCURR_TAB:NO.D.FND';
5179          LOCK TABLE gms_concurrency_control IN EXCLUSIVE MODE;
5180          INSERT INTO gms_concurrency_control
5181                      (process_name,
5182                       process_key,
5183                       request_id,
5184                       last_update_date,
5185                       last_updated_by,
5186                       created_by,
5187                       creation_date,
5188                       last_update_login)
5189               VALUES ('GMSFCTRL',
5190                       0,
5191                       0,
5192                       l_sys_date,
5193                       -1,
5194                       -1,
5195                       l_sys_date,
5196                       -1);
5197       WHEN OTHERS THEN
5198          g_error_stage := 'SET_CONCURR_TAB:OTH';
5199          RAISE;
5200    END set_concurrency_tab;
5201 
5202 
5203 ----------------------------------------------------------------------------------------------------------
5204 -- This Procedure inserts record in gms_bc_packet_arrival_order, once record is inserted lock is released.
5205 -- from gms_concurrency_control using COMMIT;
5206 
5207 --  gms_bc_packet_arrival_order Table will store the order in which packets
5208 --  have completed there setup and are ready for funds check.
5209 --  This is required becase packets arrived later can pass fundscheck as setup
5210 --  has not been completed for large packets arrived before.
5211 ----------------------------------------------------------------------------------------------------------
5212 
5213 
5214    PROCEDURE insert_arrival_order_seq (
5215       x_packetid   IN       NUMBER,
5216 	  x_mode	   IN		VARCHAR2) IS
5217 
5218 	  x_err_code   			NUMBER;
5219 	  x_arrival_order_seq           NUMBER;	  -- Bug 2176230
5220 
5221    BEGIN
5222 
5223 	  g_error_procedure_name  :=   	'insert_arrival_order_seq';
5224 
5225 	COMMIT;
5226 
5227   IF x_mode IN ('R', 'U', 'C', 'E') THEN
5228 
5229    -- ****** ===================================================
5230    -- Following procedure was defined to resolve missing control
5231    -- record into concurrency table.
5232    -- ****** ===================================================
5233     set_concurrency_tab;
5234     g_error_stage := 'IN ARRIVAL ORD: R,U,C,E';
5235     SELECT     0
5236       INTO x_err_code
5237       FROM gms_concurrency_control
5238      WHERE process_name = 'GMSFCTRL'
5239 	   FOR UPDATE;
5240 
5241 	 -- Bug 2176230
5242 	 SELECT gms_bc_packet_arrival_order_s.NEXTVAL
5243 	   INTO x_arrival_order_seq
5244 	   FROM DUAL;
5245 
5246 
5247 	 -- Bug 2176230 : Commit only in case of check funds mode as gms_bc_packet_arrival_order record
5248 	 --               is not going to be committed,to restrict the accounting of these transactions
5249 	 --               in subsequent packets.
5250 
5251 	 IF x_mode = 'C' THEN
5252 	   COMMIT;
5253 	 END IF;
5254 
5255 	/***********************  GMS Arrival Order Sequence ***************************/
5256 	--*******************************************************************************
5257 	--  Note : The Insert statement below should always be the last statement of the
5258 	--         setup, Before actual funds check happens. Do Not write any code after
5259 	--         this insert statement.
5260 	--*******************************************************************************
5261     g_error_stage := 'IN ARRIVAL ORD: INSRT';
5262             INSERT INTO gms_bc_packet_arrival_order
5263                         (packet_id,
5264                          arrival_seq,
5265                          last_update_date,
5266                          last_updated_by)
5267                  VALUES (x_packetid,
5268                          x_arrival_order_seq,	   -- Bug 2176230
5269                          SYSDATE,
5270                          fnd_global.user_id);
5271 
5272 
5273 	 -- Bug 2176230 : Commit only if mode is not check funds mode This is to enable
5274 	 --               the accounting of these transactions in subsequent packets.
5275 
5276 	  IF nvl(x_mode,'X') <> 'C' THEN
5277 	    COMMIT;
5278 	  END IF;
5279    END IF;
5280    END insert_arrival_order_seq;
5281 
5282 ----------------------------------------------------------------------------------------------------------
5283 -- This Procedure updated the burdened_cost column of gms_bc_packets .
5284 -- For EXP/PO/AP/REQ/ENC the Burdened cost = Raw Cost + Burdne Cost
5285 -- For Re-Costed Expenditures Burdened COst = Sum ( Raw Cost + Burden Cost ) of all the CDL's of that
5286 -- 	   			 			  		   		  expenditure item id.
5287 ----------------------------------------------------------------------------------------------------------
5288 
5289    PROCEDURE update_burdened_cost (
5290       x_packetid   IN       NUMBER ) IS
5291    BEGIN
5292     g_error_procedure_name  :=  'update_burdened_cost';
5293     g_error_stage := 'UPD_BURDN_COST : START';
5294    	-- --------------------------------------
5295     -- UPDATE BURDENED COST ON GMS_BC_PACKETS
5296     -- BURDENED COST = RAW COST + BURDEN COST.
5297     -- --------------------------------------
5298 -- Bug 2092791
5299         UPDATE gms_bc_packets a
5300              SET burdened_cost =
5301                     (SELECT SUM ( NVL(entered_dr,0) - NVL(entered_cr,0) )
5302                        FROM gms_bc_packets b
5303                       WHERE b.packet_id + 0 = a.packet_id /* Bug 5689194 */
5304                         AND b.document_type = a.document_type
5305                         AND b.document_header_id = a.document_header_id
5306                         AND ((b.document_type='EXP')
5307                              OR (b.document_type<>'EXP'  AND
5308                                  b.document_distribution_id = a.document_distribution_id
5309                                  )
5310                              )
5311                     )
5312            WHERE packet_id = x_packetid
5313 		     AND status_code = 'P'
5314 		   ;
5315    END update_burdened_cost;
5316 
5317 ----------------------------------------------------------------------------------------------------------
5318 -- This Function checks for Setup Failure
5319 --
5320 -- In Full Mode    (x_partial = N) If one Transaction failed setup entire Packet is marked at failed
5321 -- In Partila Mode (x_partial = Y) If one Transaction failed setup only that Expenditure is marked with
5322 -- 	  	   				   	       failure.
5323 --								   In Case of partial Mode, care has been taken to modify result_code to
5324 --								   F63/F75/F65 for raw pass burden fail/burden pass raw fail/recosted
5325 --								   Scenarios if any one of the expenditure failed Setup.
5326 ----------------------------------------------------------------------------------------------------------
5327 
5328    FUNCTION check_setup_failure (
5329       x_packetid   IN       NUMBER,
5330 	  x_partial	   IN		VARCHAR2) RETURN BOOLEAN IS
5331 
5332 	  x_err_count  NUMBER;
5333 /*
5334 
5335         -- The Update Statement below will mark the Transactions which are having Setup Data Missing
5336         -- With the result_code as follows
5337         -- F12  : Funds checking Failed becuase of Invalid Budget Version
5338         -- F13  : Funds checking Failed becuase of Invalid Resource List Member
5339         -- F14  : Funds checking Failed becuase of Invalid Budgeted Resource List Member
5340         -- F15  : Funds checking Failed becuase of Invalid Budgeted Task
5341         -- F16  : Funds checking Failed becuase of Invalid Amount Type
5342         -- F17  : Funds checking Failed becuase of Invalid Bondary code
5343         -- F18  : Funds checking Failed becuase of Invalid Parent Resource Member (Obsoleted : bug 2006221)
5344         -- F19  : Funds checking Failed becuase of Invalid Top Task
5345 */
5346 
5347     /* Bug 2006221: 1.  All Decode statements are modified to return proper result_codes
5348 		    2.  Removed decode statement  DECODE(parent_resource_id,NULL,'F18',
5349 			as parent_resource_id can be NULL in case of UNCLASSIFIED Resources
5350 			i.e. for Expenditure Types which are not there in Resource List, so removed
5351 			the check for NULL parent_resource_id
5352     */
5353 
5354     /* Bug 2605070:     Commented out NOCOPY statement DECODE(bud_resource_list_member_id,NULL,'F14',
5355 			as bud_resource_list_member_id is now obsoleted
5356     */
5357 
5358   BEGIN
5359 
5360 	g_error_procedure_name  :=  'check_setup_failure';
5361     g_error_stage := 'CHK SETUP FAIL';
5362 	x_err_count  :=0;
5363 
5364   IF x_partial = 'Y' THEN
5365     g_error_stage := 'CHK SETUP FAIL : Y';
5366          UPDATE gms_bc_packets gms
5367             SET gms.status_code = 'T',
5368                 gms.result_code         = DECODE(budget_version_id,NULL,'F12',
5369 						 DECODE(resource_list_member_id,NULL,'F13',
5370                                                  -- DECODE(bud_resource_list_member_id,NULL,'F14',
5371 						 DECODE(bud_task_id,NULL,'F15',
5372 						 DECODE(amount_type,NULL,'F16',
5373 						 DECODE(boundary_code,NULL,'F17',
5374 						 DECODE(top_task_id,NULL,'F19')))))),
5375                 gms.res_result_code     = DECODE(budget_version_id,NULL,'F12',
5376 						 DECODE(resource_list_member_id,NULL,'F13',
5377                                                  -- DECODE(bud_resource_list_member_id,NULL,'F14',
5378 						 DECODE(bud_task_id,NULL,'F15',
5379 						 DECODE(amount_type,NULL,'F16',
5380 						 DECODE(boundary_code,NULL,'F17',
5381 						 DECODE(top_task_id,NULL,'F19')))))),
5382                 gms.res_grp_result_code = DECODE(budget_version_id,NULL,'F12',
5383 						 DECODE(resource_list_member_id,NULL,'F13',
5384                                                  -- DECODE(bud_resource_list_member_id,NULL,'F14',
5385 						 DECODE(bud_task_id,NULL,'F15',
5386 						 DECODE(amount_type,NULL,'F16',
5387 						 DECODE(boundary_code,NULL,'F17',
5388 						 DECODE(top_task_id,NULL,'F19')))))),
5389                 gms.task_result_code    = DECODE(budget_version_id,NULL,'F12',
5390 						 DECODE(resource_list_member_id,NULL,'F13',
5391                                                  -- DECODE(bud_resource_list_member_id,NULL,'F14',
5392 						 DECODE(bud_task_id,NULL,'F15',
5393 						 DECODE(amount_type,NULL,'F16',
5394 						 DECODE(boundary_code,NULL,'F17',
5395 						 DECODE(top_task_id,NULL,'F19')))))),
5396                 gms.top_task_result_code= DECODE(budget_version_id,NULL,'F12',
5397 						 DECODE(resource_list_member_id,NULL,'F13',
5398                                                  -- DECODE(bud_resource_list_member_id,NULL,'F14',
5399 						 DECODE(bud_task_id,NULL,'F15',
5400 						 DECODE(amount_type,NULL,'F16',
5401 						 DECODE(boundary_code,NULL,'F17',
5402 						 DECODE(top_task_id,NULL,'F19')))))),
5403                 gms.award_result_code   = DECODE(budget_version_id,NULL,'F12',
5404 						 DECODE(resource_list_member_id,NULL,'F13',
5405                                                  -- DECODE(bud_resource_list_member_id,NULL,'F14',
5406 						 DECODE(bud_task_id,NULL,'F15',
5407 						 DECODE(amount_type,NULL,'F16',
5408 						 DECODE(boundary_code,NULL,'F17',
5409 						 DECODE(top_task_id,NULL,'F19'))))))
5410           WHERE gms.packet_id = x_packetid
5411             AND status_code = 'P'
5412             AND (  budget_version_id IS NULL
5413                 OR resource_list_member_id IS NULL
5414                 -- OR bud_resource_list_member_id IS NULL
5415                 OR bud_task_id IS NULL
5416                 OR amount_type IS NULL
5417                 OR boundary_code IS NULL
5418 		OR top_task_id IS NULL
5419 --		OR decode(categorization_code,'R',parent_resource_id,1) IS NULL  commented for bug 2006221
5420 				 );
5421 
5422        -- Added commit for the base bug 3848201
5423        commit;
5424 
5425        -- Call new procedure RAW_BURDEN_FAILURE to handle raw-burden failure ...
5426        RAW_BURDEN_FAILURE(x_packetid,     -- Packet_id
5427                           g_derived_mode, -- Mode
5428                           'ALL'           -- Level
5429                          );
5430 
5431 
5432       -- Call new FUNCTION Full_mode_failure to handle scenario of failing all cdl when one cdl failed ..
5433       If FULL_MODE_FAILURE(x_packetid,     -- Packet_id
5434                            g_mode,         -- Mode , use g_mode
5435                           'ALL'           -- Level
5436                            ) THEN
5437          gms_error_pkg.gms_debug (g_error_procedure_name||':full_mode_failure call completed','C');
5438           -- Note:We're not handling Full mode failures for REQ/PO/AP here ...
5439       End If;
5440 
5441       -- Net zero txn.s failure
5442       If g_doc_type in ('EXP','ENC') then
5443          -- Handle net zero txn.s in the same packet
5444          -- A. Fail reversing line if original line has failed
5445          UPDATE gms_bc_packets bp
5446             SET bp.result_code =  nvl(bp.result_code,'F65'),
5447                 bp.status_code = 'R',
5448 		bp.fc_error_message = decode(bp.fc_error_message,NULL,'CHECK_SETUP_FAILURE - net zero txn. - full mode failure',bp.fc_error_message)
5449           WHERE bp.packet_id            = x_packetid
5450             AND bp.effect_on_funds_code = 'I'
5451             AND bp.result_code          = 'P82'
5452             AND bp.status_code          = 'P'
5453             AND bp.document_header_id   <> bp.adjusted_document_header_id
5454             AND bp.document_type        in ('EXP','ENC')
5455             AND EXISTS (select 1
5456                          from  gms_bc_packets bp1
5457                          where bp1.packet_id          = bp.packet_id
5458                          and   bp1.document_header_id = bp.adjusted_document_header_id
5459                          and   SUBSTR (bp1.result_code, 1, 1) = 'F');
5460 
5461        -- Added commit for the base bug 3848201
5462        commit;
5463 
5464 	 -- B. Fail original line if reversing line has failed
5465          UPDATE gms_bc_packets bp
5466             SET bp.result_code =  nvl(bp.result_code,'F65'),
5467                 bp.status_code = 'R',
5468 		bp.fc_error_message = decode(bp.fc_error_message,NULL,'CHECK_SETUP_FAILURE - original fail as reversing fail-full mode failure',bp.fc_error_message)
5469           WHERE bp.packet_id            = x_packetid
5470             AND bp.effect_on_funds_code = 'I'
5471             AND bp.result_code          = 'P82'
5472             AND bp.status_code          = 'P'
5473             AND bp.document_header_id   = bp.adjusted_document_header_id
5474             AND bp.document_type        in ('EXP','ENC')
5475             AND EXISTS (select 1
5476                          from  gms_bc_packets bp1
5477                          where bp1.packet_id                   = bp.packet_id
5478                          and   bp1.adjusted_document_header_id = bp.document_header_id
5479                          and   SUBSTR (bp1.result_code, 1, 1)  = 'F');
5480 
5481        -- Added commit for the base bug 3848201
5482        commit;
5483 
5484       End If;
5485 
5486 
5487   ELSE    -- full mode
5488     g_error_stage := 'CHK SETUP FAIL : N';
5489          BEGIN
5490             SELECT 1
5491               INTO x_err_count
5492               FROM dual
5493              WHERE EXISTS (SELECT 1
5494                              FROM gms_bc_packets
5495                             WHERE packet_id = x_packetid
5496                               AND (
5497                                         budget_version_id IS NULL
5498                                      OR resource_list_member_id IS NULL
5499                                      -- OR bud_resource_list_member_id IS NULL  -- Bug 2605070
5500                                      OR bud_task_id IS NULL
5501                                      OR amount_type IS NULL
5502                                      OR boundary_code IS NULL
5503 									 OR top_task_id IS NULL
5504 									 OR budget_period_start_date IS NULL
5505 									 OR budget_period_end_date IS NULL
5506 --				     OR decode(categorization_code,'R',parent_resource_id,1) IS NULL   commented for bug 2006221
5507 				  ));
5508 
5509             -- Added commit for the base bug 3848201
5510             commit;
5511 
5512 
5513             UPDATE gms_bc_packets gms
5514             SET gms.status_code = 'T',
5515                 gms.result_code = DECODE(result_code,NULL,DECODE(budget_version_id,NULL,'F12',
5516 						 DECODE(resource_list_member_id,NULL,'F13',
5517                                                  -- DECODE(bud_resource_list_member_id,NULL,'F14',
5518 						 DECODE(bud_task_id,NULL,'F15',
5519 						 DECODE(amount_type,NULL,'F16',
5520 						 DECODE(boundary_code,NULL,'F17',
5521 						 DECODE(top_task_id,NULL,'F19','F65')))))),result_code),
5522                 gms.res_result_code     = DECODE(budget_version_id,NULL,'F12',
5523 						 DECODE(resource_list_member_id,NULL,'F13',
5524                                                  -- DECODE(bud_resource_list_member_id,NULL,'F14',
5525 						 DECODE(bud_task_id,NULL,'F15',
5526 						 DECODE(amount_type,NULL,'F16',
5527 						 DECODE(boundary_code,NULL,'F17',
5528 						 DECODE(top_task_id,NULL,'F19')))))),
5529                 gms.res_grp_result_code = DECODE(budget_version_id,NULL,'F12',
5530 						 DECODE(resource_list_member_id,NULL,'F13',
5531                                                  -- DECODE(bud_resource_list_member_id,NULL,'F14',
5532 						 DECODE(bud_task_id,NULL,'F15',
5533 						 DECODE(amount_type,NULL,'F16',
5534 						 DECODE(boundary_code,NULL,'F17',
5535 						 DECODE(top_task_id,NULL,'F19')))))),
5536                 gms.task_result_code    = DECODE(budget_version_id,NULL,'F12',
5537 						 DECODE(resource_list_member_id,NULL,'F13',
5538                                                  -- DECODE(bud_resource_list_member_id,NULL,'F14',
5539 						 DECODE(bud_task_id,NULL,'F15',
5540 						 DECODE(amount_type,NULL,'F16',
5541 						 DECODE(boundary_code,NULL,'F17',
5542 						 DECODE(top_task_id,NULL,'F19')))))),
5543                 gms.top_task_result_code= DECODE(budget_version_id,NULL,'F12',
5544 						 DECODE(resource_list_member_id,NULL,'F13',
5545                                                  -- DECODE(bud_resource_list_member_id,NULL,'F14',
5546 						 DECODE(bud_task_id,NULL,'F15',
5547 						 DECODE(amount_type,NULL,'F16',
5548 						 DECODE(boundary_code,NULL,'F17',
5549 						 DECODE(top_task_id,NULL,'F19')))))),
5550                 gms.award_result_code   = DECODE(budget_version_id,NULL,'F12',
5551 						 DECODE(resource_list_member_id,NULL,'F13',
5552                                                  -- DECODE(bud_resource_list_member_id,NULL,'F14',
5553 						 DECODE(bud_task_id,NULL,'F15',
5554 						 DECODE(amount_type,NULL,'F16',
5555 						 DECODE(boundary_code,NULL,'F17',
5556 						 DECODE(top_task_id,NULL,'F19'))))))
5557          WHERE  gms.packet_id = x_packetid
5558 	 AND    status_code   = 'P';
5559 
5560        -- Added commit for the base bug 3848201
5561        commit;
5562 
5563 	 IF g_debug = 'Y' THEN
5564          	gms_error_pkg.gms_debug ( 'GMS_SETUP - Setup Failed ', 'C' );
5565          END IF;
5566 
5567 		 RETURN FALSE;
5568 	 EXCEPTION
5569 	     WHEN NO_DATA_FOUND THEN
5570                -- This is a DUMMY Exception that can happen if all the
5571                -- records are proper. That's why null statement below.
5572           NULL;
5573      END;
5574   END IF;
5575   RETURN TRUE;
5576 END check_setup_failure;
5577 
5578 ----------------------------------------------------------------------------------------------------------
5579 /* 								  Funds Check Sequence Updation
5580 
5581    The sequence in which funds check is performed is decided by funds check Sequence
5582    (funds_check_seq) . The Records are arranged in the increasing order of there burdened cost.
5583    The transactions are grouped such that always burden cost follows the raw cost component.
5584    The order by clause used to assign the sequence is
5585    burdened_cost, document_header_id, decode(nvl(entered_dr,0),0,1), document_distribution_id, bc_packet_id.
5586 */
5587 ----------------------------------------------------------------------------------------------------------
5588 
5589 
5590 PROCEDURE update_fc_sequence (
5591       x_packetid    IN       NUMBER) IS
5592 
5593       TYPE t_fcseq	  IS TABLE OF gms_bc_packets.funds_check_seq%TYPE;
5594       TYPE t_rowid IS TABLE OF VARCHAR2 (50);
5595 	  t_row_id	   t_rowid;
5596 	  t_fc_seq	   t_fcseq;
5597       t_count      number; -- fix for bug : 2927485
5598 
5599        Cursor C_result_code is
5600           select 'ALL' rcode from dual union all
5601           select 'P82' rcode from dual
5602          order by 1;
5603 
5604 
5605    BEGIN
5606       g_error_procedure_name := 'update_fc_sequence';
5607       g_error_stage := 'UPD FC SEQ: BLK COLL';
5608       t_count      := 0; -- fix for bug : 2927485
5609 
5610          	gms_error_pkg.gms_debug ( 'update_fc_sequence : Start ', 'C' );
5611 
5612     FOR recs in C_result_code
5613     loop
5614 
5615       IF recs.rcode = 'ALL' then
5616        -- All records except P82
5617 
5618          SELECT   ROWID,
5619 		 		  funds_check_seq
5620 	 BULK COLLECT INTO
5621 	 	  		  t_row_id,
5622 				  t_fc_seq
5623              FROM gms_bc_packets
5624             WHERE packet_id = x_packetid
5625               AND nvl(result_code,'XX') <> 'P82'
5626 
5627 	 ORDER BY ( NVL(entered_dr,0) - NVL(entered_cr,0) ), --added for 10418422
5628 		          --burdened_cost, commented for 10418422
5629                   document_type,
5630                   document_header_id,
5631 				  decode(nvl(entered_dr,0),0,1), --  Bug 2092791
5632 				  								 --	 Added to include credit transaction
5633 				  								 --  first in a Re-costing scenario
5634                   document_distribution_id,
5635                   bc_packet_id;
5636 
5637       Elsif recs.rcode = 'P82' then
5638        -- Only P82 records (net zero transactions)
5639 
5640             If g_bc_packet_has_P82_records = 'Y' then
5641 
5642                t_count := t_row_id.COUNT;
5643                t_row_id.DELETE;
5644                t_fc_seq.DELETE;
5645 
5646                  SELECT   ROWID,
5647         		 		  funds_check_seq
5648         	 BULK COLLECT INTO
5649 	 	          		  t_row_id,
5650     				      t_fc_seq
5651                      FROM gms_bc_packets
5652                     WHERE packet_id = x_packetid
5653                       AND nvl(result_code,'XX') = 'P82'
5654                  ORDER BY adjusted_document_header_id,
5655                                       burdened_cost,
5656                                      bc_packet_id;
5657 
5658             End if;
5659 
5660       end if;
5661 
5662       If t_row_id.COUNT > 0 then
5663 
5664     	FOR i IN t_row_id.FIRST .. t_row_id.LAST
5665 			LOOP
5666 			  IF t_row_id.PRIOR (i) IS NULL THEN
5667 			  	 t_fc_seq(i) := 1;
5668 			  ELSE
5669 			  	 t_fc_seq(i) := t_fc_seq(t_row_id.PRIOR (i))+  t_count  + 1;
5670 			  END IF;
5671 			END LOOP;
5672          g_error_stage := 'UPD FC SEQ: FOR ALL';
5673          FORALL bcpkt_txns IN t_row_id.FIRST .. t_row_id.LAST
5674          UPDATE gms_bc_packets
5675             SET funds_check_seq = t_fc_seq(bcpkt_txns)
5676           WHERE ROWID = t_row_id (bcpkt_txns);
5677      End if;
5678 
5679     end loop;
5680          	gms_error_pkg.gms_debug ( 'update_fc_sequence : End ', 'C' );
5681   END  update_fc_sequence;
5682 
5683    ------------------------------------------------------------------------------------------------------
5684    --   Wrapper process which calls all the setup procedures and functions
5685     /***********************************************************************************************/
5686     /*********************************     FUNDS CHECK SETUP   *************************************/
5687     /***********************************************************************************************/
5688     -------------------------------------------------------------------------------------------------
5689     /* This Function populates setup columns of GMS_BC_PACKETS Table for all the transactions
5690        This  includes populating
5691                 burdened_cost
5692                 resource_list_member_id
5693                 bud_resource_list_member_id
5694                 bud_task_id
5695                 budget_period_start_date
5696                 budget_period_end_date
5697                 funds_check_seq
5698         It Also inserts record in gms_bc_packet_arrival_order Table
5699     */
5700     -------------------------------------------------------------------------------------------------
5701    FUNCTION gms_setup (
5702       x_packetid   IN       NUMBER,
5703       x_mode       IN       VARCHAR2,
5704 	  x_partial	   IN		VARCHAR2,
5705 	  x_err_code   OUT NOCOPY		NUMBER,
5706 	  x_err_buff   OUT NOCOPY 		VARCHAR2)
5707       RETURN BOOLEAN IS
5708 
5709    BEGIN
5710       g_error_procedure_name := 'gms_setup';
5711       g_error_stage := 'GMS_SETUP : START';
5712       IF g_debug = 'Y' THEN
5713       	gms_error_pkg.gms_debug ('GMS_SETUP - Before Burdened Cost Update ', 'C');
5714       END IF;
5715 
5716 	  update_burdened_cost (x_packetid);
5717 	  COMMIT;
5718 
5719 --------------------------------------------------------------------------
5720 -- Set up of effect_on_funds_code and generating resource_list_member_id
5721 --------------------------------------------------------------------------
5722       IF g_debug = 'Y' THEN
5723       	gms_error_pkg.gms_debug ('GMS_SETUP - Before Setup RLMI ', 'C');
5724       END IF;
5725       setup_rlmi (x_packetid, x_mode, x_err_code, x_err_buff);
5726 	  COMMIT;
5727 
5728 -- ------------------------------------------------------------------------
5729 -- Update the budgeted task id
5730 -- in gms bc packets for a project, task, award, budget version
5731 -- ------------------------------------------------------------------------
5732       IF g_debug = 'Y' THEN
5733       	gms_error_pkg.gms_debug ('GMS_SETUP - Before Budgeted Task Update -> packet_id'|| x_packetid, 'C');
5734       END IF;
5735       budget_task_id_update (x_packetid);
5736 	  COMMIT;
5737 
5738       IF g_debug = 'Y' THEN
5739       	gms_error_pkg.gms_debug ('GMS_SETUP - After Budgeted Task Update', 'C');
5740       END IF;
5741 
5742 -- --------------------------------------------------------------------------
5743 -- Update the  Budgeted resource list member id(rlmi)
5744 -- in gms bc packets for a project, task, award, budget version
5745 -- --------------------------------------------------------------------------
5746       IF g_debug = 'Y' THEN
5747       	gms_error_pkg.gms_debug ('GMS_SETUP - Before Budgeted rlmi Update -> packet_id'|| x_packetid, 'C');
5748       END IF;
5749       bud_res_list_id_update (x_packetid);
5750 	  COMMIT;
5751 
5752       IF g_debug = 'Y' THEN
5753       	gms_error_pkg.gms_debug ('GMS_SETUP - After Budgeted rlmi Update', 'C');
5754       END IF;
5755 
5756 -- ---------------------------------------------------------------------------
5757 -- Updating the  Funds control level code
5758 -- in gms bc packets for a project, task, award, budget version
5759 -- ---------------------------------------------------------------------------
5760       IF g_debug = 'Y' THEN
5761       	gms_error_pkg.gms_debug ('GMS_SETUP - Before Funds ctrl code -> packet_id'|| x_packetid, 'C');
5762       END IF;
5763       funds_ctrl_level_code (x_packetid);
5764 	  COMMIT;
5765 
5766 	  IF g_debug = 'Y' THEN
5767 	  	gms_error_pkg.gms_debug ('GMS_SETUP - Before start date end date -> packet_id'|| x_packetid, 'C');
5768 	  END IF;
5769 
5770       call_start_end_date_update (x_packetid,x_mode);
5771 	  COMMIT;
5772       IF g_debug = 'Y' THEN
5773       	gms_error_pkg.gms_debug ('GMS_SETUP - After startdate end date -> packet_id'|| x_packetid, 'C');
5774       END IF;
5775 
5776       IF NOT check_setup_failure (x_packetid, x_partial ) THEN
5777 	     COMMIT;
5778 	  	 RETURN FALSE;
5779 	  END IF;
5780 	  COMMIT;
5781 
5782      -- Bug 2176230 : Shifted the code update_fc_sequence above the insert_arrival_order
5783 
5784      IF x_mode IN ('R', 'U', 'C', 'E') THEN
5785 	  update_fc_sequence(x_packetid);
5786       COMMIT;
5787      END IF;
5788 
5789 
5790       /***********************  GMS Arrival Order Sequence ***************************/
5791 
5792       --*******************************************************************************
5793       --  Note : The Insert insert_arrival_order procedure call  below should always
5794       --    	 be the last statement of the setup, Before actual funds check happens.
5795       --         Do Not write any code after this insert statement.
5796       --*******************************************************************************
5797 
5798       -- Bug 2176230
5799 	  -- ********************************************************************************
5800 	  -- NOTE :- Don't Put Any Commit after this point till gms_fc_process is complete,
5801 	  --         Reason being for funds checking in C Mode (check funds mode)
5802 	  --         transactions in gms_bc_packets should not be accounted by any other
5803 	  --         subsequent packet.
5804 	  -- ********************************************************************************
5805 
5806 	  insert_arrival_order_seq (x_packetid, x_mode);
5807 
5808 --    COMMIT;  Bug 2176230
5809 
5810 -- Bug 2176230 : Shifted the code update_fc_sequence above the insert_arrival_order
5811 /*
5812      IF x_mode IN ('R', 'U', 'C', 'E') THEN
5813 	  update_fc_sequence(x_packetid);
5814       COMMIT;
5815      END IF;
5816 */
5817 
5818       g_error_stage := 'GMS_SETUP : END';
5819       IF g_debug = 'Y' THEN
5820       	gms_error_pkg.gms_debug ( 'GMS_SETUP -After Inserting Records in gms_bc_packet_arrival_order ', 'C' );
5821       END IF;
5822       RETURN TRUE;
5823    END gms_setup;
5824 
5825 /* ------------------------------------------------------------------------------------------------------
5826    This Procedure is called  in case of Budget Submit and Re-Baseline prcoess of Award Budget.
5827    This procedure compares balances from gms_balances table and Records in gms_bc_packets
5828    for that budget_version_id. If any Record fails, control comes out of the loop  and goes
5829    to the exception part where all the values of variable are dumped into the o/p file
5830 
5831    Bug 3681963 : Modified the code of procedure budget_fundscheck consider the previous consumed
5832    balances if range has increased during the execution in loop of res/res grp/task/top task/award level
5833    cursors. Added the order by clause to ensure that shortest range will always be first. Stored
5834    the previous balance and used it in the next level if all other parameters are same and range
5835    has increased.
5836    Not marked the changes with bug no as lot of code changes were done.
5837    ------------------------------------------------------------------------------------------------------ */
5838    PROCEDURE budget_fundscheck (x_packetid NUMBER,x_err_code OUT NOCOPY NUMBER ,x_err_buff OUT NOCOPY VARCHAR2) IS
5839       CURSOR gms_bc_tot_r IS
5840          SELECT   SUM (NVL (entered_dr, 0) - NVL (entered_cr, 0)) r_bc_tot,
5841                   budget_version_id,
5842                   bud_task_id,
5843                   resource_list_member_id,  -- Bug 2605070, Replaced bud_resource_list_member_id with this column
5844                   budget_period_start_date,
5845                   budget_period_end_date
5846              FROM gms_bc_packets
5847             WHERE packet_id = x_packetid
5848               AND status_code = 'P'
5849               AND
5850                   r_funds_control_level_code = 'B'
5851          GROUP BY budget_version_id,
5852                   bud_task_id,
5853                   resource_list_member_id,  -- Bug 2605070, Replaced bud_resource_list_member_id with this column
5854                   budget_period_start_date,
5855                   budget_period_end_date
5856          ORDER BY budget_version_id,
5857                   bud_task_id,
5858                   resource_list_member_id,
5859                   budget_period_start_date,
5860                   budget_period_end_date;
5861 
5862       CURSOR gms_bc_tot_rg IS
5863          SELECT   SUM (NVL (entered_dr, 0) - NVL (entered_cr, 0)) rg_bc_tot,
5864                   budget_version_id,
5865                   bud_task_id,
5866                   parent_resource_id,
5867                   budget_period_start_date,
5868                   budget_period_end_date
5869              FROM gms_bc_packets
5870             WHERE packet_id = x_packetid
5871               AND status_code = 'P'
5872               AND rg_funds_control_level_code = 'B'
5873          GROUP BY budget_version_id,
5874                   bud_task_id,
5875                   parent_resource_id,
5876                   budget_period_start_date,
5877                   budget_period_end_date
5878  	 ORDER BY budget_version_id,
5879                   bud_task_id,
5880                   parent_resource_id,
5881                   budget_period_start_date,
5882                   budget_period_end_date;
5883 
5884       CURSOR gms_bc_tot_t IS
5885          SELECT   SUM (NVL (entered_dr, 0) - NVL (entered_cr, 0)) t_bc_tot,
5886                   budget_version_id,
5887                   bud_task_id,
5888                   budget_period_start_date,
5889                   budget_period_end_date
5890              FROM gms_bc_packets
5891             WHERE packet_id = x_packetid
5892               AND status_code = 'P'
5893               AND t_funds_control_level_code = 'B'
5894          GROUP BY budget_version_id, bud_task_id, budget_period_start_date, budget_period_end_date
5895 	 ORDER BY budget_version_id, bud_task_id, budget_period_start_date, budget_period_end_date;
5896 
5897       CURSOR gms_bc_tot_tt IS
5898          SELECT   SUM (NVL (entered_dr, 0) - NVL (entered_cr, 0)) tt_bc_tot,
5899                   budget_version_id,
5900                   top_task_id,
5901                   budget_period_start_date,
5902                   budget_period_end_date
5903              FROM gms_bc_packets
5904             WHERE packet_id = x_packetid
5905               AND status_code = 'P'
5906               AND tt_funds_control_level_code = 'B'
5907          GROUP BY budget_version_id, top_task_id,
5908                   budget_period_start_date, budget_period_end_date
5909          ORDER BY budget_version_id, top_task_id,
5910                   budget_period_start_date, budget_period_end_date;
5911 
5912       CURSOR gms_bc_tot_a IS
5913          SELECT   SUM (NVL (entered_dr, 0) - NVL (entered_cr, 0)) a_bc_tot,
5914                   budget_version_id,
5915                   budget_period_start_date,
5916                   budget_period_end_date
5917              FROM gms_bc_packets
5918             WHERE packet_id = x_packetid
5919               AND status_code = 'P'
5920               AND a_funds_control_level_code = 'B'
5921          GROUP BY budget_version_id, budget_period_start_date, budget_period_end_date
5922 	 ORDER BY budget_version_id, budget_period_start_date, budget_period_end_date;
5923 
5924 
5925       x_a_bc_tot                   NUMBER (22, 5);
5926       x_r_bc_tot                   NUMBER (22, 5);
5927       x_rg_bc_tot                  NUMBER (22, 5);
5928       x_t_bc_tot                   NUMBER (22, 5);
5929       x_tt_bc_tot                  NUMBER (22, 5);
5930       x_res_list_member_id         gms_bc_packets.resource_list_member_id%TYPE;  -- Bug 2605070
5931       x_bud_task_id                gms_bc_packets.bud_task_id%TYPE;
5932       x_budget_version_id          NUMBER (22, 5);
5933       x_budget_period_start_date   DATE;
5934       x_budget_period_end_date     DATE;
5935       temp                         NUMBER;
5936       x_error_message              VARCHAR2 (2000);
5937       x_parent_resource_id         NUMBER;
5938       x_top_task_id                NUMBER;
5939 
5940       l_balance_available	   NUMBER (22,5):=0;
5941       l_old_start_date		   DATE;
5942       l_old_end_date		   DATE;
5943       l_old_res_list_member_id     gms_bc_packets.resource_list_member_id%TYPE;
5944       l_old_budget_version_id      gms_bc_packets.budget_version_id%TYPE;
5945       l_old_bud_task_id            gms_bc_packets.bud_task_id%TYPE;
5946       l_old_top_task_id            gms_bc_packets.top_task_id%TYPE;
5947       l_old_parent_resource_id     gms_bc_packets.parent_resource_id%TYPE;
5948       l_previous_tot		   NUMBER (22,5):=0;
5949 
5950 
5951    BEGIN
5952       g_error_procedure_name := 'budget_fundscheck';
5953       g_error_stage := 'BUD_FC : START';
5954       -- Resource Level
5955       IF g_debug = 'Y' THEN
5956       	gms_error_pkg.gms_debug ('BUDGET_FUNDSCHECK - PROCESS'|| x_packetid, 'C');
5957       	gms_error_pkg.gms_debug ('BUDGET_FUNDSCHECK - BEFORE RESOURCE LEVEL', 'C');
5958       END IF;
5959       g_error_stage := 'BUD_FC : RES';
5960       OPEN gms_bc_tot_r;
5961       LOOP
5962          FETCH gms_bc_tot_r INTO x_r_bc_tot,
5963                                  x_budget_version_id,
5964                                  x_bud_task_id,
5965                                  x_res_list_member_id,
5966                                  x_budget_period_start_date,
5967                                  x_budget_period_end_date;
5968 
5969 	 EXIT WHEN gms_bc_tot_r%NOTFOUND;
5970 
5971 	 IF NOT (   x_budget_version_id = l_old_budget_version_id
5972 	        AND x_budget_period_start_date <= l_old_start_date
5973 	        AND x_budget_period_end_date >= l_old_end_date
5974 	        AND x_res_list_member_id = l_old_res_list_member_id
5975 		AND x_bud_task_id = l_old_bud_task_id)
5976          THEN
5977   	       l_previous_tot := 0;
5978 	 END IF;
5979 
5980 	 IF NVL(x_r_bc_tot,0) <> 0  THEN
5981 
5982            SELECT NVL(SUM (NVL (budget_period_to_date, 0)),0)
5983 	    INTO l_balance_available
5984 	    FROM gms_balances
5985 	   WHERE budget_version_id = x_budget_version_id
5986 	     AND start_date >= x_budget_period_start_date
5987 	     AND end_date <= x_budget_period_end_date
5988 	     AND resource_list_member_id = x_res_list_member_id
5989 	     AND task_id = x_bud_task_id;
5990 
5991 	   IF l_balance_available < (x_r_bc_tot+l_previous_tot) THEN
5992 	      RAISE no_data_found;
5993 	   END IF;
5994 
5995 	   l_previous_tot		    := l_previous_tot + x_r_bc_tot;
5996 	   l_old_budget_version_id          := x_budget_version_id;
5997            l_old_bud_task_id		    := x_bud_task_id;
5998            l_old_res_list_member_id	    := x_res_list_member_id;
5999            l_old_start_date                 := x_budget_period_start_date;
6000            l_old_end_date                   := x_budget_period_end_date;
6001 
6002          END IF;
6003 
6004       END LOOP;
6005       CLOSE gms_bc_tot_r;
6006       -- Resource Group Level
6007       g_error_stage := 'BUD_FC : RESGRP';
6008 
6009       l_previous_tot	:= 0;
6010 
6011       IF g_debug = 'Y' THEN
6012       	gms_error_pkg.gms_debug ('BUDGET_FUNDSCHECK - BEFORE RESOURCE GROUP LEVEL', 'C');
6013       END IF;
6014       OPEN gms_bc_tot_rg;
6015       LOOP
6016          FETCH gms_bc_tot_rg INTO x_rg_bc_tot,
6017                                   x_budget_version_id,
6018                                   x_bud_task_id,
6019                                   x_parent_resource_id,
6020                                   x_budget_period_start_date,
6021                                   x_budget_period_end_date;
6022          EXIT WHEN gms_bc_tot_rg%NOTFOUND;
6023 
6024 
6025 	 IF NOT (   x_budget_version_id = l_old_budget_version_id
6026 	        AND x_budget_period_start_date <= l_old_start_date
6027 	        AND x_budget_period_end_date >= l_old_end_date
6028 	        AND x_parent_resource_id = l_old_parent_resource_id
6029 		AND x_bud_task_id = l_old_bud_task_id)
6030          THEN
6031   	       l_previous_tot := 0;
6032 	 END IF;
6033 
6034 	 IF NVL(x_rg_bc_tot,0) <> 0  THEN
6035 
6036            SELECT NVL(SUM (NVL (budget_period_to_date, 0)),0)
6037 	    INTO l_balance_available
6038 	    FROM gms_balances
6039 	   WHERE budget_version_id = x_budget_version_id
6040              AND start_date >= x_budget_period_start_date
6041              AND end_date <= x_budget_period_end_date
6042              AND DECODE (
6043 	         parent_member_id,
6044 	         NULL, resource_list_member_id,
6045   	         parent_member_id) = x_parent_resource_id
6046              AND task_id = x_bud_task_id;
6047 
6048 	   IF l_balance_available < (x_rg_bc_tot+l_previous_tot) THEN
6049 	      RAISE no_data_found;
6050 	   END IF;
6051 
6052 	   l_previous_tot		    := l_previous_tot + x_rg_bc_tot;
6053 	   l_old_budget_version_id          := x_budget_version_id;
6054            l_old_bud_task_id		    := x_bud_task_id;
6055            l_old_parent_resource_id	    := x_parent_resource_id;
6056            l_old_start_date                 := x_budget_period_start_date;
6057            l_old_end_date                   := x_budget_period_end_date;
6058 
6059          END IF;
6060 
6061       END LOOP;
6062       CLOSE gms_bc_tot_rg;
6063       -- Task Level
6064       g_error_stage := 'BUD_FC : TASK';
6065 
6066       l_previous_tot	:= 0;
6067 
6068       IF g_debug = 'Y' THEN
6069       	gms_error_pkg.gms_debug ('BUDGET_FUNDSCHECK - BEFORE TASK LEVEL', 'C');
6070       END IF;
6071       OPEN gms_bc_tot_t;
6072       LOOP
6073          FETCH gms_bc_tot_t INTO x_t_bc_tot,
6074                                  x_budget_version_id,
6075                                  x_bud_task_id,
6076                                  x_budget_period_start_date,
6077                                  x_budget_period_end_date;
6078          EXIT WHEN gms_bc_tot_t%NOTFOUND;
6079 
6080 	 IF NOT (   x_budget_version_id = l_old_budget_version_id
6081 	        AND x_budget_period_start_date <= l_old_start_date
6082 	        AND x_budget_period_end_date >= l_old_end_date
6083 		AND x_bud_task_id = l_old_bud_task_id)
6084          THEN
6085   	       l_previous_tot := 0;
6086 	 END IF;
6087 
6088 	 IF NVL(x_t_bc_tot,0) <> 0  THEN
6089 
6090            SELECT NVL(SUM (NVL (budget_period_to_date, 0)),0)
6091 	    INTO l_balance_available
6092 	    FROM gms_balances
6093 	   WHERE budget_version_id = x_budget_version_id
6094              AND start_date >= x_budget_period_start_date
6095              AND end_date <= x_budget_period_end_date
6096              AND task_id = x_bud_task_id;
6097 
6098 	   IF l_balance_available < (x_t_bc_tot+l_previous_tot) THEN
6099 	      RAISE no_data_found;
6100 	   END IF;
6101 
6102 	   l_previous_tot		    := l_previous_tot + x_t_bc_tot;
6103 	   l_old_budget_version_id          := x_budget_version_id;
6104            l_old_bud_task_id		    := x_bud_task_id;
6105            l_old_start_date                 := x_budget_period_start_date;
6106            l_old_end_date                   := x_budget_period_end_date;
6107 
6108          END IF;
6109 
6110       END LOOP;
6111       CLOSE gms_bc_tot_t;
6112       -- Top Task Level
6113       g_error_stage := 'BUD_FC : TOP TASK';
6114 
6115       l_previous_tot	:= 0;
6116 
6117       IF g_debug = 'Y' THEN
6118       	gms_error_pkg.gms_debug ('BUDGET_FUNDSCHECK - BEFORE TOP TASK LEVEL', 'C');
6119       END IF;
6120       OPEN gms_bc_tot_tt;
6121       LOOP
6122          FETCH gms_bc_tot_tt INTO x_tt_bc_tot,
6123                                   x_budget_version_id,
6124                                   x_top_task_id,
6125                                   x_budget_period_start_date,
6126                                   x_budget_period_end_date;
6127          EXIT WHEN gms_bc_tot_tt%NOTFOUND;
6128 
6129 	 IF NOT (   x_budget_version_id = l_old_budget_version_id
6130 	        AND x_budget_period_start_date <= l_old_start_date
6131 	        AND x_budget_period_end_date >= l_old_end_date
6132 		AND x_top_task_id = l_old_top_task_id)
6133          THEN
6134   	       l_previous_tot := 0;
6135 	 END IF;
6136 
6137 	 IF NVL(x_tt_bc_tot,0) <> 0  THEN
6138 
6139            SELECT NVL(SUM (NVL (budget_period_to_date, 0)),0)
6140 	    INTO l_balance_available
6141 	    FROM gms_balances
6142            WHERE budget_version_id = x_budget_version_id
6143 	     AND start_date >= x_budget_period_start_date
6144 	     AND end_date <= x_budget_period_end_date
6145 	     AND DECODE (top_task_id, NULL, task_id, top_task_id) = x_top_task_id;
6146 
6147 	   IF l_balance_available < (x_tt_bc_tot+l_previous_tot) THEN
6148 	      RAISE no_data_found;
6149 	   END IF;
6150 
6151 	   l_previous_tot		    := l_previous_tot + x_tt_bc_tot;
6152 	   l_old_budget_version_id          := x_budget_version_id;
6153            l_old_top_task_id		    := x_top_task_id;
6154            l_old_start_date                 := x_budget_period_start_date;
6155            l_old_end_date                   := x_budget_period_end_date;
6156 
6157          END IF;
6158 
6159       END LOOP;
6160       CLOSE gms_bc_tot_tt;
6161       -- Award Level
6162       g_error_stage := 'BUD_FC : AWARD';
6163 
6164       l_previous_tot	:= 0;
6165 
6166       IF g_debug = 'Y' THEN
6167       	gms_error_pkg.gms_debug ('BUDGET_FUNDSCHECK - BEFORE AWARD LEVEL', 'C');
6168       END IF;
6169       OPEN gms_bc_tot_a;
6170       LOOP
6171 
6172 	 FETCH gms_bc_tot_a INTO x_a_bc_tot,
6173                                  x_budget_version_id,
6174                                  x_budget_period_start_date,
6175                                  x_budget_period_end_date;
6176          EXIT WHEN gms_bc_tot_a%NOTFOUND;
6177 
6178 	 IF NOT (   x_budget_version_id = l_old_budget_version_id
6179 	        AND x_budget_period_start_date <= l_old_start_date
6180 	        AND x_budget_period_end_date >= l_old_end_date
6181 		)
6182          THEN
6183   	       l_previous_tot := 0;
6184 	 END IF;
6185 
6186 	 IF NVL(x_a_bc_tot,0) <> 0  THEN
6187 
6188            SELECT NVL(SUM (NVL (budget_period_to_date, 0)),0)
6189 	    INTO l_balance_available
6190 	    FROM gms_balances
6191 	   WHERE budget_version_id = x_budget_version_id
6192 	     AND start_date >= x_budget_period_start_date
6193 	     AND end_date <= x_budget_period_end_date;
6194 
6195 	   IF l_balance_available < (x_a_bc_tot+l_previous_tot) THEN
6196 	      RAISE no_data_found;
6197 	   END IF;
6198 
6199 	   l_previous_tot		    := l_previous_tot + x_a_bc_tot;
6200            l_old_start_date                 := x_budget_period_start_date;
6201            l_old_end_date                   := x_budget_period_end_date;
6202    	   l_old_budget_version_id          := x_budget_version_id;
6203 
6204          END IF;
6205 
6206       END LOOP;
6207       CLOSE gms_bc_tot_a;
6208       IF g_debug = 'Y' THEN
6209       	gms_error_pkg.gms_debug ('BUDGET_FUNDSCHECK BEFORE P50 UPDATE '|| x_packetid, 'C');
6210       END IF;
6211       UPDATE gms_bc_packets
6212          SET result_code = 'P50',
6213              award_result_code = 'P50',
6214              res_result_code = 'P50',
6215              res_grp_result_code = 'P50',
6216              task_result_code = 'P50',
6217              top_task_result_code = 'P50'
6218        WHERE packet_id = x_packetid
6219          AND status_code = 'P';
6220       IF g_debug = 'Y' THEN
6221       	gms_error_pkg.gms_debug ('BUDGET_FUNDSCHECK - AFTER P50 UPDATE '|| x_packetid, 'C');
6222       END IF;
6223    EXCEPTION
6224       WHEN NO_DATA_FOUND THEN
6225 -- For Resource Level
6226          IF gms_bc_tot_r%ISOPEN THEN
6227 		    g_error_stage := 'BUD_FC FAIL: RES';
6228             gms_error_pkg.gms_message (
6229                x_err_name=> 'GMS_FC_FAIL_AT_BUDG_RES',
6230                x_token_name1=> 'PACKET_ID',
6231                x_token_val1=> x_packetid,
6232                x_token_name2=> 'TASK_ID',
6233                x_token_val2=> x_bud_task_id,
6234                x_token_name3=> 'RESOURCE_LIST_MEMBER_ID',
6235                x_token_val3=> x_res_list_member_id,
6236                x_token_name4=> 'START_DATE',
6237                x_token_val4=> x_budget_period_start_date,
6238                x_token_name5=> 'END_DATE',
6239                x_token_val5=> x_budget_period_start_date,
6240                x_exec_type=> 'C',
6241                x_err_code=> x_err_code,
6242                x_err_buff=> x_err_buff);
6243             UPDATE gms_bc_packets
6244                SET result_code = 'F25',
6245                    award_result_code = 'F25',
6246                    res_result_code = 'F25',
6247                    res_grp_result_code = 'F25',
6248                    task_result_code = 'F25',
6249                    top_task_result_code = 'F25'
6250              WHERE packet_id = x_packetid;
6251             CLOSE gms_bc_tot_r;
6252          END IF;
6253 
6254 -- For Resource Group Level
6255          IF gms_bc_tot_rg%ISOPEN THEN
6256 		    g_error_stage := 'BUD_FC FAIL: RES GRP';
6257             gms_error_pkg.gms_message (
6258                x_err_name=> 'GMS_FC_FAIL_AT_BUDG_RES_GRP',
6259                x_token_name1=> 'PACKET_ID',
6260                x_token_val1=> x_packetid,
6261                x_token_name2=> 'TASK_ID',
6262                x_token_val2=> x_bud_task_id,
6263                x_token_name3=> 'RESOURCE_LIST_MEMBER_ID',
6264                x_token_val3=> x_parent_resource_id,
6265                x_token_name4=> 'START_DATE',
6266                x_token_val4=> x_budget_period_start_date,
6267                x_token_name5=> 'END_DATE',
6268                x_token_val5=> x_budget_period_start_date,
6269                x_exec_type=> 'C',
6270                x_err_code=> x_err_code,
6271                x_err_buff=> x_err_buff);
6272             UPDATE gms_bc_packets
6273                SET result_code = 'F26',
6274                    award_result_code = 'F26',
6275                    res_result_code = 'F26',
6276                    res_grp_result_code = 'F26',
6277                    task_result_code = 'F26',
6278                    top_task_result_code = 'F26'
6279              WHERE packet_id = x_packetid;
6280             CLOSE gms_bc_tot_rg;
6281          END IF;
6282 
6283 -- For Task Level
6284          IF gms_bc_tot_t%ISOPEN THEN
6285 		    g_error_stage := 'BUD_FC FAIL: TASK';
6286             gms_error_pkg.gms_message (
6287                x_err_name=> 'GMS_FC_FAIL_AT_BUDG_TASK',
6288                x_token_name1=> 'PACKET_ID',
6289                x_token_val1=> x_packetid,
6290                x_token_name2=> 'TASK_ID',
6291                x_token_val2=> x_bud_task_id,
6292                x_token_name4=> 'START_DATE',
6293                x_token_val4=> x_budget_period_start_date,
6294                x_token_name5=> 'END_DATE',
6295                x_token_val5=> x_budget_period_start_date,
6296                x_exec_type=> 'C',
6297                x_err_code=> x_err_code,
6298                x_err_buff=> x_err_buff);
6299             UPDATE gms_bc_packets
6300                SET result_code = 'F27',
6301                    award_result_code = 'F27',
6302                    res_result_code = 'F27',
6303                    res_grp_result_code = 'F27',
6304                    task_result_code = 'F27',
6305                    top_task_result_code = 'F27'
6306              WHERE packet_id = x_packetid;
6307             CLOSE gms_bc_tot_t;
6308          END IF;
6309 
6310 -- For Top Task Level
6311          IF gms_bc_tot_tt%ISOPEN THEN
6312 		    g_error_stage := 'BUD_FC FAIL: TOPTASK';
6313             gms_error_pkg.gms_message (
6314                x_err_name=> 'GMS_FC_FAIL_AT_BUDG_TOP_TASK',
6315                x_token_name1=> 'PACKET_ID',
6316                x_token_val1=> x_packetid,
6317                x_token_name2=> 'TOP_TASK_ID',
6318                x_token_val2=> x_top_task_id,
6319                x_token_name4=> 'START_DATE',
6320                x_token_val4=> x_budget_period_start_date,
6321                x_token_name5=> 'END_DATE',
6322                x_token_val5=> x_budget_period_start_date,
6323                x_exec_type=> 'C',
6324                x_err_code=> x_err_code,
6325                x_err_buff=> x_err_buff);
6326              UPDATE gms_bc_packets
6327                SET result_code = 'F28',
6328                    award_result_code = 'F28',
6329                    res_result_code = 'F28',
6330                    res_grp_result_code = 'F28',
6331                    task_result_code = 'F28',
6332                    top_task_result_code = 'F28'
6333              WHERE packet_id = x_packetid;
6334             CLOSE gms_bc_tot_tt;
6335          END IF;
6336 -- For Award Level
6337 
6338          IF gms_bc_tot_a%ISOPEN THEN
6339 		    g_error_stage := 'BUD_FC FAIL: AWARD';
6340             gms_error_pkg.gms_message (
6341                x_err_name=> 'GMS_FC_FAIL_AT_BUDG_AWARD',
6342                x_token_name1=> 'PACKET_ID',
6343                x_token_val1=> x_packetid,
6344                x_token_name4=> 'START_DATE',
6345                x_token_val4=> x_budget_period_start_date,
6346                x_token_name5=> 'END_DATE',
6347                x_token_val5=> x_budget_period_start_date,
6348                x_exec_type=> 'C',
6349                x_err_code=> x_err_code,
6350                x_err_buff=> x_err_buff);
6351             UPDATE gms_bc_packets
6352                SET result_code = 'F29',
6353                    award_result_code = 'F29',
6354                    res_result_code = 'F29',
6355                    res_grp_result_code = 'F29',
6356                    task_result_code = 'F29',
6357                    top_task_result_code = 'F29'
6358              WHERE packet_id = x_packetid;
6359             CLOSE gms_bc_tot_a;
6360          END IF;
6361          fnd_file.put_line (fnd_file.output, x_err_buff);
6362       WHEN OTHERS THEN
6363          RAISE;
6364    END budget_fundscheck;
6365 
6366 --   ===========================================================================
6367 /*
6368    This is the main funds checker process, which does the funds checking at
6369    various level eg. resource, resource group, task , top task and award.
6370    Funds are not checked in following cases
6371    		 1.	 If budgetary control setting at that level is 'N'
6372 		 2.	 If entered_dr - entered_cr < 0 ( i.e. for Negative Transaction )
6373 		 3.  If a Transaction fails Funds check at a lower level
6374 		 	 i.e. if a Transaction fails funds check at resource level, This
6375 			 transaction will not be considered for funds checking at higher
6376 			 levels
6377 */
6378 --   ===========================================================================
6379 
6380    FUNCTION gms_fc_process (
6381       x_packetid       IN       gms_bc_packets.packet_id%TYPE,
6382       x_arrival_seq1   IN       gms_bc_packet_arrival_order.packet_id%TYPE,
6383       x_mode	       IN	Char		-- Bug 2176230
6384 	  )
6385       RETURN BOOLEAN IS
6386       x_err_count       NUMBER;
6387       x_error_message   VARCHAR2 (2000);
6388 
6389 	  x_date			DATE;
6390       x_arrival_seq     gms_bc_packet_arrival_order.packet_id%TYPE;
6391    BEGIN
6392 
6393    g_error_procedure_name := 'gms_fc_process';
6394    g_error_stage := 'FC PR : START';
6395    x_err_count := 0;
6396 
6397       SELECT arrival_seq
6398         INTO x_arrival_seq
6399         FROM gms_bc_packet_arrival_order ao
6400        WHERE ao.packet_id = x_packetid;
6401 
6402 -- ==============================================================================
6403 -- *********************  RESOURCE LEVEL SUMMARY UPDATE  ************************
6404 -- ==============================================================================
6405 -- Bug 2092791
6406 -- Following Insert statement inserts records in gms_bc_packets_summary.
6407 -- Records in this table will later be used to summarize amount at
6408 -- resource,task and Award Level for previous and currne packet.
6409  IF g_debug = 'Y' THEN
6410  	gms_error_pkg.gms_debug ('RESOURCE LEVEL - SUMMARY INSERT ', 'C');
6411  END IF;
6412 	  x_date := sysdate;
6413       g_error_stage := 'FC PR : INSRT SUMM';
6414 	  INSERT INTO gms_bc_packets_bvid
6415       		       (packet_id,
6416              	   budget_version_id,
6417 				   creation_date)
6418 				      SELECT DISTINCT x_packetid,
6419                    	  		 		  budget_version_id,
6420 									  x_date
6421 						FROM gms_bc_packets
6422 					   WHERE packet_id = x_packetid
6423 					   	 AND status_code = 'P' 	  -- This is to ignore Transactions which failed during setup.
6424 					   ;
6425 
6426       -- Bug 4053891   Do not change code flow ..as lock_budget_versions uses gms_bc_packets_bvid
6427       -- Costing and Funds check is incompatible to Sweeper. 'EXP' has been added to the list for
6428       -- interface (VI->EXP)
6429 
6430       If g_doc_type in ('REQ','PO','AP','FAB','EXP') then
6431          LOCK_BUDGET_VERSIONS(x_packetid);
6432       End If;
6433 
6434            -- Bug 2605070, Replaced bud_resource_list_member_id with resource_list_member_id
6435 	   INSERT INTO gms_bc_packets_summary
6436             (packet_id,
6437 			 creation_date,
6438              budget_version_id,
6439 			 top_task_id,
6440              bud_task_id,
6441 			 parent_resource_id,
6442              resource_list_member_id,
6443              budget_period_start_date,
6444              budget_period_end_date,
6445              actual_approved,
6446              actual_pending,
6447              enc_approved,
6448              enc_pending)
6449      SELECT
6450             x_packetid,
6451 			x_date,
6452             bcpkt.budget_version_id,
6453 			bcpkt.top_task_id,
6454             bcpkt.bud_task_id,
6455 			bcpkt.parent_resource_id,
6456             bcpkt.resource_list_member_id,
6457             bcpkt.budget_period_start_date,
6458             bcpkt.budget_period_end_date,
6459             nvl(sum(decode(bcpkt.status_code || bcpkt.actual_flag, 'AA',nvl(entered_dr,0) - nvl(entered_cr,0),0)),0),
6460             nvl(sum(decode(bcpkt.status_code || bcpkt.actual_flag, 'PA',nvl(entered_dr,0) - nvl(entered_cr,0),0)),0),
6461             nvl(sum(decode(bcpkt.status_code || bcpkt.actual_flag, 'AE',nvl(entered_dr,0) - nvl(entered_cr,0),0)),0),
6462             nvl(sum(decode(bcpkt.status_code || bcpkt.actual_flag, 'PE',nvl(entered_dr,0) - nvl(entered_cr,0),0)),0)
6463        FROM gms_bc_packets bcpkt,
6464 	   		gms_bc_packet_arrival_order ao,
6465             gms_bc_packets_bvid a
6466       WHERE bcpkt.status_code IN ('A', 'P')
6467         AND bcpkt.budget_version_id = a.budget_version_id
6468 		AND bcpkt.packet_id = ao.packet_id
6469 		AND a.packet_id = x_packetid
6470 		AND ao.arrival_seq <= x_arrival_seq
6471         AND decode(bcpkt.status_code,
6472                    'A',1,
6473                    'P',decode(SIGN(NVL(bcpkt.entered_dr,0)-NVL(bcpkt.entered_cr,0)),
6474                               -1,decode(bcpkt.packet_id,x_packetid,1,0),
6475                               1)) = 1
6476    GROUP BY bcpkt.budget_version_id,
6477    		 	bcpkt.top_task_id,
6478             bcpkt.bud_task_id,
6479 			bcpkt.parent_resource_id,
6480             bcpkt.resource_list_member_id,
6481             bcpkt.budget_period_start_date,
6482             bcpkt.budget_period_end_date;
6483 
6484 -- ==============================================================================
6485 -- ************************* RESOURCE LEVEL FUNDS CHECK *************************
6486 -- ==============================================================================
6487 --===============================================================================
6488 -- 						RESOURCE LEVEL : POSTED BALANCE UPDATE
6489 --===============================================================================
6490       IF g_debug = 'Y' THEN
6491       	gms_error_pkg.gms_debug ('RESOURCE POSTED BALANCE UPDATE', 'C');
6492       END IF;
6493       g_error_stage := 'FC PR : RES P BAL';
6494       UPDATE gms_bc_packets bp
6495          SET (bp.res_budget_posted, bp.res_actual_posted, bp.res_enc_posted) =
6496                 (SELECT SUM (NVL (budget_period_to_date, 0) * DECODE (balance_type, 'BGT', 1, 0)),
6497                         SUM (NVL (actual_period_to_date, 0) * DECODE (balance_type, 'EXP', 1, 0)),
6498                         SUM (NVL (encumb_period_to_date, 0) * DECODE (balance_type, 'REQ', 1, 'PO', 1, 'AP', 1, 'ENC', 1, 0))
6499                    FROM gms_balances gb
6500                   WHERE gb.budget_version_id = bp.budget_version_id
6501                     AND gb.project_id = bp.project_id
6502                     AND gb.award_id = bp.award_id
6503                     AND (
6504 			 (bp.bud_task_id = 0) or -- budget at project
6505 			 (bp.bud_task_id > 0 and gb.task_id = bp.bud_task_id and bp.task_id = bp.bud_task_id) or  -- budget at lowest task
6506 			 (bp.bud_task_id > 0 and bp.top_task_id = bp.bud_task_id
6507 			   and DECODE (gb.top_task_id, NULL, gb.task_id, gb.top_task_id) = bp.top_task_id ) -- top task
6508 			) -- 2379815
6509                     AND gb.resource_list_member_id = bp.resource_list_member_id -- Bug 2605070
6510                     AND gb.balance_type <> 'REV'
6511                     AND gb.start_date BETWEEN DECODE (
6512                                                  bp.time_phased_type_code,
6513                                                  'N', gb.start_date,
6514                                                  bp.budget_period_start_date)
6515                                           AND DECODE (
6516                                                  bp.time_phased_type_code,
6517                                                  'N', gb.start_date,
6518                                                  bp.budget_period_end_date)
6519                     AND gb.end_date BETWEEN DECODE (
6520                                                bp.time_phased_type_code,
6521                                                'N', gb.end_date,
6522                                                bp.budget_period_start_date)
6523                                         AND DECODE (
6524                                                bp.time_phased_type_code,
6525                                                'N', gb.end_date,
6526                                                bp.budget_period_end_date))
6527        WHERE bp.packet_id = x_packetid
6528          AND bp.effect_on_funds_code = 'D'
6529          AND bp.status_code = 'P'
6530          AND bp.categorization_code = 'R'
6531          AND bp.r_funds_control_level_code <> 'N';
6532          -- AND bp.budgeted_at_resource_level = 'Y';  -- Bug 2605070
6533       IF g_debug = 'Y' THEN
6534       	gms_error_pkg.gms_debug (
6535          'RESOURCE LEVEL - APPROVED/PENDING BALANCE FOR PREVIOUS AND CURRENT PACKETS',
6536          'C');
6537       END IF;
6538 --===============================================================================
6539 -- 	RESOURCE LEVEL : APPROVED/PENDING BALANCE FOR PREVIOUS AND CURRENT PACKETS
6540 --===============================================================================
6541       IF g_debug = 'Y' THEN
6542       	gms_error_pkg.gms_debug ( 'RESOURCE LEVEL - APPROVED/PENDING BALANCE FOR PREVIOUS AND CURRENT PACKETS','C' );
6543       END IF;
6544  -- Bug 2092791
6545        g_error_stage := 'FC PR : RES A BAL';
6546 		 UPDATE gms_bc_packets bp
6547 		    SET (bp.res_actual_approved, bp.res_actual_pending, bp.res_enc_approved,
6548 			        bp.res_enc_pending) =
6549          			 (SELECT
6550 						  		  SUM(actual_approved),		--Bug 2490381 : Added SUM
6551 							          SUM(actual_pending),		--Bug 2490381 : Added SUM
6552 								  SUM(enc_approved),		--Bug 2490381 : Added SUM
6553 								  SUM(enc_pending)		--Bug 2490381 : Added SUM
6554 							     FROM gms_bc_packets_summary gmsbcs
6555 								WHERE gmsbcs.packet_id = x_packetid
6556 								  AND gmsbcs.bud_task_id = bp.bud_task_id
6557 								  AND gmsbcs.budget_version_id = bp.budget_version_id
6558 								  AND gmsbcs.resource_list_member_id =
6559 		                                                      bp.resource_list_member_id -- Bug 2605070
6560 								  --Bug 2490381 : Changed "=" to "<=" to consider all the
6561 								  --              records from summary table which fall
6562 								  --		  under budget_period_start_date and budget_period_end_date
6563 								  --		  of current transaction.
6564 								  -- Bug 2897560 : changed the strat date comparision to ">="
6565 								  -- as we need to consider all the recods from summary table which fall
6566 								  -- under budget_period_start_date and budget_period_end_date
6567 								  -- of current transaction.
6568 								  AND gmsbcs.budget_period_start_date >= bp.budget_period_start_date
6569 								  AND gmsbcs.budget_period_end_date <= bp.budget_period_end_date)
6570 		  WHERE bp.packet_id = x_packetid
6571             AND bp.effect_on_funds_code = 'D'
6572             AND bp.status_code = 'P'
6573             AND bp.r_funds_control_level_code <> 'N'
6574             AND bp.categorization_code = 'R' ;
6575             -- AND bp.budgeted_at_resource_level = 'Y';  -- Bug 2605070
6576 --===============================================================================
6577 -- 	                RESOURCE LEVEL : AVAILABLE BALANCE UPDATE
6578 --===============================================================================
6579        g_error_stage := 'FC PR : RES AVA BAL';
6580          DECLARE
6581             CURSOR available_bal_at_res IS
6582                SELECT (nvl(entered_dr,0)-nvl(entered_cr,0)) entered_dr,	--Bug 2092791
6583                       ROWID,
6584                       budget_version_id,
6585                       bud_task_id,
6586                       resource_list_member_id, -- Bug 2605070
6587 					  effect_on_funds_code,    -- Bug 2927485
6588                       TRUNC ( budget_period_start_date ) budget_period_start_date,
6589                       TRUNC ( budget_period_end_date ) budget_period_end_date,
6590                       actual_flag
6591                  FROM gms_bc_packets
6592                 WHERE packet_id = x_packetid
6593                   AND effect_on_funds_code in('D','I')	--Bug 2092791
6594                   AND status_code = 'P'
6595                   AND r_funds_control_level_code <> 'N'
6596                   AND categorization_code = 'R'
6597                   -- AND budgeted_at_resource_level = 'Y'  -- Bug 2605070
6598                 ORDER BY budget_version_id,
6599                          bud_task_id,
6600                          resource_list_member_id,
6601                          budget_period_start_date,
6602                          budget_period_end_date,
6603                          funds_check_seq DESC;
6604          x_entered_dr                     NUMBER (22, 5);
6605          x_rowid                          ROWID;
6606          x_pending_actual                 NUMBER         := -11;
6607          x_pending_enc                    NUMBER         := -11;
6608          x_amount                         NUMBER         := -11;
6609          x_budget_version_id_old          NUMBER         := -11;
6610          x_bud_task_id_old                NUMBER         := -11;
6611          x_rlmi_old                       NUMBER         := -11;   -- Bug 2605070
6612          --x_budget_period_start_date_old   DATE           := TO_DATE ('01-01-1720', 'dd-mm-yyyy'); Bug 2276479
6613          --x_budget_period_end_date_old     DATE           := TO_DATE ('01-01-1720', 'dd-mm-yyyy'); Bug 2276479
6614 	 x_budget_period_start_date_old   DATE           := NULL;
6615          x_budget_period_end_date_old     DATE           :=  NULL;
6616 
6617       BEGIN
6618          FOR res_level IN available_bal_at_res
6619          LOOP
6620             IF    res_level.budget_version_id <> x_budget_version_id_old
6621                OR res_level.bud_task_id <> x_bud_task_id_old
6622                OR res_level.resource_list_member_id <> x_rlmi_old  -- Bug 2605070
6623                --OR res_level.budget_period_start_date <> x_budget_period_start_date_old Bug 2276479
6624                --OR res_level.budget_period_end_date <> x_budget_period_end_date_old THEN Bug 2276479
6625 		OR res_level.budget_period_start_date <> nvl(x_budget_period_start_date_old ,res_level.budget_period_start_date + 1 )
6626                	OR res_level.budget_period_end_date <> nvl(x_budget_period_end_date_old , res_level.budget_period_end_date + 1 ) THEN
6627 
6628                IF res_level.actual_flag = 'A' THEN
6629                   x_pending_actual := res_level.entered_dr;
6630                ELSIF res_level.actual_flag = 'E' THEN
6631                   x_pending_enc := res_level.entered_dr;
6632                END IF;
6633                x_budget_version_id_old := res_level.budget_version_id;
6634                x_bud_task_id_old := res_level.bud_task_id;
6635                x_rlmi_old := res_level.resource_list_member_id;  -- Bug 2605070
6636                x_budget_period_start_date_old := res_level.budget_period_start_date;
6637                x_budget_period_end_date_old := res_level.budget_period_end_date;
6638             ELSE
6639              IF nvl(res_level.entered_dr,0)>= 0 AND res_level.effect_on_funds_code = 'D' THEN	--Bug 2092791 and 2927485 (Update pending balance
6640                UPDATE gms_bc_packets                                                            --                if funds are decreasing)
6641                   SET res_actual_pending = NVL (res_actual_pending, 0)
6642                                            - DECODE (
6643                                                 res_level.actual_flag,
6644                                                 'A', NVL (x_pending_actual, 0),
6645                                                 0),
6646                       res_enc_pending = NVL (res_enc_pending, 0)
6647                                         - DECODE (
6648                                              res_level.actual_flag,
6649                                              'E', NVL (x_pending_enc, 0),
6650                                              0)
6651                 WHERE ROWID = res_level.ROWID;
6652 			 END IF;
6653                IF res_level.actual_flag = 'A' THEN
6654                   x_pending_actual := x_pending_actual + res_level.entered_dr;
6655                ELSIF res_level.actual_flag = 'E' THEN
6656                   x_pending_enc := x_pending_enc + res_level.entered_dr;
6657                END IF;
6658             END IF;
6659          END LOOP;
6660       END;
6661 
6662 -- ================================================================================
6663 -- 	                RESOURCE LEVEL : RESULT CODE UPDATE
6664 -- ================================================================================
6665 --	P68	: Transaction pass Funds Check at Resource level
6666 --	P69	: Transaction pass Funds Check at Resource level in advisory
6667 --	P76	: Transaction does not require funds check at this level
6668 --  P78	: Increase Funds does not require Funds Check
6669 --	F75	: Transaction Failed because of Raw
6670 --	F63	: Transaction Failed because of Burden
6671 --	F92	: Transaction failed funds check at Resource level
6672 -- ================================================================================
6673 -- Added decode(bp.result_code,null,'P78',bp.result_code for bug : 2927485
6674       IF g_debug = 'Y' THEN
6675       	gms_error_pkg.gms_debug ('RESULT CODE UPDATE - RESOURCE', 'C');
6676       END IF;
6677       g_error_stage := 'FC PR : RES RESULT CODE';
6678       UPDATE gms_bc_packets bp
6679          SET bp.res_result_code = DECODE (
6680                                      bp.effect_on_funds_code,
6681                                      'I', decode(bp.result_code,null,'P78',bp.result_code) ,
6682                                      'D', DECODE (
6683                                              bp.r_funds_control_level_code,
6684                                              'N', 'P76',
6685                                              'D', DECODE (
6686                                                      SIGN (
6687                                                         NVL (bp.res_budget_posted, 0)
6688                                                         - NVL (bp.res_actual_posted, 0)
6689                                                         - NVL (bp.res_enc_posted, 0)
6690                                                         - NVL (bp.res_actual_approved, 0)
6691                                                         - NVL (bp.res_actual_pending, 0)
6692                                                         - NVL (bp.res_enc_approved, 0)
6693                                                         - NVL (bp.res_enc_pending, 0)),
6694                                                      -1, 'P69',
6695                                                      'P68'),
6696                                              'B', DECODE (
6697                                                      SIGN (
6698                                                         NVL (bp.res_budget_posted, 0)
6699                                                         - NVL (bp.res_actual_posted, 0)
6700                                                         - NVL (bp.res_enc_posted, 0)
6701                                                         - NVL (bp.res_actual_approved, 0)
6702                                                         - NVL (bp.res_actual_pending, 0)
6703                                                         - NVL (bp.res_enc_approved, 0)
6704                                                         - NVL (bp.res_enc_pending, 0)),
6705                                                      -1, 'F92',
6706                                                      'P68')))
6707        WHERE bp.packet_id = x_packetid
6708          AND bp.effect_on_funds_code IN ('D', 'I')
6709          AND bp.status_code = 'P';
6710 
6711 
6712       -- 7. Update all above levels with failure result code
6713       -- a. Propogate resource level to all other levels
6714       UPDATE gms_bc_packets bp
6715          SET bp.res_grp_result_code = res_result_code,
6716              bp.task_result_code = res_result_code,
6717              bp.top_task_result_code = res_result_code,
6718              bp.award_result_code = res_result_code,
6719              bp.result_code = res_result_code,
6720              bp.status_code = DECODE(x_mode,'C','F','R')
6721        WHERE bp.packet_id = x_packetid
6722          AND bp.effect_on_funds_code IN ('D', 'I')
6723          AND bp.status_code = 'P'
6724          AND SUBSTR (bp.res_result_code, 1, 1) = 'F';
6725 
6726        -- Call new procedure RAW_BURDEN_FAILURE to handle raw-burden failure ...
6727        RAW_BURDEN_FAILURE(x_packetid,     -- Packet_id
6728                           g_derived_mode, -- Mode
6729                           'RES'           -- Level
6730                          );
6731 
6732       -- Call new FUNCTION Full_mode_failure to handle full mode and scenario of failing all cdl when one cdl failed ..
6733       If FULL_MODE_FAILURE(x_packetid,     -- Packet_id
6734                         g_mode,         -- Mode , use g_mode
6735                         'RES'           -- Level
6736                          ) then
6737          If g_partial_flag = 'N' then
6738             -- If full mode, transactions have failed .. exit FC
6739             GOTO END_OF_FC_PROCESS;
6740          End if;
6741       End If;
6742 
6743 --===============================================================================
6744 --   	 RESOURCE LEVEL : INSERT NEGATIVE IN SUMMARY FOR FAILED TRANSACTIONS
6745 --===============================================================================
6746    g_error_stage := 'FC PR : RES INSERT NEG';
6747    INSERT INTO gms_bc_packets_summary
6748             (packet_id,
6749     		 creation_date,
6750              budget_version_id,
6751 			 top_task_id,
6752              bud_task_id,
6753 			 parent_resource_id,
6754              budget_period_start_date,
6755              budget_period_end_date,
6756              actual_pending,
6757              enc_pending)
6758 	Select  x_packetid,
6759 	        x_date,
6760 		    budget_version_id,
6761        		top_task_id,
6762 			bud_task_id,
6763 		    parent_resource_id,
6764       		budget_period_start_date,
6765       		budget_period_end_date,
6766        		-1 * nvl(sum(decode(status_code || actual_flag,'RA',nvl(entered_dr,0) - nvl(entered_cr,0),0)),0) ,
6767        		-1 * nvl(sum(decode(status_code || actual_flag,'RE',nvl(entered_dr,0) - nvl(entered_cr,0),0)),0)
6768 	 from   gms_bc_packets
6769 	 where  packet_id = x_packetid
6770 	 and    res_result_code in ('F92','F63','F75','F65')
6771    group by budget_version_id, top_task_id,bud_task_id, parent_resource_id,budget_period_start_date, budget_period_end_date;
6772 
6773    -- Added commit for the base bug 3848201
6774    commit;
6775 
6776 
6777 -- ==============================================================================
6778 -- ********************** RESOURCE GROUP LEVEL FUNDS CHECK **********************
6779 -- ==============================================================================
6780 --===============================================================================
6781 -- 	 			   RESOURCE GROUP LEVEL : POSTED BALANCE UPDATE
6782 --===============================================================================
6783       IF g_debug = 'Y' THEN
6784       	gms_error_pkg.gms_debug ('RESOURCE GROUP POSTED BALANCE UPDATE', 'C');
6785       END IF;
6786       g_error_stage := 'FC PR : RESG P BAL';
6787       UPDATE gms_bc_packets bp
6788          SET (bp.res_grp_budget_posted, bp.res_grp_actual_posted, bp.res_grp_enc_posted) =
6789                 (SELECT SUM (NVL (budget_period_to_date, 0) * DECODE (balance_type, 'BGT', 1, 0)),
6790                         SUM (NVL (actual_period_to_date, 0) * DECODE (balance_type, 'EXP', 1, 0)),
6791                         SUM (NVL (encumb_period_to_date, 0) * DECODE (balance_type, 'REQ', 1, 'PO', 1, 'AP', 1, 'ENC', 1, 0))
6792                    FROM gms_balances gb
6793                   WHERE gb.budget_version_id = bp.budget_version_id
6794                     AND gb.project_id = bp.project_id
6795                     AND gb.award_id = bp.award_id
6796                     AND (
6797 			 (bp.bud_task_id = 0) or -- budget at project
6798 			 (bp.bud_task_id > 0 and gb.task_id = bp.bud_task_id and bp.task_id = bp.bud_task_id) or  -- budget at lowest task
6799 			 (bp.bud_task_id > 0 and bp.top_task_id = bp.bud_task_id
6800 			   and DECODE (gb.top_task_id, NULL, gb.task_id, gb.top_task_id) = bp.top_task_id ) -- top task
6801 			) -- 2379815
6802                     --AND gb.resource_list_member_id = bp.bud_resource_list_member_id
6803                     AND ( (-- gb.balance_type = 'BGT'  and   -- Bug 2605070
6804  			   gb.resource_list_member_id = bp.parent_resource_id  -- Bug 2605070
6805   			   )
6806   			   OR
6807   			  (-- gb.balance_type <> 'BGT' and  -- Bug 2605070
6808    			   gb.parent_member_id = bp.parent_resource_id
6809    			  )
6810 			)
6811                     AND gb.balance_type <> 'REV'
6812                     AND gb.start_date BETWEEN DECODE (
6813                                                  bp.time_phased_type_code,
6814                                                  'N', gb.start_date,
6815                                                  bp.budget_period_start_date)
6816                                           AND DECODE (
6817                                                  bp.time_phased_type_code,
6818                                                  'N', gb.start_date,
6819                                                  bp.budget_period_end_date)
6820                     AND gb.end_date BETWEEN DECODE (
6821                                                bp.time_phased_type_code,
6822                                                'N', gb.end_date,
6823                                                bp.budget_period_start_date)
6824                                         AND DECODE (
6825                                                bp.time_phased_type_code,
6826                                                'N', gb.end_date,
6827                                                bp.budget_period_end_date))
6828        WHERE bp.packet_id = x_packetid
6829          AND bp.effect_on_funds_code = 'D'
6830          AND bp.status_code = 'P'
6831          AND bp.categorization_code = 'R'
6832          AND bp.rg_funds_control_level_code <> 'N' ;
6833          -- AND bp.budgeted_at_resource_level = 'N'; -- Bug 2605070
6834 
6835 --===================================================================================
6836 -- 	RESOURCE GROUP LEVEL : APPROVED/PENDING BALANCE FOR PREVIOUS AND CURRENT PACKETS
6837 --===================================================================================
6838       IF g_debug = 'Y' THEN
6839       	gms_error_pkg.gms_debug ('RESOURCE GROUP LEVEL - APPROVED/PENDING BALANCE FOR CURRENT PACKET', 'C');
6840       END IF;
6841 --  Bug 2092791
6842       g_error_stage := 'FC PR : RESG A/P BAL';
6843 		 UPDATE gms_bc_packets bp
6844 		    SET (bp.res_grp_actual_approved, bp.res_grp_actual_pending, bp.res_grp_enc_approved,
6845 			        bp.res_grp_enc_pending) =
6846          			 (SELECT
6847 					SUM(actual_approved),
6848 					SUM(actual_pending),
6849 					SUM(enc_approved),
6850 					SUM(enc_pending)
6851 				   FROM gms_bc_packets_summary gmsbcs
6852 				  WHERE gmsbcs.packet_id = x_packetid
6853 				    AND gmsbcs.bud_task_id = bp.bud_task_id
6854 				    AND gmsbcs.budget_version_id = bp.budget_version_id
6855 				    AND gmsbcs.parent_resource_id =  bp.parent_resource_id
6856 				    --Bug 2490381 : Changed "=" to "<=" to consider all the
6857 				    --              records from summary table which fall
6858 				    --	 	    under budget_period_start_date and budget_period_end_date
6859 				    --		    of current transaction.
6860 				    -- Bug 2897560 : changed the strat date comparision to ">="
6861 				    -- as we need to consider all the recods from summary table which fall
6862 			            -- under budget_period_start_date and budget_period_end_date
6863 			            -- of current transaction.
6864 	 			    AND gmsbcs.budget_period_start_date >= bp.budget_period_start_date
6865 				    AND gmsbcs.budget_period_end_date <= bp.budget_period_end_date)
6866 		  WHERE bp.packet_id = x_packetid
6867                     AND bp.effect_on_funds_code = 'D'
6868                     AND bp.status_code = 'P'
6869                     AND bp.rg_funds_control_level_code <> 'N'
6870                     AND bp.categorization_code = 'R';
6871 --           AND bp.budgeted_at_resource_level = 'N';
6872 --  Bug 2092791
6873 --===============================================================================
6874 -- 	            RESOURCE GROUP LEVEL : AVAILABLE BALANCE UPDATE
6875 --===============================================================================
6876       g_error_stage := 'FC PR : RESG A BAL';
6877       DECLARE
6878          CURSOR available_bal_at_res_grp IS
6879               SELECT (nvl(entered_dr,0)-nvl(entered_cr,0)) entered_dr,		--Bug 2092791
6880                      ROWID,
6881                      budget_version_id,
6882                      bud_task_id,
6883                      parent_resource_id,
6884                      effect_on_funds_code,    -- Bug 2927485
6885                      TRUNC (budget_period_start_date) budget_period_start_date,
6886                      TRUNC (budget_period_end_date) budget_period_end_date,
6887                      actual_flag
6888                 FROM gms_bc_packets
6889                WHERE packet_id = x_packetid
6890                  AND effect_on_funds_code in ('D','I')  -- Bug 2092791
6891                  AND status_code = 'P'
6892                  AND rg_funds_control_level_code <> 'N'
6893                  AND categorization_code = 'R'
6894             ORDER BY budget_version_id,
6895                      bud_task_id,
6896                      parent_resource_id,
6897                      budget_period_start_date,
6898                      budget_period_end_date,
6899                      funds_check_seq DESC;
6900          x_entered_dr                     NUMBER (22, 5);
6901          x_rowid                          ROWID;
6902          x_pending_actual                 NUMBER         := -11;
6903          x_pending_enc                    NUMBER         := -11;
6904          x_amount                         NUMBER         := -11;
6905          x_budget_version_id_old          NUMBER         := -11;
6906          x_bud_task_id_old                NUMBER         := -11;
6907          x_bud_rlmi_old                   NUMBER         := -11;
6908          x_parent_rlmi_old                NUMBER         := -11;
6909          --x_budget_period_start_date_old   DATE           := TO_DATE ('01-01-1920', 'dd-mm-yyyy'); Bug 2276479
6910          --x_budget_period_end_date_old     DATE           := TO_DATE ('01-01-1920', 'dd-mm-yyyy'); Bug 2276479
6911 	 x_budget_period_start_date_old   DATE           := NULL;
6912          x_budget_period_end_date_old     DATE           :=  NULL;
6913       BEGIN
6914          FOR res_grp_level IN available_bal_at_res_grp
6915          LOOP
6916             IF    res_grp_level.budget_version_id <> x_budget_version_id_old
6917                OR res_grp_level.bud_task_id <> x_bud_task_id_old
6918                OR res_grp_level.parent_resource_id <> x_parent_rlmi_old
6919                --OR res_grp_level.budget_period_start_date <> x_budget_period_start_date_old Bug 2276479
6920                --OR res_grp_level.budget_period_end_date <> x_budget_period_end_date_old THEN Bug 2276479
6921 		OR res_grp_level.budget_period_start_date <> nvl(x_budget_period_start_date_old ,res_grp_level.budget_period_start_date + 1 )
6922                	OR res_grp_level.budget_period_end_date <> nvl(x_budget_period_end_date_old , res_grp_level.budget_period_end_date + 1 ) THEN
6923                IF res_grp_level.actual_flag = 'A' THEN
6924                   x_pending_actual := res_grp_level.entered_dr;
6925                ELSIF res_grp_level.actual_flag = 'E' THEN
6926                   x_pending_enc := res_grp_level.entered_dr;
6927                END IF;
6928                x_budget_version_id_old := res_grp_level.budget_version_id;
6929                x_bud_task_id_old := res_grp_level.bud_task_id;
6930                x_parent_rlmi_old := res_grp_level.parent_resource_id;
6931                x_budget_period_start_date_old := res_grp_level.budget_period_start_date;
6932                x_budget_period_end_date_old := res_grp_level.budget_period_end_date;
6933             ELSE
6934 			 IF nvl(res_grp_level.entered_dr,0) >= 0  AND res_grp_level.effect_on_funds_code = 'D' THEN	--Bug 2092791 and 2927485 (Update pending balance
6935                UPDATE gms_bc_packets                                                                    --                if funds are decreasing)
6936                   SET res_grp_actual_pending = NVL (res_grp_actual_pending, 0)
6937                                                - DECODE (
6938                                                     res_grp_level.actual_flag,
6939                                                     'A', NVL (x_pending_actual, 0),
6940                                                     0),
6941                       res_grp_enc_pending = NVL (res_grp_enc_pending, 0)
6942                                             - DECODE (
6943                                                  res_grp_level.actual_flag,
6944                                                  'E', NVL (x_pending_enc, 0),
6945                                                  0)
6946                 WHERE ROWID = res_grp_level.ROWID;
6947 			 END IF;
6948                IF res_grp_level.actual_flag = 'A' THEN
6949                   x_pending_actual := x_pending_actual + res_grp_level.entered_dr;
6950                ELSIF res_grp_level.actual_flag = 'E' THEN
6951                   x_pending_enc := x_pending_enc + res_grp_level.entered_dr;
6952                END IF;
6953             END IF;
6954          END LOOP;
6955       END;
6956 -- ================================================================================
6957 -- 	                RESOURCE GROUP LEVEL : RESULT CODE UPDATE
6958 -- ================================================================================
6959 --	P72	: Transaction pass Funds Check at Resource Group level
6960 --	P72	: Transaction pass Funds Check at Resource Group level in advisory
6961 --	P76	: Transaction does not require funds check at this level
6962 --  P78	: Increase Funds does not require Funds Check
6963 --	F75	: Transaction Failed because of Raw
6964 --	F63	: Transaction Failed because of Burden
6965 --	F93	: Transaction failed funds check at Resource Group level
6966 -- ================================================================================
6967       -- Changed for bug : 2927485: Added decode(bp.result_code,null,'P78',bp.result_code)
6968       IF g_debug = 'Y' THEN
6969       	gms_error_pkg.gms_debug ('RESULT CODE UPDATE - RESOURCE GROUP', 'C');
6970       END IF;
6971       g_error_stage := 'FC PR : RESG RESULT UPD';
6972       UPDATE gms_bc_packets bp
6973          SET bp.res_grp_result_code = DECODE (
6974                                          bp.effect_on_funds_code,
6975                                          'I',decode(bp.result_code,null,'P78',bp.result_code)  ,
6976                                          'D', DECODE (
6977                                                  bp.rg_funds_control_level_code,
6978                                                  'N', 'P76',
6979                                                  'D', DECODE (
6980                                                          SIGN (
6981                                                             NVL (bp.res_grp_budget_posted, 0)
6982                                                             - NVL (bp.res_grp_actual_posted, 0)
6983                                                             - NVL (bp.res_grp_enc_posted, 0)
6984                                                             - NVL (bp.res_grp_actual_approved, 0)
6985                                                             - NVL (bp.res_grp_actual_pending, 0)
6986                                                             - NVL (bp.res_grp_enc_approved, 0)
6987                                                             - NVL (bp.res_grp_enc_pending, 0)),
6988                                                          -1, 'P73',
6989                                                          'P72'),
6990                                                  'B', DECODE (
6991                                                          SIGN (
6992                                                             NVL (bp.res_grp_budget_posted, 0)
6993                                                             - NVL (bp.res_grp_actual_posted, 0)
6994                                                             - NVL (bp.res_grp_enc_posted, 0)
6995                                                             - NVL (bp.res_grp_actual_approved, 0)
6996                                                             - NVL (bp.res_grp_actual_pending, 0)
6997                                                             - NVL (bp.res_grp_enc_approved, 0)
6998                                                             - NVL (bp.res_grp_enc_pending, 0)),
6999                                                          -1, 'F93',
7000                                                          'P72')))
7001        WHERE bp.packet_id = x_packetid
7002          AND bp.effect_on_funds_code IN ('D', 'I')
7003          AND bp.status_code = 'P';
7004 
7005 
7006       -- 7. Update all above levels with failure result code
7007       -- b. Propogate resource group level to all other levels
7008       UPDATE gms_bc_packets bp
7009          SET bp.task_result_code = res_grp_result_code,
7010              bp.top_task_result_code = res_grp_result_code,
7011              bp.award_result_code = res_grp_result_code,
7012              bp.result_code = res_grp_result_code,
7013              bp.status_code = DECODE(x_mode,'C','F','R')
7014        WHERE bp.packet_id = x_packetid
7015          AND bp.effect_on_funds_code IN ('D', 'I')
7016          AND bp.status_code = 'P'
7017          AND SUBSTR (bp.res_grp_result_code, 1, 1) = 'F';
7018 
7019        -- Call new procedure RAW_BURDEN_FAILURE to handle raw-burden failure ...
7020        RAW_BURDEN_FAILURE(x_packetid,     -- Packet_id
7021                           g_derived_mode, -- Mode
7022                           'RESG'          -- Level
7023                          );
7024 
7025       -- Call new FUNCTION Full_mode_failure to handle full mode and scenario of failing all cdl when one cdl failed ..
7026       If FULL_MODE_FAILURE(x_packetid,     -- Packet_id
7027                         g_mode,         -- Mode , use g_mode
7028                         'RESG'           -- Level
7029                          ) then
7030          If g_partial_flag = 'N' then
7031             -- If full mode, transactions have failed .. exit FC
7032             GOTO END_OF_FC_PROCESS;
7033          End if;
7034       End If;
7035 
7036 -- Bug 2092791
7037 --===============================================================================
7038 --  RESOURCE GROUP LEVEL : INSERT NEGATIVE IN SUMMARY FOR FAILED TRANSACTIONS
7039 --===============================================================================
7040       g_error_stage := 'FC PR : RESG IN NEG';
7041 	   INSERT INTO gms_bc_packets_summary
7042             (packet_id,
7043     		 creation_date,
7044              budget_version_id,
7045 			 top_task_id,
7046              bud_task_id,
7047              budget_period_start_date,
7048              budget_period_end_date,
7049              actual_pending,
7050              enc_pending)
7051 	Select  x_packetid,
7052 	        x_date,
7053 		    budget_version_id,
7054 			top_task_id,
7055        		bud_task_id,
7056       		budget_period_start_date,
7057       		budget_period_end_date,
7058        		-1 * nvl(sum(decode(status_code || actual_flag,'RA',nvl(entered_dr,0) - nvl(entered_cr,0),0)),0) ,
7059        		-1 * nvl(sum(decode(status_code || actual_flag,'RE',nvl(entered_dr,0) - nvl(entered_cr,0),0)),0)
7060 	 from   gms_bc_packets
7061 	 where  packet_id = x_packetid
7062 	 and    res_grp_result_code in ('F93','F63','F75','F65')
7063 	 and    nvl(substr(res_result_code,1,1),'P') = 'P'
7064     group by budget_version_id, top_task_id, bud_task_id, budget_period_start_date, budget_period_end_date;
7065 
7066     -- Added commit for the base bug 3848201
7067     commit;
7068 
7069 -- ==============================================================================
7070 -- ************************** TASK LEVEL FUNDS CHECK ****************************
7071 -- ==============================================================================
7072 --===============================================================================
7073 -- 						TASK LEVEL : POSTED BALANCE UPDATE
7074 --===============================================================================
7075       IF g_debug = 'Y' THEN
7076       	gms_error_pkg.gms_debug ('TASK :POSTED BALANCE ', 'C');
7077       END IF;
7078       g_error_stage := 'FC PR : TASK P BAL';
7079       UPDATE gms_bc_packets bp
7080          SET (bp.task_budget_posted, bp.task_actual_posted, bp.task_enc_posted) =
7081                 (SELECT SUM (NVL (budget_period_to_date, 0) * DECODE (balance_type, 'BGT', 1, 0)),
7082                         SUM (NVL (actual_period_to_date, 0) * DECODE (balance_type, 'EXP', 1, 0)),
7083                         SUM (NVL (encumb_period_to_date, 0) * DECODE (balance_type, 'REQ', 1, 'PO', 1, 'AP', 1, 'ENC', 1, 0))
7084                    FROM gms_balances gb
7085                   WHERE gb.budget_version_id = bp.budget_version_id
7086                     AND gb.project_id = bp.project_id
7087                     AND gb.award_id = bp.award_id
7088                     AND gb.task_id = bp.task_id
7089                     AND gb.balance_type <> 'REV'
7090                     AND gb.start_date BETWEEN DECODE (
7091                                                  bp.time_phased_type_code,
7092                                                  'N', gb.start_date,
7093                                                  bp.budget_period_start_date)
7094                                           AND DECODE (
7095                                                  bp.time_phased_type_code,
7096                                                  'N', gb.start_date,
7097                                                  bp.budget_period_end_date)
7098                     AND gb.end_date BETWEEN DECODE (
7099                                                bp.time_phased_type_code,
7100                                                'N', gb.end_date,
7101                                                bp.budget_period_start_date)
7102                                         AND DECODE (
7103                                                bp.time_phased_type_code,
7104                                                'N', gb.end_date,
7105                                                bp.budget_period_end_date))
7106        WHERE bp.packet_id = x_packetid
7107          AND bp.effect_on_funds_code = 'D'
7108          AND bp.status_code = 'P'
7109          AND bp.t_funds_control_level_code <> 'N'
7110          AND bp.bud_task_id <> 0;
7111       -- ** t_funds_control_level_code CAN BE 'N' if 1.Project Level Funding 2. None Control
7112 --===============================================================================
7113 -- 	TASK LEVEL : APPROVED/PENDING BALANCE FOR PREVIOUS AND CURRENT PACKETS
7114 --===============================================================================
7115       IF g_debug = 'Y' THEN
7116       	gms_error_pkg.gms_debug ('TASK LEVEL - APPROVED/PENDING BALANCE FOR PREVIOUS PACKETS', 'C');
7117       END IF;
7118 
7119       g_error_stage := 'FC PR : TASK A/P BAL';
7120 		 UPDATE gms_bc_packets bp
7121    		    SET (bp.task_actual_approved, bp.task_actual_pending, bp.task_enc_approved,
7122 			        bp.task_enc_pending) =
7123          			 (SELECT
7124 					 		 SUM (actual_approved),
7125                              SUM (actual_pending),
7126                              SUM (enc_approved),
7127                              SUM (enc_pending)
7128            			    FROM gms_bc_packets_summary gmsbcs
7129 		 	           WHERE gmsbcs.packet_id = x_packetid
7130 				     AND gmsbcs.budget_version_id = bp.budget_version_id
7131 				     AND gmsbcs.bud_task_id = bp.bud_task_id
7132 					  --Bug 2490381 : Changed "=" to "<=" to consider all the
7133 					  --              records from summary table which fall
7134 					  --		  under budget_period_start_date and budget_period_end_date
7135 					  --		  of current transaction.
7136 					  -- Bug 2897560 : changed the start date comparision to ">="
7137 					  -- as we need to consider all the recods from summary table which fall
7138 					  -- under budget_period_start_date and budget_period_end_date
7139 					  -- of current transaction.
7140 	 			     AND gmsbcs.budget_period_start_date >= bp.budget_period_start_date
7141 				     AND gmsbcs.budget_period_end_date <= bp.budget_period_end_date)
7142 		  WHERE bp.packet_id = x_packetid
7143 		    AND bp.effect_on_funds_code = 'D'
7144 			AND bp.status_code = 'P'
7145 			AND bp.t_funds_control_level_code <> 'N'
7146 			AND bp.bud_task_id <> 0;
7147 --===============================================================================
7148 -- 	                  TASK LEVEL : AVAILABLE BALANCE UPDATE
7149 --===============================================================================
7150 -- 2092791
7151       g_error_stage := 'FC PR : TASK A BAL';
7152       DECLARE
7153          CURSOR available_bal_at_task IS
7154                SELECT (nvl(entered_dr,0)- nvl(entered_cr,0)) entered_dr,  -- Bug 2092791
7155                      ROWID,
7156                      budget_version_id,
7157                      bud_task_id,
7158                      effect_on_funds_code,    -- Bug 2927485
7159                      TRUNC (budget_period_start_date) budget_period_start_date,
7160                      TRUNC (budget_period_end_date) budget_period_end_date,
7161                      actual_flag
7162                 FROM gms_bc_packets
7163                WHERE packet_id = x_packetid
7164                  AND effect_on_funds_code in ('D','I') 				 -- Bug 2092791
7165                  AND status_code = 'P'
7166                  AND t_funds_control_level_code <> 'N'
7167                  AND bud_task_id <> 0
7168             ORDER BY budget_version_id,
7169                      bud_task_id,
7170                      budget_period_start_date,
7171                      budget_period_end_date,
7172                      funds_check_seq DESC;
7173          x_entered_dr                     NUMBER (22, 5);
7174          x_rowid                          ROWID;
7175          x_pending_actual                 NUMBER         := -11;
7176          x_pending_enc                    NUMBER         := -11;
7177          x_amount                         NUMBER         := -11;
7178          x_budget_version_id_old          NUMBER         := -11;
7179          x_bud_task_id_old                NUMBER         := -11;
7180          x_bud_rlmi_old                   NUMBER         := -11;
7181          --x_budget_period_start_date_old   DATE           := TO_DATE ('01-01-1720', 'dd-mm-yyyy'); Bug 2276479
7182          --x_budget_period_end_date_old     DATE           := TO_DATE ('01-01-1720', 'dd-mm-yyyy'); Bug 2276479
7183 	x_budget_period_start_date_old   DATE           :=  NULL;
7184 	x_budget_period_end_date_old     DATE           :=  NULL;
7185       BEGIN
7186          FOR task_level IN available_bal_at_task
7187          LOOP
7188             IF    task_level.budget_version_id <> x_budget_version_id_old
7189                OR task_level.bud_task_id <> x_bud_task_id_old
7190 		OR task_level.budget_period_start_date <> nvl(x_budget_period_start_date_old ,task_level.budget_period_start_date + 1 )
7191 		OR task_level.budget_period_end_date <> nvl(x_budget_period_end_date_old , task_level.budget_period_end_date + 1 ) THEN
7192 
7193                IF task_level.actual_flag = 'A' THEN
7194                   x_pending_actual := task_level.entered_dr;
7195                ELSIF task_level.actual_flag = 'E' THEN
7196                   x_pending_enc := task_level.entered_dr;
7197                END IF;
7198                x_budget_version_id_old := task_level.budget_version_id;
7199                x_bud_task_id_old := task_level.bud_task_id;
7200                x_budget_period_start_date_old := task_level.budget_period_start_date;
7201                x_budget_period_end_date_old := task_level.budget_period_end_date;
7202             ELSE
7203              IF nvl(task_level.entered_dr,0) >=0  AND task_level.effect_on_funds_code = 'D' THEN	--Bug 2092791 and 2927485 (Update pending balance
7204                UPDATE gms_bc_packets                                                                --                if funds are decreasing)
7205                   SET task_actual_pending = NVL (task_actual_pending, 0)
7206                                             - DECODE (
7207                                                  task_level.actual_flag,
7208                                                  'A', NVL (x_pending_actual, 0),
7209                                                  0),
7210                       task_enc_pending = NVL (task_enc_pending, 0)
7211                                          - DECODE (
7212                                               task_level.actual_flag,
7213                                               'E', NVL (x_pending_enc, 0),
7214                                               0)
7215                 WHERE ROWID = task_level.ROWID;
7216 			  END IF;
7217                IF task_level.actual_flag = 'A' THEN
7218                   x_pending_actual := x_pending_actual + task_level.entered_dr;
7219                ELSIF task_level.actual_flag = 'E' THEN
7220                   x_pending_enc := x_pending_enc + task_level.entered_dr;
7221                END IF;
7222             END IF;
7223          END LOOP;
7224       END;
7225 
7226 -- ================================================================================
7227 -- 	                     TASK LEVEL : RESULT CODE UPDATE
7228 -- ================================================================================
7229 --	P64	: Transaction pass Funds Check at Task level
7230 --	P65	: Transaction pass Funds Check at Task level in advisory
7231 --	P76	: Transaction does not require funds check at this level
7232 --  P78	: Increase Funds does not require Funds Check
7233 --	F63	: Transaction Failed because of Burden
7234 --	F91	: Transaction failed funds check at Task level
7235 -- ================================================================================
7236       -- 3. Task Result Code Update
7237  -- Added decode(bp.result_code,null,'P78',bp.result_code for bug : 2927485
7238       IF g_debug = 'Y' THEN
7239       	gms_error_pkg.gms_debug ('TASK : RESULT CODE ', 'C');
7240       END IF;
7241       g_error_stage := 'FC PR : TASK RESULT CD';
7242       UPDATE gms_bc_packets bp
7243          SET bp.task_result_code = DECODE (
7244                                       bp.effect_on_funds_code,
7245                                       'I', decode(bp.result_code,null,'P78',bp.result_code) ,
7246                                       'D', DECODE (
7247                                               bp.t_funds_control_level_code,
7248                                               'N', 'P76',
7249                                               'D', DECODE (
7250                                                       SIGN (
7251                                                          NVL (bp.task_budget_posted, 0)
7252                                                          - NVL (bp.task_actual_posted, 0)
7253                                                          - NVL (bp.task_enc_posted, 0)
7254                                                          - NVL (bp.task_actual_approved, 0)
7255                                                          - NVL (bp.task_actual_pending, 0)
7256                                                          - NVL (bp.task_enc_approved, 0)
7257                                                          - NVL (bp.task_enc_pending, 0)),
7258                                                       -1, 'P65',
7259                                                       'P64'),
7260                                               'B', DECODE (
7261                                                       SIGN (
7262                                                          NVL (bp.task_budget_posted, 0)
7263                                                          - NVL (bp.task_actual_posted, 0)
7264                                                          - NVL (bp.task_enc_posted, 0)
7265                                                          - NVL (bp.task_actual_approved, 0)
7266                                                          - NVL (bp.task_actual_pending, 0)
7267                                                          - NVL (bp.task_enc_approved, 0)
7268                                                          - NVL (bp.task_enc_pending, 0)),
7269                                                       -1, 'F91',
7270                                                       'P64')))
7271        WHERE bp.packet_id = x_packetid
7272          AND bp.effect_on_funds_code IN ('D', 'I')
7273          AND bp.status_code = 'P';
7274 
7275 
7276 
7277       -- 5. Update all above levels with failure result code
7278       UPDATE gms_bc_packets bp
7279          SET bp.result_code = bp.task_result_code,
7280              bp.top_task_result_code = bp.task_result_code,
7281              bp.award_result_code = bp.task_result_code,
7282              bp.status_code = DECODE(x_mode,'C','F','R')
7283        WHERE bp.packet_id = x_packetid
7284          AND bp.effect_on_funds_code IN ('D', 'I')
7285          AND bp.status_code = 'P'
7286          AND SUBSTR (bp.task_result_code, 1, 1) = 'F';
7287 
7288        -- Call new procedure RAW_BURDEN_FAILURE to handle raw-burden failure ...
7289        RAW_BURDEN_FAILURE(x_packetid,     -- Packet_id
7290                           g_derived_mode, -- Mode
7291                           'TSK'           -- Level
7292                          );
7293 
7294 
7295       -- Call new FUNCTION Full_mode_failure to handle full mode and scenario of failing all cdl when one cdl failed ..
7296       If FULL_MODE_FAILURE(x_packetid,     -- Packet_id
7297                         g_mode,         -- Mode , use g_mode
7298                         'TSK'           -- Level
7299                          ) then
7300          If g_partial_flag = 'N' then
7301             -- If full mode, transactions have failed .. exit FC
7302             GOTO END_OF_FC_PROCESS;
7303          End if;
7304       End If;
7305 -- 	Bug 2092791
7306 --==========================================================================
7307 --     TASK LEVEL : INSERT NEGATIVE IN SUMMARY FOR FAILED TRANSACTIONS
7308 --==========================================================================
7309        g_error_stage := 'FC PR : TASK IN NEG';
7310 	   INSERT INTO gms_bc_packets_summary
7311             (packet_id,
7312     		 creation_date,
7313              budget_version_id,
7314 			 top_task_id,
7315              budget_period_start_date,
7316              budget_period_end_date,
7317              actual_pending,
7318              enc_pending)
7319 	Select  x_packetid,
7320 	        x_date,
7321 		    budget_version_id,
7322 			top_task_id,
7323       		budget_period_start_date,
7324       		budget_period_end_date,
7325        		-1 * nvl(sum(decode(status_code || actual_flag,'RA',nvl(entered_dr,0) - nvl(entered_cr,0),0)),0) ,
7326        		-1 * nvl(sum(decode(status_code || actual_flag,'RE',nvl(entered_dr,0) - nvl(entered_cr,0),0)),0)
7327 	 from   gms_bc_packets
7328 	 where  packet_id = x_packetid
7329 	 and    task_result_code in ('F91','F63','F75','F65')
7330 	 and    substr(res_grp_result_code,1,1) = 'P'
7331     group by budget_version_id, top_task_id, budget_period_start_date, budget_period_end_date;
7332 
7333     -- Added commit for the base bug 3848201
7334     commit;
7335 
7336 -- ==============================================================================
7337 -- ************************* TOP TASK LEVEL FUNDS CHECK *************************
7338 -- ==============================================================================
7339 --===============================================================================
7340 -- 						TOP TASK LEVEL : POSTED BALANCE UPDATE
7341 --===============================================================================
7342       IF g_debug = 'Y' THEN
7343       	gms_error_pkg.gms_debug ('TOP TASK :POSTED BALANCE ', 'C');
7344       END IF;
7345       g_error_stage := 'FC PR : TTASK P BAL';
7346       UPDATE gms_bc_packets bp
7347          SET (bp.top_task_budget_posted, bp.top_task_actual_posted, bp.top_task_enc_posted) =
7348                 (SELECT SUM (NVL (budget_period_to_date, 0) * DECODE (balance_type, 'BGT', 1, 0)),
7349                         SUM (NVL (actual_period_to_date, 0) * DECODE (balance_type, 'EXP', 1, 0)),
7350                         SUM (NVL (encumb_period_to_date, 0) * DECODE (balance_type, 'REQ', 1, 'PO', 1, 'AP', 1, 'ENC', 1, 0))
7351                    FROM gms_balances gb
7352                   WHERE gb.budget_version_id = bp.budget_version_id
7353                     AND gb.project_id = bp.project_id
7354                     AND gb.award_id = bp.award_id
7355                     AND gb.balance_type <> 'REV'
7356                     AND DECODE (gb.top_task_id, NULL, gb.task_id, gb.top_task_id) =
7357                                                                 bp.top_task_id
7358                     AND gb.start_date BETWEEN DECODE (
7359                                                  bp.time_phased_type_code,
7360                                                  'N', gb.start_date,
7361                                                  bp.budget_period_start_date)
7362                                           AND DECODE (
7363                                                  bp.time_phased_type_code,
7364                                                  'N', gb.start_date,
7365                                                  bp.budget_period_end_date)
7366                     AND gb.end_date BETWEEN DECODE (
7367                                                bp.time_phased_type_code,
7368                                                'N', gb.end_date,
7369                                                bp.budget_period_start_date)
7370                                         AND DECODE (
7371                                                bp.time_phased_type_code,
7372                                                'N', gb.end_date,
7373                                                bp.budget_period_end_date))
7374        WHERE bp.packet_id = x_packetid
7375          AND bp.effect_on_funds_code = 'D'
7376          AND bp.status_code = 'P'
7377          AND bp.tt_funds_control_level_code <> 'N'
7378          AND bp.bud_task_id <> 0;
7379       -- ** t_funds_control_level_code CAN BE 'N' if 1.Project Level Funding 2. None Control
7380 --===============================================================================
7381 -- 	TOP TASK LEVEL : APPROVED/PENDING BALANCE FOR PREVIOUS AND CURRENT PACKETS
7382 --===============================================================================
7383   IF g_debug = 'Y' THEN
7384   	gms_error_pkg.gms_debug ('TOP TASK LEVEL - APPROVED/PENDING BALANCE FOR PREVIOUS PACKETS', 'C');
7385   END IF;
7386   g_error_stage := 'FC PR : TTASK A/P BAL';
7387 -- Bug 2092791
7388 
7389 		 UPDATE gms_bc_packets bp
7390    		    SET (bp.top_task_actual_approved, bp.top_task_actual_pending, bp.top_task_enc_approved,
7391 			        bp.top_task_enc_pending) =
7392          			 (SELECT
7393 					 		 SUM (actual_approved),
7394                              SUM (actual_pending),
7395                              SUM (enc_approved),
7396                              SUM (enc_pending)
7397            			    FROM gms_bc_packets_summary gmsbcs
7398 				       WHERE gmsbcs.packet_id = x_packetid
7399 				         AND gmsbcs.budget_version_id = bp.budget_version_id
7400 					 AND gmsbcs.top_task_id = bp.top_task_id
7401 					  --Bug 2490381 : Changed "=" to "<=" to consider all the
7402 					  --              records from summary table which fall
7403 					  --		  under budget_period_start_date and budget_period_end_date
7404 					  --		  of current transaction.
7405 					  -- Bug 2897560 : changed the start date comparision to ">="
7406 					  -- as we need to consider all the recods from summary table which fall
7407 					  -- under budget_period_start_date and budget_period_end_date
7408 					  -- of current transaction.
7409 					 AND gmsbcs.budget_period_start_date >= bp.budget_period_start_date
7410 					 AND gmsbcs.budget_period_end_date <= bp.budget_period_end_date)
7411 		  WHERE bp.packet_id = x_packetid
7412 		    AND bp.effect_on_funds_code = 'D'
7413 			AND bp.status_code = 'P'
7414 			AND bp.tt_funds_control_level_code <> 'N'
7415 			AND bp.bud_task_id <> 0;
7416 -- Bug 2092791
7417 
7418 --===============================================================================
7419 -- 	                TOP TASK LEVEL : AVAILABLE BALANCE UPDATE
7420 --===============================================================================
7421       g_error_stage := 'FC PR : TTASK A BAL';
7422       DECLARE
7423          CURSOR available_bal_at_top_task IS
7424                SELECT (nvl(entered_dr,0)- nvl(entered_cr,0)) entered_dr,  -- Bug 2092791
7425                      ROWID,
7426                      budget_version_id,
7427                      top_task_id,
7428                      effect_on_funds_code,    -- Bug 2927485
7429                      TRUNC (budget_period_start_date) budget_period_start_date,
7430                      TRUNC (budget_period_end_date) budget_period_end_date,
7431                      actual_flag
7432                 FROM gms_bc_packets
7433                WHERE packet_id = x_packetid
7434                  AND effect_on_funds_code in ('D','I')    -- Bug 2092791
7435                  AND status_code = 'P'
7436                  AND tt_funds_control_level_code <> 'N'
7437                  AND bud_task_id <> 0
7438             ORDER BY budget_version_id,
7439                      top_task_id,
7440                      budget_period_start_date,
7441                      budget_period_end_date,
7442                      funds_check_seq DESC;
7443          x_entered_dr                     NUMBER (22, 5);
7444          x_rowid                          ROWID;
7445          x_pending_actual                 NUMBER         := -11;
7446          x_pending_enc                    NUMBER         := -11;
7447          x_amount                         NUMBER         := -11;
7448          x_budget_version_id_old          NUMBER         := -11;
7449          x_bud_task_id_old                NUMBER         := -11;
7450          x_top_task_id_old                NUMBER         := -11;
7451          x_bud_rlmi_old                   NUMBER         := -11;
7452          --x_budget_period_start_date_old   DATE           := TO_DATE ('01-01-1720', 'dd-mm-yyyy'); Bug 2276479
7453          --x_budget_period_end_date_old     DATE           := TO_DATE ('01-01-1720', 'dd-mm-yyyy'); Bug 2276479
7454 	x_budget_period_start_date_old   DATE           :=  NULL;
7455         x_budget_period_end_date_old     DATE           :=  NULL;
7456       BEGIN
7457          FOR top_task_level IN available_bal_at_top_task
7458          LOOP
7459             IF    top_task_level.budget_version_id <> x_budget_version_id_old
7460                OR top_task_level.top_task_id <> x_top_task_id_old
7461                --OR top_task_level.budget_period_start_date <> x_budget_period_start_date_old Bug 2276479
7462                --OR top_task_level.budget_period_end_date <> x_budget_period_end_date_old THEN Bug 2276479
7463 		OR top_task_level.budget_period_start_date <> nvl(x_budget_period_start_date_old ,top_task_level.budget_period_start_date + 1 )
7464 		OR top_task_level.budget_period_end_date <> nvl(x_budget_period_end_date_old , top_task_level.budget_period_end_date + 1 ) THEN
7465 
7466                IF top_task_level.actual_flag = 'A' THEN
7467                   x_pending_actual := top_task_level.entered_dr;
7468                ELSIF top_task_level.actual_flag = 'E' THEN
7469                   x_pending_enc := top_task_level.entered_dr;
7470                END IF;
7471                x_budget_version_id_old := top_task_level.budget_version_id;
7472                x_top_task_id_old := top_task_level.top_task_id;
7473                x_budget_period_start_date_old := top_task_level.budget_period_start_date;
7474                x_budget_period_end_date_old := top_task_level.budget_period_end_date;
7475             ELSE
7476              IF nvl(top_task_level.entered_dr,0) >=0  AND top_task_level.effect_on_funds_code = 'D' THEN	--Bug 2092791 and 2927485 (Update pending balance
7477 			   UPDATE gms_bc_packets                                                                        --                if funds are decreasing)
7478                   SET top_task_actual_pending = NVL (top_task_actual_pending, 0)
7479                                                 - DECODE (
7480                                                      top_task_level.actual_flag,
7481                                                      'A', NVL (x_pending_actual, 0),
7482                                                      0),
7483                       top_task_enc_pending = NVL (top_task_enc_pending, 0)
7484                                              - DECODE (
7485                                                   top_task_level.actual_flag,
7486                                                   'E', NVL (x_pending_enc, 0),
7487                                                   0)
7488                 WHERE ROWID = top_task_level.ROWID;
7489 			 END IF;
7490                IF top_task_level.actual_flag = 'A' THEN
7491                   x_pending_actual := x_pending_actual + top_task_level.entered_dr;
7492                ELSIF top_task_level.actual_flag = 'E' THEN
7493                   x_pending_enc := x_pending_enc + top_task_level.entered_dr;
7494                END IF;
7495             END IF;
7496          END LOOP;
7497       END;
7498 
7499 -- ================================================================================
7500 -- 	                   TOP TASK LEVEL : RESULT CODE UPDATE
7501 -- ================================================================================
7502 --	P79	: Transaction pass Funds Check at Top Task level
7503 --	P80	: Transaction pass Funds Check at Top Task level in advisory
7504 --	P76	: Transaction does not require funds check at this level
7505 --  P78	: Increase Funds does not require Funds Check
7506 --	F63	: Transaction Failed because of Burden
7507 --	F60	: Transaction failed funds check at Top Task level
7508 -- ================================================================================
7509       -- Top Task Result Code Update
7510       -- Added decode(bp.result_code,null,'P78',bp.result_code for bug : 2927485
7511       IF g_debug = 'Y' THEN
7512       	gms_error_pkg.gms_debug ('TOP TASK : RESULT CODE ', 'C');
7513       END IF;
7514       g_error_stage := 'FC PR : TTASK RESULT CODE';
7515       UPDATE gms_bc_packets bp
7516          SET bp.top_task_result_code = DECODE (
7517                                           bp.effect_on_funds_code,
7518                                           'I', decode(bp.result_code,null,'P78',bp.result_code),
7519                                           'D', DECODE (
7520                                                   bp.tt_funds_control_level_code,
7521                                                   'N', 'P76',
7522                                                   'D', DECODE (
7523                                                           SIGN (
7524                                                              NVL (bp.top_task_budget_posted, 0)
7525                                                              - NVL (bp.top_task_actual_posted, 0)
7526                                                              - NVL (bp.top_task_enc_posted, 0)
7527                                                              - NVL (bp.top_task_actual_approved, 0)
7528                                                              - NVL (bp.top_task_actual_pending, 0)
7529                                                              - NVL (bp.top_task_enc_approved, 0)
7530                                                              - NVL (bp.top_task_enc_pending, 0)),
7531                                                           -1, 'P80',
7532                                                           'P79'),
7533                                                   'B', DECODE (
7534                                                           SIGN (
7535                                                              NVL (bp.top_task_budget_posted, 0)
7536                                                              - NVL (bp.top_task_actual_posted, 0)
7537                                                              - NVL (bp.top_task_enc_posted, 0)
7538                                                              - NVL (bp.top_task_actual_approved, 0)
7539                                                              - NVL (bp.top_task_actual_pending, 0)
7540                                                              - NVL (bp.top_task_enc_approved, 0)
7541                                                              - NVL (bp.top_task_enc_pending, 0)),
7542                                                           -1, 'F60',
7543                                                           'P79')))
7544        WHERE bp.packet_id = x_packetid
7545          AND bp.effect_on_funds_code IN ('D', 'I')
7546          AND bp.status_code = 'P';
7547 
7548 
7549       -- 5. Update all above levels with failure result code
7550       UPDATE gms_bc_packets bp
7551          SET bp.result_code = bp.top_task_result_code,
7552              bp.award_result_code = bp.top_task_result_code,
7553              bp.status_code = DECODE(x_mode,'C','F','R')
7554        WHERE bp.packet_id = x_packetid
7555          AND bp.effect_on_funds_code IN ('D', 'I')
7556          AND bp.status_code = 'P'
7557          AND SUBSTR (bp.top_task_result_code, 1, 1) = 'F';
7558 
7559        -- Call new procedure RAW_BURDEN_FAILURE to handle raw-burden failure ...
7560        RAW_BURDEN_FAILURE(x_packetid,     -- Packet_id
7561                           g_derived_mode, -- Mode
7562                           'TTSK'           -- Level
7563                          );
7564 
7565 
7566       -- Call new FUNCTION Full_mode_failure to handle full mode and scenario of failing all cdl when one cdl failed ..
7567       If FULL_MODE_FAILURE(x_packetid,     -- Packet_id
7568                         g_mode,         -- Mode , use g_mode
7569                         'TTSK'           -- Level
7570                          ) then
7571          If g_partial_flag = 'N' then
7572             -- If full mode, transactions have failed .. exit FC
7573             GOTO END_OF_FC_PROCESS;
7574          End if;
7575       End If;
7576 
7577        g_error_stage := 'FC PR : TTASK IN NEG';
7578 	   INSERT INTO gms_bc_packets_summary
7579             (packet_id,
7580 			 creation_date,
7581              budget_version_id,
7582              budget_period_start_date,
7583              budget_period_end_date,
7584              actual_pending,
7585              enc_pending)
7586 	Select  x_packetid,
7587             x_date,
7588             budget_version_id,
7589       		budget_period_start_date,
7590       		budget_period_end_date,
7591        		-1 * nvl(sum(decode(status_code || actual_flag, 'RA',nvl(entered_dr,0) - nvl(entered_cr,0),0)),0) ,
7592        		-1 * nvl(sum(decode(status_code || actual_flag, 'RE',nvl(entered_dr,0) - nvl(entered_cr,0),0)),0)
7593          from   gms_bc_packets
7594          where  packet_id = x_packetid
7595          and    top_task_result_code in ('F60','F63','F65','F75')
7596 		 and    substr(task_result_code,1,1) = 'P'
7597          group by budget_version_id, budget_period_start_date, budget_period_end_date;
7598 
7599        -- Added commit for the base bug 3848201
7600        commit;
7601 
7602 
7603 -- ==============================================================================
7604 -- ************************* AWARD  LEVEL FUNDS CHECK ***************************
7605 -- ==============================================================================
7606 --===============================================================================
7607 -- 		     		   AWARD LEVEL : POSTED BALANCE UPDATE
7608 --===============================================================================
7609       IF g_debug = 'Y' THEN
7610       	gms_error_pkg.gms_debug ('AWARD:POSTED BALANCE  ', 'C');
7611       END IF;
7612       g_error_stage := 'FC PR : AWARD P B';
7613       UPDATE gms_bc_packets bp
7614          SET (bp.award_budget_posted, bp.award_actual_posted, bp.award_enc_posted) =
7615                 (SELECT SUM (NVL (budget_period_to_date, 0) * DECODE (balance_type, 'BGT', 1, 0)),
7616                         SUM (NVL (actual_period_to_date, 0) * DECODE (balance_type, 'EXP', 1, 0)),
7617                         SUM (NVL (encumb_period_to_date, 0) * DECODE (balance_type, 'REQ', 1, 'PO', 1, 'AP', 1, 'ENC', 1, 0))
7618                    FROM gms_balances gb
7619                   WHERE gb.budget_version_id = bp.budget_version_id
7620                     AND gb.project_id = bp.project_id
7621                     AND gb.award_id = bp.award_id
7622                     AND gb.start_date BETWEEN DECODE (
7623                                                  bp.time_phased_type_code,
7624                                                  'N', gb.start_date,
7625                                                  bp.budget_period_start_date)
7626                                           AND DECODE (
7627                                                  bp.time_phased_type_code,
7628                                                  'N', gb.start_date,
7629                                                  bp.budget_period_end_date)
7630                     AND gb.end_date BETWEEN DECODE (
7631                                                bp.time_phased_type_code,
7632                                                'N', gb.end_date,
7633                                                bp.budget_period_start_date)
7634                                         AND DECODE (
7635                                                bp.time_phased_type_code,
7636                                                'N', gb.end_date,
7637                                                bp.budget_period_end_date))
7638        WHERE bp.packet_id = x_packetid
7639          AND bp.effect_on_funds_code = 'D'
7640          AND bp.status_code = 'P'
7641          AND bp.a_funds_control_level_code <> 'N';
7642 --===============================================================================
7643 -- 	AWARD LEVEL : APPROVED/PENDING BALANCE FOR PREVIOUS AND CURRENT PACKETS
7644 --===============================================================================
7645       IF g_debug = 'Y' THEN
7646       	gms_error_pkg.gms_debug ('AWARD LEVEL - APPROVED/PENDING BALANCE FOR PREVIOUS PACKETS', 'C');
7647       END IF;
7648       g_error_stage := 'FC PR : AWARD A/P B';
7649 --  Bug 2092791
7650 	  UPDATE gms_bc_packets bp
7651 	     SET (bp.award_actual_approved, bp.award_actual_pending, bp.award_enc_approved,
7652 		         bp.award_enc_pending) =
7653          			 (SELECT
7654 						   		   SUM (actual_approved),
7655 						           SUM (actual_pending),
7656 								   SUM (enc_approved),
7657 								   SUM (enc_pending)
7658 					          FROM gms_bc_packets_summary gmsbcs
7659 							 WHERE gmsbcs.packet_id = x_packetid
7660  							   AND gmsbcs.budget_version_id = bp.budget_version_id
7661 							   --Bug 2490381 : Changed "=" to "<=" to consider all the
7662  							   --              records from summary table which fall
7663 							   --		   under budget_period_start_date and budget_period_end_date
7664  							   --		   of current transaction.
7665 					                   -- Bug 2897560 : changed the start date comparision to ">="
7666 					                   -- as we need to consider all the recods from summary table which fall
7667 					                   -- under budget_period_start_date and budget_period_end_date
7668 					                   -- of current transaction.
7669 					                   AND gmsbcs.budget_period_start_date >= bp.budget_period_start_date
7670 							   AND gmsbcs.budget_period_end_date <= bp.budget_period_end_date)
7671 	   WHERE bp.packet_id = x_packetid
7672 		 AND bp.effect_on_funds_code = 'D'
7673 		 AND bp.status_code = 'P'
7674 		 AND bp.a_funds_control_level_code <> 'N';
7675 --  Bug 2092791
7676 
7677 --===============================================================================
7678 -- 	                   AWARD LEVEL : AVAILABLE BALANCE UPDATE
7679 --===============================================================================
7680       g_error_stage := 'FC PR : AWARD A B';
7681       DECLARE
7682          CURSOR available_bal_at_awd IS
7683               SELECT (nvl(entered_dr,0) - nvl(entered_cr,0)) entered_dr,	--Bug 2092791
7684                      ROWID,
7685                      budget_version_id,
7686                      effect_on_funds_code,    -- Bug 2927485
7687                      TRUNC (budget_period_start_date) budget_period_start_date,
7688                      TRUNC (budget_period_end_date) budget_period_end_date,
7689                      actual_flag
7690                 FROM gms_bc_packets
7691                WHERE packet_id = x_packetid
7692                  AND effect_on_funds_code in  ('D','I') 	-- 2092791
7693                  AND status_code = 'P'
7694                  AND a_funds_control_level_code <> 'N'
7695             ORDER BY budget_version_id,
7696                      budget_period_start_date,
7697                      budget_period_end_date,
7698                      funds_check_seq DESC;
7699          x_entered_dr                     NUMBER (22, 5);
7700          x_rowid                          ROWID;
7701          x_pending_actual                 NUMBER         := -11;
7702          x_pending_enc                    NUMBER         := -11;
7703          x_amount                         NUMBER         := -11;
7704          x_budget_version_id_old          NUMBER         := -11;
7705          x_bud_task_id_old                NUMBER         := -11;
7706          x_bud_rlmi_old                   NUMBER         := -11;
7707          --x_budget_period_start_date_old   DATE           := TO_DATE ('01-01-1720', 'dd-mm-yyyy'); Bug 2276479
7708          --x_budget_period_end_date_old     DATE           := TO_DATE ('01-01-1720', 'dd-mm-yyyy'); Bug 2276479
7709 	x_budget_period_start_date_old   DATE           :=  NULL;
7710 	x_budget_period_end_date_old     DATE           :=  NULL;
7711       BEGIN
7712          FOR award_level IN available_bal_at_awd
7713          LOOP
7714             IF    award_level.budget_version_id <> x_budget_version_id_old
7715                --OR award_level.budget_period_start_date <> x_budget_period_start_date_old Bug 2276479
7716                --OR award_level.budget_period_end_date <> x_budget_period_end_date_old THEN Bug 2276479
7717 		OR award_level.budget_period_start_date <> nvl(x_budget_period_start_date_old ,award_level.budget_period_start_date + 1 )
7718 		OR award_level.budget_period_end_date <> nvl(x_budget_period_end_date_old , award_level.budget_period_end_date + 1 ) THEN
7719 
7720                IF award_level.actual_flag = 'A' THEN
7721                   x_pending_actual := award_level.entered_dr;
7722                ELSIF award_level.actual_flag = 'E' THEN
7723                   x_pending_enc := award_level.entered_dr;
7724                END IF;
7725                x_budget_version_id_old := award_level.budget_version_id;
7726                x_budget_period_start_date_old := award_level.budget_period_start_date;
7727                x_budget_period_end_date_old := award_level.budget_period_end_date;
7728             ELSE
7729 			  IF nvl(award_level.entered_dr,0) >= 0  AND award_level.effect_on_funds_code = 'D' THEN	--Bug 2092791 and 2927485(Update pending balance
7730 			                                                                                            --                if funds are decreasing)
7731                UPDATE gms_bc_packets
7732                   SET award_actual_pending = NVL (award_actual_pending, 0)
7733                                              - DECODE (
7734                                                   award_level.actual_flag,
7735                                                   'A', NVL (x_pending_actual, 0),
7736                                                   0),
7737                       award_enc_pending = NVL (award_enc_pending, 0)
7738                                           - DECODE (
7739                                                award_level.actual_flag,
7740                                                'E', NVL (x_pending_enc, 0),
7741                                                0)
7742                 WHERE ROWID = award_level.ROWID;
7743 			  END IF;
7744                IF award_level.actual_flag = 'A' THEN
7745                   x_pending_actual := x_pending_actual + award_level.entered_dr;
7746                ELSIF award_level.actual_flag = 'E' THEN
7747                   x_pending_enc := x_pending_enc + award_level.entered_dr;
7748                END IF;
7749             END IF;
7750          END LOOP;
7751       END;
7752 -- ================================================================================
7753 -- 	                       AWARD LEVEL : RESULT CODE UPDATE
7754 -- ================================================================================
7755 --	P60	: Transaction pass Funds Check at Award level
7756 --	P61	: Transaction pass Funds Check at Award level in advisory
7757 --	P76	: Transaction does not require funds check at this level
7758 --  P78	: Increase Funds does not require Funds Check
7759 --  F53 :
7760 --	F63	: Transaction Failed because of Burden
7761 --	F90	: Transaction failed funds check at Award level
7762 -- ================================================================================
7763   -- Added decode(bp.result_code,null,'P78',bp.result_code  for bug : 2927485
7764       IF g_debug = 'Y' THEN
7765       	gms_error_pkg.gms_debug ('AWARD: Result Code  ', 'C');
7766       END IF;
7767       g_error_stage := 'FC PR : AWARD RESULT';
7768       UPDATE gms_bc_packets bp
7769          SET bp.award_result_code = DECODE (
7770                                        bp.effect_on_funds_code,
7771                                        'I',decode(bp.result_code,null,'P78',bp.result_code ),
7772                                        'D', DECODE (
7773                                                bp.a_funds_control_level_code,
7774                                                'N', 'P76',
7775                                                'D', DECODE (
7776                                                        SIGN (
7777                                                           NVL (bp.award_budget_posted, 0)
7778                                                           - NVL (bp.award_actual_posted, 0)
7779                                                           - NVL (bp.award_enc_posted, 0)
7780                                                           - NVL (bp.award_actual_approved, 0)
7781                                                           - NVL (bp.award_actual_pending, 0)
7782                                                           - NVL (bp.award_enc_approved, 0)
7783                                                           - NVL (bp.award_enc_pending, 0)),
7784                                                        -1, 'P61',
7785                                                        'P60'),
7786                                                'B', DECODE (
7787                                                        SIGN (
7788                                                           NVL (bp.award_budget_posted, 0)
7789                                                           - NVL (bp.award_actual_posted, 0)
7790                                                           - NVL (bp.award_enc_posted, 0)
7791                                                           - NVL (bp.award_actual_approved, 0)
7792                                                           - NVL (bp.award_actual_pending, 0)
7793                                                           - NVL (bp.award_enc_approved, 0)
7794                                                           - NVL (bp.award_enc_pending, 0)),
7795                                                        -1, 'F90',
7796                                                        'P60')))
7797        WHERE bp.packet_id = x_packetid
7798          AND bp.effect_on_funds_code IN ('D', 'I')
7799          AND bp.status_code = 'P';
7800 
7801 
7802 
7803       UPDATE gms_bc_packets
7804          SET result_code = NVL (award_result_code, 'F53'),
7805              status_code = DECODE (status_code,'P', DECODE (SUBSTR (NVL (award_result_code, 'F53'), 1, 1),'F', DECODE(x_mode,'C','F','R'),status_code),status_code)
7806        WHERE packet_id = x_packetid
7807          AND effect_on_funds_code IN ('D', 'I')
7808          AND status_code = 'P';
7809 
7810        -- Call new procedure RAW_BURDEN_FAILURE to handle raw-burden failure ...
7811        RAW_BURDEN_FAILURE(x_packetid,     -- Packet_id
7812                           g_derived_mode, -- Mode
7813                           'AWD'           -- Level
7814                          );
7815 
7816 
7817       -- Call new FUNCTION Full_mode_failure to handle full mode and scenario of failing all cdl when one cdl failed ..
7818       If FULL_MODE_FAILURE(x_packetid,     -- Packet_id
7819                         g_mode,         -- Mode , use g_mode
7820                         'AWD'           -- Level
7821                          ) then
7822          If g_partial_flag = 'N' then
7823             -- If full mode, transactions have failed .. exit FC
7824             GOTO END_OF_FC_PROCESS;
7825          End if;
7826       End If;
7827 
7828          -- Bug 3426509 : Added following code to update the last advisory result code to result_code column
7829          -- e.g. if transaction passed funds check in advisory mode at Task and Resource Level , then the
7830          -- result_code will hold the result_code of Task Level funds check (i.e. 'P65').
7831 
7832          UPDATE gms_bc_packets
7833             SET result_code = DECODE (top_task_result_code,'P80', 'P80',
7834                                  DECODE (task_result_code,'P65', 'P65',
7835                                     DECODE (res_grp_result_code,'P73', 'P73',
7836                                        DECODE (res_result_code,'P69', 'P69',
7837                                           result_code))))
7838           WHERE packet_id = x_packetid
7839             AND effect_on_funds_code IN ('D', 'I')
7840             AND status_code = 'P'
7841             AND SUBSTR (result_code,1,1) <> 'F'
7842             AND result_code <> 'P61'
7843             AND (   top_task_result_code = 'P80'
7844                  OR task_result_code = 'P65'
7845                  OR res_grp_result_code = 'P73'
7846                  OR res_result_code = 'P69'
7847                 );
7848 
7849         <<END_OF_FC_PROCESS>>
7850 --================================================================================--
7851 -- Data clean up code from summary tables after Amount calculation
7852 --================================================================================--
7853       g_error_stage := 'FC PR : DELETE';
7854 		DELETE      gms_bc_packets_summary
7855 		      WHERE packet_id = x_packetid;
7856 		DELETE      gms_bc_packets_bvid
7857 		      WHERE packet_id = x_packetid;
7858 
7859        -- Added commit for the base bug 3848201
7860        commit;
7861 
7862 --  Bug 2092791
7863 
7864 --  Bug 2176230
7865 --  Delete Record from gms_bc_packet_arrival_order able to ensure that transactions inserted into
7866 --  gms_bc_packets in check funds mode should not be accounted by any other subsequent packets.
7867 
7868       IF x_mode = 'C' THEN
7869         DELETE 	    gms_bc_packet_arrival_order
7870 		      WHERE packet_id = x_packetid;
7871 	  END IF;
7872 
7873        COMMIT; -- Bug 4053891 (to release locks ...only set for REQ/PO/AP/FAB/Interface)
7874               -- lock by lock_budget_versions; If there is a failure then the commit happens
7875               -- in gms_fck.when_other commit;
7876 
7877       RETURN (TRUE);
7878       IF g_debug = 'Y' THEN
7879       	gms_error_pkg.gms_debug ('AFTER FUNDS CHECK', 'C');
7880       END IF;
7881    END gms_fc_process;
7882 
7883 --------------------------------------------------------------------------------------------------------------------------------------------------
7884 --  This function calls all the procedures and functions for funds checker
7885 --------------------------------------------------------------------------------------------------------------------------------------------------
7886   -- Funds Check Processor
7887    FUNCTION gms_fcp (
7888       x_sobid         IN       gms_bc_packets.set_of_books_id%TYPE,
7889       x_packetid      IN       gms_bc_packets.packet_id%TYPE,
7890       x_mode          IN       VARCHAR2,
7891       x_partial       IN       VARCHAR2,
7892       x_arrival_seq   IN       gl_bc_packet_arrival_order.arrival_seq%TYPE,
7893       x_err_code      OUT NOCOPY      NUMBER,
7894       x_err_buff      OUT NOCOPY      VARCHAR2)
7895       RETURN BOOLEAN IS
7896       x_error_code       NUMBER;
7897       x_error_buff       VARCHAR2(2000);
7898       x_packetid_ursvd   NUMBER;
7899       x_stage            VARCHAR2 (100);
7900    BEGIN
7901       g_error_procedure_name := 'Gms_fcp';
7902       x_error_code   :=0;
7903       x_error_buff   :=null;
7904 
7905       IF g_debug = 'Y' THEN
7906       	gms_error_pkg.gms_debug (g_error_procedure_name||':x_sobid,x_arrival_seq-'||x_sobid||','||x_arrival_seq, 'C');
7907       END IF;
7908 
7909       -- If Mode is Unreservation, assign Packet ID to Unreservation Packet ID
7910       -- and initialize Packet ID to 0. This is done here to prevent the approved
7911       -- packet from accidentally being updated to status 'Fatal' in case a fatal
7912       IF x_mode = 'U' THEN
7913          x_packetid_ursvd := x_packetid;
7914       ELSE
7915          x_packetid_ursvd := 0;
7916       END IF;
7917 
7918 -- ------------------------------------------------------------
7919      -- Setup and Summarization
7920 -- ('********* Calling GMS_SETUP *****************');
7921 -- ------------------------------------------------------------
7922       gms_error_pkg.gms_debug (g_error_procedure_name||'Calling gms_setup', 'C' );
7923       g_error_stage := 'gms_setup';
7924       IF NOT gms_setup (x_packetid, x_mode, x_partial,x_err_code,x_err_buff) THEN
7925          RETURN (FALSE);
7926       END IF;
7927 
7928       -- Bug 2176230
7929 	  -- ********************************************************************************
7930 	  -- NOTE :- Don't Put Any Commit after this point till gms_fc_process is complete,
7931 	  --         Reason being for funds checking in C Mode (check funds mode)
7932 	  --         transactions in gms_bc_packets should not be accounted by any other
7933 	  --         subsequent packet.  (Applicable for x_mode = R,U,C,E)
7934 	  -- ********************************************************************************
7935 
7936 
7937       g_error_stage := 'gms_fc_process';
7938       IF x_mode IN ('S', 'B') THEN
7939          IF g_debug = 'Y' THEN
7940          	gms_error_pkg.gms_debug ('BEFORE BASELINE', 'C');
7941          END IF;
7942          budget_fundscheck (x_packetid,x_err_code,x_err_buff);
7943          status_code_update (x_packetid, x_mode);
7944          IF g_debug = 'Y' THEN
7945          	gms_error_pkg.gms_debug ('AFTER BASELINE', 'C');
7946          END IF;
7947       ELSE
7948          IF NOT gms_fc_process (x_packetid,x_arrival_seq,x_mode) THEN		 -- Bug 2176230
7949             RETURN (FALSE);
7950          END IF;
7951       END IF;
7952       IF g_debug = 'Y' THEN
7953       	gms_error_pkg.gms_debug ( 'GMS_FCP-After Calling gms_fc_process', 'C' );
7954       	gms_error_pkg.gms_debug ( 'GMS_FCP-END', 'C' );
7955       END IF;
7956       RETURN (TRUE);
7957    END gms_fcp;
7958 
7959 --------------------------------------------------------------------------------------------------------------------------------------------------
7960 -- This process generates the status_code and return_code for funds checker
7961 --------------------------------------------------------------------------------------------------------------------------------------------------
7962 -- Get Return Status
7963   -- Return Code can be of one of the following values :
7964   --
7965   --     Code  Meaning   Description
7966   --     --  -----   --------------------
7967   --      S    Success   All transactions in packet pass Funds
7968   --                     Check or Funds Reservation
7969   --
7970   --      A    Advisory  All transactions in packet pass Funds
7971   --                     Check or Funds Reservation; but some
7972   --                     with Advisory warnings
7973   --
7974   --      F    Failure   All transactions in packet fail Funds
7975   --                     Check or Funds Reservation (partial
7976   --                     reservation allowed)
7977   --                     OR
7978   --                     One or more transactions in packet fail
7979   --                     Funds Check or Funds Reservation
7980   --                     (partial reservation not allowed)
7981   --
7982   --      P    Partial   Only part of the transactions in packet
7983   --                     pass Funds Check or Funds Reservation
7984   --                     (partial reservation allowed only)
7985   --
7986   --      T    Fatal     Irrecoverable error detected that
7987   --                     prevents funds check or reservation
7988   --                     from proceeding
7989   --
7990 -- ------------------------------------------------------------------------------------------------
7991 -- To Exit out NOCOPY of gms_fck in case of non_gms application calls.
7992 -- Bug 1966096. Funds check for non-sponsor projects and GL Transactions should happen base
7993 -- on the core functionality. Since their no entry in gms_bc_packets as those transactions
7994 -- gms funds checker should not return false which fail the above transactions.
7995 -- ------------------------------------------------------------------------------------------------
7996 
7997    FUNCTION gms_return_code (
7998       x_packetid      IN       NUMBER,
7999       x_mode          IN       CHAR,
8000       x_partial       IN       CHAR,
8001       x_return_code   IN OUT NOCOPY   VARCHAR2,
8002       x_e_code        IN OUT NOCOPY   VARCHAR2,
8003       x_err_buff      OUT NOCOPY      VARCHAR2)
8004       RETURN BOOLEAN IS
8005       x_err_code      NUMBER;
8006       x_result_code   CHAR;
8007    BEGIN
8008 
8009    g_error_procedure_name := 'gms_return_code';
8010       x_err_code := 0;
8011 
8012    IF x_mode IN ('B', 'S') THEN -- I
8013          g_error_stage := 'submit/baseline';
8014 
8015          BEGIN
8016             SELECT 0
8017               INTO x_err_code
8018               FROM DUAL
8019              WHERE EXISTS ( SELECT 'X'
8020                               FROM gms_bc_packets
8021                              WHERE packet_id = x_packetid
8022                               AND status_code IN ('S', 'B')); --Bug Fix 1350100 Change status_code from 'A'
8023                                                                --to 'B' to fix bug 2138376
8024          EXCEPTION
8025             WHEN NO_DATA_FOUND THEN
8026                x_err_code := 1;
8027          END;
8028 
8029          SELECT DECODE (x_err_code, 0, 'S', 'H')
8030            INTO x_e_code
8031            FROM DUAL;
8032 
8033       ELSIF x_mode = 'E' THEN
8034            g_error_stage := 'Encumbrance';
8035 
8036          BEGIN
8037             SELECT 'F'
8038               INTO x_result_code
8039               FROM DUAL
8040              WHERE EXISTS ( SELECT result_code
8041                               FROM gms_bc_packets
8042                              WHERE packet_id = x_packetid
8043                                AND SUBSTR (result_code, 1, 1) = 'F');
8044          EXCEPTION
8045             WHEN NO_DATA_FOUND THEN
8046                x_result_code := 'P';
8047          END;
8048 
8049          IF x_result_code = 'F' THEN
8050 	    IF g_debug = 'Y' THEN
8051 		gms_error_pkg.gms_debug ( 'Calling status_code_update for encumbrances', 'C' );
8052 	    END IF;
8053 
8054             status_code_update (x_packetid, 'E');
8055             x_e_code := 'H';
8056 
8057          ELSE -- update gms_bc_packets with approved status
8058             IF g_debug = 'Y' THEN
8059             	gms_error_pkg.gms_debug ( 'Calling status_code_update for encumbrances', 'C' );
8060             END IF;
8061             status_code_update (x_packetid, 'E');
8062             x_e_code := 'S';
8063          END IF;
8064     END IF; -- I
8065 
8066     g_error_stage := 'Common';
8067 
8068       BEGIN
8069          SELECT 1
8070            INTO x_err_code
8071            FROM DUAL
8072           WHERE EXISTS ( SELECT 'X'
8073                            FROM gms_bc_packets
8074                           WHERE packet_id = x_packetid
8075                             AND SUBSTR (result_code, 1, 1) = 'F'
8076                             AND status_code = 'T');
8077          x_return_code := 'T';
8078          RETURN TRUE;   			--Bug 2006221
8079 
8080       EXCEPTION
8081          WHEN NO_DATA_FOUND THEN
8082             IF x_partial <> 'Y' THEN
8083                BEGIN
8084                   SELECT 1
8085                     INTO x_err_code
8086                     FROM DUAL
8087                    WHERE EXISTS ( SELECT 'X'
8088                                     FROM gms_bc_packets
8089                                    WHERE packet_id = x_packetid
8090                                      AND SUBSTR (result_code, 1, 1) = 'F');
8091                EXCEPTION
8092                   WHEN NO_DATA_FOUND THEN
8093                      x_err_code := 0;
8094                END;
8095 
8096                IF x_err_code = 1 THEN
8097 
8098 -- There is no need to check for the document type , IF g_doc_type in ('REQ','PO','AP') THEN
8099                   x_return_code := 'F'; --21-SEP-2000
8100                ELSE
8101                   x_return_code := 'S';
8102                END IF;
8103             ELSE
8104                x_return_code := 'S';
8105             END IF;
8106             RETURN TRUE;
8107       END;
8108    EXCEPTION
8109       WHEN OTHERS THEN
8110          RETURN FALSE;
8111          RAISE;
8112    END gms_return_code;
8113 
8114 
8115    /* =========================================================================
8116       -- Bug : 2557041 - Added for IP check funds Enhancement
8117       -- Modified for PA-GMS C-FC Integration ..
8118 
8119       g_mode : Funds Checker Operation Mode.
8120                      C  - Check funds
8121                      R  - Reserve funds.
8122                      U  - Un-reserve  (only for REQ,PO and AP)
8123                      B  - Called from budget baseline process  (Processed like check funds)
8124                      S  - Called from Budget submission     (Processed like check funds)
8125                      X  - Called for Expenditure Items funds check
8126                      I  - Called for Supplier Cost funds check during interface AP->PA
8127                      A  - Adjustments for PO /REQ
8128 
8129       This procedure is called from gms_fck function.
8130 
8131       This procedure initializes following global variables:
8132       g_ip_fc_flag
8133 		-  'Y' Funds check is called in IP check funds mode
8134  		-  'N' Funds check is called in NON-IP mode
8135 
8136       g_gl_bc_pkt_sponsored_count  (for AP/PO/REQ)
8137 		-   0  NON-GMS related transactions are being funds checked
8138    		-  >0  GMS related transactions are being funds checked
8139 
8140       g_pa_addition_flag_t_count (for AP)
8141                 -  Count of AP invoice being funds checked that has pa_addition_flag 'T'
8142 
8143       g_non_gms_txn
8144                 -   TRUE NON-GMS related transactions are being funds checked
8145                 -   FALSE GMS related transactions are being funds checked
8146 
8147      ========================================================================= */
8148 
8149 PROCEDURE gms_fck_init(p_partial_flag IN VARCHAR2) IS
8150 
8151 -- Note: Award and Expenditure type are the additional filter criterias
8152 --       in misc_gms_insert. Those will not be added here ..helps fail
8153 --       transactions in case of incorrect award number and expenditure type.
8154 
8155 -- R12 FundsCheck Management Uptake : Shifted IP/AP/PO/REQ/FAB cursor logic to procedure copy_gl_pkt_to_gms_pkt and
8156 -- merged with the selects which fetches IP/AP/PO/REQ/FAB record for inserting into gms_bc_packets.
8157 -- Added below cursor to derive g_doc_type based on the data inserted into gms_bc_packets by procedure copy_gl_pkt_to_gms_pkt.
8158 
8159    CURSOR C_count_rec IS
8160    SELECT count(*) gms_txn_count,
8161           SUM(DECODE(gms.document_type,'REQ',1,0)) req_count,
8162           SUM(DECODE(gms.document_type,'PO',1,0))  po_count,
8163           SUM(DECODE(gms.document_type,'AP',1,0))  ap_count,
8164           SUM(DECODE(gms.document_type,'FAB',1,0)) fab_count
8165      FROM gms_bc_packets gms
8166     WHERE gms.packet_id= g_packet_id;
8167 
8168    -- R12 FundsCheck Management Uptake : New variables defined and used in deriving g_doc_type
8169    l_gms_txn_count  NUMBER;
8170    l_req_count	    NUMBER;
8171    l_po_count       NUMBER;
8172    l_ap_count       NUMBER;
8173    l_fab_count      NUMBER;
8174 
8175    -- R12 FundsCheck Management Uptake : Deleted obsolete variables
8176 
8177 BEGIN
8178   g_error_procedure_name := 'Gms_fck_init';
8179   g_error_stage := 'Initalize Global Variables';
8180 
8181   IF g_debug = 'Y' THEN
8182      gms_error_pkg.gms_debug (g_error_procedure_name||':'||g_error_stage,'C');
8183   END IF;
8184 
8185   -- ==================================
8186   -- 1. Initalize global variables ...
8187   -- ==================================
8188      g_non_gms_txn 		 := FALSE;
8189      g_bc_packet_has_P82_records := 'N' ;
8190 
8191   -- ==================================
8192   -- 2. Derive doc_type ...
8193   -- ==================================
8194      g_error_stage := 'Derive doc_type';
8195 
8196      IF g_debug = 'Y' THEN
8197         gms_error_pkg.gms_debug (g_error_procedure_name||':'||g_error_stage,'C');
8198      END IF;
8199 
8200     If    g_mode = 'X' then -- p_mode check - I
8201           g_doc_type := 'EXP';
8202     Elsif g_mode = 'I' then
8203           g_doc_type := 'AP';
8204     Elsif g_mode = 'E' then
8205           g_doc_type := 'ENC';
8206     Elsif g_mode in ('S','B') then
8207           g_doc_type := 'ALL';
8208     Elsif g_mode in ('R','U','C','A') then
8209 
8210       -- 'A' mode is used fro PO/REQ adjustments ,it should be considered as reserve 'R'
8211       IF g_mode = 'A' THEN
8212          g_mode := 'R';
8213       END IF;
8214 
8215 	-- R12 FundsCheck Management Uptake : Deleted existing logic which was deriving g_doc_type and
8216 	-- g_non_gms_txn based on records in GL_bc_packets/AP/PO/REQ tables.
8217 	-- With new architecture the gl_bc_packets/AP/PO/REQ validations will be performed during
8218 	-- insertion of GMS packets in main session and current logic derives document type based on
8219 	-- the data inserted into gms_bc_packets for current packet_id.
8220 
8221         OPEN  c_count_rec;
8222 	FETCH c_count_rec INTO l_gms_txn_count,l_req_count,l_po_count,l_ap_count,l_fab_count;
8223         CLOSE c_count_rec;
8224 
8225         IF l_gms_txn_count = 0 THEN
8226 	   g_non_gms_txn := TRUE;
8227 	ELSE
8228 	   IF NVL(l_req_count,0) <> 0  AND NVL(l_po_count,0) = 0 then
8229 	       g_doc_type := 'REQ';
8230    	   ELSIF NVL(l_po_count,0) <> 0 then
8231 	       g_doc_type := 'PO';
8232 	   ELSIF NVL(l_ap_count,0) <> 0 then
8233 	       g_doc_type := 'AP';
8234 	   ELSIF NVL(l_fab_count,0) <> 0 then
8235 	       g_doc_type := 'FAB';
8236            END IF;
8237         END IF;
8238 
8239        IF g_debug = 'Y' THEN
8240            gms_error_pkg.gms_debug (g_error_procedure_name||':'||'Derived value of document type '||g_doc_type,'C');
8241        END IF;
8242 
8243     End if; -- p_mode check - I
8244 
8245     IF g_debug = 'Y' THEN
8246        gms_error_pkg.gms_debug (g_error_procedure_name||':Total txns. in gms_bc_packets -'||l_gms_txn_count, 'C');
8247        gms_error_pkg.gms_debug (g_error_procedure_name||':Total REQ txns in gms_bc_packets -'||l_req_count,'C');
8248        gms_error_pkg.gms_debug (g_error_procedure_name||':Total PO txns in gms_bc_packets -'||l_po_count,'C');
8249        gms_error_pkg.gms_debug (g_error_procedure_name||':Total AP txns in gms_bc_packets -'||l_ap_count,'C');
8250        gms_error_pkg.gms_debug (g_error_procedure_name||':Total FAB txns in gms_bc_packets -'||l_fab_count,'C');
8251        gms_error_pkg.gms_debug (g_error_procedure_name||':Document type (ALL indicates Submit/Baseline)-'||g_doc_type,'C');
8252     END IF;
8253 
8254   -- ======================================
8255   -- 3. Initalize global variables - part B
8256   -- ======================================
8257     -- Derive mode ..
8258 
8259     If g_mode in ('X','I') then -- X: Expenditure Items, I: Interface
8260          g_derived_mode := 'R';
8261       else
8262          g_derived_mode := g_mode;
8263     End If;
8264 
8265 
8266     -- Derive partial flag ..
8267 
8268       If g_mode in ('X','E')         then  -- Expenditures/Encumbrances
8269 
8270          g_partial_flag := 'Y';
8271 
8272       Elsif g_mode in ('S','B','I')  then  -- Submit/Baseline/Interface
8273 
8274          g_partial_flag := 'N';
8275 
8276       Elsif g_mode in ('R','U','C') and g_doc_type in ('REQ','PO','AP') then -- AP/PO/REQ
8277 
8278          g_partial_flag := p_partial_flag;
8279 
8280       Elsif g_mode in ('R','U','C') and g_doc_type = 'FAB' then  -- FAB processing
8281 
8282          g_partial_flag := 'N';
8283 
8284       End If;
8285 
8286 
8287       IF g_debug = 'Y' THEN
8288       	  gms_error_pkg.gms_debug ('GMS_FCK-derived values :g_derived_mode,g_partial_flag:'
8289 						||g_derived_mode||','||g_partial_flag,'C');
8290       END IF;
8291 
8292     -- Initialize currency precision variables ..
8293     --  pa_currency.set_currency_info;
8294     -- Moved to gms_fck_init (stage 301), bug 5074028
8295 
8296 END gms_fck_init;
8297 
8298 ----------------------------------------------------------------------------------+
8299 PROCEDURE Si_adjustments ( x_packet_id in NUMBER, x_pkt_row OUT NOCOPY NUMBER )
8300 is
8301 
8302    l_pkt_row                  number ;
8303    l_stage                    varchar2(10) ;
8304    /*l_row_id                   VARCHAR2 (200); Commented for bug 6236117 */
8305    l_inv_encumbrance_type_id  financials_system_parameters.inv_encumbrance_type_id%TYPE;
8306    /*l_bc_packet_id             NUMBER; Commented for bug 6236117 */
8307 
8308    CURSOR financials_options   IS
8309    SELECT inv_encumbrance_type_id
8310      FROM financials_system_parameters;
8311 
8312    CURSOR c_po_doc (p_inv_encumbrance_type_id NUMBER) IS
8313    SELECT pkt.ROWID              pkt_row_id,
8314           pod.po_distribution_id po_dist_id,
8315           pod.po_header_id       po_header_id,
8316 	  pod.project_id         project_id,
8317 	  pod.task_id            task_id,
8318 	  adl.award_id           award_id
8319      FROM gms_bc_packets               pkt,
8320           gl_bc_packets                gl,
8321           ap_invoice_distributions_all ap,
8322           po_distributions_all         pod,
8323           gms_award_distributions      adl
8324       WHERE pkt.packet_id              = x_packet_id
8325         AND pkt.document_type          = 'AP'
8326         AND gl.packet_id               = pkt.packet_id
8327         AND ROWIDTOCHAR (gl.ROWID)     = pkt.gl_bc_packets_rowid
8328         AND gl.encumbrance_type_id     <> p_inv_encumbrance_type_id
8329         AND   NVL (pkt.entered_cr, 0) + NVL (pkt.entered_dr, 0) <> 0
8330         AND pod.po_distribution_id     = ap.po_distribution_id
8331         AND ap.distribution_line_number= pkt.document_distribution_id
8332         AND ap.invoice_id              = pkt.document_header_id
8333 	    and pod.award_id               = adl.award_set_id
8334 	    and adl.adl_line_num           = 1
8335         and nvl(pkt.burden_adjustment_flag,'N') = 'N'
8336       FOR UPDATE OF pkt.document_type,
8337                     pkt.document_header_id,
8338                     pkt.document_distribution_id NOWAIT;
8339 
8340 /* Commented for the bug 6236117
8341 CURSOR sum_aprec IS
8342    SELECT   packet_id,
8343             document_header_id,
8344             document_distribution_id,
8345             award_id,
8346             expenditure_type,
8347             document_type,
8348             SUM (  NVL (entered_dr, 0) - NVL (entered_cr, 0)) raw_cost
8349       FROM gms_bc_packets
8350      WHERE packet_id = x_packet_id
8351        and nvl(burden_adjustment_flag,'N') = 'N'
8352        AND EXISTS ( SELECT 1
8353                       FROM gms_bc_packets
8354                      WHERE packet_id = x_packet_id
8355                        AND document_type = 'AP')
8356       GROUP BY packet_id,
8357                document_header_id,
8358                document_distribution_id,
8359                award_id,
8360                expenditure_type,
8361                document_type;
8362 
8363    CURSOR min_bc_packet_id (
8364       x_packet_id                  NUMBER,
8365       x_doc_type                   VARCHAR2,
8366       x_document_header_id         NUMBER,
8367       x_document_distribution_id   NUMBER,
8368       x_award_id                   NUMBER,
8369       x_expenditure_type           VARCHAR2
8370    )
8371    IS
8372       SELECT MIN (bc_packet_id)
8373         FROM gms_bc_packets
8374        WHERE packet_id = x_packet_id
8375          AND document_type = x_doc_type
8376          AND document_header_id = x_document_header_id
8377          AND document_distribution_id = x_document_distribution_id
8378          AND award_id = x_award_id
8379          AND expenditure_type = x_expenditure_type;
8380 
8381 Ends commented for bug 6236117 */
8382 
8383 BEGIN
8384 
8385   g_error_procedure_name := 'Si_adjustments';
8386 
8387    L_pkt_row := 0 ;
8388    L_stage    := 'BEGIN' ;
8389 
8390    OPEN financials_options;
8391    FETCH financials_options INTO l_inv_encumbrance_type_id;
8392    CLOSE financials_options;
8393 
8394    FOR bc_packets IN c_po_doc (l_inv_encumbrance_type_id)
8395    LOOP
8396       UPDATE gms_bc_packets
8397          SET document_type = 'PO',
8398              document_header_id = bc_packets.po_header_id,
8399              document_distribution_id = bc_packets.po_dist_id,
8400 	     project_id         = bc_packets.project_id ,
8401 	     task_id            = bc_packets.task_id ,
8402 	     award_id           = bc_packets.award_id
8403        WHERE ROWID = bc_packets.pkt_row_id;
8404    END LOOP;
8405 
8406    Delete from gms_bc_packets
8407     Where packet_id     = x_packet_id
8408       And document_type = 'AP'
8409       And bc_packet_id in
8410      ( select a.bc_packet_id
8411         from gms_bc_packets a,
8412              ap_invoice_distributions_all apd
8413        where a.packet_id = x_packet_id
8414          and a.document_type = 'AP'
8415          and a.document_header_id = apd.invoice_id
8416          and a.document_distribution_id = apd.distribution_line_number
8417          and NVL(apd.pa_addition_flag,'X') = 'T'
8418 	union /* BUG 14216205 : Added the union for SAT */
8419        select a.bc_packet_id
8420         from GMS_BC_PACKETS a,
8421              AP_SELF_ASSESSED_TAX_DIST_ALL apsat
8422        where a.packet_id = x_packet_id
8423          and a.DOCUMENT_TYPE = 'AP'
8424          and a.DOCUMENT_HEADER_ID = APSAT.INVOICE_ID
8425          and a.DOCUMENT_DISTRIBUTION_ID = APSAT.DISTRIBUTION_LINE_NUMBER
8426          and NVL(APSAT.PA_ADDITION_FLAG,'X') = 'T') ;
8427 
8428 /* Commented for bug 6236117
8429    FOR bc_packets IN sum_aprec
8430    LOOP
8431 
8432    -- -----------------------------------------------------------
8433    -- The 1st distribution line is updated with the total CR and
8434    -- DB amount.
8435    -- -----------------------------------------------------------
8436 
8437       OPEN min_bc_packet_id (
8438          bc_packets.packet_id,
8439          bc_packets.document_type,
8440          bc_packets.document_header_id,
8441          bc_packets.document_distribution_id,
8442          bc_packets.award_id,
8443          bc_packets.expenditure_type
8444       );
8445       FETCH min_bc_packet_id INTO l_bc_packet_id;
8446       CLOSE min_bc_packet_id;
8447 
8448       IF bc_packets.raw_cost >= 0
8449       THEN
8450          UPDATE gms_bc_packets
8451             SET entered_dr = bc_packets.raw_cost,
8452                 entered_cr = 0
8453           WHERE packet_id = x_packet_id
8454             AND document_type = bc_packets.document_type
8455             AND bc_packet_id = l_bc_packet_id;
8456 
8457 
8458         -- ---------------------------------------------------------------
8459         -- Since the 1st dist line is updated with the  total other
8460         -- lines should be updated with 0. This is done for the same AP
8461         -- Distribution Line. BC packets gets data in multiple lines for
8462         -- the same distribution line.
8463         -- ---------------------------------------------------------------
8464          UPDATE gms_bc_packets
8465             SET entered_cr = 0,
8466                 entered_dr = 0
8467           WHERE packet_id = x_packet_id
8468             AND bc_packet_id > l_bc_packet_id
8469             AND document_type = bc_packets.document_type
8470             AND document_header_id = bc_packets.document_header_id
8471             AND document_distribution_id = bc_packets.document_distribution_id
8472             AND award_id = bc_packets.award_id
8473             AND expenditure_type = bc_packets.expenditure_type;
8474 
8475       ELSIF bc_packets.raw_cost < 0
8476       THEN
8477          UPDATE gms_bc_packets
8478             SET entered_cr = bc_packets.raw_cost * -1,
8479                 entered_dr = 0
8480           WHERE packet_id = x_packet_id
8481             AND document_type = bc_packets.document_type
8482             AND bc_packet_id = l_bc_packet_id;
8483 
8484          -- ---------------------------------------------------------------
8485          -- Since the 1st dist line is updated with the credit total other
8486          -- lines should be updated with 0. This is done for the same AP
8487          -- Distribution Line. BC packets gets data in multiple lines for
8488          -- the same distribution line.
8489          -- ---------------------------------------------------------------
8490          UPDATE gms_bc_packets
8491             SET entered_cr = 0,
8492                 entered_dr = 0
8493           WHERE packet_id = x_packet_id
8494             AND bc_packet_id > l_bc_packet_id
8495             AND document_type = bc_packets.document_type
8496             AND document_header_id = bc_packets.document_header_id
8497             AND document_distribution_id = bc_packets.document_distribution_id
8498             AND award_id = bc_packets.award_id
8499             AND expenditure_type = bc_packets.expenditure_type;
8500       END IF;
8501    END LOOP;
8502 
8503    Ends commenting for bug 6236117 */
8504 
8505    l_stage := 'SELECT' ;
8506 
8507    select 1
8508      into l_pkt_row
8509      from dual
8510     where exists ( select 1 from gms_bc_packets
8511                    where packet_id = x_packet_id ) ;
8512 
8513    x_pkt_row := l_pkt_row ;
8514 EXCEPTION
8515 	WHEN NO_DATA_FOUND THEN
8516            IF l_stage = 'SELECT' THEN
8517 	        X_pkt_row := 0 ;
8518            ELSE
8519               RAISE ;
8520            END IF ;
8521 	WHEN OTHERS THEN
8522             RAISE ;
8523 
8524 END SI_ADJUSTMENTS;
8525 
8526 -- ------------------------------------ R12 Start ------------------------------------------------+
8527 -- R12 Changes: New procedure
8528 -- --------------------------------------------------------------------------------+
8529 -- This procedure will update the following columns in gms_bc_packets: serial_id,
8530 -- session_id,packet_id,period_name,period_year,period_num,account_type and status.
8531 -- Status will be upated from I to P. Called from gms_fck
8532 -- --------------------------------------------------------------------------------+
8533 PROCEDURE Synch_gms_gl_packets(p_packet_id IN Number)
8534 IS
8535  l_gms_packet_id gms_bc_packets.packet_id%type;
8536 
8537 -- This cursor will fetch only if called for AP/PO/REQ
8538  CURSOR   get_temp_packet_id Is
8539  Select   gbc.packet_id
8540    from   gms_bc_packets gbc
8541   where  gbc.source_event_id in
8542              (select glbc.event_id
8543                 from gl_bc_packets glbc
8544                where glbc.packet_id = p_packet_id)
8545     and  gbc.status_code ='I'
8546     and  gbc.document_type in ('AP','PO','REQ')
8547     and rownum =1;
8548 
8549 /* Bug 5285217 : Created the cursor c_gl_bc_pkt. This cursor selects all the records from gl_bc_packets for the current packet_id. */
8550 cursor c_gl_bc_pkt IS
8551 select p_packet_id packet_id,'P' status_code,
8552 			 glbc.session_id session_id,glbc.serial_id serial_id,
8553 			 glbc.je_category_name je_category_name,
8554 			 glbc.je_source_name je_source_name,glbc.period_name period_name,
8555 			 glbc.period_year period_year,glbc.period_num period_num,
8556 			 glbc.account_type account_type,
8557 			 rowidtochar(glbc.rowid) gl_rowid,
8558                          glbc.event_id event_id,
8559                          glbc.source_distribution_id_num_1 source_distribution_id_num_1,
8560                          glet.encumbrance_type_key encumbrance_type_key,
8561                          glbc.accounted_dr entered_dr , -- Bug 5614467
8562                          glbc.accounted_cr entered_cr, -- Bug 5614467
8563 			 source_distribution_type
8564 		  from   gl_bc_packets glbc,
8565 		         gl_encumbrance_types glet
8566 		  where  glbc.packet_id           = p_packet_id
8567 		  and    glbc.encumbrance_type_id = glet.encumbrance_type_id;
8568 
8569 l_dist_id  pa_bc_packets.document_header_id%type;
8570 
8571 BEGIN
8572 
8573   IF g_debug = 'Y' THEN
8574    	gms_error_pkg.gms_debug ('Synch_gms_gl_packets Strat : packet_id = '|| p_packet_id,'C');
8575   END IF;
8576 
8577  -- Get the packet_id that was establised earlier ..
8578  OPEN get_temp_packet_id;
8579  FETCH get_temp_packet_id INTO l_gms_packet_id;
8580 
8581  IF  get_temp_packet_id%FOUND THEN
8582 
8583          IF g_debug = 'Y' THEN
8584    	    gms_error_pkg.gms_debug ('Synch_gms_gl_packets Strat : Previously establised packet is = '|| l_gms_packet_id,'C');
8585          END IF;
8586 
8587 	/* Bug 5250793 : Added a join with gl_encumbrance_types so that the gl_bc_packets_rowid on gms_bc_packets is updated
8588 	   correctly for an invoice matched to a PO scenario. Before this change , the same gl_bc_packets_rowid was updated on
8589 	   gms_bc_packets for both the PO reversal and AP reserve records irrespective of the corresponding rowid on gl_bc_packets.*/
8590 
8591 	 -- Update gms_bc_packets data
8592 	 /* Bug 5285217 : Changed the code to use "FOR" loop so that gl_bc_packets_rowid on gms_bc_packets is updated correctly
8593 	    for an invoice matched to a PO with Quantity Variance Scenario. Before this change , the same gl_bc_packets_rowid was
8594 	    updated on gms_bc_packets for both the invoice reserve and the quantity variance reserve records irrespective of
8595 	    the corresponding rowid on gl_bc_packets.*/
8596 
8597 	     FOR glbcrec in c_gl_bc_pkt LOOP
8598 
8599                  IF g_debug = 'Y' THEN
8600 	     	    gms_error_pkg.gms_debug ('Synch_gms_gl_packets:  glbcrec.source_distribution_type:'|| glbcrec.source_distribution_type,'C');
8601                  END IF;
8602 
8603                 If glbcrec.source_distribution_type = 'AP_PREPAY' then
8604                    -- This is reqd. as we cannot access ap_prepay_app_dists here .. autonomous ..
8605 
8606                   IF g_debug = 'Y' THEN
8607 	     	     gms_error_pkg.gms_debug ('Synch_gms_gl_packets:  Derive prepay dist id ','C');
8608                   END IF;
8609 
8610                    For x in g_ap_prepay_app_dist_id.FIRST..g_ap_prepay_app_dist_id.LAST loop
8611 
8612                        IF g_debug = 'Y' THEN
8613 	     	          gms_error_pkg.gms_debug ('Synch_gms_gl_packets: g_ap_prepay_app_dist_id(x)'||g_ap_prepay_app_dist_id(x),'C');
8614                        END IF;
8615 
8616 	               IF g_ap_prepay_app_dist_id(x) = glbcrec.source_distribution_id_num_1 then
8617                           -- basically, if ap_prepay_app_dists.ap_prepay_dist_id is same as in gl
8618                           -- assign ap_prepay_app_dists.invoice_distribution_id to l_dist_id
8619 
8620                           l_dist_id := g_doc_dist_id_tab(x);
8621 
8622                           If g_debug = 'Y' THEN
8623                               gms_error_pkg.gms_debug ('Synch_gms_gl_packets: Found prepay dist,its:'||l_dist_id,'C');
8624                           End If;
8625 
8626                           EXIT;
8627                         END IF;
8628 
8629 	           End loop;
8630                 End If; --If glbcrec.source_distribution_type = 'AP_PREPAY' then
8631 
8632 		 -- Update gms_bc_packets data
8633 		 Update gms_bc_packets gbc
8634 		    set (gbc.packet_id,gbc.status_code,
8635 			 gbc.session_id,gbc.serial_id,
8636 			 gbc.je_category_name,
8637 			 gbc.je_source_name,gbc.period_name,
8638 			 gbc.period_year,gbc.period_num,
8639 			 gbc.account_type,
8640 			 gl_bc_packets_rowid) =
8641 			 (select glbcrec.packet_id,glbcrec.status_code,
8642 			 glbcrec.session_id,glbcrec.serial_id,
8643 			 glbcrec.je_category_name,
8644 			 glbcrec.je_source_name,glbcrec.period_name,
8645 			 glbcrec.period_year,glbcrec.period_num,
8646 			 glbcrec.account_type,
8647 			 glbcrec.gl_rowid from dual
8648 			  )
8649 		  where   gbc.packet_id   = l_gms_packet_id
8650 		  and     gbc.status_code = 'I'
8651 		  and     gbc.source_event_id = glbcrec.event_id
8652 		  and     (( gbc.document_distribution_id = glbcrec.source_distribution_id_num_1
8653 		            AND glbcrec.source_distribution_type <> 'AP_PREPAY') OR
8654 			   (glbcrec.source_distribution_type = 'AP_PREPAY' AND -- Bug 5561741
8655                              gbc.document_distribution_id = l_dist_id
8656                               -- Following cannot be used as ap_prepay_app_dists not visible ..autonomous ..
8657                               -- (SELECT APAD.PREPAY_APP_DISTRIBUTION_ID
8658 			      --                                 FROM ap_prepay_app_dists APAD
8659 			      --				      WHERE APAD.PREPAY_APP_DIST_ID = glbcrec.source_distribution_id_num_1 )
8660                              ))
8661 		  and     gbc.document_type = decode(glbcrec.encumbrance_type_key,'Commitment','REQ'
8662 									       ,'Obligation','PO'
8663 									       ,'Invoices','AP')
8664                   /* Bug 5285217 : For an Invoice Matched to a PO with Quantity Variance , there are two records in gl_bc_packets
8665 		     with encumbrance type as 'Invoices' (one for the invoice reserve and the other for the Quantity variance reserve).
8666 		     Also the packet_id,event_id and source_distribution_id_num_1 on both the records are same.
8667 		     So the 'gl_bc_packets_rowid IS NULL' and 'ROWNUM = 1' conditions are used to differentiate between
8668 		     the two records.
8669                      For the first AP record (either invoice reserve record or the Quantity variance reserve record) in gl_bc_packets,
8670 		     the 'ROWNUM=1' and the (entered_dr-entered_cr) conditions are used to identify the corresponding record
8671 		     in gms_bc_packets. The (entered_dr-entered_cr) check is not suitable for the scenario in which the invoice amount and the quantity
8672 		     variance amount are same. But as the amounts are same for both the invoice reserve and quantity variance reserve
8673 		     records , only 'ROWNUM=1' check will suffice as we need not distinguish between the invoice reserve and the
8674 		     quantity variance record.
8675 		     For the second AP record (one among the invoice reserve record or the Quantity variance reserve record for which the
8676 		     corresponding record is not yet updated in gms_bc_packets) in gl_bc_packets , the 'gl_bc_packets_rowid IS NULL'
8677 		     condition is used to identify the corresponding record in gms_bc_packets.*/
8678 		  and    gbc.gl_bc_packets_rowid IS NULL
8679 		  and    ((nvl(gbc.entered_dr,0) - nvl(gbc.entered_cr,0)) = (nvl(glbcrec.entered_dr,0) - nvl(glbcrec.entered_cr,0)))
8680 		  and    ROWNUM = 1
8681 		  -- If it's PO mathed to an AP then for the PO reversal record in gl_bc_packets source_distribution_id_num_1 is populated as invoice_distribution_id
8682 		  -- whereas for the corresponding record gms_bc_packets will have source_distribution_id_num_1 as po_distribution_id
8683 		  -- Hence for this scenario we will check encumbrance_type_id to get PO record from GL.
8684 		  -- This update is NOT for the PO reversal record in an AP matched to a PO scenario.
8685 		  and    1 >= (select count(distinct glbc1.encumbrance_type_id)  -- This will return more than one count for the PO reversal record in an AP matched to a PO scenario.
8686 					 from  gl_bc_packets glbc1
8687 					where  glbc1.packet_id = p_packet_id
8688 					  and  glbc1.event_id  = gbc.source_event_id
8689 					  and  glbc1.source_distribution_type = 'AP_INV_DIST'
8690 					  and  gbc.document_type ='PO' );
8691 
8692 	     END LOOP;
8693 
8694 	 IF g_debug = 'Y' THEN
8695    	    gms_error_pkg.gms_debug ('Synch_gms_gl_packets Strat : Updated bc packets except PO matched to an invoice '|| SQL%ROWCOUNT,'C');
8696          END IF;
8697 
8698 	  -- If it's PO mathed to an AP then for the PO reversal record in gl_bc_packets source_distribution_id_num_1 is populated as invoice_distribution_id
8699 	  -- whereas for the corresponding record gms_bc_packets will have source_distribution_id_num_1 as po_distribution_id
8700 	  -- Hence for this scenario we will check encumbrance_type_id to get PO record from GL.
8701 	  -- This update is for the PO reversal records , which will be only records left with gbc.packet_id   = l_gms_packet_id , in an AP matched to a PO scenario.
8702 
8703 	 Update gms_bc_packets gbc
8704 	    set (gbc.packet_id,gbc.status_code,
8705 		 gbc.session_id,gbc.serial_id,
8706 		 gbc.period_name,
8707 		 gbc.period_year,gbc.period_num,
8708 		 gbc.account_type,
8709 		 gl_bc_packets_rowid) =
8710 		 (select p_packet_id,'P',
8711 			 glbc.session_id,glbc.serial_id,
8712 			 glbc.period_name,
8713 			 glbc.period_year,glbc.period_num,
8714 			 glbc.account_type,
8715 			 rowidtochar(glbc.rowid)
8716 		  from   gl_bc_packets glbc
8717 		  where  glbc.packet_id                    = p_packet_id
8718 		  and    glbc.event_id                     = gbc.source_event_id
8719 /* Start changes for bug#13975496 */
8720 	       	  and (( je_source_name ='Payables' and
8721                   glbc.source_distribution_id_num_1 <> gbc.document_distribution_id)
8722                   OR (je_source_name ='Purchasing' and glbc.source_distribution_id_num_1 =
8723                   gbc.document_distribution_id))
8724 /* End changes for bug#13975496 */
8725 		  --and    glbc.source_distribution_id_num_1 <> gbc.document_distribution_id
8726 /*Commented the line for bug#13975496 */
8727 		  and    glbc.encumbrance_type_id IN (SELECT glenc.encumbrance_type_id --Seeded encumbrance type for PO
8728 		                                        FROM gl_encumbrance_types glenc
8729 						       WHERE glenc.encumbrance_type_key = 'Obligation')
8730 		  and    rownum = 1)
8731 	  where   gbc.packet_id   = l_gms_packet_id
8732 	  and     gbc.status_code = 'I'
8733 	  and     gbc.document_type ='PO';
8734 
8735          IF g_debug = 'Y' THEN
8736    	    gms_error_pkg.gms_debug ('Synch_gms_gl_packets Strat : Updated bc packets for PO matched to an invoice '|| SQL%ROWCOUNT,'C');
8737          END IF;
8738 
8739 /* Bug 5645290 - Start */
8740 	Update gms_bc_packets gbc
8741 	    set (gbc.packet_id,gbc.status_code,
8742 		 gbc.session_id,gbc.serial_id,
8743 		 gbc.je_category_name,
8744 		 gbc.je_source_name,gbc.period_name,
8745 		 gbc.period_year,gbc.period_num,
8746 		 gbc.account_type,
8747 		 gl_bc_packets_rowid) =
8748 		 (select gbcparent.packet_id,'P',
8749 			 gbcparent.session_id,gbcparent.serial_id,
8750 			 gbcparent.je_category_name,
8751 			 gbcparent.je_source_name,gbcparent.period_name,
8752 			 gbcparent.period_year,gbcparent.period_num,
8753 			 gbcparent.account_type,
8754 			 gbcparent.gl_bc_packets_rowid
8755                   from   gms_bc_packets gbcparent
8756                   where  gbcparent.bc_packet_id = gbc.parent_bc_packet_id)
8757 	  where  gbc.packet_id   = l_gms_packet_id
8758 	  and    gbc.status_code = 'I'
8759 	  and    gbc.gl_bc_packets_rowid IS NULL
8760 	  /* Added for bug 11697170 */
8761 	  and    gbc.parent_bc_packet_id is not null;
8762 
8763 
8764          IF g_debug = 'Y' THEN
8765    	    gms_error_pkg.gms_debug ('Synch_gms_gl_packets Strat : Updated bc packets for prepayment burden adjustment lines '|| SQL%ROWCOUNT,'C');
8766          END IF;
8767 /* Bug 5645290 - End */
8768 
8769   END IF;
8770   CLOSE get_temp_packet_id;
8771 
8772   IF g_debug = 'Y' THEN
8773      gms_error_pkg.gms_debug ('Synch_gms_gl_packets Strat : End ','C');
8774   END IF;
8775 
8776 End Synch_gms_gl_packets;
8777 
8778 
8779 /* -----------------------------------------------------------------------------------------
8780  This is the funds check function which calls all the other functions and procedures.
8781    Parameters :
8782       x_sobid    : Set of Books ID   in GL accounts for the packet to funds checked.
8783       x_packetid : Packet ID of the packet to be funds checked.
8784       x_mode     : Funds Checker Operation Mode.
8785                      C  - Check funds
8786                      R  - Reserve funds.
8787                      U  - Un-reserve  (only for REQ,PO and AP)
8788                      B  - Called from budget baseline process  (Processed like check funds)
8789                      S  - Called from Budget submission     (Processed like check funds)
8790                      X  - Called for Expenditure Items funds check
8791                      I  - Called for Supplier Cost funds check during interface AP->PA
8792       x_partial  : Indicates the packet can be fundschecked/reserverd partially or not
8793                      Y  - Partial
8794                      N  - Full mode, default is N
8795       x_user_id  : User ID for Override   -- Not used
8796       x_user_resp_id : User Responsibility ID for Override   -- Not used
8797       x_return_code  : Fudscheck return status
8798 --------------------------------------------------------------------------------------------------*/
8799    FUNCTION gms_fck (
8800       x_sobid          IN       NUMBER,
8801       x_packetid       IN       NUMBER,
8802       x_mode           IN       VARCHAR2 DEFAULT 'C',
8803       x_override       IN       VARCHAR2 DEFAULT 'N',
8804       x_partial        IN       VARCHAR2 DEFAULT 'N',
8805       x_user_id        IN       NUMBER DEFAULT NULL,
8806       x_user_resp_id   IN       NUMBER DEFAULT NULL,
8807       x_execute        IN       VARCHAR2 DEFAULT 'N',
8808       x_return_code    IN OUT NOCOPY   VARCHAR2,
8809       x_e_code         OUT NOCOPY      VARCHAR2,
8810       x_e_stage        OUT NOCOPY      VARCHAR2)
8811       RETURN BOOLEAN IS
8812 
8813       x_arrival_seq   NUMBER;
8814       x_err_code      NUMBER;
8815       x_err_buff      VARCHAR2 (2000);
8816       x_status        VARCHAR2 (1);
8817       l_pkt_row       NUMBER ;
8818       l_dummy         NUMBER;
8819 
8820    BEGIN
8821       -------------------------------------------------------------------------------+
8822       -- 1. Initalize variables
8823       -------------------------------------------------------------------------------+
8824 
8825       g_error_program_name   := 'GMS_FUNDS_CONTROL_PKG';
8826       g_error_procedure_name := 'Gms_fck';
8827       g_debug                := NVL(FND_PROFILE.value('GMS_ENABLE_DEBUG_MODE'), 'N');
8828       g_packet_id            := x_packetid;
8829       g_mode                 := x_mode;
8830 
8831       x_err_code             := 0;
8832       x_err_buff             := null;
8833 
8834       gms_error_pkg.set_debug_context; -- Added for Bug: 2510024
8835 
8836       IF g_debug = 'Y' THEN
8837       	gms_error_pkg.gms_debug (g_error_procedure_name||':packet,x_mode,x_partial:'||
8838 			         g_packet_id||','||g_mode||','||x_partial,'C');
8839       END IF;
8840 
8841        -- ---------------------------------------------------------------------------------------------------------+
8842        -- 101: R12 Funds Management uptake : This procedure will synch data in gl_bc_packets to gms_bc_packets ..
8843        -- ---------------------------------------------------------------------------------------------------------+
8844        SYNCH_GMS_GL_PACKETS(x_packetid);
8845 
8846       -------------------------------------------------------------------------------+
8847       -- 2. Call gms_fck_init
8848       -------------------------------------------------------------------------------+
8849         x_e_stage     := 'Before gms_fck_init call';
8850         IF g_debug = 'Y' THEN
8851         	gms_error_pkg.gms_debug (g_error_procedure_name||':'||x_e_stage,'C');
8852         END IF;
8853 
8854 	gms_fck_init(p_partial_flag => x_partial);
8855 
8856         g_error_procedure_name := 'Gms_fck';
8857 
8858       -------------------------------------------------------------------------------+
8859       -- 3. If funds check called for non-gms transactions exit at this point ..
8860       -------------------------------------------------------------------------------+
8861             If g_non_gms_txn then
8862             x_e_stage := 'All txns. non-gms, exit GMS-FC';
8863 
8864                IF g_debug = 'Y' THEN
8865         	  gms_error_pkg.gms_debug (g_error_procedure_name||':'||x_e_stage,'C');
8866                END IF;
8867 
8868                x_return_code := 'S';
8869                RETURN g_non_gms_txn;
8870             End If;
8871 
8872       -------------------------------------------------------------------------------+
8873       -- 301. Initalize currency variables (currency code and precision)
8874       -------------------------------------------------------------------------------+
8875            x_e_stage := 'Initalize currency var';
8876 
8877            pa_currency.set_currency_info;
8878 
8879       -------------------------------------------------------------------------------+
8880       -- 4. Call delete_pending_txns to delete bc pkt txns. left in 'P' status ..
8881       --    This procedure will delete pending records from gms_bc_packets
8882       -------------------------------------------------------------------------------+
8883          x_e_stage := 'delete_pending_txns';
8884          IF g_debug = 'Y' THEN
8885             gms_error_pkg.gms_debug (g_error_procedure_name||':'||x_e_stage,'C');
8886          END IF;
8887 
8888       delete_pending_txns(x_err_code,x_err_buff);
8889 
8890       g_error_procedure_name := 'Gms_fck';
8891       -------------------------------------------------------------------------------+
8892       -- 5. Call adl synch for REQ/PO/AP ..
8893       --    Following procedure create ADLS for NON FAB
8894       -------------------------------------------------------------------------------+
8895       -- R12 Funds Management Uptake : Deleted call to misc_synch_adls as its shifted to
8896       -- main session (copy_gl_pkt_to_gms_pkt) of fundscheck process
8897 
8898       -------------------------------------------------------------------------------+
8899       -- 6. Following procedure creates gms_bc_packet from gl_bc_packet
8900       -------------------------------------------------------------------------------+
8901        -- R12 Funds Management Uptake : Deleted call to misc_gms_insert as its shifted to
8902        -- main session (copy_gl_pkt_to_gms_pkt) of fundscheck process
8903 
8904       -------------------------------------------------------------------------------+
8905       -- 7. si adjustment call
8906       -------------------------------------------------------------------------------+
8907       -- R12 Funds Management Uptake : R12 open issue
8908       IF g_doc_type = 'AP' and x_mode = 'R' then
8909          x_e_stage := 'si_adjustments';
8910          IF g_debug = 'Y' THEN
8911                 gms_error_pkg.gms_debug (g_error_procedure_name||':'||x_e_stage,'C');
8912          END IF;
8913 
8914          si_adjustments(x_packetid, l_pkt_row) ;
8915 	 IF l_pkt_row = 0 THEN
8916             x_return_code := 'S';
8917 	    return TRUE ;
8918 	 END IF ;
8919 
8920       END IF ;
8921 
8922       -------------------------------------------------------------------------------+
8923       -- 701. Calculate ind_compiled_set_id and handle net_zero for Enc...
8924       -------------------------------------------------------------------------------+
8925       -- Encumbrance ind_compiled_set_id derivation and net_zero txn. handling should
8926       -- happen before the call to calc. burdenable raw cost .. bug 3810247
8927      /* Bug 5330152 : Added the (x_mode = 'E') check such that the procedures CALC_ENC_IND_COMPILED_SET_ID
8928         and HANDLE_NET_ZERO_TXN are called only for funds check of Manual Encumbrances. */
8929      IF (x_mode = 'E') then
8930 
8931       IF g_debug = 'Y' THEN
8932          g_error_procedure_name := 'Gms_fck';
8933          x_e_stage := 'ENC:Derive ind';
8934          gms_error_pkg.gms_debug (g_error_procedure_name||':'||x_e_stage,'C');
8935       End If;
8936 
8937      CALC_ENC_IND_COMPILED_SET_ID (x_packetid);
8938 
8939       IF g_debug = 'Y' THEN
8940          g_error_procedure_name := 'Gms_fck';
8941          x_e_stage := 'ENC:Handle_net_zero_txn:Net_Zero';
8942          gms_error_pkg.gms_debug (g_error_procedure_name||':'||x_e_stage,'C');
8943       End If;
8944         -- Check if  adjusted and adjusting transactions are present in the same packet
8945         -- If so, update them with result_code 'P82' and update effect_on_funds_code
8946         -- to 'I' so that 'funds avilable' calculation ignores them.
8947 
8948      HANDLE_NET_ZERO_TXN(x_packetid,'Net_Zero');
8949 
8950       IF g_debug = 'Y' THEN
8951          g_error_procedure_name := 'Gms_fck';
8952          x_e_stage := 'ENC:Handle_net_zero_txn:Check_Adjusted';
8953          gms_error_pkg.gms_debug (g_error_procedure_name||':'||x_e_stage,'C');
8954       End If;
8955        --  Fail adjusting transaction, if original transaction has not been  FC'ed(F08)
8956 
8957      HANDLE_NET_ZERO_TXN(x_packetid, 'Check_Adjusted');
8958 
8959     END IF;
8960 
8961       -------------------------------------------------------------------------------+
8962       -- 8. Burdenable Raw Cost calculation
8963       -------------------------------------------------------------------------------+
8964       -- R12 Funds Management Uptake : For x_mode ='R'/'U'/'C', call to calculate burden
8965       -- update_bc_pkt_burden_raw_cost is shifted to main session procedure
8966       -- copy_gl_pkt_to_gms_pkt of fundscheck process.
8967 
8968       --IF x_mode IN ('R', 'U', 'C', 'E','X') THEN
8969       IF x_mode IN ( 'E','X') THEN
8970          -- Calculate burdenable_cost and update on gms_bc_packets ..............
8971          -- Calling burden calculation for all except for mode : Submit,Baseline,Interface
8972          IF g_debug = 'Y' THEN
8973             x_e_stage := 'Burdenable Raw Cost calculation';
8974             g_error_procedure_name := 'Gms_fck';
8975             gms_error_pkg.gms_debug (g_error_procedure_name||':'||x_e_stage,'C');
8976          END IF;
8977 
8978            -- 8A. CALCULATING BURDENABLE RAW COST
8979            -- pass g_derived_mode parameter ...
8980            IF NOT gms_cost_plus_extn.update_bc_pkt_burden_raw_cost (x_packetid,g_derived_mode) THEN
8981             result_status_code_update (
8982                p_packet_id=> x_packetid,
8983                p_status_code=> 'T',
8984                p_result_code=> 'F76');
8985 
8986 	    -- Bug : 2557041 - Added for IP check funds Enhancement
8987 	    -- Update gl_bc_packets result_code to F67 if update Burdenable Raw Cost
8988 	    -- failed.
8989 
8990 	    UPDATE gl_bc_packets
8991 	       SET result_code = DECODE (NVL (SUBSTR (result_code, 1, 1), 'P'),'P', 'F67',result_code)
8992  	     WHERE packet_id = x_packetid;
8993 
8994 	    x_e_code := 'U';		  -- Bug : 2557041 - Added , same as done for misc_gms_insert
8995             g_return_code := 'T';
8996             x_return_code := 'T';
8997             RETURN (FALSE);
8998            END IF;
8999 
9000            -- 8B. CHECK FOR FAILURE ..
9001            -- If any transaction has failed burdenable_raw_cost calculation and
9002            -- the mode is 'R' or 'U' or 'C' .. fail the packet with F65
9003 	   -- R12 Funds Management Uptake : burden calculation logic for x_mode ='R'/'U'/'C'
9004 	   -- shifted to main session procedure copy_gl_pkt_to_gms_pkt.
9005 
9006          IF g_debug = 'Y' THEN
9007          	gms_error_pkg.gms_debug (
9008             'GMS_FCK-After Calling gms_cost_plus_extn.update_bc_pkt_burden_raw_cost','C');
9009          END IF;
9010 
9011    END IF;
9012 
9013    -------------------------------------------------------------------------------+
9014    -- 9. Create indirect cost lines
9015    -------------------------------------------------------------------------------+
9016    IF x_mode not in ('S','B') then
9017          -- Ensure that the burden components are not re-created during budget baseline
9018 
9019          x_e_stage := 'misc_gms_idc';
9020          IF g_debug = 'Y' THEN
9021             g_error_procedure_name := 'Gms_fck';
9022             gms_error_pkg.gms_debug (g_error_procedure_name||':'||x_e_stage,'C');
9023          END IF;
9024 
9025          IF NOT misc_gms_idc (x_packetid) THEN
9026             RETURN (FALSE);
9027          END IF;
9028 
9029 	 COMMIT;
9030    END IF;
9031 
9032    -------------------------------------------------------------------------------+
9033    -- 10. Main Funds Check Processor - gms_fcp call
9034    -------------------------------------------------------------------------------+
9035 
9036          x_e_stage := 'gms_fcp';
9037          IF g_debug = 'Y' THEN
9038             g_error_procedure_name := 'Gms_fck';
9039             gms_error_pkg.gms_debug (g_error_procedure_name||':'||x_e_stage,'C');
9040          END IF;
9041 
9042            -- pass g_derived_mode parameter ...
9043       IF NOT gms_fcp (x_sobid, x_packetid, g_derived_mode, g_partial_flag, x_arrival_seq,
9044 		      x_err_code, x_err_buff) THEN
9045          RETURN (FALSE);
9046       END IF;
9047 
9048      COMMIT;
9049 
9050       IF g_debug = 'Y' THEN
9051       	gms_error_pkg.gms_debug ('GMS_FCK-After Calling gms_fcp', 'C');
9052       END IF;
9053 
9054    -------------------------------------------------------------------------------+
9055    -- 11. Posting for encumbrances ..
9056    -------------------------------------------------------------------------------+
9057       IF x_mode = 'E' THEN
9058          x_e_stage := 'Posting for encumbrances';
9059             g_error_procedure_name := 'Gms_fck';
9060          --g_error_procedure_name := 'update_source_burden_raw_cost';
9061          IF g_debug = 'Y' THEN
9062             gms_error_pkg.gms_debug (g_error_procedure_name||':'||x_e_stage,'C');
9063          END IF;
9064 
9065          IF NOT gms_cost_plus_extn.update_source_burden_raw_cost (x_packetid, x_mode, g_partial_flag) THEN
9066             IF g_debug = 'Y' THEN
9067          	gms_error_pkg.gms_debug ('GMS_FCK- Posting for encumbrances ..failed','C');
9068             END IF;
9069             result_status_code_update (
9070                p_packet_id=> x_packetid,
9071                p_status_code=> 'T',
9072                p_result_code=> 'F64');
9073 
9074          END IF;
9075       END IF;
9076 
9077    ----------------------------------------------------------------------------------+
9078    -- 12. Status code update for 'Expenditure Items' and 'Interface Items'
9079    ----------------------------------------------------------------------------------+
9080 
9081       -- Update bc_packet failed records status_code to 'R'
9082       IF  x_mode in ('X','I') then
9083 
9084         x_e_stage     := 'Update status on failed bcpkt';
9085          IF g_debug = 'Y' THEN
9086             g_error_procedure_name := 'Gms_fck';
9087             gms_error_pkg.gms_debug (g_error_procedure_name||':'||x_e_stage,'C');
9088          END IF;
9089 
9090         update_status_on_failed_txns(x_packetid);
9091 
9092       END IF;
9093 
9094    <<gms_return_code_label>>
9095    ----------------------------------------------------------------------------------+
9096    -- 13. Determine the return code sent to GL/subit/baseline ..
9097    ----------------------------------------------------------------------------------+
9098     x_e_stage     := 'Return Code derivation';
9099 
9100     If x_mode in ('R','U','C','E','S','B') then
9101         x_e_stage     := 'Determine return code';
9102          IF g_debug = 'Y' THEN
9103             g_error_procedure_name := 'Gms_fck';
9104             gms_error_pkg.gms_debug (g_error_procedure_name||':'||x_e_stage,'C');
9105          END IF;
9106 
9107       IF NOT gms_return_code (x_packetid, g_derived_mode, g_partial_flag, x_return_code, x_e_code, x_err_buff) THEN
9108          RETURN FALSE;
9109       END IF;
9110 
9111       IF g_debug = 'Y' THEN
9112         g_error_procedure_name := 'Gms_fck';
9113       	gms_error_pkg.gms_debug (g_error_procedure_name||':'||x_e_stage||':return code'||x_return_code,'C');
9114       END IF;
9115     End If;
9116 
9117       <<end_process>>
9118       IF g_debug = 'Y' THEN
9119       	gms_error_pkg.gms_debug (' ********** End of GMS_FCK **********  ', 'C');
9120       END IF;
9121       COMMIT;
9122 
9123       RETURN (TRUE);
9124 -- ********************GMS FUNDS CHECKER LAST LINE *********** END ************************
9125    EXCEPTION
9126       WHEN OTHERS THEN
9127          x_e_code := 'U';
9128          x_e_stage := SQLCODE||' '||SQLERRM;	-- Bug 2337897 : Added SQLCODE
9129          g_return_code := 'T';
9130          x_return_code := 'T';
9131 
9132          gms_error_pkg.gms_message (
9133             x_err_name=> 'GMS_UNEXPECTED_ERROR',
9134             x_token_name1=> 'PROGRAM_NAME',
9135             x_token_val1=> g_error_program_name || '.' || g_error_procedure_name || '.' || g_error_stage,
9136             x_token_name2=> 'SQLCODE',
9137             x_token_val2=> SQLCODE,
9138             x_token_name3=> 'SQLERRM',
9139             x_token_val3=> SQLERRM,
9140             x_exec_type=> 'C',
9141             x_err_code=> x_err_code,
9142             x_err_buff=> x_err_buff);
9143 
9144          result_status_code_update (
9145             p_packet_id=> x_packetid,
9146             p_status_code=> 'T',
9147             p_result_code=> 'F89',
9148      		p_fc_error_message=>SUBSTR((g_error_program_name || '.' || g_error_procedure_name || '.' || g_error_stage ||' SQLCODE :'||SQLCODE||' SQLERRM :'||SQLERRM),1,2000)
9149                                   );
9150 
9151 		 -- Bug 2176230 - Delete arrival_order record in case of any failure.
9152 
9153 		 IF x_mode = 'C' THEN
9154                    DELETE gms_bc_packet_arrival_order
9155 		    WHERE packet_id = x_packetid;
9156    	         END IF;
9157 
9158 	 -- Bug 2337897 :  If any Unhandled exception occurs, them mark gl_bc_packets
9159 	 --		   as Funds check failed
9160 
9161          If x_mode in ('R','U','C') and g_doc_type <> 'FAB' then -- not g_derived_mode
9162 
9163   	   UPDATE gl_bc_packets SET
9164 	   result_code = DECODE (NVL (SUBSTR (result_code, 1, 1), 'P'),'P', 'F71',result_code)
9165 	   WHERE packet_id = x_packetid;
9166 
9167          End If;
9168 
9169          COMMIT;
9170          RETURN(FALSE);
9171    END gms_fck;
9172 
9173 -- ==============================================================================
9174 
9175    PROCEDURE gms_gl_return_code (
9176       x_packet_id          IN       NUMBER,
9177       x_mode               IN       VARCHAR2,
9178       x_gl_return_code     IN OUT NOCOPY   VARCHAR2,
9179       x_gms_return_code    IN       VARCHAR2,
9180       x_gms_partial_flag   IN       VARCHAR2,
9181       x_er_code            IN OUT NOCOPY   VARCHAR2,
9182       x_er_stage           IN OUT NOCOPY   VARCHAR2) IS
9183 
9184    BEGIN
9185 
9186      -- Code removed as gms_funds_posting_pkg.gms_gl_return_code called for AP/PO/REQ
9187      -- ENC/EXP does not gms_gl_return_code
9188      null;
9189 
9190    END gms_gl_return_code;
9191 ----------------------------------------------------------------------------------------------------------
9192 
9193 -- R12 Funds Management Uptake : This tieback procedure is called from PSA_BC_XLA_PVT.Budgetary_control
9194 -- if SLA accounting fails.This API will mark the gms_bc_packet records to failed status.
9195 
9196 PROCEDURE TIEBACK_FAILED_ACCT_STATUS (p_bc_mode IN  VARCHAR2 DEFAULT 'C') IS
9197 BEGIN
9198 
9199    UPDATE gms_bc_packets
9200      SET  status_code = DECODE(p_bc_mode,'C','F','R'),
9201           result_code = 'F22'
9202    WHERE  status_code in ('I','A','S')
9203      AND  source_event_id IN
9204             (SELECT  event_id
9205                FROM  PSA_BC_XLA_EVENTS_GT
9206 	       WHERE upper(result_code) in ('XLA_ERROR','FATAL'));
9207 
9208 END TIEBACK_FAILED_ACCT_STATUS;
9209 
9210 
9211 END gms_funds_control_pkg;