DBA Data[Home] [Help]

PACKAGE BODY: APPS.JE_ES_MODELO_EXT_PKG

Source


1 PACKAGE BODY je_es_modelo_ext_pkg
2 -- $Header: jeesmodeloextb.pls 120.47.12020000.13 2013/04/12 09:00:07 abhijsar ship $
3 AS
4 
5   G_LE_TRN         VARCHAR2(150);
6   G_LE_NAME        VARCHAR2(150);
7   G_CURRENCY_CODE  VARCHAR2(150);
8   G_CUR_PRECISION  NUMBER;
9   G_FROM_DATE      DATE;
10   G_TO_DATE        DATE;
11 
12   G_DEBUG         BOOLEAN := TRUE;
13   G_LINENO        VARCHAR2 (20) ;  -- DEBUG LINE NO
14 
15   PROCEDURE get_vendor_address ( p_party_site_id   IN         NUMBER
16                                 , x_postal_code    OUT NOCOPY VARCHAR2
17                                 , x_city           OUT NOCOPY VARCHAR2
18                                 , x_address_detail OUT NOCOPY VARCHAR2
19                                 , x_country        OUT NOCOPY VARCHAR2
20                                 )
21   IS
22 
23   /** author: brathod
24       Modified following cursor to refer ap_supplier_sites_all to retrieve supplier address.
25       Cursor was wrongly refering to hz_party_sites_all and hz_locations as
26       jg_zz_vat_trx_details.billing_tp_address_id (value passed in parameter p_party_site_id) is actually a
27       supplier_site_id and not party_site_id.  Please refer bug#5031773
28  */
29   CURSOR c_get_address IS
30      SELECT DECODE(p_modelo,'415',DECODE(SUBSTR(assa.country, 1, 25)
31                   , 'ES',SUBSTR(assa.zip,1,5)
32                   , '99'||FT.eu_code)
33 		  , DECODE(assa.country
34                   , 'ES',SUBSTR(assa.zip,1,2)||'   '
35                   , '99'||FT.territory_code||' ') ) POSTCODE
36           , SUBSTR(assa.city,1,25)                              CITY
37           , SUBSTR(assa.address_line1,1,35)||' '||
38             SUBSTR(assa.address_line2,1,35)||' '||
39             SUBSTR(assa.address_line3,1,35)              ADDRESS_DETAIL
40           , assa.country
41      FROM  ap_supplier_sites_all assa
42           , fnd_territories    FT
43      WHERE  assa.vendor_site_id     = p_party_site_id
44      AND    assa.country(+)        = FT.territory_code;
45 
46   BEGIN
47     OPEN   c_get_address ;
48     FETCH  c_get_address INTO x_postal_code, x_city, x_address_detail, x_country;
49     CLOSE  c_get_address ;
50   END;
51 
52 
53 -- Modelo 347
54 
55 	PROCEDURE AP347_GROSS_AMT_QTR ( p_tax_registration_num IN VARCHAR2
56                                        , p_customer_name IN VARCHAR2
57                                        , p_customer_address_id IN NUMBER
58                                        , p_vendor_id      IN NUMBER
59                                        , p_property_flag  IN VARCHAR2
60                                        , p_tipo           IN VARCHAR2
61                                        , x_amount_qtr1    OUT NOCOPY NUMBER
62                                        , x_amount_qtr2    OUT NOCOPY NUMBER
63                                        , x_amount_qtr3    OUT NOCOPY NUMBER
64                                        , x_amount_qtr4    OUT NOCOPY NUMBER       ) IS
65 
66 
67         Cursor Gross_Amt_Qtr is
68 	select             SUM(DECODE(JZVTD.OFFSET_FLAG,
69 	                               'N',nvl(JZVTD.taxable_amt_funcl_curr,JZVTD.taxable_amt)*(JZVTD.tax_recovery_rate/100)
70 				       ,0))
71 			+
72 			   SUM(DECODE(NVL(JZVTD.tax_amt_funcl_curr,0)
73 			                                , 0, JZVTD.tax_amt,
74 	                                    JZVTD.tax_amt_funcl_curr)) 	SUM_TAXABLE_AMT_QTR,
75 	    decode(to_char(JZVTD.gl_date,'MM'),'01','1T','02','1T','03','1T','04','2T','05','2T','06','2T','07','3T','08','3T','09','3T','10','4T','11','4T','12','4T')   QUARTER_NUM
76 
77 	FROM     jg_zz_vat_rep_status    JZVRS
78 	         ,   jg_zz_vat_trx_details   JZVTD
79 	         , AP_SUPPLIER_SITES_ALL APSS, AP_SUPPLIERS APS, ZX_LINES_DET_FACTORS ZXDF ,AP_INVOICES_ALL API
80 	WHERE    JZVRS.vat_reporting_entity_id  = P_VAT_REP_ENTITY_ID
81 	AND     JZVTD.reporting_status_id in (SELECT DISTINCT JZRS.reporting_status_id JZRS
82 					     FROM jg_zz_vat_rep_status JZRS
83 					     WHERE JZRS.vat_reporting_entity_id = P_VAT_REP_ENTITY_ID
84 					     AND   JZRS.source = 'AP')
85 	AND      JZVTD.extract_source_ledger    ='AP'
86 	AND      JZVRS.source                   = 'AP'
87 	AND      JZVTD.gl_date BETWEEN JZVRS.period_start_date and JZVRS.period_end_date
88 	AND      JZVRS.TAX_CALENDAR_YEAR =  P_TAX_YEAR
89 	AND    JZVTD.trx_line_type                   <>'AWT'
90 	AND    JZVTD.trx_line_class                  <>  'EXPENSE REPORTS'
91 	AND    JZVTD.applied_from_line_id         IS NULL
92 	AND    SUBSTR(NVL(JZVTD.tax_rate_vat_trx_type_code,'QQQQQQ'),1,3) <> 'RET'
93 	AND    SUBSTR(zxdf.trx_business_category, INSTR(zxdf.trx_business_category,'MOD')+3,
94 	              DECODE(INSTR(zxdf.trx_business_category,'/',1,3),0,length(zxdf.trx_business_category),
95 	                 INSTR(zxdf.trx_business_category,'/',1,3) - (INSTR(zxdf.trx_business_category,'MOD')+3)))
96 	                 IN ( '347', '415_347', '347PR', '415_347PR')
97 	AND     DECODE( INSTR(ZXDF.trx_business_category,'/',1,3),0,'A',
98 	         substr(ZXDF.trx_business_category,length(ZXDF.trx_business_category),1)) = p_tipo
99 	AND     DECODE( INSTR(NVL(ZXDF.trx_business_category,'N'),'PR'), 0, 'N', 'Y') = p_property_flag
100 	AND    JZVTD.trx_id = API.invoice_id
101 	AND    JZVTD.trx_id = ZXDF.trx_id
102 	AND    JZVTD.trx_line_id = ZXDF.trx_line_id
103 	AND   JZVTD.BILLING_TRADING_PARTNER_ID       =   APS.VENDOR_ID
104         AND   nvl(JZVTD.bill_from_party_id,1)  = nvl(p_vendor_id,1)
105         AND   nvl(JZVTD.billing_tp_taxpayer_id,1) = nvl(p_tax_registration_num,1)
106         AND   nvl(substr(JZVTD.billing_tp_name,1,80),1) = nvl(p_customer_name,1)
107         AND   APSS.vendor_site_id = p_customer_address_id
108 	AND   APS.VENDOR_ID                          =  APSS.VENDOR_ID
109 	AND   NVL(APS.FEDERAL_REPORTABLE_FLAG,'Y')  =  'Y'
110 	AND   APSS.TAX_REPORTING_SITE_FLAG            =  'Y'
111 	AND   APSS.ORG_ID                             = P_ORG_ID
112 	GROUP BY   decode(to_char(JZVTD.gl_date,'MM'),'01','1T','02','1T','03','1T','04','2T','05','2T','06','2T','07','3T','08','3T','09','3T','10','4T','11','4T','12','4T')
113 
114         UNION
115 
116         SELECT
117 	SUM (ROUND (DECODE (JZVTD.taxable_amt_funcl_curr,
118 	                          0, JZVTD.taxable_amt,
119 	                          NULL, JZVTD.taxable_amt,
120 	                          JZVTD.taxable_amt_funcl_curr)
121 	                ))            sum_taxable_amt_qtr,
122 	  decode(to_char(JZVTD.gl_date,'MM'),'01','1T','02','1T','03','1T','04','2T','05','2T','06','2T','07','3T','08','3T','09','3T','10','4T','11','4T','12','4T')   QUARTER_NUM
123 
124 	FROM     jg_zz_vat_rep_status    JZVRS
125 	                  ,   jg_zz_vat_trx_details   JZVTD
126 	                  ,   ap_suppliers            aps
127 	                  ,   ap_supplier_sites_all   apss
128 	                  ,   zx_lines_det_factors    zxdf
129 	                  ,   ap_invoices_all         api
130 
131 	 WHERE    JZVRS.vat_reporting_entity_id  = P_VAT_REP_ENTITY_ID
132          AND      JZVRS.reporting_status_id      = JZVTD.reporting_status_id
133 	 AND      JZVRS.source                   = 'AP'
134 	 AND      JZVTD.extract_source_ledger    =  'AP'
135 	 AND      JZVTD.merchant_party_name      IS NOT NULL
136 	 AND      JZVTD.trx_line_type                NOT IN ('AWT','TAX','PREPAY')
137 	 AND      JZVTD.trx_line_class = 'EXPENSE REPORTS'
138 	 AND      JZVTD.applied_from_line_id         IS NULL
139 	 AND      JZVRS.tax_calendar_year = P_TAX_YEAR
140 	 AND       JZVTD.gl_date BETWEEN JZVRS.period_start_date and JZVRS.period_end_date
141 	 AND      SUBSTR(zxdf.trx_business_category, INSTR(zxdf.trx_business_category,'MOD')+3,
142 	          DECODE(INSTR(zxdf.trx_business_category,'/',1,3),0,length(zxdf.trx_business_category),
143 	          INSTR(zxdf.trx_business_category,'/',1,3) - (INSTR(zxdf.trx_business_category,'MOD')+3)))
144 	          IN ( '347', '347PR', '415_347', '415_347PR')
145 	 AND     DECODE( INSTR(ZXDF.trx_business_category,'/',1,3),0,'A',
146 	         substr(ZXDF.trx_business_category,length(ZXDF.trx_business_category),1)) = p_tipo
147 	 AND     DECODE( INSTR(NVL(ZXDF.trx_business_category,'N'),'PR'), 0, 'N', 'Y') = p_property_flag
148 	 AND      jzvtd.trx_id = zxdf.trx_id
149          AND      JZVTD.trx_id = API.invoice_id
150 	 AND      jzvtd.trx_line_id = zxdf.trx_line_id
151 	 AND      JZVTD.BILLING_TRADING_PARTNER_ID       =   APS.VENDOR_ID
152          AND     nvl(JZVTD.bill_from_party_id,1)  = nvl(p_vendor_id,1)
153          AND     NVL(JZVTD.merchant_party_name,nvl(JZVTD.billing_tp_name,1)) = nvl(p_customer_name,1)
154          AND     NVL(JZVTD.merchant_party_taxpayer_id,nvl(JZVTD.billing_tp_taxpayer_id,1)) = nvl(p_tax_registration_num,1)
155          AND     APSS.vendor_site_id = p_customer_address_id
156 	 AND      APS.VENDOR_ID         =   APSS.VENDOR_ID
157 	 AND      NVL(APS.FEDERAL_REPORTABLE_FLAG ,'Y')   = 'Y'
158 	 AND      APSS.TAX_REPORTING_SITE_FLAG   = 'Y'
159 	 AND      APSS.ORG_ID          = P_ORG_ID
160 	group by   decode(to_char(JZVTD.gl_date,'MM'),'01','1T','02','1T','03','1T','04','2T','05','2T','06','2T','07','3T','08','3T','09','3T','10','4T','11','4T','12','4T');
161 
162         Cursor prepay_applied_Qtr  is
163 	 SELECT  SUM(ROUND(DECODE( NVL(JZVTD.taxable_amt_funcl_curr,0)
164 	                              ,0 , JZVTD.taxable_amt
165 	                              ,    JZVTD.taxable_amt_funcl_curr
166 	                              )
167 	                 , g_cur_precision)
168 	                )   prepay_applied_Qtr,
169 	 decode(to_char(JZVTD.gl_date,'MM'),'01','1T','02','1T','03','1T','04','2T','05','2T','06','2T','07','3T','08','3T','09','3T','10','4T','11','4T','12','4T')   QUARTER_NUM
170 	     FROM    jg_zz_vat_trx_details      JZVTD
171 	            ,jg_zz_vat_rep_status       JZVRS
172 	            ,ap_invoice_distributions   AID
173 	            ,ap_invoice_distributions   PRE
174 	            ,hz_parties                 HP
175 	            ,ap_invoices_all            API
176 		    ,zx_lines_det_factors       ZXDF
177 
178 	     WHERE   JZVRS.reporting_status_id        = JZVTD.reporting_status_id
179 	     AND     JZVTD.billing_trading_partner_id = p_vendor_id
180 	     AND     JZVTD.trx_type_mng               = 'PREPAYMENT'
181 	     AND     AID.invoice_Id                    =  JZVTD.trx_id
182 	     AND     PRE.invoice_distribution_id      =  AID.invoice_distribution_id
183 	     AND     PRE.posted_flag                  IN ('P','Y')
184 	     AND     PRE.line_type_lookup_code        <> 'AWT'
185 	     AND     HP.party_id                      =  JZVTD.billing_trading_partner_id
186 	     AND     NVL(hp.party_type, 'XXX')        <> 'EMPLOYEE'
187 	     AND     DECODE( INSTR(NVL(ZXDF.trx_business_category,'N'),'PR'), 0, 'N', 'Y') = p_property_flag
188 	     AND     SUBSTR(ZXDF.trx_business_category, INSTR(ZXDF.trx_business_category,'MOD')+3,
189 	               DECODE(INSTR(ZXDF.trx_business_category,'/',1,3),0,length(ZXDF.trx_business_category),
190 	                 INSTR(ZXDF.trx_business_category,'/',1,3) - (INSTR(ZXDF.trx_business_category,'MOD')+3)))
191 	             IN ( '347', '347PR', '415_347', '415_347PR')
192 	     AND     DECODE( INSTR(ZXDF.trx_business_category,'/',1,3),0,'A',
193 	         substr(ZXDF.trx_business_category,length(ZXDF.trx_business_category),1)) = p_tipo
194 	     AND     JZVTD.trx_id = API.invoice_id
195 	     AND     jzvtd.trx_id = zxdf.trx_id
196 	     AND     jzvtd.trx_line_id = zxdf.trx_line_id
197 	     AND     JZVRS.vat_reporting_entity_id = P_VAT_REP_ENTITY_ID
198              AND     JZVTD.gl_date BETWEEN JZVRS.period_start_date and JZVRS.period_end_date
199 	     AND     JZVRS.tax_calendar_year       = P_TAX_YEAR
200 	   Group by decode(to_char(JZVTD.gl_date,'MM'),'01','1T','02','1T','03','1T','04','2T','05','2T','06','2T','07','3T','08','3T','09','3T','10','4T','11','4T','12','4T');
201 
202 
203 
204        BEGIN
205 
206 
207        	for  qtly_gross_amt_rec in gross_amt_qtr  loop
208 
209 	if qtly_gross_amt_rec.quarter_num = '1T' then
210 	x_amount_qtr1 :=  qtly_gross_amt_rec.sum_taxable_amt_qtr;
211 
212 
213 	elsif qtly_gross_amt_rec.quarter_num = '2T' then
214 	x_amount_qtr2 :=  qtly_gross_amt_rec.sum_taxable_amt_qtr;
215 
216 
217 	elsif qtly_gross_amt_rec.quarter_num = '3T' then
218 	x_amount_qtr3 :=  qtly_gross_amt_rec.sum_taxable_amt_qtr;
219 
220 
221 	elsif qtly_gross_amt_rec.quarter_num = '4T' then
222 	x_amount_qtr4 :=  qtly_gross_amt_rec.sum_taxable_amt_qtr;
223 
224 	end if;
225 
226        	end loop; --qtly gross amounts
227 
228         for prepay_applied_qtr_rec in prepay_applied_qtr loop
229 
230 	if prepay_applied_qtr_rec.quarter_num = '1T' then
231 
232 	x_amount_qtr1     := x_amount_qtr1 - nvl(prepay_applied_qtr_rec.prepay_applied_qtr,0);
233 
234 	elsif  prepay_applied_qtr_rec.quarter_num = '2T' then
235 
236 	x_amount_qtr2     := x_amount_qtr2 - nvl(prepay_applied_qtr_rec.prepay_applied_qtr,0) ;
237 
238 	elsif  prepay_applied_qtr_rec.quarter_num = '3T' then
239 
240 	x_amount_qtr3     := x_amount_qtr3 - nvl(prepay_applied_qtr_rec.prepay_applied_qtr,0);
241 
242 	elsif  prepay_applied_qtr_rec.quarter_num = '4T' then
243 
244 	x_amount_qtr4     := x_amount_qtr4 -  nvl(prepay_applied_qtr_rec.prepay_applied_qtr,0);
245 
246 	end if;
247 
248 	end loop; --qtly prepay applied amounts
249 
250 
251 
252        END;
253 
254 
255 -- Modelo 347
256 
257 	Procedure AR347_QTR_AMTS(p_customer_id   IN NUMBER
258 	                          , p_tipo        IN VARCHAR2
259                                   , p_year_of_amts_rec_cash IN NUMBER
260 	                          , x_sum_trx_line_amt_q1 OUT NOCOPY NUMBER
261 	                          , x_sum_trx_line_amt_q2 OUT NOCOPY NUMBER
262 	                          , x_sum_trx_line_amt_q3 OUT NOCOPY NUMBER
263 	                          , x_sum_trx_line_amt_q4 OUT NOCOPY NUMBER
264                                   , x_arrenda_amt_q1      OUT NOCOPY NUMBER
265                                   , x_arrenda_amt_q2      OUT NOCOPY NUMBER
266                                   , x_arrenda_amt_q3      OUT NOCOPY NUMBER
267                                   , x_arrenda_amt_q4      OUT NOCOPY NUMBER
268 	                          , x_sum_trans_prop_amt_q1 OUT NOCOPY NUMBER
269 	                          , x_sum_trans_prop_amt_q2 OUT NOCOPY NUMBER
270 	                          , x_sum_trans_prop_amt_q3 OUT NOCOPY NUMBER
271 	                          , x_sum_trans_prop_amt_q4 OUT NOCOPY NUMBER
272 	                              ) IS
273 
274 
275 
276 	Cursor qtr_gross_amts is
277 	  select SUM(DECODE(JZVTD.TAX_LINE_NUMBER, '1', NVL(JZVTD.taxable_amt_funcl_curr, JZVTD.taxable_amt),0))
278 			+
279 	   SUM(NVL(JZVTD.tax_amt_funcl_curr, JZVTD.tax_amt)) Sum_Trx_Line_Amt_Qtr
280 
281 
282 	,SUM(DECODE(NVL(trx.global_attribute12, 'N'),'Y', DECODE(JZVTD.TAX_LINE_NUMBER, '1', NVL(JZVTD.taxable_amt_funcl_curr, 0),0),0))
283               +
284 	 SUM(DECODE(NVL(trx.global_attribute12, 'N'),'Y', NVL(JZVTD.tax_amt_funcl_curr, 0),0)) TRANSMISSION_PROP_AMT_QTR
285 
286 	,decode(to_char(jzvtd.gl_date,'MM'),'01','1T','02','1T','03','1T','04','2T','05','2T','06','2T','07','3T','08','3T','09','3T','10','4T','11','4T','12','4T')   QUARTER_NUM
287 	 FROM     jg_zz_vat_rep_status    JZVRS
288 	      ,   jg_zz_vat_trx_details   JZVTD
289 	      ,   hz_cust_site_uses_all  hzcsu
290 	      ,   hz_cust_acct_sites_all hzcas
291 	      ,   hz_cust_accounts hzca
292 	      ,   ra_customer_trx_all trx
293 	      ,   zx_lines_det_factors zxdf
294 
295 	 WHERE  JZVRS.vat_reporting_entity_id  = P_VAT_REP_ENTITY_ID
296 	 AND    JZVTD.reporting_status_id in (SELECT DISTINCT JZRS.reporting_status_id JZRS
297 					     FROM jg_zz_vat_rep_status JZRS
298 					     WHERE JZRS.vat_reporting_entity_id = P_VAT_REP_ENTITY_ID
299 					     AND   JZRS.source  = 'AR')
300 	 AND      JZVTD.extract_source_ledger    =  'AR'
301 	 AND      JZVRS.source                   =  'AR'
302 	 AND      JZVTD.trx_line_class <> 'DEBIT'
303 	 AND    SUBSTR(NVL(JZVTD.tax_rate_vat_trx_type_code,'QQQQQQ'),1,3) <> 'RET'
304          AND      SUBSTR(ZXDF.trx_business_category, INSTR(zxdf.trx_business_category,'MOD')+3,
305                      DECODE(INSTR(zxdf.trx_business_category,'/',1,3),0,length(zxdf.trx_business_category),
306                      INSTR(zxdf.trx_business_category,'/',1,3) - (INSTR(zxdf.trx_business_category,'MOD')+3)))
307                    IN ('347','415_347','347PR','415_347PR')
308 	AND     DECODE( INSTR(ZXDF.trx_business_category,'/',1,3),0,'B',
309 	            substr(ZXDF.trx_business_category,length(ZXDF.trx_business_category),1)) = p_tipo
310 	 AND    JZVTD.trx_id = ZXDF.trx_id
311 	 AND    JZVTD.trx_line_id = ZXDF.trx_line_id
312 	 AND    JZVTD.trx_id = TRX.customer_trx_id
313          AND    nvl(trx.global_attribute15,1) = nvl(p_year_of_amts_rec_cash,1)
314 	 AND    JZVRS.tax_calendar_year  = P_TAX_YEAR
315 	 AND    JZVTD.gl_date BETWEEN JZVRS.period_start_date and JZVRS.period_end_date
316 	 AND    nvl(JZVTD.billing_trading_partner_id,1) = nvl(p_customer_id,1)
317 	 AND    JZVTD.BILLING_TRADING_PARTNER_ID  =   hzca.cust_account_id
318 	 AND    hzca.cust_account_id  = hzcas.cust_account_id
319 	 AND    hzcsu.cust_acct_site_id  = hzcas.cust_acct_site_id
320 	 AND    upper(hzcsu.site_use_code) = 'LEGAL'
321 	 AND    hzcsu.primary_flag         = 'Y'
322 	 AND    hzcsu.status               = 'A'
323 	 AND    hzcsu.ORG_ID               = P_ORG_ID
324 	group by decode(to_char(jzvtd.gl_date,'MM'),'01','1T','02','1T','03','1T','04','2T','05','2T','06','2T','07','3T','08','3T','09','3T','10','4T','11','4T','12','4T') ;
325 
326 
327 	cursor  arrenda_qtr is
328 	 select SUM(DECODE(JZVTD.TAX_LINE_NUMBER, '1', NVL(JZVTD.taxable_amt_funcl_curr, 0),0))
329 			+ SUM(NVL(JZVTD.tax_amt_funcl_curr, 0)) arrenda_amt_qtr
330 	 ,decode(to_char(jzvtd.gl_date,'MM'),'01','1T','02','1T','03','1T','04','2T','05','2T','06','2T','07','3T','08','3T','09','3T','10','4T','11','4T','12','4T')   QUARTER_NUM
331 	     FROM   jg_zz_vat_trx_details JZVTD
332                   , jg_zz_vat_rep_status JZVRS
333 	          , hz_cust_site_uses_all  HCSU
334 	          , zx_lines_det_factors ZXDF
335                   , ra_customer_trx_all trx
336 	     WHERE  nvl(JZVTD.billing_trading_partner_id,1) = nvl(p_customer_id,1)
337 	     AND    HCSU.cust_acct_site_id       =   JZVTD.billing_tp_address_id
338 	     AND    UPPER(HCSU.site_use_code)    = 'LEGAL'
339 	     AND    JZVTD.extract_source_ledger = 'AR'
340 	     AND    JZVTD.event_class_code not in ('DEBIT_MEMO', 'APP', 'ADJ')
341 	     AND   HCSU.primary_flag            = 'Y'
342 	     AND   SUBSTR(NVL(JZVTD.tax_rate_vat_trx_type_code,'QQQQQQ'),1,3) <> 'RET'
343 	     AND     SUBSTR(ZXDF.trx_business_category, INSTR(ZXDF.trx_business_category,'MOD')+3,
344 	               DECODE(INSTR(ZXDF.trx_business_category,'/',1,3),0,length(ZXDF.trx_business_category),
345 	                 INSTR(ZXDF.trx_business_category,'/',1,3) - (INSTR(ZXDF.trx_business_category,'MOD')+3)))
346 	             IN ( '347PR', '415_347PR')
347 	    AND     JZVTD.trx_id = ZXDF.trx_id
348             AND     JZVTD.trx_id = trx.customer_trx_id
349             AND    nvl(trx.global_attribute15,1) = nvl(p_year_of_amts_rec_cash,1)
350 	    AND     JZVTD.trx_line_id = ZXDF.trx_line_id
351 	    AND     DECODE( INSTR(ZXDF.trx_business_category,'/',1,3),0,'B',
352 	            substr(ZXDF.trx_business_category,length(ZXDF.trx_business_category),1)) = p_tipo
353 	     AND     JZVRS.VAT_REPORTING_ENTITY_ID = P_VAT_REP_ENTITY_ID
354 	     AND     JZVRS.source                 = 'AR'
355 	     AND     JZVRS.reporting_status_id      = JZVTD.reporting_status_id
356 	     AND     JZVRS.tax_calendar_year       = P_TAX_YEAR
357 	     AND    JZVTD.gl_date BETWEEN JZVRS.period_start_date and JZVRS.period_end_date
358 	group by decode(to_char(jzvtd.gl_date,'MM'),'01','1T','02','1T','03','1T','04','2T','05','2T','06','2T','07','3T','08','3T','09','3T','10','4T','11','4T','12','4T');
359 
360 	BEGIN
361 
362 
363 		for  qtly_gross_amt_rec in qtr_gross_amts  loop
364 
365 	        if qtly_gross_amt_rec.quarter_num = '1T' then
366 
367                  x_sum_trx_line_amt_q1   := qtly_gross_amt_rec.Sum_Trx_Line_Amt_Qtr;
368 	         x_sum_trans_prop_amt_q1 := qtly_gross_amt_rec.TRANSMISSION_PROP_AMT_QTR;
369 
370 	        elsif qtly_gross_amt_rec.quarter_num = '2T' then
371 
372 	          x_sum_trx_line_amt_q2   := qtly_gross_amt_rec.Sum_Trx_Line_Amt_Qtr;
373 	          x_sum_trans_prop_amt_q2 := qtly_gross_amt_rec.TRANSMISSION_PROP_AMT_QTR;
374 
375 	        elsif qtly_gross_amt_rec.quarter_num = '3T' then
376 
377                  x_sum_trx_line_amt_q3   := qtly_gross_amt_rec.Sum_Trx_Line_Amt_Qtr;
378 	         x_sum_trans_prop_amt_q3 := qtly_gross_amt_rec.TRANSMISSION_PROP_AMT_QTR;
379 
380 	        elsif qtly_gross_amt_rec.quarter_num = '4T' then
381 
382 	          x_sum_trx_line_amt_q4   := qtly_gross_amt_rec.Sum_Trx_Line_Amt_Qtr;
383 	          x_sum_trans_prop_amt_q4 := qtly_gross_amt_rec.TRANSMISSION_PROP_AMT_QTR;
384 
385 	        end if;
386 
387 	        end loop; --quarterly gross trx amounts
388 
389 
390 	        for  qtly_arrenda_amt_rec in arrenda_qtr  loop
391 
392 	        if qtly_arrenda_amt_rec.quarter_num = '1T' then
393 
394 	        x_sum_trx_line_amt_q1 := x_sum_trx_line_amt_q1 - nvl(qtly_arrenda_amt_rec.arrenda_amt_qtr,0);
395 	        x_arrenda_amt_q1 := nvl(qtly_arrenda_amt_rec.arrenda_amt_qtr,0);
396 
397 	        elsif qtly_arrenda_amt_rec.quarter_num = '2T' then
398 
399 	        x_sum_trx_line_amt_q2 := x_sum_trx_line_amt_q2 - nvl(qtly_arrenda_amt_rec.arrenda_amt_qtr,0);
400                 x_arrenda_amt_q2 := nvl(qtly_arrenda_amt_rec.arrenda_amt_qtr,0);
401 
402 	        elsif qtly_arrenda_amt_rec.quarter_num = '3T' then
403 
404 	        x_sum_trx_line_amt_q3 := x_sum_trx_line_amt_q3 - nvl(qtly_arrenda_amt_rec.arrenda_amt_qtr,0);
405                 x_arrenda_amt_q3 := nvl(qtly_arrenda_amt_rec.arrenda_amt_qtr,0);
406 
407 	        elsif qtly_arrenda_amt_rec.quarter_num = '4T' then
408 
409 	        x_sum_trx_line_amt_q4 := x_sum_trx_line_amt_q4 - nvl(qtly_arrenda_amt_rec.arrenda_amt_qtr,0);
410                 x_arrenda_amt_q4 := nvl(qtly_arrenda_amt_rec.arrenda_amt_qtr,0);
411 
412                 end if;
413 
414 	        end loop; --quarterly arrenda amounts
415 
416 	END;
417 
418 
419 
420   PROCEDURE get_customer_address ( p_customer_address_id   IN         NUMBER
421                                  , x_postal_code          OUT NOCOPY VARCHAR2
422                                  , x_city                 OUT NOCOPY VARCHAR2
423                                  , x_address_detail       OUT NOCOPY VARCHAR2
424                                  )
425   IS
426 
427   CURSOR c_get_address IS
428      SELECT DECODE(HL.country
429             , 'ES', SUBSTR(HL.postal_code,1,2)||'000'
430             , '99'||FT.eu_code)                   postal_code
431           , SUBSTR(HL.city,1,24)          city
432           , SUBSTR(HL.ADDRESS1,1,2)  ||
433             SUBSTR(HL.ADDRESS2,1,23) ||
434             LPAD(SUBSTR(HL.ADDRESS3,1,length(HL.ADDRESS3) -
435             NVL(LENGTH(LTRIM(TRANSLATE(HL.ADDRESS3, '123456789','000000000'),'0')),0)),5,'0') address_detail
436       FROM  hz_cust_acct_sites_all HCAS
437           , hz_party_sites         HPS
438           , hz_locations           HL  --12656346
439           , fnd_territories        FT
440           , hz_cust_site_uses_all  HCSU
441       WHERE  HCAS.cust_acct_site_id       = p_customer_address_id
442       AND    HPS.party_site_id            = HCAS.party_site_id
443       AND    HL.location_id               = HPS.location_id
444       AND    HL.country(+)                = FT.territory_code
445       AND    HCSU.cust_acct_site_id       = HCAS.cust_acct_site_id
446       AND    UPPER(HCSU.site_use_code)    = 'LEGAL'
447       AND    HCAS.bill_to_flag            IN ('P','Y')
448       AND    HCAS.status                  = 'A'
449       AND    HCSU.primary_flag            = 'Y'  ;
450 
451   BEGIN
452     OPEN   c_get_address ;
453     FETCH  c_get_address INTO x_postal_code, x_city, x_address_detail ;
454     CLOSE  c_get_address ;
455   END;
456   --
457   --
458   PROCEDURE get_customer_address2 ( p_customer_address_id  IN         NUMBER
459                                   , x_postal               OUT NOCOPY VARCHAR2
460 				          , x_post_code            OUT NOCOPY VARCHAR2
461                                   , x_city                 OUT NOCOPY VARCHAR2
462                                   , x_street_type          OUT NOCOPY VARCHAR2
463                                   , x_street               OUT NOCOPY VARCHAR2
464                                   , x_number               OUT NOCOPY VARCHAR2
465                                   , x_country              OUT NOCOPY VARCHAR2
466 
467                                   )
468   IS
469 
470   CURSOR c_get_address IS
471    SELECT DECODE(HL.country
472                   , 'ES', SUBSTR(HL.postal_code,1,2)||'   '
473                   , '99'||FT.territory_code||' ')                            codigo_postal
474           , SUBSTR(HL.postal_code,1,5)                   post_code
475           , SUBSTR(HL.city,1,24)                         city
476           , SUBSTR(HL.address1,1,2)                        sigla
477           , SUBSTR(HL.address2,1,25)                       via_publica
478           , SUBSTR(HL.address3||'Z',1,INSTR(HL.address3||'Z',
479                       LTRIM(HL.address3||'Z','1234567890')) - 1) numero
480           , HL.country
481       FROM  hz_cust_acct_sites_all HCAS
482           , hz_party_sites         HPS
483           , hz_locations           HL
484           , fnd_territories        FT
485           , hz_cust_site_uses_all  HCSU
486       WHERE  HCAS.cust_acct_site_id       = p_customer_address_id
487       AND    HPS.party_site_id            = HCAS.party_site_id
488       AND    HL.location_id               = HPS.location_id
489       AND    HL.country(+)                = FT.territory_code
490       AND    HCSU.cust_acct_site_id       = HCAS.cust_acct_site_id
491       AND    UPPER(HCSU.site_use_code)    = 'LEGAL'
492       AND    HCAS.bill_to_flag            IN ('P','Y')
493       AND    HCAS.status                  = 'A'
494       AND    HCSU.primary_flag            = 'Y'  ;
495 
496 
497   BEGIN
498     OPEN   c_get_address ;
499     FETCH  c_get_address
500     INTO   x_postal
501          , x_post_code
502          , x_city
503          , x_street_type
504          , x_street
505          , x_number
506          , x_country;
507     CLOSE  c_get_address ;
508   END get_customer_address2 ;
509 
510 
511  PROCEDURE get_ARMOD340_PROP_LOC
512 
513 (       p_trx_id                   IN         NUMBER
514         , x_property_location_code OUT NOCOPY NUMBER
515         , x_ref_catastral          OUT NOCOPY VARCHAR2
516 
517                                   )
518   IS
519 
520 
521 cursor c_prop_loc is
522               select     SUBSTR(HL.global_attribute2,1,25)       land_registry
523           ,   DECODE (HL.country,
524                             'ES', DECODE(NVL(HL.global_attribute2, 'N'),
525                                   -- 3 Property in Spain without a land registry reference
526                                   'N', 3,
527                                   -- 2 Property in the Basque or Navarra Community
528                                   DECODE (SUBSTR(HL.postal_code,1,2),
529                                   '01', 2,
530                                   '48', 2,
531                                   '20', 2,
532                                   '31', 2,
533                                   -- 1 Property with land registry in Spain except 2
534                                   1)),
535                             -- 4 Property located outside Spain
536                             4) property_location_code
537 FROM
538                  jg_zz_vat_trx_details   JZVTD
539                ,   hr_locations           HL
540                ,   ra_customer_trx       TRX
541                ,   zx_lines_det_factors   ZXDF
542 where    JZVTD.trx_id                 = p_trx_id
543 AND      JZVTD.trx_id                 = TRX.customer_trx_id
544 AND      HL.location_id               = TO_NUMBER(TRX.global_attribute2)
545 AND      (SUBSTR(ZXDF.trx_business_category, INSTR(zxdf.trx_business_category,'MOD')+3,
546                      DECODE(INSTR(zxdf.trx_business_category,'/',1,3),0,length(zxdf.trx_business_category),
547                      INSTR(zxdf.trx_business_category,'/',1,3) - (INSTR(zxdf.trx_business_category,'MOD')+3)))
548                    IN ('347PR','415_347PR')
549                     OR substr(zxdf.document_sub_type,1,6) = 'MOD340')
550 AND       nvl(zxdf.document_sub_type,'X') <> 'MOD340_EXCL'
551 AND      JZVTD.trx_id = ZXDF.trx_id
552 AND     jzvtd.trx_line_id = zxdf.trx_line_id
553 AND      TRX.global_attribute_category in ('JE.ES.ARXTWMAI.MODELO415_347PR','JE.ES.ARXTWMAI.MODELO347PR')
554 group by SUBSTR(HL.global_attribute2,1,25)
555           ,   DECODE (HL.country,
556                             'ES', DECODE(NVL(HL.global_attribute2, 'N'),
557                                   -- 3 Property in Spain without a land registry reference
558                                   'N', 3,
559                                   -- 2 Property in the Basque or Navarra Community
560                                   DECODE (SUBSTR(HL.postal_code,1,2),
561                                   '01', 2,
562                                   '48', 2,
563                                   '20', 2,
564                                   '31', 2,
565                                   -- 1 Property with land registry in Spain except 2
566                                   1)),
567                             -- 4 Property located outside Spain
568                             4) ;
569 
570 begin
571 
572 open c_prop_loc;
573 
574 fetch c_prop_loc into
575 
576 x_ref_catastral,
577 x_property_location_code;
578 
579 
580 close c_prop_loc;
581 
582 Exception
583 when others then
584 null;
585 
586 end get_ARMOD340_PROP_LOC;
587 
588 
589   -- Modelo 347
590 
591 
592  PROCEDURE arrenda ( p_vat_rep_entity_id IN VARCHAR2
593                    , p_customer_id       IN NUMBER
594                    , p_customer_name     IN VARCHAR2
595                    , p_cust_tax_reg_num  IN VARCHAR2
596                    , p_tipo              IN VARCHAR2
597                    , p_year_of_amts_rec_cash IN NUMBER)  -- Bug 8485057
598  IS
599    CURSOR arrenda
600    IS
601           /* Bug 8485057: Address information in GDF cols replaced with core HL table columns,
602              added more address details and the new property_location_code*/
603           SELECT   SUM(DECODE(JZVTD.TAX_LINE_NUMBER, '1', NVL(JZVTD.taxable_amt_funcl_curr, 0),0)) +
604                    SUM(NVL(JZVTD.tax_amt_funcl_curr, 0))   trx_line_amt
605                    /* total transaction amount was wrong, trx_line_amt calculation changed to consider
606                       the total transaction amount only once, not for each tax line
607                       SUM( NVL(JZVTD.taxable_amt_funcl_curr,JZVTD.taxable_amt ) )
608                       + SUM( NVL(JZVTD.tax_amt_funcl_curr,JZVTD.tax_amt ) ) trx_line_amt */
609                    /* removed, causing wrong number of records retrieved
610                       ,   NVL(JZVTD.tax_rate_id,0)                tax_rate_id */
611                ,   SUBSTR(HL.postal_code,1,5)              postcode
612                ,   SUBSTR(HL.global_attribute2,1,25)       land_registry
613                ,   HL.town_or_city                         city
614                ,   SUBSTR(HL.address_line_1,1,2)           address1
615                ,   SUBSTR(HL.address_line_2,1,25)          address2
616                ,   SUBSTR(HL.address_line_3||'Z',1,INSTR(HL.address_line_3||'Z',
617                              LTRIM(HL.address_line_3||'Z','1234567890')) - 1) address3
618                ,   SUBSTR(hl.loc_information15,1,3)        stairs
619                ,   SUBSTR(hl.loc_information16,1,3)        floor
620                ,   SUBSTR(hl.loc_information17,1,3)        door
621                ,   SUBSTR(hl.loc_information13,1,3)        number_type
622                ,   SUBSTR(hl.loc_information18,1,3)        qualifier
623                ,   SUBSTR(hl.loc_information14,1,3)        block
624                ,   SUBSTR(hl.loc_information19,1,3)        portal
625                ,   SUBSTR(hl.loc_information20,1,40)       complement
626                ,   SUBSTR(hl.town_or_city,1,30)            locality
627                ,   SUBSTR(hl.postal_code,1,5)              municipality_code
628                 -- Property location code:
629                ,   DECODE (HL.country,
630                             'ES', DECODE(NVL(HL.global_attribute2, 'N'),
631                                   -- 3 Property in Spain without a land registry reference
632                                   'N', 3,
633                                   -- 2 Property in the Basque or Navarra Community
634                                   DECODE (SUBSTR(HL.postal_code,1,2),
635                                   '01', 2,
636                                   '48', 2,
637                                   '20', 2,
638                                   '31', 2,
639                                   -- 1 Property with land registry in Spain except 2
640                                   1)),
641                             -- 4 Property located outside Spain
642                             4) property_location_code
643           FROM     jg_zz_vat_rep_status    JZVRS
644                ,   jg_zz_vat_trx_details   JZVTD
645                ,   hz_cust_acct_sites_all HCAS
646                ,   hz_party_sites         HPS
647                ,   hr_locations           HL
648             --   ,   fnd_territories        FT  -- Bug 8485057: not necessary
649                ,   hz_cust_site_uses_all  HCSU
650                ,   zx_lines_det_factors   ZXDF
651                ,   ra_customer_trx        TRX
652           WHERE    JZVRS.vat_reporting_entity_id  = P_VAT_REP_ENTITY_ID
653           AND      JZVRS.reporting_status_id      = JZVTD.reporting_status_id
654           AND      JZVTD.extract_source_ledger    = 'AR'
655           AND      JZVRS.source                 = 'AR'
656           AND      HCAS.cust_acct_site_id       = JZVTD.billing_tp_address_id
657           AND      HPS.party_site_id            = HCAS.party_site_id
658         /* Bug 8485057 section commented out, location is not the same as the customer's legal site, this validation can be done at the LOV level */
659         --     AND      HL.location_id               = HPS.location_id
660         --    AND      HL.country(+)                = FT.territory_code
661           AND      HCSU.cust_acct_site_id       = HCAS.cust_acct_site_id
662           AND      UPPER(HCSU.site_use_code)    = 'LEGAL'
663           AND      HCAS.bill_to_flag            IN ('P','Y')
664           AND      HCAS.status                  = 'A'
665           AND      HCSU.primary_flag            = 'Y'
666           AND      JZVTD.billing_trading_partner_id = p_customer_id
667           AND      JZVTD.gl_date               BETWEEN G_FROM_DATE AND G_TO_DATE
668           AND      SUBSTR(NVL(JZVTD.tax_rate_vat_trx_type_code,'QQQ'),1,3) <> 'RET_AR' -- tax_rate_vat_trx_type_code
669           -- Bug 8485057 verify invoice_report_type directly from Trx Tax Lines
670           AND      SUBSTR(ZXDF.trx_business_category, INSTR(zxdf.trx_business_category,'MOD')+3,
671                      DECODE(INSTR(zxdf.trx_business_category,'/',1,3),0,length(zxdf.trx_business_category),
672                      INSTR(zxdf.trx_business_category,'/',1,3) - (INSTR(zxdf.trx_business_category,'MOD')+3)))
673                    IN ('347PR','415_347PR')
674           AND      JZVTD.trx_id = ZXDF.trx_id
675           AND      JZVTD.trx_line_id = ZXDF.trx_line_id
676           AND      JZVTD.trx_id = TRX.customer_trx_id
677           AND      nvl(trx.global_attribute15,1) = nvl(p_year_of_amts_rec_cash,1)
678           AND      HL.location_id = TO_NUMBER(TRX.global_attribute2)
679           AND      TRX.global_attribute_category in ('JE.ES.ARXTWMAI.MODELO415_347PR','JE.ES.ARXTWMAI.MODELO347PR')
680           -- Bug 8485057 transaction code (tipo) filter
681           AND      DECODE( INSTR(ZXDF.trx_business_category,'/',1,3),0,'B',
682                    SUBSTR(ZXDF.trx_business_category,length(ZXDF.trx_business_category),1)) = p_tipo
683           GROUP BY  --  NVL(JZVTD.tax_rate_id,0),
684                       SUBSTR(HL.postal_code,1,5)
685                   ,   SUBSTR(HL.global_attribute2,1,25)
686                   ,   HL.town_or_city
687                   ,   SUBSTR(HL.address_line_1,1,2)
688                   ,   SUBSTR(HL.address_line_2,1,25)
689                   ,   SUBSTR(HL.address_line_3||'Z',1,INSTR(HL.address_line_3||'Z',
690                       LTRIM(HL.address_line_3||'Z','1234567890')) - 1)
691                   ,   SUBSTR(hl.loc_information15,1,3)
692                   ,   SUBSTR(hl.loc_information16,1,3)
693                   ,   SUBSTR(hl.loc_information17,1,3)
694                   ,   SUBSTR(hl.loc_information13,1,3)
695                   ,   SUBSTR(hl.loc_information18,1,3)
696                   ,   SUBSTR(hl.loc_information14,1,3)
697                   ,   SUBSTR(hl.loc_information19,1,3)
698                   ,   SUBSTR(hl.loc_information20,1,40)
699                   ,   SUBSTR(hl.town_or_city,1,30)
700                   ,   SUBSTR(hl.postal_code,1,5)
701                   ,   DECODE (HL.country,
702                             'ES', DECODE(NVL(HL.global_attribute2, 'N'),
703                                   'N', 3,
704                                   DECODE (SUBSTR(HL.postal_code,1,2),
705                                   '01', 2,
706                                   '48', 2,
707                                   '20', 2,
708                                   '31', 2,
709                                   1)),
710                             4);
711 
712     BEGIN
713        -- dbms_output.put_line ('Inside arrenda');
714 
715        FOR arrenda_rec IN arrenda LOOP
716 
717          /* IF LENGTH(arrenda_rec.postcode) = 2  -- i.e. the code is '99' only
718          THEN
719            arrenda_rec.postcode := '99958';
720          END IF; */
721 
722          /**
723            author:Brathod
724            As no such currency conversion found in R11i code
725            SELECT DECODE( G_CURRENCY_CODE, 'EUR', (arrenda_rec.trx_line_amt*100), arrenda_rec.trx_line_amt)
726            INTO   arrenda_rec.trx_line_amt
727            FROM   DUAL;
728 	       */
729 
730 	      IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'inserting with tipo 3: p_cust_tax_reg_num=' ||p_cust_tax_reg_num); END IF;
731 
732          -- dbms_output.put_line ('inserting ');
733          INSERT INTO JG_ZZ_VAT_TRX_GT
734            ( jg_info_v1    -- tipo
735            , jg_info_v6    -- p_tipo to link the arrenda record to its 'tipo' pair
736            , jg_info_n2    -- importe       -- sum_trx_line_amt
737            , jg_info_v11   -- nombre        -- cust name   (Bug 8485057 switched nif to v11)
738            , jg_info_v12   -- nif           -- cust tax ref (Bug 8485057 switched nombre to v12)
739            , jg_info_v25   -- sigla         -- address1
740            , jg_info_v2    -- municipio     -- city (corrected)
741            , jg_info_v18   -- codigo_postal -- postcode
742            , jg_info_v27   -- fin_ind       -- 'S'
743            , jg_info_v26    -- via_publica   -- address2,
744            , jg_info_v20    -- numero        -- address3
745            , jg_info_n4    -- comentario    -- Bug 8485057: 1 marks if record should not be listed in the magnetic format
746            , jg_info_v21   -- flag_arrenda  -- 'Y'
747            , jg_info_v3    -- escalera      -- loc_information15
748            , jg_info_v4    -- piso          -- loc_information16
749            , jg_info_v5    -- puerta        -- loc_information17
750            , jg_info_v8    -- number type       -- loc_information13
751            , jg_info_v9    -- number qualifier  -- loc_information18
752            , jg_info_v10   -- block             -- loc_information14
753            , jg_info_v28   -- portal            -- loc_information19
754            , jg_info_v29   -- complement        -- loc_information20
755            , jg_info_v19   -- ref_catastral     -- land_registry (corrected)
756            , jg_info_v7    -- property location code
757            , jg_info_v22   -- p_print_year      -- p_tax_calender_year
758            , jg_info_v23   -- legal entity name
759            )
760          VALUES
761            ('3'
762            , p_tipo                         -- to link the arrenda record to its pair
763            , arrenda_rec.trx_line_amt       -- importe
764            , p_cust_tax_reg_num             -- Bug 8485057 switch nif to jg_info_v11 to make delete procedure work
765            , p_customer_name                -- Bug 8485057 switch nombre to jg_info_v12
766            , arrenda_rec.address1           --v_sg
767            , arrenda_rec.city               --v_municipio
768            , arrenda_rec.postcode           --v_codigo_postal,
769            , 'S'                            --fin_ind
770            , arrenda_rec.address2           --v_via_publica,
771            , arrenda_rec.address3           --v_numero,
772            , NULL                           --comentario
773            ,'Y'                             --flag_arrenda,
774            , arrenda_rec.stairs             --v_escalera,
775            , arrenda_rec.floor              --v_piso,
776            , arrenda_rec.door               --v_puerta,
777            , arrenda_rec.number_type
778            , arrenda_rec.qualifier
779            , arrenda_rec.block
780            , arrenda_rec.portal
781            , arrenda_rec.complement
782            , arrenda_rec.land_registry      --v_ref_catastral
783            , arrenda_rec.property_location_code
784            , p_tax_year
785            , g_le_trn                       -- legal entity name
786            );
787 
788        END LOOP;
789 
790 
791   END;
792 
793   -- Modelo 349
794 
795   Procedure JGZZVEFT
796   IS
797 
798     CURSOR C_Spa_Pay_Mag_Ext
799     IS
800     /*SELECT  SUBSTR(jg_info_v11,1,2)                C_CODIGO_PAIS             -- TAX REG NUM
801          ,  RPAD(SUBSTR(jg_info_v11,3,12),15,' ')  C_NIF_OPERADOR            -- -do-
802          ,  RPAD(jg_info_v12,40,' ')               C_NOMBRE                  -- cust name
803          ,  UPPER(jg_info_v1)                      C_CLAVE_OPERACION
804          ,  jg_info_n12                            C_BASE_IMPONIBLE
805          ,  jg_info_n10                            C_RUNNING_TOTAL
806          ,  jg_info_n11                            C_FORMERLY_DECL_AMOUNT
807          ,  jg_info_v15                            C_YEAR
808          ,  jg_info_v16                            C_PERIOD
809          ,  jg_info_v17                            C_OPERACION_TRIANGULAR
810          ,  rowid                                  row_id
811     FROM    JG_ZZ_VAT_TRX_GT
812     ORDER BY  rpad(jg_info_v12,40,'A')
813            ,  jg_info_v1,jg_info_v15, jg_info_v16 desc;*/ -- Bug 5525421
814  /** This query is used to find out the report level information of the following
815     1. summ of correction amount
816     2. sum non correction amount
817     3. number of correction records and
818     4. number of non correction records
819     This information will be displayed on the header line of the report. */
820     SELECT  jg_info_v22                     YEAR_CAB
821          ,  jg_info_v26                     LE_TRN
822          ,  SUBSTR(jg_info_v11,1,2)         C_CODIGO_PAIS             -- TAX REG NUM
823          ,  RPAD(SUBSTR(jg_info_v11,3,12),15,' ') C_NIF_OPERADOR            -- -do-
824          ,  RPAD(jg_info_v12,40,' ')              C_NOMBRE                  -- cust name
825          ,  UPPER(jg_info_v1)               C_CLAVE_OPERACION
826          ,  jg_info_v15                     C_YEAR
827          ,  jg_info_v16                     C_PERIOD
828          ,  SUM(NVL(jg_info_n12,0))          C_BASE_IMPONIBLE
829          ,  SUM(jg_info_n11)                C_FORMERLY_DECL_AMOUNT
830 	 ,  SUM(jg_info_n10)                 C_RUNNING_TOTAL
831          ,  jg_info_v23                     SIGN
832          ,  jg_info_v18                     POST_CODE
833          ,  jg_info_v17                     OPERACION_TRIANGULAR
834     FROM    JG_ZZ_VAT_TRX_GT                JZVTG
835     WHERE   NVL(JZVTG.jg_info_v30, 'X') <>'H'
836     AND	    jg_info_v27	 = DECODE(P_DISPLAY_PERIOD,'OA',jg_info_v27,P_DISPLAY_PERIOD) -- Modified for Bug 7486406
837     GROUP BY jg_info_v21
838          ,   jg_info_v22
839          ,   SUBSTR(jg_info_v11,1,2)
840          ,   RPAD(SUBSTR(jg_info_v11,3,12),15,' ')
841          ,   Jg_info_v12
842          ,   jg_info_v1
843          ,   jg_info_v15
844          ,   jg_info_v16
845          ,   jg_info_v17
846          ,   jg_info_v18
847          ,   jg_info_v23
848          ,  jg_info_v26
849     ORDER BY  rpad(jg_info_v12,40,'A')
850            , jg_info_v1
851 	   , NVL(jg_info_v15,0)
852            , NVL(jg_info_v16,'A');
853 
854     ln_base_imponible               NUMBER := 0;
855     ln_base_imponible_dup           NUMBER := 0;
856     ln_base_imponible_imp           NUMBER := 0;
857     ln_running_total                NUMBER := 0;
858     ln_formerly_decl_amount         NUMBER := 0;
859     ln_corr_taxable_amount          NUMBER := 0;
860     ln_imp_1                        NUMBER := 0;
861     ln_imp_2                        NUMBER := 0;
862     ln_cuenta_1                     NUMBER := 0;
863     ln_cuenta_2                     NUMBER := 0;
864     ln_cuenta_3_4                   NUMBER := 0;
865 
866     ln_grp_imp_1                    NUMBER := 0;
867     ln_grp_imp_2                    NUMBER := 0;
868     ln_grp_cuenta_1                 NUMBER := 0;
869     ln_grp_cuenta_2                 NUMBER := 0;
870     ln_grp_cuenta_3_4               NUMBER := 0;
871 
872     lc_clave_operacion              VARCHAR2(5);
873     lc_cur_derive_type              VARCHAR2(5);
874     lc_source_ledger                VARCHAR2(30);
875 
876   BEGIN
877 
878          -- Call to common routine to fetch legal entity info.
879          BEGIN
880            SELECT DECODE(curr.derive_type,'EURO','343','349')
881            INTO lc_cur_derive_type
882            FROM fnd_currencies curr
883            WHERE currency_code = g_currency_code;
884          EXCEPTION
885             WHEN NO_DATA_FOUND THEN
886             lc_cur_derive_type := '349' ;
887          END;
888 
889          FOR c_spa_mag_rec IN C_Spa_Pay_Mag_Ext LOOP
890 
891 
892             --  Note that while inserting the correction we insert a small 'a','e' or 't'
893             --  so that the order by in the report works fine */
894 
895             lc_clave_operacion := c_spa_mag_rec.c_clave_operacion;
896 
897             --
898             -- Computation logic for running total and formerly_declared_amount should be inserted here
899             --
900             ln_running_total        := c_spa_mag_rec.c_running_total;
901             ln_formerly_decl_amount := c_spa_mag_rec.c_formerly_decl_amount;
902             ln_base_imponible       := c_spa_mag_rec.c_base_imponible;
903 
904 	    --
905             ln_corr_taxable_amount := ln_formerly_decl_amount + ln_base_imponible ;
906             --
907             -- CF_BASE_IMPON_DUPFormula return Number is
908             -- and CF_BASE_IMPONIBLEFormula return VARCHAR2 is
909             --
910             BEGIN
911 
912                IF lc_cur_derive_type = '343' /* i.e. functional Currency = EURO */
913                THEN
914                  ln_base_imponible_imp  := ln_base_imponible * 100;
915                  ln_base_imponible_dup  := (ln_base_imponible + ln_formerly_decl_amount) * 100;
916                ELSE
917                  ln_base_imponible_imp  := ln_base_imponible;
918                  ln_base_imponible_dup  :=  ln_base_imponible + ln_formerly_decl_amount;
919                END IF;
920                --srw.message('1000', 'CP Base Imponible...' || to_char(:cp_base_imponible));
921 
922             END;
923 
924             -- End of CF_BASE_IMPON_DUPFormula
925 
926             -- CF_IMP_1Formula
927 
928             IF c_spa_mag_rec.c_year IS NOT NULL AND c_spa_mag_rec.c_period IS NOT NULL THEN
929                 ln_cuenta_3_4 := 1;
930                 ln_cuenta_1   := 0;
931                 ln_imp_2      := ln_base_imponible_dup;
932 		ln_imp_1      := 0;
933             END IF;
934 
935             IF ( lc_clave_operacion = 'A' OR
936 		 		 lc_clave_operacion = 'I' OR
937 		 		 lc_clave_operacion = 'E' OR
938                  lc_clave_operacion = 'S' OR
939                  lc_clave_operacion = 'T') THEN
940 
941                IF  (c_spa_mag_rec.c_year IS NULL AND c_spa_mag_rec.c_period IS NULL ) THEN  -- Not a correction record
942                   ln_cuenta_1   := 1;
943                   ln_cuenta_3_4 := 0;
944                   ln_imp_2      := 0;
945                   ln_imp_1      := ln_base_imponible_dup;
946                ELSE
947                   ln_imp_1      := 0;
948                END IF;
949             END IF;
950 
951             -- End of CF_IMP_1Formula
952 
953             ln_grp_imp_1      := ln_grp_imp_1      + ln_imp_1 ;
954             ln_grp_imp_2      := ln_grp_imp_2      + ln_imp_2 ;
955             ln_grp_cuenta_1   := ln_grp_cuenta_1   + ln_cuenta_1;
956             ln_grp_cuenta_2   := ln_grp_cuenta_2   + ln_cuenta_2;
957             ln_grp_cuenta_3_4 := ln_grp_cuenta_3_4 + ln_cuenta_3_4;
958 
959             ln_imp_1      := 0;
960 	    -- BUG 9765740 Spanish Modelo 349 ESL EPL Changes
961             ln_imp_2      := 0;			-- Bug fix as part of ER
962             ln_cuenta_1   := 0;
963             ln_cuenta_2   := 0;
964             ln_cuenta_3_4 := 0;
965 
966          END LOOP;
967 
968          UPDATE  JG_ZZ_VAT_TRX_GT
969          SET     jg_info_N1 = ln_grp_cuenta_1
970               ,  jg_info_N2 = ln_grp_imp_1
971               ,  jg_info_N3 = ln_grp_cuenta_3_4
972               ,  jg_info_N4 = ln_grp_imp_2
973          WHERE   jg_info_v30 = 'H' ;
974 
975   END;
976 
977   -- Modelo 340
978   -- BUG 8946271
979   FUNCTION getKeyID (countryCode IN VARCHAR2) RETURN NUMBER IS
980       l_key_id   NUMBER;
981   BEGIN
982 
983     -- The report should check for first 2 characters of NIF. If it is 'ES' key_id
984     -- should be 1, if it is one of the EU member countries then it should be 2,
985     -- otherwise 6.
986     if countryCode = 'ES' THEN
987           l_key_id := 1;
988     elsif countryCode in ('AT','BE','BG','CY','CZ','DE','DK','EE','EL','ES','FI','FR','GB','GR','HU','IE','IT','LT','LU','LV','MT','NL','PL','PT','RO','SE','SI','SK') THEN
989           l_key_id := 2;
990     else
991           l_key_id := 6;
992     end if;
993 
994     return (l_key_id);
995 
996   END getKeyID;
997 
998   function format_amount ( p_amount IN NUMBER ) return Char is
999     lv_decimal_separator varchar2 (1);
1000   begin
1001 
1002   /* Get the decimal separator from the profile */
1003   lv_decimal_separator := substr(fnd_profile.value('ICX_NUMERIC_CHARACTERS'),1,1);  -- Bug 5525421
1004 
1005   -- lv_decimal_separator := '.';
1006    /** author: brathod
1007        date  : 22/05/2006
1008        Forward porting bug: 4771261 for 11i bug:4748029
1009    */
1010    /* If c_base_imponible has no decimals */  -- Bug 4748029
1011 
1012     if  instr(to_char(p_amount),lv_decimal_separator,1,1) = 0 then -- Bug 4748029
1013       RETURN((lpad(to_char(p_amount),11,'0')||'00'));  -- Bug 4748029
1014     end if;
1015 
1016     /* If c_base_imponible has decimals */     -- Bug 4748029
1017     if instr(p_amount,lv_decimal_separator,1,1) >0 then -- Bug 4748029
1018       RETURN(lpad(substr(p_amount ,1,instr(to_char(p_amount),lv_decimal_separator,1,1)-1),11,'0')||
1019                   rpad(substr(p_amount,instr(to_char(p_amount),lv_decimal_separator,1,1)+1,2),2,'0')) ; -- Bug 4748029
1020     end if;
1021   end;
1022 
1023   -- Modelo 415
1024 
1025   FUNCTION CF_POSTCODEFormula ( p_postcode IN VARCHAR2 ) return VARCHAR2 is
1026   BEGIN
1027 
1028     IF NVL(LENGTH(p_postcode), 0) = 2  /* i.e. '99' with no country code as territory unknown */
1029     THEN
1030       RETURN(p_postcode||'958'); /* 958 is default European country code for 'unknown' */
1031     ELSE
1032       RETURN(p_postcode);
1033     END IF;
1034 
1035     RETURN NULL;
1036   END;
1037 
1038   FUNCTION AP415_GROSS_AMOUNTFormula ( p_vendor_id     IN NUMBER
1039                                      , p_gross_amount  IN NUMBER )
1040                                       return Number is
1041      v_prepay_applied number;
1042 
1043   BEGIN
1044 
1045      SELECT  SUM(ROUND(DECODE( NVL(JZVTD.taxable_amt_funcl_curr,0)
1046                               ,0 , JZVTD.taxable_amt
1047                               ,    JZVTD.taxable_amt_funcl_curr
1048                               ))
1049                  -  NVL(AID.prepay_amount_remaining, 0 )* -1
1050                 )
1051      INTO    V_PREPAY_APPLIED
1052      FROM    jg_zz_vat_trx_details      JZVTD
1053             ,jg_zz_vat_rep_status       JZVRS
1054             ,ap_invoice_distributions   AID
1055      WHERE   JZVRS.reporting_status_id        = JZVTD.reporting_status_id
1056      AND     JZVTD.billing_trading_partner_id = p_vendor_id
1057      AND     JZVTD.trx_type_mng               = 'PREPAYMENT'
1058      --AND     JZVTD.posted_flag                IN ('P','Y')  /** author: brathod; Removed posted_flag checking as it is not relevent in R12 */
1059      AND     JZVTD.trx_line_type              = 'ITEM'
1060      AND     NVL(JZVTD.reverse_flag, 'N')     <> 'Y'
1061      AND     AID.invoice_distribution_id      =  JZVTD.trx_line_id
1062      AND     AID.invoice_id                   =  JZVTD.trx_id
1063      AND     AID.prepay_amount_remaining      IS NOT NULL
1064      AND     JZVTD.invoice_report_type        IN ('415'
1065                                                  ,'415_347'
1066                                                  ,'415_347PR')
1067      AND     JZVRS.vat_reporting_entity_id = P_VAT_REP_ENTITY_ID
1068      /**
1069       author: brathod
1070       date: 18/05/2006
1071       Commented only period based filtering and added AND condition to also consider year
1072       filtering for ANNUAL reports
1073       -- JZVRS.tax_calendar_period     = P_TAX_PERIOD
1074      */
1075      AND     (  (P_REPORT_NAME = 'JEESPMOR' AND JZVRS.tax_calendar_period     --= P_TAX_PERIOD
1076                                                                       in (
1077               SELECT RPS1.tax_calendar_period
1078               FROM JG_ZZ_VAT_REP_STATUS RPS1,
1079                    (Select min(vat_reporting_entity_id) vat_reporting_entity_id,
1080                            min(period_start_date) period_start_date
1081                     From JG_ZZ_VAT_REP_STATUS
1082                     Where vat_reporting_entity_id = p_vat_rep_entity_id
1083                     And tax_calendar_period = p_tax_period) RPS2,
1084                    (Select min(vat_reporting_entity_id) vat_reporting_entity_id,
1085                           min(period_end_date) period_end_date
1086                     From JG_ZZ_VAT_REP_STATUS
1087                     Where vat_reporting_entity_id = p_vat_rep_entity_id
1088                     And tax_calendar_period = p_tax_period_to) RPS3
1089               WHERE RPS1.vat_reporting_entity_id = p_vat_rep_entity_id
1090                 AND RPS2.vat_reporting_entity_id = RPS1.vat_reporting_entity_id
1091                 AND RPS3.vat_reporting_entity_id = RPS2.vat_reporting_entity_id
1092                 AND trunc(RPS1.period_start_date) >=
1093                                trunc(RPS2.period_start_date)
1094                 AND trunc(RPS1.period_end_date) <= trunc(RPS3.period_end_date)
1095               GROUP by RPS1.tax_calendar_period
1096                                                                          ))
1097              OR (P_REPORT_NAME = 'JEESAMOR' AND JZVRS.tax_calendar_year       = P_TAX_YEAR )
1098              )
1099      ;
1100      --Bug 1064230 - prepayment amount already accounted for
1101      RETURN( p_gross_amount - NVL(v_prepay_applied,0));
1102 
1103 
1104   RETURN NULL; EXCEPTION
1105     WHEN OTHERS THEN
1106       RETURN(p_gross_amount);
1107 
1108   END;
1109 
1110   FUNCTION AP347_GROSS_AMOUNTFormula ( p_vendor_id      IN NUMBER
1111                                      , p_property_flag  IN VARCHAR2
1112                                      , p_gross_amt      IN NUMBER
1113                                      , p_tipo           IN VARCHAR2) -- Bug 8485057
1114                                       return Number is
1115      v_prepay_applied number;
1116 
1117   BEGIN
1118 
1119      SELECT  SUM(ROUND(DECODE( NVL(JZVTD.taxable_amt_funcl_curr,0)
1120                               ,0 , JZVTD.taxable_amt
1121                               ,    JZVTD.taxable_amt_funcl_curr
1122                               )
1123                  , g_cur_precision)
1124                 )
1125      INTO    V_PREPAY_APPLIED
1126      FROM    jg_zz_vat_trx_details      JZVTD
1127             ,jg_zz_vat_rep_status       JZVRS
1128             ,ap_invoice_distributions   AID
1129             ,ap_invoice_distributions   PRE
1130             ,hz_parties                 HP
1131             ,ap_invoices_all            API
1132 	        ,zx_lines_det_factors       ZXDF
1133      WHERE   JZVRS.reporting_status_id        = JZVTD.reporting_status_id
1134      AND     JZVTD.billing_trading_partner_id = p_vendor_id
1135      AND     JZVTD.trx_type_mng               = 'PREPAYMENT'
1136      --AND     JZVTD.posted_flag                IN ('P','Y')  /** author:brathod; Removed posted_flag checking as it is not relevent in R12 */
1137      AND     AID.invoice_Id                    =  JZVTD.trx_id
1138      AND     PRE.invoice_distribution_id      =  AID.invoice_distribution_id
1139      AND     PRE.posted_flag                  IN ('P','Y')
1140      AND     PRE.line_type_lookup_code        <> 'AWT'
1141      AND     HP.party_id                      =  JZVTD.billing_trading_partner_id
1142      AND     NVL(hp.party_type, 'XXX')        <> 'EMPLOYEE'
1143      AND     DECODE( INSTR(NVL(ZXDF.trx_business_category,'N'),'PR'), 0, 'N', 'Y') = p_property_flag
1144      -- Bug 8485057 verify invoice_report_type directly from Invoice
1145      AND     SUBSTR(ZXDF.trx_business_category, INSTR(ZXDF.trx_business_category,'MOD')+3,
1146                DECODE(INSTR(ZXDF.trx_business_category,'/',1,3),0,length(ZXDF.trx_business_category),
1147                  INSTR(ZXDF.trx_business_category,'/',1,3) - (INSTR(ZXDF.trx_business_category,'MOD')+3)))
1148              IN ( '347', '347PR', '415_347', '415_347PR')
1149      -- Bug 8485057 added filter by tipo (transaction code)
1150      AND     DECODE( INSTR(ZXDF.trx_business_category,'/',1,3),0,'A',
1151          substr(ZXDF.trx_business_category,length(ZXDF.trx_business_category),1)) = p_tipo
1152      AND     JZVTD.trx_id = API.invoice_id
1153      AND     jzvtd.trx_id = zxdf.trx_id
1154      AND     jzvtd.trx_line_id = zxdf.trx_line_id
1155      AND     JZVRS.vat_reporting_entity_id = P_VAT_REP_ENTITY_ID
1156      /**
1157       author: brathod
1158       date  : 18/05/2006
1159       Commented only period based filtering and added AND condition to also consider year
1160       filtering for ANNUAL reports
1161       -- JZVRS.tax_calendar_period     = P_TAX_PERIOD
1162      */
1163      AND     (  (P_REPORT_NAME = 'JEESPMOR' AND JZVRS.tax_calendar_period     --= P_TAX_PERIOD
1164                                                                       in (
1165               SELECT RPS1.tax_calendar_period
1166               FROM JG_ZZ_VAT_REP_STATUS RPS1,
1167                    (Select min(vat_reporting_entity_id) vat_reporting_entity_id,
1168                            min(period_start_date) period_start_date
1169                     From JG_ZZ_VAT_REP_STATUS
1170                     Where vat_reporting_entity_id = p_vat_rep_entity_id
1171                     And tax_calendar_period = p_tax_period) RPS2,
1172                    (Select min(vat_reporting_entity_id) vat_reporting_entity_id,
1173                           min(period_end_date) period_end_date
1174                     From JG_ZZ_VAT_REP_STATUS
1175                     Where vat_reporting_entity_id = p_vat_rep_entity_id
1176                     And tax_calendar_period = p_tax_period_to) RPS3
1177               WHERE RPS1.vat_reporting_entity_id = p_vat_rep_entity_id
1178                 AND RPS2.vat_reporting_entity_id = RPS1.vat_reporting_entity_id
1179                 AND RPS3.vat_reporting_entity_id = RPS2.vat_reporting_entity_id
1180                 AND trunc(RPS1.period_start_date) >=
1181                                trunc(RPS2.period_start_date)
1182                 AND trunc(RPS1.period_end_date) <= trunc(RPS3.period_end_date)
1183               GROUP by RPS1.tax_calendar_period
1184                                                                          ))
1185              OR (P_REPORT_NAME = 'JEESAMOR' AND JZVRS.tax_calendar_year       = P_TAX_YEAR )
1186              );
1187      --Bug 1064230 - prepayment amount already accounted for
1188      RETURN( p_gross_amt - NVL(v_prepay_applied,0));
1189 
1190 
1191   RETURN NULL; EXCEPTION
1192     WHEN OTHERS THEN
1193       RETURN( p_gross_amt );
1194 
1195   END;
1196 
1197   FUNCTION AR347_GROSS_AMOUNTFormula ( p_customer_id      IN NUMBER,
1198                                        p_tipo             IN VARCHAR,
1199                                        p_year_of_amts_rec_cash IN NUMBER) -- Bug 8485057
1200   RETURN NUMBER is
1201 
1202      v_arrenda_amount number;
1203 
1204   BEGIN
1205 
1206     /**
1207     author: brathod
1208     Commented to use jg_zz_vat_trx_detail instead of JG_ZZ_VAT_TRX_GT
1209     SELECT SUM(ROUND(DECODE( NVL(JVGT.jg_info_n8,0)
1210                       ,0 , JVGT.jg_info_n6
1211                       ,    JVGT.jg_info_n8
1212                       )
1213                 , g_cur_precision)
1214                )
1215      INTO   v_arrenda_amount
1216      FROM   JG_ZZ_VAT_TRX_GT    JVGT
1217           , hz_cust_acct_sites_all HCAS
1218           , hz_party_sites         HPS
1219           , hz_locations           HL
1220           , fnd_territories        FT
1221           , hz_cust_site_uses_all  HCSU
1222      WHERE  JVGT.jg_info_n3              = p_customer_id
1223      AND    HCAS.cust_acct_site_id       = JVGT.jg_info_n2
1224      AND    HPS.party_site_id            = HCAS.party_site_id
1225      AND    HL.location_id               = HPS.location_id
1226      AND    HL.country(+)                = FT.territory_code
1227      AND    HCSU.cust_acct_site_id       = HCAS.cust_acct_site_id
1228      AND    UPPER(HCSU.site_use_code)    = 'LEGAL'
1229      AND    HCAS.bill_to_flag            = 'Y'
1230      AND    HCAS.status                  = 'A'
1231      AND    JVGT.jg_info_d5              IS NOT NULL
1232      AND    NVL(JVGT.jg_info_v25, 'N')   = 'N'           -- posted_flag
1233      AND    JVGT.jg_info_v31             <> 'DEBIT'      -- invoice_type_lookup_code
1234      AND    HCSU.primary_flag            = 'Y'
1235      AND    SUBSTR(NVL(JVGT.jg_info_v31,'QQQQQQ'),1,3) <> 'RET' -- tax_rate_vat_trx_type_code
1236      AND    JVGT.jg_info_v11      IN ('347'
1237            ,'347PR');*/
1238 
1239      /**
1240      author: brathod
1241      1.  Add code below to use jg_zz_vat_trx_detail instead of JG_ZZ_VAT_TRX_GT
1242      2.  Commented TCA related tables as they are not required to fetch the amount and directly joined HCSU with JVGT*
1243      */
1244 
1245      SELECT /* Bug 8485057: total transaction amount was wrong, trx_line_amt calculation changed to consider
1246                the total transaction amount only once, not for each tax line */
1247             SUM(DECODE(JZVTD.TAX_LINE_NUMBER, '1', NVL(JZVTD.taxable_amt_funcl_curr, 0),0))
1248 		+ SUM(NVL(JZVTD.tax_amt_funcl_curr, 0))
1249      INTO  v_arrenda_amount
1250      FROM   jg_zz_vat_trx_details JZVTD, jg_zz_vat_rep_status JZVRS
1251           , hz_cust_site_uses_all  HCSU
1252           , zx_lines_det_factors ZXDF -- Bug 8485057
1253           , ra_customer_trx_all trx
1254      WHERE  JZVTD.billing_trading_partner_id = p_customer_id
1255      AND    HCSU.cust_acct_site_id       =   JZVTD.billing_tp_address_id
1256      AND    UPPER(HCSU.site_use_code)    = 'LEGAL'
1257      AND    JZVTD.extract_source_ledger = 'AR'
1258      AND    JZVTD.event_class_code not in ('DEBIT_MEMO', 'APP', 'ADJ')
1259     -- AND   JZVTD.accounting_date IS NOT NULL --bug5557860
1260     -- AND   JZVTD.posted_flag = 'Y'   /** author: brathod; removed posted_flag checking as it is not relevent in R12*/
1261      AND   HCSU.primary_flag            = 'Y'
1262      AND   SUBSTR(NVL(JZVTD.tax_rate_vat_trx_type_code,'QQQQQQ'),1,3) <> 'RET' -- tax_rate_vat_trx_type_code
1263     -- Bug 8485057 verify invoice_report_type directly from Trx Tax Lines
1264      AND     SUBSTR(ZXDF.trx_business_category, INSTR(ZXDF.trx_business_category,'MOD')+3,
1265                DECODE(INSTR(ZXDF.trx_business_category,'/',1,3),0,length(ZXDF.trx_business_category),
1266                  INSTR(ZXDF.trx_business_category,'/',1,3) - (INSTR(ZXDF.trx_business_category,'MOD')+3)))
1267              IN ( '347PR', '415_347PR')
1268     AND     JZVTD.trx_id = ZXDF.trx_id
1269     AND     JZVTD.trx_id = trx.customer_trx_id
1270     AND    nvl(trx.global_attribute15,1) = nvl(p_year_of_amts_rec_cash,1)
1271     AND     JZVTD.trx_line_id = ZXDF.trx_line_id
1272     -- Bug 8485057 added filter by tipo (transaction code)
1273     AND     DECODE( INSTR(ZXDF.trx_business_category,'/',1,3),0,'B',
1274             substr(ZXDF.trx_business_category,length(ZXDF.trx_business_category),1)) = p_tipo
1275 
1276      /**
1277        author: brathod
1278        date  : 18/05/2006
1279        Added AND condition to consider year and period based filtering
1280      */
1281      AND     JZVRS.VAT_REPORTING_ENTITY_ID = P_VAT_REP_ENTITY_ID
1282      AND     JZVRS.source                 = 'AR'
1283      AND     JZVRS.reporting_status_id      = JZVTD.reporting_status_id
1284      AND     (  (P_REPORT_NAME = 'JEESPMOR' AND JZVRS.tax_calendar_period     --= P_TAX_PERIOD
1285                                                                       in (
1286               SELECT RPS1.tax_calendar_period
1287               FROM JG_ZZ_VAT_REP_STATUS RPS1,
1288                    (Select min(vat_reporting_entity_id) vat_reporting_entity_id,
1289                            min(period_start_date) period_start_date
1290                     From JG_ZZ_VAT_REP_STATUS
1291                     Where vat_reporting_entity_id = p_vat_rep_entity_id
1292                     And tax_calendar_period = p_tax_period) RPS2,
1293                    (Select min(vat_reporting_entity_id) vat_reporting_entity_id,
1294                           min(period_end_date) period_end_date
1295                     From JG_ZZ_VAT_REP_STATUS
1296                     Where vat_reporting_entity_id = p_vat_rep_entity_id
1297                     And tax_calendar_period = p_tax_period_to) RPS3
1298               WHERE RPS1.vat_reporting_entity_id = p_vat_rep_entity_id
1299                 AND RPS2.vat_reporting_entity_id = RPS1.vat_reporting_entity_id
1300                 AND RPS3.vat_reporting_entity_id = RPS2.vat_reporting_entity_id
1301                 AND trunc(RPS1.period_start_date) >=
1302                                trunc(RPS2.period_start_date)
1303                 AND trunc(RPS1.period_end_date) <= trunc(RPS3.period_end_date)
1304               GROUP by RPS1.tax_calendar_period
1305                                                                          ))
1306              OR (P_REPORT_NAME = 'JEESAMOR' AND JZVRS.tax_calendar_year       = P_TAX_YEAR ));
1307      --  GROUP BY ROUND( NVL(JZVTD.taxable_amt_funcl_curr,JZVTD.taxable_amt ) , 2);
1308 
1309      --Bug 1064230 - prepayment amount already accounted for
1310      RETURN( NVL(v_arrenda_amount,0));
1311 
1312 
1313   RETURN NULL; EXCEPTION
1314     WHEN OTHERS THEN
1315       RETURN( 0 );
1316 
1317   END AR347_GROSS_AMOUNTFormula;
1318 
1319   /* Bug 8485057 Calculate the cash amount received for the selected transaction.
1320   The cash receipt must have the payment method specified by the customer in the profile option. */
1321   FUNCTION AR347_CASH_RECEIVEDFormula ( p_customer_id           IN NUMBER,
1322                                         p_tipo                  IN VARCHAR,
1323                                         p_property_rental_flag  IN VARCHAR,
1324                                         p_year_of_amts_rec_cash IN NUMBER)
1325   RETURN NUMBER is
1326      v_cash_received_amount number;
1327 
1328   BEGIN
1329     /* Note to developers: the following select statements are identical, except for the ZXDF.trx_business_category
1330        values ('347PR','415_347PR') or ('347','415_347'). All changes must be applied to both statements. */
1331     IF p_property_rental_flag = 'Y'
1332     THEN
1333       SELECT SUM(NVL(DECODE(CR.type,'MISC', 0
1334                               ,DECODE(RA.status,'APP',RA.AMOUNT_APPLIED,0)),0))
1335              / COUNT(JZVTD.trx_id)
1336       INTO   v_cash_received_amount
1337       FROM   jg_zz_vat_trx_details JZVTD, jg_zz_vat_rep_status JZVRS
1338            , hz_cust_site_uses_all  HCSU
1339            , zx_lines_det_factors ZXDF
1340            , ar_receivable_applications_all RA
1341            , ar_cash_receipts_all CR
1342            , ra_customer_trx_all trx
1343       WHERE  JZVTD.billing_trading_partner_id = p_customer_id
1344       AND    HCSU.cust_acct_site_id       =   JZVTD.billing_tp_address_id
1345       AND    UPPER(HCSU.site_use_code)    = 'LEGAL'
1346       AND    JZVTD.extract_source_ledger = 'AR'
1347       AND    JZVTD.event_class_code not in ('DEBIT_MEMO', 'APP', 'ADJ')
1348       AND    HCSU.primary_flag            = 'Y'
1349       AND    SUBSTR(NVL(JZVTD.tax_rate_vat_trx_type_code,'QQQQQQ'),1,3) <> 'RET'
1350              -- Bug 8485057 verify invoice_report_type directly from Trx Tax Lines
1351       AND    SUBSTR(ZXDF.trx_business_category, INSTR(ZXDF.trx_business_category,'MOD')+3,
1352                DECODE(INSTR(ZXDF.trx_business_category,'/',1,3),0,length(ZXDF.trx_business_category),
1353                  INSTR(ZXDF.trx_business_category,'/',1,3) - (INSTR(ZXDF.trx_business_category,'MOD')+3)))
1354              IN ('347PR','415_347PR')
1355       AND    JZVTD.trx_id = ZXDF.trx_id
1356       AND    JZVTD.trx_id = trx.customer_trx_id
1357       AND    nvl(trx.global_attribute15,1) = nvl(p_year_of_amts_rec_cash,1)
1358       AND    JZVTD.trx_line_id = ZXDF.trx_line_id
1359              -- Bug 8485057 filter by tipo (transaction code)
1360       AND    DECODE( INSTR(ZXDF.trx_business_category,'/',1,3),0,'B',
1361                substr(ZXDF.trx_business_category,length(ZXDF.trx_business_category),1)) = p_tipo
1362       AND    JZVRS.VAT_REPORTING_ENTITY_ID = P_VAT_REP_ENTITY_ID
1363       AND    JZVRS.source                 = 'AR'
1364       AND    JZVRS.reporting_status_id      = JZVTD.reporting_status_id
1365       AND    (  (P_REPORT_NAME = 'JEESPMOR' AND JZVRS.tax_calendar_period in (
1366               SELECT RPS1.tax_calendar_period
1367               FROM JG_ZZ_VAT_REP_STATUS RPS1,
1368                    (Select min(vat_reporting_entity_id) vat_reporting_entity_id,
1369                            min(period_start_date) period_start_date
1370                     From JG_ZZ_VAT_REP_STATUS
1371                     Where vat_reporting_entity_id = p_vat_rep_entity_id
1372                     And tax_calendar_period = p_tax_period) RPS2,
1373                    (Select min(vat_reporting_entity_id) vat_reporting_entity_id,
1374                           min(period_end_date) period_end_date
1375                     From JG_ZZ_VAT_REP_STATUS
1376                     Where vat_reporting_entity_id = p_vat_rep_entity_id
1377                     And tax_calendar_period = p_tax_period_to) RPS3
1378               WHERE RPS1.vat_reporting_entity_id = p_vat_rep_entity_id
1379                 AND RPS2.vat_reporting_entity_id = RPS1.vat_reporting_entity_id
1380                 AND RPS3.vat_reporting_entity_id = RPS2.vat_reporting_entity_id
1381                 AND trunc(RPS1.period_start_date) >=
1382                                trunc(RPS2.period_start_date)
1383                 AND trunc(RPS1.period_end_date) <= trunc(RPS3.period_end_date)
1384               GROUP by RPS1.tax_calendar_period
1385                                                                          ))
1386              OR (P_REPORT_NAME = 'JEESAMOR' AND JZVRS.tax_calendar_year       = P_TAX_YEAR ))
1387       AND RA.CASH_RECEIPT_ID = CR.CASH_RECEIPT_ID
1388       AND JZVTD.trx_id = RA.APPLIED_CUSTOMER_TRX_ID
1389       AND CR.RECEIPT_METHOD_ID = fnd_profile.value('JEES_MOD347_RECEIPT_METHOD');
1390     ELSE
1391       SELECT SUM(NVL(DECODE(CR.type,'MISC', 0
1392                               ,DECODE(RA.status,'APP',RA.AMOUNT_APPLIED,0)),0))
1393              / COUNT(JZVTD.trx_id)
1394       INTO   v_cash_received_amount
1395       FROM   jg_zz_vat_trx_details JZVTD, jg_zz_vat_rep_status JZVRS
1396            , hz_cust_site_uses_all  HCSU
1397            , zx_lines_det_factors ZXDF
1398            , ar_receivable_applications_all RA
1399            , ar_cash_receipts_all CR
1400            , ra_customer_trx_all trx
1401       WHERE  JZVTD.billing_trading_partner_id = p_customer_id
1402       AND    HCSU.cust_acct_site_id       =   JZVTD.billing_tp_address_id
1403       AND    UPPER(HCSU.site_use_code)    = 'LEGAL'
1404       AND    JZVTD.extract_source_ledger = 'AR'
1405       AND    JZVTD.event_class_code not in ('DEBIT_MEMO', 'APP', 'ADJ')
1406       AND    HCSU.primary_flag            = 'Y'
1407       AND    SUBSTR(NVL(JZVTD.tax_rate_vat_trx_type_code,'QQQQQQ'),1,3) <> 'RET'
1408              -- Bug 8485057 verify invoice_report_type directly from Trx Tax Lines
1409       AND    SUBSTR(ZXDF.trx_business_category, INSTR(ZXDF.trx_business_category,'MOD')+3,
1410                DECODE(INSTR(ZXDF.trx_business_category,'/',1,3),0,length(ZXDF.trx_business_category),
1411                  INSTR(ZXDF.trx_business_category,'/',1,3) - (INSTR(ZXDF.trx_business_category,'MOD')+3)))
1412              IN ('347','415_347')
1413       AND    JZVTD.trx_id = ZXDF.trx_id
1414       AND    JZVTD.trx_id = trx.customer_trx_id
1415       AND    nvl(trx.global_attribute15,1) = nvl(p_year_of_amts_rec_cash,1)
1416       AND    JZVTD.trx_line_id = ZXDF.trx_line_id
1417              -- Bug 8485057 filter by tipo (transaction code)
1418       AND    DECODE( INSTR(ZXDF.trx_business_category,'/',1,3),0,'B',
1419                 substr(ZXDF.trx_business_category,length(ZXDF.trx_business_category),1)) = p_tipo
1420       AND    JZVRS.VAT_REPORTING_ENTITY_ID = P_VAT_REP_ENTITY_ID
1421       AND    JZVRS.source                 = 'AR'
1422       AND    JZVRS.reporting_status_id      = JZVTD.reporting_status_id
1423       AND    (  (P_REPORT_NAME = 'JEESPMOR' AND JZVRS.tax_calendar_period in (
1424               SELECT RPS1.tax_calendar_period
1425               FROM JG_ZZ_VAT_REP_STATUS RPS1,
1426                    (Select min(vat_reporting_entity_id) vat_reporting_entity_id,
1427                            min(period_start_date) period_start_date
1428                     From JG_ZZ_VAT_REP_STATUS
1429                     Where vat_reporting_entity_id = p_vat_rep_entity_id
1430                     And tax_calendar_period = p_tax_period) RPS2,
1431                    (Select min(vat_reporting_entity_id) vat_reporting_entity_id,
1432                           min(period_end_date) period_end_date
1433                     From JG_ZZ_VAT_REP_STATUS
1434                     Where vat_reporting_entity_id = p_vat_rep_entity_id
1435                     And tax_calendar_period = p_tax_period_to) RPS3
1436               WHERE RPS1.vat_reporting_entity_id = p_vat_rep_entity_id
1437                 AND RPS2.vat_reporting_entity_id = RPS1.vat_reporting_entity_id
1438                 AND RPS3.vat_reporting_entity_id = RPS2.vat_reporting_entity_id
1439                 AND trunc(RPS1.period_start_date) >=
1440                                trunc(RPS2.period_start_date)
1441                 AND trunc(RPS1.period_end_date) <= trunc(RPS3.period_end_date)
1442               GROUP by RPS1.tax_calendar_period
1443                                                                          ))
1444              OR (P_REPORT_NAME = 'JEESAMOR' AND JZVRS.tax_calendar_year       = P_TAX_YEAR ))
1445        AND RA.CASH_RECEIPT_ID = CR.CASH_RECEIPT_ID
1446        AND JZVTD.trx_id = RA.APPLIED_CUSTOMER_TRX_ID
1447        AND CR.RECEIPT_METHOD_ID = fnd_profile.value('JEES_MOD347_RECEIPT_METHOD');
1448      END IF;
1449 
1450      RETURN( NVL(v_cash_received_amount,0));
1451 
1452     RETURN NULL;
1453     EXCEPTION
1454     WHEN OTHERS THEN
1455       RETURN( 0 );
1456 
1457   END AR347_CASH_RECEIVEDFormula;
1458 
1459 
1460 FUNCTION AR340_CASH_RECEIVEDFormula ( p_declared_nif              IN VARCHAR2,
1461 	                                        p_transaction_code        IN VARCHAR,
1462 	                                        p_trx_id                  IN NUMBER,
1463 	                                        p_year_of_amts_rec_cash   IN NUMBER)
1464 	  RETURN NUMBER is
1465 	     v_cash_received_amount number;
1466 
1467 	  BEGIN
1468 
1469 
1470 	SELECT SUM(NVL(DECODE(CR.type,'MISC', 0
1471 	                              ,DECODE(RA.status,'APP',RA.AMOUNT_APPLIED,0)),0))
1472 	             / COUNT(JZVTD.trx_id)
1473 	      INTO   v_cash_received_amount
1474 	      FROM   jg_zz_vat_trx_details JZVTD, jg_zz_vat_rep_status JZVRS
1475 	           , hz_cust_site_uses_all  HCSU
1476 	           , zx_lines_det_factors ZXDF
1477 	           , ar_receivable_applications_all RA
1478 	           , ar_cash_receipts_all CR
1479 	           , ra_customer_trx_all trx
1480 	      WHERE  JZVTD.billing_tp_taxpayer_id = p_declared_nif
1481 	      AND    HCSU.cust_acct_site_id       =   JZVTD.billing_tp_address_id
1482 	      AND    UPPER(HCSU.site_use_code)    = 'LEGAL'
1483 	      AND    JZVTD.extract_source_ledger = 'AR'
1484 	      AND    JZVTD.event_class_code not in ('DEBIT_MEMO', 'APP', 'ADJ')
1485 	      AND    HCSU.primary_flag            = 'Y'
1486 	      AND    SUBSTR(NVL(JZVTD.tax_rate_vat_trx_type_code,'QQQQQQ'),1,3) <> 'RET'
1487 	      AND    (SUBSTR(ZXDF.trx_business_category, INSTR(ZXDF.trx_business_category,'MOD')+3,
1488                       DECODE(INSTR(ZXDF.trx_business_category,'/',1,3),0,length(ZXDF.trx_business_category),
1489                       INSTR(ZXDF.trx_business_category,'/',1,3) - (INSTR(ZXDF.trx_business_category,'MOD')+3)))
1490 	             IN ('347PR','415_347PR','347','415_347')
1491                          OR substr(zxdf.document_sub_type,1,6) = 'MOD340')
1492             AND    nvl(zxdf.document_sub_type,'X') <> 'MOD340_EXCL'
1493 	      AND    JZVTD.trx_id = ZXDF.trx_id
1494 	      AND    JZVTD.trx_id = trx.customer_trx_id
1495 	      AND    JZVTD.trx_id = p_trx_id
1496 	      AND    nvl(trx.global_attribute15,1) = nvl(p_year_of_amts_rec_cash,1)
1497 	      AND    JZVTD.trx_line_id = ZXDF.trx_line_id
1498 	      AND  decode( instr(zxdf.user_defined_fisc_class,'NONE',7,1),7,' ', substr(zxdf.user_defined_fisc_class,7,1) ) = p_transaction_code
1499 	      AND    JZVRS.VAT_REPORTING_ENTITY_ID = P_VAT_REP_ENTITY_ID
1500 	      AND    JZVRS.source                 = 'AR'
1501 	      AND    JZVRS.reporting_status_id      = JZVTD.reporting_status_id
1502 	      AND     P_REPORT_NAME = 'JEESAMOR' AND JZVRS.tax_calendar_year       = P_TAX_YEAR
1503 	      AND RA.CASH_RECEIPT_ID = CR.CASH_RECEIPT_ID
1504 	      AND JZVTD.trx_id = RA.APPLIED_CUSTOMER_TRX_ID
1505 	      AND CR.RECEIPT_METHOD_ID = fnd_profile.value('JEES_MOD347_RECEIPT_METHOD');
1506 
1507 
1508 	     RETURN( NVL(v_cash_received_amount,0));
1509 
1510 	    RETURN NULL;
1511 	    EXCEPTION
1512 	    WHEN OTHERS THEN
1513 	      RETURN( 0 );
1514 
1515 	  END AR340_CASH_RECEIVEDFormula;
1516 
1517   /* Bug 8485057 mark records (jg_info_n4 = 1) that do not satisfy the minimum amount parameter.
1518      Transaction codes(Tipos) A, B are subject to minimum amount. Tipos F,G are not.
1519        Note that there might be a record tipo '3' corresponding to the tipo passed as parameter and
1520        that should also be deleted */
1521   PROCEDURE MOD347_MIN_AMOUNT ( p_tipo IN VARCHAR2 ) IS
1522 
1523   BEGIN
1524    fnd_file.put_line(FND_FILE.LOG,'G_MIN_VALUE1 :'||G_MIN_VALUE);
1525     UPDATE JG_ZZ_VAT_TRX_GT JZVTG
1526     SET    JZVTG.jg_info_n4 = 1
1527     WHERE JZVTG.jg_info_v11  IN (
1528                SELECT    JZVTG1.jg_info_v11
1529                FROM     JG_ZZ_VAT_TRX_GT JZVTG1
1530                WHERE    JZVTG1.jg_info_v1= p_tipo
1531                AND      NVL(JZVTG.jg_info_n4,0) <> 1  -- record was not 'marked'
1532                GROUP BY JZVTG1.jg_info_v11,JZVTG1.jg_info_v1
1533                HAVING   SUM(NVL(NVL(JZVTG1.jg_info_n2,JZVTG1.jg_info_n1),0)) <= NVL(G_MIN_VALUE,0))  -- 347-AP,415-AP   ln_sum_trx_line_amt
1534     AND (JZVTG.jg_info_v1 = p_tipo OR (JZVTG.jg_info_v1 = '3' AND JZVTG.jg_info_v6 = p_tipo))
1535     AND NVL(JZVTG.jg_info_n4,0) <> 1;
1536 
1537   END MOD347_MIN_AMOUNT;
1538 
1539   /* Bug 8485057 update records that do not satisfy the minimum cash amount received parameter
1540      Transaction codes(Tipos) B, F are subject to minimum amount. Tipos A,G are not. */
1541   PROCEDURE MOD347_MIN_CASH_AMOUNT ( p_tipo IN VARCHAR2 ) IS
1542 
1543   BEGIN
1544     UPDATE JG_ZZ_VAT_TRX_GT JZVTG
1545     SET    JZVTG.jg_info_n6 = 0, JZVTG.jg_info_n28 = 0  -- Cash amount received does not meet the minimum -- bug 14725974
1546     WHERE  JZVTG.jg_info_v11  IN
1547               (SELECT   JZVTG1.jg_info_v11
1548                FROM     JG_ZZ_VAT_TRX_GT JZVTG1
1549                WHERE    JZVTG1.jg_info_v1= p_tipo  -- Tipo
1550                AND      NVL(JZVTG1.jg_info_n4,0) <> 1
1551                GROUP BY JZVTG1.jg_info_v11,JZVTG1.jg_info_v1
1552                HAVING   SUM(NVL(JZVTG1.jg_info_n6,0)) <= NVL(P_MIN_CASH_AMOUNT_VALUE,0))
1553     AND JZVTG.jg_info_v1 = p_tipo
1554     AND NVL(JZVTG.jg_info_n4,0) <> 1
1555     AND NVL(JZVTG.jg_info_n6,0) <> 0;
1556 
1557   END MOD347_MIN_CASH_AMOUNT;
1558 
1559   PROCEDURE MOD340_MIN_CASH_AMOUNT ( p_transaction_code IN VARCHAR2 ) IS
1560 
1561   BEGIN
1562      UPDATE JG_ZZ_VAT_TRX_GT JZVTG
1563      SET    JZVTG.jg_info_n6 = 0   -- Cash amount received does not meet the minimum
1564      WHERE  JZVTG.jg_info_v4  IN
1565 	              (SELECT   JZVTG1.jg_info_v4
1566 	               FROM     JG_ZZ_VAT_TRX_GT JZVTG1
1567 	               WHERE    JZVTG1.jg_info_v10= p_transaction_code
1568                        AND      JZVTG1.jg_info_v9 in ('E','F')
1569 	               GROUP BY JZVTG1.jg_info_v4,JZVTG1.jg_info_v10
1570 	               HAVING   SUM(NVL(JZVTG1.jg_info_n6,0)) <= NVL(P_MIN_CASH_AMOUNT_VALUE,0))
1571      AND JZVTG.jg_info_v10 = p_transaction_code
1572      AND JZVTG.jg_info_v9 in ('E','F')
1573      AND NVL(JZVTG.jg_info_n6,0) <> 0;
1574 
1575   END MOD340_MIN_CASH_AMOUNT;
1576 
1577 
1578 
1579   -- Set extract dates for the 340
1580   PROCEDURE set_dates IS
1581     l_from  varchar2(2);
1582     l_to    varchar2(2);
1583   BEGIN
1584 
1585     IF p_340_period = '1T' THEN
1586        l_from := '01';
1587        l_to := '03';
1588     ELSIF p_340_period = '2T' THEN
1589        l_from := '04';
1590        l_to := '06';
1591     ELSIF p_340_period = '3T' THEN
1592        l_from := '07';
1593        l_to := '09';
1594     ELSIF p_340_period = '4T' THEN
1595        l_from := '10';
1596        l_to := '12';
1597     ELSE
1598        l_from := p_340_period;
1599        l_to := p_340_period;
1600     END IF;
1601 
1602     SELECT to_date ('01'||l_from||p_tax_year, 'DDMMYYYY'),
1603            last_day(to_date ('01'||l_to||p_tax_year, 'DDMMYYYY'))
1604     INTO   p_340_start_date,
1605            p_340_end_date
1606     FROM   dual;
1607 
1608   END set_dates;
1609 
1610   FUNCTION before_Report
1611   RETURN BOOLEAN
1612   IS
1613 
1614 
1615      /**
1616      author: brathod
1617        Introduced place holder $MODELO_TABLE_LIST$ which can contain additional table required by specific modelo reports
1618        For  MODELO=415 and SOURCE=AP value will be AP_SUPPLIER_SITES APSS to add a filter in MODELO_415 AP report
1619        For  MODELO=347 and SOURCE=AP value will be AP_SUPPLIERS APS, AP_SUPPLIER_SITES APSS to add filter in MODELO_347 AP report
1620      */
1621 
1622      C_JGZZ_MODELO_GENRIC_QUERY CONSTANT VARCHAR2 (32000) :=
1623      '
1624      SELECT	/*+ NO_REWRITE */
1625              $TAX_REGISTRATION_NUM$                             TAX_REGISTRATION_NUM
1626           ,  substr(billing_tp_name,1,80)                       CUSTOMER_NAME
1627 --          ,  JZVTD.billing_tp_address_id                        CUSTOMER_ADDRESS_ID
1628           ,  $ADDRESS_ID$   CUSTOMER_ADDRESS_ID
1629           ,  DECODE( JZVTD.extract_source_ledger
1630                     , ''AP'' , JZVTD.bill_from_party_id
1631                     , JZVTD.billing_trading_partner_id )        BILLING_TRADING_PARTNER_ID
1632          ,  SUM(DECODE(JZVTD.extract_source_ledger
1633                       ,''AP'',DECODE(JZVTD.OFFSET_FLAG,
1634                                ''N'',nvl(JZVTD.taxable_amt_funcl_curr,JZVTD.taxable_amt)*(JZVTD.tax_recovery_rate/100)
1635 			       ,0)
1636 		    ----		,''AR'',DECODE(JZVTD.TAX_LINE_NUMBER, ''1'', NVL(JZVTD.taxable_amt_funcl_curr, 0),0)  Bug 12378790
1637 		     ,''AR'',DECODE(JZVTD.TAX_LINE_NUMBER, ''1'', NVL(JZVTD.taxable_amt_funcl_curr, JZVTD.taxable_amt),0)
1638 		))
1639 		+
1640 		SUM(DECODE(JZVTD.extract_source_ledger
1641 		                ,''AP'', DECODE(NVL(JZVTD.tax_amt_funcl_curr,0)
1642 		                                , 0, JZVTD.tax_amt,
1643                                     JZVTD.tax_amt_funcl_curr)
1644                     ---		, ''AR'', NVL(JZVTD.tax_amt_funcl_curr, 0) Bug 12379705
1645                     , ''AR'', NVL(JZVTD.tax_amt_funcl_curr, JZVTD.tax_amt)
1646                                     ) )
1647 		SUM_TAXABLE_AMT
1648          ,  SUM(DECODE(JZVTD.extract_source_ledger
1649                       ,''AP'',DECODE(JZVTD.OFFSET_FLAG,
1650                                ''N'',nvl(JZVTD.taxable_amt_funcl_curr,JZVTD.taxable_amt)*(JZVTD.tax_recovery_rate/100)
1651 			       ,0)
1652 		---	        , ''AR'',DECODE(JZVTD.TAX_LINE_NUMBER, ''1'', NVL(JZVTD.taxable_amt_funcl_curr, 0),0)  Bug 12378790
1653                     , ''AR'',DECODE(JZVTD.TAX_LINE_NUMBER, ''1'', NVL(JZVTD.taxable_amt_funcl_curr, JZVTD.taxable_amt),0)
1654 		))
1655 		+
1656 		SUM(DECODE(JZVTD.extract_source_ledger
1657 		                ,''AP'', DECODE(NVL(JZVTD.tax_amt_funcl_curr,0)
1658 		                                , 0, JZVTD.tax_amt,
1659                                     JZVTD.tax_amt_funcl_curr)
1660                     ---	    , ''AR'', NVL(JZVTD.tax_amt_funcl_curr, 0)  Bug 12379705
1661                     , ''AR'', NVL(JZVTD.tax_amt_funcl_curr, JZVTD.tax_amt)
1662                                     ) )
1663 		SUM_TRX_LINE_AMT
1664           /**
1665           author: brathod;
1666           Commented as correction_flag is not relevent for Spain transactions
1667           ,  SUM(DECODE(NVL( JZVTD.correction_flag,''N'')
1668                         ,''N'', NVL( JZVTD.taxable_amt, 0)
1669                         , 0))                                   NCORRECTION_AMOUNT
1670           ,  SUM(DECODE(NVL( JZVTD.correction_flag,''N'')
1671                         ,''Y'', NVL( JZVTD.taxable_amt, 0)
1672                         , 0))                                   CORRECTION_AMOUNT
1673           */
1674           ,  SUM(DECODE(JZVTD.es_correction_period,null,
1675                         DECODE(JZVTD.extract_source_ledger,''AP'',
1676                               DECODE(JZVTD.OFFSET_FLAG,''N'',
1677 		                   nvl(JZVTD.taxable_amt_funcl_curr,JZVTD.taxable_amt)*(JZVTD.tax_recovery_rate/100)
1678                                      ,0),
1679                          ---     NVL( JZVTD.taxable_amt, 0)    bug 12379705
1680 						         NVL( JZVTD.taxable_amt_funcl_curr,JZVTD.taxable_amt)
1681                                )
1682                          ,0)
1683                  )   NCORRECTION_AMOUNT
1684           ,  SUM(DECODE(JZVTD.es_correction_period , null,
1685                         0,
1686                         DECODE(JZVTD.extract_source_ledger,''AP'',
1687                                DECODE(JZVTD.OFFSET_FLAG,''N'',
1688 				nvl(JZVTD.taxable_amt_funcl_curr,JZVTD.taxable_amt)*(JZVTD.tax_recovery_rate/100)                                      ,0)
1689                  ---             ,NVL( JZVTD.taxable_amt, 0)  bug 12379705
1690 				                 ,NVL( JZVTD.taxable_amt_funcl_curr,JZVTD.taxable_amt)
1691                                )
1692                         )
1693                   )      CORRECTION_AMOUNT
1694              $CORRECTION_TRX_SEL_COL$
1695              $PROPERTY_FLAG_SEL_COL$
1696              $TRANS_CODE_SEL_COL$
1697              $TRANSMISSION_PROPERTY_AMT$
1698              $YEAR_OF_AMTS_RECEIVED_CASH$
1699        FROM     jg_zz_vat_rep_status    JZVRS
1700             ,   jg_zz_vat_trx_details   JZVTD
1701                 $MODELO_TABLE_LIST$
1702        WHERE    JZVRS.vat_reporting_entity_id  = $P_VAT_REP_ENTITY_ID$
1703        --AND   JZVRS.reporting_status_id      = JZVTD.reporting_status_id
1704        AND     JZVTD.reporting_status_id in (SELECT DISTINCT JZRS.reporting_status_id JZRS
1705 				     FROM jg_zz_vat_rep_status JZRS
1706 				     WHERE JZRS.vat_reporting_entity_id = $P_VAT_REP_ENTITY_ID$
1707 				     AND   JZRS.source IN ( ''AP'', ''AR'' ))
1708        AND      JZVTD.extract_source_ledger    IN ( ''AP'', ''AR'' )
1709        AND      JZVRS.source                   IN ( ''AP'', ''AR'' )
1710        /**
1711         author: brathod
1712         date  : 18/5/2006
1713         Commented date based filtering and introduced conditional filtering based on report type.
1714         AND      JZVTD.tax_invoice_date    BETWEEN $PERIOD_FROM_DATE$
1715                                           AND     $PERIOD_TO_DATE$
1716 
1717        */
1718        AND      $FILTER_KEY$ $FILTER_OPER$ $FILTER_VALUE$
1719        $MODELO_SPECIFIC_FILTERS$
1720        GROUP BY $TAX_REGISTRATION_NUM$
1721               , substr(billing_tp_name,1,80)
1722               ,  $ADDRESS_ID$
1723               , DECODE( JZVTD.extract_source_ledger
1724                       , ''AP'' , JZVTD.bill_from_party_id
1725                       , JZVTD.billing_trading_partner_id )
1726               --,NVL(JZVTD.taxable_amt_funcl_curr,JZVTD.taxable_amt)
1727               $CORRECTION_TRX_GRP_COL$ $PROPERTY_FLAG_GRP_COL$ $TRANS_CODE_GRP_COL$ $YEAR_OF_AMTS_REC_CASH_GRP_COL$ $MODELO_SPECIFIC_GRP_FILTER$
1728               ';
1729 
1730 
1731       C_MOD415_AP_FILTER CONSTANT VARCHAR2(4000) :=
1732     ' AND    JZVTD.extract_source_ledger        =  ''AP''
1733       AND    JZVRS.source = ''AP''
1734       AND    JZVTD.trx_line_type                <> ''AWT''
1735       /** author:brathod; removed posted_flag checking as it is not relevent in R12*/
1736       --AND    JZVTD.posted_flag                  IN (''P'',''Y'')
1737       AND    JZVTD.trx_line_class <> ''DEBIT''
1738       --AND    NVL(JZVTD.billing_tp_tax_reporting_flag, ''N'') = ''Y''
1739      -- AND    JZVTD.invoice_report_type          IN ( ''415''
1740     --                                            , ''415_347''
1741     --                                               , ''415_347PR''
1742     --                                               )
1743      /** author: brathod
1744       Added following conditions to check for TAX_REPORTING_SITE_FLAG
1745       */
1746       AND   JZVTD.BILLING_TRADING_PARTNER_ID =  APSS.VENDOR_ID
1747       AND   APSS.TAX_REPORTING_SITE_FLAG     =  ''Y''
1748       AND   APSS.ORG_ID                      = $P_ORG_ID$
1749       -- FH: Added for Modelo Project
1750       AND     jzvtd.trx_id = api.invoice_id
1751       AND     jzvtd.trx_id = zxdf.trx_id
1752       AND     jzvtd.trx_line_id = zxdf.trx_line_id
1753       AND     nvl(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3),substr(JZVTD.invoice_report_type,1,3 )) = ''415''';
1754 
1755       C_MOD415_AR_FILTER constant VARCHAR2(4000) :=
1756       '      AND    JZVTD.extract_source_ledger        = ''AR''
1757       AND	JZVRS.source = ''AR''
1758       AND    JZVTD.trx_line_type                <> ''AWT''
1759       -- AND    JZVTD.accounting_date              IS NOT NULL --bug5557860
1760       --AND    JZVTD.posted_flag                  = ''N''   /** author:brathod; removed posted_flag checking as it is not relevent in R12 */
1761       --AND    JZVTD.tax_rate_vat_trx_type_code   <> ''DEBIT''
1762       AND    JZVTD.trx_line_class <> ''DEBIT''
1763       AND    SUBSTR(NVL(JZVTD.tax_rate_vat_trx_type_code,''QQQQQQ''),1,6) <> ''RET_AR''
1764 --      AND    JZVTD.invoice_report_type          IN ( ''415''
1765 --                                                   , ''415_347''
1766 --                                                   , ''415_347PR''
1767 --                                                   )
1768       AND JZVTD.BILLING_TRADING_PARTNER_ID  =   hzca.cust_account_id
1769       AND hzca.cust_account_id  = hzcas.cust_account_id
1770       AND  hzcsu.cust_acct_site_id  = hzcas.cust_acct_site_id
1771       AND upper(hzcsu.site_use_code) = ''LEGAL''
1772       AND hzcsu.primary_flag         = ''Y''
1773       AND hzcsu.status               = ''A''
1774       AND hzcsu.ORG_ID               = $P_ORG_ID$
1775 -- FH: Added for modelo project
1776       AND     nvl(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3),substr(JZVTD.invoice_report_type,1,3 )) = ''415''
1777       AND     jzvtd.trx_id = zxdf.trx_id
1778       AND     jzvtd.trx_line_id = zxdf.trx_line_id'
1779       ;
1780 
1781       C_MOD347_AP_FILTER CONSTANT VARCHAR2(4000) :=
1782       'AND    JZVTD.extract_source_ledger           =  ''AP''
1783        AND	  JZVRS.source = ''AP''
1784        AND    JZVTD.trx_line_type                   <> ''AWT''
1785        AND    JZVTD.trx_line_class                  <>  ''EXPENSE REPORTS''
1786        AND    JZVTD.applied_from_line_id         IS NULL
1787        AND    SUBSTR(NVL(JZVTD.tax_rate_vat_trx_type_code,''QQQQQQ''),1,3) <> ''RET''
1788        -- Bug 8485057 verify invoice_report_type directly from Trx Tax Lines
1789        AND    SUBSTR(zxdf.trx_business_category, INSTR(zxdf.trx_business_category,''MOD'')+3,
1790               DECODE(INSTR(zxdf.trx_business_category,''/'',1,3),0,length(zxdf.trx_business_category),
1791                  INSTR(zxdf.trx_business_category,''/'',1,3) - (INSTR(zxdf.trx_business_category,''MOD'')+3)))
1792                  IN ( ''347'', ''415_347'', ''347PR'', ''415_347PR'')
1793        AND    JZVTD.trx_id = API.invoice_id
1794        AND    JZVTD.trx_id = ZXDF.trx_id
1795        AND    JZVTD.trx_line_id = ZXDF.trx_line_id
1796       /** author: brathod
1797       Added following conditions to check for FEDERAL_REPORTABLE_FLAG and TAX_REPORTING_SITE_FLAG
1798       */
1799       AND   JZVTD.BILLING_TRADING_PARTNER_ID       =   APS.VENDOR_ID
1800       AND   APS.VENDOR_ID                          =  APSS.VENDOR_ID
1801       AND   NVL(APS.FEDERAL_REPORTABLE_FLAG,''Y'')  =  ''Y''
1802       AND   APSS.TAX_REPORTING_SITE_FLAG            =  ''Y''
1803       AND   APSS.ORG_ID                             = $P_ORG_ID$';
1804 
1805       C_MOD347_AR_FILTER CONSTANT VARCHAR2(4000) :=
1806       '  AND    JZVTD.extract_source_ledger        =  ''AR''
1807          AND    JZVRS.source = ''AR''
1808       -- AND    JZVTD.accounting_date              IS NOT NULL --bug5557860
1809       -- AND    JZVTD.posted_flag                  = ''N'' /** author:brathod; removed posted_flag checking as it is not relevent in R12*/
1810       -- AND    JZVTD.tax_rate_vat_trx_type_code   <> ''DEBIT''
1811          AND    JZVTD.trx_line_class <> ''DEBIT''
1812       -- AND    NVL(JZVTD.billing_tp_tax_reporting_flag, ''N'') = ''Y''
1813          AND    SUBSTR(NVL(JZVTD.tax_rate_vat_trx_type_code,''QQQQQQ''),1,3) <> ''RET''
1814          -- Bug 8485057 verify invoice_report_type directly from Trx Tax Lines
1815          AND    SUBSTR(ZXDF.trx_business_category, INSTR(ZXDF.trx_business_category,''MOD'')+3,
1816                     DECODE(INSTR(ZXDF.trx_business_category,''/'',1,3),0,length(ZXDF.trx_business_category),
1817                     INSTR(ZXDF.trx_business_category,''/'',1,3) - (INSTR(ZXDF.trx_business_category,''MOD'')+3)))
1818                   IN ( ''347'', ''415_347'', ''347PR'', ''415_347PR'')
1819          AND    JZVTD.trx_id = ZXDF.trx_id
1820          AND    JZVTD.trx_line_id = ZXDF.trx_line_id
1821          AND    JZVTD.trx_id = TRX.customer_trx_id
1822 	       AND    JZVTD.BILLING_TRADING_PARTNER_ID  =   hzca.cust_account_id
1823          AND    hzca.cust_account_id  = hzcas.cust_account_id
1824          AND    hzcsu.cust_acct_site_id  = hzcas.cust_acct_site_id
1825  	       AND    upper(hzcsu.site_use_code) = ''LEGAL''
1826 	       AND    hzcsu.primary_flag         = ''Y''
1827 	       AND    hzcsu.status               = ''A''
1828          AND    hzcsu.ORG_ID               = $P_ORG_ID$';
1829 
1830 	  -- BUG 9765740 Spanish Modelo 349 ESL EPL Changes
1831       C_MOD349_AP_FILTER CONSTANT VARCHAR2(4000) :=
1832       '      AND    JZVTD.extract_source_ledger        = ''AP''
1833       AND	JZVRS.source = ''AP''
1834       --AND    JZVTD.TAX_RATE_REGISTER_TYPE_CODE       = ''TAX'' --JZVTD.tax_rate_vat_trx_type_code
1835       --AND    JZVTD.tax_recoverable_flag         = ''Y''
1836       AND    JZVTD.invoice_report_type          IN ( ''349'')
1837       AND    JZVTD.intra_eu_trx_type_code IN (''G'', ''S'')';
1838 
1839       -- BUG 9765740 Spanish Modelo 349 ESL EPL Changes
1840       C_MOD349_AR_FILTER  VARCHAR2(4000) :=
1841       '      AND    JZVTD.extract_source_ledger        = ''AR''
1842       AND	JZVRS.source = ''AR''
1843       AND    JZVTD.TAX_RATE_REGISTER_TYPE_CODE  = ''TAX'' --JZVTD.tax_rate_vat_trx_type_code
1844       --AND    JZVTD.tax_recoverable_flag         = ''Y'' /** author:brathod; commented as no such check found in R11i */
1845       AND    JZVTD.invoice_report_type          IN ( ''349'')
1846       AND    JZVTD.intra_eu_trx_type_code IN (''G'', ''T'', ''S'')';
1847 
1848       /* BUG 9765740 Spanish Modelo 349 ESL EPL Changes
1849       C_CORRECTION_TRX_SEL_COLS CONSTANT VARCHAR2(4000) :=
1850       ',   JZVTD.es_correction_year                CORRECTION_YEAR
1851        ,   JZVTD.es_correction_period              CORRECTION_PERIOD
1852        ,   DECODE( JZVTD.triangulation,''Y'',''X'',NULL) TRIANGULATION
1853        ,   DECODE(TO_CHAR(JZVTD.TRX_DATE, ''MM''),''01'',''1T'',''02'',''1T'',''03'',''1T'',
1854                                                ''04'',''2T'',''05'',''2T'',''06'',''2T'',
1855                                                ''07'',''3T'',''08'',''3T'',''09'',''3T'',
1856                                                ''10'',''4T'',''11'',''4T'',''12'',''4T'') TRX_PERIOD '; -- Bug 5525421
1857 
1858       C_CORRECTION_TRX_GRP_COLS CONSTANT VARCHAR2(4000) :=
1859       ',   JZVTD.es_correction_year
1860        ,   JZVTD.es_correction_period
1861        ,   DECODE( JZVTD.triangulation,''Y'',''X'',NULL)
1862        ,   DECODE(TO_CHAR(JZVTD.TRX_DATE, ''MM''),''01'',''1T'',''02'',''1T'',''03'',''1T'',
1863                                                ''04'',''2T'',''05'',''2T'',''06'',''2T'',
1864                                                ''07'',''3T'',''08'',''3T'',''09'',''3T'',
1865                                                ''10'',''4T'',''11'',''4T'',''12'',''4T'')
1866        order by  TRX_PERIOD';  -- Bug 5525421
1867 
1868      */
1869 
1870 
1871       C_CORRECTION_TRX_SEL_COLS  VARCHAR2(4000);
1872 
1873       C_CORRECTION_TRX_GRP_COLS  VARCHAR2(4000);
1874 
1875       C_CORRECTION_TRX_NULL_COLS CONSTANT VARCHAR2(4000) :=
1876       ',   NULL   correction_year
1877        ,   NULL   correction_period
1878        ,   NULL   triangulation
1879        ,   NULL   trx_period   ';
1880 
1881       C_347_SEL_COL  VARCHAR2(4000) :=
1882       ', DECODE (INSTR(NVL(zxdf.trx_business_category,''N''),''PR''),
1883                        0,''N'',''Y'')       PROPERTY_RETAIL_FLAG';
1884 
1885       C_NULL_SEL_COL  VARCHAR2(4000) :=
1886       ', NULL    PROPERTY_RETAIL_FLAG      ';
1887 
1888       /*  Updated filter to include G_MIN_VALUE parameter instead of hardcoded 0 value */
1889       C_MOD347415_GRP_FILTER CONSTANT VARCHAR2(1000) :=
1890       'HAVING  SUM(DECODE(NVL(JZVTD.taxable_amt_funcl_curr , 0)
1891                     , 0 ,JZVTD.taxable_amt
1892                     , JZVTD.taxable_amt_funcl_curr     )) >= $G_MIN_VALUE$ '; --0
1893 
1894       --
1895       lc_vat_code_details     VARCHAR2(240);
1896       lc_func_curr_code       VARCHAR2(240);
1897       lc_rep_legal_entity     VARCHAR2(240);
1898       lc_trx_num              VARCHAR2(240);
1899       ln_rep_legal_entity_id  NUMBER;
1900       ld_period_start_date    DATE;
1901       ld_period_end_date      DATE;
1902       lc_prev_vat_code        VARCHAR2(400) :='';
1903       lc_count                NUMBER        := 0;
1904       lc_reporting_mode       VARCHAR2(240);
1905       --
1906       lc_taxpayer_id          jg_zz_vat_trx_details.billing_tp_taxpayer_id%TYPE;
1907       lc_company_name         xle_registrations.registered_name%TYPE;
1908       lc_registration_number  xle_registrations.registration_number%TYPE;
1909       lc_country              hr_locations.country%TYPE;
1910       lc_address1             hr_locations.address_line_1%TYPE;
1911       lc_address2             hr_locations.address_line_2%TYPE;
1912       lc_address3             hr_locations.address_line_3%TYPE;
1913       lc_address4             hz_locations.address4%TYPE;
1914       lc_city                 hr_locations.town_or_city%TYPE;
1915       lc_postal_code          hr_locations.postal_code%TYPE;
1916       lc_postal_code1          hr_locations.postal_code%TYPE;
1917       lc_contact              hz_parties.party_name%TYPE;
1918       lc_phone_number         hz_contact_points.phone_number%TYPE;
1919       -- Added for Glob-006 ER
1920       l_province                      VARCHAR2(120);
1921       l_comm_num                      VARCHAR2(30);
1922       l_vat_reg_num                   VARCHAR2(50);
1923 
1924       --
1925       lc_jgzz_modelo_query            VARCHAR2(32000);
1926       lc_jgzz_modelo_query1           VARCHAR2(32000);
1927       -- Added for Modelo 340
1928       lc_jgzz_mod_query_340           VARCHAR2(32000);
1929       lc_jgzz_mod_query_340_exp       VARCHAR2(32000);
1930       lc_jgzz_mod_query_340_tax       VARCHAR2(1000);
1931       --
1932       lc_clave_operation              JG_ZZ_VAT_TRX_GT.jg_info_v11%TYPE;
1933       lc_tax_registration_number      JG_ZZ_VAT_TRX_GT.jg_info_v11%TYPE;
1934       lc_customer_name                JG_ZZ_VAT_TRX_GT.jg_info_v11%TYPE;
1935       lc_customer_address_id          JG_ZZ_VAT_TRX_GT.jg_info_v11%TYPE;
1936       lc_billing_trading_partner_id   JG_ZZ_VAT_TRX_GT.jg_info_v11%TYPE;
1937       ln_sum_taxable_amt              JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
1938       ln_sum_taxable_amt_qtr1         JG_ZZ_VAT_TRX_GT.jg_info_n16%TYPE;
1939       ln_sum_taxable_amt_qtr2         JG_ZZ_VAT_TRX_GT.jg_info_n18%TYPE;
1940       ln_sum_taxable_amt_qtr3         JG_ZZ_VAT_TRX_GT.jg_info_n20%TYPE;
1941       ln_sum_taxable_amt_qtr4         JG_ZZ_VAT_TRX_GT.jg_info_n22%TYPE;
1942 
1943 
1944       ln_sum_trx_line_amt             JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
1945       ln_sum_trx_line_amt_q1          JG_ZZ_VAT_TRX_GT.jg_info_n17%TYPE;
1946       ln_sum_trx_line_amt_q2          JG_ZZ_VAT_TRX_GT.jg_info_n19%TYPE;
1947       ln_sum_trx_line_amt_q3          JG_ZZ_VAT_TRX_GT.jg_info_n21%TYPE;
1948       ln_sum_trx_line_amt_q4          JG_ZZ_VAT_TRX_GT.jg_info_n23%TYPE;
1949 
1950       ln_ncorrection_amount           JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
1951       ln_correction_amount            JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
1952       lc_correction_year              JG_ZZ_VAT_TRX_GT.jg_info_v11%TYPE;
1953       lc_correction_period            JG_ZZ_VAT_TRX_GT.jg_info_v11%TYPE;
1954       lc_triangulation                JG_ZZ_VAT_TRX_GT.jg_info_v11%TYPE;
1955       lc_trx_period		      VARCHAR2(2);
1956       lc_print_year		      NUMBER;
1957       lc_print_period		      VARCHAR2(2);
1958       lc_property_retail_flag         JG_ZZ_VAT_TRX_GT.jg_info_v11%TYPE;
1959       lc_address_detail               JG_ZZ_VAT_TRX_GT.jg_info_v11%TYPE;
1960       --
1961       ln_base_imponiable    NUMBER;
1962       ln_running_total      NUMBER;
1963       ln_formerly_decl_amt  NUMBER;
1964 
1965       ln_arrenda_amount             NUMBER;
1966       ln_arrenda_amt_q1             NUMBER;
1967       ln_arrenda_amt_q2             NUMBER;
1968       ln_arrenda_amt_q3             NUMBER;
1969       ln_arrenda_amt_q4             NUMBER;
1970 
1971       ln_year_of_amts_received_cash NUMBER;
1972 
1973       --
1974       lc_sign               VARCHAR2(1);
1975       --
1976       lc_codigo_postal      VARCHAR2(150);
1977       ln_property_location_code    NUMBER;
1978       lc_ref_catastral      VARCHAR2(150);
1979       l_ref_catastral              VARCHAR2(150);
1980       lc_street_type        VARCHAR2(150);
1981       lc_street             VARCHAR2(150);
1982       lc_number             VARCHAR2(150);
1983       lc_escalera           VARCHAR2(150);
1984       lc_piso               VARCHAR2(150);
1985       lc_puerta             VARCHAR2(150);
1986       --Added for Modelo 347
1987       ln_transmission_property_amt NUMBER;
1988       ln_transmission_prop_amt_q1 NUMBER;
1989       ln_transmission_prop_amt_q2 NUMBER;
1990       ln_transmission_prop_amt_q3 NUMBER;
1991       ln_transmission_prop_amt_q4 NUMBER;
1992       ln_cash_received_amount      NUMBER;
1993       -- Added for Modelo 340
1994       lc_key_id                     JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
1995       lc_foreign_taxpayer_id        jg_zz_vat_trx_details.billing_tp_taxpayer_id%TYPE;
1996       lc_book_type		    VARCHAR2(1);
1997       lc_transaction_code	    VARCHAR2(1);
1998       ld_invoice_date		    JG_ZZ_VAT_TRX_GT.jg_info_d1%TYPE;
1999       ld_trx_date		    JG_ZZ_VAT_TRX_GT.jg_info_d1%TYPE;
2000       lc_doc_seq		    JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2001       lc_intra_type		    VARCHAR2(1);
2002       lc_key_declared		    VARCHAR2(1);
2003       lc_trx_deadline		    JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2004       lc_desc_of_goods	 	    JG_ZZ_VAT_TRX_GT.jg_info_v11%TYPE;
2005       ln_taxable_amt		    JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2006       ln_tax_amt		    JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2007       ln_inv_total_amt		    JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2008       ln_deductable_amt		    JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2009       ln_tax_rate                   JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2010       ln_trx_line_number            JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2011       ln_trx_line_id                JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2012       ln_trx_id                     JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2013       ln_reporting_status_id        JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2014       ln_line_count                 JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2015       ln_surcharge_amount           JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2016       ln_surcharge_rate             JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2017       ln_ar_tax_amt		    JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2018       ln_ar_tax_rate                JG_ZZ_VAT_TRX_GT.jg_info_n11%TYPE;
2019 
2020 
2021       TYPE c_modelo_ext_type IS REF CURSOR ;
2022       c_modelo_ext    c_modelo_ext_type;
2023       --
2024       lc_misc               VARCHAR2(1000);
2025       --
2026       -- Cursor for Modelo 340
2027       TYPE c_modelo_340_type IS REF CURSOR ;
2028       c_modelo_340    c_modelo_340_type;
2029 
2030       -- Cursor for Modelo 340 AR Surcharge processing
2031       TYPE c_modelo_340_artax_type IS REF CURSOR ;
2032       c_modelo_340_artax    c_modelo_340_artax_type;
2033 
2034      Cursor C_End_Date(cp_tax_registration varchar2)  Is
2035      Select max(JZVRS.period_end_date) period_end_date
2036      FROM jg_zz_vat_rep_status JZVRS
2037      WHERE JZVRS.vat_reporting_entity_id = p_vat_rep_entity_id
2038        AND JZVRS.tax_registration_number = cp_tax_registration
2039        AND JZVRS.tax_calendar_period = p_tax_period_to
2040      Group by JZVRS.tax_registration_number;
2041 
2042      Cursor C_TAX_PERIODS Is
2043         SELECT RPS1.tax_calendar_period tax_calendar_period
2044         FROM JG_ZZ_VAT_REP_STATUS RPS1,
2045                    (Select min(vat_reporting_entity_id) vat_reporting_entity_id,
2046                            min(period_start_date) period_start_date
2047                     From JG_ZZ_VAT_REP_STATUS
2048                     Where vat_reporting_entity_id = p_vat_rep_entity_id
2049                     And tax_calendar_period = p_tax_period) RPS2,
2050                    (Select min(vat_reporting_entity_id) vat_reporting_entity_id,
2051                           min(period_end_date) period_end_date
2052                     From JG_ZZ_VAT_REP_STATUS
2053                     Where vat_reporting_entity_id = p_vat_rep_entity_id
2054                     And tax_calendar_period = p_tax_period_to) RPS3
2055         WHERE RPS1.vat_reporting_entity_id = p_vat_rep_entity_id
2056           AND RPS2.vat_reporting_entity_id = RPS1.vat_reporting_entity_id
2057           AND RPS3.vat_reporting_entity_id = RPS2.vat_reporting_entity_id
2058           AND trunc(RPS1.period_start_date) >=
2059                                trunc(RPS2.period_start_date)
2060           AND trunc(RPS1.period_end_date) <= trunc(RPS3.period_end_date)
2061         GROUP by RPS1.tax_calendar_period;
2062         l_period_string varchar2(2000) := NULL;
2063         l_period_count number := 0;
2064 
2065   BEGIN
2066 
2067       -- dbms_output.put_line ('Executing before_report');
2068      fnd_file.put_line(FND_FILE.LOG,'Executing before_report');
2069       g_min_value := fnd_number.canonical_to_number(P_MIN_VALUE);
2070       -- dbms_output.put_line ('Calling funct_curr_legal');
2071 
2072       -- Start: BUG 9765740 Spanish Modelo 349 ESL EPL Changes
2073 
2074       if( p_display_period in ('1T', '2T', '3T', '4T', 'OA')) then
2075          C_CORRECTION_TRX_SEL_COLS  :=
2076       ',   JZVTD.es_correction_year                CORRECTION_YEAR
2077        ,   JZVTD.es_correction_period              CORRECTION_PERIOD
2078        ,   DECODE (jzvtd.intra_eu_trx_type_code,
2079                ''G'', (DECODE (jzvtd.extract_source_ledger, ''AP'', ''A'', ''E'')),
2080                ''S'', (DECODE (jzvtd.extract_source_ledger, ''AP'', ''I'', ''S'')),
2081                ''T'', ''T''
2082               ) TRIANGULATION
2083        ,   DECODE(TO_CHAR(JZVTD.TRX_DATE, ''MM''),''01'',''1T'',''02'',''1T'',''03'',''1T'',
2084                                                ''04'',''2T'',''05'',''2T'',''06'',''2T'',
2085                                                ''07'',''3T'',''08'',''3T'',''09'',''3T'',
2086                                       ''10'',''4T'',''11'',''4T'',''12'',''4T'') TRX_PERIOD '; -- Bug 5525421
2087 
2088          C_CORRECTION_TRX_GRP_COLS  :=
2089       ',   JZVTD.es_correction_year
2090        ,   JZVTD.es_correction_period
2091        ,   DECODE (jzvtd.intra_eu_trx_type_code,
2092                ''G'', (DECODE (jzvtd.extract_source_ledger, ''AP'', ''A'', ''E'')),
2093                ''S'', (DECODE (jzvtd.extract_source_ledger, ''AP'', ''I'', ''S'')),
2094                ''T'', ''T''
2095               )
2096        ,   DECODE(TO_CHAR(JZVTD.TRX_DATE, ''MM''),''01'',''1T'',''02'',''1T'',''03'',''1T'',
2097                                                ''04'',''2T'',''05'',''2T'',''06'',''2T'',
2098                                                ''07'',''3T'',''08'',''3T'',''09'',''3T'',
2099                                                ''10'',''4T'',''11'',''4T'',''12'',''4T'')
2100        order by  TRX_PERIOD';
2101       else
2102        C_CORRECTION_TRX_SEL_COLS  :=
2103       ',   JZVTD.es_correction_year                CORRECTION_YEAR
2104        ,   JZVTD.es_correction_period              CORRECTION_PERIOD
2105        ,   DECODE (jzvtd.intra_eu_trx_type_code,
2106                ''G'', (DECODE (jzvtd.extract_source_ledger, ''AP'', ''A'', ''E'')),
2107                ''S'', (DECODE (jzvtd.extract_source_ledger, ''AP'', ''I'', ''S'')),
2108                ''T'', ''T''
2109               ) TRIANGULATION
2110        ,   TO_CHAR(JZVTD.TRX_DATE, ''MM'') TRX_PERIOD '; -- Bug 5525421
2111 
2112          C_CORRECTION_TRX_GRP_COLS :=
2113       ',   JZVTD.es_correction_year
2114        ,   JZVTD.es_correction_period
2115        ,   DECODE (jzvtd.intra_eu_trx_type_code,
2116                ''G'', (DECODE (jzvtd.extract_source_ledger, ''AP'', ''A'', ''E'')),
2117                ''S'', (DECODE (jzvtd.extract_source_ledger, ''AP'', ''I'', ''S'')),
2118                ''T'',''T''
2119               )
2120        ,   TO_CHAR(JZVTD.TRX_DATE, ''MM'')
2121        order by  TRX_PERIOD';  -- Bug 5525421
2122 
2123       end if;
2124 
2125       -- End: BUG 9765740 Spanish Modelo 349 ESL EPL Changes
2126 
2127 
2128 
2129      If p_tax_Period is not null Then
2130         If G_DEBUG Then
2131            fnd_file.put_line(FND_FILE.LOG,'Fetching periods in a string');
2132         End If;
2133         For C_Tax_Periods_Rec in C_Tax_Periods Loop
2134            If l_period_count = 0 Then
2135               l_period_string := '(''' || C_Tax_Periods_Rec.tax_calendar_period ||'''';
2136            Else
2137               l_period_string :=  l_period_string || ', ''' ||
2138                                   C_Tax_Periods_Rec.tax_calendar_Period ||'''';
2139            End If;
2140            l_period_count := l_period_count + 1;
2141         End Loop;
2142         l_period_string := l_period_string || ')';
2143      End If;
2144 
2145      IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'l_period_string: '|| l_period_string); END IF;
2146 
2147 
2148      IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'jg_zz_common_pkg.funct_curr_legal'); END IF;
2149 
2150      jg_zz_common_pkg.funct_curr_legal( lc_func_curr_code
2151                                      , lc_rep_legal_entity
2152                                      , ln_rep_legal_entity_id
2153                                      , lc_taxpayer_id
2154                                      , p_vat_rep_entity_id
2155                                      , p_tax_period
2156                                      , p_tax_year
2157                                      );
2158      -- dbms_output.put_line ('jg_zz_common_pkg.tax_registration');
2159      IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'jg_zz_common_pkg.tax_registration'); END IF;
2160 
2161      BEGIN
2162      jg_zz_common_pkg.tax_registration( x_tax_registration    => lc_trx_num
2163                                      , x_period_start_date   => ld_period_start_date
2164                                      , x_period_end_date     => ld_period_end_date
2165                                      , x_status              => lc_reporting_mode
2166                                      , pn_vat_rep_entity_id  => p_vat_rep_entity_id
2167                                      , pv_period_name        => p_tax_period
2168                                      , pn_period_year        => p_tax_year
2169                                      , pv_source             => P_SOURCE
2170                                       );
2171      If p_tax_period_to is not null Then
2172         If G_DEBUG THEN
2173            fnd_file.put_line(FND_FILE.LOG,'Getting the Period End ');
2174         End If;
2175         If C_End_Date%IsOpen Then
2176               Close C_End_Date;
2177         End IF;
2178 
2179         Open   C_End_Date(lc_trx_num);
2180         Fetch C_End_Date into ld_period_end_date;
2181         Close C_End_Date;
2182      End If;
2183 
2184      EXCEPTION
2185         WHEN OTHERS THEN
2186         NULL;
2187      END;
2188      -- dbms_output.put_line ('jg_zz_common_pkg.tax_registration');
2189      IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'jg_zz_common_pkg.company_detail'); END IF;
2190 
2191       jg_zz_common_pkg.company_detail(x_company_name            => lc_company_name
2192                                   ,x_registration_number    => lc_registration_number
2193                                   ,x_country                => lc_country
2194                                   ,x_address1               => lc_address1
2195                                   ,x_address2               => lc_address2
2196                                   ,x_address3               => lc_address3
2197                                   ,x_address4               => lc_address4
2198                                   ,x_city                   => lc_city
2199                                   ,x_postal_code            => lc_postal_code
2200                                   ,x_contact                => lc_contact
2201                                   ,x_phone_number           => lc_phone_number
2202                                   ,x_province               => l_province
2203                                   ,x_comm_number            => l_comm_num
2204                                   ,x_vat_reg_num            => l_vat_reg_num
2205                                   ,pn_legal_entity_id       => ln_rep_legal_entity_id
2206                                   ,p_vat_reporting_entity_id => P_VAT_REP_ENTITY_ID);
2207 
2208 
2209 
2210 	IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'JG_ZZ_VAT_REP_UTILITY.get_period_status'); END IF;
2211 
2212 	-- Fetch the reporting mode only if the report name is not null
2213 	IF P_REPORT_NAME IS NOT NULL and p_tax_year is not null
2214           AND p_modelo <> '340' THEN
2215 		lc_reporting_mode :=  JG_ZZ_VAT_REP_UTILITY.get_period_status(pn_vat_reporting_entity_id => p_vat_rep_entity_id
2216 								   ,pv_tax_calendar_period => p_tax_period
2217 								   ,pv_tax_calendar_year => p_tax_year
2218 								   ,pv_source => P_SOURCE
2219 							           ,pv_report_name => P_REPORT_NAME||':'||P_MODELO);
2220         ELSE
2221 	        lc_reporting_mode := NULL;
2222 	END IF;
2223 	IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'lc_reporting_mode: '||lc_reporting_mode); END IF;
2224 
2225      G_CURRENCY_CODE :=   lc_func_curr_code;
2226     /* bug 5729082 start */
2227     /* Bug 10354561 commented below line and added a new condition for 349 template*/
2228     /* IF P_REPORT_NAME = 'JEESAMOR' AND P_MODELO = '347' THEN */
2229     IF P_REPORT_NAME = 'JEESAMOR' AND (P_MODELO = '347' OR P_MODELO = '349') THEN
2230       G_LE_TRN        :=   lc_taxpayer_id;
2231     ELSE
2232       G_LE_TRN        :=   lc_taxpayer_id; -- lc_trx_num ; Bug 13986531
2233     End IF;
2234    /* end bug 5729082 */
2235 
2236      G_LE_NAME       :=   lc_rep_legal_entity;
2237      G_FROM_DATE     :=   ld_period_start_date;
2238      G_TO_DATE       :=   ld_period_end_date;
2239      -- dbms_output.put_line ('SELECT precision INTO G_CUR_PRECISION');
2240      IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'SELECT precision INTO G_CUR_PRECISION'); END IF;
2241 
2242      BEGIN
2243       SELECT precision
2244       INTO G_CUR_PRECISION
2245       FROM fnd_currencies curr
2246       WHERE currency_code = g_currency_code;
2247      EXCEPTION
2248         WHEN NO_DATA_FOUND THEN
2249              NULL;
2250      END;
2251 
2252      IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'G_CUR_PRECISION: '||G_CUR_PRECISION); END IF;
2253 
2254      -- dbms_output.put_line ('P_REPORT_NAME:'||P_REPORT_NAME);
2255      IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'P_REPORT_NAME:'||P_REPORT_NAME); END IF;
2256 
2257      IF P_REPORT_NAME = 'JEESAMOR' AND P_MODELO = '347' THEN
2258      /* Header record information */
2259         lc_address1 := SUBSTR( lc_address1, 1,2);
2260         lc_address2 := SUBSTR( lc_address2, 1,20);
2261         lc_address3 := LPAD(SUBSTR(lc_address3,1,
2262                       NVL(LENGTH(lc_address3), 0) -
2263                       NVL(NVL(LENGTH(LTRIM(TRANSLATE(lc_address3,'123456789','000000000'),'0')), 0),0)),5,'0');
2264         lc_city    := SUBSTR(lc_city,1,30); -- Bug 8485057 changed from 12 to 30 char
2265         lc_postal_code := SUBSTR(lc_postal_code,1,5);
2266         lc_company_name := SUBSTR(lc_company_name,1,40);
2267 
2268      ELSIF P_REPORT_NAME = 'JEESAMOR' AND P_MODELO = '415' THEN
2269         lc_address1 := SUBSTR( lc_address1, 1,2);
2270         lc_address2 := SUBSTR( lc_address2, 1,25);
2271         lc_address3 := LPAD(SUBSTR(lc_address3,1,
2272                       NVL(LENGTH(lc_address3), 0) -
2273                       NVL(NVL(LENGTH(LTRIM(TRANSLATE(lc_address3,'123456789','000000000'),'0')), 0),0)),5,'0');
2274         lc_postal_code := SUBSTR(lc_postal_code,1,5);
2275         lc_company_name := SUBSTR(lc_company_name,1,40);
2276         lc_city    := SUBSTR(lc_city,1,24);
2277      END IF;
2278      -- dbms_output.put_line ('BEFORE INSERT HEADER');
2279      IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'BEFORE INSERT HEADER'); END IF;
2280 
2281      INSERT INTO JG_ZZ_VAT_TRX_GT
2282        (jg_info_v1  -- curr_code
2283        ,jg_info_v2  -- entity_name
2284        ,jg_info_v3  -- taxpayer_id
2285        ,jg_info_v4  -- company_name
2286        ,jg_info_v5  -- registration_number
2287        ,jg_info_v26 -- country
2288        ,jg_info_v27 -- address1
2289        ,jg_info_v28 -- address2
2290        ,jg_info_v29 -- address3
2291        ,jg_info_v31 -- address4
2292        ,jg_info_v32 -- city
2293        ,jg_info_v6  -- postal_code
2294        ,jg_info_v7  -- contact
2295        ,jg_info_v33 -- phone_number
2296        ,jg_info_v8  -- reporting mode
2297        ,jg_info_v9  -- P_TAX_YEAR
2298        ,jg_info_v10 -- P_TAX_PERIOD
2299        ,jg_info_v11 -- P_MODELO
2300        ,jg_info_v35 -- P_SOURCE
2301        ,jg_info_v12 -- P_CONTACT_TEL
2302        ,jg_info_v13 -- P_CONTACT_NAME
2303        ,jg_info_v14 -- P_TAX_OFFICE  -- Bug 8485057 Note: not printed in Modelo 347 magnetic format
2304        ,jg_info_v15 -- P_CONTACT_TEL_CODE
2305        ,jg_info_v16 -- P_REFERENCE_NUMBER
2306        ,jg_info_v17 -- P_MAIN_ACTIVITY
2307        ,jg_info_v18 -- P_MAIN_ACTIVITY_CD
2308        ,jg_info_v19 -- P_SECOND_ACTIVITY
2309        ,jg_info_v20 -- P_SECOND_ACTIVITY_CD
2310        ,jg_info_v21 -- P_TOTAL_PURCHASES
2311        ,jg_info_v22 -- P_TOTAL_SALES
2312        ,jg_info_v23 -- P_TAX_OFF_REG_CODES
2313        ,jg_info_v24 -- P_MEDIUM
2314        ,jg_info_v25 -- P_FORMAT_TYPE
2315        ,jg_info_v34 -- P_PRV_REFERENCE_NUMBER
2316        ,jg_info_n6  -- G_MIN_VALUE
2317        ,jg_info_d1 -- ld_period_start_date
2318        ,jg_info_d2 -- ld_period_end_date
2319        ,jg_info_v30 -- Header record indicator
2320        ,jg_info_v36 -- P_PERIOD
2321        ,jg_info_v37 -- P_ELEC_CODE
2322        ,jg_info_v38 -- P_SUBSTITUTION
2323        ,jg_info_n7  -- P_MIN_CASH_AMOUNT_VALUE Bug 8485057
2324        )
2325      VALUES
2326        (lc_func_curr_code
2327        ,lc_rep_legal_entity
2328        ,lc_taxpayer_id
2329        ,lc_company_name
2330        ,lc_taxpayer_id  --lc_trx_num Bug 13986531
2331        ,lc_country
2332        ,lc_address1
2333        ,lc_address2
2334        ,lc_address3
2335        ,lc_address4
2336        ,lc_city
2337        ,lc_postal_code
2338        ,lc_contact
2339        ,lc_phone_number
2340        ,lc_reporting_mode
2341        ,P_TAX_YEAR
2342        ,P_TAX_PERIOD || ' - ' || P_TAX_PERIOD_TO
2343        ,P_MODELO
2344        ,P_SOURCE
2345        ,P_CONTACT_TEL
2346        ,P_CONTACT_NAME
2347        ,P_TAX_OFFICE
2348        ,P_CONTACT_TEL_CODE
2349        ,P_REFERENCE_NUMBER
2350        ,P_MAIN_ACTIVITY
2351        ,P_MAIN_ACTIVITY_CD
2352        ,P_SECOND_ACTIVITY
2353        ,P_SECOND_ACTIVITY_CD
2354        ,P_TOTAL_PURCHASES
2355        ,P_TOTAL_SALES
2356        ,P_TAX_OFF_REG_CODES
2357        ,P_MEDIUM
2358        ,P_FORMAT_TYPE
2359        ,P_PRV_REFERENCE_NUMBER
2360        ,G_MIN_VALUE
2361        ,ld_period_start_date
2362        ,ld_period_end_date
2363        ,'H'
2364        ,P_340_PERIOD
2365        ,P_ELEC_CODE
2366        ,P_SUBSTITUTION
2367        ,P_MIN_CASH_AMOUNT_VALUE);
2368 
2369     -- dbms_output.put_line ('AFTER INSERT HEADER');
2370     IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'AFTER INSERT HEADER'); END IF;
2371 
2372     /**
2373       author: brathod
2374       date  : 23/05/2006
2375       User has requested "Generic Extract".  Hence no need to apply report specific filters.
2376       Only reporting entity related information is populated in JG_ZZ_VAT_TRX_GT global temp table.
2377       Returning control from here to continue with xml data template query processing
2378     */
2379 
2380     IF (P_REPORT_NAME IS NULL) THEN
2381        return (true);
2382     END IF;
2383 
2384     /**
2385       Control will not come here if GENERIC EXTRACT is executed
2386     */
2387 
2388  -- Separate existing processing for all other Modelos.
2389  -- Modelo 340 processing will be in its own part.
2390  IF P_MODELO <> 340 THEN
2391 
2392     -- Process if Period Report run from AP or if Annual
2393     IF ( P_REPORT_NAME = 'JEESPMOR' AND P_SOURCE = 'AP')
2394        OR P_REPORT_NAME = 'JEESAMOR' THEN
2395 
2396        --
2397        -- Build query to fetch ap extract for modelo from JG
2398        --
2399 
2400        lc_jgzz_modelo_query  := C_JGZZ_MODELO_GENRIC_QUERY;
2401        --
2402        /**
2403         author : brathod
2404         date   : 18/5/2006
2405         Commented following code to remove date filtering.
2406         lc_jgzz_modelo_query :=
2407                REPLACE( lc_jgzz_modelo_query
2408                       , '$PERIOD_FROM_DATE$'
2409                       , 'TO_DATE('''||TO_CHAR(ld_period_start_date, 'DD/MM/YYYY')||''',''DD/MM/YYYY'')' );
2410 
2411         lc_jgzz_modelo_query :=
2412                REPLACE( lc_jgzz_modelo_query
2413                       , '$PERIOD_TO_DATE$'
2414                       , 'TO_DATE('''||TO_CHAR(ld_period_end_date, 'DD/MM/YYYY')||''',''DD/MM/YYYY'')' );
2415 
2416           :: Introduced conditional filtering ::
2417           Based on report type, FILTER_KEY and FILTER_VALUE will have following values
2418           For, Periodic Report Filter_Key = TAX_CALENDAR_PERIOD and Filter_Value = P_TAX_PERIOD
2419                Annual   Report Filter_Key = TAX_CALENDAR_YEAR   and Filter_Value = P_TAX_YEAR
2420 
2421        */
2422         declare
2423           lv_filter_key varchar2 (150) ;
2424           lv_filter_value varchar2 (2000);
2425           lv_filter_oper varchar2(2);
2426         begin
2427           if p_report_name = 'JEESPMOR' then
2428             lv_filter_key := ' JZVTD.trx_date BETWEEN JZVRS.period_start_date and JZVRS.period_end_date
2429 		       AND JZVRS.TAX_CALENDAR_PERIOD'; --5444803
2430 --	    lv_filter_key := ' JZVRS.TAX_CALENDAR_PERIOD'; --5444803
2431 
2432 			--- Bug 11923711
2433 			if (p_driving_date = 'GL'  AND P_MODELO = 349) OR (P_MODELO = 347) then
2434 
2435             lv_filter_key :=
2436             REPLACE( lv_filter_key
2437                     ,'trx_date'
2438                     ,'gl_date'
2439                     );
2440 					fnd_file.put_line(FND_FILE.LOG,'***Updating the C_CORRECTION_TRX_SEL_COLS***:3 ');
2441 			C_CORRECTION_TRX_SEL_COLS := REPLACE( C_CORRECTION_TRX_SEL_COLS
2442 										,'TRX_DATE'
2443 										,'GL_DATE'
2444 										);
2445 
2446 
2447 			C_CORRECTION_TRX_GRP_COLS := REPLACE( C_CORRECTION_TRX_GRP_COLS
2448 										,'TRX_DATE'
2449 										,'GL_DATE'
2450 										);
2451             end if;
2452 
2453             lv_filter_value := l_period_string; --p_tax_period;
2454             lv_filter_oper := 'In';
2455           elsif p_report_name = 'JEESAMOR' then
2456             lv_filter_key := 'JZVTD.trx_date BETWEEN JZVRS.period_start_date and JZVRS.period_end_date
2457 		       AND JZVRS.TAX_CALENDAR_YEAR';  --Bug 5525421
2458 
2459 			--- Bug 11923711
2460 			if (p_driving_date = 'GL'  AND P_MODELO = 349) OR (P_MODELO = 347) then
2461 
2462             lv_filter_key :=
2463             REPLACE( lv_filter_key
2464                     ,'trx_date'
2465                     ,'gl_date'
2466                     );
2467 					fnd_file.put_line(FND_FILE.LOG,'***Updating the C_CORRECTION_TRX_SEL_COLS***:2 ');
2468 			C_CORRECTION_TRX_SEL_COLS := REPLACE( C_CORRECTION_TRX_SEL_COLS
2469 										,'TRX_DATE'
2470 										,'GL_DATE'
2471 										);
2472 
2473 			C_CORRECTION_TRX_GRP_COLS := REPLACE( C_CORRECTION_TRX_GRP_COLS
2474 										,'TRX_DATE'
2475 										,'GL_DATE'
2476 										);
2477             end if;
2478 
2479             lv_filter_value := p_tax_year;
2480             lv_filter_oper := '=';
2481           end if;
2482 
2483         lc_jgzz_modelo_query :=
2484                REPLACE( lc_jgzz_modelo_query
2485                       , '$FILTER_KEY$'
2486                       , lv_filter_key
2487                       );
2488 
2489         lc_jgzz_modelo_query :=
2490                REPLACE( lc_jgzz_modelo_query
2491                       , '$FILTER_OPER$'
2492                       , lv_filter_oper
2493                       );
2494 
2495          lc_jgzz_modelo_query :=
2496                REPLACE( lc_jgzz_modelo_query
2497                       , '$FILTER_VALUE$'
2498                       , lv_filter_value
2499                       );
2500         end;
2501 
2502          lc_jgzz_modelo_query :=
2503                REPLACE( lc_jgzz_modelo_query
2504                       , '$P_VAT_REP_ENTITY_ID$'
2505                       , TO_CHAR(P_VAT_REP_ENTITY_ID)
2506                       );
2507 
2508        IF p_modelo = '347' THEN
2509 
2510           --
2511           -- Replace Place Holder with Modelo 347 - AP Specific Values
2512           --
2513           /** author: brathod
2514             $MODELO_TABLE_LIST$ will have value ", AP_SUPPLIER_SITES APSS , AP_SUPPLIERS APS" [please note comma (,)at begining]
2515             as modelo 347 report needs to filter records based on FEDERAL_REPORTABLE_FLAG for suppliers
2516             and TAX_REPORTING_SITE_FLAG for supplier sites
2517 
2518           */
2519           -- Bug 8485057 added AP_INVOICES_ALL to retrieve the actual current invoice_report_type
2520           lc_jgzz_modelo_query :=
2521              REPLACE( lc_jgzz_modelo_query
2522                     , '$MODELO_TABLE_LIST$'
2523                     ,', AP_SUPPLIER_SITES_ALL APSS, AP_SUPPLIERS APS, AP_INVOICES_ALL API, ZX_LINES_DET_FACTORS ZXDF ' );
2524 
2525           lc_jgzz_modelo_query :=
2526              REPLACE( lc_jgzz_modelo_query
2527                     , '$ADDRESS_ID$'
2528                     ,'APSS.VENDOR_SITE_ID' );
2529 
2530           lc_jgzz_modelo_query :=
2531              REPLACE( lc_jgzz_modelo_query
2532                     , '$TAX_REGISTRATION_NUM$'
2533             --        ,'JZVTD.billing_tp_tax_reg_num' );  /* Commented for bug 5729082 */
2534                       ,'JZVTD.billing_tp_taxpayer_id');   /* added for bug 5729082 */
2535 
2536           lc_jgzz_modelo_query :=
2537              REPLACE( lc_jgzz_modelo_query
2538                     , '$CORRECTION_TRX_SEL_COL$'
2539                     , C_CORRECTION_TRX_NULL_COLS );
2540 
2541           lc_jgzz_modelo_query :=
2542              REPLACE( lc_jgzz_modelo_query
2543                     , '$CORRECTION_TRX_GRP_COL$'
2544                     , '' );
2545 
2546           lc_jgzz_modelo_query :=
2547              REPLACE( lc_jgzz_modelo_query
2548                     , '$MODELO_SPECIFIC_FILTERS$'
2549                     , C_MOD347_AP_FILTER );
2550 
2551           lc_jgzz_modelo_query :=
2552              REPLACE( lc_jgzz_modelo_query
2553                     , '$P_ORG_ID$'
2554                     , P_ORG_ID );
2555 
2556           lc_jgzz_modelo_query :=
2557              REPLACE( lc_jgzz_modelo_query
2558                     , '$MODELO_SPECIFIC_GRP_FILTER$'
2559                     , '');
2560                  -- Bug 8485057 Minimum amount checked in MOD347_MIN_AMOUNT procedure
2561                  --   , C_MOD347415_GRP_FILTER );
2562 
2563           -- Bug 8485057, select Transaction Code, default A for AP transactions
2564           lc_jgzz_modelo_query :=
2565              REPLACE( lc_jgzz_modelo_query
2566                     , '$TRANS_CODE_SEL_COL$'
2567                     , ', DECODE( INSTR(zxdf.trx_business_category,''/'',1,3),0,''A'',
2568          substr(zxdf.trx_business_category,length(zxdf.trx_business_category),1)) TIPO');
2569 
2570           lc_jgzz_modelo_query :=
2571              REPLACE( lc_jgzz_modelo_query
2572                     , '$TRANS_CODE_GRP_COL$'
2573                     , ', DECODE( INSTR(zxdf.trx_business_category,''/'',1,3),0,''A'',
2574          substr(zxdf.trx_business_category,length(zxdf.trx_business_category),1))');
2575 
2576           /* Bug 8485057: property rental flag cannot be in the main query, incorrect number of records
2577           are returned, replaced with NULL */
2578           lc_jgzz_modelo_query :=
2579              REPLACE( lc_jgzz_modelo_query
2580                     , '$PROPERTY_FLAG_SEL_COL$'
2581                    --bug10282300  ,C_NULL_SEL_COL);
2582                     , C_347_SEL_COL );  --bug10282300
2583 
2584           lc_misc := C_347_SEL_COL ; --bug10282300
2585           lc_misc := REPLACE( lc_misc, 'PROPERTY_RETAIL_FLAG',''); --bug10282300
2586 
2587 
2588           lc_jgzz_modelo_query :=
2589              REPLACE( lc_jgzz_modelo_query
2590                     , '$PROPERTY_FLAG_GRP_COL$'
2591                     -- bug10282300 ,'');
2592                     , lc_misc );--bug10282300
2593 
2594 
2595           lc_jgzz_modelo_query :=
2596              REPLACE( lc_jgzz_modelo_query
2597                     , '$TRANSMISSION_PROPERTY_AMT$'
2598                     , ', NULL TRANSMISSION_PROPERTY_AMT');
2599 
2600           lc_jgzz_modelo_query :=
2601              REPLACE( lc_jgzz_modelo_query
2602                     , '$YEAR_OF_AMTS_RECEIVED_CASH$'
2603                     , ', NULL YEAR_OF_AMTS_RECEIVED_CASH');
2604 
2605           lc_jgzz_modelo_query :=
2606              REPLACE( lc_jgzz_modelo_query
2607                     , '$YEAR_OF_AMTS_REC_CASH_GRP_COL$'
2608                     , '');
2609 
2610 
2611 
2612           IF G_DEBUG THEN
2613             fnd_file.put_line(FND_FILE.LOG,'1.3. bEFORE Merge subquery to include expense reports');
2614           END IF;
2615 
2616           --
2617           -- Remove semi colon to merge union select
2618           --
2619           lc_jgzz_modelo_query := REPLACE( lc_jgzz_modelo_query, ';', '');
2620 
2621           --
2622           -- Merge subquery to include expense reports
2623           --
2624 
2625           lc_jgzz_modelo_query1 :=
2626            'UNION
2627            /** author: brathod
2628             added two tables AP_SUPPLIER_SITES APSS , AP_SUPPLIERS APS
2629             as modelo 347 report needs to filter records based on FEDERAL_REPORTABLE_FLAG for suppliers
2630             and TAX_REPORTING_SITE_FLAG for supplier sites
2631           */
2632            SELECT /*+ NO_REWRITE */
2633                    NVL(JZVTD.merchant_party_taxpayer_id,
2634                          JZVTD.billing_tp_taxpayer_id)                      TAX_REGISTRATION_NUM
2635                 ,  NVL(JZVTD.merchant_party_name,JZVTD.billing_tp_name)     CUSTOMER_NAME
2636                 ,  APSS.VENDOR_SITE_ID                            CUSTOMER_ADDRESS_ID
2637                 ,  JZVTD.bill_from_party_id                                 BILLING_TRADING_PARTNER_ID
2638                 , SUM (ROUND (nvl(JZVTD.taxable_amt_funcl_curr,JZVTD.taxable_amt)*(JZVTD.tax_recovery_rate/100)   -- Bug 16502438
2639                 /** author:brathod
2640                   +
2641                   NVL (DECODE (alc.base_amount,
2642                           0, alc.amount,
2643                           NULL, alc.amount,
2644                           alc.base_amount), 0)
2645                 */
2646                 ,2))                             SUM_TAXABLE_AMT
2647                 , 0  SUM_TRX_LINE_AMT
2648                 , 0  NCORRECTION_AMOUNT
2649                 , 0  CORRECTION_AMOUNT
2650                 , NULL
2651                 , NULL
2652                 , NULL
2653                 , NULL
2654                , DECODE (INSTR(NVL(API.trx_business_category,''N''),''PR''),
2655                           0,''N'',''Y'')       PROPERTY_RETAIL_FLAG
2656 --bug10282300
2657 --bug10282300    , NULL
2658                 , DECODE( INSTR(zxdf.trx_business_category,''/'',1,3),0,''A'',
2659                     substr(zxdf.trx_business_category,length(zxdf.trx_business_category),1)) TIPO
2660                 , NULL TRANSMISSION_PROPERTY_AMT
2661                 , NULL YEAR_OF_AMTS_RECEIVED_CASH
2662              FROM     jg_zz_vat_rep_status    JZVRS
2663                   ,   jg_zz_vat_trx_details   JZVTD
2664                   ,   ap_suppliers            aps
2665                   ,   ap_supplier_sites_all   apss
2666                   ,   ap_invoices_all              API
2667                   ,   zx_lines_det_factors    zxdf
2668 	     /**
2669              author:brathod
2670                   ,   ap_invoice_distributions_all AID
2671                   ,   ap_invoice_distributions_all ALC
2672              WHERE    AID.invoice_distribution_id    = JZVTD.trx_line_id
2673              AND      ALC.invoice_distribution_id    = AID.charge_applicable_to_dist_id
2674              */
2675              WHERE    JZVRS.vat_reporting_entity_id  = $P_VAT_REP_ENTITY_ID$
2676              AND      JZVRS.reporting_status_id      = JZVTD.reporting_status_id
2677              AND      JZVRS.source                   = ''AP''
2678              AND      JZVTD.extract_source_ledger    =  ''AP''
2679              AND      JZVTD.merchant_party_name      IS NOT NULL
2680              AND      JZVTD.trx_line_type                NOT IN (''AWT'',''TAX'',''PREPAY'')
2681             --AND      JZVTD.posted_flag                  IN (''P'',''Y'') /** author:brathod; Removed posted_flag checking as it is not relevent in R12 */
2682             --AND      JZVTD.tax_rate_vat_trx_type_code   = ''EXPENSE REPORTS''
2683              AND      JZVTD.trx_line_class = ''EXPENSE REPORTS''
2684              --AND      NVL(JZVTD.billing_tp_tax_reporting_flag, ''N'') = ''Y''
2685              AND      JZVTD.applied_from_line_id         IS NULL
2686              /*  Commented and added period based filtering
2687              AND      JZVTD.start_expense_date           BETWEEN $PERIOD_FROM_DATE$ AND $PERIOD_TO_DATE$
2688              */
2689              AND      $FILTER_KEY$ $FILTER_OPER$ $FILTER_VALUE$
2690              -- Bug 8485057 verify invoice_report_type directly from Invoice
2691              AND      SUBSTR(zxdf.trx_business_category, INSTR(zxdf.trx_business_category,''MOD'')+3,
2692                         DECODE(INSTR(zxdf.trx_business_category,''/'',1,3),0,length(zxdf.trx_business_category),
2693                         INSTR(zxdf.trx_business_category,''/'',1,3) - (INSTR(zxdf.trx_business_category,''MOD'')+3)))
2694                       IN ( ''347'', ''347PR'', ''415_347'', ''415_347PR'')
2695              AND      JZVTD.trx_id = API.invoice_id
2696              AND      jzvtd.trx_id = zxdf.trx_id
2697              AND      jzvtd.trx_line_id = zxdf.trx_line_id
2698              AND      JZVTD.BILLING_TRADING_PARTNER_ID       =   APS.VENDOR_ID
2699              AND      APS.VENDOR_ID         =   APSS.VENDOR_ID
2700              AND      NVL(APS.FEDERAL_REPORTABLE_FLAG ,''Y'')   = ''Y''
2701 	       AND      APSS.TAX_REPORTING_SITE_FLAG   = ''Y''
2702 	       AND      APSS.ORG_ID          = $P_ORG_ID$
2703              GROUP BY NVL(JZVTD.merchant_party_taxpayer_id,
2704                          JZVTD.billing_tp_taxpayer_id)
2705                     , NVL(JZVTD.merchant_party_name,JZVTD.billing_tp_name)
2706                     , APSS.VENDOR_SITE_ID
2707                     , JZVTD.bill_from_party_id
2708                     , DECODE (INSTR(NVL(API.trx_business_category,''N''),''PR''),
2709                           0,''N'',''Y'')  --bug10282300
2710                     , DECODE( INSTR(zxdf.trx_business_category,''/'',1,3),0,''A'',
2711                           substr(zxdf.trx_business_category,length(zxdf.trx_business_category),1))
2712                     ';
2713 
2714         /**
2715         author : brathod
2716         date   : 18/5/2006
2717         Commented following code to remove date filtering.
2718         lc_jgzz_modelo_query :=
2719                REPLACE( lc_jgzz_modelo_query
2720                       , '$PERIOD_FROM_DATE$'
2721                       , 'TO_DATE('''||TO_CHAR(ld_period_start_date, 'DD/MM/YYYY')||''',''DD/MM/YYYY'')' );
2722 
2723         lc_jgzz_modelo_query :=
2724                REPLACE( lc_jgzz_modelo_query
2725                       , '$PERIOD_TO_DATE$'
2726                       , 'TO_DATE('''||TO_CHAR(ld_period_end_date, 'DD/MM/YYYY')||''',''DD/MM/YYYY'')' );
2727 
2728           :: Introduced conditional filtering ::
2729           Based on report type, FILTER_KEY and FILTER_VALUE will have following values
2730           For, Periodic Report Filter_Key = TAX_CALENDAR_PERIOD and Filter_Value = P_TAX_PERIOD
2731                Annual   Report Filter_Key = TAX_CALENDAR_YEAR   and Filter_Value = P_TAX_YEAR
2732 
2733        */
2734         declare
2735           lv_filter_key varchar2 (150) ;
2736           lv_filter_value varchar2 (2000);
2737           lv_filter_oper varchar2(2);
2738         begin
2739           if p_report_name = 'JEESPMOR' then
2740             lv_filter_key := ' JZVTD.trx_date BETWEEN JZVRS.period_start_date and JZVRS.period_end_date
2741 				AND JZVRS.TAX_CALENDAR_PERIOD'; --5444803
2742 
2743                  If P_MODELO = 347 then
2744 	                        lv_filter_key :=
2745 	                         REPLACE( lv_filter_key
2746 	                                    ,'trx_date'
2747 	                                    ,'gl_date'
2748 	                                 );
2749                  End If;
2750 
2751             lv_filter_value := l_period_string;  --p_tax_period;
2752             lv_filter_oper := 'In';
2753           elsif p_report_name = 'JEESAMOR' then
2754             lv_filter_key := 'JZVTD.trx_date BETWEEN JZVRS.period_start_date and JZVRS.period_end_date
2755 		       AND JZVRS.TAX_CALENDAR_YEAR'; --Bug 5525421
2756 
2757                  If P_MODELO = 347 then
2758 	                        lv_filter_key :=
2759 	                         REPLACE( lv_filter_key
2760 	                                    ,'trx_date'
2761 	                                    ,'gl_date'
2762 	                                 );
2763                 End If;
2764 
2765             lv_filter_value := p_tax_year;
2766             lv_filter_oper := '=';
2767           end if;
2768 		 fnd_file.put_line(FND_FILE.LOG,'lv_filter_key :='||lv_filter_key);
2769            lc_jgzz_modelo_query1 :=
2770              REPLACE( lc_jgzz_modelo_query1
2771                     , '$FILTER_KEY$'
2772                     , lv_filter_key );
2773 
2774            lc_jgzz_modelo_query1 :=
2775              REPLACE( lc_jgzz_modelo_query1
2776                     , '$FILTER_OPER$'
2777                     , lv_filter_oper );
2778 
2779            lc_jgzz_modelo_query1 :=
2780              REPLACE( lc_jgzz_modelo_query1
2781                     , '$FILTER_VALUE$'
2782                     , lv_filter_value );
2783         end;
2784 
2785             lc_jgzz_modelo_query1 :=
2786            REPLACE( lc_jgzz_modelo_query1
2787            , '$P_VAT_REP_ENTITY_ID$'
2788            , TO_CHAR(P_VAT_REP_ENTITY_ID) );
2789 
2790            lc_jgzz_modelo_query1 :=
2791            REPLACE( lc_jgzz_modelo_query1
2792            , '$P_ORG_ID$'
2793            , P_ORG_ID);
2794 
2795 
2796           lc_jgzz_modelo_query := lc_jgzz_modelo_query || lc_jgzz_modelo_query1;
2797 
2798           IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'1.4. After Merge subquery to inclue expense reports'); END IF;
2799 
2800 
2801        ELSIF p_modelo = '415' THEN
2802 
2803           --
2804           -- Replace Place Holder with Modelo 415 - AP Specific Values
2805           --
2806           -- dbms_output.put_line ('Applying palce holders for 415');
2807          /** author: brathod
2808             $MODELO_TABLE_LIST$ will have value ", AP_SUPPLIER_SITES APSS" [please note comma (,)at begining]
2809             as modelo 415 report needs to filter records based on TAX_REPORTING_SITE_FLAG for supplier sites only
2810           */
2811           -- FH Added AP_INVOICES_ALL to table list for Modelo project
2812           lc_jgzz_modelo_query :=
2813              REPLACE( lc_jgzz_modelo_query
2814                     , '$MODELO_TABLE_LIST$'
2815                     ,', AP_SUPPLIER_SITES_ALL APSS, AP_INVOICES_ALL API,  ZX_LINES_DET_FACTORS ZXDF' );
2816 
2817           -- FH: Added Transaction Code for modelo project --
2818        lc_jgzz_modelo_query :=
2819              REPLACE( lc_jgzz_modelo_query
2820                     , '$TRANS_CODE_SEL_COL$'
2821                     ,', decode(substr(zxdf.trx_business_category,length(zxdf.trx_business_category)-1,1),''/'',
2822          substr(zxdf.trx_business_category,length(zxdf.trx_business_category),1),
2823          ''A'') TRANSACTION_CODE' );
2824 
2825           lc_jgzz_modelo_query :=
2826              REPLACE( lc_jgzz_modelo_query
2827                     , '$TRANS_CODE_GRP_COL$'
2828                     , ', decode(substr(zxdf.trx_business_category,length(zxdf.trx_business_category)-1,1),''/'',
2829          substr(zxdf.trx_business_category,length(zxdf.trx_business_category),1),
2830          ''A'') ' );
2831 
2832            lc_jgzz_modelo_query :=
2833              REPLACE( lc_jgzz_modelo_query
2834                     , '$ADDRESS_ID$'
2835                     ,'APSS.VENDOR_SITE_ID' );
2836 
2837           lc_jgzz_modelo_query :=
2838              REPLACE( lc_jgzz_modelo_query
2839                     , '$TAX_REGISTRATION_NUM$'
2840             --        ,'JZVTD.billing_tp_tax_reg_num' );  /* Commented for bug 5729082 */
2841                       ,'JZVTD.billing_tp_taxpayer_id');   /* added for bug 5729082 */
2842 
2843           lc_jgzz_modelo_query :=
2844              REPLACE( lc_jgzz_modelo_query
2845                     , '$CORRECTION_TRX_SEL_COL$'
2846                     , C_CORRECTION_TRX_NULL_COLS );
2847 
2848           lc_jgzz_modelo_query :=
2849              REPLACE( lc_jgzz_modelo_query
2850                     , '$CORRECTION_TRX_GRP_COL$'
2851                     , '' );
2852 
2853           lc_jgzz_modelo_query :=
2854              REPLACE( lc_jgzz_modelo_query
2855                     , '$MODELO_SPECIFIC_FILTERS$'
2856                     , C_MOD415_AP_FILTER );
2857 
2858 	  lc_jgzz_modelo_query :=
2859              REPLACE( lc_jgzz_modelo_query
2860                     , '$P_ORG_ID$'
2861                     , P_ORG_ID );
2862 
2863           lc_jgzz_modelo_query :=
2864              REPLACE( lc_jgzz_modelo_query
2865                     , '$MODELO_SPECIFIC_GRP_FILTER$'
2866                     , C_MOD347415_GRP_FILTER );
2867 
2868           /** author:brathod
2869           Added following code to use G_MIN_VALUE parameter */
2870           lc_jgzz_modelo_query :=
2871              REPLACE( lc_jgzz_modelo_query
2872                     , '$G_MIN_VALUE$'
2873                     , 0); -- FH Moved min value processing to the xml file nvl(G_MIN_VALUE,0));
2874 
2875           lc_jgzz_modelo_query :=
2876              REPLACE( lc_jgzz_modelo_query
2877                     , '$PROPERTY_FLAG_SEL_COL$'
2878                     , C_NULL_SEL_COL );
2879 
2880           lc_jgzz_modelo_query :=
2881              REPLACE( lc_jgzz_modelo_query
2882                     , '$PROPERTY_FLAG_GRP_COL$'
2883                     , '' );
2884 
2885           lc_jgzz_modelo_query :=
2886              REPLACE( lc_jgzz_modelo_query
2887                     , '$TRANSMISSION_PROPERTY_AMT$'
2888                     , ', NULL TRANSMISSION_PROPERTY_AMT');
2889 
2890           lc_jgzz_modelo_query :=
2891              REPLACE( lc_jgzz_modelo_query
2892                     , '$YEAR_OF_AMTS_RECEIVED_CASH$'
2893                     , ', NULL YEAR_OF_AMTS_RECEIVED_CASH');
2894 
2895           lc_jgzz_modelo_query :=
2896              REPLACE( lc_jgzz_modelo_query
2897                     , '$YEAR_OF_AMTS_REC_CASH_GRP_COL$'
2898                     , '');
2899 
2900 
2901          -- dbms_output.put_line ('Completed applying place holders for 415');
2902 
2903        ELSIF p_modelo = '349' THEN
2904 
2905           --
2906           -- Replace Place Holder with Modelo 349 - AP Specific Values
2907           --
2908           /**
2909             author: brathod
2910             Relacing place holder $MODELO_TABLE_LIST$ with blank space as for 349 AP report it is not used
2911           */
2912           lc_jgzz_modelo_query :=
2913              REPLACE( lc_jgzz_modelo_query
2914                     , '$MODELO_TABLE_LIST$'
2915                     ,' ' );
2916 
2917 
2918           lc_jgzz_modelo_query :=
2919              REPLACE( lc_jgzz_modelo_query
2920                     , '$ADDRESS_ID$'
2921                     ,'NULL' );
2922 
2923           lc_jgzz_modelo_query :=
2924              REPLACE( lc_jgzz_modelo_query
2925                     , '$TAX_REGISTRATION_NUM$'
2926                     --,'JZVTD.billing_tp_taxpayer_id' ) ; Bug 13553869
2927                       ,'JZVTD.billing_tp_tax_reg_num' ) ; --Bug 13553869
2928 
2929           lc_jgzz_modelo_query :=
2930              REPLACE( lc_jgzz_modelo_query
2931                     , '$CORRECTION_TRX_SEL_COL$'
2932                     , C_CORRECTION_TRX_SEL_COLS );
2933 
2934           lc_jgzz_modelo_query :=
2935              REPLACE( lc_jgzz_modelo_query
2936                     , '$CORRECTION_TRX_GRP_COL$'
2937                     , C_CORRECTION_TRX_GRP_COLS );
2938 
2939           lc_jgzz_modelo_query :=
2940              REPLACE( lc_jgzz_modelo_query
2941                     , '$MODELO_SPECIFIC_FILTERS$'
2942                     , C_MOD349_AP_FILTER );
2943 
2944           lc_jgzz_modelo_query :=
2945              REPLACE( lc_jgzz_modelo_query
2946              , '$MODELO_SPECIFIC_GRP_FILTER$'
2947              , '' );
2948 
2949           lc_jgzz_modelo_query :=
2950              REPLACE( lc_jgzz_modelo_query
2951                     , '$PROPERTY_FLAG_SEL_COL$'
2952                     , C_NULL_SEL_COL );
2953 
2954           lc_jgzz_modelo_query :=
2955              REPLACE( lc_jgzz_modelo_query
2956                     , '$PROPERTY_FLAG_GRP_COL$'
2957                     , '' );
2958           --FH: Added TRANS_CODE_SEL_COL and TRANS_CODE_GRP_COL for modelo project
2959           lc_jgzz_modelo_query :=
2960              REPLACE( lc_jgzz_modelo_query
2961                     , '$TRANS_CODE_SEL_COL$'
2962                     ,', NULL TRANSACTION_CODE' );
2963 
2964           lc_jgzz_modelo_query :=
2965              REPLACE( lc_jgzz_modelo_query
2966                     , '$TRANS_CODE_GRP_COL$'
2967                     , '' );
2968 
2969           lc_jgzz_modelo_query :=
2970              REPLACE( lc_jgzz_modelo_query
2971                     , '$TRANSMISSION_PROPERTY_AMT$'
2972                     , ', NULL TRANSMISSION_PROPERTY_AMT');
2973 
2974           lc_jgzz_modelo_query :=
2975              REPLACE( lc_jgzz_modelo_query
2976                     , '$YEAR_OF_AMTS_RECEIVED_CASH$'
2977                     , ', NULL YEAR_OF_AMTS_RECEIVED_CASH');
2978 
2979           lc_jgzz_modelo_query :=
2980              REPLACE( lc_jgzz_modelo_query
2981                     , '$YEAR_OF_AMTS_REC_CASH_GRP_COL$'
2982                     , '');
2983 
2984        END IF;
2985 
2986        --
2987        -- Insert AP Transactions in Global Temporary Table
2988        --
2989 
2990        -- INSERT INTO JG_TEMP_TABLE values (Lc_jgzz_modelo_query); -- debug
2991 
2992        IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'1.5. Open the Dynamic Query: '||lc_jgzz_modelo_query ); END IF;
2993 
2994        OPEN c_modelo_ext FOR lc_jgzz_modelo_query ;  -- opencur
2995        LOOP
2996 
2997           IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'1.6. Fetch from the Dynamic Query'); END IF;
2998           G_LINENO := '1.6.1';
2999           FETCH  c_modelo_ext
3000           INTO   lc_tax_registration_number
3001                 ,lc_customer_name
3002                 ,lc_customer_address_id
3003                 ,lc_billing_trading_partner_id
3004                 ,ln_sum_taxable_amt
3005                 ,ln_sum_trx_line_amt
3006                 ,ln_ncorrection_amount
3007                 ,ln_correction_amount
3008                 ,lc_correction_year
3009                 ,lc_correction_period
3010                 ,lc_triangulation
3011 		    ,lc_trx_period  -- bug 5525421
3012                 ,lc_property_retail_flag
3013                 ,lc_clave_operation
3014                 ,ln_transmission_property_amt --FH: Added tipo (transaction code)
3015                 ,ln_year_of_amts_received_cash;
3016 
3017 
3018            EXIT WHEN c_modelo_ext%NOTFOUND;
3019              IF G_DEBUG THEN
3020                 fnd_file.put_line(FND_FILE.LOG,G_LINENO);
3021                 fnd_file.put_line(FND_FILE.LOG,'lc_tax_registration_number='||lc_tax_registration_number);
3022                 fnd_file.put_line(FND_FILE.LOG,'lc_clave_operation='||lc_clave_operation);
3023 				fnd_file.put_line(FND_FILE.LOG,'lc_triangulation'||lc_triangulation);
3024              END IF;
3025 
3026              G_LINENO := '1.6.2';
3027              IF    P_MODELO = '415' THEN
3028 
3029                -- ln_sum_taxable_amt  will have the gross amt for AP
3030                -- Assign NULL to ln_sum_trx_line_amt
3031                ln_sum_trx_line_amt := NULL;
3032 
3033                get_vendor_address ( p_party_site_id  => lc_customer_address_id
3034                                   , x_postal_code    => lc_postal_code
3035                                   , x_city           => lc_city
3036                                   , x_address_detail => lc_address_detail
3037                                   , x_country        => lc_country
3038                                   );
3039 
3040 
3041 
3042                lc_clave_operation         := lc_clave_operation;
3043                lc_tax_registration_number := SUBSTR( lc_tax_registration_number,1,9);
3044                lc_customer_name           := SUBSTR(lc_customer_name,1,40);
3045                lc_address_detail          := SUBSTR(lc_address_detail,1,32);
3046 
3047                 G_LINENO := '1.6.2.1';
3048 
3049                IF G_DEBUG THEN
3050                  fnd_file.put_line(FND_FILE.LOG,'lc_address_detail='||lc_address_detail);
3051                  fnd_file.put_line(FND_FILE.LOG,'lc_postal_code='||lc_postal_code);
3052                  fnd_file.put_line(FND_FILE.LOG,'lc_city='||lc_city);
3053                END IF;
3054 
3055                 G_LINENO := '1.6.2.2';
3056 
3057                ln_sum_taxable_amt         :=
3058                AP415_GROSS_AMOUNTFormula ( p_vendor_id    => lc_billing_trading_partner_id
3059                                          , p_gross_amount => ln_sum_taxable_amt );
3060 
3061 
3062              ELSIF    P_MODELO = '347' THEN
3063 
3064                  G_LINENO := '1.6.2.3';
3065                  -- ln_sum_taxable_amt  will have the gross amt for AP
3066                  -- Assign NULL to ln_sum_trx_line_amt
3067                  ln_sum_trx_line_amt := NULL;
3068 
3069                  -- Bug 8485057 this value is now selected from the record
3070                  -- lc_clave_operation := 'A' ;
3071                  G_LINENO := '1.6.2.4';
3072                  get_vendor_address ( p_party_site_id  => lc_customer_address_id
3073                                     , x_postal_code    => lc_postal_code
3074                                     , x_city           => lc_city
3075                                     , x_address_detail => lc_address_detail
3076                                     , x_country        => lc_country
3077                                     );
3078                   IF G_DEBUG THEN
3079                     fnd_file.put_line(FND_FILE.LOG,G_LINENO);
3080                     fnd_file.put_line(FND_FILE.LOG,'lc_customer_address_id='||lc_customer_address_id);
3081                     fnd_file.put_line(FND_FILE.LOG,'lc_address_detail='||lc_address_detail);
3082                     fnd_file.put_line(FND_FILE.LOG,'lc_country='||lc_country);
3083                   END IF;
3084 
3085                 G_LINENO := '1.6.2.5';
3086 
3087                 /* Bug 8485057: added p_tipo as a parameter */
3088                 ln_sum_taxable_amt :=
3089                 AP347_GROSS_AMOUNTFormula ( p_vendor_id      => lc_billing_trading_partner_id
3090                                           , p_property_flag  => lc_property_retail_flag
3091                                           , p_gross_amt      => ln_sum_taxable_amt
3092                                           , p_tipo           => lc_clave_operation);
3093 
3094 
3095                     -- initilise amts to zero
3096                     ln_sum_taxable_amt_qtr1  := 0;
3097                     ln_sum_taxable_amt_qtr2  := 0;
3098                     ln_sum_taxable_amt_qtr3  := 0;
3099                     ln_sum_taxable_amt_qtr4  := 0;
3100 
3101 
3102                	     IF P_REPORT_NAME = 'JEESAMOR' THEN
3103 
3104 	                      AP347_GROSS_AMT_QTR(
3105                                  lc_tax_registration_number
3106                                 , lc_customer_name
3107                                 , lc_customer_address_id
3108                                 , lc_billing_trading_partner_id
3109                                 , lc_property_retail_flag
3110 	                        , lc_clave_operation
3111 	                        , ln_sum_taxable_amt_qtr1
3112 	                        , ln_sum_taxable_amt_qtr2
3113 	                        , ln_sum_taxable_amt_qtr3
3114 	                        , ln_sum_taxable_amt_qtr4
3115 	                       );
3116 
3117                      END IF;
3118 
3119 
3120                IF G_DEBUG THEN
3121                     fnd_file.put_line(FND_FILE.LOG,G_LINENO);
3122                     fnd_file.put_line(FND_FILE.LOG,'ln_sum_taxable_amt='||ln_sum_taxable_amt);
3123                     fnd_file.put_line(FND_FILE.LOG,'ln_sum_taxable_amt_qtr1='||ln_sum_taxable_amt_qtr1);
3124                     fnd_file.put_line(FND_FILE.LOG,'ln_sum_taxable_amt_qtr2='||ln_sum_taxable_amt_qtr2);
3125                     fnd_file.put_line(FND_FILE.LOG,'ln_sum_taxable_amt_qtr3='||ln_sum_taxable_amt_qtr3);
3126                     fnd_file.put_line(FND_FILE.LOG,'ln_sum_taxable_amt_qtr4='||ln_sum_taxable_amt_qtr4);
3127 
3128                   END IF;
3129 
3130                 G_LINENO := '1.6.2.6';
3131 
3132              ELSIF P_MODELO = '349' THEN
3133 
3134                 G_LINENO := '1.6.2.7';
3135                 IF P_REPORT_NAME = 'JEESAMOR' THEN
3136                   G_LINENO := '1.6.2.7.1';
3137                   ln_running_total           := 0;
3138                   ln_formerly_decl_amt       := 0;
3139                   lc_tax_registration_number := SUBSTR( lc_tax_registration_number,1,14);
3140                   lc_customer_name           := SUBSTR(lc_customer_name,1,40);
3141 		      lc_print_year := P_TAX_YEAR;  -- Bug 5525421
3142 		      lc_print_period := lc_trx_period; -- Bug 5525421
3143                   G_LINENO := '1.6.2.7.2';
3144 
3145 		      -- Bug 5525421
3146 
3147                   IF lc_correction_year IS NULL AND lc_correction_period IS NULL THEN --Not a Correction
3148 
3149                      G_LINENO := '1.6.2.7.2.1';
3150 
3151                      lc_clave_operation         := lc_triangulation;  -- BUG 9765740
3152                      ln_base_imponiable         := ln_ncorrection_amount;
3153                      ln_running_total           := ln_ncorrection_amount;
3154 			  IF G_DEBUG THEN
3155 			    fnd_file.put_line(FND_FILE.LOG,'IF :
3156 							lc_clave_operation='||lc_clave_operation||',
3157 							lc_tax_registration_number='||lc_tax_registration_number||',
3158 							lc_customer_name='||lc_customer_name||',
3159 							lc_correction_year='||lc_correction_year||',
3160 							lc_correction_period='||lc_correction_period||',
3161 							lc_triangulation='||lc_triangulation||'
3162 							lc_print_year='||lc_print_year||',
3163 							lc_print_period='||lc_print_period
3164 							);
3165 			  END IF;
3166 
3167                   ELSIF lc_correction_year IS NOT NULL AND lc_correction_period IS NOT NULL THEN --IS a Correction
3168                      G_LINENO := '1.6.2.7.2.2';
3169                      lc_clave_operation	   := lower(lc_triangulation);	-- BUG 9765740
3170                      lc_correction_year    := lc_correction_year; --SUBSTR(lc_correction_year,3,2);
3171                      ln_base_imponiable    := ln_correction_amount;
3172                      ln_formerly_decl_amt  := 0;
3173                      G_LINENO := '1.6.2.7.2.3';
3174 
3175 			  IF G_DEBUG THEN
3176 			    fnd_file.put_line(FND_FILE.LOG,'ELSE :
3177 							lc_clave_operation='||lc_clave_operation||',
3178 							lc_tax_registration_number='||lc_tax_registration_number||',
3179 							lc_customer_name='||lc_customer_name||',
3180 							lc_correction_year='||lc_correction_year||',
3181 							lc_correction_period='||lc_correction_period||',
3182 							lc_triangulation='||lc_triangulation||'
3183 							lc_print_year='||lc_print_year||',
3184 							lc_print_period='||lc_print_period
3185 							);
3186 			  END IF;
3187 
3188 
3189                     -- SELECT DECODE(lc_triangulation,'X','T','E')  --Bug 5525421: No traingulation for AP.
3190                     -- INTO   lc_clave_operation
3191                     -- FROM   DUAL ;
3192                      G_LINENO := '1.6.2.7.2.4';
3193                      IF lc_clave_operation IN ('1','2') THEN
3194                           lc_sign := ' ';
3195                      ELSE
3196                           IF ln_base_imponiable < 0 THEN
3197                              lc_sign := '-' ;
3198                           ELSE
3199                              lc_sign := '+' ;
3200                           END IF;
3201                      END IF;
3202                      G_LINENO := '1.6.2.7.2.5';
3203 
3204 		         begin
3205                        SELECT NVL(jg_info_n10,0) -- running total
3206                        INTO   ln_formerly_decl_amt
3207                        FROM   JG_ZZ_VAT_TRX_GT  M349
3208                        WHERE  RTRIM(jg_info_v11)      = RTRIM(lc_tax_registration_number)
3209                        AND    RTRIM(jg_info_v12)      = RTRIM(lc_customer_name)
3210                        AND    jg_info_n13             = lc_correction_year -- correction year
3211                        AND    jg_info_v27             = lc_correction_period -- correction period
3212                   --   AND    RTRIM(jg_info_v21)      = RTRIM(lc_correction_year) --Bug 5525421
3213                   --   AND    UPPER(jg_info_v1)       ='A';						  -- BUG 9765740
3214                        AND    UPPER(jg_info_v1)       = upper(lc_triangulation)   -- BUG 9765740
3215                        AND  jg_info_v15 is NULL		-- Added as part of Bug fix for ER 9765740
3216 		       		   AND  jg_info_v16 is NULL;			-- Added as part of Bug fix for ER 9765740
3217 
3218 
3219 		         exception
3220                        when no_data_found then
3221                        ln_formerly_decl_amt :=0;
3222 		         end;
3223 
3224                     G_LINENO := '1.6.2.7.2.6';
3225                      --
3226                      -- update running total
3227                      --
3228 		     -- As per R11i logic this update should happend after the insert. Hence moving this to after insert.
3229 
3230 		    /* UPDATE JG_ZZ_VAT_TRX_GT
3231                      SET    jg_info_n10  = ln_formerly_decl_amt + ln_ncorrection_amount
3232                      WHERE  RTRIM(jg_info_v15)    = RTRIM(lc_correction_year) --RTRIM(SUBSTR(lc_correction_year,3,2))
3233                      AND    RTRIM(jg_info_v16)    = RTRIM(lc_correction_period)
3234                      AND    jg_info_v14           IS NOT NULL
3235                      AND    jg_info_v15           IS NOT NULL
3236                      AND    RTRIM(jg_info_v12)    = RTRIM(lc_tax_registration_number)
3237                      AND    RTRIM(jg_info_v13)    = RTRIM(lc_customer_name)
3238                      AND    UPPER(jg_info_v11)    ='A'; */ --Bug 5525421
3239 
3240                       G_LINENO := '1.6.2.7.2.7';
3241                   END IF;
3242 
3243                   G_LINENO := '1.6.2.7.3';
3244                  -- JGZZVEFT();  --Bug 5525421 This procedure should call only once i.e. after inserting AP and AR data in to temp table.
3245                   G_LINENO := '1.6.2.7.4';
3246 
3247                  END IF;
3248                 G_LINENO := '1.6.2.8';
3249              END IF;
3250 
3251              G_LINENO := '1.6.3';
3252 
3253           INSERT INTO JG_ZZ_VAT_TRX_GT
3254             ( jg_info_v1    -- lc_clave_operation                                   lc_clave_operation
3255             , jg_info_v6    -- lc_country Bug 8485057
3256             , jg_info_v11   -- c_modelo_rec.tax_registration_number                 lc_tax_registration_number
3257             , jg_info_v12   -- c_modelo_rec.customer_name                           lc_customer_name
3258             , jg_info_v13   -- c_modelo_rec.customer_address_id                     lc_customer_address_id
3259             , jg_info_v14   -- c_modelo_rec.billing_trading_partner_id              lc_billing_trading_partner
3260             , jg_info_n1    -- c_modelo_rec.sum_taxable_amt    -- 347-AP, 415-AP    ln_sum_taxable_amt
3261             , jg_info_n16   -- ln_sum_taxable_amt_q1 -347-AP
3262             , jg_info_n18   -- ln_sum_taxable_amt_q2 -347-AP
3263             , jg_info_n20   -- ln_sum_taxable_amt_q3 -347-AP
3264             , jg_info_n22    -- ln_sum_taxable_amt_q4 -347-AP
3265             , jg_info_n2    -- c_modelo_rec.sum_trx_line_amt    -- 347-AR,415-AR    ln_sum_trx_line_amt
3266             , jg_info_n3    -- c_modelo_rec.ncorrection_amount  -- 349              ln_ncorrection_amount
3267             , jg_info_n4    -- c_modelo_rec.correction_amount   -- 349              ln_correction_amount
3268             , jg_info_v15   -- c_modelo_rec.correction_year                         lc_correction_year
3269             , jg_info_v16   -- c_modelo_rec.correction_period                       lc_correction_period
3270             , jg_info_v17   -- c_modelo_rec.triangulation                           lc_triangulation
3271             , jg_info_v21   -- c_modelo_rec.property_retail_flag                    lc_property_retail_flag
3272             , jg_info_v18   -- lc_postal_code                                       lc_postal_code
3273             , jg_info_v19   -- lc_city                                              lc_city
3274             , jg_info_v20   -- lc_address_detail                                    lc_address_detail
3275             , jg_info_n10   -- ln_running_total                                     ln_running_total
3276             , jg_info_n11   -- ln_formerly_decl_amt                                 ln_formerly_decl_amt
3277             , jg_info_n12   -- ln_base_imponiable                                   ln_base_imponiable
3278             , jg_info_v22   -- p_print_year --p_tax_calender_year                   P_TAX_YEAR
3279             , jg_info_v23   -- legal entity name                                    G_LE_NAME
3280             , jg_info_v26   -- G_LE_TRN -- 347                                      G_LE_TRN
3281             , jg_info_v24   -- lc_sign                                              lc_sign
3282             , jg_info_v25   -- p_tax_office -- 347                                  p_tax_office
3283             , jg_info_n13   -- lc_print_year   --Bug 5525421
3284 	      , jg_info_v27   -- lc_print_period  --Bug 5525421
3285             )
3286           VALUES
3287             ( lc_clave_operation
3288             , lc_country
3289             , lc_tax_registration_number
3290             , lc_customer_name
3291             , lc_customer_address_id
3292             , lc_billing_trading_partner_id
3293             , ln_sum_taxable_amt
3294             , ln_sum_taxable_amt_qtr1
3295             , ln_sum_taxable_amt_qtr2
3296             , ln_sum_taxable_amt_qtr3
3297             , ln_sum_taxable_amt_qtr4
3298             , ln_sum_trx_line_amt
3299             , ln_ncorrection_amount
3300             , ln_correction_amount
3301             , lc_correction_year
3302             , lc_correction_period
3303             , lc_triangulation
3304             , lc_property_retail_flag
3305             , lc_postal_code
3306             , lc_city
3307             , lc_address_detail
3308             , ln_running_total
3309             , ln_formerly_decl_amt
3310             , ln_base_imponiable
3311             , P_TAX_YEAR
3312             , G_LE_NAME
3313             , G_LE_TRN
3314             , lc_sign
3315             , p_tax_office
3316             , lc_print_year    --Bug 5525421
3317 	    , lc_print_period  --Bug 5525421
3318             );
3319 
3320       IF G_DEBUG THEN
3321          fnd_file.put_line(FND_FILE.LOG,G_LINENO);
3322       END IF;
3323 
3324 		----Bug 5525421 Updation should haapend only after insertintg the correction reocrd.
3325 
3326 		IF P_MODELO = '349' THEN
3327 		  IF P_REPORT_NAME = 'JEESAMOR' THEN
3328 			IF lc_correction_year IS NOT NULL AND lc_correction_period IS NOT NULL THEN --IS a Correction
3329 
3330 		UPDATE JG_ZZ_VAT_TRX_GT
3331                      SET    jg_info_n10  = ln_formerly_decl_amt + ln_ncorrection_amount
3332                      WHERE  RTRIM(jg_info_v15)    = RTRIM(lc_correction_year) --RTRIM(SUBSTR(lc_correction_year,3,2))
3333                      AND    RTRIM(jg_info_v16)    = RTRIM(lc_correction_period)
3334                      AND    jg_info_v14           IS NOT NULL
3335                      AND    jg_info_v15           IS NOT NULL
3336                      AND    RTRIM(jg_info_v12)    = RTRIM(lc_tax_registration_number)
3337                      AND    RTRIM(jg_info_v13)    = RTRIM(lc_customer_name)
3338 		     		 --  AND    UPPER(jg_info_v11)    ='A';  Modified from V11 to V1 BUG 9765740
3339                      AND    UPPER(jg_info_v1)    =upper(lc_triangulation);  -- BUG 9765740
3340 
3341 			END IF;
3342 	            END IF;
3343 	        END IF;
3344 
3345             G_LINENO := '1.6.4';
3346 
3347        END LOOP;
3348 
3349        G_LINENO := '1.7';
3350        CLOSE c_modelo_ext; -- closecur
3351        G_LINENO := '1.8';
3352 
3353     END IF;
3354 
3355 
3356     G_LINENO := '2';
3357 
3358     IF ( P_REPORT_NAME = 'JEESPMOR' AND P_SOURCE = 'AR')
3359        OR P_REPORT_NAME = 'JEESAMOR' THEN
3360 
3361        G_LINENO := '2.1';
3362 
3363        --
3364        -- Build query to fetch ap extract for modelo from JG
3365        --
3366        lc_jgzz_modelo_query  := C_JGZZ_MODELO_GENRIC_QUERY;
3367        --
3368        /**
3369         author : brathod
3370         date   : 18/5/2006
3371         Commented following code to remove date filtering.
3372         REPLACE( lc_jgzz_modelo_query
3373                     , '$PERIOD_FROM_DATE$'
3374                     , 'TO_DATE('''||TO_CHAR(ld_period_start_date, 'DD/MM/YYYY')||''',''DD/MM/YYYY'')' );
3375 
3376        lc_jgzz_modelo_query :=
3377              REPLACE( lc_jgzz_modelo_query
3378                     , '$PERIOD_TO_DATE$'
3379                     , 'TO_DATE('''||TO_CHAR(ld_period_end_date, 'DD/MM/YYYY')||''',''DD/MM/YYYY'')' );
3380 
3381         :: Introduced conditional filtering ::
3382         Based on report type, FILTER_KEY and FILTER_VALUE will have following values
3383         For, Periodic Report Filter_Key = TAX_CALENDAR_PERIOD and Filter_Value = P_TAX_PERIOD
3384              Annual   Report Filter_Key = TAX_CALENDAR_YEAR   and Filter_Value = P_TAX_YEAR
3385 
3386        */
3387       G_LINENO := '2.2';
3388 
3389         declare
3390           lv_filter_key   varchar2 (150) ;
3391           lv_filter_value varchar2 (2000);
3392           lv_filter_oper varchar2(2);
3393         begin
3394           if p_report_name = 'JEESPMOR' then
3395             lv_filter_key := ' JZVTD.trx_date BETWEEN JZVRS.period_start_date and JZVRS.period_end_date
3396 			       AND JZVRS.TAX_CALENDAR_PERIOD'; --5444803
3397 --            lv_filter_key := 'JZVRS.TAX_CALENDAR_PERIOD'; --5444803
3398 			--- Bug 11923711
3399 			if (p_driving_date = 'GL'  AND P_MODELO = 349) or (P_MODELO = 347)  then
3400 
3401             lv_filter_key :=
3402             REPLACE( lv_filter_key
3403                     ,'trx_date'
3404                     ,'gl_date'
3405                     );
3406 			fnd_file.put_line(FND_FILE.LOG,'***Updating the C_CORRECTION_TRX_SEL_COLS***:1 ');
3407 			C_CORRECTION_TRX_SEL_COLS := REPLACE( C_CORRECTION_TRX_SEL_COLS
3408 										,'TRX_DATE'
3409 										,'GL_DATE'
3410 										);
3411 
3412 			C_CORRECTION_TRX_GRP_COLS := REPLACE( C_CORRECTION_TRX_GRP_COLS
3413 										,'TRX_DATE'
3414 										,'GL_DATE'
3415 										);
3416             end if;
3417 
3418             lv_filter_value := l_period_string; --p_tax_period;
3419             lv_filter_oper := 'In';
3420           elsif p_report_name = 'JEESAMOR' then
3421             lv_filter_key := 'JZVTD.trx_date BETWEEN JZVRS.period_start_date and JZVRS.period_end_date
3422 		       AND JZVRS.TAX_CALENDAR_YEAR';  -- Bug 5525421
3423 
3424 			--- Bug 11923711
3425 			if (p_driving_date = 'GL'  AND P_MODELO = 349) or (P_MODELO = 347) then
3426 
3427             lv_filter_key :=
3428             REPLACE( lv_filter_key
3429                     ,'trx_date'
3430                     ,'gl_date'
3431                     );
3432 		     fnd_file.put_line(FND_FILE.LOG,'***Updating the C_CORRECTION_TRX_SEL_COLS***: ');
3433 			 C_CORRECTION_TRX_SEL_COLS := REPLACE( C_CORRECTION_TRX_SEL_COLS
3434 										,'TRX_DATE'
3435 										,'GL_DATE'
3436 										);
3437 
3438 			C_CORRECTION_TRX_GRP_COLS := REPLACE( C_CORRECTION_TRX_GRP_COLS
3439 										,'TRX_DATE'
3440 										,'GL_DATE'
3441 										);
3442             end if;
3443 
3444             lv_filter_value := p_tax_year;
3445             lv_filter_oper := '=';
3446           end if;
3447 
3448           lc_jgzz_modelo_query :=
3449                  REPLACE( lc_jgzz_modelo_query
3450                         , '$FILTER_KEY$'
3451                         , lv_filter_key
3452                         );
3453 
3454           lc_jgzz_modelo_query :=
3455                  REPLACE( lc_jgzz_modelo_query
3456                         , '$FILTER_OPER$'
3457                         , lv_filter_oper
3458                         );
3459 
3460            lc_jgzz_modelo_query :=
3461                  REPLACE( lc_jgzz_modelo_query
3462                         , '$FILTER_VALUE$'
3463                         , lv_filter_value
3464                         );
3465         end;
3466 
3467        lc_jgzz_modelo_query :=
3468              REPLACE( lc_jgzz_modelo_query
3469                     , '$P_VAT_REP_ENTITY_ID$'
3470                     , TO_CHAR(P_VAT_REP_ENTITY_ID) );
3471 
3472       G_LINENO := '2.3';
3473 
3474       /**
3475       author: brathod
3476       Relacing place holder $MODELO_TABLE_LIST$ with blank space as for AR report its not in use
3477 
3478       lc_jgzz_modelo_query :=
3479              REPLACE( lc_jgzz_modelo_query
3480                     , '$MODELO_TABLE_LIST$'
3481                     ,' ' );
3482        */
3483 
3484        IF P_MODELO = '347' THEN  -- Modelo 347
3485 
3486           lc_jgzz_modelo_query :=
3487              REPLACE( lc_jgzz_modelo_query
3488               , '$MODELO_TABLE_LIST$'
3489               ,' , hz_cust_site_uses_all  hzcsu ,hz_cust_acct_sites_all hzcas ,hz_cust_accounts hzca, ra_customer_trx_all trx, zx_lines_det_factors zxdf ');
3490 
3491 	  lc_jgzz_modelo_query :=
3492              REPLACE( lc_jgzz_modelo_query
3493                     ,'$ADDRESS_ID$'
3494                     ,'hzcsu.cust_acct_site_id' );
3495           --
3496           -- Replace Place Holder with Modelo 347 - AR Specific Values
3497           --
3498           G_LINENO := '2.3.1';
3499           lc_jgzz_modelo_query :=
3500              REPLACE( lc_jgzz_modelo_query
3501                         , '$TAX_REGISTRATION_NUM$'
3502           --              ,'JZVTD.billing_tp_tax_reg_num' );      /* Commented for bug 5729082 */
3503                           , 'JZVTD.billing_tp_taxpayer_id');    /* Added for bug 5729082 */
3504 
3505           lc_jgzz_modelo_query :=
3506               REPLACE( lc_jgzz_modelo_query
3507                         , '$CORRECTION_TRX_SEL_COL$'
3508                         , C_CORRECTION_TRX_NULL_COLS );
3509 
3510           lc_jgzz_modelo_query :=
3511              REPLACE( lc_jgzz_modelo_query
3512                         , '$CORRECTION_TRX_GRP_COL$'
3513                         , '' );
3514 
3515           lc_jgzz_modelo_query :=
3516              REPLACE( lc_jgzz_modelo_query
3517                         , '$MODELO_SPECIFIC_FILTERS$'
3518                         , C_MOD347_AR_FILTER );
3519 
3520           lc_jgzz_modelo_query :=
3521              REPLACE( lc_jgzz_modelo_query
3522                     , '$P_ORG_ID$'
3523                     , P_ORG_ID );
3524 
3525           lc_jgzz_modelo_query :=
3526              REPLACE( lc_jgzz_modelo_query
3527                         , '$MODELO_SPECIFIC_GRP_FILTER$'
3528                         , '');
3529                         -- Bug 8485057 min amount is now checked in the xml
3530                         --, C_MOD347415_GRP_FILTER );
3531 
3532           -- Bug 8485057, select Transaction Code, default B for AR transactions
3533           lc_jgzz_modelo_query :=
3534              REPLACE( lc_jgzz_modelo_query
3535                     , '$TRANS_CODE_SEL_COL$'
3536                     , ', DECODE( INSTR(zxdf.trx_business_category,''/'',1,3),0,''B'',
3537          substr(zxdf.trx_business_category,length(trx_business_category),1)) TIPO');
3538 
3539           lc_jgzz_modelo_query :=
3540              REPLACE( lc_jgzz_modelo_query
3541                     , '$TRANS_CODE_GRP_COL$'
3542                     , ', DECODE( INSTR(zxdf.trx_business_category,''/'',1,3),0,''B'',
3543          substr(zxdf.trx_business_category,length(zxdf.trx_business_category),1))');
3544 
3545                    lc_jgzz_modelo_query :=
3546              REPLACE( lc_jgzz_modelo_query
3547                     , '$TRANSMISSION_PROPERTY_AMT$'
3548                     , ', SUM(DECODE(JZVTD.extract_source_ledger
3549                       ,''AR'',DECODE(NVL(trx.global_attribute12, ''N''),
3550                                   ''Y'', DECODE (JZVTD.TAX_LINE_NUMBER, ''1'', NVL(JZVTD.taxable_amt_funcl_curr, 0),0),0)
3551                       ,0 )) +
3552 		             SUM(DECODE(JZVTD.extract_source_ledger
3553 		                  ,''AR'',DECODE(NVL(trx.global_attribute12, ''N''),''Y'', NVL(JZVTD.tax_amt_funcl_curr, 0),0)
3554                       ,0 ))  TRANSMISSION_PROPERTY_AMT');
3555 
3556                   lc_jgzz_modelo_query :=
3557              REPLACE( lc_jgzz_modelo_query
3558                     , '$YEAR_OF_AMTS_RECEIVED_CASH$'
3559                     , ', trx.global_attribute15 YEAR_OF_AMTS_RECEIVED_CASH');
3560 
3561                lc_jgzz_modelo_query :=
3562              REPLACE( lc_jgzz_modelo_query
3563                     , '$YEAR_OF_AMTS_REC_CASH_GRP_COL$'
3564                     , ',trx.global_attribute15' );
3565 
3566 
3567           G_LINENO := '2.3.2';
3568           /**
3569           author:brathod
3570           Added following code to use G_MIN_VALUE parameter
3571           */
3572           lc_jgzz_modelo_query :=
3573              REPLACE( lc_jgzz_modelo_query
3574                     , '$G_MIN_VALUE$'
3575                     , nvl(G_MIN_VALUE,0));
3576 
3577           lc_jgzz_modelo_query :=
3578              REPLACE( lc_jgzz_modelo_query
3579                     , '$PROPERTY_FLAG_SEL_COL$'
3580                   --  , C_347_SEL_COL );
3581                     , C_NULL_SEL_COL );
3582 
3583          /* lc_misc := C_347_SEL_COL ;
3584 
3585           lc_misc := REPLACE( lc_misc, 'PROPERTY_RETAIL_FLAG','');
3586 
3587           lc_jgzz_modelo_query :=
3588              REPLACE( lc_jgzz_modelo_query
3589                     , '$PROPERTY_FLAG_GRP_COL$'
3590                     , lc_misc ); */
3591 
3592           lc_jgzz_modelo_query :=
3593              REPLACE( lc_jgzz_modelo_query
3594                     , '$PROPERTY_FLAG_GRP_COL$'
3595                     , '' );
3596 
3597        ELSIF P_MODELO = '415' THEN  -- Modelo 415
3598 
3599           --
3600           -- Replace Place Holder with Modelo 415 - AR Specific Values
3601           --
3602 -- FH: Added ZX table for modelo project
3603           lc_jgzz_modelo_query :=
3604             REPLACE( lc_jgzz_modelo_query
3605                     , '$MODELO_TABLE_LIST$'
3606                     ,' , hz_cust_site_uses_all  hzcsu ,hz_cust_acct_sites_all hzcas ,hz_cust_accounts hzca, ZX_LINES_DET_FACTORS zxdf ' );
3607 
3608           G_LINENO := '2.3.3';
3609           lc_jgzz_modelo_query :=
3610              REPLACE( lc_jgzz_modelo_query
3611                         , '$TAX_REGISTRATION_NUM$'
3612             --        ,'JZVTD.billing_tp_tax_reg_num' );  /* Commented for bug 5729082 */
3613                       ,'JZVTD.billing_tp_taxpayer_id');   /* added for bug 5729082 */
3614 
3615 -- FH: Added Transaction Code for modelo project --
3616        lc_jgzz_modelo_query :=
3617              REPLACE( lc_jgzz_modelo_query
3618                     , '$TRANS_CODE_SEL_COL$'
3619                     ,', decode(substr(zxdf.trx_business_category,length(zxdf.trx_business_category)-1,1),''/'',
3620          substr(zxdf.trx_business_category,length(zxdf.trx_business_category),1),
3621          ''B'') TRANSACTION_CODE' );
3622 
3623           lc_jgzz_modelo_query :=
3624              REPLACE( lc_jgzz_modelo_query
3625                     , '$TRANS_CODE_GRP_COL$'
3626                     , ', decode(substr(zxdf.trx_business_category,length(zxdf.trx_business_category)-1,1),''/'',
3627          substr(zxdf.trx_business_category,length(zxdf.trx_business_category),1),
3628          ''B'') ');
3629 
3630           lc_jgzz_modelo_query :=
3631              REPLACE( lc_jgzz_modelo_query
3632                     , '$ADDRESS_ID$'
3633                     ,'hzcsu.cust_acct_site_id' );
3634 
3635           lc_jgzz_modelo_query :=
3636              REPLACE( lc_jgzz_modelo_query
3637                         , '$CORRECTION_TRX_SEL_COL$'
3638                         , C_CORRECTION_TRX_NULL_COLS );
3639 
3640           lc_jgzz_modelo_query :=
3641              REPLACE( lc_jgzz_modelo_query
3642                         , '$CORRECTION_TRX_GRP_COL$'
3643                         , '' );
3644 
3645           lc_jgzz_modelo_query :=
3646              REPLACE( lc_jgzz_modelo_query
3647                         , '$MODELO_SPECIFIC_FILTERS$'
3648                         , C_MOD415_AR_FILTER );
3649 
3650          lc_jgzz_modelo_query :=
3651              REPLACE( lc_jgzz_modelo_query
3652                     , '$P_ORG_ID$'
3653                     , P_ORG_ID );
3654 
3655           lc_jgzz_modelo_query :=
3656              REPLACE( lc_jgzz_modelo_query
3657                         , '$MODELO_SPECIFIC_GRP_FILTER$'
3658                         , C_MOD347415_GRP_FILTER);
3659 
3660             /**
3661           author:brathod
3662           Added following code to use G_MIN_VALUE parameter
3663           */
3664           lc_jgzz_modelo_query :=
3665              REPLACE( lc_jgzz_modelo_query
3666                     , '$G_MIN_VALUE$'
3667                     , 0); --FH: Moved Min value processing to the xml file nvl(G_MIN_VALUE,0));
3668 
3669           lc_jgzz_modelo_query :=
3670              REPLACE( lc_jgzz_modelo_query
3671                     , '$PROPERTY_FLAG_SEL_COL$'
3672                     , C_NULL_SEL_COL );
3673 
3674           lc_jgzz_modelo_query :=
3675              REPLACE( lc_jgzz_modelo_query
3676                     , '$PROPERTY_FLAG_GRP_COL$'
3677                     , '' );
3678 
3679           lc_jgzz_modelo_query :=
3680              REPLACE( lc_jgzz_modelo_query
3681                     , '$TRANSMISSION_PROPERTY_AMT$'
3682                     , ', NULL TRANSMISSION_PROPERTY_AMT');
3683 
3684           lc_jgzz_modelo_query :=
3685              REPLACE( lc_jgzz_modelo_query
3686                     , '$YEAR_OF_AMTS_RECEIVED_CASH$'
3687                     , ', NULL YEAR_OF_AMTS_RECEIVED_CASH');
3688 
3689           lc_jgzz_modelo_query :=
3690              REPLACE( lc_jgzz_modelo_query
3691                     , '$YEAR_OF_AMTS_REC_CASH_GRP_COL$'
3692                     , '');
3693 
3694           G_LINENO := '2.3.4';
3695 
3696        ELSIF P_MODELO = '349' THEN  -- Modelo 349
3697           --
3698           -- Replace Place Holder with Modelo 349 - AR Specific Values
3699           --
3700 
3701           lc_jgzz_modelo_query :=
3702              REPLACE( lc_jgzz_modelo_query
3703                     , '$MODELO_TABLE_LIST$'
3704                     ,' ' );
3705 
3706           G_LINENO := '2.3.5';
3707           lc_jgzz_modelo_query :=
3708              REPLACE( lc_jgzz_modelo_query
3709                         , '$TAX_REGISTRATION_NUM$'
3710                     --,'JZVTD.billing_tp_taxpayer_id' ) ; Bug 13553869
3711                       ,'JZVTD.billing_tp_tax_reg_num' ) ; --Bug 13553869
3712 
3713           lc_jgzz_modelo_query :=
3714              REPLACE( lc_jgzz_modelo_query
3715                     , '$ADDRESS_ID$'
3716                     ,'NULL' );
3717 
3718           lc_jgzz_modelo_query :=
3719              REPLACE( lc_jgzz_modelo_query
3720                         , '$CORRECTION_TRX_SEL_COL$'
3721                         , C_CORRECTION_TRX_SEL_COLS );
3722 
3723           lc_jgzz_modelo_query :=
3724              REPLACE( lc_jgzz_modelo_query
3725                         , '$CORRECTION_TRX_GRP_COL$'
3726                         , C_CORRECTION_TRX_GRP_COLS );
3727 
3728 --FH: Added TRANS_CODE_SEL_COL and TRANS_CODE_GRP_COL for modelo project
3729            lc_jgzz_modelo_query :=
3730              REPLACE( lc_jgzz_modelo_query
3731                     , '$TRANS_CODE_SEL_COL$'
3732                     ,', NULL TRANSACTION_CODE' );
3733 
3734          lc_jgzz_modelo_query :=
3735              REPLACE( lc_jgzz_modelo_query
3736                     , '$TRANS_CODE_GRP_COL$'
3737                     , '' );
3738 
3739          lc_jgzz_modelo_query :=
3740              REPLACE( lc_jgzz_modelo_query
3741                     , '$TRANSMISSION_PROPERTY_AMT$'
3742                     , ', NULL TRANSMISSION_PROPERTY_AMT');
3743 
3744          lc_jgzz_modelo_query :=
3745              REPLACE( lc_jgzz_modelo_query
3746                     , '$YEAR_OF_AMTS_RECEIVED_CASH$'
3747                     , ', NULL YEAR_OF_AMTS_RECEIVED_CASH');
3748 
3749           lc_jgzz_modelo_query :=
3750              REPLACE( lc_jgzz_modelo_query
3751                     , '$YEAR_OF_AMTS_REC_CASH_GRP_COL$'
3752                     , '');
3753 
3754 
3755           /**
3756             author: brathod
3757             If the report is Annual Modelo AR (349) - JEESVEFT, then adding extract filtering
3758             conditions to support multi period reporting.
3759           */
3760           if p_report_name = 'JEESAMOR' then -- Annual Report
3761 
3762             declare
3763 
3764               ld_start_date     gl_periods.start_date%type;
3765               ld_end_date       gl_periods.end_date%type;
3766               lv_tax_calendar   jg_zz_vat_rep_entities.tax_calendar_name%type;
3767 
3768               /** Fetch the tax accounting calendar from Reporting Entity Setup */
3769               cursor c_get_tax_calendar
3770               is
3771               select tax_calendar_name
3772               from   jg_zz_vat_rep_entities
3773               where  vat_reporting_entity_id = p_vat_rep_entity_id;
3774 
3775               /**
3776                 Derrive start date and end date using period range parameters P_FROM_PERIOD and P_TO_PERIOD
3777               */
3778               cursor c_get_start_end_dates
3779               is
3780               select min(start_date), max(end_date)
3781               from   gl_periods
3782               where  period_set_name = lv_tax_calendar
3783               and    period_name in (P_FROM_PERIOD, P_TO_PERIOD);
3784 
3785             begin
3786 
3787               open  c_get_tax_calendar;
3788               fetch c_get_tax_calendar into lv_tax_calendar;
3789               close c_get_tax_calendar;
3790 
3791               open  c_get_start_end_dates;
3792               fetch c_get_start_end_dates  into ld_start_date, ld_end_date;
3793               close c_get_start_end_dates;
3794 
3795             /** Append the filter conditions for multi period reporting */
3796 	    --Bug 5525421 Commented. Not required these filters.
3797              /**   C_MOD349_AR_FILTER := C_MOD349_AR_FILTER || ' AND JZVRS.PERIOD_START_DATE >= ''' || ld_start_date || ''''
3798                                                        || ' AND JZVRS.PERIOD_END_DATE   <= ''' || ld_end_date || ''''
3799                                                        || ' AND JZVRS.TAX_CALENDAR_NAME = '''  || lv_tax_calendar  || ''''; */
3800            end;
3801 
3802           end if;
3803 
3804           lc_jgzz_modelo_query :=
3805              REPLACE( lc_jgzz_modelo_query
3806                         , '$MODELO_SPECIFIC_FILTERS$'
3807                         , C_MOD349_AR_FILTER );
3808 
3809           lc_jgzz_modelo_query :=
3810              REPLACE( lc_jgzz_modelo_query
3811                         , '$MODELO_SPECIFIC_GRP_FILTER$'
3812                         , '' );
3813 
3814           lc_jgzz_modelo_query :=
3815              REPLACE( lc_jgzz_modelo_query
3816                     , '$PROPERTY_FLAG_SEL_COL$'
3817                     , C_NULL_SEL_COL );
3818 
3819           lc_jgzz_modelo_query :=
3820              REPLACE( lc_jgzz_modelo_query
3821                     , '$PROPERTY_FLAG_GRP_COL$'
3822                     , '' );
3823           G_LINENO := '2.3.6';
3824 
3825        END IF;
3826        G_LINENO := '2.4';
3827        -- Insert AR Transations into Global Temp Table
3828       IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'Modelo SQL Statement: '||lc_jgzz_modelo_query); END IF;
3829 
3830        OPEN c_modelo_ext FOR lc_jgzz_modelo_query ;
3831        LOOP
3832           FETCH  c_modelo_ext
3833           INTO   lc_tax_registration_number
3834                 ,lc_customer_name
3835                 ,lc_customer_address_id
3836                 ,lc_billing_trading_partner_id
3837                 ,ln_sum_taxable_amt
3838                 ,ln_sum_trx_line_amt
3839                 ,ln_ncorrection_amount
3840                 ,ln_correction_amount
3841                 ,lc_correction_year
3842                 ,lc_correction_period
3843                 ,lc_triangulation
3844 	            	,lc_trx_period  -- Bug 5525421
3845                 ,lc_property_retail_flag
3846                 ,lc_clave_operation --FH: Added for modelo project
3847                 ,ln_transmission_property_amt
3848                 ,ln_year_of_amts_received_cash
3849                 ;
3850           IF G_DEBUG THEN
3851 	    	    fnd_file.put_line(FND_FILE.LOG,'lc_customer_name='||lc_customer_name||',lc_customer_address_id='||lc_customer_address_id);
3852                 fnd_file.put_line(FND_FILE.LOG,'lc_tax_registration_number='||lc_tax_registration_number);
3853                 fnd_file.put_line(FND_FILE.LOG,'lc_clave_operation='||lc_clave_operation);
3854 	          fnd_file.put_line(FND_FILE.LOG,'lc_triangulation'||lc_triangulation);
3855                 fnd_file.put_line(FND_FILE.LOG,'ln_year_of_amts_received_cash'||ln_year_of_amts_received_cash);
3856 	    END IF;
3857 
3858           EXIT WHEN c_modelo_ext%NOTFOUND;
3859 
3860           IF P_MODELO = '415' THEN
3861              G_LINENO := '2.4.1';
3862              -- ln_sum_trx_line_amt will have the gross amt for AR
3863              -- Assign NULL to ln_sum_taxable_amt
3864              ln_sum_taxable_amt := NULL;
3865              G_LINENO := '2.4.2';
3866              get_customer_address ( p_customer_address_id => lc_customer_address_id
3867                                   , x_postal_code         => lc_postal_code
3868                                   , x_city                => lc_city
3869                                   , x_address_detail      => lc_address_detail
3870                                   );
3871 
3872               G_LINENO := '2.4.3';
3873              lc_clave_operation         := lc_clave_operation;
3874              lc_tax_registration_number := SUBSTR( lc_tax_registration_number,1,9);
3875              lc_customer_name           := SUBSTR(lc_customer_name,1,40);
3876              lc_address_detail          := SUBSTR(lc_address_detail,1,32);
3877               G_LINENO := '2.4.4';
3878 
3879           ELSIF    P_MODELO = '347' THEN
3880 
3881                 -- Bug 8485057 this value is now selected from the record
3882                 -- lc_clave_operation := 'B' ;
3883                 IF G_DEBUG THEN
3884                   fnd_file.put_line(FND_FILE.LOG,G_LINENO);
3885                   fnd_file.put_line(FND_FILE.LOG,'lc_clave_operation: '||lc_clave_operation);
3886                   fnd_file.put_line(FND_FILE.LOG,'ln_sum_trx_line_amt  :='||ln_sum_trx_line_amt);
3887                   -- fnd_file.put_line(FND_FILE.LOG,'ln_sum_taxable_amt  :='||ln_sum_taxable_amt);
3888                   -- fnd_file.put_line(FND_FILE.LOG,'ln_transmission_property_amt='||ln_transmission_property_amt);
3889                 END IF;
3890 
3891                 G_LINENO := '2.4.5';
3892                 get_customer_address2
3893                     ( p_customer_address_id  =>  lc_customer_address_id
3894                     , x_postal               =>  lc_postal_code
3895 		        , x_post_code            =>  lc_postal_code1
3896                     , x_city                 =>  lc_city
3897                     , x_street_type          =>  lc_street_type -- sigla
3898                     , x_street               =>  lc_street      --via_publica
3899                     , x_number               =>  lc_number
3900                     , x_country              =>  lc_country
3901 
3902                     );
3903 
3904 
3905 
3906 
3907                 G_LINENO := '2.4.6';
3908 
3909                 ln_arrenda_amount :=  AR347_GROSS_AMOUNTFormula
3910                                        ( p_customer_id    => lc_billing_trading_partner_id
3911                                        , p_tipo           => lc_clave_operation
3912                                        , p_year_of_amts_rec_cash => ln_year_of_amts_received_cash);
3913 
3914                 IF G_DEBUG THEN
3915                   fnd_file.put_line(FND_FILE.LOG,G_LINENO||'After AR347_GROSS_AMOUNT');
3916                   fnd_file.put_line(FND_FILE.LOG,'ln_arrenda_amount  :='||ln_arrenda_amount);
3917                 END IF;
3918 
3919                  -- initilise amts to zero
3920 
3921                  ln_transmission_prop_amt_q1 := 0;
3922                  ln_transmission_prop_amt_q2 := 0;
3923                  ln_transmission_prop_amt_q3 := 0;
3924                  ln_transmission_prop_amt_q4 := 0;
3925                  ln_sum_trx_line_amt_q1      := 0;
3926                  ln_sum_trx_line_amt_q2      := 0;
3927                  ln_sum_trx_line_amt_q3      := 0;
3928                  ln_sum_trx_line_amt_q4      := 0;
3929                  ln_arrenda_amt_q1           := 0;
3930                  ln_arrenda_amt_q2           := 0;
3931                  ln_arrenda_amt_q3           := 0;
3932                  ln_arrenda_amt_q4           := 0;
3933 
3934                 If p_report_name = 'JEESAMOR' then
3935 
3936                  AR347_QTR_AMTS(lc_billing_trading_partner_id
3937                                 , lc_clave_operation
3938                                 , ln_year_of_amts_received_cash
3939                                 , ln_sum_trx_line_amt_q1
3940                                 , ln_sum_trx_line_amt_q2
3941                                 , ln_sum_trx_line_amt_q3
3942                                 , ln_sum_trx_line_amt_q4
3943                                 , ln_arrenda_amt_q1
3944                                 , ln_arrenda_amt_q2
3945                                 , ln_arrenda_amt_q3
3946                                 , ln_arrenda_amt_q4
3947                                 , ln_transmission_prop_amt_q1
3948                                 , ln_transmission_prop_amt_q2
3949                                 , ln_transmission_prop_amt_q3
3950                                 , ln_transmission_prop_amt_q4 );
3951 
3952                 End if;
3953 
3954 
3955 
3956                 G_LINENO := '2.4.7';
3957                 -- dbms_output.put_line ('ln_sum_trx_line_amt='||ln_sum_trx_line_amt||', ln_arrenda_amount =' || ln_arrenda_amount );
3958                 IF nvl(ln_arrenda_amount,0) <> 0 THEN
3959 
3960                   G_LINENO := '2.4.7.1';
3961                   ln_sum_trx_line_amt := ln_sum_trx_line_amt - ln_arrenda_amount ;
3962 
3963                   -- fnd_file.put_line(FND_FILE.LOG,G_LINENO);
3964                   -- fnd_file.put_line(FND_FILE.LOG,'new ln_sum_trx_line_amt  :='||ln_sum_trx_line_amt);
3965 
3966                   /* Bug 8485057: check any cash received for this customer, tipo and property rental */
3967                   ln_cash_received_amount := AR347_CASH_RECEIVEDFormula ( p_customer_id    => lc_billing_trading_partner_id
3968                                        , p_tipo           => lc_clave_operation
3969                                        , p_property_rental_flag => 'Y'
3970                                        , p_year_of_amts_rec_cash => ln_year_of_amts_received_cash);
3971 
3972                   IF G_DEBUG THEN
3973                     fnd_file.put_line(FND_FILE.LOG,'ln_cash_received_amount  :='||ln_cash_received_amount);
3974                     fnd_file.put_line(FND_FILE.LOG,'ln_sum_trx_line_amt_q1   :='||ln_sum_trx_line_amt_q1);
3975                     fnd_file.put_line(FND_FILE.LOG,'ln_sum_trx_line_amt_q2   :='||ln_sum_trx_line_amt_q2);
3976                     fnd_file.put_line(FND_FILE.LOG,'ln_sum_trx_line_amt_q3   :='||ln_sum_trx_line_amt_q3);
3977                     fnd_file.put_line(FND_FILE.LOG,'ln_sum_trx_line_amt_q4   :='||ln_sum_trx_line_amt_q4);
3978                     fnd_file.put_line(FND_FILE.LOG,'ln_arrenda_amt_q1        :='||ln_arrenda_amt_q1);
3979                     fnd_file.put_line(FND_FILE.LOG,'ln_arrenda_amt_q2        :='||ln_arrenda_amt_q2);
3980                     fnd_file.put_line(FND_FILE.LOG,'ln_arrenda_amt_q3        :='||ln_arrenda_amt_q3);
3981                     fnd_file.put_line(FND_FILE.LOG,'ln_arrenda_amt_q4        :='||ln_arrenda_amt_q4);
3982                     fnd_file.put_line(FND_FILE.LOG,'ln_transmission_prop_amt_q1 :='||ln_transmission_prop_amt_q1);
3983                     fnd_file.put_line(FND_FILE.LOG,'ln_transmission_prop_amt_q2 :='||ln_transmission_prop_amt_q2);
3984                     fnd_file.put_line(FND_FILE.LOG,'ln_transmission_prop_amt_q3 :='||ln_transmission_prop_amt_q3);
3985                     fnd_file.put_line(FND_FILE.LOG,'ln_transmission_prop_amt_q4 :='||ln_transmission_prop_amt_q4);
3986                     --fnd_file.put_line(FND_FILE.LOG,'receipt method:= '||fnd_profile.value('JEES_MOD347_RECEIPT_METHOD'));
3987                   END IF;
3988 
3989                   /* Bug 8485057 Transmission of property transactions are always different than property rental transactions */
3990                   INSERT INTO JG_ZZ_VAT_TRX_GT
3991                      ( jg_info_v1    -- lc_clave_operation
3992                      , jg_info_n2    -- ln_arrenda_amount
3993                      , jg_info_n17   -- ln_arrenda_amt_q1
3994                      , jg_info_n19   -- ln_arrenda_amt_q2
3995                      , jg_info_n21   -- ln_arrenda_amt_q3
3996                      , jg_info_n23   -- ln_arrenda_amt_q4
3997                      , jg_info_n6    -- ln_cash_received_amount
3998                      , jg_info_n28   -- ln_year_of_amts_received_cash
3999                      , jg_info_v11   -- lc_tax_registration_number
4000                      , jg_info_v12   -- c_modelo_rec.customer_name
4001                      , jg_info_v13   -- c_modelo_rec.customer_address_id
4002                      , jg_info_v14   -- c_modelo_rec.billing_trading_partner_id
4003                      , jg_info_v21   -- c_modelo_rec.property_retail_flag (flag_arrenda)
4004                      , jg_info_v18   -- lc_postal_code
4005                      , jg_info_v19   -- lc_city
4006                      , jg_info_v20   -- lc_number
4007                      , jg_info_v25   -- lc_street_type
4008                      , jg_info_v26   -- lc_street
4009                      , jg_info_v27   -- fin_id
4010                      , jg_info_v22   -- p_print_year --p_tax_calendar_year
4011                      , jg_info_v23   -- legal entity TRN
4012 		     , jg_info_v6    -- lc_country
4013 		     , jg_info_v28   -- lc_postal_code1
4014                      )
4015                   VALUES
4016                      ( lc_clave_operation
4017                      , ln_arrenda_amount
4018                      , ln_arrenda_amt_q1
4019                      , ln_arrenda_amt_q2
4020                      , ln_arrenda_amt_q3
4021                      , ln_arrenda_amt_q4
4022                      , ln_cash_received_amount
4023                      , ln_year_of_amts_received_cash
4024                      , lc_tax_registration_number
4025                      , lc_customer_name
4026                      , lc_customer_address_id
4027                      , lc_billing_trading_partner_id
4028                      , 'Y'
4029                      , lc_postal_code
4030                      , lc_city
4031                      , lc_number
4032                      , lc_street_type
4033                      , lc_street
4034                      , 'S'
4035                      , p_tax_year
4036                      , G_LE_TRN
4037 		     , lc_country
4038 		     , lc_postal_code1
4039                      ) ;
4040                   G_LINENO := '2.4.7.2';
4041                    arrenda ( p_vat_rep_entity_id => p_vat_rep_entity_id
4042                            , p_customer_id       => lc_billing_trading_partner_id
4043                            , p_customer_name     => lc_customer_name
4044                            , p_cust_tax_reg_num  => lc_tax_registration_number
4045                            , p_tipo              => lc_clave_operation
4046                            , p_year_of_amts_rec_cash => ln_year_of_amts_received_cash);
4047 
4048                   G_LINENO := '2.4.7.3';
4049 
4050                 END IF;
4051                 G_LINENO := '2.4.8';
4052                 IF ln_sum_trx_line_amt >0
4053                 THEN
4054                   G_LINENO := '2.4.8.1';
4055 
4056                   IF G_DEBUG THEN
4057                     fnd_file.put_line(FND_FILE.LOG,G_LINENO);
4058                     fnd_file.put_line(FND_FILE.LOG,'ln_sum_trx_line_amt: '||ln_sum_trx_line_amt );
4059                     fnd_file.put_line(FND_FILE.LOG,'lc_clave_operation: '||lc_clave_operation );
4060                   END IF;
4061 
4062                   /* Bug 8485057: check any cash received for this customer, tipo and no property rental */
4063                   ln_cash_received_amount := AR347_CASH_RECEIVEDFormula
4064                                        ( p_customer_id    => lc_billing_trading_partner_id
4065                                        , p_tipo           => lc_clave_operation
4066                                        , p_property_rental_flag => 'N'
4067                                        , p_year_of_amts_rec_cash => ln_year_of_amts_received_cash);
4068 
4069                   IF G_DEBUG THEN
4070                     fnd_file.put_line(FND_FILE.LOG,'ln_cash_received_amount  :='||ln_cash_received_amount);
4071                     -- fnd_file.put_line(FND_FILE.LOG,'receipt method:= '||fnd_profile.value('JEES_MOD347_RECEIPT_METHOD'));
4072                   END IF;
4073 
4074                   INSERT INTO JG_ZZ_VAT_TRX_GT
4075                     ( jg_info_v1    -- lc_clave_operation
4076                     , jg_info_n2    -- ln_sum_trx_line_amt
4077                     , jg_info_n17   -- ln_sum_trx_line_amt_q1
4078                     , jg_info_n19   -- ln_sum_trx_line_amt_q2
4079                     , jg_info_n21   -- ln_sum_trx_line_amt_q3
4080                     , jg_info_n23   -- ln_sum_trx_line_amt_q4
4081                     , jg_info_n5    -- ln_transmission_property_amt
4082                     , jg_info_n24  -- ln_transmission_prop_amt_q1
4083                     , jg_info_n25  -- ln_transmission_prop_amt_q2
4084                     , jg_info_n26  -- ln_transmission_prop_amt_q3
4085                     , jg_info_n27  -- ln_transmission_prop_amt_q4
4086                     , jg_info_n6    -- ln_cash_received_amount
4087                     , jg_info_n28   -- ln_year_of_amts_received_cash
4088                     , jg_info_v11   -- lc_tax_registration_number
4089                     , jg_info_v12   -- c_modelo_rec.customer_name
4090                     , jg_info_v13   -- c_modelo_rec.customer_address_id
4091                     , jg_info_v14   -- c_modelo_rec.billing_trading_partner_id
4092                     , jg_info_v21   -- c_modelo_rec.property_retail_flag -- flag_arrenda
4093                     , jg_info_v18   -- lc_postal_code
4094                     , jg_info_v19   -- lc_city           -- x_ref_catastral
4095                     , jg_info_v20   -- lc_number -- x_numero
4096                     , jg_info_v25   -- lc_street_type
4097                     , jg_info_v26   -- lc_street
4098                     , jg_info_v27   -- fin_id -- 'S'
4099                     , jg_info_v22   -- p_print_year --p_tax_calender_year
4100                     , jg_info_v23   -- legal entity TRN
4101 		    , jg_info_v6    -- lc_country
4102 		    , jg_info_v28   -- lc_postal_code1
4103                    )
4104                   VALUES
4105                     ( lc_clave_operation
4106                     , ln_sum_trx_line_amt
4107                     , ln_sum_trx_line_amt_q1
4108                     , ln_sum_trx_line_amt_q2
4109                     , ln_sum_trx_line_amt_q3
4110                     , ln_sum_trx_line_amt_q4
4111                     , ln_transmission_property_amt
4112                     , ln_transmission_prop_amt_q1
4113                     , ln_transmission_prop_amt_q2
4114                     , ln_transmission_prop_amt_q3
4115                     , ln_transmission_prop_amt_q4
4116                     , ln_cash_received_amount
4117                     , ln_year_of_amts_received_cash
4118                     , lc_tax_registration_number
4119                     , lc_customer_name
4120                     , lc_customer_address_id
4121                     , lc_billing_trading_partner_id
4122                     , 'N'
4123                     , lc_postal_code
4124                     , lc_city
4125                     , lc_number
4126                     , lc_street_type
4127                     , lc_street
4128                     , 'S'
4129                     , p_tax_year
4130                     , G_LE_TRN
4131 		    , lc_country
4132 		    , lc_postal_code1
4133                     ) ;
4134                    G_LINENO := '2.4.8.2';
4135 
4136                    -- fnd_file.put_line(FND_FILE.LOG,G_LINENO||'Inseted into JG_ZZ_VAT_TRX_GT');
4137 
4138                  end if;
4139                  G_LINENO := '2.4.9';
4140              ELSIF P_MODELO = '349' THEN
4141                 G_LINENO := '2.4.10';
4142                 IF P_REPORT_NAME = 'JEESAMOR' THEN
4143                   G_LINENO := '2.4.10.1';
4144                   ln_running_total           := 0;
4145                   ln_formerly_decl_amt       := 0;
4146                   lc_tax_registration_number := SUBSTR( lc_tax_registration_number,1,14);
4147                   lc_customer_name           := SUBSTR(lc_customer_name,1,40);
4148 		  lc_print_year := P_TAX_YEAR;  --Bug 5525421
4149 		  lc_print_period := lc_trx_period;  --Bug 5525421
4150                   G_LINENO := '2.4.10.2';
4151 
4152                   IF lc_correction_year IS NULL AND lc_correction_period IS NULL THEN --Not a Correction
4153 
4154                     ln_base_imponiable         := ln_ncorrection_amount;
4155                     ln_running_total           := ln_ncorrection_amount;
4156                      G_LINENO := '2.4.10.2.1';
4157 		     /*  BUG 9765740
4158                      SELECT DECODE(lc_triangulation,'X','T','E')
4159                      INTO   lc_clave_operation
4160                      FROM   DUAL ;
4161 		     */
4162 		    lc_clave_operation	       := lc_triangulation;
4163 
4164                      G_LINENO := '2.4.10.2.2';
4165 			IF G_DEBUG THEN
4166 			    fnd_file.put_line(FND_FILE.LOG,'IF :
4167 							lc_clave_operation='||lc_clave_operation||',
4168 							lc_tax_registration_number='||lc_tax_registration_number||',
4169 							lc_customer_name='||lc_customer_name||',
4170 							lc_correction_year='||lc_correction_year||',
4171 							lc_correction_period='||lc_correction_period||',
4172 							lc_triangulation='||lc_triangulation||'
4173 							lc_print_year='||lc_print_year||',
4174 							lc_print_period='||lc_print_period
4175 							);
4176 			END IF;
4177 
4178                   ELSIF lc_correction_year IS NOT NULL AND lc_correction_period IS NOT NULL THEN --IS a Correction
4179                      G_LINENO := '2.4.10.2.3';
4180                      ln_formerly_decl_amt  := 0;
4181                      lc_correction_year    := lc_correction_year; --SUBSTR(lc_correction_year,3,2);
4182                      ln_base_imponiable    := ln_correction_amount;
4183                     G_LINENO := '2.4.10.2.4';
4184 
4185 			IF G_DEBUG THEN
4186 			    fnd_file.put_line(FND_FILE.LOG,'ELSE :
4187 							lc_clave_operation='||lc_clave_operation||',
4188 							lc_tax_registration_number='||lc_tax_registration_number||',
4189 							lc_customer_name='||lc_customer_name||',
4190 							lc_correction_year='||lc_correction_year||',
4191 							lc_correction_period='||lc_correction_period||',
4192 							lc_triangulation='||lc_triangulation||'
4193 							lc_print_year='||lc_print_year||',
4194 							lc_print_period='||lc_print_period
4195 							);
4196 			END IF;
4197 		     /* -- BUG 9765740
4198 
4199                      SELECT DECODE(lc_triangulation,'X','t','e')  -- Bug 5525421
4200                      INTO   lc_clave_operation
4201                      FROM   DUAL ;
4202 		     */
4203 		     lc_clave_operation	       := lower(lc_triangulation);
4204 
4205                      G_LINENO := '2.4.10.2.5';
4206                      IF lc_clave_operation IN ('1','2') THEN
4207                           lc_sign := ' ';
4208                      ELSE
4209                           IF ln_base_imponiable < 0 THEN
4210                              lc_sign := '-' ;
4211                           ELSE
4212                              lc_sign := '+' ;
4213                           END IF;
4214                      END IF;
4215                      G_LINENO := '2.4.10.2.6';
4216 
4217 		     begin
4218                        SELECT NVL(jg_info_n10,0) -- running total
4219                        INTO   ln_formerly_decl_amt
4220                        FROM   JG_ZZ_VAT_TRX_GT  M349
4221                        WHERE  RTRIM(jg_info_v11)      = RTRIM(lc_tax_registration_number)
4222                        AND    RTRIM(jg_info_v12)   = RTRIM(lc_customer_name)
4223                        AND    jg_info_n13          = lc_correction_year -- correction year     -- Bug 5525421
4224                        AND    jg_info_v27          = lc_correction_period -- correction period -- Bug 5525421
4225                       -- AND    RTRIM(jg_info_v21)      = RTRIM(lc_correction_year) -- Bug 5525421
4226                       -- AND    UPPER(jg_info_v1)    =  upper(DECODE(lc_triangulation,'X','T','E'))  -- BUG 9765740
4227 		       		   AND  UPPER(jg_info_v1)    =  upper(lc_triangulation)  								 -- BUG 9765740
4228 		       		   AND  jg_info_v15 is NULL		-- Added as part of Bug fix for BUG 9765740
4229 		       		   AND  jg_info_v16 is NULL;	-- Added as part of Bug fix for BUG 9765740
4230 
4231 
4232                      exception
4233                        when no_data_found then
4234                        ln_formerly_decl_amt :=0;
4235   		     end;
4236                      G_LINENO := '2.4.10.2.7';
4237                      --
4238                      -- update running total
4239                      -- -- Bug 5525421 As per R11i logic this update should happend after the insert. Hence moving this to after insert.
4240                   /*   UPDATE JG_ZZ_VAT_TRX_GT
4241                      SET    jg_info_n10  = ln_formerly_decl_amt + ln_ncorrection_amount
4242                      WHERE  RTRIM(jg_info_v15)    = RTRIM(lc_correction_year)--RTRIM(SUBSTR(lc_correction_year,3,2))
4243                      AND    RTRIM(jg_info_v16)    = RTRIM(lc_correction_period)
4244                      AND    jg_info_v15           IS NOT NULL
4245                      AND    jg_info_v16           IS NOT NULL
4246                      AND    RTRIM(jg_info_v12)    = RTRIM(lc_tax_registration_number)
4247                      AND    RTRIM(jg_info_v13)    = RTRIM(lc_customer_name)
4248                      AND    UPPER(jg_info_v11)    = upper(DECODE(lc_triangulation,'X','T','E')); */
4249                      G_LINENO := '2.4.10.2.8';
4250                   END IF;
4251                   G_LINENO := '2.4.10.3 ';
4252                  -- JGZZVEFT(); -- Bug 5525421
4253                   G_LINENO := '2.4.10.4';
4254                 END IF;
4255                 G_LINENO := '2.4.11';
4256           END IF;
4257              G_LINENO := '2.5';
4258 
4259           IF  P_MODELO <> '347' THEN
4260               G_LINENO := '2.5.1';
4261              INSERT INTO JG_ZZ_VAT_TRX_GT
4262                ( jg_info_v1    -- lc_clave_operation
4263                , jg_info_v11   -- c_modelo_rec.tax_registration_number
4264                , jg_info_v12   -- c_modelo_rec.customer_name
4265                , jg_info_v13   -- c_modelo_rec.customer_address_id
4266                , jg_info_v14   -- c_modelo_rec.billing_trading_partner_id
4267                , jg_info_n1    -- c_modelo_rec.sum_taxable_amt     -- 347-AP, 415-AP
4268                , jg_info_n2    -- c_modelo_rec.sum_trx_line_amt    -- 347-AR, 415-AR
4269                , jg_info_n3    -- c_modelo_rec.ncorrection_amount  -- 349
4270                , jg_info_n4    -- c_modelo_rec.correction_amount   -- 349
4271                , jg_info_v15   -- c_modelo_rec.correction_year
4272                , jg_info_v16   -- c_modelo_rec.correction_period
4273                , jg_info_v17   -- c_modelo_rec.triangulation
4274                , jg_info_v21   -- c_modelo_rec.property_retail_flag
4275                , jg_info_v18   -- lc_postal_code
4276                , jg_info_v19   -- lc_city           -- x_ref_catastral
4277                , jg_info_v20   -- lc_address_detail -- x_numero
4278                , jg_info_n10   -- ln_running_total
4279                , jg_info_n11   -- ln_formerly_decl_amt
4280                , jg_info_n12   -- ln_base_imponiable
4281                , jg_info_v22   -- p_print_year --p_tax_calender_year
4282                , jg_info_v23   -- legal entity name
4283                , jg_info_v26   -- G_LE_TRN -- 347
4284                , jg_info_v24   -- lc_sign
4285                , jg_info_v25   -- p_tax_office -- 347
4286 	       , jg_info_n13   -- lc_prtint_year -- Bug 5525421
4287 	       , jg_info_v27   -- lc_print_period -- Bug 5525421
4288                )
4289              VALUES
4290                ( lc_clave_operation
4291                , lc_tax_registration_number
4292                , lc_customer_name
4293                , lc_customer_address_id
4294                , lc_billing_trading_partner_id
4295                , ln_sum_taxable_amt
4296                , ln_sum_trx_line_amt
4297                , ln_ncorrection_amount
4298                , ln_correction_amount
4299                , lc_correction_year
4300                , lc_correction_period
4301                , lc_triangulation
4302                , lc_property_retail_flag
4303                , lc_postal_code
4304                , lc_city
4305                , lc_address_detail
4306                , ln_running_total
4307                , ln_formerly_decl_amt
4308                , ln_base_imponiable
4309                , P_TAX_YEAR
4310                , G_LE_NAME
4311                , G_LE_TRN
4312                , lc_sign
4313                , p_tax_office
4314 	       , lc_print_year  -- Bug 5525421
4315 	       , lc_print_period -- Bug 5525421
4316                );
4317 
4318 		  -- Bug 5525421 The updating should happend only after inserting the correction record.
4319 
4320 		  IF P_MODELO = '349' THEN
4321 		    IF P_REPORT_NAME = 'JEESAMOR' THEN
4322 			IF lc_correction_year IS NOT NULL AND lc_correction_period IS NOT NULL THEN --IS a Correction
4323 
4324 		         UPDATE JG_ZZ_VAT_TRX_GT
4325                      SET    jg_info_n10  = ln_formerly_decl_amt + ln_ncorrection_amount
4326                      WHERE  RTRIM(jg_info_v15)    = RTRIM(lc_correction_year)--RTRIM(SUBSTR(lc_correction_year,3,2))
4327                      AND    RTRIM(jg_info_v16)    = RTRIM(lc_correction_period)
4328                      AND    jg_info_v15           IS NOT NULL
4329                      AND    jg_info_v16           IS NOT NULL
4330                      AND    RTRIM(jg_info_v12)    = RTRIM(lc_tax_registration_number)
4331                      AND    RTRIM(jg_info_v13)    = RTRIM(lc_customer_name)
4332                   -- AND    UPPER(jg_info_v11)    = upper(DECODE(lc_triangulation,'X','T','E')); -- BUG 9765740
4333 		     		 AND  UPPER(jg_info_v1)    =  upper(lc_triangulation);  							 -- BUG 9765740
4334 
4335 			END IF;
4336 	          END IF;
4337 	        END IF;
4338 
4339             END IF;
4340             G_LINENO := '2.5.2';
4341        END LOOP;
4342        G_LINENO := '2.5.3';
4343        CLOSE c_modelo_ext; -- closecur
4344        G_LINENO := '2.5.4';
4345     END IF;
4346 
4347     -- Bug 5525421 This procedure should call only once i.e after iserting all AP  and  AR records in to temp table.
4348 	IF P_MODELO = '349' THEN
4349 	  IF P_REPORT_NAME = 'JEESAMOR' THEN
4350 		    JGZZVEFT();
4351 	  END IF;
4352 	END IF;
4353     G_LINENO := '2.6';
4354 
4355 
4356 ELSE
4357 
4358    IF G_DEBUG THEN
4359      fnd_file.put_line(FND_FILE.LOG,'Modelo 340 AP Processing ...');
4360       fnd_file.put_line(FND_FILE.LOG,'Driving Date ...'||p_driving_date);
4361    END IF;
4362 
4363    G_LINENO := '2.5.5';
4364 
4365    -- 260974
4366    -- Declaration of base 340 query
4367    lc_jgzz_mod_query_340 :=
4368    'SELECT  decode(substrb(jzvtd.billing_tp_tax_reg_num,1,2),''ES'',
4369                    jzvtd.billing_tp_taxpayer_id)                                      DECLARED_NIF,
4370              substrb(jzvtd.billing_tp_name,1,40)                                      DECLARED_NAME,
4371              je_es_modelo_ext_pkg.getKeyID(substrb(jzvtd.billing_tp_tax_reg_num,1,2)) KEY_ID,  -- BUG 8946271
4372              decode(substrb(jzvtd.billing_tp_tax_reg_num,1,2),''ES'',null,
4373                     substrb(jzvtd.billing_tp_taxpayer_id,1,17))                       FISCAL_ID,
4374              decode(substr(api.document_sub_type,1,6), ''MOD340'', substr(api.document_sub_type,8,1),
4375              decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4376                ,''347'',''R'',''415'',''S'',''349'',''R''))                           BOOK_TYPE,
4377              decode( instr(zxdf.user_defined_fisc_class,''NONE'',7,1),7,'' '', substr(zxdf.user_defined_fisc_class,7,1) )     TRANSACTION_CODE,
4378              jzvtd.trx_date                                                           ISSUE_DATE,
4379              jzvtd.tax_invoice_date                                                   TRANSACTION_DATE,
4380              jzvtd.trx_number                                                         INVOICE_IDENT,
4381              jzvtd.doc_seq_value                                                      REGISTER_NUMBER,
4382              decode(substr(api.document_sub_type,1,8),''MOD340/U'',
4383                     substr(api.document_sub_type,9,1))                                INTRA_TYPE,
4384              decode(substr(api.document_sub_type,1,8), ''MOD340/U'',''D''
4385                 ,decode(substr(zxdf.trx_business_category,
4386                 (instr(zxdf.trx_business_category,''MOD'',1,1)+3),3),''349'',''D''))       KEY_OF_DECLARED,
4387              api.global_attribute10                                                   TRANSACTION_DEADLINE,
4388              decode(substr(api.document_sub_type,1,8)
4389                     ,''MOD340/U'',substrb(jzvtd.TRX_LINE_DESCRIPTION,1,35)
4390                     ,decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4391                     ,''349'',substrb(jzvtd.TRX_LINE_DESCRIPTION,1,35)))               DESC_OF_GOODS,
4392              SUM(DECODE(jzvtd.OFFSET_FLAG,''N'',
4393                    nvl(JZVTD.taxable_amt_funcl_curr,JZVTD.taxable_amt)*(JZVTD.tax_recovery_rate/100)
4394 			,0))                     TAXABLE_AMOUNT,
4395              SUM(nvl(jzvtd.tax_amt_funcl_curr,jzvtd.tax_amt ))                        TAX_AMOUNT,
4396              SUM(DECODE(jzvtd.OFFSET_FLAG,''N'',
4397                    nvl(JZVTD.taxable_amt_funcl_curr,JZVTD.taxable_amt)*(JZVTD.tax_recovery_rate/100),0)) +
4398              SUM(nvl(DECODE(jzvtd.tax_amt_funcl_curr,
4399                           0, jzvtd.tax_amt ,
4400                           NULL, jzvtd.tax_amt,
4401                           jzvtd.tax_amt_funcl_curr), 0))                             INV_TOTAL_AMOUNT,
4402              SUM(decode(jzvtd.tax_recoverable_flag, ''Y'',
4403                     nvl(jzvtd.tax_amt_funcl_curr,jzvtd.tax_amt )))                   DEDUCTABLE_AMOUNT,
4404              jzvtd.tax_rate                                                          TAX_RATE,
4405              APSS.VENDOR_SITE_ID
4406     FROM    jg_zz_vat_trx_details jzvtd, jg_zz_vat_rep_status  jzvrs
4407           , AP_SUPPLIER_SITES_ALL APSS, AP_SUPPLIERS APS, AP_INVOICES_ALL API,
4408 	    zx_lines_det_factors zxdf
4409     WHERE   (substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3) in (''347'',''349'',''415'')
4410              OR substr(jzvtd.document_sub_type,1,6) = ''MOD340'')
4411     AND       nvl(jzvtd.document_sub_type,''X'') <> ''MOD340_EXCL''
4412     AND     JZVRS.vat_reporting_entity_id  = $P_VAT_REP_ENTITY_ID$
4413     AND     JZVTD.reporting_status_id in (SELECT DISTINCT JZRS.reporting_status_id JZRS
4414     			                  FROM  jg_zz_vat_rep_status JZRS
4415      				          WHERE JZRS.vat_reporting_entity_id = $P_VAT_REP_ENTITY_ID$
4416                                           AND   JZRS.source = ''AP'' )
4417     AND     $FILTER_KEY$ $FILTER_OPER$ $FILTER_VALUE$
4418             $340_PERIOD_KEY$ $340_PERIOD_KEY_FROM$ $340_PERIOD_OPER$ $340_PERIOD_KEY_TO$
4419     AND     JZVTD.trx_line_type <> ''AWT''
4420     AND     JZVTD.trx_line_class <> ''EXPENSE REPORTS''
4421     AND     JZVTD.applied_from_line_id IS NULL
4422     AND     JZVTD.extract_source_ledger =  ''AP''
4423     AND     JZVRS.source =  ''AP''
4424     AND     JZVTD.BILLING_TRADING_PARTNER_ID =  APS.VENDOR_ID
4425     AND     APS.VENDOR_ID =  APSS.VENDOR_ID
4426     AND     NVL(APS.FEDERAL_REPORTABLE_FLAG,''Y'')=  ''Y''
4427     AND     APSS.TAX_REPORTING_SITE_FLAG =  ''Y''
4428     AND     APSS.ORG_ID =  $P_ORG_ID$
4429     AND     jzvtd.trx_id = api.invoice_id
4430     AND     jzvtd.trx_id = zxdf.trx_id
4431     AND     jzvtd.trx_line_id = zxdf.trx_line_id
4432 	AND      (  (decode(substr(api.document_sub_type,1,6), ''MOD340'', substr(api.document_sub_type,8,1),
4433              decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4434                ,''347'',''R'',''415'',''S'',''349'',''R'')) <> ''R'')
4435 	 OR (decode(substr(api.document_sub_type,1,6), ''MOD340'', substr(api.document_sub_type,8,1),
4436              decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4437                ,''347'',''R'',''415'',''S'',''349'',''R'')) = ''R''
4438           AND decode( instr(zxdf.user_defined_fisc_class,''NONE'',7,1),7,'' '',
4439 		                                      substr(zxdf.user_defined_fisc_class,7,1) ) NOT IN (''P'',''I'') )
4440       OR ((decode(substr(api.document_sub_type,1,6), ''MOD340'', substr(api.document_sub_type,8,1),
4441              decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4442                ,''347'',''R'',''415'',''S'',''349'',''R'')) = ''R''
4443            AND decode( instr(zxdf.user_defined_fisc_class,''NONE'',7,1),7,'' '',
4444 		                                     substr(zxdf.user_defined_fisc_class,7,1) ) IN (''P'',''I'') )
4445     AND jzvtd.reporting_code <> ''OFFSET'' ))
4446     GROUP BY decode(substrb(jzvtd.billing_tp_tax_reg_num,1,2),''ES'',
4447       jzvtd.billing_tp_taxpayer_id),
4448       substrb(jzvtd.billing_tp_name,1,40),
4449       je_es_modelo_ext_pkg.getKeyID(substrb(jzvtd.billing_tp_tax_reg_num,1,2)),
4450       decode(substrb(jzvtd.billing_tp_tax_reg_num,1,2),''ES'',null,
4451              substrb(jzvtd.billing_tp_taxpayer_id,1,17)),
4452       decode(substr(api.document_sub_type,1,6), ''MOD340'', substr(api.document_sub_type,8,1),
4453       decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4454          ,''347'',''R'',''415'',''S'',''349'',''R'')),
4455       decode( instr(zxdf.user_defined_fisc_class,''NONE'',7,1),7,'' '', substr(zxdf.user_defined_fisc_class,7,1) ),
4456       jzvtd.trx_date, jzvtd.tax_invoice_date, jzvtd.trx_number, jzvtd.doc_seq_value,
4457       decode(substr(api.document_sub_type,1,8),''MOD340/U'',
4458          substr(api.document_sub_type,9,1)),
4459       decode(substr(api.document_sub_type,1,8), ''MOD340/U'',''D''
4460         ,decode(substr(zxdf.trx_business_category,
4461         (instr(zxdf.trx_business_category,''MOD'',1,1)+3),3),''349'',''D'')),
4462       api.global_attribute10,
4463       decode(substr(api.document_sub_type,1,8)
4464         ,''MOD340/U'',substrb(jzvtd.TRX_LINE_DESCRIPTION,1,35)
4465         ,decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4466         ,''349'',substrb(jzvtd.TRX_LINE_DESCRIPTION,1,35))),
4467       jzvtd.tax_rate, APSS.VENDOR_SITE_ID ';
4468 
4469   lc_jgzz_mod_query_340_exp :=
4470   'UNION SELECT decode(substrb(jzvtd.billing_tp_tax_reg_num,1,2),''ES'',
4471              NVL(JZVTD.merchant_party_taxpayer_id, JZVTD.billing_tp_taxpayer_id))     DECLARED_NIF,
4472              substrb(NVL(JZVTD.merchant_party_name,JZVTD.billing_tp_name),1,40)       DECLARED_NAME,
4473              je_es_modelo_ext_pkg.getKeyID(substrb(jzvtd.billing_tp_tax_reg_num,1,2)) KEY_ID, --BUG 8946271
4474              decode(substrb(jzvtd.billing_tp_tax_reg_num,1,2),''ES'',null,
4475                     substrb(jzvtd.billing_tp_taxpayer_id,1,17))                       FISCAL_ID,
4476              decode(substr(api.document_sub_type,1,6), ''MOD340'', substr(api.document_sub_type,8,1),
4477              decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4478                ,''347'',''R'',''415'',''S'',''349'',''R''))                           BOOK_TYPE,
4479              decode( instr(zxdf.user_defined_fisc_class,''NONE'',7,1),7,'' '', substr(zxdf.user_defined_fisc_class,7,1) ) TRANSACTION_CODE,
4480              jzvtd.trx_date                                                           ISSUE_DATE,
4481              jzvtd.tax_invoice_date                                                   TRANSACTION_DATE,
4482              jzvtd.trx_number                                                         INVOICE_IDENT,
4483              jzvtd.doc_seq_value                                                      REGISTER_NUMBER,
4484              decode(substr(api.document_sub_type,1,8),''MOD340/U'',
4485                     substr(api.document_sub_type,9,1))                                INTRA_TYPE,
4486              decode(substr(api.document_sub_type,1,8), ''MOD340/U'',''D''
4487                 ,decode(substr(zxdf.trx_business_category,
4488                 (instr(zxdf.trx_business_category,''MOD'',1,1)+3),3),''349'',''D''))       KEY_OF_DECLARED,
4489              api.global_attribute10                                                   TRANSACTION_DEADLINE,
4490              decode(substr(api.document_sub_type,1,8)
4491                     ,''MOD340/U'',substrb(jzvtd.TRX_LINE_DESCRIPTION,1,35)
4492                     ,decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4493                     ,''349'',substrb(jzvtd.TRX_LINE_DESCRIPTION,1,35)))               DESC_OF_GOODS,
4494              SUM(DECODE(jzvtd.OFFSET_FLAG,''N'',
4495                    nvl(JZVTD.taxable_amt_funcl_curr,JZVTD.taxable_amt)*(JZVTD.tax_recovery_rate/100),0))                     TAXABLE_AMOUNT,
4496              SUM(nvl(DECODE(jzvtd.tax_amt_funcl_curr,
4497                           0, jzvtd.tax_amt ,
4498                           NULL, jzvtd.tax_amt,
4499                           jzvtd.tax_amt_funcl_curr), 0))                              TAX_AMOUNT,
4500              SUM(DECODE(jzvtd.OFFSET_FLAG,''N'',
4501                    nvl(JZVTD.taxable_amt_funcl_curr,JZVTD.taxable_amt)*(JZVTD.tax_recovery_rate/100)
4502 		   ,0)) +
4503              SUM(nvl(DECODE(jzvtd.tax_amt_funcl_curr,
4504                           0, jzvtd.tax_amt ,
4505                           NULL, jzvtd.tax_amt,
4506                           jzvtd.tax_amt_funcl_curr), 0))                             INV_TOTAL_AMOUNT,
4507              SUM(decode(jzvtd.tax_recoverable_flag, ''Y'',
4508                     nvl(jzvtd.tax_amt_funcl_curr,jzvtd.tax_amt )))                   DEDUCTABLE_AMOUNT,
4509              jzvtd.tax_rate                                                          TAX_RATE,
4510              APSS.VENDOR_SITE_ID
4511      FROM    jg_zz_vat_trx_details jzvtd, jg_zz_vat_rep_status  jzvrs
4512              , AP_SUPPLIER_SITES_ALL APSS, AP_SUPPLIERS APS, AP_INVOICES_ALL API,
4513 	    zx_lines_det_factors zxdf
4514      WHERE   (substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3) in (''347'',''349'',''415'')
4515              OR substr(jzvtd.document_sub_type,1,6) = ''MOD340'')
4516      AND       nvl(jzvtd.document_sub_type,''X'') <> ''MOD340_EXCL''
4517      AND     JZVRS.vat_reporting_entity_id = $P_VAT_REP_ENTITY_ID$
4518      AND     JZVRS.reporting_status_id = JZVTD.reporting_status_id
4519      AND     JZVTD.extract_source_ledger = ''AP''
4520      AND     JZVRS.source = ''AP''
4521      AND     $FILTER_KEY$ $FILTER_OPER$ $FILTER_VALUE$
4522              $340_PERIOD_KEY$ $340_PERIOD_KEY_FROM$ $340_PERIOD_OPER$ $340_PERIOD_KEY_TO$
4523      AND     JZVTD.trx_line_type NOT IN (''AWT'',''TAX'',''PREPAY'')
4524      AND     JZVTD.trx_line_class = ''EXPENSE REPORTS''
4525      AND     JZVTD.applied_from_line_id IS NULL
4526      AND     JZVTD.extract_source_ledger =  ''AP''
4527      AND     JZVRS.source =  ''AP''
4528      AND     JZVTD.BILLING_TRADING_PARTNER_ID =  APS.VENDOR_ID
4529      AND     APS.VENDOR_ID =  APSS.VENDOR_ID
4530      AND     NVL(APS.FEDERAL_REPORTABLE_FLAG,''Y'')=  ''Y''
4531      AND     APSS.TAX_REPORTING_SITE_FLAG = ''Y''
4532      AND     APSS.ORG_ID = $P_ORG_ID$
4533      AND     jzvtd.trx_id = api.invoice_id
4534      AND     jzvtd.trx_id = zxdf.trx_id
4535      AND     jzvtd.trx_line_id = zxdf.trx_line_id
4536 	 	AND       ( (decode(substr(api.document_sub_type,1,6), ''MOD340'', substr(api.document_sub_type,8,1),
4537              decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4538                ,''347'',''R'',''415'',''S'',''349'',''R'')) <> ''R'')
4539 	 OR (decode(substr(api.document_sub_type,1,6), ''MOD340'', substr(api.document_sub_type,8,1),
4540              decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4541                ,''347'',''R'',''415'',''S'',''349'',''R'')) = ''R''
4542           AND decode( instr(zxdf.user_defined_fisc_class,''NONE'',7,1),7,'' '',
4543 		                                      substr(zxdf.user_defined_fisc_class,7,1) ) NOT IN (''P'',''I'') )
4544       OR ((decode(substr(api.document_sub_type,1,6), ''MOD340'', substr(api.document_sub_type,8,1),
4545              decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4546                ,''347'',''R'',''415'',''S'',''349'',''R'')) = ''R''
4547            AND decode( instr(zxdf.user_defined_fisc_class,''NONE'',7,1),7,'' '',
4548 		                                     substr(zxdf.user_defined_fisc_class,7,1) ) IN (''P'',''I'') )
4549     AND jzvtd.reporting_code <> ''OFFSET'' ))
4550      GROUP BY decode(substrb(jzvtd.billing_tp_tax_reg_num,1,2),''ES'',
4551        NVL(JZVTD.merchant_party_taxpayer_id,
4552        JZVTD.billing_tp_taxpayer_id)),
4553        substrb(NVL(JZVTD.merchant_party_name,JZVTD.billing_tp_name),1,40),
4554       je_es_modelo_ext_pkg.getKeyID(substrb(jzvtd.billing_tp_tax_reg_num,1,2)),
4555       decode(substrb(jzvtd.billing_tp_tax_reg_num,1,2),''ES'',null,
4556              substrb(jzvtd.billing_tp_taxpayer_id,1,17)),
4557        decode(substr(api.document_sub_type,1,6), ''MOD340'', substr(api.document_sub_type,8,1),
4558        decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4559                ,''347'',''R'',''415'',''S'',''349'',''R'')),
4560        decode( instr(zxdf.user_defined_fisc_class,''NONE'',7,1),7,'' '', substr(zxdf.user_defined_fisc_class,7,1) ),
4561        jzvtd.trx_date, jzvtd.tax_invoice_date, jzvtd.trx_number, jzvtd.doc_seq_value,
4562        decode(substr(api.document_sub_type,1,8),''MOD340/U'', substr(api.document_sub_type,9,1)),
4563        decode(substr(api.document_sub_type,1,8), ''MOD340/U'',''D''
4564                 ,decode(substr(zxdf.trx_business_category,
4565                 (instr(zxdf.trx_business_category,''MOD'',1,1)+3),3),''349'',''D'')),
4566        api.global_attribute10,
4567        decode(substr(api.document_sub_type,1,8)
4568          ,''MOD340/U'',substrb(jzvtd.TRX_LINE_DESCRIPTION,1,35)
4569          ,decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4570          ,''349'',substrb(jzvtd.TRX_LINE_DESCRIPTION,1,35))),
4571        jzvtd.tax_rate, APSS.VENDOR_SITE_ID ';
4572 
4573   lc_jgzz_mod_query_340 := lc_jgzz_mod_query_340 || lc_jgzz_mod_query_340_exp;
4574 
4575         -- Date processing for query
4576         declare
4577           lv_filter_key varchar2 (150) ;
4578           lv_filter_value varchar2 (2000);
4579           lv_filter_oper varchar2(2);
4580 
4581         begin
4582 
4583   -- ****************************************************************************
4584   -- ****  For Period report restricts by start / end dates
4585   -- ****  AND by PERIOD
4586   -- ****************************************************************************
4587           if p_report_name = 'JEESPMOR' then
4588 
4589             lv_filter_key := ' JZVTD.trx_date BETWEEN JZVRS.period_start_date and JZVRS.period_end_date
4590 		       AND JZVRS.TAX_CALENDAR_PERIOD';
4591            if p_driving_date = 'GL' then
4592 
4593             lv_filter_key :=
4594             REPLACE( lv_filter_key
4595                     ,'trx_date'
4596                     ,'gl_date'
4597                     );
4598             end if;
4599             lv_filter_value := l_period_string;
4600             lv_filter_oper := 'In';
4601 
4602             lc_jgzz_mod_query_340 :=
4603                REPLACE( lc_jgzz_mod_query_340
4604                       , '$340_PERIOD_KEY$'
4605                       , ' ' );
4606 
4607             lc_jgzz_mod_query_340 :=
4608                REPLACE( lc_jgzz_mod_query_340
4609                       , '$340_PERIOD_KEY_FROM$'
4610                       , ' ' );
4611 
4612             lc_jgzz_mod_query_340 :=
4613                REPLACE( lc_jgzz_mod_query_340
4614                       , '$340_PERIOD_OPER$'
4615                       , ' ' );
4616 
4617             lc_jgzz_mod_query_340 :=
4618                REPLACE( lc_jgzz_mod_query_340
4619                       , '$340_PERIOD_KEY_TO$'
4620                       , ' ' );
4621 
4622   -- ****************************************************************************
4623   -- ****  For Annual report restricts by start / end dates
4624   -- ****  AND by TAX CALENDAR YEAR
4625   -- ****************************************************************************
4626           elsif p_report_name = 'JEESAMOR' then
4627 
4628             -- Original modelo processing
4629             lv_filter_key := 'JZVTD.trx_date BETWEEN JZVRS.period_start_date and JZVRS.period_end_date
4630 		       AND JZVRS.TAX_CALENDAR_YEAR';
4631            if p_driving_date = 'GL' then
4632 
4633             lv_filter_key :=
4634             REPLACE( lv_filter_key
4635                     ,'trx_date'
4636                     ,'gl_date'
4637                     );
4638             end if;
4639             lv_filter_value := p_tax_year;
4640             lv_filter_oper := '=';
4641 
4642             -- Set start and end dates, specific to 340
4643             set_dates;
4644 
4645             fnd_file.put_line(FND_FILE.LOG,'**** start '||p_340_start_date||' end '||p_340_end_date);
4646 
4647            if p_driving_date = 'GL' then
4648              lc_jgzz_mod_query_340 :=
4649                REPLACE( lc_jgzz_mod_query_340
4650                       , '$340_PERIOD_KEY$'
4651                       , 'AND JZVTD.gl_date BETWEEN  ' );
4652 
4653             else--if p_driving_date ='TRX'
4654               lc_jgzz_mod_query_340 :=
4655 	                   REPLACE( lc_jgzz_mod_query_340
4656 	                          , '$340_PERIOD_KEY$'
4657                       , 'AND JZVTD.trx_date BETWEEN  ' );
4658              end if;
4659 
4660             lc_jgzz_mod_query_340 :=
4661                REPLACE( lc_jgzz_mod_query_340
4662                       , '$340_PERIOD_KEY_FROM$'
4663                       ,'''' || p_340_start_date ||'''');
4664                       -- , p_340_start_date );
4665 
4666             lc_jgzz_mod_query_340 :=
4667                REPLACE( lc_jgzz_mod_query_340
4668                       , '$340_PERIOD_OPER$'
4669                       , 'AND' );
4670 
4671             lc_jgzz_mod_query_340 :=
4672                REPLACE( lc_jgzz_mod_query_340
4673                       , '$340_PERIOD_KEY_TO$'
4674                       ,'''' || p_340_end_date ||'''');
4675 
4676           end if;
4677 
4678         lc_jgzz_mod_query_340 :=
4679                REPLACE( lc_jgzz_mod_query_340
4680                       , '$FILTER_KEY$'
4681                       , lv_filter_key
4682                       );
4683 
4684         lc_jgzz_mod_query_340 :=
4685                REPLACE( lc_jgzz_mod_query_340
4686                       , '$FILTER_OPER$'
4687                       , lv_filter_oper
4688                       );
4689 
4690          lc_jgzz_mod_query_340 :=
4691                REPLACE( lc_jgzz_mod_query_340
4692                       , '$FILTER_VALUE$'
4693                       , lv_filter_value
4694                       );
4695 
4696         end;
4697 
4698          lc_jgzz_mod_query_340 :=
4699                REPLACE( lc_jgzz_mod_query_340
4700                       , '$P_VAT_REP_ENTITY_ID$'
4701                       , TO_CHAR(P_VAT_REP_ENTITY_ID)
4702                       );
4703 
4704          lc_jgzz_mod_query_340 :=
4705                REPLACE( lc_jgzz_mod_query_340
4706                       , '$P_ORG_ID$'
4707                       , P_ORG_ID );
4708 
4709 IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'2.5.6.1 Open the AP Dynamic Query for 340 : '||lc_jgzz_mod_query_340 ); END IF;
4710 
4711 
4712        OPEN c_modelo_340 FOR lc_jgzz_mod_query_340 ;
4713        LOOP
4714 
4715           G_LINENO := '2.5.5.1';
4716 
4717   -- ****************************************************************************
4718   -- ****  Fetches from main query into variables
4719   -- ****
4720   -- ****************************************************************************
4721           FETCH  c_modelo_340
4722           INTO   lc_tax_registration_number
4723                 ,lc_customer_name
4724                 ,lc_key_id
4725 		,lc_foreign_taxpayer_id
4726 		,lc_book_type
4727 		,lc_transaction_code
4728 		,ld_invoice_date
4729 		,ld_trx_date
4730 		,lc_trx_num
4731 		,lc_doc_seq
4732 		,lc_intra_type
4733 		,lc_key_declared
4734 	        ,lc_trx_deadline
4735 		,lc_desc_of_goods
4736 		,ln_taxable_amt
4737 		,ln_tax_amt
4738 		,ln_inv_total_amt
4739 		,ln_deductable_amt
4740 		,ln_tax_rate
4741                 ,lc_customer_address_id
4742                 ;
4743 
4744            EXIT WHEN c_modelo_340%NOTFOUND;
4745 
4746              G_LINENO := '2.5.5.2';
4747 
4748                get_vendor_address ( p_party_site_id  => lc_customer_address_id
4749                                   , x_postal_code    => lc_postal_code
4750                                   , x_city           => lc_city
4751                                   , x_address_detail => lc_address_detail
4752                                   , x_country        => lc_country
4753                                   );
4754 
4755 
4756                lc_tax_registration_number := SUBSTR( lc_tax_registration_number,1,9);
4757                lc_address_detail          := SUBSTR(lc_address_detail,1,40);
4758 
4759                 G_LINENO := '2.5.5.3';
4760 
4761                IF G_DEBUG THEN
4762                  fnd_file.put_line(FND_FILE.LOG,'lc_trx_num='||lc_trx_num);
4763                  fnd_file.put_line(FND_FILE.LOG,'lc_address_detail='||lc_address_detail);
4764                  fnd_file.put_line(FND_FILE.LOG,'lc_postal_code='||lc_postal_code);
4765                  fnd_file.put_line(FND_FILE.LOG,'lc_city='||lc_city);
4766                END IF;
4767 
4768                 G_LINENO := '2.5.5.4';
4769 
4770             -- Insert payables record
4771             INSERT INTO JG_ZZ_VAT_TRX_GT
4772             ( jg_info_v1
4773             , jg_info_v20   -- p_tax_year
4774             , jg_info_v2    -- lc_taxpayer_id
4775             , jg_info_v3    -- lc_company_name
4776             , jg_info_v4    -- c_modelo_340.lc_tax_registration_number
4777             , jg_info_v5    -- c_modelo_340.lc_customer_name
4778             , jg_info_v6    -- lc_country
4779             , jg_info_v7    -- c_modelo_340.lc_key_id
4780             , jg_info_v8    -- c_modelo_340.lc_foreign_taxpayer_id
4781             , jg_info_v9    -- c_modelo_340.lc_book_type
4782             , jg_info_v10   -- c_modelo_340.lc_transaction_code
4783             , jg_info_d1    -- c_modelo_340.ld_invoice_date
4784             , jg_info_d2    -- c_modelo_340.ld_trx_date
4785             , jg_info_n1    -- c_modelo_340.ln_tax_rate
4786             , jg_info_n2    -- c_modelo_340.ln_taxable_amt
4787             , jg_info_n3    -- c_modelo_340.ln_tax_amt
4788             , jg_info_n4    -- c_modelo_340.ln_inv_total_amt
4789             , jg_info_v11   -- c_modelo_340.lc_trx_num
4790             , jg_info_v12   -- c_modelo_340.lc_doc_seq
4791             , jg_info_n7    -- c_modelo_340.ln_deductable_amt
4792             , jg_info_v13   -- c_modelo_340.c_intra_type
4793             , jg_info_v14   -- c_modelo_340.lc_key_declared
4794             , jg_info_v15   -- lc_country
4795             , jg_info_v16   -- lc_trx_deadline
4796             , jg_info_v17   -- c_modelo_340.lc_desc_of_goods
4797             , jg_info_v21   -- lc_address_detail
4798             , jg_info_v22   -- lc_city
4799             , jg_info_v23   -- lc_postal_code
4800             )
4801           VALUES
4802             ( '340'
4803             , p_tax_year
4804             , lc_taxpayer_id
4805             , lc_company_name
4806             , lc_tax_registration_number
4807             , lc_customer_name
4808             , lc_country
4809             , lc_key_id
4810             , lc_foreign_taxpayer_id
4811             , lc_book_type
4812             , lc_transaction_code
4813             , ld_invoice_date
4814             , ld_trx_date
4815             , ln_tax_rate
4816             , ln_taxable_amt
4817             , ln_tax_amt
4818             , ln_inv_total_amt
4819             , lc_trx_num
4820             , lc_doc_seq
4821             , ln_deductable_amt
4822             , lc_intra_type
4823             , lc_key_declared
4824             , lc_country
4825             , lc_trx_deadline
4826             , lc_desc_of_goods
4827             , lc_address_detail
4828             , lc_city
4829             , lc_postal_code
4830             );
4831 
4832         END LOOP;
4833 
4834                 G_LINENO := '2.5.5.4';
4835 
4836         IF c_modelo_340%ISOPEN THEN
4837           CLOSE c_modelo_340;
4838         END IF;
4839 
4840    IF G_DEBUG THEN
4841      fnd_file.put_line(FND_FILE.LOG,'Modelo 340 AP Processing Complete');
4842      fnd_file.put_line(FND_FILE.LOG,'Modelo 340 AR Processing ...');
4843    END IF;
4844 
4845    lc_jgzz_mod_query_340 :=
4846    'SELECT  decode(substrb(jzvtd.billing_tp_tax_reg_num,1,2),''ES'',
4847                   jzvtd.billing_tp_taxpayer_id)                                      DECLARED_NIF,
4848             substrb(jzvtd.billing_tp_name,1,40)                                      DECLARED_NAME,
4849             je_es_modelo_ext_pkg.getKeyID(substrb(jzvtd.billing_tp_tax_reg_num,1,2)) KEY_ID,   --BUG 8946271
4850             decode(substrb(jzvtd.billing_tp_tax_reg_num,1,2),''ES'',null,
4851                    substrb(jzvtd.billing_tp_taxpayer_id,1,17))                       FISCAL_ID,
4852             decode(substr(zxdf.document_sub_type,1,6), ''MOD340'', substr(zxdf.document_sub_type,8,1),
4853             decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4854                 ,''347'',''E'',''415'',''F'',''349'',''E''))                         BOOK_TYPE,
4855             decode( instr(zxdf.user_defined_fisc_class,''NONE'',7,1),7,'' '', substr(zxdf.user_defined_fisc_class,7,1) ) TRANSACTION_CODE,
4856             jzvtd.trx_date                                                           ISSUE_DATE,
4857             to_date(trx.global_attribute13,''YYYY/MM/DD HH24:MI:SS'')                TRANSACTION_DATE,
4858             jzvtd.trx_number                                                         INVOICE_IDENT,
4859             jzvtd.doc_seq_value                                                      REGISTER_NUMBER,
4860             decode(substr(zxdf.document_sub_type,1,8),''MOD340/U'',
4861                    substr(zxdf.document_sub_type,9,1))                               INTRA_TYPE,
4862             decode(substr(zxdf.document_sub_type,1,8), ''MOD340/U'',''R''
4863               ,decode(substr(zxdf.trx_business_category,
4864                 (instr(zxdf.trx_business_category,''MOD'',1,1)+3),3),''349'',''R''))      KEY_OF_DECLARED,
4865             trx.global_attribute10                                                   TRANSACTION_DEADLINE,
4866             decode(substr(zxdf.document_sub_type,1,8)
4867                    ,''MOD340/U'',substrb(jzvtd.TRX_LINE_DESCRIPTION,1,35)
4868                    ,decode(substr(jzvtd.invoice_report_type,1,3),
4869                           ''349'',substrb(jzvtd.TRX_LINE_DESCRIPTION,1,35)))         DESC_OF_GOODS,
4870             jzvtd.trx_line_number,
4871             jzvtd.trx_line_id,
4872             jzvtd.trx_id,
4873             jzvtd.reporting_status_id,
4874             DECODE(NVL(jzvtd.taxable_amt_funcl_curr , 0)
4875                           , 0 , jzvtd.taxable_amt
4876                               , jzvtd.taxable_amt_funcl_curr)                      TAXABLE_AMOUNT,
4877             hzcsu.cust_acct_site_id
4878             $YEAR_AMT_RECEIVED_CASH$
4879             $TRANSMISSION_PROPERTY$
4880     FROM    jg_zz_vat_trx_details jzvtd, jg_zz_vat_rep_status  jzvrs
4881             , hz_cust_site_uses_all  hzcsu ,hz_cust_acct_sites_all hzcas ,hz_cust_accounts hzca
4882             , ZX_LINES_DET_FACTORS zxdf, ra_customer_trx_all trx
4883     WHERE   (substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3) in (''347'',''349'',''415'')
4884             OR substr(zxdf.document_sub_type,1,6) = ''MOD340'')
4885     AND       nvl(zxdf.document_sub_type,''X'') <> ''MOD340_EXCL''
4886     AND     JZVRS.vat_reporting_entity_id  = $P_VAT_REP_ENTITY_ID$
4887     AND     JZVTD.reporting_status_id in (SELECT DISTINCT JZRS.reporting_status_id JZRS
4888     			                  FROM  jg_zz_vat_rep_status JZRS
4889      				          WHERE JZRS.vat_reporting_entity_id = $P_VAT_REP_ENTITY_ID$
4890                                           AND   JZRS.source = ''AR'' )
4891     AND     $FILTER_KEY$ $FILTER_OPER$ $FILTER_VALUE$
4892             $340_PERIOD_KEY$ $340_PERIOD_KEY_FROM$ $340_PERIOD_OPER$ $340_PERIOD_KEY_TO$
4893     AND     JZVTD.extract_source_ledger =  ''AR''
4894     AND     JZVRS.source = ''AR''
4895     AND     JZVTD.trx_line_class <> ''DEBIT''
4896     AND     SUBSTR(NVL(JZVTD.tax_rate_vat_trx_type_code,''QQQQQQ''),1,3) <> ''RET''
4897     AND     JZVTD.BILLING_TRADING_PARTNER_ID  =   hzca.cust_account_id
4898     AND     hzca.cust_account_id  = hzcas.cust_account_id
4899     AND     hzcsu.cust_acct_site_id  = hzcas.cust_acct_site_id
4900     AND     upper(hzcsu.site_use_code) = ''LEGAL''
4901     AND     hzcsu.primary_flag = ''Y''
4902     AND     hzcsu.status = ''A''
4903     AND     hzcsu.ORG_ID = $P_ORG_ID$
4904     AND     jzvtd.trx_id = zxdf.trx_id
4905     AND     jzvtd.trx_line_id = zxdf.trx_line_id
4906     AND     jzvtd.trx_id = trx.customer_trx_id
4907     GROUP BY decode(substrb(jzvtd.billing_tp_tax_reg_num,1,2),''ES'',
4908                   jzvtd.billing_tp_taxpayer_id),
4909             substrb(jzvtd.billing_tp_name,1,40),
4910             je_es_modelo_ext_pkg.getKeyID(substrb(jzvtd.billing_tp_tax_reg_num,1,2)),
4911             decode(substrb(jzvtd.billing_tp_tax_reg_num,1,2),''ES'',null,
4912                    substrb(jzvtd.billing_tp_taxpayer_id,1,17)),
4913             decode(substr(zxdf.document_sub_type,1,6), ''MOD340'', substr(zxdf.document_sub_type,8,1),
4914             decode(substr(zxdf.trx_business_category,(instr(zxdf.trx_business_category,''MOD'',1,1)+3),3)
4915                 ,''347'',''E'',''415'',''F'',''349'',''E''))  ,
4916             decode( instr(zxdf.user_defined_fisc_class,''NONE'',7,1),7,'' '', substr(zxdf.user_defined_fisc_class,7,1) ),
4917             jzvtd.trx_date,
4918             to_date(trx.global_attribute13,''YYYY/MM/DD HH24:MI:SS''),
4919             jzvtd.trx_number,
4920             jzvtd.doc_seq_value,
4921             decode(substr(zxdf.document_sub_type,1,8),''MOD340/U'',
4922                    substr(zxdf.document_sub_type,9,1)),
4923             decode(substr(zxdf.document_sub_type,1,8), ''MOD340/U'',''R''
4924               ,decode(substr(zxdf.trx_business_category,
4925                 (instr(zxdf.trx_business_category,''MOD'',1,1)+3),3),''349'',''R'')),
4926             trx.global_attribute10,
4927             decode(substr(zxdf.document_sub_type,1,8)
4928                    ,''MOD340/U'',substrb(jzvtd.TRX_LINE_DESCRIPTION,1,35)
4929                    ,decode(substr(jzvtd.invoice_report_type,1,3),
4930                           ''349'',substrb(jzvtd.TRX_LINE_DESCRIPTION,1,35))),
4931             jzvtd.trx_line_number,
4932             jzvtd.trx_line_id,
4933             jzvtd.trx_id,
4934             jzvtd.reporting_status_id,
4935             DECODE(NVL(jzvtd.taxable_amt_funcl_curr , 0)
4936                            , 0 , jzvtd.taxable_amt
4937                                , jzvtd.taxable_amt_funcl_curr),
4938             hzcsu.cust_acct_site_id
4939             $YEAR_AMT_RECEIVED_CASH_GRP$ ';
4940 
4941   -- TAX LINE AMOUNTS QUERY
4942   lc_jgzz_mod_query_340_tax :=
4943   'SELECT  sum(nvl(DECODE(tax_amt_funcl_curr,
4944                           0, tax_amt ,
4945                           NULL, tax_amt,
4946                           tax_amt_funcl_curr), 0))    TAX_AMOUNT,
4947            tax_rate
4948    FROM    jg_zz_vat_trx_details
4949    WHERE   trx_line_id = :p_trx_line_id
4950    AND     trx_id = :p_trx_id
4951    AND     extract_source_ledger = ''AR''
4952    AND     reporting_status_id = :p_reporting_status_id
4953    AND     SUBSTR(NVL(tax_rate_vat_trx_type_code,''QQQQQQ''),1,3) <> ''RET''
4954    GROUP BY tax_rate
4955    ORDER BY tax_rate desc ';
4956 
4957           G_LINENO := '2.5.6';
4958 
4959         -- Date processing for query
4960         declare
4961           lv_filter_key varchar2 (150) ;
4962           lv_filter_value varchar2 (2000);
4963           lv_filter_oper varchar2(2);
4964 
4965         begin
4966 
4967           -- For Period report restricts by start / end dates AND by PERIOD
4968           if p_report_name = 'JEESPMOR' then
4969 
4970             lv_filter_key := ' JZVTD.trx_date BETWEEN JZVRS.period_start_date and JZVRS.period_end_date
4971 		       AND JZVRS.TAX_CALENDAR_PERIOD'; --5444803
4972           if p_driving_date = 'GL' then
4973 
4974             lv_filter_key :=
4975             REPLACE( lv_filter_key
4976                     ,'trx_date'
4977                     ,'gl_date'
4978                     );
4979             end if;
4980             lv_filter_value := l_period_string;
4981             lv_filter_oper := 'In';
4982 
4983             lc_jgzz_mod_query_340 :=
4984                REPLACE( lc_jgzz_mod_query_340
4985                       , '$340_PERIOD_KEY$'
4986                       , ' ' );
4987 
4988             lc_jgzz_mod_query_340 :=
4989                REPLACE( lc_jgzz_mod_query_340
4990                       , '$340_PERIOD_KEY_FROM$'
4991                       , ' ' );
4992 
4993             lc_jgzz_mod_query_340 :=
4994                REPLACE( lc_jgzz_mod_query_340
4995                       , '$340_PERIOD_OPER$'
4996                       , ' ' );
4997 
4998             lc_jgzz_mod_query_340 :=
4999                REPLACE( lc_jgzz_mod_query_340
5000                       , '$340_PERIOD_KEY_TO$'
5001                       , ' ' );
5002 
5003             lc_jgzz_mod_query_340 :=
5004              REPLACE( lc_jgzz_mod_query_340
5005                     , '$TRANSMISSION_PROPERTY$'
5006                     , ', NULL TRANSMISSION_PROPERTY');
5007 
5008             lc_jgzz_mod_query_340 :=
5009              REPLACE( lc_jgzz_mod_query_340
5010                     , '$YEAR_AMT_RECEIVED_CASH$'
5011                     , ', NULL YEAR_AMT_RECEIVED_CASH');
5012 
5013 
5014             lc_jgzz_mod_query_340 :=
5015                REPLACE( lc_jgzz_mod_query_340
5016                       , '$YEAR_AMT_RECEIVED_CASH_GRP$'
5017                       , ' ' );
5018 
5019           -- For Annual report restricts by start / end dates AND by TAX CALENDAR YEAR
5020           elsif p_report_name = 'JEESAMOR' then
5021 
5022             -- Original modelo processing
5023             lv_filter_key := 'JZVTD.trx_date BETWEEN JZVRS.period_start_date and JZVRS.period_end_date
5024 		       AND JZVRS.TAX_CALENDAR_YEAR';  --Bug 5525421
5025             if p_driving_date = 'GL' then
5026 
5027             lv_filter_key :=
5028             REPLACE( lv_filter_key
5029                     ,'trx_date'
5030                     ,'gl_date'
5031                     );
5032             end if;
5033             lv_filter_value := p_tax_year;
5034             lv_filter_oper := '=';
5035 
5036             -- Set start and end dates, specific to 340
5037             set_dates;
5038 
5039            if p_driving_date = 'GL' then
5040              lc_jgzz_mod_query_340 :=
5041                REPLACE( lc_jgzz_mod_query_340
5042                       , '$340_PERIOD_KEY$'
5043                       , 'AND JZVTD.gl_date BETWEEN  ' );
5044 
5045             else --if p_driving_date ='TRX'
5046               lc_jgzz_mod_query_340 :=
5047 	                   REPLACE( lc_jgzz_mod_query_340
5048 	                          , '$340_PERIOD_KEY$'
5049                       , 'AND JZVTD.trx_date BETWEEN  ' );
5050              end if;
5051 
5052             lc_jgzz_mod_query_340 :=
5053                REPLACE( lc_jgzz_mod_query_340
5054                       , '$340_PERIOD_KEY_FROM$'
5055                       ,'''' || p_340_start_date ||'''');
5056                       -- , p_340_start_date );
5057 
5058             lc_jgzz_mod_query_340 :=
5059                REPLACE( lc_jgzz_mod_query_340
5060                       , '$340_PERIOD_OPER$'
5061                       , 'AND' );
5062 
5063             lc_jgzz_mod_query_340 :=
5064                REPLACE( lc_jgzz_mod_query_340
5065                       , '$340_PERIOD_KEY_TO$'
5066                       ,'''' || p_340_end_date ||'''');
5067 
5068             lc_jgzz_mod_query_340 :=
5069                REPLACE( lc_jgzz_mod_query_340
5070                       , '$YEAR_AMT_RECEIVED_CASH$'
5071                       , ', trx.global_attribute15 year_amt_received_cash' );
5072 
5073             lc_jgzz_mod_query_340 :=
5074                REPLACE( lc_jgzz_mod_query_340
5075                       , '$YEAR_AMT_RECEIVED_CASH_GRP$'
5076                       , ', trx.global_attribute15' );
5077 
5078              lc_jgzz_mod_query_340 :=
5079                REPLACE( lc_jgzz_mod_query_340
5080                       , '$TRANSMISSION_PROPERTY$'
5081                          , ', SUM(DECODE(NVL(trx.global_attribute12, ''N''),''Y'', DECODE (JZVTD.TAX_LINE_NUMBER, ''1'', NVL(JZVTD.taxable_amt_funcl_curr, 0),0),0)) +
5082                              SUM(DECODE(NVL(trx.global_attribute12, ''N''),''Y'', NVL(JZVTD.tax_amt_funcl_curr, 0),0)) TRANSMISSION_PROPERTY');
5083 
5084 
5085           end if;
5086 
5087         lc_jgzz_mod_query_340 :=
5088                REPLACE( lc_jgzz_mod_query_340
5089                       , '$FILTER_KEY$'
5090                       , lv_filter_key
5091                       );
5092 
5093         lc_jgzz_mod_query_340 :=
5094                REPLACE( lc_jgzz_mod_query_340
5095                       , '$FILTER_OPER$'
5096                       , lv_filter_oper
5097                       );
5098 
5099          lc_jgzz_mod_query_340 :=
5100                REPLACE( lc_jgzz_mod_query_340
5101                       , '$FILTER_VALUE$'
5102                       , lv_filter_value
5103                       );
5104 
5105         end;
5106 
5107          lc_jgzz_mod_query_340 :=
5108                REPLACE( lc_jgzz_mod_query_340
5109                       , '$P_VAT_REP_ENTITY_ID$'
5110                       , TO_CHAR(P_VAT_REP_ENTITY_ID)
5111                       );
5112 
5113          lc_jgzz_mod_query_340 :=
5114                REPLACE( lc_jgzz_mod_query_340
5115                       , '$P_ORG_ID$'
5116                       , P_ORG_ID );
5117 
5118 
5119           G_LINENO := '2.5.6.1';
5120 IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'2.5.6.1 Open the AR Dynamic Query for 340: '||lc_jgzz_mod_query_340 ); END IF;
5121        OPEN c_modelo_340 FOR lc_jgzz_mod_query_340 ;  -- opencur
5122        LOOP
5123 
5124           IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'2.5.5. Fetch from 340 AR Query'); END IF;
5125 
5126           G_LINENO := '2.5.6.2';
5127 
5128           FETCH  c_modelo_340
5129           INTO   lc_tax_registration_number
5130                 ,lc_customer_name
5131                 ,lc_key_id
5132 		,lc_foreign_taxpayer_id
5133 		,lc_book_type
5134 		,lc_transaction_code
5135 		,ld_invoice_date
5136 		,ld_trx_date
5137 		,lc_trx_num
5138 		,lc_doc_seq
5139 		,lc_intra_type
5140 		,lc_key_declared
5141 	       ,lc_trx_deadline
5142 		 ,lc_desc_of_goods
5143              ,ln_trx_line_number
5144              ,ln_trx_line_id
5145              ,ln_trx_id
5146              ,ln_reporting_status_id
5147 		 ,ln_taxable_amt
5148              ,lc_customer_address_id
5149              ,ln_year_of_amts_received_cash
5150              ,ln_transmission_property_amt
5151                 ;
5152 
5153            EXIT WHEN c_modelo_340%NOTFOUND;
5154 
5155            G_LINENO := '2.5.6.3';
5156 
5157                 get_customer_address2
5158                     ( p_customer_address_id  =>  lc_customer_address_id
5159                     , x_postal               =>  lc_postal_code
5160 		        , x_post_code            =>  lc_postal_code1
5161                     , x_city                 =>  lc_city
5162                     , x_street_type          =>  lc_street_type -- sigla
5163                     , x_street               =>  lc_street      --via_publica
5164                     , x_number               =>  lc_number
5165                     , x_country              =>  lc_country     -- new for 340
5166 
5167                     );
5168 
5169                  get_ARMOD340_PROP_LOC
5170                    (  p_trx_id                 =>  ln_trx_id
5171                     , x_property_location_code => ln_property_location_code
5172                     , x_ref_catastral          => lc_ref_catastral
5173                     );
5174 
5175                lc_tax_registration_number := SUBSTR( lc_tax_registration_number,1,9);
5176                lc_address_detail          := SUBSTR(lc_street_type || lc_number || lc_street,1,40);
5177 
5178                -- reset variable to null
5179                l_ref_catastral := null;
5180 
5181 
5182                IF lc_transaction_code = 'R' then
5183                   l_ref_catastral := lc_ref_catastral;
5184                END IF;
5185 
5186                 G_LINENO := '2.5.6.4';
5187 
5188                IF G_DEBUG THEN
5189                  fnd_file.put_line(FND_FILE.LOG,'lc_trx_num='||lc_trx_num);
5190                  fnd_file.put_line(FND_FILE.LOG,'ln_trx_id='||ln_trx_id);
5191                  fnd_file.put_line(FND_FILE.LOG,'lc_address_detail='||lc_address_detail);
5192                  fnd_file.put_line(FND_FILE.LOG,'lc_street_type='||lc_street_type);
5193                  fnd_file.put_line(FND_FILE.LOG,'lc_postal_code='||lc_postal_code);
5194                  fnd_file.put_line(FND_FILE.LOG,'lc_city='||lc_city);
5195                  fnd_file.put_line(FND_FILE.LOG,'lc_country='||lc_country);
5196                  fnd_file.put_line(FND_FILE.LOG,'ln_property_location_code='||ln_property_location_code);
5197                  fnd_file.put_line(FND_FILE.LOG,'lc_ref_catastral='||lc_ref_catastral);
5198                END IF;
5199 
5200 
5201               ln_cash_received_amount := 0;
5202 
5203               ln_cash_received_amount := AR340_CASH_RECEIVEDFormula ( p_declared_nif           => lc_tax_registration_number
5204                                                                      ,p_transaction_code       => lc_transaction_code
5205                                                                      , p_trx_id                => ln_trx_id
5206                                                                      , p_year_of_amts_rec_cash => ln_year_of_amts_received_cash);
5207 
5208                 G_LINENO := '2.5.6.5';
5209 
5210                 -- AR Tax calculation. This processes any sucharge amounts.
5211 
5212                 -- Reset variables impacted by tax calculations
5213                 ln_line_count := 0;
5214                 ln_tax_amt := 0;
5215                 ln_tax_rate := 0;
5216                 -- Set the inv total to taxable, then add tax during tax processing
5217                 ln_inv_total_amt  := ln_taxable_amt;
5218                 ln_surcharge_amount := 0;
5219                 ln_surcharge_rate := 0;
5220 
5221                 OPEN c_modelo_340_artax FOR lc_jgzz_mod_query_340_tax USING
5222                   ln_trx_line_id,
5223                   ln_trx_id,
5224                   ln_reporting_status_id;
5225 
5226                  LOOP
5227                    FETCH c_modelo_340_artax INTO ln_ar_tax_amt, ln_ar_tax_rate;
5228                    EXIT WHEN c_modelo_340_artax%NOTFOUND;
5229 
5230                    ln_line_count := ln_line_count + 1;
5231 
5232                    IF ln_line_count = 1 THEN -- First row / greater amount is tax
5233                      ln_tax_amt := ln_ar_tax_amt;
5234                      ln_inv_total_amt := ln_inv_total_amt + ln_ar_tax_amt;
5235                      ln_tax_rate   := ln_ar_tax_rate;
5236                   ELSE -- Second tax is surcharge
5237                      ln_surcharge_amount := ln_ar_tax_amt;
5238                      ln_surcharge_rate := ln_ar_tax_rate;
5239                      ln_inv_total_amt := ln_inv_total_amt + ln_ar_tax_amt;
5240                   END IF;
5241 
5242                 END LOOP;
5243 
5244                G_LINENO := '2.5.6.6';
5245 
5246                IF G_DEBUG THEN
5247                 fnd_file.put_line(FND_FILE.LOG, 'Processed transaction: '||lc_trx_num);
5248                 fnd_file.put_line(FND_FILE.LOG, 'Tax Amount: '||ln_tax_amt);
5249                 fnd_file.put_line(FND_FILE.LOG, 'Tax rate: '||ln_tax_rate);
5250                 fnd_file.put_line(FND_FILE.LOG, 'Surcharge Amount: '||ln_surcharge_amount);
5251                 fnd_file.put_line(FND_FILE.LOG, 'Surcharge Rate: '||ln_surcharge_rate);
5252                 fnd_file.put_line(FND_FILE.LOG, 'Inv Amount: '||ln_inv_total_amt);
5253                END IF;
5254 
5255             -- Insert AR record
5256             INSERT INTO JG_ZZ_VAT_TRX_GT
5257             ( jg_info_v1
5258             , jg_info_v20   -- p_tax_year
5259             , jg_info_v2    -- lc_taxpayer_id
5260             , jg_info_v3    -- lc_company_name
5261             , jg_info_v4    -- c_modelo_340.lc_tax_registration_number
5262             , jg_info_v5    -- c_modelo_340.lc_customer_name
5263             , jg_info_v6    -- lc_country
5264             , jg_info_v7    -- c_modelo_340.lc_key_id
5265             , jg_info_v8    -- c_modelo_340.lc_foreign_taxpayer_id
5266             , jg_info_v9    -- c_modelo_340.lc_book_type
5267             , jg_info_v10   -- c_modelo_340.lc_transaction_code
5268             , jg_info_d1    -- c_modelo_340.ld_invoice_date
5269             , jg_info_d2    -- c_modelo_340.ld_trx_date
5270             , jg_info_n1    -- c_modelo_340.ln_tax_rate
5271             , jg_info_n2    -- c_modelo_340.ln_taxable_amt
5272             , jg_info_n3    -- c_modelo_340.ln_tax_amt
5273             , jg_info_n4    -- ln_inv_total_amt
5274             , jg_info_v11   -- c_modelo_340.lc_trx_num
5275             , jg_info_v12   -- c_modelo_340.lc_doc_seq
5276             , jg_info_n8    -- ln_surcharge_rate
5277             , jg_info_n9    -- ln_surcharge_amount
5278             , jg_info_v13   -- c_modelo_340.c_intra_type
5279             , jg_info_v14   -- c_modelo_340.lc_key_declared
5280             , jg_info_v15   -- lc_country
5281             , jg_info_v16   -- lc_trx_deadline
5282             , jg_info_v17   -- c_modelo_340.lc_desc_of_goods
5283             , jg_info_v21   -- lc_address_detail
5284             , jg_info_v22   -- lc_city
5285             , jg_info_v23   -- lc_postal_code
5286             , jg_info_v39     -- ln_property_location_code
5287             , jg_info_v19     -- l_ref_catastral
5288             , jg_info_n6      --ln_cash_ received_amount
5289             , jg_info_n28     --c_modelo_340.ln_year_of_amts_received_cash
5290             , jg_info_n5      --c_modelo-340.ln_transmission_property_amt
5291             )
5292           VALUES
5293             ( '340'
5294             , p_tax_year
5295             , lc_taxpayer_id
5296             , lc_company_name
5297             , lc_tax_registration_number
5298             , lc_customer_name
5299             , lc_country
5300             , lc_key_id
5301             , lc_foreign_taxpayer_id
5302             , lc_book_type
5303             , lc_transaction_code
5304             , ld_invoice_date
5305             , ld_trx_date
5306             , ln_tax_rate
5307             , ln_taxable_amt
5308             , ln_tax_amt
5309             , ln_inv_total_amt
5310             , lc_trx_num
5311             , lc_doc_seq
5312             , ln_surcharge_rate
5313             , ln_surcharge_amount
5314             , lc_intra_type
5315             , lc_key_declared
5316             , lc_country
5317             , lc_trx_deadline
5318             , lc_desc_of_goods
5319             , lc_address_detail
5320             , lc_city
5321             , lc_postal_code
5322             , ln_property_location_code
5323             , l_ref_catastral
5324             , ln_cash_received_amount
5325             , ln_year_of_amts_received_cash
5326             , ln_transmission_property_amt
5327             );
5328 
5329         END LOOP;
5330 
5331         G_LINENO := '2.5.6.7';
5332 
5333         IF c_modelo_340%ISOPEN THEN
5334           CLOSE c_modelo_340;
5335         END IF;
5336 
5337         IF c_modelo_340_artax%ISOPEN THEN
5338           CLOSE c_modelo_340_artax;
5339         END IF;
5340 
5341    -- Calculate overall amounts
5342    SELECT  sum(jg_info_n2) TAXABLE_AMT,
5343            sum(jg_info_n3) TAX_AMT,
5344            sum(jg_info_n4) INV_TOTAL_AMT
5345    INTO    ln_taxable_amt,
5346            ln_tax_amt,
5347            ln_inv_total_amt
5348    FROM    JG_ZZ_VAT_TRX_GT
5349    WHERE   nvl(jg_info_v30,'X') <> 'H';
5350 
5351    UPDATE  JG_ZZ_VAT_TRX_GT
5352    SET     jg_info_n2 = ln_taxable_amt,
5353            jg_info_n3 = ln_tax_amt,
5354            jg_info_n4 = ln_inv_total_amt
5355    WHERE   jg_info_v30 = 'H';
5356 
5357 END IF;
5358 
5359     IF P_MODELO = '340' THEN
5360 
5361       SELECT SUM(count_group) INTO P_REC_COUNT FROM (
5362         SELECT 1 count_group
5363         FROM   JG_ZZ_VAT_TRX_GT
5364         WHERE  NVL(jg_info_v30,'X') <> 'H'
5365         GROUP BY jg_info_v11, jg_info_n1, jg_info_n8);
5366 
5367 
5368 
5369       -- Apply minimum cash amount received parameter to the selected transactions
5370 	-- Note: Only transactions in AR are subject to this minimum
5371 
5372           MOD340_MIN_CASH_AMOUNT('A');
5373           MOD340_MIN_CASH_AMOUNT('B');
5374           MOD340_MIN_CASH_AMOUNT('C');
5375           MOD340_MIN_CASH_AMOUNT('D');
5376           MOD340_MIN_CASH_AMOUNT('E');
5377           MOD340_MIN_CASH_AMOUNT('F');
5378           MOD340_MIN_CASH_AMOUNT('G');
5379           MOD340_MIN_CASH_AMOUNT('H');
5380           MOD340_MIN_CASH_AMOUNT('I');
5381           MOD340_MIN_CASH_AMOUNT('J');
5382           MOD340_MIN_CASH_AMOUNT('K');
5383           MOD340_MIN_CASH_AMOUNT('L');
5384           MOD340_MIN_CASH_AMOUNT('M');
5385           MOD340_MIN_CASH_AMOUNT('N');
5386           MOD340_MIN_CASH_AMOUNT('O');
5387           MOD340_MIN_CASH_AMOUNT('P');
5388           MOD340_MIN_CASH_AMOUNT('Q');
5389           MOD340_MIN_CASH_AMOUNT('R');
5390           MOD340_MIN_CASH_AMOUNT('S');
5391           MOD340_MIN_CASH_AMOUNT('T');
5392           MOD340_MIN_CASH_AMOUNT('U');
5393           MOD340_MIN_CASH_AMOUNT('V');
5394           MOD340_MIN_CASH_AMOUNT('W');
5395           MOD340_MIN_CASH_AMOUNT('X');
5396 
5397 
5398     ELSE
5399 
5400       SELECT COUNT(*)
5401       INTO   P_REC_COUNT
5402       FROM   JG_ZZ_VAT_TRX_GT
5403       WHERE  NVL(jg_info_v30,'X') <> 'H';
5404 
5405       /* Bug 8485057 apply minimum amounts to the selected transactions */
5406        IF P_MODELO = '347'
5407       THEN
5408         IF G_DEBUG THEN
5409           fnd_file.put_line(FND_FILE.LOG,'Count of detail records in JG_ZZ_VAT_TRX_GT, before applying minimum amount='||P_REC_COUNT);
5410         END IF;
5411 
5412         -- Apply minimum amount parameter to the selected transactions
5413         -- Note: Only transactions tipo 'A' and 'B' are subject to this minimum
5414         MOD347_MIN_AMOUNT ('A');
5415         MOD347_MIN_AMOUNT ('B');
5416 
5417         -- Recount the number of transactions after applying the minimum amount
5418         SELECT COUNT(*)
5419         INTO   P_REC_COUNT
5420         FROM   JG_ZZ_VAT_TRX_GT
5421         WHERE  NVL(jg_info_v30,'X') <> 'H'
5422         AND    NVL(jg_info_n4,0) <> 1;
5423 
5424         -- Apply minimum cash amount received parameter to the selected transactions
5425         -- Note: Only transactions in AR are subject to this minimum
5426         -- Typically tipo B and F, not valid for tipo A and G
5427         MOD347_MIN_CASH_AMOUNT('B');
5428         MOD347_MIN_CASH_AMOUNT('F');
5429 
5430       END IF; -- 347
5431     END IF; -- 340
5432 
5433     UPDATE JG_ZZ_VAT_TRX_GT SET JG_INFO_N30= P_REC_COUNT WHERE jg_info_v30='H';
5434 
5435     G_LINENO := '2.7';
5436     IF G_DEBUG THEN fnd_file.put_line(FND_FILE.LOG,'Count of detail records in JG_ZZ_VAT_TRX_GT='||P_REC_COUNT); END IF;
5437     IF P_REPORT_NAME  = 'JEESAMOR' THEN
5438        G_LINENO := '2.7.1';
5439 
5440        IF P_MODELO = '347' THEN
5441           G_LINENO := '2.7.1.1';
5442 
5443          UPDATE JG_ZZ_VAT_TRX_GT
5444          SET    jg_info_n11 =  ( SELECT count(*)
5445                                  FROM   JG_ZZ_VAT_TRX_GT
5446                                  WHERE  jg_info_v1  <> '3'
5447                                  AND    NVL(jg_info_n4,0) <> 1
5448                                  AND    nvl(jg_info_v30,'X') <> 'H' )  -- CP_TOTAL_DEC_D
5449               , jg_info_n12 =  ( SELECT count(*)
5450                                  FROM   JG_ZZ_VAT_TRX_GT
5451                                  WHERE  jg_info_v1  =  '3'
5452                                  AND    NVL(jg_info_n4,0) <> 1
5453                                  AND    nvl(jg_info_v30,'X') <> 'H' )  -- CP_TOTAL_DEC_I
5454               , jg_info_n13 =    ( Select Decode(G_Currency_Code,'EUR',
5455                                          (Round(Sum(Nvl(Jg_Info_N2,Jg_Info_N1)),G_Cur_Precision)*100)
5456                                          ,round(SUM(nvl(jg_info_n2,jg_info_n1)),G_Cur_Precision))                  ----rounded by ratnadeep for bug:16169794
5457                                  FROM   JG_ZZ_VAT_TRX_GT
5458                                  WHERE  jg_info_v1 <> '3'
5459                                  AND    NVL(jg_info_n4,0) <> 1)    -- CP_TOTAL_AMT_DEC_D
5460               , jg_info_n14 =  ( SELECT SUM(jg_info_n2)
5461                                  FROM   JG_ZZ_VAT_TRX_GT
5462                                  WHERE  jg_info_v1  =  '3'
5463                                  AND    NVL(jg_info_n4,0) <> 1
5464                                  AND    NVL(jg_info_v30,'X') <> 'H' )  -- CP_TOTAL_DEC_I
5465               , jg_info_n15 =  ( SELECT count(*)
5466                                  FROM   JG_ZZ_VAT_TRX_GT
5467                                  WHERE  nvl(jg_info_v30,'X') <> 'H'
5468                                  AND    NVL(jg_info_n4,0) <> 1)  -- CP_NO_OF_TYPE2
5469          WHERE  jg_info_v30 = 'H';
5470          G_LINENO := '2.7.1.2';
5471 
5472        ELSIF P_MODELO = '415' THEN      /** author:brathod; Modified for condition from P_MODELO='347' */
5473         G_LINENO := '2.7.1.3';
5474          UPDATE JG_ZZ_VAT_TRX_GT
5475          SET  ( jg_info_n11 -- CS_IMP_VENTAS
5476               , jg_info_n12 -- CS_NUMERO_VENTAS
5477               , jg_info_n13 -- CS_IMP_MEDIACION
5478               , jg_info_n14 -- CS_NUMERO_MEDIACION
5479               , jg_info_n15 -- CS_IMP_COMPRAS
5480               , jg_info_n16 -- CS_NUMERO_COMPRAS
5481  -- FH: Added for all transaction codes
5482               , jg_info_n17 -- CS_IMP_THIRD
5483               , jg_info_n18 -- CS_NUMERO_THIRD
5484               , jg_info_n19 -- CS_IMP_BIS
5485               , jg_info_n20 -- CS_NUMERO_BIS
5486               , jg_info_n21 -- CS_IMP_PUB
5487               , jg_info_n22 -- CS_NUMERO_PUB
5488               , jg_info_n23 -- CS_IMP_TAP
5489               , jg_info_n24 -- CS_NUMERO_TAP
5490               , jg_info_n25 -- CS_IMP_TAS
5491               , jg_info_n26 -- CS_NUMERO_TAS
5492               ) =
5493               ( SELECT SUM( DECODE( jg_info_v1
5494                                     , 'A', NVL(jg_info_n1, jg_info_n2)
5495                                     , 0 ) ) CP_IMP_VENTAS
5496                      , SUM( DECODE( jg_info_v1
5497                                     , 'A', 1
5498                                     , 0 ) ) CP_NUMERO_VENTAS
5499                      , SUM( DECODE( jg_info_v1
5500                                     , 'M', NVL(jg_info_n1, jg_info_n2)
5501                                     , 0 ) )   CP_IMP_MEDIACION
5502                      , SUM( DECODE( jg_info_v1
5503                                     , 'M', 1
5504                                     , 0 ) )   CP_NUMERO_MEDIACION
5505                      , SUM( DECODE( jg_info_v1
5506                                     , 'B', NVL(jg_info_n1, jg_info_n2)
5507                                     , 0 ) )   CP_IMP_COMPRAS
5508                      , SUM( DECODE( jg_info_v1
5509                                     , 'B', 1
5510                                     , 0 ) )  CF_NUMERO_COMPRAS
5511  -- FH: Added for all transaction codes
5512                      , SUM( DECODE( jg_info_v1
5513                                     , 'C', NVL(jg_info_n1, jg_info_n2)
5514                                     , 0 ) ) CP_IMP_THIRD
5515                      , SUM( DECODE( jg_info_v1
5516                                     , 'C', 1
5517                                     , 0 ) ) CP_NUMERO_THIRD
5518                      , SUM( DECODE( jg_info_v1
5519                                     , 'D', NVL(jg_info_n1, jg_info_n2)
5520                                     , 0 ) )   CP_IMP_BIS
5521                      , SUM( DECODE( jg_info_v1
5522                                     , 'D', 1
5523                                     , 0 ) )   CP_NUMERO_BIS
5524                      , SUM( DECODE( jg_info_v1
5525                                     , 'E', NVL(jg_info_n1, jg_info_n2)
5526                                     , 0 ) )   CP_IMP_PUB
5527                      , SUM( DECODE( jg_info_v1
5528                                     , 'E', 1
5529                                     , 0 ) )  CF_NUMERO_PUB
5530                      , SUM( DECODE( jg_info_v1
5531                                     , 'F', NVL(jg_info_n1, jg_info_n2)
5532                                     , 0 ) ) CP_IMP_TAS
5533                      , SUM( DECODE( jg_info_v1
5534                                     , 'F', 1
5535                                     , 0 ) ) CP_NUMERO_TAS
5536                      , SUM( DECODE( jg_info_v1
5537                                     , 'G', NVL(jg_info_n1, jg_info_n2)
5538                                     , 0 ) )   CP_IMP_TAP
5539                      , SUM( DECODE( jg_info_v1
5540                                     , 'G', 1
5541                                     , 0 ) )   CP_NUMERO_TAP
5542 
5543                 FROM   JG_ZZ_VAT_TRX_GT
5544                 WHERE  NVL(jg_info_v30,'X') <> 'H'
5545                )
5546          WHERE  jg_info_v30 = 'H';
5547           G_LINENO := '2.7.1.4';
5548        END IF;
5549        G_LINENO := '2.7.2';
5550     END IF;
5551     G_LINENO := '2.8';
5552     RETURN (TRUE);
5553   EXCEPTION
5554   WHEN OTHERS THEN
5555     fnd_file.put_line(FND_FILE.LOG,'Error while processing Before Report Trigger. Statement No = ' || G_LINENO||' Err:'|| SQLCODE || SUBSTR(SQLERRM,1,200));
5556     RETURN (FALSE);
5557   END BEFORE_REPORT;
5558 
5559   FUNCTION after_Report  RETURN BOOLEAN
5560   IS
5561   BEGIN
5562      NULL;
5563      RETURN (TRUE);
5564   END after_Report;
5565 
5566 END je_es_modelo_ext_pkg;