DBA Data[Home] [Help]

PACKAGE BODY: APPS.AP_MATCHING_PKG

Source


1 PACKAGE BODY AP_MATCHING_PKG AS
2 /*$Header: apmatchb.pls 120.90.12020000.3 2012/10/10 13:24:32 sbonala ship $*/
3 
4 G_PKG_NAME          CONSTANT VARCHAR2(30) := 'AP_MATCHING_PKG';
5 G_MSG_UERROR        CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR;
6 G_MSG_ERROR         CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_ERROR;
7 G_MSG_SUCCESS       CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_SUCCESS;
8 G_MSG_HIGH          CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_DEBUG_HIGH;
9 G_MSG_MEDIUM        CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_DEBUG_MEDIUM;
10 G_MSG_LOW           CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_DEBUG_LOW;
11 G_LINES_PER_FETCH   CONSTANT NUMBER       := 1000;
12 
13 G_CURRENT_RUNTIME_LEVEL CONSTANT NUMBER       := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
14 G_LEVEL_UNEXPECTED      CONSTANT NUMBER       := FND_LOG.LEVEL_UNEXPECTED;
15 G_LEVEL_ERROR           CONSTANT NUMBER       := FND_LOG.LEVEL_ERROR;
16 G_LEVEL_EXCEPTION       CONSTANT NUMBER       := FND_LOG.LEVEL_EXCEPTION;
17 G_LEVEL_EVENT           CONSTANT NUMBER       := FND_LOG.LEVEL_EVENT;
18 G_LEVEL_PROCEDURE       CONSTANT NUMBER       := FND_LOG.LEVEL_PROCEDURE;
19 G_LEVEL_STATEMENT       CONSTANT NUMBER       := FND_LOG.LEVEL_STATEMENT;
20 G_MODULE_NAME           CONSTANT VARCHAR2(100) := 'AP.PLSQL.AP_MATCHING_PKG.';
21 
22 --LOCAL PROCEDURES
23 PROCEDURE Get_Info(X_Invoice_ID  	 IN NUMBER,
24  		   X_Invoice_Line_Number IN NUMBER DEFAULT NULL,
25 		   X_Match_Amount	 IN NUMBER DEFAULT NULL,
26 		   X_Po_Line_Location_Id IN NUMBER DEFAULT NULL,
27 		   X_Calling_Sequence	 IN VARCHAR2 );
28 
29 Procedure Get_Dist_Proration_Info(
30 		  X_Invoice_Id		 IN NUMBER,
31 		  X_Invoice_Line_Number  IN NUMBER,
32 		  X_Po_Line_Location_Id  IN NUMBER,
33 	  	  X_Match_Mode		 IN VARCHAR2,
34 		  X_Match_Quantity	 IN NUMBER,
35 		  X_Match_Amount	 IN NUMBER,
36 		  X_Unit_Price		 IN NUMBER,
37 		  X_Overbill_Flag	 IN VARCHAR2,
38 		  X_Dist_Tab		 IN OUT NOCOPY DIST_TAB_TYPE,
39 		  X_Calling_Sequence	 IN VARCHAR2);
40 
41 PROCEDURE Get_Total_Proration_Quantity(
42                     X_PO_Line_Location_Id  IN 	NUMBER,
43                     X_Match_Mode     	   IN 	VARCHAR2,
44 		    X_Overbill_Flag  	   IN 	VARCHAR2,
45 		    X_Total_Quantity 	   OUT  NOCOPY NUMBER,
46 		    X_Calling_Sequence	   IN	VARCHAR2);
47 
48 
49 Procedure Update_PO_Shipments_Dists(
50 		    X_Dist_Tab	          IN OUT NOCOPY  Dist_Tab_Type,
51 		    X_Po_Line_Location_Id IN 	         NUMBER,
52 		    X_Match_Amount        IN   		 NUMBER,
53 		    X_Match_Quantity	  IN		 NUMBER,
54 		    X_Uom_Lookup_Code  	  IN  		 VARCHAR2,
55   		    X_Calling_Sequence    IN  		 VARCHAR2);
56 
57 
58 Procedure Insert_Invoice_Line (
59 		    X_Invoice_Id 	      IN NUMBER,
60 		    X_Invoice_Line_Number     IN NUMBER,
61 		    X_Line_Type_Lookup_Code   IN VARCHAR2,
62 		    X_Cost_Factor_id	      IN NUMBER DEFAULT NULL,
63 		    X_Single_Dist_Flag	      IN VARCHAR2 DEFAULT 'N',
64 		    X_Po_Distribution_Id      IN NUMBER DEFAULT NULL,
65        		    X_Po_Line_Location_Id     IN NUMBER DEFAULT NULL,
66 		    X_Amount		      IN NUMBER,
67 		    X_Quantity_Invoiced	      IN NUMBER DEFAULT NULL,
68 		    X_Unit_Price	      IN NUMBER DEFAULT NULL,
69 		    X_Final_Match_Flag	      IN VARCHAR2 DEFAULT NULL,
70 		    X_Item_Line_Number	      IN NUMBER,
71 		    X_Charge_Line_Description IN VARCHAR2,
72 		    X_Retained_Amount	      IN NUMBER	  DEFAULT NULL,
73 		    X_Calling_Sequence	      IN VARCHAR2);
74 
75 PROCEDURE Insert_Invoice_Distributions (
76 		    X_Invoice_ID	  IN 	 NUMBER,
77 	  	    X_Invoice_Line_Number IN	 NUMBER,
78 		    X_Dist_Tab		  IN OUT NOCOPY Dist_Tab_Type,
79 		    X_Final_Match_Flag	  IN	 VARCHAR2,
80 		    X_Unit_Price	  IN	 NUMBER,
81 		    X_Total_Amount	  IN     NUMBER,
82 	  	    X_Calling_Sequence	  IN	 VARCHAR2);
83 
84 
85 Procedure Create_Charge_Lines(
86 		    X_Invoice_Id 	  IN  NUMBER,
87 		    X_Freight_Cost_Factor_id IN NUMBER,
88     		    X_Freight_Amount   	  IN  NUMBER,
89     		    X_Freight_Description IN  VARCHAR2,
90 		    X_Misc_Cost_Factor_id IN  NUMBER,
91     		    X_Misc_Amount	  IN  NUMBER,
92   		    X_Misc_Description	  IN  VARCHAR2,
93     		    X_Item_Line_Number	  IN  NUMBER,
94     		    X_Calling_Sequence	  IN  VARCHAR2);
95 
96 PROCEDURE Get_Corr_Dist_Proration_Info(
97                     X_Corrected_Invoice_id  IN    NUMBER,
98 		    X_Corrected_Line_Number IN    NUMBER,
99 		    X_Corr_Dist_Tab         IN OUT NOCOPY CORR_DIST_TAB_TYPE,
100 		    X_Correction_Type       IN    VARCHAR2,
101 		    X_Correction_Amount     IN    NUMBER,
102 		    X_Correction_Quantity   IN    NUMBER,
103 		    X_Correction_Price      IN    NUMBER,
104 		    X_Match_Mode	    IN    VARCHAR2,
105 		    X_Calling_Sequence      IN    VARCHAR2);
106 
107 PROCEDURE Insert_Corr_Invoice_Line(
108 	 	    X_Invoice_Id            IN  NUMBER,
109 		    X_Invoice_Line_Number   IN  NUMBER,
110 		    X_Corrected_Invoice_Id  IN  NUMBER,
111 		    X_Corrected_Line_Number IN  NUMBER,
112 		    X_Quantity     	    IN  NUMBER,
113 		    X_Amount       	    IN  NUMBER,
114 		    X_Unit_Price   	    IN  NUMBER,
115 		    X_Correction_Type	    IN  VARCHAR2,
116 		    X_Final_Match_Flag	    IN  VARCHAR2,
117 		    X_Po_Distribution_Id    IN  NUMBER,
118 		    X_Retained_Amount	    IN  NUMBER DEFAULT NULL,
119 		    X_Calling_Sequence      IN  VARCHAR2);
120 
121 PROCEDURE Insert_Corr_Invoice_Dists(
122 		    X_Invoice_Id          IN  NUMBER,
123 		    X_Invoice_Line_Number IN  NUMBER,
124 		    X_Corrected_Invoice_Id IN NUMBER,
125 		    X_Corr_Dist_Tab       IN  OUT NOCOPY CORR_DIST_TAB_TYPE,
126 		    X_Correction_Type     IN  VARCHAR2,
127 		    X_Final_Match_Flag    IN  VARCHAR2,
128 		    X_Total_Amount     	  IN  NUMBER,
129 		    X_Calling_Sequence    IN  VARCHAR2);
130 
131 PROCEDURE Update_Corr_Po_Shipments_Dists(
132 		    X_Corr_Dist_Tab       IN CORR_DIST_TAB_TYPE,
133 		    X_Po_Line_Location_Id IN NUMBER,
134     		    X_Quantity            IN NUMBER,
135 		    X_Amount              IN NUMBER,
136                     X_Correction_Type     IN VARCHAR2,
137 		    X_Uom_Lookup_Code     IN VARCHAR2,
138 		    X_Calling_Sequence    IN VARCHAR2);
139 
140 PROCEDURE Get_Shipment_List_For_QM(
141 		    X_Invoice_Id       IN  NUMBER,
142 		    X_Po_Header_Id     IN  NUMBER,
143 		    X_Match_Option     IN  VARCHAR2,
144 		    X_Match_Amount     IN  NUMBER,
145 		    X_Shipment_Table   OUT NOCOPY T_SHIPMENT_TABLE,
146 		    X_Calling_Sequence IN  VARCHAR2);
147 
148 PROCEDURE Generate_Lines_For_QuickMatch (
149 		    X_Invoice_Id       IN  NUMBER,
150 		    X_Shipment_Table   IN  T_SHIPMENT_TABLE,
151 		    X_Match_Option     IN  VARCHAR2,
152 		    X_Calling_Sequence IN  VARCHAR2);
153 
154 Procedure Generate_Release_Lines (p_po_header_id     IN NUMBER,
155                                   p_invoice_id       IN NUMBER,
156                                   p_release_amount   IN NUMBER,
157                                   x_calling_sequence IN VARCHAR2);
158 
159 --Global Variable Declaration
160 G_Max_Invoice_Line_Number	ap_invoice_lines.line_number%TYPE := 0;
161 G_Batch_id			ap_batches.batch_id%TYPE;
162 G_Accounting_Date 		ap_invoice_lines.accounting_date%TYPE;
163 G_Period_Name    		gl_period_statuses.period_name%TYPE;
164 G_Set_of_Books_ID 		ap_system_parameters.set_of_books_id%TYPE;
165 G_Awt_Group_ID 			ap_awt_groups.group_id%TYPE;
166 G_Invoice_Type_Lookup_Code	ap_invoices.invoice_type_lookup_code%TYPE;
167 G_Exchange_Rate			ap_invoices.exchange_rate%TYPE;
168 G_Precision			fnd_currencies.precision%TYPE;
169 G_Min_Acct_Unit			fnd_currencies.minimum_accountable_unit%TYPE;
170 G_System_Allow_Awt_Flag		ap_system_parameters.allow_awt_flag%TYPE;
171 G_Site_Allow_Awt_Flag		po_vendor_sites.allow_awt_flag%TYPE;
172 G_Transfer_Flag			ap_system_parameters.transfer_desc_flex_flag%TYPE;
173 G_Base_Currency_Code		ap_system_parameters.base_currency_code%TYPE;
174 G_Invoice_Currency_Code		ap_invoices.invoice_currency_code%TYPE;
175 G_Allow_PA_Override		varchar2(1);
176 G_Pa_Expenditure_Date_Default	varchar2(50);
177 G_Prepay_CCID                   ap_system_parameters.prepay_code_combination_id%TYPE;
178 G_Build_Prepay_Accts_Flag       ap_system_parameters.build_prepayment_accounts_flag%TYPE;
179 G_Income_Tax_Region		ap_system_parameters.income_tax_region%TYPE;
180 G_Project_ID			pa_projects_all.project_id%TYPE;
181 G_Task_ID			pa_tasks.task_id%TYPE;
182 G_Award_ID			po_distributions_all.award_id%TYPE;
183 G_Expenditure_Type		pa_expenditure_types.expenditure_type%TYPE;
184 G_Invoice_Date			ap_invoices.invoice_date%TYPE;
185 G_Expenditure_Organization_ID	pa_exp_orgs_it.organization_id%TYPE;
186 G_Asset_Book_Type_Code		fa_book_controls.book_type_code%TYPE;
187 G_Asset_Category_Id		mtl_system_items.asset_category_id%TYPE;
188 G_Inventory_Organization_Id	financials_system_parameters.inventory_organization_id%TYPE;
189 G_Approval_Workflow_Flag	ap_system_parameters.approval_workflow_flag%TYPE;
190 -- Removed for bug 4277744
191 -- G_Ussgl_Transaction_Code	ap_invoices.ussgl_transaction_code%TYPE;
192 G_Allow_Flex_Override_Flag      ap_system_parameters.allow_flex_override_flag%TYPE;
193 G_Shipment_Type			po_line_locations.shipment_type%TYPE;
194 G_Org_id			ap_invoices.org_id%TYPE;
195 G_Encumbrance_Flag              financials_system_parameters.purch_encumbrance_flag%TYPE;
196 G_User_Id			number;
197 G_Login_Id			number;
198 G_Account_Segment	        ap_invoice_lines.account_segment%TYPE := NULL;
199 G_Balancing_Segment		ap_invoice_lines.balancing_segment%TYPE := NULL;
200 G_Cost_Center_Segment		ap_invoice_lines.cost_center_segment%TYPE := NULL;
201 G_Overlay_Dist_Code_Concat	ap_invoice_lines.overlay_dist_code_concat%TYPE := NULL;
202 G_Default_Dist_CCid		ap_invoice_lines.default_dist_ccid%TYPE := NULL;
203 G_Line_Project_Id		ap_invoice_lines.project_id%TYPE ;
204 G_Line_Task_Id			ap_invoice_lines.task_id%TYPE ;
205 G_Line_Award_ID			ap_invoice_lines.award_id%TYPE ;
206 G_Line_Expenditure_Type		ap_invoice_lines.expenditure_type%TYPE ;
207 G_Line_Expenditure_Item_Date    ap_invoice_lines.expenditure_item_date%TYPE ;
208 G_Line_Expenditure_Org_Id       ap_invoice_lines.expenditure_organization_id%TYPE ;
209 G_Line_Base_Amount		ap_invoice_lines.base_amount%TYPE ;
210 G_Line_Awt_Group_Id		ap_invoice_lines.awt_group_id%TYPE ;
211 G_Line_Accounting_Date		ap_invoice_lines.accounting_date%TYPE;
212 G_Trx_Business_Category	        ap_invoices.trx_business_category%TYPE;
213 --Contract Payments
214 G_Vendor_Id			ap_invoices.vendor_id%TYPE;
215 G_Vendor_Site_Id		ap_invoices.vendor_site_id%TYPE;
216 G_Po_Line_Id			po_lines_all.po_line_id%TYPE;
217 G_Recoupment_Rate		po_lines_all.recoupment_rate%TYPE;
218 G_Release_Amount_Net_Of_Tax	ap_invoices_all.release_amount_net_of_tax%TYPE;
219 --Bugfix:5565310
220 G_intended_use			zx_lines_det_factors.line_intended_use%type;
221 G_product_type			zx_lines_det_factors.product_type%type;
222 G_product_category		zx_lines_det_factors.product_category%type;
223 G_product_fisc_class		zx_lines_det_factors.product_fisc_classification%type;
224 G_user_defined_fisc_class       zx_lines_det_factors.user_defined_fisc_class%type;
225 G_assessable_value		zx_lines_det_factors.assessable_value%type;
226 G_dflt_tax_class_code		zx_transaction_lines_gt.input_tax_classification_code%type;
227 G_source			ap_invoices_all.source%type;
228 G_recurring_payment_id          ap_invoices.recurring_payment_id%TYPE;  -- Bug 7305223
229 G_PAY_AWT_GROUP_ID   ap_invoices_all.pay_awt_group_id%TYPE; -- bug8222382
230 G_Line_Pay_Awt_Group_Id		ap_invoice_lines.pay_awt_group_id%TYPE ; -- bug8222382
231 
232 PROCEDURE Base_Credit_PO_Match(X_match_mode    	  IN 	VARCHAR2,
233                    	    X_invoice_id    	  IN	NUMBER,
234 		   	    X_invoice_line_number IN	NUMBER,
235 		   	    X_Po_Line_Location_id IN	NUMBER,
236 		   	    X_Dist_Tab		  IN OUT NOCOPY DIST_TAB_TYPE,
237 		   	    X_amount	          IN	NUMBER,
238 		            X_quantity 		  IN 	NUMBER,
239 		            X_unit_price    	  IN 	NUMBER,
240 		            X_uom_lookup_code     IN	VARCHAR2,
241 		   	    X_final_match_flag 	  IN   	VARCHAR2,
242 			    X_overbill_flag 	  IN	VARCHAR2,
243 			    X_freight_cost_factor_id IN NUMBER DEFAULT NULL,
244 		  	    X_freight_amount	  IN	NUMBER,
245 			    X_freight_description IN	VARCHAR2,
246 			    X_misc_cost_factor_id IN    NUMBER DEFAULT NULL,
247 			    X_misc_amount	  IN	NUMBER,
248 			    X_misc_description	  IN	VARCHAR2,
249 			    X_retained_amount     IN    NUMBER DEFAULT NULL,
250 			    X_calling_sequence	  IN	VARCHAR2) IS
251 
252 l_single_dist_flag		varchar2(1) := 'N';
253 l_po_distribution_id		po_distributions.po_distribution_id%TYPE := NULL;
254 l_invoice_distribution_id	ap_invoice_distributions.invoice_distribution_id%TYPE;
255 l_item_line_number		ap_invoice_lines_all.line_number%TYPE;
256 l_line_amt_net_retainage        ap_invoice_lines_all.amount%TYPE;
257 l_max_amount_to_recoup		ap_invoice_lines_all.amount%TYPE;
258 l_amount_to_recoup	 	ap_invoice_lines_all.amount%TYPE;
259 l_retained_amount		ap_invoice_lines_all.retained_amount%TYPE;
260 l_error_message			varchar2(4000);
261 l_debug_info			varchar2(2000);
262 l_success			boolean;
263 current_calling_sequence	varchar2(2000);
264 l_api_name                      CONSTANT VARCHAR2(200) := 'Base_Credit_PO_Match';
265 
266 --bugfix:5565310
267 l_ref_doc_application_id      zx_transaction_lines_gt.ref_doc_application_id%TYPE;
268 l_ref_doc_entity_code         zx_transaction_lines_gt.ref_doc_entity_code%TYPE;
269 l_ref_doc_event_class_code    zx_transaction_lines_gt.ref_doc_event_class_code%TYPE;
270 l_ref_doc_line_quantity       zx_transaction_lines_gt.ref_doc_line_quantity%TYPE;
271 l_po_header_curr_conv_rat     po_headers_all.rate%TYPE;
272 l_ref_doc_trx_level_type      zx_transaction_lines_gt.ref_doc_trx_level_type%TYPE;
273 l_po_header_curr_conv_rate    po_headers_all.rate%TYPE;
274 l_uom_code                    mtl_units_of_measure.uom_code%TYPE;
275 l_ref_doc_trx_id	      po_headers_all.po_header_id%TYPE;
276 l_error_code		      varchar2(2000);
277 l_po_line_location_id         po_line_locations.line_location_id%TYPE;
278 l_dummy			      number;
279 -- bug 7577673: start
280 l_product_org_id              ap_invoices.org_id%TYPE;
281 l_allow_tax_code_override     varchar2(10);
282 l_dflt_tax_class_code         zx_transaction_lines_gt.input_tax_classification_code%type;
283 -- bug 7577673: end
284 
285 l_code_combination_id         NUMBER ; -- Bug 10050094
286 l_full_mtch_amt               NUMBER;  --Bug13505998
287 l_line_amt                    NUMBER;  --Bug13505998
288 BEGIN
289    -- Update the calling sequence (for error message).
290    current_calling_sequence := 'AP_MATCHING_PKG.base_credit_po_match<-'||X_calling_sequence;
291 
292    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
293       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
294       				'AP_MATCHING_PKG.base_credit_po_match(+)');
295    END IF;
296 
297    l_debug_info := 'Get Invoice and System Options information';
298    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
299       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
300    END IF;
301 
302    get_info(X_Invoice_Id 	  => x_invoice_id,
303    	    X_Invoice_Line_Number => x_invoice_line_number,
304 	    X_Match_Amount	  => x_amount,
305 	    X_Po_Line_Location_id => x_po_line_location_id,
306             X_Calling_Sequence    => current_calling_sequence);
307 
308    IF g_invoice_type_lookup_code <> 'PREPAYMENT' THEN
309       l_retained_amount := AP_INVOICE_LINES_UTILITY_PKG.Get_Retained_Amount
310                                         (p_line_location_id => x_po_line_location_id,
311                                          p_match_amount     => x_amount);
312    END IF;
313 
314    --If shipment level match then we need to prorate the match-quantity among the
315    --po distributions of the shipment, for distribution level match we need to
316    --derive the invoice_distribution_id, base_amount, ccid.
317 
318    l_debug_info := 'Get PO information';
319    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
320       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
321    END IF;
322 
323    --bugfix:5565310
324    l_po_line_location_id := x_po_line_location_id;
325 
326    l_success := AP_ETAX_UTILITY_PKG.Get_PO_Info(
327                  P_Po_Line_Location_Id         => l_po_line_location_id,
328                  P_PO_Distribution_Id          => null,
329                  P_Application_Id              => l_ref_doc_application_id,
330                  P_Entity_code                 => l_ref_doc_entity_code,
331                  P_Event_Class_Code            => l_ref_doc_event_class_code,
332                  P_PO_Quantity                 => l_ref_doc_line_quantity,
333                  P_Product_Org_Id              => l_product_org_id, -- bug 7577673
334                  P_Po_Header_Id                => l_ref_doc_trx_id,
335                  P_Po_Header_curr_conv_rate    => l_po_header_curr_conv_rate,
336                  P_Uom_Code                    => l_uom_code,
337 		 P_Dist_Qty		       => l_dummy,
338 		 P_Ship_Price		       => l_dummy,
339                  P_Error_Code                  => l_error_code,
340                  P_Calling_Sequence            => current_calling_sequence);
341 
342 
343     -- Bug 10050094
344     AP_ETAX_UTILITY_PKG.Get_Default_CCID( p_invoice_id          => x_invoice_id,
345                                           p_default_dist_ccid   => g_default_dist_ccid,
346                                           p_po_line_location_id => l_po_line_location_id,
347 					  p_invoice_line_number => X_invoice_line_number,
348                                           p_calling_sequence    => current_calling_sequence,
349                                           x_derived_ccid        => l_code_combination_id ) ;
350 
351    l_debug_info := 'Get PO Tax Attributes';
352    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
353          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
354    END IF;
355 
356      g_dflt_tax_class_code := null; /*bug12428818*/
357 
358 -- bug 7577673: start
359    ZX_AP_TAX_CLASSIFICATN_DEF_PKG.get_default_tax_classification
360            (p_ref_doc_application_id           => l_ref_doc_application_id,
361             p_ref_doc_entity_code              => l_ref_doc_entity_code,
362             p_ref_doc_event_class_code         => l_ref_doc_event_class_code,
363             p_ref_doc_trx_id                   => l_ref_doc_trx_id,
364             p_ref_doc_line_id                  => x_po_line_location_id,
365             p_ref_doc_trx_level_type           => 'SHIPMENT',
366             p_vendor_id                        => g_vendor_id,
367             p_vendor_site_id                   => g_vendor_site_id,
368             p_code_combination_id              => l_code_combination_id, -- Bug 10050094 g_default_dist_ccid,
369             p_concatenated_segments            => null,
370             p_templ_tax_classification_cd      => null,
371             p_ship_to_location_id              => null,
372             p_ship_to_loc_org_id               => null,
373             p_inventory_item_id                => null,
374             p_item_org_id                      => l_product_org_id,
375             p_tax_classification_code          => g_dflt_tax_class_code,
376             p_allow_tax_code_override_flag     => l_allow_tax_code_override,
377             APPL_SHORT_NAME                    => 'SQLAP',
378             FUNC_SHORT_NAME                    => 'NONE',
379             p_calling_sequence                 => 'AP_ETAX_SERVICES_PKG',
380             p_event_class_code                 => l_ref_doc_event_class_code,
381             p_entity_code                      => 'AP_INVOICES',
382             p_application_id                   => 200,
383             p_internal_organization_id         => g_org_id);
384 -- bug 7577673: end
385 
386    AP_Etax_Services_Pkg.Get_Po_Tax_Attributes(
387    			    p_application_id              => l_ref_doc_application_id,
388 			    p_org_id                      => g_org_id,
389 			    p_entity_code                 => l_ref_doc_entity_code,
390 			    p_event_class_code            => l_ref_doc_event_class_code,
391 			    p_trx_level_type              => 'SHIPMENT',
392 			    p_trx_id                      => l_ref_doc_trx_id,
393 			    p_trx_line_id                 => x_po_line_location_id,
394 			    x_line_intended_use           => g_intended_use,
395 			    x_product_type                => g_product_type,
396 			    x_product_category            => g_product_category,
397 			    x_product_fisc_classification => g_product_fisc_class,
398 			    x_user_defined_fisc_class     => g_user_defined_fisc_class,
399 			    x_assessable_value            => g_assessable_value,
400 			    x_tax_classification_code     => l_dflt_tax_class_code
401 			    );
402 
403 -- bug 7577673: start
404    -- if tax classification code not retrieved from hierarchy
405    -- retrieve it from PO
406    IF (g_dflt_tax_class_code is null) THEN
407        g_dflt_tax_class_code := l_dflt_tax_class_code;
408    END IF;
409 -- bug 7577673: end
410 
411    l_debug_info := 'g_intended_use,g_product_type,g_product_category,g_product_fisc_class '||
412                     g_intended_use||','||g_product_type||','||g_product_category||','||g_product_fisc_class;
413 
414    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
415         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
416    END IF;
417 
418    l_debug_info := 'g_user_defined_fisc_class,g_assessable_value,g_dflt_tax_class_code '||
419                     g_user_defined_fisc_class||','||g_assessable_value||','||g_dflt_tax_class_code;
420 
421    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
422         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
423    END IF;
424 
425    IF g_source = 'ISP'
426       and x_invoice_line_number IS NOT NULL THEN
427 
428     --Bug#13505998
429     l_full_mtch_amt :=
430          ap_matching_utils_pkg.get_full_mtch_amt
431             (X_Po_Line_Location_id,G_Invoice_Currency_Code);
432 
433    l_debug_info := 'l_full_mtch_amt '||l_full_mtch_amt;
434 
435    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
436         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
437    END IF;
438 
439 
440    SELECT AMOUNT
441    INTO l_line_amt
442    FROM ap_invoice_lines_all
443    WHERE invoice_id  = x_invoice_id
444      AND line_number = x_invoice_line_number;
445 
446    l_debug_info := 'l_line_amt '||l_line_amt;
447 
448    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
449         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
450    END IF;
451 
452   --End Bug#13505998
453 
454       UPDATE ap_invoice_lines_all
455          SET  primary_intended_use        = nvl(primary_intended_use, g_intended_use)
456              ,product_type                = nvl(product_type, g_product_type)
457              ,product_category            = nvl(product_category, g_product_category)
458              ,product_fisc_classification = nvl(product_fisc_classification, g_product_fisc_class)
459              ,user_defined_fisc_class     = nvl(user_defined_fisc_class, g_user_defined_fisc_class)
460              ,assessable_value            = nvl(assessable_value,
461                                                 decode(l_full_mtch_amt,l_line_amt,
462                                                        g_assessable_value,NULL)) /*Introduced decode for bug#13505998*/
463              ,tax_classification_code     = nvl(tax_classification_code, g_dflt_tax_class_code)
464        WHERE invoice_id  = x_invoice_id
465          AND line_number = x_invoice_line_number;
466 
467    END IF;
468 
469    l_debug_info := 'Get Distribution Proration Info';
470 
471 
472    Get_Dist_Proration_Info( X_Invoice_Id	  => x_invoice_id,
473 			    X_Invoice_Line_Number => x_invoice_line_number,
474 			    X_Po_Line_Location_Id => x_po_line_location_id,
475      			    X_Match_Mode	  => x_match_mode,
476     			    X_Match_Quantity	  => x_quantity,
477     			    X_Match_Amount	  => x_amount,
478 			    X_Unit_Price	  => x_unit_price,
479     			    X_Overbill_Flag	  => x_overbill_flag,
480 			    X_Dist_Tab 	          => x_dist_tab,
481     			    X_Calling_Sequence    => current_calling_sequence);
482 
483 
484    IF (x_dist_tab.COUNT = 1) THEN
485 
486       l_single_dist_flag := 'Y';
487       l_po_distribution_id := x_dist_tab.FIRST;
488       l_invoice_distribution_id := x_dist_tab(l_po_distribution_id).invoice_distribution_id;
489 
490    END IF;
491 
492    --Create a invoice line if one doesn't exist already.
493    IF (x_invoice_line_number IS NULL) THEN
494 
495         l_debug_info := 'Create Matched Invoice Line';
496 
497 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
498 	   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
499 	END IF;
500   	Insert_Invoice_Line(X_Invoice_ID                => x_invoice_id,
501   			    X_Invoice_Line_Number       => g_max_invoice_line_number + 1,
502   			    X_Line_Type_Lookup_Code     => 'ITEM',
503   			    X_Single_Dist_Flag		=> l_single_dist_flag,
504   			    X_Po_Distribution_Id	=> l_po_distribution_id,
505       			    X_Po_Line_Location_id	=> x_po_line_location_id,
506   			    X_Amount			=> x_amount,
507   			    X_Quantity_Invoiced	        => x_quantity,
508   			    X_Unit_Price		=> x_unit_price,
509   			    X_Final_Match_Flag		=> x_final_match_flag,
510   			    X_Item_Line_Number		=> NULL,
511   			    X_Charge_Line_Description	=> NULL,
512 			    X_Retained_Amount		=> l_retained_amount,
513   			    X_Calling_Sequence		=> current_calling_sequence);
514         l_item_line_number := g_max_invoice_line_number;
515 
516   END IF;
517 
518   l_debug_info := 'Create Matched Invoice Distributions';
519   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
520 	   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
521   END IF;
522   Insert_Invoice_Distributions(	X_Invoice_ID		=> x_invoice_id,
523   				X_Invoice_Line_Number	=> nvl(x_invoice_line_number,
524 							       g_max_invoice_line_number),
525 				X_Dist_Tab		=> x_dist_tab,
526   				X_Final_Match_Flag	=> x_final_match_flag,
527 				X_Unit_Price		=> x_unit_price,
528 				X_Total_Amount		=> x_amount,
529   				X_Calling_Sequence	=> current_calling_sequence);
530 
531 
532   IF (x_invoice_line_number IS NOT NULL) THEN
533 
534     IF (l_single_dist_flag = 'Y') THEN
535 
536         l_debug_info := 'If the line is matched down to 1 po distribution then need to
537         		 update the line with po_distribution_id, award_id,requester_id,
538         		 ,projects related information and  generate_dists';
539        	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
540 	   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
541         END IF;
542 
543         UPDATE ap_invoice_lines ail
544         SET (generate_dists ,
545             requester_id ,
546             po_distribution_id ,
547             project_id ,
548             task_id ,
549             expenditure_type ,
550             expenditure_item_date ,
551             expenditure_organization_id ,
552             pa_quantity ,
553             award_id,
554 	    attribute_category,
555 	    attribute1,
556 	    attribute2,
557 	    attribute3,
558 	    attribute4,
559 	    attribute5,
560 	    attribute6,
561 	    attribute7,
562 	    attribute8,
563 	    attribute9,
564 	    attribute10,
565 	    attribute11,
566 	    attribute12,
567 	    attribute13,
568 	    attribute14,
569 	    attribute15,
570             retained_amount,
571             retained_amount_remaining
572 	    ) =
573 	    (SELECT 'D',
574 		    pd.deliver_to_person_id,
575 		    aid.po_distribution_id,
576 		    aid.project_id,
577 		    aid.task_id,
578 		    aid.expenditure_type,
579 		    aid.expenditure_item_date,
580 		    aid.expenditure_organization_id,
581 		    aid.pa_quantity,
582 		    gms_ap_api.get_distribution_award(aid.award_id),
583                     /* Bug 7483260.  If the attribute field is populated in the
584                      * interface, take that value. If the attribute field from
585                      * the interface is null and the transfer_desc_flex_flag is
586                      * Y, take the value from the purchase order.
587                      */
588                     nvl(ail.attribute_category, decode(g_transfer_flag, 'Y', pll.attribute_category, ail.attribute_category)),
589                     nvl(ail.attribute1, decode(g_transfer_flag, 'Y', pll.attribute1, ail.attribute1)),
590                     nvl(ail.attribute2, decode(g_transfer_flag, 'Y', pll.attribute2, ail.attribute2)),
591                     nvl(ail.attribute3, decode(g_transfer_flag, 'Y', pll.attribute3, ail.attribute3)),
592                     nvl(ail.attribute4, decode(g_transfer_flag, 'Y', pll.attribute4, ail.attribute4)),
593                     nvl(ail.attribute5, decode(g_transfer_flag, 'Y', pll.attribute5, ail.attribute5)),
594                     nvl(ail.attribute6, decode(g_transfer_flag, 'Y', pll.attribute6, ail.attribute6)),
595                     nvl(ail.attribute7, decode(g_transfer_flag, 'Y', pll.attribute7, ail.attribute7)),
596                     nvl(ail.attribute8, decode(g_transfer_flag, 'Y', pll.attribute8, ail.attribute8)),
597                     nvl(ail.attribute9, decode(g_transfer_flag, 'Y', pll.attribute9, ail.attribute9)),
598                     nvl(ail.attribute10, decode(g_transfer_flag, 'Y', pll.attribute10, ail.attribute10)),
599                     nvl(ail.attribute11, decode(g_transfer_flag, 'Y', pll.attribute11, ail.attribute11)),
600                     nvl(ail.attribute12, decode(g_transfer_flag, 'Y', pll.attribute12, ail.attribute12)),
601                     nvl(ail.attribute13, decode(g_transfer_flag, 'Y', pll.attribute13, ail.attribute13)),
602                     nvl(ail.attribute14, decode(g_transfer_flag, 'Y', pll.attribute14, ail.attribute14)),
603                     nvl(ail.attribute15, decode(g_transfer_flag, 'Y', pll.attribute15, ail.attribute15)),
604                     --end Bug 7483260
605                     l_retained_amount,
606                     -1 * l_retained_amount
607               FROM ap_invoice_distributions aid,
608 		   po_distributions pd,
609 		   po_line_locations pll
610 	      WHERE aid.invoice_distribution_id = l_invoice_distribution_id
611 	      AND pd.po_distribution_id = aid.po_distribution_id
612 	      AND pll.line_location_id = pd.line_location_id)
613 	WHERE ail.invoice_id = x_invoice_id
614 	AND   ail.line_number = x_invoice_line_number;
615 
616 
617     ELSE
618 
619         l_debug_info := 'Update the generate_dists to D after the distributions are created';
620 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
621 	  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
622 	END IF;
623 
624         UPDATE ap_invoice_lines ail
625         SET (generate_dists ,
626 	    attribute_category,
627             attribute1,
628             attribute2,
629             attribute3,
630             attribute4,
631             attribute5,
632             attribute6,
633             attribute7,
634             attribute8,
635             attribute9,
636             attribute10,
637             attribute11,
638             attribute12,
639             attribute13,
640             attribute14,
641             attribute15,
642             retained_amount,
643             retained_amount_remaining)
644 	    =
645 	  (SELECT 'D',
646                 /* Bug 7483260.  If the attribute field is populated in the
647                  * interface, take that value. If the attribute field from
648                  * the interface is null and the transfer_desc_flex_flag is
649                  * Y, take the value from the purchase order.
650                  */
651                 nvl(ail.attribute_category, decode(g_transfer_flag, 'Y', pll.attribute_category, ail.attribute_category)),
652                 nvl(ail.attribute1, decode(g_transfer_flag, 'Y', pll.attribute1, ail.attribute1)),
653                 nvl(ail.attribute2, decode(g_transfer_flag, 'Y', pll.attribute2, ail.attribute2)),
654                 nvl(ail.attribute3, decode(g_transfer_flag, 'Y', pll.attribute3, ail.attribute3)),
655                 nvl(ail.attribute4, decode(g_transfer_flag, 'Y', pll.attribute4, ail.attribute4)),
656                 nvl(ail.attribute5, decode(g_transfer_flag, 'Y', pll.attribute5, ail.attribute5)),
657                 nvl(ail.attribute6, decode(g_transfer_flag, 'Y', pll.attribute6, ail.attribute6)),
658                 nvl(ail.attribute7, decode(g_transfer_flag, 'Y', pll.attribute7, ail.attribute7)),
659                 nvl(ail.attribute8, decode(g_transfer_flag, 'Y', pll.attribute8, ail.attribute8)),
660                 nvl(ail.attribute9, decode(g_transfer_flag, 'Y', pll.attribute9, ail.attribute9)),
661                 nvl(ail.attribute10, decode(g_transfer_flag, 'Y', pll.attribute10, ail.attribute10)),
662                 nvl(ail.attribute11, decode(g_transfer_flag, 'Y', pll.attribute11, ail.attribute11)),
663                 nvl(ail.attribute12, decode(g_transfer_flag, 'Y', pll.attribute12, ail.attribute12)),
664                 nvl(ail.attribute13, decode(g_transfer_flag, 'Y', pll.attribute13, ail.attribute13)),
665                 nvl(ail.attribute14, decode(g_transfer_flag, 'Y', pll.attribute14, ail.attribute14)),
666                 nvl(ail.attribute15, decode(g_transfer_flag, 'Y', pll.attribute15, ail.attribute15)),
667                 --end Bug 7483260
668                 l_retained_amount,
669                 -1 * l_retained_amount
670            FROM ap_invoice_lines ail1,
671 		po_line_locations pll
672            WHERE ail1.invoice_id = x_invoice_id
673 	   AND ail1.line_number =x_invoice_line_number
674 	   AND pll.line_location_id = ail1.po_line_location_id)
675         WHERE invoice_id = x_invoice_id
676         AND line_number = x_invoice_line_number;
677 
678     END IF;
679 
680   END IF;
681 
682   l_debug_info := 'Create Retainage Distributions';
683   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
684      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
685   END IF;
686 
687   Ap_Retainage_Pkg.Create_Retainage_Distributions
688 			(x_invoice_id          => x_invoice_id,
689                          x_invoice_line_number => nvl(x_invoice_line_number,l_item_line_number));
690 
691   IF (G_Recoupment_Rate IS NOT NULL and x_amount > 0
692   		and g_invoice_type_lookup_code <> 'PREPAYMENT') THEN
693 
694      l_debug_info := 'Calculate the maximum amount that can be recouped from this invoice line';
695      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
696 	       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
697      END IF;
698 
699      l_line_amt_net_retainage := x_amount + nvl(l_retained_amount,0);
700 
701      l_max_amount_to_recoup := ap_utilities_pkg.ap_round_currency(
702      				(x_amount * g_recoupment_rate / 100) ,g_invoice_currency_code);
703 
704      IF (l_line_amt_net_retainage < l_max_amount_to_recoup) THEN
705         l_amount_to_recoup := l_line_amt_net_retainage;
706      ELSE
707         l_amount_to_recoup := l_max_amount_to_recoup;
708      END IF;
709 
710      l_debug_info := 'Automatically recoup any available prepayments against the same po line';
711      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
712 	       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
713      END IF;
714 
715      l_success := AP_Matching_Utils_Pkg.Ap_Recoup_Invoice_Line(
716   				P_Invoice_Id           => x_invoice_id ,
717 			        P_Invoice_Line_Number  => nvl(x_invoice_line_number,l_item_line_number) ,
718 			        P_Amount_To_Recoup     => l_amount_to_recoup,
719 			        P_Po_Line_Id           => g_po_line_id,
720 			        P_Vendor_Id            => g_vendor_id,
721 			        P_Vendor_Site_Id       => g_vendor_site_id,
722 			        P_Accounting_Date      => g_accounting_date,
723 			        P_Period_Name          => g_period_name,
724 			        P_User_Id              => g_user_id,
725 			        P_Last_Update_Login    => g_login_id ,
726 			        P_Error_Message        => l_error_message,
727 			        P_Calling_Sequence     => current_calling_sequence);
728 
729   END IF;
730 
731   l_debug_info := 'Update Quantity/Amount Billed/Financed on the Po Shipments and Distributions';
732   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
733      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
734   END IF;
735 
736   Update_PO_Shipments_Dists(X_Dist_Tab	    => x_dist_tab,
737 			    X_Po_Line_Location_Id => x_po_line_location_id,
738   			    X_Match_Amount	=> x_amount,
739 	    		    X_Match_Quantity	=> x_quantity,
740   			    X_Uom_Lookup_Code => x_uom_lookup_code,
741   			    X_Calling_Sequence => current_calling_sequence);
742 
743 
744   IF (x_freight_amount IS NOT NULL or x_misc_amount IS NOT NULL) THEN
745 
746      l_debug_info := 'Call the procedure to create charge lines';
747      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
748         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
749      END IF;
750 
751      --Due to the way PL/SQL binding is done for global variables, need
752      --pass the local instead of global variable for this as global variable
753      --is being updated before the x_item_line_number is used during runtime.
754 
755      Create_Charge_Lines(X_Invoice_Id 	 	=> x_invoice_id,
756      			 X_Freight_Cost_Factor_id  => x_freight_cost_factor_id,
757     			 X_Freight_Amount   	=> x_freight_amount,
758     			 X_Freight_Description 	=> x_freight_description,
759 			 X_Misc_Cost_Factor_id  => x_misc_cost_factor_id,
760     			 X_Misc_Amount		=> x_misc_amount,
761     			 X_Misc_Description	=> x_misc_description,
762     			 X_Item_Line_Number	=> l_item_line_number,
763     			 X_Calling_Sequence	=> current_calling_sequence);
764 
765   END IF;
766 
767   --Clean up the PL/SQL table
768   X_DIST_TAB.DELETE;
769 
770   IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
771       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.base_credit_po_match(-)');
772   END IF;
773 
774 EXCEPTION
775  WHEN OTHERS THEN
776    IF (SQLCODE <> -20001) THEN
777      FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
778      FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
779      FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',current_calling_sequence);
780      FND_MESSAGE.SET_TOKEN('PARAMETERS','Match Mode = '||x_match_mode
781      			  ||', Invoice Id = '||to_char(x_invoice_id)
782      			  ||', Invoice Line Number = '||to_char(x_invoice_line_number)
783      			  ||', Shipment ID = '||to_char(x_po_line_location_id)
784  			  ||', Match amount = '||to_char(x_amount)
785  			  ||', Match quantity = '||to_char(x_quantity)
786  			  ||', PO UOM = '||x_uom_lookup_code
787  			  ||', Final Match Flag = '||X_final_match_flag
788  			  ||', Overbill Flag = '||x_overbill_flag
789  			  ||', Freight Amount = '||to_char(x_freight_amount)
790  			  ||', Freight Description = '||x_freight_description
791  			  ||', Misc Amount = '||to_char(x_misc_amount)
792  			  ||', Misc Description = '||x_misc_description);
793      FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
794    END IF;
795 
796    --Clean up the PL/SQL table
797    X_DIST_TAB.DELETE;
798    APP_EXCEPTION.RAISE_EXCEPTION;
799 
800 END Base_Credit_PO_Match;
801 
802 
803 
804 PROCEDURE Get_Info(X_Invoice_ID  	 IN  NUMBER,
805 		   X_Invoice_Line_Number IN  NUMBER DEFAULT NULL,
806 		   X_Match_Amount	 IN  NUMBER DEFAULT NULL,
807 		   X_Po_Line_Location_Id IN  NUMBER DEFAULT NULL,
808 		   X_Calling_Sequence	 IN  VARCHAR2
809 		   ) IS
810 
811  current_calling_sequence 	VARCHAR2(2000);
812  l_debug_info		VARCHAR2(2000);
813  l_api_name VARCHAR2(30);
814  l_accounting_date ap_invoice_lines.accounting_date%TYPE;  --7463095
815 
816 BEGIN
817 
818    l_api_name := 'Get_Info';
819 
820    current_calling_sequence := 'Get_Info<-'||X_Calling_Sequence;
821    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
822       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Get_Info(+)');
823    END IF;
824 
825    SELECT ai.gl_date,
826 	  ai.batch_id,
827           ai.set_of_books_id,
828           ai.awt_group_id,
829           ai.invoice_type_lookup_code,
830           ai.exchange_rate,
831 	  fc.precision,
832 	  fc.minimum_accountable_unit,
833 	  nvl(asp.allow_awt_flag,'N'),
834           nvl(pvs.allow_awt_flag,'N'),
835           nvl(asp.transfer_desc_flex_flag,'N'),
836           asp.base_currency_code,
837           ai.invoice_currency_code,
838           nvl(pvs.prepay_code_combination_id,
839               asp.prepay_code_combination_id),
840           nvl(asp.build_prepayment_accounts_flag,'N'),
841           decode(pv.type_1099,'','',
842           	 decode(combined_filing_flag,'N',NULL,
843           	 	decode(asp.income_tax_region_flag,'Y',pvs.state,
844           	 	       asp.income_tax_region))),
845           ai.project_id,
846           ai.task_id,
847 	  ai.award_id,
848           ai.expenditure_type,
849           ai.invoice_date,
850           ai.expenditure_organization_id,
851           fsp.inventory_organization_id,
852 	  nvl(asp.approval_workflow_flag,'N'),
853        -- ai.ussgl_transaction_code,- Bug 4277744
854 	  asp.allow_flex_override_flag,
855 	  ai.org_id,
856 	  nvl(fsp.purch_encumbrance_flag,'N'),
857 	  ai.award_id,
858 	  ai.trx_business_category,
859 	  ai.vendor_id,
860 	  ai.vendor_site_id,
861 	  ai.release_amount_net_of_tax,
862 	  ai.source,
863 	  ai.recurring_payment_id,  -- Bug 7305223
864 	  ai.pay_awt_group_id   --bug 8222382
865    INTO g_accounting_date,
866 	g_batch_id,
867         g_set_of_books_id,
868         g_awt_group_id,
869         g_invoice_type_lookup_code,
870         g_exchange_rate,
871         g_precision,
872 	g_min_acct_unit,
873 	g_system_allow_awt_flag,
874         g_site_allow_awt_flag,
875         g_transfer_flag,
876         g_base_currency_code,
877         g_invoice_currency_code,
878         g_prepay_ccid,
879         g_build_prepay_accts_flag,
880         g_income_tax_region,
881         g_project_id,
882         g_task_id,
883 	g_award_id,
884         g_expenditure_type,
885         g_invoice_date,
886         g_expenditure_organization_id,
887         g_inventory_organization_id,
888 	g_approval_workflow_flag,
889      --	g_ussgl_transaction_code,- Bug 4277744
890 	g_allow_flex_override_flag,
891 	g_org_id,
892 	g_encumbrance_flag,
893 	g_award_id,
894 	g_trx_business_category,
895 	g_vendor_id,
896 	g_vendor_site_id,
897 	g_release_amount_net_of_tax,
898 	g_source,
899 	g_recurring_payment_id,   -- Bug 7305223
900    /* Bug 5572876, using base tables */
901     g_pay_awt_group_id  -- bug 8222382
902    FROM ap_invoices_all ai ,
903    	ap_system_parameters_all asp,
904    	ap_suppliers pv,
905    	ap_supplier_sites_all pvs,
906    	financials_system_params_all fsp,
907 	fnd_currencies fc
908    WHERE ai.invoice_id = x_invoice_id
909    AND   ai.vendor_site_id = pvs.vendor_site_id
910    AND   pv.vendor_id = pvs.vendor_id
911    AND   ai.org_id = asp.org_id
912    AND   asp.org_id = fsp.org_id
913    AND   ai.set_of_books_id = asp.set_of_books_id
914    AND   asp.set_of_books_id = fsp.set_of_books_id
915    AND   ai.invoice_currency_code = fc.currency_code (+);
916 
917 
918    IF (x_match_amount IS NOT NULL AND g_invoice_currency_code <> g_base_currency_code) THEN
919       g_line_base_amount := ap_utilities_pkg.ap_round_currency(
920                                    x_match_amount * g_exchange_rate,
921 				   g_base_currency_code);
922    END IF;
923 
924 
925    IF (x_invoice_line_number IS NOT NULL) THEN
926 
927    	SELECT
928 	       ail.account_segment,
929 	       ail.balancing_segment,
930 	       ail.cost_center_segment,
931 	       ail.overlay_dist_code_concat,
932 	       ail.default_dist_ccid,
933 	       ail.project_id,
934 	       ail.task_id,
935 	       ail.award_id,
936 	       ail.expenditure_type,
937 	       ail.expenditure_item_date,
938 	       ail.expenditure_organization_id,
939 	       ail.awt_group_id,
940 	       ail.accounting_date,
941 		   ail.pay_awt_group_id
942    	INTO
943 	     g_account_segment,
944 	     g_balancing_segment,
945 	     g_cost_center_segment,
946 	     g_overlay_dist_code_concat,
947 	     g_default_dist_ccid,
948 	     g_line_project_id,
949 	     g_line_task_id,
950 	     g_line_award_id,
951 	     g_line_expenditure_type,
952 	     g_line_expenditure_item_date,
953 	     g_line_expenditure_org_id,
954 	     g_line_awt_group_id,
955 	     g_line_accounting_date,
956 		 g_line_pay_awt_group_id
957    	FROM ap_invoice_lines ail
958    	WHERE ail.invoice_id = x_invoice_id
959 	AND ail.line_number = x_invoice_line_number;
960 
961    END IF;
962 
963    SELECT nvl(max(ail.line_number),0)
964    INTO g_max_invoice_line_number
965    FROM ap_invoice_lines ail
966    WHERE ail.invoice_id = x_invoice_id;
967 
968    /* Bug 5572876 */
969    g_asset_book_type_code := Ap_Utilities_Pkg.Ledger_Asset_Book
970                                (g_set_of_books_id);
971 
972    /*
973    BEGIN
974    	SELECT book_type_code
975 	INTO g_asset_book_type_code
976 	FROM fa_book_controls fc
977 	WHERE fc.book_class = 'CORP0RATE'
978 	AND fc.set_of_books_id = g_set_of_books_id
979 	AND fc.date_ineffective  IS NULL;
980     EXCEPTION
981     WHEN NO_DATA_FOUND OR TOO_MANY_ROWS THEN
982       g_asset_book_type_code := NULL;
983    END; */
984 
985    IF (x_po_line_location_id IS NOT NULL) THEN
986 
987       SELECT pll.shipment_type, pll.po_line_id, pl.recoupment_rate
988       INTO g_shipment_type, g_po_line_id, g_recoupment_rate
989       FROM po_line_locations pll, po_lines pl
990       WHERE pll.line_location_id = x_po_line_location_id
991       AND pll.po_line_id = pl.po_line_id;
992 
993    END  IF;
994 
995    l_debug_info := 'select period for accounting date';
996    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
997       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
998    END IF;
999 
1000    --get_current_gl_date will return NULL if the date passed to it doesn't fall in a
1001    --open period.
1002    --  Bug 4460697. Passed the g_org_id as some cases
1003    --  mo_global.get_current_org_id does not work
1004    g_period_name := AP_UTILITIES_PKG.get_current_gl_date(g_accounting_date,
1005                                                          g_org_id);
1006 
1007    IF (g_period_name IS NULL) THEN
1008 
1009       --Get gl_period and Date from a future period for the accounting date
1010       -- Bug 4460697. Passed the g_org_id as some cases
1011       -- mo_global.get_current_org_id does not work
1012 
1013        -- 7463095 Used l_accounting_date instead of g_accounting_date. Using
1014       --same variable for in/out parmeters causing to make in parameters
1015       -- as Null
1016       ap_utilities_pkg.get_open_gl_date(p_date        => g_accounting_date,
1017   				        p_period_name => g_period_name,
1018   				        p_gl_date     => l_accounting_date,
1019                                         p_org_id      => g_org_id);
1020 
1021          g_accounting_date :=   l_accounting_date;   --7463095
1022 
1023     --Bug 7305223 While generating recurring invoices no need of checking
1024     --gl date is open/closed. Always allowing recurring invoices to generate
1025     --any period.
1026 
1027     IF g_recurring_payment_id   is null THEN --Bug 7305223
1028       IF (g_accounting_date IS NULL) THEN
1029           fnd_message.set_name('SQLAP','AP_DISTS_NO_OPEN_FUT_PERIOD');
1030           app_exception.raise_exception;
1031       ELSE
1032           g_line_accounting_date := g_accounting_date;
1033       END IF;
1034      END IF;
1035 
1036    END IF;
1037 
1038    --Bug 6956226. Modified below statement to assign 'Yes' if this profile
1039    --has defined no value.
1040    g_allow_pa_override := NVL(FND_PROFILE.VALUE('PA_ALLOW_FLEXBUILDER_OVERRIDES'),'Y');
1041 
1042    -- Bug 5294998. API from PA will be used
1043    -- g_pa_expenditure_date_default := FND_PROFILE.VALUE('PA_AP_EI_DATE_DEFAULT');
1044 
1045    g_user_id := FND_PROFILE.VALUE('USER_ID');
1046 
1047    g_login_id := FND_PROFILE.VALUE('LOGIN_ID');
1048 
1049    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
1050       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Get_Info(-)');
1051    END IF;
1052 
1053 
1054 EXCEPTION
1055  WHEN OTHERS THEN
1056    IF (SQLCODE <> -20001) THEN
1057      FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1058      FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1059      FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',current_calling_sequence);
1060      FND_MESSAGE.SET_TOKEN('PARAMETERS','Invoice Id = '||TO_CHAR(X_Invoice_Id));
1061      FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1062    END IF;
1063    app_exception.raise_exception;
1064 
1065 END Get_Info;
1066 
1067 
1068 Procedure Get_Dist_Proration_Info(
1069 		  X_Invoice_Id		 IN NUMBER,
1070 		  X_Invoice_Line_Number  IN NUMBER,
1071 		  X_Po_Line_Location_Id  IN NUMBER,
1072 		  X_Match_Mode		 IN VARCHAR2,
1073 		  X_Match_Quantity	 IN NUMBER,
1074 		  X_Match_Amount	 IN NUMBER,
1075 		  X_Unit_Price		 IN NUMBER,
1076 		  X_Overbill_Flag	 IN VARCHAR2,
1077 		  X_Dist_Tab		 IN OUT NOCOPY DIST_TAB_TYPE,
1078 		  X_Calling_Sequence	 IN VARCHAR2) IS
1079 
1080 
1081   CURSOR po_distributions_cursor(p_total_quantity NUMBER) IS
1082      SELECT po_distribution_id,
1083 	    /*PRORATED AMOUNT*/
1084      	    DECODE(g_min_acct_unit,
1085                    '', ROUND( X_match_amount * DECODE(X_match_mode,
1086                                                       'STD-PS',DECODE(X_overbill_flag,
1087                                                                       'Y', NVL(PD.quantity_ordered, 0),
1088                                                                        NVL(DECODE(SIGN(PD.quantity_ordered -
1089                                              			                       DECODE(PD.distribution_type,'PREPAYMENT',
1090 											      NVL(PD.quantity_financed,0),
1091 										              NVL(PD.quantity_billed,0)
1092 											      ) -
1093                                               				               NVL(PD.quantity_cancelled,0)),
1094                                                				          -1, 0,
1095                                                                                   PD.quantity_ordered -
1096                                                                                   DECODE(PD.distribution_type,'PREPAYMENT',
1097 											 NVL(PD.quantity_financed,0),
1098 											 NVL(PD.quantity_billed,0)
1099 											 ) -
1100                                                                                   NVL(PD.quantity_cancelled,0)
1101                                                                                   )
1102                                           		                  ,0)
1103                                                                       ),
1104                                                        NVL(PD.quantity_billed, 0)
1105                                                      )
1106                             / p_total_quantity,
1107                            g_precision),
1108                  ROUND(((X_match_amount * DECODE(X_match_mode,
1109                                                  'STD-PS',DECODE(X_overbill_flag,
1110                                                                  'Y', NVL(PD.quantity_ordered, 0),
1111                                                                   NVL(DECODE(SIGN(PD.quantity_ordered -
1112                                               			                  DECODE(PD.distribution_type,'PREPAYMENT',
1113 											 NVL(PD.quantity_financed,0),
1114 										         NVL(PD.quantity_billed,0)
1115 											)  -
1116                                               				          NVL(PD.quantity_cancelled,0)),
1117                                                				     -1, 0,
1118                                                                              PD.quantity_ordered -
1119                                                                              DECODE(PD.distribution_type,'PREPAYMENT',
1120 										    NVL(PD.quantity_financed,0),
1121 										    NVL(PD.quantity_billed,0)
1122 										   )  -
1123                                                                              NVL(PD.quantity_cancelled,0)
1124  									    )
1125                                           		              ,0)
1126 								 ),
1127                                                  NVL(PD.quantity_billed, 0)
1128 						)
1129                            / p_total_quantity)
1130                      / g_min_acct_unit) * g_min_acct_unit)),
1131 	     /*PRORATED QUANTITY*/
1132              ROUND( X_match_quantity * DECODE(X_match_mode,
1133                                               'STD-PS',DECODE(X_overbill_flag,
1134                                                               'Y', NVL(PD.quantity_ordered, 0),
1135                                                                NVL(DECODE(SIGN(PD.quantity_ordered -
1136                                              			                DECODE(PD.distribution_type,'PREPAYMENT',
1137 										       NVL(PD.quantity_financed,0),
1138 										       NVL(PD.quantity_billed,0)
1139 										       )  -
1140                                               				        NVL(PD.quantity_cancelled,0)),
1141                                                				   -1, 0,
1142                                                                            PD.quantity_ordered -
1143                                                                            DECODE(PD.distribution_type,'PREPAYMENT',
1144 										  NVL(PD.quantity_financed,0),
1145 										  NVL(PD.quantity_billed,0)
1146 										 )  -
1147                                                                            NVL(PD.quantity_cancelled,0))
1148                                            		           , 0)),
1149                                                DECODE(PD.distribution_type,'PREPAYMENT',
1150 						      NVL(PD.quantity_financed,0),
1151 						      NVL(PD.quantity_billed,0)
1152 						     )
1153 					     )
1154                      / p_total_quantity,15),
1155             PD.code_combination_id,
1156 	    PD.accrue_on_receipt_flag,
1157 	    DECODE(PD.destination_type_code,'EXPENSE',
1158 	    	   PD.project_id,NULL), 		  --project_id
1159 	    DECODE(PD.destination_type_code,'EXPENSE',
1160 	           PD.task_id,NULL),			  --task_id
1161 	    DECODE(PD.destination_type_code,'EXPENSE',
1162 	    	   PD.expenditure_type,NULL),             --expenditure_type
1163 	    DECODE(PD.destination_type_code,
1164 	    	   'EXPENSE',PD.expenditure_item_date,
1165 		   NULL), 				  --expenditure_item_date
1166 	    DECODE(PD.destination_type_code,
1167 	    	   'EXPENSE',PD.expenditure_organization_id,
1168 		   NULL),				  --expenditure_organization_id
1169             DECODE(PD.destination_type_code,
1170                    'EXPENSE', PD.award_id),               --award_id
1171 	    ap_invoice_distributions_s.nextval
1172   FROM po_distributions_ap_v PD
1173   WHERE line_location_id = x_po_line_location_id;
1174 
1175 
1176   l_total_quantity 	  number;
1177   l_po_distribution_id	  po_distributions_all.po_distribution_id%TYPE;
1178   l_amount_invoiced	  ap_invoice_distributions_all.amount%TYPE;
1179   l_quantity_invoiced	  ap_invoice_distributions_all.quantity_invoiced%TYPE;
1180   l_po_dist_ccid	  po_distributions.code_combination_id%TYPE;
1181   l_accrue_on_receipt_flag po_distributions.accrue_on_receipt_flag%TYPE;
1182   l_project_id		  po_distributions.project_id%TYPE;
1183   l_unbuilt_flex          varchar2(240):='';
1184   l_reason_unbuilt_flex   varchar2(2000):='';
1185   l_dist_ccid		  ap_invoice_distributions_all.dist_code_combination_id%TYPE;
1186   l_invoice_distribution_id ap_invoice_distributions.invoice_distribution_id%TYPE;
1187   l_task_id		  po_distributions.task_id%TYPE;
1188   l_award_set_id	  po_distributions_all.award_id%TYPE;
1189   l_expenditure_type      po_distributions.expenditure_type%TYPE;
1190   l_po_expenditure_item_date po_distributions.expenditure_item_date%TYPE;
1191   l_expenditure_organization_id po_distributions.expenditure_organization_id%TYPE;
1192   l_max_dist_amount	  number := 0;
1193   l_sum_prorated_amount   number := 0;
1194   l_sum_dist_base_amount  number := 0;
1195   l_rounding_index	  po_distributions.po_distribution_id%TYPE;
1196   l_base_amount		  ap_invoice_distributions.base_amount%TYPE;
1197   flex_overlay_failed     exception;
1198   current_calling_sequence varchar2(2000);
1199   l_debug_info 		  varchar2(2000);
1200   l_api_name		  VARCHAR2(50);
1201 
1202 BEGIN
1203 
1204  l_api_name := 'Get_Dist_Proration_Info';
1205 
1206  current_calling_sequence := 'Get_Dist_Proration_Info<-'||x_calling_sequence;
1207  IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
1208     FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Get_Dist_Proration_Info(+)');
1209  END IF;
1210 
1211  IF(X_Match_Mode IN ('STD-PS','CR-PS')) THEN
1212 
1213    l_debug_info := 'Get Total Quantity for Proration';
1214    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1215          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1216    END IF;
1217 
1218    Get_Total_Proration_Quantity
1219 	                  ( X_PO_Line_Location_Id  => x_po_line_location_id,
1220 	                    X_Match_Mode      => x_match_mode,
1221 			    X_Overbill_Flag   => x_overbill_flag,
1222 			    X_Total_Quantity  => l_total_quantity,
1223 			    X_Calling_Sequence => current_calling_sequence);
1224 
1225    l_debug_info := 'g_min_acct_unit, x_match_amount, x_overbill_flag, x_match_mode, x_match_quantity, x_po_line_location_id'||
1226 		    g_min_acct_unit||','||x_match_amount||','||x_overbill_flag||','||x_match_mode||','||x_match_quantity||','||x_po_line_location_id;
1227 
1228    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1229             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1230    END IF;
1231 
1232    OPEN PO_Distributions_Cursor(l_total_quantity);
1233    LOOP
1234 
1235       l_debug_info := 'Fetch record from Po_Distributions_Cursor l_total_quantity is '||l_total_quantity;
1236       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1237 	   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1238       END IF;
1239 
1240       FETCH PO_Distributions_Cursor INTO l_po_distribution_id,
1241      					 l_amount_invoiced,
1242      					 l_quantity_invoiced,
1243 					 l_po_dist_ccid,
1244 					 l_accrue_on_receipt_flag,
1245 					 l_project_id,
1246 					 l_task_id,
1247 					 l_expenditure_type,
1248 					 l_po_expenditure_item_date,
1249 					 l_expenditure_organization_id,
1250 					 l_award_set_id,
1251 					 l_invoice_distribution_id;
1252 
1253       EXIT WHEN PO_Distributions_Cursor%NOTFOUND;
1254 
1255       --IF (l_amount_invoiced <> 0) THEN --Bug6321189
1256 
1257          l_debug_info := 'Populate the PL/SQL table x_dist_tab with the distribution information';
1258 	 IF(G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1259 	   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1260 	 END IF;
1261 
1262 
1263 
1264          x_dist_tab(l_po_distribution_id).po_distribution_id := l_po_distribution_id;
1265 	 x_dist_tab(l_po_distribution_id).invoice_distribution_id := l_invoice_distribution_id;
1266          x_dist_tab(l_po_distribution_id).amount := l_amount_invoiced;
1267          x_dist_tab(l_po_distribution_id).quantity_invoiced := l_quantity_invoiced;
1268 	 x_dist_tab(l_po_distribution_id).unit_price := x_unit_price;
1269 	 x_dist_tab(l_po_distribution_id).po_ccid := l_po_dist_ccid;
1270 	 x_dist_tab(l_po_distribution_id).accrue_on_receipt_flag := l_accrue_on_receipt_flag;
1271 	 x_dist_tab(l_po_distribution_id).project_id := l_project_id;
1272          x_dist_tab(l_po_distribution_id).task_id := l_task_id;
1273          x_dist_tab(l_po_distribution_id).expenditure_type := l_expenditure_type;
1274          x_dist_tab(l_po_distribution_id).expenditure_organization_id := l_expenditure_organization_id;
1275 	 x_dist_tab(l_po_distribution_id).expenditure_item_date := l_po_expenditure_item_date;
1276          --Bug 5554493
1277          x_dist_tab(l_po_distribution_id).pa_quantity := l_quantity_invoiced;
1278 
1279          If l_award_set_id Is Not Null Then
1280             x_dist_tab(l_po_distribution_id).award_id := gms_ap_api.get_distribution_award(l_award_set_id);
1281          End If;
1282 
1283 	 --For proration rounding/base amount rounding,
1284 	 --calculating max of the largest distribution's index
1285 	 IF (ABS(l_amount_invoiced) >= ABS(l_max_dist_amount)) THEN  --bug 8796561, added ABS
1286 
1287 	   l_rounding_index := l_po_distribution_id;
1288 	   l_max_dist_amount := l_amount_invoiced;
1289 
1290 	 END IF;
1291 
1292 	 l_sum_prorated_amount := l_sum_prorated_amount + l_amount_invoiced;
1293 
1294      --END IF; /* (l_amount_invoiced <> 0) */
1295 
1296    END LOOP;
1297 
1298    CLOSE PO_Distributions_Cursor;
1299 
1300    --Update the PL/SQL table's amount column with the rounding amount due
1301    --to proration, before the base_amounts are calculated.
1302 
1303    IF (l_sum_prorated_amount <> x_match_amount and l_rounding_index is not null) THEN
1304 
1305       x_dist_tab(l_rounding_index).amount := x_dist_tab(l_rounding_index).amount +
1306       								(x_match_amount - l_sum_prorated_amount);
1307 
1308    END IF;
1309 
1310  ELSE
1311 
1312    FOR i IN nvl(x_dist_tab.FIRST,0)..nvl(x_dist_tab.LAST,0) LOOP
1313 
1314      IF (x_dist_tab.exists(i)) THEN
1315 
1316        SELECT accrue_on_receipt_flag,
1317 	      code_combination_id,
1318 	      project_id,
1319 	      task_id,
1320 	      award_id,
1321 	      expenditure_type,
1322 	      expenditure_item_date,
1323 	      expenditure_organization_id,
1324 	      ap_invoice_distributions_s.nextval
1325        INTO  x_dist_tab(i).accrue_on_receipt_flag,
1326 	     x_dist_tab(i).po_ccid,
1327 	     x_dist_tab(i).project_id,
1328 	     x_dist_tab(i).task_id,
1329 	     l_award_set_id,
1330 	     x_dist_tab(i).expenditure_type,
1331 	     x_dist_tab(i).expenditure_item_date,
1332 	     x_dist_tab(i).expenditure_organization_id,
1333 	     x_dist_tab(i).invoice_distribution_id
1334        FROM  po_distributions_ap_v
1335        WHERE po_distribution_id = x_dist_tab(i).po_distribution_id;
1336 
1337        -- Bug 5554493
1338        x_dist_tab(i).pa_quantity := x_dist_tab(i).quantity_invoiced;
1339 
1340        If l_award_set_id Is Not Null Then
1341           x_dist_tab(i).award_id := gms_ap_api.get_distribution_award(l_award_set_id);
1342        End If;
1343 
1344        --calculate the max of the largest distribution's index to be
1345        --used for base amount rounding. No need to perform proration
1346        --rounding for the case when the match is distributed by the user.
1347 
1348        --Need to do the base_amount rounding only for foreign currency
1349        --invoices only.
1350 
1351 
1352        IF (g_exchange_rate IS NOT NULL) THEN
1353             --Added abs()  for bug#14315066
1354           IF (abs(x_dist_tab(i).amount) >= abs(l_max_dist_amount)) THEN
1355              l_rounding_index := i;
1356 	     l_max_dist_amount := x_dist_tab(i).amount;
1357           END IF;
1358 
1359        END IF;
1360 
1361      END IF;
1362 
1363    END LOOP;
1364 
1365  END IF;
1366 
1367 
1368  FOR i in nvl(x_dist_tab.first,0) .. nvl(x_dist_tab.last,0) LOOP
1369 
1370    IF (x_dist_tab.exists(i)) THEN
1371 
1372      l_debug_info := 'Populate Project related information';
1373      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1374            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1375      END IF;
1376 
1377      --If no project info in the PL/SQL by now,
1378      --then project info was null on po distribution
1379      --,then copy it from line for line level match, else copy it from invoice header.
1380 
1381      --Not doing NVL on the g_line_project_id, b'coz for the case of line level match
1382      --if the user has not provided any project information on the line regardless of
1383      --whether project info is present on header or not, we will not override what user
1384      --has explicitly provided.
1385 
1386 /*
1387      IF (x_dist_tab(i).project_id IS NULL ) THEN
1388 
1389         IF (l_accrue_on_receipt_flag = 'N' and g_allow_pa_override = 'Y')THEN
1390 
1391            IF (x_invoice_line_number IS NOT NULL) THEN
1392 
1393                x_dist_tab(i).project_id := g_line_project_id;
1394                x_dist_tab(i).task_id := g_line_task_id;
1395                x_dist_tab(i).expenditure_type := g_line_expenditure_type;
1396                x_dist_tab(i).expenditure_organization_id := g_line_expenditure_org_id;
1397 
1398 	   ELSE
1399 
1400 	       x_dist_tab(i).project_id := g_project_id;
1401                x_dist_tab(i).task_id := g_task_id;
1402                x_dist_tab(i).expenditure_type := g_expenditure_type;
1403                x_dist_tab(i).expenditure_organization_id := g_expenditure_organization_id;
1404 
1405 	   END IF;
1406 
1407         END IF;
1408 
1409      END IF;
1410 */
1411      IF (x_dist_tab(i).project_id IS NOT NULL) THEN
1412          --Bug 5554493
1413          --x_dist_tab(i).pa_quantity := l_quantity_invoiced;
1414          -- Bug 5294998. PA API will be used
1415          /*
1416          CASE g_pa_expenditure_date_default
1417          WHEN 'PO Expenditure Item Date/Transaction Date' THEN
1418             x_dist_tab(i).expenditure_item_date := nvl(x_dist_tab(i).expenditure_item_date,g_invoice_date);
1419          WHEN 'PO Expenditure Item Date/Transaction GL Date' THEN
1420             x_dist_tab(i).expenditure_item_date := nvl(x_dist_tab(i).expenditure_item_date,g_accounting_date);
1421          WHEN 'PO Expenditure Item Date/Transaction System Date' THEN
1422             x_dist_tab(i).expenditure_item_date := nvl(x_dist_tab(i).expenditure_item_date,sysdate);
1423          WHEN ('Receipt Date/Transaction Date' )  THEN
1424             x_dist_tab(i).expenditure_item_date := g_invoice_date;
1425          WHEN ('Receipt Date/Transaction GL Date') THEN
1426             x_dist_tab(i).expenditure_item_date := g_accounting_date;
1427          WHEN ('Receipt Date/Transaction System Date' ) THEN
1428             x_dist_tab(i).expenditure_item_date := sysdate;
1429          WHEN 'Transaction Date' THEN
1430             x_dist_tab(i).expenditure_item_date := g_invoice_date;
1431          WHEN 'Transaction GL Date' THEN
1432             x_dist_tab(i).expenditure_item_date := g_accounting_date;
1433          WHEN 'Transaction System Date' THEN
1434             x_dist_tab(i).expenditure_item_date := sysdate;
1435          ELSE
1436             x_dist_tab(i).expenditure_item_date := NULL;
1437          END CASE;
1438          */
1439          x_dist_tab(i).expenditure_item_date :=
1440            PA_AP_INTEGRATION.Get_Si_Cost_Exp_Item_Date (
1441              p_transaction_date   =>  g_invoice_date,
1442              p_gl_date            =>  g_accounting_date,
1443              p_creation_date      =>  sysdate,
1444              p_po_exp_item_date   =>  x_dist_tab(i).expenditure_item_date,
1445              p_po_distribution_id =>  x_dist_tab(i).po_distribution_id,
1446              p_calling_program    =>  'PO-MATCH');
1447 
1448      END IF;
1449 
1450      l_debug_info := 'Populate award information';
1451      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1452       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1453      END IF;
1454 
1455      IF (x_dist_tab(i).award_id IS NULL ) THEN
1456         IF (l_accrue_on_receipt_flag = 'N' and g_allow_pa_override = 'Y')THEN
1457            IF (x_invoice_line_number IS NOT NULL) THEN
1458                x_dist_tab(i).award_id := g_line_award_id;
1459            ELSE
1460                x_dist_tab(i).award_id := g_award_id;
1461            END IF;
1462         END IF;
1463      END IF; /*(x_dist_tab(i).award_id IS NULL) */
1464 
1465      l_debug_info := 'Populate awt information';
1466      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1467            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1468      END IF;
1469 
1470      IF (g_system_allow_awt_flag = 'Y' and g_site_allow_awt_flag = 'Y') THEN
1471 
1472         IF (x_invoice_line_number IS NOT NULL) THEN
1473            x_dist_tab(i).awt_group_id := g_line_awt_group_id;
1474 		   x_dist_tab(i).pay_awt_group_id := g_line_pay_awt_group_id;
1475         ELSE
1476            x_dist_tab(i).awt_group_id := g_awt_group_id;
1477 		   x_dist_tab(i).pay_awt_group_id := g_pay_awt_group_id;
1478         END IF;
1479 
1480      END IF;
1481 
1482      --Populate Base Amount
1483      --Need to populate the base amount for foreign currency invoices only.
1484      IF (g_exchange_rate IS NOT NULL) THEN
1485         l_base_amount := ap_utilities_pkg.ap_round_currency(
1486   				      x_dist_tab(i).amount * g_exchange_rate,
1487                                 g_base_currency_code);
1488         x_dist_tab(i).base_amount := l_base_amount;
1489 
1490         l_sum_dist_base_amount := l_sum_dist_base_amount + l_base_amount;
1491      END IF;
1492 
1493      --Populate dist_code_combination_id information
1494 
1495         --Can overlay account if not accruing on receipt, and either not project_related
1496         --or if project related then project account override is allowed
1497         --and encumbrance is not turned on for all invoices types, except for
1498 	--prepayment invoices and system option to allow
1499         --override of matching account is turned ON.
1500 
1501      IF (g_invoice_type_lookup_code <> 'PREPAYMENT') THEN
1502 
1503         --Can overlay account if not accruing on receipt, and either not project_related
1504         --or if project related then project account override is allowed
1505         --and encumbrance is not turned on and system option to allow
1506         --override of matching account is turned ON.
1507 
1508         IF (nvl(x_dist_tab(i).accrue_on_receipt_flag,'N') = 'N'
1509            AND ((x_dist_tab(i).project_id IS NOT NULL AND g_allow_pa_override = 'Y')
1510               OR x_dist_tab(i).project_id IS NULL)
1511            AND g_allow_flex_override_flag = 'Y'
1512            AND g_encumbrance_flag = 'N') THEN
1513 
1514            IF (g_account_segment IS NOT NULL OR
1515                g_balancing_segment IS NOT NULL OR
1516                g_cost_center_segment IS NOT NULL OR
1517                g_overlay_dist_code_concat IS NOT NULL) THEN
1518 
1519                l_dist_ccid := nvl(x_dist_tab(i).dist_ccid,x_dist_tab(i).po_ccid);
1520 
1521                IF (AP_UTILITIES_PKG.overlay_segments(
1522                                 g_balancing_segment,
1523                                 g_cost_center_segment,
1524                                 g_account_segment,
1525                                 g_overlay_dist_code_concat,
1526                                 l_dist_ccid,
1527                                 g_set_of_books_id ,
1528                                 'CREATE_COMB_NO_AT', --Bug 5005198
1529                                 l_unbuilt_flex ,
1530                                 l_reason_unbuilt_flex ,
1531                                 FND_GLOBAL.RESP_APPL_ID,
1532                                 FND_GLOBAL.RESP_ID,
1533                                 FND_GLOBAL.USER_ID,
1534                                 current_calling_sequence ,
1535                                 NULL) <> TRUE) THEN
1536 
1537                    l_debug_info := 'Overlaying Segments for this account was unsuccessful due to '||
1538                                   l_reason_unbuilt_flex;
1539                    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1540                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1541                    END IF;
1542 
1543                    --Did not handle this exception explicitly as OTHERS handler
1544                    --should be sufficient for this case.
1545 
1546                    /*OPEN ISSUE 3 */
1547                    RAISE FLEX_OVERLAY_FAILED;
1548 
1549                ELSE
1550 
1551                   x_dist_tab(i).dist_ccid := l_dist_ccid;
1552 
1553                END IF;
1554 
1555            /*No Overlay info provided */
1556            ELSE
1557 
1558               --x_dist_tab.dist_ccid is already populated by the
1559               --calling module with the overriden account ccid, so
1560               --we need not do anything for the case of 'STD-PD' and 'CR-PD'.
1561 
1562               IF (x_match_mode IN ('STD-PS','CR-PS')) THEN
1563                   x_dist_tab(i).dist_ccid := nvl(g_default_dist_ccid,x_dist_tab(i).po_ccid);
1564               END IF;
1565 
1566            END IF; /*IF g_account_segment is not null... */
1567 
1568         ELSIF (nvl(x_dist_tab(i).accrue_on_receipt_flag,'N') = 'Y' OR
1569               g_allow_flex_override_flag = 'N' OR
1570               g_encumbrance_flag = 'Y' OR
1571               --bugfix:4668058 added the following clause
1572               (x_dist_tab(i).project_id IS NOT NULL AND g_allow_pa_override = 'N')
1573               ) THEN
1574 
1575             --po_distributions_ap_v.code_combination_id is accrual account
1576             --if accruing on receipt or else charge account
1577 
1578             x_dist_tab(i).dist_ccid := x_dist_tab(i).po_ccid;
1579 
1580         END IF;  /*IF (nvl(x_dist_tab(i).accrue_on_receipt_flag,'N') = 'N'...*/
1581 
1582      --For Prepayment type invoice build the prepayment account if
1583      --the system option build_prepayment_accounts_flag is set to Y.
1584      ELSE
1585 
1586         -- Contract Payments: If matching to an advance/financing pay item do not
1587         --                    use the prepay ccid, use the po charge account.
1588 
1589         IF g_shipment_type = 'PREPAYMENT' THEN
1590 
1591 	   x_dist_tab(i).dist_ccid := x_dist_tab(i).po_ccid;
1592 
1593         ELSE
1594 
1595 	   IF (g_build_prepay_accts_flag = 'Y') THEN
1596 
1597 	       l_debug_info := 'Calling build_prepay_account to build the prepayment account';
1598                IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1599                    FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1600                END IF;
1601                -- Bug 5465722
1602                ap_matching_pkg.build_prepay_account(
1603 	                        P_base_ccid => x_dist_tab(i).po_ccid,
1604 	                        P_overlay_ccid => g_prepay_ccid,
1605 	                        P_accounting_date => g_line_accounting_date,
1606 	                        P_result_Ccid => l_dist_ccid,
1607 	                        P_reason_unbuilt_flex => l_reason_unbuilt_flex,
1608 	                        P_calling_sequence => current_calling_sequence);
1609 
1610 	       IF (l_dist_ccid <> -1) THEN
1611 
1612 		   x_dist_tab(i).dist_ccid := l_dist_ccid;
1613 
1614 	       ELSE
1615                    /*OPEN ISSUE 3 */
1616                    l_debug_info := 'Flexbuild of prepayment account failed due to '
1617                                    ||l_reason_unbuilt_flex;
1618                    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1619                        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1620                    END IF;
1621 
1622                    RAISE FLEX_OVERLAY_FAILED;
1623 
1624                END IF;
1625 
1626            ELSE
1627 
1628                x_dist_tab(i).dist_ccid := g_prepay_ccid;
1629 
1630            END IF;
1631        END IF;
1632      END IF;   /*g_invoice_type_lookup_code <> 'PREPAYMENT'*/
1633 
1634   END IF; /* x_dist_tab.exists(i) */
1635 
1636 END LOOP;
1637 
1638 
1639  --Base Amount Rounding
1640  --Need to perform base_amount rounding for only foreign currency invoices.
1641  IF (g_line_base_amount <> l_sum_dist_base_amount AND g_exchange_rate IS NOT NULL AND l_rounding_index is not null) THEN
1642 
1643     x_dist_tab(l_rounding_index).base_amount := x_dist_tab(l_rounding_index).base_amount +
1644 						(g_line_base_amount - l_sum_dist_base_amount);
1645 
1646     x_dist_tab(l_rounding_index).rounding_amt := g_line_base_amount - l_sum_dist_base_amount;
1647 
1648  END IF;
1649 
1650  IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
1651     FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Get_Dist_Proration_Info(-)');
1652  END IF;
1653 
1654 
1655 
1656 EXCEPTION
1657 
1658  WHEN OTHERS THEN
1659    IF (SQLCODE <> -20001) THEN
1660      FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1661      FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1662      FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',current_calling_sequence);
1663      FND_MESSAGE.SET_TOKEN('PARAMETERS','Shipment id '||to_char(x_po_line_location_id)
1664                           ||', Match_mode = '||X_match_mode
1665                           ||', Match Quantity = '||x_match_quantity
1666                           ||', Match Amount = '||x_match_amount
1667                           ||', Exchange Rate = '||g_exchange_rate
1668                           ||', Base Currency = '||g_base_currency_code
1669  			  ||', Overbill = '||X_overbill_flag);
1670      FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1671    END IF;
1672    APP_EXCEPTION.RAISE_EXCEPTION;
1673 
1674 END Get_Dist_Proration_Info;
1675 
1676 
1677 
1678 /*---------------------------------------------------------------------------+
1679  |This procedure will retrieve total_quantity to be used for the purpose     |
1680  |of prorating amounts and quantities.					     |
1681  |                                                                           |
1682  |           The algorithm used is                                           |
1683  |                                                                           |
1684  |           IF (matching to std invoices) THEN                              |
1685  |               IF (this is an Overbill) THEN                               |
1686  |                   total_quantity = sum(qty_ordered)                       |
1687  |               ELSE (this is not an Overbill)                              |
1688  |                   total_quantity = sum(qty_ordered - (qty_billed +        |
1689  |                                                       qty_cancelled))     |
1690  |               END                                                         |
1691  |           ELSE                                                            |
1692  |               total_quantity = sum(qty_billed)                            |
1693  |           END                                                             |
1694  |                                                                           |
1695  +---------------------------------------------------------------------------*/
1696 PROCEDURE Get_Total_Proration_Quantity
1697 	                  ( X_PO_Line_Location_Id 	IN 	NUMBER,
1698 	                    X_Match_Mode     		IN 	VARCHAR2,
1699 			    X_Overbill_Flag  		IN 	VARCHAR2,
1700 			    X_Total_Quantity 		OUT NOCOPY NUMBER,
1701 			    X_Calling_Sequence	        IN	VARCHAR2) IS
1702 
1703 l_debug_info 			VARCHAR2(2000);
1704 current_calling_sequence	VARCHAR2(2000);
1705 l_api_name			VARCHAR2(50);
1706 
1707 BEGIN
1708 
1709    l_api_name := 'Get_Total_Proration_Quantity';
1710    current_calling_sequence := 'Get_Total_Proration_Quantity<-'||x_calling_sequence;
1711 
1712    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
1713       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Get_Total_Proration_Quantity(+)');
1714    END IF;
1715 
1716    l_debug_info := 'Get total quantity for proration';
1717    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1718         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1719    END IF;
1720 
1721 
1722    SELECT SUM(DECODE(X_Match_Mode,
1723 		     'STD-PS',DECODE(X_overbill_flag,
1724 	                             'Y', NVL(pd.quantity_ordered, 0),
1725 		                     NVL(DECODE(SIGN(pd.quantity_ordered
1726 			   	     	             - DECODE(PD.distribution_type,'PREPAYMENT',
1727 							      NVL(PD.quantity_financed,0),
1728 							      NVL(PD.quantity_billed,0)
1729 							     )
1730  		                                     - NVL(pd.quantity_cancelled,0)
1731 				                    ),
1732 			                        -1, 0,
1733 			                        quantity_ordered -
1734 			   		        DECODE(PD.distribution_type,'PREPAYMENT',
1735 						       NVL(PD.quantity_financed,0),
1736 						       NVL(PD.quantity_billed,0)
1737 						      )  -
1738 			   		        NVL(pd.quantity_cancelled, 0)
1739 					       )
1740 	     	                        ,0)
1741 			            ),
1742       	              DECODE(PD.distribution_type,'PREPAYMENT',
1743 		             NVL(PD.quantity_financed,0),
1744 		             NVL(PD.quantity_billed,0)
1745 		    	    )
1746 	            )
1747 	     )
1748    INTO      X_Total_Quantity
1749    FROM      po_distributions_ap_v PD
1750    WHERE     line_location_id = X_Po_Line_Location_Id;
1751 
1752    IF(x_total_quantity = 0) THEN
1753      x_total_quantity := 1;
1754    END IF;
1755 
1756    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
1757       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Get_Total_Proration_Quantity(-)');
1758    END IF;
1759 
1760 
1761 EXCEPTION
1762  WHEN OTHERS THEN
1763    IF (SQLCODE <> -20001) THEN
1764      FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1765      FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1766      FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',current_calling_sequence);
1767      FND_MESSAGE.SET_TOKEN('PARAMETERS','Match_mode = '||X_match_mode
1768 			  ||', Shipment_id = '||TO_CHAR(X_PO_Line_Location_id)
1769  			  ||', Overbill = '||X_overbill_flag);
1770      FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1771    END IF;
1772    APP_EXCEPTION.RAISE_EXCEPTION;
1773 
1774 END Get_Total_Proration_Quantity;
1775 
1776 
1777 
1778 Procedure Insert_Invoice_Line (X_Invoice_Id 	 	IN	NUMBER,
1779 			       X_Invoice_Line_Number 	IN    	NUMBER,
1780 			       X_Line_Type_Lookup_Code 	IN 	VARCHAR2,
1781 			       X_Cost_Factor_id		IN	NUMBER DEFAULT NULL,
1782 			       X_Single_Dist_Flag	IN	VARCHAR2 DEFAULT 'N',
1783 			       X_Po_Distribution_Id	IN	NUMBER   DEFAULT NULL,
1784        			       X_Po_Line_Location_Id	IN	NUMBER   DEFAULT NULL,
1785 			       X_Amount			IN	NUMBER,
1786 			       X_Quantity_Invoiced	IN	NUMBER   DEFAULT NULL,
1787 			       X_Unit_Price		IN	NUMBER   DEFAULT NULL,
1788 			       X_Final_Match_Flag	IN	VARCHAR2 DEFAULT NULL,
1789   			       X_Item_Line_Number	IN	NUMBER,
1790 			       X_Charge_Line_Description  IN 	VARCHAR2,
1791 			       X_Retained_Amount	IN	NUMBER	 DEFAULT NULL,
1792 			       X_Calling_Sequence	IN	VARCHAR2) IS
1793 
1794  current_calling_sequence	VARCHAR2(2000);
1795  l_debug_info			VARCHAR2(2000);
1796  l_api_name			VARCHAR2(50);
1797  l_full_mtch_amt                NUMBER;  --Bug13505998
1798 
1799 BEGIN
1800 
1801    l_api_name := 'Insert_Invoice_Line';
1802    current_calling_sequence := 'Insert_Invoice_Line<-'||x_calling_sequence;
1803 
1804    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
1805          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Insert_Invoice_Line(+)');
1806    END IF;
1807 
1808 
1809 
1810    IF (X_LINE_TYPE_LOOKUP_CODE = 'ITEM') THEN
1811 
1812    	l_debug_info := 'Inserting Item Line Matched to a PO';
1813 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1814 	     FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1815 	END IF;
1816 
1817 	--Bug#13505998
1818           l_full_mtch_amt :=
1819           ap_matching_utils_pkg.get_full_mtch_amt
1820              (X_Po_Line_Location_id,G_Invoice_Currency_Code);
1821 
1822             l_debug_info := 'l_full_mtch_amt '||l_full_mtch_amt;
1823 
1824 	   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1825 		FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1826 	   END IF;
1827          --End Bug#13505998
1828 
1829         -- bug 5061826
1830         -- go to base tables PO_LINES_ALL, PO_LINE_LOCATIONS and PO_DISTRIBUTIONS
1831         -- instead of PO_LINE_LOCATIONS_AP_V and PO_DISTRIBUTIONS_AP_V
1832 
1833 	INSERT INTO AP_INVOICE_LINES (
1834 	      INVOICE_ID,
1835 	      LINE_NUMBER,
1836 	      LINE_TYPE_LOOKUP_CODE,
1837 	      REQUESTER_ID,
1838 	      DESCRIPTION,
1839 	      LINE_SOURCE,
1840 	      ORG_ID,
1841 	      INVENTORY_ITEM_ID,
1842 	      ITEM_DESCRIPTION,
1843 	      SERIAL_NUMBER,
1844 	      MANUFACTURER,
1845 	      MODEL_NUMBER,
1846 	      GENERATE_DISTS,
1847 	      MATCH_TYPE,
1848 	      DISTRIBUTION_SET_ID,
1849 	      ACCOUNT_SEGMENT,
1850 	      BALANCING_SEGMENT,
1851 	      COST_CENTER_SEGMENT,
1852 	      OVERLAY_DIST_CODE_CONCAT,
1853 	      DEFAULT_DIST_CCID,
1854 	      PRORATE_ACROSS_ALL_ITEMS,
1855 	      LINE_GROUP_NUMBER,
1856 	      ACCOUNTING_DATE,
1857 	      PERIOD_NAME,
1858 	      DEFERRED_ACCTG_FLAG,
1859 	      DEF_ACCTG_START_DATE,
1860 	      DEF_ACCTG_END_DATE,
1861 	      DEF_ACCTG_NUMBER_OF_PERIODS,
1862 	      DEF_ACCTG_PERIOD_TYPE,
1863 	      SET_OF_BOOKS_ID,
1864 	      AMOUNT,
1865 	      BASE_AMOUNT,
1866 	      ROUNDING_AMT,
1867 	      QUANTITY_INVOICED,
1868 	      UNIT_MEAS_LOOKUP_CODE,
1869 	      UNIT_PRICE,
1870 	      WFAPPROVAL_STATUS,
1871 	   -- USSGL_TRANSACTION_CODE, - Bug 4277744
1872 	      DISCARDED_FLAG,
1873 	      ORIGINAL_AMOUNT,
1874 	      ORIGINAL_BASE_AMOUNT,
1875 	      ORIGINAL_ROUNDING_AMT,
1876 	      CANCELLED_FLAG,
1877 	      INCOME_TAX_REGION,
1878 	      TYPE_1099,
1879 	      STAT_AMOUNT,
1880 	      PREPAY_INVOICE_ID,
1881 	      PREPAY_LINE_NUMBER,
1882 	      INVOICE_INCLUDES_PREPAY_FLAG,
1883 	      CORRECTED_INV_ID,
1884 	      CORRECTED_LINE_NUMBER,
1885 	      PO_HEADER_ID,
1886 	      PO_LINE_ID,
1887 	      PO_RELEASE_ID,
1888 	      PO_LINE_LOCATION_ID,
1889 	      PO_DISTRIBUTION_ID,
1890 	      RCV_TRANSACTION_ID,
1891 	      FINAL_MATCH_FLAG,
1892 	      ASSETS_TRACKING_FLAG,
1893 	      ASSET_BOOK_TYPE_CODE,
1894 	      ASSET_CATEGORY_ID,
1895 	      PROJECT_ID,
1896 	      TASK_ID,
1897 	      EXPENDITURE_TYPE,
1898 	      EXPENDITURE_ITEM_DATE,
1899 	      EXPENDITURE_ORGANIZATION_ID,
1900 	      PA_QUANTITY,
1901 	      PA_CC_AR_INVOICE_ID,
1902 	      PA_CC_AR_INVOICE_LINE_NUM,
1903 	      PA_CC_PROCESSED_CODE,
1904 	      AWARD_ID,
1905 	      AWT_GROUP_ID,
1906 	      REFERENCE_1,
1907 	      REFERENCE_2,
1908 	      RECEIPT_VERIFIED_FLAG,
1909 	      RECEIPT_REQUIRED_FLAG,
1910 	      RECEIPT_MISSING_FLAG,
1911 	      JUSTIFICATION,
1912 	      EXPENSE_GROUP,
1913 	      START_EXPENSE_DATE,
1914 	      END_EXPENSE_DATE,
1915 	      RECEIPT_CURRENCY_CODE,
1916 	      RECEIPT_CONVERSION_RATE,
1917 	      RECEIPT_CURRENCY_AMOUNT,
1918 	      DAILY_AMOUNT,
1919 	      WEB_PARAMETER_ID,
1920 	      ADJUSTMENT_REASON,
1921 	      MERCHANT_DOCUMENT_NUMBER,
1922 	      MERCHANT_NAME,
1923 	      MERCHANT_REFERENCE,
1924 	      MERCHANT_TAX_REG_NUMBER,
1925 	      MERCHANT_TAXPAYER_ID,
1926 	      COUNTRY_OF_SUPPLY,
1927 	      CREDIT_CARD_TRX_ID,
1928 	      COMPANY_PREPAID_INVOICE_ID,
1929 	      CC_REVERSAL_FLAG,
1930 	      ATTRIBUTE_CATEGORY,
1931 	      ATTRIBUTE1,
1932       	      ATTRIBUTE2,
1933       	      ATTRIBUTE3,
1934       	      ATTRIBUTE4,
1935       	      ATTRIBUTE5,
1936       	      ATTRIBUTE6,
1937       	      ATTRIBUTE7,
1938       	      ATTRIBUTE8,
1939       	      ATTRIBUTE9,
1940       	      ATTRIBUTE10,
1941       	      ATTRIBUTE11,
1942       	      ATTRIBUTE12,
1943       	      ATTRIBUTE13,
1944       	      ATTRIBUTE14,
1945       	      ATTRIBUTE15,
1946       	      /* GLOBAL_ATTRIBUTE_CATEGORY,
1947 	      GLOBAL_ATTRIBUTE1,
1948       	      GLOBAL_ATTRIBUTE2,
1949       	      GLOBAL_ATTRIBUTE3,
1950       	      GLOBAL_ATTRIBUTE4,
1951       	      GLOBAL_ATTRIBUTE5,
1952       	      GLOBAL_ATTRIBUTE6,
1953       	      GLOBAL_ATTRIBUTE7,
1954        	      GLOBAL_ATTRIBUTE8,
1955       	      GLOBAL_ATTRIBUTE9,
1956        	      GLOBAL_ATTRIBUTE10,
1957       	      GLOBAL_ATTRIBUTE11,
1958       	      GLOBAL_ATTRIBUTE12,
1959       	      GLOBAL_ATTRIBUTE13,
1960       	      GLOBAL_ATTRIBUTE14,
1961       	      GLOBAL_ATTRIBUTE15,
1962       	      GLOBAL_ATTRIBUTE16,
1963       	      GLOBAL_ATTRIBUTE17,
1964       	      GLOBAL_ATTRIBUTE18,
1965       	      GLOBAL_ATTRIBUTE19,
1966       	      GLOBAL_ATTRIBUTE20, */
1967       	      CREATION_DATE,
1968       	      CREATED_BY,
1969       	      LAST_UPDATED_BY,
1970       	      LAST_UPDATE_DATE,
1971       	      LAST_UPDATE_LOGIN,
1972       	      PROGRAM_APPLICATION_ID,
1973       	      PROGRAM_ID,
1974       	      PROGRAM_UPDATE_DATE,
1975       	      REQUEST_ID,
1976 	      RETAINED_AMOUNT,
1977 	      RETAINED_AMOUNT_REMAINING,
1978        	      SHIP_TO_LOCATION_ID,
1979 	      PRIMARY_INTENDED_USE,
1980 	      PRODUCT_FISC_CLASSIFICATION,
1981 	      TRX_BUSINESS_CATEGORY,
1982 	      PRODUCT_TYPE,
1983 	      PRODUCT_CATEGORY,
1984 	      USER_DEFINED_FISC_CLASS,
1985 	      ASSESSABLE_VALUE,
1986 	      tax_classification_code,
1987 		  pay_awt_group_id)       --bug8222382
1988        SELECT X_INVOICE_ID,			--invoice_id
1989      	      X_INVOICE_LINE_NUMBER,		--invoice_line_number
1990      	      X_LINE_TYPE_LOOKUP_CODE,		--line_type_lookup_code
1991      	      DECODE(X_SINGLE_DIST_FLAG,'Y',
1992      	      PD.DELIVER_TO_PERSON_ID,NULL),--requester_id
1993 	       --bug 5061826,5601344 (added nvl)
1994     	      NVL(PLL.DESCRIPTION,PL.ITEM_DESCRIPTION),	--description
1995      	      'HEADER MATCH',			--line_source
1996     	      PLL.ORG_ID,			--org_id
1997     	      PL.ITEM_ID,			--inventory_item_id --bug 5061826-PLL to PL
1998      	      NVL(PLL.DESCRIPTION,PL.ITEM_DESCRIPTION),	--item_description
1999      	      NULL,				--serial_number
2000      	      NULL,				--manufacturer
2001      	      NULL,				--model_number
2002      	      'D',				--generate_dists
2003      	      'ITEM_TO_PO',			--match_type
2004      	      NULL,				--distribution_set_id
2005      	      NULL,				--account_segment
2006      	      NULL,				--balancing_segment
2007      	      NULL,				--cost_center_segment
2008      	      NULL,				--overlay_dist_code_concat
2009 	      --Bug6965650
2010               NULL,                             --default_dist_ccid
2011      	      'N',				--prorate_across_all_items
2012  	      NULL,				--line_group_number
2013  	      G_ACCOUNTING_DATE,		--accounting_date
2014  	      G_PERIOD_NAME,			--period_name
2015  	      'N',				--deferred_acctg_flag
2016  	      NULL,				--def_acctg_start_date
2017  	      NULL,				--def_acctg_end_date
2018  	      NULL,				--def_acctg_number_of_periods
2019  	      NULL,				--def_acctg_period_type
2020  	      G_SET_OF_BOOKS_ID,		--set_of_books_id
2021  	      X_AMOUNT,				--amount
2022  	      AP_UTILITIES_PKG.Ap_Round_Currency(
2023                  NVL(X_AMOUNT, 0) * G_EXCHANGE_RATE,
2024                		 G_BASE_CURRENCY_CODE), --base_amount
2025  	      NULL,				--rounding_amount
2026  	      X_QUANTITY_INVOICED,		--quantity_invoiced
2027  	      PLL.UNIT_MEAS_LOOKUP_CODE,	--unit_meas_lookup_code
2028  	      X_UNIT_PRICE,			--unit_price
2029  	      decode(g_approval_workflow_flag,'Y'
2030 	            ,'REQUIRED','NOT REQUIRED'),--wf_approval_status
2031            -- Removed for bug 4277744
2032  	   -- PLL.USSGL_TRANSACTION_CODE,	--ussgl_transaction_code
2033  	      'N',				--discarded_flag
2034  	      NULL,				--original_amount
2035  	      NULL,				--original_base_amount
2036  	      NULL,				--original_rounding_amt
2037  	      'N',				--cancelled_flag
2038  	      G_INCOME_TAX_REGION,		--income_tax_region
2039  	      PL.TYPE_1099,			--type_1099 -- bug 5061826-PLL to PL
2040  	      NULL,				--stat_amount
2041  	      NULL,				--prepay_invoice_id
2042  	      NULL,				--prepay_line_number
2043  	      NULL,				--invoice_includes_prepay_flag
2044  	      NULL,				--corrected_inv_id
2045  	      NULL,				--corrected_line_number
2046  	      PLL.PO_HEADER_ID,			--po_header_id
2047  	      PLL.PO_LINE_ID,			--po_line_id
2048  	      PLL.PO_RELEASE_ID,		--po_release_id
2049  	      PLL.LINE_LOCATION_ID,		--po_line_location_id
2050  	      DECODE(X_SINGLE_DIST_FLAG,'Y',
2051  	       	     X_PO_DISTRIBUTION_ID,NULL),--po_distribution_id
2052     	      NULL,				--rcv_transaction_id
2053    	      X_FINAL_MATCH_FLAG,		--final_match_flag
2054     	      'N',				--assets_tracking_flag
2055     	      G_ASSET_BOOK_TYPE_CODE,		--asset_book_type_code
2056     	      MSI.ASSET_CATEGORY_ID,		--asset_category_id
2057     	      DECODE(X_SINGLE_DIST_FLAG,'Y',
2058     	             DECODE(PD.destination_type_code,
2059 		     	    'EXPENSE',PD.project_id,
2060 			    G_PROJECT_ID),
2061 	    	     NULL),			--project_id
2062    	      DECODE(X_SINGLE_DIST_FLAG,'Y',
2063     	             DECODE(PD.destination_type_code,
2064 		     	    'EXPENSE',PD.task_id,
2065 			    G_TASK_ID),
2066 	    	     NULL),			--task_id
2067  	      DECODE(X_SINGLE_DIST_FLAG,'Y',
2068  	             DECODE(PD.destination_type_code,
2069 		     	    'EXPENSE',PD.expenditure_type,
2070 			    G_EXPENDITURE_TYPE),
2071  	             NULL),			--expenditure_type
2072 	      DECODE(X_SINGLE_DIST_FLAG,'Y',
2073                 -- Bug 5294998. Calling project API
2074                     PA_AP_INTEGRATION.Get_Si_Cost_Exp_Item_Date (
2075                       g_invoice_date,
2076                       g_accounting_date,
2077                       NULL,
2078                       sysdate,
2079                       x_po_distribution_id,
2080                       'PO-MATCH'),
2081                      NULL),                         --expenditure_item_date
2082  	    /*   DECODE(g_pa_expenditure_date_default,
2083       		    'PO Expenditure Item Date/Transaction Date',
2084       		     DECODE(PD.Destination_type_code,
2085 		            'EXPENSE',PD.EXPENDITURE_ITEM_DATE,
2086 			    G_INVOICE_DATE),
2087       		    'PO Expenditure Item Date/Transaction GL Date',
2088       		     DECODE(PD.destination_type_code,
2089 		            'EXPENSE', PD.EXPENDITURE_ITEM_DATE,
2090 			    G_ACCOUNTING_DATE),
2091      		     'PO Expenditure Item Date/Transaction System Date',
2092      		     DECODE(PD.destination_type_code,
2093 		            'EXPENSE',PD.EXPENDITURE_ITEM_DATE,
2094 			    SYSDATE),
2095      		     'Receipt Date/Transaction Date',G_INVOICE_DATE,
2096      		     'Receipt Date/Transaction GL Date',G_ACCOUNTING_DATE,
2097      		     'Receipt Date/Transaction System Date',SYSDATE,
2098      		     'Transaction Date',G_INVOICE_DATE,
2099      		     'Transaction GL Date',G_ACCOUNTING_DATE,
2100      		     'Transaction System Date', SYSDATE), /
2101 		 NULL), */
2102  	       DECODE(X_SINGLE_DIST_FLAG,'Y',
2103  	              DECODE(PD.destination_type_code,
2104 		      	     'EXPENSE',PD.expenditure_organization_id,
2105 			     G_EXPENDITURE_ORGANIZATION_ID),
2106  	      	      NULL),		--expenditure_organization_id
2107  	       DECODE( DECODE(X_SINGLE_DIST_FLAG,'Y',
2108  	      	              DECODE(PD.destination_type_code,
2109 			             'EXPENSE',PD.project_id,
2110 				     G_PROJECT_ID),
2111  	      	              NULL),
2112 		      '','',x_quantity_invoiced),   --pa_quantity
2113  	       NULL,				--pa_cc_ar_invoice_id
2114  	       NULL,				--pa_cc_ar_invoice_line_num
2115  	       NULL,				--pa_cc_processed_code
2116                DECODE(X_SINGLE_DIST_FLAG,
2117                         'Y', nvl(gms_ap_api.get_distribution_award(PD.AWARD_ID), G_AWARD_ID),
2118                         NULL),  		--award_id
2119  	       G_AWT_GROUP_ID,			--awt_group_id
2120  	       NULL,    			--reference_1
2121  	       NULL,				--reference_2
2122  	       NULL,				--receipt_verified_flag
2123  	       NULL,				--receipt_required_flag
2124  	       NULL,				--receipt_missing_flag
2125  	       NULL,				--justification
2126  	       NULL,				--expense_group
2127  	       NULL,				--start_expense_date
2128  	       NULL,				--end_expense_date
2129  	       NULL,				--receipt_currency_code
2130  	       NULL,				--receipt_conversion_rate
2131  	       NULL,				--receipt_currency_amount
2132  	       NULL,				--daily_amount
2133  	       NULL,				--web_parameter_id
2134  	       NULL,				--adjustment_reason
2135  	       NULL,				--merchant_document_number
2136  	       NULL,				--merchant_name
2137  	       NULL,				--merchant_reference
2138  	       NULL,				--merchant_tax_reg_number
2139  	       NULL,				--merchant_taxpayer_id
2140  	       NULL,				--country_of_supply
2141  	       NULL,				--credit_card_trx_id
2142  	       NULL,				--company_prepaid_invoice_id
2143  	       NULL,				--cc_reversal_flag
2144  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute_category),''),--attribute_category
2145  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute1),''),	--attribute1
2146  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute2),''),	--attribute2
2147  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute3),''),	--attribute3
2148  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute4),''),	--attribute4
2149  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute5),''),	--attribute5
2150  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute6),''),	--attribute6
2151  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute7),''),	--attribute7
2152  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute8),''),	--attribute8
2153  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute9),''),	--attribute9
2154  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute10),''),	--attribute10
2155  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute11),''),	--attribute11
2156  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute12),''),	--attribute12
2157  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute13),''),	--attribute13
2158  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute14),''),	--attribute14
2159  	       NVL(DECODE(g_transfer_flag,'Y',PLL.attribute15),''),	--attribute15
2160  	       /* X_GLOBAL_ATTRIBUTE_CATEGORY,	--global_attribute_category
2161 	       X_GLOBAL_ATTRIBUTE1,		--global_attribute1
2162       	       X_GLOBAL_ATTRIBUTE2,		--global_attribute2
2163 	       X_GLOBAL_ATTRIBUTE3,		--global_attribute3
2164       	       X_GLOBAL_ATTRIBUTE4,		--global_attribute4
2165       	       X_GLOBAL_ATTRIBUTE5,		--global_attribute5
2166       	       X_GLOBAL_ATTRIBUTE6,		--global_attribute6
2167       	       X_GLOBAL_ATTRIBUTE7,		--global_attribute7
2168        	       X_GLOBAL_ATTRIBUTE8,		--global_attribute8
2169       	       X_GLOBAL_ATTRIBUTE9,		--global_attribute9
2170        	       X_GLOBAL_ATTRIBUTE10,		--global_attribute10
2171       	       X_GLOBAL_ATTRIBUTE11,		--global_attribute11
2172       	       X_GLOBAL_ATTRIBUTE12,		--global_attribute12
2173       	       X_GLOBAL_ATTRIBUTE13,		--global_attribute13
2174       	       X_GLOBAL_ATTRIBUTE14,		--global_attribute14
2175       	       X_GLOBAL_ATTRIBUTE15,		--global_attribute15
2176       	       X_GLOBAL_ATTRIBUTE16,		--global_attribute16
2177       	       X_GLOBAL_ATTRIBUTE17,		--global_attribute17
2178       	       X_GLOBAL_ATTRIBUTE18,		--global_attribute18
2179       	       X_GLOBAL_ATTRIBUTE19,		--global_attribute19
2180       	       X_GLOBAL_ATTRIBUTE20, */ 	--global_attribute20
2181       	       SYSDATE,				--creation_date
2182       	       G_USER_ID,			--created_by
2183       	       G_USER_ID,			--last_update_by
2184       	       SYSDATE,				--last_update_date
2185       	       G_LOGIN_ID,			--last_update_login
2186       	       NULL,				--program_application_id
2187 	       NULL,				--program_id
2188       	       NULL,				--program_update_date
2189       	       NULL,  	      		       	--request_id
2190 	       X_RETAINED_AMOUNT,		--retained_amount
2191 	       (-X_RETAINED_AMOUNT),		--retained_amount_remaining
2192 	       PLL.SHIP_TO_LOCATION_ID,         --ship_to_location_id
2193 	       --bugfix:5565310
2194 	       G_INTENDED_USE,                  --primary_intended_use
2195 	       G_PRODUCT_FISC_CLASS,            --product_fisc_classification
2196 	       G_TRX_BUSINESS_CATEGORY,         --trx_business_category
2197 	       G_PRODUCT_TYPE,                  --product_type
2198 	       G_PRODUCT_CATEGORY,              --product_category
2199 	       G_USER_DEFINED_FISC_CLASS,        --user_defined_fisc_class
2200 	       --Modified for bug#13505998 - G_ASSESSABLE_VALUE,
2201 	       DECODE(X_Amount,l_full_mtch_amt,G_ASSESSABLE_VALUE, NULL),
2202 	       G_dflt_tax_class_code,
2203 		   G_PAY_AWT_GROUP_ID			--pay_awt_group_id  bug8222382
2204           -- bug 5061826 -- new FROM clause that goes to base tables
2205           FROM PO_LINES_ALL PL,
2206                PO_LINE_LOCATIONS_ALL PLL,
2207                po_distributions pd,
2208                mtl_system_items msi
2209           WHERE pll.line_location_id =  x_po_line_location_id
2210             and pd.line_location_id = pll.line_location_id
2211             AND PLL.PO_LINE_ID = PL.PO_LINE_ID
2212             and pd.po_distribution_id = nvl(x_po_distribution_id,pd.po_distribution_id)
2213             and msi.inventory_item_id(+) = pl.item_id
2214             and msi.organization_id(+) = g_inventory_organization_id
2215             and rownum = 1;
2216  	 /* -- bug 5061826 -- commented out older FROM clause
2217          FROM PO_LINE_LOCATIONS_AP_V PLL,
2218  	       PO_DISTRIBUTIONS_AP_V PD,
2219  	       MTL_SYSTEM_ITEMS MSI
2220  	 WHERE PLL.LINE_LOCATION_ID = X_PO_LINE_LOCATION_ID
2221  	  AND PD.LINE_LOCATION_ID = PLL.LINE_LOCATION_ID
2222  	  AND PD.PO_DISTRIBUTION_ID = NVL(X_PO_DISTRIBUTION_ID,PD.PO_DISTRIBUTION_ID)
2223  	  AND MSI.INVENTORY_ITEM_ID(+) = PLL.ITEM_ID
2224  	  AND MSI.ORGANIZATION_ID(+) = G_INVENTORY_ORGANIZATION_ID
2225 	  AND ROWNUM = 1; */
2226 
2227 
2228     /* for charge lines (frt and misc) allocated during matching */
2229     ELSIF (x_line_type_lookup_code IN ('FREIGHT','MISCELLANEOUS')) THEN
2230 
2231         l_debug_info := 'Inserting Charge Line';
2232 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2233 	     FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2234 	END IF;
2235 
2236         INSERT INTO AP_INVOICE_LINES (
2237 	      INVOICE_ID,
2238 	      LINE_NUMBER,
2239 	      LINE_TYPE_LOOKUP_CODE,
2240 	      REQUESTER_ID,
2241 	      DESCRIPTION,
2242 	      LINE_SOURCE,
2243 	      ORG_ID,
2244 	      INVENTORY_ITEM_ID,
2245 	      ITEM_DESCRIPTION,
2246 	      SERIAL_NUMBER,
2247 	      MANUFACTURER,
2248 	      MODEL_NUMBER,
2249 	      GENERATE_DISTS,
2250 	      MATCH_TYPE,
2251 	      DISTRIBUTION_SET_ID,
2252 	      ACCOUNT_SEGMENT,
2253 	      BALANCING_SEGMENT,
2254 	      COST_CENTER_SEGMENT,
2255 	      OVERLAY_DIST_CODE_CONCAT,
2256 	      DEFAULT_DIST_CCID,
2257 	      PRORATE_ACROSS_ALL_ITEMS,
2258 	      LINE_GROUP_NUMBER,
2259 	      ACCOUNTING_DATE,
2260 	      PERIOD_NAME,
2261 	      DEFERRED_ACCTG_FLAG,
2262 	      DEF_ACCTG_START_DATE,
2263 	      DEF_ACCTG_END_DATE,
2264 	      DEF_ACCTG_NUMBER_OF_PERIODS,
2265 	      DEF_ACCTG_PERIOD_TYPE,
2266 	      SET_OF_BOOKS_ID,
2267 	      AMOUNT,
2268 	      BASE_AMOUNT,
2269 	      ROUNDING_AMT,
2270 	      QUANTITY_INVOICED,
2271 	      UNIT_MEAS_LOOKUP_CODE,
2272 	      UNIT_PRICE,
2273 	      WFAPPROVAL_STATUS,
2274 	   -- USSGL_TRANSACTION_CODE, - Bug 4277744
2275 	      DISCARDED_FLAG,
2276 	      ORIGINAL_AMOUNT,
2277 	      ORIGINAL_BASE_AMOUNT,
2278 	      ORIGINAL_ROUNDING_AMT,
2279 	      CANCELLED_FLAG,
2280 	      INCOME_TAX_REGION,
2281 	      TYPE_1099,
2282 	      STAT_AMOUNT,
2283 	      PREPAY_INVOICE_ID,
2284 	      PREPAY_LINE_NUMBER,
2285 	      INVOICE_INCLUDES_PREPAY_FLAG,
2286 	      CORRECTED_INV_ID,
2287 	      CORRECTED_LINE_NUMBER,
2288 	      PO_HEADER_ID,
2289 	      PO_LINE_ID,
2290 	      PO_RELEASE_ID,
2291 	      PO_LINE_LOCATION_ID,
2292 	      PO_DISTRIBUTION_ID,
2293 	      RCV_TRANSACTION_ID,
2294 	      FINAL_MATCH_FLAG,
2295 	      ASSETS_TRACKING_FLAG,
2296 	      ASSET_BOOK_TYPE_CODE,
2297 	      ASSET_CATEGORY_ID,
2298 	      PROJECT_ID,
2299 	      TASK_ID,
2300 	      EXPENDITURE_TYPE,
2301 	      EXPENDITURE_ITEM_DATE,
2302 	      EXPENDITURE_ORGANIZATION_ID,
2303 	      PA_QUANTITY,
2304 	      PA_CC_AR_INVOICE_ID,
2305 	      PA_CC_AR_INVOICE_LINE_NUM,
2306 	      PA_CC_PROCESSED_CODE,
2307 	      AWARD_ID,
2308 	      AWT_GROUP_ID,
2309 	      REFERENCE_1,
2310 	      REFERENCE_2,
2311 	      RECEIPT_VERIFIED_FLAG,
2312 	      RECEIPT_REQUIRED_FLAG,
2313 	      RECEIPT_MISSING_FLAG,
2314 	      JUSTIFICATION,
2315 	      EXPENSE_GROUP,
2316 	      START_EXPENSE_DATE,
2317 	      END_EXPENSE_DATE,
2318 	      RECEIPT_CURRENCY_CODE,
2319 	      RECEIPT_CONVERSION_RATE,
2320 	      RECEIPT_CURRENCY_AMOUNT,
2321 	      DAILY_AMOUNT,
2322 	      WEB_PARAMETER_ID,
2323 	      ADJUSTMENT_REASON,
2324 	      MERCHANT_DOCUMENT_NUMBER,
2325 	      MERCHANT_NAME,
2326 	      MERCHANT_REFERENCE,
2327 	      MERCHANT_TAX_REG_NUMBER,
2328 	      MERCHANT_TAXPAYER_ID,
2329 	      COUNTRY_OF_SUPPLY,
2330 	      CREDIT_CARD_TRX_ID,
2331 	      COMPANY_PREPAID_INVOICE_ID,
2332 	      CC_REVERSAL_FLAG,
2333 	      ATTRIBUTE_CATEGORY,
2334 	      ATTRIBUTE1,
2335       	      ATTRIBUTE2,
2336       	      ATTRIBUTE3,
2337       	      ATTRIBUTE4,
2338       	      ATTRIBUTE5,
2339       	      ATTRIBUTE6,
2340       	      ATTRIBUTE7,
2341       	      ATTRIBUTE8,
2342       	      ATTRIBUTE9,
2343       	      ATTRIBUTE10,
2344       	      ATTRIBUTE11,
2345       	      ATTRIBUTE12,
2346       	      ATTRIBUTE13,
2347       	      ATTRIBUTE14,
2348       	      ATTRIBUTE15,
2349       	      /* GLOBAL_ATTRIBUTE_CATEGORY,
2350 	      GLOBAL_ATTRIBUTE1,
2351       	      GLOBAL_ATTRIBUTE2,
2352       	      GLOBAL_ATTRIBUTE3,
2353       	      GLOBAL_ATTRIBUTE4,
2354       	      GLOBAL_ATTRIBUTE5,
2355       	      GLOBAL_ATTRIBUTE6,
2356       	      GLOBAL_ATTRIBUTE7,
2357        	      GLOBAL_ATTRIBUTE8,
2358       	      GLOBAL_ATTRIBUTE9,
2359        	      GLOBAL_ATTRIBUTE10,
2360       	      GLOBAL_ATTRIBUTE11,
2361       	      GLOBAL_ATTRIBUTE12,
2362       	      GLOBAL_ATTRIBUTE13,
2363       	      GLOBAL_ATTRIBUTE14,
2364       	      GLOBAL_ATTRIBUTE15,
2365       	      GLOBAL_ATTRIBUTE16,
2366       	      GLOBAL_ATTRIBUTE17,
2367       	      GLOBAL_ATTRIBUTE18,
2368       	      GLOBAL_ATTRIBUTE19,
2369       	      GLOBAL_ATTRIBUTE20, */
2370       	      CREATION_DATE,
2371       	      CREATED_BY,
2372       	      LAST_UPDATED_BY,
2373       	      LAST_UPDATE_DATE,
2374       	      LAST_UPDATE_LOGIN,
2375       	      PROGRAM_APPLICATION_ID,
2376       	      PROGRAM_ID,
2377       	      PROGRAM_UPDATE_DATE,
2378       	      REQUEST_ID,
2379 	      RETAINED_AMOUNT,
2380 	      RETAINED_AMOUNT_REMAINING,
2381 	      SHIP_TO_LOCATION_ID,
2382 	      --bugfix:5565310
2383 	      PRIMARY_INTENDED_USE,
2384 	      PRODUCT_FISC_CLASSIFICATION,
2385 	      TRX_BUSINESS_CATEGORY,
2386 	      PRODUCT_TYPE,
2387 	      PRODUCT_CATEGORY,
2388 	      USER_DEFINED_FISC_CLASS,
2389 	      ASSESSABLE_VALUE,
2390 	      TAX_CLASSIFICATION_CODE,
2391 	      COST_FACTOR_ID,
2392 		  PAY_AWT_GROUP_ID  --bug 8222382
2393 	      )
2394     SELECT    X_INVOICE_ID,			--invoice_id
2395      	      X_INVOICE_LINE_NUMBER,		--invoice_line_number
2396      	      X_LINE_TYPE_LOOKUP_CODE,		--line_type_lookup_code
2397      	      AIL.REQUESTER_ID,			--requester_id
2398      --bug 5102208
2399               -- RTRIM added in BUG 10329867
2400   	        RTRIM(SUBSTRB(X_CHARGE_LINE_DESCRIPTION || AIL.description, 1, 240)),--description
2401 	      'CHRG ITEM MATCH',		--line_source
2402      	      AIL.ORG_ID,			--org_id
2403      	      NULL,				--inventory_item_id
2404      	      NULL,				--item_description
2405      	      NULL,				--serial_number
2406      	      NULL,				--manufacturer
2407      	      NULL,				--model_number
2408      	      'Y',				--generate_dists
2409      	      'NOT_MATCHED',			--match_type
2410      	      NULL,				--distribution_set_id
2411     	      NULL,				--account_segment
2412      	      NULL,				--balancing_segment
2413      	      NULL,				--cost_center_segment
2414      	      NULL,				--overlay_dist_code_concat
2415      	      --Bug6965650
2416  	      NULL,                     	--default_dist_ccid
2417     	      'N',				--prorate_across_all_items
2418      	      NULL,				--line_group_number
2419      	      AIL.ACCOUNTING_DATE,		--accounting_date
2420      	      AIL.PERIOD_NAME,			--period_name
2421  	      'N',				--deferred_acctg_flag
2422  	      NULL,				--deferred_acctg_start_date
2423  	      NULL,				--deferred_acctg_end_date
2424  	      NULL,				--def_acctg_number_of_periods
2425  	      NULL,				--def_acctg_period_type
2426  	      AIL.SET_OF_BOOKS_ID,	  	--set_of_books_id
2427  	      X_AMOUNT,				--amount
2428  	      AP_UTILITIES_PKG.Ap_Round_Currency(
2429               	    NVL(X_AMOUNT, 0) * G_EXCHANGE_RATE,
2430                    	 G_BASE_CURRENCY_CODE),	--base_amount
2431  	      NULL,			 	--rounding_amount
2432  	      NULL,				--quantity_invoiced
2433  	      NULL,				--unit_meas_lookup_code
2434  	      NULL,				--unit_price
2435 	      AIL.WFAPPROVAL_STATUS,            --wf_approval_status
2436            -- Removed for bug 4277744
2437  	   -- NULL,				--ussgl_transaction_code
2438  	      'N',				--discarded_flag
2439  	      NULL,				--original_amount
2440  	      NULL,				--original_base_amount
2441  	      NULL,				--original_rounding_amt
2442  	      'N',				--cancelled_flag
2443  	      AIL.INCOME_TAX_REGION,		--income_tax_region
2444  	      AIL.TYPE_1099,			--type_1099
2445  	      NULL,				--stat_amount
2446  	      NULL,				--prepay_invoice_id
2447  	      NULL,				--prepay_line_number
2448  	      NULL,				--invoice_includes_prepay_flag
2449  	      NULL,				--corrected_inv_id
2450  	      NULL,				--corrected_line_number
2451  	      NULL,				--po_header_id
2452  	      NULL,				--po_line_id
2453  	      NULL,				--po_release_id
2454  	      NULL,				--po_line_location_id
2455  	      NULL,				--po_distribution_id
2456  	      NULL,				--rcv_transaction_id
2457  	      'N',				--final_match_flag
2458  	      'N',				--assets_tracking_flag
2459  	      NULL,				--asset_book_type_code
2460  	      NULL,				--asset_category_id
2461  	      AIL.PROJECT_ID,			--project_id
2462  	      AIL.TASK_ID,			--task_id
2463  	      AIL.EXPENDITURE_TYPE,		--expenditure_type
2464  	      AIL.EXPENDITURE_ITEM_DATE,	--expenditure_item_date
2465  	      AIL.EXPENDITURE_ORGANIZATION_ID,	--expenditure_organization_id
2466  	      NULL,				--pa_quantity
2467  	      NULL,				--pa_cc_Ar_invoice_id
2468  	      NULL,				--pa_cc_Ar_invoice_line_num
2469  	      NULL,				--pa_cc_processed_code
2470  	      AIL.AWARD_ID,			--award_id
2471  	      AIL.AWT_GROUP_ID,			--awt_group_id
2472  	      NULL,				--reference_1
2473  	      NULL,				--reference_2
2474  	      NULL,				--receipt_verified_flag
2475  	      NULL,				--receipt_required_flag
2476  	      NULL,				--receipt_missing_flag
2477  	      NULL,				--justification
2478  	      NULL,				--expense_group
2479  	      NULL,				--start_expense_date
2480  	      NULL,				--end_expense_date
2481  	      NULL,				--receipt_currency_code
2482  	      NULL,				--receipt_conversion_rate
2483  	      NULL,				--receipt_currency_amount
2484  	      NULL,				--daily_amount
2485  	      NULL,				--web_parameter_id
2486  	      NULL,				--adjustment_reason
2487  	      NULL,				--merchant_document_number
2488  	      NULL,				--merchant_name
2489  	      NULL,				--merchant_reference
2490  	      NULL,				--merchant_tax_reg_number
2491  	      NULL,				--merchant_taxpayer_id
2492  	      NULL,				--country_of_supply
2493  	      NULL,				--credit_card_trx_id
2494  	      NULL,				--company_prepaid_invoice_id
2495  	      NULL,				--cc_reversal_flag
2496  	      AIL.attribute_category,		--attribute_category
2497  	      AIL.attribute1,			--attribute1
2498  	      AIL.attribute2,			--attribute2
2499  	      AIL.attribute3,			--attribute3
2500  	      AIL.attribute4,			--attribute4
2501  	      AIL.attribute5,			--attribute5
2502  	      AIL.attribute6,			--attribute6
2503  	      AIL.attribute7,			--attribute7
2504  	      AIL.attribute8,			--attribute8
2505  	      AIL.attribute9,			--attribute9
2506  	      AIL.attribute10,			--attribute10
2507  	      AIL.attribute11,			--attribute11
2508  	      AIL.attribute12,			--attribute12
2509  	      AIL.attribute13,			--attribute13
2510  	      AIL.attribute14,			--attribute14
2511  	      AIL.attribute15,			--attribute15
2512  	      /* X_GLOBAL_ATTRIBUTE_CATEGORY,	--global_attribute_category
2513 	      X_GLOBAL_ATTRIBUTE1,		--global_attribute1
2514       	      X_GLOBAL_ATTRIBUTE2,		--global_attribute2
2515 	      X_GLOBAL_ATTRIBUTE3,		--global_attribute3
2516       	      X_GLOBAL_ATTRIBUTE4,		--global_attribute4
2517       	      X_GLOBAL_ATTRIBUTE5,		--global_attribute5
2518       	      X_GLOBAL_ATTRIBUTE6,		--global_attribute6
2519       	      X_GLOBAL_ATTRIBUTE7,		--global_attribute7
2520        	      X_GLOBAL_ATTRIBUTE8,		--global_attribute8
2521       	      X_GLOBAL_ATTRIBUTE9,		--global_attribute9
2522        	      X_GLOBAL_ATTRIBUTE10,		--global_attribute10
2523       	      X_GLOBAL_ATTRIBUTE11,		--global_attribute11
2524       	      X_GLOBAL_ATTRIBUTE12,		--global_attribute12
2525       	      X_GLOBAL_ATTRIBUTE13,		--global_attribute13
2526       	      X_GLOBAL_ATTRIBUTE14,		--global_attribute14
2527       	      X_GLOBAL_ATTRIBUTE15,		--global_attribute15
2528       	      X_GLOBAL_ATTRIBUTE16,		--global_attribute16
2529       	      X_GLOBAL_ATTRIBUTE17,		--global_attribute17
2530       	      X_GLOBAL_ATTRIBUTE18,		--global_attribute18
2531       	      X_GLOBAL_ATTRIBUTE19,		--global_attribute19
2532       	      X_GLOBAL_ATTRIBUTE20, */ 		--global_attribute20
2533       	      SYSDATE,				--creation_date
2534       	      G_USER_ID,			--created_by
2535       	      G_USER_ID,			--last_updated_by
2536       	      SYSDATE,				--last_updated_date
2537       	      G_LOGIN_ID,			--last_update_login
2538       	      NULL,				--program_application_id
2539       	      NULL,				--program_id
2540       	      NULL,				--program_update_date
2541       	      NULL,  	    	      		--request_id
2542 	       X_RETAINED_AMOUNT,		--retained_amount
2543 	       -(X_RETAINED_AMOUNT),		--retained_amount_remaining
2544               AIL.SHIP_TO_LOCATION_ID,         --ship_to_location_id
2545 	      --bugfix:5565310
2546               G_INTENDED_USE,                  --primary_intended_use
2547               G_PRODUCT_FISC_CLASS,            --product_fisc_classification
2548               G_TRX_BUSINESS_CATEGORY,         --trx_business_category
2549               G_PRODUCT_TYPE,                  --product_type
2550               G_PRODUCT_CATEGORY,              --product_category
2551               G_USER_DEFINED_FISC_CLASS,       --user_defined_fisc_class
2552 	      --G_ASSESSABLE_VALUE, --Bug#13505998
2553 	      DECODE(X_Amount,l_full_mtch_amt,X_AMOUNT, NULL),
2554 	      G_dflt_tax_class_code,
2555 	      X_COST_FACTOR_ID,		       --cost_factor_id
2556 		  AIL.PAY_AWT_GROUP_ID			--pay_awt_group_id     bug8222382
2557  	FROM  AP_INVOICE_LINES AIL
2558       	WHERE AIL.INVOICE_ID = X_INVOICE_ID
2559           AND AIL.LINE_NUMBER = X_ITEM_LINE_NUMBER;
2560 
2561     END IF;
2562 
2563     g_max_invoice_line_number := g_max_invoice_line_number + 1;
2564 
2565    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
2566          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Insert_Invoice_Line(-)');
2567    END IF;
2568 
2569 
2570   EXCEPTION WHEN OTHERS THEN
2571     IF (SQLCODE <> -20001) THEN
2572      FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
2573      FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
2574      FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',current_calling_sequence);
2575      FND_MESSAGE.SET_TOKEN('PARAMETERS','Invoice_Id = '||to_char(x_invoice_id)
2576 	||', Invoice Line Number = '||to_char(x_invoice_line_number)
2577  	||', PO Distribution Id = '||to_char(x_po_distribution_id)
2578  	||', Project Id = '||to_char(g_project_id)
2579  	||', Task_Id ='||to_char(g_task_id)
2580  	||', Expenditure Type ='||g_expenditure_type
2581  	||', Expenditure_Organization_id ='||to_char(g_expenditure_organization_id));
2582      FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
2583    END IF;
2584    APP_EXCEPTION.RAISE_EXCEPTION;
2585 
2586 END Insert_Invoice_Line;
2587 
2588 
2589 
2590 PROCEDURE Insert_Invoice_Distributions (
2591 		X_Invoice_ID		IN   NUMBER,
2592 		X_Invoice_Line_Number	IN   NUMBER,
2593 		X_Dist_Tab		IN OUT NOCOPY Dist_Tab_Type,
2594 		X_Final_Match_Flag	IN   VARCHAR2,
2595 		X_Unit_Price		IN   NUMBER,
2596 		X_Total_Amount		IN   NUMBER,
2597 		X_Calling_Sequence	IN   VARCHAR2) IS
2598 
2599 i				NUMBER;
2600 l_distribution_line_number	NUMBER := 1;
2601 l_debug_info			VARCHAR2(2000);
2602 current_calling_sequence	VARCHAR2(2000);
2603 l_api_name		        VARCHAR2(50);
2604 l_copy_line_dff_flag    VARCHAR2(1); -- Bug 6837035
2605 
2606 BEGIN
2607   l_api_name := 'Insert_Invoice_Distributions';
2608 
2609   current_calling_sequence := 'Insert_Invoice_Distributions <-'||x_calling_sequence;
2610   IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
2611         FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Insert_Invoice_Distributions(+)');
2612   END IF;
2613 
2614   l_debug_info := 'Insert Invoice Distributions';
2615   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2616        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2617   END IF;
2618 
2619   -- Bug 6837035 Retrieve the profile value to check if the DFF info should be
2620   -- copied onto distributions for imported lines.
2621   l_copy_line_dff_flag := NVL(fnd_profile.value('AP_COPY_INV_LINE_DFF'),'N');
2622   FOR i in nvl(X_Dist_tab.FIRST, 0) .. nvl(X_Dist_tab.LAST, 0) LOOP
2623 
2624      IF (x_dist_tab.exists(i)) THEN
2625 
2626        l_debug_info := 'Insert invoice distribution corresponding to po_distribution : '||x_dist_tab(i).po_distribution_id;
2627        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2628           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2629        END IF;
2630 
2631        INSERT INTO ap_invoice_distributions (
2632 		batch_id,
2633                 invoice_id,
2634                 invoice_line_number,
2635                 invoice_distribution_id,
2636                 distribution_line_number,
2637                 line_type_lookup_code,
2638                 description,
2639                 dist_match_type,
2640                 distribution_class,
2641                 org_id,
2642                 dist_code_combination_id,
2643                 accounting_date,
2644                 period_name,
2645                 amount_to_post,
2646                 base_amount_to_post,
2647                 posted_amount,
2648                 posted_base_amount,
2649                 je_batch_id,
2650                 cash_je_batch_id,
2651                 posted_flag,
2652                 accounting_event_id,
2653                 upgrade_posted_amt,
2654                 upgrade_base_posted_amt,
2655                 set_of_books_id,
2656                 amount,
2657                 base_amount,
2658                 rounding_amt,
2659                 match_status_flag,
2660                 encumbered_flag,
2661                 packet_id,
2662              -- ussgl_transaction_code, - Bug 4277744
2663              -- ussgl_trx_code_context, - Bug 4277744
2664                 reversal_flag,
2665                 parent_reversal_id,
2666                 cancellation_flag,
2667                 income_tax_region,
2668                 type_1099,
2669                 stat_amount,
2670 	        charge_applicable_to_dist_id,
2671                 prepay_amount_remaining,
2672                 prepay_distribution_id,
2673                 parent_invoice_id,
2674                 corrected_invoice_dist_id,
2675                 corrected_quantity,
2676                 other_invoice_id,
2677                 po_distribution_id,
2678                 rcv_transaction_id,
2679                 unit_price,
2680                 matched_uom_lookup_code,
2681                 quantity_invoiced,
2682                 final_match_flag,
2683                 related_id,
2684                 assets_addition_flag,
2685                 assets_tracking_flag,
2686                 asset_book_type_code,
2687                 asset_category_id,
2688                 project_id,
2689                 task_id,
2690                 expenditure_type,
2691                 expenditure_item_date,
2692                 expenditure_organization_id,
2693                 pa_quantity,
2694                 pa_addition_flag,
2695                 pa_cc_ar_invoice_id,
2696                 pa_cc_ar_invoice_line_num,
2697                 pa_cc_processed_code,
2698                 award_id,
2699                 gms_burdenable_raw_cost,
2700                 awt_flag,
2701                 awt_group_id,
2702                 awt_tax_rate_id,
2703                 awt_gross_amount,
2704                 awt_invoice_id,
2705                 awt_origin_group_id,
2706                 awt_invoice_payment_id,
2707                 awt_withheld_amt,
2708                 inventory_transfer_status,
2709                 reference_1,
2710                 reference_2,
2711                 receipt_verified_flag,
2712                 receipt_required_flag,
2713                 receipt_missing_flag,
2714                 justification,
2715                 expense_group,
2716                 start_expense_date,
2717                 end_expense_date,
2718                 receipt_currency_code,
2719                 receipt_conversion_rate,
2720                	receipt_currency_amount,
2721                	daily_amount,
2722                	web_parameter_id,
2723                 adjustment_reason,
2724                 merchant_document_number,
2725                 merchant_name,
2726                 merchant_reference,
2727                 merchant_tax_reg_number,
2728                 merchant_taxpayer_id,
2729                 country_of_supply,
2730                 credit_card_trx_id,
2731                 company_prepaid_invoice_id,
2732                 cc_reversal_flag,
2733                 attribute_category,
2734                 attribute1,
2735                 attribute2,
2736                 attribute3,
2737                 attribute4,
2738                 attribute5,
2739                 attribute6,
2740                 attribute7,
2741                 attribute8,
2742                 attribute9,
2743                 attribute10,
2744                 attribute11,
2745                 attribute12,
2746                 attribute13,
2747                	attribute14,
2748                	attribute15,
2749               	/*global_attribute_category,
2750               	global_attribute1,
2751                 global_attribute2, */
2752 		--bugfix:4674194
2753                 global_attribute3,
2754 		/*
2755                 global_attribute4,
2756                 global_attribute5,
2757                 global_attribute6,
2758                 global_attribute7,
2759                 global_attribute8,
2760                 global_attribute9,
2761                 global_attribute10,
2762                 global_attribute11,
2763                 global_attribute12,
2764                 global_attribute13,
2765                 global_attribute14,
2766                 global_attribute15,
2767                 global_attribute16,
2768                 global_attribute17,
2769                 global_attribute18,
2770                 global_attribute19,
2771                 global_attribute20,*/
2772                 created_by,
2773                 creation_date,
2774                 last_updated_by,
2775                 last_update_date,
2776                 last_update_login,
2777                 program_application_id,
2778                 program_id,
2779                 program_update_date,
2780                 request_id,
2781 		intended_use,
2782 		accrual_posted_flag,
2783 		cash_posted_flag,
2784 		--Freight and Special Charges
2785 	        rcv_charge_addition_flag,
2786 			pay_awt_group_id)   --bug 8222382
2787 	 SELECT g_batch_id,			 --batch_id
2788       	        x_invoice_id,			 --invoice_id
2789       	        x_invoice_line_number,		 --invoice_line_number
2790       	        x_dist_tab(i).invoice_distribution_id, --invoice_distribution_id
2791       	        l_distribution_line_number,	--distribution_line_number
2792       	        decode(g_invoice_type_lookup_code, 'PREPAYMENT', 'ITEM', -- bug 9081676: modify
2793                   decode (pd.accrue_on_receipt_flag,'Y',
2794       	        	'ACCRUAL','ITEM')),      --line_type_lookup_code
2795 	        --ail.item_description, 		--description
2796       	        nvl(ail.description,ail.item_description), 	--description /*Added NVL for bug 9780743 */
2797       	        'ITEM_TO_PO',			--dist_match_type
2798       		'PERMANENT',			--distribution_class
2799       		ail.org_id,			--org_id
2800                 x_dist_tab(i).dist_ccid,	--dist_code_combination_id
2801                 ail.accounting_date,		--accounting_date
2802       	        ail.period_name, 		--period_name
2803       		NULL,				--amount_to_post
2804       		NULL,				--base_amount_to_post
2805       		NULL,				--posted_amount
2806       		NULL,				--posted_base_amount
2807       		NULL,				--je_batch_id
2808       		NULL,				--cash_je_batch_id
2809       		'N',				--posted_flag
2810       		NULL,				--accounting_event_id
2811       		NULL,				--upgrade_posted_amt
2812       		NULL,				--upgrade_base_posted_amt
2813       		g_set_of_books_id,		--set_of_books_id
2814       		x_dist_tab(i).amount,		--amount
2815       		x_dist_tab(i).base_amount,	--base_amount
2816 		x_dist_tab(i).rounding_amt,	--rounding_amount
2817 		--bugfix:4959567
2818       		NULL,				--match_status_flag
2819       		'N',				--encumbered_flag
2820       		NULL,				--packet_id
2821              -- Removed for bug 4277744
2822              --	NVL(PD.ussgl_transaction_code,
2823              --	  ail.ussgl_transaction_code),	--ussgl_transaction_code
2824              --	NULL,				--ussgl_trx_code_context
2825       		'N',				--reversal_flag
2826       		NULL,				--parent_reversal_id
2827       		'N',				--cancellation_flag
2828       		DECODE(ail.type_1099,'','',
2829 			ail.income_tax_region),	--income_tax_region
2830       		ail.type_1099, 			--type_1099
2831       		NULL,				--stat_amount
2832       		NULL,				--charge_applicable_to_dist_id
2833       		NULL,				--prepay_amount_remaining
2834       		NULL,				--prepay_distribution_id
2835       		NULL,				--parent_invoice_id
2836       		NULL,				--corrected_invoice_dist_id
2837       		NULL,				--corrected_quantity
2838       		NULL,				--other_invoice_id
2839       		x_dist_tab(i).po_distribution_id,--po_distribution_id
2840       		NULL,			        --rcv_transaction_id
2841       		x_dist_tab(i).unit_price,	--unit_price
2842       		ail.unit_meas_lookup_code,      --matched_uom_lookup_code
2843       		x_dist_tab(i).quantity_invoiced,--quantity_invoiced
2844       		x_final_match_flag,		--final_match_flag
2845       		NULL,				--related_id
2846       		'U',				--assets_addition_flag
2847       		decode(gcc.account_type,'E',ail.assets_tracking_flag,'A','Y','N'),  --assets_tracking_flag
2848       		decode(decode(gcc.account_type,'E',ail.assets_tracking_flag,'A','Y','N'),
2849       			'Y',ail.asset_book_type_code,NULL), 		      --asset_book_type_code
2850       		decode(decode(gcc.account_type,'E',ail.assets_tracking_flag,'A','Y','N'),
2851       			 	 'Y',ail.asset_category_id,NULL),    	     --asset_category_id
2852       		x_dist_tab(i).project_id ,     		--project_id
2853       		x_dist_tab(i).task_id    ,     		--task_id
2854       		x_dist_tab(i).expenditure_type,		--expenditure_type
2855      	        x_dist_tab(i).expenditure_item_date,    --expenditure_item_date
2856       		x_dist_tab(i).expenditure_organization_id , --expenditure_organization_id
2857       	 	x_dist_tab(i).pa_quantity,	        --pa_quantity
2858       		decode(PD.project_id,NULL, 'E',
2859 		       decode(pd.destination_type_code,
2860 		              'INVENTORY','M','SHOP FLOOR','M','N')),   --pa_addition_flag
2861       		NULL,					--pa_cc_ar_invoice_id
2862       		NULL,					--pa_cc_ar_invoice_line_num
2863       		NULL,					--pa_cc_processed_code
2864       		NULL,					--award_id
2865       		NULL,					--gms_burdenable_raw_cost
2866       		NULL,					--awt_flag
2867 		x_dist_tab(i).awt_group_id,		--awt_group_id
2868       		NULL,					--awt_tax_rate_id
2869       		NULL,					--awt_gross_amount
2870       		NULL,					--awt_invoice_id
2871       		NULL,					--awt_origin_group_id
2872       		NULL,					--awt_invoice_payment_id
2873       		NULL,					--awt_withheld_amt
2874       		'N',					--inventory_transfer_status
2875       		NULL,					--reference_1
2876       		NULL,					--reference_2
2877       		NULL,					--receipt_verified_flag
2878       		NULL,					--receipt_required_flag
2879       		NULL,					--receipt_missing_flag
2880       		NULL,					--justification
2881       		NULL,					--expense_group
2882        		NULL,					--start_expense_date
2883       		NULL,					--end_expense_date
2884     		NULL,					--receipt_currency_code
2885       		NULL,					--receipt_conversion_rate
2886        		NULL,					--receipt_currency_amount
2887       		NULL,					--daily_amount
2888     		NULL,					--web_parameter_id
2889       		NULL,					--adjustment_reason
2890        		NULL,					--merchant_document_number
2891       		NULL,					--merchant_name
2892     		NULL,					--merchant_reference
2893       		NULL,					--merchant_tax_reg_number
2894        		NULL,					--merchant_taxpayer_id
2895       		NULL,					--country_of_supply
2896     		NULL,					--credit_card_trx_id
2897       		NULL,					--company_prepaid_invoice_id
2898        		NULL,					--cc_reversal_flag
2899        		-- Bug 6837035 Start
2900        		NVL(DECODE(g_transfer_flag,'Y',pd.attribute_category,DECODE(line_source,'IMPORTED',
2901 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute_category))), ''),--attribute_category
2902  	      	NVL(DECODE(g_transfer_flag,'Y',pd.attribute1,DECODE(line_source,'IMPORTED',
2903 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute1))), ''),	--attribute1
2904  	      	NVL(DECODE(g_transfer_flag,'Y',pd.attribute2,DECODE(line_source,'IMPORTED',
2905 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute2))), ''),	--attribute2
2906  	      	NVL(DECODE(g_transfer_flag,'Y',pd.attribute3,DECODE(line_source,'IMPORTED',
2907 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute3))), ''),	--attribute3
2908  	      	NVL(DECODE(g_transfer_flag,'Y',pd.attribute4,DECODE(line_source,'IMPORTED',
2909 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute4))), ''),	--attribute4
2910  	        NVL(DECODE(g_transfer_flag,'Y',pd.attribute5,DECODE(line_source,'IMPORTED',
2911 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute5))), ''),	--attribute5
2912  	        NVL(DECODE(g_transfer_flag,'Y',pd.attribute6,DECODE(line_source,'IMPORTED',
2913 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute6))), ''),	--attribute6
2914  	        NVL(DECODE(g_transfer_flag,'Y',pd.attribute7,DECODE(line_source,'IMPORTED',
2915 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute7))), ''),	--attribute7
2916  	        NVL(DECODE(g_transfer_flag,'Y',pd.attribute8,DECODE(line_source,'IMPORTED',
2917 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute8))), ''),	--attribute8
2918  	        NVL(DECODE(g_transfer_flag,'Y',pd.attribute9,DECODE(line_source,'IMPORTED',
2919 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute9))), ''),	--attribute9
2920  	        NVL(DECODE(g_transfer_flag,'Y',pd.attribute10,DECODE(line_source,'IMPORTED',
2921 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute10))), ''),	--attribute10
2922  	        NVL(DECODE(g_transfer_flag,'Y',pd.attribute11,DECODE(line_source,'IMPORTED',
2923 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute11))), ''),	--attribute11
2924  	        NVL(DECODE(g_transfer_flag,'Y',pd.attribute12,DECODE(line_source,'IMPORTED',
2925 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute12))), ''),	--attribute12
2926  	        NVL(DECODE(g_transfer_flag,'Y',pd.attribute13,DECODE(line_source,'IMPORTED',
2927 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute13))), ''),	--attribute13
2928  	        NVL(DECODE(g_transfer_flag,'Y',pd.attribute14,DECODE(line_source,'IMPORTED',
2929 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute14))), ''),	--attribute14
2930  	        NVL(DECODE(g_transfer_flag,'Y',pd.attribute15,DECODE(line_source,'IMPORTED',
2931 			   DECODE(l_copy_line_dff_flag,'Y',ail.attribute15))), ''),	--attribute15
2932        		-- Bug 6837035 End
2933   	        /* X_GLOBAL_ATTRIBUTE_CATEGORY,
2934 		X_GLOBAL_ATTRIBUTE1,
2935       		X_GLOBAL_ATTRIBUTE2, */
2936 		--Bugfix:4674194
2937 	        DECODE(AP_EXTENDED_WITHHOLDING_PKG.AP_EXTENDED_WITHHOLDING_OPTION,
2938 	               'Y',ail.ship_to_location_id, ''),
2939 		/*
2940       		X_GLOBAL_ATTRIBUTE4,
2941       		X_GLOBAL_ATTRIBUTE5,
2942       		X_GLOBAL_ATTRIBUTE6,
2943       		X_GLOBAL_ATTRIBUTE7,
2944        		X_GLOBAL_ATTRIBUTE8,
2945       		X_GLOBAL_ATTRIBUTE9,
2946        		X_GLOBAL_ATTRIBUTE10,
2947       		X_GLOBAL_ATTRIBUTE11,
2948       		X_GLOBAL_ATTRIBUTE12,
2949       		X_GLOBAL_ATTRIBUTE13,
2950       		X_GLOBAL_ATTRIBUTE14,
2951       		X_GLOBAL_ATTRIBUTE15,
2952       		X_GLOBAL_ATTRIBUTE16,
2953       		X_GLOBAL_ATTRIBUTE17,
2954       		X_GLOBAL_ATTRIBUTE18,
2955       		X_GLOBAL_ATTRIBUTE19,
2956       		X_GLOBAL_ATTRIBUTE20, */
2957       		ail.created_by,			--created_by
2958       		sysdate,			--creation_date
2959       		ail.last_updated_by,		--last_updated_by
2960       		sysdate,			--last_update_date
2961       		ail.last_update_login,		--last_update_login
2962       		NULL,				--program_application_id
2963       		NULL,				--program_id
2964       		NULL,				--program_update_date
2965       		NULL,				--request_id
2966 		--bugfix:5565310
2967 		G_intended_use,		        --intended_use
2968 		'N',			        --accrual_posted_flag
2969 		'N',				--cash_posted_flag
2970 		'N',				--rcv_charge_addition_flag
2971 		x_dist_tab(i).pay_awt_group_id		--pay_awt_group_id   bug8222382
2972       	  FROM  po_distributions pd,
2973       	        ap_invoice_lines ail,
2974       	        gl_code_combinations gcc
2975          WHERE ail.invoice_id = x_invoice_id
2976            AND ail.line_number = x_invoice_line_number
2977 	   AND ail.po_line_location_id = pd.line_location_id
2978   	   AND pd.po_distribution_id = x_dist_tab(i).po_distribution_id
2979   	   AND gcc.code_combination_id = decode(pd.accrue_on_receipt_flag, 'Y',    --Bug6014884
2980                                              pd.code_combination_id,x_dist_tab(i).dist_ccid);
2981 
2982 
2983 
2984          --Bugfix:4674635
2985 	 l_debug_info := 'Call the AP_EXTENDED_MATCH to populate global attributes';
2986 	 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2987             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2988          END IF;
2989 
2990          IF (AP_EXTENDED_WITHHOLDING_PKG.AP_EXTENDED_WITHHOLDING_ACTIVE) THEN
2991             AP_EXTENDED_WITHHOLDING_PKG.AP_EXTENDED_MATCH(
2992 				P_Credit_Id    => NULL,
2993 	                        P_Invoice_Id   => X_invoice_id,
2994 			        P_Inv_Line_Num => x_invoice_line_number,
2995 				P_Distribution_Id => x_dist_tab(i).invoice_distribution_id,
2996 				P_Parent_Dist_Id => NULL);
2997 
2998          END IF;
2999 
3000 
3001          GMS_AP_API.CREATE_AWARD_DISTRIBUTIONS
3002                                 ( p_invoice_id               => x_invoice_id,
3003                                   p_distribution_line_number => l_distribution_line_number,
3004                                   p_invoice_distribution_id  => x_dist_tab(i).invoice_distribution_id,
3005                                   p_award_id                 => x_dist_tab(i).award_id,
3006                                   p_mode                     => 'AP',
3007                                   p_dist_set_id              => NULL,
3008                                   p_dist_set_line_number     => NULL );
3009 
3010        l_distribution_line_number := l_distribution_line_number + 1;
3011 
3012     END IF;
3013 
3014  END LOOP;
3015 
3016  IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
3017    FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Insert_Invoice_Distributions(-)');
3018  END IF;
3019 
3020 
3021 EXCEPTION
3022  WHEN OTHERS THEN
3023 
3024    IF (SQLCODE <> -20001) THEN
3025      FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
3026      FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
3027      FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',current_calling_sequence);
3028      FND_MESSAGE.SET_TOKEN('PARAMETERS','Batch_Id = '||TO_CHAR(g_Batch_Id)
3029      	        ||', Invoice_id = '||TO_CHAR(X_invoice_id)
3030 		||', Invoice Line Number = '||X_Invoice_Line_Number
3031 		||', Dist_num = '||l_distribution_line_number
3032 		||', Allow_PA_Override = '||g_allow_pa_override
3033 		||', Transfer_Desc_Flag = '||g_Transfer_Flag);
3034      FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
3035    END IF;
3036    --Clean up the PL/SQL table
3037    X_DIST_TAB.DELETE;
3038 
3039    APP_EXCEPTION.RAISE_EXCEPTION;
3040 
3041 END Insert_Invoice_Distributions;
3042 
3043 
3044 Procedure Update_PO_Shipments_Dists(
3045 		    X_Dist_Tab	          IN OUT NOCOPY  Dist_Tab_Type,
3046 		    X_Po_Line_Location_Id IN 	         NUMBER,
3047 		    X_Match_Amount        IN   		 NUMBER,
3048 		    X_Match_Quantity	  IN		 NUMBER,
3049 		    X_Uom_Lookup_Code  	  IN  		 VARCHAR2,
3050   		    X_Calling_Sequence    IN		 VARCHAR2) IS
3051 
3052  current_calling_sequence	VARCHAR2(2000);
3053  l_debug_info			VARCHAR2(2000);
3054  i				NUMBER;
3055  l_po_ap_dist_rec		PO_AP_DIST_REC_TYPE;
3056  l_po_ap_line_loc_rec		PO_AP_LINE_LOC_REC_TYPE;
3057  l_api_name			VARCHAR2(50);
3058  l_return_status		VARCHAR2(100);
3059  l_msg_data			VARCHAR2(4000);
3060 BEGIN
3061 
3062    l_api_name := 'Update_PO_Shipments_Dists';
3063 
3064    current_calling_sequence := 'Update_Po_Shipments_Dists<-'||x_calling_sequence;
3065 
3066    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
3067          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Update_PO_Shipments_Dists(+)');
3068    END IF;
3069 
3070    l_debug_info := 'Create l_po_ap_dist_rec object';
3071    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3072         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3073    END IF;
3074 
3075    l_po_ap_dist_rec := PO_AP_DIST_REC_TYPE.create_object();
3076 
3077    l_debug_info := 'Create l_po_ap_line_loc_rec object and populate the data';
3078    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3079         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3080    END IF;
3081 
3082    IF (g_invoice_type_lookup_code <> 'PREPAYMENT') THEN
3083 
3084       l_po_ap_line_loc_rec := PO_AP_LINE_LOC_REC_TYPE.create_object(
3085 				 p_po_line_location_id => x_po_line_location_id,
3086 				 p_uom_code	       => x_uom_lookup_code,
3087 				 p_quantity_billed     => x_match_quantity,
3088 				 p_amount_billed       => x_match_amount,
3089 				 p_quantity_financed  => NULL,
3090  				 p_amount_financed    => NULL,
3091 				 p_quantity_recouped  => NULL,
3092 				 p_amount_recouped    => NULL,
3093 				 p_retainage_withheld_amt => NULL,
3094 				 p_retainage_released_amt => NULL
3095 				);
3096 
3097    ELSIF (g_invoice_type_lookup_code = 'PREPAYMENT') THEN
3098 
3099       l_po_ap_line_loc_rec := PO_AP_LINE_LOC_REC_TYPE.create_object(
3100 				 p_po_line_location_id => x_po_line_location_id,
3101 				 p_uom_code	       => x_uom_lookup_code,
3102 				 p_quantity_billed     => NULL,
3103 				 p_amount_billed       => NULL,
3104 				 p_quantity_financed   => x_match_quantity,
3105  				 p_amount_financed     => x_match_amount,
3106 				 p_quantity_recouped   => NULL,
3107 				 p_amount_recouped     => NULL,
3108 				 p_retainage_withheld_amt => NULL,
3109 				 p_retainage_released_amt => NULL
3110 				);
3111 
3112    END IF;
3113 
3114    l_debug_info := 'Populate the Po_Ap_Dist_Rec with the distribution information';
3115    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3116         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3117    END IF;
3118 
3119    FOR i in nvl(x_dist_tab.first,0)..nvl(x_dist_tab.last,0) LOOP
3120 
3121       IF (x_dist_tab.exists(i)) THEN
3122 
3123           IF (g_invoice_type_lookup_code <> 'PREPAYMENT') THEN
3124 
3125               l_po_ap_dist_rec.add_change(p_po_distribution_id => x_dist_tab(i).po_distribution_id,
3126     				p_uom_code	     => x_uom_lookup_code,
3127 				p_quantity_billed    => x_dist_tab(i).quantity_invoiced,
3128 				p_amount_billed	     => x_dist_tab(i).amount,
3129 				p_quantity_financed  => NULL,
3130 				p_amount_financed    => NULL,
3131 				p_quantity_recouped  => NULL,
3132 				p_amount_recouped    => NULL,
3133 				p_retainage_withheld_amt => NULL,
3134 				p_retainage_released_amt => NULL);
3135 
3136           ELSIF (g_invoice_type_lookup_code = 'PREPAYMENT') THEN
3137 
3138               l_po_ap_dist_rec.add_change(p_po_distribution_id => x_dist_tab(i).po_distribution_id,
3139     				p_uom_code	     => x_uom_lookup_code,
3140 				p_quantity_billed    => NULL,
3141 				p_amount_billed	     => NULL,
3142 				p_quantity_financed  => x_dist_tab(i).quantity_invoiced,
3143 				p_amount_financed    => x_dist_tab(i).amount,
3144 				p_quantity_recouped  => NULL,
3145 				p_amount_recouped    => NULL,
3146 				p_retainage_withheld_amt => NULL,
3147 				p_retainage_released_amt => NULL);
3148 
3149           END IF;
3150 
3151        END IF;
3152 
3153    END LOOP;
3154 
3155    l_debug_info := 'Call the PO_AP_INVOICE_MATCH_GRP to update the Po Distributions and Po Line Locations';
3156    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3157         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3158    END IF;
3159 
3160    PO_AP_INVOICE_MATCH_GRP.Update_Document_Ap_Values(
3161 					P_Api_Version => 1.0,
3162 					P_Line_Loc_Changes_Rec => l_po_ap_line_loc_rec,
3163 					P_Dist_Changes_Rec     => l_po_ap_dist_rec,
3164 					X_Return_Status	       => l_return_status,
3165 					X_Msg_Data	       => l_msg_data);
3166 
3167 
3168    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
3169          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Update_PO_Shipments_Dists(-)');
3170    END IF;
3171 
3172 EXCEPTION
3173  WHEN OTHERS THEN
3174    IF (SQLCODE <> -20001) THEN
3175      FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
3176      FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
3177      FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',current_calling_sequence);
3178      FND_MESSAGE.SET_TOKEN('PARAMETERS','PO Distribution Id = '||TO_CHAR(X_Dist_tab(i).po_distribution_id)
3179  			  ||', UOM= '||X_uom_lookup_code);
3180      FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
3181    END IF;
3182 
3183    --Clean up the PL/SQL table
3184    X_DIST_TAB.DELETE;
3185    APP_EXCEPTION.RAISE_EXCEPTION;
3186 
3187 END Update_PO_Shipments_Dists;
3188 
3189 
3190 Procedure Create_Charge_Lines(  X_Invoice_Id 	 	 IN 	NUMBER,
3191 				X_Freight_Cost_Factor_id IN     NUMBER,
3192     				X_Freight_Amount   	 IN 	NUMBER,
3193     				X_Freight_Description 	 IN	VARCHAR2,
3194 				X_Misc_Cost_Factor_id    IN     NUMBER,
3195     				X_Misc_Amount		 IN	NUMBER,
3196     				X_Misc_Description	 IN	VARCHAR2,
3197     				X_Item_Line_Number	 IN	NUMBER,
3198     				X_Calling_Sequence	 IN	VARCHAR2) IS
3199 
3200 l_debug_info	VARCHAR2(2000);
3201 current_calling_sequence	VARCHAR2(2000);
3202 l_api_name			VARCHAR2(50);
3203 
3204 BEGIN
3205 
3206 	l_api_name := 'Create_Charge_Lines';
3207 
3208  	current_calling_sequence := 'Create_Charge_Lines<-'||X_Calling_Sequence;
3209 
3210 	IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
3211 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Create_Charge_Lines(+)');
3212         END IF;
3213 
3214 	IF (X_Freight_Amount IS NOT NULL) THEN
3215 
3216 	    l_debug_info := 'Create Freight Line';
3217 	    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3218 	         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3219    	    END IF;
3220 
3221 	    Insert_Invoice_Line(
3222 	    		    X_Invoice_Id  	  => x_invoice_id,
3223 			    X_Invoice_Line_Number => g_max_invoice_line_number + 1,
3224 			    X_Line_Type_Lookup_Code => 'FREIGHT',
3225 			    X_Cost_Factor_id   	    => x_freight_cost_factor_id,
3226 			    X_Amount		    => x_freight_amount,
3227 			    X_Item_Line_Number	    => x_item_line_number,
3228 			    X_Charge_Line_Description => x_freight_description,
3229 			    X_Calling_Sequence	    => current_calling_sequence);
3230 
3231 
3232 	    l_debug_info := 'Create Allocation Rules for the freight line';
3233 	    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3234 	         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3235    	    END IF;
3236 
3237 	    AP_ALLOCATION_RULES_PKG.Insert_Percentage_Alloc_Rule(
3238 	              X_Invoice_id           => x_invoice_id,
3239 		      X_Chrg_Line_Number     => g_max_invoice_line_number,
3240 		      X_To_Line_Number       => x_item_line_number,
3241 		      X_Percentage           => 100,
3242 		      X_Calling_Sequence     => x_calling_sequence);
3243 
3244 
3245         END IF;
3246 
3247 
3248 	IF (X_Misc_Amount IS NOT NULL) THEN
3249 
3250 	   l_debug_info := 'Create Misc Line';
3251 	   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3252 	        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3253    	   END IF;
3254 
3255 	   Insert_Invoice_Line(
3256 	   		    X_Invoice_Id  	  => x_invoice_id,
3257 			    X_Invoice_Line_Number => g_max_invoice_line_number + 1,
3258 			    X_Line_Type_Lookup_Code => 'MISCELLANEOUS',
3259 			    X_Cost_Factor_id	    => x_misc_cost_factor_id,
3260 			    X_Amount		    => x_misc_amount,
3261 			    X_Item_Line_Number	    => x_item_line_number,
3262 			    X_Charge_Line_Description => x_misc_description,
3263 			    X_Calling_Sequence	    => current_calling_sequence);
3264 
3265 	   l_debug_info := 'Create Allocation Rules for the misc line';
3266 	   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3267 	        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3268 	   END IF;
3269 
3270 	   AP_ALLOCATION_RULES_PKG.Insert_Percentage_Alloc_Rule(
3271 	                    X_Invoice_id           => x_invoice_id,
3272 			    X_Chrg_Line_Number     => g_max_invoice_line_number,
3273 			    X_To_Line_Number       => x_item_line_number,
3274 			    X_Percentage           => 100,
3275 			    X_Calling_Sequence     => x_calling_sequence);
3276 
3277         END IF;
3278 
3279         IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
3280 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Create_Charge_Lines(-)');
3281         END IF;
3282 
3283 EXCEPTION
3284  WHEN OTHERS THEN
3285    IF (SQLCODE <> -20001) THEN
3286      FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
3287      FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
3288      FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',current_calling_sequence);
3289      FND_MESSAGE.SET_TOKEN('PARAMETERS','Invoice Id = '||to_char(X_Invoice_Id)
3290 			  ||', Freight Amount = '||to_char(x_freight_amount)
3291 			  ||', Freight Description = '||x_freight_description
3292 			  ||', Misc Amount = '||to_char(x_misc_amount)
3293 			  ||', Misc Description = '||x_misc_description
3294 			  ||', Item Line Number = '||TO_CHAR(X_Item_Line_Number)
3295 			  ||', Accounting Date = '||g_accounting_date
3296 			  ||', Period Name = '||g_period_name
3297 			  ||', Set of books id = '||to_char(g_set_of_books_id)
3298 			  ||', Exchange Rate = '||g_exchange_rate
3299 			  ||', Base Currency Code = '||g_base_currency_code
3300 			  ||', Income Tax Region = '||g_income_tax_region
3301 			  ||', Awt Group Id = '||g_awt_group_id
3302 			  ||', Transfer Flag = '||g_transfer_flag
3303 			  ||', Pay Awt Group Id = '||g_pay_awt_group_id);  --bug 8222382
3304      FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
3305    END IF;
3306    APP_EXCEPTION.RAISE_EXCEPTION;
3307 
3308 END Create_Charge_Lines;
3309 
3310 
3311 /*===========================================================================
3312 	PRICE AND QUANTITY CORRECTION OF INVOICE MATCHED TO PO
3313 ============================================================================*/
3314 
3315 
3316 PROCEDURE Price_Quantity_Correct_Inv_PO(
3317 		X_Invoice_Id		IN	NUMBER,
3318 		X_Invoice_Line_Number   IN 	NUMBER,
3319 		X_Corrected_Invoice_Id  IN	NUMBER,
3320 		X_Corrected_Line_Number IN	NUMBER,
3321 		X_Correction_Type	IN	VARCHAR2,
3322 		X_Match_Mode		IN	VARCHAR2,
3323 		X_Correction_Quantity	IN	NUMBER,
3324 		X_Correction_Amount	IN	NUMBER,
3325 		X_Correction_Price	IN	NUMBER,
3326 		X_Po_Line_Location_Id   IN	NUMBER,
3327 		X_Corr_Dist_Tab		IN OUT NOCOPY CORR_DIST_TAB_TYPE,
3328 		X_Final_Match_Flag	IN	VARCHAR2,
3329 		X_Uom_Lookup_Code	IN	VARCHAR2,
3330 		X_Retained_Amount	IN	NUMBER DEFAULT NULL,
3331 		X_Calling_Sequence	IN 	VARCHAR2) IS
3332 
3333 l_po_distribution_id     PO_DISTRIBUTIONS.PO_DISTRIBUTION_ID%TYPE := NULL;
3334 l_item_line_number	 AP_INVOICE_LINES_ALL.LINE_NUMBER%TYPE;
3335 l_line_amt_net_retainage        ap_invoice_lines_all.amount%TYPE;
3336 l_max_amount_to_recoup		ap_invoice_lines_all.amount%TYPE;
3337 l_amount_to_recoup	 	ap_invoice_lines_all.amount%TYPE;
3338 l_retained_amount		ap_invoice_lines_all.retained_amount%TYPE;
3339 l_success		 BOOLEAN;
3340 l_error_message		 VARCHAR2(4000);
3341 l_debug_info		 VARCHAR2(2000);
3342 current_calling_sequence VARCHAR2(2000);
3343 l_api_name		 VARCHAR2(50);
3344 
3345 BEGIN
3346 
3347    l_api_name := 'Price_Quantity_Correct_Inv_PO';
3348 
3349    current_calling_sequence := 'Price_Quantity_Correct_Inv_PO<-'||x_calling_sequence;
3350 
3351    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
3352          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Price_Quantity_Correct_Inv_PO(+)');
3353    END IF;
3354 
3355    l_debug_info := 'Calling Get_Info';
3356    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3357         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3358    END IF;
3359 
3360    Get_Info(x_invoice_id  => x_invoice_id,
3361 	    x_invoice_line_number => x_invoice_line_number,
3362 	    x_match_amount	  => x_correction_amount,
3363 	    x_po_line_location_id => x_po_line_location_id,
3364 	    x_calling_sequence => current_calling_sequence);
3365 
3366    IF g_invoice_type_lookup_code <> 'PREPAYMENT' THEN
3367       l_retained_amount := AP_INVOICE_LINES_UTILITY_PKG.Get_Retained_Amount
3368                                         (p_line_location_id => x_po_line_location_id,
3369                                          p_match_amount     => x_correction_amount);
3370    END IF;
3371 
3372    l_debug_info := 'Calling Get_Corr_Dist_Proration_Info';
3373    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3374         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3375    END IF;
3376 
3377    Get_Corr_Dist_Proration_Info(
3378             x_corrected_invoice_id  => x_corrected_invoice_id,
3379 	    x_corrected_line_number => x_corrected_line_number,
3380 	    x_corr_dist_tab 	    => x_corr_dist_tab,
3381 	    x_correction_type       => x_correction_type,
3382 	    x_correction_amount     => x_correction_amount,
3383 	    x_correction_quantity   => x_correction_quantity,
3384 	    x_correction_price      => x_correction_price,
3385 	    x_match_mode	    => x_match_mode,
3386 	    x_calling_sequence      => current_calling_sequence);
3387 
3388    IF (x_corr_dist_tab.COUNT = 1) THEN
3389 
3390       l_po_distribution_id := x_corr_dist_tab.FIRST;
3391 
3392    END IF;
3393 
3394    IF (x_invoice_line_number IS NULL) THEN
3395 
3396 	l_debug_info := 'Calling Insert_Corr_Invoice_Line';
3397 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3398 	       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3399 	END IF;
3400 
3401 	Insert_Corr_Invoice_Line(x_invoice_id	         => x_invoice_id,
3402 		 	         x_invoice_line_number   => g_max_invoice_line_number +1,
3403 		 	         x_corrected_invoice_id  => x_corrected_invoice_id,
3404 		 	         x_corrected_line_number => x_corrected_line_number,
3405 				 x_quantity	      => x_correction_quantity,
3406 				 x_amount	      => x_correction_amount,
3407 				 x_unit_price         => x_correction_price,
3408 				 x_correction_type    => x_correction_type,
3409 				 x_final_match_flag   => x_final_match_flag,
3410 				 x_po_distribution_id => l_po_distribution_id,
3411 				 x_retained_amount    => l_retained_amount,
3412 				 x_calling_sequence   => current_calling_sequence);
3413 
3414    END IF;
3415 
3416    l_item_line_number := g_max_invoice_line_number;
3417 
3418    l_debug_info := 'Calling Insert_Corr_Invoice_Dists';
3419    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3420        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3421    END IF;
3422 
3423    Insert_Corr_Invoice_Dists(x_invoice_id          => x_invoice_id,
3424 			     x_invoice_line_number => nvl(x_invoice_line_number,
3425 							  g_max_invoice_line_number),
3426 		             x_corrected_invoice_id=> x_corrected_invoice_id,
3427 			     x_corr_dist_tab	   => x_corr_dist_tab,
3428 			     x_correction_type	   => x_correction_type,
3429 			     x_final_match_flag	   => x_final_match_flag,
3430 			     x_total_amount	   => x_correction_amount,
3431 			     x_calling_sequence    => current_calling_sequence);
3432 
3433 
3434    IF(x_invoice_line_number IS NOT NULL) THEN
3435 
3436       l_debug_info := 'Updating Invoice Line Attributes after matching';
3437       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3438           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3439       END IF;
3440 
3441       UPDATE ap_invoice_lines ail
3442         SET (generate_dists ,
3443             attribute_category,
3444             attribute1,
3445             attribute2,
3446             attribute3,
3447             attribute4,
3448             attribute5,
3449             attribute6,
3450             attribute7,
3451             attribute8,
3452             attribute9,
3453             attribute10,
3454             attribute11,
3455             attribute12,
3456             attribute13,
3457             attribute14,
3458             attribute15,
3459             retained_amount,
3460             retained_amount_remaining)
3461             =
3462           (SELECT 'D',
3463                 /* Bug 7483260.  If the attribute field is populated in the
3464                  * interface, take that value. If the attribute field from
3465                  * the interface is null and the transfer_desc_flex_flag is
3466                  * Y, take the value from the purchase order.
3467                  */
3468                 nvl(ail.attribute_category, decode(g_transfer_flag, 'Y', pll.attribute_category, ail.attribute_category)),
3469                 nvl(ail.attribute1, decode(g_transfer_flag, 'Y', pll.attribute1, ail.attribute1)),
3470                 nvl(ail.attribute2, decode(g_transfer_flag, 'Y', pll.attribute2, ail.attribute2)),
3471                 nvl(ail.attribute3, decode(g_transfer_flag, 'Y', pll.attribute3, ail.attribute3)),
3472                 nvl(ail.attribute4, decode(g_transfer_flag, 'Y', pll.attribute4, ail.attribute4)),
3473                 nvl(ail.attribute5, decode(g_transfer_flag, 'Y', pll.attribute5, ail.attribute5)),
3474                 nvl(ail.attribute6, decode(g_transfer_flag, 'Y', pll.attribute6, ail.attribute6)),
3475                 nvl(ail.attribute7, decode(g_transfer_flag, 'Y', pll.attribute7, ail.attribute7)),
3476                 nvl(ail.attribute8, decode(g_transfer_flag, 'Y', pll.attribute8, ail.attribute8)),
3477                 nvl(ail.attribute9, decode(g_transfer_flag, 'Y', pll.attribute9, ail.attribute9)),
3478                 nvl(ail.attribute10, decode(g_transfer_flag, 'Y', pll.attribute10, ail.attribute10)),
3479                 nvl(ail.attribute11, decode(g_transfer_flag, 'Y', pll.attribute11, ail.attribute11)),
3480                 nvl(ail.attribute12, decode(g_transfer_flag, 'Y', pll.attribute12, ail.attribute12)),
3481                 nvl(ail.attribute13, decode(g_transfer_flag, 'Y', pll.attribute13, ail.attribute13)),
3482                 nvl(ail.attribute14, decode(g_transfer_flag, 'Y', pll.attribute14, ail.attribute14)),
3483                 nvl(ail.attribute15, decode(g_transfer_flag, 'Y', pll.attribute15, ail.attribute15)),
3484                 --end Bug 7483260
3485                 l_retained_amount,
3486                 -1 * l_retained_amount
3487            FROM ap_invoice_lines ail1,
3488                 po_line_locations pll
3489            WHERE ail1.invoice_id = x_invoice_id
3490            AND ail1.line_number =x_invoice_line_number
3491            AND pll.line_location_id = ail1.po_line_location_id)
3492         WHERE invoice_id = x_invoice_id
3493         AND line_number = x_invoice_line_number;
3494 
3495    END IF;
3496 
3497    l_debug_info := 'Create Retainage Distributions';
3498    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3499        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3500    END IF;
3501 
3502    Ap_Retainage_Pkg.Create_Retainage_Distributions
3503 			(x_invoice_id          => x_invoice_id,
3504                          x_invoice_line_number => nvl(x_invoice_line_number,l_item_line_number));
3505 
3506    IF (g_recoupment_rate is not null and x_correction_amount > 0
3507    		and g_invoice_type_lookup_code <> 'PREPAYMENT') THEN
3508 
3509       l_debug_info := 'Calculate the maximum amount that can be recouped from this invoice line';
3510       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3511 	       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3512       END IF;
3513 
3514       l_line_amt_net_retainage := x_correction_amount + nvl(l_retained_amount,0);
3515 
3516       l_max_amount_to_recoup := ap_utilities_pkg.ap_round_currency(
3517      				(x_correction_amount * g_recoupment_rate / 100) ,g_invoice_currency_code);
3518 
3519       IF (l_line_amt_net_retainage < l_max_amount_to_recoup) THEN
3520         l_amount_to_recoup := l_line_amt_net_retainage;
3521       ELSE
3522         l_amount_to_recoup := l_max_amount_to_recoup;
3523       END IF;
3524 
3525       l_debug_info := 'Automatically recoup any available prepayments against the same po line';
3526       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3527 	       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3528       END IF;
3529 
3530       l_success := AP_Matching_Utils_Pkg.Ap_Recoup_Invoice_Line(
3531                                 P_Invoice_Id           => x_invoice_id ,
3532                                 P_Invoice_Line_Number  => nvl(x_invoice_line_number,l_item_line_number) ,
3533                                 P_Amount_To_Recoup     => l_amount_to_recoup,
3534                                 P_Po_Line_Id           => g_po_line_id,
3535                                 P_Vendor_Id            => g_vendor_id,
3536                                 P_Vendor_Site_Id       => g_vendor_site_id,
3537                                 P_Accounting_Date      => g_accounting_date,
3538                                 P_Period_Name          => g_period_name,
3539                                 P_User_Id              => g_user_id,
3540                                 P_Last_Update_Login    => g_login_id ,
3541                                 P_Error_Message        => l_error_message,
3542                                 P_Calling_Sequence     => current_calling_sequence);
3543 
3544    END IF;
3545 
3546 
3547    l_debug_info := 'Calling Update_Corr_Po_Shipments_Dists';
3548    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3549         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3550    END IF;
3551    Update_Corr_Po_Shipments_Dists(x_corr_dist_tab    => x_corr_dist_tab,
3552 				x_po_line_location_id => x_po_line_location_id,
3553 				x_quantity	   => x_correction_quantity,
3554 				x_amount	   => x_correction_amount,
3555    				x_correction_type  => x_correction_type,
3556    				x_uom_lookup_code  => x_uom_lookup_code,
3557 				x_calling_sequence => current_calling_sequence);
3558 
3559    --Clean up the PL/SQL tables
3560    x_corr_dist_tab.delete;
3561 
3562    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
3563          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Price_Quantity_Correct_Inv_PO(-)');
3564    END IF;
3565 
3566 
3567 EXCEPTION
3568     WHEN others then
3569         If (SQLCODE <> -20001) Then
3570             fnd_message.set_name('SQLAP','AP_DEBUG');
3571             fnd_message.set_token('ERROR',SQLERRM);
3572             fnd_message.set_token('CALLING_SEQUENCE',current_calling_sequence);
3573             fnd_message.set_token('PARAMETERS',
3574                   ' invoice_id = '||to_char(x_invoice_id)
3575                 ||', invoice_line_number = ' ||to_char(x_invoice_line_number)
3576                 ||', corrected_invoice_id = '||to_char(x_corrected_invoice_id)
3577                 ||', corrected_line_number = '||to_char(x_corrected_line_number)
3578                 ||', correction_type = '||x_correction_type
3579 		||', match_mode = '||x_match_mode
3580                 ||', correction quantity = '||to_char(x_correction_quantity)
3581                 ||', correction amount = '||to_char(x_correction_amount)
3582                 ||', correction price = '||to_char(x_correction_price)
3583                 ||', final_match_flag = '||x_final_match_flag
3584                 ||', po_line_location_id = '||to_char(x_po_line_location_id));
3585             fnd_message.set_token('DEBUG_INFO',l_debug_info);
3586         End if;
3587 
3588 	--Clean up the PL/SQL tables
3589 	x_corr_dist_tab.delete;
3590 
3591         app_exception.raise_exception;
3592 
3593 END Price_Quantity_Correct_Inv_PO;
3594 
3595 
3596 
3597 PROCEDURE Get_Corr_Dist_Proration_Info(
3598 			x_corrected_invoice_id  IN    NUMBER,
3599 			x_corrected_line_number IN    NUMBER,
3600 			x_corr_dist_tab IN OUT NOCOPY CORR_DIST_TAB_TYPE,
3601 			x_correction_type     IN VARCHAR2,
3602 			x_correction_amount   IN NUMBER,
3603 			x_correction_quantity IN NUMBER,
3604 			x_correction_price    IN NUMBER,
3605 			x_match_mode	      IN VARCHAR2,
3606 			x_calling_sequence    IN VARCHAR2) IS
3607 
3608 --Modified cursor for bug#11886930
3609 -- CURSOR Price_Correction_Cursor(c_price_variance NUMBER) IS
3610   CURSOR Price_Correction_Cursor IS
3611   SELECT aid1.invoice_distribution_id corrected_inv_dist_id,
3612   	 aid1.po_distribution_id,
3613 	 decode(g_min_acct_unit,'',
3614   	        round(x_correction_amount * (aid1.amount+/*c_price_variance*/nvl(aid2.amount,0))/ail.amount,
3615 	              g_precision),
3616                 round((x_correction_amount * (aid1.amount+/*c_price_variance*/nvl(aid2.amount,0))/ail.amount)
3617 		      /g_min_acct_unit) * g_min_acct_unit
3618                 ) amount,
3619          round(x_correction_quantity * (aid1.amount+/*c_price_variance*/nvl(aid2.amount,0))/ail.amount,15) corrected_quantity,
3620 	 --bugfix:5606697
3621 	 DECODE(pd.destination_type_code,
3622 	         'EXPENSE', DECODE(pd.accrue_on_receipt_flag,
3623 	                           'Y', pd.code_combination_id,
3624 	                           aid1.dist_code_combination_id),
3625 	         pd.variance_account_id),
3626 	 ap_invoice_distributions_s.nextval
3627   FROM  ap_invoice_lines ail,
3628   	ap_invoice_distributions aid1,
3629 	po_distributions pd,
3630 	ap_invoice_distributions aid2
3631   WHERE ail.invoice_id = x_corrected_invoice_id
3632   AND ail.line_number = x_corrected_line_number
3633   AND aid1.invoice_id = ail.invoice_id
3634   AND aid1.po_distribution_id = pd.po_distribution_id /*bugfix:5606697*/
3635   AND aid1.invoice_line_number = ail.line_number
3636    -- Bug 5585744, Modified the condition below
3637   AND aid1.line_type_lookup_code IN ('ITEM', 'ACCRUAL')
3638   AND aid1.prepay_distribution_id IS NULL
3639   AND aid1.invoice_distribution_id = aid2.related_id(+)
3640   AND aid2.line_type_lookup_code(+) = 'IPV';
3641   /*AND aid.line_type_lookup_code NOT IN ('PREPAY','AWT','RETAINAGE')
3642   AND (aid.line_type_lookup_code NOT IN ('REC_TAX','NONREC_TAX')
3643        OR aid.prepay_distribution_id IS NULL);  */
3644 
3645 /*-----------------------------------------------------------
3646 Amount: Quantity_Invoiced at the line level prorated based on
3647 quantity_billed against the PO and multiplied by the unit_price
3648 at the line level.
3649 
3650 Corrected Quantity: Quantity_Invoiced selected prorated as done
3651 for the Amount.
3652 ------------------------------------------------------------*/
3653 CURSOR Quantity_Correction_Cursor(p_total_quantity_billed IN NUMBER) IS
3654   SELECT aid.invoice_distribution_id corrected_inv_dist_id,
3655   	 aid.po_distribution_id,
3656   	 decode(g_min_acct_unit,'',
3657 	        round((x_correction_quantity *
3658 			decode(pd.distribution_type,'PREPAYMENT',nvl(pd.quantity_financed,0),nvl(pd.quantity_billed,0))/p_total_quantity_billed
3659 		        ) * x_correction_price,
3660 		       g_precision),
3661                 round(((x_correction_quantity * decode(pd.distribution_type,'PREPAYMENT',nvl(pd.quantity_financed,0),nvl(pd.quantity_billed,0))/
3662 		 	 p_total_quantity_billed)*x_correction_price)
3663 			 /g_min_acct_unit) * g_min_acct_unit
3664                 ) amount,
3665          round((x_correction_quantity * decode(pd.distribution_type,'PREPAYMENT',nvl(pd.quantity_financed,0),nvl(pd.quantity_billed,0)))/
3666 	 	  p_total_quantity_billed , 15) corrected_quantity,
3667          aid.dist_code_combination_id,
3668 	 ap_invoice_distributions_s.nextval
3669   FROM ap_invoice_distributions aid,
3670        ap_invoice_lines ail,
3671        po_distributions pd
3672   WHERE ail.invoice_id = x_corrected_invoice_id
3673   AND ail.line_number = x_corrected_line_number
3674   AND aid.invoice_id = ail.invoice_id
3675   AND aid.invoice_line_number = ail.line_number
3676   AND pd.po_distribution_id = aid.po_distribution_id
3677    -- Bug 5585744, Modified the condition below
3678   AND aid.line_type_lookup_code IN ('ITEM', 'ACCRUAL')
3679   AND aid.prepay_distribution_id IS NULL;
3680   /*AND aid.line_type_lookup_code NOT IN ('PREPAY','AWT','RETAINAGE')
3681   AND (aid.line_type_lookup_code NOT IN ('REC_TAX','NONREC_TAX')
3682        OR aid.prepay_distribution_id IS NULL); */
3683 
3684 l_corrected_inv_dist_id   ap_invoice_distributions.corrected_invoice_dist_id%TYPE;
3685 l_amount                  ap_invoice_distributions.amount%TYPE;
3686 l_corrected_quantity      ap_invoice_distributions.corrected_quantity%TYPE;
3687 l_total_quantity_billed   number;
3688 l_base_amount	          ap_invoice_distributions.base_amount%TYPE;
3689 l_invoice_distribution_id ap_invoice_distributions.invoice_distribution_id%TYPE;
3690 l_max_dist_amount	  ap_invoice_distributions.amount%TYPE := 0;
3691 l_sum_prorated_amount     ap_invoice_distributions.amount%TYPE := 0;
3692 l_rounding_index	  ap_invoice_distributions.invoice_distribution_id%TYPE;
3693 l_sum_dist_base_amount    ap_invoice_distributions.base_amount%TYPE := 0;
3694 l_dist_ccid		  ap_invoice_distributions.dist_code_combination_id%TYPE;
3695 l_po_dist_id		  ap_invoice_distributions.po_distribution_id%TYPE;
3696 l_price_variance	  ap_invoice_distributions.amount%TYPE := 0;
3697 l_debug_info	          varchar2(2000);
3698 current_calling_sequence  varchar2(2000);
3699 l_api_name		  varchar2(50);
3700 
3701 BEGIN
3702 
3703    l_api_name := 'Get_Corr_Dist_Proration_Info';
3704 
3705    current_calling_sequence := 'Get_Corr_Dist_Proration_Info<-'||x_calling_sequence;
3706    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
3707          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Get_Corr_Dist_Proration_Info(+)');
3708    END IF;
3709 
3710 
3711 /* Bug 5176411, Changed the index for x_corr_dist_tab from l_po_dist_id to
3712    l_invoice_distrbution_id */
3713 
3714    IF (x_correction_type = 'PRICE_CORRECTION') THEN
3715 
3716      IF (x_match_mode IN ('STD-PS','CR-PS')) THEN
3717 
3718          --Commented for bug#11886930
3719 	 /* BEGIN
3720             SELECT nvl(sum(amount),0)  -- Bug 5629985. Added the NVL
3721               INTO l_price_variance
3722               FROM ap_invoice_distributions_all
3723              WHERE invoice_id            = x_corrected_invoice_id
3724                AND invoice_line_number   = x_corrected_line_number
3725                AND line_type_lookup_code = 'IPV';
3726         EXCEPTION
3727             WHEN OTHERS THEN NULL;
3728         END;
3729 
3730         OPEN price_correction_cursor(l_price_variance); */
3731 
3732         OPEN price_correction_cursor;
3733 
3734         LOOP
3735 
3736            FETCH price_correction_cursor INTO l_corrected_inv_dist_id,
3737 					   l_po_dist_id,
3738 					   l_amount,
3739 					   l_corrected_quantity,
3740 					   l_dist_ccid,
3741 					   l_invoice_distribution_id;
3742 
3743 	   EXIT WHEN price_correction_cursor%NOTFOUND;
3744 
3745 	   x_corr_dist_tab(l_invoice_distribution_id).po_distribution_id := l_po_dist_id;
3746 	   x_corr_dist_tab(l_invoice_distribution_id).invoice_distribution_id := l_invoice_distribution_id;
3747 	   x_corr_dist_tab(l_invoice_distribution_id).corrected_inv_dist_id := l_corrected_inv_dist_id;
3748 	   x_corr_dist_tab(l_invoice_distribution_id).amount := l_amount;
3749 	   x_corr_dist_tab(l_invoice_distribution_id).corrected_quantity := l_corrected_quantity;
3750 	   x_corr_dist_tab(l_invoice_distribution_id).unit_price := x_correction_price;
3751 	   x_corr_dist_tab(l_invoice_distribution_id).pa_quantity := l_corrected_quantity;
3752 	   x_corr_dist_tab(l_invoice_distribution_id).dist_ccid := l_dist_ccid;
3753 
3754 	   --Calculate the index of the max of the largest distribution for
3755 	   --proration/base amount rounding.
3756 	   --Added abs() for bug#14315066
3757 	   IF (abs(l_amount) >= abs(l_max_dist_amount)) THEN
3758 	      l_rounding_index := l_invoice_distribution_id;
3759 	      l_max_dist_amount := l_max_dist_amount;
3760 	   END IF;
3761 
3762 	   l_sum_prorated_amount := l_sum_prorated_amount + l_amount;
3763 
3764         END LOOP;
3765 
3766         CLOSE price_correction_cursor;
3767 
3768      END IF; /*x_match_mode in 'STD-PS','CR-PS' */
3769 
3770    ELSIF (x_correction_type = 'QTY_CORRECTION') THEN
3771 
3772      --For Shipment Level Match
3773      IF (x_match_mode IN ('STD-PS','CR-PS')) THEN
3774 
3775        SELECT sum(decode(pd.distribution_type,'PREPAYMENT',nvl(pd.quantity_financed,0),nvl(pd.quantity_billed,0)))
3776        INTO l_total_quantity_billed
3777        FROM po_distributions pd,
3778             ap_invoice_distributions aid
3779        WHERE pd.po_distribution_id = aid.po_distribution_id
3780        AND aid.invoice_id = x_corrected_invoice_id
3781        AND aid.invoice_line_number = x_corrected_line_number
3782         -- Bug 5585744, Modified the condition below
3783        AND aid.line_type_lookup_code IN ('ITEM', 'ACCRUAL')
3784        AND aid.prepay_distribution_id IS NULL;
3785       /*AND aid.line_type_lookup_code NOT IN ('PREPAY','AWT','RETAINAGE')
3786        AND (aid.line_type_lookup_code NOT IN ('REC_TAX','NONREC_TAX')
3787        OR aid.prepay_distribution_id IS NULL); */
3788 
3789        OPEN quantity_correction_cursor(l_total_quantity_billed);
3790 
3791        LOOP
3792 
3793 	 FETCH quantity_correction_cursor INTO l_corrected_inv_dist_id,
3794 	 				       l_po_dist_id,
3795 					       l_amount,
3796 					       l_corrected_quantity,
3797 					       l_dist_ccid,
3798 					       l_invoice_distribution_id;
3799 
3800 	 EXIT WHEN quantity_correction_cursor%NOTFOUND;
3801 
3802          x_corr_dist_tab(l_invoice_distribution_id).po_distribution_id := l_po_dist_id;
3803 	 x_corr_dist_tab(l_invoice_distribution_id).invoice_distribution_id := l_invoice_distribution_id;
3804          x_corr_dist_tab(l_invoice_distribution_id).corrected_inv_dist_id := l_corrected_inv_dist_id;
3805          x_corr_dist_tab(l_invoice_distribution_id).amount := l_amount;
3806          x_corr_dist_tab(l_invoice_distribution_id).corrected_quantity := l_corrected_quantity;
3807          x_corr_dist_tab(l_invoice_distribution_id).unit_price := x_correction_price;
3808          x_corr_dist_tab(l_invoice_distribution_id).pa_quantity := l_corrected_quantity;
3809 	 x_corr_dist_tab(l_invoice_distribution_id).dist_ccid := l_dist_ccid;
3810 
3811 	 --Calculate the index of the max of the largest distribution for
3812 	 --proration/base amount rounding.
3813 	 --Added abs()  for bug#14315066
3814 	 IF (abs(l_amount) >= abs(l_max_dist_amount)) THEN
3815             l_rounding_index := l_invoice_distribution_id;
3816             l_max_dist_amount := l_max_dist_amount;
3817          END IF;
3818 
3819          l_sum_prorated_amount := l_sum_prorated_amount + l_amount;
3820 
3821        END LOOP;
3822 
3823        CLOSE quantity_correction_cursor;
3824 
3825        --Perform Proration Rounding before the base amounts are populated
3826 
3827        IF (l_sum_prorated_amount <> x_correction_amount and l_rounding_index is not null) THEN
3828           x_corr_dist_tab(l_rounding_index).amount := x_corr_dist_tab(l_rounding_index).amount +
3829  						  (x_correction_amount - l_sum_prorated_amount);
3830        END IF;
3831 
3832      END IF; /*x_match_mode in 'STD-PS'... */
3833 
3834    END IF; /*x_correction_type ...*/
3835 
3836 
3837    --For the case when user distributes the correction, we still
3838    --need to populate the PL/SQL table with invoice_distribution_id...
3839    IF (x_match_mode IN ('STD-PD','CR-PD')) THEN
3840 
3841        FOR i IN nvl(x_corr_dist_tab.first,0) ..nvl(x_corr_dist_tab.last,0) LOOP
3842 
3843 	 IF (x_corr_dist_tab.exists(i)) THEN
3844 
3845  	   SELECT ap_invoice_distributions_s.nextval
3846            INTO	x_corr_dist_tab(i).invoice_distribution_id
3847            FROM DUAL;
3848 
3849 	   x_corr_dist_tab(i).pa_quantity := x_corr_dist_tab(i).corrected_quantity;
3850 
3851 	   --Calculate the index of the max of the largest distribution for
3852 	   --base amount rounding. For this case there will be no proration
3853 	   --rounding as the user distributes the correction quantity.
3854 
3855 	   --Also we will need this index only for foreign currency invoices only.
3856 
3857 	   IF (g_exchange_rate IS NOT NULL) THEN
3858 	    --Added abs()  for bug#14315066
3859 	      IF (abs(x_corr_dist_tab(i).amount) > abs(l_max_dist_amount)) THEN
3860 	         l_rounding_index := i;
3861 	         l_max_dist_amount := x_corr_dist_tab(i).amount;
3862 	      END IF;
3863            END IF;
3864 
3865          END IF;
3866 
3867        END LOOP;
3868 
3869    END IF; /*x_match_mode IN ('STD-PD','CR-PD' */
3870 
3871 
3872    FOR i in nvl(x_corr_dist_tab.first,0) .. nvl(x_corr_dist_tab.last,0) LOOP
3873 
3874      IF (x_corr_dist_tab.exists(i)) THEN
3875 
3876        --Populating the base_amount column, after proration related rounding
3877        --has been done if it is a foreign currency invoice.
3878 
3879        IF (g_exchange_rate IS NOT NULL) THEN
3880           x_corr_dist_tab(i).base_amount := ap_utilities_pkg.ap_round_currency(
3881                                                   x_corr_dist_tab(i).amount * g_exchange_rate,
3882 						  g_base_currency_code);
3883 
3884           l_sum_dist_base_amount := l_sum_dist_base_amount + x_corr_dist_tab(i).base_amount ;
3885        END IF;
3886 
3887      END IF;
3888 
3889    END LOOP;
3890 
3891    --Base Amount Rounding for foreign currency invoices only.
3892    --If it is a foreign currency invoice g_exchange_rate not be NULL
3893    IF (g_exchange_rate IS NOT NULL AND g_line_base_amount <> l_sum_dist_base_amount and l_rounding_index is not null) THEN
3894 
3895       x_corr_dist_tab(l_rounding_index).base_amount := x_corr_dist_tab(l_rounding_index).base_amount +
3896       						(g_line_base_amount - l_sum_dist_base_amount);
3897 
3898       x_corr_dist_tab(l_rounding_index).rounding_amt := g_line_base_amount - l_sum_dist_base_amount;
3899 
3900    END IF;
3901 
3902    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
3903          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Get_Corr_Dist_Proration_Info(-)');
3904    END IF;
3905 
3906 
3907 EXCEPTION
3908 WHEN others then
3909     If (SQLCODE <> -20001) Then
3910       fnd_message.set_name('SQLAP','AP_DEBUG');
3911       fnd_message.set_token('ERROR',SQLERRM);
3912       fnd_message.set_token('CALLING_SEQUENCE',current_calling_sequence);
3913       fnd_message.set_token('PARAMETERS',
3914            ' corrected_invoice_id = '||to_char(x_corrected_invoice_id)
3915 	   ||', corrected_line_number = '||to_char(x_corrected_line_number)
3916 	   ||', correction quantity ='||to_char(x_correction_quantity)
3917 	   ||', correction amount = '||to_char(x_correction_amount)
3918 	   ||', correction price = '||to_char(x_correction_price)
3919 	   ||', correction type = '||x_correction_type);
3920       fnd_message.set_token('DEBUG_INFO',l_debug_info);
3921     End if;
3922     --Clean up the PL/SQL tables on error
3923     x_corr_dist_tab.delete;
3924 
3925     app_exception.raise_exception;
3926 
3927 END Get_Corr_Dist_Proration_Info;
3928 
3929 
3930 
3931 PROCEDURE Update_Corr_Po_Shipments_Dists(
3932 		    X_Corr_Dist_Tab    IN  CORR_DIST_TAB_TYPE,
3933 		    X_Po_Line_Location_Id IN NUMBER,
3934     		    X_Quantity             IN  NUMBER,
3935 		    X_Amount               IN  NUMBER,
3936                     X_Correction_Type  IN  VARCHAR2,
3937 		    X_Uom_Lookup_Code  IN  VARCHAR2,
3938 		    X_Calling_Sequence IN  VARCHAR2) IS
3939 
3940 i			  NUMBER;
3941 l_po_ap_dist_rec          PO_AP_DIST_REC_TYPE;
3942 l_po_ap_line_loc_rec      PO_AP_LINE_LOC_REC_TYPE;
3943 l_debug_info              VARCHAR2(2000);
3944 current_calling_sequence  VARCHAR2(2000);
3945 l_api_name    		  VARCHAR2(50);
3946 l_return_status                VARCHAR2(100);
3947 l_msg_data                     VARCHAR2(4000);
3948 
3949 BEGIN
3950 
3951    l_api_name := 'Update_Corr_Po_Shipments_Dists';
3952 
3953    current_calling_sequence := 'Update_Corr_Po_Shipments_Dists<-'||x_calling_sequence;
3954 
3955    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
3956          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Update_Corr_Po_Distributions(+)');
3957    END IF;
3958 
3959    l_debug_info := 'Create l_po_ap_dist_rec object';
3960    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3961         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3962    END IF;
3963 
3964    l_po_ap_dist_rec := PO_AP_DIST_REC_TYPE.create_object();
3965 
3966    l_debug_info := 'Create l_po_ap_line_loc_rec object and populate the data';
3967    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3968         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3969    END IF;
3970 
3971    IF (x_correction_type = 'QTY_CORRECTION') THEN
3972 
3973       l_po_ap_line_loc_rec := PO_AP_LINE_LOC_REC_TYPE.create_object(
3974                                  p_po_line_location_id => x_po_line_location_id,
3975                                  p_uom_code            => x_uom_lookup_code,
3976                                  p_quantity_billed     => x_quantity,
3977                                  p_amount_billed       => X_Amount,  --Modified for bug#9718642 --NULL
3978                                  p_quantity_financed   => NULL,
3979                                  p_amount_financed     => NULL,
3980                                  p_quantity_recouped   => NULL,
3981                                  p_amount_recouped     => NULL,
3982                                  p_retainage_withheld_amt => NULL,
3983                                  p_retainage_released_amt => NULL
3984                                 );
3985 
3986    ELSIF (x_correction_type = 'PRICE_CORRECTION') THEN
3987 
3988       l_po_ap_line_loc_rec := PO_AP_LINE_LOC_REC_TYPE.create_object(
3989                                  p_po_line_location_id => x_po_line_location_id,
3990                                  p_uom_code            => x_uom_lookup_code,
3991                                  p_quantity_billed     => NULL,
3992                                  p_amount_billed       => X_Amount,  --Modified for bug#9718642 --NULL
3993                                  p_quantity_financed   => NULL,
3994                                  p_amount_financed     => NULL,
3995                                  p_quantity_recouped   => NULL,
3996                                  p_amount_recouped     => NULL,
3997                                  p_retainage_withheld_amt => NULL,
3998                                  p_retainage_released_amt => NULL
3999                                 );
4000 
4001    END IF;
4002 
4003 
4004    l_debug_info := 'Call PO api to update po_distributions and po_shipments table'
4005 		   ||' with quantity/amount billed information.';
4006 
4007    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4008         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4009    END IF;
4010 
4011    FOR i in nvl(x_corr_dist_tab.first,0)..nvl(x_corr_dist_tab.last,0) LOOP
4012 
4013      IF (x_corr_dist_tab.exists(i)) THEN
4014 
4015         IF (x_correction_type = 'PRICE_CORRECTION') THEN
4016 
4017 	   l_po_ap_dist_rec.add_change(p_po_distribution_id => x_corr_dist_tab(i).po_distribution_id,
4018                                 p_uom_code           => x_uom_lookup_code,
4019                                 p_quantity_billed    => NULL,
4020                                 p_amount_billed      => x_corr_dist_tab(i).amount,
4021                                 p_quantity_financed  => NULL,
4022                                 p_amount_financed    => NULL,
4023                                 p_quantity_recouped  => NULL,
4024                                 p_amount_recouped    => NULL,
4025                                 p_retainage_withheld_amt => NULL,
4026                                 p_retainage_released_amt => NULL);
4027 
4028         ELSIF (x_correction_type = 'QTY_CORRECTION') THEN
4029 
4030 	   l_po_ap_dist_rec.add_change(p_po_distribution_id => x_corr_dist_tab(i).po_distribution_id,
4031                                 p_uom_code           => x_uom_lookup_code,
4032                                 p_quantity_billed    => x_corr_dist_tab(i).corrected_quantity,
4033                                 p_amount_billed      => x_corr_dist_tab(i).amount,
4034                                 p_quantity_financed  => NULL,
4035                                 p_amount_financed    => NULL,
4036                                 p_quantity_recouped  => NULL,
4037                                 p_amount_recouped    => NULL,
4038                                 p_retainage_withheld_amt => NULL,
4039                                 p_retainage_released_amt => NULL);
4040 
4041         END IF;
4042 
4043      END IF;
4044 
4045    END LOOP;
4046 
4047    l_debug_info := 'Call the PO_AP_INVOICE_MATCH_GRP to update the Po Distributions and Po Line Locations';
4048    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4049         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4050    END IF;
4051 
4052    PO_AP_INVOICE_MATCH_GRP.Update_Document_Ap_Values(
4053                                         P_Api_Version => 1.0,
4054                                         P_Line_Loc_Changes_Rec => l_po_ap_line_loc_rec,
4055                                         P_Dist_Changes_Rec     => l_po_ap_dist_rec,
4056                                         X_Return_Status        => l_return_status,
4057                                         X_Msg_Data             => l_msg_data);
4058 
4059 
4060    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
4061          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Update_Corr_Po_Shipments_Dists(-)');
4062    END IF;
4063 
4064 
4065 EXCEPTION
4066 WHEN others then
4067     If (SQLCODE <> -20001) Then
4068         fnd_message.set_name('SQLAP','AP_DEBUG');
4069         fnd_message.set_token('ERROR',SQLERRM);
4070         fnd_message.set_token('CALLING_SEQUENCE',current_calling_sequence);
4071         fnd_message.set_token('PARAMETERS',
4072                   ' po_distribution_id = '||to_char(x_corr_dist_tab(i).po_distribution_id)
4073                   ||', correction_type = '||x_correction_type);
4074         fnd_message.set_token('DEBUG_INFO',l_debug_info);
4075     End if;
4076 
4077     app_exception.raise_exception;
4078 
4079 END Update_Corr_Po_Shipments_Dists;
4080 
4081 
4082 
4083 PROCEDURE Update_Corr_Po_Line_Locations(
4084 			x_po_line_location_id  IN NUMBER,
4085 			x_quantity	       IN NUMBER,
4086 			x_amount	       IN NUMBER,
4087 			x_correction_type      IN VARCHAR2,
4088 			x_uom_lookup_code      IN VARCHAR2,
4089 			x_calling_sequence     IN VARCHAR2) IS
4090 
4091 l_debug_info 	VARCHAR2(2000);
4092 current_calling_sequence	VARCHAR2(2000);
4093 l_api_name      VARCHAR2(50);
4094 
4095 BEGIN
4096 
4097    l_api_name := 'Update_Corr_Po_Line_Locations';
4098    current_calling_sequence := ' Update_Corr_Po_Line_Locations<-'||x_calling_sequence;
4099 
4100    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
4101          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Update_Corr_Po_Line_Locations(+)');
4102    END IF;
4103 
4104    l_debug_info := 'Call PO api to update the po_line_location with quantity/amount billed
4105    		    information';
4106 
4107    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4108         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4109    END IF;
4110 
4111    IF (x_correction_type = 'QTY_CORRECTION') THEN
4112 
4113       RCV_BILL_UPDATING_SV.ap_update_po_line_locations(
4114       		x_po_line_location_id	=> x_po_line_location_id,
4115       		x_quantity_billed => x_quantity,
4116       		x_uom_lookup_code => x_uom_lookup_code,
4117       		x_amount_billed   => x_amount,  --Modified for bug#9718642 --NULL,
4118       		x_matching_basis  => 'QUANTITY');
4119 
4120    END IF;
4121 
4122    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
4123          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Update_Corr_Po_Line_Locations(-)');
4124    END IF;
4125 
4126 
4127 EXCEPTION
4128 WHEN others then
4129     If (SQLCODE <> -20001) Then
4130         fnd_message.set_name('SQLAP','AP_DEBUG');
4131         fnd_message.set_token('ERROR',SQLERRM);
4132         fnd_message.set_token('CALLING_SEQUENCE',current_calling_sequence);
4133         fnd_message.set_token('PARAMETERS',
4134                     '  po_line_location_id = '||to_char(x_po_line_location_id)
4135                   ||', quantity = '|| to_char(x_quantity)
4136                   ||', amount = '|| to_char(x_amount)
4137                   ||', correction_type = '||x_correction_type);
4138         fnd_message.set_token('DEBUG_INFO',l_debug_info);
4139     End if;
4140     app_exception.raise_exception;
4141 
4142 END Update_Corr_Po_Line_Locations;
4143 
4144 
4145 PROCEDURE Insert_Corr_Invoice_Line(x_invoice_id            IN NUMBER,
4146 				   x_invoice_line_number   IN NUMBER,
4147 				   x_corrected_invoice_id  IN NUMBER,
4148 				   x_corrected_line_number IN NUMBER,
4149 				   x_quantity     	   IN NUMBER,
4150 				   x_amount       	   IN NUMBER,
4151 				   x_unit_price   	   IN NUMBER,
4152 				   x_correction_type	   IN VARCHAR2,
4153 				   x_final_match_flag	   IN VARCHAR2,
4154 				   x_po_distribution_id    IN NUMBER,
4155 				   x_retained_amount	   IN NUMBER DEFAULT NULL,
4156 				   x_calling_sequence      IN VARCHAR2 ) IS
4157 
4158 l_debug_info	VARCHAR2(2000);
4159 current_calling_sequence   VARCHAR2(2000);
4160 l_api_name	VARCHAR2(50);
4161 
4162 BEGIN
4163 
4164    l_api_name := 'Insert_Corr_Invoice_Line';
4165 
4166    current_calling_sequence := 'Insert_Corr_Invoice_Line<-'||x_calling_sequence;
4167 
4168    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
4169          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Insert_Corr_Invoice_Line(+)');
4170    END IF;
4171 
4172    INSERT INTO AP_INVOICE_LINES(INVOICE_ID,
4173 				LINE_NUMBER,
4174 				LINE_TYPE_LOOKUP_CODE,
4175 				REQUESTER_ID,
4176 				DESCRIPTION,
4177 				LINE_SOURCE,
4178 				ORG_ID,
4179 				INVENTORY_ITEM_ID,
4180 				ITEM_DESCRIPTION,
4181 				SERIAL_NUMBER,
4182 				MANUFACTURER,
4183 				MODEL_NUMBER,
4184 				GENERATE_DISTS,
4185 				MATCH_TYPE,
4186 				DISTRIBUTION_SET_ID,
4187 				ACCOUNT_SEGMENT,
4188 				BALANCING_SEGMENT,
4189 				COST_CENTER_SEGMENT,
4190 				OVERLAY_DIST_CODE_CONCAT,
4191 				DEFAULT_DIST_CCID,
4192 				PRORATE_ACROSS_ALL_ITEMS,
4193 				LINE_GROUP_NUMBER,
4194 				ACCOUNTING_DATE,
4195 				PERIOD_NAME,
4196 				DEFERRED_ACCTG_FLAG,
4197 				DEF_ACCTG_START_DATE,
4198 				DEF_ACCTG_END_DATE,
4199 				DEF_ACCTG_NUMBER_OF_PERIODS,
4200 				DEF_ACCTG_PERIOD_TYPE,
4201 				SET_OF_BOOKS_ID,
4202 				AMOUNT,
4203 				BASE_AMOUNT,
4204 				ROUNDING_AMT,
4205 				QUANTITY_INVOICED,
4206 				UNIT_MEAS_LOOKUP_CODE,
4207 				UNIT_PRICE,
4208 				WFAPPROVAL_STATUS,
4209 			     --	USSGL_TRANSACTION_CODE,- Bug 4277744
4210 				DISCARDED_FLAG,
4211 				ORIGINAL_AMOUNT,
4212 				ORIGINAL_BASE_AMOUNT,
4213 				ORIGINAL_ROUNDING_AMT,
4214 				CANCELLED_FLAG,
4215 				INCOME_TAX_REGION,
4216 				TYPE_1099,
4217 				STAT_AMOUNT,
4218 				PREPAY_INVOICE_ID,
4219 				PREPAY_LINE_NUMBER,
4220 				INVOICE_INCLUDES_PREPAY_FLAG,
4221 				CORRECTED_INV_ID,
4222 				CORRECTED_LINE_NUMBER,
4223 				PO_HEADER_ID,
4224 				PO_LINE_ID,
4225 				PO_RELEASE_ID,
4226 				PO_LINE_LOCATION_ID,
4227 				PO_DISTRIBUTION_ID,
4228 				RCV_TRANSACTION_ID,
4229 				FINAL_MATCH_FLAG,
4230 				ASSETS_TRACKING_FLAG,
4231 				ASSET_BOOK_TYPE_CODE,
4232 				ASSET_CATEGORY_ID,
4233 				PROJECT_ID,
4234 				TASK_ID,
4235 				EXPENDITURE_TYPE,
4236 				EXPENDITURE_ITEM_DATE,
4237 				EXPENDITURE_ORGANIZATION_ID,
4238  			        PA_QUANTITY,
4239 				PA_CC_AR_INVOICE_ID,
4240 				PA_CC_AR_INVOICE_LINE_NUM,
4241 				PA_CC_PROCESSED_CODE,
4242 				AWARD_ID,
4243   			        AWT_GROUP_ID,
4244 				REFERENCE_1,
4245 				REFERENCE_2,
4246 				RECEIPT_VERIFIED_FLAG,
4247 				RECEIPT_REQUIRED_FLAG,
4248 				RECEIPT_MISSING_FLAG,
4249 				JUSTIFICATION,
4250 				EXPENSE_GROUP,
4251 				START_EXPENSE_DATE,
4252 				END_EXPENSE_DATE,
4253 				RECEIPT_CURRENCY_CODE,
4254 				RECEIPT_CONVERSION_RATE,
4255 				RECEIPT_CURRENCY_AMOUNT,
4256 				DAILY_AMOUNT,
4257 				WEB_PARAMETER_ID,
4258 				ADJUSTMENT_REASON,
4259 				MERCHANT_DOCUMENT_NUMBER,
4260 				MERCHANT_NAME,
4261 				MERCHANT_REFERENCE,
4262 				MERCHANT_TAX_REG_NUMBER,
4263 				MERCHANT_TAXPAYER_ID,
4264 				COUNTRY_OF_SUPPLY,
4265 				CREDIT_CARD_TRX_ID,
4266 				COMPANY_PREPAID_INVOICE_ID,
4267 				CC_REVERSAL_FLAG,
4268 				ATTRIBUTE_CATEGORY,
4269 				ATTRIBUTE1,
4270       				ATTRIBUTE2,
4271       				ATTRIBUTE3,
4272       				ATTRIBUTE4,
4273       				ATTRIBUTE5,
4274       				ATTRIBUTE6,
4275       				ATTRIBUTE7,
4276       				ATTRIBUTE8,
4277       				ATTRIBUTE9,
4278       				ATTRIBUTE10,
4279       				ATTRIBUTE11,
4280       				ATTRIBUTE12,
4281       				ATTRIBUTE13,
4282       				ATTRIBUTE14,
4283       				ATTRIBUTE15,
4284    				/* OPEN ISSUE 1*/
4285      			        /* GLOBAL_ATTRIBUTE_CATEGORY,
4286 			        GLOBAL_ATTRIBUTE1,
4287       			        GLOBAL_ATTRIBUTE2,
4288       			        GLOBAL_ATTRIBUTE3,
4289       			        GLOBAL_ATTRIBUTE4,
4290       			        GLOBAL_ATTRIBUTE5,
4291       			        GLOBAL_ATTRIBUTE6,
4292       			        GLOBAL_ATTRIBUTE7,
4293        			        GLOBAL_ATTRIBUTE8,
4294       			        GLOBAL_ATTRIBUTE9,
4295        			        GLOBAL_ATTRIBUTE10,
4296       			        GLOBAL_ATTRIBUTE11,
4297       			        GLOBAL_ATTRIBUTE12,
4298       			        GLOBAL_ATTRIBUTE13,
4299       			        GLOBAL_ATTRIBUTE14,
4300       			        GLOBAL_ATTRIBUTE15,
4301       			        GLOBAL_ATTRIBUTE16,
4302       			        GLOBAL_ATTRIBUTE17,
4303       			        GLOBAL_ATTRIBUTE18,
4304       			        GLOBAL_ATTRIBUTE19,
4305       			        GLOBAL_ATTRIBUTE20, */
4306       			        CREATION_DATE,
4307       			        CREATED_BY,
4308       			        LAST_UPDATED_BY,
4309       			        LAST_UPDATE_DATE,
4310       			        LAST_UPDATE_LOGIN,
4311       			        PROGRAM_APPLICATION_ID,
4312       			        PROGRAM_ID,
4313       			        PROGRAM_UPDATE_DATE,
4314       			        REQUEST_ID,
4315 				RETAINED_AMOUNT,
4316 				RETAINED_AMOUNT_REMAINING,
4317 				--ETAX: Invwkb
4318 				SHIP_TO_LOCATION_ID,
4319 				PRIMARY_INTENDED_USE,
4320 				PRODUCT_FISC_CLASSIFICATION,
4321 				TRX_BUSINESS_CATEGORY,
4322 				PRODUCT_TYPE,
4323 				PRODUCT_CATEGORY,
4324 				USER_DEFINED_FISC_CLASS,
4325 				PAY_AWT_GROUP_ID
4326 				)
4327 			SELECT  x_invoice_id,			--invoice_id
4328 				x_invoice_line_number,		--line_number
4329 				'ITEM',				--line_type_lookup_code
4330 				ail.requester_id,		--requester_id
4331 				ail.description,		--description
4332 				'HEADER CORRECTION',		--line_source
4333 				ail.org_id,			--org_id
4334 				ail.inventory_item_id,		--inventory_item_id
4335 				ail.item_description,		--item_description
4336 				ail.serial_number,		--serial_number
4337 				ail.manufacturer,		--manufacturer
4338 				ail.model_number,		--model_number
4339 				'D',				--generate_dists
4340 				x_correction_type,		--match_type
4341 				NULL,				--distribution_set_id
4342 				ail.account_segment,		--account_segment
4343 				ail.balancing_segment,		--balancing_segment
4344 				ail.cost_center_segment,	--cost_center_segment
4345 				ail.overlay_dist_code_concat,	--overlay_dist_code_concat
4346 				ail.default_dist_ccid,		--default_dist_ccid
4347 				'N',				--prorate_across_all_items
4348 				NULL,				--line_group_number
4349 				g_accounting_date,		--accounting_date
4350 				g_period_name,			--period_name
4351 				'N',				--deferred_acctg_flag
4352 				NULL,                           --def_acctg_start_date
4353                                 NULL,                           --def_acctg_end_date
4354                                 NULL,                           --def_acctg_number_of_periods
4355                                 NULL,                           --def_acctg_period_type
4356                                 g_set_of_books_id,              --set_of_books_id
4357                                 x_amount,			--amount
4358                                 AP_UTILITIES_PKG.Ap_Round_Currency(
4359                                    NVL(X_AMOUNT, 0) * G_EXCHANGE_RATE,
4360                                         G_BASE_CURRENCY_CODE),  --base_amount
4361                                 NULL,                           --rounding_amount
4362                                 x_quantity,                     --quantity_invoiced
4363                                 ail.unit_meas_lookup_code,	--unit_meas_lookup_code
4364                                 x_unit_price,			--unit_price
4365                                 decode(g_approval_workflow_flag,'Y'
4366                                       ,'REQUIRED','NOT REQUIRED'),--wf_approval_status
4367                              -- Removed for bug 4277744
4368                              -- g_ussgl_transaction_code,	--ussgl_transaction_code
4369                                 'N',				--discarded_flag
4370                                 NULL,				--original_amount
4371                                 NULL,				--original_base_amount
4372                                 NULL,				--original_rounding_amt
4373                                 'N',				--cancelled_flag
4374                                 g_income_tax_region,		--income_tax_region
4375                                 pll.type_1099,			--type_1099
4376                                 NULL,                           --stat_amount
4377                                 NULL,                           --prepay_invoice_id
4378                                 NULL,                           --prepay_line_number
4379                                 NULL,                           --invoice_includes_prepay_flag
4380                                 x_corrected_invoice_id,		--corrected_invoice_id
4381                                 x_corrected_line_number,	--corrected_line_number
4382                                 ail.po_header_id,		--po_header_id
4383                                 ail.po_line_id,			--po_line_id
4384                                 ail.po_release_id,		--release_id
4385                                 ail.po_line_location_id,	--po_line_location_id
4386                                 nvl(ail.po_distribution_id,
4387                                     x_po_distribution_id),	--po_distribution_id
4388                                 NULL,				--rcv_transaction_id
4389                                 x_final_match_flag,		--final_match_flag
4390                                 ail.assets_tracking_flag,	--assets_tracking_flag
4391                                 ail.asset_book_type_code,	--asset_book_type_code
4392                                 ail.asset_category_id,		--asset_category_id
4393                                 ail.project_id,			--project_id
4394                                 ail.task_id,			--task_id
4395                                 ail.expenditure_type,		--expenditure_type
4396                                 ail.expenditure_item_date,	--expenditure_item_date
4397                                 ail.expenditure_organization_id, --expenditure_organization_id
4398                                 decode(ail.project_id,'','',
4399                                       decode(x_quantity,'',(ail.pa_quantity*x_amount/ail.amount),
4400                                       	     x_quantity)),      --pa_quantity
4401                                 NULL,				--pa_cc_ar_invoice_id
4402                                 NULL,				--pa_cc_ar_invoice_line_num
4403                                 NULL,				--pa_cc_processed_code
4404                                 ail.award_id,			--award_id
4405                                 g_awt_group_id,			--awt_group_id
4406                                 ail.reference_1,		--reference_1
4407                                 ail.reference_2,		--reference_2
4408                                 ail.receipt_verified_flag,	--receipt_verified_flag
4409                                 ail.receipt_required_flag,	--receipt_required_flag
4410                                 ail.receipt_missing_flag,	--receipt_missing_flag
4411                                 ail.justification,		--ail.justification
4412                                 ail.expense_group,		--ail.expense_group
4413                                 ail.start_expense_date,		--start_expense_date
4414                                 ail.end_expense_date,		--end_expense_date
4415                                 ail.receipt_currency_code,	--receipt_currency_code
4416                                 ail.receipt_conversion_rate,	--receipt_conversion_rate
4417                                 ail.receipt_currency_amount,	--receipt_currency_amount
4418                                 ail.daily_amount,		--daily_amount
4419                                 ail.web_parameter_id,		--web_parameter_id
4420                                 ail.adjustment_reason,		--adjustment_reason
4421                                 ail.merchant_document_number,	--merchant_document_number
4422                                 ail.merchant_name,		--merchant_name
4423                                 ail.merchant_reference,		--merchant_reference
4424                                 ail.merchant_tax_reg_number,	--merchant_tax_reg_number
4425                                 ail.merchant_taxpayer_id,	--merchant_taxpayer_id
4426                                 ail.country_of_supply,		--country_of_supply
4427                                 ail.credit_card_trx_id,		--credit_card_trx_id
4428                                 ail.company_prepaid_invoice_id, --cpmany_prepaid_invoice_id
4429                                 ail.cc_reversal_flag,		--cc_reversal_flag
4430                                 ail.attribute_category,		--attribute_category
4431  	      		    	ail.attribute1,			--attribute1
4432  	      		    	ail.attribute2,			--attribute2
4433  	      		    	ail.attribute3,			--attribute3
4434  	      		    	ail.attribute4,			--attribute4
4435  	      		    	ail.attribute5,			--attribute5
4436  	      		    	ail.attribute6,			--attribute6
4437  	      		    	ail.attribute7,			--attribute7
4438  	      		    	ail.attribute8,			--attribute8
4439  	      		    	ail.attribute9,			--attribute9
4440  	      		    	ail.attribute10,		--attribute10
4441  	      		    	ail.attribute11,		--attribute11
4442  	      		    	ail.attribute12,		--attribute12
4443  	      		    	ail.attribute13,		--attribute13
4444  	      		    	ail.attribute14,		--attribute14
4445  	      		        ail.attribute15,		--attribute15
4446  	      		        /*OPEN ISSUE 1*/
4447  	      		    	/* X_GLOBAL_ATTRIBUTE_CATEGORY,
4448 				X_GLOBAL_ATTRIBUTE1,
4449       				X_GLOBAL_ATTRIBUTE2,
4450 				X_GLOBAL_ATTRIBUTE3,
4451       				X_GLOBAL_ATTRIBUTE4,
4452       				X_GLOBAL_ATTRIBUTE5,
4453       				X_GLOBAL_ATTRIBUTE6,
4454       				X_GLOBAL_ATTRIBUTE7,
4455        				X_GLOBAL_ATTRIBUTE8,
4456       				X_GLOBAL_ATTRIBUTE9,
4457        				X_GLOBAL_ATTRIBUTE10,
4458       				X_GLOBAL_ATTRIBUTE11,
4459       				X_GLOBAL_ATTRIBUTE12,
4460       				X_GLOBAL_ATTRIBUTE13,
4461       				X_GLOBAL_ATTRIBUTE14,
4462       				X_GLOBAL_ATTRIBUTE15,
4463       				X_GLOBAL_ATTRIBUTE16,
4464       				X_GLOBAL_ATTRIBUTE17,
4465       				X_GLOBAL_ATTRIBUTE18,
4466       				X_GLOBAL_ATTRIBUTE19,
4467       				X_GLOBAL_ATTRIBUTE20, */
4468       				sysdate,			--creation_date
4469       				g_user_id,			--created_by
4470       				g_user_id,			--last_updated_by
4471       				sysdate,			--last_update_date
4472       				g_login_id,			--user_login_id
4473       				NULL,				--program_application_id
4474       				NULL,				--program_id
4475       				NULL,				--program_update_date
4476       				NULL,				--request_id
4477 				x_retained_amount,		--retained_amount
4478 				(-x_retained_amount),		--retained_amount_remaining
4479 			        --ETAX: Invwkb
4480 				PLL.SHIP_TO_LOCATION_ID,         --ship_to_location_id
4481 				AIL.PRIMARY_INTENDED_USE,        --primary_intended_use
4482                                 AIL.PRODUCT_FISC_CLASSIFICATION, --product_fisc_classification
4483 			        AIL.TRX_BUSINESS_CATEGORY,         --trx_business_category --bug10085965
4484 			        AIL.PRODUCT_TYPE,                --product_type
4485 			        AIL.PRODUCT_CATEGORY,            --product_category
4486 			        AIL.USER_DEFINED_FISC_CLASS,      --user_defined_fisc_class
4487 					g_pay_awt_group_id			--pay_awt_group_id   bug8222382
4488 			FROM  ap_invoices ai,
4489 			      ap_invoice_lines ail,
4490 			      po_line_locations_ap_v pll
4491 			WHERE ai.invoice_id = x_corrected_invoice_id
4492 			AND   ail.invoice_id = ai.invoice_id
4493 			AND   ail.line_number = x_corrected_line_number
4494 			AND   pll.line_location_id = ail.po_line_location_id;
4495 
4496     g_max_invoice_line_number := g_max_invoice_line_number + 1;
4497 
4498 
4499     IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
4500          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Insert_Corr_Invoice_Line(-)');
4501     END IF;
4502 
4503 
4504 EXCEPTION
4505     WHEN others then
4506         If (SQLCODE <> -20001) Then
4507             fnd_message.set_name('SQLAP','AP_DEBUG');
4508             fnd_message.set_token('ERROR',SQLERRM);
4509             fnd_message.set_token('CALLING_SEQUENCE',current_calling_sequence);
4510             fnd_message.set_token('PARAMETERS',
4511                   ' invoice_id = '||to_char(x_invoice_id)
4512                 ||', invoice_line_number = ' ||to_char(x_invoice_line_number)
4513                 ||', corrected_invoice_id = '||to_char(x_corrected_invoice_id)
4514                 ||', corrected_line_number = '||to_char(x_corrected_line_number)
4515                 ||', quantity = '||to_char(x_quantity)
4516                 ||', amount = '||to_char(x_amount)
4517                 ||', unit_price = '||to_char(x_unit_price)
4518                 ||', correction_type = '||x_correction_type
4519                 ||', final_match_flag = '||x_final_match_flag
4520                 ||', po_distribution_id = '||to_char(x_po_distribution_id));
4521             fnd_message.set_token('DEBUG_INFO',l_debug_info);
4522         End if;
4523         app_exception.raise_exception;
4524 
4525 
4526 END Insert_Corr_Invoice_Line;
4527 
4528 
4529 PROCEDURE Insert_Corr_Invoice_Dists(x_invoice_id          IN NUMBER,
4530 			     	    x_invoice_line_number IN NUMBER,
4531 				    x_corrected_invoice_id IN NUMBER,
4532 			     	    x_corr_dist_tab       IN OUT NOCOPY CORR_DIST_TAB_TYPE,
4533 			     	    x_correction_type     IN VARCHAR2,
4534 			     	    x_final_match_flag    IN VARCHAR2,
4535 			     	    x_total_amount     	  IN NUMBER,
4536 			     	    x_calling_sequence    IN VARCHAR2) IS
4537 
4538  i		NUMBER;
4539  l_distribution_line_number  ap_invoice_distributions.distribution_line_number%type := 1;
4540  l_debug_info  VARCHAR2(2000);
4541  current_calling_sequence VARCHAR2(2000);
4542  l_api_name    VARCHAR2(50);
4543 
4544 BEGIN
4545 
4546    l_api_name := 'Insert_Corr_Invoice_Dists';
4547 
4548    current_calling_sequence := 'Insert_Corr_Invoice_Dists<-'||x_calling_sequence;
4549 
4550    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
4551          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Insert_Corr_Invoice_Dists(+)');
4552    END IF;
4553 
4554    FOR i in NVL(x_corr_dist_tab.FIRST,0) .. NVL(x_corr_dist_tab.LAST,0) LOOP
4555 
4556      IF (x_corr_dist_tab.exists(i)) THEN
4557 
4558    	INSERT INTO AP_INVOICE_DISTRIBUTIONS(
4559    			batch_id,
4560                    	invoice_id,
4561                   	invoice_line_number,
4562                 	invoice_distribution_id,
4563                   	distribution_line_number,
4564                    	line_type_lookup_code,
4565                   	description,
4566                   	dist_match_type,
4567                   	distribution_class,
4568                   	org_id,
4569                   	dist_code_combination_id,
4570                   	accounting_date,
4571                		period_name,
4572                		amount_to_post,
4573                		base_amount_to_post,
4574                		posted_amount,
4575                		posted_base_amount,
4576                		je_batch_id,
4577                		cash_je_batch_id,
4578                		posted_flag,
4579                		accounting_event_id,
4580                		upgrade_posted_amt,
4581                  	upgrade_base_posted_amt,
4582                    	set_of_books_id,
4583                		amount,
4584                		base_amount,
4585                		rounding_amt,
4586                		match_status_flag,
4587                   	encumbered_flag,
4588                   	packet_id,
4589                      --	ussgl_transaction_code, - Bug 4277744
4590                      --	ussgl_trx_code_context, - Bug 4277744
4591                  	reversal_flag,
4592                   	parent_reversal_id,
4593                  	cancellation_flag,
4594                  	income_tax_region,
4595                  	type_1099,
4596                   	stat_amount,
4597 			charge_applicable_to_dist_id,
4598                    	prepay_amount_remaining,
4599                  	prepay_distribution_id,
4600                  	parent_invoice_id,
4601                  	corrected_invoice_dist_id,
4602                   	corrected_quantity,
4603                   	other_invoice_id,
4604                   	po_distribution_id,
4605                  	rcv_transaction_id,
4606                  	unit_price,
4607                  	matched_uom_lookup_code,
4608                  	quantity_invoiced,
4609                  	final_match_flag,
4610                   	related_id,
4611                   	assets_addition_flag,
4612                   	assets_tracking_flag,
4613                   	asset_book_type_code,
4614                   	asset_category_id,
4615                   	project_id,
4616                   	task_id,
4617                   	expenditure_type,
4618                   	expenditure_item_date,
4619                   	expenditure_organization_id,
4620                   	pa_quantity,
4621                   	pa_addition_flag,
4622                   	pa_cc_ar_invoice_id,
4623                   	pa_cc_ar_invoice_line_num,
4624                   	pa_cc_processed_code,
4625                   	award_id,
4626                    	gms_burdenable_raw_cost,
4627                   	awt_flag,
4628                   	awt_group_id,
4629                    	awt_tax_rate_id,
4630                    	awt_gross_amount,
4631                    	awt_invoice_id,
4632                    	awt_origin_group_id,
4633                   	awt_invoice_payment_id,
4634                     	awt_withheld_amt,
4635                     	inventory_transfer_status,
4636                    	reference_1,
4637                   	reference_2,
4638                  	receipt_verified_flag,
4639                   	receipt_required_flag,
4640                  	receipt_missing_flag,
4641                 	justification,
4642                 	expense_group,
4643                    	start_expense_date,
4644                  	end_expense_date,
4645                 	receipt_currency_code,
4646                 	receipt_conversion_rate,
4647                		receipt_currency_amount,
4648                		daily_amount,
4649                		web_parameter_id,
4650                    	adjustment_reason,
4651                    	merchant_document_number,
4652                    	merchant_name,
4653                    	merchant_reference,
4654                    	merchant_tax_reg_number,
4655                    	merchant_taxpayer_id,
4656                   	country_of_supply,
4657                  	credit_card_trx_id,
4658                  	company_prepaid_invoice_id,
4659                  	cc_reversal_flag,
4660                  	attribute_category,
4661                  	attribute1,
4662                  	attribute2,
4663                  	attribute3,
4664                  	attribute4,
4665                  	attribute5,
4666                  	attribute6,
4667                 	attribute7,
4668                 	attribute8,
4669                 	attribute9,
4670                         attribute10,
4671                         attribute11,
4672                         attribute12,
4673                         attribute13,
4674                	        attribute14,
4675                	        attribute15,
4676                	        /*OPEN ISSUE 1*/
4677               	        /*global_attribute_category,
4678               	         global_attribute1,
4679                          global_attribute2,*/
4680 			 --bugfix:4674194
4681                          global_attribute3,
4682                          /*global_attribute4,
4683                          global_attribute5,
4684                          global_attribute6,
4685                          global_attribute7,
4686                          global_attribute8,
4687                          global_attribute9,
4688                          global_attribute10,
4689                          global_attribute11,
4690                          global_attribute12,
4691                          global_attribute13,
4692                          global_attribute14,
4693                          global_attribute15,
4694                          global_attribute16,
4695                          global_attribute17,
4696                          global_attribute18,
4697                          global_attribute19,
4698                          global_attribute20,*/
4699                          created_by,
4700                          creation_date,
4701                          last_updated_by,
4702                          last_update_date,
4703                          last_update_login,
4704                          program_application_id,
4705                          program_id,
4706                          program_update_date,
4707                          request_id,
4708 			 --ETAX:Invwkb
4709 			 intended_use ,
4710 			 accrual_posted_flag,
4711 			 cash_posted_flag,
4712 			 --Freight and Special Charges
4713 			 rcv_charge_addition_flag,
4714 			 pay_awt_group_id
4715 			 --bug8222382
4716 			 )
4717       		  SELECT g_batch_id,		        --batch_id
4718       		         x_invoice_id,	                --invoice_id
4719       		 	 x_invoice_line_number,	        --invoice_line_number
4720       			 x_corr_dist_tab(i).invoice_distribution_id, --invoice_distribution_id
4721       			 l_distribution_line_number,    --distribution_line_number
4722       			 decode(x_correction_type,
4723       			    'PRICE_CORRECTION','IPV',
4724       			    'QTY_CORRECTION',aid.line_type_lookup_code), --line_type_lookup_code
4725       			 ail.description, 	        --description
4726       			 x_correction_type,	        --dist_match_type
4727       			 'PERMANENT',		        --distribution_class
4728       			 ail.org_id,		        --org_id
4729       			 x_corr_dist_tab(i).dist_ccid,  --dist_code_combination_id
4730                          ail.accounting_date,		--accounting_date
4731       			 ail.period_name, 		--period_name
4732       			 NULL,				--amount_to_post
4733       			 NULL,				--base_amount_to_post
4734       			 NULL,				--posted_amount
4735       			 NULL,				--posted_base_amount
4736       			 NULL,				--je_batch_id
4737       			 NULL,				--cash_je_batch_id
4738       			 'N',				--posted_flag
4739       			 NULL,				--accounting_event_id
4740       			 NULL,				--upgrade_posted_amt
4741       			 NULL,				--upgrade_base_posted_amt
4742       		         g_set_of_books_id,		--set_of_books_id
4743       			 x_corr_dist_tab(i).amount,	--amount
4744       			 x_corr_dist_tab(i).base_amount,--base_amount
4745 			 x_corr_dist_tab(i).rounding_amt,--rounding_amount
4746       			 NULL,				--match_status_flag
4747       			 'N',				--encumbered_flag
4748       			 NULL,				--packet_id
4749                       -- Removed for bug 4277744
4750       		      -- ail.ussgl_transaction_code,	--ussgl_transaction_code
4751       		      -- NULL,				--ussgl_trx_code_context
4752       			 'N',				--reversal_flag
4753       			 NULL,				--parent_reversal_id
4754       			 'N',				--cancellation_flag
4755       			 DECODE(ail.type_1099,'','',ail.income_tax_region), --income_tax_region
4756       			 ail.type_1099, 		--type_1099
4757       			 NULL,				--stat_amount
4758       			 NULL,				--charge_applicable_to_dist_id
4759       			 NULL,				--prepay_amount_remaining
4760       			 NULL,				--prepay_distribution_id
4761       			 ail.corrected_inv_id,		--parent_invoice_id
4762       			 x_corr_dist_tab(i).corrected_inv_dist_id, --corrected_invoice_dist_id
4763       			 x_corr_dist_tab(i).corrected_quantity,  --corrected_quantity
4764       			 NULL,				        --other_invoice_id
4765       			 x_corr_dist_tab(i).po_distribution_id,	--po_distribution_id
4766       			 NULL,					--rcv_transaction_id
4767       			 x_corr_dist_tab(i).unit_price,		--unit_price
4768       			 aid.matched_uom_lookup_code,		--matched_uom_lookup_code
4769 			 NULL,					--quantity_invoiced
4770       			 x_final_match_flag,			--final_match_flag
4771       			 NULL,					--related_id
4772       			 'U',					--assets_addition_flag
4773       			 aid.assets_tracking_flag,		--assets_tracking_flag
4774       			 decode(aid.assets_tracking_flag,'Y',
4775       			 	ail.asset_book_type_code,NULL), --asset_book_type_code
4776       			 decode(aid.assets_tracking_flag,'Y',
4777       			 	ail.asset_category_id,NULL),    --asset_category_id
4778       			 aid.project_id, 		        --project_id
4779 			 aid.task_id,			        --task_id
4780       			 aid.expenditure_type,		        --expenditure_type
4781       			 aid.expenditure_item_date,	        --expenditure_item_date
4782   	 	         aid.expenditure_organization_id,       --expenditure_organization_id
4783       			 decode(aid.project_id,'','',
4784 			   x_corr_dist_tab(i).pa_quantity),     --pa_quantity
4785       			 decode(aid.project_id,NULL,'E',
4786 			        decode(pd.destination_type_code,'SHOP FLOOR','M',
4787 					'INVENTORY','M','N')),   --pa_addition_flag
4788       			 NULL,				        --pa_cc_ar_invoice_id
4789       			 NULL,				        --pa_cc_ar_invoice_line_num
4790       			 NULL,				        --pa_cc_processed_code
4791       			 aid.award_id, 	  		        --award_id
4792       			 NULL,				        --gms_burdenable_raw_cost
4793       			 NULL,				        --awt_flag
4794       			 decode(g_system_allow_awt_flag,'Y',
4795       			        decode(g_site_allow_awt_flag,'Y',
4796 				       ail.awt_group_id,NULL),
4797       			        NULL), 			        --awt_group_id
4798       			 NULL,  			        --awt_tax_rate_id
4799       			 NULL,					--awt_gross_amount
4800       			 NULL,					--awt_invoice_id
4801       			 NULL,					--awt_origin_group_id
4802       			 NULL,					--awt_invoice_payment_id
4803       			 NULL,					--awt_withheld_amt
4804       			 'N',					--inventory_transfer_status
4805       			 ail.reference_1,			--reference_1
4806       			 ail.reference_2,			--reference_2
4807      			 ail.receipt_verified_flag,		--receipt_verified_flag
4808       		         ail.receipt_required_flag,		--receipt_required_flag
4809       			 ail.receipt_missing_flag,		--receipt_missing_flag
4810       			 ail.justification,			--justification
4811       			 ail.expense_group,			--expense_group
4812        			 ail.start_expense_date,		--start_expense_date
4813       			 ail.end_expense_date,			--end_expense_date
4814     			 ail.receipt_currency_code,		--receipt_currency_code
4815       			 ail.receipt_conversion_rate,		--receipt_conversion_rate
4816        			 ail.receipt_currency_amount,		--receipt_currency_amount
4817       			 ail.daily_amount,			--daily_amount
4818     			 ail.web_parameter_id,			--web_parameter_id
4819       			 ail.adjustment_reason,			--adjustment_reason
4820 			 ail.merchant_document_number,		--merchant_document_number
4821       			 ail.merchant_name,			--merchant_name
4822     			 ail.merchant_reference,		--merchant_reference
4823       			 ail.merchant_tax_reg_number,		--merchant_tax_reg_number
4824        			 ail.merchant_taxpayer_id, 	        --merchant_taxpayer_id
4825       			 ail.country_of_supply,			--country_of_supply
4826     			 ail.credit_card_trx_id,		--credit_card_trx_id
4827       			 ail.company_prepaid_invoice_id,	--company_prepaid_invoice_id
4828        			 ail.cc_reversal_flag,			--cc_reversal_flag
4829        			 aid.attribute_category,		--attribute_category
4830  	      		 aid.attribute1,			--attribute1
4831  	      		 aid.attribute2,			--attribute2
4832  	      		 aid.attribute3,			--attribute3
4833  	      		 aid.attribute4,			--attribute4
4834  	      		 aid.attribute5,			--attribute5
4835  	      		 aid.attribute6,			--attribute6
4836  	      		 aid.attribute7,			--attribute7
4837  	      		 aid.attribute8,			--attribute8
4838  	      		 aid.attribute9,			--attribute9
4839  	      		 aid.attribute10,			--attribute10
4840  	      		 aid.attribute11,			--attribute11
4841  	      		 aid.attribute12,			--attribute12
4842  	      		 aid.attribute13,			--attribute13
4843  	      		 aid.attribute14,			--attribute14
4844  	      		 aid.attribute15,			--attribute15
4845  	      		 /* X_GLOBAL_ATTRIBUTE_CATEGORY,
4846 			 X_GLOBAL_ATTRIBUTE1,
4847       			 X_GLOBAL_ATTRIBUTE2,*/
4848 			 --bugfix:4674194
4849 			 decode(ap_extended_withholding_pkg.ap_extended_withholding_option,
4850 			        'Y',ail.ship_to_location_id,''),
4851       			 /*X_GLOBAL_ATTRIBUTE4,
4852       			 X_GLOBAL_ATTRIBUTE5,
4853       			 X_GLOBAL_ATTRIBUTE6,
4854       			 X_GLOBAL_ATTRIBUTE7,
4855        			 X_GLOBAL_ATTRIBUTE8,
4856       			 X_GLOBAL_ATTRIBUTE9,
4857        			 X_GLOBAL_ATTRIBUTE10,
4858       			 X_GLOBAL_ATTRIBUTE11,
4859       			 X_GLOBAL_ATTRIBUTE12,
4860       			 X_GLOBAL_ATTRIBUTE13,
4861       			 X_GLOBAL_ATTRIBUTE14,
4862       			 X_GLOBAL_ATTRIBUTE15,
4863       			 X_GLOBAL_ATTRIBUTE16,
4864       			 X_GLOBAL_ATTRIBUTE17,
4865       			 X_GLOBAL_ATTRIBUTE18,
4866       			 X_GLOBAL_ATTRIBUTE19,
4867       			 X_GLOBAL_ATTRIBUTE20, */
4868       			 ail.created_by,		--created_by
4869       			 sysdate,			--creation_date
4870       			 ail.last_updated_by,		--last_updated_by
4871       			 sysdate,			--last_update_date
4872       			 ail.last_update_login,		--last_update_login
4873       			 NULL,				--program_application_id
4874       			 NULL,				--program_id
4875       			 NULL,				--program_update_date
4876       			 NULL,				--request_id
4877 			 --ETAX: Invwkb
4878 			 aid.intended_use,	        --intended_use
4879 			 'N',				--accrual_posted_flag
4880 			 'N',				--cash_posted_flag
4881 			 'N',				--rcv_charge_addition_flag
4882 			 decode(g_system_allow_awt_flag,'Y',
4883       			        decode(g_site_allow_awt_flag,'Y',
4884 				       ail.pay_awt_group_id,NULL),
4885       			        NULL) 			        --pay_awt_group_id    bug8222382
4886    		    FROM ap_invoice_lines ail,
4887    	     		 ap_invoice_distributions aid,
4888 			 po_distributions pd
4889    		    WHERE ail.invoice_id = x_invoice_id
4890    	  	    AND ail.line_number = x_invoice_line_number
4891    	  	    AND aid.invoice_id = ail.corrected_inv_id
4892    	  	    AND aid.invoice_line_number = ail.corrected_line_number
4893    	  	    AND aid.invoice_distribution_id = x_corr_dist_tab(i).corrected_inv_dist_id
4894 		    AND aid.po_distribution_id = pd.po_distribution_id;
4895 
4896         --Bugfix:4674635
4897         l_debug_info := 'Call the AP_EXTENDED_MATCH to populate global attributes';
4898         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4899            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4900         END IF;
4901 
4902         IF (AP_EXTENDED_WITHHOLDING_PKG.AP_EXTENDED_WITHHOLDING_ACTIVE) THEN
4903             AP_EXTENDED_WITHHOLDING_PKG.AP_EXTENDED_MATCH(
4904                                 P_Credit_Id    => X_invoice_id,
4905                                 P_Invoice_Id   => X_corrected_invoice_id,
4906                                 P_Inv_Line_Num => X_invoice_line_number,
4907 				P_Distribution_id => x_corr_dist_tab(i).invoice_distribution_id,
4908 				P_Parent_Dist_Id  => x_corr_dist_tab(i).corrected_inv_dist_id);
4909 
4910         END IF;
4911 
4912         l_distribution_line_number := l_distribution_line_number + 1;
4913 
4914      END IF; /*(x_corr_dist_tab.exists(i))  */
4915 
4916    END LOOP;
4917 
4918    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
4919          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Insert_Corr_Invoice_Dists(-)');
4920    END IF;
4921 
4922 
4923 EXCEPTION
4924     WHEN others then
4925         If (SQLCODE <> -20001) Then
4926             fnd_message.set_name('SQLAP','AP_DEBUG');
4927             fnd_message.set_token('ERROR',SQLERRM);
4928             fnd_message.set_token('CALLING_SEQUENCE',current_calling_sequence);
4929             fnd_message.set_token('PARAMETERS',
4930                   ' invoice_id = '||to_char(x_invoice_id)
4931                 ||', invoice_line_number =' ||to_char(x_invoice_line_number)
4932                 ||', correction_type = '||x_correction_type
4933                 ||', final_match_flag = '||x_final_match_flag
4934                 ||', total_amount = '||to_char(x_total_amount));
4935             fnd_message.set_token('DEBUG_INFO',l_debug_info);
4936         End if;
4937 
4938 	--Clean up the PL/SQL tables on error
4939         x_corr_dist_tab.delete;
4940 
4941         app_exception.raise_exception;
4942 
4943 END Insert_Corr_Invoice_Dists;
4944 
4945 
4946 /*=============================================================================
4947 			       QUICK MATCH
4948 ==============================================================================*/
4949 
4950 
4951 PROCEDURE Quick_Match_Line_Generation(
4952 		x_invoice_id  	   IN	NUMBER,
4953 		x_po_header_id	   IN	NUMBER,
4954 		x_match_option	   IN	VARCHAR2,
4955 	        x_invoice_amount   IN	NUMBER,
4956        	        x_calling_sequence IN 	VARCHAR2) IS
4957 
4958 l_shipment_table		T_SHIPMENT_TABLE;
4959 current_calling_sequence	VARCHAR2(2000);
4960 l_debug_info			VARCHAR2(2000);
4961 l_api_name			VARCHAR2(50);
4962 
4963 BEGIN
4964 
4965    l_api_name := 'Quick_Match_Line_Generation';
4966 
4967    current_calling_sequence := 'Quick_Match_PO_RCV<-'||x_calling_sequence;
4968 
4969    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
4970          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Quick_Match_Line_Generation(+)');
4971    END IF;
4972 
4973    l_debug_info := 'Call Get_Info';
4974    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4975         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4976    END IF;
4977 
4978    Get_Info(x_invoice_id => x_invoice_id,
4979    	    x_calling_sequence => current_calling_sequence);
4980 
4981    IF g_invoice_type_lookup_code = 'RETAINAGE RELEASE' THEN
4982 
4983       l_debug_info := 'Call Generate_Release_Lines';
4984       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4985           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4986       END IF;
4987 
4988       Generate_Release_Lines (p_po_header_id     => x_po_header_id,
4989                               p_invoice_id       => x_invoice_id,
4990                               p_release_amount   => g_release_amount_net_of_tax,
4991                               x_calling_sequence => current_calling_sequence);
4992 
4993    ELSE
4994 
4995    l_debug_info := 'Call Get_Shipment_List_For_QM';
4996    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4997         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4998    END IF;
4999 
5000    Get_Shipment_List_For_QM(X_Invoice_Id => x_invoice_id,
5001    			    X_Po_Header_Id => x_po_header_id,
5002    			    X_Match_Option => x_match_option,
5003 			    X_Match_Amount => x_invoice_amount,
5004    			    X_Shipment_Table => l_shipment_table,
5005    			    X_Calling_Sequence => current_calling_sequence);
5006 
5007    l_debug_info := 'Call Generate_Lines_For_Quickmatch';
5008    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5009         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5010    END IF;
5011 
5012    Generate_Lines_for_Quickmatch(X_Invoice_Id  => x_invoice_id,
5013 	    			 X_Match_Option => x_match_option,
5014    				 X_Shipment_Table => l_shipment_table,
5015    				 X_Calling_Sequence => current_calling_sequence);
5016 
5017 
5018   --Clean up the PL/SQL table
5019   l_shipment_table.delete;
5020 
5021   END IF;
5022 
5023    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
5024          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Quick_Match_Line_Generation(-)');
5025    END IF;
5026 
5027 EXCEPTION
5028   WHEN others then
5029      If (SQLCODE <> -20001) Then
5030         fnd_message.set_name('SQLAP','AP_DEBUG');
5031         fnd_message.set_token('ERROR',SQLERRM);
5032         fnd_message.set_token('CALLING_SEQUENCE',current_calling_sequence);
5033         fnd_message.set_token('PARAMETERS',
5034                   ' invoice_id = '||to_char(x_invoice_id)
5035                 ||', Po header id = '||to_char(x_po_header_id)
5036                 ||', match option = '||x_match_option
5037                 ||', invoice_amount = '||to_char(x_invoice_amount));
5038         fnd_message.set_token('DEBUG_INFO',l_debug_info);
5039      End if;
5040   --Clean up the PL/SQL tables on error
5041   l_shipment_table.delete;
5042 
5043   app_exception.raise_exception;
5044 
5045 END Quick_Match_Line_Generation;
5046 
5047 
5048 PROCEDURE Get_Shipment_List_For_QM(X_Invoice_Id	      IN	 NUMBER,
5049 				   X_Po_Header_Id     IN	 NUMBER,
5050 				   X_Match_Option     IN         VARCHAR2,
5051 				   X_Match_Amount     IN	 NUMBER,
5052 				   X_Shipment_Table   OUT NOCOPY T_SHIPMENT_TABLE,
5053 				   X_Calling_Sequence IN 	 VARCHAR2) IS
5054 
5055 CURSOR PO_Shipment_list_cursor IS
5056  SELECT pll.line_location_id,
5057  	pll.po_line_id,
5058  	pll.po_release_id,
5059  	decode(pll.matching_basis_lookup_code,'AMOUNT',
5060 	        (pll.amount - decode(pll.shipment_type,'PREPAYMENT',
5061 		     	             nvl(pll.amount_financed,0),
5062 	 	                     nvl(pll.amount_billed,0)
5063                                     )
5064                  - nvl(pll.amount_cancelled,0)
5065                 ),
5066 	        ap_utilities_pkg.ap_round_currency(
5067 				  		   (pll.quantity - decode(pll.shipment_type,'PREPAYMENT',
5068 		  	         		    			  nvl(pll.quantity_financed,0),
5069 				 		    			  nvl(pll.quantity_billed,0)
5070 		    				   			 )
5071 						    - nvl(pll.quantity_cancelled,0)
5072 						   ) * pll.price_override, g_invoice_currency_code
5073 						  )
5074               ) amount_available,
5075  	pll.item_id,
5076  	pll.unit_meas_lookup_code,
5077  	pll.price_override,
5078  	pll.item_description,
5079  	msi.asset_category_id,
5080      -- pll.ussgl_transaction_code, - Bug 4277744
5081  	pll.type_1099,
5082 	pll.matching_basis_lookup_code,
5083  	decode(g_transfer_flag,'Y',pll.attribute_category,NULL) attribute_category,
5084  	decode(g_transfer_flag,'Y',pll.attribute1,NULL) attribute1,
5085  	decode(g_transfer_flag,'Y',pll.attribute2,NULL) attribute2,
5086  	decode(g_transfer_flag,'Y',pll.attribute3,NULL) attribute3,
5087  	decode(g_transfer_flag,'Y',pll.attribute4,NULL) attribute4,
5088  	decode(g_transfer_flag,'Y',pll.attribute5,NULL) attribute5,
5089   	decode(g_transfer_flag,'Y',pll.attribute6,NULL) attribute6,
5090   	decode(g_transfer_flag,'Y',pll.attribute7,NULL) attribute7,
5091   	decode(g_transfer_flag,'Y',pll.attribute8,NULL) attribute8,
5092   	decode(g_transfer_flag,'Y',pll.attribute9,NULL) attribute9,
5093   	decode(g_transfer_flag,'Y',pll.attribute10,NULL) attribute10,
5094   	decode(g_transfer_flag,'Y',pll.attribute11,NULL) attribute11,
5095   	decode(g_transfer_flag,'Y',pll.attribute12,NULL) attribute12,
5096   	decode(g_transfer_flag,'Y',pll.attribute13,NULL) attribute13,
5097   	decode(g_transfer_flag,'Y',pll.attribute14,NULL) attribute14,
5098   	decode(g_transfer_flag,'Y',pll.attribute15,NULL) attribute15
5099 	--ETAX: Invwkb
5100 	--OPEN ISSUE 1
5101 	,pll.ship_to_location_id	ship_to_location_id
5102 	,decode(pll.matching_basis_lookup_code,'AMOUNT',
5103 	        (pll.amount - decode(pll.shipment_type,'PREPAYMENT',
5104 		     	             nvl(pll.amount_financed,0),
5105 	 	                     nvl(pll.amount_billed,0)
5106                                     )
5107                  - nvl(pll.amount_cancelled,0)),
5108           (pll.quantity - decode(pll.shipment_type,'PREPAYMENT',
5109 		  	         nvl(pll.quantity_financed,0),
5110 				 nvl(pll.quantity_billed,0)
5111 		    	        )- nvl(pll.quantity_cancelled,0)) * decode(pll.price_override,0,1,pll.price_override)) amount_available_unrounded /*bug 8973086*//*9559298*/
5112 	/*
5113 	pll.primary_intended_use primary_intended_use,
5114 	pll.product_fisc_classification product_fisc_classification,
5115 	pll.product_type  product_type,
5116 	pll.product_category product_category,
5117 	pll.user_defined_fisc_class user_defined_fisc_class
5118 	*/
5119  FROM po_line_locations_ap_v pll,
5120       mtl_system_items msi
5121  WHERE  pll.po_header_id = x_po_header_id
5122  AND msi.inventory_item_id(+)  = pll.item_id
5123  AND msi.organization_id(+) = g_inventory_organization_id
5124  AND pll.match_option = 'P'
5125  AND pll.closed_code NOT IN ('FINALLY CLOSED') /*, 'CLOSED', 'CLOSED FOR INVOICE')*/ --Bug9323877 --bug 8899681
5126  --Make sure there is some quantity or amount left to be billed
5127  --based on the matching_basis of the shipment
5128  AND ((g_invoice_type_lookup_code <> 'PREPAYMENT'
5129        and (
5130             (pll.matching_basis_lookup_code = 'QUANTITY' and
5131              pll.quantity - nvl(pll.quantity_billed,0) - nvl(pll.quantity_cancelled,0) > 0
5132             ) or
5133             (pll.matching_basis_lookup_code = 'AMOUNT' and
5134 	     pll.amount - nvl(pll.amount_billed,0) - nvl(pll.amount_cancelled,0) > 0
5135 	    )
5136            )
5137       ) OR
5138       (g_invoice_type_lookup_code = 'PREPAYMENT'
5139        and (
5140             (pll.matching_basis_lookup_code = 'QUANTITY' and pll.shipment_type = 'PREPAYMENT' and
5141              pll.quantity - nvl(pll.quantity_financed,0) - nvl(pll.quantity_cancelled,0) > 0
5142             ) or
5143 	    (pll.matching_basis_lookup_code = 'AMOUNT' and pll.shipment_type = 'PREPAYMENT' and
5144 	     pll.amount - nvl(pll.amount_financed,0) - nvl(pll.amount_cancelled,0) > 0
5145             ) or
5146 	    (pll.matching_basis_lookup_code = 'QUANTITY' and pll.shipment_type <> 'PREPAYMENT' and
5147 	     pll.quantity - nvl(pll.quantity_billed,0) - nvl(pll.quantity_cancelled,0) > 0
5148 	    ) or
5149 	    (pll.matching_basis_lookup_code = 'AMOUNT' and pll.shipment_type <> 'PREPAYMENT' and
5150 	     pll.amount - nvl(pll.amount_billed,0) - nvl(pll.amount_cancelled,0) > 0
5151 	    )
5152            )
5153       )
5154      )
5155  --make sure the correct shipment type is matched to correct invoice type
5156  AND
5157   ((g_invoice_type_lookup_code <> 'PREPAYMENT' and
5158     pll.shipment_type <> 'PREPAYMENT'
5159    ) OR
5160    (g_invoice_type_lookup_code = 'PREPAYMENT' and
5161     ((pll.payment_type IS NOT NULL and
5162       pll.shipment_type = 'PREPAYMENT') or
5163      (pll.payment_type IS NULL)
5164     )
5165    )
5166   )
5167  ORDER BY pll.line_location_id ;
5168 
5169 
5170 CURSOR Receipt_Shipment_List_cursor IS
5171  SELECT rcv.po_line_id,
5172   	rcv.po_release_id,
5173  	rcv.po_line_location_id,
5174  	rcv.rcv_transaction_id,
5175  	rcv.receipt_uom_lookup_code,
5176  	rcv.po_uom_lookup_code,
5177  	rcv.po_unit_price,
5178  	rcv.item_id,
5179  	rcv.item_description,
5180  	msi.asset_category_id,
5181      -- rsl.ussgl_transaction_code, - Bug 4277744
5182  	rcv.type_1099,
5183         rcv.matching_basis_lookup_code,
5184  	decode(g_transfer_flag,'Y',rsl.attribute_category,NULL) attribute_category,
5185  	decode(g_transfer_flag,'Y',rsl.attribute1,NULL) attribute1,
5186  	decode(g_transfer_flag,'Y',rsl.attribute2,NULL) attribute2,
5187  	decode(g_transfer_flag,'Y',rsl.attribute3,NULL) attribute3,
5188  	decode(g_transfer_flag,'Y',rsl.attribute4,NULL) attribute4,
5189  	decode(g_transfer_flag,'Y',rsl.attribute5,NULL) attribute5,
5190   	decode(g_transfer_flag,'Y',rsl.attribute6,NULL) attribute6,
5191   	decode(g_transfer_flag,'Y',rsl.attribute7,NULL) attribute7,
5192   	decode(g_transfer_flag,'Y',rsl.attribute8,NULL) attribute8,
5193   	decode(g_transfer_flag,'Y',rsl.attribute9,NULL) attribute9,
5194   	decode(g_transfer_flag,'Y',rsl.attribute10,NULL) attribute10,
5195   	decode(g_transfer_flag,'Y',rsl.attribute11,NULL) attribute11,
5196   	decode(g_transfer_flag,'Y',rsl.attribute12,NULL) attribute12,
5197   	decode(g_transfer_flag,'Y',rsl.attribute13,NULL) attribute13,
5198   	decode(g_transfer_flag,'Y',rsl.attribute14,NULL) attribute14,
5199   	decode(g_transfer_flag,'Y',rsl.attribute15,NULL) attribute15
5200 	--ETAX: Invwkb
5201 	--OPEN ISSUE 1
5202 	,rcv.ship_to_location_id        ship_to_location_id
5203 	/*
5204 	rcv.primary_intended_use        primary_intended_use,
5205 	rcv.product_fisc_classification product_fisc_classification,
5206 	rcv.product_type  		product_type,
5207 	rcv.product_category 		product_category,
5208 	rcv.user_defined_fisc_class 	user_defined_fisc_class
5209 	*/
5210  FROM   po_ap_receipt_match_v rcv,
5211         mtl_system_items msi,
5212         rcv_shipment_lines rsl
5213  WHERE  rcv.po_header_id = x_po_header_id
5214  AND    msi.inventory_item_id(+) = rcv.item_id
5215  AND    msi.organization_id(+) = g_inventory_organization_id
5216  AND    rcv.rcv_shipment_line_id = rsl.shipment_line_id
5217  AND    rcv.po_match_option = 'R'
5218  ORDER BY rcv.rcv_transaction_id;
5219 
5220 l_available_match_amount      NUMBER := x_match_amount;
5221 l_amount 		      ap_invoice_lines.amount%TYPE;
5222 l_base_amount		      ap_invoice_lines.base_amount%TYPE;
5223 l_quantity 		      ap_invoice_lines.quantity_invoiced%TYPE;
5224 l_po_line_location_id         po_line_locations.line_location_id%TYPE;
5225 l_po_line_id 	              po_lines.po_line_id%TYPE;
5226 l_po_release_id        	      po_releases.po_release_id%TYPE;
5227 l_rcv_transaction_id	      rcv_transactions.transaction_id%TYPE;
5228 l_receipt_uom_lookup_code     rcv_shipment_lines.unit_of_measure%TYPE;
5229 l_po_uom_lookup_code	      po_lines.unit_meas_lookup_code%TYPE;
5230 l_po_unit_price		      po_lines.unit_price%TYPE;
5231 l_available_shipment_amount   po_line_locations.amount%TYPE;
5232 l_item_id 	      	      po_lines.item_id%TYPE;
5233 l_unit_meas_lookup_code       po_lines.unit_meas_lookup_code%TYPE;
5234 l_price_override	      po_line_locations.price_override%TYPE;
5235 l_item_description	      po_lines.item_description%TYPE;
5236 l_asset_category_id	      mtl_system_items.asset_category_id%TYPE;
5237 -- Removed for bug 4277744
5238 -- l_ussgl_transaction_code   po_line_locations.ussgl_transaction_code%TYPE;
5239 l_type_1099		      po_lines.type_1099%TYPE;
5240 l_attribute_category	      po_line_locations.attribute_category%TYPE;
5241 l_attribute1		      po_line_locations.attribute1%TYPE;
5242 l_attribute2		      po_line_locations.attribute2%TYPE;
5243 l_attribute3		      po_line_locations.attribute3%TYPE;
5244 l_attribute4		      po_line_locations.attribute4%TYPE;
5245 l_attribute5		      po_line_locations.attribute5%TYPE;
5246 l_attribute6		      po_line_locations.attribute6%TYPE;
5247 l_attribute7		      po_line_locations.attribute7%TYPE;
5248 l_attribute8		      po_line_locations.attribute8%TYPE;
5249 l_attribute9		      po_line_locations.attribute9%TYPE;
5250 l_attribute10		      po_line_locations.attribute10%TYPE;
5251 l_attribute11		      po_line_locations.attribute11%TYPE;
5252 l_attribute12		      po_line_locations.attribute12%TYPE;
5253 l_attribute13		      po_line_locations.attribute13%TYPE;
5254 l_attribute14		      po_line_locations.attribute14%TYPE;
5255 l_attribute15		      po_line_locations.attribute15%TYPE;
5256 l_invoice_line_number         ap_invoice_lines.line_number%TYPE := 1;
5257 l_index                       po_line_locations.line_location_id%TYPE;
5258 l_conversion_factor	      NUMBER;
5259 l_ordered_qty 		      NUMBER;
5260 l_cancelled_qty		      NUMBER;
5261 l_received_qty  	      NUMBER;
5262 l_corrected_qty 	      NUMBER;
5263 l_delivered_qty 	      NUMBER;
5264 l_transaction_qty 	      NUMBER;
5265 l_billed_qty      	      NUMBER;
5266 l_accepted_qty    	      NUMBER;
5267 l_rejected_qty    	      NUMBER;
5268 l_rect_unit_price   	      po_lines.unit_price%TYPE;
5269 l_max_line_amount	      ap_invoice_lines.amount%TYPE := 0;
5270 l_rounded_index		      po_line_locations.line_location_id%TYPE;
5271 l_sum_prorated_amount	      ap_invoice_lines.amount%TYPE := 0;
5272 l_available_amount	      po_line_locations.amount%TYPE;
5273 l_available_qty		      po_line_locations.quantity%TYPE;
5274 l_invoice_base_amount	      ap_invoice_lines.base_amount%TYPE := 0;
5275 l_sum_line_base_amount	      ap_invoice_lines.base_amount%TYPE := 0;
5276 l_ship_to_location_id         ap_invoice_lines.ship_to_location_id%TYPE;
5277 l_primary_intended_use	      ap_invoice_lines.primary_intended_use%TYPE;
5278 l_product_fisc_classification ap_invoice_lines.product_fisc_classification%TYPE;
5279 l_product_type		      ap_invoice_lines.product_type%TYPE;
5280 l_product_category	      ap_invoice_lines.product_category%TYPE;
5281 l_user_defined_fisc_class     ap_invoice_lines.user_defined_fisc_class%TYPE;
5282 l_amount_ordered              NUMBER;
5283 l_amount_cancelled            NUMBER;
5284 l_amount_delivered            NUMBER;
5285 l_amount_billed               NUMBER;
5286 l_amount_received             NUMBER;
5287 l_amount_corrected            NUMBER;
5288 l_matching_basis	      po_line_locations.matching_basis%TYPE;
5289 l_retained_amount             ap_invoice_lines.retained_amount%TYPE;
5290 l_ret_status                  VARCHAR2(100);
5291 l_msg_count                   NUMBER;
5292 l_msg_data                    VARCHAR2(250);
5293 l_debug_info        	      VARCHAR2(2000);
5294 current_calling_sequence      VARCHAR2(2000);
5295 l_api_name		      VARCHAR2(50);
5296 
5297 --bugfix:5565310
5298 l_ref_doc_application_id      zx_transaction_lines_gt.ref_doc_application_id%TYPE;
5299 l_ref_doc_entity_code         zx_transaction_lines_gt.ref_doc_entity_code%TYPE;
5300 l_ref_doc_event_class_code    zx_transaction_lines_gt.ref_doc_event_class_code%TYPE;
5301 l_ref_doc_line_quantity       zx_transaction_lines_gt.ref_doc_line_quantity%TYPE;
5302 l_po_header_curr_conv_rat     po_headers_all.rate%TYPE;
5303 l_ref_doc_trx_level_type      zx_transaction_lines_gt.ref_doc_trx_level_type%TYPE;
5304 l_po_header_curr_conv_rate    po_headers_all.rate%TYPE;
5305 l_uom_code                    mtl_units_of_measure.uom_code%TYPE;
5306 l_ref_doc_trx_id              po_headers_all.po_header_id%TYPE;
5307 l_error_code                  varchar2(2000);
5308 l_success		      boolean;
5309 l_dummy				number;
5310 -- bug#6977104
5311 l_inv_org_id                  NUMBER;
5312 -- bug 7577673: start
5313 l_allow_tax_code_override     varchar2(10);
5314 l_dflt_tax_class_code         zx_transaction_lines_gt.input_tax_classification_code%type;
5315 -- bug 7577673: end
5316 l_available_shipment_amt_unrnd  po_line_locations.amount%TYPE; --bug 8973086
5317 l_sum_line_amount  NUMBER := 0;  --bug9802729
5318 
5319 l_code_combination_id  NUMBER ; -- Bug 10050094
5320 BEGIN
5321 
5322   l_api_name := 'Get_Shipment_List_For_QuickMatch';
5323 
5324   current_calling_sequence:= 'Get_Shipment_List_For_QuickMatch<-'||x_calling_sequence;
5325 
5326   IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
5327         FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Get_Shipment_List_For_QuickMatch(+)');
5328   END IF;
5329 
5330   IF (x_match_option = 'P') THEN
5331 
5332      OPEN Po_Shipment_List_Cursor;
5333 
5334      LOOP
5335 
5336         l_debug_info := 'Fetch next record of PO_Shipment_List_Cursor';
5337 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5338 	     FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5339         END IF;
5340 
5341         FETCH PO_Shipment_List_Cursor INTO l_po_line_location_id,
5342       					 l_po_line_id,
5343 				         l_po_release_id,
5344 				         l_available_shipment_amount,
5345 				         l_item_id,
5346 				         l_unit_meas_lookup_code,
5347 				         l_price_override,
5348 				         l_item_description,
5349 				         l_asset_category_id,
5350 				      -- l_ussgl_transaction_code, - Bug 4277744
5351 				         l_type_1099,
5352 					 l_matching_basis,
5353 				         l_attribute_category,
5354 				         l_attribute1,
5355 				         l_attribute2,
5356 				         l_attribute3,
5357 				         l_attribute4,
5358 				         l_attribute5,
5359       				         l_attribute6,
5360       				         l_attribute7,
5361       				         l_attribute8,
5362       				         l_attribute9,
5363       				         l_attribute10,
5364       				         l_attribute11,
5365       				         l_attribute12,
5366       				         l_attribute13,
5367       				         l_attribute14,
5368       				         l_attribute15,
5369 					 l_ship_to_location_id,
5370 					 l_available_shipment_amt_unrnd --bug 8973086
5371 					 ;
5372 
5373        EXIT WHEN (Po_Shipment_List_Cursor%NOTFOUND);    --OR l_available_match_amount = 0);  bug 8899681
5374 
5375 
5376        --calculate the amount on each line that will be generated
5377       /* IF(l_available_shipment_amount >= l_available_match_amount) THEN
5378                l_amount := l_available_match_amount;
5379          ELSE
5380                l_amount := l_available_shipment_amount;
5381          END IF;*/
5382 
5383        l_amount := l_available_shipment_amount;	--bug 8899681, l_amount should be available shipment amount
5384 
5385        --bugfix:5565310
5386        l_success := AP_ETAX_UTILITY_PKG.Get_PO_Info(
5387                           P_Po_Line_Location_Id         => l_po_line_location_id,
5388                           P_PO_Distribution_Id          => null,
5389                           P_Application_Id              => l_ref_doc_application_id,
5390                           P_Entity_code                 => l_ref_doc_entity_code,
5391                           P_Event_Class_Code            => l_ref_doc_event_class_code,
5392                           P_PO_Quantity                 => l_ref_doc_line_quantity,
5393                           P_Product_Org_Id              => l_inv_org_id, -- bug#6977104
5394                           P_Po_Header_Id                => l_ref_doc_trx_id,
5395                           P_Po_Header_curr_conv_rate    => l_po_header_curr_conv_rate,
5396                           P_Uom_Code                    => l_uom_code,
5397 			  P_Dist_Qty                    => l_dummy,
5398 			  P_Ship_Price                  => l_dummy,
5399                           P_Error_Code                  => l_error_code,
5400                           P_Calling_Sequence            => current_calling_sequence);
5401 
5402        -- Bug 10050094
5403        l_code_combination_id := NULL ;
5404        AP_ETAX_UTILITY_PKG.Get_Default_CCID( p_invoice_id          => x_invoice_id,
5405                                              -- p_default_dist_ccid   => g_default_dist_ccid,
5406                                              p_po_line_location_id => l_po_line_location_id,
5407                                              p_calling_sequence    => current_calling_sequence,
5408                                              x_derived_ccid        => l_code_combination_id ) ;
5409 
5410 
5411        g_dflt_tax_class_code := null; /*bug12428818*/
5412 
5413 -- bug 7577673: start
5414       ZX_AP_TAX_CLASSIFICATN_DEF_PKG.get_default_tax_classification
5415 		            (p_ref_doc_application_id           => l_ref_doc_application_id,
5416 		             p_ref_doc_entity_code              => l_ref_doc_entity_code,
5417 		             p_ref_doc_event_class_code         => l_ref_doc_event_class_code,
5418 		             p_ref_doc_trx_id                   => l_ref_doc_trx_id,
5419 		             p_ref_doc_line_id                  => l_po_line_location_id,
5420 		             p_ref_doc_trx_level_type           => 'SHIPMENT',
5421 		             p_vendor_id                        => g_vendor_id,
5422 		             p_vendor_site_id                   => g_vendor_site_id,
5423 		             p_code_combination_id              => l_code_combination_id, -- Bug 10050094  g_default_dist_ccid,
5424 		             p_concatenated_segments            => null,
5425 		             p_templ_tax_classification_cd      => null,
5426 		             p_ship_to_location_id              => null,
5427 		             p_ship_to_loc_org_id               => null,
5428 		             p_inventory_item_id                => null,
5429 		             p_item_org_id                      => g_inventory_organization_id,
5430 		             p_tax_classification_code          => g_dflt_tax_class_code,
5431 		             p_allow_tax_code_override_flag     => l_allow_tax_code_override,
5432 		             APPL_SHORT_NAME                    => 'SQLAP',
5433 		             FUNC_SHORT_NAME                    => 'NONE',
5434 		             p_calling_sequence                 => 'AP_ETAX_SERVICES_PKG',
5435 		             p_event_class_code                 => l_ref_doc_event_class_code,
5436 		             p_entity_code                      => 'AP_INVOICES',
5437 		             p_application_id                   => 200,
5438 		             p_internal_organization_id         => g_org_id);
5439 -- bug 7577673: end
5440 
5441        AP_Etax_Services_Pkg.Get_Po_Tax_Attributes(
5442                           p_application_id              => l_ref_doc_application_id,
5443 			  p_org_id                      => g_org_id,
5444 			  p_entity_code                 => l_ref_doc_entity_code,
5445 			  p_event_class_code            => l_ref_doc_event_class_code,
5446 			  p_trx_level_type              => 'SHIPMENT',
5447 			  p_trx_id                      => l_ref_doc_trx_id,
5448 			  p_trx_line_id                 => l_po_line_location_id,
5449 			  x_line_intended_use           => g_intended_use,
5450 			  x_product_type                => g_product_type,
5451 			  x_product_category            => g_product_category,
5452 			  x_product_fisc_classification => g_product_fisc_class,
5453 			  x_user_defined_fisc_class     => g_user_defined_fisc_class,
5454 			  x_assessable_value            => g_assessable_value,
5455 			  x_tax_classification_code     => l_dflt_tax_class_code);
5456 
5457 -- bug 7577673: start
5458    -- if tax classification code not retrieved from hierarchy
5459    -- retrieve it from PO
5460    IF (g_dflt_tax_class_code is null) THEN
5461        g_dflt_tax_class_code := l_dflt_tax_class_code;
5462    END IF;
5463 -- bug 7577673: end
5464 
5465        l_debug_info := 'l_po_line_location_id is '||l_po_line_location_id;
5466        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5467           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5468        END IF;
5469 
5470        --Bug9559298
5471        IF(l_price_override = 0) THEN
5472 	    l_quantity := round(l_available_shipment_amt_unrnd,15);
5473        ELSE
5474             l_quantity := round(l_available_shipment_amt_unrnd/l_price_override,15); --bug 8973086 replaced l_amount with l_available_shipment_amt_unrnd
5475        END IF;
5476        --Bug9559298
5477 
5478        /*l_available_match_amount := l_available_match_amount - l_amount;  */   --bug 8899681
5479 
5480        l_index := l_po_line_location_id;
5481 
5482        X_Shipment_Table(l_index).po_header_id := x_po_header_id;
5483        X_Shipment_Table(l_index).po_line_id := l_po_line_id;
5484        X_Shipment_Table(l_index).po_release_id := l_po_release_id;
5485        X_Shipment_Table(l_index).po_line_location_id := l_po_line_location_id;
5486        X_Shipment_Table(l_index).uom := l_unit_meas_lookup_code;
5487        X_Shipment_Table(l_index).unit_price :=  l_price_override;
5488        X_Shipment_Table(l_index).line_number := l_invoice_line_number;
5489        X_Shipment_Table(l_index).quantity_invoiced := l_quantity;
5490        X_Shipment_Table(l_index).amount := l_amount;
5491 
5492        l_debug_info := 'X_Shipment_Table('||l_index||').amount is'||X_Shipment_Table(l_index).amount;
5493        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5494            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5495        END IF;
5496        X_Shipment_Table(l_index).inventory_item_id := l_item_id;
5497        X_Shipment_Table(l_index).item_description := l_item_description;
5498        X_Shipment_Table(l_index).asset_category_id := l_asset_category_id;
5499     -- Removed for bug 4277744
5500     -- X_Shipment_Table(l_index).ussgl_transaction_code := l_ussgl_transaction_code;
5501        X_Shipment_Table(l_index).type_1099 := l_type_1099;
5502        X_Shipment_Table(l_index).attribute_category := l_attribute_category;
5503        X_Shipment_Table(l_index).attribute1 := l_attribute1;
5504        X_Shipment_Table(l_index).attribute2 := l_attribute2;
5505        X_Shipment_Table(l_index).attribute3 := l_attribute3;
5506        X_Shipment_Table(l_index).attribute4 := l_attribute4;
5507        X_Shipment_Table(l_index).attribute5 := l_attribute5;
5508        X_Shipment_Table(l_index).attribute6 := l_attribute6;
5509        X_Shipment_Table(l_index).attribute7 := l_attribute7;
5510        X_Shipment_Table(l_index).attribute8 := l_attribute8;
5511        X_Shipment_Table(l_index).attribute9 := l_attribute9;
5512        X_Shipment_Table(l_index).attribute10 := l_attribute10;
5513        X_Shipment_Table(l_index).attribute11 := l_attribute11;
5514        X_Shipment_Table(l_index).attribute12 := l_attribute12;
5515        X_Shipment_Table(l_index).attribute13 := l_attribute13;
5516        X_Shipment_Table(l_index).attribute14 := l_attribute14;
5517        X_Shipment_Table(l_index).attribute15 := l_attribute15;
5518        X_Shipment_Table(l_index).ship_to_location_id := l_ship_to_location_id;
5519        X_Shipment_Table(l_index).primary_intended_use := g_intended_use;
5520        X_Shipment_Table(l_index).product_fisc_classification := g_product_fisc_class;
5521        X_Shipment_Table(l_index).product_type := g_product_type;
5522        X_Shipment_Table(l_index).product_category := g_product_category;
5523        X_Shipment_Table(l_index).user_defined_fisc_class := g_user_defined_fisc_class;
5524        X_shipment_Table(l_index).assessable_value := g_assessable_value;
5525        X_shipment_Table(l_index).tax_classification_code := g_dflt_tax_class_code;
5526 
5527       X_Shipment_Table(l_index).matching_basis  := l_matching_basis;
5528       X_Shipment_Table(l_index).retained_amount := ap_invoice_lines_utility_pkg.get_retained_amount
5529 			                                        (l_po_line_location_id, l_amount);
5530 
5531        --Get the max of the largest invoice line, to be used for assignment of
5532        --rounding due to proration.
5533        --Uncommented for bug#9802729. This index is required while calculating
5534        --rounding amounts for base amounts
5535 
5536        IF (l_amount >= l_max_line_amount) THEN
5537 
5538            l_rounded_index := l_po_line_location_id;
5539            l_max_line_amount := l_amount;
5540 
5541        END IF;
5542 
5543          l_invoice_line_number := l_invoice_line_number + 1;
5544    /*    l_sum_prorated_amount := l_sum_prorated_amount + l_amount;*/  -- bug 8899681
5545 
5546 
5547     END LOOP;
5548 
5549     CLOSE PO_Shipment_List_Cursor;
5550 
5551     --If proration resulted in rounding error, then add  the rounding amt
5552     --onto max of the largest line, index for which was calculated above
5553     --We need to do the proration rounding only if we exhausted the match amount
5554     --and not if we exhausted the shipments.
5555     /*  IF (l_available_match_amount = 0) THEN
5556 
5557         IF (x_match_amount - l_sum_prorated_amount <> 0 and l_rounded_index is not null) THEN
5558 
5559           X_Shipment_Table(l_rounded_index).amount := X_Shipment_Table(l_rounded_index).amount +
5560       						   (l_available_match_amount - l_sum_prorated_amount);
5561 
5562         END IF;
5563 
5564         END IF;*/  -- bug 8899681
5565 
5566   ELSIF (x_match_option = 'R') THEN
5567 
5568       OPEN Receipt_Shipment_List_Cursor;
5569 
5570       LOOP
5571 
5572 	l_debug_info := 'Fetch next record of Receipt_Shipment_List_Cursor';
5573 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5574 	     FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5575 	END IF;
5576 
5577         FETCH Receipt_Shipment_List_Cursor INTO l_po_line_id,
5578         					l_po_release_id,
5579         					l_po_line_location_id,
5580         					l_rcv_transaction_id,
5581         					l_receipt_uom_lookup_code,
5582         					l_po_uom_lookup_code,
5583         					l_po_unit_price,
5584         					l_item_id,
5585         					l_item_description,
5586         					l_asset_category_id,
5587                                              -- Bug 4277744
5588         				     -- l_ussgl_transaction_code,
5589         					l_type_1099,
5590 						l_matching_basis,
5591         					l_attribute_category,
5592 				         	l_attribute1,
5593 				         	l_attribute2,
5594 				        	l_attribute3,
5595 				        	l_attribute4,
5596 				        	l_attribute5,
5597       				        	l_attribute6,
5598       				         	l_attribute7,
5599       				        	l_attribute8,
5600       				         	l_attribute9,
5601       				         	l_attribute10,
5602       				        	l_attribute11,
5603       				         	l_attribute12,
5604       				         	l_attribute13,
5605       				         	l_attribute14,
5606       				         	l_attribute15
5607 						--ETAX: Invwkb
5608 						--OPEN ISSUE 1
5609 						,l_ship_to_location_id
5610 						/*
5611 						l_primary_intended_use,
5612 						l_product_fisc_classification,
5613 						l_product_type,
5614 						l_product_category,
5615 						l_user_defined_fisc_class*/
5616 						;
5617 
5618         EXIT WHEN (Receipt_Shipment_List_Cursor%NOTFOUND);
5619 	              -- OR l_available_match_amount = 0); --bug#9802729
5620 
5621  -- Start bug#12414407
5622 
5623  l_success := AP_ETAX_UTILITY_PKG.Get_PO_Info(
5624                           P_Po_Line_Location_Id         => l_po_line_location_id,
5625                           P_PO_Distribution_Id          => null,
5626                           P_Application_Id              => l_ref_doc_application_id,
5627                           P_Entity_code                 => l_ref_doc_entity_code,
5628                           P_Event_Class_Code            => l_ref_doc_event_class_code,
5629                           P_PO_Quantity                 => l_ref_doc_line_quantity,
5630                           P_Product_Org_Id              => l_inv_org_id,
5631                           P_Po_Header_Id                => l_ref_doc_trx_id,
5632                           P_Po_Header_curr_conv_rate    => l_po_header_curr_conv_rate,
5633                           P_Uom_Code                    => l_uom_code,
5634 			                    P_Dist_Qty                    => l_dummy,
5635                   			  P_Ship_Price                  => l_dummy,
5636                           P_Error_Code                  => l_error_code,
5637                           P_Calling_Sequence            => current_calling_sequence);
5638 
5639       l_code_combination_id := NULL ;
5640       AP_ETAX_UTILITY_PKG.Get_Default_CCID(
5641                   p_invoice_id          => x_invoice_id,
5642                   p_po_line_location_id => l_po_line_location_id,
5643                   p_calling_sequence    => current_calling_sequence,
5644                   x_derived_ccid        => l_code_combination_id ) ;
5645 
5646 
5647       g_dflt_tax_class_code := null; /*bug12428818*/
5648 
5649       ZX_AP_TAX_CLASSIFICATN_DEF_PKG.get_default_tax_classification
5650 		            (p_ref_doc_application_id           => l_ref_doc_application_id,
5651 		             p_ref_doc_entity_code              => l_ref_doc_entity_code,
5652 		             p_ref_doc_event_class_code         => l_ref_doc_event_class_code,
5653 		             p_ref_doc_trx_id                   => l_ref_doc_trx_id,
5654 		             p_ref_doc_line_id                  => l_po_line_location_id,
5655 		             p_ref_doc_trx_level_type           => 'SHIPMENT',
5656 		             p_vendor_id                        => g_vendor_id,
5657 		             p_vendor_site_id                   => g_vendor_site_id,
5658 		             p_code_combination_id              => l_code_combination_id,
5659 		             p_concatenated_segments            => null,
5660 		             p_templ_tax_classification_cd      => null,
5661 		             p_ship_to_location_id              => null,
5662 		             p_ship_to_loc_org_id               => null,
5663 		             p_inventory_item_id                => null,
5664 		             p_item_org_id                      => g_inventory_organization_id,
5665 		             p_tax_classification_code          => g_dflt_tax_class_code,
5666 		             p_allow_tax_code_override_flag     => l_allow_tax_code_override,
5667 		             APPL_SHORT_NAME                    => 'SQLAP',
5668 		             FUNC_SHORT_NAME                    => 'NONE',
5669 		             p_calling_sequence                 => 'AP_ETAX_SERVICES_PKG',
5670 		             p_event_class_code                 => l_ref_doc_event_class_code,
5671 		             p_entity_code                      => 'AP_INVOICES',
5672 		             p_application_id                   => 200,
5673 		             p_internal_organization_id         => g_org_id);
5674 
5675        AP_Etax_Services_Pkg.Get_Po_Tax_Attributes(
5676         p_application_id              => l_ref_doc_application_id,
5677 			  p_org_id                      => g_org_id,
5678 			  p_entity_code                 => l_ref_doc_entity_code,
5679 			  p_event_class_code            => l_ref_doc_event_class_code,
5680 			  p_trx_level_type              => 'SHIPMENT',
5681 			  p_trx_id                      => l_ref_doc_trx_id,
5682 			  p_trx_line_id                 => l_po_line_location_id,
5683 			  x_line_intended_use           => g_intended_use,
5684 			  x_product_type                => g_product_type,
5685 			  x_product_category            => g_product_category,
5686 			  x_product_fisc_classification => g_product_fisc_class,
5687 			  x_user_defined_fisc_class     => g_user_defined_fisc_class,
5688 			  x_assessable_value            => g_assessable_value,
5689 			  x_tax_classification_code     => l_dflt_tax_class_code);
5690 
5691    IF (g_dflt_tax_class_code is null) THEN
5692        g_dflt_tax_class_code := l_dflt_tax_class_code;
5693    END IF;
5694 -- End  bug#12414407
5695 
5696         IF ((l_receipt_uom_lookup_code <> l_po_uom_lookup_code) AND
5697                 (l_po_uom_lookup_code is not null) AND
5698                 (l_receipt_uom_lookup_code is not null)) THEN
5699        	    l_debug_info := 'Call PO_UOM_S.PO_UOM_CONVERT to get the conversion
5700        	 			factor between receipt uom and po uom';
5701             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5702 	         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5703    	    END IF;
5704 
5705             l_conversion_factor := PO_UOM_S.PO_UOM_CONVERT(l_receipt_uom_lookup_code,
5706            						  l_po_uom_lookup_code,
5707            						  l_item_id);
5708             l_rect_unit_price := l_po_unit_price * l_conversion_factor;    --bug8334274
5709 
5710         ELSE
5711 
5712             l_rect_unit_price := l_po_unit_price;
5713 
5714         END IF;
5715 
5716 
5717         IF (l_matching_basis = 'QUANTITY') THEN
5718 
5719              ap_matching_utils_pkg.get_receipt_quantities(
5720 			l_rcv_transaction_id,
5721       		        l_ordered_qty,
5722                         l_cancelled_qty,
5723                         l_received_qty,
5724                         l_corrected_qty,
5725                         l_delivered_qty,
5726                         l_transaction_qty,
5727                         l_billed_qty,
5728                         l_accepted_qty,
5729                         l_rejected_qty);
5730 
5731               l_billed_qty := nvl(l_billed_qty,0);
5732               l_delivered_qty := nvl(l_delivered_qty,0);
5733               l_cancelled_qty := nvl(l_cancelled_qty,0);
5734               l_ordered_qty := nvl(l_ordered_qty,0);
5735 	      l_received_qty := nvl(l_received_qty,0); --BUG # 8229551
5736 
5737 	    /* BUG # 8229551. using l_received_qty instead of l_delivered_qty.
5738 	       because  we can create the invoice once we receive the goods
5739 	       even before delivering the goods. so we should not compare the
5740 	       deliver quantities. */
5741 
5742               --l_available_qty := l_delivered_qty - l_billed_qty;
5743 	      l_available_qty := l_received_qty - l_billed_qty;
5744 
5745               IF (l_available_qty > 0) THEN
5746 
5747                  IF (g_min_acct_unit IS NOT NULL) THEN
5748                     l_available_amount := ROUND(l_available_qty * l_rect_unit_price/g_min_acct_unit)
5749 						* g_min_acct_unit;
5750                  ELSE
5751                     l_available_amount := ROUND(l_available_qty * l_rect_unit_price, g_precision);
5752                  END IF;
5753 
5754 
5755               END IF;  /* l_available_qty > 0 */
5756 
5757            ELSIF (l_matching_basis = 'AMOUNT') THEN
5758 
5759               RCV_INVOICE_MATCHING_SV.Get_ReceiveAmount(
5760                         p_api_version   => 1.0,
5761                         p_init_msg_list => FND_API.G_TRUE,
5762                         x_return_status => l_ret_status,
5763                         x_msg_count     => l_msg_count,
5764                         x_msg_data      => l_msg_data,
5765                         p_receive_transaction_id => l_rcv_transaction_id,
5766                         x_billed_amt  => l_amount_billed,
5767                         x_received_amt => l_amount_received,
5768                         x_delivered_amt => l_amount_delivered,
5769                         x_corrected_amt => l_amount_corrected);
5770 
5771               l_amount_billed := nvl(l_amount_billed,0);
5772               l_amount_delivered := nvl(l_amount_delivered,0);
5773               l_amount_cancelled := nvl(l_amount_cancelled,0);
5774 	      l_amount_received  := nvl(l_amount_received,0); --BUG # 8229551
5775 
5776 	    /* BUG # 8229551 */
5777               --l_available_amount := l_amount_delivered - l_amount_billed;
5778               l_available_amount := l_amount_received - l_amount_billed;
5779 
5780 	      --Bug9628817: Calculate the available quantity for Amount matching.
5781 	      if(l_rect_unit_price <> 0) then
5782 		l_available_qty := ROUND(l_available_amount/l_rect_unit_price,15);
5783 	      else
5784 		l_available_qty := 0;
5785 	      end if;
5786 	      --End of bug9628817
5787 
5788            END IF; /* l_matching_basis = 'QUANTITY' */
5789 
5790 
5791 	   --Bug9628817: Added matching_basis to if condition
5792 	   IF ((l_matching_basis='AMOUNT' AND l_available_amount > 0)
5793 	    OR (l_matching_basis='QUANTITY' AND l_available_qty > 0)) THEN
5794 
5795               --Commented for bug#9802729. Quick match  has to all
5796 	      --available amounts on PO/Receipt irresepctive of
5797 	      --current amount entered for invoice.
5798 
5799              /* IF (l_available_amount  >= l_available_match_amount) THEN
5800                  l_amount := l_available_match_amount;
5801                 ELSE
5802                  l_amount := l_available_amount;
5803                 END IF;
5804               */
5805 
5806                 l_amount := l_available_amount;  --bug#9802729
5807 
5808               --Bug9628817: quantity is calculated in the previous block
5809               --l_quantity := ROUND(l_amount/l_rect_unit_price,15); /*Bug 7515118*/
5810               l_quantity := l_available_qty;  --Bug9628817
5811 
5812               -- l_available_match_amount := l_available_match_amount - l_amount; bug#9802729.
5813 
5814 	      /* BUG # 8229551 need to use l_invoice_line_number instead of l_po_line_location_id
5815 	         if there are more than one receipt for the same po line then it will fail to
5816 		 load all the records by using single po line location id.*/
5817 
5818 	      --l_index := l_po_line_location_id; -- bug 5929800 l_index was not being initialized
5819 	       l_index := l_invoice_line_number;
5820 
5821      	      X_Shipment_Table(l_index).po_header_id := x_po_header_id;
5822      	      X_Shipment_Table(l_index).po_line_id := l_po_line_id;
5823      	      X_Shipment_Table(l_index).po_release_id := l_po_release_id;
5824      	      X_Shipment_Table(l_index).po_line_location_id := l_po_line_location_id;
5825      	      X_Shipment_Table(l_index).rcv_transaction_id := l_rcv_transaction_id;
5826      	      X_Shipment_Table(l_index).uom := l_receipt_uom_lookup_code;
5827      	      X_Shipment_Table(l_index).unit_price :=  l_rect_unit_price;
5828      	      X_Shipment_Table(l_index).line_number := l_invoice_line_number;
5829      	      X_Shipment_Table(l_index).quantity_invoiced := l_quantity;
5830      	      X_Shipment_Table(l_index).amount := l_amount;
5831     	      X_Shipment_Table(l_index).inventory_item_id := l_item_id;
5832     	      X_Shipment_Table(l_index).item_description := l_item_description;
5833     	      X_Shipment_Table(l_index).asset_category_id := l_asset_category_id;
5834            -- Removed for bug 4277744
5835     	   -- X_Shipment_Table(l_index).ussgl_transaction_code := l_ussgl_transaction_code;
5836     	      X_Shipment_Table(l_index).type_1099 := l_type_1099;
5837      	      X_Shipment_Table(l_index).attribute_category := l_attribute_category;
5838      	      X_Shipment_Table(l_index).attribute1 := l_attribute1;
5839      	      X_Shipment_Table(l_index).attribute2 := l_attribute2;
5840      	      X_Shipment_Table(l_index).attribute3 := l_attribute3;
5841       	      X_Shipment_Table(l_index).attribute4 := l_attribute4;
5842       	      X_Shipment_Table(l_index).attribute5 := l_attribute5;
5843      	      X_Shipment_Table(l_index).attribute6 := l_attribute6;
5844               X_Shipment_Table(l_index).attribute7 := l_attribute7;
5845      	      X_Shipment_Table(l_index).attribute8 := l_attribute8;
5846      	      X_Shipment_Table(l_index).attribute9 := l_attribute9;
5847     	      X_Shipment_Table(l_index).attribute10 := l_attribute10;
5848     	      X_Shipment_Table(l_index).attribute11 := l_attribute11;
5849    	      X_Shipment_Table(l_index).attribute12 := l_attribute12;
5850    	      X_Shipment_Table(l_index).attribute13 := l_attribute13;
5851    	      X_Shipment_Table(l_index).attribute14 := l_attribute14;
5852    	      X_Shipment_Table(l_index).attribute15 := l_attribute15;
5853 	      X_Shipment_Table(l_index).ship_to_location_id := l_ship_to_location_id;
5854 	      --Bug#12414407. Modified all local attributes to global values.
5855 	      X_Shipment_Table(l_index).primary_intended_use := g_intended_use; /* l_primary_intended_use; */
5856 	      X_Shipment_Table(l_index).product_fisc_classification := g_product_fisc_class; /* l_product_fisc_classification; */
5857 	      X_Shipment_Table(l_index).product_type :=g_product_type; /* l_product_type; */
5858 	      X_Shipment_Table(l_index).product_category :=g_product_category;  /* l_product_category; */
5859 	      X_Shipment_Table(l_index).user_defined_fisc_class :=g_user_defined_fisc_class;  /* l_user_defined_fisc_class; */
5860 
5861 	       --Introduced below 2 values for bug#12414407
5862               X_shipment_Table(l_index).assessable_value := g_assessable_value;
5863               X_shipment_Table(l_index).tax_classification_code := g_dflt_tax_class_code;
5864 	       --End Bug#12414407
5865 
5866 	      X_Shipment_Table(l_index).matching_basis  := l_matching_basis;
5867 	      X_Shipment_Table(l_index).retained_amount := ap_invoice_lines_utility_pkg.get_retained_amount
5868 		                                                                (l_po_line_location_id, l_amount);
5869 
5870               --Get the max of the largest invoice line, to be used for assignment of
5871               --rounding due to proration.
5872               IF (l_amount >= l_max_line_amount) THEN
5873 
5874                  l_rounded_index := l_invoice_line_number;  --BUG # 8229551
5875                  l_max_line_amount := l_amount;
5876 
5877               END IF;
5878 
5879               l_invoice_line_number := l_invoice_line_number + 1;
5880               -- l_sum_prorated_amount := l_sum_prorated_amount + l_amount; -- bug#9802729
5881 
5882          END IF; /* (l_available_amount > 0)*/
5883 
5884     END LOOP;
5885 
5886     CLOSE Receipt_Shipment_List_Cursor;
5887 
5888     --If proration resulted in rounding error, then add  the rounding amt
5889     --onto max of the largest line, index for which was calculated above
5890     --We need to do the proration rounding only if we exhausted the match amount
5891     --and not if we exhausted the shipments.
5892 
5893     --Commented for bug#9802729.
5894 
5895    /* IF(l_available_match_amount = 0) THEN
5896        IF (x_match_amount - l_sum_prorated_amount <> 0 and l_rounded_index IS NOT NULL) THEN
5897 
5898           X_Shipment_Table(l_rounded_index).amount := X_Shipment_Table(l_rounded_index).amount +
5899       						   (l_available_match_amount - l_sum_prorated_amount);
5900 
5901        END IF;
5902     END IF; */
5903 
5904   END IF;/*(x_match option = 'P')*/
5905 
5906 
5907   --Calculate base_amounts and rounding for foriegn currency invoices
5908   IF (g_exchange_rate IS NOT NULL) THEN
5909 
5910     --bug#9802729 Commented and introduced below FOR LOOP
5911     /* l_invoice_base_amount := ap_utilities_pkg.ap_round_currency(
5912      				  x_match_amount * g_exchange_rate,
5913 				  g_base_currency_code); */
5914 
5915      --Populate base_amount column for foriegn currency invoice.
5916      --Base amount needs to be populated after the lines have been rounded
5917      --for proration, hence doing in the loop below.
5918 
5919      FOR i IN NVL(X_Shipment_Table.first,0) .. NVL(X_Shipment_Table.last,-1) LOOP  /*bug10630400 : replaced 0 with -1 in NVL*/
5920 
5921        /*bug10630400 begin*/
5922        l_debug_info := 'Inside shipment loop number'||i;
5923        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5924           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5925        END IF; /*bug10630400 end*/
5926 
5927        IF X_Shipment_Table.exists(i) then /*bug10630400 - begin*/
5928 	  l_base_amount := ap_utilities_pkg.ap_round_currency(
5929                                                 x_shipment_table(i).amount * g_exchange_rate,
5930 					        g_base_currency_code);
5931 
5932 	  x_shipment_table(i).base_amount := l_base_amount;
5933 
5934           l_sum_line_base_amount := l_sum_line_base_amount + l_base_amount;
5935           l_sum_line_amount := l_sum_line_amount + x_shipment_table(i).amount; --bug#9802729
5936 
5937        END IF; /*bug10630400 end*/
5938 
5939      END LOOP;
5940 
5941      --bug#9802729. Commented above and introduced here with l_sum_line_amount
5942       l_invoice_base_amount := ap_utilities_pkg.ap_round_currency(
5943      				  l_sum_line_amount * g_exchange_rate,
5944 				  g_base_currency_code);
5945 
5946      --Perform rounding
5947      IF (l_invoice_base_amount <> l_sum_line_base_amount) THEN
5948 
5949        /*bug10630400 begin*/
5950        l_debug_info := 'In foreign currency final rounding inv base- '||l_invoice_base_amount;
5951        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5952           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5953        END IF; /*bug10630400 end*/
5954 
5955        x_shipment_table(l_rounded_index).base_amount := x_shipment_table(l_rounded_index).base_amount +
5956        							  (l_invoice_base_amount - l_sum_line_base_amount);
5957        x_shipment_table(l_rounded_index).rounding_amt := l_invoice_base_amount - l_sum_line_base_amount;
5958 
5959      END IF;
5960 
5961   END IF;
5962 
5963   IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
5964         FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Get_Shipment_List_For_QuickMatch(-)');
5965   END IF;
5966 
5967 
5968 EXCEPTION
5969   WHEN others then
5970      If (SQLCODE <> -20001) Then
5971         fnd_message.set_name('SQLAP','AP_DEBUG');
5972         fnd_message.set_token('ERROR',SQLERRM);
5973         fnd_message.set_token('CALLING_SEQUENCE',current_calling_sequence);
5974         fnd_message.set_token('PARAMETERS',
5975                   ' invoice_id = '||to_char(x_invoice_id)
5976                 ||', Po header id = '||to_char(x_po_header_id)
5977                 ||', match option = '||x_match_option
5978                 ||', match_amount = '||to_char(x_match_amount));
5979         fnd_message.set_token('DEBUG_INFO',l_debug_info);
5980      End if;
5981   --Clean up the PL/SQL tables on error
5982   x_shipment_table.delete;
5983 
5984   app_exception.raise_exception;
5985 
5986 END Get_Shipment_List_For_QM;
5987 
5988 
5989 PROCEDURE Generate_Lines_For_QuickMatch (
5990 		x_invoice_id  IN NUMBER,
5991 		x_shipment_table IN T_SHIPMENT_TABLE,
5992 		x_match_option IN VARCHAR2,
5993 		x_calling_sequence IN VARCHAR2) IS
5994 
5995 i NUMBER;
5996 l_debug_info VARCHAR2(2000);
5997 current_calling_sequence VARCHAR2(2000);
5998 l_api_name   VARCHAR2(50);
5999 l_full_mtch_amt  NUMBER;  --Bug13505998
6000 
6001 BEGIN
6002 
6003 
6004    l_api_name := 'Generate_Lines_For_QuickMatch';
6005    current_calling_sequence := 'Generate_Lines_For_QuickMatch<-'||x_calling_sequence;
6006    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
6007          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Generate_Lines_For_QuickMatch(+)');
6008    END IF;
6009 
6010    FOR i IN NVL(x_shipment_table.first,0) .. NVL(x_shipment_table.last,0) LOOP
6011 
6012      IF (x_shipment_table.exists(i)) THEN
6013      /* bug 6150813 */
6014 
6015      --Bug#13505998
6016      l_full_mtch_amt :=
6017       ap_matching_utils_pkg.get_full_mtch_amt
6018            (X_SHIPMENT_TABLE(i).PO_LINE_LOCATION_ID,
6019 	    G_Invoice_Currency_Code);
6020 
6021     l_debug_info := 'l_full_mtch_amt '||l_full_mtch_amt;
6022 
6023    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6024         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6025    END IF;
6026    --End Bug#13505998
6027 
6028    INSERT INTO AP_INVOICE_LINES_ALL
6029    		(INVOICE_ID,
6030 		LINE_NUMBER,
6031 		LINE_TYPE_LOOKUP_CODE,
6032 		REQUESTER_ID,
6033 		DESCRIPTION,
6034 		LINE_SOURCE,
6035 		ORG_ID,
6036 		INVENTORY_ITEM_ID,
6037 		ITEM_DESCRIPTION,
6038 		SERIAL_NUMBER,
6039 		MANUFACTURER,
6040 		MODEL_NUMBER,
6041 		GENERATE_DISTS,
6042 		MATCH_TYPE,
6043 		DISTRIBUTION_SET_ID,
6044 		ACCOUNT_SEGMENT,
6045 		BALANCING_SEGMENT,
6046 		COST_CENTER_SEGMENT,
6047 		OVERLAY_DIST_CODE_CONCAT,
6048 		DEFAULT_DIST_CCID,
6049 		PRORATE_ACROSS_ALL_ITEMS,
6050 		LINE_GROUP_NUMBER,
6051 		ACCOUNTING_DATE,
6052 		PERIOD_NAME,
6053 		DEFERRED_ACCTG_FLAG,
6054 		DEF_ACCTG_START_DATE,
6055 		DEF_ACCTG_END_DATE,
6056 		DEF_ACCTG_NUMBER_OF_PERIODS,
6057 		DEF_ACCTG_PERIOD_TYPE,
6058 		SET_OF_BOOKS_ID,
6059 		AMOUNT,
6060 		BASE_AMOUNT,
6061 		ROUNDING_AMT,
6062 		QUANTITY_INVOICED,
6063 		UNIT_MEAS_LOOKUP_CODE,
6064 		UNIT_PRICE,
6065 		WFAPPROVAL_STATUS,
6066        	     -- USSGL_TRANSACTION_CODE,- Bug 4277744
6067 		DISCARDED_FLAG,
6068 		ORIGINAL_AMOUNT,
6069 		ORIGINAL_BASE_AMOUNT,
6070 		ORIGINAL_ROUNDING_AMT,
6071 		CANCELLED_FLAG,
6072 		INCOME_TAX_REGION,
6073 		TYPE_1099,
6074 		STAT_AMOUNT,
6075 		PREPAY_INVOICE_ID,
6076 		PREPAY_LINE_NUMBER,
6077 		INVOICE_INCLUDES_PREPAY_FLAG,
6078 		CORRECTED_INV_ID,
6079 		CORRECTED_LINE_NUMBER,
6080 		PO_HEADER_ID,
6081 		PO_LINE_ID,
6082 		PO_RELEASE_ID,
6083 		PO_LINE_LOCATION_ID,
6084 		PO_DISTRIBUTION_ID,
6085 		RCV_TRANSACTION_ID,
6086 		FINAL_MATCH_FLAG,
6087 		ASSETS_TRACKING_FLAG,
6088 		ASSET_BOOK_TYPE_CODE,
6089 		ASSET_CATEGORY_ID,
6090 		PROJECT_ID,
6091 		TASK_ID,
6092 		EXPENDITURE_TYPE,
6093 		EXPENDITURE_ITEM_DATE,
6094 		EXPENDITURE_ORGANIZATION_ID,
6095  	        PA_QUANTITY,
6096 		PA_CC_AR_INVOICE_ID,
6097 		PA_CC_AR_INVOICE_LINE_NUM,
6098 		PA_CC_PROCESSED_CODE,
6099 		AWARD_ID,
6100   		AWT_GROUP_ID,
6101 		REFERENCE_1,
6102 		REFERENCE_2,
6103 		RECEIPT_VERIFIED_FLAG,
6104 		RECEIPT_REQUIRED_FLAG,
6105 		RECEIPT_MISSING_FLAG,
6106 		JUSTIFICATION,
6107 		EXPENSE_GROUP,
6108 		START_EXPENSE_DATE,
6109 		END_EXPENSE_DATE,
6110 		RECEIPT_CURRENCY_CODE,
6111 		RECEIPT_CONVERSION_RATE,
6112 		RECEIPT_CURRENCY_AMOUNT,
6113 		DAILY_AMOUNT,
6114 		WEB_PARAMETER_ID,
6115 		ADJUSTMENT_REASON,
6116 		MERCHANT_DOCUMENT_NUMBER,
6117 		MERCHANT_NAME,
6118 		MERCHANT_REFERENCE,
6119 		MERCHANT_TAX_REG_NUMBER,
6120 		MERCHANT_TAXPAYER_ID,
6121 		COUNTRY_OF_SUPPLY,
6122 		CREDIT_CARD_TRX_ID,
6123 		COMPANY_PREPAID_INVOICE_ID,
6124 		CC_REVERSAL_FLAG,
6125 		ATTRIBUTE_CATEGORY,
6126 		ATTRIBUTE1,
6127       		ATTRIBUTE2,
6128       		ATTRIBUTE3,
6129       		ATTRIBUTE4,
6130       		ATTRIBUTE5,
6131       		ATTRIBUTE6,
6132       		ATTRIBUTE7,
6133       		ATTRIBUTE8,
6134       		ATTRIBUTE9,
6135       		ATTRIBUTE10,
6136       		ATTRIBUTE11,
6137       		ATTRIBUTE12,
6138       		ATTRIBUTE13,
6139       		ATTRIBUTE14,
6140       		ATTRIBUTE15,
6141 		/*OPEN ISSUE 1*/
6142    		/*GLOBAL_ATTRIBUTE_CATEGORY,
6143      		GLOBAL_ATTRIBUTE1,
6144       		GLOBAL_ATTRIBUTE2,
6145       		GLOBAL_ATTRIBUTE3,
6146       		GLOBAL_ATTRIBUTE4,
6147       		GLOBAL_ATTRIBUTE5,
6148       		GLOBAL_ATTRIBUTE6,
6149       		GLOBAL_ATTRIBUTE7,
6150        		GLOBAL_ATTRIBUTE8,
6151       		GLOBAL_ATTRIBUTE9,
6152        		GLOBAL_ATTRIBUTE10,
6153       		GLOBAL_ATTRIBUTE11,
6154       		GLOBAL_ATTRIBUTE12,
6155       		GLOBAL_ATTRIBUTE13,
6156       		GLOBAL_ATTRIBUTE14,
6157       		GLOBAL_ATTRIBUTE15,
6158       		GLOBAL_ATTRIBUTE16,
6159       		GLOBAL_ATTRIBUTE17,
6160       		GLOBAL_ATTRIBUTE18,
6161       		GLOBAL_ATTRIBUTE19,
6162       		GLOBAL_ATTRIBUTE20, */
6163       		CREATION_DATE,
6164       		CREATED_BY,
6165       		LAST_UPDATED_BY,
6166       		LAST_UPDATE_DATE,
6167       		LAST_UPDATE_LOGIN,
6168       		PROGRAM_APPLICATION_ID,
6169       		PROGRAM_ID,
6170       		PROGRAM_UPDATE_DATE,
6171       		REQUEST_ID,
6172 		RETAINED_AMOUNT,
6173 		RETAINED_AMOUNT_REMAINING,
6174 		--bugfix:5565310
6175 		SHIP_TO_LOCATION_ID,
6176 		PRIMARY_INTENDED_USE,
6177 		PRODUCT_FISC_CLASSIFICATION,
6178 		TRX_BUSINESS_CATEGORY,
6179 	        PRODUCT_TYPE,
6180 	        PRODUCT_CATEGORY,
6181 	        USER_DEFINED_FISC_CLASS,
6182 		ASSESSABLE_VALUE,
6183 		TAX_CLASSIFICATION_CODE,
6184 		PAY_AWT_GROUP_ID)
6185       VALUES( X_INVOICE_ID,			--invoice_id
6186        	      X_SHIPMENT_TABLE(i).line_number,  --line_number
6187               'ITEM',				--line_type_lookup_code
6188    	      NULL,				--requester_id
6189  	      X_SHIPMENT_TABLE(i).item_description,--description
6190  	      'HEADER MATCH',			--line_source
6191  	      G_ORG_ID,				--org_id
6192  	      X_SHIPMENT_TABLE(i).inventory_item_id,--inventory_item_id
6193  	      X_SHIPMENT_TABLE(i).item_description,--item_description
6194  	      NULL,				--serial_number
6195  	      NULL,				--manufacturer
6196  	      NULL,				--model_number
6197  	      'Y',				--generate_dists
6198               DECODE(X_MATCH_OPTION,'P',
6199 			(DECODE(X_SHIPMENT_TABLE(i).matching_basis,
6200 				'QUANTITY', 'ITEM_TO_PO', 'ITEM_TO_SERVICE_PO')),
6201 			(DECODE(X_SHIPMENT_TABLE(i).matching_basis,
6202 				'QUANTITY', 'ITEM_TO_RECEIPT', 'ITEM_TO_SERVICE_RECEIPT'))),  --match_type
6203  	      NULL,				--distribution_set_id
6204  	      NULL,				--account_segment
6205  	      NULL,				--balancing_segment
6206  	      NULL,				--cost_center_segment
6207  	      NULL,				--overlay_dist_code_concat
6208      	      NULL,				--default_dist_ccid
6209      	      'N',				--prorate_across_all_items
6210  	      NULL,				--line_group_number
6211  	      G_ACCOUNTING_DATE,		--accounting_date
6212  	      G_PERIOD_NAME,			--period_name
6213  	      'N',				--deferred_acctg_flag
6214  	      NULL,				--def_acctg_start_date
6215  	      NULL,				--def_acctg_end_date
6216  	      NULL,				--def_acctg_number_of_periods
6217  	      NULL,				--def_acctg_period_type
6218  	      G_SET_OF_BOOKS_ID,		--set_of_books_id
6219  	      X_SHIPMENT_TABLE(i).AMOUNT,	--amount
6220  	      X_SHIPMENT_TABLE(i).BASE_AMOUNT,  --base_amount
6221  	      X_SHIPMENT_TABLE(i).rounding_amt,	--rounding_amount
6222  	      X_SHIPMENT_TABLE(i).QUANTITY_INVOICED,--quantity_invoiced
6223  	      X_SHIPMENT_TABLE(i).UOM, 		--unit_meas_lookup_code
6224  	      X_SHIPMENT_TABLE(i).UNIT_PRICE,	--unit_price
6225  	      DECODE(G_APPROVAL_WORKFLOW_FLAG,'Y'
6226 	            ,'REQUIRED','NOT REQUIRED'),--wf_approval_status
6227            -- Removed for bug 4277744
6228  	   -- X_SHIPMENT_TABLE(i).USSGL_TRANSACTION_CODE,--ussgl_transaction_code
6229  	      'N',				--discarded_flag
6230  	      NULL,				--original_amount
6231  	      NULL,				--original_base_amount
6232  	      NULL,				--original_rounding_amt
6233  	      'N',				--cancelled_flag
6234  	      G_INCOME_TAX_REGION,		--income_tax_region
6235  	      X_SHIPMENT_TABLE(i).TYPE_1099,	--type_1099
6236  	      NULL,				--stat_amount
6237  	      NULL,				--prepay_invoice_id
6238  	      NULL,				--prepay_line_number
6239  	      NULL,				--invoice_includes_prepay_flag
6240  	      NULL,				--corrected_inv_id
6241  	      NULL,				--corrected_line_number
6242  	      X_SHIPMENT_TABLE(i).PO_HEADER_ID,	--po_header_id
6243  	      X_SHIPMENT_TABLE(i).PO_LINE_ID,	--po_line_id
6244  	      X_SHIPMENT_TABLE(i).PO_RELEASE_ID,--po_release_id
6245  	      X_SHIPMENT_TABLE(i).PO_LINE_LOCATION_ID,--po_line_location_id
6246  	      NULL,			        --po_distribution_id
6247     	      DECODE(X_MATCH_OPTION,'P',NULL,
6248     	             X_SHIPMENT_TABLE(i).RCV_TRANSACTION_ID),	--rcv_transaction_id
6249    	      NULL,				--final_match_flag
6250     	      'N',				--assets_tracking_flag
6251     	      G_ASSET_BOOK_TYPE_CODE,		--asset_book_type_code
6252     	      X_SHIPMENT_TABLE(i).ASSET_CATEGORY_ID,--asset_category_id
6253     	      NULL,				--project_id
6254    	      NULL,				--task_id
6255  	      NULL,				--expenditure_type
6256  	      NULL,	 			--expenditure_item_date
6257  	      NULL,				--expenditure_organization_id
6258  	      NULL,				--pa_quantity
6259  	      NULL,				--pa_cc_ar_invoice_id
6260  	      NULL,				--pa_cc_ar_invoice_line_num
6261  	      NULL,				--pa_cc_processed_code
6262  	      NULL,				--award_id
6263  	      G_AWT_GROUP_ID,			--awt_group_id
6264  	      NULL,    				--reference_1
6265  	      NULL,				--reference_2
6266  	      NULL,				--receipt_verified_flag
6267  	      NULL,				--receipt_required_flag
6268  	      NULL,				--receipt_missing_flag
6269  	      NULL,				--justification
6270  	      NULL,				--expense_group
6271  	      NULL,				--start_expense_date
6272  	      NULL,				--end_expense_date
6273  	      NULL,				--receipt_currency_amount
6274  	      NULL,				--receipt_conversion_rate
6275  	      NULL,				--receipt_currency_amount
6276  	      NULL,				--daily_amount
6277  	      NULL,				--web_parameter_id
6278  	      NULL,				--adjustment_reason
6279  	      NULL,				--merchant_document_number
6280  	      NULL,				--merchant_name
6281  	      NULL,				--merchant_reference
6282  	      NULL,				--merchant_tax_reg_number
6283  	      NULL,				--merchant_taxpayer_id
6284  	      NULL,				--country_of_supply
6285  	      NULL,				--credit_card_trx_id
6286  	      NULL,				--company_prepaid_invoice_id
6287  	      NULL,				--cc_reversal_flag
6288  	      X_SHIPMENT_TABLE(i).ATTRIBUTE_CATEGORY,--attribute_category
6289  	      X_SHIPMENT_TABLE(i).ATTRIBUTE1,	--attribute1
6290  	      X_SHIPMENT_TABLE(i).ATTRIBUTE2,	--attribute2
6291  	      X_SHIPMENT_TABLE(i).ATTRIBUTE3,	--attribute3
6292  	      X_SHIPMENT_TABLE(i).ATTRIBUTE4,	--attribute4
6293  	      X_SHIPMENT_TABLE(i).ATTRIBUTE5,	--attribute5
6294  	      X_SHIPMENT_TABLE(i).ATTRIBUTE6,	--attribute6
6295  	      X_SHIPMENT_TABLE(i).ATTRIBUTE7,	--attribute7
6296  	      X_SHIPMENT_TABLE(i).ATTRIBUTE8,	--attribute8
6297  	      X_SHIPMENT_TABLE(i).ATTRIBUTE9,	--attribute9
6298  	      X_SHIPMENT_TABLE(i).ATTRIBUTE10,	--attribute10
6299  	      X_SHIPMENT_TABLE(i).ATTRIBUTE11,	--attribute11
6300  	      X_SHIPMENT_TABLE(i).ATTRIBUTE12,	--attribute12
6301  	      X_SHIPMENT_TABLE(i).ATTRIBUTE13,	--attribute13
6302  	      X_SHIPMENT_TABLE(i).ATTRIBUTE14,	--attribute14
6303  	      X_SHIPMENT_TABLE(i).ATTRIBUTE15,	--attribute15
6304  	      /*X_GLOBAL_ATTRIBUTE_CATEGORY,	--global_attribute_category
6305 	      X_GLOBAL_ATTRIBUTE1,		--global_attribute1
6306       	      X_GLOBAL_ATTRIBUTE2,		--global_attribute2
6307 	      X_GLOBAL_ATTRIBUTE3,		--global_attribute3
6308       	      X_GLOBAL_ATTRIBUTE4,		--global_attribute4
6309       	      X_GLOBAL_ATTRIBUTE5,		--global_attribute5
6310       	      X_GLOBAL_ATTRIBUTE6,		--global_attribute6
6311       	      X_GLOBAL_ATTRIBUTE7,		--global_attribute7
6312        	      X_GLOBAL_ATTRIBUTE8,		--global_attribute8
6313       	      X_GLOBAL_ATTRIBUTE9,		--global_attribute9
6314        	      X_GLOBAL_ATTRIBUTE10,		--global_attribute10
6315       	      X_GLOBAL_ATTRIBUTE11,		--global_attribute11
6316       	      X_GLOBAL_ATTRIBUTE12,		--global_attribute12
6317       	      X_GLOBAL_ATTRIBUTE13,		--global_attribute13
6318       	      X_GLOBAL_ATTRIBUTE14,		--global_attribute14
6319       	      X_GLOBAL_ATTRIBUTE15,		--global_attribute15
6320       	      X_GLOBAL_ATTRIBUTE16,		--global_attribute16
6321       	      X_GLOBAL_ATTRIBUTE17,		--global_attribute17
6322       	      X_GLOBAL_ATTRIBUTE18,		--global_attribute18
6323       	      X_GLOBAL_ATTRIBUTE19,		--global_attribute19
6324       	      X_GLOBAL_ATTRIBUTE20, 	--global_attribute20*/
6325       	      SYSDATE,				--creation_date
6326       	      G_USER_ID,			--created_by
6327       	      G_USER_ID,			--last_update_by
6328       	      SYSDATE,				--last_update_date
6329       	      G_LOGIN_ID,			--last_update_login
6330       	      NULL,				--program_application_id
6331 	      NULL,				--program_id
6332       	      NULL,				--program_update_date
6333       	      NULL,  	      		       	--request_id
6334 	      X_SHIPMENT_TABLE(i).retained_amount,	-- retained_amount
6335 	      (-X_SHIPMENT_TABLE(i).retained_amount),	-- retained_amount_remaining
6336 	      --bugfix:5565310
6337 	      X_SHIPMENT_TABLE(i).SHIP_TO_LOCATION_ID,         --ship_to_location_id
6338 	      X_SHIPMENT_TABLE(i).PRIMARY_INTENDED_USE,        --primary_intended_use
6339 	      X_SHIPMENT_TABLE(i).PRODUCT_FISC_CLASSIFICATION, --product_fisc_classification
6340 	      G_TRX_BUSINESS_CATEGORY          --trx_business_category
6341 	      ,X_SHIPMENT_TABLE(i).PRODUCT_TYPE,                --product_type
6342 	      X_SHIPMENT_TABLE(i).PRODUCT_CATEGORY,            --product_category
6343 	      X_SHIPMENT_TABLE(i).USER_DEFINED_FISC_CLASS,      --user_defined_fisc_class
6344 	       --Introduced DECODE for bug#13505998
6345 	      DECODE(X_SHIPMENT_TABLE(i).AMOUNT,l_full_mtch_amt,X_SHIPMENT_TABLE(i).ASSESSABLE_VALUE,NULL),
6346 	      X_SHIPMENT_TABLE(i).TAX_CLASSIFICATION_CODE,
6347 		  G_PAY_AWT_GROUP_ID			--pay_awt_group_id   bug8222382
6348       	      );
6349 
6350       END IF;
6351 
6352    END LOOP;
6353 
6354    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
6355          FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_MATCHING_PKG.Generate_Lines_For_QuickMatch(-)');
6356    END IF;
6357 
6358 
6359 
6360 EXCEPTION
6361   WHEN others then
6362      If (SQLCODE <> -20001) Then
6363         fnd_message.set_name('SQLAP','AP_DEBUG');
6364         fnd_message.set_token('ERROR',SQLERRM);
6365         fnd_message.set_token('CALLING_SEQUENCE',current_calling_sequence);
6366         fnd_message.set_token('PARAMETERS',
6367                   ' invoice_id = '||to_char(x_invoice_id)
6368                 ||', match option = '||x_match_option);
6369         fnd_message.set_token('DEBUG_INFO',l_debug_info);
6370      End if;
6371 
6372   app_exception.raise_exception;
6373 
6374 END Generate_Lines_For_QuickMatch;
6375 
6376 Procedure Generate_Release_Lines (p_po_header_id     IN NUMBER,
6377          	                  p_invoice_id       IN NUMBER,
6378 		                  p_release_amount   IN NUMBER,
6379 				  x_calling_sequence IN VARCHAR2) Is
6380 
6381         Cursor c_line_locations (c_po_header_id NUMBER) IS
6382         Select pll.*
6383           From po_line_locations_all pll
6384          Where pll.po_header_id = c_po_header_id
6385            And nvl(retainage_withheld_amount,0) - nvl(retainage_released_amount,0) <> 0;
6386 
6387         l_line_locations c_line_locations%rowtype;
6388 
6389         i                               number:=1;
6390         l_release_amount_rtot           number;
6391 	l_shipment_release_amount	number;
6392         l_release_amount_remaining      number;
6393 
6394         l_release_shipment_tab  ap_retainage_release_pkg.release_shipments_tab;
6395 
6396 	l_debug_info		 VARCHAR2(2000);
6397 	current_calling_sequence VARCHAR2(2000);
6398 	l_api_name		 VARCHAR2(50);
6399 
6400 BEGIN
6401 
6402     l_api_name               := 'Generate_Release_Lines';
6403     current_calling_sequence := 'Generate_Release_Lines<-'||x_calling_sequence;
6404 
6405     l_debug_info := 'Generate_Release_Lines (+)';
6406     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6407         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6408     END IF;
6409 
6410     l_release_amount_rtot := p_release_amount;
6411 
6412     Open  c_line_locations (p_po_header_id);
6413     Loop
6414         Fetch c_line_locations
6415          Into l_line_locations;
6416 
6417         Exit When c_line_locations%notfound OR
6418                   l_release_amount_rtot = 0;
6419 
6420         l_release_amount_remaining := nvl(l_line_locations.retainage_withheld_amount,0) -
6421                                       nvl(l_line_locations.retainage_released_amount,0);
6422 
6423 	If l_release_amount_rtot > l_release_amount_remaining Then
6424            l_shipment_release_amount := l_release_amount_remaining;
6425         Else
6426 	   l_shipment_release_amount := l_release_amount_rtot;
6427 	End If;
6428 
6429         l_release_shipment_tab(i).po_header_id             := l_line_locations.po_header_id;
6430         l_release_shipment_tab(i).po_line_id               := l_line_locations.po_line_id;
6431         l_release_shipment_tab(i).po_release_id            := l_line_locations.po_release_id;
6432         l_release_shipment_tab(i).line_location_id         := l_line_locations.line_location_id;
6433         l_release_shipment_tab(i).release_amount           := l_shipment_release_amount;
6434         l_release_shipment_tab(i).release_amount_remaining := l_release_amount_remaining;
6435 
6436         l_debug_info := 'Call Release API: '||
6437 	                        'line_location_id: ' || l_line_locations.line_location_id||
6438 				'release_amount: '   || l_shipment_release_amount;
6439         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6440             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6441         END IF;
6442 
6443         ap_retainage_release_pkg.create_release
6444                                 (x_invoice_id            => p_invoice_id,
6445                                  x_release_shipments_tab => l_release_shipment_tab);
6446 
6447         l_release_amount_rtot := l_release_amount_rtot - l_shipment_release_amount;
6448         i:=i+1;
6449 
6450     End Loop;
6451     Close c_line_locations;
6452 
6453     l_debug_info := 'Generate_Release_Lines (-)';
6454     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6455         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6456     END IF;
6457 
6458 EXCEPTION
6459   WHEN others then
6460      If (SQLCODE <> -20001) Then
6461         fnd_message.set_name('SQLAP','AP_DEBUG');
6462         fnd_message.set_token('ERROR',SQLERRM);
6463         fnd_message.set_token('CALLING_SEQUENCE',current_calling_sequence);
6464         fnd_message.set_token('PARAMETERS',
6465                   ' invoice_id = '||to_char(p_invoice_id));
6466         fnd_message.set_token('DEBUG_INFO',l_debug_info);
6467      End if;
6468 
6469   app_exception.raise_exception;
6470 
6471 End Generate_Release_Lines;
6472 
6473 -- Bug 5465722. Building Prepay Proper Account as per Federal Functionality.
6474 -- Only Natural Account will be overlayed
6475 PROCEDURE  Build_Prepay_Account(P_base_ccid             IN     NUMBER
6476                               ,P_overlay_ccid          IN     NUMBER
6477                               ,P_accounting_date       IN     DATE
6478                               ,P_result_ccid           OUT NOCOPY    NUMBER
6479                               ,P_Reason_Unbuilt_Flex   OUT NOCOPY    VARCHAR2
6480                               ,P_calling_sequence      IN     VARCHAR2 ) IS
6481 
6482   l_base_segments                FND_FLEX_EXT.SEGMENTARRAY ;
6483   l_overlay_segments             FND_FLEX_EXT.SEGMENTARRAY ;
6484   l_segments                     FND_FLEX_EXT.SEGMENTARRAY ;
6485   l_num_of_segments              NUMBER ;
6486   l_result                       BOOLEAN ;
6487   l_curr_calling_sequence        VARCHAR2(2000);
6488   G_flex_qualifier_name          VARCHAR2(100);
6489   l_primary_sob_id               AP_SYSTEM_PARAMETERS.set_of_books_id%TYPE;
6490   l_liability_post_lookup_code   AP_SYSTEM_PARAMETERS.liability_post_lookup_code%TYPE;
6491   l_chart_of_accts_id            GL_SETS_OF_BOOKS.chart_of_accounts_id%TYPE;
6492   G_flex_segment_num             NUMBER;
6493   l_api_name                     CONSTANT VARCHAR2(200) := 'Build_Prepay_Account';
6494   l_debug_info                   VARCHAR2(2000);
6495 
6496 
6497 BEGIN
6498 
6499    l_curr_calling_sequence := 'Ap_Matching_Pkg.Build_Prepay_Account<-'
6500                              || P_calling_sequence;
6501    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6502       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_curr_calling_sequence);
6503    END IF;
6504 
6505 
6506    SELECT set_of_books_id
6507    INTO   l_primary_sob_id
6508    FROM   ap_system_parameters;
6509 
6510    SELECT chart_of_accounts_id
6511    INTO   l_chart_of_accts_id
6512    FROM   gl_sets_of_books
6513    WHERE  set_of_books_id = l_primary_sob_id;
6514 
6515 
6516    G_flex_qualifier_name := 'GL_ACCOUNT' ;
6517 
6518 
6519    l_result := FND_FLEX_APIS.GET_QUALIFIER_SEGNUM(
6520                                  101, 'GL#',
6521                                  l_chart_of_accts_id,
6522                                  G_flex_qualifier_name,
6523                                  G_flex_segment_num);
6524 
6525    l_debug_info := 'G_Flex_Segment_Num: '||G_Flex_Segment_Num;
6526    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6527      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6528    END IF;
6529 
6530   -- Get the segments of the two given accounts
6531    IF (NOT FND_FLEX_EXT.GET_SEGMENTS('SQLGL', 'GL#',
6532                                     l_chart_of_accts_id,
6533                                     P_base_ccid, l_num_of_segments,
6534                                     l_base_segments)
6535      ) THEN
6536       -- Print reason why flex failed
6537      P_result_ccid := -1;
6538      P_reason_unbuilt_flex := 'INVALID ACCOUNT';
6539 
6540      l_debug_info := 'Charge Account is Invalid -> '||FND_MESSAGE.GET;
6541      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6542        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6543      END IF;
6544 
6545      RETURN ;
6546 
6547    END IF;
6548 
6549    IF (NOT FND_FLEX_EXT.GET_SEGMENTS('SQLGL', 'GL#',
6550                                     l_chart_of_accts_id,
6551                                     P_overlay_ccid, l_num_of_segments,
6552                                     l_overlay_segments)
6553      ) THEN
6554      -- Print reason why flex failed
6555      P_result_ccid := -1;
6556      P_reason_unbuilt_flex := 'INVALID ACCOUNT';
6557 
6558      l_debug_info := 'Overlay Account is Invalid -> '||FND_MESSAGE.GET;
6559      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6560        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6561      END IF;
6562 
6563      RETURN ;
6564 
6565    END IF;
6566 
6567    /*
6568     Account Segment Overlay
6569     Base      A    A    [A]  A
6570     Overlay   B    B    [B]  B
6571     Result    A    A    [B]  A
6572 
6573    */
6574 
6575    FOR i IN 1.. l_num_of_segments LOOP
6576 
6577      l_debug_info := 'Overlaying Account Segment -> '||to_char(i);
6578      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6579        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6580      END IF;
6581 
6582      --  Account segment overlay
6583      IF (i = G_flex_segment_num) THEN
6584        l_segments(i) := l_overlay_segments(i);
6585      ELSE
6586        l_segments(i) := l_base_segments(i);
6587      END IF;
6588 
6589    END LOOP;
6590 
6591    -- Get ccid fOR overlayed segments
6592    l_result := FND_FLEX_EXT.GET_COMBINATION_ID('SQLGL', 'GL#',
6593                                    l_chart_of_accts_id,
6594                                    P_accounting_date, l_num_of_segments,
6595                                    l_segments, P_result_ccid) ;
6596 
6597    IF (NOT l_result) THEN
6598 
6599      -- Store reason why flex failed
6600      P_result_ccid := -1;
6601      P_reason_unbuilt_flex := 'INVALID ACCOUNT';
6602 
6603      l_debug_info := 'Account Based on Overlayed Segments can not be build -> '||FND_MESSAGE.GET;
6604      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6605        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6606      END IF;
6607 
6608    END IF;
6609 
6610  EXCEPTION
6611    WHEN OTHERS THEN
6612      IF (SQLCODE <> -20001) THEN
6613        FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
6614        FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
6615        FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
6616        FND_MESSAGE.SET_TOKEN('PARAMETERS','Base CCID = '||to_char(p_base_ccid)
6617                           ||', Overlay CCID = '||to_char(p_overlay_ccid));
6618        FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
6619 
6620        l_debug_info := 'Exception occured in Building Prepay Account> '||FND_MESSAGE.GET;
6621        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6622          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6623        END IF;
6624      END IF;
6625      APP_EXCEPTION.RAISE_EXCEPTION;
6626 
6627  END Build_Prepay_Account;
6628 
6629 
6630 END AP_MATCHING_PKG;