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