DBA Data[Home] [Help]

PACKAGE BODY: APPS.PO_TAX_INTERFACE_PVT

Source


1 PACKAGE BODY PO_TAX_INTERFACE_PVT AS
2 /* $Header: PO_TAX_INTERFACE_PVT.plb 120.74.12020000.5 2013/03/21 09:33:57 roqiu ship $ */
3 G_PACKAGE_NAME CONSTANT VARCHAR2(30) := 'PO_TAX_INTERFACE_PVT';
4 
5 -- Logging global constants
6 D_PACKAGE_BASE CONSTANT VARCHAR2(100) := PO_LOG.get_package_base(G_PACKAGE_NAME);
7 
8 -- Private procedure declarations
9 PROCEDURE populate_zx_headers_with_po(p_po_header_id_tbl  IN  PO_TBL_NUMBER,
10                                       p_calling_program   IN  VARCHAR2);
11 
12 PROCEDURE populate_header_po(p_po_header_id  IN  NUMBER);
13 
14 PROCEDURE populate_zx_headers_with_rel(p_po_release_id_tbl  IN  PO_TBL_NUMBER,
15                                        p_calling_program    IN  VARCHAR2);
16 
17 PROCEDURE populate_header_rel(p_po_release_id  IN  NUMBER);
18 
19 PROCEDURE populate_zx_headers_with_req(p_requisition_header_id  IN  NUMBER,
20                                        p_calling_program        IN  VARCHAR2);
21 
22 PROCEDURE populate_zx_lines_with_po(p_po_header_id_tbl  IN  PO_TBL_NUMBER,
23                                     p_calling_program   IN  VARCHAR2);
24 
25 PROCEDURE populate_zx_lines_with_rel(p_po_release_id_tbl  IN  PO_TBL_NUMBER,
26                                      p_calling_program    IN  VARCHAR2);
27 
28 PROCEDURE populate_zx_lines_with_req(p_requisition_header_id  IN  NUMBER,
29                                      p_calling_program        IN  VARCHAR2);
30 
31 PROCEDURE populate_zx_dists_with_po(p_po_header_id_tbl  IN  PO_TBL_NUMBER,
32                                     p_calling_program   IN  VARCHAR2);
33 
34 PROCEDURE populate_all_dists_po(p_po_header_id  IN  NUMBER);
35 
36 PROCEDURE populate_zx_dists_with_rel(p_po_release_id_tbl  IN  PO_TBL_NUMBER,
37                                      p_calling_program    IN  VARCHAR2);
38  -- Bug 11665348
39 PROCEDURE populate_zx_lines_with_po_cal(p_po_header_id_tbl  IN  PO_TBL_NUMBER,
40                                     p_calling_program   IN  VARCHAR2);
41 
42 PROCEDURE populate_zx_lines_with_rel_cal(p_po_release_id_tbl  IN  PO_TBL_NUMBER,
43                                      p_calling_program    IN  VARCHAR2);
44   -- Bug 11665348
45 
46 
47 PROCEDURE populate_all_dists_rel(p_po_release_id IN NUMBER);
48 
49 PROCEDURE populate_zx_dists_with_req(p_requisition_header_id  IN  NUMBER,
50                                      p_calling_program        IN  VARCHAR2);
51 
52 PROCEDURE populate_zx_record(p_requisition_header_id  IN  NUMBER);
53 
54 PROCEDURE initialize_zx_gt_tables;
55 
56 PROCEDURE wipe_zx_gt_tables;
57 
58 PROCEDURE log_header_tax_attributes(p_module_base IN VARCHAR2,
59                                     p_progress    IN NUMBER);
60 
61 PROCEDURE log_line_tax_attributes(p_module_base IN VARCHAR2,
62                                   p_progress    IN NUMBER);
63 
64 PROCEDURE log_dist_tax_attributes(p_module_base IN VARCHAR2,
65                                   p_progress    IN NUMBER);
66 
67 PROCEDURE log_po_tauc(p_module_base      IN VARCHAR2,
68                       p_progress         IN NUMBER,
69                       p_po_header_id_tbl IN PO_TBL_NUMBER);
70 
71 PROCEDURE log_rel_tauc(p_module_base       IN VARCHAR2,
72                        p_progress          IN NUMBER,
73                        p_po_release_id_tbl IN PO_TBL_NUMBER);
74 
75 PROCEDURE log_req_tauc(p_module_base           IN VARCHAR2,
76                        p_progress              IN NUMBER,
77                        p_requisition_header_id IN NUMBER);
78 
79 PROCEDURE log_global_error_record(p_module_base           IN VARCHAR2,
80                                   p_progress              IN NUMBER);
81 
82 CURSOR successful_documents_csr(p_requisition_header_id IN NUMBER) IS
83   SELECT zxlgt.trx_line_id
84   FROM zx_transaction_lines_gt zxlgt
85   WHERE p_requisition_header_id=zxlgt.trx_id
86   AND p_requisition_header_id NOT IN (SELECT trx_id FROM zx_errors_gt zxegt)
87   AND zxlgt.application_id = PO_CONSTANTS_SV.APPLICATION_ID
88   AND zxlgt.entity_code = PO_CONSTANTS_SV.REQ_ENTITY_CODE
89   AND zxlgt.event_class_code = PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE;
90 
91 
92 -----------------------------------------------------------------------------
93 --Start of Comments
94 --Name: calculate_tax
95 --Pre-reqs:
96 --  Should be called when transaction data has been posted to the database
97 --  but not yet committed
98 --Modifies:
99 --  PO_LINE_LOCATIONS_ALL.tax_attribute_update_code
100 --  PO_LINE_LOCATIONS_ALL.original_shipment_id
101 --  PO_LINE_LOCATIONS_ALL.taxable_flag
102 --  PO_DISTRIBUTIONS_ALL.nonrecoverable_tax
103 --  PO_DISTRIBUTIONS_ALL.recoverable_tax
104 --Locks:
105 --  Transaction tables if update is allowed
106 --Function:
107 --  Calculate tax amounts for the documents passed in
108 --Parameters:
109 --IN:
110 --p_po_header_id_tbl
111 --  Table of po_header_id values for which tax is to be calculated
112 --p_po_release_id_tbl
113 --  Table of po_release_id values for which tax is to be calculated
114 --p_calling_program
115 --  Identifies the module that calls this procedure eg. 'PDOI'
116 --OUT:
117 --x_return_status
118 --  Standard API specification parameter
119 --  Can hold one of the following values:
120 --    FND_API.G_RET_STS_SUCCESS (='S')
121 --    FND_API.G_RET_STS_ERROR (='E')
122 --    FND_API.G_RET_STS_UNEXP_ERROR (='U')
123 --Notes:
124 --  1. Calls out to EBTax APIs calculate_tax and determine recovery to
125 --   generate tax lines and distributions. Populates recoverable_tax and
126 --   nonrecoverable_tax columns in distributions with the corrsponding tax
127 --   amounts calculated.
128 --  2. Returns all expected errors from etax API's in global tax error record
129 --End of Comments
130 -----------------------------------------------------------------------------
131 PROCEDURE calculate_tax(p_po_header_id_tbl    IN          PO_TBL_NUMBER,
132                         p_po_release_id_tbl   IN          PO_TBL_NUMBER,
133                         p_calling_program     IN          VARCHAR2,
134                         x_return_status       OUT NOCOPY  VARCHAR2
135 ) IS
136   l_module_name CONSTANT VARCHAR2(100) := 'CALCULATE_TAX';
137   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
138                                             D_PACKAGE_BASE, l_module_name);
139   d_progress NUMBER;
140   l_msg_count NUMBER;
141   l_msg_data VARCHAR2(1000);
142   l_count NUMBER;
143   l_return_status VARCHAR2(1);
144   l_po_session_gt_key PO_SESSION_GT.key%TYPE;
145   l_transaction_line_rec_type ZX_API_PUB.transaction_line_rec_type;
146 BEGIN
147   --          PO_SESSION_GT mappings:
148   --------------------------------------------------
149   --     /  INDEX_NUM1 = trx_id            \
150   --    /   NUM1       = trx_line_id        \
151   --   /    NUM2       = trx_line_dist_id    \
152   --  /     CHAR1      = event_class_code     \
153   --  \     CHAR2      = message_text         /
154   --   \    CHAR3      = error_level         /
155   --    \   CHAR4      = trx_num            /
156   --     \                                 /
157 
158   SAVEPOINT calculate_tax_savepoint;
159 
160   IF (PO_LOG.d_proc) THEN
161     PO_LOG.proc_begin(d_module_base);
162     PO_LOG.proc_begin(d_module_base, 'p_po_header_id_tbl', p_po_header_id_tbl);
163     PO_LOG.proc_begin(d_module_base, 'p_po_release_id_tbl', p_po_release_id_tbl);
164     PO_LOG.proc_begin(d_module_base, 'p_calling_program', p_calling_program);
165   END IF;
166 
167   d_progress := 0;
168 
169   -- By default return status is SUCCESS if no exception occurs
170   x_return_status := FND_API.G_RET_STS_SUCCESS;
171   -- Set global error record to uninitialized local record to set all its
172   -- components to null
173   G_TAX_ERRORS_TBL := null;
174   d_progress := 10;
175   initialize_zx_gt_tables(); -- Wipe ZX GT tables clean
176   d_progress := 20;
177   initialize_global_error_record();
178   d_progress := 30;
179   SELECT po_session_gt_s.NEXTVAL INTO l_po_session_gt_key FROM dual;
180 
181   IF PO_LOG.d_stmt THEN
182     PO_LOG.stmt(d_module_base,d_progress,'l_po_session_gt_key='||l_po_session_gt_key);
183   END IF;
184 
185   d_progress := 40;
186   IF PO_LOG.d_stmt THEN
187     PO_LOG.stmt(d_module_base,d_progress,'initial values of tax_attribute_update_codes');
188     log_po_tauc(d_module_base,d_progress,p_po_header_id_tbl);
189     log_rel_tauc(d_module_base,d_progress,p_po_release_id_tbl);
190   END IF;
191 
192   -- Percolate header and line level tax_attribute_update_code values to
193   -- shipment level so that the shipment level values get filled up
194 
195   -- 1. If shipment tax_attribute_update_code is anything other than null
196   -- or DIST_DELETE then don't update (note that even if it is DIST_DELETE,
197   -- it will be overridden by line or headers value - last where clause
198   -- 2. If both line and header level tax_attribute_update_code are null
199   -- then keep the shipment level value. So the inner query is wrapped
200   -- inside an nvl that rewrites the shipment tax_attribute_update_code
201   -- if the shipment tax_attribute_update_code is being updated to null
202   -- 3. If none of the above problems exist then update shipment
203   -- tax_attribute_update_code with line or header tax_attribute_update_code
204   -- whichever is non-null in that order
205    -- 4.
206    /* Bug 13925912: When tax determining attribute is changed only at distribution level corresponding
207     shipment's tax_attribute_update_code is not updated and thus no records are inserted into
208     zx_transaction_lines_gt and no call is being made to ZX calculate_tax API. Updating shipments
209     tax_attribute_update_code accordingly to overcome above issue */
210 
211 FORALL i IN 1..p_po_header_id_tbl.COUNT
212    UPDATE   po_line_locations_all pll
213    SET   pll.tax_attribute_update_code =
214             NVL ((SELECT NVL (
215             (SELECT  pod.tax_attribute_update_code
216                             FROM   po_distributions_all pod
217                            WHERE   pod.tax_attribute_update_code = 'UPDATE'
218                              AND pod.line_location_id = pll.line_location_id
219                              AND ROWNUM = 1),
220                             NVL (pl.tax_attribute_update_code,
221                                  ph.tax_attribute_update_code))
222                   FROM   po_headers_all ph,
223                          po_lines_all pl
224                  WHERE       pll.po_line_id = pl.po_line_id
225                          AND pll.po_header_id = ph.po_header_id
226                ),
227                pll.tax_attribute_update_code
228             )
229  WHERE   pll.po_header_id = p_po_header_id_tbl(i)
230          AND (pll.tax_attribute_update_code IS NULL
231               OR pll.tax_attribute_update_code = 'DIST_DELETE');
232 
233   d_progress := 50;
234   FORALL i IN 1..p_po_release_id_tbl.COUNT
235     UPDATE po_line_locations_all pll
236     SET pll.tax_attribute_update_code =
237       NVL(
238         (SELECT NVL (
239             (SELECT  pod.tax_attribute_update_code
240                             FROM   po_distributions_all pod
241                            WHERE   pod.tax_attribute_update_code = 'UPDATE'
242                              AND pod.line_location_id = pll.line_location_id
243                              AND ROWNUM = 1), pr.tax_attribute_update_code)
244          FROM po_releases_all pr
245          WHERE pll.po_release_id = pr.po_release_id
246          -- following AND written only for clarity
247          --AND ph.tax_attribute_update_code IS NOT NULL
248         )
249         ,pll.tax_attribute_update_code
250       )
251     WHERE
252     pll.po_release_id = p_po_release_id_tbl(i)
253     AND (pll.tax_attribute_update_code IS NULL
254          OR pll.tax_attribute_update_code = 'DIST_DELETE');
255 
256     -- Replaced po_headers_all with po_releases_all for releases case.
257     -- End bug 13925912
258 
259   IF PO_LOG.d_stmt THEN
260     PO_LOG.stmt(d_module_base,d_progress,'tax_attribute_update_codes after denormalization');
261     log_po_tauc(d_module_base,d_progress,p_po_header_id_tbl);
262     log_rel_tauc(d_module_base,d_progress,p_po_release_id_tbl);
263   END IF;
264 
265   -- Populate ZX headers and lines GT tables with transaction data
266   d_progress := 60;
267   populate_zx_headers_with_po(p_po_header_id_tbl, p_calling_program);
268   d_progress := 70;
269   populate_zx_headers_with_rel(p_po_release_id_tbl, p_calling_program);
270   d_progress := 80;
271   populate_zx_lines_with_po(p_po_header_id_tbl, p_calling_program);
272   d_progress := 90;
273   populate_zx_lines_with_rel(p_po_release_id_tbl, p_calling_program);
274 
275   d_progress := 100;
276   -- Call eTax API calculate_tax to construct tax lines
277   -- For lines wih line_level_action of COPY_AND_CREATE eTax will use
278   -- Additional Tax Attributes from source doucment to create the tax lines
279   BEGIN
280     SELECT COUNT(1) INTO l_count FROM zx_transaction_lines_gt;
281   EXCEPTION WHEN OTHERS THEN
282     l_count := 0;
283     IF PO_LOG.d_stmt THEN
284       PO_LOG.stmt(d_module_base,d_progress,'Exception while selecting from zx_transaction_lines_gt');
285     END IF;
286   END;
287 
288   IF PO_LOG.d_stmt THEN
289     PO_LOG.stmt(d_module_base,d_progress,'Number of rows in zx_transaction_lines_gt='||l_count);
290   END IF;
291 
292   IF (l_count <> 0) THEN
293 
294     d_progress := 110;
295     -- Log table parameters
296     IF PO_LOG.d_stmt THEN
297       PO_LOG.stmt(d_module_base,d_progress,'Table parameters before eTax default/redefault');
298       log_header_tax_attributes(d_module_base,d_progress);
299       log_line_tax_attributes(d_module_base,d_progress);
300     END IF;
301 
302     -- Call the eTax defaulting/redefaulting APIs which populate the
303     -- Additional Tax Attributes back into the ZX GT tables
304     IF (p_calling_program = 'PDOI') THEN
305       d_progress := 120;
306       -- For PDOI, eTax needs to do extra validations since data is entered by
307       -- user in interface tables. So call validate_and_default_tax_attribs
308       -- Only need to default, not redefault because through PDOI POs can only
309       -- be added to, not updated
310       IF PO_LOG.d_stmt THEN
311         PO_LOG.stmt(d_module_base,d_progress,'Calling validate_and_default_tax_attr');
312       END IF;
313       ZX_API_PUB.validate_and_default_tax_attr(
314         p_api_version           =>  1.0,
315         p_init_msg_list         =>  FND_API.G_TRUE,
316         p_commit                =>  FND_API.G_FALSE,
317         p_validation_level      =>  FND_API.G_VALID_LEVEL_FULL,
318         x_return_status         =>  l_return_status,
319         x_msg_count             =>  l_msg_count,
320         x_msg_data              =>  l_msg_data
321       );
322 
323       d_progress := 130;
324       IF PO_LOG.d_stmt THEN
325         PO_LOG.stmt(d_module_base,d_progress,'validate_and_default_tax_attr returned with status '||l_return_status);
326       END IF;
327 
328       -- Raise if any unexpected error
329       IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
330         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
331       END IF;
332 
333       -- If expected errors, store error details in po_session_gt temporarily
334       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
335         d_progress := 140;
336         x_return_status := l_return_status;
337         INSERT INTO po_session_gt(
338           key
339           ,index_num1
340           ,num1
341           ,num2
342           ,char1
343           ,char2
344           ,char3
345           ,char4
346         )
347         SELECT
348           l_po_session_gt_key
349           ,zxvegt.trx_id
350           ,zxvegt.trx_line_id
351           ,null
352           ,zxvegt.event_class_code
353           ,zxvegt.message_text
354           ,'VALIDATE'
355           ,ph.segment1
356         FROM zx_validation_errors_gt zxvegt, po_headers_all ph
357         WHERE zxvegt.event_class_code = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE
358         AND zxvegt.trx_id = ph.po_header_id;
359 
360         d_progress := 150;
361         INSERT INTO po_session_gt(
362           key
363           ,index_num1
364           ,num1
365           ,num2
366           ,char1
367           ,char2
368           ,char3
369           ,char4
370         )
371         SELECT
372           l_po_session_gt_key
373           ,zxvegt.trx_id
374           ,zxvegt.trx_line_id
375           ,null
376           ,zxvegt.event_class_code
377           ,zxvegt.message_text
378           ,'VALIDATE'
379           ,ph.segment1
380         FROM zx_validation_errors_gt zxvegt, po_headers_all ph,
381              po_releases_all pr
382         WHERE zxvegt.event_class_code = PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE
383         AND zxvegt.trx_id = pr.po_release_id
384         AND pr.po_header_id = ph.po_header_id;
385 
386         BEGIN
387           SELECT COUNT(1) INTO l_count FROM zx_validation_errors_gt;
388         EXCEPTION WHEN OTHERS THEN
389           IF PO_LOG.d_stmt THEN
390             PO_LOG.stmt(d_module_base,d_progress,'Exception while hitting zx_validation_errors_gt');
391           END IF;
392           l_count := 0;
393         END;
394 
395         IF PO_LOG.d_stmt THEN
396           PO_LOG.stmt(d_module_base,d_progress,'Number of records in zx_validation_errors_gt '||l_count);
397         END IF;
398 
399 -- Bug 12907158
400       RAISE FND_API.G_EXC_ERROR;
401 
402       END IF;
403 
404       d_progress := 160;
405       -- Delete data from zx gt tables for which defaulting/redefaulting failed
406       DELETE FROM zx_trx_headers_gt
407       WHERE trx_id IN (SELECT DISTINCT index_num1 FROM po_session_gt psgt
408                        WHERE psgt.key = l_po_session_gt_key
409                        AND psgt.char1 = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE);
410       d_progress := 170;
411       DELETE FROM zx_transaction_lines_gt
412       WHERE trx_id IN (SELECT DISTINCT index_num1 FROM po_session_gt psgt
413                        WHERE psgt.key = l_po_session_gt_key
414                        AND psgt.char1 = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE);
415 
416     ELSIF (p_calling_program <> 'COPY_DOCUMENT') THEN
417       d_progress := 180;
418       -- This API cannot handle the presence of COPY_AND_CREATE line level
419       -- actions so call is prevented if calling program is COPY_DOCUMENT
420       IF PO_LOG.d_stmt THEN
421         PO_LOG.stmt(d_module_base,d_progress,'Calling get_default_tax_det_attribs');
422       END IF;
423       ZX_API_PUB.get_default_tax_det_attribs(
424         p_api_version           =>  1.0,
425         p_init_msg_list         =>  FND_API.G_TRUE,
426         p_commit                =>  FND_API.G_FALSE,
427         p_validation_level      =>  FND_API.G_VALID_LEVEL_FULL,
428         x_return_status         =>  l_return_status,
429         x_msg_count             =>  l_msg_count,
430         x_msg_data              =>  l_msg_data
431       );
432 
433       d_progress := 190;
434       IF PO_LOG.d_stmt THEN
435         PO_LOG.stmt(d_module_base,d_progress,'get_default_tax_det_attribs returned with status '||l_return_status);
436       END IF;
437 
438       -- This API cannot give expected errors. However raise unexpected errors
439       IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
440         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
441       END IF;
442 
443     END IF; -- IF (p_calling_program = 'PDOI')
444 
445  -- Bug 11665348
446   END IF;    -- IF (l_count <> 0)
447 
448 
449     -- Populate ZX headers and lines GT tables with transaction data
450   d_progress := 196;
451   populate_zx_lines_with_po_cal(p_po_header_id_tbl, p_calling_program);
452   d_progress := 198;
453   populate_zx_lines_with_rel_cal(p_po_release_id_tbl, p_calling_program);
454 
455   BEGIN
456     SELECT COUNT(1) INTO l_count FROM zx_transaction_lines_gt;
457   EXCEPTION WHEN OTHERS THEN
458     l_count := 0;
459     IF PO_LOG.d_stmt THEN
460       PO_LOG.stmt(d_module_base,d_progress,'Exception while selecting from zx_transaction_lines_gt');
461     END IF;
462   END;
463 
464   IF PO_LOG.d_stmt THEN
465     PO_LOG.stmt(d_module_base,d_progress,'Number of rows in zx_transaction_lines_gt='||l_count);
466   END IF;
467 
468    IF (l_count <> 0) THEN
469 
470    -- End bug 11665348
471 
472 
473 
474     d_progress := 200;
475     -- Override product_type Additional Tax Attribute if not already populated
476     -- by eTax defaulting/redefaulting API
477     UPDATE zx_transaction_lines_gt zxlgt
478     SET zxlgt.product_type =
479       (SELECT DECODE(pl.purchase_basis,
480                      'GOODS', 'GOODS',
481                      'SERVICES')
482        FROM po_line_locations_all pll, po_lines_all pl
483        WHERE pll.line_location_id = zxlgt.trx_line_id
484        AND pll.po_line_id = pl.po_line_id)
485     WHERE zxlgt.product_type IS NULL
486     AND zxlgt.line_level_action = 'CREATE';
487 
488     d_progress := 210;
489 
490     -- Log table parameters
491     IF PO_LOG.d_stmt THEN
492       PO_LOG.stmt(d_module_base,d_progress,'Table parameters before calculate_tax');
493       log_header_tax_attributes(d_module_base,d_progress);
494       log_line_tax_attributes(d_module_base,d_progress);
495     END IF;
496 
497     ZX_API_PUB.calculate_tax(
498       p_api_version           =>  1.0,
499       p_init_msg_list         =>  FND_API.G_TRUE,
500       p_commit                =>  FND_API.G_FALSE,
501       p_validation_level      =>  FND_API.G_VALID_LEVEL_FULL,
502       x_return_status         =>  l_return_status,
503       x_msg_count             =>  l_msg_count,
504       x_msg_data              =>  l_msg_data
505     );
506 
507     d_progress := 220;
508     IF PO_LOG.d_stmt THEN
509       PO_LOG.stmt(d_module_base,d_progress,'calculate_tax returned with status '||l_return_status);
510     END IF;
511 
512     -- Raise if any unexpected error
513     IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
514       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
515     END IF;
516 
517     -- If expected errors, store error details in po_session_gt temporarily
518     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
519       x_return_status := l_return_status;
520       d_progress := 230;
521       INSERT INTO po_session_gt(
522         key
523         ,index_num1
524         ,num1
525         ,num2
526         ,char1
527         ,char2
528         ,char3
529         ,char4
530       )
531       SELECT
532         l_po_session_gt_key
533         ,zxegt.trx_id
534         ,zxegt.trx_line_id
535         ,zxegt.trx_line_dist_id
536         ,zxegt.event_class_code
537         ,zxegt.message_text
538         ,'CALCULATE_TAX'
539         ,ph.segment1
540       FROM zx_errors_gt zxegt, po_headers_all ph
541       WHERE zxegt.event_class_code = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE
542       AND zxegt.trx_id = ph.po_header_id;
543 
544       d_progress := 240;
545       INSERT INTO po_session_gt(
546         key
547         ,index_num1
548         ,num1
549         ,num2
550         ,char1
551         ,char2
552         ,char3
553         ,char4
554       )
555       SELECT
556         l_po_session_gt_key
557         ,zxegt.trx_id
558         ,zxegt.trx_line_id
559         ,zxegt.trx_line_dist_id
560         ,zxegt.event_class_code
561         ,zxegt.message_text
562         ,'CALCULATE_TAX'
563         ,ph.segment1
564       FROM zx_errors_gt zxegt, po_headers_all ph, po_releases_all pr
565       WHERE zxegt.event_class_code = PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE
566       AND zxegt.trx_id = pr.po_release_id
567       AND pr.po_header_id = ph.po_header_id;
568 
569       BEGIN
570         SELECT COUNT(1) INTO l_count FROM zx_errors_gt;
571       EXCEPTION WHEN OTHERS THEN
572         IF PO_LOG.d_stmt THEN
573           PO_LOG.stmt(d_module_base,d_progress,'Exception while hitting zx_errors_gt');
574         END IF;
575         l_count := 0;
576       END;
577 
578 -- Bug 12907158
579     RAISE FND_API.G_EXC_ERROR;
580 
581       IF PO_LOG.d_stmt THEN
582         PO_LOG.stmt(d_module_base,d_progress,'Number of records in zx_errors_gt '||l_count);
583       END IF;
584 
585     /* Bug#12317965: ZX_LINE_DET_FACTORS should not have records inserted, in case
586                      the tax lines insertion in to ZX_LINES fails. */
587     d_progress := 245;
588     ROLLBACK TO SAVEPOINT calculate_tax_savepoint;
589 
590     END IF;
591 
592     d_progress := 250;
593     -- Delete data from zx gt tables for which defaulting/redefaulting failed
594     DELETE FROM zx_trx_headers_gt
595     WHERE trx_id IN (SELECT DISTINCT index_num1 FROM po_session_gt psgt
596                      WHERE psgt.key = l_po_session_gt_key
597                      AND psgt.char1 = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE
598                      AND psgt.char3 = 'CALCULATE_TAX');
599     d_progress := 260;
600     DELETE FROM zx_trx_headers_gt
601     WHERE trx_id IN (SELECT DISTINCT index_num1 FROM po_session_gt psgt
602                      WHERE psgt.key = l_po_session_gt_key
603                      AND psgt.char1 = PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE
604                      AND psgt.char3 = 'CALCULATE_TAX');
605     d_progress := 270;
606     DELETE FROM zx_transaction_lines_gt
607     WHERE trx_id IN (SELECT DISTINCT index_num1 FROM po_session_gt psgt
608                      WHERE psgt.key = l_po_session_gt_key
609                      AND psgt.char1 = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE
610                      AND psgt.char3 = 'CALCULATE_TAX');
611     d_progress := 280;
612     DELETE FROM zx_transaction_lines_gt
613     WHERE trx_id IN (SELECT DISTINCT index_num1 FROM po_session_gt psgt
614                      WHERE psgt.key = l_po_session_gt_key
615                      AND psgt.char1 = PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE
616                      AND psgt.char3 = 'CALCULATE_TAX');
617 
618   END IF; -- IF (l_count <> 0)
619 
620   d_progress := 290;
621   -- Populate ZX distributions GT table with transaction distribution data
622   populate_zx_dists_with_po(p_po_header_id_tbl, p_calling_program);
623   d_progress := 300;
624   populate_zx_dists_with_rel(p_po_release_id_tbl, p_calling_program);
625 
626   d_progress := 310;
627   -- Call eTax API determine_recovery to distribute tax lines
628   BEGIN
629     SELECT COUNT(1) INTO l_count FROM zx_itm_distributions_gt;
630   EXCEPTION WHEN OTHERS THEN
631     l_count := 0;
632     IF PO_LOG.d_stmt THEN
633       PO_LOG.stmt(d_module_base,d_progress,'Exception while selecting from zx_itm_distributions_gt');
634     END IF;
635   END;
636 
637   IF PO_LOG.d_stmt THEN
638     PO_LOG.stmt(d_module_base,d_progress,'Number of rows in zx_itm_distributions_gt='||l_count);
639   END IF;
640 
641   IF (l_count <> 0) THEN
642     d_progress := 320;
643     -- Update event_type_code on zx headers as required by determine_recovery API
644     -- Update to DISTRIBUTED only if all distributions have
645     -- tax_attribute_update_code = 'CREATE'
646     UPDATE zx_trx_headers_gt zxhgt
647     SET zxhgt.event_type_code =
648       DECODE(zxhgt.event_class_code,
649              PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE,
650                NVL2((SELECT 'EXISTING DISTRIBUTIONS'
651                      FROM DUAL
652                      WHERE EXISTS
653                        (SELECT 'Y'
654                         FROM po_distributions_all pd
655                         WHERE pd.po_header_id = zxhgt.trx_id
656                         AND (pd.tax_attribute_update_code <> 'CREATE'
657                              OR pd.tax_attribute_update_code IS NULL)
658                        )
659                     ),
660                     PO_CONSTANTS_SV.PO_REDISTRIBUTED,
661                     PO_CONSTANTS_SV.PO_DISTRIBUTED
662                    ),
663              --Release
664              NVL2((SELECT 'EXISTING DISTRIBUTIONS'
665                    FROM DUAL
666                    WHERE EXISTS
667                      (SELECT 'Y'
668                       FROM po_distributions_all pd
669                       WHERE pd.po_release_id = zxhgt.trx_id
670                       AND (pd.tax_attribute_update_code <> 'CREATE'
671                            OR pd.tax_attribute_update_code IS NULL)
672                      )
673                   ),
674                   PO_CONSTANTS_SV.REL_REDISTRIBUTED,
675                   PO_CONSTANTS_SV.REL_DISTRIBUTED
676                  )
677             );
678 
679     d_progress := 330;
680 
681     -- Log table parameters
682     IF PO_LOG.d_stmt THEN
683       PO_LOG.stmt(d_module_base,d_progress,'Table parameters before determine_recovery');
684       log_header_tax_attributes(d_module_base,d_progress);
685       log_line_tax_attributes(d_module_base,d_progress);
686       log_dist_tax_attributes(d_module_base,d_progress);
687     END IF;
688 
689     ZX_API_PUB.determine_recovery(
690       p_api_version           =>  1.0,
691       p_init_msg_list         =>  FND_API.G_TRUE,
692       p_commit                =>  FND_API.G_FALSE,
693       p_validation_level      =>  FND_API.G_VALID_LEVEL_FULL,
694       x_return_status         =>  l_return_status,
695       x_msg_count             =>  l_msg_count,
696       x_msg_data              =>  l_msg_data
697     );
698 
699     d_progress := 340;
700     IF PO_LOG.d_stmt THEN
701       PO_LOG.stmt(d_module_base,d_progress,'determine_recovery returned with status '||l_return_status);
702     END IF;
703 
704     -- Raise if any unexpected error
705     IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
706       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
707     END IF;
708 
709     -- If expected errors, store error details in po_session_gt temporarily
710     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
711       x_return_status := l_return_status;
712       d_progress := 350;
713       INSERT INTO po_session_gt(
714         key
715         ,index_num1
716         ,num1
717         ,num2
718         ,char1
719         ,char2
720         ,char3
721         ,char4
722       )
723       SELECT
724         l_po_session_gt_key
725         ,zxegt.trx_id
726         ,zxegt.trx_line_id
727         ,zxegt.trx_line_dist_id
728         ,zxegt.event_class_code
729         ,zxegt.message_text
730         ,'DETERMINE_RECOVERY'
731         ,ph.segment1
732       FROM zx_errors_gt zxegt, po_headers_all ph
733       WHERE zxegt.event_class_code = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE
734       AND zxegt.trx_id = ph.po_header_id;
735 
736       d_progress := 360;
737       INSERT INTO po_session_gt(
738         key
739         ,index_num1
740         ,num1
741         ,num2
742         ,char1
743         ,char2
744         ,char3
745         ,char4
746       )
747       SELECT
748         l_po_session_gt_key
749         ,zxegt.trx_id
750         ,zxegt.trx_line_id
751         ,zxegt.trx_line_dist_id
752         ,zxegt.event_class_code
753         ,zxegt.message_text
754         ,'DETERMINE_RECOVERY'
755         ,ph.segment1
756       FROM zx_errors_gt zxegt, po_headers_all ph, po_releases_all pr
757       WHERE zxegt.event_class_code = PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE
758       AND zxegt.trx_id = pr.po_release_id
759       AND pr.po_header_id = ph.po_header_id;
760 
761       d_progress := 370;
762       BEGIN
763         SELECT COUNT(1) INTO l_count FROM zx_errors_gt;
764       EXCEPTION WHEN OTHERS THEN
765         IF PO_LOG.d_stmt THEN
766           PO_LOG.stmt(d_module_base,d_progress,'Exception while hitting zx_errors_gt');
767         END IF;
768         l_count := 0;
769       END;
770 
771       IF PO_LOG.d_stmt THEN
772         PO_LOG.stmt(d_module_base,d_progress,'Number of records in zx_errors_gt '||l_count);
773       END IF;
774 
775       -- Bug 5169449. Removed deletions from headers and lines gt tables
776       -- because if the control has come till determine_recovery then the
777       -- tax lines for the document have been calculated correctly. Hence,
778       -- for failure of determine_recovery, only the tauc's at distribution
779       -- level should be retained. The tauc's at header and shipment level
780       -- should be nulled out otherwise the header and line level action will
781       -- get passed as CREATE in the next tax calculation (ie at the time of
782       -- approve)
783       d_progress := 380;
784       DELETE FROM zx_itm_distributions_gt
785       WHERE trx_id IN (SELECT DISTINCT index_num1 FROM po_session_gt psgt
786                        WHERE psgt.key = l_po_session_gt_key
787                        AND psgt.char1 = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE
788                        AND psgt.char3 = 'DETERMINE_RECOVERY');
789       d_progress := 390;
790       DELETE FROM zx_itm_distributions_gt
791       WHERE trx_id IN (SELECT DISTINCT index_num1 FROM po_session_gt psgt
792                        WHERE psgt.key = l_po_session_gt_key
793                        AND psgt.char1 = PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE
794                        AND psgt.char3 = 'DETERMINE_RECOVERY');
795 
796       d_progress := 400;
797       -- Update the distributions that errored out and yet have a tauc of null
798       -- If this is not done, it is possible that all dist tauc's for a
799       -- document get nulled out and hence tax redistribution does not take
800       -- place despite determine_recovery erroring out for some of them
801       UPDATE po_distributions_all
802       SET tax_attribute_update_code = 'NO_ACTION'
803       WHERE tax_attribute_update_code IS NULL
804       AND po_distribution_id IN (SELECT psgt.NUM2 FROM po_session_gt psgt
805                                  WHERE psgt.key = l_po_session_gt_key
806                                  AND psgt.char3 = 'DETERMINE_RECOVERY');
807 
808 --Bug 12907158
809     RAISE FND_API.G_EXC_ERROR;
810 
811     END IF; --IF (l_return_status = FND_API.G_RET_STS_ERROR)
812 
813     d_progress := 450;
814 
815     -- bug5650927
816     -- Modified the subquery by adding a where clause so that trx_id is joined
817     -- to different columns based on entity_code and event_class_code
818 
819     -- Populate recoverable/nonrecoverable tax columns in distributions table
820     UPDATE po_distributions_all pd
821     SET pd.recoverable_tax =
822           (SELECT SUM(zxdist.rec_nrec_tax_amt)
823            FROM zx_rec_nrec_dist zxdist
824            WHERE zxdist.trx_line_dist_id = pd.po_distribution_id
825            AND zxdist.recoverable_flag = 'Y'
826            AND zxdist.application_id = PO_CONSTANTS_SV.APPLICATION_ID
827            AND ( (zxdist.entity_code = PO_CONSTANTS_SV.PO_ENTITY_CODE
828                   AND zxdist.event_class_code =
829                         PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE
830                   AND zxdist.trx_id = pd.po_header_id)
831                  OR
832                  (zxdist.entity_code = PO_CONSTANTS_SV.REL_ENTITY_CODE
833                   AND zxdist.event_class_code =
834                         PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE
835                   AND zxdist.trx_id = pd.po_release_id)))
836        ,pd.nonrecoverable_tax =
837           (SELECT SUM(zxdist.rec_nrec_tax_amt)
838            FROM zx_rec_nrec_dist zxdist
839            WHERE zxdist.trx_line_dist_id = pd.po_distribution_id
840            AND zxdist.recoverable_flag = 'N'
841            AND zxdist.application_id = PO_CONSTANTS_SV.APPLICATION_ID
842            AND ( (zxdist.entity_code = PO_CONSTANTS_SV.PO_ENTITY_CODE
843                   AND zxdist.event_class_code =
844                         PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE
845                   AND zxdist.trx_id = pd.po_header_id)
846                  OR
847                  (zxdist.entity_code = PO_CONSTANTS_SV.REL_ENTITY_CODE
848                   AND zxdist.event_class_code =
849                         PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE
850                   AND zxdist.trx_id = pd.po_release_id))),
851 		--Bug 10305728 start
852         last_update_date = sysdate,
853 	    last_updated_by = fnd_global.user_id,
854         last_update_login = fnd_global.login_id
855         --Bug 10305728  end
856     WHERE pd.po_distribution_id IN
857       (SELECT trx_line_dist_id FROM zx_itm_distributions_gt);
858 
859   END IF;
860 
861   d_progress := 460;
862 
863   -- Null out tax_attribute_update_code columns
864 
865   -- Headers and lines tables can be nulled out because they were
866   -- denormalized. However in headers, the exception is where CREATE
867   -- needs to be passed to eTax on PO updation as during an error occured
868   -- during creation. For lines, this is not an issue because eTax requires
869   -- no action for a line (while correct actions are required for header and
870   -- shipment levels). So if an error occurs during creation, and user then
871   -- updates a line, tax_attribute_update_code at line will be UPDATE, but
872   -- shipment level tax_attribute_update_code's will be correct (because of
873   -- denormalization) and will have a higher priority than those of the line
874   FORALL i IN 1..p_po_header_id_tbl.COUNT
875       UPDATE po_headers_all ph
876       SET ph.tax_attribute_update_code = null
877       WHERE ph.po_header_id = p_po_header_id_tbl(i)
878       -- Bug 4774900 null out if even a single shipment has been processed
879       -- correctly. That would mean that the header has been recorded
880       -- in eTax, and next time we need to pass UPDATE
881       AND EXISTS (SELECT 'Y'
882                   FROM zx_transaction_lines_gt zxlgt
883                   WHERE zxlgt.trx_id=ph.po_header_id);
884 
885   d_progress := 470;
886   FORALL i IN 1..p_po_release_id_tbl.COUNT
887       UPDATE po_releases_all pr
888       SET pr.tax_attribute_update_code = null
889       WHERE pr.po_release_id = p_po_release_id_tbl(i)
890       AND EXISTS (SELECT 'Y'
891                   FROM zx_transaction_lines_gt zxlgt
892                   WHERE zxlgt.trx_id=pr.po_release_id);
893 
894   d_progress := 480;
895   FORALL i IN 1..p_po_header_id_tbl.COUNT
896       UPDATE po_lines_all
897       SET tax_attribute_update_code = null
898       WHERE po_header_id = p_po_header_id_tbl(i);
899 
900   d_progress := 490;
901 
902   -- bug5219124 START
903   -- Separate the original update statement into two
904 
905   UPDATE po_line_locations_all
906   SET tax_attribute_update_code = null,
907       original_shipment_id = null
908   WHERE line_location_id IN (SELECT trx_line_id FROM zx_transaction_lines_gt);
909 
910   UPDATE po_line_locations_all
911   SET tax_attribute_update_code = null,
912       original_shipment_id = null
913   WHERE tax_attribute_update_code = 'DIST_DELETE'
914   AND line_location_id IN (SELECT trx_line_id FROM zx_itm_distributions_gt);
915 
916 
917   -- bug5219124 END
918 
919   d_progress := 500;
920   UPDATE po_distributions_all
921   SET tax_attribute_update_code = null
922   WHERE po_distribution_id IN (SELECT trx_line_dist_id
923                                FROM zx_itm_distributions_gt);
924 
925   d_progress := 510;
926 
927   -- bug5685869
928   -- Add a join to zl.trx_id so that the index on zx_lines can be used
929   -- more efficiently
930 
931   -- Set the taxable flag column of shipments which have tax lines
932   UPDATE po_line_locations_all pll
933   SET pll.taxable_flag =
934     DECODE((SELECT COUNT(1) FROM zx_lines zl
935             WHERE zl.trx_line_id = pll.line_location_id
936             AND zl.application_id = PO_CONSTANTS_SV.APPLICATION_ID
937             AND ( (zl.entity_code = PO_CONSTANTS_SV.PO_ENTITY_CODE
938                    AND zl.event_class_code = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE
939                    AND zl.trx_id = pll.po_header_id)
940                   OR
941                   (zl.entity_code = PO_CONSTANTS_SV.REL_ENTITY_CODE
942                    AND zl.event_class_code = PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE
943                    AND zl.trx_id = pll.po_release_id))),
944            0, 'N',
945            'Y'
946           )
947   WHERE
948     pll.line_location_id IN (SELECT trx_line_id FROM zx_transaction_lines_gt);
949 
950 
951   d_progress := 520;
952   -- Pour all errors in po_session_gt into global tax error record
953   SELECT
954     psgt.char3 --error_level
955     ,decode(psgt.char1,--document_type_code
956             PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE, PO_CONSTANTS_SV.PO,
957             PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE, PO_CONSTANTS_SV.RELEASE
958            )
959     ,psgt.index_num1 --document_id
960     ,psgt.char4 --document_num
961     ,null --line_id
962     ,null --line_num
963     ,pll.line_location_id --line_location_id
964     ,pll.shipment_num --shipment_num
965     ,pd.po_distribution_id --distribution_id
966     ,pd.distribution_num --distribution_num
967     ,psgt.char2 --message_text
968   BULK COLLECT INTO G_TAX_ERRORS_TBL
969   FROM po_session_gt psgt, po_line_locations_all pll, po_distributions_all pd
970   WHERE psgt.num1 = pll.line_location_id
971   AND psgt.num2 = pd.po_distribution_id(+)
972   AND psgt.key = l_po_session_gt_key;
973 
974   IF PO_LOG.d_stmt THEN
975     PO_LOG.stmt(d_module_base,d_progress,'Number of error records collected '||G_TAX_ERRORS_TBL.error_level.COUNT);
976     log_global_error_record(d_module_base, d_progress);
977   END IF;
978 
979   -- Bug 5363122. Wipe out ZX GT tables at the end of tax call
980   wipe_zx_gt_tables();
981 
982   IF (PO_LOG.d_proc) THEN
983     PO_LOG.proc_end(d_module_base);
984     PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
985   END IF;
986   d_progress := 540;
987 
988 EXCEPTION
989 
990 /* Bug 12907158: Raise the expected error and collect the data into
991                G_TAX_ERRORS_TBL so that they can be processed. */
992 
993   WHEN FND_API.G_EXC_ERROR THEN
994     x_return_status := FND_API.G_RET_STS_ERROR ;
995 
996 
997      SELECT
998     psgt.char3 --error_level
999     ,decode(psgt.char1,--document_type_code
1000             PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE, PO_CONSTANTS_SV.PO,
1001             PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE, PO_CONSTANTS_SV.RELEASE
1002            )
1003     ,psgt.index_num1 --document_id
1004     ,psgt.char4 --document_num
1005     ,null --line_id
1006     ,null --line_num
1007     ,pll.line_location_id --line_location_id
1008     ,pll.shipment_num --shipment_num
1009     ,pd.po_distribution_id --distribution_id
1010     ,pd.distribution_num --distribution_num
1011     ,psgt.char2 --message_text
1012   BULK COLLECT INTO G_TAX_ERRORS_TBL
1013   FROM po_session_gt psgt, po_line_locations_all pll, po_distributions_all pd
1014   WHERE psgt.index_num1 = pll.po_header_id
1015   AND pll.line_location_id = pd.line_location_id
1016   AND psgt.key = l_po_session_gt_key;
1017 
1018   IF PO_LOG.d_stmt THEN
1019     PO_LOG.stmt(d_module_base,d_progress,'Number of error records collected '||G_TAX_ERRORS_TBL.error_level.COUNT);
1020     log_global_error_record(d_module_base, d_progress);
1021   END IF;
1022 
1023    ROLLBACK TO SAVEPOINT calculate_tax_savepoint;
1024 
1025   -- end Bug 12907158
1026 
1027   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1028     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1029     ROLLBACK TO SAVEPOINT calculate_tax_savepoint;
1030 
1031     -- Initialize to flush out expected errors if any
1032     initialize_global_error_record();
1033     -- Add a new error
1034     append_error(p_error_level => null,
1035                  p_document_type_code => null,
1036                  p_document_id => null,
1037                  p_document_num => null,
1038                  p_line_id => null,
1039                  p_line_num => null,
1040                  p_line_location_id => null,
1041                  p_shipment_num => null,
1042                  p_distribution_id => null,
1043                  p_distribution_num => null,
1044                  p_message_text => l_msg_data);
1045 
1046     IF (PO_LOG.d_exc) THEN
1047       PO_LOG.exc(d_module_base, d_progress, l_msg_data);
1048       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
1049     END IF;
1050 
1051   WHEN OTHERS THEN
1052     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1053     ROLLBACK TO SAVEPOINT calculate_tax_savepoint;
1054 
1055     -- Initialize to flush out expected errors if any
1056     initialize_global_error_record();
1057     -- Add a new error
1058     append_error(p_error_level => null,
1059                  p_document_type_code => null,
1060                  p_document_id => null,
1061                  p_document_num => null,
1062                  p_line_id => null,
1063                  p_line_num => null,
1064                  p_line_location_id => null,
1065                  p_shipment_num => null,
1066                  p_distribution_id => null,
1067                  p_distribution_num => null,
1068                  p_message_text => d_module_base||'#'||d_progress||':'
1069                                    ||SQLCODE || SQLERRM);
1070 
1071     IF (PO_LOG.d_exc) THEN
1072       PO_LOG.exc(d_module_base, d_progress, SQLCODE || SQLERRM);
1073       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
1074     END IF;
1075 
1076 END calculate_tax;
1077 
1078 
1079 -----------------------------------------------------------------------------
1080 --Start of Comments
1081 --Name: calculate_tax
1082 --Pre-reqs:
1083 --  Should be called when transaction data has been posted to the database
1084 --  but not yet committed
1085 --Modifies:
1086 --  PO_LINE_LOCATIONS_ALL.tax_attribute_update_code
1087 --  PO_LINE_LOCATIONS_ALL.original_shipment_id
1088 --  PO_LINE_LOCATIONS_ALL.taxable_flag
1089 --  PO_DISTRIBUTIONS_ALL.nonrecoverable_tax
1090 --  PO_DISTRIBUTIONS_ALL.recoverable_tax
1091 --Locks:
1092 --  Transaction tables if update is allowed
1093 --Function:
1094 --  Calculate tax amounts for the documents passed in
1095 --Parameters:
1096 --IN:
1097 --p_po_header_id
1098 --  po_header_id of the PO for which tax is to be calculated
1099 --p_po_release_id
1100 --  po_release_id of the Releasefor which tax is to be calculated
1101 --p_calling_program
1102 --  Identifies the module that calls this procedure eg. 'PDOI'
1103 --OUT:
1104 --x_return_status
1105 --  Standard API specification parameter
1106 --  Can hold one of the following values:
1107 --    FND_API.G_RET_STS_SUCCESS (='S')
1108 --    FND_API.G_RET_STS_ERROR (='E')
1109 --    FND_API.G_RET_STS_UNEXP_ERROR (='U')
1110 --Notes:
1111 --  1. Wrapper procedure that calls its overloaded bulk version
1112 --   Called from forms (where passing a pl/sql table from client side is not
1113 --   possible and other modules where only a single document is processed
1114 --   at a time
1115 --  2. Returns all expected errors from etax API's in global tax error record
1116 --End of Comments
1117 -----------------------------------------------------------------------------
1118 PROCEDURE calculate_tax(p_po_header_id        IN          NUMBER,
1119                         p_po_release_id       IN          NUMBER,
1120                         p_calling_program     IN          VARCHAR2,
1121                         x_return_status       OUT NOCOPY  VARCHAR2
1122 ) IS
1123   l_module_name CONSTANT VARCHAR2(100) := 'CALCULATE_TAX_WRAPPER';
1124   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
1125                                             D_PACKAGE_BASE, l_module_name);
1126   d_progress NUMBER;
1127   l_po_header_id_tbl  PO_TBL_NUMBER;
1128   l_po_release_id_tbl PO_TBL_NUMBER;
1129 
1130   l_is_clm_po VARCHAR2(1);             -- Bug 9718704
1131   l_type_lookup_code VARCHAR2(50) ; --Bug 13798221
1132 
1133 BEGIN
1134 
1135   IF (PO_LOG.d_proc) THEN
1136     PO_LOG.proc_begin(d_module_base);
1137     PO_LOG.proc_begin(d_module_base, 'p_po_header_id', p_po_header_id);
1138     PO_LOG.proc_begin(d_module_base, 'p_po_release_id', p_po_release_id);
1139     PO_LOG.proc_begin(d_module_base, 'p_calling_program', p_calling_program);
1140   END IF;
1141 
1142   d_progress := 0;
1143 
1144   -- Bug 9718704 Start: Tax should not be calculated for CLM PO.
1145   l_is_clm_po := po_clm_intg_grp.is_clm_po( p_po_header_id        => p_po_header_id,
1146                                             p_po_line_id          => NULL,
1147                                             p_po_line_location_id => NULL,
1148                                             p_po_distribution_id  => NULL);
1149 
1150   IF PO_LOG.d_stmt THEN
1151       PO_LOG.stmt(d_module_base,d_progress,'Tax will not be calculated for CLM Order. l_is_clm_po = ' || l_is_clm_po );
1152   END IF;
1153 
1154   IF Nvl(l_is_clm_po, 'N') = 'Y' THEN
1155     x_return_status := FND_API.G_RET_STS_SUCCESS;
1156     -- Return SUCCESS for CLM PO.
1157     RETURN ;
1158   END IF;
1159   -- Bug 9718704 End.
1160 
1161   --Bug 13798221   - Skip tax calculation for BPA
1162   --Bug 13848107 - As part of this fix, the below code was giving No Data Found exception as  p_po_header_id
1163   -- is null in case of 'RELEASE'. So added if check.
1164   IF (p_po_header_id <> NULL) THEN
1165     SELECT type_lookup_code
1166     INTO   l_type_lookup_code
1167     FROM   po_headers_all
1168     WHERE  po_header_id = p_po_header_id;
1169 
1170     IF PO_LOG.d_stmt THEN
1171         PO_LOG.stmt(d_module_base,d_progress,'Tax will not be calculated for blanket purchase  = ' || l_type_lookup_code );
1172     END IF;
1173 
1174     IF (l_type_lookup_code = 'BLANKET') THEN
1175       x_return_status := FND_API.G_RET_STS_SUCCESS;
1176       -- Return SUCCESS for Blanket PA .
1177       RETURN ;
1178     END IF ;
1179   END IF ;
1180 
1181 --Bug 13798221
1182 
1183   IF (p_po_header_id IS NULL) THEN
1184     l_po_header_id_tbl := PO_TBL_NUMBER();
1185   ELSE
1186     l_po_header_id_tbl := PO_TBL_NUMBER(p_po_header_id);
1187   END IF;
1188 
1189   IF (p_po_release_id IS NULL) THEN
1190     l_po_release_id_tbl := PO_TBL_NUMBER();
1191   ELSE
1192     l_po_release_id_tbl := PO_TBL_NUMBER(p_po_release_id);
1193   END IF;
1194 
1195   calculate_tax(p_po_header_id_tbl    => l_po_header_id_tbl,
1196                 p_po_release_id_tbl   => l_po_release_id_tbl,
1197                 p_calling_program     => p_calling_program,
1198                 x_return_status       => x_return_status
1199   );
1200 
1201   d_progress := 10;
1202   IF PO_LOG.d_stmt THEN
1203     PO_LOG.stmt(d_module_base,d_progress,'PO calculate_tax returned with status '||x_return_status);
1204   END IF;
1205 
1206   IF (PO_LOG.d_proc) THEN
1207     PO_LOG.proc_end(d_module_base);
1208     PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
1209   END IF;
1210 
1211   d_progress := 20;
1212 EXCEPTION
1213   WHEN OTHERS THEN
1214     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1215 
1216     -- Initialize to flush out expected errors if any
1217     initialize_global_error_record();
1218     -- Add a new error
1219     append_error(p_error_level => null,
1220                  p_document_type_code => null,
1221                  p_document_id => null,
1222                  p_document_num => null,
1223                  p_line_id => null,
1224                  p_line_num => null,
1225                  p_line_location_id => null,
1226                  p_shipment_num => null,
1227                  p_distribution_id => null,
1228                  p_distribution_num => null,
1229                  p_message_text => d_module_base||'#'||d_progress||':'
1230                                   ||SQLCODE || SQLERRM);
1231 
1232     IF (PO_LOG.d_exc) THEN
1233       PO_LOG.exc(d_module_base, d_progress, SQLCODE || SQLERRM);
1234       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
1235     END IF;
1236 END calculate_tax;
1237 
1238 
1239 -----------------------------------------------------------------------------
1240 --Start of Comments
1241 --Name: calculate_tax_requisition
1242 --Pre-reqs:
1243 --  Should be called when transaction data has been posted to the database
1244 --  but not yet committed
1245 --Modifies:
1246 --  PO_REQUISITION_LINES_ALL.tax_attribute_update_code
1247 --  PO_REQUISITION_LINES_ALL.original_shipment_id
1248 --  PO_REQ_DISTRIBUTIONS_ALL.nonrecoverable_tax
1249 --  PO_REQ_DISTRIBUTIONS_ALL.recoverable_tax
1250 --Locks:
1251 --  Transaction tables if update is allowed
1252 --Function:
1253 --  Calculate tax amounts for the requisition passed in
1254 --Parameters:
1255 --IN:
1256 --p_requisition_header_id
1257 --  requisition_header_id for which tax is to be calculated
1258 --p_calling_program
1259 --  Identifies the module that calls this procedure eg. 'REQIMPORT'
1260 --OUT:
1261 --x_return_status
1262 --  Standard API specification parameter
1263 --  Can hold one of the following values:
1264 --    FND_API.G_RET_STS_SUCCESS (='S')
1265 --    FND_API.G_RET_STS_ERROR (='E')
1266 --    FND_API.G_RET_STS_UNEXP_ERROR (='U')
1267 --Notes:
1268 --  1. Calls out to EBTax APIs calculate_tax and determine recovery to
1269 --   calculaterecoverable_tax and nonrecoverable_tax
1270 --  2. Returns all expected errors from etax API's in global tax error record
1271 --End of Comments
1272 -----------------------------------------------------------------------------
1273 PROCEDURE calculate_tax_requisition(p_requisition_header_id  IN     NUMBER,
1274                                     p_calling_program        IN     VARCHAR2,
1275                                     x_return_status     OUT NOCOPY  VARCHAR2
1276 ) IS
1277   l_module_name CONSTANT VARCHAR2(100) := 'CALCULATE_TAX_REQUISITION';
1278   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
1279                                             D_PACKAGE_BASE, l_module_name);
1280   d_progress NUMBER;
1281   l_count NUMBER;
1282   l_line_count NUMBER;
1283   l_msg_count NUMBER;
1284   l_msg_data VARCHAR2(1000);
1285   l_trx_rec ZX_API_PUB.transaction_rec_type;
1286   l_org_id PO_HEADERS_ALL.org_id%TYPE;
1287   l_return_status VARCHAR2(1);
1288 
1289   l_is_clm_req VARCHAR2(1);        -- Bug 9718704
1290 
1291 BEGIN
1292 
1293   SAVEPOINT calculate_tax_req_savepoint;
1294 
1295   IF (PO_LOG.d_proc) THEN
1296     PO_LOG.proc_begin(d_module_base);
1297     PO_LOG.proc_begin(d_module_base, 'p_requisition_header_id', p_requisition_header_id);
1298     PO_LOG.proc_begin(d_module_base, 'p_calling_program', p_calling_program);
1299   END IF;
1300 
1301   d_progress := 0;
1302 
1303   -- By default return status is SUCCESS if no exception occurs
1304   x_return_status := FND_API.G_RET_STS_SUCCESS;
1305 
1306   -- Bug 9718704 Start: Tax should not be calculated for CLM Req.
1307   l_is_clm_req := PO_CLM_INTG_GRP.is_clm_document( p_doc_type => 'REQUISITION',
1308                                                    p_document_id => p_requisition_header_id );
1309   IF PO_LOG.d_stmt THEN
1310      PO_LOG.stmt(d_module_base,d_progress,'Tax will not be calculated for CLM Req. l_is_clm_req = ' || l_is_clm_req );
1311   END IF;
1312 
1313   IF Nvl(l_is_clm_req, 'N') = 'Y' THEN
1314      RETURN ;                 -- SUCCESS for CLM Req. x_return_status is already set to SUCCESS.
1315   END IF;
1316   -- Bug 9718704 End.
1317 
1318   --<Bug 5242803> Skip all processing if requisition_header_id is null
1319   IF p_requisition_header_id IS NOT NULL THEN
1320     -- Set global error record to uninitialized local record to set all its
1321     -- components to null
1322     G_TAX_ERRORS_TBL := null;
1323     d_progress := 10;
1324     initialize_zx_gt_tables(); -- Wipe ZX GT tables clean
1325     d_progress := 20;
1326     initialize_global_error_record();
1327 
1328     IF PO_LOG.d_stmt THEN
1329       PO_LOG.stmt(d_module_base,d_progress,'initial values of tax_attribute_update_codes');
1330       log_req_tauc(d_module_base,d_progress,p_requisition_header_id);
1331     END IF;
1332 
1333     -- Populate ZX headers and lines GT tables with transaction data
1334     d_progress := 30;
1335     populate_zx_headers_with_req(p_requisition_header_id, p_calling_program);
1336 
1337     d_progress := 40;
1338     populate_zx_lines_with_req(p_requisition_header_id, p_calling_program);
1339 
1340     d_progress := 50;
1341     -- Check if zx lines gt table is empty
1342     BEGIN
1343       SELECT COUNT(1) INTO l_line_count FROM zx_transaction_lines_gt;
1344     EXCEPTION WHEN OTHERS THEN
1345       l_line_count := 0;
1346       IF PO_LOG.d_stmt THEN
1347         PO_LOG.stmt(d_module_base,d_progress,'Exception while selecting from zx_transaction_lines_gt');
1348       END IF;
1349     END;
1350 
1351     d_progress := 60;
1352     IF PO_LOG.d_stmt THEN
1353       PO_LOG.stmt(d_module_base,d_progress,'Number of rows in zx_transaction_lines_gt='||l_line_count);
1354     END IF;
1355 
1356     IF (l_line_count <> 0) THEN
1357       d_progress := 70;
1358       -- Log table parameters
1359       IF PO_LOG.d_stmt THEN
1360         PO_LOG.stmt(d_module_base,d_progress,'Table parameters before eTax default/redefault');
1361         log_header_tax_attributes(d_module_base,d_progress);
1362         log_line_tax_attributes(d_module_base,d_progress);
1363       END IF;
1364 
1365       -- Call the eTax defaulting/redefaulting APIs which populate the
1366       --  Additional Tax Attributes back into the ZX GT tables
1367       IF (p_calling_program = 'REQIMPORT') THEN
1368         d_progress := 80;
1369         -- For Req Import, eTax needs to do extra validations since data is
1370         -- entered by user in interface tables. So call
1371         -- validate_and_default_tax_attribs
1372         ZX_API_PUB.validate_and_default_tax_attr(
1373           p_api_version           =>  1.0,
1374           p_init_msg_list         =>  FND_API.G_TRUE,
1375           p_commit                =>  FND_API.G_FALSE,
1376           p_validation_level      =>  FND_API.G_VALID_LEVEL_FULL,
1377           x_return_status         =>  l_return_status,
1378           x_msg_count             =>  l_msg_count,
1379           x_msg_data              =>  l_msg_data
1380         );
1381 
1382         d_progress := 90;
1383         IF PO_LOG.d_stmt THEN
1384           PO_LOG.stmt(d_module_base,d_progress,'validate_and_default_tax_attr returned with status '||l_return_status);
1385         END IF;
1386 
1387         -- Raise if any unexpected error
1388         IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
1389           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1390         END IF;
1391 
1392         -- If there are any expected errors then set the return status
1393         -- accordingly and copy over errors into global tax errors record
1394         IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
1395           d_progress := 100;
1396           -- Read validation errors from zx_validation_errors_gt into the global
1397           -- error record defined in the spec
1398           SELECT
1399             'VALIDATE' --error_level,
1400             ,PO_CONSTANTS_SV.REQUISITION --document_type_code,
1401             ,p_requisition_header_id --document_id,
1402             ,prh.segment1 --document_num,
1403             ,zxvegt.trx_line_id --line_id,
1404             ,prl.line_num --line_num,
1405             ,null --line_location_id,
1406             ,null --shipment_num,
1407             ,null --distribution_id,
1408             ,null --distribution_num,
1409             ,zxvegt.message_text --message_text
1410           BULK COLLECT INTO G_TAX_ERRORS_TBL
1411           FROM zx_validation_errors_gt zxvegt, po_requisition_headers_all prh,
1412                po_requisition_lines_all prl
1413           WHERE zxvegt.trx_id = prh.requisition_header_id
1414           AND zxvegt.trx_line_id = prl.requisition_line_id(+);
1415 
1416           d_progress := 110;
1417           BEGIN
1418             SELECT COUNT(1) INTO l_count FROM zx_validation_errors_gt;
1419           EXCEPTION WHEN OTHERS THEN
1420             IF PO_LOG.d_stmt THEN
1421               PO_LOG.stmt(d_module_base,d_progress,'Exception while hitting zx_validation_errors_gt');
1422             END IF;
1423             l_count := 0;
1424           END;
1425 
1426           IF PO_LOG.d_stmt THEN
1427             PO_LOG.stmt(d_module_base,d_progress,'Number of records in zx_validation_errors_gt '||l_count);
1428             PO_LOG.stmt(d_module_base,d_progress,'Number of error records collected '||G_TAX_ERRORS_TBL.error_level.COUNT);
1429           END IF;
1430 
1431           d_progress := 120;
1432           RAISE FND_API.G_EXC_ERROR;
1433         END IF;
1434 
1435       ELSE
1436         d_progress := 130;
1437         ZX_API_PUB.get_default_tax_det_attribs(
1438           p_api_version           =>  1.0,
1439           p_init_msg_list         =>  FND_API.G_TRUE,
1440           p_commit                =>  FND_API.G_FALSE,
1441           p_validation_level      =>  FND_API.G_VALID_LEVEL_FULL,
1442           x_return_status         =>  l_return_status,
1443           x_msg_count             =>  l_msg_count,
1444           x_msg_data              =>  l_msg_data
1445         );
1446 
1447         d_progress := 140;
1448         IF PO_LOG.d_stmt THEN
1449           PO_LOG.stmt(d_module_base,d_progress,'get_default_tax_det_attribs returned with status '||l_return_status);
1450         END IF;
1451 
1452         -- This API cannot give expected errors. However raise unexpected errors
1453         IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
1454           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1455         END IF;
1456 
1457       END IF; -- IF (p_calling_program = 'REQIMPORT')
1458 
1459       d_progress := 150;
1460       -- Override product_type Additional Tax Attribute if not already populated
1461       -- by eTax defaulting/redefaulting API
1462       UPDATE zx_transaction_lines_gt zxlgt
1463       SET zxlgt.product_type =
1464         (SELECT DECODE(prl.purchase_basis,
1465                        'GOODS', 'GOODS',
1466                        'SERVICES')
1467          FROM po_requisition_lines_all prl
1468          WHERE prl.requisition_line_id = zxlgt.trx_line_id)
1469       WHERE zxlgt.product_type IS NULL
1470       AND zxlgt.line_level_action = 'CREATE';
1471 
1472       d_progress := 160;
1473 
1474       -- Log table parameters
1475       IF PO_LOG.d_stmt THEN
1476         PO_LOG.stmt(d_module_base,d_progress,'Table parameters before calculate_tax');
1477         log_header_tax_attributes(d_module_base,d_progress);
1478         log_line_tax_attributes(d_module_base,d_progress);
1479       END IF;
1480 
1481       ZX_API_PUB.calculate_tax(
1482         p_api_version           =>  1.0,
1483         p_init_msg_list         =>  FND_API.G_FALSE,
1484         p_commit                =>  FND_API.G_FALSE,
1485         p_validation_level      =>  FND_API.G_VALID_LEVEL_FULL,
1486         x_return_status         =>  l_return_status,
1487         x_msg_count             =>  l_msg_count,
1488         x_msg_data              =>  l_msg_data
1489       );
1490 
1491       d_progress := 170;
1492       IF PO_LOG.d_stmt THEN
1493         PO_LOG.stmt(d_module_base,d_progress,'calculate_tax returned with status '||l_return_status);
1494       END IF;
1495 
1496 
1497       -- Raise if any unexpected error
1498       IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
1499         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1500       END IF;
1501 
1502       -- If there are any expected errors then set the return status accordingly
1503       -- and copy over errors into global tax errors record
1504       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
1505         x_return_status := l_return_status;
1506         d_progress := 180;
1507         -- Read validation errors from zx_errors_gt into the global
1508         -- error record defined in the spec
1509         SELECT
1510           'CALCULATE_TAX' --error_level,
1511           ,PO_CONSTANTS_SV.REQUISITION --document_type_code,
1512           ,p_requisition_header_id --document_id,
1513           ,prh.segment1 --document_num,
1514           ,zxegt.trx_line_id --line_id,
1515           ,prl.line_num --line_num,
1516           ,null --line_location_id,
1517           ,null --shipment_num,
1518           ,null --distribution_id,
1519           ,null --distribution_num,
1520           ,zxegt.message_text --message_text
1521         BULK COLLECT INTO G_TAX_ERRORS_TBL
1522         FROM zx_errors_gt zxegt, po_requisition_headers_all prh,
1523              po_requisition_lines_all prl
1524         WHERE zxegt.trx_id = prh.requisition_header_id
1525         AND zxegt.trx_line_id = prl.requisition_line_id(+);
1526 
1527         BEGIN
1528           SELECT COUNT(1) INTO l_count FROM zx_errors_gt;
1529         EXCEPTION WHEN OTHERS THEN
1530           IF PO_LOG.d_stmt THEN
1531             PO_LOG.stmt(d_module_base,d_progress,'Exception while hitting zx_errors_gt');
1532           END IF;
1533           l_count := 0;
1534         END;
1535 
1536         IF PO_LOG.d_stmt THEN
1537           PO_LOG.stmt(d_module_base,d_progress,'Number of records in zx_errors_gt '||l_count);
1538           PO_LOG.stmt(d_module_base,d_progress,'Number of error records collected '||G_TAX_ERRORS_TBL.error_level.COUNT);
1539         END IF;
1540 
1541         d_progress := 190;
1542         RAISE FND_API.G_EXC_ERROR;
1543       END IF;
1544 
1545     END IF; -- IF (l_line_count <> 0)
1546 
1547     d_progress := 200;
1548     -- Populate ZX distributions GT table with transaction distribution data
1549     populate_zx_dists_with_req(p_requisition_header_id, p_calling_program);
1550 
1551     d_progress := 210;
1552     -- Call eTax API determine_recovery if zx distributions gt is not empty
1553     BEGIN
1554       SELECT COUNT(1) INTO l_count FROM zx_itm_distributions_gt;
1555     EXCEPTION WHEN OTHERS THEN
1556       l_count := 0;
1557       IF PO_LOG.d_stmt THEN
1558         PO_LOG.stmt(d_module_base,d_progress,'Exception while selecting from zx_itm_distributions_gt');
1559       END IF;
1560     END;
1561 
1562     d_progress := 220;
1563     IF PO_LOG.d_stmt THEN
1564       PO_LOG.stmt(d_module_base,d_progress,'Number of rows in zx_itm_distributions_gt='||l_count);
1565     END IF;
1566 
1567     IF (l_count <> 0) THEN
1568       d_progress := 230;
1569       -- Update event_type_code on zx headers as required by determine_recovery API
1570       UPDATE zx_trx_headers_gt
1571       SET event_type_code = PO_CONSTANTS_SV.REQ_DISTRIBUTED;
1572 
1573       d_progress := 240;
1574 
1575       -- Log table parameters
1576       IF PO_LOG.d_stmt THEN
1577         PO_LOG.stmt(d_module_base,d_progress,'Table parameters before determine_recovery');
1578         log_header_tax_attributes(d_module_base,d_progress);
1579         log_line_tax_attributes(d_module_base,d_progress);
1580         log_dist_tax_attributes(d_module_base,d_progress);
1581       END IF;
1582 
1583       ZX_API_PUB.determine_recovery(
1584         p_api_version           =>  1.0,
1585         p_init_msg_list         =>  FND_API.G_FALSE,
1586         p_commit                =>  FND_API.G_FALSE,
1587         p_validation_level      =>  FND_API.G_VALID_LEVEL_FULL,
1588         x_return_status         =>  l_return_status,
1589         x_msg_count             =>  l_msg_count,
1590         x_msg_data              =>  l_msg_data
1591       );
1592 
1593       d_progress := 250;
1594       IF PO_LOG.d_stmt THEN
1595         PO_LOG.stmt(d_module_base,d_progress,'determine_recovery returned with status '||l_return_status);
1596       END IF;
1597 
1598       -- Raise if any unexpected error
1599       IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
1600         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1601       END IF;
1602 
1603       -- If there are any expected errors then set the return status accordingly
1604       -- and copy over errors into global tax errors record
1605       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
1606         x_return_status := l_return_status;
1607         -- Read validation errors from zx_errors_gt into the global
1608         -- error record defined in the spec
1609         d_progress := 260;
1610         SELECT
1611           'DETERMINE_RECOVERY' --error_level,
1612           ,PO_CONSTANTS_SV.REQUISITION --document_type_code,
1613           ,p_requisition_header_id --document_id,
1614           ,prh.segment1 --document_num,
1615           ,zxegt.trx_line_id --line_id,
1616           ,prl.line_num --line_num,
1617           ,null --line_location_id,
1618           ,null --shipment_num,
1619           ,zxegt.trx_line_dist_id --distribution_id,
1620           ,prd.distribution_id --distribution_num,
1621           ,zxegt.message_text --message_text
1622         BULK COLLECT INTO G_TAX_ERRORS_TBL
1623         FROM zx_errors_gt zxegt, po_requisition_headers_all prh,
1624              po_requisition_lines_all prl, po_req_distributions_all prd
1625         WHERE zxegt.trx_id = prh.requisition_header_id
1626         AND zxegt.trx_line_id = prl.requisition_line_id(+)
1627         AND zxegt.trx_line_dist_id = prd.distribution_id(+);
1628 
1629         d_progress := 270;
1630         BEGIN
1631           SELECT COUNT(1) INTO l_count FROM zx_errors_gt;
1632         EXCEPTION WHEN OTHERS THEN
1633           IF PO_LOG.d_stmt THEN
1634             PO_LOG.stmt(d_module_base,d_progress,'Exception while hitting zx_errors_gt');
1635           END IF;
1636           l_count := 0;
1637         END;
1638 
1639         IF PO_LOG.d_stmt THEN
1640           PO_LOG.stmt(d_module_base,d_progress,'Number of records in zx_errors_gt '||l_count);
1641           PO_LOG.stmt(d_module_base,d_progress,'Number of error records collected '||G_TAX_ERRORS_TBL.error_level.COUNT);
1642         END IF;
1643 
1644         d_progress := 280;
1645         RAISE FND_API.G_EXC_ERROR;
1646       END IF;
1647 
1648       d_progress := 290;
1649       -- Populate recoverable/nonrecoverable tax columns in distributions table
1650       UPDATE po_req_distributions_all prd
1651       SET prd.recoverable_tax =
1652             (SELECT SUM(zxdist.rec_nrec_tax_amt)
1653              FROM zx_rec_nrec_dist_gt zxdist
1654              WHERE zxdist.trx_line_dist_id = prd.distribution_id
1655              AND zxdist.recoverable_flag = 'Y'
1656              AND zxdist.application_id = PO_CONSTANTS_SV.APPLICATION_ID
1657              AND zxdist.entity_code = PO_CONSTANTS_SV.REQ_ENTITY_CODE
1658              AND zxdist.event_class_code = PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE)
1659          ,prd.nonrecoverable_tax =
1660             (SELECT SUM(zxdist.rec_nrec_tax_amt)
1661              FROM zx_rec_nrec_dist_gt zxdist
1662              WHERE zxdist.trx_line_dist_id = prd.distribution_id
1663              AND zxdist.recoverable_flag = 'N'
1664              AND zxdist.application_id = PO_CONSTANTS_SV.APPLICATION_ID
1665              AND zxdist.entity_code = PO_CONSTANTS_SV.REQ_ENTITY_CODE
1666              AND zxdist.event_class_code = PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE)
1667       WHERE prd.distribution_id IN
1668         (SELECT trx_line_dist_id FROM zx_itm_distributions_gt);
1669 
1670     END IF; -- IF (l_count <> 0)
1671 
1672     -- If any lines were processed then update eTax repository with the line
1673     -- determining factors
1674     IF (l_line_count <> 0) THEN
1675       d_progress := 300;
1676       SELECT prh.org_id
1677       INTO l_org_id
1678       FROM po_requisition_headers_all prh
1679       WHERE prh.requisition_header_id=p_requisition_header_id;
1680 
1681       IF PO_LOG.d_stmt THEN
1682         PO_LOG.stmt(d_module_base,d_progress,'org_id = '||l_org_id);
1683       END IF;
1684 
1685       d_progress := 310;
1686       -- Call global_document_update API to delete all data for this requistion
1687       l_trx_rec.internal_organization_id := l_org_id;
1688       l_trx_rec.application_id           := PO_CONSTANTS_SV.APPLICATION_ID;
1689       l_trx_rec.entity_code              := PO_CONSTANTS_SV.REQ_ENTITY_CODE;
1690       l_trx_rec.event_class_code         := PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE;
1691       l_trx_rec.event_type_code          := PO_CONSTANTS_SV.REQ_DELETED;
1692       l_trx_rec.trx_id                   := p_requisition_header_id;
1693       l_trx_rec.application_doc_status   := null;
1694 
1695       d_progress := 320;
1696       ZX_API_PUB.global_document_update(
1697         p_api_version         =>  1.0,
1698         p_init_msg_list       =>  FND_API.G_FALSE,
1699         p_commit              =>  FND_API.G_FALSE,
1700         p_validation_level    =>  FND_API.G_VALID_LEVEL_FULL,
1701         x_return_status       =>  l_return_status,
1702         x_msg_count           =>  l_msg_count,
1703         x_msg_data            =>  l_msg_data,
1704         p_transaction_rec     =>  l_trx_rec);
1705 
1706       -- Raise if any unexpected error
1707       IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
1708         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1709       END IF;
1710 
1711       d_progress := 330;
1712       -- Populate zx record TRX_LINE_DIST_TBL
1713       populate_zx_record(p_requisition_header_id);
1714       d_progress := 340;
1715       -- Populate the tax determining attributes in eTax repository if there were
1716       -- no errors in processing the document. Control will reach here only if
1717       -- all l_return_status until now have been successful
1718       ZX_API_PUB.insert_line_det_factors(
1719         p_api_version         =>  1.0,
1720         p_init_msg_list       =>  FND_API.G_FALSE,
1721         p_commit              =>  FND_API.G_FALSE,
1722         p_validation_level    =>  FND_API.G_VALID_LEVEL_FULL,
1723         x_return_status       =>  l_return_status,
1724         x_msg_count           =>  l_msg_count,
1725         x_msg_data            =>  l_msg_data);
1726 
1727       d_progress := 350;
1728       IF PO_LOG.d_stmt THEN
1729         PO_LOG.stmt(d_module_base,d_progress,'insert_line_det_factors returned with status '||l_return_status);
1730       END IF;
1731 
1732       -- Raise if any unexpected error
1733       IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
1734         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1735       END IF;
1736 
1737     END IF; --IF (l_line_count <> 0)
1738 
1739     d_progress := 360;
1740     -- Null out tax_attribute_update_code columns
1741     update po_requisition_headers_all
1742     set tax_attribute_update_code = null
1743     where requisition_header_id = p_requisition_header_id;
1744 
1745     d_progress := 370;
1746     update po_requisition_lines_all
1747     set tax_attribute_update_code = null
1748     where requisition_header_id = p_requisition_header_id;
1749 
1750     -- Bug 5363122. Wipe out ZX GT tables at the end of tax call
1751     wipe_zx_gt_tables();
1752 
1753   END IF; -- IF p_requisition_header_id IS NOT NULL
1754 
1755   IF (PO_LOG.d_proc) THEN
1756     PO_LOG.proc_end(d_module_base);
1757     PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
1758   END IF;
1759   d_progress := 380;
1760 
1761 EXCEPTION
1762   WHEN FND_API.G_EXC_ERROR THEN
1763     x_return_status := FND_API.G_RET_STS_ERROR ;
1764     ROLLBACK TO SAVEPOINT calculate_tax_req_savepoint;
1765 
1766     IF (PO_LOG.d_exc) THEN
1767       PO_LOG.exc(d_module_base,d_progress,'Number of error records collected '||G_TAX_ERRORS_TBL.error_level.COUNT);
1768       log_global_error_record(d_module_base, d_progress);
1769       PO_LOG.exc(d_module_base, d_progress, null);
1770       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
1771     END IF;
1772 
1773   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1774     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1775     ROLLBACK TO SAVEPOINT calculate_tax_req_savepoint;
1776 
1777     -- Initialize to flush out expected errors if any
1778     initialize_global_error_record();
1779     -- Add a new error
1780     append_error(p_error_level => null,
1781                  p_document_type_code => null,
1782                  p_document_id => null,
1783                  p_document_num => null,
1784                  p_line_id => null,
1785                  p_line_num => null,
1786                  p_line_location_id => null,
1787                  p_shipment_num => null,
1788                  p_distribution_id => null,
1789                  p_distribution_num => null,
1790                  p_message_text => l_msg_data);
1791 
1792     IF (PO_LOG.d_exc) THEN
1793       PO_LOG.exc(d_module_base, d_progress, l_msg_data);
1794       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
1795     END IF;
1796 
1797   WHEN OTHERS THEN
1798     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1799     ROLLBACK TO SAVEPOINT calculate_tax_req_savepoint;
1800 
1801     -- Initialize to flush out expected errors if any
1802     initialize_global_error_record();
1803     -- Add a new error
1804     append_error(p_error_level => null,
1805                  p_document_type_code => null,
1806                  p_document_id => null,
1807                  p_document_num => null,
1808                  p_line_id => null,
1809                  p_line_num => null,
1810                  p_line_location_id => null,
1811                  p_shipment_num => null,
1812                  p_distribution_id => null,
1813                  p_distribution_num => null,
1814                  p_message_text => d_module_base||'#'||d_progress||':'
1815                                   ||SQLCODE || SQLERRM);
1816 
1817     IF (PO_LOG.d_exc) THEN
1818       PO_LOG.exc(d_module_base, d_progress, SQLCODE || SQLERRM);
1819       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
1820     END IF;
1821 END calculate_tax_requisition;
1822 
1823 
1824 
1825 -----------------------------------------------------------------------------
1826 --Start of Comments
1827 --Name: determine_recovery_po
1828 --Pre-reqs:
1829 --  None
1830 --Modifies:
1831 --  PO_DISTRIBUTIONS_ALL.nonrecoverable_tax
1832 --  PO_DISTRIBUTIONS_ALL.recoverable_tax
1833 --Locks:
1834 --  Transaction tables if update is allowed
1835 --Function:
1836 --  Distribute tax lines and calculate recoverable and nonrecoverable tax
1837 --  amounts
1838 --Parameters:
1839 --IN:
1840 --p_po_header_id
1841 --  po_header_id for which tax is to be distributed
1842 --OUT:
1843 --x_return_status
1844 --  Standard API specification parameter
1845 --  Can hold one of the following values:
1846 --    FND_API.G_RET_STS_SUCCESS (='S')
1847 --    FND_API.G_RET_STS_ERROR (='E')
1848 --    FND_API.G_RET_STS_UNEXP_ERROR (='U')
1849 --Notes:
1850 --  1. Calls out to EBTax API determine recovery to distribute tax lines
1851 --   Populates recoverable_tax and nonrecoverable_tax columns in distributions
1852 --   with the corrsponding tax amounts calculated.
1853 --  2. Returns all expected errors from etax API's in global tax error record
1854 --End of Comments
1855 -----------------------------------------------------------------------------
1856 PROCEDURE determine_recovery_po(p_po_header_id  IN         NUMBER,
1857                                 x_return_status OUT NOCOPY VARCHAR2
1858 ) IS
1859   l_module_name CONSTANT VARCHAR2(100) := 'DETERMINE_RECOVERY_PO';
1860   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
1861                                             D_PACKAGE_BASE, l_module_name);
1862   d_progress NUMBER;
1863   l_msg_count NUMBER;
1864   l_msg_data VARCHAR2(1000);
1865   l_count NUMBER;
1866   l_return_status VARCHAR2(1);
1867 BEGIN
1868   SAVEPOINT det_recovery_po_savepoint;
1869 
1870   IF (PO_LOG.d_proc) THEN
1871     PO_LOG.proc_begin(d_module_base);
1872     PO_LOG.proc_begin(d_module_base, 'p_po_header_id', p_po_header_id);
1873   END IF;
1874 
1875   d_progress := 0;
1876 
1877   -- By default return status is SUCCESS if no exception occurs
1878   x_return_status := FND_API.G_RET_STS_SUCCESS;
1879   -- Set global error record to uninitialized local record to set all its
1880   -- components to null
1881   G_TAX_ERRORS_TBL := null;
1882   initialize_zx_gt_tables(); -- Wipe ZX GT tables clean
1883   initialize_global_error_record();
1884 
1885   d_progress := 10;
1886   -- Populate ZX headers GT table with transaction header data
1887   populate_header_po(p_po_header_id);
1888   d_progress := 20;
1889   -- Populate ZX distributions GT table with transaction distribution data
1890   populate_all_dists_po(p_po_header_id);
1891 
1892   d_progress := 30;
1893   -- Call eTax API determine_recovery to distribute tax lines
1894   BEGIN
1895     SELECT COUNT(1) INTO l_count FROM zx_itm_distributions_gt;
1896   EXCEPTION WHEN OTHERS THEN
1897     l_count := 0;
1898   END;
1899 
1900   IF (l_count <> 0) THEN
1901     d_progress := 40;
1902 
1903     -- Log table parameters
1904     IF PO_LOG.d_stmt THEN
1905       PO_LOG.stmt(d_module_base,d_progress,'Table parameters before determine_recovery');
1906       log_header_tax_attributes(d_module_base,d_progress);
1907       log_dist_tax_attributes(d_module_base,d_progress);
1908     END IF;
1909 
1910     ZX_API_PUB.determine_recovery(
1911       p_api_version           =>  1.0,
1912       p_init_msg_list         =>  FND_API.G_TRUE,
1913       p_commit                =>  FND_API.G_FALSE,
1914       p_validation_level      =>  FND_API.G_VALID_LEVEL_FULL,
1915       x_return_status         =>  l_return_status,
1916       x_msg_count             =>  l_msg_count,
1917       x_msg_data              =>  l_msg_data
1918     );
1919 
1920     -- Raise if any unexpected error
1921     IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
1922       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1923     END IF;
1924 
1925     -- If expected errors, store error details in po_session_gt temporarily
1926     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
1927       x_return_status := l_return_status;
1928       -- Read validation errors from zx_errors_gt into the global
1929       -- error record defined in the spec
1930       SELECT
1931         'DETERMINE_RECOVERY' --error_level,
1932         ,PO_CONSTANTS_SV.PO --document_type_code,
1933         ,p_po_header_id --document_id,
1934         ,ph.segment1 --document_num,
1935         ,null --line_id,
1936         ,null --line_num,
1937         ,zxegt.trx_line_id --line_location_id,
1938         ,pll.shipment_num --shipment_num,
1939         ,zxegt.trx_line_dist_id --distribution_id,
1940         ,pd.distribution_num --distribution_num,
1941         ,zxegt.message_text --message_text
1942       BULK COLLECT INTO G_TAX_ERRORS_TBL
1943       FROM zx_errors_gt zxegt, po_headers_all ph,
1944            po_line_locations_all pll, po_distributions_all pd
1945       WHERE zxegt.trx_id = ph.po_header_id
1946       AND zxegt.trx_line_id = pll.line_location_id(+)
1947       AND zxegt.trx_line_dist_id = pd.po_distribution_id(+);
1948 
1949       BEGIN
1950         SELECT COUNT(1) INTO l_count FROM zx_errors_gt;
1951       EXCEPTION WHEN OTHERS THEN
1952         IF PO_LOG.d_stmt THEN
1953           PO_LOG.stmt(d_module_base,d_progress,'Exception while hitting zx_errors_gt');
1954         END IF;
1955         l_count := 0;
1956       END;
1957 
1958       IF PO_LOG.d_stmt THEN
1959         PO_LOG.stmt(d_module_base,d_progress,'Number of records in zx_errors_gt '||l_count);
1960         PO_LOG.stmt(d_module_base,d_progress,'Number of error records collected '||G_TAX_ERRORS_TBL.error_level.COUNT);
1961       END IF;
1962 
1963       RAISE FND_API.G_EXC_ERROR;
1964     END IF; --IF (l_return_status = FND_API.G_RET_STS_ERROR)
1965 
1966     d_progress := 50;
1967     -- Populate recoverable/nonrecoverable tax columns in distributions table
1968     UPDATE po_distributions_all pd
1969     SET pd.recoverable_tax =
1970           (SELECT SUM(zxdist.rec_nrec_tax_amt)
1971            FROM zx_rec_nrec_dist zxdist
1972            WHERE zxdist.trx_line_dist_id = pd.po_distribution_id
1973            AND zxdist.recoverable_flag = 'Y'
1974            AND zxdist.application_id = PO_CONSTANTS_SV.APPLICATION_ID
1975            AND zxdist.entity_code = PO_CONSTANTS_SV.PO_ENTITY_CODE
1976            AND zxdist.event_class_code = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE)
1977        ,pd.nonrecoverable_tax =
1978           (SELECT SUM(zxdist.rec_nrec_tax_amt)
1979            FROM zx_rec_nrec_dist zxdist
1980            WHERE zxdist.trx_line_dist_id = pd.po_distribution_id
1981            AND zxdist.recoverable_flag = 'N'
1982            AND zxdist.application_id = PO_CONSTANTS_SV.APPLICATION_ID
1983            AND zxdist.entity_code = PO_CONSTANTS_SV.PO_ENTITY_CODE
1984            AND zxdist.event_class_code = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE),
1985 		--Bug 10305728 start
1986         last_update_date = sysdate,
1987      	last_updated_by = fnd_global.user_id,
1988         last_update_login = fnd_global.login_id
1989         --Bug 10305728  end
1990     WHERE pd.po_distribution_id IN
1991       (SELECT trx_line_dist_id FROM zx_itm_distributions_gt);
1992 
1993       /*  Bug 6157632 Start */
1994       UPDATE po_line_locations_all pll
1995   SET pll.taxable_flag =
1996     DECODE((SELECT COUNT(1) FROM zx_lines zl
1997             WHERE zl.trx_line_id = pll.line_location_id
1998             AND zl.application_id = PO_CONSTANTS_SV.APPLICATION_ID
1999             AND ( (zl.entity_code = PO_CONSTANTS_SV.PO_ENTITY_CODE
2000                    AND zl.event_class_code = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE
2001                    AND zl.trx_id = pll.po_header_id)
2002                   OR
2003                   (zl.entity_code = PO_CONSTANTS_SV.REL_ENTITY_CODE
2004                    AND zl.event_class_code = PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE
2005                    AND zl.trx_id = pll.po_release_id))),
2006            0, 'N',
2007            'Y'
2008           ),
2009 	  --Bug 10305728 start
2010 	    tax_attribute_update_code = null,
2011         last_update_date = sysdate,
2012      	last_updated_by = fnd_global.user_id,
2013         last_update_login = fnd_global.login_id
2014     --Bug 10305728  end
2015   WHERE
2016     pll.line_location_id IN (SELECT line_location_id FROM po_distributions_all pd,zx_itm_distributions_gt zi WHERE pd.po_distribution_id=zi.trx_line_dist_id);
2017     /*  Bug 6157632 End */
2018   END IF;
2019 
2020   d_progress := 60;
2021   UPDATE po_distributions_all
2022   SET tax_attribute_update_code = null,
2023       --Bug 10305728 start
2024 	 last_update_date = sysdate,
2025      last_updated_by = fnd_global.user_id,
2026      last_update_login = fnd_global.login_id
2027     --Bug 10305728  end
2028   WHERE po_distribution_id IN (SELECT trx_line_dist_id
2029                                FROM zx_itm_distributions_gt);
2030 
2031   IF (PO_LOG.d_proc) THEN
2032     PO_LOG.proc_end(d_module_base);
2033     PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
2034   END IF;
2035   d_progress := 70;
2036 
2037 EXCEPTION
2038   WHEN FND_API.G_EXC_ERROR THEN
2039     x_return_status := FND_API.G_RET_STS_ERROR ;
2040     ROLLBACK TO SAVEPOINT det_recovery_po_savepoint;
2041 
2042     IF (PO_LOG.d_exc) THEN
2043       PO_LOG.exc(d_module_base, d_progress, null);
2044       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
2045     END IF;
2046 
2047   WHEN OTHERS THEN
2048     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2049     ROLLBACK TO SAVEPOINT det_recovery_po_savepoint;
2050 
2051     IF (PO_LOG.d_exc) THEN
2052       PO_LOG.exc(d_module_base, d_progress, SQLCODE || SQLERRM);
2053       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
2054     END IF;
2055 
2056 END determine_recovery_po;
2057 
2058 
2059 -----------------------------------------------------------------------------
2060 --Start of Comments
2061 --Name: determine_recovery_rel
2062 --Pre-reqs:
2063 --  None
2064 --Modifies:
2065 --  PO_DISTRIBUTIONS_ALL.nonrecoverable_tax
2066 --  PO_DISTRIBUTIONS_ALL.recoverable_tax
2067 --Locks:
2068 --  Transaction tables if update is allowed
2069 --Function:
2070 --  Distribute tax lines and calculate recoverable and nonrecoverable tax
2071 --  amounts
2072 --Parameters:
2073 --IN:
2074 --p_po_release_id
2075 --  po_release_id for which tax is to be distributed
2076 --OUT:
2077 --x_return_status
2078 --  Standard API specification parameter
2079 --  Can hold one of the following values:
2080 --    FND_API.G_RET_STS_SUCCESS (='S')
2081 --    FND_API.G_RET_STS_ERROR (='E')
2082 --    FND_API.G_RET_STS_UNEXP_ERROR (='U')
2083 --Notes:
2084 --  1. Calls out to EBTax API determine recovery to distribute tax lines
2085 --   Populates recoverable_tax and nonrecoverable_tax columns in distributions
2086 --   with the corrsponding tax amounts calculated.
2087 --  2. Returns all expected errors from etax API's in global tax error record
2088 --End of Comments
2089 -----------------------------------------------------------------------------
2090 PROCEDURE determine_recovery_rel(p_po_release_id  IN       NUMBER,
2091                                 x_return_status OUT NOCOPY VARCHAR2
2092 ) IS
2093   l_module_name CONSTANT VARCHAR2(100) := 'DETERMINE_RECOVERY_REL';
2094   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
2095                                             D_PACKAGE_BASE, l_module_name);
2096   d_progress NUMBER;
2097   l_msg_count NUMBER;
2098   l_msg_data VARCHAR2(1000);
2099   l_count NUMBER;
2100   l_return_status VARCHAR2(1);
2101 BEGIN
2102   SAVEPOINT det_recovery_rel_savepoint;
2103 
2104   IF (PO_LOG.d_proc) THEN
2105     PO_LOG.proc_begin(d_module_base);
2106     PO_LOG.proc_begin(d_module_base, 'p_po_release_id', p_po_release_id);
2107   END IF;
2108 
2109   d_progress := 0;
2110 
2111   -- By default return status is SUCCESS if no exception occurs
2112   x_return_status := FND_API.G_RET_STS_SUCCESS;
2113   -- Set global error record to uninitialized local record to set all its
2114   -- components to null
2115   G_TAX_ERRORS_TBL := null;
2116   initialize_zx_gt_tables(); -- Wipe ZX GT tables clean
2117   initialize_global_error_record();
2118 
2119   d_progress := 10;
2120   -- Populate ZX headers GT table with transaction header data
2121   populate_header_rel(p_po_release_id);
2122   d_progress := 20;
2123   -- Populate ZX distributions GT table with transaction distribution data
2124   populate_all_dists_rel(p_po_release_id);
2125 
2126   d_progress := 30;
2127   -- Call eTax API determine_recovery to distribute tax lines
2128   BEGIN
2129     SELECT COUNT(1) INTO l_count FROM zx_itm_distributions_gt;
2130   EXCEPTION WHEN OTHERS THEN
2131     l_count := 0;
2132   END;
2133 
2134   IF (l_count <> 0) THEN
2135     d_progress := 40;
2136 
2137     -- Log table parameters
2138     IF PO_LOG.d_stmt THEN
2139       PO_LOG.stmt(d_module_base,d_progress,'Table parameters before determine_recovery');
2140       log_header_tax_attributes(d_module_base,d_progress);
2141       log_dist_tax_attributes(d_module_base,d_progress);
2142     END IF;
2143 
2144     ZX_API_PUB.determine_recovery(
2145       p_api_version           =>  1.0,
2146       p_init_msg_list         =>  FND_API.G_TRUE,
2147       p_commit                =>  FND_API.G_FALSE,
2148       p_validation_level      =>  FND_API.G_VALID_LEVEL_FULL,
2149       x_return_status         =>  l_return_status,
2150       x_msg_count             =>  l_msg_count,
2151       x_msg_data              =>  l_msg_data
2152     );
2153 
2154     -- Raise if any unexpected error
2155     IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
2156       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2157     END IF;
2158 
2159     -- If expected errors, store error details in po_session_gt temporarily
2160     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
2161       x_return_status := l_return_status;
2162       -- Read validation errors from zx_errors_gt into the global
2163       -- error record defined in the spec
2164       SELECT
2165         'DETERMINE_RECOVERY' --error_level,
2166         ,PO_CONSTANTS_SV.PO --document_type_code,
2167         ,p_po_release_id --document_id,
2168         ,ph.segment1 --document_num,
2169         ,null --line_id,
2170         ,null --line_num,
2171         ,zxegt.trx_line_id --line_location_id,
2172         ,pll.shipment_num --shipment_num,
2173         ,zxegt.trx_line_dist_id --distribution_id,
2174         ,pd.distribution_num --distribution_num,
2175         ,zxegt.message_text --message_text
2176       BULK COLLECT INTO G_TAX_ERRORS_TBL
2177       FROM zx_errors_gt zxegt, po_releases_all pr, po_headers_all ph,
2178            po_line_locations_all pll, po_distributions_all pd
2179       WHERE zxegt.trx_id = pr.po_release_id
2180       AND pr.po_header_id = ph.po_header_id
2181       AND zxegt.trx_line_id = pll.line_location_id(+)
2182       AND zxegt.trx_line_dist_id = pd.po_distribution_id(+);
2183 
2184       BEGIN
2185         SELECT COUNT(1) INTO l_count FROM zx_errors_gt;
2186       EXCEPTION WHEN OTHERS THEN
2187         IF PO_LOG.d_stmt THEN
2188           PO_LOG.stmt(d_module_base,d_progress,'Exception while hitting zx_errors_gt');
2189         END IF;
2190         l_count := 0;
2191       END;
2192 
2193       IF PO_LOG.d_stmt THEN
2194         PO_LOG.stmt(d_module_base,d_progress,'Number of records in zx_errors_gt '||l_count);
2195         PO_LOG.stmt(d_module_base,d_progress,'Number of error records collected '||G_TAX_ERRORS_TBL.error_level.COUNT);
2196       END IF;
2197 
2198       RAISE FND_API.G_EXC_ERROR;
2199     END IF; --IF (l_return_status = FND_API.G_RET_STS_ERROR)
2200 
2201     d_progress := 50;
2202     -- Populate recoverable/nonrecoverable tax columns in distributions table
2203     UPDATE po_distributions_all pd
2204     SET pd.recoverable_tax =
2205           (SELECT SUM(zxdist.rec_nrec_tax_amt)
2206            FROM zx_rec_nrec_dist zxdist
2207            WHERE zxdist.trx_line_dist_id = pd.po_distribution_id
2208            AND zxdist.recoverable_flag = 'Y'
2209            AND zxdist.application_id = PO_CONSTANTS_SV.APPLICATION_ID
2210            AND zxdist.entity_code = PO_CONSTANTS_SV.REL_ENTITY_CODE
2211            AND zxdist.event_class_code = PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE)
2212        ,pd.nonrecoverable_tax =
2213           (SELECT SUM(zxdist.rec_nrec_tax_amt)
2214            FROM zx_rec_nrec_dist zxdist
2215            WHERE zxdist.trx_line_dist_id = pd.po_distribution_id
2216            AND zxdist.recoverable_flag = 'N'
2217            AND zxdist.application_id = PO_CONSTANTS_SV.APPLICATION_ID
2218            AND zxdist.entity_code = PO_CONSTANTS_SV.REL_ENTITY_CODE
2219            AND zxdist.event_class_code = PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE)
2220     WHERE pd.po_distribution_id IN
2221       (SELECT trx_line_dist_id FROM zx_itm_distributions_gt);
2222 
2223       /*  Bug 6157632 Start */
2224       UPDATE po_line_locations_all pll
2225   SET pll.taxable_flag =
2226     DECODE((SELECT COUNT(1) FROM zx_lines zl
2227             WHERE zl.trx_line_id = pll.line_location_id
2228             AND zl.application_id = PO_CONSTANTS_SV.APPLICATION_ID
2229             AND ( (zl.entity_code = PO_CONSTANTS_SV.PO_ENTITY_CODE
2230                    AND zl.event_class_code = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE
2231                    AND zl.trx_id = pll.po_header_id)
2232                   OR
2233                   (zl.entity_code = PO_CONSTANTS_SV.REL_ENTITY_CODE
2234                    AND zl.event_class_code = PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE
2235                    AND zl.trx_id = pll.po_release_id))),
2236            0, 'N',
2237            'Y'
2238           )
2239   WHERE
2240     pll.line_location_id IN (SELECT line_location_id FROM po_distributions_all pd,zx_itm_distributions_gt zi WHERE pd.po_distribution_id=zi.trx_line_dist_id);
2241     /*  Bug 6157632 End */
2242 
2243 
2244   END IF;
2245 
2246   d_progress := 60;
2247   UPDATE po_distributions_all
2248   SET tax_attribute_update_code = null
2249   WHERE po_distribution_id IN (SELECT trx_line_dist_id
2250                                FROM zx_itm_distributions_gt);
2251 
2252   IF (PO_LOG.d_proc) THEN
2253     PO_LOG.proc_end(d_module_base);
2254     PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
2255   END IF;
2256   d_progress := 70;
2257 
2258 EXCEPTION
2259   WHEN FND_API.G_EXC_ERROR THEN
2260     x_return_status := FND_API.G_RET_STS_ERROR ;
2261     ROLLBACK TO SAVEPOINT det_recovery_rel_savepoint;
2262 
2263     IF (PO_LOG.d_exc) THEN
2264       PO_LOG.exc(d_module_base, d_progress, null);
2265       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
2266     END IF;
2267 
2268   WHEN OTHERS THEN
2269     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2270     ROLLBACK TO SAVEPOINT det_recovery_rel_savepoint;
2271 
2272     IF (PO_LOG.d_exc) THEN
2273       PO_LOG.exc(d_module_base, d_progress, SQLCODE || SQLERRM);
2274       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
2275     END IF;
2276 
2277 END determine_recovery_rel;
2278 
2279 
2280 -----------------------------------------------------------------------------
2281 --Start of Comments
2282 --Name: determine_recovery_req
2283 --Pre-reqs:
2284 --  None
2285 --Modifies:
2286 --  PO_REQ_DISTRIBUTIONS_ALL.nonrecoverable_tax
2287 --  PO_REQ_DISTRIBUTIONS_ALL.recoverable_tax
2288 --Locks:
2289 --  Transaction tables if update is allowed
2290 --Function:
2291 --  Distributes tax lines and calculates_recoverable and nonrecoverable tax
2292 --  amounts
2293 --Parameters:
2294 --IN:
2295 --p_requisition_header_id
2296 --  requisition_header_id for which tax is to be distributed
2297 --OUT:
2298 --x_return_status
2299 --  Standard API specification parameter
2300 --  Can hold one of the following values:
2301 --    FND_API.G_RET_STS_SUCCESS (='S')
2302 --    FND_API.G_RET_STS_ERROR (='E')
2303 --    FND_API.G_RET_STS_UNEXP_ERROR (='U')
2304 --Notes:
2305 --  1. Calls out to EBTax API determine recovery to calculate recoverable_tax
2306 --   and nonrecoverable_tax
2307 --  2. Returns all expected errors from etax API in global tax error record
2308 --End of Comments
2309 -----------------------------------------------------------------------------
2310 PROCEDURE determine_recovery_req(p_requisition_header_id IN NUMBER,
2311                                  x_return_status OUT NOCOPY VARCHAR2
2312 ) IS
2313   l_module_name CONSTANT VARCHAR2(100) := 'DETERMINE_RECOVERY_REQ';
2314   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
2315                                             D_PACKAGE_BASE, l_module_name);
2316   d_progress NUMBER;
2317   l_count NUMBER;
2318   l_msg_count NUMBER;
2319   l_msg_data VARCHAR2(1000);
2320   l_return_status VARCHAR2(1);
2321 BEGIN
2322 
2323   SAVEPOINT det_recovery_req_savepoint;
2324 
2325   IF (PO_LOG.d_proc) THEN
2326     PO_LOG.proc_begin(d_module_base);
2327     PO_LOG.proc_begin(d_module_base, 'p_requisition_header_id', p_requisition_header_id);
2328   END IF;
2329 
2330   d_progress := 0;
2331 
2332   -- By default return status is SUCCESS if no exception occurs
2333   x_return_status := FND_API.G_RET_STS_SUCCESS;
2334   -- Set global error record to uninitialized local record to set all its
2335   -- components to null
2336   G_TAX_ERRORS_TBL := null;
2337   initialize_zx_gt_tables(); -- Wipe ZX GT tables clean
2338   initialize_global_error_record();
2339 
2340   d_progress := 10;
2341   -- Populate ZX headers GT table with transaction header data
2342   populate_zx_headers_with_req(p_requisition_header_id, 'DETERMINE_RECOVERY_REQ');
2343 
2344   d_progress := 20;
2345   -- Populate ZX distributions GT table with transaction distribution data
2346   populate_zx_dists_with_req(p_requisition_header_id, 'DETERMINE_RECOVERY_REQ');
2347 
2348   d_progress := 30;
2349   -- Call eTax API determine_recovery if zx distributions gt is not empty
2350   BEGIN
2351     SELECT COUNT(1) INTO l_count FROM zx_itm_distributions_gt;
2352   EXCEPTION WHEN OTHERS THEN
2353     l_count := 0;
2354   END;
2355 
2356   IF (l_count <> 0) THEN
2357     d_progress := 40;
2358 
2359     -- Log table parameters
2360     IF PO_LOG.d_stmt THEN
2361       PO_LOG.stmt(d_module_base,d_progress,'Table parameters before determine_recovery');
2362       log_header_tax_attributes(d_module_base,d_progress);
2363       log_dist_tax_attributes(d_module_base,d_progress);
2364     END IF;
2365 
2366     ZX_API_PUB.determine_recovery(
2367       p_api_version           =>  1.0,
2368       p_init_msg_list         =>  FND_API.G_FALSE,
2369       p_commit                =>  FND_API.G_FALSE,
2370       p_validation_level      =>  FND_API.G_VALID_LEVEL_FULL,
2371       x_return_status         =>  l_return_status,
2372       x_msg_count             =>  l_msg_count,
2373       x_msg_data              =>  l_msg_data
2374     );
2375 
2376     -- Raise if any unexpected error
2377     IF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
2378       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2379     END IF;
2380 
2381     -- If there are any expected errors then set the return status accordingly
2382     -- and copy over errors into global tax errors record
2383     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
2384       x_return_status := l_return_status;
2385       -- Read validation errors from zx_errors_gt into the global
2386       -- error record defined in the spec
2387       SELECT
2388         'DETERMINE_RECOVERY' --error_level,
2389         ,PO_CONSTANTS_SV.REQUISITION --document_type_code,
2390         ,p_requisition_header_id --document_id,
2391         ,prh.segment1 --document_num,
2392         ,zxegt.trx_line_id --line_id,
2393         ,prl.line_num --line_num,
2394         ,null --line_location_id,
2395         ,null --shipment_num,
2396         ,zxegt.trx_line_dist_id --distribution_id,
2397         ,prd.distribution_num --distribution_num,
2398         ,zxegt.message_text --message_text
2399       BULK COLLECT INTO G_TAX_ERRORS_TBL
2400       FROM zx_errors_gt zxegt, po_requisition_headers_all prh,
2401            po_requisition_lines_all prl, po_req_distributions_all prd
2402       WHERE zxegt.trx_id = prh.requisition_header_id
2403       AND zxegt.trx_line_id = prl.requisition_line_id(+)
2404       AND zxegt.trx_line_dist_id = prd.distribution_id(+);
2405 
2406       BEGIN
2407         SELECT COUNT(1) INTO l_count FROM zx_errors_gt;
2408       EXCEPTION WHEN OTHERS THEN
2409         IF PO_LOG.d_stmt THEN
2410           PO_LOG.stmt(d_module_base,d_progress,'Exception while hitting zx_errors_gt');
2411         END IF;
2412         l_count := 0;
2413       END;
2414 
2415       IF PO_LOG.d_stmt THEN
2416         PO_LOG.stmt(d_module_base,d_progress,'Number of records in zx_errors_gt '||l_count);
2417         PO_LOG.stmt(d_module_base,d_progress,'Number of error records collected '||G_TAX_ERRORS_TBL.error_level.COUNT);
2418       END IF;
2419 
2420       RAISE FND_API.G_EXC_ERROR;
2421     END IF;
2422 
2423     d_progress := 50;
2424     -- Populate recoverable/nonrecoverable tax columns in distributions table
2425     UPDATE po_req_distributions_all prd
2426     SET prd.recoverable_tax =
2427           (SELECT SUM(zxdist.rec_nrec_tax_amt)
2428            FROM zx_rec_nrec_dist_gt zxdist
2429            WHERE zxdist.trx_line_dist_id = prd.distribution_id
2430            AND zxdist.recoverable_flag = 'Y'
2431            AND zxdist.application_id = PO_CONSTANTS_SV.APPLICATION_ID
2432            AND zxdist.entity_code = PO_CONSTANTS_SV.REQ_ENTITY_CODE
2433            AND zxdist.event_class_code = PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE)
2434        ,prd.nonrecoverable_tax =
2435           (SELECT SUM(zxdist.rec_nrec_tax_amt)
2436            FROM zx_rec_nrec_dist_gt zxdist
2437            WHERE zxdist.trx_line_dist_id = prd.distribution_id
2438            AND zxdist.recoverable_flag = 'N'
2439            AND zxdist.application_id = PO_CONSTANTS_SV.APPLICATION_ID
2440            AND zxdist.entity_code = PO_CONSTANTS_SV.REQ_ENTITY_CODE
2441            AND zxdist.event_class_code = PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE)
2442     WHERE prd.distribution_id IN
2443       (SELECT trx_line_dist_id FROM zx_itm_distributions_gt);
2444 
2445   END IF; -- IF (l_count <> 0)
2446 
2447   IF (PO_LOG.d_proc) THEN
2448     PO_LOG.proc_end(d_module_base);
2449     PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
2450   END IF;
2451   d_progress := 60;
2452 
2453 EXCEPTION
2454   WHEN FND_API.G_EXC_ERROR THEN
2455     x_return_status := FND_API.G_RET_STS_ERROR ;
2456     ROLLBACK TO SAVEPOINT det_recovery_req_savepoint;
2457 
2458     IF (PO_LOG.d_exc) THEN
2459       PO_LOG.exc(d_module_base, d_progress, null);
2460       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
2461     END IF;
2462 
2463   WHEN OTHERS THEN
2464     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2465     ROLLBACK TO SAVEPOINT det_recovery_req_savepoint;
2466 
2467     IF (PO_LOG.d_exc) THEN
2468       PO_LOG.exc(d_module_base, d_progress, SQLCODE || SQLERRM);
2469       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
2470     END IF;
2471 END determine_recovery_req;
2472 
2473 
2474 -----------------------------------------------------------------------------
2475 --Start of Comments
2476 --Name: populate_zx_headers_with_po
2477 --Pre-reqs:
2478 --  None
2479 --Modifies:
2480 --  ZX_TRX_HEADERS_GT
2481 --Locks:
2482 --  ZX_TRX_HEADERS_GT
2483 --Function:
2484 --  Populate ZX_TRX_HEADERS_GT with transaction header data
2485 --Parameters:
2486 --IN:
2487 --p_po_header_id_tbl
2488 --  PL/SQL table with list of po_header_id's to process for tax_calculation
2489 --p_calling_program
2490 --  Identifies the module that calls this procedure eg. 'PDOI'
2491 --Notes:
2492 --  Used by calculate_tax procedure. Not to be used externally
2493 --End of Comments
2494 -----------------------------------------------------------------------------
2495 PROCEDURE populate_zx_headers_with_po(p_po_header_id_tbl  IN  PO_TBL_NUMBER,
2496                                       p_calling_program   IN  VARCHAR2
2497 ) IS
2498 BEGIN
2499 
2500   -- Populate zx_trx_headers_gt for POs
2501   FORALL i IN 1..p_po_header_id_tbl.COUNT
2502     INSERT INTO zx_trx_headers_gt(
2503       internal_organization_id
2504       ,application_id
2505       ,entity_code
2506       ,event_class_code
2507       ,event_type_code
2508       ,trx_id
2509       ,trx_date
2510       ,trx_doc_revision
2511       ,ledger_id
2512       ,trx_currency_code
2513       ,currency_conversion_date
2514       ,currency_conversion_rate
2515       ,currency_conversion_type
2516       ,minimum_accountable_unit --Bug 5474336. Pass mau to EBTax
2517       ,precision
2518       ,legal_entity_id
2519       ,rounding_ship_from_party_id
2520       ,default_taxation_country
2521       ,quote_flag
2522       ,trx_number
2523       ,trx_description
2524       ,trx_communicated_date
2525       ,document_sub_type
2526       ,provnl_tax_determination_date
2527       -- Bug 5025018. Updated tax attribute mappings
2528       ,rounding_bill_to_party_id
2529       ,rndg_ship_from_party_site_id
2530     )
2531     SELECT
2532       ph.org_id --internal_organization_id
2533       ,PO_CONSTANTS_SV.APPLICATION_ID --application_id
2534       ,PO_CONSTANTS_SV.PO_ENTITY_CODE --entity_code
2535       ,PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE --event_class_code
2536       ,DECODE(ph.tax_attribute_update_code, --event_type_code
2537          'CREATE', PO_CONSTANTS_SV.PO_CREATED,
2538          'COPY_AND_CREATE', PO_CONSTANTS_SV.PO_CREATED,
2539          PO_CONSTANTS_SV.PO_ADJUSTED) --for null and 'UPDATE'
2540       ,ph.po_header_id --trx_id
2541       ,sysdate --trx_date
2542       ,ph.revision_num --trx_doc_revision
2543       ,(SELECT set_of_books_id --ledger_id
2544         FROM financials_system_params_all WHERE org_id=ph.org_id)
2545       ,ph.currency_code --trx_currency_code
2546       ,ph.rate_date --currency_conversion_date
2547       ,ph.rate --currency_conversion_rate
2548       ,ph.rate_type --currency_conversion_type
2549       --Bug 5474336. Pass mau to EBTax
2550       ,fc.minimum_accountable_unit --minimum_accountable_unit
2551       ,NVL(fc.precision, 2) --precision
2552       ,PO_CORE_S.get_default_legal_entity_id(ph.org_id) --legal_entity_id
2553       ,(SELECT pv.party_id FROM po_vendors pv --rounding_ship_from_party_id
2554         WHERE pv.vendor_id=ph.vendor_id)
2555       ,DECODE(p_calling_program, --default_taxation_country
2556               'COPY_DOCUMENT', null,
2557               zxldet.default_taxation_country)
2558       ,'N' --quote_flag
2559       ,ph.segment1 --trx_number
2560       ,ph.comments --trx_description
2561       ,sysdate --ph.print_date --trx_communicated_date
2562       ,DECODE(p_calling_program, --document_sub_type
2563               'COPY_DOCUMENT', null,
2564               zxldet.document_sub_type)
2565       ,DECODE(ph.document_creation_method, --provnl_tax_determination_date
2566          'CREATE_CONSUMPTION',
2567          (SELECT pll.need_by_date
2568           FROM po_line_locations_all pll
2569           WHERE pll.po_header_id=ph.po_header_id
2570           AND pll.need_by_date IS NOT NULL
2571           AND rownum=1),
2572           null)
2573       -- Bug 5025018. Updated tax attribute mappings
2574       ,ph.org_id --rounding_bill_to_party_id
2575       ,(SELECT pvs.party_site_id from po_vendor_sites_all pvs --rndg_ship_from_party_site_id
2576         WHERE pvs.vendor_site_id=ph.vendor_site_id)
2577     FROM po_headers_all ph, zx_lines_det_factors zxldet, fnd_currencies fc
2578     WHERE ph.po_header_id = p_po_header_id_tbl(i)
2579     AND fc.currency_code = ph.currency_code
2580     -- Conditions for getting Additional Tax Attributes
2581     -- Note that the po_header_id is of current document being processed,
2582     -- not of any source document. Get the first row obtained from join
2583     -- with zx_lines_det_factors because that table is denormalized
2584     AND zxldet.trx_id(+) = ph.po_header_id
2585     AND zxldet.application_id(+) = PO_CONSTANTS_SV.APPLICATION_ID
2586     AND zxldet.entity_code(+) = PO_CONSTANTS_SV.PO_ENTITY_CODE
2587     AND zxldet.event_class_code(+) = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE
2588     AND zxldet.trx_level_type(+) = PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE
2589     AND rownum = 1;
2590 
2591 END populate_zx_headers_with_po;
2592 
2593 
2594 -----------------------------------------------------------------------------
2595 --Start of Comments
2596 --Name: populate_header_po
2597 --Pre-reqs:
2598 --  None
2599 --Modifies:
2600 --  ZX_TRX_HEADERS_GT
2601 --Locks:
2602 --  ZX_TRX_HEADERS_GT
2603 --Function:
2604 --  Populate ZX_TRX_HEADERS_GT with transaction header data
2605 --Parameters:
2606 --IN:
2607 --p_po_header_id
2608 --  po__header_id to process for tax distribution
2609 --Notes:
2610 --  Used by determine_recovery_po procedure. Not to be used externally
2611 --End of Comments
2612 -----------------------------------------------------------------------------
2613 PROCEDURE populate_header_po(p_po_header_id  IN  NUMBER) IS
2614 BEGIN
2615 
2616   -- Populate zx_trx_headers_gt for the PO
2617   INSERT INTO zx_trx_headers_gt(
2618     internal_organization_id
2619     ,application_id
2620     ,entity_code
2621     ,event_class_code
2622     ,event_type_code
2623     ,trx_id
2624     ,trx_date
2625     ,trx_doc_revision
2626     ,ledger_id
2627     ,trx_currency_code
2628     ,currency_conversion_date
2629     ,currency_conversion_rate
2630     ,currency_conversion_type
2631     ,minimum_accountable_unit --Bug 5474336. Pass mau to EBTax
2632     ,precision
2633     ,legal_entity_id
2634     ,rounding_ship_from_party_id
2635     ,default_taxation_country
2636     ,quote_flag
2637     ,trx_number
2638     ,trx_description
2639     ,trx_communicated_date
2640     ,document_sub_type
2641     ,provnl_tax_determination_date
2642     -- Bug 5025018. Updated tax attribute mappings
2643     ,rounding_bill_to_party_id
2644     ,rndg_ship_from_party_site_id
2645   )
2646   SELECT
2647     ph.org_id --internal_organization_id
2648     ,PO_CONSTANTS_SV.APPLICATION_ID --application_id
2649     ,PO_CONSTANTS_SV.PO_ENTITY_CODE --entity_code
2650     ,PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE --event_class_code
2651     ,PO_CONSTANTS_SV.PO_REDISTRIBUTED --event_type_code
2652     ,ph.po_header_id --trx_id
2653     ,sysdate --trx_date
2654     ,ph.revision_num --trx_doc_revision
2655     ,(SELECT set_of_books_id --ledger_id
2656       FROM financials_system_params_all WHERE org_id=ph.org_id)
2657     ,ph.currency_code --trx_currency_code
2658     ,ph.rate_date --currency_conversion_date
2659     ,ph.rate --currency_conversion_rate
2660     ,ph.rate_type --currency_conversion_type
2661     --Bug 5474336. Pass mau to EBTax
2662     ,fc.minimum_accountable_unit --minimum_accountable_unit
2663     ,NVL(fc.precision, 2) --precision
2664     ,PO_CORE_S.get_default_legal_entity_id(ph.org_id) --legal_entity_id
2665     ,(SELECT pv.party_id FROM po_vendors pv --rounding_ship_from_party_id
2666       WHERE pv.vendor_id=ph.vendor_id)
2667     ,null --default_taxation_country
2668     ,'N' --quote_flag
2669     ,ph.segment1 --trx_number
2670     ,ph.comments --trx_description
2671     ,sysdate --ph.print_date --trx_communicated_date
2672     ,null --document_sub_type
2673     ,DECODE(ph.document_creation_method, --provnl_tax_determination_date
2674        'CREATE_CONSUMPTION',
2675        (SELECT pll.need_by_date
2676         FROM po_line_locations_all pll
2677         WHERE pll.po_header_id=ph.po_header_id
2678         AND pll.need_by_date IS NOT NULL
2679         AND rownum=1),
2680         null)
2681     -- Bug 5025018. Updated tax attribute mappings
2682     ,ph.org_id --rounding_bill_to_party_id
2683     ,(SELECT pvs.party_site_id from po_vendor_sites_all pvs --rndg_ship_from_party_site_id
2684       WHERE pvs.vendor_site_id=ph.vendor_site_id)
2685   FROM po_headers_all ph, fnd_currencies fc
2686   WHERE ph.po_header_id = p_po_header_id
2687   AND fc.currency_code = ph.currency_code;
2688 
2689 END populate_header_po;
2690 
2691 
2692 -----------------------------------------------------------------------------
2693 --Start of Comments
2694 --Name: populate_zx_headers_with_rel
2695 --Pre-reqs:
2696 --  None
2697 --Modifies:
2698 --  ZX_TRX_HEADERS_GT
2699 --Locks:
2700 --  ZX_TRX_HEADERS_GT
2701 --Function:
2702 --  Populate ZX_TRX_HEADERS_GT with transaction header data
2703 --Parameters:
2704 --IN:
2705 --p_po_release_id_tbl
2706 --  PL/SQL table with list of po_release_id's to process for tax_calculation
2707 --p_calling_program
2708 --  Identifies the module that calls this procedure eg. 'PDOI'
2709 --Notes:
2710 --  Used by calculate_tax procedure. Not to be used externally
2711 --End of Comments
2712 -----------------------------------------------------------------------------
2713 PROCEDURE populate_zx_headers_with_rel(p_po_release_id_tbl  IN  PO_TBL_NUMBER,
2714                                        p_calling_program    IN  VARCHAR2
2715 ) IS
2716 BEGIN
2717   -- Populate zx_trx_headers_gt for Releases
2718   FORALL i in 1..p_po_release_id_tbl.COUNT
2719     INSERT INTO zx_trx_headers_gt(
2720       internal_organization_id
2721       ,application_id
2722       ,entity_code
2723       ,event_class_code
2724       ,event_type_code
2725       ,trx_id
2726       ,trx_date
2727       ,trx_doc_revision
2728       ,ledger_id
2729       ,trx_currency_code
2730       ,currency_conversion_date
2731       ,currency_conversion_rate
2732       ,currency_conversion_type
2733       ,minimum_accountable_unit --Bug 5474336. Pass mau to EBTax
2734       ,precision
2735       ,legal_entity_id
2736       ,rounding_ship_from_party_id
2737       ,default_taxation_country
2738       ,quote_flag
2739       ,trx_number
2740       ,trx_description
2741       ,trx_communicated_date
2742       ,document_sub_type
2743       ,provnl_tax_determination_date
2744       -- Bug 5025018. Updated tax attribute mappings
2745       ,rounding_bill_to_party_id
2746       ,rndg_ship_from_party_site_id
2747     )
2748     SELECT
2749       pr.org_id --internal_organization_id
2750       ,PO_CONSTANTS_SV.APPLICATION_ID --application_id
2751       ,PO_CONSTANTS_SV.REL_ENTITY_CODE --entity_code
2752       ,PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE --event_class_code
2753       ,DECODE(pr.tax_attribute_update_code, --event_type_code
2754          'CREATE', PO_CONSTANTS_SV.REL_CREATED,
2755          'COPY_AND_CREATE', PO_CONSTANTS_SV.REL_CREATED,
2756          PO_CONSTANTS_SV.REL_ADJUSTED) -- for null and 'UPDATE'
2757       ,pr.po_release_id --trx_id
2758       ,sysdate --trx_date
2759       ,pr.revision_num --trx_doc_revision
2760       ,(select set_of_books_id  --ledger_id
2761         from financials_system_params_all where org_id=pr.org_id)
2762       ,ph.currency_code --trx_currency_code
2763       ,ph.rate_date --currency_conversion_date
2764       ,ph.rate --currency_conversion_rate
2765       ,ph.rate_type --currency_conversion_type
2766       --Bug 5474336. Pass mau to EBTax
2767       ,fc.minimum_accountable_unit --minimum_accountable_unit
2768       ,NVL(fc.precision, 2) --precision
2769       ,PO_CORE_S.get_default_legal_entity_id(pr.org_id) --legal_entity_id
2770       ,(SELECT pv.party_id FROM po_vendors pv --rounding_ship_from_party_id
2771         WHERE pv.vendor_id=ph.vendor_id)
2772       ,zxldet.default_taxation_country --default_taxation_country
2773       ,'N' --quote_flag
2774       ,ph.segment1 --trx_number
2775       ,null --trx_description
2776       ,sysdate --pr.print_date --trx_communicated_date
2777       ,zxldet.document_sub_type --document_sub_type
2778       ,DECODE(pr.document_creation_method, --provnl_tax_determination_date
2779          'CREATE_CONSUMPTION',
2780          (SELECT pll.need_by_date
2781           FROM po_line_locations_all pll
2782           WHERE pll.po_release_id=pr.po_release_id
2783           AND pll.need_by_date IS NOT NULL
2784           AND rownum=1),
2785           null)
2786       -- Bug 5025018. Updated tax attribute mappings
2787       ,pr.org_id --rounding_bill_to_party_id
2788       ,(SELECT pvs.party_site_id from po_vendor_sites_all pvs --rndg_ship_from_party_site_id
2789         WHERE pvs.vendor_site_id=ph.vendor_site_id)
2790     -- Using OUTER JOIN in FROM clause syntax here because (+) operator
2791     -- is not flexible enough to be used inside an OR condition
2792     FROM po_headers_all ph
2793          ,fnd_currencies fc
2794          ,po_releases_all pr
2795            -- Conditions for getting Additional Tax Attributes
2796            -- Copy from Planned PO if its a newly created Scheduled Release
2797            -- ELSE simply copy from existing release header (ie. in case of a
2798            -- shipment split or regular blanket/scheduled release create/update
2799            LEFT OUTER JOIN zx_lines_det_factors zxldet
2800              ON ((pr.po_header_id = zxldet.trx_id
2801                   AND PO_CONSTANTS_SV.APPLICATION_ID = zxldet.application_id
2802                   AND PO_CONSTANTS_SV.PO_ENTITY_CODE = zxldet.entity_code
2803                   AND PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE = zxldet.event_class_code
2804                   AND PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE = zxldet.trx_level_type
2805                   AND pr.release_type = PO_CONSTANTS_SV.SCHEDULED
2806                   AND pr.tax_attribute_update_code = 'CREATE')
2807                  OR
2808                  (pr.po_release_id = zxldet.trx_id
2809                   AND PO_CONSTANTS_SV.APPLICATION_ID = zxldet.application_id
2810                   AND PO_CONSTANTS_SV.REL_ENTITY_CODE = zxldet.entity_code
2811                   AND PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE = zxldet.event_class_code
2812                   AND PO_CONSTANTS_SV.REL_TRX_LEVEL_TYPE = zxldet.trx_level_type)
2813                 )
2814     WHERE pr.po_release_id = p_po_release_id_tbl(i)
2815     AND pr.po_header_id = ph.po_header_id
2816     AND fc.currency_code = ph.currency_code
2817     AND rownum = 1;
2818 
2819 END populate_zx_headers_with_rel;
2820 
2821 
2822 -----------------------------------------------------------------------------
2823 --Start of Comments
2824 --Name: populate_header_rel
2825 --Pre-reqs:
2826 --  None
2827 --Modifies:
2828 --  ZX_TRX_HEADERS_GT
2829 --Locks:
2830 --  ZX_TRX_HEADERS_GT
2831 --Function:
2832 --  Populate ZX_TRX_HEADERS_GT with transaction header data
2833 --Parameters:
2834 --IN:
2835 --p_po_release_id
2836 --  po_release_id to process for tax distribution
2837 --Notes:
2838 --  Used by determine_recovery_rel procedure. Not to be used externally
2839 --End of Comments
2840 -----------------------------------------------------------------------------
2841 PROCEDURE populate_header_rel(p_po_release_id  IN  NUMBER) IS
2842 BEGIN
2843 
2844   -- Populate zx_trx_headers_gt for Releases
2845   INSERT INTO zx_trx_headers_gt(
2846     internal_organization_id
2847     ,application_id
2848     ,entity_code
2849     ,event_class_code
2850     ,event_type_code
2851     ,trx_id
2852     ,trx_date
2853     ,trx_doc_revision
2854     ,ledger_id
2855     ,trx_currency_code
2856     ,currency_conversion_date
2857     ,currency_conversion_rate
2858     ,currency_conversion_type
2859     ,minimum_accountable_unit --Bug 5474336. Pass mau to EBTax
2860     ,precision
2861     ,legal_entity_id
2862     ,rounding_ship_from_party_id
2863     ,default_taxation_country
2864     ,quote_flag
2865     ,trx_number
2866     ,trx_description
2867     ,trx_communicated_date
2868     ,document_sub_type
2869     ,provnl_tax_determination_date
2870     -- Bug 5025018. Updated tax attribute mappings
2871     ,rounding_bill_to_party_id
2872     ,rndg_ship_from_party_site_id
2873   )
2874   SELECT
2875     pr.org_id --internal_organization_id
2876     ,PO_CONSTANTS_SV.APPLICATION_ID --application_id
2877     ,PO_CONSTANTS_SV.REL_ENTITY_CODE --entity_code
2878     ,PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE --event_class_code
2879     ,PO_CONSTANTS_SV.REL_REDISTRIBUTED --event_type_code
2880     ,pr.po_release_id --trx_id
2881     ,sysdate --trx_date
2882     ,pr.revision_num --trx_doc_revision
2883     ,(select set_of_books_id  --ledger_id
2884       from financials_system_params_all where org_id=pr.org_id)
2885     ,ph.currency_code --trx_currency_code
2886     ,ph.rate_date --currency_conversion_date
2887     ,ph.rate --currency_conversion_rate
2888     ,ph.rate_type --currency_conversion_type
2889     --Bug 5474336. Pass mau to EBTax
2890     ,fc.minimum_accountable_unit --minimum_accountable_unit
2891     ,NVL(fc.precision, 2) --precision
2892     ,PO_CORE_S.get_default_legal_entity_id(pr.org_id) --legal_entity_id
2893     ,(SELECT pv.party_id FROM po_vendors pv --rounding_ship_from_party_id
2894       WHERE pv.vendor_id=ph.vendor_id)
2895     ,null --default_taxation_country
2896     ,'N' --quote_flag
2897     ,ph.segment1 --trx_number
2898     ,null --trx_description
2899     ,sysdate --pr.print_date --trx_communicated_date
2900     ,null --document_sub_type
2901     ,DECODE(pr.document_creation_method, --provnl_tax_determination_date
2902        'CREATE_CONSUMPTION',
2903        (SELECT pll.need_by_date
2904         FROM po_line_locations_all pll
2905         WHERE pll.po_release_id=pr.po_release_id
2906         AND pll.need_by_date IS NOT NULL
2907         AND rownum=1),
2908         null)
2909     -- Bug 5025018. Updated tax attribute mappings
2910     ,ph.org_id --rounding_bill_to_party_id
2911     ,(SELECT pvs.party_site_id from po_vendor_sites_all pvs --rndg_ship_from_party_site_id
2912       WHERE pvs.vendor_site_id=ph.vendor_site_id)
2913   FROM po_releases_all pr, po_headers_all ph, fnd_currencies fc
2914   WHERE pr.po_release_id = p_po_release_id
2915   AND pr.po_header_id = ph.po_header_id
2916   AND fc.currency_code = ph.currency_code;
2917 
2918 END populate_header_rel;
2919 
2920 
2921 -----------------------------------------------------------------------------
2922 --Start of Comments
2923 --Name: populate_zx_headers_with_req
2924 --Pre-reqs:
2925 --  None
2926 --Modifies:
2927 --  ZX_TRX_HEADERS_GT
2928 --Locks:
2929 --  ZX_TRX_HEADERS_GT
2930 --Function:
2931 --  Populate ZX_TRX_HEADERS_GT with transaction header data
2932 --Parameters:
2933 --IN:
2934 --p_requisition_header_id
2935 --  PL/SQL table with list of po_header_id's to process for tax_calculation
2936 --p_calling_program
2937 --  Identifies the module that calls this procedure eg. 'PDOI'
2938 --Notes:
2939 --  Used by calculate_tax_requisition procedure. Not to be used externally
2940 --End of Comments
2941 -----------------------------------------------------------------------------
2942 PROCEDURE populate_zx_headers_with_req(p_requisition_header_id  IN  NUMBER,
2943                                        p_calling_program        IN  VARCHAR2
2944 ) IS
2945 BEGIN
2946   -- populate zx_trx_headers_gt for the requisition
2947   -- Addition/Removal of any attribute entails similar change in
2948   -- populate_zx_record as well
2949   INSERT INTO zx_trx_headers_gt(
2950     internal_organization_id
2951     ,application_id
2952     ,entity_code
2953     ,event_class_code
2954     ,event_type_code
2955     ,trx_id
2956     ,trx_date
2957     ,ledger_id
2958     ,legal_entity_id
2959     ,rounding_bill_to_party_id
2960     ,quote_flag
2961     ,document_sub_type
2962     ,default_taxation_country
2963     ,icx_session_id
2964   )
2965   SELECT
2966     prh.org_id --internal_organization_id
2967     ,PO_CONSTANTS_SV.APPLICATION_ID --application_id
2968     ,PO_CONSTANTS_SV.REQ_ENTITY_CODE --entity_code
2969     ,PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE --event_class_code
2970     ,DECODE(p_calling_program, --event_type_code
2971       --If calling determine_recovery from ATI page flow
2972       'DETERMINE_RECOVERY_REQ', PO_CONSTANTS_SV.REQ_DISTRIBUTED,
2973       --Regular UI flow
2974       PO_CONSTANTS_SV.REQ_CREATED)
2975     ,prh.requisition_header_id --trx_id
2976     ,sysdate --trx_date
2977     ,(select set_of_books_id  --ledger_id
2978       from financials_system_params_all where org_id=prh.org_id)
2979     ,PO_CORE_S.get_default_legal_entity_id(prh.org_id) --legal_entity_id
2980     ,prh.org_id --rounding_bill_to_party_id
2981     ,'Y' --quote_flag
2982     ,zxldet.document_sub_type --document_sub_type
2983     ,zxldet.default_taxation_country --default_taxation_country
2984     ,DECODE(p_calling_program, --icx_session_id
2985       'DETERMINE_RECOVERY_REQ', null,
2986       FND_GLOBAL.session_id)
2987   FROM po_requisition_headers_all prh, zx_lines_det_factors zxldet
2988   WHERE prh.requisition_header_id = p_requisition_header_id
2989   -- Conditions for getting Additional Tax Attributes
2990   -- Note that the req_header_id is of current document being processed,
2991   -- not of any source document. Get the first row obtained from join
2992   -- with zx_lines_det_factors because that table is denormalized
2993   AND zxldet.trx_id(+) = prh.requisition_header_id
2994   AND zxldet.application_id(+) = PO_CONSTANTS_SV.APPLICATION_ID
2995   AND zxldet.entity_code(+) = PO_CONSTANTS_SV.REQ_ENTITY_CODE
2996   AND zxldet.event_class_code(+) = PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE
2997   AND zxldet.trx_level_type(+) = PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE
2998   AND rownum = 1;
2999 
3000 END populate_zx_headers_with_req;
3001 
3002 /* Bug 11665348: Modified the procedures populate_zx_lines_with_po and
3003 populate_zx_lines_with_rel so that they will dump the data into zx_transaction_lines_gt
3004 only for the PO shipments which are not received and not billed and not shipped.
3005 */
3006 
3007 -----------------------------------------------------------------------------
3008 --Start of Comments
3009 --Name: populate_zx_lines_with_po
3010 --Pre-reqs:
3011 --  None
3012 --Modifies:
3013 --  ZX_TRANSACTION_LINES_GT
3014 --Locks:
3015 --  ZX_TRANSACTION_LINES_GT
3016 --Function:
3017 --  Populate ZX_TRANSACTION_LINES_GT with transaction line and shipment data
3018 --  which are not received/billed
3019 
3020 --Parameters:
3021 --IN:
3022 --p_po_header_id_tbl
3023 --  PL/SQL table with list of po_header_id's to process for tax_calculation
3024 --p_calling_program
3025 --  Identifies the module that calls this procedure eg. 'PDOI'
3026 --Notes:
3027 --  Used by calculate_tax procedure. Not to be used externally
3028 --End of Comments
3029 -----------------------------------------------------------------------------
3030 PROCEDURE populate_zx_lines_with_po(p_po_header_id_tbl  IN  PO_TBL_NUMBER,
3031                                     p_calling_program   IN  VARCHAR2
3032 ) IS
3033 BEGIN
3034 
3035     -- Populate zx_transaction_lines_gt for POs
3036   FORALL i in 1..p_po_header_id_tbl.COUNT
3037     INSERT INTO zx_transaction_lines_gt(
3038       application_id
3039       ,entity_code
3040       ,event_class_code
3041       ,trx_id
3042       ,trx_level_type
3043       ,trx_line_id
3044       ,line_level_action
3045       ,line_class
3046       ,trx_line_type
3047       ,trx_line_date
3048       ,trx_business_category
3049       ,line_intended_use
3050       ,user_defined_fisc_class
3051       ,line_amt
3052       ,trx_line_quantity
3053       ,product_id
3054       ,product_org_id
3055       ,product_fisc_classification
3056       ,uom_code
3057       ,product_type
3058       ,product_code
3059       ,product_category
3060       ,fob_point
3061       ,ship_from_party_id
3062       ,bill_from_party_id
3063       ,ship_from_party_site_id
3064       ,bill_from_party_site_id
3065       ,ship_to_location_id
3066       ,ship_from_location_id
3067       ,bill_to_location_id
3068       ,bill_from_location_id /* 6524317 */
3069       ,account_ccid
3070       ,ref_doc_application_id
3071       ,ref_doc_entity_code
3072       ,ref_doc_event_class_code
3073       ,ref_doc_trx_id
3074       ,ref_doc_line_id
3075       ,line_trx_user_key1
3076       ,line_trx_user_key2
3077       -- Bug 5079867. Ordering of complex work payment lines in ATI page
3078       ,line_trx_user_key3
3079       ,trx_line_number
3080       ,trx_line_description
3081       ,product_description
3082       ,assessable_value
3083       ,line_amt_includes_tax_flag
3084       ,input_tax_classification_code
3085       ,source_application_id
3086       ,source_entity_code
3087       ,source_event_class_code
3088       ,source_trx_id
3089       ,source_line_id
3090       ,source_trx_level_type
3091       ,unit_price
3092       ,ref_doc_trx_level_type
3093       -- Bug 5025018. Updated tax attribute mappings
3094       ,ship_third_pty_acct_id
3095       ,bill_third_pty_acct_id
3096       ,ship_third_pty_acct_site_id
3097       ,bill_third_pty_acct_site_id
3098       ,ship_to_party_id
3099       ,user_upd_det_factors_flag --Bug 5632300
3100       ,defaulting_attribute1 --Bug#6902111
3101     )
3102     SELECT
3103       PO_CONSTANTS_SV.APPLICATION_ID --application_id
3104       ,PO_CONSTANTS_SV.PO_ENTITY_CODE --entity_code
3105       ,PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE --event_class_code
3106       ,ph.po_header_id --trx_id
3107       ,PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE --trx_level_type
3108       ,pll.line_location_id --trx_line_id
3109       ,pll.tax_attribute_update_code --line_level_action
3110       ,DECODE(pll.shipment_type,--line_class
3111               'PREPAYMENT', DECODE(pll.payment_type,
3112                                    'ADVANCE', 'ADVANCE',
3113                                    'FINANCING'),
3114               DECODE(pll.value_basis,
3115                      'QUANTITY', 'INVOICE',
3116                       'AMOUNT_MATCHED') /* Modified for bug 1018858 ,'AMOUNT' is not valid zx_line_class,so changed it to be 'AMOUNT_MATCHED'*/
3117              )
3118       ,'ITEM' --trx_line_type
3119       ,COALESCE(pll.need_by_date, pll.promised_date, sysdate) --trx_line_date
3120       ,DECODE(p_calling_program, --trx_business_category
3121               'COPY_DOCUMENT', null,
3122               zxldet.trx_business_category)
3123       ,DECODE(p_calling_program, --line_intended_use
3124               'COPY_DOCUMENT', null,
3125               zxldet.line_intended_use)
3126       ,DECODE(p_calling_program, --user_defined_fisc_class
3127               'COPY_DOCUMENT', null,
3128               zxldet.user_defined_fisc_class)
3129       ,NVL(pll.amount, pll.price_override*pll.quantity) --line_amt
3130       ,pll.quantity --trx_line_quantity
3131       ,pl.item_id --product_id
3132        -- Bug 5335818. Pass in inventory_organization_id
3133       ,(SELECT fsp.inventory_organization_id --product_org_id
3134         FROM financials_system_params_all fsp
3135         WHERE fsp.org_id=pll.org_id)
3136       ,DECODE(p_calling_program, --product_fisc_classification
3137               'COPY_DOCUMENT', null,
3138               zxldet.product_fisc_classification)
3139       ,(SELECT mum.uom_code FROM mtl_units_of_measure mum
3140         WHERE mum.unit_of_measure=pll.unit_meas_lookup_code) --uom_code
3141       ,DECODE(p_calling_program, --product_type
3142               'COPY_DOCUMENT', null,
3143               zxldet.product_type)
3144       ,msib.segment1 --product_code
3145       ,DECODE(p_calling_program, --product_category
3146               'COPY_DOCUMENT', null,
3147               zxldet.product_category)
3148       ,ph.fob_lookup_code --fob_point
3149       ,pv.party_id --ship_from_party_id
3150       ,pv.party_id --bill_from_party_id
3151       ,pvs.party_site_id --ship_from_party_site_id
3152       ,pvs.party_site_id --bill_from_party_site_id
3153       ,pll.ship_to_location_id --ship_to_location_id
3154       ,(SELECT hzps.location_id --ship_from_location_id
3155         FROM hz_party_sites hzps
3156         WHERE hzps.party_site_id = pvs.party_site_id)
3157       ,ph.bill_to_location_id --bill_to_location_id
3158       ,(SELECT pvs.location_id from po_vendor_sites_all pvs   /* 6524317 - Passing Location Id as Bill From Location Id */
3159         WHERE pvs.vendor_site_id=ph.vendor_site_id)
3160       -- Get account id from first distribution, if created
3161       -- otherwise from the expense account of the item
3162       ,NVL((SELECT pd.code_combination_id --account_ccid
3163             FROM po_distributions_all pd
3164             WHERE pd.line_location_id = pll.line_location_id
3165             AND rownum = 1),
3166            msib.expense_account
3167           )
3168       ,null --ref_doc_application_id
3169       ,null --ref_doc_entity_code
3170       ,null --ref_doc_event_class_code
3171       ,null --ref_doc_trx_id
3172       ,null --ref_doc_line_id
3173       ,pl.line_num --line_trx_user_key1
3174       ,PO_LOCATIONS_S.get_location_code(pll.ship_to_location_id) --line_trx_user_key2
3175       -- Bug 5079867. Ordering of complex work payment lines in ATI page
3176       ,DECODE(pll.payment_type, null, 0, --line_trx_user_key3
3177                 'DELIVERY', 1,
3178                 'ADVANCE', 2, 3)
3179       ,DECODE(pll.payment_type, --trx_line_number
3180               'ADVANCE', null,
3181               'DELIVERY', null,
3182               pll.shipment_num)
3183       ,DECODE(pll.shipment_type, --trx_line_description
3184               'STANDARD', DECODE(pll.payment_type,
3185                                  null, pl.item_description, --non complex work Standard PO
3186                                  pll.description --complex work Standard PO
3187                                 ),
3188               pl.item_description -- for shipment_type='PLANNED'
3189              )
3190       ,DECODE(pll.shipment_type, --product_description
3191               'STANDARD', DECODE(pll.payment_type,
3192                                  null, pl.item_description, --non complex work Standard PO
3193                                  pll.description --complex work Standard PO
3194                                 ),
3195               pl.item_description -- for shipment_type='PLANNED'
3196              )
3197       ,DECODE(p_calling_program, --assessable_value
3198               'COPY_DOCUMENT', null,
3199               zxldet.assessable_value)
3200       ,'N' --line_amt_includes_tax_flag
3201       -- Only newly added lines can come in through PDOI (no updated
3202       -- lines), so tax_name can be populated to override.
3203       -- Once the new line has been created, if it is modified
3204       -- through any other program, tax_name field will be populated
3205       -- but will not get populated as overridden tax_classification
3206       ,DECODE(p_calling_program, --input_tax_classification_code
3207               'PDOI', pll.tax_name,
3208               'COPY_DOCUMENT', null,
3209               zxldet.input_tax_classification_code)
3210       -- Bug 7337548
3211       -- Copying requisition information as Source Document Information
3212       -- in case of Autocreate. This information will be used to copy
3213       -- Tax attributes when the attributes are overridden in requisition.
3214       -- Calling program for autocreate is 'POXBWVRP_PO','AUTOCREATED_DOC_WF'
3215       -- and 'PORELGEB'
3216       ,DECODE(p_calling_program, --source_application_id
3217               'COPY_DOCUMENT', PO_CONSTANTS_SV.APPLICATION_ID,
3218               'POXBWVRP_PO', PO_CONSTANTS_SV.APPLICATION_ID,
3219               'AUTOCREATED_DOC_WF' ,PO_CONSTANTS_SV.APPLICATION_ID,
3220               'PORELGEB',PO_CONSTANTS_SV.APPLICATION_ID,
3221               null)
3222       ,DECODE(p_calling_program, --source_entity_code
3223               'COPY_DOCUMENT', PO_CONSTANTS_SV.PO_ENTITY_CODE,
3224               'POXBWVRP_PO', PO_CONSTANTS_SV.REQ_ENTITY_CODE,
3225               'AUTOCREATED_DOC_WF', PO_CONSTANTS_SV.REQ_ENTITY_CODE,
3226               'PORELGEB', PO_CONSTANTS_SV.REQ_ENTITY_CODE,
3227               null)
3228       ,DECODE(p_calling_program, --source_event_class_code
3229               'COPY_DOCUMENT', PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE,
3230               'POXBWVRP_PO', PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE,
3231               'AUTOCREATED_DOC_WF', PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE,
3232               'PORELGEB', PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE,
3233               null)
3234       ,DECODE(p_calling_program, -- source_trx_id
3235               'COPY_DOCUMENT', (SELECT source_shipment.po_header_id --source_trx_id
3236                                 FROM po_line_locations_all source_shipment
3237                                 WHERE source_shipment.line_location_id = pll.original_shipment_id
3238                                 AND p_calling_program = 'COPY_DOCUMENT'),
3239 
3240               'POXBWVRP_PO',  (SELECT requisition_header_id FROM
3241                                 po_requisition_lines_all
3242                                 WHERE  requisition_line_id IN (SELECT Min(requisition_line_id)    --source_trx_id
3243                                                                FROM po_requisition_lines_all prl
3244                                                                WHERE prl.line_location_id = pll.line_location_id)
3245                                 AND p_calling_program = 'POXBWVRP_PO'),
3246               'AUTOCREATED_DOC_WF',  (SELECT requisition_header_id FROM
3247                                 po_requisition_lines_all
3248                                 WHERE  requisition_line_id IN (SELECT Min(requisition_line_id)    --source_trx_id
3249                                                                FROM po_requisition_lines_all prl
3250                                                                WHERE prl.line_location_id = pll.line_location_id)
3251                                 AND p_calling_program = 'AUTOCREATED_DOC_WF'),
3252               'PORELGEB',      (SELECT requisition_header_id FROM
3253                                 po_requisition_lines_all
3254                                 WHERE  requisition_line_id IN (SELECT Min(requisition_line_id)    --source_trx_id
3255                                                                FROM po_requisition_lines_all prl
3256                                                                WHERE prl.line_location_id = pll.line_location_id)
3257                                 AND p_calling_program = 'PORELGEB'),
3258               null)
3259       ,DECODE(p_calling_program, --source_line_id
3260               'COPY_DOCUMENT', pll.original_shipment_id,
3261               'POXBWVRP_PO',(SELECT Min(requisition_line_id)    --source_line_id
3262                              FROM po_requisition_lines_all prl
3263                              WHERE prl.line_location_id = pll.line_location_id
3264                              AND p_calling_program = 'POXBWVRP_PO'),
3265               'AUTOCREATED_DOC_WF',(SELECT Min(requisition_line_id)    --source_line_id
3266                              FROM po_requisition_lines_all prl
3267                              WHERE prl.line_location_id = pll.line_location_id
3268                              AND p_calling_program = 'AUTOCREATED_DOC_WF'),
3269               'PORELGEB',(SELECT Min(requisition_line_id)    --source_line_id
3270                              FROM po_requisition_lines_all prl
3271                              WHERE prl.line_location_id = pll.line_location_id
3272                              AND p_calling_program = 'PORELGEB'),
3273               null)
3274       ,DECODE(p_calling_program, --source_trx_level_type
3275               'COPY_DOCUMENT', PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE,
3276               'POXBWVRP_PO', PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE ,
3277 	      'AUTOCREATED_DOC_WF', PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE ,
3278               'PORELGEB', PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE ,
3279               null)
3280       ,pll.price_override --unit_price
3281       ,null --ref_doc_trx_level_type
3282       -- Bug 5025018. Updated tax attribute mappings
3283       ,pv.vendor_id --ship_third_pty_acct_id
3284       ,pv.vendor_id --bill_third_pty_acct_id
3285       ,pvs.vendor_site_id --ship_third_pty_acct_site_id
3286       ,pvs.vendor_site_id --bill_third_pty_acct_site_id
3287       ,pll.ship_to_organization_id --ship_to_party_id
3288       --Bug 5632300. Parameter to confirm that tax classification is overridden
3289       ,(SELECT 'Y' FROM DUAL --user_upd_det_factors_flag
3290         WHERE p_calling_program = 'PDOI'
3291         AND pll.tax_name IS NOT NULL)
3292       ,pll.ship_to_organization_id  --Bug#6902111
3293     FROM po_headers_all ph, po_lines_all pl, po_line_locations_all pll,
3294          zx_lines_det_factors zxldet, po_vendors pv,
3295          po_vendor_sites_all pvs, mtl_system_items_b msib
3296     WHERE ph.po_header_id = pll.po_header_id
3297     AND pl.po_line_id = pll.po_line_id
3298       ---Bug# 13091627 : Condition added to exclude the Release shipments for BLANKET/PLANNED PO
3299     AND pll.po_release_id IS NULL
3300     AND pll.tax_attribute_update_code IS NOT NULL
3301     AND pll.tax_attribute_update_code <> 'DIST_DELETE'
3302     -- Bug 11665348
3303     AND Nvl(pll.quantity_billed,0)=0
3304     AND Nvl(pll.quantity_received,0)=0
3305     AND Nvl(pll.quantity_shipped,0)=0
3306     AND Nvl(pll.amount_billed,0)=0
3307     AND Nvl(pll.amount_received,0)=0
3308     AND Nvl(pll.amount_shipped,0)=0
3309      -- end bug 11665348
3310 
3311     AND ph.po_header_id = p_po_header_id_tbl(i)
3312     -- Conditions for getting Additional Tax Attributes
3313     --  Do not put a condition on zxldet.trx_id here because that would
3314     --  entail bringing the source_header_id for the shipment being currently
3315     --  processed. Join with trx_line_id itself is unique because the document
3316     --  type has been classified with event_class_code and line_location_id
3317     --  will always be unique whether PO shipment or Release shipment
3318     AND zxldet.application_id(+) = PO_CONSTANTS_SV.APPLICATION_ID
3319     AND zxldet.entity_code(+) = PO_CONSTANTS_SV.PO_ENTITY_CODE
3320     AND zxldet.event_class_code(+) = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE
3321     AND zxldet.trx_level_type(+) = PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE
3322     AND zxldet.trx_line_id(+) = pll.original_shipment_id
3323     -- Join with vendor tables to get party and party site information
3324     AND pv.vendor_id(+) = ph.vendor_id
3325     AND pvs.vendor_site_id(+) = ph.vendor_site_id
3326     -- Join with items table for item information
3327     AND msib.inventory_item_id(+) = pl.item_id
3328     AND msib.organization_id(+) = pl.org_id;
3329     -- Conditions that determine that po line is 'Active'
3330     --AND nvl(pl.cancel_flag,'N') = 'N'
3331     --AND nvl(pl.closed_code,'OPEN') <> 'FINALLY CLOSED'
3332     --AND nvl(pll.cancel_flag,'N') = 'N'
3333     --AND nvl(pll.closed_code,'OPEN') <> 'FINALLY CLOSED';
3334 
3335 END populate_zx_lines_with_po;
3336 
3337 
3338 -----------------------------------------------------------------------------
3339 --Start of Comments
3340 --Name: populate_zx_lines_with_rel
3341 --Pre-reqs:
3342 --  None
3343 --Modifies:
3344 --  ZX_TRANSACTION_LINES_GT
3345 --Locks:
3346 --  ZX_TRANSACTION_LINES_GT
3347 --Function:
3348 --  Populate ZX_TRANSACTION_LINES_GT with transaction line and shipment data
3349 --  which are not received/billed
3350 
3351 --Parameters:
3352 --IN:
3353 --p_po_release_id_tbl
3354 --  PL/SQL table with list of po_release_id's to process for tax_calculation
3355 --p_calling_program
3356 --  Identifies the module that calls this procedure eg. 'PDOI'
3357 --Notes:
3358 --  Used by calculate_tax procedure. Not to be used externally
3359 --End of Comments
3360 -----------------------------------------------------------------------------
3361 PROCEDURE populate_zx_lines_with_rel(p_po_release_id_tbl  IN  PO_TBL_NUMBER,
3362                                      p_calling_program    IN  VARCHAR2
3363 ) IS
3364 BEGIN
3365   -- Populate zx_transaction_lines_gt for Releases
3366   FORALL i IN 1..p_po_release_id_tbl.COUNT
3367     INSERT INTO zx_transaction_lines_gt(
3368       application_id
3369       ,entity_code
3370       ,event_class_code
3371       ,trx_id
3372       ,trx_level_type
3373       ,trx_line_id
3374       ,line_level_action
3375       ,line_class
3376       ,trx_line_type
3377       ,trx_line_date
3378       ,trx_business_category
3379       ,line_intended_use
3380       ,user_defined_fisc_class
3381       ,line_amt
3382       ,trx_line_quantity
3383       ,product_id
3384       ,product_org_id
3385       ,product_fisc_classification
3386       ,uom_code
3387       ,product_type
3388       ,product_code
3389       ,product_category
3390       ,fob_point
3391       ,ship_from_party_id
3392       ,bill_from_party_id
3393       ,ship_from_party_site_id
3394       ,bill_from_party_site_id
3395       ,ship_to_location_id
3396       ,ship_from_location_id
3397       ,bill_to_location_id
3398       ,bill_from_location_id   /* 6524317 */
3399       ,account_ccid
3400       ,ref_doc_application_id
3401       ,ref_doc_entity_code
3402       ,ref_doc_event_class_code
3403       ,ref_doc_trx_id
3404       ,ref_doc_line_id
3405       ,line_trx_user_key1
3406       ,line_trx_user_key2
3407       ,trx_line_number
3408       ,trx_line_description
3409       ,product_description
3410       ,assessable_value
3411       ,line_amt_includes_tax_flag
3412       ,input_tax_classification_code
3413       ,source_application_id
3414       ,source_entity_code
3415       ,source_event_class_code
3416       ,source_trx_id
3417       ,source_line_id
3418       ,source_trx_level_type
3419       ,unit_price
3420       ,ref_doc_trx_level_type
3421       -- Bug 5025018. Updated tax attribute mappings
3422       ,ref_doc_line_quantity
3423       ,ship_third_pty_acct_id
3424       ,bill_third_pty_acct_id
3425       ,ship_third_pty_acct_site_id
3426       ,bill_third_pty_acct_site_id
3427       ,ship_to_party_id
3428       ,defaulting_attribute1 --Bug#6902111
3429     )
3430     SELECT
3431       PO_CONSTANTS_SV.APPLICATION_ID --application_id
3432       ,PO_CONSTANTS_SV.REL_ENTITY_CODE --entity_code
3433       ,PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE --event_class_code
3434       ,pr.po_release_id --trx_id
3435       ,PO_CONSTANTS_SV.REL_TRX_LEVEL_TYPE --trx_level_type
3436       ,pll.line_location_id --trx_line_id
3437       ,pll.tax_attribute_update_code --line_level_action
3438       ,DECODE(pll.shipment_type,--line_class
3439               'PREPAYMENT', DECODE(pll.payment_type,
3440                                    'ADVANCE', 'ADVANCE',
3441                                    'FINANCING'),
3442               DECODE(pll.value_basis,
3443                      'QUANTITY', 'INVOICE',
3444                      'AMOUNT')
3445              )
3446       ,'ITEM' --trx_line_type
3447       ,COALESCE(pll.need_by_date, pll.promised_date, sysdate) --trx_line_date
3448       ,zxldet.trx_business_category --trx_business_category
3449       ,zxldet.line_intended_use --line_intended_use
3450       ,zxldet.user_defined_fisc_class --user_defined_fisc_class
3451       ,nvl(pll.amount, pll.price_override*pll.quantity) --line_amt
3452       ,pll.quantity --trx_line_quantity
3453       ,pl.item_id --product_id
3454        -- Bug 5335818. Pass in inventory_organization_id
3455       ,(SELECT fsp.inventory_organization_id --product_org_id
3456         FROM financials_system_params_all fsp
3457         WHERE fsp.org_id=pll.org_id)
3458       ,zxldet.product_fisc_classification --product_fisc_classification
3459       ,(SELECT mum.uom_code FROM mtl_units_of_measure mum
3460         WHERE mum.unit_of_measure=pll.unit_meas_lookup_code) --uom_code
3461       ,zxldet.product_type --product_type
3462       ,msib.segment1 --product_code
3463       ,zxldet.product_category --product_category
3464       ,ph.fob_lookup_code --fob_point
3465       ,pov.party_id --ship_from_party_id
3466       ,pov.party_id --bill_from_party_id
3467       ,pvs.party_site_id --ship_from_party_site_id
3468       ,pvs.party_site_id --bill_from_party_site_id
3469       ,pll.ship_to_location_id --ship_to_location_id
3470       ,(SELECT hzps.location_id --ship_from_location_id
3471         FROM hz_party_sites hzps
3472         WHERE hzps.party_site_id = pvs.party_site_id)
3473       ,ph.bill_to_location_id --bill_to_location_id
3474       ,(SELECT pvs.location_id from po_vendor_sites_all pvs   /* 6524317 - Passing Location Id as Bill From Location Id */
3475         WHERE pvs.vendor_site_id=ph.vendor_site_id)
3476       -- Get account id from first distribution, if created
3477       -- otherwise from the expense account of the item
3478       ,NVL((SELECT pd.code_combination_id --account_ccid
3479             FROM po_distributions_all pd
3480             WHERE pd.line_location_id = pll.line_location_id
3481             AND rownum = 1),
3482            msib.expense_account
3483           )
3484       -- If scheduled release, pass Planned PO as a reference
3485       ,DECODE(pr.release_type, --ref_doc_application_id
3486               PO_CONSTANTS_SV.SCHEDULED, PO_CONSTANTS_SV.APPLICATION_ID,
3487               null)
3488       ,DECODE(pr.release_type, --ref_doc_entity_code
3489               PO_CONSTANTS_SV.SCHEDULED, PO_CONSTANTS_SV.PO_ENTITY_CODE,
3490               null)
3491       ,DECODE(pr.release_type, --ref_doc_event_class_code
3492               PO_CONSTANTS_SV.SCHEDULED, PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE,
3493               null)
3494       ,DECODE(pr.release_type, --ref_doc_trx_id
3495               PO_CONSTANTS_SV.SCHEDULED, pr.po_header_id,
3496               null)
3497       ,DECODE(pr.release_type, --ref_doc_line_id
3498               PO_CONSTANTS_SV.SCHEDULED, pll.po_line_id,
3499               null)
3500       ,pl.line_num --line_trx_user_key1
3501       ,PO_LOCATIONS_S.get_location_code(pll.ship_to_location_id) --line_trx_user_key2
3502       ,DECODE(pll.payment_type, --trx_line_number
3503               'ADVANCE', null,
3504               'DELIVERY', null,
3505               pll.shipment_num)
3506       ,DECODE(pll.shipment_type, --trx_line_description
3507               'STANDARD', DECODE(pll.payment_type,
3508                                  null, pl.item_description, --non complex work Standard PO
3509                                  pll.description --complex work Standard PO
3510                                 ),
3511               pl.item_description -- for shipment_type='PLANNED'
3512              )
3513       ,DECODE(pll.shipment_type, --product_description
3514               'STANDARD', DECODE(pll.payment_type,
3515                                  null, pl.item_description, --non complex work Standard PO
3516                                  pll.description --complex work Standard PO
3517                                 ),
3518               pl.item_description -- for shipment_type='PLANNED'
3519              )
3520       ,zxldet.assessable_value --assessable_value
3521       ,'N' --line_amt_includes_tax_flag
3522       -- Releases cannot be created through PDOI
3523       ,zxldet.input_tax_classification_code --input_tax_classification_code
3524       -- Bug 7337548
3525       -- Copying requisition information as Source Document Information
3526       -- in case of Autocreate. This information will be used to copy
3527       -- Tax attributes when the attributes are overridden in requisition.
3528       -- Calling program for autocreate is 'POXBWVRP_PO','AUTOCREATED_DOC_WF'
3529       -- and 'PORELGEB'
3530       ,DECODE(p_calling_program, --source_application_id
3531               'POXBWVRP_PO', PO_CONSTANTS_SV.APPLICATION_ID,
3532               'AUTOCREATED_DOC_WF', PO_CONSTANTS_SV.APPLICATION_ID,
3533               'PORELGEB', PO_CONSTANTS_SV.APPLICATION_ID,
3534               null)
3535       ,DECODE(p_calling_program, --source_entity_code
3536               'POXBWVRP_PO', PO_CONSTANTS_SV.REQ_ENTITY_CODE,
3537               'AUTOCREATED_DOC_WF', PO_CONSTANTS_SV.REQ_ENTITY_CODE,
3538               'PORELGEB', PO_CONSTANTS_SV.REQ_ENTITY_CODE,
3539               null)
3540       ,DECODE(p_calling_program, --source_event_class_code
3541               'POXBWVRP_PO', PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE,
3542               'AUTOCREATED_DOC_WF', PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE,
3543               'PORELGEB', PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE,
3544               null)
3545       ,DECODE(p_calling_program, -- source_trx_id
3546               'POXBWVRP_PO',  (SELECT requisition_header_id FROM
3547                                 po_requisition_lines_all
3548                                 WHERE  requisition_line_id IN (SELECT Min(requisition_line_id)    --source_trx_id
3549                                                                FROM po_requisition_lines_all prl
3550                                                                WHERE prl.line_location_id = pll.line_location_id)
3551                                 AND p_calling_program = 'POXBWVRP_PO'),
3552  	      'AUTOCREATED_DOC_WF',  (SELECT requisition_header_id FROM
3553                                 po_requisition_lines_all
3554                                 WHERE  requisition_line_id IN (SELECT Min(requisition_line_id)    --source_trx_id
3555                                                                FROM po_requisition_lines_all prl
3556                                                                WHERE prl.line_location_id = pll.line_location_id)
3557                                 AND p_calling_program = 'AUTOCREATED_DOC_WF'),
3558  	       'PORELGEB',  (SELECT requisition_header_id FROM
3559                                 po_requisition_lines_all
3560                                 WHERE  requisition_line_id IN (SELECT Min(requisition_line_id)    --source_trx_id
3561                                                                FROM po_requisition_lines_all prl
3562                                                                WHERE prl.line_location_id = pll.line_location_id)
3563                                 AND p_calling_program = 'PORELGEB'),
3564               null)
3565       ,DECODE(p_calling_program, --source_line_id
3566               'POXBWVRP_PO',(SELECT Min(requisition_line_id)    --source_line_id
3567                              FROM po_requisition_lines_all prl
3568                              WHERE prl.line_location_id = pll.line_location_id
3569                              AND p_calling_program = 'POXBWVRP_PO'),
3570               'AUTOCREATED_DOC_WF',(SELECT Min(requisition_line_id)    --source_line_id
3571                              FROM po_requisition_lines_all prl
3572                              WHERE prl.line_location_id = pll.line_location_id
3573                              AND p_calling_program = 'AUTOCREATED_DOC_WF'),
3574               'PORELGEB',(SELECT Min(requisition_line_id)    --source_line_id
3575                              FROM po_requisition_lines_all prl
3576                              WHERE prl.line_location_id = pll.line_location_id
3577                              AND p_calling_program = 'PORELGEB'),
3578 			 null)
3579       ,DECODE(p_calling_program, --source_trx_level_type
3580               'POXBWVRP_PO', PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE ,
3581               'AUTOCREATED_DOC_WF', PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE ,
3582               'PORELGEB', PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE ,
3583               null)
3584       ,pll.price_override --unit_price
3585       ,DECODE(pr.release_type, --ref_doc_trx_level_type
3586               PO_CONSTANTS_SV.SCHEDULED, PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE,
3587               null)
3588       -- Bug 5025018. Updated tax attribute mappings
3589       ,DECODE(pr.release_type, --ref_doc_line_quantity
3590               PO_CONSTANTS_SV.SCHEDULED, pll.quantity,
3591               null)
3592       ,pov.vendor_id --ship_third_pty_acct_id
3593       ,pov.vendor_id --bill_third_pty_acct_id
3594       ,pvs.vendor_site_id --ship_third_pty_acct_site_id
3595       ,pvs.vendor_site_id --bill_third_pty_acct_site_id
3596       ,pll.ship_to_organization_id --ship_to_party_id
3597 	  ,pll.ship_to_organization_id  --Bug#6902111
3598     -- Using OUTER JOIN in FROM clause syntax here because (+) operator
3599     -- is not flexible enough to be used inside an OR condition
3600     FROM po_releases_all pr
3601          ,po_headers_all ph
3602            -- Join with vendor tables to get party and party site information
3603            LEFT OUTER JOIN po_vendors pov ON (ph.vendor_id = pov.vendor_id)
3604            LEFT OUTER JOIN po_vendor_sites_all pvs
3605              ON (ph.vendor_site_id = pvs.vendor_site_id)
3606          ,po_lines_all pl--Blanket/Scheduled header and line
3607            -- Join with items table for item information
3608            LEFT OUTER JOIN mtl_system_items_b msib
3609              ON (pl.item_id = msib.inventory_item_id
3610                  AND pl.org_id = msib.organization_id)
3611          ,po_line_locations_all pll
3612            -- Conditions for getting Additional Tax Attributes
3613            --  Do not put a condition on zxldet.trx_id here because that would
3614            --  entail bringing the source_header_id for the shipment being
3615            --  currently processed. Join with trx_line_id itself is unique
3616            --  because the document type has been classified with
3617            --  event_class_code and line_location_id will always be unique
3618            --  whether PO shipment or Release shipment
3619            LEFT OUTER JOIN zx_lines_det_factors zxldet ON
3620             ((PO_CONSTANTS_SV.APPLICATION_ID = zxldet.application_id
3621               AND PO_CONSTANTS_SV.REL_ENTITY_CODE = zxldet.entity_code
3622               AND PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE = zxldet.event_class_code
3623               AND PO_CONSTANTS_SV.REL_TRX_LEVEL_TYPE = zxldet.trx_level_type
3624               AND pll.original_shipment_id = zxldet.trx_line_id
3625               AND pll.original_shipment_id IS NOT NULL)
3626              OR
3627              (PO_CONSTANTS_SV.APPLICATION_ID = zxldet.application_id
3628               AND PO_CONSTANTS_SV.PO_ENTITY_CODE = zxldet.entity_code
3629               AND PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE = zxldet.event_class_code
3630               AND PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE = zxldet.trx_level_type
3631               AND pll.source_shipment_id = zxldet.trx_line_id
3632               AND pll.shipment_type = PO_CONSTANTS_SV.SCHEDULED
3633               AND pll.tax_attribute_update_code = 'CREATE'
3634               AND pll.original_shipment_id IS NULL)
3635             )
3636     WHERE pr.po_release_id = pll.po_release_id
3637     AND pll.tax_attribute_update_code IS NOT NULL
3638     AND pll.tax_attribute_update_code <> 'DIST_DELETE'
3639         -- Bug 11665348
3640          AND Nvl(pll.quantity_billed,0)=0
3641          AND Nvl(pll.quantity_received,0)=0
3642          AND Nvl(pll.quantity_shipped,0)=0
3643          AND Nvl(pll.amount_billed,0)=0
3644          AND Nvl(pll.amount_received,0)=0
3645          AND Nvl(pll.amount_shipped,0)=0
3646      -- end bug 11665348
3647 
3648     AND pr.po_release_id = p_po_release_id_tbl(i)
3649     AND ph.po_header_id = pr.po_header_id
3650     AND pl.po_line_id = pll.po_line_id;
3651 
3652 END populate_zx_lines_with_rel;
3653 
3654 
3655 -----------------------------------------------------------------------------
3656 
3657 /* Bug 11665348 : Creating 2 new procedures populate_zx_lines_with_po_cal and
3658    populate_zx_lines_with_rel_cal which populates the data in zx_transaction_lines_gt
3659    only when the shipments is received or billed or shipped. This has been done for these
3660    shipments to skip the call to get_default_tax_attribs as per the EB Tax suggestion in bug
3661    11665348 to fulfill requirement by PO PM team to retain the tax on received/billed shipments.
3662    These 2 procedures uses a join for zx_lines_det_factors and po_line_locations_all
3663    ON line_location_id instead of original_shipment_id as we need to pass value of
3664    input_tax_classification_code for receioved/billed PO shipments as per the suggestion
3665    of EB Tax team in bug 11711366
3666 */
3667 
3668 -----------------------------------------------------------------------------
3669 --Start of Comments
3670 --Name: populate_zx_lines_with_po_cal
3671 --Pre-reqs:
3672 --  None
3673 --Modifies:
3674 --  ZX_TRANSACTION_LINES_GT
3675 --Locks:
3676 --  ZX_TRANSACTION_LINES_GT
3677 --Function:
3678 --  Populate ZX_TRANSACTION_LINES_GT with transaction line and shipment data
3679 --  which are not already populated by populate_zx_lines_with_po
3680 --Parameters:
3681 --IN:
3682 --p_po_header_id_tbl
3683 --  PL/SQL table with list of po_header_id's to process for tax_calculation
3684 --p_calling_program
3685 --  Identifies the module that calls this procedure eg. 'PDOI'
3686 --Notes:
3687 --  Used by calculate_tax procedure. Not to be used externally
3688 --End of Comments
3689 -----------------------------------------------------------------------------
3690 PROCEDURE populate_zx_lines_with_po_cal(p_po_header_id_tbl  IN  PO_TBL_NUMBER,
3691                                     p_calling_program   IN  VARCHAR2
3692 ) IS
3693 BEGIN
3694 
3695     -- Populate zx_transaction_lines_gt for POs
3696   FORALL i in 1..p_po_header_id_tbl.COUNT
3697     INSERT INTO zx_transaction_lines_gt(
3698       application_id
3699       ,entity_code
3700       ,event_class_code
3701       ,trx_id
3702       ,trx_level_type
3703       ,trx_line_id
3704       ,line_level_action
3705       ,line_class
3706       ,trx_line_type
3707       ,trx_line_date
3708       ,trx_business_category
3709       ,line_intended_use
3710       ,user_defined_fisc_class
3711       ,line_amt
3712       ,trx_line_quantity
3713       ,product_id
3714       ,product_org_id
3715       ,product_fisc_classification
3716       ,uom_code
3717       ,product_type
3718       ,product_code
3719       ,product_category
3720       ,fob_point
3721       ,ship_from_party_id
3722       ,bill_from_party_id
3723       ,ship_from_party_site_id
3724       ,bill_from_party_site_id
3725       ,ship_to_location_id
3726       ,ship_from_location_id
3727       ,bill_to_location_id
3728       ,bill_from_location_id /* 6524317 */
3729       ,account_ccid
3730       ,ref_doc_application_id
3731       ,ref_doc_entity_code
3732       ,ref_doc_event_class_code
3733       ,ref_doc_trx_id
3734       ,ref_doc_line_id
3735       ,line_trx_user_key1
3736       ,line_trx_user_key2
3737       -- Bug 5079867. Ordering of complex work payment lines in ATI page
3738       ,line_trx_user_key3
3739       ,trx_line_number
3740       ,trx_line_description
3741       ,product_description
3742       ,assessable_value
3743       ,line_amt_includes_tax_flag
3744       ,input_tax_classification_code
3745       ,source_application_id
3746       ,source_entity_code
3747       ,source_event_class_code
3748       ,source_trx_id
3749       ,source_line_id
3750       ,source_trx_level_type
3751       ,unit_price
3752       ,ref_doc_trx_level_type
3753       -- Bug 5025018. Updated tax attribute mappings
3754       ,ship_third_pty_acct_id
3755       ,bill_third_pty_acct_id
3756       ,ship_third_pty_acct_site_id
3757       ,bill_third_pty_acct_site_id
3758       ,ship_to_party_id
3759       ,user_upd_det_factors_flag --Bug 5632300
3760       ,defaulting_attribute1 --Bug#6902111
3761     )
3762     SELECT
3763       PO_CONSTANTS_SV.APPLICATION_ID --application_id
3764       ,PO_CONSTANTS_SV.PO_ENTITY_CODE --entity_code
3765       ,PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE --event_class_code
3766       ,ph.po_header_id --trx_id
3767       ,PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE --trx_level_type
3768       ,pll.line_location_id --trx_line_id
3769       ,pll.tax_attribute_update_code --line_level_action
3770       ,DECODE(pll.shipment_type,--line_class
3771               'PREPAYMENT', DECODE(pll.payment_type,
3772                                    'ADVANCE', 'ADVANCE',
3773                                    'FINANCING'),
3774               DECODE(pll.value_basis,
3775                      'QUANTITY', 'INVOICE',
3776                      'AMOUNT')
3777              )
3778       ,'ITEM' --trx_line_type
3779       ,COALESCE(pll.need_by_date, pll.promised_date, sysdate) --trx_line_date
3780       ,DECODE(p_calling_program, --trx_business_category
3781               'COPY_DOCUMENT', null,
3782               zxldet.trx_business_category)
3783       ,DECODE(p_calling_program, --line_intended_use
3784               'COPY_DOCUMENT', null,
3785               zxldet.line_intended_use)
3786       ,DECODE(p_calling_program, --user_defined_fisc_class
3787               'COPY_DOCUMENT', null,
3788               zxldet.user_defined_fisc_class)
3789       ,NVL(pll.amount, pll.price_override*pll.quantity) --line_amt
3790       ,pll.quantity --trx_line_quantity
3791       ,pl.item_id --product_id
3792        -- Bug 5335818. Pass in inventory_organization_id
3793       ,(SELECT fsp.inventory_organization_id --product_org_id
3794         FROM financials_system_params_all fsp
3795         WHERE fsp.org_id=pll.org_id)
3796       ,DECODE(p_calling_program, --product_fisc_classification
3797               'COPY_DOCUMENT', null,
3798               zxldet.product_fisc_classification)
3799       ,(SELECT mum.uom_code FROM mtl_units_of_measure mum
3800         WHERE mum.unit_of_measure=pll.unit_meas_lookup_code) --uom_code
3801       ,DECODE(p_calling_program, --product_type
3802               'COPY_DOCUMENT', null,
3803               zxldet.product_type)
3804       ,msib.segment1 --product_code
3805       ,DECODE(p_calling_program, --product_category
3806               'COPY_DOCUMENT', null,
3807               zxldet.product_category)
3808       ,ph.fob_lookup_code --fob_point
3809       ,pv.party_id --ship_from_party_id
3810       ,pv.party_id --bill_from_party_id
3811       ,pvs.party_site_id --ship_from_party_site_id
3812       ,pvs.party_site_id --bill_from_party_site_id
3813       ,pll.ship_to_location_id --ship_to_location_id
3814       ,(SELECT hzps.location_id --ship_from_location_id
3815         FROM hz_party_sites hzps
3816         WHERE hzps.party_site_id = pvs.party_site_id)
3817       ,ph.bill_to_location_id --bill_to_location_id
3818       ,(SELECT pvs.location_id from po_vendor_sites_all pvs   /* 6524317 - Passing Location Id as Bill From Location Id */
3819         WHERE pvs.vendor_site_id=ph.vendor_site_id)
3820       -- Get account id from first distribution, if created
3821       -- otherwise from the expense account of the item
3822       ,NVL((SELECT pd.code_combination_id --account_ccid
3823             FROM po_distributions_all pd
3824             WHERE pd.line_location_id = pll.line_location_id
3825             AND rownum = 1),
3826            msib.expense_account
3827           )
3828       ,null --ref_doc_application_id
3829       ,null --ref_doc_entity_code
3830       ,null --ref_doc_event_class_code
3831       ,null --ref_doc_trx_id
3832       ,null --ref_doc_line_id
3833       ,pl.line_num --line_trx_user_key1
3834       ,PO_LOCATIONS_S.get_location_code(pll.ship_to_location_id) --line_trx_user_key2
3835       -- Bug 5079867. Ordering of complex work payment lines in ATI page
3836       ,DECODE(pll.payment_type, null, 0, --line_trx_user_key3
3837                 'DELIVERY', 1,
3838                 'ADVANCE', 2, 3)
3839       ,DECODE(pll.payment_type, --trx_line_number
3840               'ADVANCE', null,
3841               'DELIVERY', null,
3842               pll.shipment_num)
3843       ,DECODE(pll.shipment_type, --trx_line_description
3844               'STANDARD', DECODE(pll.payment_type,
3845                                  null, pl.item_description, --non complex work Standard PO
3846                                  pll.description --complex work Standard PO
3847                                 ),
3848               pl.item_description -- for shipment_type='PLANNED'
3849              )
3850       ,DECODE(pll.shipment_type, --product_description
3851               'STANDARD', DECODE(pll.payment_type,
3852                                  null, pl.item_description, --non complex work Standard PO
3853                                  pll.description --complex work Standard PO
3854                                 ),
3855               pl.item_description -- for shipment_type='PLANNED'
3856              )
3857       ,DECODE(p_calling_program, --assessable_value
3858               'COPY_DOCUMENT', null,
3859               zxldet.assessable_value)
3860       ,'N' --line_amt_includes_tax_flag
3861       -- Only newly added lines can come in through PDOI (no updated
3862       -- lines), so tax_name can be populated to override.
3863       -- Once the new line has been created, if it is modified
3864       -- through any other program, tax_name field will be populated
3865       -- but will not get populated as overridden tax_classification
3866       ,DECODE(p_calling_program, --input_tax_classification_code
3867               'PDOI', pll.tax_name,
3868               'COPY_DOCUMENT', null,
3869               zxldet.input_tax_classification_code)
3870       -- Bug 7337548
3871       -- Copying requisition information as Source Document Information
3872       -- in case of Autocreate. This information will be used to copy
3873       -- Tax attributes when the attributes are overridden in requisition.
3874       -- Calling program for autocreate is 'POXBWVRP_PO','AUTOCREATED_DOC_WF'
3875       -- and 'PORELGEB'
3876       ,DECODE(p_calling_program, --source_application_id
3877               'COPY_DOCUMENT', PO_CONSTANTS_SV.APPLICATION_ID,
3878               'POXBWVRP_PO', PO_CONSTANTS_SV.APPLICATION_ID,
3879               'AUTOCREATED_DOC_WF' ,PO_CONSTANTS_SV.APPLICATION_ID,
3880               'PORELGEB',PO_CONSTANTS_SV.APPLICATION_ID,
3881               null)
3882       ,DECODE(p_calling_program, --source_entity_code
3883               'COPY_DOCUMENT', PO_CONSTANTS_SV.PO_ENTITY_CODE,
3884               'POXBWVRP_PO', PO_CONSTANTS_SV.REQ_ENTITY_CODE,
3885               'AUTOCREATED_DOC_WF', PO_CONSTANTS_SV.REQ_ENTITY_CODE,
3886               'PORELGEB', PO_CONSTANTS_SV.REQ_ENTITY_CODE,
3887               null)
3888       ,DECODE(p_calling_program, --source_event_class_code
3889               'COPY_DOCUMENT', PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE,
3890               'POXBWVRP_PO', PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE,
3891               'AUTOCREATED_DOC_WF', PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE,
3892               'PORELGEB', PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE,
3893               null)
3894       ,DECODE(p_calling_program, -- source_trx_id
3895               'COPY_DOCUMENT', (SELECT source_shipment.po_header_id --source_trx_id
3896                                 FROM po_line_locations_all source_shipment
3897                                 WHERE source_shipment.line_location_id = pll.original_shipment_id
3898                                 AND p_calling_program = 'COPY_DOCUMENT'),
3899 
3900               'POXBWVRP_PO',  (SELECT requisition_header_id FROM
3901                                 po_requisition_lines_all
3902                                 WHERE  requisition_line_id IN (SELECT Min(requisition_line_id)    --source_trx_id
3903                                                                FROM po_requisition_lines_all prl
3904                                                                WHERE prl.line_location_id = pll.line_location_id)
3905                                 AND p_calling_program = 'POXBWVRP_PO'),
3906               'AUTOCREATED_DOC_WF',  (SELECT requisition_header_id FROM
3907                                 po_requisition_lines_all
3908                                 WHERE  requisition_line_id IN (SELECT Min(requisition_line_id)    --source_trx_id
3909                                                                FROM po_requisition_lines_all prl
3910                                                                WHERE prl.line_location_id = pll.line_location_id)
3911                                 AND p_calling_program = 'AUTOCREATED_DOC_WF'),
3912               'PORELGEB',      (SELECT requisition_header_id FROM
3913                                 po_requisition_lines_all
3914                                 WHERE  requisition_line_id IN (SELECT Min(requisition_line_id)    --source_trx_id
3915                                                                FROM po_requisition_lines_all prl
3916                                                                WHERE prl.line_location_id = pll.line_location_id)
3917                                 AND p_calling_program = 'PORELGEB'),
3918               null)
3919       ,DECODE(p_calling_program, --source_line_id
3920               'COPY_DOCUMENT', pll.original_shipment_id,
3921               'POXBWVRP_PO',(SELECT Min(requisition_line_id)    --source_line_id
3922                              FROM po_requisition_lines_all prl
3923                              WHERE prl.line_location_id = pll.line_location_id
3924                              AND p_calling_program = 'POXBWVRP_PO'),
3925               'AUTOCREATED_DOC_WF',(SELECT Min(requisition_line_id)    --source_line_id
3926                              FROM po_requisition_lines_all prl
3927                              WHERE prl.line_location_id = pll.line_location_id
3928                              AND p_calling_program = 'AUTOCREATED_DOC_WF'),
3929               'PORELGEB',(SELECT Min(requisition_line_id)    --source_line_id
3930                              FROM po_requisition_lines_all prl
3931                              WHERE prl.line_location_id = pll.line_location_id
3932                              AND p_calling_program = 'PORELGEB'),
3933               null)
3934       ,DECODE(p_calling_program, --source_trx_level_type
3935               'COPY_DOCUMENT', PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE,
3936               'POXBWVRP_PO', PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE ,
3937 	      'AUTOCREATED_DOC_WF', PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE ,
3938               'PORELGEB', PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE ,
3939               null)
3940       ,pll.price_override --unit_price
3941       ,null --ref_doc_trx_level_type
3942       -- Bug 5025018. Updated tax attribute mappings
3943       ,pv.vendor_id --ship_third_pty_acct_id
3944       ,pv.vendor_id --bill_third_pty_acct_id
3945       ,pvs.vendor_site_id --ship_third_pty_acct_site_id
3946       ,pvs.vendor_site_id --bill_third_pty_acct_site_id
3947       ,pll.ship_to_organization_id --ship_to_party_id
3948       --Bug 5632300. Parameter to confirm that tax classification is overridden
3949       ,(SELECT 'Y' FROM DUAL --user_upd_det_factors_flag
3950         WHERE p_calling_program = 'PDOI'
3951         AND pll.tax_name IS NOT NULL)
3952       ,pll.ship_to_organization_id  --Bug#6902111
3953     FROM po_headers_all ph, po_lines_all pl, po_line_locations_all pll,
3954          zx_lines_det_factors zxldet, po_vendors pv,
3955          po_vendor_sites_all pvs, mtl_system_items_b msib
3956     WHERE ph.po_header_id = pll.po_header_id
3957     AND pl.po_line_id = pll.po_line_id
3958       ---Bug# 13091627 : Condition added to exclude the Release shipments for BLANKET/PLANNED PO
3959     AND pll.po_release_id IS NULL
3960     AND pll.tax_attribute_update_code IS NOT NULL
3961     AND pll.tax_attribute_update_code <> 'DIST_DELETE'
3962         -- Bug 11665348
3963     AND pll.line_location_id NOT IN (SELECT trx_line_id
3964                                      FROM zx_transaction_lines_gt)
3965      -- end bug 11665348
3966     AND ph.po_header_id = p_po_header_id_tbl(i)
3967     -- Conditions for getting Additional Tax Attributes
3968     --  Do not put a condition on zxldet.trx_id here because that would
3969     --  entail bringing the source_header_id for the shipment being currently
3970     --  processed. Join with trx_line_id itself is unique because the document
3971     --  type has been classified with event_class_code and line_location_id
3972     --  will always be unique whether PO shipment or Release shipment
3973     AND zxldet.application_id(+) = PO_CONSTANTS_SV.APPLICATION_ID
3974     AND zxldet.entity_code(+) = PO_CONSTANTS_SV.PO_ENTITY_CODE
3975     AND zxldet.event_class_code(+) = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE
3976     AND zxldet.trx_level_type(+) = PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE
3977     AND zxldet.trx_line_id(+) = pll.line_location_id
3978 	AND zxldet.trx_id(+) = p_po_header_id_tbl(i) --bug:14028744
3979    -- Bug 11665348: For received/billed PO shipments.
3980     -- Join with vendor tables to get party and party site information
3981     AND pv.vendor_id(+) = ph.vendor_id
3982     AND pvs.vendor_site_id(+) = ph.vendor_site_id
3983     -- Join with items table for item information
3984     AND msib.inventory_item_id(+) = pl.item_id
3985     AND msib.organization_id(+) = pl.org_id;
3986     -- Conditions that determine that po line is 'Active'
3987     --AND nvl(pl.cancel_flag,'N') = 'N'
3988     --AND nvl(pl.closed_code,'OPEN') <> 'FINALLY CLOSED'
3989     --AND nvl(pll.cancel_flag,'N') = 'N'
3990     --AND nvl(pll.closed_code,'OPEN') <> 'FINALLY CLOSED';
3991 
3992 END populate_zx_lines_with_po_cal;
3993 
3994 
3995 -----------------------------------------------------------------------------
3996 --Start of Comments
3997 --Name: populate_zx_lines_with_rel_cal
3998 --Pre-reqs:
3999 --  None
4000 --Modifies:
4001 --  ZX_TRANSACTION_LINES_GT
4002 --Locks:
4003 --  ZX_TRANSACTION_LINES_GT
4004 --Function:
4005 --  Populate ZX_TRANSACTION_LINES_GT with transaction line and shipment data
4006 --  which are not already populated by populate_zx_lines_with_rel.
4007 --Parameters:
4008 --IN:
4009 --p_po_release_id_tbl
4010 --  PL/SQL table with list of po_release_id's to process for tax_calculation
4011 --p_calling_program
4012 --  Identifies the module that calls this procedure eg. 'PDOI'
4013 --Notes:
4014 --  Used by calculate_tax procedure. Not to be used externally
4015 --End of Comments
4016 -----------------------------------------------------------------------------
4017 PROCEDURE populate_zx_lines_with_rel_cal(p_po_release_id_tbl  IN  PO_TBL_NUMBER,
4018                                      p_calling_program    IN  VARCHAR2
4019 ) IS
4020 BEGIN
4021   -- Populate zx_transaction_lines_gt for Releases
4022   FORALL i IN 1..p_po_release_id_tbl.COUNT
4023     INSERT INTO zx_transaction_lines_gt(
4024       application_id
4025       ,entity_code
4026       ,event_class_code
4027       ,trx_id
4028       ,trx_level_type
4029       ,trx_line_id
4030       ,line_level_action
4031       ,line_class
4032       ,trx_line_type
4033       ,trx_line_date
4034       ,trx_business_category
4035       ,line_intended_use
4036       ,user_defined_fisc_class
4037       ,line_amt
4038       ,trx_line_quantity
4039       ,product_id
4040       ,product_org_id
4041       ,product_fisc_classification
4042       ,uom_code
4043       ,product_type
4044       ,product_code
4045       ,product_category
4046       ,fob_point
4047       ,ship_from_party_id
4048       ,bill_from_party_id
4049       ,ship_from_party_site_id
4050       ,bill_from_party_site_id
4051       ,ship_to_location_id
4052       ,ship_from_location_id
4053       ,bill_to_location_id
4054       ,bill_from_location_id   /* 6524317 */
4055       ,account_ccid
4056       ,ref_doc_application_id
4057       ,ref_doc_entity_code
4058       ,ref_doc_event_class_code
4059       ,ref_doc_trx_id
4060       ,ref_doc_line_id
4061       ,line_trx_user_key1
4062       ,line_trx_user_key2
4063       ,trx_line_number
4064       ,trx_line_description
4065       ,product_description
4066       ,assessable_value
4067       ,line_amt_includes_tax_flag
4068       ,input_tax_classification_code
4069       ,source_application_id
4070       ,source_entity_code
4071       ,source_event_class_code
4072       ,source_trx_id
4073       ,source_line_id
4074       ,source_trx_level_type
4075       ,unit_price
4076       ,ref_doc_trx_level_type
4077       -- Bug 5025018. Updated tax attribute mappings
4078       ,ref_doc_line_quantity
4079       ,ship_third_pty_acct_id
4080       ,bill_third_pty_acct_id
4081       ,ship_third_pty_acct_site_id
4082       ,bill_third_pty_acct_site_id
4083       ,ship_to_party_id
4084       ,defaulting_attribute1 --Bug#6902111
4085     )
4086     SELECT
4087       PO_CONSTANTS_SV.APPLICATION_ID --application_id
4088       ,PO_CONSTANTS_SV.REL_ENTITY_CODE --entity_code
4089       ,PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE --event_class_code
4090       ,pr.po_release_id --trx_id
4091       ,PO_CONSTANTS_SV.REL_TRX_LEVEL_TYPE --trx_level_type
4092       ,pll.line_location_id --trx_line_id
4093       ,pll.tax_attribute_update_code --line_level_action
4094       ,DECODE(pll.shipment_type,--line_class
4095               'PREPAYMENT', DECODE(pll.payment_type,
4096                                    'ADVANCE', 'ADVANCE',
4097                                    'FINANCING'),
4098               DECODE(pll.value_basis,
4099                      'QUANTITY', 'INVOICE',
4100                      'AMOUNT')
4101              )
4102       ,'ITEM' --trx_line_type
4103       ,COALESCE(pll.need_by_date, pll.promised_date, sysdate) --trx_line_date
4104       ,zxldet.trx_business_category --trx_business_category
4105       ,zxldet.line_intended_use --line_intended_use
4106       ,zxldet.user_defined_fisc_class --user_defined_fisc_class
4107       ,nvl(pll.amount, pll.price_override*pll.quantity) --line_amt
4108       ,pll.quantity --trx_line_quantity
4109       ,pl.item_id --product_id
4110        -- Bug 5335818. Pass in inventory_organization_id
4111       ,(SELECT fsp.inventory_organization_id --product_org_id
4112         FROM financials_system_params_all fsp
4113         WHERE fsp.org_id=pll.org_id)
4114       ,zxldet.product_fisc_classification --product_fisc_classification
4115       ,(SELECT mum.uom_code FROM mtl_units_of_measure mum
4116         WHERE mum.unit_of_measure=pll.unit_meas_lookup_code) --uom_code
4117       ,zxldet.product_type --product_type
4118       ,msib.segment1 --product_code
4119       ,zxldet.product_category --product_category
4120       ,ph.fob_lookup_code --fob_point
4121       ,pov.party_id --ship_from_party_id
4122       ,pov.party_id --bill_from_party_id
4123       ,pvs.party_site_id --ship_from_party_site_id
4124       ,pvs.party_site_id --bill_from_party_site_id
4125       ,pll.ship_to_location_id --ship_to_location_id
4126       ,(SELECT hzps.location_id --ship_from_location_id
4127         FROM hz_party_sites hzps
4128         WHERE hzps.party_site_id = pvs.party_site_id)
4129       ,ph.bill_to_location_id --bill_to_location_id
4130       ,(SELECT pvs.location_id from po_vendor_sites_all pvs   /* 6524317 - Passing Location Id as Bill From Location Id */
4131         WHERE pvs.vendor_site_id=ph.vendor_site_id)
4132       -- Get account id from first distribution, if created
4133       -- otherwise from the expense account of the item
4134       ,NVL((SELECT pd.code_combination_id --account_ccid
4135             FROM po_distributions_all pd
4136             WHERE pd.line_location_id = pll.line_location_id
4137             AND rownum = 1),
4138            msib.expense_account
4139           )
4140       -- If scheduled release, pass Planned PO as a reference
4141       ,DECODE(pr.release_type, --ref_doc_application_id
4142               PO_CONSTANTS_SV.SCHEDULED, PO_CONSTANTS_SV.APPLICATION_ID,
4143               null)
4144       ,DECODE(pr.release_type, --ref_doc_entity_code
4145               PO_CONSTANTS_SV.SCHEDULED, PO_CONSTANTS_SV.PO_ENTITY_CODE,
4146               null)
4147       ,DECODE(pr.release_type, --ref_doc_event_class_code
4148               PO_CONSTANTS_SV.SCHEDULED, PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE,
4149               null)
4150       ,DECODE(pr.release_type, --ref_doc_trx_id
4151               PO_CONSTANTS_SV.SCHEDULED, pr.po_header_id,
4152               null)
4153       ,DECODE(pr.release_type, --ref_doc_line_id
4154               PO_CONSTANTS_SV.SCHEDULED, pll.po_line_id,
4155               null)
4156       ,pl.line_num --line_trx_user_key1
4157       ,PO_LOCATIONS_S.get_location_code(pll.ship_to_location_id) --line_trx_user_key2
4158       ,DECODE(pll.payment_type, --trx_line_number
4159               'ADVANCE', null,
4160               'DELIVERY', null,
4161               pll.shipment_num)
4162       ,DECODE(pll.shipment_type, --trx_line_description
4163               'STANDARD', DECODE(pll.payment_type,
4164                                  null, pl.item_description, --non complex work Standard PO
4165                                  pll.description --complex work Standard PO
4166                                 ),
4167               pl.item_description -- for shipment_type='PLANNED'
4168              )
4169       ,DECODE(pll.shipment_type, --product_description
4170               'STANDARD', DECODE(pll.payment_type,
4171                                  null, pl.item_description, --non complex work Standard PO
4172                                  pll.description --complex work Standard PO
4173                                 ),
4174               pl.item_description -- for shipment_type='PLANNED'
4175              )
4176       ,zxldet.assessable_value --assessable_value
4177       ,'N' --line_amt_includes_tax_flag
4178       -- Releases cannot be created through PDOI
4179       ,zxldet.input_tax_classification_code --input_tax_classification_code
4180       -- Bug 7337548
4181       -- Copying requisition information as Source Document Information
4182       -- in case of Autocreate. This information will be used to copy
4183       -- Tax attributes when the attributes are overridden in requisition.
4184       -- Calling program for autocreate is 'POXBWVRP_PO','AUTOCREATED_DOC_WF'
4185       -- and 'PORELGEB'
4186       ,DECODE(p_calling_program, --source_application_id
4187               'POXBWVRP_PO', PO_CONSTANTS_SV.APPLICATION_ID,
4188               'AUTOCREATED_DOC_WF', PO_CONSTANTS_SV.APPLICATION_ID,
4189               'PORELGEB', PO_CONSTANTS_SV.APPLICATION_ID,
4190               null)
4191       ,DECODE(p_calling_program, --source_entity_code
4192               'POXBWVRP_PO', PO_CONSTANTS_SV.REQ_ENTITY_CODE,
4193               'AUTOCREATED_DOC_WF', PO_CONSTANTS_SV.REQ_ENTITY_CODE,
4194               'PORELGEB', PO_CONSTANTS_SV.REQ_ENTITY_CODE,
4195               null)
4196       ,DECODE(p_calling_program, --source_event_class_code
4197               'POXBWVRP_PO', PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE,
4198               'AUTOCREATED_DOC_WF', PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE,
4199               'PORELGEB', PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE,
4200               null)
4201       ,DECODE(p_calling_program, -- source_trx_id
4202               'POXBWVRP_PO',  (SELECT requisition_header_id FROM
4203                                 po_requisition_lines_all
4204                                 WHERE  requisition_line_id IN (SELECT Min(requisition_line_id)    --source_trx_id
4205                                                                FROM po_requisition_lines_all prl
4206                                                                WHERE prl.line_location_id = pll.line_location_id)
4207                                 AND p_calling_program = 'POXBWVRP_PO'),
4208  	      'AUTOCREATED_DOC_WF',  (SELECT requisition_header_id FROM
4209                                 po_requisition_lines_all
4210                                 WHERE  requisition_line_id IN (SELECT Min(requisition_line_id)    --source_trx_id
4211                                                                FROM po_requisition_lines_all prl
4212                                                                WHERE prl.line_location_id = pll.line_location_id)
4213                                 AND p_calling_program = 'AUTOCREATED_DOC_WF'),
4214  	       'PORELGEB',  (SELECT requisition_header_id FROM
4215                                 po_requisition_lines_all
4216                                 WHERE  requisition_line_id IN (SELECT Min(requisition_line_id)    --source_trx_id
4217                                                                FROM po_requisition_lines_all prl
4218                                                                WHERE prl.line_location_id = pll.line_location_id)
4219                                 AND p_calling_program = 'PORELGEB'),
4220               null)
4221       ,DECODE(p_calling_program, --source_line_id
4222               'POXBWVRP_PO',(SELECT Min(requisition_line_id)    --source_line_id
4223                              FROM po_requisition_lines_all prl
4224                              WHERE prl.line_location_id = pll.line_location_id
4225                              AND p_calling_program = 'POXBWVRP_PO'),
4226               'AUTOCREATED_DOC_WF',(SELECT Min(requisition_line_id)    --source_line_id
4227                              FROM po_requisition_lines_all prl
4228                              WHERE prl.line_location_id = pll.line_location_id
4229                              AND p_calling_program = 'AUTOCREATED_DOC_WF'),
4230               'PORELGEB',(SELECT Min(requisition_line_id)    --source_line_id
4231                              FROM po_requisition_lines_all prl
4232                              WHERE prl.line_location_id = pll.line_location_id
4233                              AND p_calling_program = 'PORELGEB'),
4234 			 null)
4235       ,DECODE(p_calling_program, --source_trx_level_type
4236               'POXBWVRP_PO', PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE ,
4237               'AUTOCREATED_DOC_WF', PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE ,
4238               'PORELGEB', PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE ,
4239               null)
4240       ,pll.price_override --unit_price
4241       ,DECODE(pr.release_type, --ref_doc_trx_level_type
4242               PO_CONSTANTS_SV.SCHEDULED, PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE,
4243               null)
4244       -- Bug 5025018. Updated tax attribute mappings
4245       ,DECODE(pr.release_type, --ref_doc_line_quantity
4246               PO_CONSTANTS_SV.SCHEDULED, pll.quantity,
4247               null)
4248       ,pov.vendor_id --ship_third_pty_acct_id
4249       ,pov.vendor_id --bill_third_pty_acct_id
4250       ,pvs.vendor_site_id --ship_third_pty_acct_site_id
4251       ,pvs.vendor_site_id --bill_third_pty_acct_site_id
4252       ,pll.ship_to_organization_id --ship_to_party_id
4253 	  ,pll.ship_to_organization_id  --Bug#6902111
4254     -- Using OUTER JOIN in FROM clause syntax here because (+) operator
4255     -- is not flexible enough to be used inside an OR condition
4256     FROM po_releases_all pr
4257          ,po_headers_all ph
4258            -- Join with vendor tables to get party and party site information
4259            LEFT OUTER JOIN po_vendors pov ON (ph.vendor_id = pov.vendor_id)
4260            LEFT OUTER JOIN po_vendor_sites_all pvs
4261              ON (ph.vendor_site_id = pvs.vendor_site_id)
4262          ,po_lines_all pl--Blanket/Scheduled header and line
4263            -- Join with items table for item information
4264            LEFT OUTER JOIN mtl_system_items_b msib
4265              ON (pl.item_id = msib.inventory_item_id
4266                  AND pl.org_id = msib.organization_id)
4267          ,po_line_locations_all pll
4268           LEFT OUTER JOIN zx_lines_det_factors zxldet ON
4269           -- Bug#12622509: The performance of the SQL is very bad, as FULL TABLE
4270           --               SCAN is performed on the table zx_lines_det_factors.
4271           (PO_CONSTANTS_SV.APPLICATION_ID = zxldet.application_id
4272            AND PO_CONSTANTS_SV.REL_ENTITY_CODE = zxldet.entity_code
4273            AND PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE = zxldet.event_class_code
4274            AND PO_CONSTANTS_SV.REL_TRX_LEVEL_TYPE = zxldet.trx_level_type
4275            AND pll.line_location_id = zxldet.trx_line_id)
4276    -- Bug 11665348: For received/billed PO shipments
4277          /*  -- Conditions for getting Additional Tax Attributes
4278            --  Do not put a condition on zxldet.trx_id here because that would
4279            --  entail bringing the source_header_id for the shipment being
4280            --  currently processed. Join with trx_line_id itself is unique
4281            --  because the document type has been classified with
4282            --  event_class_code and line_location_id will always be unique
4283            --  whether PO shipment or Release shipment
4284            LEFT OUTER JOIN zx_lines_det_factors zxldet ON
4285             ((PO_CONSTANTS_SV.APPLICATION_ID = zxldet.application_id
4286               AND PO_CONSTANTS_SV.REL_ENTITY_CODE = zxldet.entity_code
4287               AND PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE = zxldet.event_class_code
4288               AND PO_CONSTANTS_SV.REL_TRX_LEVEL_TYPE = zxldet.trx_level_type
4289               AND pll.original_shipment_id = zxldet.trx_line_id
4290               AND pll.original_shipment_id IS NOT NULL)
4291              OR
4292              (PO_CONSTANTS_SV.APPLICATION_ID = zxldet.application_id
4293               AND PO_CONSTANTS_SV.PO_ENTITY_CODE = zxldet.entity_code
4294               AND PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE = zxldet.event_class_code
4295               AND PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE = zxldet.trx_level_type
4296               AND pll.source_shipment_id = zxldet.trx_line_id
4297               AND pll.shipment_type = PO_CONSTANTS_SV.SCHEDULED
4298               AND pll.tax_attribute_update_code = 'CREATE'
4299               AND pll.original_shipment_id IS NULL)
4300             )       */
4301     WHERE pr.po_release_id = pll.po_release_id
4302     AND pll.tax_attribute_update_code IS NOT NULL
4303     AND pll.tax_attribute_update_code <> 'DIST_DELETE'
4304         -- Bug 11665348
4305      AND pll.line_location_id NOT IN (SELECT trx_line_id
4306                                      FROM zx_transaction_lines_gt)
4307      -- end bug 11665348
4308     AND pr.po_release_id = p_po_release_id_tbl(i)
4309     AND ph.po_header_id = pr.po_header_id
4310     AND pl.po_line_id = pll.po_line_id;
4311 
4312 END populate_zx_lines_with_rel_cal;
4313 
4314 
4315 -----------------------------------------------------------------------------
4316 --End Bug 11665348
4317 
4318 -----------------------------------------------------------------------------
4319 
4320 --Start of Comments
4321 --Name: populate_zx_lines_with_req
4322 --Pre-reqs:
4323 --  None
4324 --Modifies:
4325 --  ZX_TRANSACTION_LINES_GT
4326 --Locks:
4327 --  ZX_TRANSACTION_LINES_GT
4328 --Function:
4329 --  Populate ZX_TRANSACTION_LINES_GT with transaction line data
4330 --Parameters:
4331 --IN:
4332 --p_requisition_header_id
4333 --  requisition_header_id of the requisition to process for tax_calculation
4334 --p_calling_program
4335 --  Identifies the module that calls this procedure eg. 'PDOI'
4336 --Notes:
4337 --  Used by calculate_tax_requisition procedure. Not to be used externally
4338 --End of Comments
4339 -----------------------------------------------------------------------------
4340 PROCEDURE populate_zx_lines_with_req(p_requisition_header_id  IN  NUMBER,
4341                                      p_calling_program        IN  VARCHAR2
4342 ) IS
4343   l_functional_currency_code PO_REQUISITION_LINES_ALL.currency_code%TYPE;
4344   l_set_of_books_id FINANCIALS_SYSTEM_PARAMS_ALL.set_of_books_id%TYPE;
4345   l_rate_type PO_REQUISITION_LINES_ALL.rate_type%TYPE;
4346   l_rate PO_REQUISITION_LINES_ALL.rate%TYPE;
4347 BEGIN
4348 
4349   -- <Bug 4742335 Start> Get the requisition's functional currency info.
4350   -- This piece of code is similar to PO_GA_PVT.get_currency_info except that
4351   -- this works for a requisition
4352   SELECT sob.currency_code, fsp.set_of_books_id, psp.default_rate_type
4353   INTO l_functional_currency_code, l_set_of_books_id, l_rate_type
4354   FROM financials_system_params_all fsp, gl_sets_of_books sob,
4355        po_requisition_headers_all prh, po_system_parameters_all psp
4356   WHERE fsp.set_of_books_id = sob.set_of_books_id
4357   AND fsp.org_id = prh.org_id
4358   AND prh.requisition_header_id = p_requisition_header_id
4359   AND psp.org_id = prh.org_id;
4360 
4361   -- Retrieve rate based on above values
4362   l_rate := PO_CORE_S.get_conversion_rate(
4363               x_set_of_books_id => l_set_of_books_id,
4364               x_from_currency   => l_functional_currency_code,
4365               x_conversion_date => sysdate,
4366               x_conversion_type => l_rate_type);
4367   -- <Bug 4742335 End>
4368 
4369   -- Populate zx_transaction_lines_gt for Requisitions
4370   -- Addition/Removal of any attribute entails similar change in
4371   -- populate_zx_record as well
4372   INSERT INTO zx_transaction_lines_gt(
4373     application_id
4374     ,entity_code
4375     ,event_class_code
4376     ,trx_id
4377     ,trx_level_type
4378     ,trx_line_id
4379     ,line_class
4380     ,line_level_action
4381     ,trx_line_type
4382     ,trx_line_date
4383     ,line_amt_includes_tax_flag
4384     ,line_amt
4385     ,trx_line_quantity
4386     ,unit_price
4387     ,product_id
4388     ,product_org_id
4389     ,uom_code
4390     ,product_code
4391     ,ship_to_party_id
4392     ,ship_from_party_id
4393     ,bill_to_party_id
4394     ,bill_from_party_id
4395     ,ship_from_party_site_id
4396     ,bill_from_party_site_id
4397     ,ship_to_location_id
4398     ,ship_from_location_id
4399     ,bill_to_location_id
4400     ,bill_from_location_id   /* 8752470 */
4401     ,ship_third_pty_acct_id
4402     ,ship_third_pty_acct_site_id
4403     ,historical_flag
4404     ,trx_line_currency_code
4405     ,trx_line_currency_conv_date
4406     ,trx_line_currency_conv_rate
4407     ,trx_line_currency_conv_type
4408     ,trx_line_mau
4409     ,trx_line_precision
4410     ,historical_tax_code_id
4411     ,trx_business_category
4412     ,product_category
4413     ,product_fisc_classification
4414     ,line_intended_use
4415     ,product_type
4416     ,user_defined_fisc_class
4417     ,assessable_value
4418     ,input_tax_classification_code
4419     ,account_ccid
4420     -- Bug 5025018. Updated tax attribute mappings
4421     ,bill_third_pty_acct_id
4422     ,bill_third_pty_acct_site_id
4423     -- Bug 5079867. Line number and description in ATI page
4424     ,trx_line_number
4425     ,trx_line_description
4426     ,product_description
4427     ,user_upd_det_factors_flag --Bug 5632300
4428     ,defaulting_attribute1 --Bug#6902111
4429   )
4430   SELECT
4431     PO_CONSTANTS_SV.APPLICATION_ID --application_id
4432     ,PO_CONSTANTS_SV.REQ_ENTITY_CODE --entity_code
4433     ,PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE --event_class_code
4434     ,prl.requisition_header_id --trx_id
4435     ,PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE --trx_level_type
4436     ,prl.requisition_line_id --trx_line_id
4437     ,'INVOICE' --line_class
4438     ,nvl(prl.tax_attribute_update_code,'UPDATE') --line_level_action
4439     ,'ITEM' --trx_line_type
4440     ,NVL(prl.need_by_date, sysdate) --trx_line_date
4441     ,'N' --line_amt_includes_tax_flag
4442     ,nvl(prl.amount, prl.unit_price*prl.quantity) --line_amt
4443     ,prl.quantity --trx_line_quantity
4444     ,prl.unit_price --unit_price
4445     ,prl.item_id --product_id
4446      -- Bug 5335818. Pass in inventory_organization_id
4447     ,(SELECT fsp.inventory_organization_id --product_org_id
4448       FROM financials_system_params_all fsp
4449       WHERE fsp.org_id=prl.org_id)
4450     ,(SELECT mum.uom_code FROM mtl_units_of_measure mum
4451       WHERE mum.unit_of_measure=prl.unit_meas_lookup_code) --uom_code
4452     ,msib.segment1 --product_code
4453     ,prl.destination_organization_id --ship_to_party_id
4454     ,pv.party_id --ship_from_party_id
4455     ,prh.org_id --bill_to_party_id
4456     ,pv.party_id --bill_from_party_id
4457     ,pvs.party_site_id --ship_from_party_site_id
4458     ,pvs.party_site_id --bill_from_party_site_id
4459     ,prl.deliver_to_location_id --ship_to_location_id
4460     ,(SELECT hzps.location_id --ship_from_location_id
4461       FROM hz_party_sites hzps
4462       WHERE hzps.party_site_id = pvs.party_site_id)
4463     ,(SELECT location_id FROM hr_all_organization_units --bill_to_location_id
4464       WHERE organization_id=prh.org_id)
4465     , (SELECT pvs.location_id from po_vendor_sites_all pvs   /* 8752470 - Passing Location Id as Bill From Location Id */
4466         WHERE pvs.vendor_site_id=prl.vendor_site_id)
4467     ,prl.vendor_id --ship_third_pty_acct_id
4468     ,prl.vendor_site_id --ship_third_pty_acct_site_id
4469     ,null --historical_flag
4470     -- If prl.currency_code is null, insert values corresponding to functional
4471     -- currency for currency_code, rate_date, rate and rate_type
4472     ,NVL(prl.currency_code, l_functional_currency_code) --trx_line_currency_code
4473     ,NVL2(prl.currency_code, prl.rate_date, sysdate) --trx_line_currency_conv_date
4474     ,NVL2(prl.currency_code, prl.rate, l_rate) --trx_line_currency_conv_rate
4475     ,NVL2(prl.currency_code, prl.rate_type, l_rate_type) --trx_line_currency_conv_type
4476     ,fc.minimum_accountable_unit --trx_line_mau
4477     ,NVL(fc.precision, 2) --trx_line_precision
4478     ,null --historical_tax_code_id
4479     -- parent_req_line_id is persistent and is not nulled out as in case of
4480     -- PO, so need to insert ATAs(Additional Tax Attributes) here only if the
4481     -- action is create and parent_req_line_id is not null (so that ATAs
4482     -- are populated only for the req line split case)
4483     ,DECODE(prl.tax_attribute_update_code, --trx_business_category
4484             'CREATE', NVL2(prl.parent_req_line_id,
4485                            zxldet.trx_business_category, null),
4486             null
4487            )
4488     ,DECODE(prl.tax_attribute_update_code, --product_category
4489             'CREATE', NVL2(prl.parent_req_line_id,
4490                            zxldet.product_category, null),
4491             null
4492            )
4493     ,DECODE(prl.tax_attribute_update_code, --product_fisc_classification
4494             'CREATE', NVL2(prl.parent_req_line_id,
4495                            zxldet.product_fisc_classification, null),
4496             null
4497            )
4498     ,DECODE(prl.tax_attribute_update_code, --line_intended_use
4499             'CREATE', NVL2(prl.parent_req_line_id,
4500                            zxldet.line_intended_use, null),
4501             null
4502            )
4503     ,DECODE(prl.tax_attribute_update_code, --product_type
4504             'CREATE', NVL2(prl.parent_req_line_id,
4505                            zxldet.product_type, null),
4506             null
4507            )
4508     ,DECODE(prl.tax_attribute_update_code, --user_defined_fisc_class
4509             'CREATE', NVL2(prl.parent_req_line_id,
4510                            zxldet.user_defined_fisc_class, null),
4511             null
4512            )
4513     ,DECODE(prl.tax_attribute_update_code, --assessable_value
4514             'CREATE', NVL2(prl.parent_req_line_id,
4515                            zxldet.assessable_value, null),
4516             null
4517            )
4518     ,DECODE(p_calling_program, --input_tax_classification_code
4519             'REQIMPORT', prl.tax_name,
4520             DECODE(prl.tax_attribute_update_code,
4521                    'CREATE', NVL2(prl.parent_req_line_id,
4522                                   zxldet.input_tax_classification_code, null),
4523                    null
4524                   )
4525            )
4526     -- Get account id from first distribution, if created
4527     -- otherwise from the expense account of the item
4528     ,NVL((SELECT prd.code_combination_id --account_ccid
4529           FROM po_req_distributions_all prd
4530           WHERE prd.requisition_line_id = prl.requisition_line_id
4531           AND rownum = 1),
4532          msib.expense_account
4533         )
4534     -- Bug 5025018. Updated tax attribute mappings
4535     ,pv.vendor_id --bill_third_pty_acct_id
4536     ,pvs.vendor_site_id --bill_third_pty_acct_site_id
4537     -- Bug 5079867. Line number and description in ATI page
4538     ,prl.line_num --trx_line_number
4539     ,prl.item_description --trx_line_description
4540     ,prl.item_description --product_description
4541     --Bug 5632300. Parameter to confirm that tax classification is overridden
4542 
4543   -- Bug 14155908: When the req split is done passing the user_upd_det_factors_flag
4544   -- to zx_transaction_lines_gt as that of the parent req line's value in
4545   -- zx_lines_det_factors table.
4546     ,Decode (p_calling_program,
4547 	    'REQUISITION_MODIFY', zxldet.user_upd_det_factors_flag,
4548             'REQIMPORT', NVL2(PRL.TAX_NAME,'Y',NULL))
4549     ,prl.destination_organization_id --Bug#6902111
4550   FROM po_requisition_headers_all prh, po_requisition_lines_all prl,
4551        zx_lines_det_factors zxldet, po_vendors pv, po_vendor_sites_all pvs,
4552        mtl_system_items_b msib, fnd_currencies fc
4553   WHERE prh.requisition_header_id = p_requisition_header_id
4554   AND prh.requisition_header_id = prl.requisition_header_id
4555   -- Conditions for getting Additional Tax Attributes
4556   --  Do not put a condition on zxldet.trx_id here because that would
4557   --  entail bringing the source_header_id for the shipment being currently
4558   --  processed. Join with trx_line_id itself is unique because the document
4559   --  type has been classified with event_class_code and requisition_line_id
4560   --  will always be unique
4561   AND zxldet.application_id(+) = PO_CONSTANTS_SV.APPLICATION_ID
4562   AND zxldet.entity_code(+) = PO_CONSTANTS_SV.REQ_ENTITY_CODE
4563   AND zxldet.event_class_code(+) = PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE
4564   AND zxldet.trx_level_type(+) = PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE
4565   AND zxldet.trx_line_id(+) = prl.parent_req_line_id
4566   -- Join with vendor tables to get party and party site information
4567   AND pv.vendor_id(+) = prl.vendor_id
4568   AND pvs.vendor_site_id(+) = prl.vendor_site_id
4569   -- Join with items table for item information
4570   AND msib.inventory_item_id(+) = prl.item_id
4571   AND msib.organization_id(+) = prl.org_id
4572   -- Join with fnd_currencies for currency information
4573   AND fc.currency_code(+) = prl.currency_code
4574   -- Conditions that determine that requisition line is 'Active'
4575   AND nvl(prl.modified_by_agent_flag, 'N') = 'N'
4576   AND nvl(prl.cancel_flag, 'N') = 'N'
4577   AND nvl(prl.closed_code, 'OPEN') <> 'FINALLY CLOSED'
4578   AND prl.line_location_id IS NULL
4579   AND prl.at_sourcing_flag IS NULL;
4580 
4581 END populate_zx_lines_with_req;
4582 
4583 
4584 -----------------------------------------------------------------------------
4585 --Start of Comments
4586 --Name: populate_zx_dists_with_po
4587 --Pre-reqs:
4588 --  None
4589 --Modifies:
4590 --  ZX_ITM_DISTRIBUTIONS_GT
4591 --Locks:
4592 --  ZX_ITM_DISTRIBUTIONS_GT
4593 --Function:
4594 --  Populate ZX_ITM_DISTRIBUTIONS_GT with transaction distribution data
4595 --Parameters:
4596 --IN:
4597 --p_po_header_id_tbl
4598 --  PL/SQL table with list of po_header_id's to process for tax_calculation
4599 --p_calling_program
4600 --  Identifies the module that calls this procedure eg. 'PDOI'
4601 --Notes:
4602 --  Used by calculate_tax procedure. Not to be used externally
4603 --End of Comments
4604 -----------------------------------------------------------------------------
4605 PROCEDURE populate_zx_dists_with_po(p_po_header_id_tbl  IN  PO_TBL_NUMBER,
4606                                     p_calling_program   IN  VARCHAR2
4607 ) IS
4608 BEGIN
4609   -- Populate ZX_ITM_DISTRIBUTIONS_GT with all such distributions for which:
4610   -- 1. A sibling distribution was changed OR
4611   -- 2. There was a change at a parent level (header, line, shipment) OR
4612   -- 3. A distribution was deleted, hence its shipment is marked as 'DIST_DELETE'
4613   -- All the above cases are in one sql so that the same distribution is not
4614   -- populated twice, hence the OR conditions
4615 
4616   FORALL i in 1..p_po_header_id_tbl.COUNT
4617     INSERT INTO zx_itm_distributions_gt(
4618       application_id
4619       ,entity_code
4620       ,event_class_code
4621       ,trx_id
4622       ,trx_line_id
4623       ,trx_level_type
4624       ,trx_line_dist_id
4625       ,dist_level_action
4626       ,trx_line_dist_date
4627       ,item_dist_number
4628       ,task_id
4629       ,award_id
4630       ,project_id
4631       ,expenditure_type
4632       ,expenditure_organization_id
4633       ,expenditure_item_date
4634       ,trx_line_dist_amt
4635       ,trx_line_dist_qty
4636       ,trx_line_quantity
4637       ,account_ccid
4638       ,currency_exchange_rate
4639       ,overriding_recovery_rate
4640     )
4641     SELECT
4642       PO_CONSTANTS_SV.APPLICATION_ID --application_id
4643       ,PO_CONSTANTS_SV.PO_ENTITY_CODE --entity_code
4644       ,PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE --event_class_code
4645       ,pd1.po_header_id --trx_id
4646       ,pll.line_location_id --trx_line_id
4647       ,PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE --trx_level_type
4648       ,pd1.po_distribution_id --trx_line_dist_id
4649       ,NVL(pd1.tax_attribute_update_code, 'NO_ACTION') --dist_level_action
4650       ,sysdate --trx_line_dist_date
4651       ,pd1.distribution_num --item_dist_number
4652       ,pd1.task_id --task_id
4653       ,pd1.award_id --award_id
4654       ,pd1.project_id --project_id
4655       ,pd1.expenditure_type --expenditure_type
4656       ,pd1.expenditure_organization_id --expenditure_organization_id
4657       ,pd1.expenditure_item_date --expenditure_item_date
4658       ,DECODE(nvl(pll.matching_basis,pl.matching_basis), --trx_line_dist_amt
4659               'AMOUNT', pd1.amount_ordered,
4660               pd1.quantity_ordered*pll.price_override)
4661       ,pd1.quantity_ordered --trx_line_dist_qty
4662       ,pll.quantity --trx_line_quantity
4663       ,pd1.code_combination_id --account_ccid
4664       ,pd1.rate --currency_exchange_rate
4665       , decode(pd1.tax_recovery_override_flag, 'Y', pd1.recovery_rate, null) --overriding_recovery_rate
4666     FROM po_distributions_all pd1, po_line_locations_all pll, po_lines_all pl
4667     WHERE pd1.po_header_id = p_po_header_id_tbl(i)
4668     AND pd1.line_location_id=pll.line_location_id
4669     AND pll.po_line_id=pl.po_line_id
4670     AND (EXISTS(SELECT 'SIBLING DIST WITH TAUC'
4671                 FROM po_distributions_all pd2
4672                 WHERE pd2.line_location_id = pd1.line_location_id
4673                 --AND pd2.po_distribution_id<>pd1.po_distribution_id
4674                 AND pd2.tax_attribute_update_code IS NOT NULL
4675                )
4676          --OR pd1.tax_attribute_update_code IS NOT NULL
4677          OR pll.tax_attribute_update_code IS NOT NULL
4678         )
4679     AND pd1.po_header_id IN
4680       (SELECT trx_id FROM zx_trx_headers_gt
4681        WHERE event_class_code = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE);
4682 
4683 END populate_zx_dists_with_po;
4684 
4685 
4686 -----------------------------------------------------------------------------
4687 --Start of Comments
4688 --Name: populate_all_dists_po
4689 --Pre-reqs:
4690 --  None
4691 --Modifies:
4692 --  ZX_ITM_DISTRIBUTIONS_GT
4693 --Locks:
4694 --  ZX_ITM_DISTRIBUTIONS_GT
4695 --Function:
4696 --  Populate ZX_ITM_DISTRIBUTIONS_GT with transaction distribution data
4697 --Parameters:
4698 --IN:
4699 --p_po_header_id
4700 --  po_header_id to process
4701 --Notes:
4702 --  Used by determine_recovery_po procedure. Not to be used externally
4703 --End of Comments
4704 -----------------------------------------------------------------------------
4705 PROCEDURE populate_all_dists_po(p_po_header_id  IN  NUMBER) IS
4706 BEGIN
4707   -- Populate ZX_ITM_DISTRIBUTIONS_GT with all distributions of the given PO
4708 
4709   INSERT INTO zx_itm_distributions_gt(
4710     application_id
4711     ,entity_code
4712     ,event_class_code
4713     ,trx_id
4714     ,trx_line_id
4715     ,trx_level_type
4716     ,trx_line_dist_id
4717     ,dist_level_action
4718     ,trx_line_dist_date
4719     ,item_dist_number
4720     ,task_id
4721     ,award_id
4722     ,project_id
4723     ,expenditure_type
4724     ,expenditure_organization_id
4725     ,expenditure_item_date
4726     ,trx_line_dist_amt
4727     ,trx_line_dist_qty
4728     ,trx_line_quantity
4729     ,account_ccid
4730     ,currency_exchange_rate
4731     ,overriding_recovery_rate
4732   )
4733   SELECT
4734     PO_CONSTANTS_SV.APPLICATION_ID --application_id
4735     ,PO_CONSTANTS_SV.PO_ENTITY_CODE --entity_code
4736     ,PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE --event_class_code
4737     ,pd.po_header_id --trx_id
4738     ,pll.line_location_id --trx_line_id
4739     ,PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE --trx_level_type
4740     ,pd.po_distribution_id --trx_line_dist_id
4741     ,'NO_ACTION' --dist_level_action
4742     ,sysdate --trx_line_dist_date
4743     ,pd.distribution_num --item_dist_number
4744     ,pd.task_id --task_id
4745     ,pd.award_id --award_id
4746     ,pd.project_id --project_id
4747     ,pd.expenditure_type --expenditure_type
4748     ,pd.expenditure_organization_id --expenditure_organization_id
4749     ,pd.expenditure_item_date --expenditure_item_date
4750     -- Bug 5202059. Pass in correct amount
4751     ,DECODE(nvl(pll.matching_basis,pl.matching_basis), --trx_line_dist_amt
4752             'AMOUNT', pd.amount_ordered,
4753             pd.quantity_ordered*pll.price_override)
4754     ,pd.quantity_ordered --trx_line_dist_qty
4755     ,pll.quantity --trx_line_quantity
4756     ,pd.code_combination_id --account_ccid
4757     ,pd.rate --currency_exchange_rate
4758     , decode(pd.tax_recovery_override_flag, 'Y', pd.recovery_rate, null) --overriding_recovery_rate
4759   FROM po_distributions_all pd, po_line_locations_all pll, po_lines_all pl
4760   WHERE pd.po_header_id = p_po_header_id
4761   AND pd.line_location_id=pll.line_location_id
4762   AND pll.po_line_id = pl.po_line_id
4763   -- Conditions that determine that po line is 'Active'
4764   AND nvl(pl.cancel_flag,'N') = 'N'
4765   AND nvl(pl.closed_code,'OPEN') <> 'FINALLY CLOSED'
4766   AND nvl(pll.cancel_flag,'N') = 'N'
4767   AND nvl(pll.closed_code,'OPEN') <> 'FINALLY CLOSED'
4768   --Bug 10305728 start
4769   AND (EXISTS(SELECT 'SIBLING DIST WITH TAUC'
4770               FROM po_distributions_all pd2
4771               WHERE pd2.line_location_id = pd.line_location_id
4772               AND pd2.tax_attribute_update_code IS NOT NULL
4773              )
4774        OR pll.tax_attribute_update_code IS NOT NULL
4775       );
4776   --Bug 10305728 end
4777 
4778 END populate_all_dists_po;
4779 
4780 
4781 -----------------------------------------------------------------------------
4782 --Start of Comments
4783 --Name: populate_zx_dists_with_rel
4784 --Pre-reqs:
4785 --  None
4786 --Modifies:
4787 --  ZX_ITM_DISTRIBUTIONS_GT
4788 --Locks:
4789 --  ZX_ITM_DISTRIBUTIONS_GT
4790 --Function:
4791 --  Populate ZX_ITM_DISTRIBUTIONS_GT with transaction distribution data
4792 --Parameters:
4793 --IN:
4794 --p_po_release_id_tbl
4795 --  PL/SQL table with list of po_release_id's to process for tax_calculation
4796 --p_calling_program
4797 --  Identifies the module that calls this procedure eg. 'PDOI'
4798 --Notes:
4799 --  Used by calculate_tax procedure. Not to be used externally
4800 --End of Comments
4801 -----------------------------------------------------------------------------
4802 PROCEDURE populate_zx_dists_with_rel(p_po_release_id_tbl  IN  PO_TBL_NUMBER,
4803                                      p_calling_program    IN  VARCHAR2
4804 ) IS
4805 BEGIN
4806   -- Populate ZX_ITM_DISTRIBUTIONS_GT with all such distributions for which:
4807   -- 1. A sibling distribution was changed OR
4808   -- 2. There was a change at a parent level (header, line, shipment) OR
4809   -- 3. A distribution was deleted, hence its shipment is marked as 'DIST_DELETE'
4810   -- All the above cases are in one sql so that the same distribution is not
4811   -- populated twice, hence the OR conditions
4812 
4813   FORALL i in 1..p_po_release_id_tbl.COUNT
4814     INSERT INTO zx_itm_distributions_gt(
4815       application_id
4816       ,entity_code
4817       ,event_class_code
4818       ,trx_id
4819       ,trx_line_id
4820       ,trx_level_type
4821       ,trx_line_dist_id
4822       ,dist_level_action
4823       ,trx_line_dist_date
4824       ,item_dist_number
4825       ,task_id
4826       ,award_id
4827       ,project_id
4828       ,expenditure_type
4829       ,expenditure_organization_id
4830       ,expenditure_item_date
4831       ,trx_line_dist_amt
4832       ,trx_line_dist_qty
4833       ,trx_line_quantity
4834       ,account_ccid
4835       ,currency_exchange_rate
4836       ,overriding_recovery_rate
4837     )
4838     SELECT
4839       PO_CONSTANTS_SV.APPLICATION_ID --application_id
4840       ,PO_CONSTANTS_SV.REL_ENTITY_CODE --entity_code
4841       ,PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE --event_class_code
4842       ,pd1.po_release_id --trx_id
4843       ,pll.line_location_id --trx_line_id
4844       ,PO_CONSTANTS_SV.REL_TRX_LEVEL_TYPE --trx_level_type
4845       ,pd1.po_distribution_id --trx_line_dist_id
4846       ,nvl(pd1.tax_attribute_update_code, 'NO_ACTION') --dist_level_action
4847       ,sysdate --trx_line_dist_date
4848       ,pd1.distribution_num --item_dist_number
4849       ,pd1.task_id --task_id
4850       ,pd1.award_id --award_id
4851       ,pd1.project_id --project_id
4852       ,pd1.expenditure_type --expenditure_type
4853       ,pd1.expenditure_organization_id --expenditure_organization_id
4854       ,pd1.expenditure_item_date --expenditure_item_date
4855       ,DECODE(pl.matching_basis, --trx_line_dist_amt
4856               'AMOUNT', pd1.amount_ordered,
4857               pd1.quantity_ordered*pll.price_override)
4858       ,pd1.quantity_ordered --trx_line_dist_qty
4859       ,pll.quantity --trx_line_quantity
4860       ,pd1.code_combination_id --account_ccid
4861       ,pd1.rate --currency_exchange_rate
4862       , decode(pd1.tax_recovery_override_flag, 'Y', pd1.recovery_rate, null) --overriding_recovery_rate
4863     FROM po_distributions_all pd1, po_line_locations_all pll, po_lines_all pl
4864     WHERE pd1.po_release_id = p_po_release_id_tbl(i)
4865     AND pd1.line_location_id=pll.line_location_id
4866     AND pll.po_line_id=pl.po_line_id
4867     AND (EXISTS(SELECT 'SIBLING DIST WITH TAUC'
4868                 FROM po_distributions_all pd2
4869                 WHERE pd2.line_location_id = pd1.line_location_id
4870                 --AND pd2.po_distribution_id<>pd1.po_distribution_id
4871                 AND pd2.tax_attribute_update_code IS NOT NULL
4872                )
4873          --OR pd1.tax_attribute_update_code IS NOT NULL
4874          OR pll.tax_attribute_update_code IS NOT NULL
4875         )
4876     AND pd1.po_release_id IN
4877       (SELECT trx_id FROM zx_trx_headers_gt
4878        WHERE event_class_code = PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE);
4879 
4880 END populate_zx_dists_with_rel;
4881 
4882 
4883 -----------------------------------------------------------------------------
4884 --Start of Comments
4885 --Name: populate_all_dists_rel
4886 --Pre-reqs:
4887 --  None
4888 --Modifies:
4889 --  ZX_ITM_DISTRIBUTIONS_GT
4890 --Locks:
4891 --  ZX_ITM_DISTRIBUTIONS_GT
4892 --Function:
4893 --  Populate ZX_ITM_DISTRIBUTIONS_GT with transaction distribution data
4894 --Parameters:
4895 --IN:
4896 --p_po_release_id
4897 --  po_release_id to process
4898 --Notes:
4899 --  Used by determine_recovery_rel procedure. Not to be used externally
4900 --End of Comments
4901 -----------------------------------------------------------------------------
4902 PROCEDURE populate_all_dists_rel(p_po_release_id IN NUMBER) IS
4903 BEGIN
4904   -- Populate ZX_ITM_DISTRIBUTIONS_GT with all distributions of the given Release
4905 
4906   INSERT INTO zx_itm_distributions_gt(
4907     application_id
4908     ,entity_code
4909     ,event_class_code
4910     ,trx_id
4911     ,trx_line_id
4912     ,trx_level_type
4913     ,trx_line_dist_id
4914     ,dist_level_action
4915     ,trx_line_dist_date
4916     ,item_dist_number
4917     ,task_id
4918     ,award_id
4919     ,project_id
4920     ,expenditure_type
4921     ,expenditure_organization_id
4922     ,expenditure_item_date
4923     ,trx_line_dist_amt
4924     ,trx_line_dist_qty
4925     ,trx_line_quantity
4926     ,account_ccid
4927     ,currency_exchange_rate
4928     ,overriding_recovery_rate
4929   )
4930   SELECT
4931     PO_CONSTANTS_SV.APPLICATION_ID --application_id
4932     ,PO_CONSTANTS_SV.REL_ENTITY_CODE --entity_code
4933     ,PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE --event_class_code
4934     ,pd.po_release_id --trx_id
4935     ,pll.line_location_id --trx_line_id
4936     ,PO_CONSTANTS_SV.REL_TRX_LEVEL_TYPE --trx_level_type
4937     ,pd.po_distribution_id --trx_line_dist_id
4938     ,'NO_ACTION' --dist_level_action
4939     ,sysdate --trx_line_dist_date
4940     ,pd.distribution_num --item_dist_number
4941     ,pd.task_id --task_id
4942     ,pd.award_id --award_id
4943     ,pd.project_id --project_id
4944     ,pd.expenditure_type --expenditure_type
4945     ,pd.expenditure_organization_id --expenditure_organization_id
4946     ,pd.expenditure_item_date --expenditure_item_date
4947     -- Bug 5202059. Pass in correct amount
4948     ,DECODE(pl.matching_basis, --trx_line_dist_amt
4949             'AMOUNT', pd.amount_ordered,
4950             pd.quantity_ordered*pll.price_override)
4951     ,pd.quantity_ordered --trx_line_dist_qty
4952     ,pll.quantity --trx_line_quantity
4953     ,pd.code_combination_id --account_ccid
4954     ,pd.rate --currency_exchange_rate
4955     , decode(pd.tax_recovery_override_flag, 'Y', pd.recovery_rate, null) --overriding_recovery_rate
4956   FROM po_distributions_all pd, po_line_locations_all pll, po_lines_all pl
4957   WHERE pd.po_release_id = p_po_release_id
4958   AND pd.line_location_id = pll.line_location_id
4959   AND pll.po_line_id = pl.po_line_id
4960   -- Conditions that determine that po line is 'Active'
4961   AND nvl(pl.cancel_flag,'N') = 'N'
4962   AND nvl(pl.closed_code,'OPEN') <> 'FINALLY CLOSED'
4963   AND nvl(pll.cancel_flag,'N') = 'N'
4964   AND nvl(pll.closed_code,'OPEN') <> 'FINALLY CLOSED';
4965 
4966 END populate_all_dists_rel;
4967 
4968 
4969 
4970 -----------------------------------------------------------------------------
4971 --Start of Comments
4972 --Name: populate_zx_dists_with_req
4973 --Pre-reqs:
4974 --  None
4975 --Modifies:
4976 --  ZX_TRX_DISTS_GT
4977 --Locks:
4978 --  ZX_TRX_DISTS_GT
4979 --Function:
4980 --  Populate ZX_TRX_DISTS_GT with transaction distribution data
4981 --Parameters:
4982 --IN:
4983 --p_requisition_header_id
4984 --  requisition_header_id of the requisition to process for tax_calculation
4985 --p_calling_program
4986 --  Identifies the module that calls this procedure eg. 'PDOI'
4987 --Notes:
4988 --  Used by calculate_tax_requisition procedure. Not to be used externally
4989 --End of Comments
4990 -----------------------------------------------------------------------------
4991 PROCEDURE populate_zx_dists_with_req(p_requisition_header_id  IN  NUMBER,
4992                                      p_calling_program        IN  VARCHAR2
4993 ) IS
4994 BEGIN
4995   -- Populate zx distributions table with requisition distributions data
4996   INSERT INTO zx_itm_distributions_gt(
4997     application_id
4998     ,entity_code
4999     ,event_class_code
5000     ,trx_id
5001     ,trx_line_id
5002     ,trx_level_type
5003     ,trx_line_dist_id
5004     ,dist_level_action
5005     ,trx_line_dist_date
5006     ,item_dist_number
5007     ,dist_intended_use
5008     ,task_id
5009     ,award_id
5010     ,project_id
5011     ,expenditure_type
5012     ,expenditure_organization_id
5013     ,expenditure_item_date
5014     ,trx_line_dist_amt
5015     ,trx_line_dist_qty
5016     ,trx_line_quantity
5017     ,account_ccid
5018     ,historical_flag
5019     ,overriding_recovery_rate)
5020   SELECT
5021     PO_CONSTANTS_SV.APPLICATION_ID --application_id
5022     ,PO_CONSTANTS_SV.REQ_ENTITY_CODE --entity_code
5023     ,PO_CONSTANTS_SV.REQ_EVENT_CLASS_CODE --event_class_code
5024     ,prl.requisition_header_id --trx_id
5025     ,prl.requisition_line_id --trx_line_id
5026     ,PO_CONSTANTS_SV.REQ_TRX_LEVEL_TYPE --trx_level_type
5027     ,prd.distribution_id --trx_line_dist_id
5028     ,'CREATE' --dist_level_action
5029     ,sysdate --trx_line_dist_date
5030     ,prd.distribution_num --item_dist_number
5031     ,null --dist_intended_use
5032     ,prd.task_id --task_id
5033     ,prd.award_id --award_id
5034     ,prd.project_id --project_id
5035     ,prd.expenditure_type --expenditure_type
5036     ,prd.expenditure_organization_id --expenditure_organization_id
5037     ,prd.expenditure_item_date --expenditure_item_date
5038     ,DECODE(prl.matching_basis,'AMOUNT', prd.REQ_LINE_AMOUNT, prd.req_line_quantity*prl.unit_price) --trx_line_dist_amt
5039     ,prd.req_line_quantity --trx_line_dist_qty
5040     ,prl.quantity --trx_line_quantity
5041     ,prd.code_combination_id --account_ccid
5042     ,null --historical_flag
5043     ,decode(prd.tax_recovery_override_flag, 'Y', prd.recovery_rate, null) --overriding_recovery_rate
5044   FROM po_requisition_lines_all prl, po_req_distributions_all prd
5045   WHERE prl.requisition_header_id = p_requisition_header_id
5046   AND prl.requisition_line_id = prd.requisition_line_id
5047   -- Conditions that determine that requisition line is 'Active'
5048   AND prl.SOURCE_TYPE_CODE<>'INVENTORY'
5049   AND nvl(prl.modified_by_agent_flag, 'N') = 'N'
5050   AND nvl(prl.cancel_flag, 'N') = 'N'
5051   AND nvl(prl.closed_code, 'OPEN') <> 'FINALLY CLOSED'
5052   AND prl.line_location_id IS NULL
5053   AND prl.at_sourcing_flag IS NULL;
5054 
5055 END populate_zx_dists_with_req;
5056 
5057 
5058 
5059 -----------------------------------------------------------------------------
5060 --Start of Comments
5061 --Name: calculate_tax_yes_no
5062 --Pre-reqs:
5063 --  Should be called before document is to be submitted for approval
5064 --Modifies:
5065 --  None
5066 --Locks:
5067 --  None
5068 --Function:
5069 --  Checks if tax needs to be re-calculated for a document before approval
5070 --Parameters:
5071 --IN:
5072 --p_po_header_id
5073 --  po_header_id of document for which tax re-calculation check has to be done
5074 --p_po_release_id
5075 --  po_header_id of document for which tax re-calculation check has to be done
5076 --p_req_header_id
5077 --  req_header_id of document for which tax re-calculation check has to be done
5078 --OUT:
5079 -- None
5080 --RETURN:
5081 --  Returns true if tax needs to be re-calculated else returns false
5082 --Notes:
5083 --  Checks if tax_attribute_update_code flag has a NOT NULL value
5084 --  at any level in the document, if yes then return true else return false
5085 --End of Comments
5086 -----------------------------------------------------------------------------
5087 FUNCTION calculate_tax_yes_no
5088         (p_po_header_id        IN          NUMBER,
5089          p_po_release_id       IN          NUMBER,
5090          p_req_header_id       IN          NUMBER)
5091 RETURN VARCHAR2
5092 IS
5093 
5094 l_Result VARCHAR2(4);
5095 l_module_name CONSTANT VARCHAR2(100) := 'CALCULATE_TAX_YES_NO';
5096 d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
5097                                           D_PACKAGE_BASE, l_module_name);
5098 d_progress NUMBER;
5099 
5100 BEGIN
5101 
5102   IF (PO_LOG.d_proc) THEN
5103     PO_LOG.proc_begin(d_module_base);
5104     PO_LOG.proc_begin(d_module_base, 'p_po_header_id',  p_po_header_id);
5105     PO_LOG.proc_begin(d_module_base, 'p_po_release_id', p_po_release_id);
5106     PO_LOG.proc_begin(d_module_base, 'p_req_header_id', p_req_header_id);
5107   END IF;
5108 
5109   d_progress := 0;
5110   l_result := 'N';
5111 
5112   BEGIN
5113 
5114     IF p_po_header_id IS NOT NULL THEN
5115 
5116       d_progress := 10;
5117 
5118       SELECT 'Y' INTO l_result
5119       FROM DUAL
5120       WHERE EXISTS
5121        (SELECT 'Y'
5122         FROM  po_headers_all        POH,
5123               po_lines_all          POL,
5124               po_line_locations_all PLL,
5125               po_distributions_all  POD
5126         WHERE POH.po_header_id = p_po_header_id
5127           AND POH.po_header_id = POL.po_header_id
5128           AND POL.po_line_id   = PLL.po_line_id
5129           AND PLL.line_location_id = POD.line_location_id
5130           AND NVL(POL.cancel_flag,'N') = 'N'
5131           AND NVL(POL.closed_code,'OPEN') <> 'FINALLY CLOSED'
5132           AND NVL(PLL.cancel_flag,'N') = 'N'
5133           AND NVL(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
5134           AND (POH.tax_attribute_update_code IS NOT NULL OR
5135                POL.tax_attribute_update_code IS NOT NULL OR
5136                PLL.tax_attribute_update_code IS NOT NULL OR
5137                POD.tax_attribute_update_code IS NOT NULL
5138               )
5139        );
5140 
5141     ELSIF p_po_release_id  IS NOT NULL THEN
5142 
5143       d_progress := 20;
5144 
5145       SELECT 'Y' INTO l_Result
5146       FROM DUAL
5147       WHERE EXISTS
5148        (SELECT 'Y'
5149         FROM  po_releases_all       POR,
5150               po_line_locations_all PLL,
5151               po_distributions_all  POD
5152         WHERE POR.po_release_id = p_po_release_id
5153           AND POR.po_release_id = PLL.po_release_id
5154           AND PLL.line_location_id = POD.line_location_id
5155           AND NVL(PLL.cancel_flag,'N') = 'N'
5156           AND NVL(PLL.closed_code,'OPEN') <> 'FINALLY CLOSED'
5157           AND (POR.tax_attribute_update_code IS NOT NULL OR
5158                PLL.tax_attribute_update_code IS NOT NULL OR
5159                POD.tax_attribute_update_code IS NOT NULL
5160               )
5161        );
5162     ELSIF p_req_header_id IS NOT NULL THEN
5163 
5164       d_progress := 30;
5165 
5166       SELECT 'Y' INTO l_Result
5167       FROM DUAL
5168       WHERE EXISTS
5169        (SELECT 'Y'
5170         FROM  po_requisition_headers_all  PRH,
5171               po_requisition_lines_all    PRL
5172         WHERE PRH.requisition_header_id = p_req_header_id
5173           AND PRH.requisition_header_id = PRL.requisition_header_id
5174           AND NVL(PRL.cancel_flag, 'N') = 'N'
5175           AND NVL(PRL.closed_code, 'OPEN') <> 'FINALLY CLOSED'
5176           AND (PRH.tax_attribute_update_code IS NOT NULL OR
5177                PRL.tax_attribute_update_code IS NOT NULL
5178               )
5179        );
5180     END IF;
5181 
5182     d_progress := 40;
5183 
5184   EXCEPTION
5185     WHEN NO_DATA_FOUND THEN
5186       l_result := 'N';
5187   END;
5188 
5189   d_progress := 50;
5190 
5191   IF (PO_LOG.d_proc) THEN
5192     PO_LOG.proc_end(d_module_base);
5193     PO_LOG.proc_end(d_module_base, 'l_result', l_result);
5194   END IF;
5195 
5196   RETURN l_result;
5197 
5198 EXCEPTION
5199   WHEN OTHERS THEN
5200     IF (PO_LOG.d_exc) THEN
5201       PO_LOG.exc(d_module_base, d_progress, SQLCODE || SQLERRM);
5202     END IF;
5203     RAISE;
5204 END calculate_tax_yes_no;
5205 
5206 
5207 
5208 -----------------------------------------------------------------------------
5209 --Start of Comments
5210 --Name: populate_zx_record
5211 --Pre-reqs:
5212 --  None
5213 --Modifies:
5214 --  None
5215 --Locks:
5216 --  None
5217 --Function:
5218 --  Populates zx record TRX_LINE_DIST_TBL
5219 --Parameters:
5220 --IN:
5221 --p_req_header_id
5222 --  req_header_id of requisition
5223 --OUT:
5224 -- None
5225 --End of Comments
5226 -----------------------------------------------------------------------------
5227 PROCEDURE populate_zx_record(p_requisition_header_id    IN    NUMBER) IS
5228   l_count NUMBER;
5229 BEGIN
5230   l_count :=0;
5231   FOR i IN successful_documents_csr(p_requisition_header_id) LOOP
5232     l_count := l_count + 1;
5233     ZX_GLOBAL_STRUCTURES_PKG.INIT_TRX_LINE_DIST_TBL(l_count);
5234 
5235     SELECT
5236       zxhgt.internal_organization_id
5237       ,zxhgt.application_id
5238       ,zxhgt.entity_code
5239       ,zxhgt.event_class_code
5240       ,zxhgt.event_type_code
5241       ,zxhgt.trx_id
5242       ,zxhgt.trx_date
5243       ,zxhgt.ledger_id
5244       ,zxhgt.legal_entity_id
5245       ,zxhgt.rounding_bill_to_party_id
5246       ,zxhgt.quote_flag
5247       ,zxhgt.document_sub_type
5248       ,zxhgt.default_taxation_country
5249 
5250       ,zxlgt.trx_level_type
5251       ,zxlgt.trx_line_id
5252       ,zxlgt.line_class
5253       ,zxlgt.line_level_action
5254       ,zxlgt.trx_line_type
5255       ,zxlgt.trx_line_date
5256       ,zxlgt.trx_business_category
5257       ,zxlgt.line_intended_use
5258       ,zxlgt.user_defined_fisc_class
5259       ,zxlgt.line_amt_includes_tax_flag
5260       ,zxlgt.line_amt
5261       ,zxlgt.trx_line_quantity
5262       ,zxlgt.unit_price
5263       ,zxlgt.product_id
5264       ,zxlgt.product_fisc_classification
5265       ,zxlgt.product_org_id
5266       ,zxlgt.uom_code
5267       ,zxlgt.product_type
5268       ,zxlgt.product_code
5269       ,zxlgt.product_category
5270       ,zxlgt.ship_to_party_id
5271       ,zxlgt.ship_from_party_id
5272       ,zxlgt.bill_to_party_id
5273       ,zxlgt.bill_from_party_id
5274       ,zxlgt.ship_from_party_site_id
5275       ,zxlgt.bill_from_party_site_id
5276       ,zxlgt.ship_to_location_id
5277       ,ship_from_location_id
5278       ,zxlgt.bill_to_location_id
5279       ,zxlgt.bill_from_location_id      /* 8752470 */
5280       ,zxlgt.ship_third_pty_acct_id
5281       ,zxlgt.ship_third_pty_acct_site_id
5282       ,zxlgt.assessable_value
5283       ,zxlgt.historical_flag
5284       ,zxlgt.trx_line_currency_code
5285       ,zxlgt.trx_line_currency_conv_date
5286       ,zxlgt.trx_line_currency_conv_rate
5287       ,zxlgt.trx_line_currency_conv_type
5288       ,zxlgt.trx_line_mau
5289       ,zxlgt.trx_line_precision
5290       ,zxlgt.historical_tax_code_id
5291       ,zxlgt.input_tax_classification_code
5292       ,zxlgt.account_ccid
5293       -- Bug 5025018. Updated tax attribute mappings
5294       ,zxlgt.bill_third_pty_acct_id
5295       ,zxlgt.bill_third_pty_acct_site_id
5296       -- Bug 5079867. Line number and description in ATI page
5297       ,zxlgt.trx_line_number
5298       ,zxlgt.trx_line_description
5299       ,zxlgt.product_description
5300       -- Bug 5082762. Product Type dropped when line is updated
5301       ,zxlgt.user_upd_det_factors_flag
5302       ,zxlgt.DEFAULTING_ATTRIBUTE1 --Bug#6902111
5303 
5304     INTO
5305       ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.internal_organization_id(l_count)
5306       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.application_id(l_count)
5307       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.entity_code(l_count)
5308       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.event_class_code(l_count)
5309       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.event_type_code(l_count)
5310       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_id(l_count)
5311       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_date(l_count)
5312       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.ledger_id(l_count)
5313       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.legal_entity_id(l_count)
5314       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.rounding_bill_to_party_id(l_count)
5315       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.quote_flag(l_count)
5316       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.document_sub_type(l_count)
5317       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.default_taxation_country(l_count)
5318 
5319       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_level_type(l_count)
5320       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_line_id(l_count)
5321       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.line_class(l_count)
5322       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.line_level_action(l_count)
5323       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_line_type(l_count)
5324       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_line_date(l_count)
5325       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_business_category(l_count)
5326       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.line_intended_use(l_count)
5327       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.user_defined_fisc_class(l_count)
5328       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.line_amt_includes_tax_flag(l_count)
5329       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.line_amt(l_count)
5330       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_line_quantity(l_count)
5331       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.unit_price(l_count)
5332       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.product_id(l_count)
5333       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.product_fisc_classification(l_count)
5334       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.product_org_id(l_count)
5335       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.uom_code(l_count)
5336       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.product_type(l_count)
5337       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.product_code(l_count)
5338       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.product_category(l_count)
5339       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.ship_to_party_id(l_count)
5340       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.ship_from_party_id(l_count)
5341       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.bill_to_party_id(l_count)
5342       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.bill_from_party_id(l_count)
5343       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.ship_from_party_site_id(l_count)
5344       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.bill_from_party_site_id(l_count)
5345       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.ship_to_location_id(l_count)
5346       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.ship_from_location_id(l_count)
5347       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.bill_to_location_id(l_count)
5348       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.bill_from_location_id(l_count)      /* 8752470 */
5349       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.ship_third_pty_acct_id(l_count)
5350       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.ship_third_pty_acct_site_id(l_count)
5351       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.assessable_value(l_count)
5352       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.historical_flag(l_count)
5353       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_line_currency_code(l_count)
5354       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_line_currency_conv_date(l_count)
5355       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_line_currency_conv_rate(l_count)
5356       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_line_currency_conv_type(l_count)
5357       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_line_mau(l_count)
5358       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_line_precision(l_count)
5359       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.historical_tax_code_id(l_count)
5360       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.input_tax_classification_code(l_count)
5361       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.account_ccid(l_count)
5362       -- Bug 5025018. Updated tax attribute mappings
5363       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.bill_third_pty_acct_id(l_count)
5364       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.bill_third_pty_acct_site_id(l_count)
5365       -- Bug 5079867. Line number and description in ATI page
5366       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_line_number(l_count)
5367       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.trx_line_description(l_count)
5368       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.product_description(l_count)
5369       -- Bug 5082762. Product Type dropped when line is updated
5370       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.user_upd_det_factors_flag(l_count)
5371       ,ZX_GLOBAL_STRUCTURES_PKG.TRX_LINE_DIST_TBL.DEFAULTING_ATTRIBUTE1(l_count) --Bug#6902111
5372     FROM zx_trx_headers_gt zxhgt, zx_transaction_lines_gt zxlgt
5373     WHERE zxlgt.trx_line_id = i.trx_line_id
5374     AND zxlgt.trx_id = zxhgt.trx_id;
5375 
5376   END LOOP;
5377 
5378 END populate_zx_record;
5379 
5380 
5381 
5382 -----------------------------------------------------------------------------
5383 --Start of Comments
5384 --Name: shipments_deleted_from_oa
5385 --Pre-reqs:
5386 --  Should be called before document is to be submitted for approval
5387 --Modifies:
5388 --  None
5389 --Locks:
5390 --  None
5391 --Function:
5392 --  Checks if tax needs to be re-calculated for a document before approval
5393 --Parameters:
5394 --IN:
5395 --p_po_header_id
5396 --  po_header_id of document for which shipments or distributions are deleted
5397 --p_del_shipment_table
5398 --  PL/SQL table of Numbers holding non-repeated deleted shipments Ids
5399 --  deleted
5400 --p_del_dist_shipment_table
5401 --  /SQL table of Numbers holding non-repeated deleted shipments Ids for which
5402 --  corresponding distributions are deleted
5403 --OUT:
5404 -- None
5405 --Notes:
5406 --  This routine populates zx tables with the shipments that are deleted or have
5407 --  their distribution deleted
5408 --End of Comments
5409 -----------------------------------------------------------------------------
5410 PROCEDURE SHIPMENT_DIST_DELETED_FROM_OA
5411 (
5412  P_PO_HEADER_ID              IN NUMBER,
5413  P_DEL_SHIPMENT_TABLE        IN PO_TBL_NUMBER,
5414  P_DEL_DIST_SHIPMENT_TABLE   IN PO_TBL_NUMBER
5415  )
5416  IS
5417   l_module_name CONSTANT VARCHAR2(100) := 'shipment_dist_deleted_from_oa';
5418   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
5419                                            D_PACKAGE_BASE, l_module_name);
5420   d_progress NUMBER;
5421 
5422 BEGIN
5423   IF (PO_LOG.d_proc) THEN
5424     PO_LOG.proc_begin(d_module_base);
5425     PO_LOG.proc_begin(d_module_base, 'p_po_header_id', p_po_header_id);
5426     PO_LOG.proc_begin(d_module_base, 'p_del_shipment_table', P_DEL_SHIPMENT_TABLE.COUNT);
5427     PO_LOG.proc_begin(d_module_base, 'p_del_dist_shipment_table', P_DEL_DIST_SHIPMENT_TABLE.COUNT);
5428   END IF;
5429 
5430   d_progress := 0;
5431   --
5432   -- Update line tax_attribute_code to DIST_DELETE
5433   -- for which distributions are deleted
5434   --
5435 
5436   FORALL i IN 1..p_del_dist_shipment_table.COUNT
5437     UPDATE po_line_locations_all
5438        SET tax_attribute_update_code = NVL(tax_attribute_update_code,'DIST_DELETE')
5439      WHERE line_location_id = p_del_dist_shipment_table(i);
5440 
5441   d_progress := 10;
5442 
5443   --
5444   -- Insert shipments ids that are deleted. However do not insert rows if
5445   -- there are no corresponding tax lines to delete
5446   --
5447 
5448   FORALL i IN 1..p_del_shipment_table.COUNT
5449   INSERT INTO zx_transaction_lines_gt
5450    (
5451      application_id,
5452      entity_code,
5453      event_class_code,
5454      trx_id,
5455      trx_level_type,
5456      trx_line_id,
5457      line_level_action,
5458      line_amt,
5459      line_amt_includes_tax_flag
5460    )
5461   SELECT
5462       PO_CONSTANTS_SV.APPLICATION_ID,        --application_id
5463       PO_CONSTANTS_SV.PO_ENTITY_CODE,        --entity_code
5464       PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE,   --event_class_code
5465       p_po_header_id,                        --trx_id
5466       PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE,     --trx_level_type
5467       p_del_shipment_table(i),               --trx_line_id
5468       'DELETE',                              --line_level_action
5469       -1,
5470       'N'
5471   --<Bug 5135037> Added the EXISTS clause so that only one row is inserted
5472   --into gt table even if there are multiple tax lines for each shipment
5473   FROM dual
5474   WHERE EXISTS
5475     (SELECT 'TAX LINES EXIST'
5476      FROM zx_lines zl
5477      -- Restrict to only rows that have corresponding tax lines
5478      -- Since this is called from OA, so conditions are only for SPO
5479      WHERE zl.trx_id = p_po_header_id
5480      AND zl.trx_line_id = p_del_shipment_table(i)
5481      AND zl.application_id = PO_CONSTANTS_SV.APPLICATION_ID
5482      AND zl.entity_code = PO_CONSTANTS_SV.PO_ENTITY_CODE
5483      AND zl.event_class_code = PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE
5484      AND zl.trx_level_type = PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE
5485     );
5486 
5487   d_progress := 20;
5488   IF (PO_LOG.d_proc) THEN
5489     PO_LOG.proc_end(d_module_base);
5490   END IF;
5491 
5492 EXCEPTION
5493   WHEN OTHERS THEN
5494     IF (PO_LOG.d_exc) THEN
5495       PO_LOG.exc(d_module_base, d_progress, SQLCODE || SQLERRM);
5496     END IF;
5497     RAISE;
5498 END SHIPMENT_DIST_DELETED_FROM_OA;
5499 
5500 
5501 
5502 -----------------------------------------------------------------------------
5503 --Start of Comments
5504 --Name: append_error
5505 --Pre-reqs:
5506 --  None
5507 --Modifies:
5508 --  None
5509 --Locks:
5510 --  None
5511 --Function:
5512 --  Append an error to the global error record G_TAX_ERRORS_TBL
5513 --Parameters:
5514 --IN:
5515 --p_error_level
5516 --  The depth in current flow at which error occured
5517 --p_document_type_code
5518 --  Type of document for which error occured
5519 --p_document_id
5520 --  Id of document for which error occured
5521 --p_document_num
5522 --  Number of document for which error occured
5523 --p_line_id
5524 --  Id of line in the document for which error occured
5525 --p_line_num
5526 --  Number of line in the document for which error occured
5527 --p_line_location_id
5528 --  Id of shipment in the document for which error occured
5529 --p_shipment_num
5530 --  Number of shipment in the document for which error occured
5531 --p_distribution_id
5532 --  Id of distribution in the document for which error occured
5533 --p_distribution_num
5534 --  Number of distribution in the document for which error occured
5535 --p_message_text
5536 --  Error message to add
5537 --End of Comments
5538 -----------------------------------------------------------------------------
5539 PROCEDURE append_error(p_error_level         IN VARCHAR2,
5540                        p_document_type_code  IN VARCHAR2,
5541                        p_document_id         IN NUMBER,
5542                        p_document_num        IN VARCHAR2, --<Bug 9688923>
5543                        p_line_id             IN NUMBER,
5544                        p_line_num            IN NUMBER,
5545                        p_line_location_id    IN NUMBER,
5546                        p_shipment_num        IN NUMBER,
5547                        p_distribution_id     IN NUMBER,
5548                        p_distribution_num    IN NUMBER,
5549                        p_message_text        IN VARCHAR2
5550 ) IS
5551   l_module_name CONSTANT VARCHAR2(100) := 'APPEND_ERROR';
5552   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
5553                                             D_PACKAGE_BASE, l_module_name);
5554   d_progress NUMBER;
5555   l_length NUMBER;
5556 BEGIN
5557   IF (PO_LOG.d_proc) THEN
5558     PO_LOG.proc_begin(d_module_base);
5559     PO_LOG.proc_begin(d_module_base, 'p_error_level', p_error_level);
5560     PO_LOG.proc_begin(d_module_base, 'p_document_type_code', p_document_type_code);
5561     PO_LOG.proc_begin(d_module_base, 'p_document_id', p_document_id);
5562     PO_LOG.proc_begin(d_module_base, 'p_document_num', p_document_num);
5563     PO_LOG.proc_begin(d_module_base, 'p_line_id', p_line_id);
5564     PO_LOG.proc_begin(d_module_base, 'p_line_num', p_line_num);
5565     PO_LOG.proc_begin(d_module_base, 'p_line_location_id', p_line_location_id);
5566     PO_LOG.proc_begin(d_module_base, 'p_shipment_num', p_shipment_num);
5567     PO_LOG.proc_begin(d_module_base, 'p_distribution_id', p_distribution_id);
5568     PO_LOG.proc_begin(d_module_base, 'p_distribution_num', p_distribution_num);
5569     PO_LOG.proc_begin(d_module_base, 'p_message_text', p_message_text);
5570   END IF;
5571 
5572   d_progress := 0;
5573   G_TAX_ERRORS_TBL.error_level.extend;
5574   G_TAX_ERRORS_TBL.document_type_code.extend;
5575   G_TAX_ERRORS_TBL.document_id.extend;
5576   G_TAX_ERRORS_TBL.document_num.extend;
5577   G_TAX_ERRORS_TBL.line_id.extend;
5578   G_TAX_ERRORS_TBL.line_num.extend;
5579   G_TAX_ERRORS_TBL.line_location_id.extend;
5580   G_TAX_ERRORS_TBL.shipment_num.extend;
5581   G_TAX_ERRORS_TBL.distribution_id.extend;
5582   G_TAX_ERRORS_TBL.distribution_num.extend;
5583   G_TAX_ERRORS_TBL.message_text.extend;
5584 
5585   d_progress := 10;
5586   l_length := G_TAX_ERRORS_TBL.error_level.COUNT;
5587 
5588   d_progress := 20;
5589   IF PO_LOG.d_stmt THEN
5590     PO_LOG.stmt(d_module_base,d_progress,'l_length= '||l_length);
5591   END IF;
5592 
5593   d_progress := 30;
5594   G_TAX_ERRORS_TBL.error_level(l_length)        := p_error_level;
5595   G_TAX_ERRORS_TBL.document_type_code(l_length) := p_document_type_code;
5596   G_TAX_ERRORS_TBL.document_id(l_length)        := p_document_id;
5597   G_TAX_ERRORS_TBL.document_num(l_length)       := p_document_num;
5598   G_TAX_ERRORS_TBL.line_id(l_length)            := p_line_id;
5599   G_TAX_ERRORS_TBL.line_num(l_length)           := p_line_num;
5600   G_TAX_ERRORS_TBL.line_location_id(l_length)   := p_line_location_id;
5601   G_TAX_ERRORS_TBL.shipment_num(l_length)       := p_shipment_num;
5602   G_TAX_ERRORS_TBL.distribution_id(l_length)    := p_distribution_id;
5603   G_TAX_ERRORS_TBL.distribution_num(l_length)   := p_distribution_num;
5604   G_TAX_ERRORS_TBL.message_text(l_length)       := p_message_text;
5605 
5606   d_progress := 40;
5607   IF (PO_LOG.d_proc) THEN
5608     PO_LOG.proc_end(d_module_base);
5609   END IF;
5610 EXCEPTION
5611   WHEN OTHERS THEN
5612     IF (PO_LOG.d_exc) THEN
5613       PO_LOG.exc(d_module_base, d_progress, SQLCODE || SQLERRM);
5614     END IF;
5615 END append_error;
5616 
5617 
5618 
5619 -----------------------------------------------------------------------------
5620 --Start of Comments
5621 --Name: initialize_zx_gt_tables
5622 --Pre-reqs:
5623 --  None
5624 --Modifies:
5625 --  ZX_TRX_HEADERS_GT
5626 --  ZX_TRANSACTION_LINES_GT
5627 --  ZX_ITM_DISTRIBUTIONS_GT
5628 --  ZX_VALIDATION_ERRORS_GT
5629 --  ZX_ERRORS_GT
5630 --Locks:
5631 --  ZX_TRX_HEADERS_GT
5632 --  ZX_TRANSACTION_LINES_GT
5633 --  ZX_ITM_DISTRIBUTIONS_GT
5634 --  ZX_VALIDATION_ERRORS_GT
5635 --  ZX_ERRORS_GT
5636 --Function:
5637 --  Wipe out all data from ZX Global Temporary tables used in PO transaction
5638 --  except if there are lines populated that have action='DELETE'. This
5639 --  exception is for HTML Orders case where deletions come pre-populated in
5640 --  the GT tables
5641 --Parameters:
5642 --IN:
5643 --Notes:
5644 --  For internal use only. Used within this package by calculate_tax,
5645 --  calculate_tax_requisition and determine_recovery.. procedures
5646 --End of Comments
5647 -----------------------------------------------------------------------------
5648 PROCEDURE initialize_zx_gt_tables IS
5649   l_module_name CONSTANT VARCHAR2(100) := 'INITIALIZE_ZX_GT_TABLES';
5650   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
5651                                             D_PACKAGE_BASE, l_module_name);
5652   d_progress NUMBER;
5653   l_clear_tables VARCHAR2(1);
5654 BEGIN
5655   d_progress := 0;
5656 
5657   IF PO_LOG.d_stmt THEN
5658     PO_LOG.stmt(d_module_base,d_progress,'Initial state of zx gt tables:');
5659     log_header_tax_attributes(d_module_base,d_progress);
5660     log_line_tax_attributes(d_module_base,d_progress);
5661     log_dist_tax_attributes(d_module_base,d_progress);
5662   END IF;
5663 
5664   BEGIN
5665     SELECT 'Y'
5666     INTO l_clear_tables
5667     FROM dual
5668     WHERE EXISTS
5669       (SELECT 'CLEAR'
5670        FROM zx_transaction_lines_gt zxlgt
5671        WHERE zxlgt.line_level_action <> 'DELETE');
5672   EXCEPTION
5673     WHEN NO_DATA_FOUND THEN
5674       l_clear_tables := 'N';
5675   END;
5676 
5677   d_progress := 10;
5678   IF PO_LOG.d_stmt THEN
5679     PO_LOG.stmt(d_module_base,d_progress,'l_clear_tables = '||l_clear_tables);
5680   END IF;
5681 
5682   IF (l_clear_tables = 'Y') THEN
5683     wipe_zx_gt_tables();
5684   END IF;
5685   d_progress := 20;
5686 
5687   IF (PO_LOG.d_proc) THEN
5688     PO_LOG.proc_end(d_module_base);
5689   END IF;
5690 EXCEPTION
5691   WHEN OTHERS THEN
5692     IF (PO_LOG.d_exc) THEN
5693       PO_LOG.exc(d_module_base, d_progress, SQLCODE || SQLERRM);
5694     END IF;
5695 END initialize_zx_gt_tables;
5696 
5697 
5698 
5699 -----------------------------------------------------------------------------
5700 --Start of Comments
5701 --Name: wipe_zx_gt_tables
5702 --Pre-reqs:
5703 --  None
5704 --Modifies:
5705 --  ZX_TRX_HEADERS_GT
5706 --  ZX_TRANSACTION_LINES_GT
5707 --  ZX_ITM_DISTRIBUTIONS_GT
5708 --  ZX_VALIDATION_ERRORS_GT
5709 --  ZX_ERRORS_GT
5710 --Locks:
5711 --  ZX_TRX_HEADERS_GT
5712 --  ZX_TRANSACTION_LINES_GT
5713 --  ZX_ITM_DISTRIBUTIONS_GT
5714 --  ZX_VALIDATION_ERRORS_GT
5715 --  ZX_ERRORS_GT
5716 --Function:
5717 --  Wipe out all data from ZX Global Temporary tables
5718 --Parameters:
5719 --IN:
5720 --Notes:
5721 --  For internal use only. Used within this package by calculate_tax,
5722 --  calculate_tax_requisition and initialize_zx_gt_tables
5723 --  Introduced with Bug 5363122
5724 --End of Comments
5725 -----------------------------------------------------------------------------
5726 PROCEDURE wipe_zx_gt_tables IS
5727   l_module_name CONSTANT VARCHAR2(100) := 'WIPE_ZX_GT_TABLES';
5728   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
5729                                             D_PACKAGE_BASE, l_module_name);
5730   d_progress NUMBER;
5731   l_clear_tables VARCHAR2(1);
5732 BEGIN
5733   d_progress := 0;
5734   IF (PO_LOG.d_proc) THEN
5735     PO_LOG.proc_begin(d_module_base);
5736   END IF;
5737 
5738   DELETE ZX_TRX_HEADERS_GT;
5739   d_progress := 10;
5740   DELETE ZX_TRANSACTION_LINES_GT;
5741   d_progress := 20;
5742   DELETE ZX_ITM_DISTRIBUTIONS_GT;
5743   d_progress := 30;
5744   DELETE ZX_VALIDATION_ERRORS_GT;
5745   d_progress := 40;
5746   DELETE ZX_ERRORS_GT;
5747 
5748   d_progress := 50;
5749   IF (PO_LOG.d_proc) THEN
5750     PO_LOG.proc_end(d_module_base);
5751   END IF;
5752 EXCEPTION
5753   WHEN OTHERS THEN
5754     IF (PO_LOG.d_exc) THEN
5755       PO_LOG.exc(d_module_base, d_progress, SQLCODE || SQLERRM);
5756     END IF;
5757 END wipe_zx_gt_tables;
5758 
5759 
5760 
5761 -----------------------------------------------------------------------------
5762 --Start of Comments
5763 --Name: initialize_global_error_record
5764 --Pre-reqs:
5765 --  None
5766 --Modifies:
5767 --  None
5768 --Locks:
5769 --  None
5770 --Function:
5771 --  Initialized global error record and resets its counter to 0
5772 --Parameters:
5773 --IN:
5774 --OUT:
5775 --End of Comments
5776 -----------------------------------------------------------------------------
5777 PROCEDURE initialize_global_error_record IS BEGIN
5778   G_TAX_ERRORS_TBL.error_level        := PO_TBL_VARCHAR20();
5779   G_TAX_ERRORS_TBL.document_type_code := PO_TBL_VARCHAR25();
5780   G_TAX_ERRORS_TBL.document_id        := PO_TBL_NUMBER();
5781   G_TAX_ERRORS_TBL.document_num       := PO_TBL_VARCHAR20(); --<9688923>
5782   G_TAX_ERRORS_TBL.line_id            := PO_TBL_NUMBER();
5783   G_TAX_ERRORS_TBL.line_num           := PO_TBL_NUMBER();
5784   G_TAX_ERRORS_TBL.line_location_id   := PO_TBL_NUMBER();
5785   G_TAX_ERRORS_TBL.shipment_num       := PO_TBL_NUMBER();
5786   G_TAX_ERRORS_TBL.distribution_id    := PO_TBL_NUMBER();
5787   G_TAX_ERRORS_TBL.distribution_num   := PO_TBL_NUMBER();
5788   G_TAX_ERRORS_TBL.message_text       := PO_TBL_VARCHAR2000();
5789 END initialize_global_error_record;
5790 
5791 
5792 
5793 -----------------------------------------------------------------------------
5794 --Start of Comments
5795 --Name: any_tax_attributes_updated
5796 --Pre-reqs:
5797 --  None
5798 --Modifies:
5799 --  None
5800 --Locks:
5801 --  None
5802 --Function
5803 --  Determine whether any tax attributes are modified or not.
5804 --Parameters:
5805 --IN:
5806 --p_doc_type
5807 --  document type
5808 --p_doc_level
5809 --  this determines the level of the document HEADER,LINE
5810 --p_doc_level_id
5811 --  unique identifier of the level
5812 --p_trx_currency
5813 --  currency of the transaction
5814 --p_rate_type
5815 --  rate type of the document
5816 --p_rate_date
5817 --  rate date of the document
5818 --p_rate
5819 --  currency rate
5820 --p_fob :
5821 --  FOB of the document
5822 --p_vendor_id
5823 --  Supplier id
5824 --p_vendor_site_id
5825 --  Supplier Site id
5826 --p_uom
5827 --  Unit of measure
5828 --p_price
5829 --  unit price
5830 --p_qty
5831 --  quantity
5832 --p_amt
5833 --  Amount
5834 --p_ship_to_org
5835 --  Ship to Org
5836 --p_ship_to_loc
5837 --  Ship to Location ID
5838 --p_need_by_date
5839 --  Need by date
5840 --p_src_doc
5841 --  Source document
5842 --p_src_ship
5843 --  Source Shipment
5844 --p_ccid
5845 --  Code combination id
5846 --p_tax_rec_rate
5847 --  Tax Recovery Rate
5848 --p_project
5849 --  Project
5850 --p_task
5851 --  Project Task
5852 --p_award
5853 --  Project Award
5854 --p_exp_type
5855 --  Project Expenditure Type
5856 --p_exp_org
5857 --  Project Expenditure Organization
5858 --p_exp_date
5859 --  Project Expenditure date
5860 -- OUT:
5861 --End of Comments
5862 -----------------------------------------------------------------------------
5863 FUNCTION any_tax_attributes_updated(
5864   p_doc_type        IN  VARCHAR2,
5865   p_doc_level       IN  VARCHAR2,
5866   p_doc_level_id    IN  NUMBER,
5867   p_trx_currency    IN  VARCHAR2  DEFAULT NULL,
5868   p_rate_type       IN  VARCHAR2  DEFAULT NULL,
5869   p_rate_date       IN  DATE      DEFAULT NULL,
5870   p_rate            IN  NUMBER    DEFAULT NULL,
5871   p_fob             IN  VARCHAR2  DEFAULT NULL,
5872   p_vendor_id       IN  NUMBER    DEFAULT NULL,
5873   p_vendor_site_id  IN  NUMBER    DEFAULT NULL,
5874   p_bill_to_loc     IN  NUMBER    DEFAULT NULL, --<ECO 5524555>
5875   p_uom             IN  VARCHAR2  DEFAULT NULL,
5876   p_price           IN  NUMBER    DEFAULT NULL,
5877   p_qty             IN  NUMBER    DEFAULT NULL,
5878   p_price_override  IN  NUMBER    DEFAULT NULL, --<Bug 5647417>
5879   p_amt             IN  NUMBER    DEFAULT NULL,
5880   p_ship_to_org     IN  NUMBER    DEFAULT NULL,
5881   p_ship_to_loc     IN  NUMBER    DEFAULT NULL,
5882   p_need_by_date    IN  DATE      DEFAULT NULL,
5883   p_src_doc         IN  NUMBER    DEFAULT NULL,
5884   p_src_ship        IN  NUMBER    DEFAULT NULL,
5885   p_ccid            IN  NUMBER    DEFAULT NULL,
5886   p_tax_rec_rate    IN  NUMBER    DEFAULT NULL,
5887   p_project         IN  NUMBER    DEFAULT NULL,
5888   p_task            IN  NUMBER    DEFAULT NULL,
5889   p_award           IN  NUMBER    DEFAULT NULL,
5890   p_exp_type        IN  VARCHAR2  DEFAULT NULL,
5891   p_exp_org         IN  NUMBER    DEFAULT NULL,
5892   p_exp_date        IN  DATE      DEFAULT NULL,
5893   p_dist_quantity_ordered  IN  NUMBER    DEFAULT NULL,
5894   p_dist_amount_ordered  IN  NUMBER    DEFAULT NULL
5895 ) RETURN BOOLEAN IS
5896   pragma AUTONOMOUS_TRANSACTION;
5897 
5898   l_trx_currency    PO_HEADERS_ALL.CURRENCY_CODE%type;
5899   l_rate_type       PO_HEADERS_ALL.RATE_TYPE %type;
5900   l_rate_date       PO_HEADERS_ALL. RATE_DATE%type;
5901   l_rate            PO_HEADERS_ALL. RATE%type;
5902   l_fob             PO_HEADERS_ALL.FOB_LOOKUP_CODE%type ;
5903   l_vendor_id       PO_HEADERS_ALL.VENDOR_ID%type;
5904   l_vendor_site_id  PO_HEADERS_ALL.VENDOR_SITE_ID%type ;
5905   l_bill_to_loc     PO_HEADERS_ALL.BILL_TO_LOCATION_ID%type; --<ECO 5524555>
5906   l_uom             PO_LINES_ALL.UNIT_MEAS_LOOKUP_CODE%type;
5907   l_price           PO_LINES_ALL.UNIT_PRICE%type;
5908   l_qty             PO_LINE_LOCATIONS_ALL.QUANTITY%type;
5909   l_price_override  PO_LINE_LOCATIONS_ALL.PRICE_OVERRIDE%type; --<Bug 5647417>
5910   l_amt             PO_LINE_LOCATIONS_ALL.AMOUNT%type;
5911   l_ship_to_org     PO_LINE_LOCATIONS_ALL.SHIP_TO_ORGANIZATION_ID%type;
5912   l_ship_to_loc     PO_LINE_LOCATIONS_ALL.SHIP_TO_LOCATION_ID%type;
5913   l_need_by_date    PO_LINE_LOCATIONS_ALL.NEED_BY_DATE%type ;
5914   l_ccid            PO_DISTRIBUTIONS_ALL.CODE_COMBINATION_ID%type;
5915   l_tax_rec_rate    PO_DISTRIBUTIONS_ALL.RECOVERY_RATE%type;
5916   l_project         PO_DISTRIBUTIONS_ALL.PROJECT_ID%type;
5917   l_task            PO_DISTRIBUTIONS_ALL.TASK_ID%type;
5918   l_award           PO_DISTRIBUTIONS_ALL.AWARD_ID%type;
5919   l_exp_type        PO_DISTRIBUTIONS_ALL.EXPENDITURE_TYPE%type;
5920   l_exp_org         PO_DISTRIBUTIONS_ALL.EXPENDITURE_ORGANIZATION_ID%type;
5921   l_exp_date        PO_DISTRIBUTIONS_ALL.EXPENDITURE_ITEM_DATE%type;
5922   l_dist_quantity_ordered PO_DISTRIBUTIONS_ALL.QUANTITY_ORDERED%type;
5923   l_dist_amount_ordered PO_DISTRIBUTIONS_ALL.AMOUNT_ORDERED%type;
5924 
5925   l_module_name CONSTANT VARCHAR2(100) := 'ANY_TAX_ATTRIBUTES_UPDATED';
5926   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
5927                                            D_PACKAGE_BASE, l_module_name);
5928   d_progress NUMBER;
5929 BEGIN
5930 
5931   IF (PO_LOG.d_proc) THEN
5932     PO_LOG.proc_begin(d_module_base);
5933     PO_LOG.proc_begin(d_module_base, 'p_doc_type', p_doc_type);
5934     PO_LOG.proc_begin(d_module_base, 'p_doc_level', p_doc_level);
5935     PO_LOG.proc_begin(d_module_base, 'p_doc_level_id',p_doc_level_id );
5936     PO_LOG.proc_begin(d_module_base,'p_trx_currency',p_trx_currency);
5937     PO_LOG.proc_begin(d_module_base,'p_rate_type',p_rate_type);
5938     PO_LOG.proc_begin(d_module_base,'p_rate_date',p_rate_date);
5939     PO_LOG.proc_begin(d_module_base,'p_rate',p_rate);
5940     PO_LOG.proc_begin(d_module_base,'p_fob',p_fob);
5941     PO_LOG.proc_begin(d_module_base,'p_vendor_id',p_vendor_id);
5942     PO_LOG.proc_begin(d_module_base,'p_vendor_site_id',p_vendor_site_id);
5943     PO_LOG.proc_begin(d_module_base,'p_bill_to_loc',p_bill_to_loc);
5944     PO_LOG.proc_begin(d_module_base,'p_uom',p_uom);
5945     PO_LOG.proc_begin(d_module_base,'p_price',p_price);
5946     PO_LOG.proc_begin(d_module_base,'p_qty',p_qty);
5947     PO_LOG.proc_begin(d_module_base,'p_price_override',p_price_override);
5948     PO_LOG.proc_begin(d_module_base,'p_amt',p_amt);
5949     PO_LOG.proc_begin(d_module_base,'p_ship_to_org',p_ship_to_org);
5950     PO_LOG.proc_begin(d_module_base,'p_ship_to_loc',p_ship_to_loc);
5951     PO_LOG.proc_begin(d_module_base,'p_need_by_date',p_need_by_date);
5952     PO_LOG.proc_begin(d_module_base,'p_src_doc',p_src_doc);
5953     PO_LOG.proc_begin(d_module_base,'p_src_ship',p_src_ship);
5954     PO_LOG.proc_begin(d_module_base,'p_ccid',p_ccid);
5955     PO_LOG.proc_begin(d_module_base,'p_tax_rec_rate',p_tax_rec_rate);
5956     PO_LOG.proc_begin(d_module_base,'p_project',p_project);
5957     PO_LOG.proc_begin(d_module_base,'p_task',p_task);
5958     PO_LOG.proc_begin(d_module_base,'p_award',p_award);
5959     PO_LOG.proc_begin(d_module_base,'p_exp_type',p_exp_type);
5960     PO_LOG.proc_begin(d_module_base,'p_exp_org',p_exp_org);
5961     PO_LOG.proc_begin(d_module_base,'p_exp_date',p_exp_date);
5962   END IF;
5963 
5964   d_progress := 0;
5965   IF (p_doc_type = 'PO') THEN
5966     CASE p_doc_level
5967       WHEN 'HEADER' THEN
5968         BEGIN
5969           -- PO Header Tax determining attributes
5970           d_progress := 10;
5971           SELECT currency_code,
5972                  rate_type,
5973                  rate_date,
5974                  rate,
5975                  fob_lookup_code,
5976                  vendor_id,
5977                  vendor_site_id,
5978                  bill_to_location_id --<ECO 5524555>
5979           INTO   l_trx_currency,
5980                  l_rate_type,
5981                  l_rate_date,
5982                  l_rate,
5983                  l_fob,
5984                  l_vendor_id,
5985                  l_vendor_site_id,
5986                  l_bill_to_loc --<ECO 5524555>
5987           FROM   po_headers_all
5988           WHERE  po_header_id = p_doc_level_id;
5989         END;
5990         IF ((nvl(l_trx_currency,'-99') = nvl(p_trx_currency,'-99'))  AND
5991            (nvl(l_rate_type,'-99')    = nvl(p_rate_type,'-99'))     AND
5992            (nvl(l_rate_date,sysdate) = nvl(p_rate_date,sysdate)) AND
5993            (nvl(l_rate,99)         = nvl(p_rate,99))            AND
5994            (nvl(l_fob,99)          = nvl(p_fob,99))             AND
5995            (nvl(l_vendor_id,99)    = nvl(p_vendor_id,99))       AND
5996            (nvl(l_vendor_site_id,99) = nvl(p_vendor_site_id,99)) AND
5997            (nvl(l_bill_to_loc,-99) = nvl(p_bill_to_loc,-99)) ) --<ECO 5524555>
5998         THEN
5999           PO_LOG.stmt(d_module_base,d_progress,'header is unchanged');
6000           return(FALSE);
6001         ELSE
6002           PO_LOG.stmt(d_module_base,d_progress,'header is changed');
6003           return (TRUE);
6004         END IF;
6005       WHEN 'LINE' THEN
6006         -- Line Tax determining attributes
6007         d_progress := 20;
6008         SELECT unit_meas_lookup_code,
6009                unit_price
6010         INTO   l_uom,
6011                l_price
6012         FROM   po_lines_all
6013         WHERE  po_line_id =p_doc_level_id;
6014 
6015        IF nvl(l_uom,-99)    = nvl(p_uom,-99)   AND
6016           nvl(l_price,-99)  = nvl(p_price,-99)
6017         THEN
6018           PO_LOG.stmt(d_module_base,d_progress,'line is unchanged');
6019           return(FALSE);
6020         ELSE
6021           PO_LOG.stmt(d_module_base,d_progress,'line is changed');
6022           return (TRUE);
6023         END IF;
6024 
6025       WHEN 'SHIPMENT' THEN
6026         -- Shipment Tax determining attributes
6027         d_progress := 30;
6028         SELECT quantity,
6029                price_override, --<Bug 5647417>
6030                amount,
6031                ship_to_organization_id,
6032                ship_to_location_id,
6033                need_by_date
6034         INTO   l_qty,
6035                l_price_override, --<Bug 5647417>
6036                l_amt,
6037                l_ship_to_org,
6038                l_ship_to_loc,
6039                l_need_by_date
6040         FROM   po_line_locations_all
6041         WHERE  line_location_id = p_doc_level_id;
6042 
6043         IF nvl(l_qty ,-99)   =  nvl(p_qty ,-99)    AND
6044            --<Bug 5647417> Shipment level price is also tax determining
6045            -- attribute because it results in change of amount
6046            nvl(l_price_override ,-99)   =  nvl(p_price_override ,-99) AND
6047            nvl(l_amt ,-99)   =  nvl(p_amt ,-99)    AND
6048            nvl(l_ship_to_org ,-99)  = nvl(p_ship_to_org,-99)    AND
6049            nvl(l_ship_to_loc,-99) = nvl(p_ship_to_loc ,-99)     AND
6050            nvl(l_need_by_date ,sysdate) = nvl(p_need_by_date,sysdate)
6051         THEN
6052           PO_LOG.stmt(d_module_base,d_progress,'shipment is unchanged');
6053           return(FALSE);
6054         ELSE
6055           PO_LOG.stmt(d_module_base,d_progress,'shipment is changed');
6056           return (TRUE);
6057         END IF;
6058 
6059       WHEN 'DISTRIBUTION' THEN
6060         -- Distribution Level Tax determining attributes
6061         d_progress := 40;
6062         SELECT CODE_COMBINATION_ID,
6063                RECOVERY_RATE,
6064                PROJECT_ID,
6065                TASK_ID,
6066                AWARD_ID,
6067                EXPENDITURE_TYPE,
6068                EXPENDITURE_ORGANIZATION_ID,
6069                EXPENDITURE_ITEM_DATE,
6070                QUANTITY_ORDERED,
6071                AMOUNT_ORDERED
6072         INTO   l_ccid,
6073                l_tax_rec_rate,
6074                l_project,
6075                l_task ,
6076                l_award,
6077                l_exp_type,
6078                l_exp_org,
6079                l_exp_date,
6080                l_dist_quantity_ordered,
6081                l_dist_amount_ordered
6082         FROM   po_distributions_all
6083         WHERE  po_distribution_id =p_doc_level_id;
6084 
6085         IF  nvl(l_tax_rec_rate,-99) =  nvl(p_tax_rec_rate ,-99)  AND
6086             nvl(l_project,-99)    = nvl(p_project,-99)         AND
6087             nvl(l_task,-99)     = nvl(p_task,-99)            AND
6088             nvl(l_award,-99)     = nvl(p_award,-99)           AND
6089             nvl(l_exp_type,-99)   = nvl(p_exp_type,-99)        AND
6090             nvl(l_exp_org,-99)   = nvl(p_exp_org,-99)         AND
6091             nvl(l_exp_date,sysdate) = nvl(p_exp_date,sysdate)    AND
6092             nvl(l_ccid,-99)   = nvl(p_ccid,-99)   AND
6093             nvl(l_dist_quantity_ordered,-99)  = nvl(p_dist_quantity_ordered,-99) AND
6094             nvl(l_dist_amount_ordered ,-99)  = nvl(p_dist_amount_ordered,-99)
6095         THEN
6096           PO_LOG.stmt(d_module_base,d_progress,'distribution is unchanged');
6097           return(FALSE);
6098         ELSE
6099           PO_LOG.stmt(d_module_base,d_progress,'distribution is changed');
6100           return (TRUE);
6101         END IF;
6102 
6103     END CASE;
6104 
6105   ELSIF (p_doc_type = 'RELEASE') THEN
6106     CASE p_doc_level
6107       WHEN 'HEADER' THEN
6108         -- RELEASE Header Tax determining attributes
6109         d_progress := 50;
6110         SELECT currency_code,
6111                rate_type,
6112                rate_date,
6113                rate,
6114                fob_lookup_code,
6115                vendor_id,
6116                vendor_site_id
6117         INTO   l_trx_currency,
6118                l_rate_type,
6119                l_rate_date,
6120                l_rate,
6121                l_fob,
6122                l_vendor_id,
6123                l_vendor_site_id
6124         FROM   po_headers_all
6125         WHERE  po_header_id = p_doc_level_id;
6126 
6127        IF ((nvl(l_trx_currency,'-99') = nvl(p_trx_currency,'-99'))  AND
6128            (nvl(l_rate_type,'-99')    = nvl(p_rate_type,'-99'))     AND
6129            (nvl(l_rate_date,sysdate) = nvl(p_rate_date,sysdate)) AND
6130            (nvl(l_rate,99)         = nvl(p_rate,99))            AND
6131            (nvl(l_fob,99)          = nvl(p_fob,99))             AND
6132            (nvl(l_vendor_id,99)    = nvl(p_vendor_id,99))       AND
6133            (nvl(l_vendor_site_id,99) = nvl(p_vendor_site_id,99)) AND
6134            (nvl(l_bill_to_loc,-99) = nvl(p_bill_to_loc,-99)) ) --<ECO 5524555>
6135         THEN
6136           PO_LOG.stmt(d_module_base,d_progress,'release header is unchanged');
6137           return(FALSE);
6138         ELSE
6139           PO_LOG.stmt(d_module_base,d_progress,'release header is changed');
6140           return (TRUE);
6141         END IF;
6142 
6143     END CASE;
6144 
6145   END IF;
6146   d_progress := 60;
6147 EXCEPTION
6148    WHEN OTHERS THEN
6149      IF (PO_LOG.d_exc) THEN
6150        PO_LOG.exc(d_module_base, d_progress, SQLCODE || SQLERRM);
6151      END IF;
6152      return(TRUE);
6153 
6154 END any_tax_attributes_updated;
6155 
6156 
6157 
6158 -----------------------------------------------------------------------------
6159 --Start of Comments
6160 --Name: log_header_tax_attributes
6161 --Pre-reqs:
6162 --  None
6163 --Modifies:
6164 --  None
6165 --Locks:
6166 --  None
6167 --Function:
6168 --  Logs attributes in table zx_trx_headers_all
6169 --Parameters:
6170 --IN:
6171 --p_module_base
6172 --  Unique identifier for module in the package
6173 --p_progress
6174 --  Measure of depth traversed in the procedure
6175 --Notes:
6176 --  Used by calculate_tax, calculate_tax_req and determine_recovery_po/rel/req
6177 --  procedures. For use within this package only.
6178 --End of Comments
6179 -----------------------------------------------------------------------------
6180 PROCEDURE log_header_tax_attributes(p_module_base IN VARCHAR2,
6181                                     p_progress    IN NUMBER) IS
6182   d_module_base CONSTANT VARCHAR2(100) := p_module_base;
6183   d_progress NUMBER := p_progress;
6184   TYPE zx_headers_type IS TABLE OF ZX_TRX_HEADERS_GT%ROWTYPE;
6185   l_zx_headers_tbl zx_headers_type;
6186 BEGIN
6187   PO_LOG.stmt(d_module_base,d_progress,'Contents of zx_trx_headers_gt:');
6188 
6189   SELECT *
6190   BULK COLLECT INTO l_zx_headers_tbl
6191   FROM zx_trx_headers_gt;
6192 
6193   FOR i IN 1..l_zx_headers_tbl.COUNT LOOP
6194     PO_LOG.stmt(d_module_base,d_progress,'  row '||i||':');
6195     PO_LOG.stmt(d_module_base,d_progress,'    internal_organization_id = '||l_zx_headers_tbl(i).internal_organization_id);
6196     PO_LOG.stmt(d_module_base,d_progress,'    application_id = '||l_zx_headers_tbl(i).application_id);
6197     PO_LOG.stmt(d_module_base,d_progress,'    entity_code = '||l_zx_headers_tbl(i).entity_code);
6198     PO_LOG.stmt(d_module_base,d_progress,'    event_class_code = '||l_zx_headers_tbl(i).event_class_code);
6199     PO_LOG.stmt(d_module_base,d_progress,'    event_type_code = '||l_zx_headers_tbl(i).event_type_code);
6200     PO_LOG.stmt(d_module_base,d_progress,'    trx_id = '||l_zx_headers_tbl(i).trx_id);
6201     PO_LOG.stmt(d_module_base,d_progress,'    trx_date = '||l_zx_headers_tbl(i).trx_date);
6202     PO_LOG.stmt(d_module_base,d_progress,'    trx_doc_revision = '||l_zx_headers_tbl(i).trx_doc_revision);
6203     PO_LOG.stmt(d_module_base,d_progress,'    ledger_id = '||l_zx_headers_tbl(i).ledger_id);
6204     PO_LOG.stmt(d_module_base,d_progress,'    trx_currency_code = '||l_zx_headers_tbl(i).trx_currency_code);
6205     PO_LOG.stmt(d_module_base,d_progress,'    currency_conversion_date = '||l_zx_headers_tbl(i).currency_conversion_date);
6206     PO_LOG.stmt(d_module_base,d_progress,'    currency_conversion_rate = '||l_zx_headers_tbl(i).currency_conversion_rate);
6207     PO_LOG.stmt(d_module_base,d_progress,'    currency_conversion_type = '||l_zx_headers_tbl(i).currency_conversion_type);
6208     PO_LOG.stmt(d_module_base,d_progress,'    minimum_accountable_unit = '||l_zx_headers_tbl(i).minimum_accountable_unit);
6209     PO_LOG.stmt(d_module_base,d_progress,'    precision = '||l_zx_headers_tbl(i).precision);
6210     PO_LOG.stmt(d_module_base,d_progress,'    legal_entity_id = '||l_zx_headers_tbl(i).legal_entity_id);
6211     PO_LOG.stmt(d_module_base,d_progress,'    rounding_ship_from_party_id = '||l_zx_headers_tbl(i).rounding_ship_from_party_id);
6212     PO_LOG.stmt(d_module_base,d_progress,'    default_taxation_country = '||l_zx_headers_tbl(i).default_taxation_country);
6213     PO_LOG.stmt(d_module_base,d_progress,'    quote_flag = '||l_zx_headers_tbl(i).quote_flag);
6214     PO_LOG.stmt(d_module_base,d_progress,'    trx_number = '||l_zx_headers_tbl(i).trx_number);
6215     PO_LOG.stmt(d_module_base,d_progress,'    trx_description = '||l_zx_headers_tbl(i).trx_description);
6216     PO_LOG.stmt(d_module_base,d_progress,'    trx_communicated_date = '||l_zx_headers_tbl(i).trx_communicated_date);
6217     PO_LOG.stmt(d_module_base,d_progress,'    document_sub_type = '||l_zx_headers_tbl(i).document_sub_type);
6218     PO_LOG.stmt(d_module_base,d_progress,'    provnl_tax_determination_date = '||l_zx_headers_tbl(i).provnl_tax_determination_date);
6219     PO_LOG.stmt(d_module_base,d_progress,'    rounding_bill_to_party_id = '||l_zx_headers_tbl(i).rounding_bill_to_party_id);
6220     PO_LOG.stmt(d_module_base,d_progress,'    icx_session_id = '||l_zx_headers_tbl(i).icx_session_id);
6221   END LOOP;
6222 EXCEPTION
6223   WHEN OTHERS THEN
6224     PO_LOG.stmt(d_module_base,d_progress,'Failure while writing log '||SQLCODE||SQLERRM);
6225 END log_header_tax_attributes;
6226 
6227 
6228 
6229 -----------------------------------------------------------------------------
6230 --Start of Comments
6231 --Name: log_line_tax_attributes
6232 --Pre-reqs:
6233 --  None
6234 --Modifies:
6235 --  None
6236 --Locks:
6237 --  None
6238 --Function:
6239 --  Logs attributes in table zx_transaction_lines_all
6240 --Parameters:
6241 --IN:
6242 --p_module_base
6243 --  Unique identifier for module in the package
6244 --p_progress
6245 --  Measure of depth traversed in the procedure
6246 --Notes:
6247 --  Used by calculate_tax, calculate_tax_req and determine_recovery_po/rel/req
6248 --  procedures. For use within this package only.
6249 --End of Comments
6250 -----------------------------------------------------------------------------
6251 PROCEDURE log_line_tax_attributes(p_module_base IN VARCHAR2,
6252                                   p_progress    IN NUMBER) IS
6253   d_module_base CONSTANT VARCHAR2(100) := p_module_base;
6254   d_progress NUMBER := p_progress;
6255   TYPE zx_lines_type IS TABLE OF ZX_TRANSACTION_LINES_GT%ROWTYPE;
6256   l_zx_lines_tbl zx_lines_type;
6257 BEGIN
6258   PO_LOG.stmt(d_module_base,d_progress,'Contents of zx_transaction_lines_gt:');
6259 
6260   SELECT *
6261   BULK COLLECT INTO l_zx_lines_tbl
6262   FROM zx_transaction_lines_gt;
6263 
6264   FOR i IN 1..l_zx_lines_tbl.COUNT LOOP
6265     PO_LOG.stmt(d_module_base,d_progress,'  row '||i||':');
6266     PO_LOG.stmt(d_module_base,d_progress,'    application_id = '||l_zx_lines_tbl(i).application_id);
6267     PO_LOG.stmt(d_module_base,d_progress,'    entity_code = '||l_zx_lines_tbl(i).entity_code);
6268     PO_LOG.stmt(d_module_base,d_progress,'    event_class_code = '||l_zx_lines_tbl(i).event_class_code);
6269     PO_LOG.stmt(d_module_base,d_progress,'    trx_id = '||l_zx_lines_tbl(i).trx_id);
6270     PO_LOG.stmt(d_module_base,d_progress,'    trx_level_type = '||l_zx_lines_tbl(i).trx_level_type);
6271     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_id = '||l_zx_lines_tbl(i).trx_line_id);
6272     PO_LOG.stmt(d_module_base,d_progress,'    line_level_action = '||l_zx_lines_tbl(i).line_level_action);
6273     PO_LOG.stmt(d_module_base,d_progress,'    line_class = '||l_zx_lines_tbl(i).line_class);
6274     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_type = '||l_zx_lines_tbl(i).trx_line_type);
6275     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_date = '||l_zx_lines_tbl(i).trx_line_date);
6276     PO_LOG.stmt(d_module_base,d_progress,'    trx_business_category = '||l_zx_lines_tbl(i).trx_business_category);
6277     PO_LOG.stmt(d_module_base,d_progress,'    line_intended_use = '||l_zx_lines_tbl(i).line_intended_use);
6278     PO_LOG.stmt(d_module_base,d_progress,'    user_defined_fisc_class = '||l_zx_lines_tbl(i).user_defined_fisc_class);
6279     PO_LOG.stmt(d_module_base,d_progress,'    line_amt = '||l_zx_lines_tbl(i).line_amt);
6280     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_quantity = '||l_zx_lines_tbl(i).trx_line_quantity);
6281     PO_LOG.stmt(d_module_base,d_progress,'    product_id = '||l_zx_lines_tbl(i).product_id);
6282     PO_LOG.stmt(d_module_base,d_progress,'    product_fisc_classification = '||l_zx_lines_tbl(i).product_fisc_classification);
6283     PO_LOG.stmt(d_module_base,d_progress,'    uom_code = '||l_zx_lines_tbl(i).uom_code);
6284     PO_LOG.stmt(d_module_base,d_progress,'    product_type = '||l_zx_lines_tbl(i).product_type);
6285     PO_LOG.stmt(d_module_base,d_progress,'    product_code = '||l_zx_lines_tbl(i).product_code);
6286     PO_LOG.stmt(d_module_base,d_progress,'    product_category = '||l_zx_lines_tbl(i).product_category);
6287     PO_LOG.stmt(d_module_base,d_progress,'    fob_point = '||l_zx_lines_tbl(i).fob_point);
6288     PO_LOG.stmt(d_module_base,d_progress,'    ship_from_party_id = '||l_zx_lines_tbl(i).ship_from_party_id);
6289     PO_LOG.stmt(d_module_base,d_progress,'    bill_from_party_id = '||l_zx_lines_tbl(i).bill_from_party_id);
6290     PO_LOG.stmt(d_module_base,d_progress,'    ship_from_party_site_id = '||l_zx_lines_tbl(i).ship_from_party_site_id);
6291     PO_LOG.stmt(d_module_base,d_progress,'    bill_from_party_site_id = '||l_zx_lines_tbl(i).bill_from_party_site_id);
6292     PO_LOG.stmt(d_module_base,d_progress,'    ship_to_location_id = '||l_zx_lines_tbl(i).ship_to_location_id);
6293     PO_LOG.stmt(d_module_base,d_progress,'    ship_from_location_id = '||l_zx_lines_tbl(i).ship_from_location_id);
6294     PO_LOG.stmt(d_module_base,d_progress,'    bill_to_location_id = '||l_zx_lines_tbl(i).bill_to_location_id);
6295     PO_LOG.stmt(d_module_base,d_progress,'    bill_from_location_id = '||l_zx_lines_tbl(i).bill_from_location_id); /* 8752470 */
6296     PO_LOG.stmt(d_module_base,d_progress,'    account_ccid = '||l_zx_lines_tbl(i).account_ccid);
6297     PO_LOG.stmt(d_module_base,d_progress,'    ref_doc_application_id = '||l_zx_lines_tbl(i).ref_doc_application_id);
6298     PO_LOG.stmt(d_module_base,d_progress,'    ref_doc_entity_code = '||l_zx_lines_tbl(i).ref_doc_entity_code);
6299     PO_LOG.stmt(d_module_base,d_progress,'    ref_doc_event_class_code = '||l_zx_lines_tbl(i).ref_doc_event_class_code);
6300     PO_LOG.stmt(d_module_base,d_progress,'    ref_doc_trx_id = '||l_zx_lines_tbl(i).ref_doc_trx_id);
6301     PO_LOG.stmt(d_module_base,d_progress,'    ref_doc_line_id = '||l_zx_lines_tbl(i).ref_doc_line_id);
6302     PO_LOG.stmt(d_module_base,d_progress,'    line_trx_user_key1 = '||l_zx_lines_tbl(i).line_trx_user_key1);
6303     PO_LOG.stmt(d_module_base,d_progress,'    line_trx_user_key2 = '||l_zx_lines_tbl(i).line_trx_user_key2);
6304     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_number = '||l_zx_lines_tbl(i).trx_line_number);
6305     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_description = '||l_zx_lines_tbl(i).trx_line_description);
6306     PO_LOG.stmt(d_module_base,d_progress,'    product_description = '||l_zx_lines_tbl(i).product_description);
6307     PO_LOG.stmt(d_module_base,d_progress,'    assessable_value = '||l_zx_lines_tbl(i).assessable_value);
6308     PO_LOG.stmt(d_module_base,d_progress,'    line_amt_includes_tax_flag = '||l_zx_lines_tbl(i).line_amt_includes_tax_flag);
6309     PO_LOG.stmt(d_module_base,d_progress,'    input_tax_classification_code = '||l_zx_lines_tbl(i).input_tax_classification_code);
6310     PO_LOG.stmt(d_module_base,d_progress,'    source_application_id = '||l_zx_lines_tbl(i).source_application_id);
6311     PO_LOG.stmt(d_module_base,d_progress,'    source_entity_code = '||l_zx_lines_tbl(i).source_entity_code);
6312     PO_LOG.stmt(d_module_base,d_progress,'    source_event_class_code = '||l_zx_lines_tbl(i).source_event_class_code);
6313     PO_LOG.stmt(d_module_base,d_progress,'    source_trx_id = '||l_zx_lines_tbl(i).source_trx_id);
6314     PO_LOG.stmt(d_module_base,d_progress,'    source_line_id = '||l_zx_lines_tbl(i).source_line_id);
6315     PO_LOG.stmt(d_module_base,d_progress,'    source_trx_level_type = '||l_zx_lines_tbl(i).source_trx_level_type);
6316     PO_LOG.stmt(d_module_base,d_progress,'    unit_price = '||l_zx_lines_tbl(i).unit_price);
6317     PO_LOG.stmt(d_module_base,d_progress,'    ref_doc_trx_level_type = '||l_zx_lines_tbl(i).ref_doc_trx_level_type);
6318     PO_LOG.stmt(d_module_base,d_progress,'    product_org_id = '||l_zx_lines_tbl(i).product_org_id);
6319     PO_LOG.stmt(d_module_base,d_progress,'    ship_to_party_id = '||l_zx_lines_tbl(i).ship_to_party_id);
6320     PO_LOG.stmt(d_module_base,d_progress,'    bill_to_party_id = '||l_zx_lines_tbl(i).bill_to_party_id);
6321     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_currency_code = '||l_zx_lines_tbl(i).trx_line_currency_code);
6322     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_currency_conv_date = '||l_zx_lines_tbl(i).trx_line_currency_conv_date);
6323     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_currency_conv_rate = '||l_zx_lines_tbl(i).trx_line_currency_conv_rate);
6324     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_currency_conv_type = '||l_zx_lines_tbl(i).trx_line_currency_conv_type);
6325     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_mau = '||l_zx_lines_tbl(i).trx_line_mau);
6326     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_precision = '||l_zx_lines_tbl(i).trx_line_precision);
6327     PO_LOG.stmt(d_module_base,d_progress,'    user_upd_det_factors_flag = '||l_zx_lines_tbl(i).user_upd_det_factors_flag);
6328   END LOOP;
6329 EXCEPTION
6330   WHEN OTHERS THEN
6331     PO_LOG.stmt(d_module_base,d_progress,'Failure while writing log '||SQLCODE||SQLERRM);
6332 END log_line_tax_attributes;
6333 
6334 
6335 
6336 -----------------------------------------------------------------------------
6337 --Start of Comments
6338 --Name: log_dist_tax_attributes
6339 --Pre-reqs:
6340 --  None
6341 --Modifies:
6342 --  None
6343 --Locks:
6344 --  None
6345 --Function:
6346 --  Logs attributes in table zx_itm_distributions_all
6347 --Parameters:
6348 --IN:
6349 --p_module_base
6350 --  Unique identifier for module in the package
6351 --p_progress
6352 --  Measure of depth traversed in the procedure
6353 --Notes:
6354 --  Used by calculate_tax, calculate_tax_req and determine_recovery_po/rel/req
6355 --  procedures. For use within this package only.
6356 --End of Comments
6357 -----------------------------------------------------------------------------
6358 PROCEDURE log_dist_tax_attributes(p_module_base IN VARCHAR2,
6359                                   p_progress    IN NUMBER) IS
6360   d_module_base CONSTANT VARCHAR2(100) := p_module_base;
6361   d_progress NUMBER := p_progress;
6362   TYPE zx_dists_type IS TABLE OF ZX_ITM_DISTRIBUTIONS_GT%ROWTYPE;
6363   l_zx_dists_tbl zx_dists_type;
6364 BEGIN
6365   PO_LOG.stmt(d_module_base,d_progress,'Contents of zx_itm_distributions_gt:');
6366 
6367   SELECT *
6368   BULK COLLECT INTO l_zx_dists_tbl
6369   FROM zx_itm_distributions_gt;
6370 
6371   FOR i IN 1..l_zx_dists_tbl.COUNT LOOP
6372     PO_LOG.stmt(d_module_base,d_progress,'  row '||i||':');
6373     PO_LOG.stmt(d_module_base,d_progress,'    application_id = '||l_zx_dists_tbl(i).application_id);
6374     PO_LOG.stmt(d_module_base,d_progress,'    entity_code = '||l_zx_dists_tbl(i).entity_code);
6375     PO_LOG.stmt(d_module_base,d_progress,'    event_class_code = '||l_zx_dists_tbl(i).event_class_code);
6376     PO_LOG.stmt(d_module_base,d_progress,'    trx_id = '||l_zx_dists_tbl(i).trx_id);
6377     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_id = '||l_zx_dists_tbl(i).trx_line_id);
6378     PO_LOG.stmt(d_module_base,d_progress,'    trx_level_type = '||l_zx_dists_tbl(i).trx_level_type);
6379     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_dist_id = '||l_zx_dists_tbl(i).trx_line_dist_id);
6380     PO_LOG.stmt(d_module_base,d_progress,'    dist_level_action = '||l_zx_dists_tbl(i).dist_level_action);
6381     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_dist_date = '||l_zx_dists_tbl(i).trx_line_dist_date);
6382     PO_LOG.stmt(d_module_base,d_progress,'    item_dist_number = '||l_zx_dists_tbl(i).item_dist_number);
6383     PO_LOG.stmt(d_module_base,d_progress,'    task_id = '||l_zx_dists_tbl(i).task_id);
6384     PO_LOG.stmt(d_module_base,d_progress,'    award_id = '||l_zx_dists_tbl(i).award_id);
6385     PO_LOG.stmt(d_module_base,d_progress,'    project_id = '||l_zx_dists_tbl(i).project_id);
6386     PO_LOG.stmt(d_module_base,d_progress,'    expenditure_type = '||l_zx_dists_tbl(i).expenditure_type);
6387     PO_LOG.stmt(d_module_base,d_progress,'    expenditure_organization_id = '||l_zx_dists_tbl(i).expenditure_organization_id);
6388     PO_LOG.stmt(d_module_base,d_progress,'    expenditure_item_date = '||l_zx_dists_tbl(i).expenditure_item_date);
6389     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_dist_amt = '||l_zx_dists_tbl(i).trx_line_dist_amt);
6390     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_dist_qty = '||l_zx_dists_tbl(i).trx_line_dist_qty);
6391     PO_LOG.stmt(d_module_base,d_progress,'    trx_line_quantity = '||l_zx_dists_tbl(i).trx_line_quantity);
6392     PO_LOG.stmt(d_module_base,d_progress,'    account_ccid = '||l_zx_dists_tbl(i).account_ccid);
6393     PO_LOG.stmt(d_module_base,d_progress,'    currency_exchange_rate = '||l_zx_dists_tbl(i).currency_exchange_rate);
6394     PO_LOG.stmt(d_module_base,d_progress,'    overriding_recovery_rate = '||l_zx_dists_tbl(i).overriding_recovery_rate);
6395     PO_LOG.stmt(d_module_base,d_progress,'    dist_intended_use = '||l_zx_dists_tbl(i).dist_intended_use);
6396     PO_LOG.stmt(d_module_base,d_progress,'    historical_flag = '||l_zx_dists_tbl(i).historical_flag);
6397   END LOOP;
6398 EXCEPTION
6399   WHEN OTHERS THEN
6400     PO_LOG.stmt(d_module_base,d_progress,'Failure while writing log '||SQLCODE||SQLERRM);
6401 END log_dist_tax_attributes;
6402 
6403 
6404 
6405 -----------------------------------------------------------------------------
6406 --Start of Comments
6407 --Name: log_po_tauc
6408 --Pre-reqs:
6409 --  None
6410 --Modifies:
6411 --  None
6412 --Locks:
6413 --  None
6414 --Function:
6415 --  Logs tax_attribute_update_code values for the given list of po's
6416 --Parameters:
6417 --IN:
6418 --p_module_base
6419 --  Unique identifier for module in the package
6420 --p_progress
6421 --  Measure of depth traversed in the procedure
6422 --p_po_header_id_tbl
6423 --  List of po header id's to log for
6424 --Notes:
6425 --  1. Used by calculate_tax procedure. For use within this package only
6426 --  2. Everything is not collected in bulk because requirement is to log data
6427 --     in a structured format. In any case, this procedure is called only
6428 --     statement logging is on
6429 --End of Comments
6430 -----------------------------------------------------------------------------
6431 PROCEDURE log_po_tauc(p_module_base      IN VARCHAR2,
6432                       p_progress         IN NUMBER,
6433                       p_po_header_id_tbl IN PO_TBL_NUMBER) IS
6434   d_module_base   CONSTANT VARCHAR2(100) := p_module_base;
6435   d_progress      NUMBER := p_progress;
6436   l_line_id_tbl   PO_TBL_NUMBER;
6437   l_ship_id_tbl   PO_TBL_NUMBER;
6438   l_dist_id_tbl   PO_TBL_NUMBER;
6439   l_header_tauc   PO_HEADERS_ALL.tax_attribute_update_code%TYPE;
6440   l_line_tauc_tbl PO_TBL_VARCHAR15;
6441   l_ship_tauc_tbl PO_TBL_VARCHAR15;
6442   l_dist_tauc_tbl PO_TBL_VARCHAR15;
6443 BEGIN
6444 
6445   FOR i IN 1..p_po_header_id_tbl.COUNT LOOP
6446 
6447     SELECT ph.tax_attribute_update_code
6448     INTO l_header_tauc
6449     FROM po_headers_all ph
6450     WHERE ph.po_header_id = p_po_header_id_tbl(i);
6451 
6452     PO_LOG.stmt(d_module_base,d_progress,'  po_header_id = '||p_po_header_id_tbl(i)||':'||l_header_tauc);
6453 
6454     l_line_id_tbl := null; l_line_tauc_tbl := null;
6455     SELECT pl.po_line_id, pl.tax_attribute_update_code
6456     BULK COLLECT INTO l_line_id_tbl, l_line_tauc_tbl
6457     FROM po_lines_all pl
6458     WHERE pl.po_header_id = p_po_header_id_tbl(i);
6459 
6460     FOR j IN 1..l_line_id_tbl.COUNT LOOP
6461       PO_LOG.stmt(d_module_base,d_progress,'    po_line_id = '||l_line_id_tbl(j)||':'||l_line_tauc_tbl(j));
6462 
6463       l_ship_id_tbl := null; l_ship_tauc_tbl := null;
6464       SELECT pll.line_location_id, pll.tax_attribute_update_code
6465       BULK COLLECT INTO l_ship_id_tbl, l_ship_tauc_tbl
6466       FROM po_line_locations_all pll
6467       WHERE pll.po_line_id = l_line_id_tbl(j);
6468 
6469       FOR k IN 1..l_ship_id_tbl.COUNT LOOP
6470         PO_LOG.stmt(d_module_base,d_progress,'      line_location_id = '||l_ship_id_tbl(k)||':'||l_ship_tauc_tbl(k));
6471 
6472         l_dist_id_tbl := null; l_dist_tauc_tbl := null;
6473         SELECT pd.po_distribution_id, pd.tax_attribute_update_code
6474         BULK COLLECT INTO l_dist_id_tbl, l_dist_tauc_tbl
6475         FROM po_distributions_all pd
6476         WHERE pd.line_location_id = l_ship_id_tbl(k);
6477 
6478         FOR l IN 1..l_dist_id_tbl.COUNT LOOP
6479           PO_LOG.stmt(d_module_base,d_progress,'        po_distribution_id = '||l_dist_id_tbl(l)||':'||l_dist_tauc_tbl(l));
6480         END LOOP;
6481 
6482       END LOOP;
6483 
6484     END LOOP;
6485 
6486   END LOOP;
6487 
6488 EXCEPTION
6489   WHEN OTHERS THEN
6490     PO_LOG.stmt(d_module_base,d_progress,'Failure while writing log '||SQLCODE||SQLERRM);
6491 END log_po_tauc;
6492 
6493 
6494 
6495 -----------------------------------------------------------------------------
6496 --Start of Comments
6497 --Name: log_rel_tauc
6498 --Pre-reqs:
6499 --  None
6500 --Modifies:
6501 --  None
6502 --Locks:
6503 --  None
6504 --Function:
6505 --  Logs tax_attribute_update_code values for the given list of releaes
6506 --Parameters:
6507 --IN:
6508 --p_module_base
6509 --  Unique identifier for module in the package
6510 --p_progress
6511 --  Measure of depth traversed in the procedure
6512 --p_po_release_id_tbl
6513 --  List of po header id's to log for
6514 --Notes:
6515 --  1. Used by calculate_tax procedure. For use within this package only
6516 --  2. Everything is not collected in bulk because requirement is to log data
6517 --     in a structured format. In any case, this procedure is called only
6518 --     statement logging is on
6519 --End of Comments
6520 -----------------------------------------------------------------------------
6521 PROCEDURE log_rel_tauc(p_module_base       IN VARCHAR2,
6522                        p_progress          IN NUMBER,
6523                        p_po_release_id_tbl IN PO_TBL_NUMBER) IS
6524   d_module_base   CONSTANT VARCHAR2(100) := p_module_base;
6525   d_progress      NUMBER := p_progress;
6526   l_ship_id_tbl   PO_TBL_NUMBER;
6527   l_dist_id_tbl   PO_TBL_NUMBER;
6528   l_header_tauc   PO_RELEASES_ALL.tax_attribute_update_code%TYPE;
6529   l_ship_tauc_tbl PO_TBL_VARCHAR15;
6530   l_dist_tauc_tbl PO_TBL_VARCHAR15;
6531 BEGIN
6532 
6533   FOR i IN 1..p_po_release_id_tbl.COUNT LOOP
6534 
6535     SELECT pr.tax_attribute_update_code
6536     INTO l_header_tauc
6537     FROM po_releases_all pr
6538     WHERE pr.po_release_id = p_po_release_id_tbl(i);
6539 
6540     PO_LOG.stmt(d_module_base,d_progress,'  po_release_id = '||p_po_release_id_tbl(i)||':'||l_header_tauc);
6541 
6542     l_ship_id_tbl := null; l_ship_tauc_tbl := null;
6543     SELECT pll.line_location_id, pll.tax_attribute_update_code
6544     BULK COLLECT INTO l_ship_id_tbl, l_ship_tauc_tbl
6545     FROM po_line_locations_all pll
6546     WHERE pll.po_release_id = p_po_release_id_tbl(i);
6547 
6548     FOR j IN 1..l_ship_id_tbl.COUNT LOOP
6549       PO_LOG.stmt(d_module_base,d_progress,'      line_location_id = '||l_ship_id_tbl(j)||':'||l_ship_tauc_tbl(j));
6550 
6551       l_dist_id_tbl := null; l_dist_tauc_tbl := null;
6552       SELECT pd.po_distribution_id, pd.tax_attribute_update_code
6553       BULK COLLECT INTO l_dist_id_tbl, l_dist_tauc_tbl
6554       FROM po_distributions_all pd
6555       WHERE pd.line_location_id = l_ship_id_tbl(j);
6556 
6557       FOR k IN 1..l_dist_id_tbl.COUNT LOOP
6558         PO_LOG.stmt(d_module_base,d_progress,'        po_distribution_id = '||l_dist_id_tbl(k)||':'||l_dist_tauc_tbl(k));
6559       END LOOP;
6560 
6561     END LOOP;
6562 
6563   END LOOP;
6564 
6565 EXCEPTION
6566   WHEN OTHERS THEN
6567     PO_LOG.stmt(d_module_base,d_progress,'Failure while writing log '||SQLCODE||SQLERRM);
6568 END log_rel_tauc;
6569 
6570 
6571 
6572 -----------------------------------------------------------------------------
6573 --Start of Comments
6574 --Name: log_req_tauc
6575 --Pre-reqs:
6576 --  None
6577 --Modifies:
6578 --  None
6579 --Locks:
6580 --  None
6581 --Function:
6582 --  Logs tax_attribute_update_code values for the given requisition
6583 --Parameters:
6584 --IN:
6585 --p_module_base
6586 --  Unique identifier for module in the package
6587 --p_progress
6588 --  Measure of depth traversed in the procedure
6589 --p_requisition_header_id
6590 --  req header id to log for
6591 --Notes:
6592 --  Used by calculate_tax_requisition procedure. For use within this
6593 --  package only
6594 --End of Comments
6595 -----------------------------------------------------------------------------
6596 PROCEDURE log_req_tauc(p_module_base           IN VARCHAR2,
6597                        p_progress              IN NUMBER,
6598                        p_requisition_header_id IN NUMBER) IS
6599   d_module_base   CONSTANT VARCHAR2(100) := p_module_base;
6600   d_progress      NUMBER := p_progress;
6601   l_line_id_tbl   PO_TBL_NUMBER;
6602   l_header_tauc   PO_REQUISITION_HEADERS_ALL.tax_attribute_update_code%TYPE;
6603   l_line_tauc_tbl PO_TBL_VARCHAR15;
6604 BEGIN
6605 
6606   SELECT prh.tax_attribute_update_code
6607   INTO l_header_tauc
6608   FROM po_requisition_headers_all prh
6609   WHERE prh.requisition_header_id = p_requisition_header_id;
6610 
6611   PO_LOG.stmt(d_module_base,d_progress,'  requisition_header_id = '||p_requisition_header_id||':'||l_header_tauc);
6612 
6613   l_line_id_tbl := null; l_line_tauc_tbl := null;
6614   SELECT prl.requisition_line_id, prl.tax_attribute_update_code
6615   BULK COLLECT INTO l_line_id_tbl, l_line_tauc_tbl
6616   FROM po_requisition_lines_all prl
6617   WHERE prl.requisition_header_id = p_requisition_header_id;
6618 
6619   FOR i IN 1..l_line_id_tbl.COUNT LOOP
6620     PO_LOG.stmt(d_module_base,d_progress,'    requisition_line_id = '||l_line_id_tbl(i)||':'||l_line_tauc_tbl(i));
6621   END LOOP;
6622 
6623 EXCEPTION
6624   WHEN OTHERS THEN
6625     PO_LOG.stmt(d_module_base,d_progress,'Failure while writing log '||SQLCODE||SQLERRM);
6626 END log_req_tauc;
6627 
6628 
6629 -----------------------------------------------------------------------------
6630 --Start of Comments
6631 --Name: log_global_error_record
6632 --Pre-reqs:
6633 --  None
6634 --Modifies:
6635 --  None
6636 --Locks:
6637 --  None
6638 --Function:
6639 --  Logs global tax error record G_TAX_ERRORS_TBL
6640 --Parameters:
6641 --IN:
6642 --p_module_base
6643 --  Unique identifier for module in the package
6644 --p_progress
6645 --  Measure of depth traversed in the procedure
6646 --Notes:
6647 --  Used by calculate_tax and calculate_tax_requisition procedures.
6648 --  For use within this package only
6649 --End of Comments
6650 -----------------------------------------------------------------------------
6651 PROCEDURE log_global_error_record(p_module_base           IN VARCHAR2,
6652                                   p_progress              IN NUMBER) IS
6653   d_module_base   CONSTANT VARCHAR2(100) := p_module_base;
6654   d_progress      NUMBER := p_progress;
6655 BEGIN
6656   PO_LOG.stmt(d_module_base,d_progress,'Global tax error record:');
6657   FOR i in 1..G_TAX_ERRORS_TBL.error_level.COUNT LOOP
6658     PO_LOG.stmt(d_module_base,d_progress,'  error level '||G_TAX_ERRORS_TBL.error_level(i));
6659     PO_LOG.stmt(d_module_base,d_progress,'  '||G_TAX_ERRORS_TBL.document_type_code(i)||' '||
6660                                               G_TAX_ERRORS_TBL.document_num(i)||'(id '||
6661                                               G_TAX_ERRORS_TBL.document_id(i)||'), '||
6662                                      'Line '||G_TAX_ERRORS_TBL.line_num(i)||'(id '||
6663                                               G_TAX_ERRORS_TBL.line_id(i)||'), '||
6664                                  'Shipment '||G_TAX_ERRORS_TBL.shipment_num(i)||'(id '||
6665                                               G_TAX_ERRORS_TBL.line_location_id(i)||'), '||
6666                              'Distribution '||G_TAX_ERRORS_TBL.distribution_num(i)||'(id '||
6667                                               G_TAX_ERRORS_TBL.distribution_id(i)||')');
6668     PO_LOG.stmt(d_module_base,d_progress,'  '||G_TAX_ERRORS_TBL.message_text(i));
6669   END LOOP;
6670 EXCEPTION
6671   WHEN OTHERS THEN
6672     PO_LOG.exc(d_module_base,d_progress,'Failure while logging global error record '||SQLCODE||SQLERRM);
6673 END log_global_error_record;
6674 
6675 
6676 -----------------------------------------------------------------------------
6677 --Start of Comments
6678 --Name: cancel_tax_lines
6679 --Pre-reqs:
6680 --  ZX GT tables should be populated with required data to cancel tax lines
6681 --Modifies:
6682 --  None
6683 --Locks:
6684 --  None
6685 --Function:
6686 --  Cancels tax lines after corresponding PO shipment has been cancelled
6687 --Parameters:
6688 --IN:
6689 --p_api_version
6690 --  Standard API specification parameter
6691 --p_init_msg_list
6692 --  Standard API specification parameter
6693 --p_commit
6694 --  Standard API specification parameter
6695 --p_validation_level
6696 --  Standard API specification parameter
6697 --OUT:
6698 --x_return_status
6699 --  Standard API specification parameter
6700 --x_msg_count
6701 --  Standard API specification parameter
6702 --x_msg_data
6703 --  Standard API specification parameter
6704 --Notes:
6705 --  Wrapper over ZX_API_PUB.calculate_tax for cancel. Called from pocca.lpc.
6706 --  Introduced with Bug 4695557.
6707 --End of Comments
6708 -----------------------------------------------------------------------------
6709 PROCEDURE cancel_tax_lines(p_document_type  IN VARCHAR2,
6710                            p_document_id    IN NUMBER,
6711                            p_line_id        IN NUMBER,
6712                            p_shipment_id    IN NUMBER,
6713                            x_return_status  OUT NOCOPY VARCHAR2,
6714                            x_msg_count      OUT NOCOPY NUMBER,
6715                            x_msg_data       OUT NOCOPY VARCHAR2
6716 ) IS
6717   l_module_name CONSTANT VARCHAR2(100) := 'CANCEL_TAX_LINES';
6718   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
6719                                             D_PACKAGE_BASE, l_module_name);
6720   d_progress NUMBER;
6721   l_org_id   PO_HEADERS_ALL.org_id%TYPE;
6722 BEGIN
6723 
6724   IF (PO_LOG.d_proc) THEN
6725     PO_LOG.proc_begin(d_module_base);
6726     PO_LOG.proc_begin(d_module_base, 'p_document_type', p_document_type);
6727     PO_LOG.proc_begin(d_module_base, 'p_document_id', p_document_id);
6728     PO_LOG.proc_begin(d_module_base, 'p_line_id', p_line_id);
6729     PO_LOG.proc_begin(d_module_base, 'p_shipment_id', p_shipment_id);
6730   END IF;
6731 
6732 --Bug 16366878: Added calls to below API's so
6733 --that ZX transaction tables data is wiped out for all type of documents in this API call.
6734 
6735       initialize_zx_gt_tables(); -- Wipe ZX GT tables clean
6736       initialize_global_error_record();
6737 
6738   d_progress := 0;
6739   IF p_document_type = PO_CONSTANTS_SV.PO THEN
6740     d_progress := 10;
6741     IF p_shipment_id <> 0 THEN
6742       d_progress := 20;
6743 
6744       /* bug 13896725 */
6745       initialize_zx_gt_tables(); -- Wipe ZX GT tables clean
6746       initialize_global_error_record();
6747       /* bug 13896725 */
6748 
6749       INSERT INTO zx_trx_headers_gt
6750       (
6751         internal_organization_id,
6752         application_id,
6753         entity_code,
6754         event_class_code,
6755         event_type_code,
6756         trx_id,
6757         trx_date,
6758         legal_entity_id,
6759         rounding_ship_from_party_id
6760       )
6761       SELECT
6762         ph.org_id,
6763         PO_CONSTANTS_SV.APPLICATION_ID,
6764         PO_CONSTANTS_SV.PO_ENTITY_CODE,
6765         PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE,
6766         PO_CONSTANTS_SV.PO_ADJUSTED,
6767         ph.po_header_id,
6768         sysdate, -- dummy value
6769         PO_CORE_S.get_default_legal_entity_id(ph.org_id),
6770         (SELECT party_id FROM po_vendors --rounding_ship_from_party_id
6771          WHERE vendor_id=ph.vendor_id)
6772       FROM po_headers_all ph
6773       WHERE ph.po_header_id = p_document_id;
6774 
6775       d_progress := 30;
6776       INSERT INTO zx_transaction_lines_gt
6777       (
6778         application_id,
6779         entity_code,
6780         event_class_code,
6781         trx_id,
6782         trx_level_type,
6783         trx_line_id,
6784         line_level_action,
6785         line_amt_includes_tax_flag,
6786         line_amt
6787       )
6788       VALUES
6789       (
6790         PO_CONSTANTS_SV.APPLICATION_ID,
6791         PO_CONSTANTS_SV.PO_ENTITY_CODE,
6792         PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE,
6793         p_document_id,
6794         PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE,
6795         p_shipment_id,
6796         'CANCEL',
6797         'N', -- dummy value
6798         0 -- dummy value
6799       );
6800 
6801       d_progress := 40;
6802       log_header_tax_attributes(d_module_base,d_progress);
6803       log_line_tax_attributes(d_module_base,d_progress);
6804 
6805       d_progress := 50;
6806       IF (PO_LOG.d_stmt) THEN
6807         PO_LOG.stmt(d_module_base,d_progress,'Before Calling ZX_API_PUB.calculate_tax');
6808       END IF;
6809 
6810       ZX_API_PUB.calculate_tax(
6811         p_api_version           =>   1.0,
6812         p_init_msg_list         =>   FND_API.G_TRUE,
6813         p_commit                =>   FND_API.G_FALSE,
6814         p_validation_level      =>   FND_API.G_VALID_LEVEL_FULL,
6815         x_return_status         =>   x_return_status,
6816         x_msg_count             =>   x_msg_count,
6817         x_msg_data              =>   x_msg_data);
6818 
6819       d_progress := 60;
6820       IF (PO_LOG.d_stmt) THEN
6821         PO_LOG.stmt(d_module_base,d_progress,'After Calling ZX_API_PUB.calculate_tax');
6822         PO_LOG.stmt(d_module_base,d_progress,'x_return_status', x_return_status);
6823         PO_LOG.stmt(d_module_base,d_progress,'x_msg_count', x_msg_count);
6824         PO_LOG.stmt(d_module_base,d_progress,'x_msg_data', x_msg_data);
6825       END IF;
6826 
6827     ELSIF p_line_id <> 0 THEN
6828       -- Partial document cancel. Populate eTax GT tables and call calculate_tax
6829       d_progress := 70;
6830       INSERT INTO zx_trx_headers_gt
6831       (
6832         internal_organization_id,
6833         application_id,
6834         entity_code,
6835         event_class_code,
6836         event_type_code,
6837         trx_id,
6838         trx_date,
6839         legal_entity_id,
6840         rounding_ship_from_party_id
6841       )
6842       SELECT
6843         ph.org_id,
6844         PO_CONSTANTS_SV.APPLICATION_ID,
6845         PO_CONSTANTS_SV.PO_ENTITY_CODE,
6846         PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE,
6847         PO_CONSTANTS_SV.PO_ADJUSTED,
6848         ph.po_header_id,
6849         sysdate, -- dummy value
6850         PO_CORE_S.get_default_legal_entity_id(ph.org_id),
6851         (SELECT party_id FROM po_vendors --rounding_ship_from_party_id
6852          WHERE vendor_id=ph.vendor_id)
6853       FROM po_headers_all ph
6854       WHERE ph.po_header_id = p_document_id;
6855 
6856       d_progress := 80;
6857       INSERT INTO zx_transaction_lines_gt
6858       (
6859         application_id,
6860         entity_code,
6861         event_class_code,
6862         trx_id,
6863         trx_level_type,
6864         trx_line_id,
6865         line_level_action,
6866         line_amt_includes_tax_flag,
6867         line_amt
6868       )
6869       SELECT
6870         PO_CONSTANTS_SV.APPLICATION_ID,
6871         PO_CONSTANTS_SV.PO_ENTITY_CODE,
6872         PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE,
6873         pll.po_header_id,
6874         PO_CONSTANTS_SV.PO_TRX_LEVEL_TYPE,
6875         pll.line_location_id,
6876         'CANCEL', --line_level_action
6877         'N', -- dummy value
6878          0 -- dummy value
6879       FROM po_line_locations_all pll
6880       WHERE pll.po_line_id = p_line_id;
6881 
6882       d_progress := 90;
6883       log_header_tax_attributes(d_module_base,d_progress);
6884       log_line_tax_attributes(d_module_base,d_progress);
6885 
6886       d_progress := 100;
6887       IF (PO_LOG.d_stmt) THEN
6888         PO_LOG.stmt(d_module_base,d_progress,'Before Calling ZX_API_PUB.calculate_tax');
6889       END IF;
6890 
6891       ZX_API_PUB.calculate_tax(
6892         p_api_version           =>   1.0,
6893         p_init_msg_list         =>   FND_API.G_TRUE,
6894         p_commit                =>   FND_API.G_FALSE,
6895         p_validation_level      =>   FND_API.G_VALID_LEVEL_FULL,
6896         x_return_status         =>   x_return_status,
6897         x_msg_count             =>   x_msg_count,
6898         x_msg_data              =>   x_msg_data);
6899 
6900       d_progress := 110;
6901       IF (PO_LOG.d_stmt) THEN
6902         PO_LOG.stmt(d_module_base,d_progress,'After Calling ZX_API_PUB.calculate_tax');
6903         PO_LOG.stmt(d_module_base,d_progress,'x_return_status', x_return_status);
6904         PO_LOG.stmt(d_module_base,d_progress,'x_msg_count', x_msg_count);
6905         PO_LOG.stmt(d_module_base,d_progress,'x_msg_data', x_msg_data);
6906       END IF;
6907     ELSIF p_document_id <> 0 THEN
6908       -- Complete document cancel. Call global_document_update API
6909       -- in Cancel mode
6910       d_progress := 120;
6911       SELECT ph.org_id
6912       INTO l_org_id
6913       FROM po_headers_all ph
6914       WHERE ph.po_header_id = p_document_id;
6915 
6916       d_progress := 130;
6917       IF (PO_LOG.d_stmt) THEN
6918          PO_LOG.stmt(d_module_base,d_progress,'l_org_id', l_org_id);
6919       END IF;
6920 
6921       d_progress := 140;
6922       PO_TAX_INTERFACE_PVT.global_document_update(
6923         p_api_version       =>  1.0,
6924         p_init_msg_list     =>  FND_API.G_FALSE,
6925         p_commit            =>  FND_API.G_FALSE,
6926         p_validation_level  =>  FND_API.G_VALID_LEVEL_FULL,
6927         x_return_status     =>  x_return_status,
6928         x_msg_count         =>  x_msg_count,
6929         x_msg_data          =>  x_msg_data,
6930         p_org_id            =>  l_org_id,
6931         p_document_type     =>  PO_CONSTANTS_SV.PO,
6932         p_document_id       =>  p_document_id,
6933         p_event_type_code   =>  PO_CONSTANTS_SV.PO_CANCELLED);
6934 
6935       d_progress := 150;
6936       IF (PO_LOG.d_stmt) THEN
6937         PO_LOG.stmt(d_module_base,d_progress,'After Calling PO_TAX_INTERFACE_PVT.global_document_update');
6938         PO_LOG.stmt(d_module_base,d_progress,'x_return_status', x_return_status);
6939         PO_LOG.stmt(d_module_base,d_progress,'x_msg_count', x_msg_count);
6940         PO_LOG.stmt(d_module_base,d_progress,'x_msg_data', x_msg_data);
6941       END IF;
6942     END IF; --IF p_shipment_id IS NOT NULL
6943   ELSIF p_document_type = PO_CONSTANTS_SV.RELEASE THEN
6944     d_progress := 160;
6945     IF p_shipment_id <> 0 THEN
6946       d_progress := 170;
6947       INSERT INTO zx_trx_headers_gt
6948       (
6949         internal_organization_id,
6950         application_id,
6951         entity_code,
6952         event_class_code,
6953         event_type_code,
6954         trx_id,
6955         trx_date,
6956         legal_entity_id,
6957         rounding_ship_from_party_id
6958       )
6959       SELECT
6960         pr.org_id, -- dummy value
6961         PO_CONSTANTS_SV.APPLICATION_ID,
6962         PO_CONSTANTS_SV.REL_ENTITY_CODE,
6963         PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE,
6964         PO_CONSTANTS_SV.REL_ADJUSTED,
6965         pr.po_release_id,
6966         sysdate, -- dummy value
6967         PO_CORE_S.get_default_legal_entity_id(pr.org_id),
6968         (SELECT party_id FROM po_vendors --rounding_ship_from_party_id
6969          WHERE vendor_id=ph.vendor_id)
6970       FROM po_releases_all pr, po_headers_all ph
6971       WHERE pr.po_release_id = p_document_id
6972       AND pr.po_header_id = ph.po_header_id;
6973 
6974       d_progress := 180;
6975       INSERT INTO zx_transaction_lines_gt
6976       (
6977         application_id,
6978         entity_code,
6979         event_class_code,
6980         trx_id,
6981         trx_level_type,
6982         trx_line_id,
6983         line_level_action,
6984         line_amt_includes_tax_flag,
6985         line_amt
6986       )
6987       VALUES
6988       (
6989         PO_CONSTANTS_SV.APPLICATION_ID,
6990         PO_CONSTANTS_SV.REL_ENTITY_CODE,
6991         PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE,
6992         p_document_id,
6993         PO_CONSTANTS_SV.REL_TRX_LEVEL_TYPE,
6994         p_shipment_id,
6995         'CANCEL',
6996         'N', -- dummy value
6997         0 -- dummy value
6998       );
6999 
7000       d_progress := 190;
7001       log_header_tax_attributes(d_module_base,d_progress);
7002       log_line_tax_attributes(d_module_base,d_progress);
7003 
7004       d_progress := 200;
7005       IF (PO_LOG.d_stmt) THEN
7006         PO_LOG.stmt(d_module_base,d_progress,'Before Calling ZX_API_PUB.calculate_tax');
7007       END IF;
7008 
7009       ZX_API_PUB.calculate_tax(
7010         p_api_version           =>   1.0,
7011         p_init_msg_list         =>   FND_API.G_TRUE,
7012         p_commit                =>   FND_API.G_FALSE,
7013         p_validation_level      =>   FND_API.G_VALID_LEVEL_FULL,
7014         x_return_status         =>   x_return_status,
7015         x_msg_count             =>   x_msg_count,
7016         x_msg_data              =>   x_msg_data);
7017 
7018       d_progress := 210;
7019       IF (PO_LOG.d_stmt) THEN
7020         PO_LOG.stmt(d_module_base,d_progress,'After Calling ZX_API_PUB.calculate_tax');
7021         PO_LOG.stmt(d_module_base,d_progress,'x_return_status', x_return_status);
7022         PO_LOG.stmt(d_module_base,d_progress,'x_msg_count', x_msg_count);
7023         PO_LOG.stmt(d_module_base,d_progress,'x_msg_data', x_msg_data);
7024       END IF;
7025 
7026     ELSIF p_document_id <> 0 THEN
7027       -- Complete document cancel. Call global_document_update API
7028       -- in Cancel mode
7029       d_progress := 220;
7030       SELECT pr.org_id
7031       INTO l_org_id
7032       FROM po_releases_all pr
7033       WHERE pr.po_release_id = p_document_id;
7034 
7035       d_progress := 230;
7036       IF (PO_LOG.d_stmt) THEN
7037          PO_LOG.stmt(d_module_base,d_progress,'l_org_id', l_org_id);
7038       END IF;
7039 
7040       d_progress := 240;
7041       PO_TAX_INTERFACE_PVT.global_document_update(
7042         p_api_version       =>  1.0,
7043         p_init_msg_list     =>  FND_API.G_FALSE,
7044         p_commit            =>  FND_API.G_FALSE,
7045         p_validation_level  =>  FND_API.G_VALID_LEVEL_FULL,
7046         x_return_status     =>  x_return_status,
7047         x_msg_count         =>  x_msg_count,
7048         x_msg_data          =>  x_msg_data,
7049         p_org_id            =>  l_org_id,
7050         p_document_type     =>  PO_CONSTANTS_SV.RELEASE,
7051         p_document_id       =>  p_document_id,
7052         p_event_type_code   =>  PO_CONSTANTS_SV.REL_CANCELLED);
7053 
7054       d_progress := 250;
7055       IF (PO_LOG.d_stmt) THEN
7056          PO_LOG.stmt(d_module_base,d_progress,'After Calling PO_TAX_INTERFACE_PVT.global_document_update');
7057          PO_LOG.stmt(d_module_base,d_progress,'x_return_status', x_return_status);
7058          PO_LOG.stmt(d_module_base,d_progress,'x_msg_count', x_msg_count);
7059          PO_LOG.stmt(d_module_base,d_progress,'x_msg_data', x_msg_data);
7060       END IF;
7061 
7062     END IF; --IF p_shipment_id IS NOT NULL
7063   END IF; --IF p_document_type = PO_CONSTANTS_SV.PO
7064 
7065   d_progress := 260;
7066   IF (PO_LOG.d_proc) THEN
7067     PO_LOG.proc_end(d_module_base);
7068     PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
7069     PO_LOG.proc_end(d_module_base, 'x_msg_count', x_msg_count);
7070     PO_LOG.proc_end(d_module_base, 'x_msg_data', x_msg_data);
7071   END IF;
7072 
7073   d_progress := 30;
7074 EXCEPTION
7075   WHEN OTHERS THEN
7076     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7077     IF (PO_LOG.d_exc) THEN
7078       PO_LOG.exc(d_module_base, d_progress, 'Unhandled exception in procedure cancel_tax_lines');
7079       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
7080       PO_LOG.proc_end(d_module_base, 'x_msg_count', x_msg_count);
7081       PO_LOG.proc_end(d_module_base, 'x_msg_data', x_msg_data);
7082     END IF;
7083 END cancel_tax_lines;
7084 
7085 
7086 -----------------------------------------------------------------------------
7087 --Start of Comments
7088 --Name: global_document_update
7089 --Pre-reqs:
7090 --  None
7091 --Modifies:
7092 --  None
7093 --Locks:
7094 --  None
7095 --Function:
7096 --  Wrapper over Etax API ZX_API_PUB.global_document_update
7097 --Parameters:
7098 --IN:
7099 --p_api_version
7100 --  Standard API specification parameter
7101 --p_init_msg_list
7102 --  Standard API specification parameter
7103 --p_commit
7104 --  Standard API specification parameter
7105 --p_validation_level
7106 --  Standard API specification parameter
7107 --p_org_id
7108 --  organization id on the document
7109 --p_document_type
7110 --  'PO' or 'RELEASE'
7111 --p_document_id
7112 --  po_header_id or po_release_id of the document
7113 --p_event_type_code
7114 --  document level action code taken from PO_CONSTANTS_SV
7115 --OUT:
7116 --x_return_status
7117 --  Standard API specification parameter
7118 --x_msg_count
7119 --  Standard API specification parameter
7120 --x_msg_data
7121 --  Standard API specification parameter
7122 --End of Comments
7123 -----------------------------------------------------------------------------
7124 PROCEDURE global_document_update(p_api_version      IN  NUMBER,
7125                                  p_init_msg_list    IN  VARCHAR2,
7126                                  p_commit           IN  VARCHAR2,
7127                                  p_validation_level IN  NUMBER,
7128                                  x_return_status    OUT NOCOPY VARCHAR2,
7129                                  x_msg_count        OUT NOCOPY NUMBER,
7130                                  x_msg_data         OUT NOCOPY VARCHAR2,
7131                                  p_org_id           IN  NUMBER,
7132                                  p_document_type    IN  VARCHAR2,
7133                                  p_document_id      IN  NUMBER,
7134                                  p_event_type_code  IN  VARCHAR2
7135 ) IS
7136   l_module_name CONSTANT VARCHAR2(100) := 'GLOBAL_DOCUMENT_UPDATE';
7137   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
7138                                             D_PACKAGE_BASE, l_module_name);
7139   d_progress NUMBER;
7140   l_trx_rec  ZX_API_PUB.transaction_rec_type;
7141 BEGIN
7142   IF (PO_LOG.d_proc) THEN
7143     PO_LOG.proc_begin(d_module_base);
7144     PO_LOG.proc_begin(d_module_base, 'p_api_version', p_api_version);
7145     PO_LOG.proc_begin(d_module_base, 'p_init_msg_list', p_init_msg_list);
7146     PO_LOG.proc_begin(d_module_base, 'p_commit', p_commit);
7147     PO_LOG.proc_begin(d_module_base, 'p_validation_level', p_validation_level);
7148     PO_LOG.proc_begin(d_module_base, 'p_org_id', p_org_id);
7149     PO_LOG.proc_begin(d_module_base, 'p_document_type', p_document_type);
7150     PO_LOG.proc_begin(d_module_base, 'p_document_id', p_document_id);
7151     PO_LOG.proc_begin(d_module_base, 'p_event_type_code', p_event_type_code);
7152   END IF;
7153 
7154   d_progress := 0;
7155   IF p_document_type = PO_CONSTANTS_SV.PO THEN
7156     l_trx_rec.entity_code      := PO_CONSTANTS_SV.PO_ENTITY_CODE;
7157     l_trx_rec.event_class_code := PO_CONSTANTS_SV.PO_EVENT_CLASS_CODE;
7158   ELSIF p_document_type = PO_CONSTANTS_SV.RELEASE THEN
7159     l_trx_rec.entity_code      := PO_CONSTANTS_SV.REL_ENTITY_CODE;
7160     l_trx_rec.event_class_code := PO_CONSTANTS_SV.REL_EVENT_CLASS_CODE;
7161   END IF;
7162 
7163   d_progress := 10;
7164   l_trx_rec.application_id           := PO_CONSTANTS_SV.APPLICATION_ID;
7165   l_trx_rec.internal_organization_id := p_org_id;
7166   l_trx_rec.event_type_code          := p_event_type_code;
7167   l_trx_rec.trx_id                   := p_document_id;
7168   l_trx_rec.application_doc_status   := null;
7169 
7170   d_progress := 20;
7171   IF (PO_LOG.d_stmt) THEN
7172     PO_LOG.stmt(d_module_base,d_progress,'l_trx_rec.application_id', l_trx_rec.application_id);
7173     PO_LOG.stmt(d_module_base,d_progress,'l_trx_rec.entity_code', l_trx_rec.entity_code);
7174     PO_LOG.stmt(d_module_base,d_progress,'l_trx_rec.event_class_code', l_trx_rec.event_class_code);
7175     PO_LOG.stmt(d_module_base,d_progress,'l_trx_rec.internal_organization_id', l_trx_rec.internal_organization_id);
7176     PO_LOG.stmt(d_module_base,d_progress,'l_trx_rec.event_type_code', l_trx_rec.event_type_code);
7177     PO_LOG.stmt(d_module_base,d_progress,'l_trx_rec.trx_id', l_trx_rec.trx_id);
7178     PO_LOG.stmt(d_module_base,d_progress,'l_trx_rec.application_doc_status', l_trx_rec.application_doc_status);
7179     PO_LOG.stmt(d_module_base,d_progress,'Before Calling ZX_API_PUB.global_document_update');
7180   END IF;
7181 
7182   d_progress := 30;
7183   ZX_API_PUB.global_document_update(
7184     p_api_version         =>  p_api_version,
7185     p_init_msg_list       =>  p_init_msg_list,
7186     p_commit              =>  p_commit,
7187     p_validation_level    =>  p_validation_level,
7188     x_return_status       =>  x_return_status,
7189     x_msg_count           =>  x_msg_count,
7190     x_msg_data            =>  x_msg_data,
7191     p_transaction_rec     =>  l_trx_rec);
7192 
7193   d_progress := 40;
7194   IF (PO_LOG.d_proc) THEN
7195     PO_LOG.proc_end(d_module_base);
7196     PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
7197     PO_LOG.proc_end(d_module_base, 'x_msg_count', x_msg_count);
7198     PO_LOG.proc_end(d_module_base, 'x_msg_data', x_msg_data);
7199   END IF;
7200 
7201   d_progress := 50;
7202 EXCEPTION
7203   WHEN OTHERS THEN
7204     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7205     IF (PO_LOG.d_exc) THEN
7206       PO_LOG.exc(d_module_base, d_progress, 'Unhandled exception in procedure global_document_update');
7207       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
7208       PO_LOG.proc_end(d_module_base, 'x_msg_count', x_msg_count);
7209       PO_LOG.proc_end(d_module_base, 'x_msg_data', x_msg_data);
7210     END IF;
7211 END global_document_update;
7212 
7213 
7214 -----------------------------------------------------------------------------
7215 --Start of Comments
7216 --Name: unapprove_doc_header
7217 --Pre-reqs:
7218 --  None
7219 --Modifies:
7220 --  PO_HEADERS_ALL.authorization_status
7221 --  PO_HEADERS_ALL.approved_flag
7222 --  PO_RELEASES_ALL.authorization_status
7223 --  PO_RELEASES_ALL.approved_flag
7224 --Locks:
7225 --  PO_HEADERS_ALL
7226 --  PO_RELEASES_ALL
7227 --Function:
7228 --  Unapprove the header for the given document
7229 --Parameters:
7230 --IN:
7231 --p_document_id
7232 --  Unique identifier for the document header that is to be unapproved
7233 --p_document_type
7234 --  Document Type (PO or RELEASE) for the document that is to be unapproved
7235 --OUT:
7236 --x_return_status
7237 --  Standard API specification parameter
7238 --  Can hold one of the following values:
7239 --    FND_API.G_RET_STS_SUCCESS (='S')
7240 --    FND_API.G_RET_STS_ERROR (='E')
7241 --    FND_API.G_RET_STS_UNEXP_ERROR (='U')
7242 --Notes:
7243 --  Called from ManageTaxSvrCmd when the user changes any Additional Tax
7244 --  Attribute on the Additional Tax Information page and presses Apply
7245 --End of Comments
7246 -----------------------------------------------------------------------------
7247 PROCEDURE unapprove_doc_header(p_document_id   IN         NUMBER,
7248                                p_document_type IN         VARCHAR2,
7249                                x_return_status OUT NOCOPY VARCHAR2
7250 ) IS
7251   l_module_name CONSTANT VARCHAR2(100) := 'UNAPPROVE_DOC_HEADER';
7252   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
7253                                             D_PACKAGE_BASE, l_module_name);
7254   d_progress NUMBER;
7255   l_authorization_status PO_HEADERS_ALL.authorization_status%TYPE;
7256 BEGIN
7257 
7258   IF (PO_LOG.d_proc) THEN
7259     PO_LOG.proc_begin(d_module_base);
7260     PO_LOG.proc_begin(d_module_base, 'p_document_id', p_document_id);
7261     PO_LOG.proc_begin(d_module_base, 'p_document_type', p_document_type);
7262   END IF;
7263 
7264   d_progress := 0;
7265   -- By default return status is SUCCESS if no exception occurs
7266   x_return_status := FND_API.G_RET_STS_SUCCESS;
7267 
7268   d_progress := 10;
7269   IF p_document_type = PO_CONSTANTS_SV.PO THEN
7270     SELECT ph.authorization_status
7271     INTO l_authorization_status
7272     FROM po_headers_all ph
7273     WHERE ph.po_header_id = p_document_id;
7274   ELSIF p_document_type = PO_CONSTANTS_SV.RELEASE THEN
7275     SELECT pr.authorization_status
7276     INTO l_authorization_status
7277     FROM po_releases_all pr
7278     WHERE pr.po_release_id = p_document_id;
7279   END IF;
7280 
7281   IF PO_LOG.d_stmt THEN
7282     PO_LOG.stmt(d_module_base,d_progress,'l_authorization_status='||l_authorization_status);
7283   END IF;
7284 
7285   IF l_authorization_status = PO_CONSTANTS_SV.PRE_APPROVED THEN
7286 
7287     d_progress := 20;
7288     IF p_document_type = PO_CONSTANTS_SV.PO THEN
7289       UPDATE po_headers_all ph
7290       SET ph.authorization_status = PO_CONSTANTS_SV.IN_PROCESS,
7291           ph.approved_flag = 'N'
7292       WHERE ph.po_header_id = p_document_id;
7293     ELSIF p_document_type = PO_CONSTANTS_SV.RELEASE THEN
7294       UPDATE po_releases_all pr
7295       SET pr.authorization_status = PO_CONSTANTS_SV.IN_PROCESS,
7296           pr.approved_flag = 'N'
7297       WHERE pr.po_release_id = p_document_id;
7298     END IF;
7299 
7300   ELSIF l_authorization_status = PO_CONSTANTS_SV.APPROVED THEN
7301 
7302     d_progress := 30;
7303     IF p_document_type = PO_CONSTANTS_SV.PO THEN
7304       UPDATE po_headers_all ph
7305       SET ph.authorization_status = PO_CONSTANTS_SV.REQUIRES_REAPPROVAL,
7306           ph.approved_flag = 'R'
7307       WHERE ph.po_header_id = p_document_id;
7308     ELSIF p_document_type = PO_CONSTANTS_SV.RELEASE THEN
7309       UPDATE po_releases_all pr
7310       SET pr.authorization_status = PO_CONSTANTS_SV.REQUIRES_REAPPROVAL,
7311           pr.approved_flag = 'R'
7312       WHERE pr.po_release_id = p_document_id;
7313     END IF;
7314 
7315   END IF;
7316 
7317   d_progress := 40;
7318   IF (PO_LOG.d_proc) THEN
7319     PO_LOG.proc_end(d_module_base);
7320     PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
7321   END IF;
7322 
7323 EXCEPTION
7324   WHEN OTHERS THEN
7325     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7326 
7327     IF (PO_LOG.d_exc) THEN
7328       PO_LOG.exc(d_module_base, d_progress, SQLCODE || SQLERRM);
7329       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
7330     END IF;
7331 END unapprove_doc_header;
7332 
7333 
7334 -----------------------------------------------------------------------------
7335 --Start of Comments
7336 --Name: unapprove_schedules
7337 --Pre-reqs:
7338 --  None
7339 --Modifies:
7340 --  PO_LINE_LOCATIONS_ALL.approved_flag
7341 --Locks:
7342 --  PO_LINE_LOCATIONS_ALL
7343 --Function:
7344 --  Unapprove the given schedules if they are approved
7345 --Parameters:
7346 --IN:
7347 --p_line_location_id_tbl
7348 --  List of schedule ids that were modified in the Additional Tax Information
7349 --  page
7350 --OUT:
7351 --x_return_status
7352 --  Standard API specification parameter
7353 --  Can hold one of the following values:
7354 --    FND_API.G_RET_STS_SUCCESS (='S')
7355 --    FND_API.G_RET_STS_ERROR (='E')
7356 --    FND_API.G_RET_STS_UNEXP_ERROR (='U')
7357 --Notes:
7358 --  Called from ManageTaxSvrCmd when the user changes any schedule level
7359 --  Additional Tax Attribute on the Additional Tax Information page and
7360 --  presses Apply
7361 --End of Comments
7362 -----------------------------------------------------------------------------
7363 PROCEDURE unapprove_schedules(p_line_location_id_tbl  IN PO_TBL_NUMBER,
7364                               x_return_status OUT NOCOPY VARCHAR2
7365 ) IS
7366   l_module_name CONSTANT VARCHAR2(100) := 'UNAPPROVE_SCHEDULES';
7367   d_module_base CONSTANT VARCHAR2(100) := PO_LOG.get_subprogram_base(
7368                                             D_PACKAGE_BASE, l_module_name);
7369   d_progress NUMBER;
7370 BEGIN
7371 
7372   IF (PO_LOG.d_proc) THEN
7373     PO_LOG.proc_begin(d_module_base);
7374     PO_LOG.proc_begin(d_module_base, 'p_line_location_id_tbl', p_line_location_id_tbl);
7375   END IF;
7376 
7377   d_progress := 0;
7378   -- By default return status is SUCCESS if no exception occurs
7379   x_return_status := FND_API.G_RET_STS_SUCCESS;
7380 
7381   -- Unapprove line locations that are approved
7382   d_progress := 10;
7383   FORALL i IN 1..p_line_location_id_tbl.COUNT
7384     UPDATE po_line_locations_all pll
7385     SET pll.approved_flag = 'R'
7386     WHERE pll.line_location_id = p_line_location_id_tbl(i)
7387     AND pll.approved_flag = 'Y';
7388 
7389   d_progress := 20;
7390   IF (PO_LOG.d_proc) THEN
7391     PO_LOG.proc_end(d_module_base);
7392     PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
7393   END IF;
7394 
7395 EXCEPTION
7396   WHEN OTHERS THEN
7397     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7398 
7399     IF (PO_LOG.d_exc) THEN
7400       PO_LOG.exc(d_module_base, d_progress, SQLCODE || SQLERRM);
7401       PO_LOG.proc_end(d_module_base, 'x_return_status', x_return_status);
7402     END IF;
7403 END unapprove_schedules;
7404 
7405 
7406 END PO_TAX_INTERFACE_PVT;