DBA Data[Home] [Help]

PACKAGE BODY: APPS.GL_CALCULATE_TAX_PKG

Source


1 PACKAGE BODY GL_CALCULATE_TAX_PKG as
2 /* $Header: glujetxb.pls 120.6.12000000.2 2007/07/03 17:10:50 djogg ship $ */
3 
4   ---
5   --- PRIVATE FUNCTIONS
6   ---
7 
8   --
9   -- Procedure
10   --   setup
11   -- Purpose
12   --   Validates the parameters and gets various information
13   -- History
14   --   10-DEC-96  D. J. Ogg    Created
15   -- Arguments
16   --   header_id			The header id
17   --   tax_level                        'J'ournal or 'B'atch
18   --   lgr_id				Ledger id of header or batch
19   --   org_id                           Org of batch
20   --   per_name				Period of header or batch
21   --   calculation_level		Calculation level for ledger and org
22   --   base_currency			Currency of ledger
23   --   tax_mau				Tax mau for ledger and org
24   -- Notes
25   --
26   PROCEDURE setup(header_id		NUMBER,
27                   tax_level             VARCHAR2,
28 		  lgr_id		IN OUT NOCOPY  VARCHAR2,
29                   org_id                IN OUT NOCOPY  NUMBER,
30 		  per_name		IN OUT NOCOPY  VARCHAR2,
31 		  calculation_level	IN OUT NOCOPY  VARCHAR2,
32 		  base_currency		IN OUT NOCOPY  VARCHAR2,
33 		  tax_mau		IN OUT NOCOPY  VARCHAR2) IS
34 
35     tax_status_code	VARCHAR2(1); -- Tax status of journal or batch
36     dummy2		NUMBER;  -- Dummy variable
37     dummy		VARCHAR2(100);  -- Dummy variable
38     le_id               NUMBER; -- legal entity id
39     x_return_status     VARCHAR2(30);
40     x_msg_out           VARCHAR2(2000);
41   BEGIN
42 
43     -- Validate the header and get batch level information
44     BEGIN
45       -- Lock the header
46       SELECT je_batch_id
47       INTO   dummy2
48       FROM   gl_je_headers jeh
49       WHERE  jeh.je_header_id = header_id
50       FOR UPDATE;
51 
52       -- Lock the batch
53       SELECT 'Good batch'
54       INTO   dummy
55       FROM   gl_je_batches jeb
56       WHERE  jeb.je_batch_id = dummy2
57       FOR UPDATE;
58 
59       -- Get various information
60       SELECT jeh.tax_status_code, jeh.ledger_id, jeb.org_id,
61              jeb.default_period_name, lgr.currency_code
62       INTO   tax_status_code, lgr_id, org_id, per_name,
63              base_currency
64       FROM   gl_je_headers jeh, gl_je_batches jeb, gl_ledgers lgr
65       WHERE  jeh.je_header_id = header_id
66       AND    jeb.je_batch_id = jeh.je_batch_id
67       AND    lgr.ledger_id = jeh.ledger_id;
68     EXCEPTION
69       WHEN NO_DATA_FOUND THEN
70         fnd_message.set_name('SQLGL', 'GL_CTAX_BAD_HEADER');
71         fnd_message.set_token('HEADER_ID', to_char(header_id));
72         app_exception.raise_exception;
73     END;
74 
75     IF (tax_status_code <> 'R') THEN
76       fnd_message.set_name('SQLGL', 'GL_CTAX_HEADER_TAXED');
77       app_exception.raise_exception;
78     END IF;
79 
80     -- Get various information about the tax setup
81     BEGIN
82       --SELECT calculation_level_code,
83       --       decode(tax_mau, NULL, power(10, -1*tax_precision), tax_mau)
84       --INTO   calculation_level, tax_mau
85       --FROM   gl_tax_options
86       --WHERE  ledger_id = lgr_id
87       --AND    org_id = current_org_id;
88 
89       le_id := XLE_UTILITIES_GRP.Get_DefaultLegalContext_OU(
90                  org_id);
91 
92       zx_gl_tax_options_pkg.get_ledger_controls(
93             1.0,
94             lgr_id,
95             org_id,
96             le_id,
97             calculation_level,
98             tax_mau,
99             x_return_status,
100             x_msg_out);
101     EXCEPTION
102       WHEN NO_DATA_FOUND THEN
103         BEGIN
104           SELECT nvl(multi_org_flag, 'N')
105           INTO   dummy
106           FROM   fnd_product_groups;
107 	EXCEPTION
108 	  WHEN NO_DATA_FOUND THEN
109 	    dummy := 'Y';
110 	END;
111 
112 	IF (dummy = 'N') THEN
113           fnd_message.set_name('SQLGL', 'GL_CTAX_NO_OPTIONS');
114         ELSIF (tax_level = 'J') THEN
115           fnd_message.set_name('SQLGL', 'GL_CTAX_NO_OPTIONS_JOURN_ORG');
116         ELSE
117           fnd_message.set_name('SQLGL', 'GL_CTAX_NO_OPTIONS_BATCH_ORG');
118 	END IF;
119         app_exception.raise_exception;
120     END;
121   END setup;
122 
123   --
124   -- Procedure
125   --   round_it
126   -- Purpose
127   --   Rounds a number to the appropriate precision
128   -- History
129   --   10-DEC-96  D. J. Ogg    Created
130   -- Arguments
131   --   amount				The amount to be rounded
132   --   rounding_rule			Rounding rule to be followed: Up, Down, or
133   --					Nearest
134   --   mau				Mau of currency
135   -- Notes
136   --
137   FUNCTION round_it(amount		NUMBER,
138 		    rounding_rule	VARCHAR2,
139 		    mau			NUMBER) RETURN NUMBER IS
140   BEGIN
141     IF (amount IS NULL) THEN
142       RETURN(NULL);
143     ELSIF (rounding_rule = 'U') THEN
144       IF (sign(amount) >= 0) THEN
145         RETURN(ceil(amount / mau) * mau);
146       ELSE
147         RETURN(-1 * ceil(abs(amount) / mau) * mau);
148       END IF;
149     ELSIF (rounding_rule = 'D') THEN
150       IF (sign(amount) >= 0) THEN
151         RETURN(floor(amount / mau) * mau);
152       ELSE
153         RETURN(-1 * floor(abs(amount) / mau) * mau);
154       END IF;
155     ELSE
156       RETURN(round(amount / mau) * mau);
157     END IF;
158   END round_it;
159 
160 
161   ---
162   --- PUBLIC FUNCTIONS
163   ---
164 
165   PROCEDURE calculate(	tax_level			VARCHAR2,
166 			batch_header_id			NUMBER,
167 			disp_header_id			NUMBER DEFAULT NULL,
168 			resp_appl_id			NUMBER,
169 			resp_id				NUMBER,
170 			user_id				NUMBER,
171 			login_id			NUMBER,
172 			coa_id				NUMBER,
173 			header_total_dr		IN OUT NOCOPY	NUMBER,
174 			header_total_cr		IN OUT NOCOPY 	NUMBER,
175 			header_total_acc_dr	IN OUT NOCOPY	NUMBER,
176 			header_total_acc_cr	IN OUT NOCOPY 	NUMBER,
177 			batch_total_dr		IN OUT NOCOPY	NUMBER,
178 			batch_total_cr		IN OUT NOCOPY	NUMBER,
179 			batch_total_acc_dr	IN OUT NOCOPY	NUMBER,
180 			batch_total_acc_cr	IN OUT NOCOPY	NUMBER,
181 			has_bad_accounts	IN OUT NOCOPY 	BOOLEAN
182 		     ) IS
183 
184 
185     lgr_id          	NUMBER;  -- Current ledger
186     org_id              NUMBER;  -- Current operating unit
187     per_name        	VARCHAR2(15);  -- Current period name
188     calculation_level	VARCHAR2(1);  -- Calculation level: Journal or Line
189     tax_mau		NUMBER;  -- Minimum accountable unit for tax
190 			         -- calculations
191     base_currency	VARCHAR2(15);  -- Functional currency of set of books
192     tax_status_code     VARCHAR2(1);  -- Tax status code of batch
193 
194     header_id		NUMBER;  -- Id of current header
195     next_line_num	NUMBER;  -- Next unused line number for current header
196     conv_rate		NUMBER;  -- Conversion rate for current header
197     header_eff_date	DATE; -- Effective date of header
198     currency		VARCHAR2(15);  -- Currency for header
199     curr_mau		NUMBER;  -- Minimum accountable unit for header
200 
201     line_count		NUMBER;  -- Total number of lines processed
202 
203     line_num		NUMBER;  -- Number of current line
204     eff_date		DATE;  -- effective date of line
205     ent_dr		NUMBER;  -- line debits
206     ent_cr		NUMBER;  -- line credits
207     description		VARCHAR2(250);  -- line description
208     rounding_rule	VARCHAR2(1);  -- line rounding rule
209     incl_tax		VARCHAR2(1);  -- line includes tax
210     tax_code		VARCHAR2(50);  -- line tax code
211     tax_rate		NUMBER;  -- line tax rate
212     tax_ccid		NUMBER;  -- tax ccid for line
213     tax_group		NUMBER;  -- tax group for line
214 
215     no_more_records	BOOLEAN;  -- Indicates that all records have been
216 				  -- fetched
217     last_in_group	BOOLEAN;  -- Indicates this is the last record in
218 				  -- the group
219     first_in_group	BOOLEAN;  -- Indicates this is the first record
220 				  -- in the group
221 
222     bad_acct		BOOLEAN;  -- Indicates that the current tax
223 				  -- account is bad
224     bad_csegs		VARCHAR2(750);  -- Gives the bad tax account
225 
226     line_tax_dr		NUMBER;  -- Tax debits for current line
227     line_tax_cr		NUMBER;  -- Tax credits for next line
228     line_tax_acc_dr	NUMBER;  -- Tax accounted debits for current line
229     line_tax_acc_cr	NUMBER;  -- Tax accounted credits for next line
230 
231     total_ent_dr	NUMBER;  -- Total entered debits
232     total_ent_cr	NUMBER;  -- Total entered credits
233 
234     total_jtax_dr	NUMBER;  -- Correct tax dr for current tax line
235     total_jtax_cr	NUMBER;  -- Correct tax cr for current tax line
236     total_jtax_acc_dr	NUMBER;  -- Correct tax accounted dr for current tax
237 				 -- line
238     total_jtax_acc_cr	NUMBER;  -- Correct tax accounted cr current tax line
239 
240     total_ltax_dr	NUMBER;  -- Sum of individual lines tax dr (needed for
241 				 -- journal method)
242     total_ltax_cr	NUMBER;  -- Sum of individual lines tax cr (needed for
243 				 -- journal method)
244     total_ltax_acc_dr	NUMBER;  -- Correct tax accounted dr for current tax
245 				 -- line
246     total_ltax_acc_cr	NUMBER;  -- Correct tax accounted cr current tax line
247 
248     tax_line_descr	VARCHAR2(240);  -- Description of tax line
249     line_curs_init      BOOLEAN;
250 
251     CURSOR j_journals IS
252         SELECT max(jeh.je_header_id), max(jel.je_line_num) + 1,
253                max(jeh.currency_conversion_rate),
254 	       max(jeh.default_effective_date),
255 	       max(jeh.currency_code),
256 	       max(decode(curr.minimum_accountable_unit,
257 			  NULL, power(10, -1*curr.precision),
258 			  curr.minimum_accountable_unit))
259         FROM   gl_je_headers jeh, fnd_currencies curr, gl_je_lines jel
260         WHERE  jeh.je_header_id = batch_header_id
261 	AND    curr.currency_code = jeh.currency_code
262         AND    jel.je_header_id = jeh.je_header_id;
263 
264     CURSOR b_journals IS
265         SELECT jeh.je_header_id, max(jel.je_line_num) + 1,
266                max(jeh.currency_conversion_rate),
267 	       max(jeh.default_effective_date),
268 	       max(jeh.currency_code),
269 	       max(decode(curr.minimum_accountable_unit,
270 		          NULL, power(10, -1*curr.precision),
271 		          curr.minimum_accountable_unit))
272         FROM   gl_je_headers jeh, fnd_currencies curr, gl_je_lines jel
273         WHERE  jeh.je_batch_id = batch_header_id
274 	AND    jeh.tax_status_code = 'R'
275 	AND    curr.currency_code = jeh.currency_code
276         AND    jel.je_header_id = jeh.je_header_id
277         GROUP BY jeh.je_header_id;
278   BEGIN
279 
280     -- Initially, there are no bad accounts
281     has_bad_accounts := FALSE;
282 
283     -- Build a cursor to loop through each journal
284     IF (tax_level = 'J') THEN
285       OPEN j_journals;
286     ELSE
287 
288       BEGIN
289         SELECT max(decode(jeh.tax_status_code, 'R', '1', '0'))
290         INTO tax_status_code
291         FROM gl_je_headers jeh
292         WHERE jeh.je_batch_id = batch_header_id;
293       EXCEPTION
294         WHEN NO_DATA_FOUND THEN
295           fnd_message.set_name('SQLGL', 'GL_CTAX_BAD_BATCH');
296           fnd_message.set_token('BATCH_ID', to_char(batch_header_id));
297           app_exception.raise_exception;
298       END;
299 
300       IF (nvl(tax_status_code,'0') <> '1') THEN
301         fnd_message.set_name('SQLGL', 'GL_CTAX_BATCH_TAXED');
302         app_exception.raise_exception;
303       END IF;
304 
305       OPEN b_journals;
306     END IF;
307 
308     line_curs_init := FALSE;
309     LOOP
310       -- Get a journal
311       IF (tax_level = 'J') THEN
312         FETCH j_journals INTO header_id, next_line_num, conv_rate,
313 			      header_eff_date, currency, curr_mau;
314         EXIT WHEN j_journals%NOTFOUND;
315       ELSE
316         FETCH b_journals INTO header_id, next_line_num, conv_rate,
317 			      header_eff_date, currency, curr_mau;
318         EXIT WHEN b_journals%NOTFOUND;
319       END IF;
320 
321       -- Initialize the journal information
322       setup(header_id           => header_id,
323             tax_level           => tax_level,
324 	    lgr_id		=> lgr_id,
325 	    org_id		=> org_id,
326 	    per_name		=> per_name,
327 	    calculation_level	=> calculation_level,
328 	    base_currency	=> base_currency,
329 	    tax_mau		=> tax_mau);
330 
331       -- Setup the lines cursor.  Must be done
332       -- after setup call.  Close the cursor if it is already open
333       IF (line_curs_init) THEN
334         gl_calculate_tax2_pkg.close_cursor;
335       END IF;
336       gl_calculate_tax2_pkg.define_cursor(
337         coa_id	  	        => coa_id,
338         calculation_level	=> calculation_level);
339       line_curs_init := TRUE;
340 
341 
342       -- If this is a functional currency journal, then use the tax mau
343       IF (currency = base_currency) THEN
344         curr_mau := tax_mau;
345       END IF;
346 
347       -- Prepare the lines cursor for fetch
348       gl_calculate_tax2_pkg.bind_cursor(header_id);
349       gl_calculate_tax2_pkg.execute_cursor;
350 
351       -- Initialize everything
352       line_count := 0;
353       first_in_group := TRUE;
354       total_ent_dr := null;
355       total_ent_cr := null;
356       total_jtax_dr := null;
357       total_jtax_cr := null;
358       total_jtax_acc_dr := null;
359       total_jtax_acc_cr := null;
360       total_ltax_dr := null;
361       total_ltax_cr := null;
362       total_ltax_acc_dr := null;
363       total_ltax_acc_cr := null;
364 
365 
366       LOOP
367 
368 	gl_calculate_tax2_pkg.fetch_cursor(
369 	  coa_id			=> coa_id,
370           lgr_id                        => lgr_id,
371           org_id                        => org_id,
372 	  calculation_level		=> calculation_level,
373 	  journal_effective_date	=> header_eff_date,
374 	  resp_appl_id			=> resp_appl_id,
375 	  resp_id			=> resp_id,
376 	  user_id			=> user_id,
377 	  no_more_records		=> no_more_records,
378 	  last_in_group			=> last_in_group,
379 	  bad_acct			=> bad_acct,
380 	  bad_csegs			=> bad_csegs,
381 	  line_num			=> line_num,
382 	  eff_date			=> eff_date,
383 	  ent_dr			=> ent_dr,
384 	  ent_cr			=> ent_cr,
385 	  rounding_rule			=> rounding_rule,
386 	  description			=> description,
387 	  incl_tax			=> incl_tax,
388 	  tax_code			=> tax_code,
389 	  tax_rate			=> tax_rate,
390 	  tax_ccid			=> tax_ccid,
391 	  tax_group			=> tax_group);
392 
393 	EXIT WHEN no_more_records;
394 
395         line_count := line_count + 1;
396 
397         -- Update the entered debits and credits totals
398         IF (first_in_group) THEN
399 	  total_ent_dr := ent_dr;
400   	  total_ent_cr := ent_cr;
401 	ELSE
402           total_ent_dr := total_ent_dr + ent_dr;
403 	  total_ent_cr := total_ent_cr + ent_cr;
404 	END IF;
405 
406         -- If this is the last in the group, then finish the group
407         IF (last_in_group) THEN
408 
409 	  -- Keep track of bad accounts
410           IF (bad_acct) THEN
411 	    has_bad_accounts := TRUE;
412  	  END IF;
413 
414 	  IF (calculation_level <> 'L') THEN
415 	    -- Determine description for tax line
416 	    fnd_message.set_name('SQLGL', 'GL_CTAX_LINE_DESCRIPTION');
417 	    fnd_message.set_token('TAX_CODE', tax_code);
418 	    fnd_message.set_token('PERCENTAGE', to_char(tax_rate*100));
419 	    IF (bad_acct) THEN
420 	      tax_line_descr := substrb(bad_csegs||': '||fnd_message.get,
421 					1, 240);
422 	    ELSE
423 	      tax_line_descr := substrb(fnd_message.get, 1, 240);
424 	    END IF;
425 
426 	    -- Calculate tax amount for tax line
427             IF (incl_tax = 'Y') THEN
428 	      total_jtax_dr := round_it((total_ent_dr*tax_rate)/(1+tax_rate),
429 				        rounding_rule, curr_mau);
430 	      total_jtax_cr := round_it((total_ent_cr*tax_rate)/(1+tax_rate),
431 				        rounding_rule, curr_mau);
432             ELSE
433   	      total_jtax_dr := round_it(total_ent_dr * tax_rate,
434 				        rounding_rule, curr_mau);
435 	      total_jtax_cr := round_it(total_ent_cr * tax_rate,
436 				        rounding_rule, curr_mau);
437 	    END IF;
438 	    total_jtax_acc_dr := round_it(total_jtax_dr * conv_rate,
439 				          rounding_rule, tax_mau);
440 	    total_jtax_acc_cr := round_it(total_jtax_cr * conv_rate,
441 				          rounding_rule, tax_mau);
442 
443 	    -- Calculate tax amount for last taxable line
444 	    IF (first_in_group) THEN
445   	      line_tax_dr := total_jtax_dr;
446 	      line_tax_cr := total_jtax_cr;
447 	      line_tax_acc_dr := total_jtax_acc_dr;
448 	      line_tax_acc_cr := total_jtax_acc_cr;
449 	    ELSE
450   	      line_tax_dr := total_jtax_dr - total_ltax_dr;
451 	      line_tax_cr := total_jtax_cr - total_ltax_cr;
452 	      line_tax_acc_dr := total_jtax_acc_dr - total_ltax_acc_dr;
453 	      line_tax_acc_cr := total_jtax_acc_cr - total_ltax_acc_cr;
454 	    END IF;
455 	  ELSE
456 	    -- Determine description for tax line
457 	    fnd_message.set_name('SQLGL', 'GL_CTAX_JOURNAL_DESCRIPTION');
458 	    fnd_message.set_token('TAX_CODE', tax_code);
459 	    fnd_message.set_token('PERCENTAGE', to_char(tax_rate*100));
460             fnd_message.set_token('LINE_NUM', to_char(line_num));
461 	    fnd_message.set_token('LINE_DESCRIPTION', description);
462 	    IF (bad_acct) THEN
463 	      tax_line_descr := substrb(bad_csegs||': '||fnd_message.get,
464 					1, 240);
465 	    ELSE
466 	      tax_line_descr := substrb(fnd_message.get,1,240);
467 	    END IF;
468 
469 	    -- Calculate tax amount for line
470             IF (incl_tax = 'Y') THEN
471 	      line_tax_dr := round_it((ent_dr*tax_rate)/(1+tax_rate),
472 				      rounding_rule, curr_mau);
473 	      line_tax_cr := round_it((ent_cr*tax_rate)/(1+tax_rate),
474 				      rounding_rule, curr_mau);
475             ELSE
476   	      line_tax_dr := round_it(ent_dr * tax_rate,
477 				      rounding_rule, curr_mau);
478 	      line_tax_cr := round_it(ent_cr * tax_rate,
479 				      rounding_rule, curr_mau);
480 	    END IF;
481 	    line_tax_acc_dr := round_it(line_tax_dr * conv_rate,
482 				        rounding_rule, tax_mau);
483 	    line_tax_acc_cr := round_it(line_tax_cr * conv_rate,
484 				        rounding_rule, tax_mau);
485 
486 	    total_jtax_dr := line_tax_dr;
487 	    total_jtax_cr := line_tax_cr;
488 	    total_jtax_acc_dr := line_tax_acc_dr;
489 	    total_jtax_acc_cr := line_tax_acc_cr;
490 	  END IF;
491 
492           INSERT INTO gl_je_lines
493 	    (ledger_id, je_header_id, je_line_num,
494 	     code_combination_id, status,
495 	     period_name, effective_date,
496 	     entered_dr, entered_cr,
497 	     accounted_dr, accounted_cr,
498 	     taxable_line_flag, tax_line_flag, tax_group_id,
499 	     description,
500 	     creation_date, created_by,
501 	     last_update_date, last_updated_by, last_update_login)
502 	  VALUES
503 	    (lgr_id, header_id, next_line_num,
504 	     tax_ccid, 'U',
505 	     per_name, eff_date,
506 	     total_jtax_dr, total_jtax_cr,
507 	     total_jtax_acc_dr, total_jtax_acc_cr,
508 	     'N', 'Y', tax_group,
509 	     ltrim(rtrim(tax_line_descr)),
510 	     sysdate, user_id,
511 	     sysdate, user_id, login_id);
512 
513 	  -- Clear everything out
514 	  total_ent_dr := null;
515 	  total_ent_cr := null;
516           total_jtax_dr := null;
517           total_jtax_cr := null;
518   	  total_jtax_acc_dr := null;
519 	  total_jtax_acc_cr := null;
520           total_ltax_dr := null;
521           total_ltax_cr := null;
522   	  total_ltax_acc_dr := null;
523 	  total_ltax_acc_cr := null;
524 
525 	  -- Get the next line number
526 	  next_line_num := next_line_num + 1;
527 	ELSE
528 	  -- Calculate tax amount for line
529           IF (incl_tax = 'Y') THEN
530 	    line_tax_dr := round_it((ent_dr*tax_rate)/(1+tax_rate),
531 				    rounding_rule, curr_mau);
532 	    line_tax_cr := round_it((ent_cr*tax_rate)/(1+tax_rate),
533 				    rounding_rule, curr_mau);
534           ELSE
535   	    line_tax_dr := round_it(ent_dr * tax_rate,
536 				    rounding_rule, curr_mau);
537 	    line_tax_cr := round_it(ent_cr * tax_rate,
538 				    rounding_rule, curr_mau);
539 	  END IF;
540 	  line_tax_acc_dr := round_it(line_tax_dr * conv_rate,
541 				      rounding_rule, tax_mau);
542 	  line_tax_acc_cr := round_it(line_tax_cr * conv_rate,
543 				      rounding_rule, tax_mau);
544 
545 	  -- Update the totals
546 	  IF (first_in_group) THEN
547  	    total_ltax_dr := line_tax_dr;
548 	    total_ltax_cr := line_tax_cr;
549 	    total_ltax_acc_dr := line_tax_acc_dr;
550 	    total_ltax_acc_cr := line_tax_acc_cr;
551 	  ELSE
552  	    total_ltax_dr := total_ltax_dr + line_tax_dr;
553 	    total_ltax_cr := total_ltax_cr + line_tax_cr;
554 	    total_ltax_acc_dr := total_ltax_acc_dr + line_tax_acc_dr;
555 	    total_ltax_acc_cr := total_ltax_acc_cr + line_tax_acc_cr;
556 	  END IF;
557         END IF;
558 
559 	UPDATE gl_je_lines jel
560 	SET    entered_dr = decode(incl_tax,
561 				   'Y', jel.entered_dr - line_tax_dr,
562 				   jel.entered_dr),
563 	       entered_cr = decode(incl_tax,
564 				   'Y', jel.entered_cr - line_tax_cr,
565 				   jel.entered_cr),
566 	       accounted_dr = decode(incl_tax,
567 				     'Y', jel.accounted_dr - line_tax_acc_dr,
568 				     jel.accounted_dr),
569 	       accounted_cr = decode(incl_tax,
570 				     'Y', jel.accounted_cr - line_tax_acc_cr,
571 				     jel.accounted_cr),
572 	       tax_group_id = tax_group,
573 	       last_update_date = sysdate,
574 	       last_updated_by = user_id,
575 	       last_update_login = login_id
576 	WHERE jel.je_header_id = header_id
577 	AND   jel.je_line_num = line_num;
578 
579         first_in_group := last_in_group;
580       END LOOP;
581 
582       -- Raise an error
583       IF (line_count = 0) THEN
584         IF (tax_level = 'B') THEN
585           fnd_message.set_name('SQLGL', 'GL_CTAX_BATCH_NO_TAX_LINES');
586           app_exception.raise_exception;
587 	ELSE
588           fnd_message.set_name('SQLGL', 'GL_CTAX_JOURNAL_NO_TAX_LINES');
589           app_exception.raise_exception;
590 	END IF;
591       END IF;
592 
593       -- Update the running totals
594       IF (    (tax_level = 'B')
595 	  AND (nvl(disp_header_id, -1) <> header_id)
596          ) THEN
597         UPDATE gl_je_headers
598 	SET tax_status_code = 'T',
599 	    last_updated_by = user_id,
600 	    last_update_date = sysdate,
601 	    last_update_login = login_id,
602             (running_total_dr, running_total_cr,
603 	     running_total_accounted_dr, running_total_accounted_cr)
604 	       = (SELECT sum(entered_dr), sum(entered_cr),
605 		         sum(accounted_dr), sum(accounted_cr)
606 	          FROM   gl_je_lines
607 	          WHERE  je_header_id = header_id)
608         WHERE je_header_id = header_id
609         AND   tax_status_code = 'R';
610       ELSE
611         SELECT sum(entered_dr), sum(entered_cr),
612 	       sum(accounted_dr), sum(accounted_cr)
613 	INTO   header_total_dr, header_total_cr,
614 	       header_total_acc_dr, header_total_acc_cr
615 	FROM   gl_je_lines
616 	WHERE  je_header_id = header_id;
617       END IF;
618     END LOOP;
619 
620     -- Get the updated batch running totals, if necessary.
621     IF (tax_level = 'B') THEN
622       SELECT sum(running_total_dr), sum(running_total_cr),
623 	     sum(running_total_accounted_dr),
624 	     sum(running_total_accounted_cr)
625       INTO   batch_total_dr, batch_total_cr,
626 	     batch_total_acc_dr, batch_total_acc_cr
627       FROM   gl_je_headers
628       WHERE  je_batch_id = batch_header_id
629       AND    je_header_id <> nvl(disp_header_id, -1);
630 
631       IF (disp_header_id IS NOT NULL) THEN
632         batch_total_dr := nvl(batch_total_dr, 0) + nvl(header_total_dr, 0);
633         batch_total_cr := nvl(batch_total_cr, 0) + nvl(header_total_cr, 0);
634         batch_total_acc_dr := nvl(batch_total_acc_dr, 0)
635 				+ nvl(header_total_acc_dr, 0);
636         batch_total_acc_cr := nvl(batch_total_acc_cr, 0)
637 				+ nvl(header_total_acc_cr, 0);
638       END IF;
639     END IF;
640 
641     -- Close the cursors
642     IF (tax_level = 'J') THEN
643       CLOSE j_journals;
644     ELSE
645       CLOSE b_journals;
646     END IF;
647     gl_calculate_tax2_pkg.close_cursor;
648   END calculate;
649 
650 END GL_CALCULATE_TAX_PKG;