DBA Data[Home] [Help]

PACKAGE BODY: APPS.PSB_BUDGET_REVISIONS_PVT

Source


1 PACKAGE BODY PSB_BUDGET_REVISIONS_PVT AS
2 /* $Header: PSBVBRVB.pls 120.39 2006/06/02 13:49:05 matthoma ship $ */
3 
4   G_PKG_NAME CONSTANT VARCHAR2(30):= 'PSB_BUDGET_REVISIONS_PVT';
5 
6   g_default_fte       CONSTANT NUMBER := 1;
7 
8   -- Bug#4675858
9   -- This variable will be used to check whether the call is
10   -- from "Revise_Elements"
11   g_elem_projection  BOOLEAN := FALSE ;
12 
13   -- use this variable to determine if cost recalc is for new position
14   g_new_position      BOOLEAN;
15 
16   -- use this variable to determine if this position should be revised
17   g_revised_position  BOOLEAN;
18 
19   -- use this variable to determine if the revised fte for a position is zero.
20 
21   TYPE TokNameArray IS TABLE OF VARCHAR2(100) INDEX BY BINARY_INTEGER;
22 
23   -- TokValArray contains values for all tokens
24   TYPE TokValArray IS TABLE OF VARCHAR2(1000) INDEX BY BINARY_INTEGER;
25 
26   -- Number of Message Tokens
27   no_msg_tokens       NUMBER := 0;
28 
29   -- Message Token Name
30   msg_tok_names       TokNameArray;
31 
32   -- Message Token Value
33   msg_tok_val         TokValArray;
34 
35   -- For Bug#2150471
36   TYPE g_ccid_rec_type IS RECORD
37   ( ccid                NUMBER(15),
38     apply_balance_flag  VARCHAR2(15)
39   ) ;
40   --
41   TYPE g_ccid_rec_table IS TABLE OF g_ccid_rec_type INDEX BY BINARY_INTEGER;
42   g_ccid_rec          g_ccid_rec_table;
43   g_no_ccids          NUMBER;
44 
45   cursor c_Global_Rev (RevID NUMBER) is
46     select nvl(global_budget_revision_id, budget_revision_id) global_revision_id
47       from PSB_BUDGET_REVISIONS
48      where budget_revision_id = RevID;
49 
50   cursor c_Distribute_Rev (GlobalRevID NUMBER, BudgetGroupID NUMBER) is
51     select budget_revision_id
52       from PSB_BUDGET_REVISIONS
53      where nvl(global_budget_revision_id, budget_revision_id) = GlobalRevID
54        and budget_group_id in
55           (select budget_group_id
56              from PSB_BUDGET_GROUPS
57             where budget_group_type = 'R'
58             start with budget_group_id = BudgetGroupID
59           connect by prior parent_budget_group_id = budget_group_id);
60 
61   TYPE g_revpos_rec_type IS RECORD
62   ( budget_revision_pos_line_id NUMBER, position_id NUMBER,
63     budget_group_id NUMBER, effective_start_date DATE, effective_end_date DATE,
64     revision_type VARCHAR2(1), revision_value_type VARCHAR2(1),
65     revision_value NUMBER, note_id NUMBER, delete_flag BOOLEAN ) ;
66 
67   TYPE g_costs_rec_type IS RECORD
68       (pay_element_id NUMBER, element_type VARCHAR2(1), element_cost NUMBER,
69        start_date DATE, end_date DATE, currency_code VARCHAR2(15));
70 
71   TYPE g_dists_rec_type IS RECORD
72       (ccid NUMBER, budget_group_id NUMBER, currency_code VARCHAR2(15),
73        start_date DATE, end_date DATE, amount NUMBER, calc_rev BOOLEAN);
74 
75   TYPE g_revaccts_rec_type IS RECORD
76       (ccid NUMBER, amount NUMBER);
77 
78   TYPE g_elem_assignments_rec_type IS RECORD
79   ( budget_revision_id NUMBER, start_date DATE, end_date DATE,
80     pay_element_id NUMBER, pay_element_option_id NUMBER, pay_basis VARCHAR2(10),
81     element_value_type VARCHAR2(2), element_value NUMBER, use_in_calc BOOLEAN);
82 
83   TYPE g_elem_rates_rec_type IS RECORD
84   ( budget_revision_id NUMBER, start_date DATE, end_date DATE,
85     pay_element_id NUMBER, pay_element_option_id NUMBER, pay_basis VARCHAR2(10),
86     element_value_type VARCHAR2(2), element_value NUMBER, formula_id NUMBER);
87 
88   TYPE g_fte_assignments_rec_type IS RECORD
89       (start_date DATE, end_date DATE, fte NUMBER);
90 
91   TYPE g_wkh_assignments_rec_type IS RECORD
92       (start_date DATE, end_date DATE, default_weekly_hours NUMBER);
93 
94   -- Bug#4310411 Start
95   -- Declare three TYPES represnting required datatypes
96   TYPE Number_Tbl_Type IS TABLE OF NUMBER        INDEX BY PLS_INTEGER;
97   TYPE Char_Tbl_Type   IS TABLE OF VARCHAR2(100) INDEX BY PLS_INTEGER;
98   TYPE Date_Tbl_Type   IS TABLE OF DATE          INDEX BY PLS_INTEGER;
99   -- Bug#4310411 End
100 
101   TYPE g_revpos_tbl_type IS TABLE OF g_revpos_rec_type
102       INDEX BY BINARY_INTEGER;
103 
104   TYPE g_costs_tbl_type IS TABLE OF g_costs_rec_type
105       INDEX BY BINARY_INTEGER;
106 
107   TYPE g_dists_tbl_type IS TABLE OF g_dists_rec_type
108       INDEX BY BINARY_INTEGER;
109 
110   TYPE g_revaccts_tbl_type IS TABLE OF g_revaccts_rec_type
111       INDEX BY BINARY_INTEGER;
112 
113   TYPE g_elem_assignments_tbl_type IS TABLE OF g_elem_assignments_rec_type
114       INDEX BY BINARY_INTEGER;
115 
116   TYPE g_elem_rates_tbl_type IS TABLE OF g_elem_rates_rec_type
117       INDEX BY BINARY_INTEGER;
118 
119   TYPE g_fte_assignments_tbl_type IS TABLE OF g_fte_assignments_rec_type
120       INDEX BY BINARY_INTEGER;
121 
122   TYPE g_wkh_assignments_tbl_type IS TABLE OF g_wkh_assignments_rec_type
123       INDEX BY BINARY_INTEGER;
124 
125   g_revpos                g_revpos_tbl_type;
126   g_num_revpos            NUMBER;
127 
128   g_costs                 g_costs_tbl_type;
129   g_num_costs             NUMBER;
130 
131   g_dists                 g_dists_tbl_type;
132   g_num_dists             NUMBER;
133 
134   g_revaccts              g_revaccts_tbl_type;
135   g_num_revaccts          NUMBER;
136 
137   g_elem_assignments      g_elem_assignments_tbl_type;
138   g_num_elem_assignments  NUMBER;
139 
140   g_elem_rates            g_elem_rates_tbl_type;
141   g_num_elem_rates        NUMBER;
142 
143   g_fte_assignments       g_fte_assignments_tbl_type;
144   g_num_fte_assignments   NUMBER;
145 
146   g_wkh_assignments       g_wkh_assignments_tbl_type;
147   g_num_wkh_assignments   NUMBER;
148 
149   -- Bug No 2135165
150   g_brr_rule_set_id       NUMBER;
151   g_brr_sob_id            NUMBER;
152 
153   -- bug no 3439168
154   TYPE g_last_update_flag_tbl_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
155   g_last_update_flag_tbl g_last_update_flag_tbl_type;
156   -- bug no 3439168
157 
158 PROCEDURE message_token
159 ( tokname  IN  VARCHAR2,
160   tokval   IN  VARCHAR2
161 );
162 
163 PROCEDURE add_message
164 (appname  IN  VARCHAR2,
165  msgname  IN  VARCHAR2);
166 
167 
168 /*===========================================================================+
169  |                             PROCEDURE pd                                  |
170  +===========================================================================*/
171 -- API to print debug information, used during only development.
172 PROCEDURE pd( p_message   IN     VARCHAR2)
173 IS
174 BEGIN
175   NULL ;
176   --DBMS_OUTPUT.Put_Line(p_message) ;
177 END pd ;
178 /*---------------------------------------------------------------------------*/
179 
180 
181 /*==========================================================================+
182  |                       FUNCTION Get_Rounded_Amount                        |
183  +==========================================================================*/
184 FUNCTION Get_Rounded_Amount
185 ( p_currency_code             IN       VARCHAR2,
186   p_amount                    IN       NUMBER
187 )  RETURN NUMBER IS
188 
189   l_precision                   NUMBER;
190   l_minimum_accountable_unit    NUMBER;
191   l_rounded_amount              NUMBER;
192 
193 BEGIN
194 
195     SELECT minimum_accountable_unit,
196            precision
197     INTO   l_minimum_accountable_unit,
198            l_precision
199     FROM   fnd_currencies
200     WHERE  currency_code = p_currency_code
201     AND    enabled_flag = 'Y'
202     AND    currency_flag = 'Y'
203     AND    (start_date_active <= sysdate or start_date_active is null)
204     AND    (end_date_active >= sysdate or end_date_active is null);
205 
206   -- General Ledger rounds distributed amount(distributed to budget periods) to
207   -- minimum accountable unit of the currency. This is to handle
208   -- difference resulting from amounts that cannot be divided exactly
209 
210   l_minimum_accountable_unit := nvl(l_minimum_accountable_unit, power( 10, (-1 * l_precision)));
211   --
212   -- Calculate Rounded Amount
213 
214   l_rounded_amount := round( p_amount / l_minimum_accountable_unit ) * l_minimum_accountable_unit;
215 
216   RETURN(l_rounded_amount);
217 
218 EXCEPTION
219 
220   WHEN OTHERS THEN
221     RETURN p_amount;
222 
223 END Get_Rounded_Amount;
224 
225 /*==========================================================================+
226  |                       PROCEDURE Delete_Row                               |
227  +==========================================================================*/
228 
229 PROCEDURE Delete_Row
230 ( p_api_version         IN      NUMBER,
231   p_init_msg_list       IN      VARCHAR2 := FND_API.G_FALSE,
232   p_commit              IN      VARCHAR2 := FND_API.G_FALSE,
233   p_validation_level    IN      NUMBER  := FND_API.G_VALID_LEVEL_FULL,
234   p_return_status       OUT  NOCOPY     VARCHAR2,
235   p_msg_count           OUT  NOCOPY     NUMBER,
236   p_msg_data            OUT  NOCOPY     VARCHAR2,
237   p_budget_revision_id  IN      NUMBER
238 ) IS
239 
240   l_api_name            CONSTANT VARCHAR2(30)   := 'DELETE_ROW';
241   l_api_version         CONSTANT NUMBER         := 1.0;
242 
243 BEGIN
244 
245   -- Standard Start of API savepoint
246 
247   SAVEPOINT     DELETE_ROW;
248 
249   -- Standard call to check for call compatibility.
250 
251   if not FND_API.Compatible_API_Call (l_api_version,
252                                       p_api_version,
253                                       l_api_name,
254                                       G_PKG_NAME)
255   THEN
256     raise FND_API.G_EXC_UNEXPECTED_ERROR;
257   END IF;
258 
259   -- Initialize message list if p_init_msg_list is set to TRUE.
260 
261   if FND_API.to_Boolean (p_init_msg_list) THEN
262     FND_MSG_PUB.initialize;
263   END IF;
264 
265   -- Perform the delete
266 
267   delete FROM PSB_BUDGET_REVISIONS
268   WHERE BUDGET_REVISION_ID = P_BUDGET_REVISION_ID;
269 
270   if (sql%notfound) THEN
271     raise no_data_found;
272   END IF;
273 
274   p_return_status := FND_API.G_RET_STS_SUCCESS;
275 
276   -- Standard call to get message count and if count is 1, get message info.
277 
278   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
279                              p_data  => p_msg_data);
280 
281   -- Standard check of p_commit.
282 
283   if FND_API.to_Boolean (p_commit) THEN
284     commit work;
285   END IF;
286 
287 
288 EXCEPTION
289 
290    WHEN FND_API.G_EXC_ERROR THEN
291      rollback to DELETE_ROW;
292      p_return_status := FND_API.G_RET_STS_ERROR;
293 
294      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
295                                 p_data  => p_msg_data);
296 
297    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
298      rollback to DELETE_ROW;
299      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
300 
301      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
302                                 p_data  => p_msg_data);
303 
304    WHEN OTHERS THEN
305      rollback to DELETE_ROW;
306      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
307 
308      if FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
309        FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
310                                 l_api_name);
311      END IF;
312 
313      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
314                                 p_data  => p_msg_data);
315 
316 END Delete_Row;
317 
318 /*==========================================================================+
319  |                       PROCEDURE Create_Budget_Revision                   |
320  +==========================================================================*/
321 PROCEDURE Create_Budget_Revision
322 ( p_api_version                    IN   NUMBER,
323   p_init_msg_list                  IN   VARCHAR2 := FND_API.G_FALSE,
324   p_commit                         IN   VARCHAR2 := FND_API.G_FALSE,
325   p_validation_level               IN   NUMBER  := FND_API.G_VALID_LEVEL_FULL,
326   p_return_status                  OUT  NOCOPY  VARCHAR2,
327   p_msg_count                      OUT  NOCOPY  NUMBER,
328   p_msg_data                       OUT  NOCOPY  VARCHAR2,
329   p_budget_revision_id          IN OUT  NOCOPY   NUMBER,
330   p_budget_group_id                 IN   NUMBER  := FND_API.G_MISS_NUM,
331   p_gl_budget_set_id                IN   NUMBER  := FND_API.G_MISS_NUM,
332   p_hr_budget_id                    IN   NUMBER  := FND_API.G_MISS_NUM,
333   p_justification                   IN   VARCHAR2 := FND_API.G_MISS_CHAR,
334   p_from_gl_period_name             IN   VARCHAR2 := FND_API.G_MISS_CHAR,
335   p_to_gl_period_name               IN   VARCHAR2 := FND_API.G_MISS_CHAR,
336   p_currency_code                   IN   VARCHAR2 := FND_API.G_MISS_CHAR,
337   p_effective_start_date            IN   DATE := FND_API.G_MISS_DATE,
338   p_effective_end_date              IN   DATE := FND_API.G_MISS_DATE,
339   p_budget_revision_type            IN   VARCHAR2 := FND_API.G_MISS_CHAR,
340   p_transaction_type                IN   VARCHAR2 := FND_API.G_MISS_CHAR,
341   p_permanent_revision              IN   VARCHAR2 := FND_API.G_MISS_CHAR,
342   p_revise_by_position              IN   VARCHAR2 := FND_API.G_MISS_CHAR,
343   p_balance_type                    IN   VARCHAR2 := FND_API.G_MISS_CHAR,
344   p_global_budget_revision          IN   VARCHAR2 := FND_API.G_MISS_CHAR,
345   p_global_budget_revision_id       IN   NUMBER := FND_API.G_MISS_NUM,
346   p_requestor                       IN   NUMBER := FND_API.G_MISS_NUM,
347   p_parameter_set_id                IN   NUMBER := FND_API.G_MISS_NUM,
348   p_constraint_set_id               IN   NUMBER := FND_API.G_MISS_NUM,
349   p_submission_date                 IN   DATE := FND_API.G_MISS_DATE,
350   p_submission_status               IN   VARCHAR2 := FND_API.G_MISS_CHAR,
351   p_approval_orig_system            IN   VARCHAR2 := FND_API.G_MISS_CHAR,
352   p_approval_override_by            IN   NUMBER := FND_API.G_MISS_NUM,
353   p_freeze_flag                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
354   p_request_id                      IN   NUMBER := FND_API.G_MISS_NUM,
355   p_base_line_revision              IN   VARCHAR2 := FND_API.G_MISS_CHAR,
356   p_attribute1                      IN   VARCHAR2 := FND_API.G_MISS_CHAR,
357   p_attribute2                      IN   VARCHAR2 := FND_API.G_MISS_CHAR,
358   p_attribute3                      IN   VARCHAR2 := FND_API.G_MISS_CHAR,
359   p_attribute4                      IN   VARCHAR2 := FND_API.G_MISS_CHAR,
360   p_attribute5                      IN   VARCHAR2 := FND_API.G_MISS_CHAR,
361   p_attribute6                      IN   VARCHAR2 := FND_API.G_MISS_CHAR,
362   p_attribute7                      IN   VARCHAR2 := FND_API.G_MISS_CHAR,
363   p_attribute8                      IN   VARCHAR2 := FND_API.G_MISS_CHAR,
364   p_attribute9                      IN   VARCHAR2 := FND_API.G_MISS_CHAR,
365   p_attribute10                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
366   p_attribute11                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
367   p_attribute12                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
368   p_attribute13                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
369   p_attribute14                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
370   p_attribute15                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
371   p_attribute16                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
372   p_attribute17                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
373   p_attribute18                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
374   p_attribute19                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
375   p_attribute20                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
376   p_attribute21                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
377   p_attribute22                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
378   p_attribute23                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
379   p_attribute24                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
380   p_attribute25                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
381   p_attribute26                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
382   p_attribute27                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
383   p_attribute28                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
384   p_attribute29                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
385   p_attribute30                     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
386   p_context                         IN   VARCHAR2 := FND_API.G_MISS_CHAR
387 ) IS
388 
389   l_api_name                        CONSTANT VARCHAR2(30) := 'Create_Budget_Revision';
390   l_api_version                     CONSTANT NUMBER       := 1.0;
391 
392   l_budget_revision_id              NUMBER;
393 
394   cursor c_Seq is
395     select psb_budget_revisions_s.nextval budget_revision_id
396       from dual;
397 
398 BEGIN
399 
400   -- Standard Start of API savepoint
401 
402   SAVEPOINT     Create_Budget_Revision;
403 
404   -- Standard call to check for call compatibility.
405 
406   if not FND_API.Compatible_API_Call (l_api_version,
407                                       p_api_version,
408                                       l_api_name,
409                                       G_PKG_NAME)
410   then
411     raise FND_API.G_EXC_UNEXPECTED_ERROR;
412   end if;
413 
414   -- Initialize message list if p_init_msg_list is set to TRUE.
415 
416   IF FND_API.to_Boolean (p_init_msg_list) THEN
417     FND_MSG_PUB.initialize;
418   END IF;
419 
420   if p_budget_revision_id is not null then
421 
422     update PSB_BUDGET_REVISIONS
423        set justification = decode(p_justification, FND_API.G_MISS_CHAR, justification, p_justification),
424            from_gl_period_name = decode(p_from_gl_period_name,FND_API.G_MISS_CHAR, from_gl_period_name, p_from_gl_period_name),
425            to_gl_period_name = decode(p_to_gl_period_name, FND_API.G_MISS_CHAR, to_gl_period_name, p_to_gl_period_name),
426            currency_code = decode(p_currency_code, FND_API.G_MISS_CHAR, currency_code, p_currency_code),
427            effective_start_date = decode(p_effective_start_date, FND_API.G_MISS_DATE, effective_start_date, p_effective_start_date),
428            effective_end_date = decode(p_effective_end_date, FND_API.G_MISS_DATE, effective_end_date, p_effective_end_date),
429            hr_budget_id = decode(p_hr_budget_id, FND_API.G_MISS_NUM, hr_budget_id, p_hr_budget_id),
430            budget_revision_type = decode(p_budget_revision_type, FND_API.G_MISS_CHAR, budget_revision_type, p_budget_revision_type),
431            transaction_type = decode(p_transaction_type, FND_API.G_MISS_CHAR, transaction_type, p_transaction_type),
432            permanent_revision = decode(p_permanent_revision, FND_API.G_MISS_CHAR, permanent_revision, p_permanent_revision),
433            revise_by_position = decode(p_revise_by_position, FND_API.G_MISS_CHAR, revise_by_position, p_revise_by_position),
434            balance_type = decode(p_balance_type, FND_API.G_MISS_CHAR, balance_type, p_balance_type),
435            global_budget_revision = decode(p_global_budget_revision, FND_API.G_MISS_CHAR,global_budget_revision,p_global_budget_revision),
436            global_budget_revision_id = decode(p_global_budget_revision_id, FND_API.G_MISS_NUM,global_budget_revision_id,p_global_budget_revision_id),
437            parameter_set_id = decode(p_parameter_set_id, FND_API.G_MISS_NUM, parameter_set_id, p_parameter_set_id),
438            constraint_set_id = decode(p_constraint_set_id, FND_API.G_MISS_NUM, constraint_set_id, p_constraint_set_id),
439            submission_date = decode(p_submission_date, FND_API.G_MISS_DATE, submission_date, p_submission_date),
440            submission_status = decode(p_submission_status, FND_API.G_MISS_CHAR, submission_status, p_submission_status),
441            approval_orig_system = decode(p_approval_orig_system, FND_API.G_MISS_CHAR, approval_orig_system, p_approval_orig_system),
442            approval_override_by  = decode(p_approval_override_by, FND_API.G_MISS_NUM, approval_override_by, p_approval_override_by),
443            freeze_flag = decode(p_freeze_flag, FND_API.G_MISS_CHAR, freeze_flag, p_freeze_flag),
444            request_id = decode(p_request_id, FND_API.G_MISS_NUM, request_id, p_request_id),
445            base_line_revision = decode(p_base_line_revision, FND_API.G_MISS_CHAR, base_line_revision, p_base_line_revision),
446            last_update_date = sysdate,
447            last_updated_by = FND_GLOBAL.USER_ID,
448            last_update_login = FND_GLOBAL.LOGIN_ID,
449            attribute1 = decode(p_attribute1, FND_API.G_MISS_CHAR, attribute1, p_attribute1),
450            attribute2 = decode(p_attribute2, FND_API.G_MISS_CHAR, attribute2, p_attribute2),
451            attribute3 = decode(p_attribute3, FND_API.G_MISS_CHAR, attribute3, p_attribute3),
452            attribute4 = decode(p_attribute4, FND_API.G_MISS_CHAR, attribute4, p_attribute4),
453            attribute5 = decode(p_attribute5, FND_API.G_MISS_CHAR, attribute5, p_attribute5),
454            attribute6 = decode(p_attribute6, FND_API.G_MISS_CHAR, attribute6, p_attribute6),
455            attribute7 = decode(p_attribute7, FND_API.G_MISS_CHAR, attribute7, p_attribute7),
456            attribute8 = decode(p_attribute8, FND_API.G_MISS_CHAR, attribute8, p_attribute8),
457            attribute9 = decode(p_attribute9, FND_API.G_MISS_CHAR, attribute9, p_attribute9),
458            attribute10 = decode(p_attribute10, FND_API.G_MISS_CHAR, attribute10, p_attribute10),
459            attribute11 = decode(p_attribute11, FND_API.G_MISS_CHAR, attribute11, p_attribute11),
460            attribute12 = decode(p_attribute12, FND_API.G_MISS_CHAR, attribute12, p_attribute12),
461            attribute13 = decode(p_attribute13, FND_API.G_MISS_CHAR, attribute13, p_attribute13),
462            attribute14 = decode(p_attribute14, FND_API.G_MISS_CHAR, attribute14, p_attribute14),
463            attribute15 = decode(p_attribute15, FND_API.G_MISS_CHAR, attribute15, p_attribute15),
464            attribute16 = decode(p_attribute16, FND_API.G_MISS_CHAR, attribute16, p_attribute16),
465            attribute17 = decode(p_attribute17, FND_API.G_MISS_CHAR, attribute17, p_attribute17),
466            attribute18 = decode(p_attribute18, FND_API.G_MISS_CHAR, attribute18, p_attribute18),
467            attribute19 = decode(p_attribute19, FND_API.G_MISS_CHAR, attribute19, p_attribute19),
468            attribute20 = decode(p_attribute20, FND_API.G_MISS_CHAR, attribute20, p_attribute20),
469            attribute21 = decode(p_attribute21, FND_API.G_MISS_CHAR, attribute21, p_attribute21),
470            attribute22 = decode(p_attribute22, FND_API.G_MISS_CHAR, attribute22, p_attribute22),
471            attribute23 = decode(p_attribute23, FND_API.G_MISS_CHAR, attribute23, p_attribute23),
472            attribute24 = decode(p_attribute24, FND_API.G_MISS_CHAR, attribute24, p_attribute24),
473            attribute25 = decode(p_attribute25, FND_API.G_MISS_CHAR, attribute25, p_attribute25),
474            attribute26 = decode(p_attribute26, FND_API.G_MISS_CHAR, attribute26, p_attribute26),
475            attribute27 = decode(p_attribute27, FND_API.G_MISS_CHAR, attribute27, p_attribute27),
476            attribute28 = decode(p_attribute28, FND_API.G_MISS_CHAR, attribute28, p_attribute28),
477            attribute29 = decode(p_attribute29, FND_API.G_MISS_CHAR, attribute29, p_attribute29),
478            attribute30 = decode(p_attribute30, FND_API.G_MISS_CHAR, attribute30, p_attribute30),
479            context = decode(p_context,FND_API.G_MISS_CHAR, context, p_context)
480      where budget_revision_id = p_budget_revision_id;
481 
482      IF (SQL%NOTFOUND) THEN
483        RAISE NO_DATA_FOUND;
484      END IF;
485 
486   else
487 
488     for c_Seq_Rec in c_Seq loop
489       l_budget_revision_id := c_Seq_Rec.budget_revision_id;
490     end loop;
491 
492     INSERT INTO PSB_BUDGET_REVISIONS
493      (budget_revision_id,
494       justification,
495       budget_group_id,
496       gl_budget_set_id,
497       hr_budget_id,
498       from_gl_period_name,
499       to_gl_period_name,
500       currency_code,
501       effective_start_date,
502       effective_end_date,
503       budget_revision_type,
504       transaction_type,
505       permanent_revision,
506       revise_by_position,
507       balance_type,
508       requestor,
509       parameter_set_id,
510       constraint_set_id,
511       submission_date,
512       submission_status,
513       approval_orig_system,
514       approval_override_by,
515       freeze_flag,
516       request_id,
517       base_line_revision,
518       global_budget_revision,
519       global_budget_revision_id,
520       last_update_date,
521       last_updated_by,
522       last_update_login,
523       created_by,
524       creation_date,
525       attribute1,
526       attribute2,
527       attribute3,
528       attribute4,
529       attribute5,
530       attribute6,
531       attribute7,
532       attribute8,
533       attribute9,
534       attribute10,
535       attribute11,
536       attribute12,
537       attribute13,
538       attribute14,
539       attribute15,
540       attribute16,
541       attribute17,
542       attribute18,
543       attribute19,
544       attribute20,
545       attribute21,
546       attribute22,
547       attribute23,
548       attribute24,
549       attribute25,
550       attribute26,
551       attribute27,
552       attribute28,
553       attribute29,
554       attribute30,
555       context)
556   values (l_budget_revision_id,
557           decode(p_justification,FND_API.G_MISS_CHAR,null,p_justification),
558           decode(p_budget_group_id,FND_API.G_MISS_NUM,null,p_budget_group_id),
559           decode(p_gl_budget_set_id,FND_API.G_MISS_NUM,null,p_gl_budget_set_id),
560           decode(p_hr_budget_id,FND_API.G_MISS_NUM,null,p_hr_budget_id),
561           decode(p_from_gl_period_name,FND_API.G_MISS_CHAR,null,p_from_gl_period_name),
562           decode(p_to_gl_period_name,FND_API.G_MISS_CHAR,null,p_to_gl_period_name),
563           decode(p_currency_code,FND_API.G_MISS_CHAR,null,p_currency_code),
564           decode(p_effective_start_date,FND_API.G_MISS_DATE,null,p_effective_start_date),
565           decode(p_effective_end_date,FND_API.G_MISS_DATE,null,p_effective_end_date),
566           decode(p_budget_revision_type,FND_API.G_MISS_CHAR,null,p_budget_revision_type),
567           decode(p_transaction_type,FND_API.G_MISS_CHAR,null,p_transaction_type),
568           decode(p_permanent_revision,FND_API.G_MISS_CHAR,null,p_permanent_revision),
569           decode(p_revise_by_position,FND_API.G_MISS_CHAR,null,p_revise_by_position),
570           decode(p_balance_type,FND_API.G_MISS_CHAR,'YTD',p_balance_type),
571           decode(p_requestor,FND_API.G_MISS_NUM, FND_GLOBAL.USER_ID, -1, FND_GLOBAL.USER_ID, p_requestor),
572           decode(p_parameter_set_id,FND_API.G_MISS_NUM,null,p_parameter_set_id),
573           decode(p_constraint_set_id,FND_API.G_MISS_NUM,null,p_constraint_set_id),
574           decode(p_submission_date,FND_API.G_MISS_DATE,null,p_submission_date),
575           decode(p_submission_status,FND_API.G_MISS_CHAR,null,p_submission_status),
576           decode(p_approval_orig_system,FND_API.G_MISS_CHAR,null,p_approval_orig_system),
577           decode(p_approval_override_by,FND_API.G_MISS_NUM,null,p_approval_override_by),
578           decode(p_freeze_flag,FND_API.G_MISS_CHAR,null,p_freeze_flag),
579           decode(p_request_id,FND_API.G_MISS_NUM,null,p_request_id),
580           decode(p_base_line_revision,FND_API.G_MISS_CHAR,null,p_base_line_revision),
581           decode(p_global_budget_revision,FND_API.G_MISS_CHAR,null,p_global_budget_revision),
582           decode(p_global_budget_revision_id,FND_API.G_MISS_NUM,null,p_global_budget_revision_id),
583           sysdate,
584           FND_GLOBAL.USER_ID,
585           FND_GLOBAL.LOGIN_ID,
586           FND_GLOBAL.USER_ID,
587           sysdate,
588           decode(p_attribute1,FND_API.G_MISS_CHAR,null,p_attribute1),
589           decode(p_attribute2,FND_API.G_MISS_CHAR,null,p_attribute2),
590           decode(p_attribute3,FND_API.G_MISS_CHAR,null,p_attribute3),
591           decode(p_attribute4,FND_API.G_MISS_CHAR,null,p_attribute4),
592           decode(p_attribute5,FND_API.G_MISS_CHAR,null,p_attribute5),
593           decode(p_attribute6,FND_API.G_MISS_CHAR,null,p_attribute6),
594           decode(p_attribute7,FND_API.G_MISS_CHAR,null,p_attribute7),
595           decode(p_attribute8,FND_API.G_MISS_CHAR,null,p_attribute8),
596           decode(p_attribute9,FND_API.G_MISS_CHAR,null,p_attribute9),
597           decode(p_attribute10,FND_API.G_MISS_CHAR,null,p_attribute10),
598           decode(p_attribute11,FND_API.G_MISS_CHAR,null,p_attribute11),
599           decode(p_attribute12,FND_API.G_MISS_CHAR,null,p_attribute12),
600           decode(p_attribute13,FND_API.G_MISS_CHAR,null,p_attribute13),
601           decode(p_attribute14,FND_API.G_MISS_CHAR,null,p_attribute14),
602           decode(p_attribute15,FND_API.G_MISS_CHAR,null,p_attribute15),
603           decode(p_attribute16,FND_API.G_MISS_CHAR,null,p_attribute16),
604           decode(p_attribute17,FND_API.G_MISS_CHAR,null,p_attribute17),
605           decode(p_attribute18,FND_API.G_MISS_CHAR,null,p_attribute18),
606           decode(p_attribute19,FND_API.G_MISS_CHAR,null,p_attribute19),
607           decode(p_attribute20,FND_API.G_MISS_CHAR,null,p_attribute20),
608           decode(p_attribute21,FND_API.G_MISS_CHAR,null,p_attribute21),
609           decode(p_attribute22,FND_API.G_MISS_CHAR,null,p_attribute22),
610           decode(p_attribute23,FND_API.G_MISS_CHAR,null,p_attribute23),
611           decode(p_attribute24,FND_API.G_MISS_CHAR,null,p_attribute24),
612           decode(p_attribute25,FND_API.G_MISS_CHAR,null,p_attribute25),
613           decode(p_attribute26,FND_API.G_MISS_CHAR,null,p_attribute26),
614           decode(p_attribute27,FND_API.G_MISS_CHAR,null,p_attribute27),
615           decode(p_attribute28,FND_API.G_MISS_CHAR,null,p_attribute28),
616           decode(p_attribute29,FND_API.G_MISS_CHAR,null,p_attribute29),
617           decode(p_attribute30,FND_API.G_MISS_CHAR,null,p_attribute30),
618           decode(p_context,FND_API.G_MISS_CHAR,null,p_context));
619 
620   end if;
621 
622   p_budget_revision_id := l_budget_revision_id;
623 
624   -- Initialize API return status to success
625 
626   p_return_status := FND_API.G_RET_STS_SUCCESS;
627 
628   -- Standard check of p_commit.
629 
630   IF FND_API.to_Boolean (p_commit) THEN
631     commit work;
632   END IF;
633 
634   -- Standard call to get message count and if count is 1, get message info.
635 
636   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
637                              p_data  => p_msg_data);
638 
639 EXCEPTION
640 
641    WHEN FND_API.G_EXC_ERROR THEN
642      rollback to Create_Budget_Revision;
643      p_return_status := FND_API.G_RET_STS_ERROR;
644      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
645                                 p_data  => p_msg_data);
646 
647    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
648      rollback to Create_Budget_Revision;
649      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
650      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
651                                 p_data  => p_msg_data);
652 
653    WHEN OTHERS THEN
654      rollback to Create_Budget_Revision;
655      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
656 
657      IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
658        FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
659                                 l_api_name);
660      END IF;
661 
662      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
663                                 p_data  => p_msg_data);
664 END Create_Budget_Revision;
665 
666 /* ----------------------------------------------------------------------- */
667 
668 PROCEDURE Cache_Revision_Variables
669 (p_return_status          OUT  NOCOPY   VARCHAR2,
670  p_budget_revision_id     IN   NUMBER
671 ) IS
672 
673   l_return_status         VARCHAR2(1);
674 
675   cursor c_Budget_Revision is
676     select budget_group_id,
677            budget_revision_type,
678            transaction_type,
679            permanent_revision,
680            nvl(revise_by_position,'N') revise_by_position,
681            balance_type,
682            parameter_set_id,
683            constraint_set_id,
684            gl_budget_set_id,
685            from_gl_period_name,
686            to_gl_period_name,
687            effective_start_date,
688            effective_end_date,
689            freeze_flag,
690            base_line_revision,
691            currency_code,
692            approval_orig_system,
693            approval_override_by,
694            nvl(global_budget_revision_id, budget_revision_id) global_budget_revision_id,
695            hr_budget_id
696      from  PSB_BUDGET_REVISIONS
697     where  budget_revision_id = p_budget_revision_id;
698 
699   cursor c_BG is
700     select nvl(root_budget_group_id, budget_group_id) root_budget_group_id,
701            nvl(set_of_books_id, root_set_of_books_id) set_of_books_id,
702            nvl(business_group_id, root_business_group_id) business_group_id,
703            name
704       from PSB_BUDGET_GROUPS_V
705      where budget_group_id = g_budget_group_id;
706 
707   cursor c_Sob is
708     select currency_code,
709            chart_of_accounts_id,
710            name,
711            enable_budgetary_control_flag
712       from GL_SETS_OF_BOOKS
713      where set_of_books_id = g_set_of_books_id;
714 
715   cursor c_gl_dates is
716     select period_name, start_date,end_date
717       from gl_period_statuses
718      where application_id = 101
719        and set_of_books_id = g_set_of_books_id
720        and period_name in (g_from_gl_period_name, g_to_gl_period_name);
721 
722   cursor c_gl_dates_br is
723     select min(start_date) start_date, max(end_date) end_date
724       from GL_PERIOD_STATUSES
725      where application_id = 101
726        and set_of_books_id = g_set_of_books_id
727        and period_name in
728           (select b.gl_period_name from psb_budget_revision_lines a, psb_budget_revision_accounts b
729             where a.budget_revision_id = p_budget_revision_id
730               and b.budget_revision_acct_line_id = a.budget_revision_acct_line_id);
731 
732   cursor c_position_dates_br is
733     select min(effective_start_date) start_date, max(effective_end_date) end_date
734       from PSB_BUDGET_REVISION_POSITIONS a, PSB_BUDGET_REVISION_POS_LINES b
735      where b.budget_revision_id = p_budget_revision_id
736        and a.budget_revision_pos_line_id = b.budget_revision_pos_line_id;
737 
738   cursor c_position_exists is
739     select 'Exists'
740       from dual
741      where exists
742           (select a.position_id, a.effective_start_date, a.effective_end_date, a.budget_group_id
743              from PSB_POSITIONS a,
744                  (select budget_group_id from PSB_BUDGET_GROUPS
745                    start with budget_group_id = g_budget_group_id
746                  connect by prior budget_group_id = parent_budget_group_id) b
747             where a.data_extract_id = g_data_extract_id
748               and a.budget_group_id = b.budget_group_id);
749 
750   cursor c_constraint_set is
751     select name, constraint_threshold
752       from PSB_CONSTRAINT_SETS_V
753      where constraint_set_id = g_constraint_set_id;
754 
755 begin
756 
757    for c_Budget_Revision_Rec in c_Budget_Revision loop
758      g_budget_group_id      := c_Budget_Revision_Rec.budget_group_id;
759      g_budget_revision_type := c_Budget_Revision_Rec.budget_revision_type;
760      g_transaction_type     := c_Budget_Revision_Rec.transaction_type;
761      g_permanent_revision   := c_Budget_Revision_Rec.permanent_revision;
762      g_revise_by_position   := c_Budget_Revision_Rec.revise_by_position;
763      g_balance_type         := c_Budget_Revision_Rec.balance_type;
764      g_from_gl_period_name  := c_Budget_Revision_Rec.from_gl_period_name;
765      g_to_gl_period_name    := c_Budget_Revision_Rec.to_gl_period_name;
766      g_effective_start_date := c_Budget_Revision_Rec.effective_start_date;
767      g_effective_end_date   := c_Budget_Revision_Rec.effective_end_date;
768      g_parameter_set_id     := c_Budget_Revision_Rec.parameter_set_id;
769      /* For Bug No. 2810621 Start */
770      g_constraint_set_id    := fnd_profile.value('PSB_DEFAULT_CONSTRAINT_SET_BUDGET_REVISIONS');
771      /* For Bug No. 2810621 End */
772      g_freeze_flag          := c_Budget_Revision_Rec.freeze_flag;
773      g_base_line_revision   := c_Budget_Revision_Rec.base_line_revision;
774      g_approval_orig_system := c_Budget_Revision_Rec.approval_orig_system;
775      g_approval_override_by := c_Budget_Revision_Rec.approval_override_by;
776      g_currency_code        := c_Budget_Revision_Rec.currency_code;
777      g_gl_budget_set_id     := c_Budget_Revision_Rec.gl_budget_set_id;
778      g_global_budget_revision_id := c_Budget_Revision_Rec.global_budget_revision_id;
779      g_hr_budget_id         := c_Budget_Revision_Rec.hr_budget_id;
780    end loop;
781 
782    if g_global_budget_revision_id is null then
783      g_global_revision := FND_API.G_TRUE;
784    else
785      g_global_revision := FND_API.G_FALSE;
786    end if;
787 
788    for c_BG_Rec in c_BG loop
789      g_root_budget_group_id := c_BG_Rec.root_budget_group_id;
790      g_set_of_books_id      := c_BG_Rec.set_of_books_id;
791      g_business_group_id    := c_BG_Rec.business_group_id;
792      g_budget_group_name    := c_BG_Rec.name;
793    End loop;
794 
795    for c_gl_dates_rec in c_gl_dates Loop
796      if g_from_gl_period_name = g_to_gl_period_name then
797        g_from_date := c_gl_dates_rec.start_date;
798        g_to_date := c_gl_dates_rec.end_date;
799      else
800      begin
801 
802        if c_gl_dates_rec.period_name = g_from_gl_period_name then
803          g_from_date := c_gl_dates_rec.start_date;
804        elsif c_gl_dates_rec.period_name = g_to_gl_period_name then
805         g_to_date := c_gl_dates_rec.end_date;
806        end if;
807 
808      end;
809      end if;
810    end loop;
811 
812    if (g_from_date is null or g_to_date is null) then
813    begin
814 
815      for c_gl_dates_rec in c_gl_dates_br loop
816        g_from_date := c_gl_dates_rec.start_date;
817        g_to_date := c_gl_dates_rec.end_date;
818      end loop;
819 
820    end;
821    end if;
822 
823    if (g_effective_start_date is null or g_effective_end_date is null) then
824    begin
825 
826      for c_position_dates_rec in c_position_dates_br loop
827        g_effective_start_date := c_position_dates_rec.start_date;
828        g_effective_end_date := c_position_dates_rec.end_date;
829      end loop;
830 
831    end;
832    else
833      g_position_mass_revision := TRUE;
834    end if;
835 
836    if g_revise_by_position = 'Y' then
837    begin
838 
839      g_data_extract_id := Find_System_Data_Extract(g_budget_group_id);
840 
841      for c_position_exists_rec in c_position_exists loop
842        g_position_exists := TRUE;
843      end loop;
844 
845      if g_position_exists then
846      begin
847 
848        PSB_WS_POS1.Cache_Named_Attributes
849           (p_return_status => l_return_status,
850            p_business_group_id => g_business_group_id);
851 
852        if l_return_status <> FND_API.G_RET_STS_SUCCESS then
853          raise FND_API.G_EXC_ERROR;
854        end if;
855 
856      end;
857      end if;
858 
859    end;
860    end if;
861 
862    for c_Sob_Rec in c_Sob loop
863      g_func_currency := c_Sob_Rec.currency_code;
864      g_flex_code             := c_Sob_Rec.chart_of_accounts_id;
865      g_set_of_books_name     := c_Sob_Rec.name;
866      g_budgetary_control     := c_Sob_Rec.enable_budgetary_control_flag;
867    end loop;
868 
869   FND_PROFILE.GET(name => 'PSB_CREATE_ZERO_BALANCE_ACCT',
870                   val => g_create_zero_bal);
871 
872   if g_create_zero_bal is null then
873     g_create_zero_bal := 'Y';
874   end if;
875 
876   g_gl_journal_source := 'Budget Journal';
877   g_gl_journal_category := 'Budget';
878 
879   for c_constraint_set_rec in c_constraint_set loop
880     g_constraint_set_name := c_constraint_set_rec.name;
881     g_constraint_threshold := c_constraint_set_rec.constraint_threshold;
882   end loop;
883 
884   g_flex_delimiter := FND_FLEX_EXT.Get_Delimiter
885                          (application_short_name => 'SQLGL',
886                           key_flex_code => 'GL#',
887                           structure_number => g_flex_code);
888 
889   p_return_status := FND_API.G_RET_STS_SUCCESS;
890 
891 EXCEPTION
892 
893    when OTHERS then
894      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
895 
896 End Cache_Revision_Variables;
897 
898 /*===========================================================================+
899  |                      PROCEDURE Create_Revision_Accounts                   |
900  +===========================================================================*/
901 PROCEDURE Create_Revision_Accounts
902 ( p_api_version                  IN      NUMBER,
903   p_init_msg_list                IN      VARCHAR2 := FND_API.G_FALSE,
904   p_commit                       IN      VARCHAR2 := FND_API.G_FALSE,
905   p_validation_level             IN      NUMBER   := FND_API.G_VALID_LEVEL_FULL,
906   p_return_status                OUT  NOCOPY  VARCHAR2,
907   p_msg_count                    OUT  NOCOPY  NUMBER,
908   p_msg_data                     OUT  NOCOPY  VARCHAR2,
909   p_budget_revision_id           IN      NUMBER ,
910   p_budget_revision_acct_line_id IN OUT NOCOPY  NUMBER,
911   p_code_combination_id          IN      NUMBER,
912   p_budget_group_id              IN      NUMBER,
913   p_position_id                  IN      NUMBER := FND_API.G_MISS_NUM,
914   p_gl_period_name               IN      VARCHAR2,
915   p_gl_budget_version_id         IN      NUMBER := FND_API.G_MISS_NUM,
916   p_currency_code                IN      VARCHAR2,
917   p_budget_balance               IN      NUMBER,
918   p_revision_type                IN      VARCHAR2,
919   p_revision_value_type          IN      VARCHAR2,
920   p_revision_amount              IN      NUMBER,
921   p_funds_status_code            IN      VARCHAR2,
922   p_funds_result_code            IN      VARCHAR2,
923   p_funds_control_timestamp      IN      DATE,
924   p_note_id                      IN      NUMBER,
925   p_freeze_flag                  IN      VARCHAR2,
926   p_view_line_flag               IN      VARCHAR2,
927   p_functional_transaction       IN      VARCHAR2 := NULL
928 )
929 IS
930   --
931   l_api_name              CONSTANT  VARCHAR2(30) := 'Create_Revision_Accounts';
932   l_api_version           CONSTANT  NUMBER       := 1.0;
933   l_return_status                   VARCHAR2(1);
934   l_msg_count                       NUMBER;
935   l_msg_data                        VARCHAR2(2000);
936   --
937   l_account_type                    VARCHAR2(1);
938   l_template_id                     NUMBER;
939   l_budget_revision_acct_line_id    NUMBER;
940   l_budget_version_id               NUMBER;
941   l_start_date                      DATE;
942   l_end_date                        DATE;
943   l_global_revision_id              NUMBER;
944   l_concat_segments                 VARCHAR2(2000);
945   sql_bra                           VARCHAR2(2000);
946   --
947   TYPE l_sql_bra_cursor_type IS REF CURSOR;
948   l_sql_bra_csr                     l_sql_bra_cursor_type;
949   l_first_br_al_id                  NUMBER;
950   --
951   CURSOR l_gl_dates_csr
952   IS
953   SELECT start_date, end_date
954   FROM   gl_period_statuses
955   WHERE  application_id  = 101
956   AND    set_of_books_id = g_set_of_books_id
957   AND    period_name     = p_gl_period_name;
958   --
959 BEGIN
960   -- Standard call to check for call compatibility.
961   IF NOT FND_API.Compatible_API_Call (l_api_version,
962                                       p_api_version,
963                                       l_api_name,
964                                       G_PKG_NAME)
965   THEN
966     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
967   END IF;
968 
969   -- Initialize message list if p_init_msg_list is set to TRUE.
970   IF FND_API.to_Boolean (p_init_msg_list) THEN
971     FND_MSG_PUB.initialize;
972   END IF;
973 
974   Cache_Revision_Variables
975   ( p_return_status       => l_return_status,
976     p_budget_revision_id  => p_budget_revision_id ) ;
977   --
978   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
979     raise FND_API.G_EXC_ERROR;
980   end if;
981 
982   For l_gl_dates_rec in l_gl_dates_csr Loop
983     l_start_date := l_gl_dates_rec.start_date;
984     l_end_date   := l_gl_dates_rec.end_date;
985   End Loop;
986 
987   if nvl(p_gl_budget_version_id, FND_API.G_MISS_NUM) = FND_API.G_MISS_NUM then
988 
989     PSB_GL_BUDGET_PVT.Find_GL_Budget
990     ( p_api_version          => 1.0,
991       p_return_status        => l_return_status,
992       p_msg_count            => l_msg_count,
993       p_msg_data             => l_msg_data,
994       p_gl_budget_set_id     => g_gl_budget_set_id,
995       p_code_combination_id  => p_code_combination_id,
996       p_start_date           => l_start_date,
997       p_dual_posting_type    => 'A',
998       p_gl_budget_version_id => l_budget_version_id
999     ) ;
1000     --
1001     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1002       raise FND_API.G_EXC_ERROR;
1003     end if;
1004 
1005     if ( l_budget_version_id is null and g_permanent_revision = 'Y' ) then
1006 
1007       PSB_GL_BUDGET_PVT.Find_GL_Budget
1008       ( p_api_version          => 1.0,
1009         p_return_status        => l_return_status,
1010         p_msg_count            => l_msg_count,
1011         p_msg_data             => l_msg_data,
1012         p_gl_budget_set_id     => g_gl_budget_set_id,
1013         p_code_combination_id  => p_code_combination_id,
1014         p_start_date           => l_start_date,
1015         p_gl_budget_version_id => l_budget_version_id
1016       ) ;
1017       --
1018       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1019         raise FND_API.G_EXC_ERROR;
1020       end if;
1021       --
1022     end if;
1023     --
1024   else
1025     l_budget_version_id := p_gl_budget_version_id;
1026   end if;
1027 
1028   if l_budget_version_id is null then
1029     l_concat_segments := FND_FLEX_EXT.Get_Segs
1030                          ( application_short_name => 'SQLGL',
1031                            key_flex_code          => 'GL#',
1032                            structure_number       => g_flex_code,
1033                            combination_id         => p_code_combination_id ) ;
1034     message_token('ACCOUNT', l_concat_segments);
1035     add_message('PSB', 'PSB_CANNOT_ASSIGN_BUDGET');
1036     raise FND_API.G_EXC_ERROR;
1037   end if;
1038 
1039   IF ( p_budget_revision_acct_line_id is not null ) THEN
1040     l_budget_revision_acct_line_id := p_budget_revision_acct_line_id;
1041   ELSE
1042     --
1043 
1044     -- Bug#3340060: Refixed bug#3022417 using bind variables. Earlier fix was
1045     -- causing duplicate record creation.
1046     sql_bra := '' ||
1047     'select pbra.budget_revision_acct_line_id '||
1048     'from psb_budget_revision_accounts pbra, psb_budget_revision_lines pbrl ' ||
1049     'where pbra.code_combination_id = :code_combination_id ' ||
1050     'and pbra.gl_period_name = :gl_period_name ' ||
1051     'and NVL(pbra.currency_code, :gmc1) = NVL(:currency_code, :gmc2) ' ||
1052     'and NVL(pbra.gl_budget_version_id,:gmn1)=NVL(:budget_version_id,:gmn2) '||
1053     'and NVL(pbra.position_id, :gmn3) = NVL(:position_id, :gmn4) ' ||
1054     'and pbrl.budget_revision_id = :budget_revision_id ' ||
1055     'and pbrl.budget_revision_acct_line_id = pbra.budget_revision_acct_line_id';
1056 
1057     l_first_br_al_id := NULL;
1058     OPEN  l_sql_bra_csr FOR sql_bra
1059     USING p_code_combination_id, p_gl_period_name, FND_API.G_MISS_CHAR,
1060           p_currency_code, FND_API.G_MISS_CHAR, FND_API.G_MISS_NUM,
1061           l_budget_version_id, FND_API.G_MISS_NUM, FND_API.G_MISS_NUM,
1062           p_position_id, FND_API.G_MISS_NUM, p_budget_revision_id ;
1063     LOOP
1064       --
1065       FETCH l_sql_bra_csr INTO l_budget_revision_acct_line_id;
1066       EXIT WHEN l_sql_bra_csr%NOTFOUND;
1067 
1068       -- The is defensive cleanup code. If there exists duplicate rollup CCID
1069       -- due to prior issues, we will delete all but first one and update it
1070       -- in later code as is the normal flow of the code.
1071       IF l_first_br_al_id IS NULL THEN
1072         l_first_br_al_id := l_budget_revision_acct_line_id;
1073       ELSE
1074         --
1075         DELETE psb_budget_revision_lines
1076         WHERE  budget_revision_acct_line_id = l_budget_revision_acct_line_id;
1077         --
1078         DELETE psb_budget_revision_accounts
1079         WHERE  budget_revision_acct_line_id = l_budget_revision_acct_line_id;
1080         --
1081       END IF;
1082 
1083     END LOOP;
1084     CLOSE l_sql_bra_csr;
1085     l_budget_revision_acct_line_id := l_first_br_al_id ;
1086     -- Bug#3340060: End
1087 
1088     /* Commented as this code is replated by the above loop logic.
1089     sql_bra := 'select pbra.budget_revision_acct_line_id '||
1090     'from psb_budget_revision_accounts pbra, psb_budget_revision_lines pbrl ' ||
1091     'where pbra.code_combination_id = :code_combination_id ' ||
1092     'and pbra.gl_period_name = :gl_period_name ' ||
1093     'and nvl(pbra.currency_code, ''' || FND_API.G_MISS_CHAR ||
1094     ''') = nvl(:currency_code, ''' || FND_API.G_MISS_CHAR || ''') ' ||
1095     -- Start bug # 3022417
1096     'and nvl(pbra.gl_budget_version_id,-9999)=nvl(:budget_version_id,-9999) '||
1097     'and nvl(pbra.position_id, -9999) = nvl(:position_id, -9999) ' ||
1098     -- End bug # 3022417
1099     'and pbrl.budget_revision_id = :budget_revision_id ' ||
1100     'and pbrl.budget_revision_acct_line_id = pbra.budget_revision_acct_line_id';
1101     */
1102     --
1103   END IF;
1104 
1105   if l_budget_revision_acct_line_id is null then
1106   begin
1107 
1108     -- Create new entries for all cases other than revise projections
1109     GL_CODE_COMBINATIONS_PKG.Select_Columns
1110       (X_code_combination_id => p_code_combination_id,
1111        X_account_type        => l_account_type,
1112        X_template_id         => l_template_id);
1113 
1114     Insert into PSB_BUDGET_REVISION_ACCOUNTS (BUDGET_REVISION_ACCT_LINE_ID, CODE_COMBINATION_ID,
1115                 BUDGET_GROUP_ID, POSITION_ID, GL_PERIOD_NAME, GL_BUDGET_VERSION_ID, CURRENCY_CODE,
1116                 BUDGET_BALANCE, ACCOUNT_TYPE, REVISION_TYPE, REVISION_VALUE_TYPE,
1117                 REVISION_AMOUNT, functional_transaction,
1118                 FUNDS_CONTROL_STATUS_CODE, FUNDS_CONTROL_RESULTS_CODE,
1119                 NOTE_ID, FUNDS_CONTROL_TIMESTAMP, LAST_UPDATE_DATE, LAST_UPDATED_BY, LAST_UPDATE_LOGIN, CREATED_BY, CREATION_DATE)
1120          values (psb_budget_revision_accounts_s.nextval, p_code_combination_id,
1121                 p_budget_group_id, decode(p_position_id, FND_API.G_MISS_NUM, null, p_position_id), p_gl_period_name, l_budget_version_id, p_currency_code,
1122                 p_budget_balance, l_account_type, p_revision_type, p_revision_value_type,
1123                 decode(p_revision_value_type, 'A', Get_Rounded_Amount(p_currency_code, p_revision_amount), p_revision_amount), p_functional_transaction,
1124                 decode(p_funds_status_code, FND_API.G_MISS_CHAR, null, p_funds_status_code),
1125                 decode(p_funds_result_code, FND_API.G_MISS_CHAR, null, p_funds_result_code),
1126                 decode(p_note_id, FND_API.G_MISS_NUM, null, p_note_id), p_funds_control_timestamp,
1127                 sysdate, FND_GLOBAL.USER_ID, FND_GLOBAL.LOGIN_ID, FND_GLOBAL.USER_ID, sysdate)
1128     returning budget_revision_acct_line_id into l_budget_revision_acct_line_id;
1129 
1130     for c_Global_Rev_Rec in c_Global_Rev (p_budget_revision_id) loop
1131       l_global_revision_id := c_Global_Rev_Rec.global_revision_id;
1132     end loop;
1133 
1134     -- this is used to propagate new budget revision entries created at any level to all the distributed levels
1135 
1136     for c_Distribute_Rev_Rec in c_Distribute_Rev (l_global_revision_id, p_budget_group_id) loop
1137 
1138       INSERT INTO PSB_BUDGET_REVISION_LINES
1139                  (BUDGET_REVISION_ACCT_LINE_ID, BUDGET_REVISION_ID, FREEZE_FLAG,
1140                   VIEW_LINE_FLAG, LAST_UPDATE_DATE, LAST_UPDATED_BY, LAST_UPDATE_LOGIN, CREATED_BY,
1141                   CREATION_DATE)
1142          VALUES (l_budget_revision_acct_line_id, c_Distribute_Rev_Rec.budget_revision_id, p_freeze_flag,
1143                  p_view_line_flag, sysdate, FND_GLOBAL.USER_ID, FND_GLOBAL.LOGIN_ID, FND_GLOBAL.USER_ID,
1144                  sysdate);
1145 
1146     end loop;
1147 
1148   end;
1149   else
1150   begin
1151 
1152     Update PSB_BUDGET_REVISION_ACCOUNTS
1153        set code_combination_id = p_code_combination_id,
1154            budget_group_id = p_budget_group_id,
1155            gl_period_name = p_gl_period_name,
1156            budget_balance = p_budget_balance,
1157            revision_type = p_revision_type,
1158            revision_value_type = p_revision_value_type,
1159            revision_amount = decode(p_revision_value_type, 'A', Get_Rounded_Amount(p_currency_code, p_revision_amount), p_revision_amount),
1160            funds_control_status_code = decode(p_funds_status_code, FND_API.G_MISS_CHAR, funds_control_status_code, null, funds_control_status_code, p_funds_status_code),
1161            funds_control_results_code = decode(p_funds_result_code, FND_API.G_MISS_CHAR, funds_control_results_code, null, funds_control_results_code, p_funds_result_code),
1162            funds_control_timestamp = p_funds_control_timestamp,
1163            note_id = decode(p_note_id, FND_API.G_MISS_NUM, note_id, null, note_id, p_note_id),
1164            freeze_flag = p_freeze_flag,
1165            last_update_date = sysdate,
1166            last_updated_by = FND_GLOBAL.USER_ID,
1167            last_update_login = FND_GLOBAL.LOGIN_ID,
1168            currency_code = p_currency_code  -- Bug 3029168
1169      where budget_revision_acct_line_id = l_budget_revision_acct_line_id;
1170 
1171   end;
1172   end if;
1173 
1174   p_budget_revision_acct_line_id := l_budget_revision_acct_line_id;
1175 
1176    -- Initialize API return status to success
1177 
1178   p_return_status := FND_API.G_RET_STS_SUCCESS;
1179 
1180   -- Standard call to get message count and if count is 1, get message info.
1181 
1182   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
1183                              p_data  => p_msg_data);
1184 
1185 EXCEPTION
1186 
1187    when FND_API.G_EXC_ERROR then
1188      p_return_status := FND_API.G_RET_STS_ERROR;
1189 
1190      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
1191                                 p_data  => p_msg_data);
1192 
1193    when FND_API.G_EXC_UNEXPECTED_ERROR then
1194      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1195 
1196      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
1197                                 p_data  => p_msg_data);
1198 
1199    when OTHERS then
1200      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1201 
1202      if FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
1203        FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
1204                                 l_api_name);
1205      end if;
1206 
1207      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
1208                                 p_data  => p_msg_data);
1209 
1210 End Create_Revision_Accounts;
1211 
1212 /* ----------------------------------------------------------------------- */
1213 
1214 -- Check if parameter exists for account for this period
1215 
1216 FUNCTION AcctParam_Exists
1217 (p_parameter_id        NUMBER,
1218  p_budget_revision_id  NUMBER,
1219  p_period_name         VARCHAR2,
1220  p_local_parameter     VARCHAR2,
1221  p_ccid                NUMBER,
1222  p_ccid_start_period   DATE,
1223  p_ccid_end_period     DATE,
1224  p_period_start_date   DATE,
1225  p_period_end_date     DATE
1226 ) RETURN BOOLEAN IS
1227 
1228   l_parameter_exists   BOOLEAN := FALSE;
1229   l_account_exists     BOOLEAN := FALSE;
1230 
1231   cursor c_Exists is
1232     select 'Exists'
1233       from PSB_PARAMETER_ASSIGNMENTS_V a
1234      where p_local_parameter = 'N'
1235        and parameter_set_id = g_parameter_set_id
1236        and parameter_id = p_parameter_id
1237        and exists
1238           (select 1
1239              from PSB_SET_RELATIONS_V b,
1240                   PSB_BUDGET_ACCOUNTS c
1241             where b.account_or_position_type = 'A'
1242               and b.account_position_set_id = c.account_position_set_id
1243               and b.parameter_id = p_parameter_id
1244               and c.code_combination_id = p_ccid)
1245        and a.parameter_type = 'ACCOUNT'
1246        and (((a.effective_start_date <= nvl(p_ccid_end_period, p_period_end_date))
1247          and (a.effective_end_date is null))
1248          or ((a.effective_start_date between nvl(p_ccid_start_period, p_period_start_date) and nvl(p_ccid_end_period, p_period_end_date))
1249           or (a.effective_end_date between nvl(p_ccid_start_period, p_period_start_date) and nvl(p_ccid_end_period, p_period_end_date))
1250           or ((effective_start_date < nvl(p_ccid_start_period, p_period_start_date))
1251           and (effective_end_date > nvl(p_ccid_end_period, p_period_end_date)))))
1252        and (((a.effective_start_date <= p_period_end_date)
1253          and (a.effective_end_date is null))
1254          or ((a.effective_start_date between p_period_start_date and p_period_end_date)
1255           or (a.effective_end_date between p_period_start_date and p_period_end_date)
1256           or ((effective_start_date < p_period_start_date)
1257           and (effective_end_date > p_period_end_date))))
1258     UNION
1259     select 'Exists'
1260       from PSB_ENTITY a
1261      where p_local_parameter = 'Y'
1262        and entity_id = p_parameter_id
1263        and exists
1264           (select 1
1265              from PSB_SET_RELATIONS_V b,
1266                   PSB_BUDGET_ACCOUNTS c
1267             where b.account_or_position_type = 'A'
1268               and b.account_position_set_id = c.account_position_set_id
1269               and b.parameter_id = p_parameter_id
1270               and c.code_combination_id = p_ccid)
1271        and a.entity_subtype = 'ACCOUNT'
1272        and (((a.effective_start_date <= nvl(p_ccid_end_period, p_period_end_date))
1273          and (a.effective_end_date is null))
1274          or ((a.effective_start_date between nvl(p_ccid_start_period, p_period_start_date) and nvl(p_ccid_end_period, p_period_end_date))
1275           or (a.effective_end_date between nvl(p_ccid_start_period, p_period_start_date) and nvl(p_ccid_end_period, p_period_end_date))
1276           or ((effective_start_date < nvl(p_ccid_start_period, p_period_start_date))
1277           and (effective_end_date > nvl(p_ccid_end_period, p_period_end_date)))))
1278        and (((a.effective_start_date <= p_period_end_date)
1279          and (a.effective_end_date is null))
1280          or ((a.effective_start_date between p_period_start_date and p_period_end_date)
1281           or (a.effective_end_date between p_period_start_date and p_period_end_date)
1282           or ((effective_start_date < p_period_start_date)
1283           and (effective_end_date > p_period_end_date))));
1284 
1285   cursor c_AcctExists is
1286     select 'Exists'
1287       from psb_budget_revision_accounts a, psb_budget_revision_lines b
1288      where a.code_combination_id = p_ccid
1289        and a.gl_period_name = p_period_name
1290        and b.budget_revision_id = p_budget_revision_id
1291        and b.budget_revision_acct_line_id = a.budget_revision_acct_line_id;
1292 
1293 Begin
1294 
1295   for c_Exists_Rec in c_Exists loop
1296     l_parameter_exists := TRUE;
1297   end loop;
1298 
1299   if p_local_parameter = 'Y' then
1300   begin
1301 
1302     for c_AcctExists_Rec in c_AcctExists loop
1303       l_account_exists := TRUE;
1304     end loop;
1305 
1306     if not l_account_exists then
1307       l_parameter_exists := FALSE;
1308     end if;
1309 
1310   end;
1311   end if;
1312 
1313   return l_parameter_exists;
1314 
1315 END AcctParam_Exists;
1316 
1317 /*==========================================================================+
1318  |                       PROCEDURE Apply_Revision_Acct_Parameters           |
1319  +==========================================================================*/
1320 
1321 PROCEDURE Apply_Revision_Acct_Parameters
1322 ( p_api_version            IN   NUMBER,
1323   p_init_msg_list          IN   VARCHAR2 := FND_API.G_FALSE,
1324   p_validation_level       IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL,
1325   p_return_status          OUT  NOCOPY  VARCHAR2,
1326   p_parameter_id           IN   NUMBER,
1327   p_parameter_name         IN   VARCHAR2,
1328   p_compound_annually      IN   VARCHAR2,
1329   p_compound_factor        IN   NUMBER,
1330   p_original_budget        IN   NUMBER,
1331   p_current_budget         IN   NUMBER,
1332   p_revision_amount        OUT  NOCOPY  NUMBER
1333 ) IS
1334 
1335   l_return_status          VARCHAR2(1);
1336 
1337   cursor c_Formula is
1338     select step_number,
1339            prefix_operator,
1340            budget_year_type_id,
1341            balance_type,
1342            segment1, segment2, segment3,
1343            segment4, segment5, segment6,
1344            segment7, segment8, segment9,
1345            segment10, segment11, segment12,
1346            segment13, segment14, segment15,
1347            segment16, segment17, segment18,
1348            segment19, segment20, segment21,
1349            segment22, segment23, segment24,
1350            segment25, segment26, segment27,
1351            segment28, segment29, segment30,
1352            currency_code,
1353            nvl(amount, 0) amount,
1354            postfix_operator
1355       from PSB_PARAMETER_FORMULAS
1356      where parameter_id = p_parameter_id
1357      order by step_number;
1358 
1359   l_first_line          VARCHAR2(1) := FND_API.G_TRUE;
1360   l_first_time          VARCHAR2(1) := FND_API.G_TRUE;
1361 
1362   l_num_lines           NUMBER := 0;
1363   l_compound_total      NUMBER := 0;
1364 
1365   l_type4               VARCHAR2(1);
1366 
1367   l_line_total          NUMBER := 0;
1368   l_diff                NUMBER := 0;
1369   l_budget_amount       NUMBER;
1370   l_return_status       VARCHAR2(1);
1371   l_running_total       NUMBER := 0;
1372 
1373 Begin
1374 
1375   for c_Formula_Rec in c_Formula loop
1376    l_type4 := FND_API.G_FALSE;
1377    l_line_total := 0;
1378 
1379    l_num_lines := l_num_lines + 1;
1380 
1381     -- The prefix operator for the 1st Formula line must be '=';
1382     -- for the other Formula lines, the
1383     -- prefix operator can be '+', '-', '*', '/'
1384 
1385     if FND_API.to_Boolean(l_first_line) then
1386     begin
1387 
1388       l_first_line := FND_API.G_FALSE;
1389 
1390       if c_Formula_Rec.prefix_operator <> '=' then
1391         message_token('PARAMETER', p_parameter_name);
1392         message_token('STEPID', c_Formula_Rec.step_number);
1393         message_token('OPERATOR', '[=]');
1394         add_message('PSB', 'PSB_INVALID_PARAM_OPR');
1395         raise FND_API.G_EXC_ERROR;
1396       end if;
1397 
1398     end;
1399     else
1400     begin
1401 
1402       if c_Formula_Rec.prefix_operator not in ('+', '-', '*', '/') then
1403         message_token('PARAMETER', p_parameter_name);
1404         message_token('STEPID', c_Formula_Rec.step_number);
1405         message_token('OPERATOR', '[+, -, *, /]');
1406         add_message('PSB', 'PSB_INVALID_PARAM_OPR');
1407         raise FND_API.G_EXC_ERROR;
1408       end if;
1409     end;
1410     end if;
1411 
1412     -- Check Formula Type :
1413     if ((c_Formula_Rec.prefix_operator is not null) and
1414            (c_Formula_Rec.postfix_operator is not null) and
1415            (c_Formula_Rec.balance_type in ('O', 'C')) and
1416            (c_Formula_Rec.currency_code is not null) and
1417            (c_Formula_Rec.amount is not null) and
1418           ((c_Formula_Rec.budget_year_type_id is null) and
1419            (c_Formula_Rec.segment1 is null) and (c_Formula_Rec.segment2 is null) and (c_Formula_Rec.segment3 is null) and
1420            (c_Formula_Rec.segment4 is null) and (c_Formula_Rec.segment5 is null) and (c_Formula_Rec.segment6 is null) and
1421            (c_Formula_Rec.segment7 is null) and (c_Formula_Rec.segment8 is null) and (c_Formula_Rec.segment9 is null) and
1422            (c_Formula_Rec.segment10 is null) and (c_Formula_Rec.segment11 is null) and (c_Formula_Rec.segment12 is null) and
1423            (c_Formula_Rec.segment13 is null) and (c_Formula_Rec.segment14 is null) and (c_Formula_Rec.segment15 is null) and
1424            (c_Formula_Rec.segment16 is null) and (c_Formula_Rec.segment17 is null) and (c_Formula_Rec.segment18 is null) and
1425            (c_Formula_Rec.segment19 is null) and (c_Formula_Rec.segment20 is null) and (c_Formula_Rec.segment21 is null) and
1426            (c_Formula_Rec.segment22 is null) and (c_Formula_Rec.segment23 is null) and (c_Formula_Rec.segment24 is null) and
1427            (c_Formula_Rec.segment25 is null) and (c_Formula_Rec.segment26 is null) and (c_Formula_Rec.segment27 is null) and
1428            (c_Formula_Rec.segment28 is null) and (c_Formula_Rec.segment29 is null) and (c_Formula_Rec.segment30 is null))) then
1429     begin
1430       l_type4 := FND_API.G_TRUE;
1431     end;
1432     else
1433     begin
1434       message_token('PARAMETER', p_parameter_name);
1435       add_message('PSB', 'PSB_INVALID_PARAM_FORMULA');
1436       raise FND_API.G_EXC_ERROR;
1437     end;
1438     end if;
1439 
1440     if FND_API.to_Boolean(l_type4) then
1441     begin
1442 
1443       if (c_Formula_Rec.balance_type  = 'O') then
1444          l_budget_amount := p_original_budget;
1445       elsif (c_Formula_Rec.balance_type  = 'C') then
1446          l_budget_amount := p_current_budget;
1447       end if;
1448 
1449       if c_Formula_Rec.postfix_operator = '+' then
1450         l_line_total := l_budget_amount + c_Formula_Rec.amount;
1451       elsif c_Formula_Rec.postfix_operator = '-' then
1452         l_line_total := l_budget_amount - c_Formula_Rec.amount;
1453       elsif c_Formula_Rec.postfix_operator = '*' then
1454       begin
1455 
1456         l_line_total := l_budget_amount * c_Formula_Rec.amount;
1457 
1458         if FND_API.to_Boolean(p_compound_annually) then
1459           l_compound_total := l_budget_amount * POWER(c_Formula_Rec.amount, p_compound_factor);
1460         end if;
1461 
1462       end;
1463       elsif c_Formula_Rec.postfix_operator = '/' then
1464       begin
1465 
1466         -- Avoid a divide-by-zero error
1467 
1468         if c_Formula_Rec.amount = 0 then
1469           l_line_total := 0;
1470         else
1471           l_line_total := l_budget_amount / c_Formula_Rec.amount;
1472         end if;
1473 
1474       end;
1475       end if;
1476 
1477     end; /* For Budget Revisions only */
1478     end if;
1479 
1480 
1481     if c_Formula_Rec.prefix_operator = '=' then
1482       l_running_total := l_line_total;
1483     elsif c_Formula_Rec.prefix_operator = '+' then
1484       l_running_total := l_running_total + l_line_total;
1485     elsif c_Formula_Rec.prefix_operator = '-' then
1486       l_running_total := l_running_total - l_line_total;
1487     elsif c_Formula_Rec.prefix_operator = '*' then
1488       l_running_total := l_running_total * l_line_total;
1489     elsif c_Formula_Rec.prefix_operator = '/' then
1490     begin
1491 
1492       -- Avoid divide-by-zero error
1493 
1494       if l_line_total = 0 then
1495         l_running_total := 0;
1496       else
1497         l_running_total := l_running_total / l_line_total;
1498       end if;
1499 
1500     end;
1501     end if;
1502 
1503   End Loop; /*c_Formula*/
1504 
1505   if ((l_num_lines = 1) and
1506       (FND_API.to_boolean(l_type4)) and
1507       (FND_API.to_Boolean(p_compound_annually))) then
1508     l_running_total := l_compound_total;
1509   end if;
1510 
1511    /* The difference is always between the current budget and revised budget */
1512    p_revision_amount := l_running_total;
1513 
1514   -- Set API return status to success
1515 
1516   p_return_status := FND_API.G_RET_STS_SUCCESS;
1517 
1518 
1519 EXCEPTION
1520 
1521    when FND_API.G_EXC_ERROR then
1522      p_return_status := FND_API.G_RET_STS_ERROR;
1523 
1524    when FND_API.G_EXC_UNEXPECTED_ERROR then
1525      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1526 
1527    when OTHERS then
1528      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1529 
1530 End Apply_Revision_Acct_Parameters;
1531 
1532 /*==========================================================================+
1533  |                       PROCEDURE Create_Base_Budget_Revision              |
1534  +==========================================================================*/
1535 
1536 PROCEDURE Create_Base_Budget_Revision
1537 ( p_api_version         IN      NUMBER,
1538   p_init_msg_list       IN      VARCHAR2 := FND_API.G_FALSE,
1539   p_commit              IN      VARCHAR2 := FND_API.G_FALSE,
1540   p_validation_level    IN      NUMBER  := FND_API.G_VALID_LEVEL_FULL,
1541   p_return_status       OUT  NOCOPY     VARCHAR2,
1542   p_msg_count           OUT  NOCOPY     NUMBER,
1543   p_msg_data            OUT  NOCOPY     VARCHAR2,
1544   p_worksheet_id        IN      NUMBER,
1545   p_event_type          IN      VARCHAR2 DEFAULT 'BP'
1546 
1547 ) IS
1548 
1549   l_api_name                      CONSTANT VARCHAR2(30)   := 'CREATE_BASE_BUDGET_REVISION';
1550   l_api_version                   CONSTANT NUMBER         := 1.0;
1551 
1552   l_return_status                 VARCHAR2(1);
1553   l_msg_count                     NUMBER;
1554   l_msg_data                      VARCHAR2(2000);
1555 
1556   l_budget_group_id               NUMBER;
1557   l_budget_revision_id            NUMBER;
1558   l_budget_revision_exists        BOOLEAN := FALSE;
1559 
1560   l_budget_revision_acct_line_id  NUMBER;
1561 
1562   l_account_type                  VARCHAR2(1);
1563   l_template_id                   NUMBER;
1564   l_currency_code                 VARCHAR2(15);
1565 
1566   cursor c_bg is
1567     select nvl(b.root_budget_group_id, b.budget_group_id) budget_group_id
1568       from PSB_WORKSHEETS a, PSB_BUDGET_GROUPS b
1569      where a.worksheet_id = p_worksheet_id
1570        and b.budget_group_id = a.budget_group_id;
1571 
1572   -- Bug 3029168 added the join with currency code
1573   -- in the c_budrev_exists cursor
1574   cursor c_budrev_exists is
1575     select budget_revision_id
1576       from psb_budget_revisions
1577      where budget_group_id = l_budget_group_id
1578        and base_line_revision = 'Y'
1579        AND ((currency_code = 'STAT' AND p_event_type = 'SW')
1580         OR ((currency_code <> 'STAT' OR currency_code IS NULL) AND p_event_type = 'BP'));
1581 
1582   -- Shigva Start
1583   /*cursor c_lines is
1584     select aeh.ae_header_id, aeh.budget_version_id, aeh.period_name,
1585            ael.code_combination_id, ael.currency_code,
1586       sum(nvl(ael.entered_dr, 0) - nvl(ael.entered_cr, 0)) budget_balance
1587       from PSB_AE_LINES_ALL ael,
1588            PSB_AE_HEADERS_ALL aeh
1589      where ael.source_id = p_worksheet_id
1590        and ael.source_table = 'PSB_WORKSHEETS'
1591        and ael.actual_flag = 'B'
1592        and aeh.ae_header_id = ael.ae_header_id
1593   group by aeh.ae_header_id, aeh.budget_version_id, aeh.period_name, ael.code_combination_id, ael.currency_code;*/
1594 
1595   -- Bug#4310411 Start
1596   CURSOR c_lines
1597   IS
1598   SELECT pgi.worksheet_id,
1599          pgi.budget_version_id,
1600          pgi.period_name,
1601          pgi.code_combination_id,
1602          pgi.currency_code,
1603          SUM(NVL(pgi.entered_dr, 0) - NVL(pgi.entered_cr, 0)) budget_balance
1604   FROM psb_gl_interfaces pgi
1605   WHERE pgi.worksheet_id = p_worksheet_id
1606   AND pgi.actual_flag = 'B'
1607   AND pgi.budget_source_type = p_event_type -- Bug 3029168
1608   GROUP BY pgi.worksheet_id,
1609            pgi.budget_version_id,
1610            pgi.period_name,
1611            pgi.code_combination_id,
1612            pgi.currency_code;
1613   -- Bug#4310411 End
1614 BEGIN
1615 
1616   -- Standard Start of API savepoint
1617 
1618   SAVEPOINT     CREATE_BASE_BUDGET_REVISION;
1619 
1620   -- Standard call to check for call compatibility.
1621 
1622   if not FND_API.Compatible_API_Call (l_api_version,
1623                                       p_api_version,
1624                                       l_api_name,
1625                                       G_PKG_NAME)
1626   THEN
1627     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1628   END IF;
1629 
1630   -- Initialize message list if p_init_msg_list is set to TRUE.
1631 
1632   IF FND_API.to_Boolean (p_init_msg_list) THEN
1633     FND_MSG_PUB.initialize;
1634   END IF;
1635 
1636   /* Bug 3029168 Start */
1637   IF p_event_type = 'SW' THEN
1638     l_currency_code := 'STAT';
1639   END IF;
1640   /* Bug 3029168 End */
1641 
1642   for c_bg_rec in c_bg loop
1643     l_budget_group_id := c_bg_rec.budget_group_id;
1644   end loop;
1645 
1646   for c_budrev_exists_rec IN c_budrev_exists loop
1647     l_budget_revision_id := c_budrev_exists_rec.budget_revision_id;
1648     l_budget_revision_exists := TRUE;
1649   end loop;
1650 
1651   IF not l_budget_revision_exists THEN
1652   begin
1653 
1654     PSB_BUDGET_REVISIONS_PVT.Create_Budget_Revision
1655        (p_api_version                      => 1.0,
1656         p_return_status                    => l_return_status,
1657         p_msg_count                        => l_msg_count,
1658         p_msg_data                         => l_msg_data,
1659         p_budget_revision_id               => l_budget_revision_id,
1660         p_budget_group_id                  => l_budget_group_id,
1661         p_gl_budget_set_id                 => 0,
1662         p_justification                    => null,
1663         p_from_gl_period_name              => null,
1664         p_to_gl_period_name                => null,
1665         p_currency_code                    => l_currency_code, -- Bug 3029168
1666         p_effective_start_date             => null,
1667         p_effective_end_date               => null,
1668         p_budget_revision_type             => 'R',
1669         p_transaction_type                 => NULL,
1670         p_permanent_revision               => 'Y',
1671         p_global_budget_revision           => null,
1672         p_global_budget_revision_id        => null,
1673         p_requestor                        => FND_GLOBAL.USER_ID,
1674         p_parameter_set_id                 => null,
1675         p_constraint_set_id                => null,
1676         p_submission_date                  => null,
1677         p_submission_status                => null,
1678         p_approval_orig_system             => null,
1679         p_approval_override_by             => null,
1680         p_freeze_flag                      => null,
1681         p_base_line_revision               => 'Y',
1682         p_attribute1                       => null,
1683         p_attribute2                       => null,
1684         p_attribute3                       => null,
1685         p_attribute4                       => null,
1686         p_attribute5                       => null,
1687         p_attribute6                       => null,
1688         p_attribute7                       => null,
1689         p_attribute8                       => null,
1690         p_attribute9                       => null,
1691         p_attribute10                      => null,
1692         p_attribute11                      => null,
1693         p_attribute12                      => null,
1694         p_attribute13                      => null,
1695         p_attribute14                      => null,
1696         p_attribute15                      => null,
1697         p_attribute16                      => null,
1698         p_attribute17                      => null,
1699         p_attribute18                      => null,
1700         p_attribute19                      => null,
1701         p_attribute20                      => null,
1702         p_attribute21                      => null,
1703         p_attribute22                      => null,
1704         p_attribute23                      => null,
1705         p_attribute24                      => null,
1706         p_attribute25                      => null,
1707         p_attribute26                      => null,
1708         p_attribute27                      => null,
1709         p_attribute28                      => null,
1710         p_attribute29                      => null,
1711         p_attribute30                      => null,
1712         p_context                          => null);
1713 
1714     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1715       RAISE FND_API.G_EXC_ERROR;
1716     END IF;
1717 
1718   end;
1719   END IF;
1720 
1721   for c_lines_rec in c_lines loop
1722 
1723     l_budget_revision_acct_line_id := NULL;
1724 
1725     GL_CODE_COMBINATIONS_PKG.Select_Columns
1726      (X_code_combination_id => c_lines_rec.code_combination_id,
1727       X_account_type        => l_account_type,
1728       X_template_id         => l_template_id);
1729 
1730     PSB_BUDGET_REVISIONS_PVT.Create_Revision_Accounts
1731       (p_api_version                    => 1.0,
1732        p_return_status                   => l_return_status,
1733        p_msg_count                       => l_msg_count,
1734        p_msg_data                        => l_msg_data,
1735        p_budget_revision_acct_line_id    => l_budget_revision_acct_line_id,
1736        p_budget_revision_id              => l_budget_revision_id,
1737        p_code_combination_id             => c_lines_rec.code_combination_id,
1738        p_budget_group_id                 => l_budget_group_id,
1739        p_gl_period_name                  => c_lines_rec.period_name,
1740        p_gl_budget_version_id            => c_lines_rec.budget_version_id,
1741        p_currency_code                   => c_lines_rec.currency_code,
1742        p_budget_balance                  => c_lines_rec.budget_balance,
1743        p_revision_type                   => null,
1744        p_revision_value_type             => null,
1745        p_revision_amount                 => null,
1746        p_funds_status_code               => null,
1747        p_funds_result_code               => null,
1748        p_funds_control_timestamp         => null,
1749        p_note_id                         => null,
1750        p_freeze_flag                     => 'N',
1751        p_view_line_flag                  => 'Y');
1752 
1753     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1754       RAISE FND_API.G_EXC_ERROR;
1755     END IF;
1756 
1757   end loop;
1758 
1759   -- Initialize API return status to success
1760 
1761   p_return_status := FND_API.G_RET_STS_SUCCESS;
1762 
1763   -- Standard check of p_commit.
1764 
1765   IF FND_API.to_Boolean (p_commit) THEN
1766     commit work;
1767   END IF;
1768 
1769   -- Standard call to get message count and if count is 1, get message info.
1770 
1771   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
1772                              p_data  => p_msg_data);
1773 
1774 
1775 EXCEPTION
1776 
1777    WHEN FND_API.G_EXC_ERROR THEN
1778      rollback to CREATE_BASE_BUDGET_REVISIONS;
1779      p_return_status := FND_API.G_RET_STS_ERROR;
1780 
1781      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
1782                                 p_data  => p_msg_data);
1783 
1784    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1785      rollback to CREATE_BASE_BUDGET_REVISIONS;
1786      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1787 
1788      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
1789                                 p_data  => p_msg_data);
1790 
1791    WHEN OTHERS THEN
1792      rollback to CREATE_BASE_BUDGET_REVISIONS;
1793      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1794 
1795      if FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
1796        FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
1797                                 l_api_name);
1798      END IF;
1799 
1800      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
1801                                 p_data  => p_msg_data);
1802 
1803 END Create_Base_Budget_Revision;
1804 
1805 /*==========================================================================+
1806  |                       FUNCTION Find_Original_Budget_Balance              |
1807  +==========================================================================*/
1808 
1809 /* The original budget for a ccid is obtained from the base line budget revision */
1810 
1811 Function Find_Original_Budget_Balance
1812  (p_code_combination_id    IN      NUMBER,
1813   p_budget_group_id        IN      NUMBER,
1814   p_gl_period              IN      VARCHAR2,
1815   p_gl_budget_version_id   IN      NUMBER,
1816   p_set_of_books_id        IN      NUMBER := FND_API.G_MISS_NUM,
1817   p_end_gl_period          IN      VARCHAR2 := FND_API.G_MISS_CHAR,
1818   p_currency_code          IN      VARCHAR2 DEFAULT NULL
1819 ) RETURN NUMBER IS
1820 
1821 l_budget_group_id              NUMBER;
1822 l_from_date                    DATE;
1823 l_to_date                      DATE;
1824 l_gl_period_name               VARCHAR2(15);
1825 
1826 l_original_budget_balance      NUMBER := 0;
1827 l_sum_budget_balance           NUMBER := 0;
1828 
1829 /* start bug 3687997 */
1830 l_account_type                 VARCHAR2(1);
1831 /* end bug 3687997 */
1832 
1833 cursor c_original_budget is
1834   Select budget_balance
1835     from psb_budget_revision_accounts pbra,
1836          psb_budget_revision_lines    pbrl,
1837          psb_budget_revisions         pbr
1838    where pbra.code_combination_id = p_code_combination_id
1839      and pbra.gl_period_name = p_gl_period
1840      and pbra.gl_budget_version_id = p_gl_budget_version_id
1841      and pbra.budget_revision_acct_line_id = pbrl.budget_revision_acct_line_id
1842      and pbra.position_id is null
1843      and pbrl.budget_revision_id = pbr.budget_revision_id
1844      and pbr.budget_group_id = l_budget_group_id
1845      and pbr.base_line_revision = 'Y'
1846      and NVL(pbr.currency_code,p_currency_code) = p_currency_code
1847      and NVL(pbra.currency_code,p_currency_code)
1848        = NVL(pbr.currency_code,p_currency_code);  -- Bug 3029168
1849 
1850 Cursor c_original_budget_sum is
1851   Select sum(budget_balance) sum_budget_balance
1852     from psb_budget_revision_accounts pbra,
1853          psb_budget_revision_lines    pbrl,
1854          psb_budget_revisions         pbr
1855    where pbra.code_combination_id = p_code_combination_id
1856      and pbra.gl_budget_version_id = p_gl_budget_version_id
1857      and pbra.position_id is null
1858      and pbra.budget_revision_acct_line_id = pbrl.budget_revision_acct_line_id
1859      and pbrl.budget_revision_id = pbr.budget_revision_id
1860      and pbr.budget_group_id = l_budget_group_id
1861      and pbr.base_line_revision = 'Y'
1862      and NVL(pbr.currency_code,p_currency_code) = p_currency_code
1863      and NVL(pbra.currency_code,p_currency_code)
1864        = NVL(pbr.currency_code,p_currency_code)   -- Bug 3029168
1865      and pbra.gl_period_name in
1866            (select period_name
1867             from gl_period_statuses
1868            where application_id = 101
1869              and set_of_books_id = p_set_of_books_id
1870              and start_date between l_from_date and l_to_date
1871              and end_date between l_from_date and l_to_date);
1872 
1873 Cursor c_gl_dates is
1874   Select period_name, start_date,end_date
1875     from gl_period_statuses
1876    where application_id = 101
1877      and set_of_books_id = p_set_of_books_id
1878      and period_name in (p_gl_period, p_end_gl_period);
1879 
1880 Cursor c_Root_Budget_Group is
1881   Select nvl(root_budget_group_id,budget_group_id) root_budget_group_id
1882     from psb_budget_groups_v
1883    where budget_group_id = p_budget_group_id;
1884 
1885 Begin
1886 
1887  For c_Root_Budget_Group_Rec in c_Root_Budget_Group Loop
1888    l_budget_group_id := c_Root_Budget_Group_Rec.root_budget_group_id;
1889  End Loop;
1890 
1891  /* Start Bug 3687997 */
1892  -- Fetch the account type from Gl_Code_Combinations table.
1893  FOR l_account_type_csr IN
1894      (SELECT account_type
1895       FROM gl_code_combinations
1896       WHERE code_combination_id = p_code_combination_id)
1897  LOOP
1898    l_account_type := l_account_type_csr.account_type;
1899  END LOOP;
1900  /* end bug 3687997 */
1901 
1902 
1903  if (p_end_gl_period = FND_API.G_MISS_CHAR) then
1904  begin
1905 
1906   For c_original_budget_rec in c_original_budget Loop
1907     l_original_budget_balance := c_original_budget_rec.budget_balance;
1908   End Loop;
1909 
1910   /* start bug 3687997 */
1911   IF l_account_type in ('A','D','E') THEN
1912     RETURN(l_original_budget_balance);
1913   ELSE
1914     RETURN(-1 * l_original_budget_balance);
1915   END IF;
1916   /* end bug 3687997 */
1917 
1918  end;
1919  else
1920 
1921    for c_gl_dates_rec in c_gl_dates Loop
1922      if p_gl_period = p_end_gl_period then
1923        l_from_date := c_gl_dates_rec.start_date;
1924        l_to_date := c_gl_dates_rec.end_date;
1925      else
1926      begin
1927 
1928        if c_gl_dates_rec.period_name = p_gl_period then
1929          l_from_date := c_gl_dates_rec.start_date;
1930        elsif c_gl_dates_rec.period_name = p_end_gl_period then
1931          l_to_date := c_gl_dates_rec.end_date;
1932        end if;
1933 
1934      end;
1935      end if;
1936    End Loop;
1937 
1938    For c_original_sum_budget_rec in c_original_budget_sum Loop
1939     l_sum_budget_balance := c_original_sum_budget_rec.sum_budget_balance;
1940    End Loop;
1941 
1942   /* start bug 3687997 */
1943   IF l_account_type In ('A', 'D', 'E') THEN
1944     RETURN(l_sum_budget_balance);
1945   ELSE
1946     RETURN(-1 * l_sum_budget_balance);
1947   END IF;
1948   /* end bug 3687997 */
1949 
1950  end if;
1951 
1952 End Find_original_budget_balance;
1953 
1954 /*==========================================================================+
1955  |                       PROCEDURE Insert_Into_GL_BCP                       |
1956  +==========================================================================*/
1957 PROCEDURE Insert_Into_GL_BCP
1958 (x_return_status       OUT NOCOPY VARCHAR2,
1959  p_packet_id           IN         NUMBER,
1960  p_budget_revision_id  IN         NUMBER,
1961  p_code_combination_id IN         Number_Tbl_Type,
1962  p_account_type        IN         Char_Tbl_Type,
1963  p_period_name         IN         Char_Tbl_Type,
1964  p_period_year         IN         Number_Tbl_Type,
1965  p_period_num          IN         Number_Tbl_Type,
1966  p_quarter_num         IN         Char_Tbl_Type,
1967  p_currency_code       IN         Char_Tbl_Type,
1968  p_status_code         IN         Char_Tbl_Type,
1969  p_budget_version_id   IN         Number_Tbl_Type,
1970  p_entered_dr          IN         Number_Tbl_Type,
1971  p_entered_cr          IN         Number_Tbl_Type,
1972  p_accounted_dr        IN         Number_Tbl_Type,
1973  p_accounted_cr        IN         Number_Tbl_Type,
1974  p_reference1          IN         Char_Tbl_Type
1975 )
1976 IS
1977   l_session_id     NUMBER(38);
1978   l_serial_id      NUMBER(38);
1979   PRAGMA autonomous_transaction;
1980 
1981 BEGIN
1982 
1983   -- bug 4589283 added the below clause
1984   SELECT s.sid, s.serial#
1985     INTO l_session_id,
1986          l_serial_id
1987     FROM v$session s,v$process p
1988    WHERE s.paddr = p.addr
1989      AND audsid  = USERENV('SESSIONID');
1990 
1991   --++ Bulk insert into GL_BC_PACKETS
1992   FORALL l_indx IN 1..p_budget_version_id.COUNT
1993     INSERT INTO GL_BC_PACKETS
1994     (packet_id,
1995      ledger_id,
1996      je_source_name,
1997      je_category_name,
1998      code_combination_id,
1999      account_type,
2000      actual_flag,
2001      period_name,
2002      period_year,
2003      period_num,
2004      quarter_num,
2005      currency_code,
2006      status_code,
2007      last_update_date,
2008      last_updated_by,
2009      budget_version_id,
2010      entered_dr,
2011      entered_cr,
2012      accounted_dr,
2013      accounted_cr,
2014      reference1,
2015      reference2,
2016      application_id, -- Bug 4589283 added the below columns
2017      session_id,
2018      serial_id
2019     )
2020     VALUES
2021     (p_packet_id,
2022      g_set_of_books_id,
2023      g_gl_journal_source,
2024      g_gl_journal_category,
2025      p_code_combination_id(l_indx),
2026      p_account_type(l_indx),
2027      'B',
2028      p_period_name(l_indx),
2029      p_period_year(l_indx),
2030      p_period_num(l_indx),
2031      p_quarter_num(l_indx),
2032      p_currency_code(l_indx),
2033      p_status_code(l_indx),
2034      SYSDATE,
2035      FND_GLOBAL.USER_ID,
2036      p_budget_version_id(l_indx),
2037      p_entered_dr(l_indx),
2038      p_entered_cr(l_indx),
2039      p_accounted_dr(l_indx),
2040      p_accounted_cr(l_indx),
2041      p_reference1(l_indx),
2042      p_budget_revision_id,
2043      8401,      --Bug 4589283 added the below columns
2044      l_session_id,
2045      l_serial_id
2046     );
2047 
2048   COMMIT;
2049 
2050   x_return_status := FND_API.G_RET_STS_SUCCESS;
2051 
2052 EXCEPTION
2053   WHEN FND_API.G_EXC_ERROR THEN
2054     x_return_status := FND_API.G_RET_STS_ERROR;
2055    /* FND_MSG_PUB.Count_And_Get(p_count => p_msg_count, p_data => p_msg_data
2056                              );*/
2057 
2058   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2059     ROLLBACK TO Budget_Revision_Funds_Check;
2060     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2061 /*    FND_MSG_PUB.Count_And_Get (p_count => p_msg_count, p_data  => p_msg_data);*/
2062 
2063   WHEN OTHERS THEN
2064     ROLLBACK TO Budget_Revision_Funds_Check;
2065     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2066 /*    FND_MSG_PUB.Count_And_Get(p_count => p_msg_count, p_data  => p_msg_data);
2067 
2068     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
2069       FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME, l_api_name);
2070     END IF;*/
2071 END;
2072 
2073 /*==========================================================================+
2074  |                       PROCEDURE Budget_Revision_Funds_Check              |
2075  +==========================================================================*/
2076 
2077 PROCEDURE Budget_Revision_Funds_Check
2078 ( p_api_version            IN   NUMBER,
2079   p_init_msg_list          IN   VARCHAR2 := FND_API.G_FALSE,
2080   p_commit                 IN   VARCHAR2 := FND_API.G_FALSE,
2081   p_validation_level       IN   NUMBER := FND_API.G_VALID_LEVEL_FULL,
2082   p_return_status          OUT  NOCOPY  VARCHAR2,
2083   p_msg_count              OUT  NOCOPY  NUMBER,
2084   p_msg_data               OUT  NOCOPY  VARCHAR2,
2085   p_budget_revision_id     IN   NUMBER,
2086   p_funds_reserve_flag     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
2087   p_fund_check_failures    OUT  NOCOPY  NUMBER,
2088   p_called_from            IN   VARCHAR2 DEFAULT 'PSBBGRVS' -- Bug#4310411
2089 ) IS
2090 
2091   l_api_name               CONSTANT VARCHAR2(30)   := 'Budget_Revision_Funds_Check';
2092   l_api_version            CONSTANT NUMBER   := 1.0;
2093 
2094   l_return_status        VARCHAR2(1);
2095   l_msg_count            NUMBER;
2096   l_msg_data             VARCHAR2(2000);
2097   l_packet_id            NUMBER;
2098   l_account_type         VARCHAR2(1);
2099   l_template_id          NUMBER;
2100   l_entered_dr           NUMBER;
2101   l_entered_cr           NUMBER;
2102 
2103   l_budget_balance         NUMBER;
2104   l_budget_revision_acct_line_id NUMBER;
2105 
2106   l_sql_code varchar2(1000);
2107   l_sql_errm varchar2(1000);
2108 
2109   l_status_code          VARCHAR2(1);
2110   l_mode                 VARCHAR2(1);
2111 
2112   -- Bug#4310411 Start
2113   l_code_combination_id_tab Number_Tbl_Type;
2114   l_account_type_tab        Char_Tbl_Type;
2115   l_period_name_tab         Char_Tbl_Type;
2116   l_period_year_tab         Number_Tbl_Type;
2117   l_period_num_tab          Number_Tbl_Type;
2118   l_quarter_num_tab         Char_Tbl_Type;
2119   l_currency_code_tab       Char_Tbl_Type;
2120   l_status_code_tab         Char_Tbl_Type;
2121   l_budget_version_id_tab   Number_Tbl_Type;
2122   l_entered_dr_tab          Number_Tbl_Type;
2123   l_entered_cr_tab          Number_Tbl_Type;
2124   l_accounted_dr_tab        Number_Tbl_Type;
2125   l_accounted_cr_tab        Number_Tbl_Type;
2126   l_reference1_tab          Char_Tbl_Type;
2127 
2128   TYPE l_rev_lines_rec IS RECORD
2129   (budget_revision_acct_line_id NUMBER(20),
2130    budget_revision_id           NUMBER(20),
2131    code_combination_id          NUMBER(20),
2132    gl_period_name               VARCHAR2(15),
2133    period_year                  NUMBER(15),
2134    period_num                   NUMBER(15),
2135    quarter_num                  NUMBER(15),
2136    gl_budget_version_id         NUMBER,
2137    currency_code                VARCHAR2(15),
2138    budget_balance               NUMBER,
2139    revision_type                VARCHAR2(1),
2140    revision_value_type          VARCHAR2(1),
2141    revision_amount              NUMBER
2142   );
2143 
2144   TYPE l_rev_lines_tab IS TABLE OF l_rev_lines_rec;
2145   l_rev_lines_tab_inst l_rev_lines_tab := l_rev_lines_tab();
2146   -- Bug#4310411 End
2147 
2148 Cursor c_Fund_Balances is
2149   Select code_combination_id,
2150          budget_version_id,
2151          currency_code,
2152          period_name,
2153          result_code,
2154          status_code,
2155          reference1
2156     from GL_BC_PACKETS
2157    where packet_id = l_packet_id;
2158 
2159 Cursor c_Revision_Accounts is
2160   Select pbra.budget_revision_acct_line_id,
2161          pbra.budget_revision_id,
2162          pbra.code_combination_id,
2163          pbra.gl_period_name,
2164          gps.period_year,
2165          gps.period_num,
2166          gps.quarter_num,
2167          pbra.gl_budget_version_id,
2168          pbra.currency_code,
2169          pbra.budget_balance,
2170          pbra.revision_type,
2171          pbra.revision_value_type,
2172          pbra.revision_amount
2173    from psb_budget_revision_accounts_v pbra,
2174         GL_PERIOD_STATUSES gps
2175   where budget_revision_id = p_budget_revision_id
2176     and gps.application_id = 101
2177     and gps.set_of_books_id = g_set_of_books_id
2178     and gps.period_name = pbra.gl_period_name;
2179 
2180 CURSOR c_Seq IS
2181    select gl_bc_packets_s.nextval seq
2182      from dual;
2183 
2184 Begin
2185 
2186   -- Standard Start of API savepoint
2187   SAVEPOINT Budget_Revision_Funds_Check;
2188 
2189   -- Standard call to check for call compatibility.
2190   IF NOT FND_API.Compatible_API_Call (l_api_version,
2191                                       p_api_version,
2192                                       l_api_name,
2193                                       G_PKG_NAME)
2194   THEN
2195     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2196   END IF;
2197 
2198   -- Initialize message list if p_init_msg_list is set to TRUE.
2199   if FND_API.to_Boolean (p_init_msg_list) then
2200     FND_MSG_PUB.initialize;
2201   end if;
2202 
2203   -- Initialize global variables.
2204   Cache_Revision_Variables(p_budget_revision_id => p_budget_revision_id,
2205                            p_return_status => l_return_status);
2206 
2207   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
2208     raise FND_API.G_EXC_ERROR;
2209   end if;
2210   IF (g_budget_revision_type = 'C')
2211   AND g_currency_code <> 'STAT' THEN -- Bug 3029168
2212    /* Budget Rev Stub for CBC */
2213 
2214      PSB_COMMITMENTS_PVT.Commitment_Funds_Check
2215      ( p_api_version        => 1.0,
2216        p_init_msg_list      => FND_API.G_FALSE,
2217        p_commit             => FND_API.G_FALSE,
2218        p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
2219        p_return_status      => l_return_status,
2220        p_msg_count          => l_msg_count,
2221        p_msg_data           => l_msg_data,
2222        p_budget_revision_id => p_budget_revision_id);
2223 
2224 
2225     if (l_return_status <> FND_API.G_RET_STS_SUCCESS) then
2226       Savepoint Budget_Revision_Funds_Check;
2227       RAISE FND_API.G_EXC_ERROR;
2228     end if;
2229   else
2230   begin
2231 
2232     for c_seq_rec in c_seq loop
2233       l_packet_id := c_seq_rec.seq;
2234     end loop;
2235 
2236     -- Added IF statement for Bug:3681872
2237     if (fnd_global.conc_request_id <> -1) then
2238       FND_FILE.PUT_LINE(FND_FILE.LOG, 'Packet ID: ' || l_packet_id);
2239     end if;
2240 
2241     -- Bug#4310411 Start
2242 
2243     /* Bug 5148554 Made the following changes
2244        funds check will be called in check mode only */
2245     IF p_called_from = 'PSBBGRVS' OR p_called_from = 'PSBBR' THEN
2246       -- The Funds Checker/Freeze/Unfreeze/Validate program is called
2247       -- from Budget Revision form so we don't need to post them to GL.
2248       l_status_code := 'C';
2249       l_mode        := 'C'; -- Funds check only.
2250     END IF;
2251     /* ELSIF p_called_from = 'PSBBR' THEN
2252       -- The Funds Checker program is called from PSB_Submit_Revision_PVT
2253       -- as the part of Budget Revision Submission process, so we will
2254       -- check records and need to post them to GL also.
2255       l_status_code := 'P';
2256       l_mode        := 'R'; -- Funds Reservation check.
2257     END IF; */
2258 
2259     OPEN c_Revision_Accounts;
2260     FETCH c_Revision_Accounts BULK COLLECT INTO l_rev_lines_tab_inst;
2261     CLOSE c_Revision_Accounts;
2262 
2263     IF l_rev_lines_tab_inst.COUNT > 0 THEN
2264       FOR l_indx IN 1..l_rev_lines_tab_inst.COUNT LOOP
2265         l_entered_dr := NULL;
2266         l_entered_cr := NULL;
2267 
2268         GL_CODE_COMBINATIONS_PKG.Select_Columns
2269         (X_code_combination_id => l_rev_lines_tab_inst(l_indx).code_combination_id,
2270          X_account_type        => l_account_type,
2271          X_template_id         => l_template_id
2272         );
2273 
2274         IF (l_account_type IN ('A','E')) then
2275           BEGIN
2276             IF l_rev_lines_tab_inst(l_indx).revision_value_type = 'P' THEN
2277               IF l_rev_lines_tab_inst(l_indx).revision_type = 'I' THEN
2278                 l_entered_dr := l_rev_lines_tab_inst(l_indx).revision_amount * l_rev_lines_tab_inst(l_indx).budget_balance / 100;
2279               ELSE
2280                 l_entered_cr := l_rev_lines_tab_inst(l_indx).revision_amount * l_rev_lines_tab_inst(l_indx).budget_balance / 100;
2281               END IF;
2282             ELSE
2283               IF l_rev_lines_tab_inst(l_indx).revision_type = 'I' THEN
2284                 l_entered_dr := l_rev_lines_tab_inst(l_indx).revision_amount;
2285               ELSE
2286                 l_entered_cr := l_rev_lines_tab_inst(l_indx).revision_amount;
2287               END IF;
2288             END IF;
2289           END;
2290         ELSIF (l_account_type IN ('L','O','R')) THEN
2291           BEGIN
2292             IF l_rev_lines_tab_inst(l_indx).revision_value_type = 'P' THEN
2293               IF l_rev_lines_tab_inst(l_indx).revision_type = 'I' then
2294                 l_entered_cr := l_rev_lines_tab_inst(l_indx).revision_amount * l_rev_lines_tab_inst(l_indx).budget_balance / 100;
2295               ELSE
2296                 l_entered_dr := l_rev_lines_tab_inst(l_indx).revision_amount * l_rev_lines_tab_inst(l_indx).budget_balance / 100;
2297               END IF;
2298             ELSE
2299               IF l_rev_lines_tab_inst(l_indx).revision_type = 'I' THEN
2300                 l_entered_cr := l_rev_lines_tab_inst(l_indx).revision_amount;
2301               ELSE
2302                 l_entered_dr := l_rev_lines_tab_inst(l_indx).revision_amount;
2303               END IF;
2304             END IF;
2305           END;
2306         END IF;
2307 
2308         -- Now assign the values to the PL/SQL table.
2309         l_code_combination_id_tab(l_indx) := l_rev_lines_tab_inst(l_Indx).code_combination_id;
2310         l_account_type_tab(l_indx)        := l_account_type;
2311         l_period_name_tab(l_indx)         := l_rev_lines_tab_inst(l_Indx).gl_period_name;
2312         l_period_year_tab(l_indx)         := l_rev_lines_tab_inst(l_Indx).period_year;
2313         l_period_num_tab(l_indx)          := l_rev_lines_tab_inst(l_Indx).period_num;
2314         l_quarter_num_tab(l_indx)         := l_rev_lines_tab_inst(l_Indx).quarter_num;
2315         l_currency_code_tab(l_indx)       := l_rev_lines_tab_inst(l_Indx).currency_code;
2316         l_status_code_tab(l_indx)         := l_status_code;
2317         l_budget_version_id_tab(l_indx)   := l_rev_lines_tab_inst(l_Indx).gl_budget_version_id;
2318         l_entered_dr_tab(l_indx)          := l_entered_dr;
2319         l_entered_cr_tab(l_indx)          := l_entered_cr;
2320         l_accounted_dr_tab(l_indx)        := l_entered_dr;
2321         l_accounted_cr_tab(l_indx)        := l_entered_cr;
2322         l_reference1_tab(l_indx)
2323           := TO_NUMBER(l_rev_lines_tab_inst(l_Indx).budget_revision_acct_line_id);
2324       END LOOP;
2325     END IF;
2326 
2327     --Now call the procedue in autonomous transaction.
2328     Insert_Into_GL_BCP
2329     (x_return_status       => l_return_status,
2330      p_packet_id           => l_packet_id,
2331      p_budget_revision_id  => p_budget_revision_id,
2332      p_code_combination_id => l_code_combination_id_tab,
2333      p_account_type        => l_account_type_tab,
2334      p_period_name         => l_period_name_tab,
2335      p_period_year         => l_period_year_tab,
2336      p_period_num          => l_period_num_tab,
2337      p_quarter_num         => l_quarter_num_tab,
2338      p_currency_code       => l_currency_code_tab,
2339      p_status_code         => l_status_code_tab,
2340      p_budget_version_id   => l_budget_version_id_tab,
2341      p_entered_dr          => l_entered_dr_tab,
2342      p_entered_cr          => l_entered_cr_tab,
2343      p_accounted_dr        => l_accounted_dr_tab,
2344      p_accounted_cr        => l_accounted_cr_tab,
2345      p_reference1          => l_reference1_tab
2346     );
2347 
2348     IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
2349       Savepoint Budget_Revision_Funds_Check;
2350       RAISE FND_API.G_EXC_ERROR;
2351     END IF;
2352 
2353     -- Changed from R to P as PSA has removed the p_partial_resv_flag
2354     -- and added a new value 'P' to have the asme effect.
2355     -- To have the effect R and p_partial_resv_flag=Y, now
2356     -- we need to supply 'P'. For C, no need as Fund checked will
2357     -- always be performed with p_partial_resv_flag as Y.
2358     IF NOT PSA_FUNDS_CHECKER_PKG.GLXFCK
2359            (p_ledgerid    => g_set_of_books_id,
2360             p_packetid    => l_packet_id,
2361             p_mode        => l_mode,
2362             p_conc_flag   => 'N',
2363             p_return_code => l_return_status,
2364             p_calling_prog_flag => 'P' -- Bug 4589283
2365            )
2366     THEN
2367     -- Bug#4310411 End
2368       Savepoint Budget_Revision_Funds_Check;
2369       FND_MSG_PUB.Add;
2370       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2371    end if;
2372 
2373    p_fund_check_failures := 0;
2374 
2375    for c_Fund_Balances_rec in c_Fund_Balances loop
2376 
2377      /* start bug 4341619 */
2378      if ( (c_Fund_Balances_Rec.status_code = 'F') OR
2379           (c_Fund_Balances_Rec.status_code = 'T') OR
2380           (c_Fund_Balances_Rec.status_code = 'R') ) then
2381 
2382         p_fund_check_failures := p_fund_check_failures + 1;
2383 
2384      end if;
2385      /* end bug 4341619 */
2386      l_budget_balance
2387        := Get_GL_Balance
2388           (p_revision_type        => g_budget_revision_type,
2389            p_balance_type         => g_balance_type,
2390            p_set_of_books_id      => g_set_of_books_id,
2391            p_xbc_enabled_flag     => g_budgetary_control,
2392            p_gl_period_name       => c_Fund_Balances_Rec.period_name,
2393            p_gl_budget_version_id => c_Fund_Balances_rec.budget_version_id,
2394            p_currency_code        => c_Fund_Balances_Rec.currency_code,
2395            p_code_combination_id  => c_Fund_Balances_Rec.code_combination_id
2396           );
2397 
2398      UPDATE PSB_BUDGET_REVISION_ACCOUNTS
2399      SET budget_balance = l_budget_balance,
2400          funds_control_timestamp = sysdate,
2401          funds_control_status_code = c_Fund_Balances_Rec.status_code,
2402          funds_control_results_code = c_Fund_Balances_Rec.result_code
2403      WHERE budget_revision_acct_line_id = TO_NUMBER(c_Fund_Balances_Rec.reference1);
2404 
2405    end loop;
2406 
2407  end;
2408  end if;
2409 
2410  -- Initialize API return status to success
2411   p_return_status := FND_API.G_RET_STS_SUCCESS;
2412 
2413   -- Standard check of p_commit.
2414   if FND_API.to_Boolean (p_commit) then
2415     commit work;
2416   end if;
2417 
2418 EXCEPTION
2419    WHEN FND_API.G_EXC_ERROR THEN
2420      ROLLBACK TO Budget_Revision_Funds_Check;
2421      l_sql_code := sqlcode;
2422      l_sql_errm := sqlerrm;
2423      p_return_status := FND_API.G_RET_STS_ERROR;
2424      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
2425                                 p_data  => p_msg_data);
2426 
2427    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2428      l_sql_code := sqlcode;
2429      l_sql_errm := sqlerrm;
2430      ROLLBACK TO Budget_Revision_Funds_Check;
2431      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2432      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
2433                                 p_data  => p_msg_data);
2434 
2435    WHEN OTHERS THEN
2436      l_sql_code := sqlcode;
2437      l_sql_errm := sqlerrm;
2438      ROLLBACK TO Budget_Revision_Funds_Check;
2439      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2440 
2441      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
2442                                 p_data  => p_msg_data);
2443 
2444      if FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
2445        FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
2446                                 l_api_name);
2447      end if;
2448 
2449 End Budget_Revision_Funds_Check;
2450 
2451 /*==========================================================================+
2452  |                       FUNCTION Get_GL_Balance                            |
2453  +==========================================================================*/
2454 
2455 FUNCTION Get_GL_Balance
2456 (p_revision_type         IN    VARCHAR2,
2457  p_balance_type          IN    VARCHAR2,
2458  p_set_of_books_id       IN    NUMBER,
2459  p_xbc_enabled_flag      IN    VARCHAR2,
2460  p_gl_period_name        IN    VARCHAR2,
2461  p_gl_budget_version_id  IN    NUMBER,
2462  p_currency_code         IN    VARCHAR2,
2463  p_code_combination_id   IN    NUMBER)
2464 RETURN NUMBER IS
2465 
2466   /*For Bug No : 2925078 Start*/
2467   CURSOR C_Account_Type IS
2468     SELECT account_type
2469       FROM gl_code_combinations
2470      WHERE code_combination_id = p_code_combination_id;
2471   l_account_type    VARCHAR2(1);
2472   /*For Bug No : 2925078 End*/
2473 
2474   l_ccid_balance    NUMBER := 0;
2475 BEGIN
2476 
2477   if p_revision_type = 'R' then
2478     -- Bug 4474717
2479     -- Replaced parameter xset_of_books_id with
2480     -- xledger_id in the following call.
2481 
2482     l_ccid_balance := gl_budget_transfer_pkg.get_balance
2483      (balance_type         => p_balance_type,
2484       xledger_id           => p_set_of_books_id,
2485       xbc_enabled_flag     => p_xbc_enabled_flag,
2486       xperiod_name         => p_gl_period_name,
2487       xbudget_version_id   => p_gl_budget_version_id,
2488       xcurrency_code       => p_currency_code,
2489       code_combination_id  => p_code_combination_id);
2490 
2491       /*For Bug No : 2925078 Start*/
2492       FOR C_Account_Type_rec IN C_Account_Type LOOP
2493         l_account_type := C_Account_Type_Rec.account_type;
2494       END LOOP;
2495 
2496       /* For Bug No: 3687997 */
2497       if l_account_type IN ('A','D','E') then
2498         return(l_ccid_balance);
2499       else
2500         return(-1 * l_ccid_balance);
2501       end if;
2502       /*For Bug No : 2925078 End*/
2503 
2504   elsif p_revision_type = 'C' then
2505     return(0);
2506   end if;
2507 
2508 End Get_GL_Balance;
2509 
2510 /*==========================================================================+
2511  |                       FUNCTION Find_System_Data_Extract                  |
2512  +==========================================================================*/
2513 
2514 Function Find_System_Data_Extract
2515 ( p_budget_group_id        IN      NUMBER)
2516 RETURN NUMBER IS
2517 
2518   Cursor C_system_data_extract is
2519     Select data_extract_id
2520       from psb_data_extracts pde,
2521            psb_budget_groups pbg
2522      where system_data_extract = 'Y'
2523        and (((pde.budget_group_id = pbg.root_budget_group_id)
2524             and (pbg.budget_group_id = p_budget_group_id))
2525         or ((pde.budget_group_id = pbg.budget_group_id)
2526             and (pbg.budget_group_id = p_budget_group_id)
2527             and (pbg.root_budget_group_id is null)));
2528 
2529   l_system_data_extract_id number := NULL;
2530 
2531 Begin
2532 
2533    For C_system_data_extract_rec in C_system_data_extract Loop
2534     l_system_data_extract_id  := C_system_data_extract_rec.data_extract_id;
2535    End Loop;
2536 
2537    return(l_system_data_extract_id);
2538 
2539 End Find_System_Data_Extract;
2540 
2541 /*==========================================================================+
2542  |                       PROCEDURE Apply_Element_Parameters                 |
2543  +==========================================================================*/
2544 
2545 PROCEDURE Apply_Element_Parameters
2546 ( p_return_status          OUT  NOCOPY  VARCHAR2,
2547   p_budget_revision_id     IN   NUMBER,
2548   p_local_parameter        IN   VARCHAR2,
2549   p_parameter_id           IN   NUMBER,
2550   p_revision_start_date    IN   DATE,
2551   p_revision_end_date      IN   DATE) IS
2552 
2553   l_return_status          VARCHAR2(1);
2554   l_compound_annually      VARCHAR2(1);
2555   l_start_date             DATE;
2556   l_end_date               DATE;
2557 
2558   l_compound_factor        NUMBER;
2559   l_num_revision_years     NUMBER;
2560 
2561   cursor c_Parameter is
2562     select parameter_id,
2563            name,
2564            priority,
2565            parameter_autoinc_rule,
2566            parameter_compound_annually,
2567            currency_code,
2568            effective_start_date,
2569            effective_end_date
2570       from PSB_PARAMETER_ASSIGNMENTS_V
2571      where p_local_parameter = 'N'
2572        and data_extract_id = g_data_extract_id
2573        and parameter_type = 'ELEMENT'
2574        and (((effective_start_date <= p_revision_end_date)
2575          and (effective_end_date is null))
2576          or ((effective_start_date between p_revision_start_date and p_revision_end_date)
2577           or (effective_end_date between p_revision_start_date and p_revision_end_date)
2578          or ((effective_start_date < p_revision_start_date)
2579          and (effective_end_date > p_revision_end_date))))
2580        and parameter_set_id = g_parameter_set_id
2581      union
2582     select parameter_id,
2583            name,
2584            0 priority,
2585            parameter_autoinc_rule,
2586            parameter_compound_annually,
2587            currency_code,
2588            effective_start_date,
2589            effective_end_date
2590       from PSB_PARAMETERS_V
2591      where p_local_parameter = 'Y'
2592        and data_extract_id = g_data_extract_id
2593        and parameter_type = 'ELEMENT'
2594        and (((effective_start_date <= p_revision_end_date)
2595          and (effective_end_date is null))
2596        or ((effective_start_date between p_revision_start_date and p_revision_end_date)
2597           or (effective_end_date between p_revision_start_date and p_revision_end_date)
2598          or ((effective_start_date < p_revision_start_date)
2599          and (effective_end_date > p_revision_end_date))))
2600        and parameter_id = p_parameter_id
2601      order by effective_start_date, priority;
2602 
2603 BEGIN
2604 
2605   for c_parameter_rec in c_parameter loop
2606 
2607     -- Bug#4675858
2608     -- Set the global variable to TRUE.
2609     g_elem_projection := TRUE ;
2610     --
2611     if ((c_Parameter_Rec.parameter_compound_annually is null) or
2612         (c_Parameter_Rec.parameter_compound_annually = 'N')) then
2613     begin
2614 
2615       l_compound_annually := FND_API.G_FALSE;
2616 
2617       if ((c_Parameter_Rec.parameter_autoinc_rule is null) or
2618           (c_Parameter_Rec.parameter_autoinc_rule = 'N')) then
2619       begin
2620 
2621         PSB_WS_POS3.Process_ElemParam
2622               (p_return_status => l_return_status,
2623                p_worksheet_id => p_budget_revision_id,
2624                p_parameter_id => c_parameter_rec.parameter_id,
2625                p_currency_code => nvl(c_parameter_rec.currency_code, g_func_currency),
2626                p_start_date => greatest(p_revision_start_date, c_parameter_rec.effective_start_date),
2627                p_end_date => least(p_revision_end_date, nvl(c_parameter_rec.effective_end_date, p_revision_end_date)),
2628                p_compound_annually => l_compound_annually,
2629                p_compound_factor => l_compound_factor);
2630 
2631         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
2632           raise FND_API.G_EXC_ERROR;
2633         end if;
2634 
2635       end;
2636       else
2637       begin
2638 
2639         if FND_API.to_Boolean(g_global_revision) then
2640         begin
2641 
2642           PSB_WS_POS3.Process_ElemParam_AutoInc
2643                 (p_return_status => l_return_status,
2644                  p_worksheet_id => p_budget_revision_id,
2645                  p_data_extract_id => g_data_extract_id,
2646                  p_business_group_id => g_business_group_id,
2647                  p_parameter_id => c_parameter_rec.parameter_id,
2648                  p_currency_code => nvl(c_parameter_rec.currency_code, g_func_currency),
2649                  p_start_date => greatest(p_revision_start_date, c_parameter_rec.effective_start_date),
2650                  p_end_date => least(p_revision_end_date, nvl(c_parameter_rec.effective_end_date, p_revision_end_date)),
2651                  p_compound_factor => l_compound_factor);
2652 
2653           if l_return_status <> FND_API.G_RET_STS_SUCCESS then
2654             raise FND_API.G_EXC_ERROR;
2655           end if;
2656 
2657         end;
2658         end if;
2659 
2660       end;
2661       end if;
2662 
2663     end;
2664     else
2665     begin
2666 
2667       l_num_revision_years := ceil(months_between(p_revision_end_date, p_revision_start_date) / 12);
2668       l_start_date := p_revision_start_date;
2669 
2670       for i in 1..l_num_revision_years loop
2671 
2672         l_end_date := least(add_months(l_start_date, 12), p_revision_end_date);
2673 
2674         if ((c_Parameter_Rec.parameter_autoinc_rule is null) or
2675             (c_Parameter_Rec.parameter_autoinc_rule = 'N')) then
2676         begin
2677 
2678           l_compound_annually := FND_API.G_TRUE;
2679           l_compound_factor := greatest(ceil(months_between(l_start_date, c_Parameter_Rec.effective_start_date) / 12), 0) + 1;
2680 
2681           PSB_WS_POS3.Process_ElemParam
2682                 (p_return_status => l_return_status,
2683                  p_worksheet_id => p_budget_revision_id,
2684                  p_parameter_id => c_Parameter_Rec.parameter_id,
2685                  p_currency_code => nvl(c_Parameter_Rec.currency_code, g_func_currency),
2686                  p_start_date => greatest(l_start_date, c_Parameter_Rec.effective_start_date),
2687                  p_end_date => least(l_end_date, nvl(c_Parameter_Rec.effective_end_date, l_end_date)),
2688                  p_compound_annually => l_compound_annually,
2689                  p_compound_factor => l_compound_factor);
2690 
2691           if l_return_status <> FND_API.G_RET_STS_SUCCESS then
2692             raise FND_API.G_EXC_ERROR;
2693           end if;
2694 
2695         end;
2696         else
2697         begin
2698 
2699           if FND_API.to_Boolean(g_global_revision) then
2700           begin
2701 
2702             l_compound_factor := greatest(ceil(months_between(l_start_date, c_Parameter_Rec.effective_start_date) / 12), 0) + 1;
2703 
2704             PSB_WS_POS3.Process_ElemParam_AutoInc
2705                    (p_return_status => l_return_status,
2706                     p_worksheet_id => p_budget_revision_id,
2707                     p_data_extract_id => g_data_extract_id,
2708                     p_business_group_id => g_business_group_id,
2709                     p_parameter_id => c_Parameter_Rec.parameter_id,
2710                     p_currency_code => nvl(c_Parameter_Rec.currency_code, g_func_currency),
2711                     p_start_date => greatest(l_start_date, c_Parameter_Rec.effective_start_date),
2712                     p_end_date => least(l_end_date, nvl(c_Parameter_Rec.effective_end_date, l_end_date)),
2713                     p_compound_factor => l_compound_factor);
2714 
2715             if l_return_status <> FND_API.G_RET_STS_SUCCESS then
2716               raise FND_API.G_EXC_ERROR;
2717             end if;
2718 
2719           end;
2720           end if;
2721 
2722         end;
2723         end if;
2724 
2725         l_start_date := l_end_date;
2726 
2727       end loop;
2728 
2729     end;
2730     end if;
2731 
2732   end loop;
2733 
2734   -- Set API return status to success
2735 
2736   p_return_status := FND_API.G_RET_STS_SUCCESS;
2737 
2738 EXCEPTION
2739 
2740    when FND_API.G_EXC_ERROR then
2741      p_return_status := FND_API.G_RET_STS_ERROR;
2742 
2743    when FND_API.G_EXC_UNEXPECTED_ERROR then
2744      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2745 
2746    when OTHERS then
2747      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2748 
2749 End Apply_Element_Parameters;
2750 
2751 /*==========================================================================+
2752  |                       PROCEDURE Apply_Position_Parameters                |
2753  +==========================================================================*/
2754 
2755 PROCEDURE Apply_Position_Parameters
2756 ( p_return_status          OUT  NOCOPY  VARCHAR2,
2757   p_budget_revision_id     IN   NUMBER,
2758   p_local_parameter        IN   VARCHAR2,
2759   p_parameter_id           IN   NUMBER,
2760   p_revision_start_date    IN   DATE,
2761   p_revision_end_date      IN   DATE) IS
2762 
2763   l_start_date             DATE;
2764   l_end_date               DATE;
2765   l_num_revision_years     NUMBER;
2766   l_compound_annually      VARCHAR2(1);
2767   l_compound_factor        NUMBER;
2768   l_return_status          VARCHAR2(1);
2769 
2770   cursor c_parameter is
2771     select parameter_id,
2772            name,
2773            priority,
2774            parameter_compound_annually,
2775            currency_code,
2776            effective_start_date,
2777            effective_end_date
2778       from PSB_PARAMETER_ASSIGNMENTS_V
2779      where p_local_parameter = 'N'
2780        and parameter_autoinc_rule = 'N'
2781        and data_extract_id = g_data_extract_id
2782        and parameter_type = 'POSITION'
2783        and (((effective_start_date <= p_revision_end_date)
2784          and (effective_end_date is null))
2785        or ((effective_start_date between p_revision_start_date and p_revision_end_date)
2786           or (effective_end_date between p_revision_start_date and p_revision_end_date)
2787          or ((effective_start_date < p_revision_start_date)
2788          and (effective_end_date > p_revision_end_date))))
2789        and parameter_set_id = g_parameter_set_id
2790      union
2791     select parameter_id,
2792            name,
2793            0 priority,
2794            parameter_compound_annually,
2795            currency_code,
2796            effective_start_date,
2797            effective_end_date
2798       from PSB_PARAMETERS_V
2799      where p_local_parameter = 'Y'
2800        and parameter_autoinc_rule = 'N'
2801        and data_extract_id = g_data_extract_id
2802        and parameter_type = 'POSITION'
2803        and (((effective_start_date <= p_revision_end_date)
2804          and (effective_end_date is null))
2805        or ((effective_start_date between p_revision_start_date and p_revision_end_date)
2806           or (effective_end_date between p_revision_start_date and p_revision_end_date)
2807          or ((effective_start_date < p_revision_start_date)
2808          and (effective_end_date > p_revision_end_date))))
2809        and parameter_id = p_parameter_id
2810      order by effective_start_date, priority;
2811 
2812   cursor c_ParamAutoInc is
2813     select parameter_id,
2814            name,
2815            priority,
2816            parameter_compound_annually,
2817            currency_code,
2818            effective_start_date,
2819            effective_end_date
2820       from PSB_PARAMETER_ASSIGNMENTS_V
2821      where p_local_parameter = 'N'
2822        and parameter_autoinc_rule = 'Y'
2823        and data_extract_id = g_data_extract_id
2824        and parameter_type = 'POSITION'
2825        and (((effective_start_date <= p_revision_end_date)
2826          and (effective_end_date is null))
2827          or ((effective_start_date between p_revision_start_date and p_revision_end_date)
2828           or (effective_end_date between p_revision_start_date and p_revision_end_date)
2829          or ((effective_start_date < p_revision_start_date)
2830          and (effective_end_date > p_revision_end_date))))
2831        and parameter_set_id = g_parameter_set_id
2832      union
2833     select parameter_id,
2834            name,
2835            0 priority,
2836            parameter_compound_annually,
2837            currency_code,
2838            effective_start_date,
2839            effective_end_date
2840       from PSB_PARAMETERS_V
2841      where p_local_parameter = 'Y'
2842        and parameter_id = p_parameter_id
2843        and parameter_autoinc_rule = 'Y'
2844        and data_extract_id = g_data_extract_id
2845        and parameter_type = 'POSITION'
2846        and (((effective_start_date <= l_end_date)
2847          and (effective_end_date is null))
2848          or ((effective_start_date between p_revision_start_date and p_revision_end_date)
2849           or (effective_end_date between p_revision_start_date and p_revision_end_date)
2850          or ((effective_start_date < p_revision_start_date)
2851          and (effective_end_date > p_revision_end_date))))
2852      order by effective_start_date,
2853               priority;
2854 
2855 BEGIN
2856 
2857   for c_parameter_rec in c_parameter loop
2858 
2859     if ((c_Parameter_Rec.parameter_compound_annually is null) or
2860         (c_Parameter_Rec.parameter_compound_annually = 'N')) then
2861     begin
2862 
2863       l_compound_annually := FND_API.G_FALSE;
2864 
2865       PSB_WS_POS3.Process_PosParam_Detailed
2866             (p_return_status => l_return_status,
2867              p_event_type => 'BR',
2868              p_local_parameter => p_local_parameter,
2869              p_worksheet_id => p_budget_revision_id,
2870              p_global_worksheet_id => g_global_budget_revision_id,
2871              p_global_worksheet => g_global_revision,
2872              p_data_extract_id => g_data_extract_id,
2873              p_business_group_id => g_business_group_id,
2874              p_parameter_id => c_parameter_rec.parameter_id,
2875              p_parameter_start_date => c_parameter_rec.effective_start_date,
2876              p_compound_annually => l_compound_annually,
2877              p_compound_factor => l_compound_factor,
2878              p_parameter_autoinc_rule => 'N',
2879              p_currency_code => nvl(c_parameter_rec.currency_code, g_func_currency),
2880              p_start_date => greatest(p_revision_start_date, c_Parameter_Rec.effective_start_date),
2881              p_end_date => least(p_revision_end_date, nvl(c_Parameter_Rec.effective_end_date, p_revision_end_date)));
2882 
2883       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
2884         raise FND_API.G_EXC_ERROR;
2885       end if;
2886 
2887     end;
2888     else
2889     begin
2890 
2891       l_num_revision_years := ceil(months_between(p_revision_end_date, p_revision_start_date) / 12);
2892       l_start_date := p_revision_start_date;
2893 
2894       for i in 1..l_num_revision_years loop
2895 
2896         l_end_date := least(add_months(l_start_date, 12), p_revision_end_date);
2897 
2898         l_compound_annually := FND_API.G_TRUE;
2899         l_compound_factor := greatest(ceil(months_between(l_start_date, c_Parameter_Rec.effective_start_date) / 12), 0) + 1;
2900 
2901         PSB_WS_POS3.Process_PosParam_Detailed
2902             (p_return_status => l_return_status,
2903              p_event_type => 'BR',
2904              p_local_parameter => p_local_parameter,
2905              p_worksheet_id => p_budget_revision_id,
2906              p_global_worksheet_id => g_global_budget_revision_id,
2907              p_global_worksheet => g_global_revision,
2908              p_data_extract_id => g_data_extract_id,
2909              p_business_group_id => g_business_group_id,
2910              p_parameter_id => c_parameter_rec.parameter_id,
2911              p_parameter_start_date => c_parameter_rec.effective_start_date,
2912              p_compound_annually => l_compound_annually,
2913              p_compound_factor => l_compound_factor,
2914              p_parameter_autoinc_rule => 'N',
2915              p_currency_code => nvl(c_parameter_rec.currency_code, g_func_currency),
2916              p_start_date => greatest(l_start_date, c_Parameter_Rec.effective_start_date),
2917              p_end_date => least(l_end_date, nvl(c_Parameter_Rec.effective_end_date, l_end_date)));
2918 
2919         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
2920           raise FND_API.G_EXC_ERROR;
2921         end if;
2922 
2923         l_start_date := l_end_date;
2924 
2925       end loop;
2926 
2927     end;
2928     end if;
2929 
2930   end loop;
2931 
2932   for c_parameter_rec in c_ParamAutoInc loop
2933 
2934     if ((c_Parameter_Rec.parameter_compound_annually is null) or
2935         (c_Parameter_Rec.parameter_compound_annually = 'N')) then
2936       l_compound_annually := FND_API.G_FALSE;
2937     else
2938       l_compound_annually := FND_API.G_TRUE;
2939     end if;
2940 
2941     PSB_WS_POS3.Process_PosParam_Detailed
2942           (p_return_status => l_return_status,
2943            p_event_type => 'BR',
2944            p_local_parameter => p_local_parameter,
2945            p_worksheet_id => p_budget_revision_id,
2946            p_global_worksheet_id => g_global_budget_revision_id,
2947            p_global_worksheet => g_global_revision,
2948            p_data_extract_id => g_data_extract_id,
2949            p_business_group_id => g_business_group_id,
2950            p_parameter_id => c_parameter_rec.parameter_id,
2951            p_parameter_start_date => c_parameter_rec.effective_start_date,
2952            p_compound_annually => l_compound_annually,
2953            p_compound_factor => l_compound_factor,
2954            p_parameter_autoinc_rule => 'Y',
2955            p_currency_code => nvl(c_parameter_rec.currency_code, g_func_currency),
2956            p_start_date => greatest(p_revision_start_date, c_Parameter_Rec.effective_start_date),
2957            p_end_date => least(p_revision_end_date, nvl(c_Parameter_Rec.effective_end_date, p_revision_end_date)));
2958 
2959     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
2960       raise FND_API.G_EXC_ERROR;
2961     end if;
2962 
2963   end loop;
2964 
2965   p_return_status := FND_API.G_RET_STS_SUCCESS;
2966 
2967 EXCEPTION
2968 
2969    when FND_API.G_EXC_ERROR then
2970      p_return_status := FND_API.G_RET_STS_ERROR;
2971 
2972    when FND_API.G_EXC_UNEXPECTED_ERROR then
2973      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2974 
2975    when OTHERS then
2976      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2977 
2978 End Apply_Position_Parameters;
2979 /*---------------------------------------------------------------------------*/
2980 
2981 
2982 /*===========================================================================+
2983  |                      PROCEDURE Create_Summary_Position_Line               |
2984  +===========================================================================*/
2985 PROCEDURE Create_Summary_Position_Line
2986 ( p_return_status         OUT  NOCOPY  VARCHAR2,
2987   p_budget_revision_id    IN   NUMBER,
2988   p_currency_code         IN   VARCHAR2,
2989   p_gl_period_name        IN   VARCHAR2
2990 )
2991 IS
2992   l_return_status                 VARCHAR2(1);
2993   l_msg_count                     NUMBER;
2994   l_msg_data                      VARCHAR2(2000);
2995   l_budget_revision_acct_line_id  NUMBER;
2996   l_revision_type                 VARCHAR2(1);
2997   l_revision_amount               NUMBER;
2998   l_current_budget_balance        NUMBER;
2999 
3000   CURSOR l_rollup_ccid_csr
3001   IS
3002   SELECT pbra.code_combination_id, pbra.gl_budget_version_id,
3003          pbra.budget_group_id,
3004          sum(decode(pbra.revision_type, 'D', -1 * pbra.revision_amount,
3005              pbra.revision_amount)) sum_revision
3006   FROM   psb_budget_revision_lines pbrl, psb_budget_revision_accounts pbra
3007   WHERE  pbrl.budget_revision_id           = p_budget_revision_id
3008   AND    pbra.budget_revision_acct_line_id = pbrl.budget_revision_acct_line_id
3009   AND    pbra.gl_period_name               = p_gl_period_name
3010   AND    pbra.position_id IS NOT NULL
3011   GROUP  BY pbra.code_combination_id, pbra.gl_budget_version_id,
3012          pbra.budget_group_id ;
3013 
3014 BEGIN
3015 
3016   -- Process all the rollup ccids corresponding to positions.
3017   FOR l_rollup_ccid_rec IN l_rollup_ccid_csr LOOP
3018 
3019     l_budget_revision_acct_line_id := NULL;
3020 
3021     if l_rollup_ccid_rec.sum_revision < 0 then
3022       l_revision_type := 'D';
3023       l_revision_amount := -1 * l_rollup_ccid_rec.sum_revision;
3024     else
3025       l_revision_type := 'I';
3026       l_revision_amount := l_rollup_ccid_rec.sum_revision;
3027     end if;
3028 
3029     l_current_budget_balance :=
3030     Get_GL_Balance
3031     ( p_revision_type         => g_budget_revision_type,
3032       p_balance_type          => g_balance_type,
3033       p_set_of_books_id       => g_set_of_books_id,
3034       p_xbc_enabled_flag      => g_budgetary_control,
3035       p_gl_period_name        => p_gl_period_name,
3036       p_gl_budget_version_id  => l_rollup_ccid_rec.gl_budget_version_id,
3037       p_currency_code         => g_currency_code,
3038       p_code_combination_id   => l_rollup_ccid_rec.code_combination_id
3039     );
3040 
3041     Create_Revision_Accounts
3042     ( p_api_version => 1.0,
3043       p_return_status => l_return_status,
3044       p_msg_count => l_msg_count,
3045       p_msg_data => l_msg_data,
3046       p_budget_revision_id => p_budget_revision_id,
3047       p_budget_revision_acct_line_id => l_budget_revision_acct_line_id,
3048       p_code_combination_id => l_rollup_ccid_rec.code_combination_id,
3049       p_budget_group_id => l_rollup_ccid_rec.budget_group_id,
3050       p_gl_period_name => p_gl_period_name,
3051       p_gl_budget_version_id => l_rollup_ccid_rec.gl_budget_version_id,
3052       p_currency_code => p_currency_code,
3053       p_budget_balance => l_current_budget_balance,
3054       p_revision_type => l_revision_type,
3055       p_revision_value_type => 'A',
3056       p_revision_amount => l_revision_amount,
3057       p_note_id => FND_API.G_MISS_NUM,
3058       p_funds_control_timestamp => sysdate,
3059       p_funds_status_code => FND_API.G_MISS_CHAR,
3060       p_funds_result_code => FND_API.G_MISS_CHAR,
3061       p_freeze_flag => 'N',
3062       p_view_line_flag => 'Y'
3063     ) ;
3064     --
3065     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3066       raise FND_API.G_EXC_ERROR;
3067     end if;
3068     --
3069   END LOOP;
3070 
3071   p_return_status := FND_API.G_RET_STS_SUCCESS;
3072 
3073 EXCEPTION
3074 
3075    when FND_API.G_EXC_ERROR then
3076      p_return_status := FND_API.G_RET_STS_ERROR;
3077 
3078    when FND_API.G_EXC_UNEXPECTED_ERROR then
3079      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3080 
3081    when OTHERS then
3082      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3083 
3084 End Create_Summary_Position_Line;
3085 /*---------------------------------------------------------------------------*/
3086 
3087 
3088 /*---------------------------------------------------------------------------*/
3089 PROCEDURE Create_Mass_Revision_Entries
3090 ( p_api_version         IN      NUMBER,
3091   p_init_msg_list       IN      VARCHAR2 := FND_API.G_FALSE,
3092   p_commit              IN      VARCHAR2 := FND_API.G_FALSE,
3093   p_validation_level    IN      NUMBER  := FND_API.G_VALID_LEVEL_FULL,
3094   p_return_status       OUT  NOCOPY     VARCHAR2,
3095   p_msg_count           OUT  NOCOPY     NUMBER,
3096   p_msg_data            OUT  NOCOPY     VARCHAR2,
3097   p_data_extract_id     IN      NUMBER,
3098   p_budget_revision_id  IN      NUMBER,
3099   p_parameter_id        IN      NUMBER := FND_API.G_MISS_NUM
3100 ) IS
3101 
3102   l_api_name                     CONSTANT VARCHAR2(30)  := 'Create_Mass_Revision_Entries';
3103   l_api_version                  CONSTANT NUMBER        := 1.0;
3104 
3105   l_effective_start_date         DATE;
3106   l_effective_end_date           DATE;
3107 
3108   l_budget_revision_acct_line_id NUMBER;
3109   l_original_budget_balance      NUMBER;
3110   l_current_budget_balance       NUMBER;
3111 
3112   l_budget_version_id            NUMBER;
3113   l_budget_revision_pos_line_id  NUMBER;
3114   l_revision_type                VARCHAR2(1);
3115   l_revision_amount              NUMBER := 0;
3116   l_revised_amount               NUMBER := 0;
3117   l_concat_segments              VARCHAR2(2000);
3118   l_ccid_valid                   VARCHAR2(1) := FND_API.G_FALSE;
3119   l_out_ccid                     NUMBER;
3120   l_compound_annually            VARCHAR2(1);
3121   l_compound_factor              NUMBER;
3122 
3123   l_ccid_index                   NUMBER;
3124   l_ccid_type                    VARCHAR2(30);
3125   l_pos_line_id                  NUMBER;
3126   l_parameter_id                 NUMBER;
3127   l_local_parameter              VARCHAR2(1);
3128   lx_from_date                   DATE;
3129   l_return_status                VARCHAR2(1);
3130   l_msg_count                    NUMBER;
3131   l_msg_data                     VARCHAR2(2000);
3132 
3133   l_gl_period_name               VARCHAR2(15);
3134   l_ccid_start_period            DATE;
3135   l_ccid_end_period              DATE;
3136 
3137 /* Bug No 1808330 Start */
3138   l_note                         VARCHAR2(4000); -- Bug#4675858
3139 /* Bug No 1808330 End */
3140 
3141   cursor c_gl_periods is
3142     Select period_name,
3143            start_date,end_date
3144       from gl_period_statuses
3145      where application_id = 101
3146        and set_of_books_id = g_set_of_books_id
3147        and start_date between g_from_date and g_to_date
3148        and end_date between g_from_date and g_to_date
3149        and closing_status <> 'C'
3150        /*Bug No. 4018446 Start*/
3151        and adjustment_period_flag = 'N';
3152        /*Bug No. 4018446 End*/
3153 
3154   -- Bug 3029168 In the following cursor added the join with g_currency_code
3155   -- Bug#4675858
3156   -- Added NVL condition for nullable currency_code column.
3157   cursor c_AccParam is
3158     Select parameter_id,
3159            name,
3160            effective_start_date,
3161            effective_end_date,
3162            priority priority,
3163            parameter_compound_annually,
3164            currency_code
3165       from PSB_PARAMETER_ASSIGNMENTS_V
3166      where l_parameter_id is null
3167        and parameter_set_id = g_parameter_set_id
3168        and parameter_type = 'ACCOUNT'
3169        and NVL(currency_code, g_currency_code) = g_currency_code
3170     UNION
3171     Select entity_id,
3172            name,
3173            effective_start_date,
3174            effective_end_date,
3175            0 priority,
3176            parameter_compound_annually,
3177            currency_code
3178       from PSB_ENTITY
3179      where entity_id = l_parameter_id
3180        and entity_subtype = 'ACCOUNT'
3181        and NVL(currency_code, g_currency_code) = g_currency_code
3182      order by effective_start_date, priority;
3183 
3184   cursor c_account_sets is
3185     select account_position_set_id, account_or_position_type, budget_group_id,
3186            effective_start_date, effective_end_date
3187       from PSB_SET_RELATIONS_V
3188      where budget_group_id in
3189           (select budget_group_id
3190              from psb_budget_groups
3191             where effective_start_date <= l_effective_start_date
3192               and (effective_end_date is null or
3193                    effective_end_date >= l_effective_end_date)
3194             start with budget_group_id = g_budget_group_id
3195             connect by prior budget_group_id = parent_budget_group_id)
3196        and account_or_position_type = 'A';
3197 
3198   cursor c_positions is
3199     select a.position_id, a.effective_start_date, a.effective_end_date, a.budget_group_id
3200       from PSB_POSITIONS a,
3201           (select budget_group_id from PSB_BUDGET_GROUPS
3202             start with budget_group_id = g_budget_group_id
3203           connect by prior budget_group_id = parent_budget_group_id) b
3204      where a.data_extract_id = p_data_extract_id
3205        and a.budget_group_id = b.budget_group_id
3206        and a.hr_position_id is not null;
3207 
3208   cursor c_localparam_positions is
3209     select pbrp.position_id, pbrp.effective_start_date, pbrp.effective_end_date,
3210 /* Bug No 1808330 Start */
3211     pbrp.budget_revision_pos_line_id
3212 /* Bug No 1808330 End */
3213       from PSB_BUDGET_REVISION_POS_LINES pbrpl, PSB_BUDGET_REVISION_POSITIONS pbrp
3214      where pbrpl.budget_revision_id = p_budget_revision_id
3215        and pbrp.budget_revision_pos_line_id = pbrpl.budget_revision_pos_line_id;
3216 
3217  cursor c_period_list is
3218    Select period_name,
3219            start_date,end_date
3220       from gl_period_statuses
3221      where application_id = 101
3222        and set_of_books_id = g_set_of_books_id
3223        and start_date between g_effective_start_date and g_effective_end_date
3224        and end_date between g_effective_start_date and g_effective_end_date
3225        and closing_status <> 'C'
3226        /*Bug No. 4018446 Start*/
3227        and adjustment_period_flag = 'N';
3228        /*Bug No. 4018446 End*/
3229 
3230   cursor c_period (startdate DATE) is
3231     select period_name,
3232            start_date,
3233            end_date
3234       from gl_period_statuses
3235      where application_id  = 101
3236        and set_of_books_id = g_set_of_books_id
3237        and startdate between start_date and end_date;
3238 
3239 Begin
3240 
3241   -- Standard Start of API savepoint
3242 
3243   SAVEPOINT     Create_Mass_Revision_Entries;
3244 
3245   -- Standard call to check for call compatibility.
3246 
3247   if not FND_API.Compatible_API_Call (l_api_version,
3248                                       p_api_version,
3249                                       l_api_name,
3250                                       G_PKG_NAME)
3251   then
3252     raise FND_API.G_EXC_UNEXPECTED_ERROR;
3253   end if;
3254 
3255   -- Initialize message list if p_init_msg_list is set to TRUE.
3256 
3257   if FND_API.to_Boolean (p_init_msg_list) then
3258     FND_MSG_PUB.initialize;
3259   end if;
3260 
3261   -- Initialize global variables.
3262 
3263   Cache_Revision_Variables(p_budget_revision_id => p_budget_revision_id,
3264                            p_return_status => l_return_status);
3265 
3266   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3267     raise FND_API.G_EXC_ERROR;
3268   end if;
3269 
3270   -- Bug 3029168 commitment apis will not be called for STAT
3271   IF g_budget_revision_type = 'C'
3272   AND g_currency_code <> 'STAT' THEN
3273   begin -- commitment budget revision
3274 
3275      PSB_Commitments_PVT.Create_Commitment_Revisions
3276      ( p_api_version        => 1.0,
3277        p_init_msg_list      => FND_API.G_FALSE,
3278        p_commit             => FND_API.G_FALSE,
3279        p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
3280        p_return_status      => l_return_status,
3281        p_msg_count          => l_msg_count,
3282        p_msg_data           => l_msg_data,
3283        p_budget_revision_id => p_budget_revision_id);
3284 
3285     if (l_return_status <> FND_API.G_RET_STS_SUCCESS) then
3286       add_message('PSB','PSB_CBC_MASS_REVISION_FAILED');
3287       RAISE FND_API.G_EXC_ERROR;
3288     end if;
3289 
3290   end; -- commitment budget revision
3291   elsif (g_budget_revision_type = 'R') then -- regular budget revision
3292   begin
3293 
3294     if (p_parameter_id = FND_API.G_MISS_NUM) then
3295       l_parameter_id := null;
3296       l_local_parameter := 'N';
3297     else
3298       l_parameter_id := p_parameter_id;
3299       l_local_parameter := 'Y';
3300     end if;
3301 
3302     lx_from_date := g_from_date;
3303 
3304     for c_gl_periods_rec in c_gl_periods loop
3305       l_effective_start_date := c_gl_periods_rec.start_date;
3306       l_effective_end_date   := c_gl_periods_rec.end_date;
3307 
3308       For C_Account_Sets_Rec in C_Account_Sets Loop
3309 
3310         PSB_WS_ACCT1.Find_CCIDs
3311         (p_return_status => l_return_status,
3312          p_account_set_id => c_Account_Sets_Rec.account_position_set_id);
3313 
3314         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3315           raise FND_API.G_EXC_ERROR;
3316         end if;
3317 
3318         for l_ccid_index in 1..PSB_WS_ACCT1.g_num_ccids loop
3319 
3320           l_ccid_start_period := greatest(nvl(PSB_WS_ACCT1.g_ccids(l_ccid_index).start_date,
3321                                           c_Account_Sets_Rec.effective_start_date), c_Account_Sets_Rec.effective_start_date);
3322           l_ccid_end_period := least(nvl(PSB_WS_ACCT1.g_ccids(l_ccid_index).end_date,
3323                                          c_Account_Sets_Rec.effective_end_date), c_Account_Sets_Rec.effective_end_date);
3324 
3325           l_ccid_type := null;
3326 
3327           if (g_revise_by_position = 'Y') then
3328           begin
3329 
3330             PSB_WS_ACCT1.Check_CCID_Type
3331                (p_api_version => 1.0,
3332                 p_return_status => l_return_status,
3333                 p_ccid_type => l_ccid_type,
3334                 p_flex_code => g_flex_code,
3335                 p_ccid => PSB_WS_ACCT1.g_ccids(l_ccid_index).ccid,
3336                 p_budget_group_id => c_Account_Sets_Rec.budget_group_id);
3337 
3338             if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3339               raise FND_API.G_EXC_ERROR;
3340             end if;
3341 
3342           end;
3343           end if;
3344 
3345           if ((g_revise_by_position = 'N') or
3346              ((g_revise_by_position = 'Y') and (l_ccid_type = 'NON_PERSONNEL_SERVICES'))) then
3347           begin
3348 
3349             l_budget_version_id := null;
3350             l_original_budget_balance := 0;
3351             l_current_budget_balance := 0;
3352 
3353             PSB_GL_BUDGET_PVT.Find_GL_Budget
3354                (p_api_version => 1.0,
3355                 p_return_status => l_return_status,
3356                 p_msg_count => l_msg_count,
3357                 p_msg_data => l_msg_data,
3358                 p_gl_budget_set_id => g_gl_budget_set_id,
3359                 p_code_combination_id => PSB_WS_ACCT1.g_ccids(l_ccid_index).ccid,
3360                 p_start_date => c_gl_periods_rec.start_date,
3361                 p_dual_posting_type => 'A',
3362                 p_gl_budget_version_id => l_budget_version_id);
3363 
3364             if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3365               raise FND_API.G_EXC_ERROR;
3366             end if;
3367 
3368             if ((l_budget_version_id is null) and (g_permanent_revision = 'Y')) then
3369             begin
3370 
3371               PSB_GL_BUDGET_PVT.Find_GL_Budget
3372                  (p_api_version => 1.0,
3373                   p_return_status => l_return_status,
3374                   p_msg_count => l_msg_count,
3375                   p_msg_data => l_msg_data,
3376                   p_gl_budget_set_id => g_gl_budget_set_id,
3377                   p_code_combination_id => PSB_WS_ACCT1.g_ccids(l_ccid_index).ccid,
3378                   p_start_date => c_gl_periods_rec.start_date,
3379                   p_gl_budget_version_id => l_budget_version_id);
3380 
3381               if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3382                 raise FND_API.G_EXC_ERROR;
3383               end if;
3384 
3385             end;
3386             end if;
3387 
3388             -- Get Original Budget for the ccid + gl_period + budget_version_id
3389             l_original_budget_balance := Find_Original_Budget_Balance
3390              (p_code_combination_id   => PSB_WS_ACCT1.g_ccids(l_ccid_index).ccid,
3391               p_budget_group_id        => c_Account_Sets_Rec.budget_group_id,
3392               p_gl_period              => c_gl_periods_rec.period_name,
3393               p_gl_budget_version_id   => l_budget_version_id,
3394               p_currency_code          => g_currency_code); -- Bug 3029168
3395 
3396             l_current_budget_balance := Get_GL_Balance
3397              (p_revision_type         => g_budget_revision_type,
3398               p_balance_type          => g_balance_type,
3399               p_set_of_books_id       => g_set_of_books_id,
3400               p_xbc_enabled_flag      => g_budgetary_control,
3401               p_gl_period_name        => c_gl_periods_rec.period_name,
3402               p_gl_budget_version_id  => l_budget_version_id,
3403               p_currency_code         => g_currency_code,
3404               p_code_combination_id   => PSB_WS_ACCT1.g_ccids(l_ccid_index).ccid);
3405 
3406             -- Compute a Compound Factor for each Budget Year if Compound Annually is set
3407 
3408             if (c_gl_periods_rec.start_date > add_months(lx_from_date, 12)) then
3409               lx_from_date := add_months(lx_from_date, 12);
3410             end if;
3411 
3412             For c_AccParam_Rec in c_AccParam loop
3413 
3414               if (AcctParam_Exists(p_parameter_id => c_AccParam_Rec.parameter_id,
3415                                    p_budget_revision_id => p_budget_revision_id,
3416                                    p_period_name => c_gl_periods_rec.period_name,
3417                                    p_local_parameter => l_local_parameter,
3418                                    p_ccid => PSB_WS_ACCT1.g_ccids(l_ccid_index).ccid,
3419                                    p_ccid_start_period => l_ccid_start_period,
3420                                    p_ccid_end_period => l_ccid_end_period,
3421                                    p_period_start_date => c_gl_periods_rec.start_date,
3422                                    p_period_end_date => c_gl_periods_rec.end_date)) then
3423               begin
3424 
3425                 if ((c_AccParam_Rec.parameter_compound_annually is null) or
3426                     (c_AccParam_Rec.parameter_compound_annually = 'N')) then
3427                   l_compound_annually := FND_API.G_FALSE;
3428                 else
3429                   l_compound_annually := FND_API.G_TRUE;
3430                   l_compound_factor := greatest(ceil(months_between(lx_from_date, c_AccParam_Rec.effective_start_date) / 12), 0) + 1;
3431                 end if;
3432 
3433                 Apply_Revision_Acct_Parameters
3434                      (p_api_version         => 1.0,
3435                       p_return_status       => l_return_status,
3436                       p_parameter_id        => c_AccParam_Rec.parameter_id,
3437                       p_parameter_name      => c_AccParam_Rec.name,
3438                       p_compound_annually   => l_compound_annually,
3439                       p_compound_factor     => l_compound_factor,
3440                       p_original_budget     => l_original_budget_balance,
3441                       p_current_budget      => l_current_budget_balance,
3442                       p_revision_amount     => l_revision_amount);
3443 
3444                 if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3445                   raise FND_API.G_EXC_ERROR;
3446                 end if;
3447 
3448                 l_budget_revision_acct_line_id := null;
3449 
3450         if (l_revision_amount < 0) then
3451            l_revision_type := 'D';
3452            l_revision_amount := (-1) * l_revision_amount;
3453         else
3454            l_revision_type := 'I';
3455         end if;
3456 
3457                 if ((l_revision_amount <> 0) or ((l_revision_amount = 0) and (g_create_zero_bal = 'Y'))) then
3458                 begin
3459 
3460                   Create_Revision_Accounts
3461                         (p_api_version                    => 1.0,
3462                          p_init_msg_list                  => FND_API.G_FALSE,
3463                          p_commit                         => FND_API.G_FALSE,
3464                          p_validation_level               => FND_API.G_VALID_LEVEL_FULL,
3465                          p_return_status                  => l_return_status,
3466                          p_msg_count                      => l_msg_count,
3467                          p_msg_data                       => l_msg_data,
3468                          p_budget_revision_id             => p_budget_revision_id,
3469                          p_budget_revision_acct_line_id   => l_budget_revision_acct_line_id,
3470                          p_code_combination_id            => PSB_WS_ACCT1.g_ccids(l_ccid_index).ccid,
3471                          p_budget_group_id                => c_Account_Sets_Rec.budget_group_id,
3472                          p_gl_period_name                 => c_gl_periods_rec.period_name,
3473                          p_gl_budget_version_id           => l_budget_version_id,
3474                          p_currency_code                  => g_currency_code,
3475                          p_budget_balance                 => l_current_budget_balance,
3476                          p_revision_type                  => l_revision_type,
3477                          p_revision_value_type            => 'A',
3478                          p_revision_amount                => l_revision_amount,
3479                          p_funds_status_code              => FND_API.G_MISS_CHAR,
3480                          p_funds_result_code              => FND_API.G_MISS_CHAR,
3481                          p_note_id                        => FND_API.G_MISS_NUM,
3482                          p_funds_control_timestamp        => sysdate,
3483                          p_freeze_flag                    => 'N',
3484                          p_view_line_flag                 => 'Y');
3485 
3486                   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3487                     raise FND_API.G_EXC_ERROR;
3488                   end if;
3489 
3490 /* Bug No 1808330 Start */
3491 ---- Creates or Updates a Note Id in PSB_WS_ACCOUNT_LINE_NOTES table
3492 
3493       FND_MESSAGE.SET_NAME('PSB', 'PSB_PARAMETER_NOTE_CREATION');
3494       FND_MESSAGE.SET_TOKEN('NAME', c_AccParam_Rec.name);
3495       FND_MESSAGE.SET_TOKEN('DATE', sysdate);
3496       l_note := FND_MESSAGE.GET;
3497 
3498       -- Bug#4571412
3499       -- Added p_flex_code to make the call in
3500       -- in sync with its definition.
3501       Create_Note
3502       ( p_return_status    => l_return_status
3503       , p_account_line_id  => l_budget_revision_acct_line_id
3504       , p_position_line_id => NULL
3505       , p_note             => l_note
3506       , p_flex_code        => g_flex_code
3507       , p_cc_id            => PSB_WS_ACCT1.g_ccids(l_ccid_index).ccid -- Bug#4675858
3508       ) ;
3509 
3510       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3511         raise FND_API.G_EXC_ERROR;
3512       end if;
3513 ----
3514 /* Bug No 1808330 End */
3515 
3516                 end;
3517                 end if;
3518 
3519               end;
3520               end if; -- Parameter exists for account
3521 
3522             End Loop; /* Parameter Account Set */
3523 
3524           end;
3525           end if; --Non Position Account if revise by position
3526 
3527         End Loop; -- CCid Loop
3528 
3529       End Loop; -- Budget Accounts Loop
3530 
3531     End Loop; -- Gl Periods Loop
3532 
3533 
3534     -- Mass Entries for Position Revision
3535     if g_position_exists then
3536     begin
3537 
3538       if ((g_position_mass_revision) or (l_local_parameter = 'Y')) then
3539       begin
3540 
3541         Apply_Element_Parameters
3542            (p_return_status       => l_return_status,
3543             p_budget_revision_id  => p_budget_revision_id,
3544             p_local_parameter     => l_local_parameter,
3545             p_parameter_id        => p_parameter_id,
3546             p_revision_start_date => g_effective_start_date,
3547             p_revision_end_date   => g_effective_end_date);
3548 
3549         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3550           raise FND_API.G_EXC_ERROR;
3551         end if;
3552 
3553         Apply_Position_Parameters
3554              (p_return_status       => l_return_status,
3555               p_budget_revision_id  => p_budget_revision_id,
3556               p_local_parameter     => l_local_parameter,
3557               p_parameter_id        => p_parameter_id,
3558               p_revision_start_date => g_effective_start_date,
3559               p_revision_end_date   => g_effective_end_date);
3560 
3561         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3562           raise FND_API.G_EXC_ERROR;
3563         end if;
3564 
3565       if l_local_parameter = 'N' then
3566       begin
3567 
3568         for c_positions_rec in c_positions loop
3569 
3570           Calculate_Position_Cost
3571           ( p_api_version         => 1.0
3572           , p_return_status       => l_return_status
3573           , p_msg_count           => l_msg_count
3574           , p_msg_data            => l_msg_data
3575           , p_mass_revision       => TRUE
3576           , p_budget_revision_id  => p_budget_revision_id
3577           , p_position_id         => c_positions_rec.position_id
3578           , p_revision_start_date => g_effective_start_date
3579           , p_revision_end_date   => g_effective_end_date
3580           , p_parameter_id        => p_parameter_id -- Bug#4675858
3581           );
3582 
3583           if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3584             raise FND_API.G_EXC_ERROR;
3585           end if;
3586 
3587         end loop; -- Positions
3588 
3589 
3590         for c_period_list_rec in c_period_list loop
3591           l_gl_period_name := null;
3592           l_gl_period_name := c_period_list_rec.period_name;
3593 
3594         Create_Summary_Position_Line
3595               (p_return_status => l_return_status,
3596                p_budget_revision_id => p_budget_revision_id,
3597                p_currency_code => g_func_currency,
3598                p_gl_period_name => l_gl_period_name);
3599 
3600         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3601           raise FND_API.G_EXC_ERROR;
3602         end if;
3603         end loop;
3604 
3605       end;
3606       else
3607       begin
3608 
3609         for c_positions_rec in c_localparam_positions loop
3610 
3611           Calculate_Position_Cost
3612           ( p_api_version         => 1.0
3613           , p_return_status       => l_return_status
3614           , p_msg_count           => l_msg_count
3615           , p_msg_data            => l_msg_data
3616           , p_mass_revision       => TRUE
3617           , p_budget_revision_id  => p_budget_revision_id
3618           , p_position_id         => c_positions_rec.position_id
3619           , p_revision_start_date => c_positions_rec.effective_start_date
3620           , p_revision_end_date   => c_positions_rec.effective_end_date
3621           , p_parameter_id        => p_parameter_id -- Bug#4675858
3622           ) ;
3623 
3624           if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3625             raise FND_API.G_EXC_ERROR;
3626           end if;
3627 
3628           l_gl_period_name := null;
3629 
3630           for c_period_rec in c_period(c_positions_rec.effective_start_date) loop
3631             l_gl_period_name := c_period_rec.period_name;
3632           end loop;
3633 
3634           Create_Summary_Position_Line
3635                 (p_return_status => l_return_status,
3636                  p_budget_revision_id => p_budget_revision_id,
3637                  p_currency_code => g_func_currency,
3638                  p_gl_period_name => l_gl_period_name);
3639 
3640           if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3641             raise FND_API.G_EXC_ERROR;
3642           end if;
3643 
3644         end loop; -- Positions
3645 
3646       end;
3647       end if;
3648 
3649     end; -- mass revision position entries
3650     end if;
3651 
3652     end; -- Position Exists
3653     end if;
3654 
3655   end; -- regular budget revision
3656   end if;
3657 
3658   -- Initialize API return status to success
3659 
3660   p_return_status := FND_API.G_RET_STS_SUCCESS;
3661 
3662   -- Standard check of p_commit.
3663 
3664   if FND_API.to_Boolean (p_commit) then
3665     commit work;
3666   end if;
3667 
3668   -- Standard call to get message count and if count is 1, get message info.
3669 
3670   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
3671                              p_data  => p_msg_data);
3672 
3673 EXCEPTION
3674 
3675    when FND_API.G_EXC_ERROR then
3676      rollback to Create_Mass_Revision_Entries;
3677      p_return_status := FND_API.G_RET_STS_ERROR;
3678 
3679      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
3680                                 p_data  => p_msg_data);
3681 
3682    when FND_API.G_EXC_UNEXPECTED_ERROR then
3683      rollback to Create_Mass_Revision_Entries;
3684      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3685 
3686      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
3687                                 p_data  => p_msg_data);
3688 
3689    when OTHERS then
3690      rollback to Create_Mass_Revision_Entries;
3691      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3692 
3693      if FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
3694 
3695        FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
3696                                 l_api_name);
3697      end if;
3698 
3699      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
3700                                 p_data  => p_msg_data);
3701 
3702 END Create_Mass_Revision_Entries;
3703 
3704 /* ----------------------------------------------------------------------- */
3705 
3706 PROCEDURE Insert_Revision_Positions
3707 ( p_return_status                   OUT  NOCOPY     VARCHAR2,
3708   p_budget_revision_pos_line_id     OUT  NOCOPY     NUMBER,
3709   p_budget_revision_id              IN      NUMBER,
3710   p_position_id                     IN      NUMBER,
3711   p_budget_group_id                 IN      NUMBER,
3712   p_effective_start_date            IN      DATE,
3713   p_effective_end_date              IN      DATE,
3714   p_revision_type                   IN      VARCHAR2,
3715   p_revision_value_type             IN      VARCHAR2,
3716   p_revision_value                  IN      NUMBER,
3717   p_note_id                         IN      NUMBER,
3718   p_freeze_flag                     IN      VARCHAR2,
3719   p_view_line_flag                  IN      VARCHAR2
3720 ) IS
3721 
3722   l_budget_revision_pos_line_id     NUMBER;
3723   l_global_revision_id              NUMBER;
3724 
3725   cursor c_seq is
3726     select psb_budget_revision_pos_line_s.nextval seq
3727       from dual;
3728 
3729 BEGIN
3730 
3731   for c_seq_rec in c_seq loop
3732     l_budget_revision_pos_line_id := c_seq_rec.seq;
3733   end loop;
3734 
3735   INSERT INTO PSB_BUDGET_REVISION_POSITIONS
3736         (budget_revision_pos_line_id, position_id, budget_group_id, effective_start_date,
3737          effective_end_date, revision_type, revision_value_type, revision_value, note_id,
3738          last_update_date, last_updated_by, last_update_login, created_by, creation_date)
3739   VALUES (l_budget_revision_pos_line_id, p_position_id, p_budget_group_id, p_effective_start_date,
3740           p_effective_end_date, p_revision_type, p_revision_value_type, p_revision_value, p_note_id,
3741           sysdate, FND_GLOBAL.USER_ID, FND_GLOBAL.LOGIN_ID, FND_GLOBAL.USER_ID, sysdate);
3742 
3743   for c_Global_Rev_Rec in c_Global_Rev (p_budget_revision_id) loop
3744     l_global_revision_id := c_Global_Rev_Rec.global_revision_id;
3745   end loop;
3746 
3747   -- this is used to propagate new budget revision entries created at any level to all the distributed levels
3748 
3749   for c_Distribute_Rev_Rec in c_Distribute_Rev (l_global_revision_id, p_budget_group_id) loop
3750 
3751     INSERT INTO PSB_BUDGET_REVISION_POS_LINES (budget_revision_pos_line_id, budget_revision_id,
3752            freeze_flag, view_line_flag, last_update_date, last_updated_by, last_update_login,
3753            created_by, creation_date)
3754     VALUES (l_budget_revision_pos_line_id, c_Distribute_Rev_Rec.budget_revision_id,
3755             p_freeze_flag, p_view_line_flag, sysdate, FND_GLOBAL.USER_ID, FND_GLOBAL.LOGIN_ID,
3756             FND_GLOBAL.USER_ID, sysdate);
3757 
3758   end loop;
3759 
3760   p_budget_revision_pos_line_id := l_budget_revision_pos_line_id;
3761 
3762   -- Initialize API return status to success
3763 
3764   p_return_status := FND_API.G_RET_STS_SUCCESS;
3765 
3766 EXCEPTION
3767 
3768    when FND_API.G_EXC_ERROR then
3769      p_return_status := FND_API.G_RET_STS_ERROR;
3770 
3771    when FND_API.G_EXC_UNEXPECTED_ERROR then
3772      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3773 
3774    when OTHERS then
3775      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3776 
3777 End Insert_Revision_Positions;
3778 
3779 /* ----------------------------------------------------------------------- */
3780 
3781 PROCEDURE Update_Revision_Positions
3782 ( p_return_status                OUT  NOCOPY  VARCHAR2,
3783   p_budget_revision_pos_line_id  IN   NUMBER,
3784   p_budget_group_id              IN   NUMBER,
3785   p_effective_start_date         IN   DATE := FND_API.G_MISS_DATE,
3786   p_effective_end_date           IN   DATE := FND_API.G_MISS_DATE,
3787   p_revision_type                IN   VARCHAR2,
3788   p_revision_value_type          IN   VARCHAR2,
3789   p_revision_value               IN   NUMBER,
3790   p_note_id                      IN   NUMBER
3791 ) IS
3792 
3793 BEGIN
3794 
3795   update PSB_BUDGET_REVISION_POSITIONS
3796      set budget_group_id = p_budget_group_id,
3797          effective_start_date = decode(p_effective_start_date, FND_API.G_MISS_DATE, effective_start_date, p_effective_start_date),
3798          effective_end_date = decode(p_effective_end_date, FND_API.G_MISS_DATE, effective_end_date, p_effective_end_date),
3799          revision_type = p_revision_type,
3800          revision_value_type = p_revision_value_type,
3801          revision_value = p_revision_value,
3802          note_id = decode(p_note_id, null, note_id, p_note_id),
3803          last_update_date = sysdate,
3804          last_updated_by = FND_GLOBAL.USER_ID,
3805          last_update_login = FND_GLOBAL.LOGIN_ID
3806    WHERE budget_revision_pos_line_id  = p_budget_revision_pos_line_id;
3807 
3808   -- Initialize API return status to success
3809 
3810   p_return_status := FND_API.G_RET_STS_SUCCESS;
3811 
3812 EXCEPTION
3813 
3814    when FND_API.G_EXC_ERROR then
3815      p_return_status := FND_API.G_RET_STS_ERROR;
3816 
3817    when FND_API.G_EXC_UNEXPECTED_ERROR then
3818      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3819 
3820    when OTHERS then
3821      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3822 
3823 End Update_Revision_Positions;
3824 
3825 /* ----------------------------------------------------------------------- */
3826 
3827 PROCEDURE Delete_Revision_Positions
3828 ( p_return_status                OUT  NOCOPY  VARCHAR2,
3829   p_budget_revision_pos_line_id  IN   NUMBER
3830 ) IS
3831 
3832 BEGIN
3833 
3834   delete from PSB_BUDGET_REVISION_POSITIONS
3835    where budget_revision_pos_line_id = p_budget_revision_pos_line_id;
3836 
3837   -- Initialize API return status to success
3838 
3839   p_return_status := FND_API.G_RET_STS_SUCCESS;
3840 
3841 EXCEPTION
3842 
3843    when FND_API.G_EXC_ERROR then
3844      p_return_status := FND_API.G_RET_STS_ERROR;
3845 
3846    when FND_API.G_EXC_UNEXPECTED_ERROR then
3847      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3848 
3849    when OTHERS then
3850      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3851 
3852 End Delete_Revision_Positions;
3853 
3854 /* ----------------------------------------------------------------------- */
3855 
3856 PROCEDURE Create_Revision_Positions
3857 ( p_api_version                     IN      NUMBER,
3858   p_init_msg_list                   IN      VARCHAR2 := FND_API.G_FALSE,
3859   p_commit                          IN      VARCHAR2 := FND_API.G_FALSE,
3860   p_validation_level                IN      NUMBER   := FND_API.G_VALID_LEVEL_FULL,
3861   p_return_status                   OUT  NOCOPY     VARCHAR2,
3862   p_msg_count                       OUT  NOCOPY     NUMBER,
3863   p_msg_data                        OUT  NOCOPY     VARCHAR2,
3864   p_budget_revision_id              IN      NUMBER,
3865   p_budget_revision_pos_line_id     IN  OUT  NOCOPY NUMBER,
3866   p_position_id                     IN      NUMBER,
3867   p_budget_group_id                 IN      NUMBER,
3868   p_effective_start_date            IN      DATE,
3869   p_effective_end_date              IN      DATE,
3870   p_revision_type                   IN      VARCHAR2,
3871   p_revision_value_type             IN      VARCHAR2,
3872   p_revision_value                  IN      NUMBER,
3873   p_note_id                         IN      NUMBER,
3874   p_freeze_flag                     IN      VARCHAR2,
3875   p_view_line_flag                  IN      VARCHAR2
3876 ) IS
3877 
3878   l_api_name                        CONSTANT VARCHAR2(30) := 'Create_Revision_Positions';
3879   l_api_version                     CONSTANT NUMBER       := 1.0;
3880 
3881   l_created_record                  BOOLEAN := FALSE;
3882   l_updated_record                  BOOLEAN;
3883 
3884   l_budget_revision_pos_line_id     NUMBER;
3885   l_return_status                   VARCHAR2(1);
3886 
3887   cursor c_Overlap is
3888     select pbrp.*
3889       from psb_budget_revision_positions pbrp,
3890            psb_budget_revision_pos_lines pbrl
3891      where pbrp.position_id = p_position_id
3892        and ((((p_effective_end_date is not null)
3893          and ((pbrp.effective_start_date <= p_effective_end_date)
3894           and (pbrp.effective_end_date is null))
3895           or ((pbrp.effective_start_date between p_effective_start_date and p_effective_end_date)
3896            or (pbrp.effective_end_date between p_effective_start_date and p_effective_end_date)
3897           or ((pbrp.effective_start_date < p_effective_start_date)
3898           and (pbrp.effective_end_date > p_effective_end_date)))))
3899           or ((p_effective_end_date is null)
3900           and (nvl(pbrp.effective_end_date, p_effective_start_date) >= p_effective_start_date)))
3901        and pbrl.budget_revision_id = p_budget_revision_id
3902        and pbrp.budget_revision_pos_line_id = pbrl.budget_revision_pos_line_id;
3903 
3904 BEGIN
3905 
3906   SAVEPOINT Create_Revision_Positions;
3907 
3908   -- Standard call to check for call compatibility.
3909 
3910   IF NOT FND_API.Compatible_API_Call (l_api_version,
3911                                       p_api_version,
3912                                       l_api_name,
3913                                       G_PKG_NAME)
3914   THEN
3915     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3916   END IF;
3917 
3918   -- Initialize message list if p_init_msg_list is set to TRUE.
3919 
3920   IF FND_API.to_Boolean (p_init_msg_list) THEN
3921     FND_MSG_PUB.initialize;
3922   END IF;
3923 
3924   update PSB_BUDGET_REVISION_POSITIONS brp
3925      set budget_group_id = p_budget_group_id,
3926          revision_type = p_revision_type,
3927          revision_value_type = p_revision_value_type,
3928          revision_value = p_revision_value,
3929          last_update_date = sysdate,
3930          last_updated_by = FND_GLOBAL.USER_ID,
3931          last_update_login = FND_GLOBAL.LOGIN_ID
3932    where position_id = p_position_id
3933      and effective_start_date = p_effective_start_date
3934      and nvl(effective_end_date, FND_API.G_MISS_DATE) = nvl(p_effective_end_date, FND_API.G_MISS_DATE)
3935      and exists
3936         (select 1
3937            from PSB_BUDGET_REVISION_POS_LINES brpl
3938           where brpl.budget_revision_id = p_budget_revision_id
3939             and brpl.budget_revision_pos_line_id = brp.budget_revision_pos_line_id);
3940 
3941   if SQL%NOTFOUND then
3942   begin
3943 
3944     for l_init_index in 1..g_revpos.Count loop
3945       g_revpos(l_init_index).budget_revision_pos_line_id := null;
3946       g_revpos(l_init_index).position_id := null;
3947       g_revpos(l_init_index).budget_group_id := null;
3948       g_revpos(l_init_index).effective_start_date := null;
3949       g_revpos(l_init_index).effective_end_date := null;
3950       g_revpos(l_init_index).revision_type := null;
3951       g_revpos(l_init_index).revision_value_type := null;
3952       g_revpos(l_init_index).revision_value := null;
3953       g_revpos(l_init_index).note_id := null;
3954       g_revpos(l_init_index).delete_flag := null;
3955     end loop;
3956 
3957     g_num_revpos := 0;
3958 
3959     for c_Overlap_Rec in c_Overlap loop
3960       g_num_revpos := g_num_revpos + 1;
3961 
3962       g_revpos(g_num_revpos).budget_revision_pos_line_id := c_Overlap_Rec.budget_revision_pos_line_id;
3963       g_revpos(g_num_revpos).position_id := c_Overlap_Rec.position_id;
3964       g_revpos(g_num_revpos).budget_group_id := c_Overlap_Rec.budget_group_id;
3965       g_revpos(g_num_revpos).effective_start_date := c_Overlap_Rec.effective_start_date;
3966       g_revpos(g_num_revpos).effective_end_date := c_Overlap_Rec.effective_end_date;
3967       g_revpos(g_num_revpos).revision_type := c_Overlap_Rec.revision_type;
3968       g_revpos(g_num_revpos).revision_value_type := c_Overlap_Rec.revision_value_type;
3969       g_revpos(g_num_revpos).revision_value := c_Overlap_Rec.revision_value;
3970       g_revpos(g_num_revpos).note_id := c_Overlap_Rec.note_id;
3971       g_revpos(g_num_revpos).delete_flag := TRUE;
3972     end loop;
3973 
3974     if g_num_revpos = 0 then
3975     begin
3976 
3977       Insert_Revision_Positions
3978            (p_return_status => l_return_status,
3979             p_budget_revision_pos_line_id => l_budget_revision_pos_line_id,
3980             p_budget_revision_id => p_budget_revision_id,
3981             p_position_id => p_position_id,
3982             p_budget_group_id => p_budget_group_id,
3983             p_effective_start_date => p_effective_start_date,
3984             p_effective_end_date => p_effective_end_date,
3985             p_revision_type => p_revision_type,
3986             p_revision_value_type => p_revision_value_type,
3987             p_revision_value => p_revision_value,
3988             p_note_id => p_note_id,
3989             p_freeze_flag => p_freeze_flag,
3990             p_view_line_flag => p_view_line_flag);
3991 
3992         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3993           raise FND_API.G_EXC_ERROR;
3994         end if;
3995 
3996         p_budget_revision_pos_line_id := l_budget_revision_pos_line_id;
3997 
3998     end;
3999     else
4000     begin
4001 
4002       for l_revpos_index in 1..g_num_revpos loop
4003 
4004         l_updated_record := FALSE;
4005 
4006         /* Effective Start Date Matches */
4007 
4008         if g_revpos(l_revpos_index).effective_start_date = p_effective_start_date then
4009         begin
4010 
4011           Update_Revision_Positions
4012                 (p_return_status => l_return_status,
4013                  p_budget_revision_pos_line_id => g_revpos(l_revpos_index).budget_revision_pos_line_id,
4014                  p_budget_group_id => p_budget_group_id,
4015                  p_effective_end_date => p_effective_end_date,
4016                  p_revision_type => g_revpos(l_revpos_index).revision_type,
4017                  p_revision_value_type => g_revpos(l_revpos_index).revision_value_type,
4018                  p_revision_value => g_revpos(l_revpos_index).revision_value,
4019                  p_note_id => g_revpos(l_revpos_index).note_id);
4020 
4021           if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4022             raise FND_API.G_EXC_ERROR;
4023           end if;
4024 
4025           g_revpos(l_revpos_index).delete_flag := FALSE;
4026 
4027         end;
4028 
4029         /* Effective Dates Overlap */
4030         elsif (((g_revpos(l_revpos_index).effective_start_date <= (p_effective_start_date - 1)) and
4031                ((g_revpos(l_revpos_index).effective_end_date is null) or
4032                 (g_revpos(l_revpos_index).effective_end_date > (p_effective_start_date - 1)))) or
4033                ((g_revpos(l_revpos_index).effective_start_date > p_effective_start_date) and
4034                ((g_revpos(l_revpos_index).effective_end_date is null) or
4035                 (g_revpos(l_revpos_index).effective_end_date > (p_effective_end_date + 1))))) then
4036         begin
4037 
4038           if ((g_revpos(l_revpos_index).effective_start_date < (p_effective_start_date - 1)) and
4039              ((g_revpos(l_revpos_index).effective_end_date is null) or
4040               (g_revpos(l_revpos_index).effective_end_date > (p_effective_start_date - 1)))) then
4041           begin
4042 
4043             Update_Revision_Positions
4044                   (p_return_status => l_return_status,
4045                    p_budget_revision_pos_line_id => g_revpos(l_revpos_index).budget_revision_pos_line_id,
4046                    p_budget_group_id => p_budget_group_id,
4047                    p_effective_end_date => p_effective_start_date - 1,
4048                    p_revision_type => g_revpos(l_revpos_index).revision_type,
4049                    p_revision_value_type => g_revpos(l_revpos_index).revision_value_type,
4050                    p_revision_value => g_revpos(l_revpos_index).revision_value,
4051                    p_note_id => g_revpos(l_revpos_index).note_id);
4052 
4053             if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4054               raise FND_API.G_EXC_ERROR;
4055             else
4056               l_updated_record := TRUE;
4057             end if;
4058 
4059             g_revpos(l_revpos_index).delete_flag := FALSE;
4060 
4061           end;
4062           elsif ((g_revpos(l_revpos_index).effective_start_date > p_effective_start_date) and
4063                 ((p_effective_end_date is not null) and
4064                 ((g_revpos(l_revpos_index).effective_end_date is null) or
4065                  (g_revpos(l_revpos_index).effective_end_date > (p_effective_end_date + 1))))) then
4066           begin
4067 
4068             Update_Revision_Positions
4069                   (p_return_status => l_return_status,
4070                    p_budget_revision_pos_line_id => g_revpos(l_revpos_index).budget_revision_pos_line_id,
4071                    p_budget_group_id => p_budget_group_id,
4072                    p_effective_start_date => p_effective_end_date + 1,
4073                    p_revision_type => g_revpos(l_revpos_index).revision_type,
4074                    p_revision_value_type => g_revpos(l_revpos_index).revision_value_type,
4075                    p_revision_value => g_revpos(l_revpos_index).revision_value,
4076                    p_note_id => g_revpos(l_revpos_index).note_id);
4077 
4078             if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4079               raise FND_API.G_EXC_ERROR;
4080             else
4081               l_updated_record := FALSE;
4082             end if;
4083 
4084             g_revpos(l_revpos_index).delete_flag := FALSE;
4085 
4086           end;
4087           end if;
4088 
4089           if not l_created_record then
4090           begin
4091 
4092             Insert_Revision_Positions
4093                  (p_return_status => l_return_status,
4094                   p_budget_revision_pos_line_id => l_budget_revision_pos_line_id,
4095                   p_budget_revision_id => p_budget_revision_id,
4096                   p_position_id => p_position_id,
4097                   p_budget_group_id => p_budget_group_id,
4098                   p_effective_start_date => p_effective_start_date,
4099                   p_effective_end_date => p_effective_end_date,
4100                   p_revision_type => p_revision_type,
4101                   p_revision_value_type => p_revision_value_type,
4102                   p_revision_value => p_revision_value,
4103                   p_note_id => p_note_id,
4104                   p_freeze_flag => p_freeze_flag,
4105                   p_view_line_flag => p_view_line_flag);
4106 
4107             if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4108               raise FND_API.G_EXC_ERROR;
4109             else
4110               l_created_record := TRUE;
4111             end if;
4112 
4113             p_budget_revision_pos_line_id := l_budget_revision_pos_line_id;
4114 
4115           end;
4116           end if;
4117 
4118           if p_effective_end_date is not null then
4119           begin
4120 
4121             if nvl(g_revpos(l_revpos_index).effective_end_date, (p_effective_end_date + 1)) > (p_effective_end_date + 1) then
4122             begin
4123 
4124               if l_updated_record then
4125               begin
4126 
4127                 Insert_Revision_Positions
4128                      (p_return_status => l_return_status,
4129                       p_budget_revision_pos_line_id => l_budget_revision_pos_line_id,
4130                       p_budget_revision_id => p_budget_revision_id,
4131                       p_position_id => p_position_id,
4132                       p_budget_group_id => p_budget_group_id,
4133                       p_effective_start_date => p_effective_end_date + 1,
4134                       p_effective_end_date => g_revpos(l_revpos_index).effective_end_date,
4135                       p_revision_type => p_revision_type,
4136                       p_revision_value_type => p_revision_value_type,
4137                       p_revision_value => p_revision_value,
4138                       p_note_id => p_note_id,
4139                       p_freeze_flag => p_freeze_flag,
4140                       p_view_line_flag => p_view_line_flag);
4141 
4142                 if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4143                   raise FND_API.G_EXC_ERROR;
4144                 end if;
4145 
4146               end;
4147               else
4148               begin
4149 
4150                 Update_Revision_Positions
4151                       (p_return_status => l_return_status,
4152                        p_budget_revision_pos_line_id => g_revpos(l_revpos_index).budget_revision_pos_line_id,
4153                        p_budget_group_id => p_budget_group_id,
4154                        p_effective_start_date => p_effective_end_date + 1,
4155                        p_effective_end_date => g_revpos(l_revpos_index).effective_end_date,
4156                        p_revision_type => g_revpos(l_revpos_index).revision_type,
4157                        p_revision_value_type => g_revpos(l_revpos_index).revision_value_type,
4158                        p_revision_value => g_revpos(l_revpos_index).revision_value,
4159                        p_note_id => g_revpos(l_revpos_index).note_id);
4160 
4161                 if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4162                   raise FND_API.G_EXC_ERROR;
4163                 end if;
4164 
4165                 g_revpos(l_revpos_index).delete_flag := FALSE;
4166 
4167               end;
4168               end if;
4169 
4170             end;
4171             end if;
4172 
4173           end;
4174           end if;
4175 
4176         end;
4177         end if;
4178 
4179       end loop;
4180 
4181     end;
4182     end if;
4183 
4184     for l_revpos_index in 1..g_num_revpos loop
4185 
4186       if g_revpos(l_revpos_index).delete_flag then
4187       begin
4188 
4189         Delete_Revision_Positions
4190               (p_return_status => l_return_status,
4191                p_budget_revision_pos_line_id => g_revpos(l_revpos_index).budget_revision_pos_line_id);
4192 
4193         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4194           raise FND_API.G_EXC_ERROR;
4195         end if;
4196 
4197       end;
4198       end if;
4199 
4200     end loop;
4201 
4202   end;
4203   end if;
4204 
4205   -- Initialize API return status to success
4206 
4207   p_return_status := FND_API.G_RET_STS_SUCCESS;
4208 
4209   -- Standard check of p_commit.
4210 
4211   if FND_API.to_Boolean (p_commit) then
4212     commit work;
4213   end if;
4214 
4215   -- Standard call to get message count and if count is 1, get message info.
4216 
4217   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
4218                              p_data  => p_msg_data);
4219 
4220 EXCEPTION
4221 
4222    when FND_API.G_EXC_ERROR then
4223      rollback to Create_Revision_Positions;
4224      p_return_status := FND_API.G_RET_STS_ERROR;
4225 
4226      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
4227                                 p_data  => p_msg_data);
4228 
4229    when FND_API.G_EXC_UNEXPECTED_ERROR then
4230      rollback to Create_Revision_Positions;
4231      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4232 
4233      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
4234                                 p_data  => p_msg_data);
4235 
4236    when OTHERS then
4237      rollback to Create_Revision_Positions;
4238      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4239 
4240      if FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
4241        FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
4242                                 l_api_name);
4243      end if;
4244 
4245      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
4246                                 p_data  => p_msg_data);
4247 
4248 End Create_Revision_Positions;
4249 
4250 
4251 /* ----------------------------------------------------------------------- */
4252 
4253 PROCEDURE Initialize_Revisions IS
4254 BEGIN
4255 
4256   -- Initialize the entire structure
4257 
4258   for l_init_index in 1..g_costs.Count loop
4259     g_costs(l_init_index).pay_element_id := null;
4260     g_costs(l_init_index).element_type := null;
4261     g_costs(l_init_index).element_cost := null;
4262     g_costs(l_init_index).currency_code := null;
4263     g_costs(l_init_index).start_date := null;
4264     g_costs(l_init_index).end_date := null;
4265   end loop;
4266 
4267   g_num_costs := 0;
4268 
4269   for l_init_index in 1..g_dists.Count loop
4270      g_dists(l_init_index).ccid := null;
4271      g_dists(l_init_index).budget_group_id := null;
4272      g_dists(l_init_index).currency_code := null;
4273      g_dists(l_init_index).start_date := null;
4274      g_dists(l_init_index).end_date := null;
4275      g_dists(l_init_index).amount := null;
4276      g_dists(l_init_index).calc_rev := null;
4277   end loop;
4278 
4279   g_num_dists := 0;
4280 
4281   for l_init_index in 1..g_revaccts.count loop
4282     g_revaccts(l_init_index).ccid := null;
4283     g_revaccts(l_init_index).amount := null;
4284   end loop;
4285 
4286   g_num_revaccts := 0;
4287 
4288   for l_init_index in 1..g_elem_assignments.Count loop
4289     g_elem_assignments(l_init_index).budget_revision_id := null;
4290     g_elem_assignments(l_init_index).start_date := null;
4291     g_elem_assignments(l_init_index).end_date := null;
4292     g_elem_assignments(l_init_index).pay_element_id := null;
4293     g_elem_assignments(l_init_index).pay_element_option_id := null;
4294     g_elem_assignments(l_init_index).pay_basis := null;
4295     g_elem_assignments(l_init_index).element_value_type := null;
4296     g_elem_assignments(l_init_index).element_value := null;
4297     g_elem_assignments(l_init_index).use_in_calc := null;
4298   end loop;
4299 
4300   g_num_elem_assignments := 0;
4301 
4302   for l_init_index in 1..g_elem_rates.Count loop
4303     g_elem_rates(l_init_index).budget_revision_id := null;
4304     g_elem_rates(l_init_index).start_date := null;
4305     g_elem_rates(l_init_index).end_date := null;
4306     g_elem_rates(l_init_index).pay_element_id := null;
4307     g_elem_rates(l_init_index).pay_element_option_id := null;
4308     g_elem_rates(l_init_index).pay_basis := null;
4309     g_elem_rates(l_init_index).element_value_type := null;
4310     g_elem_rates(l_init_index).element_value := null;
4311     g_elem_rates(l_init_index).formula_id := null;
4312   end loop;
4313 
4314   g_num_elem_rates := 0;
4315 
4316   for l_init_index in 1..g_wkh_assignments.Count loop
4317     g_wkh_assignments(l_init_index).start_date := null;
4318     g_wkh_assignments(l_init_index).end_date := null;
4319     g_wkh_assignments(l_init_index).default_weekly_hours := null;
4320   end loop;
4321 
4322   g_num_wkh_assignments := 0;
4323 
4324   for l_init_index in 1..g_fte_assignments.Count loop
4325     g_fte_assignments(l_init_index).start_date := null;
4326     g_fte_assignments(l_init_index).end_date := null;
4327     g_fte_assignments(l_init_index).fte := null;
4328   end loop;
4329 
4330   g_num_fte_assignments := 0;
4331 
4332   PSB_WS_POS1.g_salary_budget_group_id := null;
4333   PSB_WS_POS1.Initialize_Salary_Dist;
4334 
4335   for l_init_index in 1..PSB_WS_POS1.g_elements.Count loop
4336     PSB_WS_POS1.g_elements(l_init_index).pay_element_id := null;
4337     PSB_WS_POS1.g_elements(l_init_index).element_name := null;
4338     PSB_WS_POS1.g_elements(l_init_index).processing_type := null;
4339     PSB_WS_POS1.g_elements(l_init_index).max_element_value_type := null;
4340     PSB_WS_POS1.g_elements(l_init_index).max_element_value := null;
4341     PSB_WS_POS1.g_elements(l_init_index).salary_flag := null;
4342     PSB_WS_POS1.g_elements(l_init_index).option_flag := null;
4343     PSB_WS_POS1.g_elements(l_init_index).overwrite_flag := null;
4344     PSB_WS_POS1.g_elements(l_init_index).salary_type := null;
4345     PSB_WS_POS1.g_elements(l_init_index).follow_salary := null;
4346     PSB_WS_POS1.g_elements(l_init_index).period_type := null;
4347     PSB_WS_POS1.g_elements(l_init_index).process_period_type := null;
4348   end loop;
4349 
4350   PSB_WS_POS1.g_num_elements := 0;
4351 
4352 End Initialize_Revisions;
4353 
4354 /* ----------------------------------------------------------------------- */
4355 
4356 PROCEDURE Cache_Elements
4357 (p_return_status      OUT  NOCOPY VARCHAR2,
4358  p_start_date         IN  DATE,
4359  p_end_date           IN  DATE) IS
4360 
4361   cursor c_Elements is
4362     select pay_element_id,
4363            name,
4364            processing_type,
4365            max_element_value_type,
4366            max_element_value,
4367            option_flag,
4368            overwrite_flag,
4369            salary_flag,
4370            salary_type,
4371            follow_salary,
4372            period_type,
4373            process_period_type
4374       from PSB_PAY_ELEMENTS
4375      where data_extract_id = g_data_extract_id
4376        and business_group_id = g_business_group_id
4377        and ((start_date >= p_start_date) and ((start_date <= p_end_date)
4378          or (end_date is null))
4379          or ((start_date between p_start_date and p_end_date))
4380          or (p_start_date between  start_date and nvl(end_date,p_end_date)))
4381      order by salary_flag desc,
4382               pay_element_id;
4383 BEGIN
4384 
4385   for c_Elements_Rec in c_Elements loop
4386 
4387     PSB_WS_POS1.g_num_elements := PSB_WS_POS1.g_num_elements + 1;
4388 
4389     PSB_WS_POS1.g_elements(PSB_WS_POS1.g_num_elements).pay_element_id := c_Elements_Rec.pay_element_id;
4390     PSB_WS_POS1.g_elements(PSB_WS_POS1.g_num_elements).element_name := c_Elements_Rec.name;
4391     PSB_WS_POS1.g_elements(PSB_WS_POS1.g_num_elements).processing_type := c_Elements_Rec.processing_type;
4392     PSB_WS_POS1.g_elements(PSB_WS_POS1.g_num_elements).max_element_value_type := c_Elements_Rec.max_element_value_type;
4393     PSB_WS_POS1.g_elements(PSB_WS_POS1.g_num_elements).max_element_value := c_Elements_Rec.max_element_value;
4394     PSB_WS_POS1.g_elements(PSB_WS_POS1.g_num_elements).option_flag := c_Elements_Rec.option_flag;
4395     PSB_WS_POS1.g_elements(PSB_WS_POS1.g_num_elements).overwrite_flag := c_Elements_Rec.overwrite_flag;
4396     PSB_WS_POS1.g_elements(PSB_WS_POS1.g_num_elements).salary_flag := c_Elements_Rec.salary_flag;
4397     PSB_WS_POS1.g_elements(PSB_WS_POS1.g_num_elements).salary_type := c_Elements_Rec.salary_type;
4398     PSB_WS_POS1.g_elements(PSB_WS_POS1.g_num_elements).follow_salary := c_Elements_Rec.follow_salary;
4399     PSB_WS_POS1.g_elements(PSB_WS_POS1.g_num_elements).period_type := c_Elements_Rec.period_type;
4400     PSB_WS_POS1.g_elements(PSB_WS_POS1.g_num_elements).process_period_type := c_Elements_Rec.process_period_type;
4401   end loop;
4402 
4403   p_return_status := FND_API.G_RET_STS_SUCCESS;
4404 
4405 EXCEPTION
4406 
4407    when FND_API.G_EXC_ERROR then
4408      p_return_status := FND_API.G_RET_STS_ERROR;
4409 
4410    when FND_API.G_EXC_UNEXPECTED_ERROR then
4411      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4412 
4413    when OTHERS then
4414      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4415 
4416 End Cache_Elements;
4417 
4418 /* ----------------------------------------------------------------------- */
4419 
4420 -- Cache Salary Distribution for a Position for specific date range
4421 
4422 PROCEDURE Cache_Salary_Dist
4423 ( p_return_status         OUT  NOCOPY  VARCHAR2,
4424   p_budget_revision_id    IN   NUMBER,
4425   p_position_id           IN   NUMBER,
4426   p_position_name         IN   VARCHAR2,
4427   p_start_date            IN   DATE,
4428   p_end_date              IN   DATE
4429 ) IS
4430 
4431   l_saldist_found         BOOLEAN := FALSE;
4432   l_budget_group_found    BOOLEAN := FALSE;
4433 
4434   l_concat_segments       VARCHAR2(2000);
4435 
4436   l_return_status         VARCHAR2(1);
4437 
4438   cursor c_WSDist is
4439     select code_combination_id,
4440            distribution_percent,
4441            effective_start_date,
4442            effective_end_date
4443       from PSB_POSITION_PAY_DISTRIBUTIONS
4444      where position_id = p_position_id
4445        and worksheet_id = p_budget_revision_id
4446        and code_combination_id is not null
4447        and chart_of_accounts_id = g_flex_code
4448        and (((p_end_date is not null)
4449          and (((effective_start_date <= p_end_date)
4450            and (effective_end_date is null))
4451            or ((effective_start_date between p_start_date and p_end_date)
4452            or (effective_end_date between p_start_date and p_end_date)
4453            or ((effective_start_date < p_start_date)
4454            and (effective_end_date > p_end_date)))))
4455         or ((p_end_date is null)
4456         and (nvl(effective_end_date, p_start_date) >= p_start_date)))
4457      order by distribution_percent desc;
4458 
4459   cursor c_Dist is
4460     select code_combination_id,
4461            distribution_percent,
4462            effective_start_date,
4463            effective_end_date
4464       from PSB_POSITION_PAY_DISTRIBUTIONS
4465      where position_id = p_position_id
4466        and worksheet_id is null
4467        and code_combination_id is not null
4468        and chart_of_accounts_id = g_flex_code
4469        and (((p_end_date is not null)
4470          and (((effective_start_date <= p_end_date)
4471            and (effective_end_date is null))
4472            or ((effective_start_date between p_start_date and p_end_date)
4473            or (effective_end_date between p_start_date and p_end_date)
4474            or ((effective_start_date < p_start_date)
4475            and (effective_end_date > p_end_date)))))
4476         or ((p_end_date is null)
4477         and (nvl(effective_end_date, p_start_date) >= p_start_date)))
4478      order by distribution_percent desc;
4479 
4480   cursor c_Budget_Group (CCID NUMBER) is
4481     select a.budget_group_id,
4482            b.num_proposed_years
4483       from PSB_SET_RELATIONS a,
4484            PSB_BUDGET_GROUPS b,
4485            PSB_BUDGET_ACCOUNTS c
4486      where a.budget_group_id = b.budget_group_id
4487        and b.effective_start_date <= p_start_date
4488        and (b.effective_end_date is null
4489          or b.effective_end_date >= p_end_date)
4490        and b.budget_group_type = 'R'
4491        and ((b.budget_group_id = g_root_budget_group_id) or
4492             (b.root_budget_group_id = g_root_budget_group_id))
4493        and a.account_position_set_id = c.account_position_set_id
4494        and c.code_combination_id = CCID;
4495 
4496 BEGIN
4497 
4498   for c_Dist_Rec in c_WSDist loop
4499 
4500     l_saldist_found := TRUE;
4501     g_revised_position := TRUE;
4502 
4503     if nvl(PSB_WS_POS1.g_salary_budget_group_id, FND_API.G_MISS_NUM) = FND_API.G_MISS_NUM then
4504     begin
4505 
4506       for c_Budget_Group_Rec in c_Budget_Group (c_Dist_Rec.code_combination_id) loop
4507         PSB_WS_POS1.g_salary_budget_group_id := c_Budget_Group_Rec.budget_group_id;
4508         l_budget_group_found := TRUE;
4509       end loop;
4510 
4511       -- Budget Group for a Position is the Budget Group assigned to the CCID with
4512       -- the maximum distribution percentage
4513 
4514       if not l_budget_group_found then
4515       begin
4516 
4517         l_concat_segments := FND_FLEX_EXT.Get_Segs
4518                                 (application_short_name => 'SQLGL',
4519                                  key_flex_code => 'GL#',
4520                                  structure_number => g_flex_code,
4521                                  combination_id => c_Dist_Rec.code_combination_id);
4522 
4523         message_token('CCID', l_concat_segments);
4524         message_token('POSITION', p_position_name);
4525         add_message('PSB', 'PSB_CANNOT_ASSIGN_BUDGET_GROUP');
4526         raise FND_API.G_EXC_ERROR;
4527 
4528       end;
4529       end if;
4530 
4531     end;
4532     end if;
4533 
4534     PSB_WS_POS1.g_num_salary_dist := PSB_WS_POS1.g_num_salary_dist + 1;
4535 
4536     PSB_WS_POS1.g_salary_dist(PSB_WS_POS1.g_num_salary_dist).ccid := c_Dist_Rec.code_combination_id;
4537     PSB_WS_POS1.g_salary_dist(PSB_WS_POS1.g_num_salary_dist).percent := c_Dist_Rec.distribution_percent;
4538     PSB_WS_POS1.g_salary_dist(PSB_WS_POS1.g_num_salary_dist).start_date := c_Dist_Rec.effective_start_date;
4539     PSB_WS_POS1.g_salary_dist(PSB_WS_POS1.g_num_salary_dist).end_date := c_Dist_Rec.effective_end_date;
4540 
4541   end loop;
4542 
4543   if not l_saldist_found then
4544   begin
4545 
4546     for c_Dist_Rec in c_Dist loop
4547 
4548       l_saldist_found := TRUE;
4549 
4550       if nvl(PSB_WS_POS1.g_salary_budget_group_id, FND_API.G_MISS_NUM) = FND_API.G_MISS_NUM then
4551       begin
4552 
4553         for c_Budget_Group_Rec in c_Budget_Group (c_Dist_Rec.code_combination_id) loop
4554           PSB_WS_POS1.g_salary_budget_group_id := c_Budget_Group_Rec.budget_group_id;
4555           l_budget_group_found := TRUE;
4556         end loop;
4557 
4558         -- Budget Group for a Position is the Budget Group assigned to the CCID with
4559         -- the maximum distribution percentage
4560 
4561         if not l_budget_group_found then
4562         begin
4563 
4564           l_concat_segments := FND_FLEX_EXT.Get_Segs
4565                                   (application_short_name => 'SQLGL',
4566                                    key_flex_code => 'GL#',
4567                                    structure_number => g_flex_code,
4568                                    combination_id => c_Dist_Rec.code_combination_id);
4569 
4570           message_token('CCID', l_concat_segments);
4571           message_token('POSITION', p_position_name);
4572           add_message('PSB', 'PSB_CANNOT_ASSIGN_BUDGET_GROUP');
4573           raise FND_API.G_EXC_ERROR;
4574 
4575         end;
4576         end if;
4577 
4578       end;
4579       end if;
4580 
4581       PSB_WS_POS1.g_num_salary_dist := PSB_WS_POS1.g_num_salary_dist + 1;
4582 
4583       PSB_WS_POS1.g_salary_dist(PSB_WS_POS1.g_num_salary_dist).ccid := c_Dist_Rec.code_combination_id;
4584       PSB_WS_POS1.g_salary_dist(PSB_WS_POS1.g_num_salary_dist).percent := c_Dist_Rec.distribution_percent;
4585       PSB_WS_POS1.g_salary_dist(PSB_WS_POS1.g_num_salary_dist).start_date := c_Dist_Rec.effective_start_date;
4586       PSB_WS_POS1.g_salary_dist(PSB_WS_POS1.g_num_salary_dist).end_date := c_Dist_Rec.effective_end_date;
4587 
4588     end loop;
4589 
4590   end;
4591   end if;
4592 
4593   -- If Salary Distribution is not found return an error. Salary Distribution is
4594   -- needed to create a Worksheet specific instance of a Position (identified by
4595   -- position_line_id)
4596 
4597   if not l_saldist_found then
4598     message_token('POSITION', p_position_name);
4599     message_token('START_DATE', p_start_date);
4600     message_token('END_DATE', p_end_date);
4601     add_message('PSB', 'PSB_NO_SALARY_DISTRIBUTION');
4602     raise FND_API.G_EXC_ERROR;
4603   end if;
4604 
4605   if g_flex_code <> nvl(PSB_WS_ACCT1.g_flex_code, FND_API.G_MISS_NUM) then
4606   begin
4607 
4608     PSB_WS_ACCT1.Flex_Info
4609        (p_flex_code => g_flex_code,
4610         p_return_status => l_return_status);
4611 
4612     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4613       raise FND_API.G_EXC_ERROR;
4614     end if;
4615 
4616   end;
4617   end if;
4618 
4619 
4620   -- Initialize API return status to success
4621 
4622   p_return_status := FND_API.G_RET_STS_SUCCESS;
4623 
4624 
4625 EXCEPTION
4626 
4627    when FND_API.G_EXC_ERROR then
4628      p_return_status := FND_API.G_RET_STS_ERROR;
4629 
4630    when FND_API.G_EXC_UNEXPECTED_ERROR then
4631      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4632 
4633    when OTHERS then
4634      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4635 
4636 END Cache_Salary_Dist;
4637 
4638 /*==========================================================================+
4639  |                       PROCEDURE Update_Baseline_Values                   |
4640  +==========================================================================*/
4641 
4642 PROCEDURE Update_Baseline_Values
4643 ( p_api_version         IN      NUMBER,
4644   p_init_msg_list       IN      VARCHAR2 := FND_API.G_FALSE,
4645   p_commit              IN      VARCHAR2 := FND_API.G_FALSE,
4646   p_validation_level    IN      NUMBER  := FND_API.G_VALID_LEVEL_FULL,
4647   p_return_status       OUT  NOCOPY     VARCHAR2,
4648   p_msg_count           OUT  NOCOPY     NUMBER,
4649   p_msg_data            OUT  NOCOPY     VARCHAR2,
4650   p_budget_revision_id  IN      NUMBER
4651 ) IS
4652 
4653   l_api_name            CONSTANT VARCHAR2(30)   := 'Update_Baseline_Values';
4654   l_api_version         CONSTANT NUMBER         := 1.0;
4655 
4656   l_return_status       VARCHAR2(1);
4657   l_msg_count           NUMBER;
4658   l_msg_data            VARCHAR2(2000);
4659 
4660   l_assignment_id       NUMBER;
4661   l_position_id         NUMBER;
4662   l_distribution_id     NUMBER;
4663   l_rowid               VARCHAR2(100);
4664 
4665   cursor c_fte is
4666     select position_fte_line_id, position_id, start_date,
4667            end_date, fte
4668       from psb_position_fte
4669      where budget_revision_id = p_budget_revision_id;
4670 
4671   cursor c_costs is
4672     select position_element_line_id, position_id, pay_element_id,
4673            element_cost, start_date, end_date, currency_code
4674       from PSB_POSITION_COSTS
4675      where budget_revision_id = p_budget_revision_id;
4676 
4677   cursor c_distributions is
4678     select position_account_line_id, position_id, code_combination_id,
4679            budget_group_id, amount, start_date, end_date, currency_code
4680       from PSB_POSITION_ACCOUNTS
4681      where budget_revision_id = p_budget_revision_id;
4682 
4683   cursor c_assignments is
4684     select *
4685       from PSB_POSITION_ASSIGNMENTS
4686      where worksheet_id = p_budget_revision_id
4687        and assignment_type = 'ELEMENT';
4688 
4689   cursor c_accdistr is
4690     select *
4691       from PSB_POSITION_PAY_DISTRIBUTIONS
4692      where worksheet_id = p_budget_revision_id;
4693 
4694    cursor c_Positions is
4695      select *
4696        from PSB_POSITIONS
4697       where position_id = l_position_id;
4698 
4699   cursor c_pay_element_rates is
4700     select *
4701       from PSB_PAY_ELEMENT_RATES
4702      where worksheet_id = p_budget_revision_id;
4703 
4704 BEGIN
4705 
4706   -- Standard Start of API savepoint
4707 
4708 /* Bug No 2532617 Start */
4709 -- Removed the savepoint since intermediate commits have been introduced in called procedures
4710 --  SAVEPOINT     Update_Baseline_Values;
4711 /* Bug No 2532617 End */
4712 
4713   -- Standard call to check for call compatibility.
4714 
4715   if not FND_API.Compatible_API_Call (l_api_version,
4716                                       p_api_version,
4717                                       l_api_name,
4718                                       G_PKG_NAME)
4719   THEN
4720     raise FND_API.G_EXC_UNEXPECTED_ERROR;
4721   END IF;
4722 
4723   -- Initialize message list if p_init_msg_list is set to TRUE.
4724 
4725   if FND_API.to_Boolean (p_init_msg_list) THEN
4726     FND_MSG_PUB.initialize;
4727   END IF;
4728 
4729   Cache_Revision_Variables(p_budget_revision_id => p_budget_revision_id,
4730                            p_return_status => l_return_status);
4731 
4732   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4733     raise FND_API.G_EXC_ERROR;
4734   end if;
4735 
4736   for c_fte_rec in c_fte loop
4737 
4738     PSB_POSITION_CONTROL_PVT.Modify_Position_FTE
4739        (p_api_version => 1.0,
4740         p_return_status => l_return_status,
4741         p_msg_count => l_msg_count,
4742         p_msg_data => l_msg_data,
4743         p_position_id => c_fte_rec.position_id,
4744         p_hr_budget_id => g_hr_budget_id,
4745         p_budget_revision_id => null,
4746         p_fte => c_fte_rec.fte,
4747         p_start_date => c_fte_rec.start_date,
4748         p_end_date => c_fte_rec.end_date,
4749         p_base_line_version => 'C');
4750 
4751     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4752       raise FND_API.G_EXC_ERROR;
4753     end if;
4754 
4755   end loop;
4756 
4757   for c_costs_rec in c_costs loop
4758 
4759     PSB_POSITION_CONTROL_PVT.Modify_Position_Costs
4760       (p_api_version => 1.0,
4761        p_return_status => l_return_status,
4762        p_msg_count => l_msg_count,
4763        p_msg_data => l_msg_data,
4764        p_position_id => c_costs_rec.position_id,
4765        p_hr_budget_id => g_hr_budget_id,
4766        p_pay_element_id => c_costs_rec.pay_element_id,
4767        p_budget_revision_id => null,
4768        p_base_line_version => 'C',
4769        p_start_date => c_costs_rec.start_date,
4770        p_end_date => c_costs_rec.end_date,
4771        p_currency_code => c_costs_rec.currency_code,
4772        p_element_cost => c_costs_rec.element_cost);
4773 
4774     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4775       raise FND_API.G_EXC_ERROR;
4776     end if;
4777 
4778   end loop;
4779 
4780   for c_distributions_rec in c_distributions loop
4781 
4782     PSB_POSITION_CONTROL_PVT.Modify_Position_Accounts
4783        (p_api_version => 1.0,
4784         p_return_status => l_return_status,
4785         p_msg_count => l_msg_count,
4786         p_msg_data => l_msg_data,
4787         p_position_id => c_distributions_rec.position_id,
4788         p_hr_budget_id => g_hr_budget_id,
4789         p_budget_revision_id => null,
4790         p_budget_group_id => c_distributions_rec.budget_group_id,
4791         p_base_line_version => 'C',
4792         p_start_date => c_distributions_rec.start_date,
4793         p_end_date => c_distributions_Rec.end_date,
4794         p_code_combination_id => c_distributions_rec.code_combination_id,
4795         p_currency_code => c_distributions_rec.currency_code,
4796         p_amount => c_distributions_rec.amount);
4797 
4798     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4799       raise FND_API.G_EXC_ERROR;
4800     end if;
4801 
4802   end loop;
4803 
4804   for c_assignments_rec in c_assignments loop
4805 
4806     PSB_POSITIONS_PVT.Modify_Assignment
4807        (p_api_version => 1.0,
4808         p_return_status => l_return_status,
4809         p_msg_count => l_msg_count,
4810         p_msg_data => l_msg_data,
4811         p_position_assignment_id => l_assignment_id,
4812         p_data_extract_id => g_data_extract_id,
4813         p_worksheet_id => null,
4814         p_position_id => c_assignments_rec.position_id,
4815         p_assignment_type => c_assignments_rec.assignment_type,
4816         p_attribute_id => c_assignments_rec.attribute_id,
4817         p_attribute_value_id => c_assignments_rec.attribute_value_id,
4818         p_attribute_value => c_assignments_rec.attribute_value,
4819         p_pay_element_id => c_assignments_rec.pay_element_id,
4820         p_pay_element_option_id => c_assignments_rec.pay_element_option_id,
4821         p_effective_start_date => c_assignments_rec.effective_start_date,
4822         p_effective_end_date => c_assignments_rec.effective_end_date,
4823         p_element_value_type => c_assignments_rec.element_value_type,
4824         p_element_value => c_assignments_rec.element_value,
4825         p_currency_code => c_assignments_rec.currency_code,
4826         p_pay_basis => c_assignments_rec.pay_basis,
4827         p_employee_id => c_assignments_rec.employee_id,
4828         p_primary_employee_flag => c_assignments_rec.primary_employee_flag,
4829         p_global_default_flag => c_assignments_rec.global_default_flag,
4830         p_assignment_default_rule_id => c_assignments_rec.assignment_default_rule_id,
4831         p_modify_flag => c_assignments_rec.modify_flag,
4832         p_rowid => l_rowid);
4833 
4834     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4835       raise FND_API.G_EXC_ERROR;
4836     end if;
4837 
4838   end loop;
4839 
4840   for c_accdistr_rec in c_accdistr loop
4841 
4842     l_position_id := c_accdistr_rec.position_id;
4843 
4844     PSB_WS_POS1.Initialize_Salary_Dist;
4845 
4846     for c_Positions_Rec in c_Positions loop
4847 
4848     Cache_Salary_Dist
4849        (p_return_status => l_return_status,
4850         p_budget_revision_id => p_budget_revision_id,
4851         p_position_id => c_accdistr_rec.position_id,
4852         p_position_name => C_Positions_Rec.name,
4853         p_start_date => c_accdistr_rec.effective_start_date,
4854         p_end_date => c_accdistr_rec.effective_end_date);
4855 
4856     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4857         raise FND_API.G_EXC_ERROR;
4858     end if;
4859 
4860     PSB_POSITIONS_PVT.UPDATE_ROW
4861     (
4862             p_api_version            => 1.0,
4863             p_init_msg_list          => FND_API.G_FALSE,
4864             p_commit                 => FND_API.G_FALSE,
4865             p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
4866             p_return_status          => l_return_status,
4867             p_msg_count              => l_msg_count,
4868             p_msg_data               => l_msg_data,
4869             p_position_id            => l_position_id,
4870             p_data_extract_id        => c_positions_rec.data_extract_id,
4871             p_position_definition_id => c_positions_rec.position_definition_id,
4872             p_hr_position_id         => c_positions_rec.hr_position_id,
4873             p_hr_employee_id         => c_positions_rec.hr_employee_id,
4874             p_business_group_id      => c_positions_rec.business_group_id,
4875             p_budget_group_id        => PSB_WS_POS1.g_salary_budget_group_id,
4876             p_effective_start_DATE   => c_positions_rec.effective_start_date,
4877             p_effective_END_DATE     => c_positions_rec.effective_end_date,
4878             p_set_of_books_id        => c_positions_rec.set_of_books_id,
4879             p_vacant_position_flag   => c_positions_rec.vacant_position_flag,
4880         /*For Bug No : 1527423 Start*/
4881             p_availability_status    => c_positions_rec.availability_status,
4882         /*For Bug No : 1527423 End*/
4883             p_attribute1             => c_positions_rec.attribute1,
4884             p_attribute2             => c_positions_rec.attribute2,
4885             p_attribute3             => c_positions_rec.attribute3,
4886             p_attribute4             => c_positions_rec.attribute4,
4887             p_attribute5             => c_positions_rec.attribute5,
4888             p_attribute6             => c_positions_rec.attribute6,
4889             p_attribute7             => c_positions_rec.attribute7,
4890             p_attribute8             => c_positions_rec.attribute8,
4891             p_attribute9             => c_positions_rec.attribute9,
4892             p_attribute10            => c_positions_rec.attribute10,
4893             p_attribute11            => c_positions_rec.attribute11,
4894             p_attribute12            => c_positions_rec.attribute12,
4895             p_attribute13            => c_positions_rec.attribute13,
4896             p_attribute14            => c_positions_rec.attribute14,
4897             p_attribute15            => c_positions_rec.attribute15,
4898             p_attribute16            => c_positions_rec.attribute16,
4899             p_attribute17            => c_positions_rec.attribute17,
4900             p_attribute18            => c_positions_rec.attribute18,
4901             p_attribute19            => c_positions_rec.attribute19,
4902             p_attribute20            => c_positions_rec.attribute20,
4903             p_attribute_category     => c_positions_rec.attribute_category,
4904             p_name                   => c_positions_rec.name,
4905             p_mode                   => 'R'
4906           );
4907 
4908     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4909       raise FND_API.G_EXC_ERROR;
4910     end if;
4911     end loop;
4912 
4913     PSB_POSITION_PAY_DISTR_PVT.Modify_Distribution
4914        (p_api_version => 1.0,
4915         p_return_status => l_return_status,
4916         p_msg_count => l_msg_count,
4917         p_msg_data => l_msg_data,
4918         p_distribution_id => l_distribution_id,
4919         p_position_id => c_accdistr_rec.position_id,
4920         p_data_extract_id => g_data_extract_id,
4921         p_worksheet_id => null,
4922         p_effective_start_date => c_accdistr_rec.effective_start_date,
4923         p_effective_end_date => c_accdistr_rec.effective_end_date,
4924         p_chart_of_accounts_id => c_accdistr_rec.chart_of_accounts_id,
4925         p_code_combination_id => c_accdistr_rec.code_combination_id,
4926         p_distribution_percent => c_accdistr_rec.distribution_percent,
4927         p_global_default_flag => c_accdistr_rec.global_default_flag,
4928         p_distribution_default_rule_id => c_accdistr_rec.distribution_default_rule_id,
4929         p_rowid => l_rowid,
4930         p_project_id => c_accdistr_rec.project_id,
4931         p_task_id => c_accdistr_rec.task_id,
4932         p_award_id => c_accdistr_rec.award_id,
4933         p_expenditure_type => c_accdistr_rec.expenditure_type,
4934         p_expenditure_organization_id => c_accdistr_rec.expenditure_organization_id,
4935         p_description => c_accdistr_rec.description);
4936 
4937     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4938       raise FND_API.G_EXC_ERROR;
4939     end if;
4940 
4941   end loop;
4942 
4943   for c_rates_rec in c_pay_element_rates
4944   loop
4945      PSB_PAY_ELEMENT_RATES_PVT.Modify_Element_Rates
4946              (p_api_version => 1.0,
4947               p_return_status => l_return_status,
4948               p_msg_count => l_msg_count,
4949               p_msg_data => l_msg_data,
4950               p_pay_element_id => c_rates_rec.pay_element_id,
4951               p_pay_element_option_id => c_rates_rec.pay_element_option_id,
4952               p_effective_start_date => c_rates_rec.effective_start_date,
4953               p_effective_end_date => c_rates_rec.effective_end_date,
4954               p_worksheet_id => null,
4955               p_element_value_type => c_rates_rec.element_value_type,
4956               p_element_value => c_rates_rec.element_value,
4957               p_formula_id => c_rates_rec.formula_id,
4958               p_pay_basis => c_rates_rec.pay_basis,
4959               p_maximum_value => c_rates_rec.maximum_value,
4960               p_mid_value => c_rates_rec.mid_value,
4961               p_minimum_value => c_rates_rec.minimum_value,
4962               p_currency_code => c_rates_rec.currency_code);
4963 
4964           if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4965             raise FND_API.G_EXC_ERROR;
4966           end if;
4967 
4968   end loop;
4969 
4970   --++ upload to HRMS budget
4971 
4972   if g_hr_budget_id is not null then
4973      psb_position_control_pvt.Upload_Budget_HRMS
4974          (p_api_version => 1.0,
4975           p_return_status => l_return_status,
4976           p_validation_level => FND_API.G_VALID_LEVEL_FULL,
4977           p_msg_count => l_msg_count,
4978           p_msg_data => l_msg_data,
4979           p_event_type => 'BR',
4980           p_source_id => p_budget_revision_id,
4981           p_hr_budget_id => g_hr_budget_id,
4982           p_from_budget_year_id => null,
4983           p_to_budget_year_id => null);
4984 
4985      if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4986         raise FND_API.G_EXC_ERROR;
4987      end if;
4988 
4989      psb_position_control_pvt.Upload_Budget_HRMS
4990          (p_api_version => 1.0,
4991           p_return_status => l_return_status,
4992           p_validation_level => FND_API.G_VALID_LEVEL_NONE,
4993           p_msg_count => l_msg_count,
4994           p_msg_data => l_msg_data,
4995           p_event_type => 'BR',
4996           p_source_id => p_budget_revision_id,
4997           p_hr_budget_id => g_hr_budget_id,
4998           p_from_budget_year_id => null,
4999           p_to_budget_year_id => null);
5000 
5001      if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5002         raise FND_API.G_EXC_ERROR;
5003      end if;
5004 
5005   end if;
5006   --++ end upload to HRMS budget
5007 
5008 
5009   -- Standard check of p_commit.
5010 
5011   if FND_API.to_Boolean (p_commit) THEN
5012     commit work;
5013   END IF;
5014 
5015   -- Standard call to get message count and if count is 1, get message info.
5016 
5017   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
5018                              p_data  => p_msg_data);
5019 
5020   p_return_status := FND_API.G_RET_STS_SUCCESS;
5021 
5022 EXCEPTION
5023 
5024    WHEN FND_API.G_EXC_ERROR THEN
5025 /* Bug No 2532617 Start */
5026 --     rollback to Update_Baseline_Values;
5027 /* Bug No 2532617 End */
5028      p_return_status := FND_API.G_RET_STS_ERROR;
5029 
5030      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
5031                                 p_data  => p_msg_data);
5032 
5033    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5034 /* Bug No 2532617 Start */
5035 --     rollback to Update_Baseline_Values;
5036 /* Bug No 2532617 End */
5037      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5038 
5039      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
5040                                 p_data  => p_msg_data);
5041 
5042    WHEN OTHERS THEN
5043 /* Bug No 2532617 Start */
5044 --     rollback to Update_Baseline_Values;
5045 /* Bug No 2532617 End */
5046      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5047 
5048      if FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5049        FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
5050                                 l_api_name);
5051      END IF;
5052 
5053      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
5054                                 p_data  => p_msg_data);
5055 
5056 End Update_Baseline_Values;
5057 
5058 /* ----------------------------------------------------------------------- */
5059 
5060 FUNCTION Prorate_Ratio
5061 (p_original_amount      NUMBER,
5062  p_original_start_date  DATE,
5063  p_original_end_date    DATE,
5064  p_new_start_date       DATE,
5065  p_new_end_date         DATE
5066 ) RETURN NUMBER
5067 IS
5068   --
5069   l_prorated_value       NUMBER;
5070   --
5071 BEGIN
5072 
5073   l_prorated_value := p_original_amount
5074       * months_between(p_new_end_date ,p_new_start_date  - 1)
5075       / months_between(p_original_end_date ,p_original_start_date - 1) ;
5076 
5077   RETURN l_prorated_value;
5078 
5079 END Prorate_Ratio;
5080 
5081 /* ----------------------------------------------------------------------- */
5082 
5083 FUNCTION Prorate
5084 (p_element_type          VARCHAR2,
5085  p_element_value         NUMBER,
5086  p_pay_basis             VARCHAR2,
5087  p_period_type           VARCHAR2,
5088  p_effective_start_date  DATE,
5089  p_effective_end_date    DATE
5090 ) RETURN NUMBER IS
5091 
5092   l_prorated_value       NUMBER;
5093 
5094 BEGIN
5095 
5096   if p_element_type = 'S' then
5097   begin
5098 
5099     if p_pay_basis = 'ANNUAL' then
5100       l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value / 12;
5101     elsif p_pay_basis = 'HOURLY' then
5102       l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 4.33333333;
5103     elsif p_pay_basis = 'MONTHLY' then
5104       l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value;
5105     elsif p_pay_basis = 'PERIOD' then
5106       if p_period_type = 'BM' then
5107         l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 0.5;
5108       elsif p_period_type = 'CM' then
5109         l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 1;
5110       elsif p_period_type = 'F' then
5111         l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 2.16666667;
5112       elsif p_period_type = 'LM' then
5113         l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 1;
5114       elsif p_period_type = 'Q' then
5115         l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 0.333333333;
5116       elsif p_period_type = 'SM' then
5117         l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 2;
5118       elsif p_period_type = 'SY' then
5119         l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 0.166666667;
5120       elsif p_period_type = 'W' then
5121         l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 4.33333333;
5122       elsif p_period_type = 'Y' then
5123         l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 0.083333333;
5124       end if;
5125 
5126     end if;
5127 
5128   end;
5129   else
5130   begin
5131 
5132     if p_period_type = 'BM' then
5133       l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 0.5;
5134     elsif p_period_type = 'CM' then
5135       l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 1;
5136     elsif p_period_type = 'F' then
5137       l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 2.16666667;
5138     elsif p_period_type = 'LM' then
5139       l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 1;
5140     elsif p_period_type = 'Q' then
5141       l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 0.333333333;
5142     elsif p_period_type = 'SM' then
5143       l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 2;
5144     elsif p_period_type = 'SY' then
5145       l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 0.166666667;
5146     elsif p_period_type = 'W' then
5147       l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 4.33333333;
5148     elsif p_period_type = 'Y' then
5149       l_prorated_value := months_between(p_effective_end_date, (p_effective_start_date - 1)) * p_element_value * 0.083333333;
5150     end if;
5151 
5152   end;
5153   end if;
5154 
5155   return l_prorated_value;
5156 
5157 END Prorate;
5158 /*---------------------------------------------------------------------------*/
5159 
5160 
5161 /*===========================================================================+
5162  |                       PROCEDURE Find_FTE                                  |
5163  +===========================================================================*/
5164 PROCEDURE Find_FTE
5165 ( p_api_version            IN           NUMBER,
5166   p_init_msg_list          IN           VARCHAR2 := FND_API.G_FALSE,
5167   p_validation_level       IN           NUMBER := FND_API.G_VALID_LEVEL_FULL,
5168   p_return_status          OUT  NOCOPY  VARCHAR2,
5169   p_msg_count              OUT  NOCOPY  NUMBER,
5170   p_msg_data               OUT  NOCOPY  VARCHAR2,
5171   p_position_id            IN           NUMBER,
5172   p_hr_budget_id           IN           NUMBER,
5173   p_budget_revision_id     IN           NUMBER,
5174   p_revision_type          IN           VARCHAR2,
5175   p_revision_value_type    IN           VARCHAR2,
5176   p_revision_value         IN           NUMBER,
5177   p_effective_start_date   IN           DATE,
5178   p_effective_end_date     IN           DATE,
5179   p_original_fte           OUT  NOCOPY  NUMBER,
5180   p_current_fte            OUT  NOCOPY  NUMBER,
5181   p_revised_fte            OUT  NOCOPY  NUMBER
5182 )
5183 IS
5184   --
5185   l_api_version        CONSTANT NUMBER       := 1.0;
5186   l_api_name           CONSTANT VARCHAR2(30) := 'Find_FTE';
5187   --
5188   l_new_position                BOOLEAN := TRUE;
5189   l_original_fte                NUMBER := 0;
5190   l_original_fte_count          NUMBER := 0;
5191   l_current_fte                 NUMBER := 0;
5192   l_current_fte_count           NUMBER := 0;
5193   l_revised_fte                 NUMBER;
5194   --
5195   CURSOR l_base_fte_csr IS
5196   SELECT base_line_version, position_fte_line_id, start_date, end_date, fte
5197   FROM   psb_position_fte
5198   WHERE  position_id = p_position_id
5199   AND    NVL (hr_budget_id, -1) = NVL (p_hr_budget_id, -1)
5200   AND    base_line_version IN ('O', 'C')
5201   AND    budget_revision_id IS NULL
5202   AND    (
5203               start_date BETWEEN p_effective_start_date AND p_effective_end_date
5204            OR end_date BETWEEN p_effective_start_date AND p_effective_end_date
5205            OR (
5206                     start_date < p_effective_start_date
5207                 AND end_date > p_effective_end_date
5208               )
5209          );
5210   --
5211   CURSOR l_rev_fte_csr IS
5212   SELECT position_fte_line_id, start_date, end_date, fte
5213   FROM   psb_position_fte
5214   WHERE  position_id = p_position_id
5215   AND    NVL (hr_budget_id, -1) = NVL (p_hr_budget_id, -1)
5216   AND    budget_revision_id = p_budget_revision_id
5217   AND    (
5218               start_date BETWEEN p_effective_start_date AND p_effective_end_date
5219            OR end_date BETWEEN p_effective_start_date AND p_effective_end_date
5220            OR (
5221                     start_date < p_effective_start_date
5222                 AND end_date > p_effective_end_date
5223               )
5224          );
5225   --
5226 BEGIN
5227   -- Standard call to check for call compatibility.
5228   IF NOT FND_API.Compatible_API_Call (l_api_version,
5229                                       p_api_version,
5230                                       l_api_name,
5231                                       G_PKG_NAME)
5232   THEN
5233     raise FND_API.G_EXC_UNEXPECTED_ERROR;
5234   END IF;
5235 
5236   -- Initialize message list if p_init_msg_list is set to TRUE.
5237   IF FND_API.to_Boolean (p_init_msg_list) THEN
5238     FND_MSG_PUB.initialize;
5239   END IF;
5240 
5241   -- Find base FTE for the position.
5242   FOR l_base_fte_rec IN l_base_fte_csr LOOP
5243 
5244     l_new_position := FALSE;
5245 
5246     IF l_base_fte_rec.base_line_version = 'C' THEN
5247       l_current_fte_count := l_current_fte_count + 1;
5248       l_current_fte := l_current_fte + l_base_fte_rec.fte;
5249     ELSIF l_base_fte_rec.base_line_version = 'O' THEN
5250       l_original_fte_count := l_original_fte_count + 1;
5251       l_original_fte := l_original_fte + l_base_fte_rec.fte;
5252     END IF;
5253 
5254   END LOOP;
5255 
5256   -- Process if new position.
5257   IF l_new_position THEN
5258 
5259     FOR l_rev_fte_rec IN l_rev_fte_csr LOOP
5260 
5261       -- Bug#1802309
5262       /* l_current_fte := l_current_fte + l_rev_fte_rec.fte; */
5263       l_current_fte_count := l_current_fte_count + 1;
5264 
5265       -- Bug#1802309
5266       /* l_original_fte := l_original_fte + l_rev_fte_rec.fte; */
5267       l_original_fte_count := l_original_fte_count + 1;
5268 
5269       -- Bug# 2576216
5270       IF p_revision_type IS NULL THEN
5271         l_revised_fte := l_rev_fte_rec.fte;
5272       END IF;
5273 
5274     END LOOP;
5275 
5276   END IF;
5277   -- End processing if new position.
5278 
5279   /* Bug No 2576216 Start */
5280   -- Added the IF conditions
5281   if l_original_fte_count <> 0 then
5282     l_original_fte := l_original_fte / l_original_fte_count;
5283   end if;
5284 
5285   if l_current_fte_count <> 0 then
5286     l_current_fte := l_current_fte / l_current_fte_count;
5287   end if;
5288   /* Bug No 2576216 End */
5289 
5290 
5291   if p_revision_type = 'I' then
5292     --
5293     if p_revision_value_type = 'A' then
5294       l_revised_fte := l_current_fte + p_revision_value;
5295     elsif p_revision_value_type = 'P' then
5296       l_revised_fte := l_current_fte * (1 + p_revision_value / 100);
5297     end if;
5298     --
5299   elsif p_revision_type = 'D' then
5300     --
5301     if p_revision_value_type = 'A' then
5302       l_revised_fte := l_current_fte - p_revision_value;
5303     elsif p_revision_value_type = 'P' then
5304       l_revised_fte := l_current_fte * (1 - p_revision_value / 100);
5305     end if;
5306     --
5307   /* Bug No 2576216 Start */
5308   else
5309     --
5310     if not l_new_position then
5311 
5312       -- Bug#3340060: If revision type is not given, it should mean we
5313       -- display current FTE as it is without applying revision amount.
5314       /* l_revised_fte := l_current_fte + l_current_fte; */
5315       l_revised_fte := l_current_fte ;
5316       --
5317     end if;
5318   /* Bug No 2576216 End */
5319   end if;
5320 
5321   p_original_fte := l_original_fte;
5322   p_current_fte  := l_current_fte;
5323   p_revised_fte  := nvl(l_revised_fte, l_current_fte);
5324 
5325   -- Initialize API return status to success
5326   p_return_status := FND_API.G_RET_STS_SUCCESS;
5327 
5328   -- Standard call to get message count and if count is 1, get message info.
5329   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
5330                              p_data  => p_msg_data);
5331   --
5332 EXCEPTION
5333   when OTHERS then
5334     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5335 
5336     if FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
5337       FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
5338                                l_api_name);
5339     end if;
5340 
5341     FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
5342                                p_data  => p_msg_data);
5343 End Find_FTE;
5344 /*---------------------------------------------------------------------------*/
5345 
5346 
5347 /*---------------------------------------------------------------------------*/
5348 PROCEDURE Reverse_Position_Accounts
5349 ( p_return_status         OUT  NOCOPY     VARCHAR2,
5350   p_budget_revision_id    IN      NUMBER,
5351   p_position_id           IN      NUMBER,
5352   p_effective_start_date  IN      DATE,
5353   p_effective_end_date    IN      DATE) IS
5354 
5355   l_gl_period             VARCHAR2(15);
5356   l_return_status         VARCHAR2(1);
5357   l_msg_count             NUMBER;
5358   l_msg_data              VARCHAR2(2000);
5359 
5360   cursor c_gl_period is
5361     select period_name
5362       from gl_period_statuses
5363      where application_id = 101
5364        and set_of_books_id = g_set_of_books_id
5365        and p_effective_start_date between start_date and end_date;
5366 
5367   cursor c_position_account is
5368     select pbra.budget_revision_acct_line_id,
5369            pbra.code_combination_id,
5370            pbra.budget_group_id,
5371            pbra.gl_period_name,
5372            pbra.gl_budget_version_id,
5373            pbra.currency_code,
5374            pbra.budget_balance,
5375            pbra.revision_type,
5376            pbra.revision_value_type,
5377            pbra.revision_amount,
5378            pbrl.freeze_flag, pbrl.view_line_flag
5379       from psb_budget_revision_accounts pbra, psb_budget_revision_lines pbrl
5380      where pbrl.budget_revision_id = p_budget_revision_id
5381        and pbra.budget_revision_acct_line_id = pbrl.budget_revision_acct_line_id
5382        and position_id = p_position_id
5383        and gl_period_name = l_gl_period;
5384 
5385   cursor c_zero_accounts is
5386     select pbra.budget_revision_acct_line_id
5387       from psb_budget_revision_accounts pbra, psb_budget_revision_lines pbrl
5388      where pbrl.budget_revision_id = p_budget_revision_id
5389        and pbra.budget_revision_acct_line_id = pbrl.budget_revision_acct_line_id
5390        and pbra.revision_amount = 0;
5391 
5392 BEGIN
5393 
5394   For c_gl_period_rec in c_gl_period Loop
5395     l_gl_period := c_gl_period_rec.period_name;
5396   End Loop;
5397 
5398   For c_position_account_rec in c_position_account Loop
5399 
5400     Create_Revision_Accounts
5401           (p_api_version => 1.0,
5402            p_return_status => l_return_status,
5403            p_msg_count => l_msg_count,
5404            p_msg_data => l_msg_data,
5405            p_budget_revision_id => p_budget_revision_id,
5406            p_budget_revision_acct_line_id  => c_position_account_rec.budget_revision_acct_line_id,
5407            p_code_combination_id => c_position_account_rec.code_combination_id,
5408            p_budget_group_id => c_position_account_rec.budget_group_id,
5409            p_position_id => p_position_id,
5410            p_gl_period_name => c_position_account_rec.gl_period_name,
5411            p_gl_budget_version_id => c_position_account_rec.gl_budget_version_id,
5412            p_currency_code => c_position_account_rec.currency_code,
5413            p_budget_balance => c_position_account_rec.budget_balance,
5414            p_revision_type => c_position_account_rec.revision_type,
5415            p_revision_value_type => c_position_account_rec.revision_value_type,
5416            p_revision_amount => 0,
5417            p_funds_status_code => FND_API.G_MISS_CHAR,
5418            p_funds_result_code => FND_API.G_MISS_CHAR,
5419            p_funds_control_timestamp => sysdate,
5420            p_note_id => FND_API.G_MISS_NUM,
5421            p_freeze_flag => c_position_account_rec.freeze_flag,
5422            p_view_line_flag => c_position_account_rec.view_line_flag);
5423 
5424     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5425       raise FND_API.G_EXC_ERROR;
5426     end if;
5427 
5428   End Loop;
5429 
5430   Create_Summary_Position_Line
5431         (p_return_status => l_return_status,
5432          p_budget_revision_id => p_budget_revision_id,
5433          p_currency_code => g_func_currency,
5434          p_gl_period_name => l_gl_period);
5435 
5436   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5437      raise FND_API.G_EXC_ERROR;
5438   end if;
5439   /* start bug 3418071 */
5440   /*if g_create_zero_bal <> 'Y' then */
5441   begin
5442 
5443     for c_zero_accounts_rec in c_zero_accounts loop
5444 
5445       Delete_Revision_Accounts
5446             (p_api_version => 1.0,
5447              p_return_status => l_return_status,
5448              p_msg_count => l_msg_count,
5449              p_msg_data => l_msg_data,
5450              p_budget_revision_id  => p_budget_revision_id,
5451              p_budget_revision_acct_line_id => c_zero_accounts_rec.budget_revision_acct_line_id);
5452 
5453       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5454         raise FND_API.G_EXC_ERROR;
5455       end if;
5456 
5457     end loop;
5458 
5459   end;
5460   /*end if;*/
5461   /* end bug 3418071 */
5462 
5463   p_return_status := FND_API.G_RET_STS_SUCCESS;
5464 
5465 EXCEPTION
5466 
5467    when FND_API.G_EXC_ERROR then
5468      p_return_status := FND_API.G_RET_STS_ERROR;
5469 
5470    when FND_API.G_EXC_UNEXPECTED_ERROR then
5471      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5472 
5473    when OTHERS then
5474      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5475 
5476 End Reverse_Position_Accounts;
5477 
5478 /* ----------------------------------------------------------------------- */
5479 
5480 PROCEDURE Calculate_Position_Cost_Dates
5481 ( p_return_status        OUT  NOCOPY  VARCHAR2,
5482   p_position_id          IN   NUMBER,
5483   p_position_name        IN   VARCHAR2,
5484   p_revision_start_date  IN   DATE,
5485   p_revision_end_date    IN   DATE
5486 ) IS
5487 
5488   l_pay_element_id            NUMBER;
5489   l_pay_element_option_id     NUMBER;
5490   l_period_type               VARCHAR2(10);
5491   l_element_name              VARCHAR2(30);
5492   l_element_cost              NUMBER;
5493   l_salary_value              NUMBER;
5494   l_element_value             NUMBER;
5495   l_element_value_type        VARCHAR2(2);
5496   l_element_assigned          BOOLEAN;
5497   l_start_date                DATE;
5498   l_end_date                  DATE;
5499   l_pay_basis                 VARCHAR2(10);
5500   l_fte                       NUMBER;
5501   l_default_weekly_hours      NUMBER;
5502   l_rate_found                BOOLEAN;
5503 
5504 BEGIN
5505 
5506   for l_element_index in 1..PSB_WS_POS1.g_num_elements loop
5507     l_pay_element_id := PSB_WS_POS1.g_elements(l_element_index).pay_element_id;
5508     l_period_type := PSB_WS_POS1.g_elements(l_element_index).period_type;
5509     l_element_name := PSB_WS_POS1.g_elements(l_element_index).element_name;
5510     l_element_cost := 0;
5511     l_salary_value := 0;
5512     l_element_assigned := FALSE;
5513 
5514     for l_assign_index in 1..g_num_elem_assignments loop
5515 
5516       if ((g_elem_assignments(l_assign_index).pay_element_id = l_pay_element_id) and
5517         (((g_elem_assignments(l_assign_index).start_date <= p_revision_end_date) and
5518           (g_elem_assignments(l_assign_index).end_date is null)) or
5519          ((g_elem_assignments(l_assign_index).start_date between p_revision_start_date and p_revision_end_date) or
5520           (g_elem_assignments(l_assign_index).end_date between p_revision_start_date and p_revision_end_date) or
5521          ((g_elem_assignments(l_assign_index).start_date < p_revision_start_date) and
5522           (g_elem_assignments(l_assign_index).end_date > p_revision_end_date)))) and
5523           (g_elem_assignments(l_assign_index).use_in_calc)) then
5524       begin
5525 
5526         l_element_assigned := TRUE;
5527         l_element_value_type := g_elem_assignments(l_assign_index).element_value_type;
5528         l_element_value := g_elem_assignments(l_assign_index).element_value;
5529         l_pay_element_option_id := g_elem_assignments(l_assign_index).pay_element_option_id;
5530         l_start_date := greatest(p_revision_start_date, g_elem_assignments(l_assign_index).start_date);
5531         l_end_date := least(p_revision_end_date, nvl(g_elem_assignments(l_assign_index).end_date, p_revision_end_date));
5532         l_pay_basis := g_elem_assignments(l_assign_index).pay_basis;
5533 
5534 /* Bug No 1832091 Start */
5535 -- Commented
5536 --        if l_element_value is null then
5537 /* Bug No 1832091 End */
5538         begin
5539 
5540           for l_rate_index in 1..g_num_elem_rates loop
5541 
5542             if ((g_elem_rates(l_rate_index).pay_element_id = l_pay_element_id) and
5543                 (nvl(g_elem_rates(l_rate_index).pay_element_option_id, FND_API.G_MISS_NUM) = nvl(l_pay_element_option_id, FND_API.G_MISS_NUM)) and
5544               (((g_elem_rates(l_rate_index).start_date <= l_start_date) and
5545                 (g_elem_rates(l_rate_index).end_date is null)) or
5546                ((g_elem_rates(l_rate_index).start_date between l_start_date and l_end_date) or
5547                 (g_elem_rates(l_rate_index).end_date between l_start_date and l_end_date) or
5548                ((g_elem_rates(l_rate_index).start_date < l_start_date) and
5549                 (g_elem_rates(l_rate_index).end_date > l_end_date))))) then
5550             begin
5551 
5552               if g_elem_rates(l_rate_index).budget_revision_id is not null then
5553               begin
5554 
5555                 if l_pay_basis is null then
5556                   l_pay_basis := g_elem_rates(l_rate_index).pay_basis;
5557                 end if;
5558 
5559                 l_element_value_type := g_elem_rates(l_rate_index).element_value_type;
5560                 l_element_value := g_elem_rates(l_rate_index).element_value;
5561                 exit;
5562 
5563               end;
5564               else
5565               begin
5566 
5567                 if l_pay_basis is null then
5568                   l_pay_basis := g_elem_rates(l_rate_index).pay_basis;
5569                 end if;
5570 
5571 /* Bug No 1832091 Start */
5572 -- Following 2 lines of code is uncommented and put inside the IF statement, which is added.
5573                if l_element_value is null then
5574                   l_element_value_type := g_elem_rates(l_rate_index).element_value_type;
5575                   l_element_value := g_elem_rates(l_rate_index).element_value;
5576                end if;
5577 /* Bug No 1832091 End */
5578                 exit;
5579 
5580               end;
5581               end if;
5582 
5583             end;
5584             end if;
5585 
5586           end loop;
5587 
5588         end;
5589 
5590 /* Bug No 1832091 Start */
5591 -- Commented
5592 --        end if;
5593 /* Bug No 1832091 End */
5594 
5595         for l_assign_index in 1..g_num_fte_assignments loop
5596 
5597           if (((g_fte_assignments(l_assign_index).start_date <= l_end_date) and
5598               (g_fte_assignments(l_assign_index).end_date is null)) or
5599              ((g_fte_assignments(l_assign_index).start_date between l_start_date and l_end_date) or
5600               (g_fte_assignments(l_assign_index).end_date between l_start_date and l_end_date) or
5601              ((g_fte_assignments(l_assign_index).start_date < l_start_date) and
5602               (g_fte_assignments(l_assign_index).end_date > l_end_date)))) then
5603           begin
5604 
5605             l_fte := g_fte_assignments(l_assign_index).fte;
5606             exit;
5607 
5608           end;
5609           end if;
5610 
5611         end loop;
5612 
5613         for l_assign_index in 1..g_num_wkh_assignments loop
5614 
5615           if (((g_wkh_assignments(l_assign_index).start_date <= l_end_date) and
5616               (g_wkh_assignments(l_assign_index).end_date is null)) or
5617              ((g_wkh_assignments(l_assign_index).start_date between l_start_date and l_end_date) or
5618               (g_wkh_assignments(l_assign_index).end_date between l_start_date and l_end_date) or
5619              ((g_wkh_assignments(l_assign_index).start_date < l_start_date) and
5620               (g_wkh_assignments(l_assign_index).end_date > l_end_date)))) then
5621           begin
5622 
5623             l_default_weekly_hours := g_wkh_assignments(l_assign_index).default_weekly_hours;
5624             exit;
5625 
5626           end;
5627           end if;
5628 
5629         end loop;
5630 
5631         if l_element_value_type = 'PI' then
5632           message_token('ELEMENT_VALUE_TYPE', l_element_value_type);
5633           message_token('ELEMENT', PSB_WS_POS1.g_elements(l_element_index).element_name);
5634           message_token('POSITION', p_position_name);
5635           add_message('PSB', 'PSB_INVALID_ASSIGNMENT_TYPE');
5636           raise FND_API.G_EXC_ERROR;
5637         end if;
5638 
5639         if PSB_WS_POS1.g_elements(l_element_index).salary_flag = 'Y' then
5640         begin
5641           if l_pay_basis = 'ANNUAL' then
5642           begin
5643             l_element_cost := l_element_cost + l_fte
5644                             * Prorate(p_element_type => 'S', p_element_value => l_element_value,
5645                                       p_pay_basis => l_pay_basis, p_period_type => null,
5646                                       p_effective_start_date => l_start_date, p_effective_end_date => l_end_date);
5647           end;
5648           elsif l_pay_basis = 'HOURLY' then
5649           begin
5650             if l_default_weekly_hours is null then
5651               message_token('ATTRIBUTE', 'DEFAULT_WEEKLY_HOURS');
5652               message_token('POSITION', p_position_name);
5653               message_token('START_DATE', l_start_date);
5654               message_token('END_DATE', l_end_date);
5655               add_message('PSB', 'PSB_INVALID_NAMED_ATTRIBUTE');
5656               raise FND_API.G_EXC_ERROR;
5657             end if;
5658 
5659             l_element_cost := l_element_cost + l_fte * l_default_weekly_hours
5660                             * Prorate(p_element_type => 'S', p_element_value => l_element_value,
5661                                       p_pay_basis => l_pay_basis, p_period_type => null,
5662                                       p_effective_start_date => l_start_date, p_effective_end_date => l_end_date);
5663           end;
5664           elsif l_pay_basis = 'MONTHLY' then
5665           begin
5666             l_element_cost := l_element_cost + l_fte
5667                             * Prorate(p_element_type => 'S', p_element_value => l_element_value,
5668                                       p_pay_basis => l_pay_basis, p_period_type => null,
5669                                       p_effective_start_date => l_start_date, p_effective_end_date => l_end_date);
5670           end;
5671           elsif l_pay_basis = 'PERIOD' then
5672           begin
5673             l_element_cost := l_element_cost + l_fte
5674                             * Prorate(p_element_type => 'S', p_element_value => l_element_value,
5675                                       p_pay_basis => l_pay_basis, p_period_type => l_period_type,
5676                                       p_effective_start_date => l_start_date, p_effective_end_date => l_end_date);
5677           end;
5678           else
5679             message_token('POSITION', p_position_name);
5680             message_token('START_DATE', l_start_date);
5681             message_token('END_DATE', l_end_date);
5682             add_message('PSB', 'PSB_INVALID_SALARY_BASIS');
5683             raise FND_API.G_EXC_ERROR;
5684           end if;
5685 
5686         end;
5687         else  -- non salary element
5688         begin
5689 
5690           if l_element_value_type = 'PS' then
5691           begin
5692 
5693             if l_element_value >= 1 then
5694               l_element_value := l_element_value / 100;
5695             end if;
5696 
5697             for l_salary_index in 1..g_num_costs loop
5698 
5699               if g_costs(l_salary_index).element_type = 'S' then
5700                 l_salary_value := l_salary_value + Prorate_Ratio (p_original_amount => g_costs(l_salary_index).element_cost,
5701                                                                   p_original_start_date => g_costs(l_salary_index).start_date,
5702                                                                   p_original_end_date => g_costs(l_salary_index).end_date,
5703                                                                   p_new_start_date => l_start_date,
5704                                                                   p_new_end_date => l_end_date);
5705               end if;
5706 
5707             end loop;
5708 
5709             l_element_cost := l_salary_value * l_element_value ;
5710 
5711           end;
5712           elsif l_element_value_type = 'A' then
5713             l_element_cost := l_element_cost + l_fte
5714                             * Prorate(p_element_type => 'F', p_element_value => l_element_value,
5715                                       p_pay_basis => l_pay_basis, p_period_type => l_period_type,
5716                                       p_effective_start_date => l_start_date, p_effective_end_date => l_end_date);
5717           end if;
5718 
5719         end;
5720         end if;
5721 
5722       end;
5723       end if;
5724 
5725     end loop; -- element assignments
5726 
5727     -- Bug#3340060: Element cost will be null if an element does not have rates
5728     -- defined. However element cost is required in psb_position_costs table.
5729     -- The following will address this rare scenario.
5730     /* if l_element_assigned then */
5731     IF l_element_assigned AND l_element_cost IS NOT NULL THEN
5732 
5733       g_num_costs := g_num_costs + 1;
5734       g_costs(g_num_costs).pay_element_id := l_pay_element_id;
5735 
5736       if PSB_WS_POS1.g_elements(l_element_index).salary_flag = 'Y' then
5737         g_costs(g_num_costs).element_type := 'S';
5738       elsif PSB_WS_POS1.g_elements(l_element_index).follow_salary = 'Y' then
5739         g_costs(g_num_costs).element_type := 'F';
5740       end if;
5741 
5742       g_costs(g_num_costs).element_cost := l_element_cost;
5743       g_costs(g_num_costs).start_date := l_start_date;
5744       g_costs(g_num_costs).end_date := l_end_date;
5745       g_costs(g_num_costs).currency_code := g_func_currency;
5746 
5747     END IF;
5748 
5749   end loop;
5750 
5751   -- Initialize API return status to success
5752   p_return_status := FND_API.G_RET_STS_SUCCESS;
5753 
5754 EXCEPTION
5755    when FND_API.G_EXC_ERROR then
5756      p_return_status := FND_API.G_RET_STS_ERROR;
5757 
5758    when FND_API.G_EXC_UNEXPECTED_ERROR then
5759      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5760 
5761    when OTHERS then
5762      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5763 
5764 END Calculate_Position_Cost_Dates;
5765 /*---------------------------------------------------------------------------*/
5766 
5767 
5768 /*---------------------------------------------------------------------------*/
5769 PROCEDURE Distribute_Salary
5770 ( p_return_status         OUT  NOCOPY  VARCHAR2,
5771   p_pay_element_id        IN   NUMBER,
5772   p_position_id           IN   NUMBER,
5773   p_revision_start_date   IN   DATE,
5774   p_revision_end_date     IN   DATE
5775 ) IS
5776 
5777   l_amount                NUMBER;
5778   l_percent               NUMBER;
5779 
5780   l_start_date            DATE;
5781   l_end_date              DATE;
5782 
5783   l_dist_found            BOOLEAN;
5784   l_account_index         BINARY_INTEGER;
5785 
5786 BEGIN
5787 
5788   for l_calc_index in 1..g_num_costs loop
5789 
5790     if g_costs(l_calc_index).pay_element_id = p_pay_element_id then
5791     begin
5792 
5793       for l_saldist_index in 1..PSB_WS_POS1.g_num_salary_dist loop
5794 
5795         if (((PSB_WS_POS1.g_salary_dist(l_saldist_index).start_date <= g_costs(l_calc_index).end_date) and
5796              (PSB_WS_POS1.g_salary_dist(l_saldist_index).end_date is null)) or
5797             ((PSB_WS_POS1.g_salary_dist(l_saldist_index).start_date between g_costs(l_calc_index).start_date and g_costs(l_calc_index).end_date) or
5798              (PSB_WS_POS1.g_salary_dist(l_saldist_index).end_date between g_costs(l_calc_index).start_date and g_costs(l_calc_index).end_date) or
5799             ((PSB_WS_POS1.g_salary_dist(l_saldist_index).start_date < g_costs(l_calc_index).start_date) and
5800              (PSB_WS_POS1.g_salary_dist(l_saldist_index).end_date > g_costs(l_calc_index).end_date)))) then
5801         begin
5802 
5803           l_dist_found := FALSE;
5804           l_account_index := null;
5805           l_start_date := greatest(g_costs(l_calc_index).start_date, PSB_WS_POS1.g_salary_dist(l_saldist_index).start_date);
5806           l_end_date := least(g_costs(l_calc_index).end_date, nvl(PSB_WS_POS1.g_salary_dist(l_saldist_index).end_date, g_costs(l_calc_index).end_date));
5807 
5808           for l_dist_index in 1..g_num_dists loop
5809 
5810             if g_dists(l_dist_index).ccid = PSB_WS_POS1.g_salary_dist(l_saldist_index).ccid then
5811               l_dist_found := TRUE;
5812               l_account_index := l_dist_index;
5813             end if;
5814 
5815           end loop;
5816 
5817           -- commented for bug # 4502946
5818           /*if PSB_WS_POS1.g_salary_dist(l_saldist_index).percent < 1 then
5819             l_percent := PSB_WS_POS1.g_salary_dist(l_saldist_index).percent;
5820           else
5821             l_percent := PSB_WS_POS1.g_salary_dist(l_saldist_index).percent / 100;
5822           end if;*/
5823 
5824           -- added for bug # 4502946
5825           l_percent := PSB_WS_POS1.g_salary_dist(l_saldist_index).percent / 100;
5826 
5827           if not l_dist_found then
5828           begin
5829 
5830             g_num_dists := g_num_dists + 1;
5831 
5832             g_dists(g_num_dists).ccid := PSB_WS_POS1.g_salary_dist(l_saldist_index).ccid;
5833             g_dists(g_num_dists).budget_group_id := PSB_WS_POS1.g_salary_budget_group_id;
5834             g_dists(g_num_dists).currency_code := g_func_currency;
5835             g_dists(g_num_dists).start_date := l_start_date;
5836             g_dists(g_num_dists).end_date := l_end_date;
5837 
5838 
5839             g_dists(g_num_dists).amount := l_percent *
5840                                            Prorate_Ratio (p_original_amount => g_costs(l_calc_index).element_cost,
5841                                                           p_original_start_date => g_costs(l_calc_index).start_date,
5842                                                           p_original_end_date => g_costs(l_calc_index).end_date,
5843                                                           p_new_start_date => l_start_date,
5844                                                           p_new_end_date => l_end_date);
5845 
5846 
5847             g_dists(g_num_dists).calc_rev := FALSE;
5848 
5849           end;
5850           else
5851             g_dists(l_account_index).amount := nvl(g_dists(l_account_index).amount, 0) + l_percent *
5852                                                Prorate_Ratio (p_original_amount => g_costs(l_calc_index).element_cost,
5853                                                               p_original_start_date => g_costs(l_calc_index).start_date,
5854                                                               p_original_end_date => g_costs(l_calc_index).end_date,
5855                                                               p_new_start_date => l_start_date,
5856                                                               p_new_end_date => l_end_date);
5857 
5858           end if;
5859 
5860         end;
5861         end if;
5862 
5863       end loop;
5864 
5865     end;
5866     end if;
5867 
5868   end loop;
5869 
5870   -- Initialize API return status to success
5871 
5872   p_return_status := FND_API.G_RET_STS_SUCCESS;
5873 
5874 
5875 EXCEPTION
5876 
5877    when FND_API.G_EXC_ERROR then
5878      p_return_status := FND_API.G_RET_STS_ERROR;
5879 
5880    when FND_API.G_EXC_UNEXPECTED_ERROR then
5881      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5882 
5883    when OTHERS then
5884      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5885 
5886 END Distribute_Salary;
5887 
5888 /* ----------------------------------------------------------------------- */
5889 
5890 PROCEDURE Distribute_Following_Elements
5891 ( p_return_status         OUT  NOCOPY  VARCHAR2,
5892   p_pay_element_id        IN   NUMBER,
5893   p_position_id           IN   NUMBER,
5894   p_revision_start_date   IN   DATE,
5895   p_revision_end_date     IN   DATE
5896 ) IS
5897 
5898   l_ccid_val             FND_FLEX_EXT.SegmentArray;
5899   l_seg_val              FND_FLEX_EXT.SegmentArray;
5900   l_ccid                 NUMBER;
5901 
5902   l_start_date           DATE;
5903   l_end_date             DATE;
5904 
5905   l_dist_start_date      DATE;
5906   l_dist_end_date        DATE;
5907 
5908   l_amount               NUMBER;
5909   l_percent              NUMBER;
5910 
5911   l_dist_found           BOOLEAN;
5912   l_account_index        BINARY_INTEGER;
5913 
5914   cursor c_Dist is
5915     select a.segment1, a.segment2, a.segment3, a.segment4,
5916            a.segment5, a.segment6, a.segment7, a.segment8,
5917            a.segment9, a.segment10, a.segment11, a.segment12,
5918            a.segment13, a.segment14, a.segment15, a.segment16,
5919            a.segment17, a.segment18, a.segment19, a.segment20,
5920            a.segment21, a.segment22, a.segment23, a.segment24,
5921            a.segment25, a.segment26, a.segment27, a.segment28,
5922            a.segment29, a.segment30, a.effective_start_date, a.effective_end_date
5923       from PSB_PAY_ELEMENT_DISTRIBUTIONS a,
5924            PSB_ELEMENT_POS_SET_GROUPS b,
5925            PSB_SET_RELATIONS c,
5926            PSB_BUDGET_POSITIONS d
5927      where a.chart_of_accounts_id = g_flex_code
5928        and (((a.effective_start_date <= p_revision_end_date)
5929          and (a.effective_end_date is null))
5930          or ((a.effective_start_date between p_revision_start_date and p_revision_end_date)
5931           or (a.effective_end_date between p_revision_start_date and p_revision_end_date)
5932          or ((a.effective_start_date < p_revision_start_date)
5933          and (a.effective_end_date > p_revision_end_date))))
5934        and a.position_set_group_id = b.position_set_group_id
5935        and b.position_set_group_id = c.position_set_group_id
5936        and b.pay_element_id = p_pay_element_id
5937        and c.account_position_set_id = d.account_position_set_id
5938        and d.data_extract_id = g_data_extract_id
5939        and d.position_id = p_position_id;
5940 
5941 BEGIN
5942 
5943   for l_calc_index in 1..g_num_costs loop
5944 
5945 
5946     if g_costs(l_calc_index).pay_element_id = p_pay_element_id then
5947     begin
5948 
5949       for c_Dist_Rec in c_Dist loop
5950 
5951         if (((c_Dist_Rec.effective_start_date <= g_costs(l_calc_index).end_date) and
5952              (c_Dist_Rec.effective_end_date is null)) or
5953             ((c_Dist_Rec.effective_start_date between g_costs(l_calc_index).start_date and g_costs(l_calc_index).end_date) or
5954              (c_Dist_Rec.effective_end_date between g_costs(l_calc_index).start_date and g_costs(l_calc_index).end_date) or
5955             ((c_Dist_Rec.effective_start_date < g_costs(l_calc_index).start_date) and
5956              (c_Dist_Rec.effective_end_date > g_costs(l_calc_index).end_date)))) then
5957         begin
5958 
5959           l_start_date := greatest(g_costs(l_calc_index).start_date, c_Dist_Rec.effective_start_date);
5960           l_end_date := least(g_costs(l_calc_index).end_date, nvl(c_Dist_Rec.effective_end_date, g_costs(l_calc_index).end_date));
5961 
5962           for l_saldist_index in 1..PSB_WS_POS1.g_num_salary_dist loop
5963 
5964             if (((PSB_WS_POS1.g_salary_dist(l_saldist_index).start_date <= l_end_date) and
5965                  (PSB_WS_POS1.g_salary_dist(l_saldist_index).end_date is null)) or
5966                 ((PSB_WS_POS1.g_salary_dist(l_saldist_index).start_date between l_start_date and l_end_date) or
5967                  (PSB_WS_POS1.g_salary_dist(l_saldist_index).end_date between l_start_date and l_end_date) or
5968                 ((PSB_WS_POS1.g_salary_dist(l_saldist_index).start_date < l_start_date) and
5969                  (PSB_WS_POS1.g_salary_dist(l_saldist_index).end_date > l_end_date)))) then
5970             begin
5971 
5972           l_dist_start_date := greatest(l_start_date, PSB_WS_POS1.g_salary_dist(l_saldist_index).start_date);
5973           l_dist_end_date := least(l_end_date, nvl(PSB_WS_POS1.g_salary_dist(l_saldist_index).end_date, l_end_date));
5974 
5975               l_dist_found := FALSE;
5976               l_account_index := null;
5977 
5978               -- commented for bug # 4502946
5979               /*if PSB_WS_POS1.g_salary_dist(l_saldist_index).percent < 1 then
5980                 l_percent := PSB_WS_POS1.g_salary_dist(l_saldist_index).percent;
5981               else
5982                 l_percent := PSB_WS_POS1.g_salary_dist(l_saldist_index).percent / 100;
5983               end if;*/
5984 
5985               -- added for bug # 4502946
5986               l_percent := PSB_WS_POS1.g_salary_dist(l_saldist_index).percent / 100;
5987 
5988               for l_init_index in 1..PSB_WS_ACCT1.g_num_segs loop
5989                 l_ccid_val(l_init_index) := null;
5990                 l_seg_val(l_init_index) := null;
5991               end loop;
5992 
5993               if not FND_FLEX_EXT.Get_Segments
5994                 (application_short_name => 'SQLGL',
5995                  key_flex_code => 'GL#',
5996                  structure_number => g_flex_code,
5997                  combination_id => PSB_WS_POS1.g_salary_dist(l_saldist_index).ccid,
5998                  n_segments => PSB_WS_ACCT1.g_num_segs,
5999                  segments => l_ccid_val) then
6000 
6001                 FND_MSG_PUB.Add;
6002                 raise FND_API.G_EXC_ERROR;
6003               end if;
6004 
6005               for l_index in 1..PSB_WS_ACCT1.g_num_segs loop
6006 
6007                 if ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT1') and
6008                     (c_Dist_Rec.segment1 is not null)) then
6009                   l_seg_val(l_index) := c_Dist_Rec.segment1;
6010 
6011                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT2') and
6012                     (c_Dist_Rec.segment2 is not null)) then
6013                   l_seg_val(l_index) := c_Dist_Rec.segment2;
6014 
6015                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT3') and
6016                     (c_Dist_Rec.segment3 is not null)) then
6017                   l_seg_val(l_index) := c_Dist_Rec.segment3;
6018 
6019                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT4') and
6020                     (c_Dist_Rec.segment4 is not null)) then
6021                   l_seg_val(l_index) := c_Dist_Rec.segment4;
6022 
6023                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT5') and
6024                     (c_Dist_Rec.segment5 is not null)) then
6025                   l_seg_val(l_index) := c_Dist_Rec.segment5;
6026 
6027                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT6') and
6028                     (c_Dist_Rec.segment6 is not null)) then
6029                   l_seg_val(l_index) := c_Dist_Rec.segment6;
6030 
6031                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT7') and
6032                     (c_Dist_Rec.segment7 is not null)) then
6033                   l_seg_val(l_index) := c_Dist_Rec.segment7;
6034 
6035                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT8') and
6036                     (c_Dist_Rec.segment8 is not null)) then
6037                   l_seg_val(l_index) := c_Dist_Rec.segment8;
6038 
6039                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT9') and
6040                     (c_Dist_Rec.segment9 is not null)) then
6041                   l_seg_val(l_index) := c_Dist_Rec.segment9;
6042 
6043                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT10') and
6044                     (c_Dist_Rec.segment10 is not null)) then
6045                   l_seg_val(l_index) := c_Dist_Rec.segment10;
6046 
6047                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT11') and
6048                     (c_Dist_Rec.segment11 is not null)) then
6049                   l_seg_val(l_index) := c_Dist_Rec.segment11;
6050 
6051                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT12') and
6052                     (c_Dist_Rec.segment12 is not null)) then
6053                   l_seg_val(l_index) := c_Dist_Rec.segment12;
6054 
6055                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT13') and
6056                     (c_Dist_Rec.segment13 is not null)) then
6057                   l_seg_val(l_index) := c_Dist_Rec.segment13;
6058 
6059                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT14') and
6060                     (c_Dist_Rec.segment14 is not null)) then
6061                   l_seg_val(l_index) := c_Dist_Rec.segment14;
6062 
6063                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT15') and
6064                     (c_Dist_Rec.segment15 is not null)) then
6065                   l_seg_val(l_index) := c_Dist_Rec.segment15;
6066 
6067                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT16') and
6068                     (c_Dist_Rec.segment16 is not null)) then
6069                   l_seg_val(l_index) := c_Dist_Rec.segment16;
6070 
6071                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT17') and
6072                     (c_Dist_Rec.segment17 is not null)) then
6073                   l_seg_val(l_index) := c_Dist_Rec.segment17;
6074 
6075                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT18') and
6076                     (c_Dist_Rec.segment18 is not null)) then
6077                   l_seg_val(l_index) := c_Dist_Rec.segment18;
6078 
6079                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT19') and
6080                     (c_Dist_Rec.segment19 is not null)) then
6081                   l_seg_val(l_index) := c_Dist_Rec.segment19;
6082 
6083                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT20') and
6084                     (c_Dist_Rec.segment20 is not null)) then
6085                   l_seg_val(l_index) := c_Dist_Rec.segment20;
6086 
6087                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT21') and
6088                     (c_Dist_Rec.segment21 is not null)) then
6089                   l_seg_val(l_index) := c_Dist_Rec.segment21;
6090 
6091                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT22') and
6092                     (c_Dist_Rec.segment22 is not null)) then
6093                   l_seg_val(l_index) := c_Dist_Rec.segment22;
6094 
6095                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT23') and
6096                     (c_Dist_Rec.segment23 is not null)) then
6097                   l_seg_val(l_index) := c_Dist_Rec.segment23;
6098 
6099                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT24') and
6100                     (c_Dist_Rec.segment24 is not null)) then
6101                   l_seg_val(l_index) := c_Dist_Rec.segment24;
6102 
6103                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT25') and
6104                     (c_Dist_Rec.segment25 is not null)) then
6105                   l_seg_val(l_index) := c_Dist_Rec.segment25;
6106 
6107                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT26') and
6108                     (c_Dist_Rec.segment26 is not null)) then
6109                   l_seg_val(l_index) := c_Dist_Rec.segment26;
6110 
6111                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT27') and
6112                     (c_Dist_Rec.segment27 is not null)) then
6113                   l_seg_val(l_index) := c_Dist_Rec.segment27;
6114 
6115                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT28') and
6116                     (c_Dist_Rec.segment28 is not null)) then
6117                   l_seg_val(l_index) := c_Dist_Rec.segment28;
6118 
6119                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT29') and
6120                     (c_Dist_Rec.segment29 is not null)) then
6121                   l_seg_val(l_index) := c_Dist_Rec.segment29;
6122 
6123                 elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT30') and
6124                     (c_Dist_Rec.segment30 is not null)) then
6125                   l_seg_val(l_index) := c_Dist_Rec.segment30;
6126 
6127                 else
6128                   l_seg_val(l_index) := l_ccid_val(l_index);
6129                 end if;
6130 
6131               end loop;
6132 
6133               if not FND_FLEX_EXT.Get_Combination_ID
6134                 (application_short_name => 'SQLGL',
6135                  key_flex_code => 'GL#',
6136                  structure_number => g_flex_code,
6137                  validation_date => sysdate,
6138                  n_segments => PSB_WS_ACCT1.g_num_segs,
6139                  segments => l_seg_val,
6140                  combination_id => l_ccid) then
6141 
6142                 FND_MSG_PUB.Add;
6143                 raise FND_API.G_EXC_ERROR;
6144               end if;
6145 
6146 
6147               for l_dist_index in 1..g_num_dists loop
6148 
6149                 if g_dists(l_dist_index).ccid = l_ccid then
6150                   l_dist_found := TRUE;
6151                   l_account_index := l_dist_index;
6152                 end if;
6153 
6154               end loop;
6155 
6156               if not l_dist_found then
6157               begin
6158 
6159                 g_num_dists := g_num_dists + 1;
6160 
6161                 g_dists(g_num_dists).ccid := l_ccid;
6162                 g_dists(g_num_dists).budget_group_id := PSB_WS_POS1.g_salary_budget_group_id;
6163                 g_dists(g_num_dists).currency_code := g_func_currency;
6164                 g_dists(g_num_dists).start_date := l_start_date;
6165                 g_dists(g_num_dists).end_date := l_end_date;
6166                 g_dists(g_num_dists).amount := l_percent *
6167                                                Prorate_Ratio (p_original_amount => g_costs(l_calc_index).element_cost,
6168                                                               p_original_start_date => g_costs(l_calc_index).start_date,
6169                                                               p_original_end_date => g_costs(l_calc_index).end_date,
6170                                                               p_new_start_date => l_dist_start_date,
6171                                                               p_new_end_date => l_dist_end_date);
6172                 g_dists(g_num_dists).calc_rev := FALSE;
6173 
6174               end;
6175               else
6176                 g_dists(l_account_index).amount := nvl(g_dists(l_account_index).amount, 0) + l_percent *
6177                                                    Prorate_Ratio (p_original_amount => g_costs(l_calc_index).element_cost,
6178                                                                   p_original_start_date => g_costs(l_calc_index).start_date,
6179                                                                   p_original_end_date => g_costs(l_calc_index).end_date,
6180                                                                   p_new_start_date => l_dist_start_date,
6181                                                                   p_new_end_date => l_dist_end_date);
6182               end if;
6183 
6184             end;
6185             end if;
6186 
6187           end loop;
6188 
6189         end;
6190         end if;
6191 
6192       end loop;
6193 
6194     end;
6195     end if;
6196 
6197   end loop;
6198 
6199   -- Initialize API return status to success
6200 
6201   p_return_status := FND_API.G_RET_STS_SUCCESS;
6202 
6203 
6204 EXCEPTION
6205 
6206    when FND_API.G_EXC_ERROR then
6207      p_return_status := FND_API.G_RET_STS_ERROR;
6208 
6209    when FND_API.G_EXC_UNEXPECTED_ERROR then
6210      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6211 
6212    when OTHERS then
6213      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6214 
6215 END Distribute_Following_Elements;
6216 
6217 /* ----------------------------------------------------------------------- */
6218 
6219 PROCEDURE Distribute_Other_Elements
6220 ( p_return_status         OUT  NOCOPY  VARCHAR2,
6221   p_pay_element_id        IN   NUMBER,
6222   p_position_id           IN   NUMBER,
6223   p_revision_start_date   IN   DATE,
6224   p_revision_end_date     IN   DATE
6225 ) IS
6226 
6227   l_amount                NUMBER;
6228   l_percent               NUMBER;
6229 
6230   l_start_date            DATE;
6231   l_end_date              DATE;
6232 
6233   l_dist_found            BOOLEAN;
6234   l_account_index         BINARY_INTEGER;
6235 
6236   cursor c_Dist is
6237     select a.code_combination_id,
6238            a.distribution_percent, a.effective_start_date, a.effective_end_date
6239       from PSB_PAY_ELEMENT_DISTRIBUTIONS a,
6240            PSB_ELEMENT_POS_SET_GROUPS b,
6241            PSB_SET_RELATIONS c,
6242            PSB_BUDGET_POSITIONS d
6243      where a.chart_of_accounts_id = g_flex_code
6244        and (((a.effective_start_date <= p_revision_end_date)
6245          and (a.effective_end_date is null))
6246          or ((a.effective_start_date between p_revision_start_date and p_revision_end_date)
6247           or (a.effective_end_date between p_revision_start_date and p_revision_end_date)
6248          or ((a.effective_start_date < p_revision_start_date)
6249          and (a.effective_end_date > p_revision_end_date))))
6250        and a.position_set_group_id = b.position_set_group_id
6251        and b.position_set_group_id = c.position_set_group_id
6252        and b.pay_element_id = p_pay_element_id
6253        and c.account_position_set_id = d.account_position_set_id
6254        and d.data_extract_id = g_data_extract_id
6255        and d.position_id = p_position_id
6256      order by a.distribution_percent desc;
6257 
6258 BEGIN
6259 
6260   for l_calc_index in 1..g_num_costs loop
6261 
6262     if g_costs(l_calc_index).pay_element_id = p_pay_element_id then
6263     begin
6264 
6265       for c_Dist_Rec in c_Dist loop
6266 
6267         if (((c_Dist_Rec.effective_start_date <= g_costs(l_calc_index).end_date) and
6268              (c_Dist_Rec.effective_end_date is null)) or
6269             ((c_Dist_Rec.effective_start_date between g_costs(l_calc_index).start_date and g_costs(l_calc_index).end_date) or
6270              (c_Dist_Rec.effective_end_date between g_costs(l_calc_index).start_date and g_costs(l_calc_index).end_date) or
6271             ((c_Dist_Rec.effective_start_date < g_costs(l_calc_index).start_date) and
6272              (c_Dist_Rec.effective_end_date > g_costs(l_calc_index).end_date)))) then
6273         begin
6274 
6275           l_start_date := greatest(g_costs(l_calc_index).start_date, c_Dist_Rec.effective_start_date);
6276           l_end_date := least(g_costs(l_calc_index).end_date, nvl(c_Dist_Rec.effective_end_date, g_costs(l_calc_index).end_date));
6277 
6278           l_dist_found := FALSE;
6279           l_account_index := null;
6280 
6281           g_costs(l_calc_index).pay_element_id := p_pay_element_id;
6282 
6283           -- commented for bug # 4502946
6284           /*if c_Dist_Rec.distribution_percent < 1 then
6285             l_percent := c_Dist_Rec.distribution_percent;
6286           else
6287             l_percent := c_Dist_Rec.distribution_percent / 100;
6288           end if;*/
6289 
6290           -- added for bug # 4502946
6291           l_percent := c_Dist_Rec.distribution_percent / 100;
6292 
6293           for l_dist_index in 1..g_num_dists loop
6294 
6295             if g_dists(l_dist_index).ccid = c_Dist_Rec.code_combination_id then
6296               l_dist_found := TRUE;
6297               l_account_index := l_dist_index;
6298             end if;
6299 
6300           end loop;
6301 
6302           if not l_dist_found then
6303           begin
6304 
6305             g_num_dists := g_num_dists + 1;
6306             g_dists(g_num_dists).ccid := c_Dist_Rec.code_combination_id;
6307             g_dists(g_num_dists).budget_group_id := PSB_WS_POS1.g_salary_budget_group_id;
6308             g_dists(g_num_dists).currency_code := g_func_currency;
6309             g_dists(g_num_dists).start_date := l_start_date;
6310             g_dists(g_num_dists).end_date := l_end_date;
6311             g_dists(g_num_dists).amount := l_percent *
6312                                            Prorate_Ratio (p_original_amount => g_costs(l_calc_index).element_cost,
6313                                                           p_original_start_date => g_costs(l_calc_index).start_date,
6314                                                           p_original_end_date => g_costs(l_calc_index).end_date,
6315                                                           p_new_start_date => l_start_date,
6316                                                           p_new_end_date => l_end_date);
6317             g_dists(g_num_dists).calc_rev := FALSE;
6318 
6319           end;
6320           else
6321             g_dists(l_account_index).amount := nvl(g_dists(l_account_index).amount, 0) + l_percent *
6322                                                Prorate_Ratio (p_original_amount => g_costs(l_calc_index).element_cost,
6323                                                               p_original_start_date => g_costs(l_calc_index).start_date,
6324                                                               p_original_end_date => g_costs(l_calc_index).end_date,
6325                                                               p_new_start_date => l_start_date,
6326                                                               p_new_end_date => l_end_date);
6327           end if;
6328 
6329         end;
6330         end if;
6331 
6332       end loop;
6333 
6334     end;
6335     end if;
6336 
6337   end loop;
6338 
6339   -- Initialize API return status to success
6340 
6341   p_return_status := FND_API.G_RET_STS_SUCCESS;
6342 
6343 
6344 EXCEPTION
6345 
6346    when FND_API.G_EXC_ERROR then
6347      p_return_status := FND_API.G_RET_STS_ERROR;
6348 
6349    when FND_API.G_EXC_UNEXPECTED_ERROR then
6350      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6351 
6352    when OTHERS then
6353      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6354 
6355 END Distribute_Other_Elements;
6356 
6357 /*-------------------------------------------------------------------------*/
6358 
6359 PROCEDURE Distribute_Position_Cost
6360 ( p_return_status         OUT  NOCOPY  VARCHAR2,
6361   p_position_id           IN   NUMBER,
6362   p_revision_start_date   IN   DATE,
6363   p_revision_end_date     IN   DATE
6364 ) IS
6365 
6366   l_return_status         VARCHAR2(1);
6367 
6368 BEGIN
6369 
6370   for l_element_index in 1..PSB_WS_POS1.g_num_elements loop
6371 
6372     if PSB_WS_POS1.g_elements(l_element_index).salary_flag = 'Y' then
6373     begin
6374 
6375       Distribute_Salary
6376              (p_return_status  => l_return_status,
6377               p_pay_element_id => PSB_WS_POS1.g_elements(l_element_index).pay_element_id,
6378               p_position_id => p_position_id,
6379               p_revision_start_date => p_revision_start_date,
6380               p_revision_end_date => p_revision_end_date);
6381 
6382       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6383         raise FND_API.G_EXC_ERROR;
6384       end if;
6385 
6386     end;
6387     else
6388     begin
6389 
6390       if PSB_WS_POS1.g_elements(l_element_index).follow_salary = 'Y' then
6391       begin
6392 
6393         Distribute_Following_Elements
6394            (p_return_status => l_return_status,
6395             p_pay_element_id => PSB_WS_POS1.g_elements(l_element_index).pay_element_id,
6396             p_position_id => p_position_id,
6397             p_revision_start_date => p_revision_start_date,
6398             p_revision_end_date => p_revision_end_date);
6399 
6400         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6401           raise FND_API.G_EXC_ERROR;
6402         end if;
6403 
6404       end;
6405       else
6406       begin
6407 
6408         Distribute_Other_Elements
6409            (p_return_status => l_return_status,
6410             p_pay_element_id => PSB_WS_POS1.g_elements(l_element_index).pay_element_id,
6411             p_position_id => p_position_id,
6412             p_revision_start_date => p_revision_start_date,
6413             p_revision_end_date => p_revision_end_date);
6414 
6415         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6416           raise FND_API.G_EXC_ERROR;
6417         end if;
6418 
6419       end;
6420       end if;
6421 
6422      end;
6423      end if;
6424 
6425   end loop;
6426 
6427 -- Initialize API return status to success
6428 
6429   p_return_status := FND_API.G_RET_STS_SUCCESS;
6430 
6431 
6432 EXCEPTION
6433 
6434    when FND_API.G_EXC_ERROR then
6435      p_return_status := FND_API.G_RET_STS_ERROR;
6436 
6437    when FND_API.G_EXC_UNEXPECTED_ERROR then
6438      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6439 
6440    when OTHERS then
6441      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6442 
6443 End Distribute_Position_Cost;
6444 /*---------------------------------------------------------------------------*/
6445 
6446 
6447 /*===========================================================================+
6448  |                       PROCEDURE Update_Position_Cost                      |
6449  +===========================================================================*/
6450 PROCEDURE Update_Position_Cost
6451 ( p_return_status       OUT NOCOPY VARCHAR2
6452 , p_mass_revision       IN         BOOLEAN
6453 , p_position_id         IN         NUMBER
6454 , p_hr_budget_id        IN         NUMBER
6455 , p_budget_revision_id  IN         NUMBER
6456 , p_revision_start_date IN         DATE
6457 , p_revision_end_date   IN         DATE
6458 -- Added p_zero_revised_fte for bug 2896687
6459 , p_zero_revised_fte    IN         BOOLEAN := FALSE
6460 , p_parameter_id        IN         NUMBER -- Bug#4675858
6461 )
6462 IS
6463   --
6464   l_return_status             VARCHAR2(1);
6465   l_msg_count                 NUMBER;
6466   l_msg_data                  VARCHAR2(2000);
6467   --
6468   l_budget_rev_acct_line_id   NUMBER;
6469   l_budget_rev_pos_line_id    NUMBER;
6470   l_revision_amount           NUMBER;
6471   l_rounded_amount            NUMBER;
6472   l_revision_type             VARCHAR2(1);
6473   l_gl_budget_version_id      NUMBER;
6474   l_start_date                DATE;
6475   l_end_date                  DATE;
6476   l_gl_period_name            VARCHAR2(15);
6477   l_account_found             BOOLEAN;
6478   --
6479   l_pos_line_id               NUMBER ;         -- Bug#4675858
6480   l_note_parameter_name       VARCHAR2(30) ;   -- Bug#4675858
6481   l_note                      VARCHAR2(4000) ; -- Bug#4675858
6482   --
6483   CURSOR c_currdist IS
6484   SELECT code_combination_id, start_date, end_date, amount
6485   FROM   psb_position_accounts
6486   WHERE  position_id = p_position_id
6487   AND    NVL (hr_budget_id, -1) = NVL (p_hr_budget_id, -1)
6488   AND    currency_code = g_func_currency
6489   AND    base_line_version = 'C'
6490   AND    (
6491               start_date BETWEEN p_revision_start_date AND p_revision_end_date
6492            OR end_date BETWEEN p_revision_start_date AND p_revision_end_date
6493            OR (
6494                     start_date < p_revision_start_date
6495                 AND end_date > p_revision_end_date
6496               )
6497          );
6498   --
6499   CURSOR c_period IS
6500   SELECT period_name, start_date, end_date
6501   FROM   gl_period_statuses
6502   WHERE  application_id = 101
6503   AND    set_of_books_id = g_set_of_books_id
6504   AND    p_revision_start_date BETWEEN start_date AND end_date;
6505   --
6506   -- Bug#4675858
6507   CURSOR c_ParamName
6508   IS
6509   SELECT name
6510   FROM psb_entity
6511   WHERE entity_id = p_parameter_id ;
6512   --
6513 BEGIN
6514 
6515   --pd('g_num_dists:' || g_num_dists);
6516   for c_currdist_rec in c_currdist loop
6517 
6518     l_account_found := FALSE;
6519 
6520     FOR l_dist_index in 1..g_num_dists LOOP
6521 
6522       IF (     g_dists(l_dist_index).ccid = c_currdist_rec.code_combination_id
6523            AND (
6524                     g_dists(l_dist_index).start_date BETWEEN
6525                     c_currdist_rec.start_date AND c_currdist_rec.end_date
6526                  OR g_dists(l_dist_index).end_date BETWEEN
6527                     c_currdist_rec.start_date AND c_currdist_rec.end_date
6528                  OR (
6529                     g_dists(l_dist_index).start_date < c_currdist_rec.start_date
6530                     AND g_dists(l_dist_index).end_date > c_currdist_rec.end_date
6531                     )
6532                )
6533          )
6534       THEN
6535         l_account_found := TRUE;
6536         g_num_revaccts := g_num_revaccts + 1;
6537         g_revaccts(g_num_revaccts).ccid := c_currdist_rec.code_combination_id;
6538 
6539         /* Bug No 2141915 Start */
6540         /* Added OR condition for Bug 2896687 */
6541         if nvl(g_dists(l_dist_index).amount, 0) <> 0
6542            OR p_zero_revised_fte then
6543           --
6544           g_revaccts(g_num_revaccts).amount := g_dists(l_dist_index).amount -
6545             Prorate_Ratio( p_original_amount     => c_currdist_rec.amount,
6546                            p_original_start_date => c_currdist_rec.start_date,
6547                            p_original_end_date   => c_currdist_rec.end_date,
6548                            p_new_start_date      => p_revision_start_date,
6549                            p_new_end_date        => p_revision_end_date ) ;
6550           --
6551         else
6552           g_revaccts(g_num_revaccts).amount :=
6553             Prorate_Ratio ( p_original_amount     => c_currdist_rec.amount,
6554                             p_original_start_date => c_currdist_rec.start_date,
6555                             p_original_end_date   => c_currdist_rec.end_date,
6556                             p_new_start_date      => p_revision_start_date,
6557                             p_new_end_date        => p_revision_end_date ) ;
6558         end if;
6559         /* Bug No 2141915 End */
6560         g_dists(l_dist_index).calc_rev := TRUE;
6561       end if;
6562 
6563     END LOOP;
6564 
6565     -- account not in current distribution; need to reverse them
6566     if not l_account_found then
6567       --
6568       g_num_revaccts := g_num_revaccts + 1;
6569       g_revaccts(g_num_revaccts).ccid := c_currdist_rec.code_combination_id;
6570       g_revaccts(g_num_revaccts).amount := -1 *
6571         Prorate_Ratio ( p_original_amount     => c_currdist_rec.amount,
6572                         p_original_start_date => c_currdist_rec.start_date,
6573                         p_original_end_date   => c_currdist_rec.end_date,
6574                         p_new_start_date      => p_revision_start_date,
6575                         p_new_end_date        => p_revision_end_date ) ;
6576       --
6577     end if;
6578 
6579   end loop;
6580 
6581   -- now find out which accounts in g_dist are new; add as is
6582   for l_dist_index in 1..g_num_dists loop
6583     if not g_dists(l_dist_index).calc_rev then
6584       --
6585       g_num_revaccts := g_num_revaccts + 1;
6586       g_revaccts(g_num_revaccts).ccid := g_dists(l_dist_index).ccid;
6587       g_revaccts(g_num_revaccts).amount := nvl(g_dists(l_dist_index).amount, 0);
6588       --
6589     end if;
6590 
6591   end loop;
6592 
6593   -- update fte values in the budget revisions table
6594   for l_fte_index in 1..g_num_fte_assignments loop
6595 
6596     PSB_POSITION_CONTROL_PVT.Modify_Position_FTE
6597     ( p_api_version => 1.0,
6598       p_return_status => l_return_status,
6599       p_msg_count => l_msg_count,
6600       p_msg_data => l_msg_data,
6601       p_position_id => p_position_id,
6602       p_hr_budget_id => p_hr_budget_id,
6603       p_budget_revision_id => p_budget_revision_id,
6604       p_base_line_version => null,
6605       p_start_date => g_fte_assignments(l_fte_index).start_date,
6606       p_end_date => g_fte_assignments(l_fte_index).end_date,
6607       p_fte => g_fte_assignments(l_fte_index).fte
6608     ) ;
6609     --
6610     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6611       raise FND_API.G_EXC_ERROR;
6612     end if;
6613     --
6614 
6615     if ((p_mass_revision) or (g_new_position)) then
6616     begin
6617 
6618       Create_Revision_Positions
6619             (p_api_version => 1.0,
6620              p_return_status => l_return_status,
6621              p_msg_count => l_msg_count,
6622              p_msg_data => l_msg_data,
6623              p_budget_revision_id => p_budget_revision_id,
6624              p_budget_revision_pos_line_id => l_budget_rev_pos_line_id,
6625              p_position_id => p_position_id,
6626              p_budget_group_id => PSB_WS_POS1.g_salary_budget_group_id,
6627              p_effective_start_date => p_revision_start_date,
6628              p_effective_end_date => p_revision_end_date,
6629              p_revision_type => null,
6630              p_revision_value_type => null,
6631              p_revision_value => null,
6632              p_note_id => null,
6633              p_freeze_flag => 'N',
6634              p_view_line_flag => 'Y');
6635 
6636       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6637         raise FND_API.G_EXC_ERROR;
6638       end if;
6639 
6640     end;
6641     end if;
6642 
6643   end loop;
6644 
6645   for l_cost_index in 1..g_num_costs loop
6646 
6647     PSB_POSITION_CONTROL_PVT.Modify_Position_Costs
6648        (p_api_version => 1.0,
6649         p_return_status => l_return_status,
6650         p_msg_count => l_msg_count,
6651         p_msg_data => l_msg_data,
6652         p_position_id => p_position_id,
6653         p_hr_budget_id => p_hr_budget_id,
6654         p_pay_element_id => g_costs(l_cost_index).pay_element_id,
6655         p_budget_revision_id => p_budget_revision_id,
6656         p_base_line_version => null,
6657         p_start_date => g_costs(l_cost_index).start_date,
6658         p_end_date => g_costs(l_cost_index).end_date,
6659         p_currency_code => g_costs(l_cost_index).currency_code,
6660         p_element_cost => g_costs(l_cost_index).element_cost);
6661 
6662     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6663       raise FND_API.G_EXC_ERROR;
6664     end if;
6665 
6666   end loop;
6667 
6668   for l_dist_index in 1..g_num_dists loop
6669 
6670     PSB_POSITION_CONTROL_PVT.Modify_Position_Accounts
6671        (p_api_version => 1.0,
6672         p_return_status => l_return_status,
6673         p_msg_count => l_msg_count,
6674         p_msg_data => l_msg_data,
6675         p_position_id => p_position_id,
6676         p_hr_budget_id => p_hr_budget_id,
6677         p_budget_revision_id => p_budget_revision_id,
6678         p_budget_group_id => g_dists(l_dist_index).budget_group_id,
6679         p_base_line_version => null,
6680         p_start_date => g_dists(l_dist_index).start_date,
6681         p_end_date => g_dists(l_dist_index).end_date,
6682         p_code_combination_id => g_dists(l_dist_index).ccid,
6683         p_currency_code => g_dists(l_dist_index).currency_code,
6684         p_amount => g_dists(l_dist_index).amount);
6685 
6686     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6687       raise FND_API.G_EXC_ERROR;
6688     end if;
6689 
6690   end loop;
6691 
6692   for c_period_rec in c_period loop
6693     l_gl_period_name := c_period_rec.period_name;
6694     l_start_date  := c_period_rec.start_date;
6695     l_end_date  := c_period_rec.end_date;
6696   end loop;
6697 
6698   for l_revacct_index in 1..g_num_revaccts loop
6699 
6700     l_gl_budget_version_id := null;
6701     l_budget_rev_acct_line_id := null;
6702 
6703     PSB_GL_BUDGET_PVT.Find_GL_Budget
6704        (p_api_version => 1.0,
6705         p_return_status => l_return_status,
6706         p_msg_count => l_msg_count,
6707         p_msg_data => l_msg_data,
6708         p_gl_budget_set_id => g_gl_budget_set_id,
6709         p_code_combination_id => g_revaccts(l_revacct_index).ccid,
6710         p_start_date => l_start_date,
6711         p_dual_posting_type => 'A',
6712         p_gl_budget_version_id => l_gl_budget_version_id);
6713 
6714     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6715       raise FND_API.G_EXC_ERROR;
6716     end if;
6717 
6718     if l_gl_budget_version_id is null then
6719     begin
6720 
6721       if g_permanent_revision = 'N' then
6722       begin
6723 
6724         PSB_GL_BUDGET_PVT.Find_GL_Budget
6725          (p_api_version => 1.0,
6726           p_return_status => l_return_status,
6727           p_msg_count => l_msg_count,
6728           p_msg_data => l_msg_data,
6729           p_gl_budget_set_id => g_gl_budget_set_id,
6730           p_code_combination_id => g_revaccts(l_revacct_index).ccid,
6731           p_start_date => l_start_date,
6732           p_gl_budget_version_id => l_gl_budget_version_id);
6733 
6734        if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6735          raise FND_API.G_EXC_ERROR;
6736        end if;
6737 
6738       end;
6739       end if;
6740 
6741     end;
6742     end if;
6743 
6744     l_revision_amount := g_revaccts(l_revacct_index).amount;
6745     if l_revision_amount < 0 then
6746        l_revision_type := 'D';
6747        l_revision_amount := abs(l_revision_amount);
6748     else
6749        l_revision_type := 'I';
6750     end if;
6751 
6752      l_rounded_amount := Get_Rounded_Amount(g_func_currency,l_revision_amount);
6753 
6754     if ((l_rounded_amount <> 0) or ((l_rounded_amount = 0) and (g_create_zero_bal = 'Y'))) then
6755     begin
6756 
6757       Create_Revision_Accounts
6758             (p_api_version                  => 1.0,
6759              p_return_status              => l_return_status,
6760              p_msg_count                    => l_msg_count,
6761              p_msg_data                   => l_msg_data,
6762              p_budget_revision_id           => p_budget_revision_id,
6763              p_budget_revision_acct_line_id => l_budget_rev_acct_line_id,
6764              p_code_combination_id          => g_revaccts(l_revacct_index).ccid,
6765              p_budget_group_id              => PSB_WS_POS1.g_salary_budget_group_id,
6766              p_gl_period_name               => l_gl_period_name,
6767              p_gl_budget_version_id         => l_gl_budget_version_id,
6768              p_position_id                  => p_position_id,
6769              p_currency_code                => g_func_currency,
6770              p_budget_balance               => null,
6771              p_revision_type                => l_revision_type,
6772              p_revision_value_type          => 'A',
6773              p_revision_amount              => l_rounded_amount,
6774              p_note_id                      => null,
6775              p_funds_control_timestamp      => null,
6776              p_funds_status_code            => null,
6777              p_funds_result_code            => null,
6778              p_freeze_flag                  => 'N',
6779              p_view_line_flag               => 'Y');
6780 
6781       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6782         raise FND_API.G_EXC_ERROR;
6783       end if;
6784 
6785       -- Bug#4675858 Start
6786       -- Added note call for Revise Elements Projections.
6787       IF ( g_elem_projection )
6788       THEN
6789         -- Get parameter name
6790         FOR c_ParamName_Rec IN c_ParamName
6791         LOOP
6792           l_note_parameter_name := c_ParamName_Rec.name ;
6793         END LOOP ;
6794         --
6795         -- Get position line id for corresponding position.
6796         SELECT
6797           budget_revision_pos_line_id
6798         INTO
6799 	  l_pos_line_id
6800         FROM
6801 	  psb_budget_revision_position_v
6802         WHERE
6803           budget_revision_id = p_budget_revision_id
6804           AND position_id    = p_position_id ;
6805         --
6806         FND_MESSAGE.Set_Name('PSB', 'PSB_PARAMETER_NOTE_CREATION') ;
6807         FND_MESSAGE.Set_Token('NAME', l_note_parameter_name) ;
6808         FND_MESSAGE.Set_Token('DATE', sysdate) ;
6809 
6810         l_note := FND_MESSAGE.Get ;
6811 
6812         Create_Note
6813         ( p_return_status    => l_return_status
6814         , p_account_line_id  => NULL
6815         , p_position_line_id => l_pos_line_id
6816         , p_note             => l_note
6817         , p_flex_code        => g_flex_code
6818         , p_cc_id            => g_revaccts(l_revacct_index).ccid
6819         ) ;
6820 
6821         IF l_return_status <> FND_API.G_RET_STS_SUCCESS
6822         THEN
6823           RAISE FND_API.G_EXC_ERROR ;
6824         END IF ;
6825       END IF ;
6826       -- Bug#4675858 End
6827 
6828     end;
6829     end if;
6830 
6831   end loop;
6832 
6833   if not p_mass_revision then
6834   begin
6835 
6836     Create_Summary_Position_Line
6837           (p_return_status => l_return_status,
6838            p_budget_revision_id => p_budget_revision_id,
6839            p_currency_code => g_func_currency,
6840            p_gl_period_name => l_gl_period_name);
6841 
6842     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6843       raise FND_API.G_EXC_ERROR;
6844     end if;
6845 
6846   end;
6847   end if;
6848 
6849   p_return_status := FND_API.G_RET_STS_SUCCESS;
6850 
6851 EXCEPTION
6852 
6853    when FND_API.G_EXC_ERROR then
6854      p_return_status := FND_API.G_RET_STS_ERROR;
6855 
6856    when FND_API.G_EXC_UNEXPECTED_ERROR then
6857      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6858 
6859    when OTHERS then
6860      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6861 
6862 End Update_Position_Cost;
6863 
6864 /* ----------------------------------------------------------------------- */
6865 
6866 PROCEDURE Calculate_Position_Cost
6867 ( p_api_version         IN         NUMBER
6868 , p_init_msg_list       IN         VARCHAR2 := FND_API.G_FALSE
6869 , p_commit              IN         VARCHAR2 := FND_API.G_FALSE
6870 , p_validation_level    IN         NUMBER   := FND_API.G_VALID_LEVEL_FULL
6871 , p_msg_count           OUT NOCOPY NUMBER
6872 , p_msg_data            OUT NOCOPY VARCHAR2
6873 , p_return_status       OUT NOCOPY VARCHAR2
6874 , p_mass_revision       IN         BOOLEAN  := FALSE
6875 , p_budget_revision_id  IN         NUMBER
6876 , p_position_id         IN         NUMBER
6877 , p_revision_start_date IN         DATE
6878 , p_revision_end_date   IN         DATE
6879 , p_parameter_id        IN         NUMBER DEFAULT NULL -- Bug#4675858
6880 )
6881 IS
6882 
6883    l_api_name          CONSTANT VARCHAR2(30) := 'Calculate_Position_Cost';
6884    l_api_version       CONSTANT NUMBER       := 1.0;
6885 
6886    l_start_date                 DATE;
6887    l_end_date                   DATE;
6888 
6889    l_element_found              BOOLEAN;
6890    l_fte_found                  BOOLEAN := FALSE;
6891    l_position_name              VARCHAR2(240);
6892    l_position_start_date        DATE;
6893    l_position_end_date          DATE;
6894    --UTF8 changes for Bug No : 2615261
6895    l_attribute_value            psb_attribute_values.attribute_value%TYPE;
6896 
6897    l_msg_count                  NUMBER;
6898    l_msg_data                   VARCHAR2(2000);
6899    l_original_fte               NUMBER;
6900    l_current_fte                NUMBER;
6901    l_revised_fte                NUMBER;
6902    -- Added l_zero_revised_fte for bug 2896687
6903    l_zero_revised_fte           BOOLEAN := FALSE;
6904 
6905    l_return_status              VARCHAR2(1);
6906    -- bug no 3439168
6907    l_acct_line_cnt              NUMBER;
6908 
6909    cursor c_Positions is
6910      select position_id,
6911             name,
6912             effective_start_date,
6913             effective_end_date
6914        from PSB_POSITIONS
6915       where position_id = p_position_id;
6916 
6917    cursor c_Element_Assignments is
6918      select worksheet_id,
6919             pay_element_id,
6920             pay_element_option_id,
6921             pay_basis,
6922             element_value_type,
6923             element_value,
6924             effective_start_date,
6925             effective_end_date
6926        from PSB_POSITION_ASSIGNMENTS
6927       where ((worksheet_id = g_global_budget_revision_id) or (worksheet_id is null))
6928         and currency_code = g_func_currency
6929         and assignment_type = 'ELEMENT'
6930         and (((effective_start_date <= l_end_date)
6931           and (effective_end_date is null))
6932           or ((effective_start_date between l_start_date and l_end_date)
6933            or (effective_end_date between l_start_date and l_end_date)
6934           or ((effective_start_date < l_start_date)
6935           and (effective_end_date > l_end_date))))
6936         and position_id = p_position_id
6937       order by effective_start_date, effective_end_date, element_value desc;
6938 
6939    cursor c_Element_Rates is
6940      select a.worksheet_id,
6941             a.pay_element_id,
6942             a.pay_element_option_id,
6943             a.pay_basis,
6944             a.element_value_type,
6945             a.element_value,
6946             a.formula_id,
6947             a.effective_start_date,
6948             a.effective_end_date
6949        from PSB_PAY_ELEMENT_RATES a,
6950             PSB_PAY_ELEMENTS b
6951       where (a.worksheet_id is null or a.worksheet_id = g_global_budget_revision_id)
6952         and a.currency_code = g_func_currency
6953         and exists
6954            (select 1
6955               from PSB_POSITION_ASSIGNMENTS c
6956              where nvl(c.pay_element_option_id, FND_API.G_MISS_NUM) = nvl(a.pay_element_option_id, FND_API.G_MISS_NUM)
6957                and ((c.worksheet_id = g_global_budget_revision_id) or (c.worksheet_id is null))
6958                and c.currency_code = g_func_currency
6959                and (((c.effective_start_date <= l_end_date)
6960                  and (c.effective_end_date is null))
6961                  or ((c.effective_start_date between l_start_date and l_end_date)
6962                   or (c.effective_end_date between l_start_date and l_end_date)
6963                  or ((c.effective_start_date < l_start_date)
6964                  and (c.effective_end_date > l_end_date))))
6965                and c.pay_element_id = a.pay_element_id
6966                and c.position_id = p_position_id)
6967         and (((a.effective_start_date <= l_end_date)
6968           and (a.effective_end_date is null))
6969           or ((a.effective_start_date between l_start_date and l_end_date)
6970            or (a.effective_end_date between l_start_date and l_end_date)
6971           or ((a.effective_start_date < l_start_date)
6972           and (a.effective_end_date > l_end_date))))
6973         and a.pay_element_id = b.pay_element_id
6974         and b.business_group_id = g_business_group_id
6975         and b.data_extract_id = g_data_extract_id
6976       order by a.worksheet_id, a.effective_start_date, a.effective_end_date, a.element_value desc;
6977 
6978   cursor c_Attribute_Assignments is
6979     select worksheet_id,
6980            effective_start_date,
6981            effective_end_date,
6982            attribute_id,
6983            -- Fixed bug # 3683644
6984            FND_NUMBER.canonical_to_number(attribute_value) attribute_value,
6985            attribute_value_id
6986       from PSB_POSITION_ASSIGNMENTS
6987      where attribute_id in (PSB_WS_POS1.g_default_wklyhrs_id, PSB_WS_POS1.g_fte_id)
6988        and (( worksheet_id = g_global_budget_revision_id) or (worksheet_id is null))
6989        and assignment_type = 'ATTRIBUTE'
6990        and (((effective_start_date <= l_end_date)
6991          and (effective_end_date is null))
6992          or ((effective_start_date between l_start_date and l_end_date)
6993           or (effective_end_date between l_start_date and l_end_date)
6994          or ((effective_start_date < l_start_date)
6995          and (effective_end_date > l_end_date))))
6996        and position_id = p_position_id
6997      order by worksheet_id,
6998               effective_start_date,
6999               effective_end_date,
7000               FND_NUMBER.canonical_to_number(attribute_value) desc; -- Fixed bug # 3683644
7001 
7002   cursor c_fte is
7003     select brp.revision_type, brp.revision_value_type, brp.revision_value, brp.effective_start_date, brp.effective_end_date
7004       from PSB_BUDGET_REVISION_POSITIONS brp, PSB_BUDGET_REVISION_POS_LINES brpl
7005      where brp.position_id = p_position_id
7006        and ((effective_start_date between p_revision_start_date and p_revision_end_date)
7007          or (effective_end_date between p_revision_start_date and p_revision_end_date)
7008          or ((effective_start_date < p_revision_start_date)
7009          and (effective_end_date > p_revision_end_date)))
7010        and brpl.budget_revision_id = p_budget_revision_id
7011        and brpl.budget_revision_pos_line_id = brp.budget_revision_pos_line_id
7012        and brp.revision_value is not null;
7013 
7014   cursor c_current_fte is
7015     select start_date, end_date, fte
7016       from PSB_POSITION_FTE
7017      where position_id = p_position_id
7018        and nvl(hr_budget_id, -1) = nvl(g_hr_budget_id, -1)
7019        and base_line_version = 'C'
7020        and ((start_date between p_revision_start_date and p_revision_end_date)
7021          or (end_date between p_revision_start_date and p_revision_end_date)
7022          or ((start_date < p_revision_start_date)
7023          and (end_date > p_revision_end_date)));
7024 
7025 BEGIN
7026 
7027  -- Standard call to check for call compatibility
7028 
7029   if not FND_API.Compatible_API_Call (l_api_version,
7030                                       p_api_version,
7031                                       l_api_name,
7032                                       G_PKG_NAME)
7033   then
7034     raise FND_API.G_EXC_UNEXPECTED_ERROR;
7035   end if;
7036 
7037   -- initialize this variable to track if this procedure is invoked when creating a new position
7038   g_new_position := FALSE;
7039   g_revised_position := FALSE;
7040 
7041   /* bug no 3439168 */
7042 -- check if the distribution table has been updated
7043   IF g_last_update_flag_tbl.exists (p_position_id) AND
7044      g_last_update_flag_tbl (p_position_id) = 1 THEN
7045 
7046     FOR l_acc_line_rec IN
7047     (SELECT COUNT(1) acct_line_cnt
7048      FROM psb_budget_revision_accounts
7049      WHERE budgeT_revision_acct_line_id
7050      IN (SELECT budget_revision_Acct_line_id
7051          FROM psb_budget_revision_lines
7052          WHERE budget_revision_id = p_budget_revision_id)
7053          AND position_id = p_position_id)
7054     LOOP
7055       l_acct_line_cnt := l_acc_line_rec.acct_line_cnt;
7056     END LOOP;
7057 
7058     -- Call reverse position accounts procedure
7059     -- to ensure proper account distributions
7060     IF l_acct_line_cnt > 0 THEN
7061       Reverse_Position_Accounts
7062       ( p_return_status         => l_return_status,
7063         p_budget_revision_id    => p_budget_revision_id,
7064         p_position_id           => p_position_id,
7065         p_effective_start_date  => p_revision_start_date,
7066         p_effective_end_date    => p_revision_end_date
7067       );
7068 
7069       IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
7070         RAISE FND_API.G_EXC_ERROR;
7071       END IF;
7072     END IF;
7073     -- reset the status of the position id back to 0
7074     g_last_update_flag_tbl(p_position_id) := 0;
7075   END IF;
7076   /* bug no 3439168 */
7077 
7078 
7079   Cache_Revision_Variables
7080        (p_return_status => l_return_status,
7081         p_budget_revision_id => p_budget_revision_id);
7082 
7083   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7084     raise FND_API.G_EXC_ERROR;
7085   end if;
7086 
7087 
7088   for c_Positions_Rec in c_Positions loop
7089     l_position_name := c_Positions_Rec.name;
7090     l_position_start_date := c_Positions_Rec.effective_start_date;
7091     l_position_end_date := c_Positions_Rec.effective_end_date;
7092   end loop;
7093 
7094   l_start_date := greatest(p_revision_start_date, l_position_start_date);
7095   l_end_date := least(p_revision_end_date,nvl(l_position_end_date, p_revision_end_date));
7096 
7097   Initialize_Revisions;
7098 
7099   Cache_Salary_Dist
7100        (p_return_status => l_return_status,
7101         p_budget_revision_id => p_budget_revision_id,
7102         p_position_id => p_position_id,
7103         p_position_name => l_position_name,
7104         p_start_date => l_start_date,
7105         p_end_date => l_end_date);
7106 
7107   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7108     raise FND_API.G_EXC_ERROR;
7109   end if;
7110 
7111   Cache_Elements
7112       (p_return_status     => l_return_status,
7113        p_start_date        => l_start_date,
7114        p_end_date          => l_end_date);
7115 
7116   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7117     raise FND_API.G_EXC_ERROR;
7118   end if;
7119 
7120   for c_Element_Assignments_Rec in c_Element_Assignments loop
7121 
7122     g_num_elem_assignments := g_num_elem_assignments + 1;
7123     g_elem_assignments(g_num_elem_assignments).budget_revision_id := c_Element_Assignments_Rec.worksheet_id;
7124     g_elem_assignments(g_num_elem_assignments).start_date := c_Element_Assignments_Rec.effective_start_date;
7125     g_elem_assignments(g_num_elem_assignments).end_date := c_Element_Assignments_Rec.effective_end_date;
7126     g_elem_assignments(g_num_elem_assignments).pay_element_id := c_Element_Assignments_Rec.pay_element_id;
7127     g_elem_assignments(g_num_elem_assignments).pay_element_option_id := c_Element_Assignments_Rec.pay_element_option_id;
7128     g_elem_assignments(g_num_elem_assignments).pay_basis := c_Element_Assignments_Rec.pay_basis;
7129     g_elem_assignments(g_num_elem_assignments).element_value_type := c_Element_Assignments_Rec.element_value_type;
7130     g_elem_assignments(g_num_elem_assignments).element_value := c_Element_Assignments_Rec.element_value;
7131     g_elem_assignments(g_num_elem_assignments).use_in_calc := FALSE;
7132 
7133   end loop;
7134 
7135   for l_element_index in 1..PSB_WS_POS1.g_num_elements loop
7136 
7137     l_element_found := FALSE;
7138 
7139     for l_elemassign_index in 1..g_num_elem_assignments loop
7140 
7141       if ((g_elem_assignments(l_elemassign_index).pay_element_id = PSB_WS_POS1.g_elements(l_element_index).pay_element_id) and
7142           (g_elem_assignments(l_elemassign_index).budget_revision_id is not null)) then
7143       begin
7144         l_element_found := TRUE;
7145         g_revised_position := TRUE;
7146         g_elem_assignments(l_elemassign_index).use_in_calc := TRUE;
7147       end;
7148       end if;
7149 
7150     end loop;
7151 
7152     if not l_element_found then
7153     begin
7154 
7155       for l_elemassign_index in 1..g_num_elem_assignments loop
7156 
7157         if ((g_elem_assignments(l_elemassign_index).pay_element_id = PSB_WS_POS1.g_elements(l_element_index).pay_element_id) and
7158             (g_elem_assignments(l_elemassign_index).budget_revision_id is null)) then
7159           g_elem_assignments(l_elemassign_index).use_in_calc := TRUE;
7160         end if;
7161 
7162       end loop;
7163 
7164     end;
7165     end if;
7166 
7167   end loop;
7168 
7169   for c_Element_Rates_Rec in c_Element_Rates loop
7170 
7171     g_num_elem_rates := g_num_elem_rates + 1;
7172 
7173     g_elem_rates(g_num_elem_rates).budget_revision_id := c_Element_Rates_Rec.worksheet_id;
7174 
7175     if c_Element_Rates_Rec.worksheet_id is not null then
7176       g_revised_position := TRUE;
7177     end if;
7178 
7179     g_elem_rates(g_num_elem_rates).start_date := c_Element_Rates_Rec.effective_start_date;
7180     g_elem_rates(g_num_elem_rates).end_date := c_Element_Rates_Rec.effective_end_date;
7181     g_elem_rates(g_num_elem_rates).pay_element_id := c_Element_Rates_Rec.pay_element_id;
7182     g_elem_rates(g_num_elem_rates).pay_element_option_id := c_Element_Rates_Rec.pay_element_option_id;
7183     g_elem_rates(g_num_elem_rates).pay_basis := c_Element_Rates_Rec.pay_basis;
7184     g_elem_rates(g_num_elem_rates).element_value_type := c_Element_Rates_Rec.element_value_type;
7185     g_elem_rates(g_num_elem_rates).element_value := c_Element_Rates_Rec.element_value;
7186     g_elem_rates(g_num_elem_rates).formula_id := c_Element_Rates_Rec.formula_id;
7187 
7188   end loop;
7189 
7190   for c_fte_rec in c_fte loop
7191 
7192     l_fte_found := TRUE;
7193     g_num_fte_assignments := g_num_fte_assignments + 1;
7194 
7195     Find_FTE
7196         (p_api_version => 1.0,
7197          p_return_status => l_return_status,
7198          p_msg_count => l_msg_count,
7199          p_msg_data => l_msg_data,
7200          p_position_id => p_position_id,
7201          p_hr_budget_id => g_hr_budget_id,
7202          p_budget_revision_id => p_budget_revision_id,
7203          p_revision_type => c_fte_rec.revision_type,
7204          p_revision_value_type => c_fte_rec.revision_value_type,
7205          p_revision_value => c_fte_rec.revision_value,
7206          p_effective_start_date => c_fte_rec.effective_start_date,
7207          p_effective_end_date => c_fte_rec.effective_end_date,
7208          p_original_fte => l_original_fte,
7209          p_current_fte => l_current_fte,
7210          p_revised_fte => l_revised_fte);
7211 
7212     g_fte_assignments(g_num_fte_assignments).start_date := c_fte_rec.effective_start_date;
7213     g_fte_assignments(g_num_fte_assignments).end_date := c_fte_rec.effective_end_date;
7214     g_fte_assignments(g_num_fte_assignments).fte := l_revised_fte;
7215 
7216     -- Added the following IF condition for bug 2896687.
7217     IF l_revised_fte = 0 THEN
7218       l_zero_revised_fte := TRUE;
7219     ELSE
7220       l_zero_revised_fte := FALSE;
7221     END IF;
7222   end loop;
7223 
7224   if not l_fte_found then
7225   begin
7226 
7227     for c_current_fte_rec in c_current_fte loop
7228       l_fte_found := TRUE;
7229       g_num_fte_assignments := g_num_fte_assignments + 1;
7230       g_fte_assignments(g_num_fte_assignments).start_date := c_current_fte_rec.start_date;
7231       g_fte_assignments(g_num_fte_assignments).end_date := c_current_fte_rec.end_date;
7232       g_fte_assignments(g_num_fte_assignments).fte := c_current_fte_rec.fte;
7233 
7234     -- Added the following IF condition for bug 2896687.
7235     IF c_current_fte_rec.fte = 0 THEN
7236       l_zero_revised_fte := TRUE;
7237     ELSE
7238       l_zero_revised_fte := FALSE;
7239     END IF;
7240     end loop;
7241 
7242   end;
7243   end if;
7244 
7245   for c_Attributes_Rec in c_Attribute_Assignments loop
7246 
7247     l_attribute_value := null;
7248 
7249     if ((c_Attributes_Rec.attribute_value is null) and (c_Attributes_Rec.attribute_value_id is not null)) then
7250       l_attribute_value := PSB_WS_POS2.Get_Attribute_Value(c_Attributes_Rec.attribute_value_id);
7251     end if;
7252 
7253     if c_Attributes_Rec.attribute_id = PSB_WS_POS1.g_default_wklyhrs_id then
7254     begin
7255       g_num_wkh_assignments := g_num_wkh_assignments + 1;
7256       g_wkh_assignments(g_num_wkh_assignments).start_date := c_Attributes_Rec.effective_start_date;
7257       g_wkh_assignments(g_num_wkh_assignments).end_date := c_Attributes_Rec.effective_end_date;
7258       g_wkh_assignments(g_num_wkh_assignments).default_weekly_hours := nvl(c_Attributes_Rec.attribute_value, l_attribute_value);
7259     end;
7260     elsif ((c_Attributes_Rec.attribute_id = PSB_WS_POS1.g_fte_id) and not (l_fte_found)) then
7261     begin
7262       g_new_position := TRUE;
7263       g_num_fte_assignments := g_num_fte_assignments + 1;
7264       g_fte_assignments(g_num_fte_assignments).start_date := c_Attributes_Rec.effective_start_date;
7265       g_fte_assignments(g_num_fte_assignments).end_date := nvl(c_Attributes_Rec.effective_end_date, l_end_date);
7266       g_fte_assignments(g_num_fte_assignments).fte := nvl(c_Attributes_Rec.attribute_value, l_attribute_value);
7267     end;
7268     end if;
7269 
7270   end loop;
7271 
7272   if g_num_fte_assignments = 0 then
7273     g_new_position := TRUE;
7274     g_num_fte_assignments := g_num_fte_assignments + 1;
7275     g_fte_assignments(g_num_fte_assignments).start_date := p_revision_start_date;
7276     g_fte_assignments(g_num_fte_assignments).end_date := p_revision_end_date;
7277     g_fte_assignments(g_num_fte_assignments).fte := g_default_fte;
7278   end if;
7279 
7280   if g_num_elem_assignments > 0 then
7281   begin
7282 
7283     Calculate_Position_Cost_Dates
7284        (p_return_status => l_return_status,
7285         p_position_id => p_position_id,
7286         p_position_name => l_position_name,
7287         p_revision_start_date => l_start_date,
7288         p_revision_end_date => l_end_date);
7289 
7290     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7291       raise FND_API.G_EXC_ERROR;
7292     end if;
7293 
7294     Distribute_Position_Cost
7295            (p_return_status => l_return_status,
7296             p_position_id => p_position_id,
7297             p_revision_start_date => l_start_date,
7298             p_revision_end_date => l_end_date);
7299 
7300     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7301       raise FND_API.G_EXC_ERROR;
7302     end if;
7303 
7304   end;
7305   end if;
7306 
7307   if ((not p_mass_revision) or ((p_mass_revision) and (g_revised_position))) then
7308   begin
7309 
7310     Update_Position_Cost
7311     ( p_return_status       => l_return_status
7312     , p_mass_revision       => p_mass_revision
7313     , p_position_id         => p_position_id
7314     , p_hr_budget_id        => g_hr_budget_id
7315     , p_budget_revision_id  => p_budget_revision_id
7316     , p_revision_start_date => l_start_date
7317     , p_revision_end_date   => l_end_date
7318     -- Added p_zero_revised_fte for bug 2896687
7319     , p_zero_revised_fte    => l_zero_revised_fte
7320     , p_parameter_id        => p_parameter_id -- Bug#4675858
7321     ) ;
7322 
7323     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7324       raise FND_API.G_EXC_ERROR;
7325     end if;
7326 
7327   end;
7328   end if;
7329 
7330   p_return_status := FND_API.G_RET_STS_SUCCESS;
7331 
7332 EXCEPTION
7333    -- Fix for Bug:3337401
7334    -- Added call to FND_MSG_PUB.Count_And_Get()
7335 
7336    when FND_API.G_EXC_ERROR then
7337      p_return_status := FND_API.G_RET_STS_ERROR;
7338      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7339                                 p_data  => p_msg_data);
7340 
7341    when FND_API.G_EXC_UNEXPECTED_ERROR then
7342      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7343      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7344                                 p_data  => p_msg_data);
7345 
7346    when OTHERS then
7347      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7348      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7349                                 p_data  => p_msg_data);
7350 
7351 End Calculate_Position_Cost;
7352 
7353 /* ----------------------------------------------------------------------- */
7354 PROCEDURE Process_Constraint
7355 ( p_return_status                 OUT  NOCOPY  VARCHAR2,
7356   p_constraint_validation_status  OUT  NOCOPY  VARCHAR2,
7357   p_budget_revision_id            IN   NUMBER,
7358   p_constraint_id                 IN   NUMBER,
7359   p_constraint_name               IN   VARCHAR2,
7360   p_ccid                          IN   NUMBER := FND_API.G_MISS_NUM,
7361   p_currency_code                 IN   VARCHAR2,
7362   p_severity_level                IN   NUMBER,
7363   p_summ_flag                     IN   VARCHAR2
7364 ) IS
7365 
7366   cursor c_Formula is
7367     select step_number, prefix_operator, budget_year_type_id, balance_type, currency_code,
7368            nvl(amount, 0) amount, postfix_operator,
7369            segment1, segment2, segment3, segment4, segment5, segment6, segment7, segment8, segment9,
7370            segment10, segment11, segment12, segment13, segment14, segment15, segment16, segment17, segment18,
7371            segment19, segment20, segment21, segment22, segment23, segment24, segment25, segment26, segment27,
7372            segment28, segment29, segment30
7373       from PSB_CONSTRAINT_FORMULAS
7374      where constraint_id = p_constraint_id
7375      order by step_number;
7376 
7377   -- Compute Sum of BR Account Lines (all Account Sets assigned to the Constraint)
7378 
7379   cursor c_SumAll is
7380     select Sum(decode(a.revision_type,'I',nvl(a.revision_amount, 0),'D',-nvl(a.revision_amount,0))) Sum_Acc
7381       from PSB_BUDGET_REVISION_LINES b,
7382            PSB_BUDGET_REVISION_ACCOUNTS a
7383      where b.budget_revision_id = p_budget_revision_id
7384        and a.budget_revision_acct_line_id = b.budget_revision_acct_line_id
7385        and a.currency_code = p_currency_code
7386        and a.position_id is null
7387        and exists
7388           (select 1
7389              from psb_budget_accounts d,
7390                   psb_set_relations_v e
7391             where d.account_position_set_id = e.account_position_set_id
7392               and d.code_combination_id = a.code_combination_id
7393               and e.account_or_position_type = 'A'
7394               and e.constraint_id = p_constraint_id);
7395 
7396   -- Compute sum of BR Account Lines for a Constraint Formula of type 4
7397   cursor c_Sum (CCID NUMBER) is
7398     select Sum(decode(a.revision_type,'I',nvl(a.revision_amount, 0),'D',-nvl(a.revision_amount,0))) Sum_Acc
7399       from PSB_BUDGET_REVISION_ACCOUNTS a,
7400            PSB_BUDGET_REVISION_LINES b
7401      where a.code_combination_id = CCID
7402        and a.currency_code = p_currency_code
7403        and a.position_id is null
7404        and b.budget_revision_id = p_budget_revision_id
7405        and a.budget_revision_acct_line_id = b.budget_revision_acct_line_id;
7406 
7407   cursor c_Original_Balance (CCID NUMBER, Currency VARCHAR2) is
7408     select sum(budget_balance) original_balance
7409       from psb_budget_revision_accounts pbra,
7410            psb_budget_revision_lines pbrl,
7411            psb_budget_revisions pbr
7412      where pbra.code_combination_id = CCID
7413        and pbra.currency_code = Currency
7414        and pbra.position_id is null
7415        and pbra.budget_revision_acct_line_id = pbrl.budget_revision_acct_line_id
7416        and pbrl.budget_revision_id = pbr.budget_revision_id
7417        and pbr.budget_group_id = g_budget_group_id
7418        and pbr.base_line_revision = 'Y'
7419        and pbra.gl_period_name in
7420           (select period_name
7421              from gl_period_statuses
7422             where application_id = 101
7423               and set_of_books_id = g_set_of_books_id
7424               and start_date between g_from_date and g_to_date
7425               and end_date between g_from_date and g_to_date);
7426 
7427   cursor c_Original_Balance_Sum (Currency VARCHAR2) is
7428     select sum(budget_balance) original_balance
7429       from psb_budget_revision_accounts pbra,
7430            psb_budget_revision_lines pbrl,
7431            psb_budget_revisions pbr
7432      where pbra.code_combination_id in
7433           (select d.code_combination_id
7434              from psb_budget_accounts d,
7435                   psb_set_relations_v e
7436             where d.account_position_set_id = e.account_position_set_id
7437               and d.code_combination_id = pbra.code_combination_id
7438               and e.account_or_position_type = 'A'
7439               and e.constraint_id = p_constraint_id)
7440        and pbra.currency_code = Currency
7441        and pbra.position_id is null
7442        and pbra.budget_revision_acct_line_id = pbrl.budget_revision_acct_line_id
7443        and pbrl.budget_revision_id = pbr.budget_revision_id
7444        and pbr.budget_group_id = g_budget_group_id
7445        and pbr.base_line_revision = 'Y'
7446        and pbra.gl_period_name in
7447           (select period_name
7448              from gl_period_statuses
7449             where application_id = 101
7450               and set_of_books_id = g_set_of_books_id
7451               and start_date between g_from_date and g_to_date
7452               and end_date between g_from_date and g_to_date);
7453 
7454   CURSOR c_Account (CCID NUMBER) IS
7455     SELECT pbra.gl_budget_version_id,
7456            pbra.gl_period_name    -- Bug 5148786
7457       FROM psb_budget_revision_accounts pbra,
7458            psb_budget_revision_lines pbrl
7459      WHERE pbra.code_combination_id = CCID
7460        AND pbra.position_id is null
7461        AND pbrl.budget_revision_id = p_budget_revision_id
7462        AND pbrl.budget_revision_acct_line_id = pbra.budget_revision_acct_line_id;
7463 
7464   CURSOR c_Account_Sum is
7465     SELECT pbra.code_combination_id,
7466            pbra.gl_budget_version_id,
7467            pbra.gl_period_name  -- Bug 5148786
7468       FROM psb_budget_revision_accounts pbra,
7469            psb_budget_revision_lines pbrl
7470      WHERE pbra.code_combination_id in
7471           (select d.code_combination_id
7472              from psb_budget_accounts d,
7473                   psb_set_relations_v e
7474             where d.account_position_set_id = e.account_position_set_id
7475               and d.code_combination_id = pbra.code_combination_id
7476               and e.account_or_position_type = 'A'
7477               and e.constraint_id = p_constraint_id)
7478        and pbra.position_id is null
7479        and pbrl.budget_revision_id = p_budget_revision_id
7480        and pbra.budget_revision_acct_line_id = pbrl.budget_revision_acct_line_id;
7481 
7482   l_first_line       VARCHAR2(1) := FND_API.G_TRUE;
7483 
7484   l_cons_failed      VARCHAR2(1) := FND_API.G_FALSE;
7485   l_type1            VARCHAR2(1);
7486   l_type2            VARCHAR2(1);
7487   l_type3            VARCHAR2(1);
7488   l_type4            VARCHAR2(1);
7489   l_type5            VARCHAR2(1);
7490 
7491   l_line_total       NUMBER := 0;
7492   l_accset_total     NUMBER := 0;
7493   l_cons_total       NUMBER := 0;
7494   l_operator         VARCHAR2(2);
7495   l_description      VARCHAR2(2000) := null;
7496   l_current_balance  NUMBER;
7497   l_concat_segments  VARCHAR2(2000);
7498 
7499 BEGIN
7500 
7501   -- Parse the Constraint Formula
7502 
7503   for c_Formula_Rec in c_Formula loop
7504 
7505     -- Each Formula Line is of the following types (Type3, Type4, Type5 are applicable for budget revisions) :
7506 
7507     -- Type1: Depends on Account Set Assignments
7508     --       (Step, Prefix Operator, Postfix Operator, Period, Balance Type, Currency, Amount have values; Account is blank; this is valid only if 'Detailed' flag is set for the Constraint)
7509     --
7510     -- Type2: Depends on Account defined in Formula Line
7511     --       (Step, Prefix Operator, Period, Balance Type, Account, Currency have values; Amount and Postfix Operator are optional; all the Segment Values should be entered if 'Detailed' flag is not set for the Constraint)
7512     --
7513     -- Type3: Flat Amount assignment
7514     --       (Step, Prefix Operator, Amount have values; Period, Balance Type, Account, Currency, Postfix Operator are blank)
7515     --
7516     -- Type4: Depends on Account Set Assignments (Detailed Constraint)
7517     --       (Step, Prefix Operator, Postfix Operator, Balance Type, Currency, Amount have values; Account, Period are blank; this is valid only if 'Detailed' flag is set for the Constraint)
7518     --
7519     -- Type5: Depends on Account Set Assignments (Summary Constraint)
7520     --       (Step, Prefix Operator, Postfix Operator, Balance Type, Currency, Amount have values; Account, Period are blank; this is valid only if 'Detailed' flag is not set for the Constraint)
7521     --
7522 
7523     l_type1 := FND_API.G_FALSE;
7524     l_type2 := FND_API.G_FALSE;
7525     l_type3 := FND_API.G_FALSE;
7526     l_type4 := FND_API.G_FALSE;
7527     l_type5 := FND_API.G_FALSE;
7528 
7529     if FND_API.to_Boolean(l_first_line) then
7530 
7531       l_first_line := FND_API.G_FALSE;
7532 
7533       -- Prefix Operator for the 1st line of a Constraint Formula should be either of :
7534       -- '<=', '>=', '<', '>', '=', '<>'
7535 
7536       if c_Formula_Rec.prefix_operator not in ('<=', '>=', '<', '>', '=', '<>') then
7537         message_token('CONSTRAINT', p_constraint_name);
7538         message_token('STEPID', c_Formula_Rec.step_number);
7539         message_token('OPERATOR', '[<=, >=, <, >, =, <>]');
7540         add_message('PSB', 'PSB_INVALID_CONS_OPR');
7541         raise FND_API.G_EXC_ERROR;
7542       else
7543         l_operator := c_Formula_Rec.prefix_operator;
7544       end if;
7545 
7546     else
7547 
7548       -- Prefix Operator for the other lines of a Constraint Formula should be either of :
7549       -- '+', '-', '*', '/'
7550 
7551       if c_Formula_Rec.prefix_operator not in ('+', '-', '*', '/') then
7552         message_token('CONSTRAINT', p_constraint_name);
7553         message_token('STEPID', c_Formula_Rec.step_number);
7554         message_token('OPERATOR', '[+, -, *, /]');
7555         add_message('PSB', 'PSB_INVALID_CONS_OPR');
7556         raise FND_API.G_EXC_ERROR;
7557       end if;
7558     end if;
7559 
7560     -- Check Formula Type
7561 
7562     if ((c_Formula_Rec.prefix_operator is not null) and (c_Formula_Rec.postfix_operator is not null) and
7563         (c_Formula_Rec.budget_year_type_id is not null) and (c_Formula_Rec.balance_type is not null) and
7564         (c_Formula_Rec.currency_code is not null) and (c_Formula_Rec.amount is not null) and
7565        ((c_Formula_Rec.segment1 is null) and (c_Formula_Rec.segment2 is null) and (c_Formula_Rec.segment3 is null) and
7566         (c_Formula_Rec.segment4 is null) and (c_Formula_Rec.segment5 is null) and (c_Formula_Rec.segment6 is null) and
7567         (c_Formula_Rec.segment7 is null) and (c_Formula_Rec.segment8 is null) and (c_Formula_Rec.segment9 is null) and
7568         (c_Formula_Rec.segment10 is null) and (c_Formula_Rec.segment11 is null) and (c_Formula_Rec.segment12 is null) and
7569         (c_Formula_Rec.segment13 is null) and (c_Formula_Rec.segment14 is null) and (c_Formula_Rec.segment15 is null) and
7570         (c_Formula_Rec.segment16 is null) and (c_Formula_Rec.segment17 is null) and (c_Formula_Rec.segment18 is null) and
7571         (c_Formula_Rec.segment19 is null) and (c_Formula_Rec.segment20 is null) and (c_Formula_Rec.segment21 is null) and
7572         (c_Formula_Rec.segment22 is null) and (c_Formula_Rec.segment23 is null) and (c_Formula_Rec.segment24 is null) and
7573         (c_Formula_Rec.segment25 is null) and (c_Formula_Rec.segment26 is null) and (c_Formula_Rec.segment27 is null) and
7574         (c_Formula_Rec.segment28 is null) and (c_Formula_Rec.segment29 is null) and (c_Formula_Rec.segment30 is null))) then
7575     begin
7576 
7577       if FND_API.to_Boolean(p_summ_flag) then
7578       begin
7579         message_token('CONSTRAINT', p_constraint_name);
7580         add_message('PSB', 'PSB_INVALID_CONS_FORMULA');
7581         raise FND_API.G_EXC_ERROR;
7582       end;
7583       else
7584         l_type1 := FND_API.G_TRUE;
7585       end if;
7586 
7587     end;
7588     elsif ((c_Formula_Rec.prefix_operator is not null) and (c_Formula_Rec.budget_year_type_id is not null) and
7589            (c_Formula_Rec.balance_type is not null) and (c_Formula_Rec.currency_code is not null) and
7590           ((c_Formula_Rec.segment1 is not null) or (c_Formula_Rec.segment2 is not null) or (c_Formula_Rec.segment3 is not null) or
7591            (c_Formula_Rec.segment4 is not null) or (c_Formula_Rec.segment5 is not null) or (c_Formula_Rec.segment6 is not null) or
7592            (c_Formula_Rec.segment7 is not null) or (c_Formula_Rec.segment8 is not null) or (c_Formula_Rec.segment9 is not null) or
7593            (c_Formula_Rec.segment10 is not null) or (c_Formula_Rec.segment11 is not null) or (c_Formula_Rec.segment12 is not null) or
7594            (c_Formula_Rec.segment13 is not null) or (c_Formula_Rec.segment14 is not null) or (c_Formula_Rec.segment15 is not null) or
7595            (c_Formula_Rec.segment16 is not null) or (c_Formula_Rec.segment17 is not null) or (c_Formula_Rec.segment18 is not null) or
7596            (c_Formula_Rec.segment19 is not null) or (c_Formula_Rec.segment20 is not null) or (c_Formula_Rec.segment21 is not null) or
7597            (c_Formula_Rec.segment22 is not null) or (c_Formula_Rec.segment23 is not null) or (c_Formula_Rec.segment24 is not null) or
7598            (c_Formula_Rec.segment25 is not null) or (c_Formula_Rec.segment26 is not null) or (c_Formula_Rec.segment27 is not null) or
7599            (c_Formula_Rec.segment28 is not null) or (c_Formula_Rec.segment29 is not null) or (c_Formula_Rec.segment30 is not null))) then
7600     begin
7601       l_type2 := FND_API.G_TRUE;
7602     end;
7603     elsif ((c_Formula_Rec.prefix_operator is not null) and
7604            (c_Formula_Rec.amount is not null) and
7605            (c_Formula_Rec.budget_year_type_id is null) and
7606            (c_Formula_Rec.balance_type is null) and
7607            (c_Formula_Rec.currency_code is null) and
7608            (c_Formula_Rec.postfix_operator is null) and
7609           ((c_Formula_Rec.segment1 is null) and (c_Formula_Rec.segment2 is null) and (c_Formula_Rec.segment3 is null) and
7610            (c_Formula_Rec.segment4 is null) and (c_Formula_Rec.segment5 is null) and (c_Formula_Rec.segment6 is null) and
7611            (c_Formula_Rec.segment7 is null) and (c_Formula_Rec.segment8 is null) and (c_Formula_Rec.segment9 is null) and
7612            (c_Formula_Rec.segment10 is null) and (c_Formula_Rec.segment11 is null) and (c_Formula_Rec.segment12 is null) and
7613            (c_Formula_Rec.segment13 is null) and (c_Formula_Rec.segment14 is null) and (c_Formula_Rec.segment15 is null) and
7614            (c_Formula_Rec.segment16 is null) and (c_Formula_Rec.segment17 is null) and (c_Formula_Rec.segment18 is null) and
7615            (c_Formula_Rec.segment19 is null) and (c_Formula_Rec.segment20 is null) and (c_Formula_Rec.segment21 is null) and
7616            (c_Formula_Rec.segment22 is null) and (c_Formula_Rec.segment23 is null) and (c_Formula_Rec.segment24 is null) and
7617            (c_Formula_Rec.segment25 is null) and (c_Formula_Rec.segment26 is null) and (c_Formula_Rec.segment27 is null) and
7618            (c_Formula_Rec.segment28 is null) and (c_Formula_Rec.segment29 is null) and (c_Formula_Rec.segment30 is null))) then
7619     begin
7620       l_type3 := FND_API.G_TRUE;
7621     end;
7622     elsif ((c_Formula_Rec.prefix_operator is not null) and (c_Formula_Rec.postfix_operator is not null) and
7623            (c_Formula_Rec.balance_type in ('O', 'C')) and (c_Formula_Rec.currency_code is not null) and
7624            (c_Formula_Rec.amount is not null) and (c_Formula_Rec.budget_year_type_id is null) and
7625            (c_Formula_Rec.segment1 is null) and (c_Formula_Rec.segment2 is null) and
7626            (c_Formula_Rec.segment3 is null) and (c_Formula_Rec.segment4 is null) and
7627            (c_Formula_Rec.segment5 is null) and (c_Formula_Rec.segment6 is null) and
7628            (c_Formula_Rec.segment7 is null) and (c_Formula_Rec.segment8 is null) and
7629            (c_Formula_Rec.segment9 is null) and (c_Formula_Rec.segment10 is null) and
7630            (c_Formula_Rec.segment11 is null) and (c_Formula_Rec.segment12 is null) and
7631            (c_Formula_Rec.segment13 is null) and (c_Formula_Rec.segment14 is null) and
7632            (c_Formula_Rec.segment15 is null) and (c_Formula_Rec.segment16 is null) and
7633            (c_Formula_Rec.segment17 is null) and (c_Formula_Rec.segment18 is null) and
7634            (c_Formula_Rec.segment19 is null) and (c_Formula_Rec.segment20 is null) and
7635            (c_Formula_Rec.segment21 is null) and (c_Formula_Rec.segment22 is null) and
7636            (c_Formula_Rec.segment23 is null) and (c_Formula_Rec.segment24 is null) and
7637            (c_Formula_Rec.segment25 is null) and (c_Formula_Rec.segment26 is null) and
7638            (c_Formula_Rec.segment27 is null) and (c_Formula_Rec.segment28 is null) and
7639            (c_Formula_Rec.segment29 is null) and (c_Formula_Rec.segment30 is null)) then
7640     begin
7641 
7642       if FND_API.to_Boolean(p_summ_flag) then
7643         l_type5 := FND_API.G_TRUE;
7644       else
7645         l_type4 := FND_API.G_TRUE;
7646       end if;
7647 
7648     end;
7649     else
7650     begin
7651       message_token('CONSTRAINT', p_constraint_name);
7652       add_message('PSB', 'PSB_INVALID_CONS_FORMULA');
7653       raise FND_API.G_EXC_ERROR;
7654     end;
7655     end if;
7656 
7657     if FND_API.to_Boolean(l_type3) then
7658       l_line_total := c_Formula_Rec.amount;
7659     elsif FND_API.to_Boolean(l_type4) then
7660     begin
7661 
7662 
7663       if c_Formula_Rec.balance_type = 'O' then
7664       begin
7665 
7666         for c_Original_Balance_Rec in c_Original_Balance (p_ccid, c_Formula_Rec.currency_code) loop
7667 
7668           if c_Formula_Rec.postfix_operator = '+' then
7669             l_line_total := c_Original_Balance_Rec.original_balance + c_Formula_Rec.amount;
7670           elsif c_Formula_Rec.postfix_operator = '-' then
7671             l_line_total := c_Original_Balance_Rec.original_balance - c_Formula_Rec.amount;
7672           elsif c_Formula_Rec.postfix_operator = '*' then
7673             l_line_total := c_Original_Balance_Rec.original_balance * c_Formula_Rec.amount;
7674           elsif c_Formula_Rec.postfix_operator = '/' then
7675           begin
7676 
7677             -- Avoid divide-by-zero error
7678 
7679             if nvl(c_Formula_Rec.amount, 0) = 0 then
7680               l_line_total := 0;
7681             else
7682               l_line_total := c_Original_Balance_Rec.original_balance / c_Formula_Rec.amount;
7683             end if;
7684 
7685           end;
7686           else
7687           begin
7688             message_token('CONSTRAINT', p_constraint_name);
7689             add_message('PSB', 'PSB_INVALID_CONS_FORMULA');
7690             raise FND_API.G_EXC_ERROR;
7691           end;
7692           end if;
7693 
7694         end loop;
7695 
7696       end;
7697       elsif c_Formula_Rec.balance_type = 'C' then
7698       begin
7699 
7700         l_current_balance := 0;
7701 
7702         for c_Account_Rec in c_Account(p_ccid) loop
7703 
7704           l_current_balance
7705             := l_current_balance + Get_GL_Balance (p_revision_type => 'R',
7706                                                    p_balance_type => g_balance_type,
7707                                                    p_set_of_books_id => g_set_of_books_id,
7708                                                    p_xbc_enabled_flag => g_budgetary_control,
7709                                                    p_gl_period_name => c_account_rec.gl_period_name,
7710                                                    p_gl_budget_version_id => c_Account_Rec.gl_budget_version_id,
7711                                                    p_currency_code => c_Formula_Rec.currency_code,
7712                                                    p_code_combination_id => p_ccid);
7713         end loop;
7714 
7715         if c_Formula_Rec.postfix_operator = '+' then
7716           l_line_total := l_current_balance + c_Formula_Rec.amount;
7717         elsif c_Formula_Rec.postfix_operator = '-' then
7718           l_line_total := l_current_balance - c_Formula_Rec.amount;
7719         elsif c_Formula_Rec.postfix_operator = '*' then
7720           l_line_total := l_current_balance * c_Formula_Rec.amount;
7721         elsif c_Formula_Rec.postfix_operator = '/' then
7722         begin
7723 
7724           -- Avoid divide-by-zero error
7725 
7726           if nvl(c_Formula_Rec.amount, 0) = 0 then
7727             l_line_total := 0;
7728           else
7729             l_line_total := l_current_balance / c_Formula_Rec.amount;
7730           end if;
7731 
7732         end;
7733         else
7734         begin
7735           message_token('CONSTRAINT', p_constraint_name);
7736           add_message('PSB', 'PSB_INVALID_CONS_FORMULA');
7737           raise FND_API.G_EXC_ERROR;
7738         end;
7739         end if;
7740 
7741       end;
7742       end if;
7743 
7744     end;
7745     elsif FND_API.to_Boolean(l_type5) then
7746     begin
7747 
7748       if c_Formula_Rec.balance_type = 'O' then
7749       begin
7750 
7751         for c_Original_Balance_Rec in c_Original_Balance_Sum (c_Formula_Rec.currency_code) loop
7752 
7753           if c_Formula_Rec.postfix_operator = '+' then
7754             l_line_total := c_Original_Balance_Rec.original_balance + c_Formula_Rec.amount;
7755           elsif c_Formula_Rec.postfix_operator = '-' then
7756             l_line_total := c_Original_Balance_Rec.original_balance - c_Formula_Rec.amount;
7757           elsif c_Formula_Rec.postfix_operator = '*' then
7758             l_line_total := c_Original_Balance_Rec.original_balance * c_Formula_Rec.amount;
7759           elsif c_Formula_Rec.postfix_operator = '/' then
7760           begin
7761 
7762             -- Avoid divide-by-zero error
7763 
7764             if nvl(c_Formula_Rec.amount, 0) = 0 then
7765               l_line_total := 0;
7766             else
7767               l_line_total := c_Original_Balance_Rec.original_balance / c_Formula_Rec.amount;
7768             end if;
7769 
7770           end;
7771           else
7772           begin
7773             message_token('CONSTRAINT', p_constraint_name);
7774             add_message('PSB', 'PSB_INVALID_CONS_FORMULA');
7775             raise FND_API.G_EXC_ERROR;
7776           end;
7777           end if;
7778 
7779         end loop;
7780 
7781       end;
7782       elsif c_Formula_Rec.balance_type = 'C' then
7783       begin
7784 
7785         l_current_balance := 0;
7786 
7787         for c_Account_Rec in c_Account_Sum loop
7788 
7789           l_current_balance
7790             := l_current_balance + Get_GL_Balance (p_revision_type => 'R',
7791                                                    p_balance_type => g_balance_type,
7792                                                    p_set_of_books_id => g_set_of_books_id,
7793                                                    p_xbc_enabled_flag => g_budgetary_control,
7794                                                    p_gl_period_name => c_account_rec.gl_period_name,
7795                                                    p_gl_budget_version_id => c_Account_Rec.gl_budget_version_id,
7796                                                    p_currency_code => c_Formula_Rec.currency_code,
7797                                                    p_code_combination_id => C_Account_Rec.code_combination_id);
7798        end loop;
7799 
7800 
7801         if c_Formula_Rec.postfix_operator = '+' then
7802           l_line_total := l_current_balance + c_Formula_Rec.amount;
7803         elsif c_Formula_Rec.postfix_operator = '-' then
7804           l_line_total := l_current_balance - c_Formula_Rec.amount;
7805         elsif c_Formula_Rec.postfix_operator = '*' then
7806           l_line_total := l_current_balance * c_Formula_Rec.amount;
7807         elsif c_Formula_Rec.postfix_operator = '/' then
7808         begin
7809 
7810           -- Avoid divide-by-zero error
7811 
7812           if nvl(c_Formula_Rec.amount, 0) = 0 then
7813             l_line_total := 0;
7814           else
7815             l_line_total := l_current_balance / c_Formula_Rec.amount;
7816           end if;
7817 
7818         end;
7819         else
7820         begin
7821           message_token('CONSTRAINT', p_constraint_name);
7822           add_message('PSB', 'PSB_INVALID_CONS_FORMULA');
7823           raise FND_API.G_EXC_ERROR;
7824         end;
7825         end if;
7826 
7827       end;
7828       end if;
7829 
7830     end;
7831     end if;
7832 
7833     if c_Formula_Rec.prefix_operator in ('=', '<>', '<=', '>=', '<', '>') then
7834       l_cons_total := l_line_total;
7835     elsif c_Formula_Rec.prefix_operator = '+' then
7836       l_cons_total := l_cons_total + l_line_total;
7837     elsif c_Formula_Rec.prefix_operator = '-' then
7838       l_cons_total := l_cons_total - l_line_total;
7839     elsif c_Formula_Rec.prefix_operator = '*' then
7840       l_cons_total := l_cons_total * l_line_total;
7841     elsif c_Formula_Rec.prefix_operator = '/' then
7842     begin
7843 
7844       -- Avoid divide-by-zero error
7845 
7846       if nvl(l_line_total, 0) = 0 then
7847         l_cons_total := 0;
7848       else
7849         l_cons_total := l_cons_total / l_line_total;
7850       end if;
7851 
7852     end;
7853     end if;
7854 
7855   end loop;
7856 
7857   -- Compute Sum of Account Sets or CCID assigned to the Constraint
7858 
7859   if not FND_API.to_Boolean(p_summ_flag) then
7860   begin
7861 
7862     for c_Sum_Rec in c_Sum (p_ccid) loop
7863       l_accset_total := c_Sum_Rec.Sum_Acc;
7864     end loop;
7865 
7866   end;
7867   else
7868   begin
7869 
7870     for c_SumAll_Rec in c_SumAll loop
7871       l_accset_total := c_SumAll_Rec.Sum_Acc;
7872     end loop;
7873 
7874   end;
7875   end if;
7876 
7877   if l_accset_total is not null then
7878   begin
7879 
7880     if l_operator = '<=' then
7881 
7882       if l_accset_total <= l_cons_total then
7883         l_cons_failed := FND_API.G_TRUE;
7884       end if;
7885 
7886     elsif l_operator = '>=' then
7887 
7888       if l_accset_total >= l_cons_total then
7889         l_cons_failed := FND_API.G_TRUE;
7890       end if;
7891 
7892     elsif l_operator = '<' then
7893 
7894       if l_accset_total < l_cons_total then
7895         l_cons_failed := FND_API.G_TRUE;
7896       end if;
7897 
7898     elsif l_operator = '>' then
7899 
7900       if l_accset_total > l_cons_total then
7901         l_cons_failed := FND_API.G_TRUE;
7902       end if;
7903 
7904     elsif l_operator = '=' then
7905 
7906       if l_accset_total = l_cons_total then
7907         l_cons_failed := FND_API.G_TRUE;
7908       end if;
7909 
7910     elsif l_operator = '<>' then
7911 
7912       if l_accset_total <> l_cons_total then
7913         l_cons_failed := FND_API.G_TRUE;
7914       end if;
7915 
7916     end if;
7917 
7918     if FND_API.to_Boolean(l_cons_failed) then
7919     begin
7920 
7921       if nvl(p_severity_level, -1) >= g_constraint_threshold then
7922         p_constraint_validation_status := 'F';
7923       else
7924         p_constraint_validation_status := 'E';
7925       end if;
7926 /* For Bug No : 1321519 Start */
7927       message_token('EFFECTIVE_START_DATE', nvl(g_constraint_start_date,g_from_date));
7928       message_token('EFFECTIVE_END_DATE', nvl(g_constraint_end_date,g_to_date));
7929       message_token('FIRST_GL_PERIOD_NAME', g_from_date);
7930       message_token('LAST_GL_PERIOD_NAME', g_to_date);
7931 /* For Bug No : 1321519 End */
7932       message_token('CONSTRAINT_SET', g_constraint_set_name);
7933       message_token('THRESHOLD', g_constraint_threshold);
7934       message_token('CONSTRAINT', p_constraint_name);
7935       message_token('SEVERITY_LEVEL', p_severity_level);
7936       message_token('ASSIGNMENT_VALUE', l_accset_total);
7937       message_token('OPERATOR', l_operator);
7938       message_token('FORMULA_VALUE', l_cons_total);
7939 
7940 
7941       if FND_API.to_Boolean(p_summ_flag) then
7942         message_token('NAME', p_constraint_name);
7943       else
7944       begin
7945 
7946         l_concat_segments := FND_FLEX_EXT.Get_Segs
7947                                 (application_short_name => 'SQLGL',
7948                                  key_flex_code => 'GL#',
7949                                  structure_number => g_flex_code,
7950                                  combination_id => p_ccid);
7951 
7952         message_token('NAME', l_concat_segments);
7953 
7954       end;
7955       end if;
7956 
7957       add_message('PSB', 'PSB_REV_CONSTRAINT_FAILURE');
7958 
7959       l_description := FND_MSG_PUB.Get
7960                           (p_encoded => FND_API.G_FALSE);
7961       FND_MSG_PUB.Delete_Msg;
7962 
7963       -- Constraint Validation failures are logged in PSB_ERROR_MESSAGES and
7964       -- viewed using a Form
7965 
7966       insert into PSB_ERROR_MESSAGES
7967                  (Concurrent_Request_ID,
7968                   Process_ID,
7969                   Source_Process,
7970                   Description,
7971                   Creation_Date,
7972                   Created_By)
7973           values (FND_GLOBAL.CONC_REQUEST_ID,
7974                   p_budget_revision_id,
7975                   'BUDGET_REVISION',
7976                   l_description,
7977                   sysdate,
7978                   FND_GLOBAL.USER_ID);
7979 
7980     end;
7981     else
7982       p_constraint_validation_status := 'S';
7983     end if;
7984 
7985   end;
7986   end if;
7987 
7988   -- Initialize API return status to success
7989 
7990   p_return_status := FND_API.G_RET_STS_SUCCESS;
7991 
7992 
7993 EXCEPTION
7994 
7995    when FND_API.G_EXC_ERROR then
7996      p_return_status := FND_API.G_RET_STS_ERROR;
7997 
7998    when FND_API.G_EXC_UNEXPECTED_ERROR then
7999      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8000 
8001    when OTHERS then
8002      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8003 
8004 END Process_Constraint;
8005 
8006 /* ----------------------------------------------------------------------- */
8007 
8008 PROCEDURE Apply_Detailed_Account
8009 ( p_return_status                 OUT  NOCOPY  VARCHAR2,
8010   p_constraint_validation_status  OUT  NOCOPY  VARCHAR2,
8011   p_budget_revision_id            IN   NUMBER,
8012   p_constraint_id                 IN   NUMBER,
8013   p_constraint_name               IN   VARCHAR2,
8014   p_currency_code                 IN   VARCHAR2,
8015   p_severity_level                IN   NUMBER
8016 ) IS
8017 
8018   l_return_status                 VARCHAR2(1);
8019 
8020   l_cons_validation_status        VARCHAR2(1) := 'S';
8021   l_detailed_status               VARCHAR2(1);
8022 
8023   -- CCIDs assigned to the Constraint : select CCIDs that also belong to the Budget Group Hierarchy
8024 
8025   cursor c_CCID is
8026     select a.code_combination_id ccid
8027       from PSB_BUDGET_ACCOUNTS a,
8028            PSB_SET_RELATIONS_V b
8029      where exists
8030           (select 1
8031              from PSB_BUDGET_ACCOUNTS c,
8032                   PSB_SET_RELATIONS_V d
8033             where c.account_position_set_id = d.account_position_set_id
8034               and c.code_combination_id = a.code_combination_id
8035               and d.account_or_position_type = 'A'
8036               and exists
8037                  (select 1
8038                     from psb_budget_groups e
8039                    where e.budget_group_type = 'R'
8040                      and e.budget_group_id = d.budget_group_id
8041                    start with e.budget_group_id = g_budget_group_id
8042                  connect by prior e.budget_group_id = e.parent_budget_group_id))
8043        and a.account_position_set_id = b.account_position_set_id
8044        and b.account_or_position_type = 'A'
8045        and b.constraint_id = p_constraint_id;
8046 
8047 BEGIN
8048 
8049   for c_CCID_Rec in c_CCID loop
8050 
8051     Process_Constraint
8052            (p_budget_revision_id => p_budget_revision_id,
8053             p_constraint_id => p_constraint_id,
8054             p_constraint_name => p_constraint_name,
8055             p_ccid => c_CCID_Rec.ccid,
8056             p_currency_code => p_currency_code,
8057             p_severity_level => p_severity_level,
8058             p_summ_flag => FND_API.G_FALSE,
8059             p_constraint_validation_status => l_detailed_status,
8060             p_return_status => l_return_status);
8061 
8062     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
8063       raise FND_API.G_EXC_ERROR;
8064     end if;
8065 
8066     if ((l_cons_validation_status = 'S') and
8067         (l_detailed_status <> 'S')) then
8068       l_cons_validation_status := l_detailed_status;
8069     elsif ((l_cons_validation_status = 'E') and
8070            (l_detailed_status = 'F')) then
8071       l_cons_validation_status := l_detailed_status;
8072     end if;
8073 
8074   end loop;
8075 
8076   -- Initialize API return status to success
8077 
8078   p_constraint_validation_status := l_cons_validation_status;
8079   p_return_status := FND_API.G_RET_STS_SUCCESS;
8080 
8081 EXCEPTION
8082 
8083    when FND_API.G_EXC_ERROR then
8084      p_return_status := FND_API.G_RET_STS_ERROR;
8085 
8086    when FND_API.G_EXC_UNEXPECTED_ERROR then
8087      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8088 
8089    when OTHERS then
8090      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8091 
8092 END Apply_Detailed_Account;
8093 
8094 /* ----------------------------------------------------------------------- */
8095 
8096 PROCEDURE Apply_Account_Constraints
8097 ( p_return_status      OUT  NOCOPY  VARCHAR2,
8098   p_validation_status  OUT  NOCOPY  VARCHAR2,
8099   p_budget_revision_id IN   NUMBER
8100 ) IS
8101 
8102   l_return_status              VARCHAR2(1);
8103 
8104   l_cons_validation_status     VARCHAR2(1);
8105   l_consset_validation_status  VARCHAR2(1) := 'S';
8106 
8107   cursor c_Constraint is
8108     select constraint_id,
8109            name,
8110            currency_code,
8111            severity_level,
8112            effective_start_date,
8113            effective_end_date,
8114            constraint_detailed_flag
8115       from PSB_CONSTRAINT_ASSIGNMENTS_V
8116      where constraint_type = 'ACCOUNT'
8117        and constraint_set_id = g_constraint_set_id
8118        and currency_code     = g_currency_code -- Bug 3029168
8119      order by severity_level desc;
8120 
8121 BEGIN
8122 
8123   for c_Constraint_Rec in c_Constraint loop
8124   /* ForBug No : 1321519 Start */
8125     g_constraint_start_date := c_Constraint_Rec.effective_start_date;
8126     g_constraint_end_date   := c_Constraint_Rec.effective_end_date;
8127   /* ForBug No : 1321519 End */
8128     if ((c_Constraint_Rec.constraint_detailed_flag is null) or
8129         (c_Constraint_Rec.constraint_detailed_flag = 'N')) then
8130     begin
8131 
8132       Process_Constraint
8133              (p_budget_revision_id => p_budget_revision_id,
8134               p_constraint_id => c_Constraint_Rec.constraint_id,
8135               p_constraint_name => c_Constraint_Rec.name,
8136               p_currency_code => nvl(c_Constraint_Rec.currency_code, g_func_currency),
8137               p_severity_level => c_Constraint_Rec.severity_level,
8138               p_summ_flag => FND_API.G_TRUE,
8139               p_constraint_validation_status => l_cons_validation_status,
8140               p_return_status => l_return_status);
8141 
8142       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
8143          raise FND_API.G_EXC_ERROR;
8144       end if;
8145 
8146       -- Assign a proper validation status for the Constraint Set based on the validation
8147       -- status for the individual Constraints
8148 
8149       if ((l_consset_validation_status = 'S') and
8150           (l_cons_validation_status <> 'S')) then
8151         l_consset_validation_status := l_cons_validation_status;
8152       elsif ((l_consset_validation_status = 'E') and
8153              (l_cons_validation_status = 'F')) then
8154         l_consset_validation_status := l_cons_validation_status;
8155       elsif ((l_consset_validation_status = 'W') and
8156              (l_cons_validation_status in ('F', 'E'))) then
8157         l_consset_validation_status := l_cons_validation_status;
8158       end if;
8159 
8160     end;
8161     else
8162     begin
8163 
8164       -- For a Constraint with the detailed flag set, call this procedure which
8165       -- processes constraints for individual CCIDs. This is to avoid static
8166       -- binding
8167 
8168       Apply_Detailed_Account
8169            (p_return_status => l_return_status,
8170             p_constraint_validation_status => l_cons_validation_status,
8171             p_budget_revision_id => p_budget_revision_id,
8172             p_constraint_id => c_Constraint_Rec.constraint_id,
8173             p_constraint_name => c_Constraint_Rec.name,
8174             p_currency_code => nvl(c_Constraint_Rec.currency_code, g_func_currency),
8175             p_severity_level => c_Constraint_Rec.severity_level);
8176 
8177       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
8178          raise FND_API.G_EXC_ERROR;
8179       end if;
8180 
8181       -- Assign a proper validation status for the Constraint Set based on the validation
8182       -- status for the individual Constraints
8183 
8184       if ((l_consset_validation_status = 'S') and
8185           (l_cons_validation_status <> 'S')) then
8186         l_consset_validation_status := l_cons_validation_status;
8187       elsif ((l_consset_validation_status = 'E') and
8188              (l_cons_validation_status = 'F')) then
8189         l_consset_validation_status := l_cons_validation_status;
8190       elsif ((l_consset_validation_status = 'W') and
8191              (l_cons_validation_status in ('F', 'E'))) then
8192         l_consset_validation_status := l_cons_validation_status;
8193       end if;
8194 
8195     end;
8196     end if;
8197 
8198   end loop;
8199 
8200   -- Initialize API return status to success
8201 
8202   p_validation_status := l_consset_validation_status;
8203   p_return_status := FND_API.G_RET_STS_SUCCESS;
8204 
8205 
8206 EXCEPTION
8207 
8208    when FND_API.G_EXC_ERROR then
8209      p_return_status := FND_API.G_RET_STS_ERROR;
8210 
8211    when FND_API.G_EXC_UNEXPECTED_ERROR then
8212      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8213 
8214    when OTHERS then
8215      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8216 
8217 END Apply_Account_Constraints;
8218 
8219 /* ----------------------------------------------------------------------- */
8220 
8221 PROCEDURE Apply_Element_Constraints
8222 ( p_return_status       OUT  NOCOPY  VARCHAR2,
8223   p_budget_revision_id  IN   NUMBER
8224 ) IS
8225 
8226   l_return_status       VARCHAR2(1);
8227 
8228   cursor c_Constraint is
8229     select constraint_id,
8230            name,
8231            currency_code,
8232            severity_level,
8233            effective_start_date,
8234            effective_end_date
8235       from PSB_CONSTRAINT_ASSIGNMENTS_V
8236      where constraint_type = 'ELEMENT'
8237        and (((effective_start_date <= g_effective_end_date)
8238          and (effective_end_date is null))
8239          or ((effective_start_date between g_effective_start_date and g_effective_end_date)
8240           or (effective_end_date between g_effective_start_date and g_effective_end_date)
8241          or ((effective_start_date < g_effective_start_date)
8242          and (effective_end_date > g_effective_end_date))))
8243        and constraint_set_id = g_constraint_set_id;
8244 
8245 BEGIN
8246 
8247   for c_Constraint_Rec in c_Constraint loop
8248 
8249     PSB_WS_POS3.Process_ElemCons_Detailed
8250        (p_return_status => l_return_status,
8251         p_worksheet_id => g_global_budget_revision_id,
8252         p_data_extract_id => g_data_extract_id,
8253         p_constraint_id => c_Constraint_Rec.constraint_id,
8254         p_start_date => c_Constraint_Rec.effective_start_date,
8255         p_end_date => nvl(c_Constraint_Rec.effective_end_date, g_effective_end_date));
8256 
8257     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
8258       raise FND_API.G_EXC_ERROR;
8259     end if;
8260 
8261   end loop;
8262 
8263   -- Initialize API return status to success
8264 
8265   p_return_status := FND_API.G_RET_STS_SUCCESS;
8266 
8267 EXCEPTION
8268 
8269    when FND_API.G_EXC_ERROR then
8270      p_return_status := FND_API.G_RET_STS_ERROR;
8271 
8272    when FND_API.G_EXC_UNEXPECTED_ERROR then
8273      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8274 
8275    when OTHERS then
8276      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8277 
8278 END Apply_Element_Constraints;
8279 
8280 /* ----------------------------------------------------------------------- */
8281 
8282 PROCEDURE Process_PosCons_Step
8283 ( p_return_status                 OUT  NOCOPY  VARCHAR2,
8284   p_constraint_validation_status  OUT  NOCOPY  VARCHAR2,
8285   p_budget_revision_id            IN   NUMBER,
8286   p_constraint_id                 IN   NUMBER,
8287   p_constraint_name               IN   VARCHAR2,
8288   p_position_id                   IN   NUMBER := FND_API.G_MISS_NUM,
8289   p_position_name                 IN   VARCHAR2 := FND_API.G_MISS_CHAR,
8290   p_currency_code                 IN   VARCHAR2,
8291   p_severity_level                IN   NUMBER,
8292   p_summ_flag                     IN   VARCHAR2,
8293   p_pay_element_id                IN   NUMBER,
8294   p_pay_element_option_id         IN   NUMBER,
8295   p_prefix_operator               IN   VARCHAR2,
8296   p_element_value_type            IN   VARCHAR2,
8297   p_element_value                 IN   NUMBER
8298 ) IS
8299 
8300   l_cons_failed                   VARCHAR2(1) := FND_API.G_FALSE;
8301 
8302   l_salary_total                  NUMBER := 0;
8303   l_posset_total                  NUMBER := 0;
8304   l_cons_total                    NUMBER := 0;
8305 
8306   l_description                   VARCHAR2(2000);
8307 
8308   l_grade_name                    VARCHAR2(80);
8309   l_grade_step                    NUMBER;
8310 
8311   cursor c_Grade is
8312     select name grade_name,
8313            grade_step
8314       from PSB_PAY_ELEMENT_OPTIONS
8315      where pay_element_option_id = p_pay_element_option_id;
8316 
8317   cursor c_SalaryNeqAll is
8318     select a.name position_name,
8319            b.name,
8320            b.grade_step
8321       from PSB_POSITIONS a,
8322            PSB_PAY_ELEMENT_OPTIONS b,
8323            PSB_POSITION_ASSIGNMENTS c
8324      where exists
8325           (select 1
8326              from PSB_BUDGET_POSITIONS d,
8327                   PSB_SET_RELATIONS e
8328             where d.data_extract_id = g_data_extract_id
8329               and d.position_id = c.position_id
8330               and d.account_position_set_id = e.account_position_set_id
8331               and e.constraint_id = p_constraint_id)
8332        and a.position_id = c.position_id
8333        and b.pay_element_option_id = c.pay_element_option_id
8334        and c.pay_element_option_id <> p_pay_element_option_id
8335        and ((c.worksheet_id is null) or (c.worksheet_id = p_budget_revision_id))
8336        and c.pay_element_id = p_pay_element_id;
8337 
8338   cursor c_SalaryNeq is
8339     select a.name,
8340            a.grade_step
8341       from PSB_PAY_ELEMENT_OPTIONS a,
8342            PSB_POSITION_ASSIGNMENTS b
8343      where a.pay_element_option_id = b.pay_element_option_id
8344        and b.pay_element_option_id <> p_pay_element_option_id
8345        and b.pay_element_id = p_pay_element_id
8346        and b.position_id = p_position_id;
8347 
8348   cursor c_SumAll is
8349     select sum(nvl(a.element_cost, 0)) Sum_Elem
8350       from PSB_POSITION_COSTS a
8351      where exists
8352           (select 1
8353              from PSB_BUDGET_REVISION_POSITIONS c,
8354                   PSB_BUDGET_REVISION_POS_LINES d,
8355                   PSB_BUDGET_POSITIONS e,
8356                   PSB_SET_RELATIONS f
8357             where d.budget_revision_id = a.budget_revision_id
8358               and c.budget_revision_pos_line_id = d.budget_revision_pos_line_id
8359               and d.budget_revision_id = p_budget_revision_id
8360               and c.position_id = e.position_id
8361               and e.data_extract_id = g_data_extract_id
8362               and e.account_position_set_id = f.account_position_set_id
8363               and f.constraint_id = p_constraint_id)
8364        and a.currency_code = p_currency_code
8365        and a.pay_element_id = p_pay_element_id
8366        and a.budget_revision_id = p_budget_revision_id;
8367 
8368   cursor c_SumAll_Salary is
8369     select sum(nvl(a.element_cost, 0)) Sum_Elem
8370       from PSB_POSITION_COSTS a,
8371            PSB_PAY_ELEMENTS c
8372      where exists
8373           (select 1
8374              from PSB_BUDGET_REVISION_POSITIONS d,
8375                   PSB_BUDGET_REVISION_POS_LINES e,
8376                   PSB_BUDGET_POSITIONS f,
8377                   PSB_SET_RELATIONS g
8378             where e.budget_revision_id = a.budget_revision_id
8379               and d.budget_revision_pos_line_id = e.budget_revision_pos_line_id
8380               and e.budget_revision_id = p_budget_revision_id
8381               and d.position_id = f.position_id
8382               and f.data_extract_id = g_data_extract_id
8383               and f.account_position_set_id = g.account_position_set_id
8384               and g.constraint_id = p_constraint_id)
8385        and a.currency_code = p_currency_code
8386        and a.pay_element_id = c.pay_element_id
8387        and a.budget_revision_id = p_budget_revision_id
8388        and c.processing_type = 'R'
8389        and c.salary_flag = 'Y'
8390        and c.business_group_id = g_business_group_id
8391        and c.data_extract_id = g_data_extract_id;
8392 
8393   cursor c_Sum is
8394     select sum(nvl(a.element_cost, 0)) Sum_Elem
8395       from PSB_POSITION_COSTS a
8396      where a.currency_code = p_currency_code
8397        and a.pay_element_id = p_pay_element_id
8398        and a.position_id = p_position_id
8399        and a.budget_revision_id = p_budget_revision_id;
8400 
8401   cursor c_Sum_Salary is
8402     select sum(nvl(a.element_cost, 0)) Sum_Elem
8403       from PSB_POSITION_COSTS a,
8404            PSB_PAY_ELEMENTS c
8405      where a.currency_code = p_currency_code
8406        and a.pay_element_id = c.pay_element_id
8407        and a.position_id = p_position_id
8408        and a.budget_revision_id = p_budget_revision_id
8409        and c.processing_type = 'R'
8410        and c.salary_flag = 'Y'
8411        and c.business_group_id = g_business_group_id
8412        and c.data_extract_id = g_data_extract_id;
8413 
8414 
8415 BEGIN
8416 
8417   if not FND_API.to_Boolean(p_summ_flag) then
8418   begin
8419 
8420     if p_pay_element_option_id is null then
8421     begin
8422 
8423       for c_Sum_Rec in c_Sum loop
8424         l_posset_total := c_Sum_Rec.Sum_Elem;
8425       end loop;
8426 
8427       if p_element_value_type = 'PS' then
8428       begin
8429 
8430         for c_Sum_Salary_Rec in c_Sum_Salary loop
8431           l_salary_total := c_Sum_Salary_Rec.Sum_Elem;
8432         end loop;
8433 
8434       end;
8435       end if;
8436 
8437     end;
8438     end if;
8439 
8440     if p_pay_element_option_id is not null then
8441     begin
8442 
8443       for c_Grade_Rec in c_Grade loop
8444         l_grade_name := c_Grade_Rec.grade_name;
8445         l_grade_step := c_Grade_Rec.grade_step;
8446       end loop;
8447 
8448       if p_prefix_operator = '<>' then
8449       begin
8450 
8451         for c_SalaryNeq_Rec in c_SalaryNeq loop
8452 /* For Bug No : 1321519 Start */
8453           message_token('EFFECTIVE_START_DATE', nvl(g_constraint_start_date,g_from_date));
8454           message_token('EFFECTIVE_END_DATE', nvl(g_constraint_end_date,g_to_date));
8455           message_token('FIRST_GL_PERIOD_NAME', g_from_date);
8456           message_token('LAST_GL_PERIOD_NAME', g_to_date);
8457 /* For Bug No : 1321519 End */
8458           message_token('CONSTRAINT_SET', g_constraint_set_name);
8459           message_token('THRESHOLD', g_constraint_threshold);
8460           message_token('CONSTRAINT', p_constraint_name);
8461           message_token('SEVERITY_LEVEL', p_severity_level);
8462           message_token('ASSIGNMENT_VALUE', c_SalaryNeq_Rec.name || ' ' || c_SalaryNeq_Rec.grade_step);
8463           message_token('OPERATOR', p_prefix_operator);
8464           message_token('FORMULA_VALUE', l_grade_name || ' ' || l_grade_step);
8465           message_token('NAME', p_position_name);
8466           add_message('PSB', 'PSB_REV_CONSTRAINT_FAILURE');
8467 
8468           l_description := FND_MSG_PUB.Get
8469                               (p_encoded => FND_API.G_FALSE);
8470           FND_MSG_PUB.Delete_Msg;
8471 
8472           insert into PSB_ERROR_MESSAGES
8473                      (Concurrent_Request_ID,
8474                       Process_ID,
8475                       Source_Process,
8476                       Description,
8477                       Creation_Date,
8478                       Created_By)
8479               values (FND_GLOBAL.CONC_REQUEST_ID,
8480                       p_budget_revision_id,
8481                       'BUDGET_REVISION',
8482                       l_description,
8483                       sysdate,
8484                       FND_GLOBAL.USER_ID);
8485 
8486           if nvl(p_severity_level, -1) >= g_constraint_threshold then
8487             p_constraint_validation_status := 'F';
8488           else
8489             p_constraint_validation_status := 'E';
8490           end if;
8491 
8492         end loop;
8493 
8494       end;
8495       end if;
8496 
8497     end;
8498     end if;
8499 
8500   end;
8501   else
8502   begin
8503 
8504     if p_pay_element_option_id is null then
8505     begin
8506 
8507       for c_SumAll_Rec in c_SumAll loop
8508         l_posset_total := c_SumAll_Rec.Sum_Elem;
8509       end loop;
8510 
8511       if p_element_value_type = 'PS' then
8512       begin
8513 
8514         for c_SumAll_Salary_Rec in c_SumAll_Salary loop
8515           l_salary_total := c_SumAll_Salary_Rec.Sum_Elem;
8516         end loop;
8517 
8518       end;
8519       end if;
8520 
8521     end;
8522     end if;
8523 
8524     if p_pay_element_option_id is not null then
8525     begin
8526 
8527       for c_Grade_Rec in c_Grade loop
8528         l_grade_name := c_Grade_Rec.grade_name;
8529         l_grade_step := c_Grade_Rec.grade_step;
8530       end loop;
8531 
8532       if p_prefix_operator = '<>' then
8533       begin
8534 
8535         for c_SalaryNeqAll_Rec in c_SalaryNeqAll loop
8536 /* For Bug No : 1321519 Start */
8537           message_token('EFFECTIVE_START_DATE', nvl(g_constraint_start_date,g_from_date));
8538           message_token('EFFECTIVE_END_DATE', nvl(g_constraint_end_date,g_to_date));
8539           message_token('FIRST_GL_PERIOD_NAME', g_from_date);
8540           message_token('LAST_GL_PERIOD_NAME', g_to_date);
8541 /* For Bug No : 1321519 End */
8542           message_token('CONSTRAINT_SET', g_constraint_set_name);
8543           message_token('THRESHOLD', g_constraint_threshold);
8544           message_token('CONSTRAINT', p_constraint_name);
8545           message_token('SEVERITY_LEVEL', p_severity_level);
8546           message_token('ASSIGNMENT_VALUE', c_SalaryNeqAll_Rec.name || ' ' || c_SalaryNeqAll_Rec.grade_step);
8547           message_token('OPERATOR', p_prefix_operator);
8548           message_token('FORMULA_VALUE', l_grade_name || ' ' || l_grade_step);
8549           message_token('NAME', c_SalaryNeqAll_Rec.position_name);
8550           add_message('PSB', 'PSB_REV_CONSTRAINT_FAILURE');
8551 
8552           l_description := FND_MSG_PUB.Get
8553                               (p_encoded => FND_API.G_FALSE);
8554           FND_MSG_PUB.Delete_Msg;
8555 
8556           insert into PSB_ERROR_MESSAGES
8557                      (Concurrent_Request_ID,
8558                       Process_ID,
8559                       Source_Process,
8560                       Description,
8561                       Creation_Date,
8562                       Created_By)
8563               values (FND_GLOBAL.CONC_REQUEST_ID,
8564                       p_budget_revision_id,
8565                       'BUDGET_REVISION',
8566                       l_description,
8567                       sysdate,
8568                       FND_GLOBAL.USER_ID);
8569 
8570           if nvl(p_severity_level, -1) >= g_constraint_threshold then
8571             p_constraint_validation_status := 'F';
8572           else
8573             p_constraint_validation_status := 'E';
8574           end if;
8575 
8576         end loop;
8577 
8578       end;
8579       end if;
8580 
8581     end;
8582     end if;
8583 
8584   end;
8585   end if;
8586 
8587   if p_element_value_type = 'PS' then
8588   begin
8589 
8590     if p_element_value < 1 then
8591       l_cons_total := p_element_value * l_salary_total;
8592     else
8593       l_cons_total := p_element_value * l_salary_total / 100;
8594     end if;
8595 
8596   end;
8597   elsif p_element_value_type = 'A' then
8598     l_cons_total := p_element_value;
8599   end if;
8600 
8601   if l_posset_total is not null then
8602   begin
8603 
8604     if p_prefix_operator = '<=' then
8605 
8606       if l_posset_total <= l_cons_total then
8607         l_cons_failed := FND_API.G_TRUE;
8608       end if;
8609 
8610     elsif p_prefix_operator = '>=' then
8611 
8612       if l_posset_total >= l_cons_total then
8613         l_cons_failed := FND_API.G_TRUE;
8614       end if;
8615 
8616     elsif p_prefix_operator = '<' then
8617 
8618       if l_posset_total < l_cons_total then
8619         l_cons_failed := FND_API.G_TRUE;
8620       end if;
8621 
8622     elsif p_prefix_operator = '>' then
8623 
8624       if l_posset_total > l_cons_total then
8625         l_cons_failed := FND_API.G_TRUE;
8626       end if;
8627 
8628     elsif p_prefix_operator = '=' then
8629 
8630       if l_posset_total = l_cons_total then
8631         l_cons_failed := FND_API.G_TRUE;
8632       end if;
8633 
8634     elsif p_prefix_operator = '<>' then
8635 
8636       if l_posset_total <> l_cons_total then
8637         l_cons_failed := FND_API.G_TRUE;
8638       end if;
8639 
8640     end if;
8641 
8642   end;
8643   end if;
8644 
8645   if FND_API.to_Boolean(l_cons_failed) then
8646   begin
8647 
8648     if nvl(p_severity_level, -1) >= g_constraint_threshold then
8649       p_constraint_validation_status := 'F';
8650     else
8651       p_constraint_validation_status := 'E';
8652     end if;
8653 /* For Bug No : 1321519 Start */
8654     message_token('EFFECTIVE_START_DATE', nvl(g_constraint_start_date,g_from_date));
8655     message_token('EFFECTIVE_END_DATE', nvl(g_constraint_end_date,g_to_date));
8656     message_token('FIRST_GL_PERIOD_NAME', g_from_date);
8657     message_token('LAST_GL_PERIOD_NAME', g_to_date);
8658 /* For Bug No : 1321519 End */
8659     message_token('CONSTRAINT_SET', g_constraint_set_name);
8660     message_token('THRESHOLD', g_constraint_threshold);
8661     message_token('CONSTRAINT', p_constraint_name);
8662     message_token('SEVERITY_LEVEL', p_severity_level);
8663     message_token('ASSIGNMENT_VALUE', l_posset_total);
8664     message_token('OPERATOR', p_prefix_operator);
8665     message_token('FORMULA_VALUE', l_cons_total);
8666 
8667     if FND_API.to_Boolean(p_summ_flag) then
8668       message_token('NAME', p_constraint_name);
8669     else
8670       message_token('NAME', p_position_name);
8671     end if;
8672 
8673     add_message('PSB', 'PSB_REV_CONSTRAINT_FAILURE');
8674 
8675     l_description := FND_MSG_PUB.Get
8676                         (p_encoded => FND_API.G_FALSE);
8677     FND_MSG_PUB.Delete_Msg;
8678 
8679     insert into PSB_ERROR_MESSAGES
8680                (Concurrent_Request_ID,
8681                 Process_ID,
8682                 Source_Process,
8683                 Description,
8684                 Creation_Date,
8685                 Created_By)
8686         values (FND_GLOBAL.CONC_REQUEST_ID,
8687                 p_budget_revision_id,
8688                 'BUDGET_REVISION',
8689                 l_description,
8690                 sysdate,
8691                 FND_GLOBAL.USER_ID);
8692 
8693   end;
8694   else
8695     p_constraint_validation_status := 'S';
8696   end if;
8697 
8698 
8699   -- Initialize API return status to success
8700 
8701   p_return_status := FND_API.G_RET_STS_SUCCESS;
8702 
8703 
8704 EXCEPTION
8705 
8706    when FND_API.G_EXC_ERROR then
8707      p_return_status := FND_API.G_RET_STS_ERROR;
8708 
8709    when FND_API.G_EXC_UNEXPECTED_ERROR then
8710      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8711 
8712    when OTHERS then
8713      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8714 
8715 END Process_PosCons_Step;
8716 
8717 /* ----------------------------------------------------------------------- */
8718 
8719 PROCEDURE Process_PosCons
8720 ( p_return_status                 OUT  NOCOPY  VARCHAR2,
8721   p_constraint_validation_status  OUT  NOCOPY  VARCHAR2,
8722   p_budget_revision_id            IN   NUMBER,
8723   p_constraint_id                 IN   NUMBER,
8724   p_constraint_name               IN   VARCHAR2,
8725   p_position_id                   IN   NUMBER := FND_API.G_MISS_NUM,
8726   p_position_name                 IN   VARCHAR2 := FND_API.G_MISS_CHAR,
8727   p_currency_code                 IN   VARCHAR2,
8728   p_severity_level                IN   NUMBER,
8729   p_summ_flag                     IN   VARCHAR2
8730 ) IS
8731 
8732   l_cons_validation_status        VARCHAR2(1) := 'S';
8733   l_detailed_status               VARCHAR2(1);
8734 
8735   l_return_status                 VARCHAR2(1);
8736 
8737   cursor c_Formula is
8738     select pay_element_id,
8739            pay_element_option_id,
8740            prefix_operator,
8741            nvl(currency_code, p_currency_code) currency_code,
8742            element_value_type,
8743            element_value
8744       from PSB_CONSTRAINT_FORMULAS
8745      where constraint_id = p_constraint_id
8746      order by step_number;
8747 
8748 BEGIN
8749 
8750   for c_Formula_Rec in c_Formula loop
8751 
8752     Process_PosCons_Step
8753            (p_return_status => l_return_status,
8754             p_constraint_validation_status => l_detailed_status,
8755             p_budget_revision_id => p_budget_revision_id,
8756             p_constraint_id => p_constraint_id,
8757             p_constraint_name => p_constraint_name,
8758             p_position_id => p_position_id,
8759             p_position_name => p_position_name,
8760             p_currency_code => c_Formula_Rec.currency_code,
8761             p_severity_level => p_severity_level,
8762             p_summ_flag => p_summ_flag,
8763             p_pay_element_id => c_Formula_Rec.pay_element_id,
8764             p_pay_element_option_id => c_Formula_Rec.pay_element_option_id,
8765             p_prefix_operator => c_Formula_Rec.prefix_operator,
8766             p_element_value_type => c_Formula_Rec.element_value_type,
8767             p_element_value => c_Formula_Rec.element_value);
8768 
8769     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
8770       raise FND_API.G_EXC_ERROR;
8771     end if;
8772 
8773     if ((l_cons_validation_status = 'S') and
8774         (l_detailed_status <> 'S')) then
8775       l_cons_validation_status := l_detailed_status;
8776     elsif ((l_cons_validation_status = 'E') and
8777            (l_detailed_status = 'F')) then
8778       l_cons_validation_status := l_detailed_status;
8779     end if;
8780 
8781   end loop;
8782 
8783   -- Initialize API return status to success
8784 
8785   p_constraint_validation_status := l_cons_validation_status;
8786   p_return_status := FND_API.G_RET_STS_SUCCESS;
8787 
8788 EXCEPTION
8789 
8790    when FND_API.G_EXC_ERROR then
8791      p_return_status := FND_API.G_RET_STS_ERROR;
8792 
8793    when FND_API.G_EXC_UNEXPECTED_ERROR then
8794      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8795 
8796    when OTHERS then
8797      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8798 
8799 END Process_PosCons;
8800 
8801 /* ----------------------------------------------------------------------- */
8802 
8803 PROCEDURE Process_FTECons
8804 ( p_return_status                 OUT  NOCOPY  VARCHAR2,
8805   p_constraint_validation_status  OUT  NOCOPY  VARCHAR2,
8806   p_budget_revision_id            IN   NUMBER,
8807   p_constraint_id                 IN   NUMBER,
8808   p_constraint_name               IN   VARCHAR2,
8809   p_position_id                   IN   NUMBER := FND_API.G_MISS_NUM,
8810   p_position_name                 IN   VARCHAR2 := FND_API.G_MISS_CHAR,
8811   p_currency_code                 IN   VARCHAR2,
8812   p_severity_level                IN   NUMBER,
8813   p_summ_flag                     IN   VARCHAR2
8814 ) IS
8815 
8816   l_cons_failed                   VARCHAR2(1) := FND_API.G_FALSE;
8817 
8818   l_posset_total                  NUMBER := 0;
8819   l_cons_total                    NUMBER := 0;
8820 
8821   l_description                   VARCHAR2(2000);
8822 
8823   cursor c_Formula is
8824     select prefix_operator,
8825            amount
8826       from PSB_CONSTRAINT_FORMULAS
8827      where constraint_id = p_constraint_id;
8828 
8829   cursor c_SumAll is
8830     select sum(nvl(a.fte, 0)) Sum_FTE
8831       from PSB_POSITION_FTE a
8832      where exists
8833           (select 1
8834              from PSB_BUDGET_REVISION_POSITIONS c,
8835                   PSB_BUDGET_REVISION_POS_LINES d,
8836                   PSB_BUDGET_POSITIONS e,
8837                   PSB_SET_RELATIONS f
8838             where c.budget_revision_pos_line_id = d.budget_revision_pos_line_id
8839               and d.budget_revision_id = p_budget_revision_id
8840               and c.position_id = e.position_id
8841               and e.data_extract_id = g_data_extract_id
8842               and e.account_position_set_id = f.account_position_set_id
8843               and f.constraint_id = p_constraint_id)
8844        and a.budget_revision_id = p_budget_revision_id;
8845 
8846   cursor c_Sum is
8847     select sum(nvl(fte, 0)) Sum_FTE
8848       from PSB_POSITION_FTE
8849      where position_id = p_position_id
8850        and budget_revision_id = p_budget_revision_id;
8851 
8852 BEGIN
8853 
8854   for c_Formula_Rec in c_Formula loop
8855 
8856     l_cons_total := c_Formula_Rec.amount;
8857 
8858     if not FND_API.to_Boolean(p_summ_flag) then
8859     begin
8860 
8861       for c_Sum_Rec in c_Sum loop
8862         l_posset_total := c_Sum_Rec.Sum_FTE;
8863       end loop;
8864 
8865     end;
8866     else
8867     begin
8868 
8869       for c_SumAll_Rec in c_SumAll loop
8870         l_posset_total := c_SumAll_Rec.Sum_FTE;
8871       end loop;
8872 
8873     end;
8874     end if;
8875 
8876     if l_posset_total is not null then
8877     begin
8878 
8879       if c_Formula_Rec.prefix_operator = '<=' then
8880 
8881         if l_posset_total <= l_cons_total then
8882           l_cons_failed := FND_API.G_TRUE;
8883         end if;
8884 
8885       elsif c_Formula_Rec.prefix_operator = '>=' then
8886 
8887         if l_posset_total >= l_cons_total then
8888           l_cons_failed := FND_API.G_TRUE;
8889         end if;
8890 
8891       elsif c_Formula_Rec.prefix_operator = '<' then
8892 
8893         if l_posset_total < l_cons_total then
8894           l_cons_failed := FND_API.G_TRUE;
8895         end if;
8896 
8897       elsif c_Formula_Rec.prefix_operator = '>' then
8898 
8899         if l_posset_total > l_cons_total then
8900           l_cons_failed := FND_API.G_TRUE;
8901         end if;
8902 
8903       elsif c_Formula_Rec.prefix_operator = '=' then
8904 
8905         if l_posset_total = l_cons_total then
8906           l_cons_failed := FND_API.G_TRUE;
8907         end if;
8908 
8909       elsif c_Formula_Rec.prefix_operator = '<>' then
8910 
8911         if l_posset_total = l_cons_total then
8912           l_cons_failed := FND_API.G_TRUE;
8913         end if;
8914 
8915       end if;
8916 
8917     end;
8918     end if;
8919 
8920     if FND_API.to_Boolean(l_cons_failed) then
8921     begin
8922 
8923       if nvl(p_severity_level, -1) >= g_constraint_threshold then
8924         p_constraint_validation_status := 'F';
8925       else
8926         p_constraint_validation_status := 'E';
8927       end if;
8928 /* For Bug No : 1321519 Start */
8929       message_token('EFFECTIVE_START_DATE', nvl(g_constraint_start_date,g_from_date));
8930       message_token('EFFECTIVE_END_DATE', nvl(g_constraint_end_date,g_to_date));
8931       message_token('FIRST_GL_PERIOD_NAME', g_from_date);
8932       message_token('LAST_GL_PERIOD_NAME', g_to_date);
8933 /* For Bug No : 1321519 End */
8934       message_token('CONSTRAINT_SET', g_constraint_set_name);
8935       message_token('THRESHOLD', g_constraint_threshold);
8936       message_token('CONSTRAINT', p_constraint_name);
8937       message_token('SEVERITY_LEVEL', p_severity_level);
8938       message_token('ASSIGNMENT_VALUE', l_posset_total);
8939       message_token('OPERATOR', c_Formula_Rec.prefix_operator);
8940       message_token('FORMULA_VALUE', l_cons_total);
8941 
8942       if FND_API.to_Boolean(p_summ_flag) then
8943         message_token('NAME', p_constraint_name);
8944       else
8945         message_token('NAME', p_position_name);
8946       end if;
8947 
8948       add_message('PSB', 'PSB_REV_CONSTRAINT_FAILURE');
8949 
8950       l_description := FND_MSG_PUB.Get
8951                           (p_encoded => FND_API.G_FALSE);
8952       FND_MSG_PUB.Delete_Msg;
8953 
8954       insert into PSB_ERROR_MESSAGES
8955                  (Concurrent_Request_ID,
8956                   Process_ID,
8957                   Source_Process,
8958                   Description,
8959                   Creation_Date,
8960                   Created_By)
8961           values (FND_GLOBAL.CONC_REQUEST_ID,
8962                   p_budget_revision_id,
8963                   'BUDGET_REVISION',
8964                   l_description,
8965                   sysdate,
8966                   FND_GLOBAL.USER_ID);
8967 
8968     end;
8969     else
8970       p_constraint_validation_status := 'S';
8971     end if;
8972 
8973   end loop;
8974 
8975   -- Initialize API return status to success
8976 
8977   p_return_status := FND_API.G_RET_STS_SUCCESS;
8978 
8979 EXCEPTION
8980 
8981    when FND_API.G_EXC_ERROR then
8982      p_return_status := FND_API.G_RET_STS_ERROR;
8983 
8984    when FND_API.G_EXC_UNEXPECTED_ERROR then
8985      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8986 
8987    when OTHERS then
8988      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8989 
8990 END Process_FTECons;
8991 
8992 /* ----------------------------------------------------------------------- */
8993 
8994 PROCEDURE Process_PosCons_Detailed
8995 ( p_return_status                 OUT  NOCOPY  VARCHAR2,
8996   p_constraint_validation_status  OUT  NOCOPY  VARCHAR2,
8997   p_budget_revision_id            IN   NUMBER,
8998   p_constraint_id                 IN   NUMBER,
8999   p_constraint_name               IN   VARCHAR2,
9000   p_fte_constraint                IN   VARCHAR2,
9001   p_currency_code                 IN   VARCHAR2,
9002   p_severity_level                IN   NUMBER
9003 ) IS
9004 
9005   l_cons_validation_status        VARCHAR2(1) := 'S';
9006   l_detailed_status               VARCHAR2(1);
9007 
9008   l_return_status                 VARCHAR2(1);
9009 
9010   cursor c_Positions is
9011     select d.position_id,
9012            c.name
9013       from PSB_BUDGET_REVISION_POSITIONS a,
9014            PSB_BUDGET_REVISION_POS_LINES b,
9015            PSB_POSITIONS c,
9016            PSB_BUDGET_POSITIONS d,
9017            PSB_SET_RELATIONS e
9018      where a.budget_revision_pos_line_id = b.budget_revision_pos_line_id
9019        and b.budget_revision_id = p_budget_revision_id
9020        and a.position_id = c.position_id
9021        and c.position_id = d.position_id
9022        and d.data_extract_id = g_data_extract_id
9023        and d.account_position_set_id = e.account_position_set_id
9024        and e.constraint_id = p_constraint_id;
9025 
9026 BEGIN
9027 
9028   for c_Positions_Rec in c_Positions loop
9029 
9030     if ((p_fte_constraint is null) or (p_fte_constraint = 'N')) then
9031     begin
9032 
9033       Process_PosCons
9034              (p_budget_revision_id => p_budget_revision_id,
9035               p_constraint_id => p_constraint_id,
9036               p_constraint_name => p_constraint_name,
9037               p_position_id => c_Positions_Rec.position_id,
9038               p_position_name => c_Positions_Rec.name,
9039               p_currency_code => p_currency_code,
9040               p_severity_level => p_severity_level,
9041               p_summ_flag => FND_API.G_FALSE,
9042               p_constraint_validation_status => l_detailed_status,
9043               p_return_status => l_return_status);
9044 
9045       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
9046         raise FND_API.G_EXC_ERROR;
9047       end if;
9048 
9049     end;
9050     else
9051     begin
9052 
9053       Process_FTECons
9054              (p_budget_revision_id => p_budget_revision_id,
9055               p_constraint_id => p_constraint_id,
9056               p_constraint_name => p_constraint_name,
9057               p_position_id => c_Positions_Rec.position_id,
9058               p_position_name => c_Positions_Rec.name,
9059               p_currency_code => p_currency_code,
9060               p_severity_level => p_severity_level,
9061               p_summ_flag => FND_API.G_FALSE,
9062               p_constraint_validation_status => l_detailed_status,
9063               p_return_status => l_return_status);
9064 
9065       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
9066         raise FND_API.G_EXC_ERROR;
9067       end if;
9068 
9069     end;
9070     end if;
9071 
9072     if ((l_cons_validation_status = 'S') and
9073         (l_detailed_status <> 'S')) then
9074       l_cons_validation_status := l_detailed_status;
9075     elsif ((l_cons_validation_status = 'E') and
9076            (l_detailed_status = 'F')) then
9077       l_cons_validation_status := l_detailed_status;
9078     end if;
9079 
9080   end loop;
9081 
9082 
9083   -- Initialize API return status to success
9084 
9085   p_constraint_validation_status := l_cons_validation_status;
9086   p_return_status := FND_API.G_RET_STS_SUCCESS;
9087 
9088 
9089 EXCEPTION
9090 
9091    when FND_API.G_EXC_ERROR then
9092      p_return_status := FND_API.G_RET_STS_ERROR;
9093 
9094    when FND_API.G_EXC_UNEXPECTED_ERROR then
9095      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9096 
9097    when OTHERS then
9098      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9099 
9100 END Process_PosCons_Detailed;
9101 
9102 /* ----------------------------------------------------------------------- */
9103 
9104 PROCEDURE Apply_Position_Constraints
9105 ( p_return_status         OUT  NOCOPY  VARCHAR2,
9106   p_validation_status     OUT  NOCOPY  VARCHAR2,
9107   p_budget_revision_id    IN   NUMBER
9108 ) IS
9109 
9110   l_cons_validation_status     VARCHAR2(1);
9111   l_consset_validation_status  VARCHAR2(1) := 'S';
9112 
9113   l_return_status              VARCHAR2(1);
9114 
9115   cursor c_Constraint is
9116     select constraint_id,
9117            name,
9118            currency_code,
9119            severity_level,
9120            fte_constraint,
9121            effective_start_date,
9122            effective_end_date,
9123            constraint_detailed_flag
9124       from PSB_CONSTRAINT_ASSIGNMENTS_V
9125      where constraint_type = 'POSITION'
9126        and constraint_set_id = g_constraint_set_id
9127      order by severity_level desc;
9128 
9129 BEGIN
9130 
9131   for c_Constraint_Rec in c_Constraint loop
9132   /* ForBug No : 1321519 Start */
9133     g_constraint_start_date := c_Constraint_Rec.effective_start_date;
9134     g_constraint_end_date   := c_Constraint_Rec.effective_end_date;
9135   /* ForBug No : 1321519 End */
9136     if ((c_Constraint_Rec.constraint_detailed_flag is null) or
9137         (c_Constraint_Rec.constraint_detailed_flag = 'N')) then
9138     begin
9139 
9140       if ((c_Constraint_Rec.fte_constraint is null) or (c_Constraint_Rec.fte_constraint = 'N')) then
9141       begin
9142 
9143         Process_PosCons
9144                (p_budget_revision_id => p_budget_revision_id,
9145                 p_constraint_id => c_Constraint_Rec.constraint_id,
9146                 p_constraint_name => c_Constraint_Rec.name,
9147                 p_currency_code => nvl(c_Constraint_Rec.currency_code, g_func_currency),
9148                 p_severity_level => c_Constraint_Rec.severity_level,
9149                 p_summ_flag => FND_API.G_TRUE,
9150                 p_constraint_validation_status => l_cons_validation_status,
9151                 p_return_status => l_return_status);
9152 
9153         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
9154           raise FND_API.G_EXC_ERROR;
9155         end if;
9156 
9157       end;
9158       else
9159       begin
9160 
9161         Process_FTECons
9162                (p_budget_revision_id => p_budget_revision_id,
9163                 p_constraint_id => c_Constraint_Rec.constraint_id,
9164                 p_constraint_name => c_Constraint_Rec.name,
9165                 p_currency_code => nvl(c_Constraint_Rec.currency_code, g_func_currency),
9166                 p_severity_level => c_Constraint_Rec.severity_level,
9167                 p_summ_flag => FND_API.G_TRUE,
9168                 p_constraint_validation_status => l_cons_validation_status,
9169                 p_return_status => l_return_status);
9170 
9171         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
9172           raise FND_API.G_EXC_ERROR;
9173         end if;
9174 
9175       end;
9176       end if;
9177 
9178       if ((l_consset_validation_status = 'S') and
9179           (l_cons_validation_status <> 'S')) then
9180         l_consset_validation_status := l_cons_validation_status;
9181       elsif ((l_consset_validation_status = 'E') and
9182              (l_cons_validation_status = 'F')) then
9183         l_consset_validation_status := l_cons_validation_status;
9184       elsif ((l_consset_validation_status = 'W') and
9185              (l_cons_validation_status in ('F', 'E'))) then
9186         l_consset_validation_status := l_cons_validation_status;
9187       end if;
9188 
9189     end;
9190     else
9191     begin
9192 
9193       Process_PosCons_Detailed
9194              (p_return_status => l_return_status,
9195               p_constraint_validation_status => l_cons_validation_status,
9196               p_budget_revision_id => p_budget_revision_id,
9197               p_constraint_id => c_Constraint_Rec.constraint_id,
9198               p_constraint_name => c_Constraint_Rec.name,
9199               p_fte_constraint => c_Constraint_Rec.fte_constraint,
9200               p_currency_code => nvl(c_Constraint_Rec.currency_code, g_func_currency),
9201               p_severity_level => c_Constraint_Rec.severity_level);
9202 
9203       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
9204         raise FND_API.G_EXC_ERROR;
9205       end if;
9206 
9207       if ((l_consset_validation_status = 'S') and
9208           (l_cons_validation_status <> 'S')) then
9209         l_consset_validation_status := l_cons_validation_status;
9210       elsif ((l_consset_validation_status = 'E') and
9211              (l_cons_validation_status = 'F')) then
9212         l_consset_validation_status := l_cons_validation_status;
9213       elsif ((l_consset_validation_status = 'W') and
9214              (l_cons_validation_status in ('F', 'E'))) then
9215         l_consset_validation_status := l_cons_validation_status;
9216       end if;
9217 
9218     end;
9219     end if;
9220 
9221   end loop;
9222 
9223   -- Initialize API return status to success
9224 
9225   p_validation_status := l_consset_validation_status;
9226   p_return_status := FND_API.G_RET_STS_SUCCESS;
9227 
9228 
9229 EXCEPTION
9230 
9231    when FND_API.G_EXC_ERROR then
9232      p_return_status := FND_API.G_RET_STS_ERROR;
9233 
9234    when FND_API.G_EXC_UNEXPECTED_ERROR then
9235      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9236 
9237    when OTHERS then
9238      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9239 
9240 END Apply_Position_Constraints;
9241 
9242 /* ----------------------------------------------------------------------- */
9243 
9244 PROCEDURE Apply_Constraints
9245 ( p_api_version               IN   NUMBER,
9246   p_validation_level          IN   NUMBER := FND_API.G_VALID_LEVEL_FULL,
9247   p_return_status             OUT  NOCOPY  VARCHAR2,
9248   p_validation_status         OUT  NOCOPY  VARCHAR2,
9249   p_budget_revision_id        IN   NUMBER,
9250   p_constraint_set_id         IN   NUMBER
9251 ) IS
9252 
9253   l_api_name                  CONSTANT VARCHAR2(30)   := 'Apply_Constraints';
9254   l_api_version               CONSTANT NUMBER         := 1.0;
9255 
9256   l_constraint_set_status     VARCHAR2(1) := 'S';
9257   l_validation_status         VARCHAR2(1);
9258 
9259   l_return_status             VARCHAR2(1);
9260 
9261 BEGIN
9262 
9263   -- Standard call to check for call compatibility.
9264 
9265   if not FND_API.Compatible_API_Call (l_api_version,
9266                                       p_api_version,
9267                                       l_api_name,
9268                                       G_PKG_NAME)
9269   then
9270     raise FND_API.G_EXC_UNEXPECTED_ERROR;
9271   end if;
9272 
9273   Cache_Revision_Variables
9274        (p_return_status => l_return_status,
9275         p_budget_revision_id => p_budget_revision_id);
9276 
9277   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
9278     raise FND_API.G_EXC_ERROR;
9279   end if;
9280 
9281   if g_constraint_set_id is not null then
9282   begin
9283 
9284     delete from PSB_ERROR_MESSAGES
9285      where source_process = 'BUDGET_REVISION'
9286        and process_id = p_budget_revision_id;
9287 
9288     if g_constraint_set_id <> p_constraint_set_id then
9289       g_constraint_set_id := p_constraint_set_id;
9290     end if;
9291 
9292   end;
9293   end if;
9294 
9295   Apply_Account_Constraints
9296        (p_return_status => l_return_status,
9297         p_validation_status => l_validation_status,
9298         p_budget_revision_id => p_budget_revision_id);
9299 
9300   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
9301     raise FND_API.G_EXC_ERROR;
9302   end if;
9303 
9304   l_constraint_set_status := l_validation_status;
9305 
9306   Apply_Element_Constraints
9307        (p_return_status => l_return_status,
9308         p_budget_revision_id => p_budget_revision_id);
9309 
9310   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
9311     raise FND_API.G_EXC_ERROR;
9312   end if;
9313 
9314   Apply_Position_Constraints
9315        (p_return_status => l_return_status,
9316         p_validation_status => l_validation_status,
9317         p_budget_revision_id => p_budget_revision_id);
9318 
9319   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
9320     raise FND_API.G_EXC_ERROR;
9321   end if;
9322 
9323   if ((l_constraint_set_status = 'S') and
9324       (l_validation_status <> 'S')) then
9325     l_constraint_set_status := l_validation_status;
9326   elsif ((l_constraint_set_status = 'E') and
9327          (l_validation_status = 'F')) then
9328     l_constraint_set_status := l_validation_status;
9329   elsif ((l_constraint_set_status = 'W') and
9330          (l_validation_status in ('F', 'E'))) then
9331     l_constraint_set_status := l_validation_status;
9332   end if;
9333 
9334    /* For Bug No.2810621 Start*/
9335   Update PSB_BUDGET_REVISIONS
9336    set constraint_set_id = g_constraint_set_id,
9337        last_update_date = sysdate,
9338        last_updated_by = FND_GLOBAL.USER_ID,
9339        last_update_login = FND_GLOBAL.LOGIN_ID
9340    where budget_revision_id = p_budget_revision_id;
9341   /* For Bug No. 2810621 End*/
9342 
9343 
9344 
9345   -- Initialize API return status to success
9346 
9347   p_validation_status := l_constraint_set_status;
9348   p_return_status := FND_API.G_RET_STS_SUCCESS;
9349 
9350 EXCEPTION
9351 
9352    when FND_API.G_EXC_ERROR then
9353      p_return_status := FND_API.G_RET_STS_ERROR;
9354 
9355    when FND_API.G_EXC_UNEXPECTED_ERROR then
9356      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9357 
9358    when OTHERS then
9359      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9360 
9361      if FND_MSG_PUB.Check_Msg_Level
9362        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
9363        FND_MSG_PUB.Add_Exc_Msg
9364           (p_pkg_name => G_PKG_NAME,
9365            p_procedure_name => l_api_name);
9366 
9367      end if;
9368 
9369 END Apply_Constraints;
9370 
9371 /* ----------------------------------------------------------------------- */
9372 
9373 -- removing savepoints from this API to allow invocation from HR User Hooks
9374 
9375 PROCEDURE Delete_Revision_Positions
9376 ( p_api_version                     IN      NUMBER,
9377   p_init_msg_list                   IN      VARCHAR2 := FND_API.G_FALSE,
9378   p_commit                          IN      VARCHAR2 := FND_API.G_FALSE,
9379   p_validation_level                IN      NUMBER   := FND_API.G_VALID_LEVEL_FULL,
9380   p_return_status                   OUT  NOCOPY     VARCHAR2,
9381   p_msg_count                       OUT  NOCOPY     NUMBER,
9382   p_msg_data                        OUT  NOCOPY     VARCHAR2,
9383   p_budget_revision_id              IN      NUMBER ,
9384   p_budget_revision_pos_line_id     IN      NUMBER
9385 ) IS
9386 
9387   l_api_name                        CONSTANT VARCHAR2(30) := 'Delete_Revision_Positions';
9388   l_api_version                     CONSTANT NUMBER       := 1.0;
9389 
9390   l_position_id                     NUMBER;
9391   l_effective_start_date            DATE;
9392   l_effective_end_date              DATE;
9393   l_global_revision                 VARCHAR2(1);
9394 
9395   l_return_status                   VARCHAR2(1);
9396 
9397   cursor c_global_revision is
9398     select pbr.global_budget_revision
9399       from psb_budget_revisions pbr
9400      where pbr.budget_revision_id = p_budget_revision_id;
9401 
9402   cursor c_Position_Revision is
9403     select position_id, effective_start_date, effective_end_date
9404       from psb_budget_revision_positions
9405      where budget_revision_pos_line_id = p_budget_revision_pos_line_id;
9406 
9407 BEGIN
9408 
9409   -- Standard call to check for call compatibility.
9410 
9411   IF NOT FND_API.Compatible_API_Call (l_api_version,
9412                                       p_api_version,
9413                                       l_api_name,
9414                                       G_PKG_NAME)
9415   THEN
9416     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9417   END IF;
9418 
9419   -- Initialize message list if p_init_msg_list is set to TRUE.
9420 
9421   IF FND_API.to_Boolean (p_init_msg_list) THEN
9422     FND_MSG_PUB.initialize;
9423   END IF;
9424 
9425   Cache_Revision_Variables
9426        (p_return_status => l_return_status,
9427         p_budget_revision_id => p_budget_revision_id);
9428 
9429   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
9430     raise FND_API.G_EXC_ERROR;
9431   end if;
9432 
9433   For C_Position_Revision_rec in C_Position_Revision Loop
9434     l_position_id := C_Position_Revision_Rec.position_id;
9435     l_effective_start_date := C_Position_Revision_Rec.effective_start_date;
9436     l_effective_end_date := C_Position_Revision_Rec.effective_end_date;
9437   End Loop;
9438 
9439   For C_Global_Revision_Rec in C_Global_Revision Loop
9440     l_global_revision := C_global_Revision_Rec.global_budget_revision;
9441   End Loop;
9442 
9443 /* Bug No 2482305 Start */
9444 -- Added to remove the worksheet specific position records for deleted positions
9445   delete from PSB_POSITION_ASSIGNMENTS pa
9446         where pa.position_id = l_position_id
9447           and pa.worksheet_id = p_budget_revision_id
9448           and pa.data_extract_id = g_data_extract_id;
9449 /* Bug No 2482305 End */
9450 
9451   If l_global_revision = 'Y' Then
9452 
9453     DELETE PSB_BUDGET_REVISION_POSITIONS
9454      WHERE budget_revision_pos_line_id  = p_budget_revision_pos_line_id;
9455 
9456     DELETE PSB_BUDGET_REVISION_POS_LINES
9457      WHERE budget_revision_pos_line_id  = p_budget_revision_pos_line_id;
9458 
9459   Else
9460 
9461     DELETE PSB_BUDGET_REVISION_POS_LINES
9462      WHERE budget_revision_pos_line_id = p_budget_revision_pos_line_id
9463        AND budget_revision_id = p_budget_revision_id;
9464 
9465   End If;
9466 
9467   Reverse_Position_Accounts
9468          (p_return_status => l_return_status,
9469           p_budget_revision_id => p_budget_revision_id,
9470           p_position_id => l_position_id,
9471           p_effective_start_date => l_effective_start_date,
9472           p_effective_end_date => l_effective_end_date);
9473 
9474   -- Initialize API return status to success
9475 
9476   p_return_status := FND_API.G_RET_STS_SUCCESS;
9477 
9478   -- Standard call to get message count and if count is 1, get message info.
9479 
9480   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
9481                              p_data  => p_msg_data);
9482 
9483 EXCEPTION
9484 
9485    when FND_API.G_EXC_ERROR then
9486      p_return_status := FND_API.G_RET_STS_ERROR;
9487 
9488      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
9489                                 p_data  => p_msg_data);
9490 
9491    when FND_API.G_EXC_UNEXPECTED_ERROR then
9492      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9493 
9494      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
9495                                 p_data  => p_msg_data);
9496 
9497    when OTHERS then
9498      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9499 
9500      if FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
9501        FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
9502                                 l_api_name);
9503      end if;
9504 
9505      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
9506                                 p_data  => p_msg_data);
9507 
9508 End Delete_Revision_Positions;
9509 
9510 /* ----------------------------------------------------------------------- */
9511 
9512 -- removed savepoints to allow invocation from HR User Hooks
9513 
9514 PROCEDURE Delete_Revision_Accounts
9515 ( p_api_version                     IN      NUMBER,
9516   p_init_msg_list                   IN      VARCHAR2 := FND_API.G_FALSE,
9517   p_commit                          IN      VARCHAR2 := FND_API.G_FALSE,
9518   p_validation_level                IN      NUMBER   := FND_API.G_VALID_LEVEL_FULL,
9519   p_return_status                   OUT  NOCOPY     VARCHAR2,
9520   p_msg_count                       OUT  NOCOPY     NUMBER,
9521   p_msg_data                        OUT  NOCOPY     VARCHAR2,
9522   p_budget_revision_id              IN      NUMBER ,
9523   p_budget_revision_acct_line_id    IN      NUMBER)
9524 
9525 IS
9526 
9527   l_api_name    CONSTANT VARCHAR2(30)   := 'Delete_Revision_Accounts';
9528   l_api_version CONSTANT NUMBER         := 1.0;
9529 
9530   l_budget_revision_acct_line_id NUMBER := '';
9531   l_budget_version_id   number;
9532   l_global_revision     varchar2(1);
9533   l_return_status       varchar2(1);
9534   l_msg_count           number;
9535   l_msg_data            varchar2(2000);
9536 
9537 Cursor C_global_revision is
9538   Select pbr.global_budget_revision
9539     from psb_budget_revisions pbr
9540    where pbr.budget_revision_id = p_budget_revision_id;
9541 
9542 Cursor C_Account_line is
9543   Select pbrl.budget_revision_acct_line_id
9544     from psb_budget_revision_lines pbrl
9545    where pbrl.budget_revision_id = p_budget_revision_id
9546      and pbrl.budget_revision_acct_line_id  = p_budget_revision_acct_line_id;
9547 
9548 Begin
9549 
9550   -- Standard call to check for call compatibility.
9551 
9552   IF NOT FND_API.Compatible_API_Call (l_api_version,
9553                                       p_api_version,
9554                                       l_api_name,
9555                                       G_PKG_NAME)
9556   THEN
9557     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9558   END IF;
9559 
9560   -- Initialize message list if p_init_msg_list is set to TRUE.
9561 
9562   IF FND_API.to_Boolean (p_init_msg_list) THEN
9563     FND_MSG_PUB.initialize;
9564   END IF;
9565 
9566    -- Initialize API return status to success
9567 
9568   p_return_status := FND_API.G_RET_STS_SUCCESS;
9569 
9570 
9571   For C_Account_Line_Rec  in C_Account_Line
9572   Loop
9573      l_budget_revision_acct_line_id := C_account_Line_rec.budget_revision_acct_line_id;
9574   End Loop;
9575 
9576   if (l_budget_revision_acct_line_id is null) then
9577      null;
9578   else
9579 
9580       For C_Global_Revision_Rec in C_Global_Revision
9581       Loop
9582          l_global_revision := C_global_Revision_Rec.global_budget_revision;
9583       End Loop;
9584 
9585       IF l_global_revision = 'Y' THEN
9586          Delete PSB_BUDGET_REVISION_ACCOUNTS
9587           where budget_revision_acct_line_id = p_budget_revision_acct_line_id;
9588 
9589          Delete PSB_BUDGET_REVISION_LINES
9590           where budget_revision_acct_line_id = p_budget_revision_acct_line_id;
9591       ELSE
9592          Delete PSB_BUDGET_REVISION_LINES
9593           where budget_revision_acct_line_id = p_budget_revision_acct_line_id
9594            and budget_revision_id           = p_budget_revision_id;
9595       END IF;
9596 
9597   end if;
9598 
9599   -- Standard call to get message count and if count is 1, get message info.
9600 
9601   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
9602                              p_data  => p_msg_data);
9603 
9604 EXCEPTION
9605 
9606    when FND_API.G_EXC_ERROR then
9607      p_return_status := FND_API.G_RET_STS_ERROR;
9608 
9609      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
9610                                 p_data  => p_msg_data);
9611 
9612    when FND_API.G_EXC_UNEXPECTED_ERROR then
9613      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9614 
9615      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
9616                                 p_data  => p_msg_data);
9617 
9618    when OTHERS then
9619      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9620 
9621      if FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
9622 
9623        FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
9624                                 l_api_name);
9625      end if;
9626 
9627      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
9628                                 p_data  => p_msg_data);
9629 
9630 End Delete_Revision_Accounts;
9631 
9632 /*===========================================================================+
9633  |                 PROCEDURE Delete_Budget_Revision_Pvt ( Private )          |
9634  +===========================================================================*/
9635 --
9636 -- This API deletes an official budget_revision by performing deletes on
9637 -- psb_budget_revisions and matrix tables (psb_budget_revision_lines and
9638 -- psb_budget_revision_pos_lines).
9639 -- It also deletes budget_revision related data from other tables.
9640 --
9641 PROCEDURE Delete_Budget_Revision_Pvt
9642 (
9643   p_budget_revision_id   IN      NUMBER,
9644   p_revise_by_position   IN      VARCHAR2,
9645   p_budget_group_id      IN      NUMBER,
9646   p_return_status        OUT  NOCOPY     VARCHAR2
9647 )
9648 IS
9649   --
9650   l_api_name           CONSTANT    VARCHAR2(30):= 'Delete_Budget_Revision_Pvt';
9651   --
9652   l_account_line_id       NUMBER;
9653   l_position_line_id      NUMBER;
9654   l_data_extract_id       NUMBER;
9655   l_revise_by_position    VARCHAR2(1);
9656   l_msg_count             NUMBER ;
9657   l_msg_data              VARCHAR2(2000) ;
9658   l_return_status         VARCHAR2(1) ;
9659   --
9660 
9661   CURSOR l_br_account_lines_csr
9662     IS
9663     SELECT budget_revision_acct_line_id
9664     FROM   psb_budget_revision_lines
9665     WHERE  budget_revision_id = p_budget_revision_id;
9666 
9667   CURSOR l_br_position_lines_csr
9668     IS
9669     SELECT budget_revision_pos_line_id
9670     FROM   psb_budget_revision_pos_lines
9671     WHERE  budget_revision_id = p_budget_revision_id;
9672 
9673   CURSOR l_br_distribution_csr
9674     IS
9675     SELECT distribution_id
9676     FROM   psb_ws_distributions
9677     WHERE  worksheet_id = p_budget_revision_id
9678     AND    distribution_option_flag = 'R';
9679 
9680   CURSOR l_br_position_csr
9681     IS
9682     SELECT   position_assignment_id, pay_element_rate_id
9683     FROM     psb_position_assignments
9684     WHERE    worksheet_id = p_budget_revision_id
9685     AND      data_extract_id = l_data_extract_id
9686     GROUP BY position_assignment_id, pay_element_rate_id;
9687 
9688   /*For Bug No : 1527423 Start*/
9689   CURSOR l_br_pos_csr  is
9690 
9691     SELECT   position_id
9692       FROM   psb_positions pp
9693      WHERE   pp.data_extract_id = l_data_extract_id
9694        AND   nvl(pp.new_position_flag, 'N') = 'Y'
9695        AND   EXISTS (SELECT 1
9696                        FROM psb_budget_revision_positions brp,
9697                             psb_budget_revision_pos_lines brpl,
9698                             psb_budget_revisions br
9699                       WHERE br.budget_revision_id = p_budget_revision_id
9700                         AND br.budget_revision_id = brpl.budget_revision_id
9701                         AND brpl.budget_revision_pos_line_id = brp.budget_revision_pos_line_id
9702                         AND brp.position_id = pp.position_id
9703                      );
9704   /*For Bug No : 1527423 End*/
9705 
9706 BEGIN
9707   --
9708   p_return_status := FND_API.G_RET_STS_SUCCESS ;
9709 
9710   --
9711   -- Deleting account related information.
9712   --
9713   /*For Bug No : 1527423 Start*/
9714    l_data_extract_id := PSB_BUDGET_REVISIONS_PVT.FIND_SYSTEM_DATA_EXTRACT(p_budget_group_id);
9715   /*For Bug No : 1527423 End*/
9716 
9717   OPEN l_br_account_lines_csr;
9718 
9719   LOOP
9720     --
9721     FETCH l_br_account_lines_csr INTO l_account_line_id;
9722 
9723     IF (l_br_account_lines_csr%NOTFOUND) THEN
9724       EXIT;
9725     END IF;
9726 
9727     -- Deleting records from psb_budget_revision_lines.
9728 
9729     PSB_BUDGET_REVISIONS_PVT.Delete_Revision_Accounts
9730     ( p_api_version                    => 1.0 ,
9731       p_init_msg_list                  => FND_API.G_FALSE,
9732       p_commit                         => FND_API.G_FALSE,
9733       p_validation_level               => FND_API.G_VALID_LEVEL_FULL,
9734       p_return_status                  => l_return_status,
9735       p_msg_count                      => l_msg_count,
9736       p_msg_data                       => l_msg_data,
9737       p_budget_revision_id             => p_budget_revision_id,
9738       p_budget_revision_acct_line_id   => l_account_line_id);
9739 
9740      IF l_return_status = FND_API.G_RET_STS_ERROR THEN
9741        RAISE FND_API.G_EXC_ERROR ;
9742      ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
9743        RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
9744      END IF;
9745       --
9746   END LOOP;
9747 
9748   CLOSE l_br_account_lines_csr;
9749 
9750   --
9751   -- Deleting position related information.
9752   --
9753   IF ( p_revise_by_position = 'Y' ) THEN
9754   --
9755     /*For Bug No ; 1527423 Start*/
9756     -- Delete from psb_positions
9757     FOR l_br_pos_csr_rec IN l_br_pos_csr LOOP
9758       DELETE psb_position_assignments
9759        WHERE position_id = l_br_pos_csr_rec.position_id;
9760 
9761       DELETE psb_positions
9762        WHERE position_id = l_br_pos_csr_rec.position_id;
9763     END LOOP;
9764     /*For Bug No : 1527423 End*/
9765 
9766     OPEN l_br_position_lines_csr ;
9767 
9768     LOOP
9769     --
9770       FETCH l_br_position_lines_csr INTO l_position_line_id;
9771 
9772       IF ( l_br_position_lines_csr%NOTFOUND ) THEN
9773         EXIT;
9774       END IF;
9775 
9776       -- Deleting records from psb_budget_revision_lines.
9777 
9778       PSB_BUDGET_REVISIONS_PVT.Delete_Revision_Positions
9779       ( p_api_version                    => 1.0 ,
9780         p_init_msg_list                  => FND_API.G_FALSE,
9781         p_commit                         => FND_API.G_FALSE,
9782         p_validation_level               => FND_API.G_VALID_LEVEL_FULL,
9783         p_return_status                  => l_return_status,
9784         p_msg_count                      => l_msg_count,
9785         p_msg_data                       => l_msg_data,
9786         p_budget_revision_id             => p_budget_revision_id,
9787         p_budget_revision_pos_line_id    => l_position_line_id);
9788 
9789        IF l_return_status = FND_API.G_RET_STS_ERROR THEN
9790          RAISE FND_API.G_EXC_ERROR ;
9791        ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
9792          RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
9793        END IF;
9794 
9795     END LOOP;
9796 
9797     CLOSE l_br_position_lines_csr ;
9798 
9799   END IF ;    -- if p_revise_by_position is 'Y'.
9800   --
9801   --
9802   -- Delete from psb_ws_distribution_details.
9803 
9804   FOR    l_br_distribution_rec IN l_br_distribution_csr
9805   LOOP
9806     DELETE psb_ws_distribution_details
9807     WHERE  distribution_id = l_br_distribution_rec.distribution_id;
9808 
9809     -- Delete from psb_ws_distributions.
9810     DELETE psb_ws_distributions
9811     WHERE  distribution_id = l_br_distribution_rec.distribution_id;
9812   END LOOP;
9813 
9814   -- Delete from psb_workflow_processes.
9815   DELETE psb_workflow_processes
9816   WHERE  worksheet_id = p_budget_revision_id
9817   AND    document_type = 'BR' ;
9818 
9819   --Find system data extract for the given budget revision
9820   /*For Bug No ; 1527423 Start*/
9821   --following code has been moved to up
9822   --l_data_extract_id := PSB_BUDGET_REVISIONS_PVT.FIND_SYSTEM_DATA_EXTRACT(p_budget_group_id);
9823   /*For Bug No ; 1527423 End*/
9824 
9825   IF l_return_status = FND_API.G_RET_STS_ERROR THEN
9826     RAISE FND_API.G_EXC_ERROR ;
9827   ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
9828     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
9829   END IF;
9830 
9831   For l_br_position_rec In l_br_position_csr
9832   LOOP
9833     -- Delete from psb_position_assignments.
9834     DELETE psb_position_assignments
9835     WHERE  position_assignment_id = l_br_position_rec.position_assignment_id;
9836 
9837     -- Delete from psb_pay_elements_rates.
9838     DELETE psb_pay_element_rates
9839     WHERE  pay_element_rate_id = l_br_position_rec.pay_element_rate_id;
9840 
9841   END LOOP;
9842 
9843 /* Bug No 2482305 Start */
9844   -- Delete from psb_pay_element_rates.
9845   DELETE psb_pay_element_rates
9846   WHERE  worksheet_id = p_budget_revision_id ;
9847 /* Bug No 2482305 End */
9848 
9849   -- Delete from psb_position_accounts.
9850   DELETE psb_position_accounts
9851   WHERE  budget_revision_id = p_budget_revision_id ;
9852 
9853   -- Delete from psb_position_fte.
9854   DELETE psb_position_fte
9855   WHERE  budget_revision_id = p_budget_revision_id ;
9856 
9857   -- Delete from psb_position_costs
9858   DELETE psb_position_costs
9859   WHERE  budget_revision_id = p_budget_revision_id ;
9860 
9861   -- Delete from psb_ws_submit_comments.
9862   DELETE psb_ws_submit_comments
9863   WHERE  worksheet_id = p_budget_revision_id ;
9864 
9865   /*For Bug No : 2613269 Start*/
9866   fnd_attached_documents2_pkg.delete_attachments
9867              (X_entity_name => 'PSB_BUDGET_REVISIONS',
9868               X_pk1_value => p_budget_revision_id,
9869               X_delete_document_flag => 'Y'
9870              );
9871   /*For Bug No : 2613269 End*/
9872 
9873   -- Delete from psb_budget_revisions.
9874 
9875   PSB_BUDGET_REVISIONS_PVT.Delete_Row
9876   (p_api_version        =>   1.0 ,
9877    p_init_msg_list      =>   FND_API.G_FALSE,
9878    p_commit             =>   FND_API.G_FALSE,
9879    p_validation_level   =>   FND_API.G_VALID_LEVEL_FULL,
9880    p_return_status      =>   l_return_status,
9881    p_msg_count          =>   l_msg_count,
9882    p_msg_data           =>   l_msg_data,
9883    p_budget_revision_id =>   p_budget_revision_id);
9884 
9885   IF l_return_status = FND_API.G_RET_STS_ERROR THEN
9886     RAISE FND_API.G_EXC_ERROR ;
9887   ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
9888     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
9889   END IF;
9890 
9891 EXCEPTION
9892   --
9893  WHEN OTHERS THEN
9894     --
9895     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9896     --
9897     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
9898       FND_MSG_PUB.Add_Exc_Msg (  G_PKG_NAME,
9899                                  l_api_name );
9900     END IF;
9901     --
9902 END Delete_Budget_Revision_Pvt ;
9903 
9904 /*===========================================================================+
9905  |                     PROCEDURE Delete_Budget_Revision                      |
9906  +===========================================================================*/
9907 --
9908 -- The API This API deletes a local or global budget revision.
9909 --
9910 PROCEDURE Delete_Budget_Revision
9911 (
9912   p_api_version               IN       NUMBER   ,
9913   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
9914   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
9915   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
9916   p_return_status             OUT  NOCOPY      VARCHAR2 ,
9917   p_msg_count                 OUT  NOCOPY      NUMBER   ,
9918   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
9919   --
9920   p_budget_revision_id        IN       NUMBER
9921 )
9922 IS
9923   --
9924   l_api_name                CONSTANT VARCHAR2(30)   := 'Delete_Budget_Revision';
9925   l_api_version             CONSTANT NUMBER         :=  1.0 ;
9926   --
9927   l_return_status           VARCHAR2(1) ;
9928   l_msg_count               NUMBER ;
9929   l_msg_data                VARCHAR2(2000) ;
9930   --
9931   l_global_budget_revision  VARCHAR2(1);
9932   l_budget_group_id         NUMBER;
9933   l_data_extract_id         NUMBER;
9934   l_revise_by_position      VARCHAR2(1);
9935   l_budget_revisions_tab    PSB_Create_BR_Pvt.Budget_Revision_Tbl_Type;
9936   --
9937 BEGIN
9938   --
9939   SAVEPOINT Delete_Budget_Revision;
9940   --
9941   IF NOT FND_API.Compatible_API_Call ( l_api_version,
9942                                        p_api_version,
9943                                        l_api_name,
9944                                        G_PKG_NAME )
9945   THEN
9946     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
9947   END IF;
9948   --
9949 
9950   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
9951     FND_MSG_PUB.initialize ;
9952   END IF;
9953   --
9954   p_return_status := FND_API.G_RET_STS_SUCCESS ;
9955   --
9956 
9957   SELECT NVL( global_budget_revision, 'N') ,
9958          NVL( revise_by_position, 'N'),
9959          budget_group_id
9960        INTO
9961          l_global_budget_revision,
9962          l_revise_by_position,
9963          l_budget_group_id
9964   FROM   psb_budget_revisions
9965   WHERE  budget_revision_id = p_budget_revision_id ;
9966 
9967   --
9968   -- Take action bases on the type of the budget_revision.
9969   --
9970   IF l_global_budget_revision = 'Y' THEN
9971     --
9972     -- ( It means it is a global budget_revision.)
9973     -- Lock all the child budget revisions.
9974     --
9975 
9976     -- Find all related budget_revisions.
9977     FOR l_budget_revision_rec IN
9978     (
9979        SELECT budget_revision_id
9980        FROM   psb_budget_revisions
9981        WHERE  global_budget_revision_id  = p_budget_revision_id
9982        AND    NVL( global_budget_revision, 'N' ) = 'N'
9983     )
9984     LOOP
9985       --
9986       PSB_Create_BR_Pvt.Enforce_BR_Concurrency
9987       (
9988          p_api_version              => 1.0 ,
9989          p_init_msg_list            => FND_API.G_FALSE ,
9990          p_validation_level         => FND_API.G_VALID_LEVEL_FULL ,
9991          p_return_status            => l_return_status ,
9992          p_msg_count                => l_msg_count ,
9993          p_msg_data                 => l_msg_data  ,
9994          --
9995          p_budget_revision_id        => l_budget_revision_rec.budget_revision_id,
9996          p_parent_or_child_mode      => 'CHILD',
9997          p_maintenance_mode          => 'MAINTENANCE'
9998       );
9999       --
10000       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
10001         RAISE FND_API.G_EXC_ERROR ;
10002       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10003         RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
10004       END IF;
10005       --
10006     END LOOP ;  -- Lock child official, review group and local budget_revisions.
10007 
10008     -- Delete all the child official budget_revisions.
10009     FOR l_budget_revision_rec IN
10010     (
10011        SELECT budget_revision_id, revise_by_position, budget_group_id
10012        FROM   psb_budget_revisions
10013        WHERE  global_budget_revision_id    = p_budget_revision_id
10014        AND    NVL( global_budget_revision, 'N' ) = 'N'
10015     )
10016     LOOP
10017       --
10018       Delete_Budget_Revision_Pvt
10019       (
10020          p_budget_revision_id  =>  l_budget_revision_rec.budget_revision_id,
10021          p_revise_by_position  =>  l_budget_revision_rec.revise_by_position,
10022          p_budget_group_id     =>  l_budget_revision_rec.budget_group_id,
10023          p_return_status       =>  l_return_status
10024       ) ;
10025       --
10026       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
10027         RAISE FND_API.G_EXC_ERROR ;
10028       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10029         RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
10030       END IF;
10031       --
10032     END LOOP;
10033 
10034     -- Delete the global budget_revision now.
10035     Delete_Budget_Revision_Pvt
10036     (
10037        p_budget_revision_id  =>  p_budget_revision_id        ,
10038        p_revise_by_position  =>  l_revise_by_position  ,
10039        p_budget_group_id     =>  l_budget_group_id,
10040        p_return_status       =>  l_return_status
10041     ) ;
10042     --
10043     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
10044       RAISE FND_API.G_EXC_ERROR ;
10045     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10046       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
10047     END IF;
10048     --
10049   ELSE
10050     --
10051     -- Find all the child budget_revisions.
10052     PSB_Create_BR_Pvt.Find_Child_Budget_Revisions
10053     (
10054        p_api_version        =>   1.0 ,
10055        p_init_msg_list      =>   FND_API.G_FALSE,
10056        p_commit             =>   FND_API.G_FALSE,
10057        p_validation_level   =>   FND_API.G_VALID_LEVEL_FULL,
10058        p_return_status      =>   l_return_status,
10059        p_msg_count          =>   l_msg_count,
10060        p_msg_data           =>   l_msg_data,
10061        --
10062        p_budget_revision_id       =>   p_budget_revision_id,
10063        p_budget_revision_tbl      =>   l_budget_revisions_tab
10064     );
10065     --
10066     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
10067       RAISE FND_API.G_EXC_ERROR ;
10068     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10069       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
10070     END IF;
10071     --
10072     -- Adding the current budget_revision in the table as it has to go through
10073     -- the same processing
10074     l_budget_revisions_tab(0) := p_budget_revision_id ;
10075 
10076     --
10077     -- Process the current and all the child budget_revisions for locking.
10078     -- (Use 0 and COUNT-1 now).
10079     --
10080     FOR i IN 0..l_budget_revisions_tab.COUNT-1
10081     LOOP
10082 
10083       -- Lock the current budget_revision.
10084 
10085       PSB_Create_BR_Pvt.Enforce_BR_Concurrency
10086       (
10087          p_api_version              => 1.0 ,
10088          p_init_msg_list            => FND_API.G_FALSE ,
10089          p_validation_level         => FND_API.G_VALID_LEVEL_NONE ,
10090          p_return_status            => l_return_status ,
10091          p_msg_count                => l_msg_count ,
10092          p_msg_data                 => l_msg_data  ,
10093          --
10094          p_budget_revision_id        => l_budget_revisions_tab(i),
10095          p_parent_or_child_mode      => 'CHILD',
10096          p_maintenance_mode          => 'MAINTENANCE'
10097       );
10098 
10099       --
10100       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
10101         RAISE FND_API.G_EXC_ERROR ;
10102       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10103         RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
10104       END IF;
10105       --
10106     END LOOP; -- For locking phase.
10107     --
10108     -- Process the current and all the child budget revisions for deletion.
10109     -- (Use 0 and COUNT-1 now).
10110     --
10111     FOR i IN 0..l_budget_revisions_tab.COUNT-1
10112     LOOP
10113 
10114       -- Delete the current worksheet.
10115       Delete_Budget_Revision_Pvt
10116       (
10117          p_budget_revision_id  =>  l_budget_revisions_tab(i),
10118          p_revise_by_position  =>  l_revise_by_position,
10119          p_budget_group_id     =>  l_budget_group_id,
10120          p_return_status       =>  l_return_status
10121       ) ;
10122       --
10123       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
10124         RAISE FND_API.G_EXC_ERROR ;
10125       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10126         RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
10127       END IF;
10128 
10129     END LOOP;
10130 
10131   END IF; -- For the main IF statement, check for global_budget_revision.
10132 
10133   IF  FND_API.To_Boolean ( p_commit ) THEN
10134     COMMIT WORK;
10135   END IF;
10136   --
10137   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
10138                               p_data  => p_msg_data );
10139   --
10140 EXCEPTION
10141   --
10142   WHEN FND_API.G_EXC_ERROR THEN
10143     --
10144     ROLLBACK TO Delete_Budget_Revision ;
10145     p_return_status := FND_API.G_RET_STS_ERROR;
10146     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
10147                                 p_data  => p_msg_data );
10148   --
10149   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10150     --
10151     ROLLBACK TO Delete_Budget_Revision ;
10152     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10153     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
10154                                 p_data  => p_msg_data );
10155   --
10156   WHEN OTHERS THEN
10157     --
10158     ROLLBACK TO Delete_Budget_Revision ;
10159     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10160     --
10161     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
10162       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
10163                                 l_api_name);
10164     END if;
10165     --
10166     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
10167                                 p_data  => p_msg_data );
10168      --
10169 END Delete_Budget_Revision ;
10170 
10171 /*===========================================================================+
10172  |                   PROCEDURE Mass_Budget_Revision_CP                       |
10173  +===========================================================================*/
10174 --
10175 -- This is the execution file for the concurrent program 'Create Mass Budget
10176 -- Revision Entries'
10177 
10178 PROCEDURE Mass_Budget_Revision_CP
10179 (
10180   errbuf                      OUT  NOCOPY      VARCHAR2  ,
10181   retcode                     OUT  NOCOPY      VARCHAR2  ,
10182   --
10183   p_budget_revision_id        IN      NUMBER
10184 )
10185 IS
10186   --
10187   l_api_name       CONSTANT VARCHAR2(30)   := 'Mass_Budget_Revision_CP';
10188   l_api_version    CONSTANT NUMBER         :=  1.0 ;
10189   --
10190   l_error_api_name          VARCHAR2(2000);
10191   l_account_set_id          NUMBER;
10192   l_data_extract_id         NUMBER;
10193   l_return_status           VARCHAR2(1) ;
10194   l_msg_count               NUMBER ;
10195   l_msg_data                VARCHAR2(2000) ;
10196   l_msg_index_out           NUMBER;
10197   l_currency_code           VARCHAR2(15);
10198 
10199  Cursor C_Budget_Group is
10200    Select budget_group_id,currency_code -- Bug 3029168 added currency_code
10201      from psb_budget_revisions
10202     where budget_revision_id = p_budget_revision_id;
10203 
10204 BEGIN
10205 
10206   FND_FILE.Put_Line( FND_FILE.OUTPUT,
10207    'Processing the Budget Revision Batch Number : ' ||p_budget_revision_id);
10208 
10209   -- Enforce Concurrency Control
10210 
10211   PSB_CONCURRENCY_CONTROL_PVT.Enforce_Concurrency_Control
10212      (p_api_version => 1.0,
10213       p_return_status => l_return_status,
10214       p_concurrency_class => 'BUDGET_REVISION_CREATION',
10215       p_concurrency_entity_name => 'BUDGET_REVISION',
10216       p_concurrency_entity_id => p_budget_revision_id);
10217 
10218   if l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10219     raise FND_API.G_EXC_ERROR;
10220   end if;
10221 
10222   PSB_BUDGET_ACCOUNT_PVT.Populate_Budget_Accounts
10223      (p_api_version       =>  1.0,
10224       p_init_msg_list     =>  FND_API.G_TRUE,
10225       p_commit            =>  FND_API.G_TRUE,
10226       p_return_status     =>  l_return_status,
10227       p_msg_count         =>  l_msg_count,
10228       p_msg_data          =>  l_msg_data,
10229       p_account_set_id    =>  l_account_set_id
10230       );
10231 
10232   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
10233      raise FND_API.G_EXC_ERROR;
10234   end if;
10235 
10236   For C_Budget_Group_Rec in C_Budget_Group
10237   Loop
10238   l_data_extract_id := Find_System_Data_Extract
10239                 ( p_budget_group_id  => C_Budget_Group_Rec.budget_group_id);
10240   l_currency_code   := c_budget_group_rec.currency_code;
10241   End Loop;
10242 
10243   IF (l_data_extract_id IS NOT NULL)
10244   AND l_currency_code <> 'STAT' THEN -- Bug 3029168
10245   PSB_BUDGET_POSITION_PVT.Populate_Budget_Positions
10246      (p_api_version       =>  1.0,
10247       p_commit            =>  FND_API.G_TRUE,
10248       p_return_status     =>  l_return_status,
10249       p_msg_count         =>  l_msg_count,
10250       p_msg_data          =>  l_msg_data,
10251       p_data_extract_id   =>  l_data_extract_id
10252       );
10253 
10254   if l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10255     raise FND_API.G_EXC_ERROR;
10256   end if;
10257   end if;
10258 
10259   Create_Mass_Revision_Entries
10260   (p_api_version            => 1.0,
10261    p_init_msg_list          => FND_API.G_TRUE,
10262    p_commit                 => FND_API.G_TRUE,
10263    p_validation_level       => FND_API.G_VALID_LEVEL_NONE,
10264    p_return_status          => l_return_status,
10265    p_msg_count              => l_msg_count,
10266    p_msg_data               => l_msg_data,
10267    p_data_extract_id        => l_data_extract_id,
10268    p_budget_revision_id     => p_budget_revision_id);
10269 
10270    if l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10271     raise FND_API.G_EXC_ERROR;
10272   end if;
10273 
10274    PSB_CONCURRENCY_CONTROL_PVT.Release_Concurrency_Control
10275      (p_api_version => 1.0,
10276       p_return_status => l_return_status,
10277       p_concurrency_class => 'BUDGET_REVISION_CREATION',
10278       p_concurrency_entity_name => 'BUDGET_REVISION',
10279       p_concurrency_entity_id => p_budget_revision_id);
10280 
10281    if l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10282      RAISE FND_API.G_EXC_ERROR;
10283    end if;
10284     /* Start Bug No. 2322856 */
10285 --  PSB_MESSAGE_S.Print_Success;
10286     /* End Bug No. 2322856 */
10287   retcode := 0 ;
10288 
10289 EXCEPTION
10290 
10291    WHEN FND_API.G_EXC_ERROR THEN
10292      --
10293      PSB_MESSAGE_S.Print_Error ( p_mode         => FND_FILE.LOG ,
10294                                p_print_header =>  FND_API.G_TRUE );
10295      retcode := 2 ;
10296      COMMIT WORK ;
10297      --
10298    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10299     --
10300      PSB_MESSAGE_S.Print_Error ( p_mode         => FND_FILE.LOG ,
10301                                 p_print_header =>  FND_API.G_TRUE );
10302      retcode := 2 ;
10303      COMMIT WORK ;
10304      --
10305    WHEN OTHERS THEN
10306      --
10307      IF FND_MSG_PUB.Check_Msg_Level( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR ) THEN
10308       --
10309      FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME ,
10310                                l_api_name  ) ;
10311      END IF ;
10312      --
10313      l_return_status := FND_API.G_RET_STS_ERROR;
10314 
10315      PSB_MESSAGE_S.Print_Error ( p_mode         => FND_FILE.LOG ,
10316                                  p_print_header =>  FND_API.G_TRUE );
10317      --
10318      retcode := 2 ;
10319      COMMIT WORK ;
10320      --
10321 
10322 End Mass_Budget_Revision_CP;
10323 
10324 /*===========================================================================+
10325  |                   PROCEDURE Revision_Funds_Check_CP                       |
10326  +===========================================================================*/
10327 --
10328 -- This is the execution file for the concurrent program 'Perform Funds Check
10329 -- for Revision Entries'
10330 
10331 PROCEDURE Revision_Funds_Check_CP
10332 (
10333   errbuf                      OUT  NOCOPY      VARCHAR2  ,
10334   retcode                     OUT  NOCOPY      VARCHAR2  ,
10335   --
10336   p_budget_revision_id        IN      NUMBER
10337 )
10338 IS
10339   l_api_name       CONSTANT VARCHAR2(30)   := 'Revision_Funds_Check_CP';
10340   l_api_version    CONSTANT NUMBER         :=  1.0 ;
10341   --
10342   l_error_api_name          VARCHAR2(2000);
10343   l_account_set_id          NUMBER;
10344   l_data_extract_id         NUMBER;
10345   l_return_status           VARCHAR2(1) ;
10346   l_msg_count               NUMBER ;
10347   l_msg_data                VARCHAR2(2000) ;
10348   l_msg_index_out           NUMBER;
10349   l_fund_check_failures     NUMBER;
10350 
10351 BEGIN
10352 
10353   FND_FILE.Put_Line( FND_FILE.OUTPUT,
10354    'Processing the Budget Revision Batch Number : ' ||p_budget_revision_id);
10355 
10356     Budget_Revision_Funds_Check
10357     (
10358        p_api_version                 => 1.0 ,
10359        p_init_msg_list               => FND_API.G_FALSE,
10360        p_commit                      => FND_API.G_FALSE,
10361        p_validation_level            => FND_API.G_VALID_LEVEL_NONE,
10362        p_return_status               => l_return_status,
10363        p_msg_count                   => l_msg_count,
10364        p_msg_data                    => l_msg_data ,
10365        --
10366        p_funds_reserve_flag          => 'N',
10367        p_budget_revision_id          => p_budget_revision_id,
10368        p_fund_check_failures         => l_fund_check_failures,
10369        p_called_from                 => 'B' -- Bug#4310411
10370     );
10371 
10372     --
10373     IF l_return_status <> FND_API.G_RET_STS_SUCCESS  THEN
10374       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
10375     END IF ;
10376 
10377     /* Start Bug No. 2322856 */
10378 --  PSB_MESSAGE_S.Print_Success;
10379     /* End Bug No. 2322856 */
10380   retcode := 0 ;
10381 
10382 EXCEPTION
10383 
10384    WHEN FND_API.G_EXC_ERROR THEN
10385      --
10386      PSB_MESSAGE_S.Print_Error ( p_mode         => FND_FILE.LOG ,
10387                                p_print_header =>  FND_API.G_TRUE );
10388      retcode := 2 ;
10389      COMMIT WORK ;
10390      --
10391    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10392     --
10393      PSB_MESSAGE_S.Print_Error ( p_mode         => FND_FILE.LOG ,
10394                                 p_print_header =>  FND_API.G_TRUE );
10395      retcode := 2 ;
10396      COMMIT WORK ;
10397      --
10398    WHEN OTHERS THEN
10399      --
10400      IF FND_MSG_PUB.Check_Msg_Level( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR ) THEN
10401       --
10402      FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME ,
10403                                l_api_name  ) ;
10404      END IF ;
10405      --
10406      l_return_status := FND_API.G_RET_STS_ERROR;
10407 
10408      PSB_MESSAGE_S.Print_Error ( p_mode         => FND_FILE.LOG ,
10409                                  p_print_header =>  FND_API.G_TRUE );
10410      --
10411      retcode := 2 ;
10412      COMMIT WORK ;
10413      --
10414 End Revision_Funds_Check_CP;
10415 
10416 /*===========================================================================+
10417  |                   PROCEDURE Revise_Projections_CP                         |
10418  +===========================================================================*/
10419 --
10420 PROCEDURE Revise_Projections_CP
10421 (
10422   errbuf                OUT  NOCOPY      VARCHAR2  ,
10423   retcode               OUT  NOCOPY      VARCHAR2  ,
10424   --
10425   p_budget_revision_id  IN   NUMBER,
10426   p_parameter_id        IN   NUMBER
10427 ) IS
10428 
10429   l_api_name          CONSTANT VARCHAR2(30)     := 'Revise_Projections_CP';
10430   l_api_version       CONSTANT NUMBER           := 1.0;
10431 
10432   l_error_api_name          VARCHAR2(2000);
10433   l_data_extract_id         NUMBER;
10434   l_return_status           VARCHAR2(1) ;
10435   l_msg_count               NUMBER ;
10436   l_msg_data                VARCHAR2(2000) ;
10437   l_msg_index_out           NUMBER;
10438   l_validation_status       VARCHAR2(1);
10439 
10440   l_set_CP_status           BOOLEAN := FALSE; -- Bug#4571412
10441   l_currency_code           VARCHAR2(15);
10442 
10443  Cursor C_Budget_Group is
10444    Select budget_group_id,currency_code  -- Bug 3029168 added currency code
10445      from psb_budget_revisions
10446     where budget_revision_id = p_budget_revision_id;
10447 
10448 BEGIN
10449 
10450   FND_FILE.Put_Line( FND_FILE.OUTPUT,
10451    'Revise Projections for Budget Revision Batch Number : ' ||p_budget_revision_id);
10452 
10453   -- Enforce Concurrency Control
10454 
10455   PSB_CONCURRENCY_CONTROL_PVT.Enforce_Concurrency_Control
10456      (p_api_version => 1.0,
10457       p_return_status => l_return_status,
10458       p_concurrency_class => 'BUDGET_REVISION_CREATION',
10459       p_concurrency_entity_name => 'BUDGET_REVISION',
10460       p_concurrency_entity_id => p_budget_revision_id);
10461 
10462   if l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10463     raise FND_API.G_EXC_ERROR;
10464   end if;
10465 
10466   For C_Budget_Group_Rec in C_Budget_Group
10467   Loop
10468   l_data_extract_id := Find_System_Data_Extract
10469                 ( p_budget_group_id  => C_Budget_Group_Rec.budget_group_id);
10470   l_currency_code   := c_budget_group_rec.currency_code;
10471 
10472   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
10473      raise FND_API.G_EXC_ERROR;
10474   end if;
10475   End Loop;
10476 
10477   Create_Mass_Revision_Entries
10478   (p_api_version            => 1.0,
10479    p_init_msg_list          => FND_API.G_TRUE,
10480    p_commit                 => FND_API.G_TRUE,
10481    p_validation_level       => FND_API.G_VALID_LEVEL_NONE,
10482    p_return_status          => l_return_status,
10483    p_msg_count              => l_msg_count,
10484    p_msg_data               => l_msg_data,
10485    p_data_extract_id        => l_data_extract_id,
10486    p_budget_revision_id     => p_budget_revision_id,
10487    p_parameter_id           => p_parameter_id);
10488 
10489    -- Bug#4571412
10490    -- Set the CP status to Warning if
10491    -- Note updation had errors.
10492    IF NVL(PSB_BUDGET_REVISIONS_PVT.g_soft_error_flag, 'N') ='Y' THEN
10493      l_set_CP_status
10494        := FND_CONCURRENT.SET_COMPLETION_STATUS
10495           (status  => 'WARNING',
10496            message => NULL
10497           );
10498    END IF;
10499 
10500    if l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10501     raise FND_API.G_EXC_ERROR;
10502   end if;
10503 
10504    Apply_Constraints
10505      (p_api_version => 1.0,
10506       p_return_status => l_return_status,
10507       p_validation_status => l_validation_status,
10508       p_budget_revision_id => p_budget_revision_id,
10509       p_constraint_set_id => g_constraint_set_id);
10510 
10511   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
10512     raise FND_API.G_EXC_ERROR;
10513   end if;
10514 
10515   PSB_CONCURRENCY_CONTROL_PVT.Release_Concurrency_Control
10516      (p_api_version => 1.0,
10517       p_return_status => l_return_status,
10518       p_concurrency_class => 'BUDGET_REVISION_CREATION',
10519       p_concurrency_entity_name => 'BUDGET_REVISION',
10520       p_concurrency_entity_id => p_budget_revision_id);
10521 
10522    if l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10523      RAISE FND_API.G_EXC_ERROR;
10524    end if;
10525 
10526     /* Start Bug No. 2322856 */
10527 --  PSB_MESSAGE_S.Print_Success;
10528     /* End Bug No. 2322856 */
10529   retcode := 0 ;
10530 
10531 EXCEPTION
10532 
10533    WHEN FND_API.G_EXC_ERROR THEN
10534      --
10535      PSB_MESSAGE_S.Print_Error ( p_mode         => FND_FILE.LOG ,
10536                                p_print_header =>  FND_API.G_TRUE );
10537      retcode := 2 ;
10538      COMMIT WORK ;
10539      --
10540    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10541     --
10542      PSB_MESSAGE_S.Print_Error ( p_mode         => FND_FILE.LOG ,
10543                                 p_print_header =>  FND_API.G_TRUE );
10544      retcode := 2 ;
10545      COMMIT WORK ;
10546      --
10547    WHEN OTHERS THEN
10548      --
10549      IF FND_MSG_PUB.Check_Msg_Level( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR ) THEN
10550       --
10551      FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME ,
10552                                l_api_name  ) ;
10553      END IF ;
10554      --
10555      l_return_status := FND_API.G_RET_STS_ERROR;
10556 
10557      PSB_MESSAGE_S.Print_Error ( p_mode         => FND_FILE.LOG ,
10558                                  p_print_header =>  FND_API.G_TRUE );
10559      --
10560      retcode := 2 ;
10561      COMMIT WORK ;
10562      --
10563 END Revise_Projections_CP;
10564 
10565 /*===========================================================================+
10566  |                      PROCEDURE Delete_Budget_Revision_CP                  |
10567  +===========================================================================*/
10568 --
10569 -- This is the execution file for the concurrent program 'Maintain Budget
10570 -- Account Codes'.
10571 --
10572 PROCEDURE Delete_Budget_Revision_CP
10573 ( errbuf                      OUT  NOCOPY      VARCHAR2,
10574   retcode                     OUT  NOCOPY      VARCHAR2,
10575   p_from_budget_revision_id   IN       NUMBER,
10576   p_to_budget_revision_id     IN       NUMBER,
10577   p_submission_status         IN       VARCHAR2) IS
10578 
10579     l_api_name         CONSTANT VARCHAR2(30)   := 'Delete_Budget_Revision_CP' ;
10580     l_api_version      CONSTANT NUMBER         :=  1.0 ;
10581     --
10582     l_return_status             VARCHAR2(1) ;
10583     l_msg_count                 NUMBER ;
10584     l_msg_data                  VARCHAR2(2000) ;
10585     --
10586     l_string                   VARCHAR2(25) ;
10587 
10588   CURSOR l_budget_revisions_csr IS
10589   SELECT budget_revision_id
10590   FROM   psb_budget_revisions
10591   WHERE  budget_revision_id BETWEEN p_from_budget_revision_id
10592   AND    p_to_budget_revision_id
10593   AND    submission_status = p_submission_status;
10594 
10595   CURSOR l_budget_rev_csr IS
10596   SELECT budget_revision_id
10597   FROM   psb_budget_revisions
10598   WHERE  budget_revision_id BETWEEN p_from_budget_revision_id
10599   AND    p_to_budget_revision_id;
10600 
10601 BEGIN
10602   --
10603   SAVEPOINT Delete_Budget_Revision_CP_Pvt ;
10604   --
10605 
10606   If p_submission_status IN ('A', 'R') THEN
10607 
10608   FOR l_budget_revisions_rec IN l_budget_revisions_csr
10609   LOOP
10610   Delete_Budget_Revision
10611   (
10612      p_api_version             =>   1.0 ,
10613      p_init_msg_list           =>   FND_API.G_TRUE,
10614      p_commit                  =>   FND_API.G_FALSE,
10615      p_validation_level        =>   FND_API.G_VALID_LEVEL_FULL,
10616      p_return_status           =>   l_return_status,
10617      p_msg_count               =>   l_msg_count,
10618      p_msg_data                =>   l_msg_data,
10619      --
10620      p_budget_revision_id      =>   l_budget_revisions_rec.budget_revision_id
10621   );
10622   --
10623   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10624     RAISE FND_API.G_EXC_ERROR;
10625   END IF;
10626   --
10627   END LOOP;
10628   ELSE
10629 
10630   FOR l_budget_revisions_rec IN l_budget_rev_csr
10631   LOOP
10632   Delete_Budget_Revision
10633   (
10634      p_api_version             =>   1.0 ,
10635      p_init_msg_list           =>   FND_API.G_TRUE,
10636      p_commit                  =>   FND_API.G_FALSE,
10637      p_validation_level        =>   FND_API.G_VALID_LEVEL_FULL,
10638      p_return_status           =>   l_return_status,
10639      p_msg_count               =>   l_msg_count,
10640      p_msg_data                =>   l_msg_data,
10641      --
10642      p_budget_revision_id      =>   l_budget_revisions_rec.budget_revision_id
10643   );
10644 
10645   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10646     RAISE FND_API.G_EXC_ERROR;
10647   END IF;
10648 
10649   END LOOP;
10650 
10651   END IF;
10652 
10653   --
10654   retcode := 0 ;
10655   COMMIT WORK;
10656   --
10657 EXCEPTION
10658   --
10659   WHEN FND_API.G_EXC_ERROR THEN
10660     --
10661     ROLLBACK TO Delete_Budget_Revision_CP_Pvt ;
10662     PSB_MESSAGE_S.Print_Error ( p_mode         => FND_FILE.LOG ,
10663                                 p_print_header =>  FND_API.G_TRUE ) ;
10664     retcode := 2 ;
10665     --
10666   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10667     --
10668     ROLLBACK TO Delete_Budget_Revision_CP_Pvt ;
10669     PSB_MESSAGE_S.Print_Error ( p_mode         => FND_FILE.LOG ,
10670                                 p_print_header =>  FND_API.G_TRUE ) ;
10671     retcode := 2 ;
10672     --
10673   WHEN OTHERS THEN
10674     --
10675     ROLLBACK TO Delete_Budget_Revision_CP_Pvt ;
10676     IF FND_MSG_PUB.Check_Msg_Level( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR ) THEN
10677       --
10678       FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME ,
10679                                l_api_name  ) ;
10680     END IF ;
10681     --
10682     PSB_MESSAGE_S.Print_Error ( p_mode         =>  FND_FILE.LOG ,
10683                                 p_print_header =>  FND_API.G_TRUE ) ;
10684     retcode := 2 ;
10685     --
10686 END Delete_Budget_Revision_CP ;
10687 
10688 /* ----------------------------------------------------------------------- */
10689 
10690 -- Add Token and Value to the Message Token array
10691 
10692 PROCEDURE message_token(tokname IN VARCHAR2,
10693                         tokval  IN VARCHAR2) AS
10694 
10695 Begin
10696 
10697   if no_msg_tokens is null then
10698     no_msg_tokens := 1;
10699   else
10700     no_msg_tokens := no_msg_tokens + 1;
10701   end if;
10702 
10703   msg_tok_names(no_msg_tokens) := tokname;
10704   msg_tok_val(no_msg_tokens) := tokval;
10705 
10706 END message_token;
10707 
10708 /* ----------------------------------------------------------------------- */
10709 
10710 -- Define a Message Token with a Value and set the Message Name
10711 
10712 -- Calls FND_MESSAGE server package to set the Message Stack. This message is
10713 -- retrieved by the calling program.
10714 
10715 PROCEDURE add_message(appname IN VARCHAR2,
10716                       msgname IN VARCHAR2) AS
10717 
10718   i  BINARY_INTEGER;
10719 
10720 Begin
10721 
10722   if ((appname is not null) and
10723       (msgname is not null)) then
10724 
10725     FND_MESSAGE.SET_NAME(appname, msgname);
10726 
10727     if no_msg_tokens is not null then
10728 
10729       for i in 1..no_msg_tokens loop
10730         FND_MESSAGE.SET_TOKEN(msg_tok_names(i), msg_tok_val(i));
10731       end loop;
10732 
10733     end if;
10734 
10735     FND_MSG_PUB.Add;
10736 
10737   end if;
10738 
10739   -- Clear Message Token stack
10740 
10741   no_msg_tokens := 0;
10742 
10743 END add_message;
10744 
10745 
10746 /* Budget Revision Rules Enhancement Start */
10747 /*===========================================================================+
10748  |                      PROCEDURE Apply_Revision_Rules                  |
10749  +===========================================================================*/
10750 --
10751 -- This procedure has been added for validating the Budget Revision Rules
10752 --
10753 
10754 PROCEDURE Apply_Revision_Rules
10755 ( p_api_version                 IN   NUMBER,
10756   p_validation_level            IN   NUMBER := FND_API.G_VALID_LEVEL_FULL,
10757   p_return_status               OUT  NOCOPY  VARCHAR2,
10758   p_validation_status           OUT  NOCOPY  VARCHAR2,
10759   p_budget_revision_id          IN   NUMBER
10760 )
10761 IS
10762   --
10763   l_return_status               VARCHAR2(1);
10764   l_rule_validation_status      VARCHAR2(1);
10765   l_ruleset_validation_status   VARCHAR2(1);
10766   l_budget_group_id             NUMBER(20);
10767 
10768   /* Bug 3622182 Start */
10769   --  l_transaction_type            VARCHAR2(2);
10770   l_transaction_type            VARCHAR2(30);
10771   /* Bug 3622182 End */
10772 
10773   l_chart_of_accounts_id        NUMBER;
10774   l_severity_level              NUMBER;
10775   l_constraint_threshold        NUMBER;
10776   l_rule_set_name               VARCHAR2(30);
10777   l_description                 VARCHAR2(2000) := NULL;
10778   l_cnt                         NUMBER := 0;
10779   l_ctr                         NUMBER := 0;
10780   /*For Bug No : 2150471 Start*/
10781   l_apply_message               VARCHAR2(2000);
10782   l_balance_message             VARCHAR2(2000);
10783   l_con_segments                VARCHAR2(233);
10784   /*For Bug No : 2150471 End*/
10785   /*For Bug No : 2129723 Start*/
10786   l_acct_exists                 VARCHAR2(10);
10787   CURSOR c_acct_exists(l_rule_id NUMBER) IS
10788      SELECT 'exists' result FROM dual WHERE EXISTS
10789       (
10790         SELECT bra.code_combination_id
10791           FROM PSB_BUDGET_REVISION_LINES brl,
10792                PSB_BUDGET_REVISION_ACCOUNTS bra
10793          WHERE brl.budget_revision_id = p_budget_revision_id
10794            AND bra.budget_revision_acct_line_id = brl.budget_revision_acct_line_id
10795            /*For Bug No : 2161125 Start*/
10796            AND bra.position_id IS  NULL
10797            /*For Bug No : 2161125 End*/
10798            AND EXISTS (SELECT 1
10799                          FROM PSB_BUDGET_ACCOUNTS ba,
10800                               PSB_SET_RELATIONS_V sr
10801                         WHERE ba.code_combination_id = bra.code_combination_id
10802                           AND ba.account_position_set_id = sr.account_position_set_id
10803                           AND sr.rule_id = l_rule_id
10804                           AND sr.account_or_position_type = 'A'
10805                           AND sr.apply_balance_flag = 'A'
10806                       )
10807            AND EXISTS (SELECT 1
10808                          FROM PSB_ENTITY ent
10809                         WHERE ent.entity_id = l_rule_id
10810                           AND (   (ent.apply_account_set_flag = 'B')
10811 /* Bug No 2144364 Start */
10812 ----                           OR (ent.apply_account_set_flag = bra.revision_type)
10813                                OR (ent.apply_account_set_flag =
10814                                                 DECODE(SIGN(bra.revision_amount), -1,
10815                                                         DECODE(bra.revision_type, 'I', 'D', 'I'), bra.revision_type)
10816                                   )
10817 /* Bug No 2144364 End */
10818                               )
10819                       )
10820 /* Bug No 2133484 Start */
10821            AND EXISTS (SELECT 1
10822                          FROM PSB_RULE_TRANSACTION_TYPE rtt
10823                         WHERE rtt.rule_id = l_rule_id
10824                           AND rtt.transaction_type = l_transaction_type
10825                        -- Next 1 line added for Bug # 2123930
10826                           AND rtt.enable_flag = 'Y'
10827                       )
10828 /* Bug No 2133484 End */
10829 /* Bug No 2135165 Start */
10830            AND EXISTS (SELECT 1 FROM PSB_ENTITY_ASSIGNMENT ea, gl_sets_of_books sob, gl_periods_v gp
10831                         WHERE ea.entity_set_id = g_brr_rule_set_id
10832                           AND ea.entity_id = l_rule_id
10833                           AND sob.set_of_books_id = g_brr_sob_id
10834                           AND gp.period_set_name = sob.period_set_name
10835                           AND gp.adjustment_period_flag = 'N'
10836                           AND gp.period_name = bra.gl_period_name
10837                           AND (((ea.effective_start_date <= gp.end_date)
10838                           AND (ea.effective_end_date IS NULL))
10839                                 OR ((ea.effective_start_date BETWEEN gp.start_date AND gp.end_date)
10840                                 OR (ea.effective_end_date BETWEEN gp.start_date AND gp.end_date)
10841                                 OR ((ea.effective_start_date < gp.start_date)
10842                                         AND (ea.effective_end_date > gp.end_date))))
10843                         )
10844 /* Bug No 2135165 End */
10845       );
10846   /*For Bug No : 2129723 End*/
10847 
10848   cursor c_Brrule is
10849      SELECT  rra.rule_set_id, rra.rule_id, rra.name, rra.rule_type,
10850              rra.severity_level, rra.apply_account_set_flag,
10851              rra.balance_account_set_flag
10852      FROM    PSB_REVISION_RULE_ASSIGNMENT_V rra
10853      WHERE   rra.rule_set_id IN (
10854                SELECT  rrs.rule_set_id
10855                FROM    PSB_REVISION_RULE_SETS_V rrs
10856                WHERE   rrs.enable_flag = 'Y'
10857                and rrs.budget_group_id in (select budget_group_id
10858                         FROM    PSB_BUDGET_GROUPS
10859                         WHERE   budget_group_type = 'R'
10860                         START WITH budget_group_id = l_budget_group_id
10861                         CONNECT BY PRIOR parent_budget_group_id = budget_group_id));
10862 
10863 BEGIN
10864   --
10865 
10866   SELECT budget_group_id, transaction_type
10867     INTO l_budget_group_id, l_transaction_type
10868     FROM psb_budget_revisions_v
10869    WHERE budget_revision_id = p_budget_revision_id ;
10870   --
10871   SELECT chart_of_accounts_id,
10872 /* Bug No 2135165 Start */
10873          set_of_books_id
10874 /* Bug No 2135165 End */
10875     INTO l_chart_of_accounts_id,
10876 /* Bug No 2135165 Start */
10877          g_brr_sob_id
10878 /* Bug No 2135165 End */
10879     FROM gl_sets_of_books
10880    WHERE set_of_books_id = (SELECT b.set_of_books_id FROM PSB_BUDGET_GROUPS a,
10881                 PSB_BUDGET_GROUPS b
10882                 WHERE a.budget_group_id = l_budget_group_id
10883                 and nvl(a.root_budget_group_id, a.budget_group_id) = b.budget_group_id);
10884   --
10885   /*For Bug No : 2125969 Start*/
10886   IF g_constraint_set_id IS NULL THEN
10887   BEGIN
10888     DELETE FROM PSB_ERROR_MESSAGES
10889      WHERE source_process = 'BUDGET_REVISION'
10890        AND process_id = p_budget_revision_id;
10891   END;
10892   END IF;
10893   /*For Bug No : 2125969 End*/
10894 
10895   FOR c_Brrule_Rec IN c_Brrule LOOP
10896     BEGIN
10897 
10898 /* Bug No 2135165 Start */
10899     g_brr_rule_set_id := c_Brrule_Rec.rule_set_id;
10900 /* Bug No 2135165 End */
10901       -- Call this procedure which apply rules for individual CCIDs
10902         SELECT name, constraint_threshold
10903           INTO l_rule_set_name, l_constraint_threshold
10904           FROM psb_revision_rule_sets_v
10905          WHERE rule_set_id = c_Brrule_Rec.rule_set_id AND
10906         /*For Bug No : 2125969 Start*/
10907                --budget_group_id = l_budget_group_id  AND
10908         /*For Bug No : 2125969 End*/
10909                enable_flag = 'Y';
10910         l_severity_level := c_Brrule_Rec.severity_level;
10911       --
10912       /*For Bug No : 2129723 Start*/
10913       l_acct_exists := 'Not exists';
10914       FOR c_acct_exists_rec IN c_acct_exists(c_Brrule_Rec.rule_id) LOOP
10915         l_acct_exists := c_acct_exists_rec.result;
10916       END LOOP;
10917 
10918       IF (l_acct_exists <> 'Not exists' ) THEN
10919       BEGIN
10920         /*For Bug No : 2129723 End*/
10921         g_no_ccids := 0;
10922         /*For Bug No : 2150471 End*/
10923 
10924 
10925         if c_Brrule_Rec.rule_type in ('TEMPORARY', 'PERMANENT') then
10926             Apply_Detail_Revision_Rules
10927            (
10928             p_return_status             =>      l_return_status,
10929             p_rule_validation_status    =>      l_rule_validation_status,
10930             p_budget_revision_id        =>      p_budget_revision_id,
10931             p_rule_id                   =>      c_Brrule_Rec.rule_id,
10932             p_rule_type                 =>      c_Brrule_Rec.rule_type,
10933             p_apply_account_set_flag    =>      c_Brrule_Rec.apply_account_set_flag,
10934             p_balance_account_set_flag  =>      c_Brrule_Rec.balance_account_set_flag,
10935             p_segment_name              =>      NULL,
10936             p_application_column_name   =>      NULL,
10937             p_chart_of_accounts_id      =>      l_chart_of_accounts_id
10938            );
10939 
10940         else
10941          SELECT count(*) into l_cnt
10942          FROM   PSB_RULE_WITHIN_SEGMENT
10943          WHERE  rule_id = c_Brrule_Rec.rule_id;
10944 
10945          if l_cnt = 0 then
10946             Apply_Detail_Revision_Rules
10947            (
10948             p_return_status             =>      l_return_status,
10949             p_rule_validation_status    =>      l_rule_validation_status,
10950             p_budget_revision_id        =>      p_budget_revision_id,
10951             p_rule_id                   =>      c_Brrule_Rec.rule_id,
10952             p_rule_type                 =>      c_Brrule_Rec.rule_type,
10953             p_apply_account_set_flag    =>      c_Brrule_Rec.apply_account_set_flag,
10954             p_balance_account_set_flag  =>      c_Brrule_Rec.balance_account_set_flag,
10955             p_segment_name              =>      NULL,
10956             p_application_column_name   =>      NULL,
10957             p_chart_of_accounts_id      =>      l_chart_of_accounts_id
10958            );
10959          else
10960 
10961          for c_rule_seg in (Select segment_name, application_column_name
10962                         from psb_rule_within_segment
10963                         where rule_id = c_Brrule_Rec.rule_id)
10964          Loop
10965             Apply_Detail_Revision_Rules
10966             (
10967              p_return_status            =>      l_return_status,
10968              p_rule_validation_status   =>      l_rule_validation_status,
10969              p_budget_revision_id       =>      p_budget_revision_id,
10970              p_rule_id                  =>      c_Brrule_Rec.rule_id,
10971              p_rule_type                =>      c_Brrule_Rec.rule_type,
10972              p_apply_account_set_flag   =>      c_Brrule_Rec.apply_account_set_flag,
10973              p_balance_account_set_flag =>      c_Brrule_Rec.balance_account_set_flag,
10974              p_segment_name             =>      c_rule_seg.segment_name,
10975              p_application_column_name  =>      c_rule_seg.application_column_name,
10976              p_chart_of_accounts_id     =>      l_chart_of_accounts_id
10977            );
10978 
10979            if l_rule_validation_status = 'F' then
10980               l_rule_validation_status := 'F';
10981            end if;
10982           End Loop;
10983          end if;
10984         end if;
10985 
10986 /* Bug No 2133484 Start */
10987 --- Commented for the fix done.
10988 /*      if l_rule_validation_status = 'S' then
10989           Select count(*) into l_ctr
10990           from psb_rule_transaction_type
10991           where rule_id = c_Brrule_Rec.rule_id
10992           and transaction_type = l_transaction_type
10993           -- Following 1 line added for Bug # 2123930
10994           and enable_flag = 'Y';
10995 
10996           IF l_ctr > 0 THEN
10997              l_rule_validation_status := 'S';
10998           Else
10999              l_rule_validation_status := 'F';
11000           End if;
11001         end if;*/
11002 /* Bug No 2133484 End */
11003 
11004       /*For Bug No : 2129723 Start*/
11005       END;
11006       ELSE
11007       BEGIN
11008         l_rule_validation_status := 'S';
11009       END;
11010       END IF;
11011       /*For Bug No : 2129723 End*/
11012 
11013       --
11014        IF l_rule_validation_status = 'F' THEN
11015         /*For Bug No : 2125969 Start*/
11016         --commented because of the error has to be inserted into the table
11017         --irrespective of the threshold level
11018         --IF l_severity_level > l_constraint_threshold  THEN    -- Absolute
11019         /*For Bug No : 2125969 End*/
11020 
11021           /*For Bug No : 2150471 Start*/
11022           message_token('RULSET', l_rule_set_name);
11023           message_token('THRESHOLD', l_constraint_threshold);
11024           message_token('SEVERITY', l_severity_level);
11025           /*For Bug No : 2150471 End*/
11026           message_token('RULNAME', c_Brrule_Rec.name);
11027           message_token('RULETYPE', c_Brrule_Rec.rule_type);
11028           add_message('PSB', 'PSB_REVISION_RULE_VIOLATION');
11029 
11030           l_description := FND_MSG_PUB.Get
11031                           (p_encoded => FND_API.G_FALSE);
11032           FND_MSG_PUB.Delete_Msg;
11033 
11034           /*For Bug No : 2150471 Start*/
11035           l_apply_message := NULL;
11036           l_balance_message := NULL;
11037           --Pls. do not change the formatted new line characters below.
11038           --as AD coding standards doesn't allow chr(10), it has been changed
11039           FOR l_index IN 1..g_no_ccids LOOP
11040             SELECT concatenated_segments INTO l_con_segments
11041               FROM GL_CODE_COMBINATIONS_KFV
11042              WHERE code_combination_id = g_ccid_rec(l_index).ccid;
11043             IF (NVL(g_ccid_rec(l_index).apply_balance_flag,'A') = 'A') THEN
11044               l_apply_message := l_apply_message||fnd_global.local_chr(10)||l_con_segments;
11045             ELSIF (g_ccid_rec(l_index).apply_balance_flag = 'B') THEN
11046               l_balance_message := l_balance_message||fnd_global.local_chr(10)||l_con_segments;
11047             END IF;
11048           END LOOP;
11049 
11050           l_description := l_description||l_apply_message;
11051           IF (c_Brrule_Rec.rule_type ='BALANCE') THEN
11052             add_message('PSB', 'PSB_REV_RULE_BALANCE_ACCTS');
11053             l_apply_message := FND_MSG_PUB.Get
11054                                (p_encoded => FND_API.G_FALSE);
11055             FND_MSG_PUB.Delete_Msg;
11056             l_description := l_description||fnd_global.local_chr(10)||l_apply_message||l_balance_message;
11057           END IF;
11058           /*For Bug No : 2150471 End*/
11059 
11060 
11061           insert into PSB_ERROR_MESSAGES
11062                  (Concurrent_Request_ID,
11063                   Process_ID,
11064                   Source_Process,
11065                   Description,
11066                   Creation_Date,
11067                   Created_By)
11068           values (FND_GLOBAL.CONC_REQUEST_ID,
11069                   p_budget_revision_id,
11070                   'BUDGET_REVISION',
11071                   l_description,
11072                   sysdate,
11073                   FND_GLOBAL.USER_ID);
11074         /*For Bug No : 2125969 Start*/
11075         --END IF;
11076         /*For Bug No : 2125969 Start*/
11077       End IF;
11078       --
11079       IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
11080          raise FND_API.G_EXC_ERROR;
11081       END IF;
11082 
11083 
11084       if (l_rule_validation_status = 'F'
11085       /*For Bug No : 2125969 Start*/
11086         --If severity is greater than or equal to thresold, wee need to treat it as an error
11087          AND l_severity_level >= l_constraint_threshold)
11088       /*For Bug No : 2125969 Endt*/   then
11089          p_validation_status := 'F';
11090       end if;
11091     END;
11092   END LOOP;
11093   --
11094   -- Initialize API return status to success
11095 
11096   p_return_status := FND_API.G_RET_STS_SUCCESS;
11097   --
11098 EXCEPTION
11099   --
11100   WHEN FND_API.G_EXC_ERROR THEN
11101     p_return_status := FND_API.G_RET_STS_ERROR;
11102   --
11103   WHEN OTHERS THEN
11104      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11105   --
11106 END Apply_Revision_Rules;
11107 
11108 
11109 /*===========================================================================+
11110  |                        PROCEDURE Apply_Detail_Revision_Rules              |
11111  +===========================================================================*/
11112 
11113 PROCEDURE Apply_Detail_Revision_Rules
11114 (
11115   p_return_status               OUT  NOCOPY  VARCHAR2,
11116   p_rule_validation_status      OUT  NOCOPY  VARCHAR2,
11117   p_budget_revision_id          IN   NUMBER,
11118   p_rule_id                     IN   NUMBER,
11119   p_rule_type                   IN   VARCHAR2,
11120   p_apply_account_set_flag      IN   VARCHAR2,
11121   p_balance_account_set_flag    IN   VARCHAR2,
11122   p_segment_name                IN   VARCHAR2,
11123   p_application_column_name     IN   VARCHAR2,
11124   p_chart_of_accounts_id        IN   NUMBER
11125 )
11126 IS
11127 --
11128   l_return_status               VARCHAR2(1);
11129   l_apply_cr                    NUMBER := 0;
11130   l_apply_dr                    NUMBER := 0;
11131   l_balance_cr                  NUMBER := 0;
11132   l_balance_dr                  NUMBER := 0;
11133   l_seg_apply_cr                fnd_flex_ext.SegmentArray;
11134   l_seg_apply_dr                fnd_flex_ext.SegmentArray;
11135   l_seg_balance_cr              fnd_flex_ext.SegmentArray;
11136   l_seg_balance_dr              fnd_flex_ext.SegmentArray;
11137 
11138   l_cnt                         NUMBER := 0;
11139   --
11140   -- CCIDs assigned to the Revision Rule : select CCIDs that also belong to the Budget Group Hierarchy
11141   --
11142 /*For Bug No : 2127951 Start*/
11143   TYPE SegmentCurTyp IS REF CURSOR;
11144   l_seg_cur             SegmentCurTyp;
11145   TYPE AcctCurTyp IS REF CURSOR;
11146   l_acct_cur            AcctCurTyp;
11147   l_seg_sql             VARCHAR2(3000);
11148   l_acct_sql            VARCHAR2(3000);
11149   l_acct_sql_temp       VARCHAR2(3000);
11150   l_seg_value           VARCHAR2(30);
11151   l_cc_id               NUMBER(15);
11152   l_apply_balance_flag  VARCHAR2(5);
11153   --
11154 /*For Bug No : 2127951 End*/
11155 /*For Bug No : 2150471 Start*/
11156   l_temp                NUMBER;
11157 /*For Bug No : 2150471 End*/
11158 
11159   cursor c_CCID is
11160      SELECT  ba.code_combination_id, sr.apply_balance_flag
11161      FROM    PSB_BUDGET_ACCOUNTS ba, PSB_SET_RELATIONS_V sr
11162      WHERE   ba.account_position_set_id = sr.account_position_set_id
11163      AND     sr.account_or_position_type = 'A'
11164      AND     sr.rule_id = p_rule_id
11165 /* Bug No 2135165 Start */
11166         AND  ba.code_combination_id in (SELECT bra.code_combination_id
11167                    FROM PSB_BUDGET_REVISION_LINES brl, PSB_BUDGET_REVISION_ACCOUNTS bra
11168                   WHERE brl.budget_revision_id = p_budget_revision_id
11169                     AND bra.budget_revision_acct_line_id = brl.budget_revision_acct_line_id
11170                     AND EXISTS (SELECT 1 FROM PSB_ENTITY_ASSIGNMENT ea, gl_sets_of_books sob, gl_periods_v gp
11171                                  WHERE ea.entity_set_id = g_brr_rule_set_id
11172                                    AND ea.entity_id = p_rule_id
11173                                    AND sob.set_of_books_id = g_brr_sob_id
11174                                    AND gp.period_set_name = sob.period_set_name
11175                                    AND gp.adjustment_period_flag = 'N'
11176                                    AND gp.period_name = bra.gl_period_name
11177                                    AND (((ea.effective_start_date <= gp.end_date)
11178                                         AND (ea.effective_end_date IS NULL))
11179                                         OR ((ea.effective_start_date BETWEEN gp.start_date AND gp.end_date)
11180                                         OR (ea.effective_end_date BETWEEN gp.start_date AND gp.end_date)
11181                                         OR ((ea.effective_start_date < gp.start_date)
11182                                                 AND (ea.effective_end_date > gp.end_date))))
11183                                 )
11184                   );
11185 /* Bug No 2135165 End */
11186 
11187   --
11188 BEGIN
11189 
11190   PSB_Flex_Mapping_PVT.Flex_Info(
11191                                  p_flex_code         =>  p_chart_of_accounts_id,
11192                                  p_return_status     =>  l_return_status
11193                                 );
11194   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
11195     --
11196     raise FND_API.G_EXC_ERROR;
11197     --
11198   END IF;
11199   --
11200   --
11201  /*For Bug No : 2127951 Start*/
11202  --the following code has been commented because of the
11203  --initialization has to be done for each segment group
11204   /*FOR l_index IN 1..PSB_Flex_Mapping_PVT.g_num_segs  LOOP
11205    l_seg_apply_cr(l_index) := 0;
11206    l_seg_balance_cr(l_index) := 0;
11207    l_seg_apply_dr(l_index) := 0;
11208    l_seg_balance_dr(l_index) := 0;
11209   END LOOP;*/
11210   --
11211   /*For Bug No : 2127951 End*/
11212 
11213 IF p_rule_type = 'BALANCE' THEN
11214       --
11215   /*For Bug No : 2127951 Start*/
11216   IF (p_application_column_name IS NULL) THEN
11217   /*For Bug No : 2127951 End*/
11218      FOR c_CCID_Rec IN c_CCID LOOP
11219 
11220          Process_Balance_Rule(
11221                               p_budget_revision_id        =>  p_budget_revision_id,
11222                               p_rule_id                   =>  p_rule_id,
11223                               p_apply_account_set_flag    =>  p_apply_account_set_flag,
11224                               p_balance_account_set_flag  =>  p_balance_account_set_flag,
11225                               p_segment_name              =>  p_segment_name,
11226                               p_application_column_name   =>  p_application_column_name,
11227                               p_ccid                      =>  c_CCID_Rec.code_combination_id,
11228                               p_apply_balance_flag        =>  c_CCID_Rec.apply_balance_flag,
11229                               p_apply_cr                  =>  l_apply_cr,
11230                               p_apply_dr                  =>  l_apply_dr,
11231                               p_balance_cr                =>  l_balance_cr,
11232                               p_balance_dr                =>  l_balance_dr,
11233                               p_seg_apply_cr              =>  l_seg_apply_cr,
11234                               p_seg_apply_dr              =>  l_seg_apply_dr,
11235                               p_seg_balance_cr            =>  l_seg_balance_cr,
11236                               p_seg_balance_dr            =>  l_seg_balance_dr,
11237                               p_return_status             =>  l_return_status
11238                              );
11239       END LOOP;
11240       --
11241       /*For Bug No : 2127951 Start*/
11242 
11243       IF p_application_column_name IS null THEN
11244         --
11245         IF ((l_apply_dr = l_balance_cr) AND (l_apply_cr = l_balance_dr))  THEN
11246           p_rule_validation_status  := 'S';
11247         ELSE
11248           p_rule_validation_status := 'F';
11249         END IF;
11250         --
11251       END IF;
11252 
11253   ELSIF (p_application_column_name IS NOT NULL) THEN
11254 
11255      l_seg_sql := 'SELECT DISTINCT glcc.'||p_application_column_name||
11256                     ' FROM gl_code_combinations glcc,'||
11257                           ' (SELECT DISTINCT bra.code_combination_id '||
11258                              ' FROM PSB_BUDGET_REVISION_LINES brl,'||
11259                                   ' PSB_BUDGET_REVISION_ACCOUNTS bra '||
11260                             ' WHERE brl.budget_revision_id = '||to_char(p_budget_revision_id)||
11261                               ' AND bra.budget_revision_acct_line_id = brl.budget_revision_acct_line_id'||
11262                           ' ) rcc'||
11263                    ' WHERE glcc.code_combination_id = rcc.code_combination_id';
11264 
11265      -- Bug 5030405 used bind variables in the following string
11266      l_acct_sql_temp := 'SELECT ba.code_combination_id, sr.apply_balance_flag '||
11267                                     ' FROM PSB_BUDGET_ACCOUNTS ba, PSB_SET_RELATIONS_V sr'||
11268                                    ' WHERE ba.account_position_set_id = sr.account_position_set_id'||
11269                                      ' AND sr.account_or_position_type = '||''''||'A'||''''||
11270                                      ' AND sr.rule_id = :b_rule_id'||
11271                                      ' AND ba.code_combination_id in (SELECT bra.code_combination_id '||
11272                                                    ' FROM PSB_BUDGET_REVISION_LINES brl,'||
11273                                                         ' PSB_BUDGET_REVISION_ACCOUNTS bra'||
11274                                                         ' WHERE brl.budget_revision_id = :b_budget_revision_id'||
11275                                                         ' AND bra.budget_revision_acct_line_id = brl.budget_revision_acct_line_id'||
11276                                                         ' AND EXISTS (SELECT 1 '||
11277                                                                       ' FROM PSB_ENTITY_ASSIGNMENT ea, gl_sets_of_books sob, gl_periods_v gp'||
11278                                                                       ' WHERE ea.entity_set_id = :b_brr_rule_set_id'||
11279                                                                       ' AND ea.entity_id = :b_rule_id'||
11280                                                                       ' AND sob.set_of_books_id = :b_brr_sob_id'||
11281                                                                       ' AND gp.period_set_name = sob.period_set_name'||
11282                                                                       ' AND gp.adjustment_period_flag = '||''''||'N' ||''''||
11283                                                                       ' AND gp.period_name = bra.gl_period_name'||
11284                                                                       ' AND (((ea.effective_start_date <= gp.end_date)'||
11285                                                                       ' AND (ea.effective_end_date IS NULL))'||
11286                                                                       ' OR ((ea.effective_start_date BETWEEN gp.start_date AND gp.end_date)'||
11287                                                                       ' OR (ea.effective_end_date BETWEEN gp.start_date AND gp.end_date)'||
11288                                                                       ' OR ((ea.effective_start_date < gp.start_date)'||
11289                                                                       ' AND (ea.effective_end_date > gp.end_date))))'||
11290 
11291                                                    ' ))'||
11292 
11293                                      ' AND EXISTS (SELECT 1 '||
11294                                                    ' FROM PSB_BUDGET_REVISION_LINES brl,'||
11295                                                         ' PSB_BUDGET_REVISION_ACCOUNTS bra'||
11296                                                         ' WHERE brl.budget_revision_id = :b_budget_revision_id'||
11297                                                         ' AND bra.budget_revision_acct_line_id = brl.budget_revision_acct_line_id'||
11298                                                         ' AND bra.code_combination_id = ba.code_combination_id)'||
11299                                      ' AND EXISTS (SELECT 1'||
11300                                                    ' FROM gl_code_combinations glcc'||
11301                                                    ' WHERE glcc.code_combination_id = ba.code_combination_id';
11302 
11303 
11304      OPEN l_seg_cur FOR l_seg_sql;
11305 
11306      LOOP
11307        FETCH l_seg_cur INTO l_seg_value;
11308        EXIT WHEN l_seg_cur%NOTFOUND;
11309 
11310        --for every segment initialize the array to zero values
11311        FOR l_index IN 1..PSB_Flex_Mapping_PVT.g_num_segs  LOOP
11312          l_seg_apply_cr(l_index) := 0;
11313          l_seg_balance_cr(l_index) := 0;
11314          l_seg_apply_dr(l_index) := 0;
11315          l_seg_balance_dr(l_index) := 0;
11316        END LOOP;
11317 
11318 
11319        l_acct_sql := l_acct_sql_temp||' AND glcc.'||p_application_column_name||' = '||''''||l_seg_value||''''||')';
11320 
11321        -- Bug 5030405 used using clause below
11322        OPEN l_acct_cur FOR l_acct_sql
11323        USING p_rule_id,
11324              p_budget_revision_id,
11325              g_brr_rule_set_id,
11326              p_rule_id,
11327              g_brr_sob_id,
11328              p_budget_revision_id;
11329        LOOP
11330          FETCH l_acct_cur INTO l_cc_id, l_apply_balance_flag;
11331          EXIT WHEN l_acct_cur%NOTFOUND;
11332 
11333          --Process each cc_id within the segment
11334                   Process_Balance_Rule(
11335                               p_budget_revision_id        =>  p_budget_revision_id,
11336                               p_rule_id                   =>  p_rule_id,
11337                               p_apply_account_set_flag    =>  p_apply_account_set_flag,
11338                               p_balance_account_set_flag  =>  p_balance_account_set_flag,
11339                               p_segment_name              =>  p_segment_name,
11340                               p_application_column_name   =>  p_application_column_name,
11341                               p_ccid                      =>  l_cc_id,
11342                               p_apply_balance_flag        =>  l_apply_balance_flag,
11343                               p_apply_cr                  =>  l_apply_cr,
11344                               p_apply_dr                  =>  l_apply_dr,
11345                               p_balance_cr                =>  l_balance_cr,
11346                               p_balance_dr                =>  l_balance_dr,
11347                               p_seg_apply_cr              =>  l_seg_apply_cr,
11348                               p_seg_apply_dr              =>  l_seg_apply_dr,
11349                               p_seg_balance_cr            =>  l_seg_balance_cr,
11350                               p_seg_balance_dr            =>  l_seg_balance_dr,
11351                               p_return_status             =>  l_return_status
11352                              );
11353 
11354        END LOOP;
11355        CLOSE l_acct_cur;
11356        --End of the segment validation cursor
11357        FOR l_index IN 1..PSB_Flex_Mapping_PVT.g_num_segs  LOOP
11358          IF ( l_seg_apply_dr(l_index) = l_seg_balance_cr(l_index) AND l_seg_apply_cr(l_index) = l_seg_balance_dr(l_index) )  THEN
11359            IF (p_rule_validation_status IS NULL OR p_rule_validation_status  <> 'F' ) THEN
11360              p_rule_validation_status  := 'S';
11361            END IF;
11362          ELSE
11363             p_rule_validation_status :=  'F';
11364             EXIT;
11365          END IF;
11366 
11367        END LOOP;
11368        --end of result validation loop
11369          IF (p_rule_validation_status  = 'F') THEN
11370            EXIT;
11371          END IF;
11372      END LOOP;
11373      CLOSE l_seg_cur;
11374      --End of the cc_id validation cursor
11375 
11376   END IF;
11377   --commented the following code because of the validations are to be done
11378   --seperately as per the application_column_name with in segment
11379   /*  IF p_application_column_name IS null THEN
11380       --
11381       IF ((l_apply_dr = l_balance_cr) AND (l_apply_cr = l_balance_dr))  THEN
11382         p_rule_validation_status  := 'S';
11383       ELSE
11384         p_rule_validation_status := 'F';
11385       END IF;
11386       --
11387     ELSIF p_application_column_name IS NOT null THEN
11388       --
11389       FOR l_index IN 1..PSB_Flex_Mapping_PVT.g_num_segs  LOOP
11390          IF ( l_seg_apply_dr(l_index) = l_seg_balance_cr(l_index) AND l_seg_apply_cr(l_index) = l_seg_balance_dr(l_index) )  THEN
11391            if p_rule_validation_status = 'F' then
11392               p_rule_validation_status := 'F';
11393            else
11394               p_rule_validation_status  := 'S';
11395            end if;
11396          ELSE
11397            p_rule_validation_status :=  'F';
11398            exit;
11399          END IF;
11400       END LOOP;
11401       --
11402     END IF;*/
11403       --
11404   /*For Bug No : 2127951 End*/
11405 
11406 ELSIF p_rule_type IN ('PERMANENT', 'TEMPORARY') THEN
11407     --
11408     FOR c_CCID_Rec in c_CCID loop
11409         /*For Bug No : 2150471 Start*/
11410         --commented due to performance issues
11411         /*select count(bra.code_combination_id) into l_cnt
11412         from psb_budget_revision_accounts bra, psb_budget_revision_lines brl
11413         where bra.budget_revision_acct_line_id = brl.budget_revision_acct_line_id
11414         and brl.budget_revision_id = p_budget_revision_id
11415         and bra.code_combination_id = c_CCID_Rec.code_combination_id;*/
11416 
11417         l_temp := 1;
11418         LOOP
11419           EXIT WHEN ( (l_temp > g_no_ccids) OR (c_CCID_Rec.code_combination_id = g_ccid_rec(l_temp).ccid) );
11420           l_temp := l_temp + 1;
11421         END LOOP;
11422         IF (l_temp > g_no_ccids) THEN
11423           g_no_ccids := g_no_ccids + 1;
11424           g_ccid_rec(g_no_ccids).ccid := c_CCID_Rec.code_combination_id;
11425         END IF;
11426         --The following code is commented and calling it from out side of the loop
11427            /*Process_Perm_Temp_Rule(
11428                               p_budget_revision_id       =>  p_budget_revision_id,
11429                               p_rule_id                  =>  p_rule_id,
11430                               p_rule_type                =>  p_rule_type,
11431                               p_apply_account_set_flag   =>  p_apply_account_set_flag,
11432                               p_ccid                     =>  c_CCID_Rec.code_combination_id,
11433                               p_apply_balance_flag       =>  c_CCID_Rec.apply_balance_flag,
11434                               p_rule_validation_status   =>  p_rule_validation_status,
11435                               p_return_status            =>  l_return_status
11436                             );*/
11437         /*For Bug No : 2150471 End*/
11438     END LOOP;
11439         /*For Bug No : 2150471 Start*/
11440         Process_Perm_Temp_Rule(
11441                               p_budget_revision_id       =>  p_budget_revision_id,
11442                               p_rule_id                  =>  p_rule_id,
11443                               p_rule_type                =>  p_rule_type,
11444                               p_apply_account_set_flag   =>  NULL,
11445                               p_ccid                     =>  NULL,
11446                               p_apply_balance_flag       =>  NULL,
11447                               p_rule_validation_status   =>  p_rule_validation_status,
11448                               p_return_status            =>  l_return_status
11449                             );
11450         /*For Bug No : 2150471 End*/
11451     if p_rule_validation_status = 'F' then
11452       p_rule_validation_status := 'F';
11453     else
11454       p_rule_validation_status := 'S';
11455     end if;
11456 
11457     --
11458 END IF;
11459   --
11460 
11461   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
11462       raise FND_API.G_EXC_ERROR;
11463   END IF;
11464   --
11465 EXCEPTION
11466    --
11467    WHEN FND_API.G_EXC_ERROR THEN
11468      p_return_status := FND_API.G_RET_STS_ERROR;
11469    --
11470    WHEN OTHERS THEN
11471      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11472    --
11473 END Apply_Detail_Revision_Rules;
11474 
11475 
11476 /*===========================================================================+
11477  |                        PROCEDURE Process_Balance_Rule                     |
11478  +===========================================================================*/
11479 
11480 PROCEDURE Process_Balance_Rule
11481 (
11482   p_return_status               OUT  NOCOPY             VARCHAR2,
11483   p_apply_cr                    IN  OUT  NOCOPY         NUMBER,
11484   p_apply_dr                    IN  OUT  NOCOPY         NUMBER,
11485   p_balance_cr                  IN  OUT  NOCOPY         NUMBER,
11486   p_balance_dr                  IN  OUT  NOCOPY         NUMBER,
11487   p_seg_apply_cr                IN  OUT  NOCOPY         fnd_flex_ext.SegmentArray,
11488   p_seg_apply_dr                IN  OUT  NOCOPY         fnd_flex_ext.SegmentArray,
11489   p_seg_balance_cr              IN  OUT  NOCOPY         fnd_flex_ext.SegmentArray,
11490   p_seg_balance_dr              IN  OUT  NOCOPY         fnd_flex_ext.SegmentArray,
11491   p_budget_revision_id          IN              NUMBER,
11492   p_rule_id                     IN              NUMBER,
11493   p_apply_account_set_flag      IN              VARCHAR2,
11494   p_balance_account_set_flag    IN              VARCHAR2,
11495   p_segment_name                IN              VARCHAR2,
11496   p_application_column_name     IN              VARCHAR2,
11497   p_ccid                        IN              NUMBER,
11498   p_apply_balance_flag          IN              VARCHAR2
11499 )
11500 IS
11501   -- Compute Sum of BR Account Lines (all Account Sets assigned to the Rule)
11502   l_revision_type               VARCHAR2(1);
11503   l_revision_value_type         VARCHAR2(1);
11504   l_account_type                VARCHAR2(1);
11505   l_revision_amount             NUMBER;
11506   l_budget_balance              NUMBER;
11507   l_chart_of_accounts_id        NUMBER;
11508   l_temp                        NUMBER;
11509 /* Bug No 2156263 Start */
11510   l_num                         NUMBER;
11511 /* Bug No 2156263 End */
11512 
11513   cursor c_SumAll is
11514      SELECT a.revision_type, a.revision_value_type, a.revision_amount,
11515              a.account_type, a.budget_balance
11516      FROM PSB_BUDGET_REVISION_LINES b,
11517           PSB_BUDGET_REVISION_ACCOUNTS a
11518      WHERE b.budget_revision_id = p_budget_revision_id
11519        AND a.budget_revision_acct_line_id = b.budget_revision_acct_line_id
11520        AND a.code_combination_id = p_ccid
11521        /*For Bug No : 2161125 Start*/
11522        AND a.position_id IS NULL;
11523        /*For Bug No : 2161125 End*/
11524   --
11525 BEGIN
11526   --
11527 
11528   FOR c_sumall_rec IN c_sumall LOOP
11529      --
11530      l_revision_type := c_sumall_rec.revision_type ;
11531      l_revision_value_type := c_sumall_rec.revision_value_type;
11532      l_revision_amount := nvl(c_sumall_rec.revision_amount,0);
11533      l_account_type := c_sumall_rec.account_type;
11534      l_budget_balance := nvl(c_sumall_rec.budget_balance,0);
11535      l_temp := 1;
11536      --
11537      IF l_revision_value_type = 'P' THEN
11538        l_temp := nvl(l_budget_balance, 0) / 100;
11539      END IF;
11540      --
11541 
11542 /* Bug No 2144364 Start */
11543     if l_revision_amount < 0 then
11544       if l_revision_type = 'I' then
11545          l_revision_type := 'D';
11546       else
11547          l_revision_type := 'I';
11548       end if;
11549 
11550       l_revision_amount := abs(l_revision_amount);
11551     end if;
11552 /* Bug No 2144364 End */
11553 
11554     /*For Bug No : 2150471 Start*/
11555     IF ( ((p_apply_balance_flag = 'A') AND
11556           ((l_revision_type = 'I' AND p_apply_account_set_flag <> 'D')
11557             OR (l_revision_type = 'D' AND p_apply_account_set_flag <> 'I'))) OR
11558          ((p_apply_balance_flag = 'B') AND
11559           ((l_revision_type = 'I' AND p_balance_account_set_flag <> 'D')
11560             OR (l_revision_type = 'D' AND p_balance_account_set_flag <> 'I'))) ) THEN
11561     BEGIN
11562 /* Bug No 2156263 Start */
11563 -- Changed the variable name from 'l_temp' to 'l_num'
11564 -- since 'l_temp' is being used for later on in this package for some purpose.
11565       l_num := 1;
11566       LOOP
11567         EXIT WHEN ( (l_num > g_no_ccids) OR ((p_ccid = g_ccid_rec(l_num).ccid) AND (p_apply_balance_flag = g_ccid_rec(l_num).apply_balance_flag)) );
11568         l_num := l_num + 1;
11569       END LOOP;
11570       IF (l_num > g_no_ccids) THEN
11571 /* Bug No 2156263 End */
11572         g_no_ccids := g_no_ccids + 1;
11573         g_ccid_rec(g_no_ccids).ccid := p_ccid;
11574         g_ccid_rec(g_no_ccids).apply_balance_flag := p_apply_balance_flag;
11575       END IF;
11576     END;
11577     END IF;
11578     /*For Bug No : 2150471 End*/
11579 
11580      /*For Bug No : 2125969 Start*/
11581      --p_apply_account_set_flag, p_balance_account_set_flag variables
11582      --are added in the if conditions
11583      IF p_application_column_name is null THEN
11584         --
11585         IF p_apply_balance_flag = 'A' then
11586           IF l_account_type IN('A','E') THEN
11587           --
11588             IF (l_revision_type = 'I' AND p_apply_account_set_flag <> 'D') THEN
11589               p_apply_dr := p_apply_dr + nvl(l_revision_amount, 0) * l_temp;
11590             ELSIF (l_revision_type = 'D' AND p_apply_account_set_flag <> 'I') THEN
11591               p_apply_cr := p_apply_cr + nvl(l_revision_amount, 0) * l_temp;
11592             END IF;
11593             --
11594           ELSIF l_account_type IN('L','O','R') THEN
11595             IF (l_revision_type = 'I' AND p_apply_account_set_flag <> 'D') THEN
11596               p_apply_cr := p_apply_cr + nvl(l_revision_amount, 0) * l_temp;
11597             ELSIF (l_revision_type = 'D' AND p_apply_account_set_flag <> 'I') THEN
11598               p_apply_dr := p_apply_dr + nvl(l_revision_amount, 0) * l_temp;
11599             END IF;
11600           END IF;
11601         ELSIF p_apply_balance_flag = 'B' THEN
11602           IF l_account_type IN('A','E') THEN
11603             IF (l_revision_type = 'I' AND p_balance_account_set_flag <> 'D') THEN
11604               p_balance_dr := p_balance_dr + nvl(l_revision_amount, 0) * l_temp;
11605             ELSIF (l_revision_type = 'D' AND p_balance_account_set_flag <> 'I') THEN
11606               p_balance_cr := p_balance_cr + nvl(l_revision_amount, 0) * l_temp;
11607             END IF;
11608           ELSIF l_account_type IN('L','O','R') THEN
11609             IF (l_revision_type = 'I' AND p_balance_account_set_flag <> 'D') THEN
11610               p_balance_cr := p_balance_cr + nvl(l_revision_amount, 0) * l_temp;
11611             ELSIF (l_revision_type = 'D' AND p_balance_account_set_flag <> 'I') THEN
11612               p_balance_dr := p_balance_dr + nvl(l_revision_amount, 0) * l_temp;
11613             END IF;
11614           END IF;
11615         END IF;
11616      ELSIF p_application_column_name is not null THEN
11617      --
11618      FOR i in 1..PSB_Flex_Mapping_PVT.g_num_segs LOOP
11619       --
11620       IF PSB_Flex_Mapping_PVT.g_seg_name(i) = p_application_column_name THEN
11621       --
11622         IF p_apply_balance_flag = 'A' then
11623           IF l_account_type IN('A','E') THEN
11624             IF (l_revision_type = 'I' AND p_apply_account_set_flag <> 'D') THEN
11625               p_seg_apply_dr(i) := p_seg_apply_dr(i) + nvl(l_revision_amount, 0) * l_temp;
11626             ELSIF (l_revision_type = 'D' AND p_apply_account_set_flag <> 'I') THEN
11627               p_seg_apply_cr(i) := p_seg_apply_cr(i) + nvl(l_revision_amount, 0) * l_temp;
11628             END IF;
11629           ELSIF l_account_type IN('L','O','R') THEN
11630             IF (l_revision_type = 'I' AND p_apply_account_set_flag <> 'D') THEN
11631               p_seg_apply_cr(i) := p_seg_apply_cr(i) + nvl(l_revision_amount, 0) * l_temp;
11632             ELSIF (l_revision_type = 'D' AND p_apply_account_set_flag <> 'I') THEN
11633               p_seg_apply_dr(i) := p_seg_apply_dr(i) + nvl(l_revision_amount, 0) * l_temp;
11634             END IF;
11635           END IF;
11636         ELSIF p_apply_balance_flag = 'B' THEN
11637           IF l_account_type IN('A','E') THEN
11638             IF (l_revision_type = 'I' AND p_balance_account_set_flag <> 'D') THEN
11639               p_seg_balance_dr(i) := p_seg_balance_dr(i) + nvl(l_revision_amount, 0) * l_temp;
11640             ELSIF (l_revision_type = 'D' AND p_balance_account_set_flag <> 'I') THEN
11641               p_seg_balance_cr(i) := p_seg_balance_cr(i) + nvl(l_revision_amount, 0) * l_temp;
11642             END IF;
11643           ELSIF l_account_type IN('L','O','R') THEN
11644             IF (l_revision_type = 'I' AND p_balance_account_set_flag <> 'D') THEN
11645               p_seg_balance_cr(i) := p_seg_balance_cr(i) + nvl(l_revision_amount, 0) * l_temp;
11646             ELSIF (l_revision_type = 'D' AND p_balance_account_set_flag <> 'I') THEN
11647               p_seg_balance_dr(i) := p_seg_balance_dr(i) + nvl(l_revision_amount, 0) * l_temp;
11648             END IF;
11649           END IF;
11650         END IF;
11651       --
11652       END IF;
11653       --
11654      END LOOP;
11655      --
11656      END IF;
11657      --
11658      /*For Bug No : 2125969 End*/
11659   END LOOP;
11660   --
11661 
11662 END Process_Balance_Rule;
11663 
11664 /*===========================================================================+
11665  |                        PROCEDURE Process_Perm_Temp_Rule                   |
11666  +===========================================================================*/
11667 
11668 PROCEDURE Process_Perm_Temp_Rule
11669 (
11670   p_return_status               OUT  NOCOPY  VARCHAR2,
11671   p_rule_validation_status      OUT  NOCOPY  VARCHAR2,
11672   p_budget_revision_id          IN   NUMBER,
11673   p_rule_id                     IN   NUMBER,
11674   p_rule_type                   IN   VARCHAR2,
11675   p_apply_account_set_flag      IN   VARCHAR2,
11676   p_ccid                        IN   VARCHAR2,
11677   p_apply_balance_flag          IN   VARCHAR2
11678 )
11679 IS
11680   --
11681   --Compute Sum of BR Account Lines (all Account Sets assigned to the Rule)
11682   cursor c_type is
11683       SELECT DECODE(permanent_revision, 'Y', 'PERMANENT', 'TEMPORARY') permanent_revision
11684       FROM   PSB_BUDGET_REVISIONS
11685       WHERE  budget_revision_id = p_budget_revision_id;
11686   l_permanent_revision          VARCHAR2(30);
11687   --
11688 BEGIN
11689   --
11690 
11691   FOR c_type_rec IN c_type LOOP
11692     l_permanent_revision := c_type_rec.permanent_revision;
11693   END LOOP;
11694 
11695   --
11696   IF l_permanent_revision = p_rule_type THEN
11697     p_rule_validation_status := 'S';
11698   ELSE
11699     p_rule_validation_status := 'F';
11700   END IF;
11701 
11702 END Process_Perm_Temp_Rule;
11703 
11704 /* Budget Revision Rules Enhancements End */
11705 
11706 /* ----------------------------------------------------------------------- */
11707 /* Bug No 1808330 Start */
11708 
11709 PROCEDURE Create_Note
11710 ( p_return_status    OUT NOCOPY VARCHAR2
11711 , p_account_line_id  IN         NUMBER
11712 , p_position_line_id IN         NUMBER
11713 , p_note             IN         VARCHAR2
11714 , p_flex_code        IN         NUMBER  -- Bug#4571412
11715 , p_cc_id            IN         NUMBER DEFAULT NULL -- Bug#4675858
11716 ) IS
11717 
11718   l_change_note      VARCHAR2(1);
11719   l_note_id          NUMBER;
11720   l_msg_count        NUMBER;
11721   l_msg_data         VARCHAR2(2000);
11722 
11723   -- Bug#4571412
11724   l_concat_segments     VARCHAR2(2000);
11725   l_code_combination_id NUMBER;
11726   l_message_text        VARCHAR2(4000);
11727 
11728   cursor c_acct_note_id is
11729     select note_id from PSB_BUDGET_REVISION_ACCOUNTS where budget_revision_acct_line_id = p_account_line_id;
11730 
11731   -- Bug#4571412
11732   -- Get the code_combination_id.
11733   CURSOR c_pos_note_id
11734   IS
11735   SELECT pbrp.note_id,
11736          pbra.code_combination_id
11737   FROM PSB_BUDGET_REVISION_POSITIONS pbrp, PSB_BUDGET_REVISION_ACCOUNTS pbra
11738   WHERE pbrp.budget_revision_pos_line_id = p_position_line_id
11739   AND pbrp.position_id = pbra.position_id;
11740 
11741 BEGIN
11742 
11743   FND_PROFILE.GET
11744      (name => 'PSB_EDIT_CREATE_NOTES',
11745       val => l_change_note);
11746 
11747   if nvl(l_change_note, 'Y') = 'Y' then
11748   begin
11749 
11750     if p_account_line_id is not null and p_position_line_id is null then
11751        for c_acct_note_rec in c_acct_note_id loop
11752          l_note_id             := c_acct_note_rec.note_id;
11753 	 l_code_combination_id := p_cc_id ; -- Bug#4675858
11754        end loop;
11755     elsif p_account_line_id is null and p_position_line_id is not null then
11756        for c_pos_note_rec in c_pos_note_id loop
11757          l_note_id := c_pos_note_rec.note_id;
11758 	 l_code_combination_id := c_pos_note_rec.code_combination_id; -- Bug#4675858
11759        end loop;
11760     end if;
11761 
11762     if l_note_id is null then
11763     begin
11764 
11765       Insert into PSB_WS_ACCOUNT_LINE_NOTES
11766         (note_id, note, last_update_date, last_updated_by, last_update_login, created_by, creation_date)
11767       values (psb_ws_account_line_notes_s.nextval, p_note, sysdate, FND_GLOBAL.USER_ID, FND_GLOBAL.LOGIN_ID, FND_GLOBAL.USER_ID, sysdate)
11768       returning note_id into l_note_id;
11769 
11770       if p_account_line_id is not null then
11771          update PSB_BUDGET_REVISION_ACCOUNTS
11772          set note_id = l_note_id
11773          where budget_revision_acct_line_id = p_account_line_id;
11774       elsif p_position_line_id is not null then
11775          update PSB_BUDGET_REVISION_POSITIONS
11776          set note_id = l_note_id
11777          where budget_revision_pos_line_id = p_position_line_id;
11778       end if;
11779 
11780     end;
11781     else
11782       -- Bug#4571412
11783       BEGIN
11784         Update PSB_WS_ACCOUNT_LINE_NOTES
11785 	SET note = note || FND_GLOBAL.NewLine || p_note,
11786 	    last_update_date = sysdate,
11787 	    last_updated_by = FND_GLOBAL.USER_ID,
11788 	    last_update_login = FND_GLOBAL.LOGIN_ID,
11789 	    created_by = FND_GLOBAL.USER_ID,
11790 	    creation_date = sysdate
11791         WHERE note_id = l_note_id;
11792       EXCEPTION
11793         WHEN others THEN
11794           -- Set the global variable to Yes so that
11795           -- CP can be set to Warning status.
11796           PSB_BUDGET_REVISIONS_PVT.g_soft_error_flag := 'Y';
11797 
11798           l_concat_segments
11799             := FND_FLEX_EXT.Get_Segs
11800                ( application_short_name => 'SQLGL'
11801                , key_flex_code          => 'GL#'
11802                , structure_number       => g_flex_code -- Bug#4675858
11803                , combination_id         => l_code_combination_id
11804                );
11805            FND_MESSAGE.SET_NAME('PSB', 'PSB_BR_NOTES_EXCEEDED_LIMIT');
11806            FND_MESSAGE.SET_TOKEN('ACCOUNTING_FLEXFIELD', l_concat_segments);
11807 
11808            l_message_text := FND_MESSAGE.Get;
11809            FND_FILE.PUT_LINE(FND_FILE.LOG, l_message_text);
11810            --
11811       END;
11812     end if;
11813   end;
11814   end if;
11815 
11816   -- Initialize API return status to success
11817 
11818   p_return_status := FND_API.G_RET_STS_SUCCESS;
11819 
11820 
11821 EXCEPTION
11822 
11823    when FND_API.G_EXC_ERROR then
11824      p_return_status := FND_API.G_RET_STS_ERROR;
11825      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
11826                                 p_data  => l_msg_data);
11827 
11828    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
11829      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11830      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
11831                                 p_data  => l_msg_data);
11832 
11833    WHEN OTHERS THEN
11834      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11835 
11836      FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
11837                                 'Create_Note');
11838 
11839 END Create_Note;
11840 /* Bug No 1808330 End */
11841 
11842 /* bug no 3439168 */
11843 PROCEDURE set_position_update_flag
11844 (
11845   x_return_status        OUT  NOCOPY VARCHAR2, -- Bug#4460150
11846   p_position_id          IN          NUMBER
11847 )
11848 IS
11849   l_api_name     VARCHAR2(30) := 'set_update_position_flag';
11850 BEGIN
11851   g_last_update_flag_tbl(p_position_id) := 1;
11852   x_return_status := FND_API.G_RET_STS_SUCCESS;
11853 EXCEPTION
11854   WHEN others THEN
11855     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11856 
11857     FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
11858                                 l_api_name);
11859 
11860 END set_position_update_flag;
11861 /* bug no 3439168 */
11862 
11863 
11864 /* ----------------------------------------------------------------------- */
11865 
11866 END PSB_BUDGET_REVISIONS_PVT;