DBA Data[Home] [Help]

PACKAGE BODY: APPS.AR_UNPOSTED_ITEM_UTIL

Source


1 PACKAGE BODY ar_unposted_item_util AS
2 /* $Header: ARCBUPTB.pls 120.0 2006/07/25 22:00:41 hyu noship $ */
3 
4 
5 --
6 -- Declaration of local specs and variables
7 --
8 
9 TYPE CurrencyCodeType  IS TABLE OF VARCHAR2(15)  INDEX BY BINARY_INTEGER;
10 TYPE PrecisionType     IS TABLE OF NUMBER(1)     INDEX BY BINARY_INTEGER;
11 TYPE MauType           IS TABLE OF NUMBER        INDEX BY BINARY_INTEGER;
12 NextElement            BINARY_INTEGER := 0;
13 CurrencyCode           CurrencyCodeType;
14 Precision              PrecisionType;
15 Mau                    MauType;
19 
16 g_cust_inv_rec         ra_customer_trx%ROWTYPE;
17 
18 
20 --
21 -- local body code
22 --
23 
24 CURSOR CurrencyCursor( cp_currency_code VARCHAR2 ) IS
25 SELECT precision,
26        minimum_accountable_unit
27   FROM fnd_currencies
28  WHERE currency_code = cp_currency_code;
29 
30 
31 PROCEDURE GetCurrencyDetails( p_currency_code IN  VARCHAR2,
32                               p_precision     OUT NOCOPY NUMBER,
33                               p_mau           OUT NOCOPY NUMBER ) IS
34      i BINARY_INTEGER := 0;
35 BEGIN
36    WHILE i < NextElement
37    LOOP
38         EXIT WHEN CurrencyCode(i) = p_currency_code;
39         i := i + 1;
40    END LOOP;
41    IF i = NextElement   THEN
42         OPEN CurrencyCursor( p_currency_code );
43         DECLARE
44             l_Precision NUMBER;
45             l_Mau       NUMBER;
46         BEGIN
47             FETCH CurrencyCursor
48             INTO    l_Precision,
49                     l_Mau;
50             IF CurrencyCursor%NOTFOUND THEN
51                 RAISE NO_DATA_FOUND;
52             END IF;
53             Precision(i)    := l_Precision;
54             Mau(i)          := l_Mau;
55         END;
56         CLOSE CurrencyCursor;
57         CurrencyCode(i) := p_currency_code;
58         NextElement     := i + 1;
59     END IF;
60     p_precision := Precision(i);
61     p_mau       := Mau(i);
62 EXCEPTION
63     WHEN OTHERS THEN
64         IF CurrencyCursor%ISOPEN THEN
65            CLOSE CurrencyCursor;
66         END IF;
67 END;
68 
69 
70 
71 FUNCTION functional_amount(amount        IN NUMBER,
72                            currency_code IN VARCHAR2,
73                            exchange_rate IN NUMBER,
74                            precision     IN NUMBER,
75                            min_acc_unit  IN NUMBER) RETURN NUMBER IS
76 
77     CURSOR curr_info (cc FND_CURRENCIES.CURRENCY_CODE%TYPE) IS
78         SELECT PRECISION,
79                MINIMUM_ACCOUNTABLE_UNIT,
80                CURRENCY_CODE
81         FROM   FND_CURRENCIES
82         WHERE  CURRENCY_CODE = cc;
83     curr       curr_info%ROWTYPE;
84     loc_amount NUMBER;
85     invalid_params EXCEPTION;
86 
87 BEGIN
88 
89     IF (((currency_code IS NULL) AND
90          (precision IS NULL) AND
91          (min_acc_unit IS NULL)) OR
92         (amount IS NULL) ) THEN
93       BEGIN
94          RAISE invalid_params;
95       END;
96     END IF;
97 
98     IF ((precision IS NULL) AND (min_acc_unit IS NULL)) THEN
99       BEGIN
100          OPEN curr_info(currency_code);
101          FETCH curr_info INTO curr;
102          CLOSE curr_info;
103 
104          IF (curr.currency_code IS NULL) THEN
105               RAISE invalid_params;
106          END IF;
107       END;
108     ELSE
109       BEGIN
110          curr.precision := precision;
111          curr.minimum_accountable_unit := min_acc_unit;
112       END;
113     END IF;
114 
115     loc_amount := amount * NVL(exchange_rate, 1);
116 
117     IF (curr.minimum_accountable_unit IS NULL) THEN
118        RETURN( ROUND(loc_amount, curr.precision));
119     ELSE
120        RETURN( ROUND((loc_amount / curr.minimum_accountable_unit)) *
121                curr.minimum_accountable_unit);
122     END IF;
123 
124 EXCEPTION
125      WHEN OTHERS THEN
126          -- Bug 2191876
127          IF curr_info%ISOPEN THEN
128             CLOSE curr_info;
129          END IF;
130 
131 END functional_amount;
132 
133 
134 
135 
136 
137 
138 
139 FUNCTION CurrRound( p_amount        IN NUMBER,
140                     p_currency_code IN VARCHAR2 DEFAULT NULL)
141 RETURN NUMBER
142 IS
143     l_precision NUMBER(1);
144     l_mau       NUMBER;
145 BEGIN
146     GetCurrencyDetails( p_currency_code, l_precision, l_mau );
147     IF l_mau IS NOT NULL
148     THEN
149          RETURN( ROUND( p_amount / l_mau) * l_mau );
150     ELSE
151          RETURN( ROUND( p_amount, l_precision ));
152     END IF;
153 EXCEPTION
154     WHEN OTHERS THEN
155         RAISE;
156 END;
157 
158 
159 
160 
161 PROCEDURE Set_Buckets(
162       p_header_acctd_amt   IN     NUMBER        ,
163       p_base_currency      IN     fnd_currencies.currency_code%TYPE,
164       p_exchange_rate      IN     NUMBER        ,
165       p_base_precision     IN     NUMBER        ,
166       p_base_min_acc_unit  IN     NUMBER        ,
167       p_tax_amt            IN     NUMBER        ,
168       p_charges_amt        IN     NUMBER        ,
169       p_line_amt           IN     NUMBER        ,
170       p_freight_amt        IN     NUMBER        ,
171       p_tax_acctd_amt      IN OUT NOCOPY NUMBER        ,
172       p_charges_acctd_amt  IN OUT NOCOPY NUMBER        ,
173       p_line_acctd_amt     IN OUT NOCOPY NUMBER        ,
174       p_freight_acctd_amt  IN OUT NOCOPY NUMBER         ) IS
175 
176 l_run_amt_tot         NUMBER;
177 l_run_acctd_amt_tot   NUMBER;
178 l_last_bucket         VARCHAR2(1);
179 
180 /* Bug 2013601
181  Variables to hold running total of amount, accounted amount and the
182  total adjusted amount */
183 l_run_oth_amt_tot       NUMBER;
184 l_run_oth_acctd_amt_tot NUMBER;
185 l_amt_tot               NUMBER;
186 BEGIN
187 
188      l_run_amt_tot       := 0;
189      l_run_acctd_amt_tot := 0;
190 
191      /* Bug 2013601
192         Initialise the variables */
193      l_run_oth_amt_tot := 0;
194      l_run_oth_acctd_amt_tot := 0;
195      l_amt_tot := p_tax_amt + p_charges_amt + p_line_amt + p_freight_amt ;
196 
197      l_run_amt_tot                 := l_run_amt_tot + p_tax_amt;
198      p_tax_acctd_amt               := functional_amount(
199                                           l_run_amt_tot,
200                                           p_base_currency,
201                                           p_exchange_rate,
202                                           p_base_precision,
203                                           p_base_min_acc_unit) - l_run_acctd_amt_tot;
204 
205      l_run_acctd_amt_tot           := l_run_acctd_amt_tot + p_tax_acctd_amt;
206 
207      IF p_tax_acctd_amt <> 0 THEN
208         l_last_bucket    := 'T';
209      END IF;
210 
211      l_run_amt_tot                := l_run_amt_tot + p_charges_amt;
212      p_charges_acctd_amt          := functional_amount(
213                                          l_run_amt_tot,
214                                          p_base_currency,
215                                          p_exchange_rate,
216                                          p_base_precision,
217                                          p_base_min_acc_unit) - l_run_acctd_amt_tot;
218 
219      l_run_acctd_amt_tot          := l_run_acctd_amt_tot + p_charges_acctd_amt;
220 
221      IF p_charges_acctd_amt <> 0 THEN
222         l_last_bucket    := 'C';
223      END IF;
224 
225      l_run_amt_tot                := l_run_amt_tot + p_line_amt;
226      p_line_acctd_amt             := functional_amount(
227                                          l_run_amt_tot,
228                                          p_base_currency,
229                                          p_exchange_rate,
230                                          p_base_precision,
231                                          p_base_min_acc_unit) - l_run_acctd_amt_tot;
232 
233      l_run_acctd_amt_tot          := l_run_acctd_amt_tot + p_line_acctd_amt;
234 
235      IF p_line_acctd_amt <> 0 THEN
236         l_last_bucket    := 'L';
237      END IF;
238 
239      l_run_amt_tot                := l_run_amt_tot + p_freight_amt;
240      p_freight_acctd_amt          := functional_amount(
241                                          l_run_amt_tot,
242                                          p_base_currency,
243                                          p_exchange_rate,
244                                          p_base_precision,
245                                          p_base_min_acc_unit) - l_run_acctd_amt_tot;
246 
247      l_run_acctd_amt_tot          := l_run_acctd_amt_tot + p_freight_acctd_amt;
248 
249      IF p_freight_acctd_amt <> 0 THEN
250         l_last_bucket    := 'F';
251      END IF;
252 
253 
254      IF l_last_bucket IS NULL THEN
255 
256            l_run_oth_amt_tot       := l_run_oth_amt_tot + p_tax_amt;
257            p_tax_acctd_amt         := Currround((l_run_oth_amt_tot/l_amt_tot)*
258                                          p_header_acctd_amt,p_base_currency) -
259                                          l_run_oth_acctd_amt_tot;
260            l_run_oth_acctd_amt_tot := l_run_oth_acctd_amt_tot + p_tax_acctd_amt;
261 
262            l_run_oth_amt_tot       := l_run_oth_amt_tot + p_charges_amt;
263            p_charges_acctd_amt     := Currround((l_run_oth_amt_tot/l_amt_tot)*
264                                          p_header_acctd_amt,p_base_currency) -
265                                          l_run_oth_acctd_amt_tot;
266            l_run_oth_acctd_amt_tot := l_run_oth_acctd_amt_tot +p_charges_acctd_amt;
267 
268            l_run_oth_amt_tot       := l_run_oth_amt_tot + p_line_amt;
269            p_line_acctd_amt        := Currround((l_run_oth_amt_tot/l_amt_tot)*
270                                          p_header_acctd_amt,p_base_currency) -
271                                          l_run_oth_acctd_amt_tot;
272            l_run_oth_acctd_amt_tot := l_run_oth_acctd_amt_tot + p_line_acctd_amt;
273 
274            l_run_oth_amt_tot       := l_run_oth_amt_tot + p_freight_amt;
275            p_freight_acctd_amt     := Currround((l_run_oth_amt_tot/l_amt_tot)*
276                                          p_header_acctd_amt,p_base_currency) -
277                                          l_run_oth_acctd_amt_tot;
278            l_run_oth_acctd_amt_tot := l_run_oth_acctd_amt_tot + p_freight_acctd_amt;
279 
280 
281      ELSIF    l_last_bucket = 'T' THEN
282            p_tax_acctd_amt     := p_tax_acctd_amt     - (l_run_acctd_amt_tot - p_header_acctd_amt);
283      ELSIF l_last_bucket = 'C' THEN
284            p_charges_acctd_amt := p_charges_acctd_amt - (l_run_acctd_amt_tot - p_header_acctd_amt);
285      ELSIF l_last_bucket = 'L' THEN
286            p_line_acctd_amt    := p_line_acctd_amt    - (l_run_acctd_amt_tot - p_header_acctd_amt);
287      ELSIF l_last_bucket = 'F' THEN
288            p_freight_acctd_amt := p_freight_acctd_amt - (l_run_acctd_amt_tot - p_header_acctd_amt);
289      END IF;
290 
291 EXCEPTION
292   WHEN OTHERS THEN    RAISE;
293 
294 END Set_Buckets;
295 
296 END;