DBA Data[Home] [Help]

PACKAGE BODY: APPS.CSI_COUNTER_TEMPLATE_PVT

Source


1 PACKAGE BODY CSI_COUNTER_TEMPLATE_PVT AS
2 /* $Header: csivcttb.pls 120.56.12020000.2 2012/07/05 06:06:44 sjawaji ship $ */
3 
4 -- --------------------------------------------------------
5 -- Define global variables
6 -- --------------------------------------------------------
7 
8 G_PKG_NAME CONSTANT VARCHAR2(30):= 'CSI_COUNTER_TEMPLATE_PVT';
9 G_FILE_NAME CONSTANT VARCHAR2(12) := 'csivcttb.pls';
10 
11 PROCEDURE validate_counter_group
12 (
13    p_name               VARCHAR2,
14    p_template_flag      VARCHAR2
15 ) IS
16 
17    l_dummy  VARCHAR2(1);
18 BEGIN
19    IF nvl(p_template_flag,'N') =  'Y' THEN
20       BEGIN
21          SELECT 'x'
22          INTO   l_dummy
23          FROM   cs_csi_counter_groups
24          WHERE  name = p_name
25          AND    template_flag = p_template_flag;
26 
27          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_GRP_DUPLICATE');
28       EXCEPTION
29          WHEN NO_DATA_FOUND THEN
30             NULL;
31          WHEN OTHERS THEN
32             csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_GRP_DUPLICATE');
33       END;
34    END IF;
35 END validate_counter_group;
36 
37 PROCEDURE validate_start_date
38 (
39    p_start_date   DATE
40 ) IS
41 BEGIN
42    IF p_start_date IS NULL THEN
43       CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_STDATE_INVALID');
44    END IF;
45 END validate_start_date;
46 
47 PROCEDURE validate_inventory_item
48 (
49    p_inventory_item_id NUMBER
50 ) IS
51 
52    l_dummy VARCHAR2(1);
53    l_inv_valdn_org_id NUMBER := fnd_profile.value('CS_INV_VALIDATION_ORG');
54 BEGIN
55    IF p_inventory_item_id IS NOT NULL THEN
56       BEGIN
57          SELECT 'x'
58          INTO   l_dummy
59          FROM   mtl_system_items
60          WHERE  inventory_item_id = p_inventory_item_id
61          AND    organization_id = l_inv_valdn_org_id;
62          -- AND    organization_id = cs_std.get_item_valdn_orgzn_id;
63       EXCEPTION
64          WHEN NO_DATA_FOUND THEN
65             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_INVALID','INVENTORY_ITEM',p_inventory_item_id,'ORG_ID',l_inv_valdn_org_id);
66       END;
67    ELSE
68       CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_ISNULL');
69    END IF;
70 END validate_inventory_item;
71 
72 PROCEDURE validate_lookups
73 (
74    p_lookup_type   VARCHAR2
75    ,p_lookup_code  VARCHAR2
76 ) IS
77    l_dummy  VARCHAR2(1);
78 
79 BEGIN
80    BEGIN
81       SELECT 'x'
82       INTO   l_dummy
83       FROM   csi_lookups
84       WHERE  lookup_type = p_lookup_type
85       AND    lookup_code = p_lookup_code
86       OR     meaning= p_lookup_code; --Added for bug #6904836
87 
88    EXCEPTION
89       WHEN NO_DATA_FOUND THEN
90          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_LOOKUP_INVALID','LOOKUP_TYPE',p_lookup_type,'LOOKUP_CODE',p_lookup_code);
91    END;
92 END validate_lookups;
93 
94 PROCEDURE Validate_Data_Type
95 (
96    p_property_data_type	IN	VARCHAR2,
97    p_default_value		IN	VARCHAR2,
98    p_minimum_value		IN	VARCHAR2,
99    p_maximum_value		IN	VARCHAR2
100 )
101 IS
102    l_char	VARCHAR2(240);
103    l_num	NUMBER;
104    l_date	DATE;
105    l_default_value	VARCHAR2(240);
106    l_minimum_value	VARCHAR2(240);
107    l_maximum_value	VARCHAR2(240);
108 BEGIN
109    IF p_default_value = FND_API.G_MISS_CHAR THEN
110       l_default_value := NULL;
111    END IF;
112 
113    IF p_maximum_value = FND_API.G_MISS_CHAR THEN
114       l_maximum_value := NULL;
115    END IF;
116 
117    IF p_minimum_value = FND_API.G_MISS_CHAR THEN
118       l_minimum_value := NULL;
119    END IF;
120 
121    IF p_property_data_type = 'CHAR' THEN
122       NULL;
123       -- any value is okay even if the values are numbers or dates
124       -- they are going to be varchar2
125    ELSIF p_property_data_type = 'NUMBER' THEN
126       l_num :=	l_default_value;
127       l_num :=	l_minimum_value;
128       l_num :=	l_maximum_value;
129    ELSIF p_property_data_type = 'DATE' THEN
130       l_date :=	l_default_value;
131       l_date :=	l_minimum_value;
132       l_date :=	l_maximum_value;
133    ELSE
134       CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_PROP_DATA_TYPE');
135    END IF;
136 EXCEPTION
137    WHEN OTHERS THEN
138       CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_VAL_DATATYPE','DATA_TYPE',p_property_data_type);
139 END Validate_Data_Type;
140 
141 PROCEDURE validate_uom
142 (
143    p_uom_code varchar2
144 ) IS
145 
146    l_dummy	varchar2(1);
147 BEGIN
148    SELECT 'x'
149    INTO   l_dummy
150    FROM   mtl_units_of_measure
151    WHERE  uom_code = p_uom_code;
152 EXCEPTION
153    WHEN no_data_found THEN
154       CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_INVALID_UOM_CODE','UNIT_OF_MEASURE',p_uom_code);
155 END validate_uom;
156 
157 PROCEDURE validate_ctr_relationship
158 (
159    p_counter_id  IN NUMBER,
160    x_direction   OUT NOCOPY VARCHAR2,
161    x_start_date  OUT NOCOPY DATE,
162    x_end_date    OUT NOCOPY DATE
163 ) IS
164 
165    l_direction   varchar2(1);
166 
167 BEGIN
168    SELECT direction, start_date_active, end_date_active
169    INTO   x_direction, x_start_date, x_end_date
170    FROM   csi_counters_b
171    WHERE  counter_id = p_counter_id;
172 
173 EXCEPTION
174    WHEN no_data_found THEN
175       CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_INVALID_COUNTER');
176 END validate_ctr_relationship;
177 
178 PROCEDURE Validate_Counter
179 (
180    p_group_id                 NUMBER
181    ,p_name                    VARCHAR2
182    ,p_counter_type            VARCHAR2
183    ,p_uom_code                VARCHAR2
184    ,p_usage_item_id           NUMBER
185    ,p_reading_type            NUMBER
186    ,p_direction               VARCHAR2
187    ,p_estimation_id           NUMBER
188    ,p_derive_function         VARCHAR2
189    ,p_formula_text            VARCHAR2
190    ,p_derive_counter_id       NUMBER
191    ,p_filter_type             VARCHAR2
192    ,p_filter_reading_count    NUMBER
193    ,p_filter_time_uom         VARCHAR2
194    ,p_automatic_rollover      VARCHAR2
195    ,p_rollover_last_reading   NUMBER
196    ,p_rollover_first_reading  NUMBER
197    ,p_tolerance_plus          NUMBER
198    ,p_tolerance_minus         NUMBER
199    ,p_used_in_scheduling      VARCHAR2
200    ,p_initial_reading         NUMBER
201    ,p_default_usage_rate      NUMBER
202    ,p_use_past_reading        NUMBER
203    ,p_counter_id              NUMBER
204    ,p_start_date_active       DATE
205    ,p_end_date_active         DATE
206    ,p_update_flag             VARCHAR2
207 )  IS
208 
209    l_dummy	    varchar2(1);
210    l_exists	    varchar2(1);
211    l_time_uom       varchar2(1);
212    l_ctr_type_valid varchar2(1);
213    l_inv_valdn_org_id NUMBER := fnd_profile.value('CS_INV_VALIDATION_ORG');
214 BEGIN
215   -- Read the debug profiles values in to global variable 7197402
216    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
217 
218    -- validate counter name is not null
219    if p_name is null then
220       CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_PARM_CTR_NAME');
221    else
222 	--Changed for Bug 7462345, validating the name while update
223          BEGIN
224             SELECT 'x'
225             INTO   l_exists
226             FROM   csi_counter_template_vl
227             WHERE  upper(name) = upper(p_name)
228             AND    Nvl(group_id, FND_API.G_MISS_NUM) = Nvl(p_group_id, FND_API.G_MISS_NUM)-- Added for bug 9088368
229 	          AND COUNTER_ID <> Nvl(p_counter_id, FND_API.G_MISS_NUM);
230             csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_DUP_NAME','CTR_NAME',p_name);
231          EXCEPTION
232             WHEN NO_DATA_FOUND THEN
233                NULL;
234             WHEN TOO_MANY_ROWS THEN
235                csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_DUP_NAME','CTR_NAME',p_name);
236          END;
237 
238    END IF;
239 
240    -- validate uom code
241    validate_uom(p_uom_code);
242 
243    --validate Usage item id
244    if p_usage_item_id is not null then
245       begin
246          select 'x'
247          into   l_dummy
248          from   mtl_system_items
249          where  inventory_item_id = p_usage_item_id
250          and    organization_id = l_inv_valdn_org_id
251          -- and    organization_id = cs_std.get_item_valdn_orgzn_id
252          and    usage_item_flag = 'Y';
253       exception
254          when no_data_found then
255             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_USAGE_ITEM');
256       end;
257 
258       if p_group_id is null then
259          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_PARM_GRP_NAME');
260       end if;
261    end if;
262 
263    -- validate counter group id
264    if p_group_id is not null then
265       begin
266          select 'x'
267 	 into   l_dummy
268          from   cs_csi_counter_groups
269          where  counter_group_id = p_group_id;
270       exception
271          when no_data_found then
272             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_GRP_INVALID');
273       end;
274    end if;
275 
276    --validate estimation id exists only if direction is not null
277    if p_estimation_id is not null and p_direction is null then
278       CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_EST_NO_DIRECTION');
279    end if;
280 
281    -- Validate that automatic rollover should not exist if direction is Bi-Direction
282    if nvl(p_automatic_rollover,'N') = 'Y' and p_direction not in ('A','D') then
283       csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_ARO_NO_DIRECTION');
284    end if;
285 
286    --validate tolerance plus and tolerance minus for negative values
287    if p_tolerance_plus < 0  or p_tolerance_minus <0 then
288       CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_TOLERANCE');
289    end if;
290 
291    --validate counter type
292    IF p_counter_type IS NULL THEN
293       CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INVALID_CTR_TYPE');
294    ELSE
295       BEGIN
296          select 'x'
297          into   l_ctr_type_valid
298          from   csi_lookups
299          where  lookup_type = 'CSI_COUNTER_TYPE'
300          and    lookup_code = p_counter_type;
301       EXCEPTION
302          WHEN NO_DATA_FOUND THEN
303             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INVALID_CTR_TYPE');
304       END;
305    end if;
306 
307    --validate counter type parameters
308    if p_counter_type = 'REGULAR' then
309       begin
310          select 'Y'
311          into   l_time_uom
312          from   mtl_units_of_measure
313          where  uom_code = p_uom_code
314          and    upper(UOM_CLASS) = 'TIME';
315       exception
316          when no_data_found then
317             l_time_uom := 'N';
318       end;
319 
320       if l_time_uom = 'N' then
321          if p_reading_type not in (1,2) then
322          csi_ctr_gen_utility_pvt.put_line(' Regular ');
323          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_PARM_CTR_TYPE','PARAM','p_reading_type','CTR_TYPE',p_counter_type);
324          end if;
325 
326          if p_derive_function is not null or
327             p_formula_text is not null or
328             p_derive_counter_id is not null or
329             p_filter_type is not null or
330             p_filter_reading_count is not null or
331             p_filter_time_uom is not null  then
332 
333             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_PARM_CTR_TYPE','CTR_TYPE',p_counter_type);
334          end if;
335 
336          --validate required parameter values exist for automatic rollover
337          if nvl(p_automatic_rollover,'N') = 'Y' then
338            if p_rollover_last_reading is null then
339              csi_ctr_gen_utility_pvt.ExitWithErrMsg
340              ( p_msg_name     =>  'CSI_API_CTR_REQ_PARM',
341                p_token1_name  =>  'PARAM',
342                p_token1_val   =>  'for rollover attribute'
343              );
344            elsif p_rollover_first_reading is null then
345              csi_ctr_gen_utility_pvt.ExitWithErrMsg
346              ( p_msg_name     =>  'CSI_API_CTR_REQ_PARM',
347                p_token1_name  =>  'PARAM',
348                p_token1_val   =>  'for rollover attribute'
349                -- p_token1_val   =>  'p_rollover_first_reading'
350              );
351            end if;
352            --Rollover from must be greater than Rollover to for direction Ascending
353            if  p_direction = 'A' and p_rollover_last_reading <= p_rollover_first_reading then
354              csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_INV_RO_DATA');
355            end if;
356             --Rollover from must be less than Rollover to for direction Descending
357            if p_direction = 'D' and p_rollover_last_reading >= p_rollover_first_reading then
358              csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_INV_RO_DATA');
359            end if;
360         end if;
361         -- Rollover from and Rollover to field should not have values if automatic rollover is not checked.
362         if nvl(p_automatic_rollover,'N') = 'N' and (p_rollover_last_reading is NOT NULL or p_rollover_first_reading IS NOT NULL) then
363            csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_INV_RO_ARO');
364         end if;
365 
366          --validate if required parameter values exist for used in scheduling
367          if nvl(p_used_in_scheduling,'N') = 'Y' then
368             if p_initial_reading is null then
369                CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_PARM_USE_SCHD');
370             elsif p_default_usage_rate  is null then
371                CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_PARM_USE_SCHD');
372             elsif p_use_past_reading is null then
373                CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_PARM_USE_SCHD');
374             end if;
375          end if;
376       else -- if type is a time counter
377          if p_derive_function is not null or
378             p_formula_text is not null or
379             p_derive_counter_id is not null or
380             p_filter_type is not null or
381             p_filter_reading_count is not null or
382             p_filter_time_uom is not null or
383             nvl(p_automatic_rollover,'N') = 'Y' or
384             p_rollover_last_reading is not null or
385             p_rollover_first_reading is not null or
386             p_tolerance_plus is not null or
387             p_tolerance_minus is not null or
388             p_estimation_id is not null
389          then
390             csi_ctr_gen_utility_pvt.put_line(' Time ');
391             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_PARM_CTR_TYPE','CTR_TYPE',p_counter_type);
392          end if;
393 
394          if nvl(p_used_in_scheduling,'N') = 'Y' then
395             if p_initial_reading is null then
396                CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_PARM_USE_SCHD');
397             elsif p_default_usage_rate  is null then
398                CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_PARM_USE_SCHD');
399             elsif p_use_past_reading is null then
400                CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_PARM_USE_SCHD');
401             end if;
402          end if;
403 
404       end if; --l_time_uom
405    elsif p_counter_type = 'FORMULA' then
406       if p_derive_function is null then
407          if p_formula_text is null then
408             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_PARM_FORMULA');
409          end if;
410 
411          if p_derive_counter_id is not null or
412             p_filter_type is not null or
413             p_filter_reading_count is not null or
414             p_filter_time_uom is not null or
415             nvl(p_automatic_rollover,'N') = 'Y' or
416             p_rollover_last_reading is not null or
417             p_rollover_first_reading is not null or
418             p_initial_reading is not null
419             -- p_tolerance_plus is not null or
420             -- p_tolerance_minus is not null
421          then
422             csi_ctr_gen_utility_pvt.put_line(' Formula');
423             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_PARM_CTR_TYPE','CTR_TYPE',p_counter_type);
424          end if;
425       elsif p_derive_function='AVERAGE' and p_filter_type='COUNT' then
426          if p_filter_reading_count < 0 then
427             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_PARM_FORMULA');
428          end if;
429 
430          if p_formula_text is not null or
431             p_filter_time_uom is not null or
432             nvl(p_automatic_rollover,'N') = 'Y' or
433             p_rollover_last_reading is not null or
434             p_rollover_first_reading is not null or
435             nvl(p_used_in_scheduling,'N') = 'Y' or
436             p_initial_reading is not null or
437             -- p_default_usage_rate is not null or
438             -- p_use_past_reading is not null  or
439             p_estimation_id is not null
440             -- p_tolerance_plus is not null or
441             -- p_tolerance_minus is not null
442          then
443             csi_ctr_gen_utility_pvt.put_line(' Failed Average and Count');
444             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_PARM_CTR_TYPE','CTR_TYPE',p_counter_type);
445          end if;
446       elsif p_derive_function='AVERAGE' and p_filter_type='TIME' then
447          if p_filter_time_uom is null then
448             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_PARM_FORMULA');
449          end if;
450 
451          if p_formula_text is not null or
452             p_filter_reading_count is not null or
453             nvl(p_automatic_rollover,'N') = 'Y' or
454             p_rollover_last_reading is not null or
455             p_rollover_first_reading is not null or
456             nvl(p_used_in_scheduling,'N') = 'Y' or
457             p_initial_reading is not null or
458             -- p_default_usage_rate is not null or
459             -- p_use_past_reading is not null  or
460             p_estimation_id is not null
461             -- p_tolerance_plus is not null or
462             -- p_tolerance_minus is not null
463          then
464             csi_ctr_gen_utility_pvt.put_line(' Failed Average and Time');
465             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_PARM_CTR_TYPE','CTR_TYPE',p_counter_type);
466          end if;
467       elsif p_derive_function in ('SUM','COUNT') then
468          /* if p_derive_counter_id is null then
469             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_PARM_FORMULA');
470          end if;
471         */
472 
473          if p_formula_text is not null or
474             p_filter_time_uom is not null or
475             p_filter_reading_count is not null or
476             nvl(p_automatic_rollover,'N') = 'Y' or
477             p_rollover_last_reading is not null or
478             p_rollover_first_reading is not null or
479             nvl(p_used_in_scheduling,'N') = 'Y' or
480             p_initial_reading is not null or
481             -- p_default_usage_rate is not null or
482             -- p_use_past_reading is not null  or
483             p_estimation_id is not null
484             -- p_tolerance_plus is not null or
485             -- p_tolerance_minus is not null
486          then
487             csi_ctr_gen_utility_pvt.put_line(' Failed Sum and Count');
488             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_PARM_CTR_TYPE','CTR_TYPE',p_counter_type);
489          end if;
490       end if;
491    end if; -- p_counter_type
492 
493 END validate_counter;
494 
495 PROCEDURE create_counter_group
496  (
497      p_api_version               IN     NUMBER
498     ,p_commit                    IN     VARCHAR2
499     ,p_init_msg_list             IN     VARCHAR2
500     ,p_validation_level          IN     NUMBER
501     ,p_counter_groups_rec        IN OUT NOCOPY CSI_CTR_DATASTRUCTURES_PUB.counter_groups_rec
502     ,x_return_status                OUT    NOCOPY VARCHAR2
503     ,x_msg_count                    OUT    NOCOPY NUMBER
504     ,x_msg_data                     OUT    NOCOPY VARCHAR2
505  ) IS
506    l_api_name                     CONSTANT VARCHAR2(30)   := 'CREATE_COUNTER_GROUP';
507    l_api_version                  CONSTANT NUMBER         := 1.0;
508    l_msg_data                     VARCHAR2(2000);
509    l_msg_index                    NUMBER;
510    l_msg_count                    NUMBER;
511    -- l_debug_level                  NUMBER;
512 
513 
514    l_COUNTER_GROUP_ID 		  NUMBER;
515    l_NAME                         VARCHAR2(100);
516    l_DESCRIPTION                  VARCHAR2(240);
517    l_TEMPLATE_FLAG                VARCHAR2(1);
518    -- l_CP_SERVICE_ID                NUMBER;
519    -- l_CUSTOMER_PRODUCT_ID          NUMBER;
520    l_LAST_UPDATE_DATE             DATE;
521    l_LAST_UPDATED_BY              NUMBER;
522    l_CREATION_DATE                DATE;
523    l_CREATED_BY                   NUMBER;
524    l_LAST_UPDATE_LOGIN            NUMBER;
525    l_START_DATE_ACTIVE            DATE;
526    l_END_DATE_ACTIVE              DATE;
527    l_ATTRIBUTE1                   VARCHAR2(150);
528    l_ATTRIBUTE2                   VARCHAR2(150);
529    l_ATTRIBUTE3                   VARCHAR2(150);
530    l_ATTRIBUTE4                   VARCHAR2(150);
531    l_ATTRIBUTE5                   VARCHAR2(150);
532    l_ATTRIBUTE6                   VARCHAR2(150);
533    l_ATTRIBUTE7                   VARCHAR2(150);
534    l_ATTRIBUTE8                   VARCHAR2(150);
535    l_ATTRIBUTE9                   VARCHAR2(150);
536    l_ATTRIBUTE10                  VARCHAR2(150);
537    l_ATTRIBUTE11                  VARCHAR2(150);
538    l_ATTRIBUTE12                  VARCHAR2(150);
539    l_ATTRIBUTE13                  VARCHAR2(150);
540    l_ATTRIBUTE14                  VARCHAR2(150);
541    l_ATTRIBUTE15                  VARCHAR2(150);
542    l_CONTEXT                      VARCHAR2(30);
543    l_OBJECT_VERSION_NUMBER        NUMBER;
544    l_CREATED_FROM_CTR_GRP_TMPL_ID NUMBER;
545    l_ASSOCIATION_TYPE             VARCHAR2(30);
546    l_SOURCE_OBJECT_CODE           VARCHAR2(30);
547    l_SOURCE_OBJECT_ID             NUMBER;
548    l_SOURCE_COUNTER_GROUP_ID      NUMBER;
549    l_SECURITY_GROUP_ID            NUMBER;
550    l_UPGRADED_STATUS_FLAG         VARCHAR2(1);
551 BEGIN
552    -- Standard Start of API savepoint
553    SAVEPOINT  create_counter_group_pvt;
554 
555    -- Standard call to check for call compatibility.
556    IF NOT FND_API.Compatible_API_Call (l_api_version,
557                                        p_api_version,
558                                        l_api_name   ,
559                                        G_PKG_NAME   ) THEN
560       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
561    END IF;
562 
563    -- Initialize message list if p_init_msg_list is set to TRUE.
564    IF FND_API.to_Boolean(nvl(p_init_msg_list,FND_API.G_FALSE)) THEN
565       FND_MSG_PUB.initialize;
566    END IF;
567 
568    --  Initialize API return status to success
569    x_return_status := FND_API.G_RET_STS_SUCCESS;
570 
571    -- Read the debug profiles values in to global variable 7197402
572    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
573 
574    --
575    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
576       csi_ctr_gen_utility_pvt.put_line
577          ('create_counter_group_pvt'              ||'-'||
578            p_api_version                              ||'-'||
579            nvl(p_commit,FND_API.G_FALSE)              ||'-'||
580            nvl(p_init_msg_list,FND_API.G_FALSE)       ||'-'||
581            nvl(p_validation_level,FND_API.G_VALID_LEVEL_FULL) );
582    END IF;
583 
584    ----
585    -- Initialize_Desc_Flex(p_ctr_grp_rec.desc_flex, l_desc_flex);
586 
587    -- validate_DFF_ReqdParms(l_api_name, l_desc_flex, 'CS_COUNTER_GROUPS',
588 	--  		  p_ctr_grp_rec.name, 'p_ctr_grp_rec.name',
589 	--		  p_ctr_grp_rec.association_type, 'p_ctr_grp_rec.association_type');
590 
591    l_name := p_counter_groups_rec.name;
592 
593 
594    IF p_counter_groups_rec.description = FND_API.G_MISS_CHAR then
595       l_description := null;
596    ELSE
597       l_description := p_counter_groups_rec.description;
598    END IF;
599 
600    IF p_counter_groups_rec.template_flag = FND_API.G_MISS_CHAR then
601       l_template_flag := 'N';
602    ELSE
603       l_template_flag := nvl(p_counter_groups_rec.template_flag,'N');
604    END IF;
605 
606    validate_counter_group(l_name, l_template_flag);
607 
608    IF p_counter_groups_rec.association_type = FND_API.G_MISS_CHAR then
609       l_association_type := null;
610    ELSE
611       l_association_type := p_counter_groups_rec.association_type;
612    END IF;
613 
614    if nvl(p_counter_groups_rec.start_date_active,FND_API.G_MISS_DATE) = FND_API.G_MISS_DATE then
615       l_start_date_active := sysdate;
616    else
617       l_start_date_active := p_counter_groups_rec.start_date_active;
618    end if;
619 
620    validate_start_date(l_start_date_active);
621 
622    if p_counter_groups_rec.end_date_active = FND_API.G_MISS_DATE then
623       l_end_date_active := null;
624    else
625       l_end_date_active := p_counter_groups_rec.end_date_active;
626    end if;
627 
628    if l_end_date_active IS NOT NULL then
629       if l_end_date_active < l_start_date_active then
630          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_ALL_END_DATE');
631       end if;
632    end if;
633 
634    if p_counter_groups_rec.attribute1 = FND_API.G_MISS_CHAR then
635       l_attribute1 := null;
636    else
637       l_attribute1 := p_counter_groups_rec.attribute1;
638    end if;
639 
640    if p_counter_groups_rec.attribute2 = FND_API.G_MISS_CHAR then
641       l_attribute2 := null;
642    else
643       l_attribute2 := p_counter_groups_rec.attribute2;
644    end if;
645 
646    if p_counter_groups_rec.attribute3 = FND_API.G_MISS_CHAR then
647       l_attribute3 := null;
648    else
649       l_attribute3 := p_counter_groups_rec.attribute3;
650    end if;
651 
652    if p_counter_groups_rec.attribute4 = FND_API.G_MISS_CHAR then
653       l_attribute4 := null;
654    else
655       l_attribute4 := p_counter_groups_rec.attribute4;
656    end if;
657 
658    if p_counter_groups_rec.attribute5 = FND_API.G_MISS_CHAR then
659       l_attribute5 := null;
660    else
661       l_attribute5 := p_counter_groups_rec.attribute5;
662    end if;
663 
664    if p_counter_groups_rec.attribute6 = FND_API.G_MISS_CHAR then
665       l_attribute6 := null;
666    else
667       l_attribute6 := p_counter_groups_rec.attribute6;
668    end if;
669 
670    if p_counter_groups_rec.attribute7 = FND_API.G_MISS_CHAR then
671       l_attribute7 := null;
672    else
673       l_attribute7 := p_counter_groups_rec.attribute7;
674    end if;
675 
676    if p_counter_groups_rec.attribute8 = FND_API.G_MISS_CHAR then
677       l_attribute8 := null;
678    else
679       l_attribute8 := p_counter_groups_rec.attribute8;
680    end if;
681 
682    if p_counter_groups_rec.attribute9 = FND_API.G_MISS_CHAR then
683       l_attribute9 := null;
684    else
685       l_attribute9 := p_counter_groups_rec.attribute9;
686    end if;
687 
688    if p_counter_groups_rec.attribute10 = FND_API.G_MISS_CHAR then
689       l_attribute10 := null;
690    else
691       l_attribute10 := p_counter_groups_rec.attribute10;
692    end if;
693 
694    if p_counter_groups_rec.attribute11 = FND_API.G_MISS_CHAR then
695       l_attribute11 := null;
696    else
697       l_attribute11 := p_counter_groups_rec.attribute11;
698    end if;
699 
700    if p_counter_groups_rec.attribute12 = FND_API.G_MISS_CHAR then
701       l_attribute12 := null;
702    else
703       l_attribute12 := p_counter_groups_rec.attribute12;
704    end if;
705 
706    if p_counter_groups_rec.attribute13 = FND_API.G_MISS_CHAR then
707       l_attribute13 := null;
708    else
709       l_attribute13 := p_counter_groups_rec.attribute13;
710    end if;
711 
712    if p_counter_groups_rec.attribute14 = FND_API.G_MISS_CHAR then
713       l_attribute14 := null;
714    else
715       l_attribute14 := p_counter_groups_rec.attribute14;
716    end if;
717 
718    if p_counter_groups_rec.attribute15 = FND_API.G_MISS_CHAR then
719       l_attribute15 := null;
720    else
721       l_attribute15 := p_counter_groups_rec.attribute15;
722    end if;
723 
724    if p_counter_groups_rec.context = FND_API.G_MISS_CHAR then
725       l_context := null;
726    else
727       l_context := p_counter_groups_rec.context;
728    end if;
729 
730    if p_counter_groups_rec.created_from_ctr_grp_tmpl_id = FND_API.G_MISS_NUM then
731       l_created_from_ctr_grp_tmpl_id := null;
732    else
733       l_created_from_ctr_grp_tmpl_id := p_counter_groups_rec.created_from_ctr_grp_tmpl_id;
734    end if;
735 
736    --Code added for bug 8326815 -Start
737    l_source_object_code:=p_counter_groups_rec.SOURCE_OBJECT_CODE;
738    l_source_object_id:=p_counter_groups_rec.SOURCE_OBJECT_ID;
739    l_SOURCE_COUNTER_GROUP_ID:=p_counter_groups_rec.SOURCE_COUNTER_GROUP_ID;
740    --Code added for bug 8326815 -End
741 
742    -- IF NOT(CS_COUNTERS_EXT_PVT.Is_StartEndDate_Valid(l_st_dt,l_end_dt)) THEN
743       -- RAISE FND_API.G_EXC_ERROR;
744    -- END IF;
745 
746    /* Call the table Handler */
747    CSI_GROUPING_PKG.Insert_Row(
748         px_COUNTER_GROUP_ID 		=> l_counter_group_id
749         ,p_NAME                         => l_name
750         ,p_DESCRIPTION                  => l_description
751         ,p_TEMPLATE_FLAG                => l_template_flag
752         ,p_CP_SERVICE_ID                => NULL
753         ,p_CUSTOMER_PRODUCT_ID          => NULL
754         ,p_LAST_UPDATE_DATE             => sysdate
755         ,p_LAST_UPDATED_BY              => FND_GLOBAL.USER_ID
756         ,p_CREATION_DATE                => sysdate
757         ,p_CREATED_BY                   => FND_GLOBAL.USER_ID
758         ,p_LAST_UPDATE_LOGIN            => FND_GLOBAL.USER_ID
759         ,p_START_DATE_ACTIVE            => l_start_date_active
760         ,p_END_DATE_ACTIVE              => l_end_date_active
761         ,p_ATTRIBUTE1                   => l_attribute1
762         ,p_ATTRIBUTE2                   => l_attribute2
763         ,p_ATTRIBUTE3                   => l_attribute3
764         ,p_ATTRIBUTE4                   => l_attribute4
765         ,p_ATTRIBUTE5                   => l_attribute5
766         ,p_ATTRIBUTE6                   => l_attribute6
767         ,p_ATTRIBUTE7                   => l_attribute7
768         ,p_ATTRIBUTE8                   => l_attribute8
769         ,p_ATTRIBUTE9                   => l_attribute9
770         ,p_ATTRIBUTE10                  => l_attribute10
771         ,p_ATTRIBUTE11                  => l_attribute11
772         ,p_ATTRIBUTE12                  => l_attribute12
773         ,p_ATTRIBUTE13                  => l_attribute13
774         ,p_ATTRIBUTE14                  => l_attribute14
775         ,p_ATTRIBUTE15                  => l_attribute15
776         ,p_CONTEXT                      => l_context
777         ,p_OBJECT_VERSION_NUMBER        => 1
778 	,p_CREATED_FROM_CTR_GRP_TMPL_ID => l_created_from_ctr_grp_tmpl_id
779 	,p_ASSOCIATION_TYPE             => l_association_type
780 	,p_SOURCE_OBJECT_CODE           => l_source_object_code
781 	,p_SOURCE_OBJECT_ID             => l_source_object_id
782 	,p_SOURCE_COUNTER_GROUP_ID      => l_source_counter_group_id
783 	,p_SECURITY_GROUP_ID            => l_security_group_id
784         );
785 
786    p_counter_groups_rec.counter_group_id := l_counter_group_id;
787 
788    IF NOT (x_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
789         ROLLBACK TO create_counter_group_pvt;
790         RETURN;
791    END IF;
792 
793    /* End of table handler call */
794 
795    IF FND_API.to_Boolean(nvl(p_commit,FND_API.G_FALSE)) THEN
796       COMMIT WORK;
797    END IF;
798 
799    -- Standard call to get message count and IF count is  get message info.
800    FND_MSG_PUB.Count_And_Get
801       ( p_count  =>  x_msg_count,
802         p_data   =>  x_msg_data
803       );
804 EXCEPTION
805    WHEN FND_API.G_EXC_ERROR THEN
806       x_return_status := FND_API.G_RET_STS_ERROR ;
807       ROLLBACK TO create_counter_group_pvt;
808       FND_MSG_PUB.Count_And_Get
809          ( p_count => x_msg_count,
810            p_data  => x_msg_data
811          );
812    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
813       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
814       ROLLBACK TO create_counter_group_pvt;
815       FND_MSG_PUB.Count_And_Get
816          ( p_count => x_msg_count,
817            p_data  => x_msg_data
818          );
819    WHEN OTHERS THEN
820       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
821       ROLLBACK TO create_counter_group_pvt;
822       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
823          FND_MSG_PUB.Add_Exc_Msg
824             ( G_PKG_NAME,
825               l_api_name
826             );
827       END IF;
828       FND_MSG_PUB.Count_And_Get
829          ( p_count  => x_msg_count,
830            p_data   => x_msg_data
831          );
832 END create_counter_group;
833 
834 --|---------------------------------------------------
835 --| procedure name: create_item_association
836 --| description :   procedure used to
837 --|                 create item association to
838 --|                 counter group or counters
839 --|---------------------------------------------------
840 
841 PROCEDURE create_item_association
842  (
843      p_api_version               IN     NUMBER
844     ,p_commit                    IN     VARCHAR2
845     ,p_init_msg_list             IN     VARCHAR2
846     ,p_validation_level          IN     NUMBER
847     ,p_ctr_item_associations_rec IN OUT NOCOPY CSI_CTR_DATASTRUCTURES_PUB.ctr_item_associations_rec
848     ,x_return_status                OUT    NOCOPY VARCHAR2
849     ,x_msg_count                    OUT    NOCOPY NUMBER
850     ,x_msg_data                     OUT    NOCOPY VARCHAR2
851  ) IS
852    l_api_name                     CONSTANT VARCHAR2(30)   := 'CREATE_ITEM_ASSOCIATION';
853    l_api_version                  CONSTANT NUMBER         := 1.0;
854    l_msg_data                     VARCHAR2(2000);
855    l_msg_index                    NUMBER;
856    l_msg_count                    NUMBER;
857    -- l_debug_level                  NUMBER;
858 
859    l_CTR_ASSOCIATION_ID		  NUMBER;
860    l_GROUP_ID		          NUMBER;
861    l_INVENTORY_ITEM_ID            NUMBER;
862    l_LAST_UPDATE_DATE             DATE;
863    l_LAST_UPDATED_BY              NUMBER;
864    l_CREATION_DATE                DATE;
865    l_CREATED_BY                   NUMBER;
866    l_LAST_UPDATE_LOGIN            NUMBER;
867    l_START_DATE_ACTIVE            DATE;
868    l_END_DATE_ACTIVE              DATE;
869    l_ATTRIBUTE1                   VARCHAR2(150);
870    l_ATTRIBUTE2                   VARCHAR2(150);
871    l_ATTRIBUTE3                   VARCHAR2(150);
872    l_ATTRIBUTE4                   VARCHAR2(150);
873    l_ATTRIBUTE5                   VARCHAR2(150);
874    l_ATTRIBUTE6                   VARCHAR2(150);
875    l_ATTRIBUTE7                   VARCHAR2(150);
876    l_ATTRIBUTE8                   VARCHAR2(150);
877    l_ATTRIBUTE9                   VARCHAR2(150);
878    l_ATTRIBUTE10                  VARCHAR2(150);
879    l_ATTRIBUTE11                  VARCHAR2(150);
880    l_ATTRIBUTE12                  VARCHAR2(150);
881    l_ATTRIBUTE13                  VARCHAR2(150);
882    l_ATTRIBUTE14                  VARCHAR2(150);
883    l_ATTRIBUTE15                  VARCHAR2(150);
884    l_ATTRIBUTE_CATEGORY           VARCHAR2(30);
885    l_OBJECT_VERSION_NUMBER        NUMBER;
886    l_COUNTER_ID			  NUMBER;
887    l_USE_PAST_READING             NUMBER;
888    l_USAGE_RATE                   NUMBER;
889    l_MAINT_ORGANIZATION_ID        NUMBER;
890    l_ASSOCIATED_TO_GROUP          VARCHAR2(1);
891    l_SECURITY_GROUP_ID            NUMBER;
892    l_MIGRATED_FLAG                VARCHAR2(1);
893    l_ITEM_FOUND                   VARCHAR2(1);
894    l_ITEM_INVALID                 VARCHAR2(1);
895    l_PRIMARY_FAILURE_FLAG         VARCHAR2(1);
896    l_eam_item_type                NUMBER;
897    l_valid_start_date             DATE;
898 
899    CURSOR c1_group(p_group_id NUMBER) IS
900    select start_date_active
901    from   cs_csi_counter_groups
902    where  counter_group_id = p_group_id;
903 
904    CURSOR c2_counter(p_counter_id NUMBER) IS
905    select start_date_active
906    from   csi_counter_template_b
907    where  counter_id = p_counter_id;
908 
909 BEGIN
910    -- Standard Start of API savepoint
911    SAVEPOINT  create_item_association_pvt;
912 
913    -- Standard call to check for call compatibility.
914    IF NOT FND_API.Compatible_API_Call (l_api_version,
915                                        p_api_version,
916                                        l_api_name   ,
917                                        G_PKG_NAME   ) THEN
918       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
919    END IF;
920 
921    -- Initialize message list if p_init_msg_list is set to TRUE.
922    IF FND_API.to_Boolean(nvl(p_init_msg_list,FND_API.G_FALSE)) THEN
923       FND_MSG_PUB.initialize;
924    END IF;
925 
926    -- Read the debug profiles values in to global variable 7197402
927    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
928 
929    --  Initialize API return status to success
930    x_return_status := FND_API.G_RET_STS_SUCCESS;
931 
932    --
933    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
934       csi_ctr_gen_utility_pvt.put_line
935          ( 'create_item_association_pvt'           ||'-'||
936            p_api_version                              ||'-'||
937            nvl(p_commit,FND_API.G_FALSE)              ||'-'||
938            nvl(p_init_msg_list,FND_API.G_FALSE)       ||'-'||
939            nvl(p_validation_level,FND_API.G_VALID_LEVEL_FULL) );
940    END IF;
941 
942    if p_ctr_item_associations_rec.group_id = FND_API.G_MISS_NUM then
943       l_group_id := null;
944    else
945       l_group_id := p_ctr_item_associations_rec.group_id;
946    end if;
947 
948    if p_ctr_item_associations_rec.inventory_item_id = FND_API.G_MISS_NUM then
949       l_inventory_item_id := null;
950    else
951       l_inventory_item_id := p_ctr_item_associations_rec.inventory_item_id;
952    end if;
953 
954    if p_ctr_item_associations_rec.associated_to_group= FND_API.G_MISS_CHAR then
955       l_associated_to_group := 'N';
956    else
957       l_associated_to_group := p_ctr_item_associations_rec.associated_to_group;
958    end if;
959 
960    if p_ctr_item_associations_rec.counter_id = FND_API.G_MISS_NUM then
961       l_counter_id := null;
962    else
963       l_counter_id := p_ctr_item_associations_rec.counter_id;
964    end if;
965 
966    if p_ctr_item_associations_rec.security_group_id = FND_API.G_MISS_NUM then
967       l_security_group_id := null;
968    else
969       l_security_group_id := p_ctr_item_associations_rec.security_group_id;
970    end if;
971 
972    if p_ctr_item_associations_rec.use_past_reading = FND_API.G_MISS_NUM then
973       l_use_past_reading := null;
974    else
975       l_use_past_reading := p_ctr_item_associations_rec.use_past_reading;
976    end if;
977 
978    if p_ctr_item_associations_rec.usage_rate = FND_API.G_MISS_NUM then
979       l_usage_rate := null;
980    else
981       l_usage_rate := p_ctr_item_associations_rec.usage_rate;
982    end if;
983 
984    if nvl(p_ctr_item_associations_rec.start_date_active, FND_API.G_MISS_DATE) = FND_API.G_MISS_DATE then
985       l_start_date_active := sysdate;
986    else
987       l_start_date_active := p_ctr_item_associations_rec.start_date_active;
988    end if;
989 
990    validate_start_date(l_start_date_active);
991 
992    IF l_start_date_active > sysdate THEN
993       CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_GRP_FUT_DATE');
994    END IF;
995 
996    IF l_associated_to_group = 'Y' THEN
997       OPEN c1_group(l_group_id);
998       FETCH c1_group INTO l_valid_start_date;
999 
1000       IF l_start_date_active < l_valid_start_date THEN
1001          csi_ctr_gen_utility_pvt.ExitWithErrMsg
1002              ( p_msg_name     =>  'CSI_API_CTR_VAL_ST_DATE',
1003                p_token1_name  =>  'PARAM',
1004                p_token1_val   =>  'The item association start date cannot be less than the active start date of the counter group.');
1005       END IF;
1006       CLOSE c1_group;
1007    ELSE
1008       OPEN c2_counter(l_counter_id);
1009       FETCH c2_counter INTO l_valid_start_date;
1010 
1011       IF l_start_date_active < l_valid_start_date THEN
1012          csi_ctr_gen_utility_pvt.ExitWithErrMsg
1013              ( p_msg_name     =>  'CSI_API_CTR_VAL_ST_DATE',
1014                p_token1_name  =>  'PARAM',
1015                p_token1_val   =>  'The item association start date cannot be less than the active start date of the counter.');
1016       END IF;
1017       CLOSE c2_counter;
1018    END IF;
1019 
1020    if p_ctr_item_associations_rec.end_date_active = FND_API.G_MISS_DATE then
1021       l_end_date_active := null;
1022    else
1023       l_end_date_active := p_ctr_item_associations_rec.end_date_active;
1024    end if;
1025 
1026    if l_end_date_active IS NOT NULL then
1027       if l_end_date_active < l_start_date_active then
1028          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_ALL_END_DATE');
1029       end if;
1030    end if;
1031 
1032    if p_ctr_item_associations_rec.attribute1 = FND_API.G_MISS_CHAR then
1033       l_attribute1 := null;
1034    else
1035       l_attribute1 := p_ctr_item_associations_rec.attribute1;
1036    end if;
1037 
1038    if p_ctr_item_associations_rec.attribute2 = FND_API.G_MISS_CHAR then
1039       l_attribute2 := null;
1040    else
1041       l_attribute2 := p_ctr_item_associations_rec.attribute2;
1042    end if;
1043 
1044    if p_ctr_item_associations_rec.attribute3 = FND_API.G_MISS_CHAR then
1045       l_attribute3 := null;
1046    else
1047       l_attribute3 := p_ctr_item_associations_rec.attribute3;
1048    end if;
1049 
1050    if p_ctr_item_associations_rec.attribute4 = FND_API.G_MISS_CHAR then
1051       l_attribute4 := null;
1052    else
1053       l_attribute4 := p_ctr_item_associations_rec.attribute4;
1054    end if;
1055 
1056    if p_ctr_item_associations_rec.attribute5 = FND_API.G_MISS_CHAR then
1057       l_attribute5 := null;
1058    else
1059       l_attribute5 := p_ctr_item_associations_rec.attribute5;
1060    end if;
1061 
1062    if p_ctr_item_associations_rec.attribute6 = FND_API.G_MISS_CHAR then
1063       l_attribute6 := null;
1064    else
1065       l_attribute6 := p_ctr_item_associations_rec.attribute6;
1066    end if;
1067 
1068    if p_ctr_item_associations_rec.attribute7 = FND_API.G_MISS_CHAR then
1069       l_attribute7 := null;
1070    else
1071       l_attribute7 := p_ctr_item_associations_rec.attribute7;
1072    end if;
1073 
1074    if p_ctr_item_associations_rec.attribute8 = FND_API.G_MISS_CHAR then
1075       l_attribute8 := null;
1076    else
1077       l_attribute8 := p_ctr_item_associations_rec.attribute8;
1078    end if;
1079 
1080    if p_ctr_item_associations_rec.attribute9 = FND_API.G_MISS_CHAR then
1081       l_attribute9 := null;
1082    else
1083       l_attribute9 := p_ctr_item_associations_rec.attribute9;
1084    end if;
1085 
1086    if p_ctr_item_associations_rec.attribute10 = FND_API.G_MISS_CHAR then
1087       l_attribute10 := null;
1088    else
1089       l_attribute10 := p_ctr_item_associations_rec.attribute10;
1090    end if;
1091 
1092    if p_ctr_item_associations_rec.attribute11 = FND_API.G_MISS_CHAR then
1093       l_attribute11 := null;
1094    else
1095       l_attribute11 := p_ctr_item_associations_rec.attribute11;
1096    end if;
1097 
1098    if p_ctr_item_associations_rec.attribute12 = FND_API.G_MISS_CHAR then
1099       l_attribute12 := null;
1100    else
1101       l_attribute12 := p_ctr_item_associations_rec.attribute12;
1102    end if;
1103 
1104    if p_ctr_item_associations_rec.attribute13 = FND_API.G_MISS_CHAR then
1105       l_attribute13 := null;
1106    else
1107       l_attribute13 := p_ctr_item_associations_rec.attribute13;
1108    end if;
1109 
1110    if p_ctr_item_associations_rec.attribute14 = FND_API.G_MISS_CHAR then
1111       l_attribute14 := null;
1112    else
1113       l_attribute14 := p_ctr_item_associations_rec.attribute14;
1114    end if;
1115 
1116    if p_ctr_item_associations_rec.attribute15 = FND_API.G_MISS_CHAR then
1117       l_attribute15 := null;
1118    else
1119       l_attribute15 := p_ctr_item_associations_rec.attribute15;
1120    end if;
1121 
1122    if p_ctr_item_associations_rec.attribute_category = FND_API.G_MISS_CHAR then
1123       l_attribute_category := null;
1124    else
1125       l_attribute_category := p_ctr_item_associations_rec.attribute_category;
1126    end if;
1127 
1128    if p_ctr_item_associations_rec.migrated_flag = FND_API.G_MISS_CHAR then
1129       l_migrated_flag := null;
1130    else
1131       l_migrated_flag := p_ctr_item_associations_rec.migrated_flag;
1132    end if;
1133 
1134    if p_ctr_item_associations_rec.maint_organization_id = FND_API.G_MISS_NUM then
1135       l_maint_organization_id := null;
1136    else
1137       l_maint_organization_id := p_ctr_item_associations_rec.maint_organization_id;
1138    end if;
1139 
1140    if p_ctr_item_associations_rec.primary_failure_flag = FND_API.G_MISS_CHAR then
1141       l_primary_failure_flag := null;
1142    else
1143       l_primary_failure_flag := p_ctr_item_associations_rec.primary_failure_flag;
1144    end if;
1145 
1146    validate_start_date(l_start_date_active);
1147 
1148    /* Check if the item is an EAM item */
1149    BEGIN
1150       SELECT distinct nvl(eam_item_type,0) eam_item_type
1151       INTO   l_eam_item_type
1152       FROM   mtl_system_items_b
1153       WHERE  inventory_item_id = l_inventory_item_id;
1154    EXCEPTION
1155       WHEN NO_DATA_FOUND THEN
1156          l_eam_item_type := 0;
1157    END;
1158 
1159    IF l_eam_item_type = 0 THEN
1160       validate_inventory_item(l_inventory_item_id);
1161    END IF;
1162    /* end of EAM item checking */
1163 
1164    IF l_group_id IS NOT NULL THEN
1165       IF l_associated_to_group = 'Y' and l_counter_id IS NULL THEN
1166          BEGIN
1167             SELECT 'x'
1168             INTO   l_item_invalid
1169             FROM   csi_ctr_item_associations
1170             WHERE  inventory_item_id = l_inventory_item_id
1171             AND    nvl(associated_to_group,'N') = 'Y';
1172 
1173             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_ASSOC');
1174          EXCEPTION
1175             WHEN NO_DATA_FOUND THEN NULL;
1176             WHEN TOO_MANY_ROWS THEN
1177                CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_ASSOC');
1178          END;
1179       END IF;
1180    END IF;
1181 
1182    IF l_associated_to_group = 'Y' THEN
1183       IF l_group_id IS NOT NULL THEN
1184          BEGIN
1185             SELECT 'x'
1186             INTO   l_item_invalid
1187             FROM   csi_ctr_item_associations
1188             WHERE  inventory_item_id = l_inventory_item_id
1189             AND    associated_to_group = l_associated_to_group
1190             -- AND    group_id = l_group_id
1191             AND    counter_id = l_counter_id;
1192 
1193             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_ASSOC');
1194          EXCEPTION
1195             WHEN NO_DATA_FOUND THEN NULL;
1196             WHEN TOO_MANY_ROWS THEN
1197                CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_ASSOC');
1198          END;
1199       ELSE
1200          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_PARM_GRP_NAME');
1201       END IF;
1202    ELSE
1203       IF l_group_id IS NULL THEN
1204          IF l_counter_id IS NULL THEN
1205             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_CTR_ID');
1206          END IF;
1207 
1208          BEGIN
1209             SELECT 'Y'
1210             INTO   l_item_found
1211             FROM   csi_ctr_item_associations
1212             WHERE  inventory_item_id = l_inventory_item_id
1213             AND    associated_to_group = 'Y';
1214 
1215          EXCEPTION
1216             WHEN NO_DATA_FOUND THEN
1217                l_item_found := 'N';
1218             WHEN TOO_MANY_ROWS THEN
1219                l_item_found := 'Y';
1220          END;
1221 
1222          IF l_item_found = 'Y' THEN
1223             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_ASSOC');
1224          ELSE
1225             BEGIN
1226                SELECT 'x'
1227                INTO   l_item_invalid
1228                FROM   csi_ctr_item_associations
1229                WHERE  inventory_item_id = l_inventory_item_id
1230                AND    associated_to_group = l_associated_to_group
1231                AND    group_id IS NULL
1232                AND    counter_id = l_counter_id;
1233 
1234                CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_ASSOC');
1235             EXCEPTION
1236                WHEN NO_DATA_FOUND THEN NULL;
1237                WHEN OTHERS THEN
1238                   CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_ASSOC');
1239             END;
1240          END IF;
1241       ELSE
1242          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_GRP_NULL');
1243       END IF;
1244    END IF;
1245 
1246    /* Call the table Handler */
1247    CSI_CTR_ITEM_ASSOCIATIONS_PKG.Insert_Row(
1248 	px_CTR_ASSOCIATION_ID           => l_ctr_association_id
1249 	,p_GROUP_ID                     => l_group_id
1250 	,p_INVENTORY_ITEM_ID            => l_inventory_item_id
1251 	,p_OBJECT_VERSION_NUMBER        => 1
1252 	,p_LAST_UPDATE_DATE             => sysdate
1253 	,p_LAST_UPDATED_BY              => FND_GLOBAL.USER_ID
1254 	,p_LAST_UPDATE_LOGIN            => FND_GLOBAL.USER_ID
1255 	,p_CREATION_DATE                => sysdate
1256         ,p_CREATED_BY                   => FND_GLOBAL.USER_ID
1257         ,p_ATTRIBUTE1                   => l_attribute1
1258         ,p_ATTRIBUTE2                   => l_attribute2
1259         ,p_ATTRIBUTE3                   => l_attribute3
1260         ,p_ATTRIBUTE4                   => l_attribute4
1261         ,p_ATTRIBUTE5                   => l_attribute5
1262         ,p_ATTRIBUTE6                   => l_attribute6
1263         ,p_ATTRIBUTE7                   => l_attribute7
1264         ,p_ATTRIBUTE8                   => l_attribute8
1265         ,p_ATTRIBUTE9                   => l_attribute9
1266         ,p_ATTRIBUTE10                  => l_attribute10
1267         ,p_ATTRIBUTE11                  => l_attribute11
1268         ,p_ATTRIBUTE12                  => l_attribute12
1269         ,p_ATTRIBUTE13                  => l_attribute13
1270         ,p_ATTRIBUTE14                  => l_attribute14
1271         ,p_ATTRIBUTE15                  => l_attribute15
1272 	,p_ATTRIBUTE_CATEGORY           => l_attribute_category
1273 	,p_SECURITY_GROUP_ID            => l_security_group_id
1274 	,p_MIGRATED_FLAG                => l_migrated_flag
1275 	,p_COUNTER_ID                   => l_counter_id
1276         ,p_START_DATE_ACTIVE            => l_start_date_active
1277         ,p_END_DATE_ACTIVE              => l_end_date_active
1278         ,p_USAGE_RATE                   => l_usage_rate
1279         -- ,p_ASSOCIATION_TYPE             => l_association_type
1280         ,p_USE_PAST_READING             => l_use_past_reading
1281 	,p_ASSOCIATED_TO_GROUP          => l_associated_to_group
1282 	,p_MAINT_ORGANIZATION_ID        => NULL
1283 	,p_PRIMARY_FAILURE_FLAG         => l_primary_failure_flag
1284         );
1285 
1286 
1287    IF NOT (x_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
1288         ROLLBACK TO create_item_association_pvt;
1289         RETURN;
1290    END IF;
1291 
1292    /* End of table handler call */
1293 
1294    IF FND_API.to_Boolean(nvl(p_commit,FND_API.G_FALSE)) THEN
1295       COMMIT WORK;
1296    END IF;
1297 
1298    -- Standard call to get message count and IF count is  get message info.
1299    FND_MSG_PUB.Count_And_Get
1300       ( p_count  =>  x_msg_count,
1301         p_data   =>  x_msg_data
1302       );
1303 EXCEPTION
1304    WHEN FND_API.G_EXC_ERROR THEN
1305       x_return_status := FND_API.G_RET_STS_ERROR ;
1306       ROLLBACK TO create_item_association_pvt;
1307       FND_MSG_PUB.Count_And_Get
1308          ( p_count => x_msg_count,
1309            p_data  => x_msg_data
1310          );
1311    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1312       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1313       ROLLBACK TO create_item_association_pvt;
1314       FND_MSG_PUB.Count_And_Get
1315          ( p_count => x_msg_count,
1316            p_data  => x_msg_data
1317          );
1318    WHEN OTHERS THEN
1319       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1320       ROLLBACK TO create_item_association_pvt;
1321       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
1322          FND_MSG_PUB.Add_Exc_Msg
1323             ( G_PKG_NAME,
1324               l_api_name
1325             );
1326       END IF;
1327       FND_MSG_PUB.Count_And_Get
1328          ( p_count  => x_msg_count,
1329            p_data   => x_msg_data
1330          );
1331 END create_item_association;
1332 
1333 --|---------------------------------------------------
1334 --| procedure name: create_counter_template
1335 --| description :   procedure used to
1336 --|                 create counter template
1337 --|---------------------------------------------------
1338 
1339 PROCEDURE create_counter_template
1340  (
1341      p_api_version               IN     NUMBER
1342     ,p_commit                    IN     VARCHAR2
1343     ,p_init_msg_list             IN     VARCHAR2
1344     ,p_validation_level          IN     NUMBER
1345     ,p_counter_template_rec      IN OUT NOCOPY CSI_CTR_DATASTRUCTURES_PUB.counter_template_rec
1346     ,x_return_status                OUT NOCOPY VARCHAR2
1347     ,x_msg_count                    OUT NOCOPY NUMBER
1348     ,x_msg_data                     OUT NOCOPY VARCHAR2
1349  ) IS
1350    l_api_name                     CONSTANT VARCHAR2(30)   := 'CREATE_COUNTER_TEMPLATE';
1351    l_api_version                  CONSTANT NUMBER         := 1.0;
1352    l_msg_data                     VARCHAR2(2000);
1353    l_msg_index                    NUMBER;
1354    l_msg_count                    NUMBER;
1355    -- l_debug_level                  NUMBER;
1356 
1357    l_counter_id                    NUMBER;
1358    l_group_id                      NUMBER;
1359    l_name                          VARCHAR2(50);
1360    l_description                   VARCHAR2(240);
1361    l_counter_type                  VARCHAR2(30);
1362    l_uom_code                      VARCHAR2(3);
1363    l_usage_item_id                 NUMBER;
1364    l_reading_type                  NUMBER;
1365    l_direction                     VARCHAR2(1);
1366    l_customer_view                 VARCHAR2(1);
1367    l_estimation_id                 NUMBER;
1368    l_derive_function               VARCHAR2(30);
1369    l_formula_text                  VARCHAR2(1996);
1370    l_derive_counter_id             NUMBER;
1371    l_filter_type                   VARCHAR2(30);
1372    l_filter_reading_count          NUMBER;
1373    l_filter_time_uom               VARCHAR2(30);
1374    l_automatic_rollover            VARCHAR2(1);
1375    l_rollover_last_reading         NUMBER;
1376    l_rollover_first_reading        NUMBER;
1377    l_tolerance_plus                NUMBER;
1378    l_tolerance_minus               NUMBER;
1379    l_formula_incomplete_flag       VARCHAR2(1);
1380    l_used_in_scheduling            VARCHAR2(1);
1381    l_initial_reading               NUMBER;
1382    l_default_usage_rate            NUMBER;
1383    l_use_past_reading              NUMBER;
1384    l_start_date_active             DATE;
1385    l_end_date_active               DATE;
1386    l_initial_reading_date          DATE;
1387    l_defaulted_group_id            NUMBER;
1388    l_step_value                    NUMBER;
1389    l_security_group_id             NUMBER;
1390    l_created_from_counter_tmpl_id  NUMBER;
1391    l_attribute1                    VARCHAR2(150);
1392    l_attribute2                    VARCHAR2(150);
1393    l_attribute3                    VARCHAR2(150);
1394    l_attribute4                    VARCHAR2(150);
1395    l_attribute5                    VARCHAR2(150);
1396    l_attribute6                    VARCHAR2(150);
1397    l_attribute7                    VARCHAR2(150);
1398    l_attribute8                    VARCHAR2(150);
1399    l_attribute9                    VARCHAR2(150);
1400    l_attribute10                   VARCHAR2(150);
1401    l_attribute11                   VARCHAR2(150);
1402    l_attribute12                   VARCHAR2(150);
1403    l_attribute13                   VARCHAR2(150);
1404    l_attribute14                   VARCHAR2(150);
1405    l_attribute15                   VARCHAR2(150);
1406    l_attribute16                   VARCHAR2(150);
1407    l_attribute17                   VARCHAR2(150);
1408    l_attribute18                   VARCHAR2(150);
1409    l_attribute19                   VARCHAR2(150);
1410    l_attribute20                   VARCHAR2(150);
1411    l_attribute21                   VARCHAR2(150);
1412    l_attribute22                   VARCHAR2(150);
1413    l_attribute23                   VARCHAR2(150);
1414    l_attribute24                   VARCHAR2(150);
1415    l_attribute25                   VARCHAR2(150);
1416    l_attribute26                   VARCHAR2(150);
1417    l_attribute27                   VARCHAR2(150);
1418    l_attribute28                   VARCHAR2(150);
1419    l_attribute29                   VARCHAR2(150);
1420    l_attribute30                   VARCHAR2(150);
1421    l_attribute_category            VARCHAR2(30);
1422    l_association_type              VARCHAR2(30);
1423    l_migrated_flag                 VARCHAR2(1);
1424    l_dummy                         VARCHAR2(1);
1425    l_time_based_manual_entry       VARCHAR2(1);
1426    l_eam_required_flag             VARCHAR2(1);
1427 
1428    l_counter_groups_rec   CSI_CTR_DATASTRUCTURES_PUB.counter_groups_rec;
1429    l_valid_start_date             DATE;
1430 
1431    CURSOR c1_group(p_group_id NUMBER) IS
1432    select start_date_active
1433    from   cs_csi_counter_groups
1434    where  counter_group_id = p_group_id;
1435 
1436 BEGIN
1437    -- Standard Start of API savepoint
1438    SAVEPOINT  create_counter_template_pvt;
1439 
1440    -- Standard call to check for call compatibility.
1441    IF NOT FND_API.Compatible_API_Call (l_api_version,
1442                                        p_api_version,
1443                                        l_api_name   ,
1444                                        G_PKG_NAME   ) THEN
1445       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1446    END IF;
1447 
1448    -- Initialize message list if p_init_msg_list is set to TRUE.
1449    IF FND_API.to_Boolean(nvl(p_init_msg_list,FND_API.G_FALSE)) THEN
1450       FND_MSG_PUB.initialize;
1451    END IF;
1452 
1453    -- Read the debug profiles values in to global variable 7197402
1454    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
1455 
1456    --  Initialize API return status to success
1457    x_return_status := FND_API.G_RET_STS_SUCCESS;
1458 
1459    --
1460    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
1461       csi_ctr_gen_utility_pvt.put_line
1462          ( 'create_counter_template_pvt'           ||'-'||
1463            p_api_version                              ||'-'||
1464            nvl(p_commit,FND_API.G_FALSE)              ||'-'||
1465            nvl(p_init_msg_list,FND_API.G_FALSE)       ||'-'||
1466            nvl(p_validation_level,FND_API.G_VALID_LEVEL_FULL) );
1467    END IF;
1468 
1469    if p_counter_template_rec.group_id = FND_API.G_MISS_NUM then
1470        l_group_id := null;
1471    else
1472        l_group_id := p_counter_template_rec.group_id;
1473    end if;
1474 
1475    if p_counter_template_rec.counter_id = FND_API.G_MISS_NUM then
1476        l_counter_id := null;
1477    else
1478        l_counter_id := p_counter_template_rec.counter_id;
1479    end if;
1480 
1481    if p_counter_template_rec.name = FND_API.G_MISS_CHAR then
1482       l_name := null;
1483    else
1484       l_name := p_counter_template_rec.name;
1485    end if;
1486 
1487    if p_counter_template_rec.description = FND_API.G_MISS_CHAR then
1488       l_description := null;
1489    else
1490       l_description := p_counter_template_rec.description;
1491    end if;
1492 
1493    if p_counter_template_rec.counter_type = FND_API.G_MISS_CHAR then
1494       l_counter_type := null;
1495    else
1496       l_counter_type := p_counter_template_rec.counter_type;
1497    end if;
1498 
1499    if p_counter_template_rec.uom_code = FND_API.G_MISS_CHAR then
1500        l_uom_code := null;
1501    else
1502        l_uom_code := p_counter_template_rec.uom_code;
1503    end if;
1504 
1505    if p_counter_template_rec.usage_item_id = FND_API.G_MISS_NUM then
1506        l_usage_item_id := null;
1507    else
1508        l_usage_item_id := p_counter_template_rec.usage_item_id;
1509    end if;
1510 
1511    if p_counter_template_rec.reading_type = FND_API.G_MISS_NUM then
1512        l_reading_type := null;
1513    else
1514        l_reading_type := p_counter_template_rec.reading_type;
1515    end if;
1516 
1517    if p_counter_template_rec.step_value = FND_API.G_MISS_NUM then
1518        l_step_value := null;
1519    else
1520        l_step_value := p_counter_template_rec.step_value;
1521    end if;
1522 
1523    if p_counter_template_rec.direction = FND_API.G_MISS_CHAR then
1524        l_direction := null;
1525    else
1526        l_direction := p_counter_template_rec.direction;
1527    end if;
1528 
1529    if p_counter_template_rec.estimation_id = FND_API.G_MISS_NUM then
1530        l_estimation_id := null;
1531    else
1532        l_estimation_id := p_counter_template_rec.estimation_id;
1533    end if;
1534 
1535    if p_counter_template_rec.derive_function = FND_API.G_MISS_CHAR then
1536        l_derive_function := null;
1537    else
1538        l_derive_function := p_counter_template_rec.derive_function;
1539    end if;
1540 
1541    if p_counter_template_rec.formula_text = FND_API.G_MISS_CHAR then
1542        l_formula_text := null;
1543    else
1544        l_formula_text := p_counter_template_rec.formula_text;
1545    end if;
1546 
1547    if p_counter_template_rec.derive_counter_id = FND_API.G_MISS_NUM then
1548        l_derive_counter_id := null;
1549    else
1550        l_derive_counter_id := p_counter_template_rec.derive_counter_id;
1551    end if;
1552 
1553    if p_counter_template_rec.filter_type = FND_API.G_MISS_CHAR then
1554        l_filter_type := null;
1555    else
1556        l_filter_type := p_counter_template_rec.filter_type;
1557    end if;
1558 
1559    if p_counter_template_rec.filter_reading_count = FND_API.G_MISS_NUM then
1560        l_filter_reading_count := null;
1561    else
1562        l_filter_reading_count := p_counter_template_rec.filter_reading_count;
1563    end if;
1564 
1565    if p_counter_template_rec.filter_time_uom = FND_API.G_MISS_CHAR then
1566        l_filter_time_uom := null;
1567    else
1568        l_filter_time_uom := p_counter_template_rec.filter_time_uom;
1569    end if;
1570 
1571    if p_counter_template_rec.automatic_rollover = FND_API.G_MISS_CHAR then
1572        l_automatic_rollover := null;
1573    else
1574        l_automatic_rollover := p_counter_template_rec.automatic_rollover;
1575    end if;
1576 
1577    if p_counter_template_rec.rollover_last_reading = FND_API.G_MISS_NUM then
1578        l_rollover_last_reading := null;
1579    else
1580        l_rollover_last_reading := p_counter_template_rec.rollover_last_reading;
1581    end if;
1582 
1583    if p_counter_template_rec.rollover_first_reading = FND_API.G_MISS_NUM then
1584        l_rollover_first_reading := null;
1585    else
1586        l_rollover_first_reading := p_counter_template_rec.rollover_first_reading;
1587    end if;
1588 
1589    if p_counter_template_rec.tolerance_plus = FND_API.G_MISS_NUM then
1590        l_tolerance_plus := null;
1591    else
1592        l_tolerance_plus := p_counter_template_rec.tolerance_plus;
1593    end if;
1594 
1595    if p_counter_template_rec.security_group_id = FND_API.G_MISS_NUM then
1596        l_security_group_id := null;
1597    else
1598        l_security_group_id := p_counter_template_rec.security_group_id;
1599    end if;
1600 
1601    if p_counter_template_rec.tolerance_minus = FND_API.G_MISS_NUM then
1602        l_tolerance_minus := null;
1603    else
1604        l_tolerance_minus := p_counter_template_rec.tolerance_minus;
1605    end if;
1606 
1607    if p_counter_template_rec.used_in_scheduling = FND_API.G_MISS_CHAR then
1608        l_used_in_scheduling := null;
1609    else
1610        l_used_in_scheduling := p_counter_template_rec.used_in_scheduling;
1611    end if;
1612 
1613    if p_counter_template_rec.initial_reading = FND_API.G_MISS_NUM then
1614        l_initial_reading := null;
1615    else
1616        l_initial_reading := p_counter_template_rec.initial_reading;
1617    end if;
1618 
1619    if p_counter_template_rec.default_usage_rate = FND_API.G_MISS_NUM then
1620        l_default_usage_rate := null;
1621    else
1622        l_default_usage_rate := p_counter_template_rec.default_usage_rate;
1623    end if;
1624 
1625    if p_counter_template_rec.use_past_reading = FND_API.G_MISS_NUM then
1626        l_use_past_reading := null;
1627    else
1628        l_use_past_reading := p_counter_template_rec.use_past_reading;
1629    end if;
1630 
1631    if nvl(p_counter_template_rec.start_date_active,FND_API.G_MISS_DATE) = FND_API.G_MISS_DATE then
1632        l_start_date_active := sysdate;
1633    else
1634        l_start_date_active := p_counter_template_rec.start_date_active;
1635    end if;
1636 
1637    validate_start_date(l_start_date_active);
1638 
1639    IF l_group_id IS NOT NULL THEN
1640       OPEN c1_group(l_group_id);
1641       FETCH c1_group INTO l_valid_start_date;
1642 
1643       IF l_start_date_active < l_valid_start_date THEN
1644          csi_ctr_gen_utility_pvt.ExitWithErrMsg
1645              ( p_msg_name     =>  'CSI_API_CTR_VAL_ST_DATE',
1646                p_token1_name  =>  'PARAM',
1647                p_token1_val   =>  'The counter template start date cannot be less than the active start date of the counter group.');
1648       END IF;
1649       CLOSE c1_group;
1650 
1651    END IF;
1652 
1653    if p_counter_template_rec.end_date_active = FND_API.G_MISS_DATE then
1654        l_end_date_active := null;
1655    else
1656        l_end_date_active := p_counter_template_rec.end_date_active;
1657    end if;
1658 
1659    if l_end_date_active IS NOT NULL then
1660       if l_end_date_active < l_start_date_active then
1661          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_ALL_END_DATE');
1662       end if;
1663    end if;
1664 
1665    if p_counter_template_rec.attribute1 = FND_API.G_MISS_CHAR then
1666        l_attribute1 := null;
1667    else
1668        l_attribute1 := p_counter_template_rec.attribute1;
1669    end if;
1670 
1671    if p_counter_template_rec.attribute2 = FND_API.G_MISS_CHAR then
1672        l_attribute2 := null;
1673    else
1674        l_attribute2 := p_counter_template_rec.attribute2;
1675    end if;
1676 
1677    if p_counter_template_rec.attribute3 = FND_API.G_MISS_CHAR then
1678        l_attribute3 := null;
1679    else
1680        l_attribute3 := p_counter_template_rec.attribute3;
1681    end if;
1682 
1683    if p_counter_template_rec.attribute4 = FND_API.G_MISS_CHAR then
1684        l_attribute4 := null;
1685    else
1686        l_attribute4 := p_counter_template_rec.attribute4;
1687    end if;
1688 
1689    if p_counter_template_rec.attribute5 = FND_API.G_MISS_CHAR then
1690        l_attribute5 := null;
1691    else
1692        l_attribute5 := p_counter_template_rec.attribute5;
1693    end if;
1694 
1695    if p_counter_template_rec.attribute6 = FND_API.G_MISS_CHAR then
1696        l_attribute6 := null;
1697    else
1698        l_attribute6 := p_counter_template_rec.attribute6;
1699    end if;
1700 
1701    if p_counter_template_rec.attribute7 = FND_API.G_MISS_CHAR then
1702       l_attribute7 := null;
1703    else
1704        l_attribute7 := p_counter_template_rec.attribute7;
1705    end if;
1706 
1707    if p_counter_template_rec.attribute8 = FND_API.G_MISS_CHAR then
1708       l_attribute8 := null;
1709    else
1710       l_attribute8 := p_counter_template_rec.attribute8;
1711    end if;
1712 
1713    if p_counter_template_rec.attribute9 = FND_API.G_MISS_CHAR then
1714       l_attribute9 := null;
1715    else
1716        l_attribute9 := p_counter_template_rec.attribute9;
1717    end if;
1718 
1719    if p_counter_template_rec.attribute10 = FND_API.G_MISS_CHAR then
1720       l_attribute10 := null;
1721    else
1722       l_attribute10 := p_counter_template_rec.attribute10;
1723    end if;
1724 
1725    if p_counter_template_rec.attribute11 = FND_API.G_MISS_CHAR then
1726       l_attribute11 := null;
1727    else
1728       l_attribute11 := p_counter_template_rec.attribute11;
1729    end if;
1730 
1731    if p_counter_template_rec.attribute12 = FND_API.G_MISS_CHAR then
1732       l_attribute12 := null;
1733    else
1734       l_attribute12 := p_counter_template_rec.attribute12;
1735    end if;
1736 
1737    if p_counter_template_rec.attribute13 = FND_API.G_MISS_CHAR then
1738       l_attribute13 := null;
1739    else
1740       l_attribute13 := p_counter_template_rec.attribute13;
1741    end if;
1742 
1743    if p_counter_template_rec.attribute14 = FND_API.G_MISS_CHAR then
1744       l_attribute14 := null;
1745    else
1746       l_attribute14 := p_counter_template_rec.attribute14;
1747    end if;
1748 
1749    if p_counter_template_rec.attribute15 = FND_API.G_MISS_CHAR then
1750       l_attribute15 := null;
1751    else
1752       l_attribute15 := p_counter_template_rec.attribute15;
1753    end if;
1754 
1755    if p_counter_template_rec.attribute16 = FND_API.G_MISS_CHAR then
1756        l_attribute16 := null;
1757    else
1758        l_attribute16 := p_counter_template_rec.attribute16;
1759    end if;
1760 
1761    if p_counter_template_rec.attribute17 = FND_API.G_MISS_CHAR then
1762       l_attribute17 := null;
1763    else
1764        l_attribute17 := p_counter_template_rec.attribute17;
1765    end if;
1766 
1767    if p_counter_template_rec.attribute18 = FND_API.G_MISS_CHAR then
1768       l_attribute18 := null;
1769    else
1770       l_attribute18 := p_counter_template_rec.attribute18;
1771    end if;
1772 
1773    if p_counter_template_rec.attribute19 = FND_API.G_MISS_CHAR then
1774       l_attribute19 := null;
1775    else
1776        l_attribute19 := p_counter_template_rec.attribute19;
1777    end if;
1778 
1779    if p_counter_template_rec.attribute20 = FND_API.G_MISS_CHAR then
1780       l_attribute20 := null;
1781    else
1782       l_attribute20 := p_counter_template_rec.attribute20;
1783    end if;
1784 
1785    if p_counter_template_rec.attribute21 = FND_API.G_MISS_CHAR then
1786        l_attribute21 := null;
1787    else
1788        l_attribute21 := p_counter_template_rec.attribute21;
1789    end if;
1790 
1791    if p_counter_template_rec.attribute22 = FND_API.G_MISS_CHAR then
1792        l_attribute22 := null;
1793    else
1794        l_attribute22 := p_counter_template_rec.attribute22;
1795    end if;
1796 
1797    if p_counter_template_rec.attribute23 = FND_API.G_MISS_CHAR then
1798        l_attribute23 := null;
1799    else
1800        l_attribute23 := p_counter_template_rec.attribute23;
1801    end if;
1802 
1803    if p_counter_template_rec.attribute24 = FND_API.G_MISS_CHAR then
1804        l_attribute24 := null;
1805    else
1806        l_attribute24 := p_counter_template_rec.attribute24;
1807    end if;
1808 
1809    if p_counter_template_rec.attribute25 = FND_API.G_MISS_CHAR then
1810        l_attribute25 := null;
1811    else
1812        l_attribute25 := p_counter_template_rec.attribute25;
1813    end if;
1814 
1815    if p_counter_template_rec.attribute26 = FND_API.G_MISS_CHAR then
1816        l_attribute26 := null;
1817    else
1818        l_attribute26 := p_counter_template_rec.attribute26;
1819    end if;
1820 
1821    if p_counter_template_rec.attribute27 = FND_API.G_MISS_CHAR then
1822       l_attribute27 := null;
1823    else
1824        l_attribute27 := p_counter_template_rec.attribute27;
1825    end if;
1826 
1827    if p_counter_template_rec.attribute28 = FND_API.G_MISS_CHAR then
1828       l_attribute28 := null;
1829    else
1830       l_attribute28 := p_counter_template_rec.attribute28;
1831    end if;
1832 
1833    if p_counter_template_rec.attribute29 = FND_API.G_MISS_CHAR then
1834       l_attribute29 := null;
1835    else
1836        l_attribute29 := p_counter_template_rec.attribute29;
1837    end if;
1838 
1839    if p_counter_template_rec.attribute30 = FND_API.G_MISS_CHAR then
1840       l_attribute30 := null;
1841    else
1842       l_attribute30 := p_counter_template_rec.attribute30;
1843    end if;
1844 
1845    if p_counter_template_rec.attribute_category = FND_API.G_MISS_CHAR then
1846       l_attribute_category := null;
1847    else
1848       l_attribute_category := p_counter_template_rec.attribute_category;
1849    end if;
1850 
1851    if p_counter_template_rec.association_type = FND_API.G_MISS_CHAR then
1852        l_association_type := null;
1853    else
1854        l_association_type := p_counter_template_rec.association_type;
1855    end if;
1856 
1857    if l_association_type is null then
1858       l_association_type := 'TRACKABLE';
1859    end if;
1860 
1861    if p_counter_template_rec.group_id = FND_API.G_MISS_NUM then
1862        l_group_id := null;
1863    else
1864        l_group_id := p_counter_template_rec.group_id;
1865    end if;
1866 
1867    if p_counter_template_rec.time_based_manual_entry = FND_API.G_MISS_CHAR then
1868        l_time_based_manual_entry := 'N';
1869    else
1870        l_time_based_manual_entry := p_counter_template_rec.time_based_manual_entry;
1871    end if;
1872 
1873    if p_counter_template_rec.eam_required_flag = FND_API.G_MISS_CHAR then
1874        l_eam_required_flag := null;
1875    else
1876        l_eam_required_flag := p_counter_template_rec.eam_required_flag;
1877    end if;
1878 
1879    if nvl(p_counter_template_rec.initial_reading_date, FND_API.G_MISS_DATE) = FND_API.G_MISS_DATE then
1880       l_initial_reading_date := sysdate;
1881    else
1882       l_initial_reading_date := p_counter_template_rec.initial_reading_date;
1883    end if;
1884 
1885    if l_group_id IS NULL THEN
1886       l_counter_groups_rec.NAME  :=  l_name||'-Group';
1887       l_counter_groups_rec.DESCRIPTION   := l_name||'-Group';
1888 
1889       create_counter_group
1890         (
1891          p_api_version        => p_api_version
1892          ,p_commit             => p_commit
1893          ,p_init_msg_list      => p_init_msg_list
1894          ,p_validation_level   => p_validation_level
1895          ,p_counter_groups_rec => l_counter_groups_rec
1896          ,x_return_status      => x_return_status
1897          ,x_msg_count          => x_msg_count
1898          ,x_msg_data           => x_msg_data
1899         );
1900 
1901        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1902           l_msg_index := 1;
1903           l_msg_count := x_msg_count;
1904 
1905           WHILE l_msg_count > 0 LOOP
1906              x_msg_data := FND_MSG_PUB.GET
1907                            (l_msg_index,
1908                             FND_API.G_FALSE );
1909              csi_ctr_gen_utility_pvt.put_line('Error from CSI_COUNTER_PVT.CREATE_COUNTER_GROUP');
1910              csi_ctr_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
1911              l_msg_index := l_msg_index + 1;
1912              l_msg_count := l_msg_count - 1;
1913           END LOOP;
1914           RAISE FND_API.G_EXC_ERROR;
1915       END IF;
1916 
1917       l_defaulted_group_id := l_counter_groups_rec.counter_group_id;
1918    else
1919       l_defaulted_group_id := l_group_id;
1920    end if;
1921 
1922    --call validate counter to validate the counter instance
1923    validate_counter(l_group_id, l_name, l_counter_type, l_uom_code,
1924                     l_usage_item_id, l_reading_type, l_direction,
1925                     l_estimation_id, l_derive_function, l_formula_text,
1926                     l_derive_counter_id, l_filter_type, l_filter_reading_count,
1927                     l_filter_time_uom, l_automatic_rollover,
1928                     l_rollover_last_reading, l_rollover_first_reading,
1929                     l_tolerance_plus, l_tolerance_minus, l_used_in_scheduling,
1930                     l_initial_reading, l_default_usage_rate,
1931                     l_use_past_reading, -1,
1932                     l_start_date_active, l_end_date_active,'N'
1933                    );
1934 
1935    /* Call the table Handler */
1936    CSI_COUNTER_TEMPLATE_PKG.Insert_Row(
1937 	    px_COUNTER_ID	        => l_counter_id
1938 	   ,p_GROUP_ID                  => l_group_id
1939 	   ,p_COUNTER_TYPE              => l_counter_type
1940 	   ,p_INITIAL_READING           => l_initial_reading
1941 	   ,p_INITIAL_READING_DATE      => l_initial_reading_date
1942 	   ,p_TOLERANCE_PLUS            => l_tolerance_plus
1943 	   ,p_TOLERANCE_MINUS           => l_tolerance_minus
1944 	   ,p_UOM_CODE                  => l_uom_code
1945 	   ,p_DERIVE_COUNTER_ID         => l_derive_counter_id
1946 	   ,p_DERIVE_FUNCTION           => l_derive_function
1947 	   ,p_DERIVE_PROPERTY_ID        => null
1948 	   ,p_VALID_FLAG                => 'Y'
1949 	   ,p_FORMULA_INCOMPLETE_FLAG   => l_formula_incomplete_flag
1950 	   ,p_FORMULA_TEXT              => l_formula_text
1951 	   ,p_ROLLOVER_LAST_READING     => l_rollover_last_reading
1952 	   ,p_ROLLOVER_FIRST_READING	=> l_rollover_first_reading
1953 	   ,p_USAGE_ITEM_ID             => l_usage_item_id
1954 	   ,p_CTR_VAL_MAX_SEQ_NO        => 1
1955 	   ,p_START_DATE_ACTIVE         => l_start_date_active
1956 	   ,p_END_DATE_ACTIVE           => l_end_date_active
1957 	   ,p_OBJECT_VERSION_NUMBER     => 1
1958            ,p_SECURITY_GROUP_ID         => l_security_group_id
1959 	   ,p_LAST_UPDATE_DATE          => sysdate
1960 	   ,p_LAST_UPDATED_BY           => FND_GLOBAL.USER_ID
1961 	   ,p_CREATION_DATE             => sysdate
1962 	   ,p_CREATED_BY                => FND_GLOBAL.USER_ID
1963 	   ,p_LAST_UPDATE_LOGIN         => FND_GLOBAL.USER_ID
1964 	   ,p_ATTRIBUTE1                => l_attribute1
1965 	   ,p_ATTRIBUTE2                => l_attribute2
1966 	   ,p_ATTRIBUTE3                => l_attribute3
1967 	   ,p_ATTRIBUTE4                => l_attribute4
1968 	   ,p_ATTRIBUTE5                => l_attribute5
1969 	   ,p_ATTRIBUTE6                => l_attribute6
1970 	   ,p_ATTRIBUTE7                => l_attribute7
1971 	   ,p_ATTRIBUTE8                => l_attribute8
1972 	   ,p_ATTRIBUTE9                => l_attribute9
1973 	   ,p_ATTRIBUTE10               => l_attribute10
1974 	   ,p_ATTRIBUTE11               => l_attribute11
1975 	   ,p_ATTRIBUTE12               => l_attribute12
1976 	   ,p_ATTRIBUTE13               => l_attribute13
1977 	   ,p_ATTRIBUTE14               => l_attribute14
1978 	   ,p_ATTRIBUTE15               => l_attribute15
1979 	   ,p_ATTRIBUTE16               => l_attribute16
1980 	   ,p_ATTRIBUTE17               => l_attribute17
1981 	   ,p_ATTRIBUTE18               => l_attribute18
1982 	   ,p_ATTRIBUTE19               => l_attribute19
1983 	   ,p_ATTRIBUTE20               => l_attribute20
1984 	   ,p_ATTRIBUTE21               => l_attribute21
1985 	   ,p_ATTRIBUTE22               => l_attribute22
1986 	   ,p_ATTRIBUTE23               => l_attribute23
1987 	   ,p_ATTRIBUTE24               => l_attribute24
1988 	   ,p_ATTRIBUTE25               => l_attribute25
1989 	   ,p_ATTRIBUTE26               => l_attribute26
1990 	   ,p_ATTRIBUTE27               => l_attribute27
1991 	   ,p_ATTRIBUTE28               => l_attribute28
1992 	   ,p_ATTRIBUTE29               => l_attribute29
1993 	   ,p_ATTRIBUTE30               => l_attribute30
1994 	   ,p_ATTRIBUTE_CATEGORY        => l_attribute_category
1995 	   ,p_MIGRATED_FLAG             => l_migrated_flag
1996 	   ,p_CUSTOMER_VIEW             => l_customer_view
1997 	   ,p_DIRECTION                 => l_direction
1998 	   ,p_FILTER_TYPE               => l_filter_type
1999 	   ,p_FILTER_READING_COUNT      => l_filter_reading_count
2000 	   ,p_FILTER_TIME_UOM           => l_filter_time_uom
2001 	   ,p_ESTIMATION_ID             => l_estimation_id
2002 	   ,p_ASSOCIATION_TYPE          => l_association_type
2003 	   ,p_READING_TYPE              => l_reading_type
2004 	   ,p_AUTOMATIC_ROLLOVER        => l_automatic_rollover
2005 	   ,p_DEFAULT_USAGE_RATE        => l_default_usage_rate
2006 	   ,p_USE_PAST_READING          => l_use_past_reading
2007 	   ,p_USED_IN_SCHEDULING        => l_used_in_scheduling
2008 	   ,p_DEFAULTED_GROUP_ID        => l_defaulted_group_id
2009 	   ,p_STEP_VALUE                => l_step_value
2010 	   ,p_NAME	                => l_name
2011 	   ,p_DESCRIPTION               => l_description
2012 	   ,p_TIME_BASED_MANUAL_ENTRY   => l_time_based_manual_entry
2013 	   ,p_EAM_REQUIRED_FLAG         => l_eam_required_flag
2014       );
2015 
2016    p_counter_template_rec.counter_id := l_counter_id;
2017 
2018    IF NOT (x_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
2019         ROLLBACK TO create_counter_template_pvt;
2020         RETURN;
2021    END IF;
2022 
2023    /* End of table handler call */
2024 
2025    IF FND_API.to_Boolean(nvl(p_commit,FND_API.G_FALSE)) THEN
2026       COMMIT WORK;
2027    END IF;
2028 
2029    -- Standard call to get message count and IF count is  get message info.
2030    FND_MSG_PUB.Count_And_Get
2031       ( p_count  =>  x_msg_count,
2032         p_data   =>  x_msg_data
2033       );
2034 EXCEPTION
2035    WHEN FND_API.G_EXC_ERROR THEN
2036       x_return_status := FND_API.G_RET_STS_ERROR ;
2037       ROLLBACK TO create_counter_template_pvt;
2038       FND_MSG_PUB.Count_And_Get
2039          ( p_count => x_msg_count,
2040            p_data  => x_msg_data
2041          );
2042    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2043       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2044       ROLLBACK TO create_counter_template_pvt;
2045       FND_MSG_PUB.Count_And_Get
2046          ( p_count => x_msg_count,
2047            p_data  => x_msg_data
2048          );
2049    WHEN OTHERS THEN
2050       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2051       ROLLBACK TO create_counter_template_pvt;
2052       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
2053          FND_MSG_PUB.Add_Exc_Msg
2054             ( G_PKG_NAME,
2055               l_api_name
2056             );
2057       END IF;
2058       FND_MSG_PUB.Count_And_Get
2059          ( p_count  => x_msg_count,
2060            p_data   => x_msg_data
2061          );
2062 END create_counter_template;
2063 
2064 --|---------------------------------------------------
2065 --| procedure name: create_ctr_property_template
2066 --| description :   procedure used to
2067 --|                 create counter properties
2068 --|---------------------------------------------------
2069 
2070 PROCEDURE create_ctr_property_template
2071  (
2072      p_api_version               IN     NUMBER
2073     ,p_commit                    IN     VARCHAR2
2074     ,p_init_msg_list             IN     VARCHAR2
2075     ,p_validation_level          IN     NUMBER
2076     ,p_ctr_property_template_rec IN OUT NOCOPY CSI_CTR_DATASTRUCTURES_PUB.ctr_property_template_rec
2077     ,x_return_status                OUT    NOCOPY VARCHAR2
2078     ,x_msg_count                    OUT    NOCOPY NUMBER
2079     ,x_msg_data                     OUT    NOCOPY VARCHAR2
2080  ) IS
2081    l_api_name                     CONSTANT VARCHAR2(30)   := 'CREATE_CTR_PROPERTY_TEMPLATE';
2082    l_api_version                  CONSTANT NUMBER         := 1.0;
2083    l_msg_data                     VARCHAR2(2000);
2084    l_msg_index                    NUMBER;
2085    l_msg_count                    NUMBER;
2086    -- l_debug_level                  NUMBER;
2087 
2088    l_COUNTER_PROPERTY_ID	  NUMBER;
2089    l_COUNTER_ID                   NUMBER;
2090    l_PROPERTY_DATA_TYPE           VARCHAR2(30);
2091    l_IS_NULLABLE                  VARCHAR2(1);
2092    l_DEFAULT_VALUE                VARCHAR2(240);
2093    l_MINIMUM_VALUE                VARCHAR2(240);
2094    l_MAXIMUM_VALUE                VARCHAR2(240);
2095    l_UOM_CODE                     VARCHAR2(3);
2096    l_LAST_UPDATE_DATE             DATE;
2097    l_LAST_UPDATED_BY              NUMBER;
2098    l_CREATION_DATE                DATE;
2099    l_CREATED_BY                   NUMBER;
2100    l_LAST_UPDATE_LOGIN            NUMBER;
2101    l_START_DATE_ACTIVE            DATE;
2102    l_END_DATE_ACTIVE              DATE;
2103    l_ATTRIBUTE1                   VARCHAR2(150);
2104    l_ATTRIBUTE2                   VARCHAR2(150);
2105    l_ATTRIBUTE3                   VARCHAR2(150);
2106    l_ATTRIBUTE4                   VARCHAR2(150);
2107    l_ATTRIBUTE5                   VARCHAR2(150);
2108    l_ATTRIBUTE6                   VARCHAR2(150);
2109    l_ATTRIBUTE7                   VARCHAR2(150);
2110    l_ATTRIBUTE8                   VARCHAR2(150);
2111    l_ATTRIBUTE9                   VARCHAR2(150);
2112    l_ATTRIBUTE10                  VARCHAR2(150);
2113    l_ATTRIBUTE11                  VARCHAR2(150);
2114    l_ATTRIBUTE12                  VARCHAR2(150);
2115    l_ATTRIBUTE13                  VARCHAR2(150);
2116    l_ATTRIBUTE14                  VARCHAR2(150);
2117    l_ATTRIBUTE15                  VARCHAR2(150);
2118    l_ATTRIBUTE_CATEGORY           VARCHAR2(30);
2119    l_OBJECT_VERSION_NUMBER        NUMBER;
2120    l_PROPERTY_LOV_TYPE            VARCHAR2(30);
2121    l_SECURITY_GROUP_ID            NUMBER;
2122    l_MIGRATED_FLAG                VARCHAR2(1);
2123    l_NAME                         VARCHAR2(50);
2124    l_DESCRIPTION                  VARCHAR2(240);
2125 BEGIN
2126    -- Standard Start of API savepoint
2127    SAVEPOINT  create_ctr_property_tmpl_pvt;
2128 
2129    -- Standard call to check for call compatibility.
2130    IF NOT FND_API.Compatible_API_Call (l_api_version,
2131                                        p_api_version,
2132                                        l_api_name   ,
2133                                        G_PKG_NAME   ) THEN
2134       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2135    END IF;
2136 
2137    -- Initialize message list if p_init_msg_list is set to TRUE.
2138    IF FND_API.to_Boolean(nvl(p_init_msg_list,FND_API.G_FALSE)) THEN
2139       FND_MSG_PUB.initialize;
2140    END IF;
2141 
2142    -- Read the debug profiles values in to global variable 7197402
2143    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
2144 
2145    --  Initialize API return status to success
2146    x_return_status := FND_API.G_RET_STS_SUCCESS;
2147 
2148    --
2149    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
2150       csi_ctr_gen_utility_pvt.put_line( 'create_ctr_property_tmpl_pvt'          ||'-'||
2151                                      p_api_version                              ||'-'||
2152                                      nvl(p_commit,FND_API.G_FALSE)              ||'-'||
2153                                      nvl(p_init_msg_list,FND_API.G_FALSE)       ||'-'||
2154                                      nvl(p_validation_level,FND_API.G_VALID_LEVEL_FULL) );
2155    END IF;
2156 
2157    if p_ctr_property_template_rec.name = FND_API.G_MISS_CHAR then
2158       l_name := null;
2159    else
2160       l_name := p_ctr_property_template_rec.name;
2161    end if;
2162 
2163    if p_ctr_property_template_rec.description = FND_API.G_MISS_CHAR then
2164       l_description := null;
2165    else
2166       l_description := p_ctr_property_template_rec.description;
2167    end if;
2168 
2169    if p_ctr_property_template_rec.counter_property_id = FND_API.G_MISS_NUM then
2170       l_counter_property_id := null;
2171    else
2172       l_counter_property_id := p_ctr_property_template_rec.counter_property_id;
2173    end if;
2174 
2175    if p_ctr_property_template_rec.property_data_type = FND_API.G_MISS_CHAR then
2176       l_property_data_type := null;
2177    else
2178       l_property_data_type := p_ctr_property_template_rec.property_data_type;
2179    end if;
2180 
2181    if p_ctr_property_template_rec.IS_NULLABLE= FND_API.G_MISS_CHAR then
2182       l_IS_NULLABLE := null;
2183    else
2184       l_IS_NULLABLE := p_ctr_property_template_rec.IS_NULLABLE;
2185    end if;
2186 
2187    if p_ctr_property_template_rec.DEFAULT_VALUE= FND_API.G_MISS_CHAR then
2188       l_DEFAULT_VALUE := null;
2189    else
2190       l_DEFAULT_VALUE := p_ctr_property_template_rec.DEFAULT_VALUE;
2191    end if;
2192 
2193    if p_ctr_property_template_rec.MINIMUM_VALUE= FND_API.G_MISS_CHAR then
2194       l_MINIMUM_VALUE := null;
2195    else
2196       l_MINIMUM_VALUE := p_ctr_property_template_rec.MINIMUM_VALUE;
2197    end if;
2198 
2199    if p_ctr_property_template_rec.MAXIMUM_VALUE = FND_API.G_MISS_CHAR then
2200       l_MAXIMUM_VALUE := null;
2201    else
2202       l_MAXIMUM_VALUE := p_ctr_property_template_rec.MAXIMUM_VALUE;
2203    end if;
2204 
2205    if p_ctr_property_template_rec.UOM_CODE = FND_API.G_MISS_CHAR then
2206       l_UOM_CODE := null;
2207    else
2208       l_UOM_CODE := p_ctr_property_template_rec.UOM_CODE;
2209    end if;
2210 
2211    if p_ctr_property_template_rec.property_lov_type = FND_API.G_MISS_CHAR then
2212       l_property_lov_type := null;
2213    else
2214       l_property_lov_type := p_ctr_property_template_rec.property_lov_type;
2215    end if;
2216 
2217    if p_ctr_property_template_rec.counter_id = FND_API.G_MISS_NUM then
2218       l_counter_id := null;
2219    else
2220       l_counter_id := p_ctr_property_template_rec.counter_id;
2221    end if;
2222 
2223    if p_ctr_property_template_rec.security_group_id = FND_API.G_MISS_NUM then
2224       l_security_group_id := null;
2225    else
2226       l_security_group_id := p_ctr_property_template_rec.security_group_id;
2227    end if;
2228 
2229    if nvl(p_ctr_property_template_rec.start_date_active,FND_API.G_MISS_DATE) = FND_API.G_MISS_DATE then
2230       l_start_date_active := sysdate;
2231    else
2232       l_start_date_active := p_ctr_property_template_rec.start_date_active;
2233    end if;
2234 
2235    if p_ctr_property_template_rec.end_date_active = FND_API.G_MISS_DATE then
2236       l_end_date_active := null;
2237    else
2238       l_end_date_active := p_ctr_property_template_rec.end_date_active;
2239    end if;
2240 
2241    if p_ctr_property_template_rec.attribute1 = FND_API.G_MISS_CHAR then
2242       l_attribute1 := null;
2243    else
2244       l_attribute1 := p_ctr_property_template_rec.attribute1;
2245    end if;
2246 
2247    if p_ctr_property_template_rec.attribute2 = FND_API.G_MISS_CHAR then
2248       l_attribute2 := null;
2249    else
2250       l_attribute2 := p_ctr_property_template_rec.attribute2;
2251    end if;
2252 
2253    if p_ctr_property_template_rec.attribute3 = FND_API.G_MISS_CHAR then
2254       l_attribute3 := null;
2255    else
2256       l_attribute3 := p_ctr_property_template_rec.attribute3;
2257    end if;
2258 
2259    if p_ctr_property_template_rec.attribute4 = FND_API.G_MISS_CHAR then
2260       l_attribute4 := null;
2261    else
2262       l_attribute4 := p_ctr_property_template_rec.attribute4;
2263    end if;
2264 
2265    if p_ctr_property_template_rec.attribute5 = FND_API.G_MISS_CHAR then
2266       l_attribute5 := null;
2267    else
2268       l_attribute5 := p_ctr_property_template_rec.attribute5;
2269    end if;
2270 
2271    if p_ctr_property_template_rec.attribute6 = FND_API.G_MISS_CHAR then
2272       l_attribute6 := null;
2273    else
2274       l_attribute6 := p_ctr_property_template_rec.attribute6;
2275    end if;
2276 
2277    if p_ctr_property_template_rec.attribute7 = FND_API.G_MISS_CHAR then
2278       l_attribute7 := null;
2279    else
2280       l_attribute7 := p_ctr_property_template_rec.attribute7;
2281    end if;
2282 
2283    if p_ctr_property_template_rec.attribute8 = FND_API.G_MISS_CHAR then
2284       l_attribute8 := null;
2285    else
2286       l_attribute8 := p_ctr_property_template_rec.attribute8;
2287    end if;
2288 
2289    if p_ctr_property_template_rec.attribute9 = FND_API.G_MISS_CHAR then
2290       l_attribute9 := null;
2291    else
2292       l_attribute9 := p_ctr_property_template_rec.attribute9;
2293    end if;
2294 
2295    if p_ctr_property_template_rec.attribute10 = FND_API.G_MISS_CHAR then
2296       l_attribute10 := null;
2297    else
2298       l_attribute10 := p_ctr_property_template_rec.attribute10;
2299    end if;
2300 
2301    if p_ctr_property_template_rec.attribute11 = FND_API.G_MISS_CHAR then
2302       l_attribute11 := null;
2303    else
2304       l_attribute11 := p_ctr_property_template_rec.attribute11;
2305    end if;
2306 
2307    if p_ctr_property_template_rec.attribute12 = FND_API.G_MISS_CHAR then
2308       l_attribute12 := null;
2309    else
2310       l_attribute12 := p_ctr_property_template_rec.attribute12;
2311    end if;
2312 
2313    if p_ctr_property_template_rec.attribute13 = FND_API.G_MISS_CHAR then
2314       l_attribute13 := null;
2315    else
2316       l_attribute13 := p_ctr_property_template_rec.attribute13;
2317    end if;
2318 
2319    if p_ctr_property_template_rec.attribute14 = FND_API.G_MISS_CHAR then
2320       l_attribute14 := null;
2321    else
2322       l_attribute14 := p_ctr_property_template_rec.attribute14;
2323    end if;
2324 
2325    if p_ctr_property_template_rec.attribute15 = FND_API.G_MISS_CHAR then
2326       l_attribute15 := null;
2327    else
2328       l_attribute15 := p_ctr_property_template_rec.attribute15;
2329    end if;
2330 
2331    if p_ctr_property_template_rec.attribute_category = FND_API.G_MISS_CHAR then
2332       l_attribute_category := null;
2333    else
2334       l_attribute_category := p_ctr_property_template_rec.attribute_category;
2335    end if;
2336 
2337    if p_ctr_property_template_rec.migrated_flag = FND_API.G_MISS_CHAR then
2338       l_migrated_flag := null;
2339    else
2340       l_migrated_flag := p_ctr_property_template_rec.migrated_flag;
2341    end if;
2342 
2343    validate_start_date(l_start_date_active);
2344    IF l_property_data_type IS NOT NULL THEN
2345       validate_lookups('CSI_CTR_PROPERTY_DATA_TYPE',l_property_data_type);
2346       Validate_Data_Type(l_property_data_type, l_default_value, l_minimum_value, l_maximum_value);
2347    END IF;
2348 
2349    IF l_property_lov_type IS NOT NULL THEN
2350       validate_lookups('CSI_CTR_PROPERTY_LOV_TYPE',l_property_lov_type);
2351    END IF;
2352 
2353    IF l_property_lov_type IS NOT NULL and l_default_value IS NOT NULL THEN
2354       validate_lookups(l_property_lov_type,l_default_value);
2355    END IF;
2356 
2357    IF l_uom_code IS NOT NULL THEN
2358        validate_uom(l_uom_code);
2359    END IF;
2360 
2361    /* Call the table Handler */
2362    CSI_CTR_PROPERTY_TEMPLATE_PKG.Insert_Row(
2363  	 px_COUNTER_PROPERTY_ID         => l_counter_property_id
2364 	,p_COUNTER_ID                   => l_counter_id
2365 	,p_PROPERTY_DATA_TYPE           => l_property_data_type
2366 	,p_IS_NULLABLE                  => l_is_nullable
2367 	,p_DEFAULT_VALUE                => l_default_value
2368 	,p_MINIMUM_VALUE                => l_minimum_value
2369 	,p_MAXIMUM_VALUE                => l_maximum_value
2370 	,p_UOM_CODE                     => l_uom_code
2371 	,p_START_DATE_ACTIVE            => l_start_date_active
2372 	,p_END_DATE_ACTIVE              => l_end_date_active
2373 	,p_OBJECT_VERSION_NUMBER        => 1
2374         ,p_LAST_UPDATE_DATE             => sysdate
2375         ,p_LAST_UPDATED_BY              => FND_GLOBAL.USER_ID
2376         ,p_CREATION_DATE                => sysdate
2377         ,p_CREATED_BY                   => FND_GLOBAL.USER_ID
2378         ,p_LAST_UPDATE_LOGIN            => FND_GLOBAL.USER_ID
2379         ,p_ATTRIBUTE1                   => l_attribute1
2380         ,p_ATTRIBUTE2                   => l_attribute2
2381         ,p_ATTRIBUTE3                   => l_attribute3
2382         ,p_ATTRIBUTE4                   => l_attribute4
2383         ,p_ATTRIBUTE5                   => l_attribute5
2384         ,p_ATTRIBUTE6                   => l_attribute6
2385         ,p_ATTRIBUTE7                   => l_attribute7
2386         ,p_ATTRIBUTE8                   => l_attribute8
2387         ,p_ATTRIBUTE9                   => l_attribute9
2388         ,p_ATTRIBUTE10                  => l_attribute10
2389         ,p_ATTRIBUTE11                  => l_attribute11
2390         ,p_ATTRIBUTE12                  => l_attribute12
2391         ,p_ATTRIBUTE13                  => l_attribute13
2392         ,p_ATTRIBUTE14                  => l_attribute14
2393         ,p_ATTRIBUTE15                  => l_attribute15
2394         ,p_ATTRIBUTE_CATEGORY           => l_attribute_category
2395 	,p_MIGRATED_FLAG                => l_migrated_flag
2396 	,p_PROPERTY_LOV_TYPE            => l_property_lov_type
2397         ,p_SECURITY_GROUP_ID            => l_security_group_id
2398         ,p_NAME	                        => l_name
2399         ,p_DESCRIPTION                  => l_description
2400         );
2401 
2402    IF NOT (x_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
2403         ROLLBACK TO create_ctr_property_tmpl_pvt;
2404         RETURN;
2405    END IF;
2406 
2407    /* End of table handler call */
2408 
2409    IF FND_API.to_Boolean(nvl(p_commit,FND_API.G_FALSE)) THEN
2410       COMMIT WORK;
2411    END IF;
2412 
2413    -- Standard call to get message count and IF count is  get message info.
2414    FND_MSG_PUB.Count_And_Get
2415       ( p_count  =>  x_msg_count,
2416         p_data   =>  x_msg_data
2417       );
2418 EXCEPTION
2419    WHEN FND_API.G_EXC_ERROR THEN
2420       x_return_status := FND_API.G_RET_STS_ERROR ;
2421       ROLLBACK TO create_ctr_property_tmpl_pvt;
2422       FND_MSG_PUB.Count_And_Get
2423          ( p_count => x_msg_count,
2424            p_data  => x_msg_data
2425          );
2426    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2427       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2428       ROLLBACK TO create_ctr_property_tmpl_pvt;
2429       FND_MSG_PUB.Count_And_Get
2430          ( p_count => x_msg_count,
2431            p_data  => x_msg_data
2432          );
2433    WHEN OTHERS THEN
2434       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2435       ROLLBACK TO create_ctr_property_tmpl_pvt;
2436       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
2437          FND_MSG_PUB.Add_Exc_Msg
2438             ( G_PKG_NAME,
2439               l_api_name
2440             );
2441       END IF;
2442       FND_MSG_PUB.Count_And_Get
2443          ( p_count  => x_msg_count,
2444            p_data   => x_msg_data
2445          );
2446 END create_ctr_property_template;
2447 
2448 --|---------------------------------------------------
2449 --| procedure name: create_counter_relationship
2450 --| description :   procedure used to
2451 --|                 create counter relationship
2452 --|---------------------------------------------------
2453 
2454 PROCEDURE create_counter_relationship
2455  (
2456      p_api_version               IN     NUMBER
2457     ,p_commit                    IN     VARCHAR2
2458     ,p_init_msg_list             IN     VARCHAR2
2459     ,p_validation_level          IN     NUMBER
2460     ,p_counter_relationships_rec IN OUT NOCOPY CSI_CTR_DATASTRUCTURES_PUB.counter_relationships_rec
2461     ,x_return_status                OUT    NOCOPY VARCHAR2
2462     ,x_msg_count                    OUT    NOCOPY NUMBER
2463     ,x_msg_data                     OUT    NOCOPY VARCHAR2
2464  ) IS
2465    l_api_name                     CONSTANT VARCHAR2(30)   := 'CREATE_COUNTER_RELATIONSHIP';
2466    l_api_version                  CONSTANT NUMBER         := 1.0;
2467    l_msg_data                     VARCHAR2(2000);
2468    l_msg_index                    NUMBER;
2469    l_msg_count                    NUMBER;
2470    -- l_debug_level                  NUMBER;
2471 
2472    l_RELATIONSHIP_ID  	          NUMBER;
2473    l_CTR_ASSOCIATION_ID           NUMBER;
2474    l_RELATIONSHIP_TYPE_CODE       VARCHAR2(30);
2475    l_BIND_VARIABLE_NAME           VARCHAR2(30);
2476    l_SOURCE_COUNTER_ID            NUMBER;
2477    l_OBJECT_COUNTER_ID            NUMBER;
2478    l_LAST_UPDATE_DATE             DATE;
2479    l_LAST_UPDATED_BY              NUMBER;
2480    l_CREATION_DATE                DATE;
2481    l_CREATED_BY                   NUMBER;
2482    l_LAST_UPDATE_LOGIN            NUMBER;
2483    l_ACTIVE_START_DATE            DATE;
2484    l_ACTIVE_END_DATE              DATE;
2485    l_ATTRIBUTE1                   VARCHAR2(150);
2486    l_ATTRIBUTE2                   VARCHAR2(150);
2487    l_ATTRIBUTE3                   VARCHAR2(150);
2488    l_ATTRIBUTE4                   VARCHAR2(150);
2489    l_ATTRIBUTE5                   VARCHAR2(150);
2490    l_ATTRIBUTE6                   VARCHAR2(150);
2491    l_ATTRIBUTE7                   VARCHAR2(150);
2492    l_ATTRIBUTE8                   VARCHAR2(150);
2493    l_ATTRIBUTE9                   VARCHAR2(150);
2494    l_ATTRIBUTE10                  VARCHAR2(150);
2495    l_ATTRIBUTE11                  VARCHAR2(150);
2496    l_ATTRIBUTE12                  VARCHAR2(150);
2497    l_ATTRIBUTE13                  VARCHAR2(150);
2498    l_ATTRIBUTE14                  VARCHAR2(150);
2499    l_ATTRIBUTE15                  VARCHAR2(150);
2500    l_ATTRIBUTE_CATEGORY           VARCHAR2(30);
2501    l_OBJECT_VERSION_NUMBER        NUMBER;
2502    l_PROPERTY_LOV_TYPE            VARCHAR2(30);
2503    l_SECURITY_GROUP_ID            NUMBER;
2504    l_MIGRATED_FLAG                VARCHAR2(1);
2505    l_VALID_FLAG                   VARCHAR2(1);
2506    l_FACTOR                       NUMBER;
2507    l_source_direction             VARCHAR2(1);
2508    l_object_direction             VARCHAR2(1);
2509    l_src_ctr_start_date           DATE;
2510    l_src_ctr_end_date             DATE;
2511    l_obj_ctr_start_date           DATE;
2512    l_obj_ctr_end_date             DATE;
2513    l_reading_date                 DATE;
2514 
2515    CURSOR c1(p_counter_id IN NUMBER) IS
2516    SELECT max(value_timestamp)
2517    FROM   csi_counter_readings
2518    WHERE  counter_id = p_counter_id;
2519 BEGIN
2520    -- Standard Start of API savepoint
2521    SAVEPOINT  create_ctr_relationship_pvt;
2522 
2523    -- Standard call to check for call compatibility.
2524    IF NOT FND_API.Compatible_API_Call (l_api_version,
2525                                        p_api_version,
2526                                        l_api_name   ,
2527                                        G_PKG_NAME   ) THEN
2528       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2529    END IF;
2530 
2531    -- Initialize message list if p_init_msg_list is set to TRUE.
2532    IF FND_API.to_Boolean(nvl(p_init_msg_list,FND_API.G_FALSE)) THEN
2533       FND_MSG_PUB.initialize;
2534    END IF;
2535 
2536    -- Read the debug profiles values in to global variable 7197402
2537    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
2538 
2539    --  Initialize API return status to success
2540    x_return_status := FND_API.G_RET_STS_SUCCESS;
2541 
2542    --
2543    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
2544       csi_ctr_gen_utility_pvt.put_line
2545          ( 'create_ctr_relationship_pvt'       ||'-'||
2546             p_api_version                              ||'-'||
2547             nvl(p_commit,FND_API.G_FALSE)              ||'-'||
2548             nvl(p_init_msg_list,FND_API.G_FALSE)       ||'-'||
2549             nvl(p_validation_level,FND_API.G_VALID_LEVEL_FULL) );
2550    END IF;
2551 
2552    if p_counter_relationships_rec.relationship_id = FND_API.G_MISS_NUM then
2553       l_relationship_id := null;
2554    else
2555       l_relationship_id := p_counter_relationships_rec.relationship_id;
2556    end if;
2557 
2558    if p_counter_relationships_rec.ctr_association_id = FND_API.G_MISS_NUM then
2559       l_ctr_association_id := null;
2560    else
2561       l_ctr_association_id := p_counter_relationships_rec.ctr_association_id;
2562    end if;
2563 
2564    if p_counter_relationships_rec.relationship_type_code = FND_API.G_MISS_CHAR then
2565       l_relationship_type_code := null;
2566    else
2567       l_relationship_type_code := p_counter_relationships_rec.relationship_type_code;
2568    end if;
2569 
2570    if p_counter_relationships_rec.source_counter_id = FND_API.G_MISS_NUM then
2571       l_source_counter_id := null;
2572    else
2573       l_source_counter_id := p_counter_relationships_rec.source_counter_id;
2574    end if;
2575 
2576    if p_counter_relationships_rec.object_counter_id = FND_API.G_MISS_NUM then
2577       l_object_counter_id := null;
2578    else
2579       l_object_counter_id := p_counter_relationships_rec.object_counter_id;
2580    end if;
2581 
2582    if p_counter_relationships_rec.security_group_id = FND_API.G_MISS_NUM then
2583       l_security_group_id := null;
2584    else
2585       l_security_group_id := p_counter_relationships_rec.security_group_id;
2586    end if;
2587 
2588    if nvl(p_counter_relationships_rec.active_start_date,FND_API.G_MISS_DATE) = FND_API.G_MISS_DATE then
2589       l_active_start_date := sysdate;
2590    else
2591       l_active_start_date := p_counter_relationships_rec.active_start_date;
2592    end if;
2593 
2594    if p_counter_relationships_rec.active_end_date = FND_API.G_MISS_DATE then
2595       l_active_end_date := null;
2596    else
2597       l_active_end_date := p_counter_relationships_rec.active_end_date;
2598    end if;
2599 
2600    if p_counter_relationships_rec.attribute1 = FND_API.G_MISS_CHAR then
2601       l_attribute1 := null;
2602    else
2603       l_attribute1 := p_counter_relationships_rec.attribute1;
2604    end if;
2605 
2606    if p_counter_relationships_rec.attribute2 = FND_API.G_MISS_CHAR then
2607       l_attribute2 := null;
2608    else
2609       l_attribute2 := p_counter_relationships_rec.attribute2;
2610    end if;
2611 
2612    if p_counter_relationships_rec.attribute3 = FND_API.G_MISS_CHAR then
2613       l_attribute3 := null;
2614    else
2615       l_attribute3 := p_counter_relationships_rec.attribute3;
2616    end if;
2617 
2618    if p_counter_relationships_rec.attribute4 = FND_API.G_MISS_CHAR then
2619       l_attribute4 := null;
2620    else
2621       l_attribute4 := p_counter_relationships_rec.attribute4;
2622    end if;
2623 
2624    if p_counter_relationships_rec.attribute5 = FND_API.G_MISS_CHAR then
2625       l_attribute5 := null;
2626    else
2627       l_attribute5 := p_counter_relationships_rec.attribute5;
2628    end if;
2629 
2630    if p_counter_relationships_rec.attribute6 = FND_API.G_MISS_CHAR then
2631       l_attribute6 := null;
2632    else
2633       l_attribute6 := p_counter_relationships_rec.attribute6;
2634    end if;
2635 
2636    if p_counter_relationships_rec.attribute7 = FND_API.G_MISS_CHAR then
2637       l_attribute7 := null;
2638    else
2639       l_attribute7 := p_counter_relationships_rec.attribute7;
2640    end if;
2641 
2642    if p_counter_relationships_rec.attribute8 = FND_API.G_MISS_CHAR then
2643       l_attribute8 := null;
2644    else
2645       l_attribute8 := p_counter_relationships_rec.attribute8;
2646    end if;
2647 
2648    if p_counter_relationships_rec.attribute9 = FND_API.G_MISS_CHAR then
2649       l_attribute9 := null;
2650    else
2651       l_attribute9 := p_counter_relationships_rec.attribute9;
2652    end if;
2653 
2654    if p_counter_relationships_rec.attribute10 = FND_API.G_MISS_CHAR then
2655       l_attribute10 := null;
2656    else
2657       l_attribute10 := p_counter_relationships_rec.attribute10;
2658    end if;
2659 
2660    if p_counter_relationships_rec.attribute11 = FND_API.G_MISS_CHAR then
2661       l_attribute11 := null;
2662    else
2663       l_attribute11 := p_counter_relationships_rec.attribute11;
2664    end if;
2665 
2666    if p_counter_relationships_rec.attribute12 = FND_API.G_MISS_CHAR then
2667       l_attribute12 := null;
2668    else
2669       l_attribute12 := p_counter_relationships_rec.attribute12;
2670    end if;
2671 
2672    if p_counter_relationships_rec.attribute13 = FND_API.G_MISS_CHAR then
2673       l_attribute13 := null;
2674    else
2675       l_attribute13 := p_counter_relationships_rec.attribute13;
2676    end if;
2677 
2678    if p_counter_relationships_rec.attribute14 = FND_API.G_MISS_CHAR then
2679       l_attribute14 := null;
2680    else
2681       l_attribute14 := p_counter_relationships_rec.attribute14;
2682    end if;
2683 
2684    if p_counter_relationships_rec.attribute15 = FND_API.G_MISS_CHAR then
2685       l_attribute15 := null;
2686    else
2687       l_attribute15 := p_counter_relationships_rec.attribute15;
2688    end if;
2689 
2690    if p_counter_relationships_rec.attribute_category = FND_API.G_MISS_CHAR then
2691       l_attribute_category := null;
2692    else
2693       l_attribute_category := p_counter_relationships_rec.attribute_category;
2694    end if;
2695 
2696    if p_counter_relationships_rec.migrated_flag = FND_API.G_MISS_CHAR then
2697       l_migrated_flag := null;
2698    else
2699       l_migrated_flag := p_counter_relationships_rec.migrated_flag;
2700    end if;
2701 
2702    if p_counter_relationships_rec.bind_variable_name = FND_API.G_MISS_CHAR then
2703       l_bind_variable_name := null;
2704    else
2705       l_bind_variable_name := p_counter_relationships_rec.bind_variable_name;
2706    end if;
2707 
2708    if p_counter_relationships_rec.factor = FND_API.G_MISS_NUM then
2709       l_factor := null;
2710    else
2711       l_factor := p_counter_relationships_rec.factor;
2712    end if;
2713 
2714 csi_ctr_gen_utility_pvt.put_line('Relationship type code = '||l_relationship_type_code);
2715 
2716    validate_start_date(l_active_start_date);
2717    validate_lookups('CSI_CTR_RELATIONSHIP_TYPE_CODE', l_relationship_type_code);
2718 
2719    IF l_relationship_type_code = 'CONFIGURATION' THEN
2720       validate_ctr_relationship(l_source_counter_id, l_source_direction
2721                                 ,l_src_ctr_start_date, l_src_ctr_end_date);
2722       validate_ctr_relationship(l_object_counter_id, l_object_direction
2723                                 ,l_obj_ctr_start_date, l_obj_ctr_end_date);
2724       /* Validate direction */
2725       IF l_source_direction = 'B' and l_object_direction <> 'B'  THEN
2726          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_REL_DIR');
2727       END IF;
2728 
2729       IF l_object_direction = 'B' and l_source_direction <> 'B' THEN
2730          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_REL_DIR');
2731       END IF;
2732 
2733       IF l_active_start_date < l_src_ctr_start_date THEN
2734          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_SOURCE_ST_DATE');
2735       ELSIF l_active_start_date < l_obj_ctr_start_date THEN
2736          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_SOURCE_ST_DATE');
2737       END IF;
2738 
2739       IF l_src_ctr_end_date IS NOT NULL THEN
2740          IF l_active_start_date > l_src_ctr_end_date THEN
2741             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_SOURCE_ST_DATE');
2742          END IF;
2743       END IF;
2744 
2745       IF l_obj_ctr_end_date IS NOT NULL THEN
2746          IF l_active_start_date > l_obj_ctr_end_date THEN
2747             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_SOURCE_ST_DATE');
2748          END IF;
2749       END IF;
2750 
2751       OPEN c1(l_source_counter_id);
2752       FETCH c1 into l_reading_date;
2753       IF l_reading_date IS NOT  NULL THEN
2754          IF l_active_start_date < l_reading_date THEN
2755             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_SOURCE_ST_DATE');
2756          END IF;
2757       END IF;
2758       CLOSE c1;
2759 
2760       OPEN c1(l_object_counter_id);
2761       FETCH c1 into l_reading_date;
2762       IF l_reading_date IS NOT  NULL THEN
2763          IF l_active_start_date < l_reading_date THEN
2764             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_SOURCE_ST_DATE');
2765          END IF;
2766       END IF;
2767       CLOSE c1;
2768 
2769       /* A source counter cannot be a target counter */
2770       BEGIN
2771          SELECT 'N'
2772          INTO   l_valid_flag
2773          FROM   csi_counter_relationships
2774          WHERE  relationship_type_code = 'CONFIGURATION'
2775          AND    source_counter_id = l_object_counter_id
2776          AND    NVL(active_end_date, SYSDATE+1) > SYSDATE; --bug9160706
2777 
2778 	 csi_ctr_gen_utility_pvt.put_line('Source relationship exists');--bug9160706
2779 
2780          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_TARGET_CTR_EXIST');
2781 
2782       EXCEPTION
2783          WHEN NO_DATA_FOUND THEN
2784             csi_ctr_gen_utility_pvt.put_line('No source relationship exists');--bug9160706
2785 	    NULL;
2786          WHEN TOO_MANY_ROWS THEN
2787             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_TARGET_CTR_EXIST');
2788 
2789       END;
2790    ELSIF l_relationship_type_code = 'FORMULA' THEN
2791       IF l_source_counter_id IS NULL THEN
2792          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_FORMULA_REF');
2793       END IF;
2794    END IF;
2795 
2796    /* Call the table Handler */
2797    CSI_COUNTER_RELATIONSHIP_PKG.Insert_Row(
2798 	px_RELATIONSHIP_ID              => l_relationship_id
2799   	,p_CTR_ASSOCIATION_ID           => l_ctr_association_id
2800   	,p_RELATIONSHIP_TYPE_CODE       => l_relationship_type_code
2801   	,p_SOURCE_COUNTER_ID            => l_source_counter_id
2802   	,p_OBJECT_COUNTER_ID            => l_object_counter_id
2803   	,p_ACTIVE_START_DATE            => l_active_start_date
2804   	,p_ACTIVE_END_DATE              => l_active_end_date
2805   	,p_OBJECT_VERSION_NUMBER        => 1
2806         ,p_LAST_UPDATE_DATE             => sysdate
2807         ,p_LAST_UPDATED_BY              => FND_GLOBAL.USER_ID
2808         ,p_CREATION_DATE                => sysdate
2809         ,p_CREATED_BY                   => FND_GLOBAL.USER_ID
2810         ,p_LAST_UPDATE_LOGIN            => FND_GLOBAL.USER_ID
2811   	,p_ATTRIBUTE_CATEGORY           => l_attribute_category
2812         ,p_ATTRIBUTE1                   => l_attribute1
2813         ,p_ATTRIBUTE2                   => l_attribute2
2814         ,p_ATTRIBUTE3                   => l_attribute3
2815         ,p_ATTRIBUTE4                   => l_attribute4
2816         ,p_ATTRIBUTE5                   => l_attribute5
2817         ,p_ATTRIBUTE6                   => l_attribute6
2818         ,p_ATTRIBUTE7                   => l_attribute7
2819         ,p_ATTRIBUTE8                   => l_attribute8
2820         ,p_ATTRIBUTE9                   => l_attribute9
2821         ,p_ATTRIBUTE10                  => l_attribute10
2822         ,p_ATTRIBUTE11                  => l_attribute11
2823         ,p_ATTRIBUTE12                  => l_attribute12
2824         ,p_ATTRIBUTE13                  => l_attribute13
2825         ,p_ATTRIBUTE14                  => l_attribute14
2826         ,p_ATTRIBUTE15                  => l_attribute15
2827         ,p_SECURITY_GROUP_ID            => l_security_group_id
2828 	,p_MIGRATED_FLAG                => l_migrated_flag
2829   	,p_BIND_VARIABLE_NAME           => l_bind_variable_name
2830   	,p_FACTOR                       => l_factor);
2831 
2832    IF NOT (x_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
2833         ROLLBACK TO create_ctr_relationship_pvt;
2834         RETURN;
2835    END IF;
2836 
2837    /* End of table handler call */
2838 
2839    IF FND_API.to_Boolean(nvl(p_commit,FND_API.G_FALSE)) THEN
2840       COMMIT WORK;
2841    END IF;
2842 
2843    -- Standard call to get message count and IF count is  get message info.
2844    FND_MSG_PUB.Count_And_Get
2845       ( p_count  =>  x_msg_count,
2846         p_data   =>  x_msg_data
2847       );
2848 EXCEPTION
2849    WHEN FND_API.G_EXC_ERROR THEN
2850       x_return_status := FND_API.G_RET_STS_ERROR ;
2851       ROLLBACK TO create_ctr_relationship_pvt;
2852       FND_MSG_PUB.Count_And_Get
2853          ( p_count => x_msg_count,
2854            p_data  => x_msg_data
2855          );
2856    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2857       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2858       ROLLBACK TO create_ctr_relationship_pvt;
2859       FND_MSG_PUB.Count_And_Get
2860          ( p_count => x_msg_count,
2861            p_data  => x_msg_data
2862          );
2863    WHEN OTHERS THEN
2864       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2865       ROLLBACK TO create_ctr_relationship_pvt;
2866       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
2867          FND_MSG_PUB.Add_Exc_Msg
2868             ( G_PKG_NAME,
2869               l_api_name
2870             );
2871       END IF;
2872       FND_MSG_PUB.Count_And_Get
2873          ( p_count  => x_msg_count,
2874            p_data   => x_msg_data
2875          );
2876 END create_counter_relationship;
2877 
2878 --|---------------------------------------------------
2879 --| procedure name: create_derived_filters
2880 --| description :   procedure used to
2881 --|                 create derived filters
2882 --|---------------------------------------------------
2883 
2884 PROCEDURE create_derived_filters
2885 (
2886    p_api_version               IN     NUMBER
2887    ,p_commit                    IN     VARCHAR2
2888    ,p_init_msg_list             IN     VARCHAR2
2889    ,p_validation_level          IN     NUMBER
2890    ,p_ctr_derived_filters_tbl IN OUT NOCOPY CSI_CTR_DATASTRUCTURES_PUB.ctr_derived_filters_tbl
2891    ,x_return_status                OUT    NOCOPY VARCHAR2
2892    ,x_msg_count                    OUT    NOCOPY NUMBER
2893    ,x_msg_data                     OUT    NOCOPY VARCHAR2
2894 ) IS
2895 
2896    l_api_name                   CONSTANT VARCHAR2(30)   := 'CREATE_DERIVED_FILTERS';
2897    l_api_version                CONSTANT NUMBER         := 1.0;
2898 
2899    l_dummy			VARCHAR2(1);
2900    -- l_debug_level                NUMBER;
2901    l_msg_data                   VARCHAR2(2000);
2902    l_msg_count                  NUMBER;
2903 
2904    --l_rec_number			NUMBER;
2905    l_ctr_derived_filters_rec	CSI_CTR_DATASTRUCTURES_PUB.ctr_derived_filters_rec;
2906    l_desc_flex                  CSI_CTR_DATASTRUCTURES_PUB.dff_rec_type;
2907    l_type			VARCHAR2(30);
2908    l_name			VARCHAR2(50);
2909    l_log_op			VARCHAR2(30);
2910    l_seq_no			NUMBER;
2911    l_return_status		VARCHAR2(1);
2912    l_valid_flag			VARCHAR2(1);
2913 
2914 BEGIN
2915    -- Standard Start of API savepoint
2916    SAVEPOINT  create_derived_filters;
2917 
2918    -- Check for freeze_flag in csi_install_parameters is set to 'Y'
2919 
2920    -- csi_ctr_gen_utility_pvt.check_ib_active;
2921 
2922    -- Standard call to check for call compatibility.
2923    IF NOT FND_API.Compatible_API_Call (l_api_version,
2924                                        p_api_version,
2925                                        l_api_name   ,
2926                                        G_PKG_NAME   )
2927    THEN
2928       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2929    END IF;
2930    -- Initialize message list if p_init_msg_list is set to TRUE.
2931    IF FND_API.to_Boolean( p_init_msg_list ) THEN
2932       FND_MSG_PUB.initialize;
2933    END IF;
2934 
2935    --  Initialize API return status to success
2936    x_return_status := FND_API.G_RET_STS_SUCCESS;
2937 
2938    -- Read the debug profiles values in to global variable 7197402
2939    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
2940 
2941    -- Check the profile option debug_level for debug message reporting
2942    -- l_debug_level:=fnd_profile.value('CSI_COUNTER_DEBUG_LEVEL');
2943 
2944    -- If debug_level = 1 then dump the procedure name
2945    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
2946       csi_ctr_gen_utility_pvt.put_line( 'create_derived_filters');
2947    END IF;
2948 
2949    -- If the debug level = 2 then dump all the parameters values.
2950    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 1) THEN
2951       csi_ctr_gen_utility_pvt.put_line( 'create_derived_filters'   ||
2952 					p_api_version         ||'-'||
2953 					p_commit              ||'-'||
2954                                         p_init_msg_list       ||'-'||
2955 					p_validation_level );
2956       csi_ctr_gen_utility_pvt.dump_ctr_derived_filters_tbl(p_ctr_derived_filters_tbl);
2957    END IF;
2958 
2959    IF (p_ctr_derived_filters_tbl.count > 0) THEN
2960       FOR tab_row IN p_ctr_derived_filters_tbl.FIRST .. p_ctr_derived_filters_tbl.LAST
2961       LOOP
2962    l_ctr_derived_filters_rec := p_ctr_derived_filters_tbl(tab_row);
2963 
2964    BEGIN
2965       -- validate all counters
2966       SELECT nvl(type, 'REGULAR'), name
2967       into   l_type, l_name
2968       FROM   csi_counters_bc_v
2969       WHERE  counter_id = l_ctr_derived_filters_rec.counter_id;
2970 
2971    EXCEPTION
2972       WHEN NO_DATA_FOUND THEN
2973       csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_INVALID');
2974 
2975       -- validate all counter types are GROUP type
2976       IF l_type <> 'FORMULA' THEN
2977          csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_INV_GROUP_CTR','CTR_NAME',l_name);
2978       END IF;
2979    END;
2980 
2981    BEGIN
2982       -- validate all counter properties
2983       SELECT 'x'
2984       into   l_dummy
2985       FROM   csi_ctr_properties_bc_v
2986       WHERE  counter_property_id = l_ctr_derived_filters_rec.counter_property_id;
2987 
2988    EXCEPTION WHEN NO_DATA_FOUND THEN
2989       csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_PROP_INVALID');
2990    END;
2991 
2992    -- validate LEFT_PARENT
2993    IF l_ctr_derived_filters_rec.LEFT_PARENT NOT IN ('(', '((', '(((', '((((', '(((((') THEN
2994       csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_INV_DER_LEFT_PARENT',
2995 					     'PARM',l_ctr_derived_filters_rec.LEFT_PARENT);
2996    END IF;
2997 
2998    -- validate RIGHT_PARENT
2999    IF l_ctr_derived_filters_rec.RIGHT_PARENT NOT IN (')', '))', ')))', '))))', ')))))') THEN
3000       csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_INV_DER_RIGHT_PARENT',
3001 					     'PARM',l_ctr_derived_filters_rec.RIGHT_PARENT);
3002    END IF;
3003 
3004    -- validate RELATIONAL_OPERATOR
3005    IF l_ctr_derived_filters_rec.RELATIONAL_OPERATOR NOT IN ('=', '<', '<=', '>', '>=', '!=', '<>') THEN
3006       csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_INV_DER_REL_OPERATOR',
3007 					     'PARM',l_ctr_derived_filters_rec.RELATIONAL_OPERATOR);
3008    END IF;
3009 
3010    -- validate LOGICAL_OPERATOR
3011    -- l_log_op := upper(l_ctr_derived_filters_rec.LOGICAL_OPERATOR);
3012 
3013    IF l_ctr_derived_filters_rec.LOGICAL_OPERATOR = FND_API.G_MISS_CHAR then
3014       l_log_op := null;
3015    ELSE
3016       l_log_op := upper(l_ctr_derived_filters_rec.LOGICAL_OPERATOR);
3017    END IF;
3018 
3019    IF l_log_op IS NOT NULL THEN
3020       IF l_log_op NOT IN ('AND', 'OR') THEN
3021          csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_INV_DER_LOG_OPERATOR',
3022   					     'PARM',l_ctr_derived_filters_rec.LOGICAL_OPERATOR);
3023       END IF;
3024    END IF;
3025 
3026    -- initialize descritive flexfield
3027    --csi_ctr_gen_utility_pvt.Initialize_Desc_Flex(l_desc_flex);
3028 
3029    -- validate Descritive Flexfields
3030 /*
3031    csi_ctr_gen_utility_pvt.Is_DescFlex_Valid
3032    (
3033       p_api_name => l_api_name,
3034       p_appl_short_name => 'CSI',
3035       p_desc_flex_name => 'CSI_COUNTER_DERIVED_FILTERS',
3036       p_seg_partial_name => 'ATTRIBUTE',
3037       p_num_of_attributes => 15,
3038       p_seg_values => l_desc_flex
3039    );
3040 */
3041    -- get the SEQ_NO
3042    IF l_ctr_derived_filters_rec.seq_no = FND_API.G_MISS_NUM then
3043       l_ctr_derived_filters_rec.seq_no := null;
3044    END IF;
3045 
3046    IF l_ctr_derived_filters_rec.SEQ_NO IS NULL THEN
3047       SELECT nvl(max(seq_no), 0) + 1
3048              into l_seq_no
3049       FROM   CSI_COUNTER_DERIVED_FILTERS
3050       WHERE  counter_id = l_ctr_derived_filters_rec.COUNTER_ID;
3051    ELSE
3052       BEGIN
3053          SELECT	'x'
3054                 into l_dummy
3055          FROM	CSI_COUNTER_DERIVED_FILTERS
3056          WHERE	counter_id = l_ctr_derived_filters_rec.COUNTER_ID
3057                 AND seq_no = l_ctr_derived_filters_rec.SEQ_NO;
3058 
3059          -- this means that for this counter, there is one another
3060          -- derived filter record with the same sequence number.
3061 	 -- Raise error.
3062          csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_DUP_DERFILTER_SEQNO',
3063 						'SEQNO',to_char(l_ctr_derived_filters_rec.SEQ_NO),
3064 						'CTR_NAME',l_name);
3065 
3066       EXCEPTION WHEN NO_DATA_FOUND THEN
3067          -- good. you can proceed.
3068          NULL;
3069       END;
3070    END IF;
3071 
3072    -- call table handler here
3073    CSI_CTR_DERIVED_FILTERS_PKG.Insert_Row
3074    (
3075       l_ctr_derived_filters_rec.COUNTER_DERIVED_FILTER_ID
3076       ,l_ctr_derived_filters_rec.COUNTER_ID
3077       ,l_seq_no
3078       ,l_ctr_derived_filters_rec.LEFT_PARENT
3079       ,l_ctr_derived_filters_rec.COUNTER_PROPERTY_ID
3080       ,l_ctr_derived_filters_rec.RELATIONAL_OPERATOR
3081       ,l_ctr_derived_filters_rec.RIGHT_VALUE
3082       ,l_ctr_derived_filters_rec.RIGHT_PARENT
3083       ,l_ctr_derived_filters_rec.LOGICAL_OPERATOR
3084       ,l_ctr_derived_filters_rec.START_DATE_ACTIVE
3085       ,l_ctr_derived_filters_rec.END_DATE_ACTIVE
3086       ,1
3087       ,sysdate
3088       ,FND_GLOBAL.USER_ID
3089       ,sysdate
3090       ,FND_GLOBAL.USER_ID
3091       ,FND_GLOBAL.USER_ID
3092       ,l_ctr_derived_filters_rec.ATTRIBUTE1
3093       ,l_ctr_derived_filters_rec.ATTRIBUTE2
3094       ,l_ctr_derived_filters_rec.ATTRIBUTE3
3095       ,l_ctr_derived_filters_rec.ATTRIBUTE4
3096       ,l_ctr_derived_filters_rec.ATTRIBUTE5
3097       ,l_ctr_derived_filters_rec.ATTRIBUTE6
3098       ,l_ctr_derived_filters_rec.ATTRIBUTE7
3099       ,l_ctr_derived_filters_rec.ATTRIBUTE8
3100       ,l_ctr_derived_filters_rec.ATTRIBUTE9
3101       ,l_ctr_derived_filters_rec.ATTRIBUTE10
3102       ,l_ctr_derived_filters_rec.ATTRIBUTE11
3103       ,l_ctr_derived_filters_rec.ATTRIBUTE12
3104       ,l_ctr_derived_filters_rec.ATTRIBUTE13
3105       ,l_ctr_derived_filters_rec.ATTRIBUTE14
3106       ,l_ctr_derived_filters_rec.ATTRIBUTE15
3107       ,l_ctr_derived_filters_rec.ATTRIBUTE_CATEGORY
3108       ,l_ctr_derived_filters_rec.SECURITY_GROUP_ID
3109       ,l_ctr_derived_filters_rec.MIGRATED_FLAG
3110    );
3111     END LOOP;
3112   END IF;
3113    --Check if the counter can be marked "valid" with the
3114    --addition of this filter.
3115    csi_ctr_gen_utility_pvt.Validate_GrpOp_ctr
3116    (
3117       p_api_version => 1.0,
3118       p_commit => FND_API.G_FALSE,
3119       p_validation_level => FND_API.G_VALID_LEVEL_NONE,
3120       x_return_status => l_return_status,
3121       x_msg_count	=> l_msg_count,
3122       x_msg_data => l_msg_data,
3123       p_counter_id => l_ctr_derived_filters_rec.COUNTER_ID,
3124       x_valid_flag => l_valid_flag
3125    );
3126 
3127    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3128       RAISE FND_API.G_EXC_ERROR;
3129    END IF;
3130 
3131    UPDATE	csi_counters_b
3132    SET	valid_flag = decode(l_valid_flag, 'Y', 'Y', 'N')
3133    WHERE	counter_id = l_ctr_derived_filters_rec.COUNTER_ID;
3134 
3135    --l_ctr_derived_filters_rec.OBJECT_VERSION_NUMBER := 1;
3136 
3137    -- End of API body
3138 
3139    -- Standard check of p_commit.
3140    IF FND_API.To_Boolean( p_commit ) THEN
3141       COMMIT WORK;
3142    END IF;
3143 
3144    FND_MSG_PUB.Count_And_Get
3145       (p_count => x_msg_count,
3146        p_data  => x_msg_data
3147       );
3148 
3149 EXCEPTION
3150 	WHEN FND_API.G_EXC_ERROR THEN
3151 		x_return_status := FND_API.G_RET_STS_ERROR ;
3152 		ROLLBACK TO create_derived_filters;
3153 		FND_MSG_PUB.Count_And_Get
3154                 (p_count => x_msg_count,
3155                  p_data  => x_msg_data
3156                 );
3157 
3158 	WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3159 		x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3160 		ROLLBACK TO create_derived_filters;
3161 		FND_MSG_PUB.Count_And_Get
3162       			(p_count => x_msg_count,
3163                  p_data  => x_msg_data
3164                 );
3165 	WHEN OTHERS THEN
3166 		x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3167 		ROLLBACK TO create_derived_filters;
3168 		IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
3169 		THEN
3170 			FND_MSG_PUB.Add_Exc_Msg
3171 				(G_PKG_NAME,
3172 				 l_api_name
3173 				);
3174 		END IF;
3175 		FND_MSG_PUB.Count_And_Get
3176 			(p_count => x_msg_count,
3177              p_data  => x_msg_data
3178             );
3179 
3180 END create_derived_filters;
3181 
3182 
3183 --|---------------------------------------------------
3184 --| procedure name: update_counter_group
3185 --| description :   procedure used to
3186 --|                 update counter group
3187 --|---------------------------------------------------
3188 
3189 PROCEDURE update_counter_group
3190  (
3191      p_api_version               IN     NUMBER
3192     ,p_commit                    IN     VARCHAR2
3193     ,p_init_msg_list             IN     VARCHAR2
3194     ,p_validation_level          IN     NUMBER
3195     ,p_counter_groups_rec        IN OUT NOCOPY CSI_CTR_DATASTRUCTURES_PUB.counter_groups_rec
3196     ,x_return_status                OUT    NOCOPY VARCHAR2
3197     ,x_msg_count                    OUT    NOCOPY NUMBER
3198     ,x_msg_data                     OUT    NOCOPY VARCHAR2
3199  ) IS
3200 
3201    CURSOR cur_group_rec(p_counter_group_id IN NUMBER) IS
3202    SELECT name
3203           ,description
3204           ,template_flag
3205           ,cp_service_id
3206           ,customer_product_id
3207           ,last_update_date
3208           ,last_updated_by
3209           ,creation_date
3210           ,created_by
3211           ,last_update_login
3212           ,start_date_active
3213           ,end_date_active
3214           ,attribute1
3215           ,attribute2
3216           ,attribute3
3217           ,attribute4
3218           ,attribute5
3219           ,attribute6
3220           ,attribute7
3221           ,attribute8
3222           ,attribute9
3223           ,attribute10
3224           ,attribute11
3225           ,attribute12
3226           ,attribute13
3227           ,attribute14
3228           ,attribute15
3229           ,context
3230           ,object_version_number
3231           ,created_from_ctr_grp_tmpl_id
3232           ,association_type
3233           ,source_object_code
3234           ,source_object_id
3235           ,source_counter_group_id
3236           ,security_group_id
3237    FROM  cs_csi_counter_groups
3238    WHERE counter_group_id = p_counter_group_id
3239    FOR UPDATE OF OBJECT_VERSION_NUMBER;
3240    l_old_counter_groups_rec  cur_group_rec%ROWTYPE;
3241 
3242    l_api_name                      CONSTANT VARCHAR2(30)   := 'UPDATE_COUNTER_GROUP';
3243    l_api_version                   CONSTANT NUMBER         := 1.0;
3244    l_msg_data                      VARCHAR2(2000);
3245    l_msg_index                     NUMBER;
3246    l_msg_count                     NUMBER;
3247    -- l_debug_level                   NUMBER;
3248 
3249    l_counter_groups_rec            CSI_CTR_DATASTRUCTURES_PUB.counter_groups_rec;
3250 BEGIN
3251    -- Standard Start of API savepoint
3252    SAVEPOINT  update_counter_group_pvt;
3253 
3254    -- Standard call to check for call compatibility.
3255    IF NOT FND_API.Compatible_API_Call (l_api_version,
3256                                        p_api_version,
3257                                        l_api_name   ,
3258                                        G_PKG_NAME   ) THEN
3259       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3260    END IF;
3261 
3262    -- Initialize message list if p_init_msg_list is set to TRUE.
3263    IF FND_API.to_Boolean(nvl(p_init_msg_list,FND_API.G_FALSE)) THEN
3264       FND_MSG_PUB.initialize;
3265    END IF;
3266 
3267    --  Initialize API return status to success
3268    x_return_status := FND_API.G_RET_STS_SUCCESS;
3269 
3270    -- Read the debug profiles values in to global variable 7197402
3271    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
3272 
3273    --
3274    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
3275       csi_ctr_gen_utility_pvt.put_line
3276          ( 'update_counter_group_pvt'              ||'-'||
3277            p_api_version                              ||'-'||
3278            nvl(p_commit,FND_API.G_FALSE)              ||'-'||
3279            nvl(p_init_msg_list,FND_API.G_FALSE)       ||'-'||
3280            nvl(p_validation_level,FND_API.G_VALID_LEVEL_FULL) );
3281    END IF;
3282 
3283    /* Start of API Body */
3284    OPEN cur_group_rec(p_counter_groups_rec.counter_group_id);
3285    FETCH cur_group_rec INTO l_old_counter_groups_rec;
3286    IF  (l_old_counter_groups_rec.object_version_number <> nvl(p_counter_groups_rec.OBJECT_VERSION_NUMBER,0)) THEN
3287       FND_MESSAGE.Set_Name('CSI', 'CSI_API_OBJ_VER_MISMATCH');
3288       FND_MSG_PUB.ADD;
3289       RAISE FND_API.G_EXC_ERROR;
3290    END IF;
3291 
3292    CLOSE cur_group_rec;
3293 
3294    -- IF SQL%NOTFOUND THEN
3295    --    CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_GRP_NOTEXISTS');
3296    -- END IF;
3297 
3298    l_counter_groups_rec := p_counter_groups_rec;
3299 
3300    IF p_counter_groups_rec.name IS NULL THEN
3301       l_counter_groups_rec.name:= l_old_counter_groups_rec.name;
3302    ELSIF p_counter_groups_rec.name = FND_API.G_MISS_CHAR THEN
3303       l_counter_groups_rec.name := NULL;
3304    END IF;
3305 
3306    IF p_counter_groups_rec.description IS NULL THEN
3307       l_counter_groups_rec.description := l_old_counter_groups_rec.description;
3308    ELSIF p_counter_groups_rec.description = FND_API.G_MISS_CHAR THEN
3309       l_counter_groups_rec.description := NULL;
3310    END IF;
3311 
3312    IF p_counter_groups_rec.start_date_active IS NULL THEN
3313       l_counter_groups_rec.start_date_active := l_old_counter_groups_rec.start_date_active;
3314    ELSIF p_counter_groups_rec.start_date_active = FND_API.G_MISS_DATE THEN
3315       l_counter_groups_rec.start_date_active := NULL;
3316    END IF;
3317 
3318    IF p_counter_groups_rec.end_date_active IS NULL THEN
3319       l_counter_groups_rec.end_date_active := l_old_counter_groups_rec.end_date_active;
3320    ELSIF p_counter_groups_rec.end_date_active = FND_API.G_MISS_DATE THEN
3321       l_counter_groups_rec.end_date_active := NULL;
3322    END IF;
3323 
3324    if l_counter_groups_rec.end_date_active IS NOT NULL then
3325       if l_counter_groups_rec.end_date_active < l_counter_groups_rec.start_date_active then
3326          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_ALL_END_DATE');
3327       end if;
3328    end if;
3329 
3330    IF p_counter_groups_rec.attribute1 IS NULL THEN
3331       l_counter_groups_rec.attribute1 := l_old_counter_groups_rec.attribute1;
3332    ELSIF p_counter_groups_rec.attribute1 = FND_API.G_MISS_CHAR THEN
3333       l_counter_groups_rec.attribute1 := NULL;
3334    END IF;
3335 
3336    IF p_counter_groups_rec.attribute2 IS NULL THEN
3337       l_counter_groups_rec.attribute2 := l_old_counter_groups_rec.attribute2;
3338    ELSIF p_counter_groups_rec.attribute2 = FND_API.G_MISS_CHAR THEN
3339       l_counter_groups_rec.attribute2 := NULL;
3340    END IF;
3341 
3342    IF p_counter_groups_rec.attribute3 IS NULL THEN
3343       l_counter_groups_rec.attribute3 := l_old_counter_groups_rec.attribute3;
3344    ELSIF p_counter_groups_rec.attribute3 = FND_API.G_MISS_CHAR THEN
3345       l_counter_groups_rec.attribute3 := NULL;
3346    END IF;
3347 
3348    IF p_counter_groups_rec.attribute4 IS NULL THEN
3349       l_counter_groups_rec.attribute4 := l_old_counter_groups_rec.attribute4;
3350    ELSIF p_counter_groups_rec.attribute4 = FND_API.G_MISS_CHAR THEN
3351       l_counter_groups_rec.attribute4 := NULL;
3352    END IF;
3353 
3354    IF p_counter_groups_rec.attribute5 IS NULL THEN
3355       l_counter_groups_rec.attribute5 := l_old_counter_groups_rec.attribute5;
3356    ELSIF p_counter_groups_rec.attribute5 = FND_API.G_MISS_CHAR THEN
3357       l_counter_groups_rec.attribute5 := NULL;
3358    END IF;
3359 
3360    IF p_counter_groups_rec.attribute6 IS NULL THEN
3361       l_counter_groups_rec.attribute6 := l_old_counter_groups_rec.attribute6;
3362    ELSIF p_counter_groups_rec.attribute6 = FND_API.G_MISS_CHAR THEN
3363       l_counter_groups_rec.attribute6 := NULL;
3364    END IF;
3365 
3366    IF p_counter_groups_rec.attribute7 IS NULL THEN
3367       l_counter_groups_rec.attribute7 := l_old_counter_groups_rec.attribute6;
3368    ELSIF p_counter_groups_rec.attribute7 = FND_API.G_MISS_CHAR THEN
3369       l_counter_groups_rec.attribute7 := NULL;
3370    END IF;
3371 
3372    IF p_counter_groups_rec.attribute8 IS NULL THEN
3373       l_counter_groups_rec.attribute8 := l_old_counter_groups_rec.attribute8;
3374    ELSIF p_counter_groups_rec.attribute8 = FND_API.G_MISS_CHAR THEN
3375       l_counter_groups_rec.attribute8 := NULL;
3376    END IF;
3377 
3378    IF p_counter_groups_rec.attribute9 IS NULL THEN
3379       l_counter_groups_rec.attribute9 := l_old_counter_groups_rec.attribute9;
3380    ELSIF p_counter_groups_rec.attribute9 = FND_API.G_MISS_CHAR THEN
3381       l_counter_groups_rec.attribute9 := NULL;
3382    END IF;
3383 
3384    IF p_counter_groups_rec.attribute10 IS NULL THEN
3385       l_counter_groups_rec.attribute10 := l_old_counter_groups_rec.attribute10;
3386    ELSIF p_counter_groups_rec.attribute10 = FND_API.G_MISS_CHAR THEN
3387       l_counter_groups_rec.attribute10 := NULL;
3388    END IF;
3389 
3390    IF p_counter_groups_rec.attribute11 IS NULL THEN
3391       l_counter_groups_rec.attribute11 := l_old_counter_groups_rec.attribute11;
3392    ELSIF p_counter_groups_rec.attribute11 = FND_API.G_MISS_CHAR THEN
3393       l_counter_groups_rec.attribute11 := NULL;
3394    END IF;
3395 
3396    IF p_counter_groups_rec.attribute12 IS NULL THEN
3397       l_counter_groups_rec.attribute12 := l_old_counter_groups_rec.attribute12;
3398    ELSIF p_counter_groups_rec.attribute12 = FND_API.G_MISS_CHAR THEN
3399       l_counter_groups_rec.attribute12 := NULL;
3400    END IF;
3401 
3402    IF p_counter_groups_rec.attribute13 IS NULL THEN
3403       l_counter_groups_rec.attribute13 := l_old_counter_groups_rec.attribute13;
3404    ELSIF p_counter_groups_rec.attribute13 = FND_API.G_MISS_CHAR THEN
3405       l_counter_groups_rec.attribute13 := NULL;
3406    END IF;
3407 
3408    IF p_counter_groups_rec.attribute14 IS NULL THEN
3409       l_counter_groups_rec.attribute14 := l_old_counter_groups_rec.attribute14;
3410    ELSIF p_counter_groups_rec.attribute14 = FND_API.G_MISS_CHAR THEN
3411       l_counter_groups_rec.attribute14 := NULL;
3412    END IF;
3413 
3414    IF p_counter_groups_rec.attribute15 IS NULL THEN
3415       l_counter_groups_rec.attribute15 := l_old_counter_groups_rec.attribute15;
3416    ELSIF p_counter_groups_rec.attribute15 = FND_API.G_MISS_CHAR THEN
3417       l_counter_groups_rec.attribute15 := NULL;
3418    END IF;
3419 
3420    IF p_counter_groups_rec.context IS NULL THEN
3421       l_counter_groups_rec.context := l_old_counter_groups_rec.context;
3422    ELSIF p_counter_groups_rec.context = FND_API.G_MISS_CHAR THEN
3423       l_counter_groups_rec.context := NULL;
3424    END IF;
3425 
3426    IF p_counter_groups_rec.association_type IS NULL THEN
3427       l_counter_groups_rec.association_type := l_old_counter_groups_rec.association_type;
3428    ELSIF p_counter_groups_rec.association_type = FND_API.G_MISS_CHAR THEN
3429       l_counter_groups_rec.association_type := NULL;
3430    END IF;
3431 
3432    IF p_counter_groups_rec.source_object_code IS NULL THEN
3433       l_counter_groups_rec.source_object_code := l_old_counter_groups_rec.source_object_code;
3434    ELSIF p_counter_groups_rec.source_object_code = FND_API.G_MISS_CHAR THEN
3435       l_counter_groups_rec.source_object_code := NULL;
3436    END IF;
3437 
3438    IF p_counter_groups_rec.source_object_id IS NULL THEN
3439       l_counter_groups_rec.source_object_id := l_old_counter_groups_rec.source_object_id;
3440    ELSIF p_counter_groups_rec.source_object_id = FND_API.G_MISS_NUM THEN
3441       l_counter_groups_rec.source_object_id := NULL;
3442    END IF;
3443 
3444    IF p_counter_groups_rec.source_counter_group_id IS NULL THEN
3445       l_counter_groups_rec.source_counter_group_id := l_old_counter_groups_rec.source_counter_group_id;
3446    ELSIF p_counter_groups_rec.source_counter_group_id = FND_API.G_MISS_NUM THEN
3447       l_counter_groups_rec.source_counter_group_id := NULL;
3448    END IF;
3449 
3450    IF p_counter_groups_rec.security_group_id IS NULL THEN
3451       l_counter_groups_rec.security_group_id := l_old_counter_groups_rec.security_group_id;
3452    ELSIF p_counter_groups_rec.security_group_id = FND_API.G_MISS_NUM THEN
3453       l_counter_groups_rec.security_group_id := NULL;
3454    END IF;
3455 
3456    -- Counter group name is not updateable
3457 
3458    IF l_counter_groups_rec.name <> l_old_counter_groups_rec.name THEN
3459        CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_GRP_NOT_UPDATABLE');
3460    END IF;
3461 
3462 
3463    -- Call the table handler
3464    CSI_GROUPING_PKG.Update_Row(
3465         p_COUNTER_GROUP_ID 		=> p_counter_groups_rec.counter_group_id
3466         ,p_NAME                         => p_counter_groups_rec.name
3467         ,p_DESCRIPTION                  => p_counter_groups_rec.description
3468         ,p_TEMPLATE_FLAG                => p_counter_groups_rec.template_flag
3469         ,p_CP_SERVICE_ID                => NULL
3470         ,p_CUSTOMER_PRODUCT_ID          => NULL
3471         ,p_LAST_UPDATE_DATE             => sysdate
3472         ,p_LAST_UPDATED_BY              => FND_GLOBAL.USER_ID
3473         ,p_CREATION_DATE                => p_counter_groups_rec.creation_date
3474         ,p_CREATED_BY                   => p_counter_groups_rec.created_by
3475         ,p_LAST_UPDATE_LOGIN            => FND_GLOBAL.USER_ID
3476         ,p_START_DATE_ACTIVE            => p_counter_groups_rec.start_date_active
3477         ,p_END_DATE_ACTIVE              => p_counter_groups_rec.end_date_active
3478         ,p_ATTRIBUTE1                   => p_counter_groups_rec.attribute1
3479         ,p_ATTRIBUTE2                   => p_counter_groups_rec.attribute2
3480         ,p_ATTRIBUTE3                   => p_counter_groups_rec.attribute3
3481         ,p_ATTRIBUTE4                   => p_counter_groups_rec.attribute4
3482         ,p_ATTRIBUTE5                   => p_counter_groups_rec.attribute5
3483         ,p_ATTRIBUTE6                   => p_counter_groups_rec.attribute6
3484         ,p_ATTRIBUTE7                   => p_counter_groups_rec.attribute7
3485         ,p_ATTRIBUTE8                   => p_counter_groups_rec.attribute8
3486         ,p_ATTRIBUTE9                   => p_counter_groups_rec.attribute9
3487         ,p_ATTRIBUTE10                  => p_counter_groups_rec.attribute10
3488         ,p_ATTRIBUTE11                  => p_counter_groups_rec.attribute11
3489         ,p_ATTRIBUTE12                  => p_counter_groups_rec.attribute12
3490         ,p_ATTRIBUTE13                  => p_counter_groups_rec.attribute13
3491         ,p_ATTRIBUTE14                  => p_counter_groups_rec.attribute14
3492         ,p_ATTRIBUTE15                  => p_counter_groups_rec.attribute15
3493         ,p_CONTEXT                      => p_counter_groups_rec.context
3494         ,p_OBJECT_VERSION_NUMBER        => p_counter_groups_rec.object_version_number + 1
3495 	,p_CREATED_FROM_CTR_GRP_TMPL_ID => p_counter_groups_rec.created_from_ctr_grp_tmpl_id
3496 	,p_ASSOCIATION_TYPE             => p_counter_groups_rec.association_type
3497 	,p_SOURCE_OBJECT_CODE           => p_counter_groups_rec.source_object_code
3498 	,p_SOURCE_OBJECT_ID             => p_counter_groups_rec.source_object_id
3499 	,p_SOURCE_COUNTER_GROUP_ID      => p_counter_groups_rec.source_counter_group_id
3500 	,p_SECURITY_GROUP_ID            => p_counter_groups_rec.security_group_id
3501         );
3502 
3503    IF NOT (x_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
3504         ROLLBACK TO update_counter_group_pvt;
3505         RETURN;
3506    END IF;
3507 
3508 
3509    /* End of API Body */
3510 
3511    IF FND_API.to_Boolean(nvl(p_commit,FND_API.G_FALSE)) THEN
3512       COMMIT WORK;
3513    END IF;
3514 
3515    -- Standard call to get message count and IF count is  get message info.
3516    FND_MSG_PUB.Count_And_Get
3517       ( p_count  =>  x_msg_count,
3518         p_data   =>  x_msg_data
3519       );
3520 EXCEPTION
3521    WHEN FND_API.G_EXC_ERROR THEN
3522       x_return_status := FND_API.G_RET_STS_ERROR ;
3523       ROLLBACK TO update_counter_group_pvt;
3524       FND_MSG_PUB.Count_And_Get
3525          ( p_count => x_msg_count,
3526            p_data  => x_msg_data
3527          );
3528    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3529       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3530       ROLLBACK TO update_counter_group_pvt;
3531       FND_MSG_PUB.Count_And_Get
3532          ( p_count => x_msg_count,
3533            p_data  => x_msg_data
3534          );
3535    WHEN OTHERS THEN
3536       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3537       ROLLBACK TO update_counter_group_pvt;
3538       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
3539          FND_MSG_PUB.Add_Exc_Msg
3540             ( G_PKG_NAME,
3541               l_api_name
3542             );
3543       END IF;
3544       FND_MSG_PUB.Count_And_Get
3545          ( p_count  => x_msg_count,
3546            p_data   => x_msg_data
3547          );
3548 END update_counter_group;
3549 
3550 --|---------------------------------------------------
3551 --| procedure name: update_item_association
3552 --| description :   procedure used to
3553 --|                 update item association to
3554 --|                 counter group or counters
3555 --|---------------------------------------------------
3556 
3557 PROCEDURE update_item_association
3558  (
3559      p_api_version               IN     NUMBER
3560     ,p_commit                    IN     VARCHAR2
3561     ,p_init_msg_list             IN     VARCHAR2
3562     ,p_validation_level          IN     NUMBER
3563     ,p_ctr_item_associations_rec IN OUT NOCOPY CSI_CTR_DATASTRUCTURES_PUB.ctr_item_associations_rec
3564     ,x_return_status                OUT    NOCOPY VARCHAR2
3565     ,x_msg_count                    OUT    NOCOPY NUMBER
3566     ,x_msg_data                     OUT    NOCOPY VARCHAR2
3567  ) IS
3568 
3569    CURSOR cur_item_assoc_rec(p_ctr_association_id IN NUMBER) IS
3570    SELECT group_id
3571 	  ,inventory_item_id
3572   	  ,object_version_number
3573 	  ,last_update_date
3574   	  ,last_updated_by
3575   	  ,last_update_login
3576 	  ,creation_date
3577 	  ,created_by
3578 	  ,attribute1
3579 	  ,attribute2
3580 	  ,attribute3
3581 	  ,attribute4
3582 	  ,attribute5
3583 	  ,attribute6
3584 	  ,attribute7
3585 	  ,attribute8
3586 	  ,attribute9
3587 	  ,attribute10
3588 	  ,attribute11
3589 	  ,attribute12
3590 	  ,attribute13
3591 	  ,attribute14
3592 	  ,attribute15
3593 	  ,attribute_category
3594 	  ,security_group_id
3595 	  ,migrated_flag
3596 	  ,counter_id
3597 	  ,start_date_active
3598 	  ,end_date_active
3599 	  ,usage_rate
3600 	  -- ,association_type
3601   	  ,use_past_reading
3602 	  ,associated_to_group
3603 	  ,maint_organization_id
3604 	  ,primary_failure_flag
3605    FROM  csi_ctr_item_associations_v
3606    WHERE ctr_association_id = p_ctr_association_id
3607    FOR UPDATE OF OBJECT_VERSION_NUMBER;
3608    l_old_item_associations_rec  cur_item_assoc_rec%ROWTYPE;
3609 
3610    l_api_name                      CONSTANT VARCHAR2(30)   := 'UPDATE_ITEM_ASSOCIATION';
3611    l_api_version                   CONSTANT NUMBER         := 1.0;
3612    l_msg_data                      VARCHAR2(2000);
3613    l_msg_index                     NUMBER;
3614    l_msg_count                     NUMBER;
3615    -- l_debug_level                   NUMBER;
3616    l_group_id                      NUMBER;
3617    l_counter_id                    NUMBER;
3618    l_inventory_item_id             NUMBER;
3619    l_associated_to_group           VARCHAR2(1);
3620    l_item_found                    VARCHAR2(1);
3621    l_item_invalid                  VARCHAR2(1);
3622    l_item_associations_rec         CSI_CTR_DATASTRUCTURES_PUB.ctr_item_associations_rec;
3623 BEGIN
3624    -- Standard Start of API savepoint
3625    SAVEPOINT  update_item_association_pvt;
3626 
3627    -- Standard call to check for call compatibility.
3628    IF NOT FND_API.Compatible_API_Call (l_api_version,
3629                                        p_api_version,
3630                                        l_api_name   ,
3631                                        G_PKG_NAME   ) THEN
3632       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3633    END IF;
3634 
3635    -- Initialize message list if p_init_msg_list is set to TRUE.
3636    IF FND_API.to_Boolean(nvl(p_init_msg_list,FND_API.G_FALSE)) THEN
3637       FND_MSG_PUB.initialize;
3638    END IF;
3639 
3640    --  Initialize API return status to success
3641    x_return_status := FND_API.G_RET_STS_SUCCESS;
3642 
3643    -- Read the debug profiles values in to global variable 7197402
3644    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
3645 
3646    --
3647    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
3648       csi_ctr_gen_utility_pvt.put_line( 'update_item_association_pvt'           ||'-'||
3649                                      p_api_version                              ||'-'||
3650                                      nvl(p_commit,FND_API.G_FALSE)              ||'-'||
3651                                      nvl(p_init_msg_list,FND_API.G_FALSE)       ||'-'||
3652                                      nvl(p_validation_level,FND_API.G_VALID_LEVEL_FULL) );
3653    END IF;
3654 
3655    /* Start of API Body */
3656    OPEN cur_item_assoc_rec(p_ctr_item_associations_rec.ctr_association_id);
3657    FETCH cur_item_assoc_rec INTO l_old_item_associations_rec;
3658    IF  (l_old_item_associations_rec.object_version_number <> nvl(p_ctr_item_associations_rec.OBJECT_VERSION_NUMBER,0)) THEN
3659       FND_MESSAGE.Set_Name('CSI', 'CSI_API_OBJ_VER_MISMATCH');
3660       FND_MSG_PUB.ADD;
3661       RAISE FND_API.G_EXC_ERROR;
3662    END IF;
3663 
3664    CLOSE cur_item_assoc_rec;
3665 
3666    -- IF SQL%NOTFOUND THEN
3667    --     CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_ITEM_ASSOC_NOTEXISTS');
3668    -- END IF;
3669 
3670    l_item_associations_rec := p_ctr_item_associations_rec;
3671 
3672    IF p_ctr_item_associations_rec.group_id  IS NULL THEN
3673       l_item_associations_rec.group_id := l_old_item_associations_rec.group_id;
3674    ELSIF p_ctr_item_associations_rec.group_id = FND_API.G_MISS_NUM THEN
3675       l_item_associations_rec.group_id := NULL;
3676    END IF;
3677 
3678    IF p_ctr_item_associations_rec.inventory_item_id IS NULL THEN
3679       l_item_associations_rec.inventory_item_id := l_old_item_associations_rec.inventory_item_id;
3680    ELSIF p_ctr_item_associations_rec.inventory_item_id = FND_API.G_MISS_NUM THEN
3681       l_item_associations_rec.inventory_item_id := NULL;
3682    END IF;
3683 
3684    IF p_ctr_item_associations_rec.start_date_active IS NULL THEN
3685       l_item_associations_rec.start_date_active := l_old_item_associations_rec.start_date_active;
3686    ELSIF p_ctr_item_associations_rec.start_date_active = FND_API.G_MISS_DATE THEN
3687       l_item_associations_rec.start_date_active := NULL;
3688    END IF;
3689 
3690    IF p_ctr_item_associations_rec.end_date_active IS NULL THEN
3691       l_item_associations_rec.end_date_active := l_old_item_associations_rec.end_date_active;
3692    ELSIF p_ctr_item_associations_rec.end_date_active = FND_API.G_MISS_DATE THEN
3693       l_item_associations_rec.end_date_active := NULL;
3694    END IF;
3695 
3696    if l_item_associations_rec.end_date_active IS NOT NULL then
3697       if l_item_associations_rec.end_date_active < l_item_associations_rec.start_date_active then
3698          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_ALL_END_DATE');
3699       end if;
3700    end if;
3701 
3702    IF p_ctr_item_associations_rec.attribute1 IS NULL THEN
3703       l_item_associations_rec.attribute1 := l_old_item_associations_rec.attribute1;
3704    ELSIF p_ctr_item_associations_rec.attribute1 = FND_API.G_MISS_CHAR THEN
3705       l_item_associations_rec.attribute1 := NULL;
3706    END IF;
3707 
3708    IF p_ctr_item_associations_rec.attribute2 IS NULL THEN
3709       l_item_associations_rec.attribute2 := l_old_item_associations_rec.attribute2;
3710    ELSIF p_ctr_item_associations_rec.attribute2 = FND_API.G_MISS_CHAR THEN
3711       l_item_associations_rec.attribute2 := NULL;
3712    END IF;
3713 
3714    IF p_ctr_item_associations_rec.attribute3 IS NULL THEN
3715       l_item_associations_rec.attribute3 := l_old_item_associations_rec.attribute3;
3716    ELSIF p_ctr_item_associations_rec.attribute3 = FND_API.G_MISS_CHAR THEN
3717       l_item_associations_rec.attribute3 := NULL;
3718    END IF;
3719 
3720    IF p_ctr_item_associations_rec.attribute4 IS NULL THEN
3721       l_item_associations_rec.attribute4 := l_old_item_associations_rec.attribute4;
3722    ELSIF p_ctr_item_associations_rec.attribute4 = FND_API.G_MISS_CHAR THEN
3723       l_item_associations_rec.attribute4 := NULL;
3724    END IF;
3725 
3726    IF p_ctr_item_associations_rec.attribute5 IS NULL THEN
3727       l_item_associations_rec.attribute5 := l_old_item_associations_rec.attribute5;
3728    ELSIF p_ctr_item_associations_rec.attribute5 = FND_API.G_MISS_CHAR THEN
3729       l_item_associations_rec.attribute5 := NULL;
3730    END IF;
3731 
3732    IF p_ctr_item_associations_rec.attribute6 IS NULL THEN
3733       l_item_associations_rec.attribute6 := l_old_item_associations_rec.attribute6;
3734    ELSIF p_ctr_item_associations_rec.attribute6 = FND_API.G_MISS_CHAR THEN
3735       l_item_associations_rec.attribute6 := NULL;
3736    END IF;
3737 
3738    IF p_ctr_item_associations_rec.attribute7 IS NULL THEN
3739       l_item_associations_rec.attribute7 := l_old_item_associations_rec.attribute7;
3740    ELSIF p_ctr_item_associations_rec.attribute7 = FND_API.G_MISS_CHAR THEN
3741       l_item_associations_rec.attribute7 := NULL;
3742    END IF;
3743 
3744    IF p_ctr_item_associations_rec.attribute8 IS NULL THEN
3745       l_item_associations_rec.attribute8 := l_old_item_associations_rec.attribute8;
3746    ELSIF p_ctr_item_associations_rec.attribute8 = FND_API.G_MISS_CHAR THEN
3747       l_item_associations_rec.attribute8 := NULL;
3748    END IF;
3749 
3750    IF p_ctr_item_associations_rec.attribute9 IS NULL THEN
3751       l_item_associations_rec.attribute9 := l_old_item_associations_rec.attribute9;
3752    ELSIF p_ctr_item_associations_rec.attribute9 = FND_API.G_MISS_CHAR THEN
3753       l_item_associations_rec.attribute9 := NULL;
3754    END IF;
3755 
3756    IF p_ctr_item_associations_rec.attribute10 IS NULL THEN
3757       l_item_associations_rec.attribute10 := l_old_item_associations_rec.attribute10;
3758    ELSIF p_ctr_item_associations_rec.attribute10 = FND_API.G_MISS_CHAR THEN
3759       l_item_associations_rec.attribute10 := NULL;
3760    END IF;
3761 
3762    IF p_ctr_item_associations_rec.attribute11 IS NULL THEN
3763       l_item_associations_rec.attribute11 := l_old_item_associations_rec.attribute11;
3764    ELSIF p_ctr_item_associations_rec.attribute11 = FND_API.G_MISS_CHAR THEN
3765       l_item_associations_rec.attribute11 := NULL;
3766    END IF;
3767 
3768    IF p_ctr_item_associations_rec.attribute12 IS NULL THEN
3769       l_item_associations_rec.attribute12 := l_old_item_associations_rec.attribute12;
3770    ELSIF p_ctr_item_associations_rec.attribute12 = FND_API.G_MISS_CHAR THEN
3771       l_item_associations_rec.attribute12 := NULL;
3772    END IF;
3773 
3774    IF p_ctr_item_associations_rec.attribute13 IS NULL THEN
3775       l_item_associations_rec.attribute13 := l_old_item_associations_rec.attribute13;
3776    ELSIF p_ctr_item_associations_rec.attribute13 = FND_API.G_MISS_CHAR THEN
3777       l_item_associations_rec.attribute13 := NULL;
3778    END IF;
3779 
3780    IF p_ctr_item_associations_rec.attribute14 IS NULL THEN
3781       l_item_associations_rec.attribute14 := l_old_item_associations_rec.attribute14;
3782    ELSIF p_ctr_item_associations_rec.attribute14 = FND_API.G_MISS_CHAR THEN
3783       l_item_associations_rec.attribute14 := NULL;
3784    END IF;
3785 
3786    IF p_ctr_item_associations_rec.attribute15 IS NULL THEN
3787       l_item_associations_rec.attribute15 := l_old_item_associations_rec.attribute15;
3788    ELSIF p_ctr_item_associations_rec.attribute15 = FND_API.G_MISS_CHAR THEN
3789       l_item_associations_rec.attribute15 := NULL;
3790    END IF;
3791 
3792    IF p_ctr_item_associations_rec.attribute_category IS NULL THEN
3793       l_item_associations_rec.attribute_category := l_old_item_associations_rec.attribute_category;
3794    ELSIF p_ctr_item_associations_rec.attribute_category = FND_API.G_MISS_CHAR THEN
3795       l_item_associations_rec.attribute_category := NULL;
3796    END IF;
3797 
3798    IF p_ctr_item_associations_rec.associated_to_group IS NULL THEN
3799       l_item_associations_rec.associated_to_group := l_old_item_associations_rec.associated_to_group;
3800    ELSIF p_ctr_item_associations_rec.associated_to_group = FND_API.G_MISS_CHAR THEN
3801       l_item_associations_rec.associated_to_group := NULL;
3802    END IF;
3803 
3804    IF p_ctr_item_associations_rec.usage_rate IS NULL THEN
3805       l_item_associations_rec.usage_rate := l_old_item_associations_rec.usage_rate;
3806    ELSIF p_ctr_item_associations_rec.usage_rate = FND_API.G_MISS_NUM THEN
3807       l_item_associations_rec.usage_rate := NULL;
3808    END IF;
3809 
3810    IF p_ctr_item_associations_rec.counter_id IS NULL THEN
3811       l_item_associations_rec.counter_id := l_old_item_associations_rec.counter_id;
3812    ELSIF p_ctr_item_associations_rec.counter_id = FND_API.G_MISS_NUM THEN
3813       l_item_associations_rec.counter_id := NULL;
3814    END IF;
3815 
3816    IF p_ctr_item_associations_rec.security_group_id IS NULL THEN
3817       l_item_associations_rec.security_group_id := l_old_item_associations_rec.security_group_id;
3818    ELSIF p_ctr_item_associations_rec.security_group_id = FND_API.G_MISS_NUM THEN
3819       l_item_associations_rec.security_group_id := NULL;
3820 
3821    END IF;
3822 
3823    IF p_ctr_item_associations_rec.migrated_flag IS NULL THEN
3824       l_item_associations_rec.migrated_flag := l_old_item_associations_rec.migrated_flag;
3825    ELSIF p_ctr_item_associations_rec.migrated_flag = FND_API.G_MISS_CHAR THEN
3826       l_item_associations_rec.migrated_flag := NULL;
3827    END IF;
3828 
3829    IF p_ctr_item_associations_rec.use_past_reading IS NULL THEN
3830       l_item_associations_rec.use_past_reading := l_old_item_associations_rec.use_past_reading;
3831    ELSIF p_ctr_item_associations_rec.use_past_reading = FND_API.G_MISS_NUM THEN
3832       l_item_associations_rec.use_past_reading := NULL;
3833    END IF;
3834 
3835    IF p_ctr_item_associations_rec.maint_organization_id IS NULL THEN
3836       l_item_associations_rec.maint_organization_id := l_old_item_associations_rec.maint_organization_id;
3837    ELSIF p_ctr_item_associations_rec.maint_organization_id = FND_API.G_MISS_NUM THEN
3838       l_item_associations_rec.maint_organization_id := NULL;
3839    END IF;
3840 
3841    IF p_ctr_item_associations_rec.primary_failure_flag IS NULL THEN
3842       l_item_associations_rec.primary_failure_flag := l_old_item_associations_rec.primary_failure_flag;
3843    ELSIF p_ctr_item_associations_rec.primary_failure_flag = FND_API.G_MISS_CHAR THEN
3844       l_item_associations_rec.primary_failure_flag := NULL;
3845    END IF;
3846 
3847    IF l_item_associations_rec.group_id IS NOT NULL THEN
3848       IF l_item_associations_rec.inventory_item_id IS NOT NULL THEN
3849          IF l_item_associations_rec.inventory_item_id <> l_old_item_associations_rec.inventory_item_id THEN
3850             BEGIN
3851                SELECT 'x'
3852                INTO   l_item_invalid
3853                FROM   csi_ctr_item_associations
3854                WHERE  inventory_item_id = l_item_associations_rec.inventory_item_id;
3855 
3856                CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_ASSOC');
3857             EXCEPTION
3858                WHEN NO_DATA_FOUND THEN NULL;
3859                WHEN TOO_MANY_ROWS THEN
3860                CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_ASSOC');
3861             END;
3862          END IF;
3863       END IF;
3864    END IF;
3865 
3866    /* IF l_item_associations_rec.associated_to_group = 'Y' THEN
3867       IF l_item_associations_rec.group_id IS NOT NULL THEN
3868          BEGIN
3869             SELECT 'x'
3870             INTO   l_item_invalid
3871             FROM   csi_ctr_item_associations
3872             WHERE  inventory_item_id = l_item_associations_rec.inventory_item_id
3873             AND    associated_to_group = l_item_associations_rec.associated_to_group
3874             -- AND    group_id = l_group_id
3875             AND    counter_id = l_item_associations_rec.counter_id;
3876 
3877             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_ASSOC');
3878          EXCEPTION
3879             WHEN NO_DATA_FOUND THEN NULL;
3880             WHEN TOO_MANY_ROWS THEN
3881                CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_ASSOC');
3882          END;
3883       ELSE
3884          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_PARM_GRP_NAME');
3885       END IF;
3886    ELSE
3887       IF l_item_associations_rec.group_id IS NULL THEN
3888          IF l_item_associations_rec.counter_id IS NULL THEN
3889             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_CTR_ID');
3890          END IF;
3891 
3892          BEGIN
3893             SELECT 'Y'
3894             INTO   l_item_found
3895             FROM   csi_ctr_item_associations
3896             WHERE  inventory_item_id = l_item_associations_rec.inventory_item_id
3897             AND    associated_to_group = 'Y';
3898 
3899          EXCEPTION
3900             WHEN NO_DATA_FOUND THEN
3901                l_item_found := 'N';
3902             WHEN TOO_MANY_ROWS THEN
3903                l_item_found := 'Y';
3904          END;
3905 
3906          IF l_item_found = 'Y' THEN
3907             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_ASSOC');
3908          ELSE
3909             BEGIN
3910                SELECT 'x'
3911                INTO   l_item_invalid
3912                FROM   csi_ctr_item_associations
3913                WHERE  inventory_item_id = l_item_associations_rec.inventory_item_id
3914                AND    associated_to_group = l_item_associations_rec.associated_to_group
3915                AND    group_id IS NULL
3916                AND    counter_id = l_item_associations_rec.counter_id;
3917 
3918                CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_ASSOC');
3919             EXCEPTION
3920                WHEN NO_DATA_FOUND THEN NULL;
3921                WHEN OTHERS THEN
3922                   CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_ITEM_ASSOC');
3923             END;
3924          END IF;
3925       ELSE
3926          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_GRP_NULL');
3927       END IF;
3928    END IF;
3929 
3930    */
3931    -- Call the table handler
3932 
3933    CSI_CTR_ITEM_ASSOCIATIONS_PKG.Update_Row(
3934 	p_CTR_ASSOCIATION_ID            => p_ctr_item_associations_rec.ctr_association_id
3935 	,p_GROUP_ID                     => p_ctr_item_associations_rec.group_id
3936 	,p_INVENTORY_ITEM_ID            => p_ctr_item_associations_rec.inventory_item_id
3937 	,p_OBJECT_VERSION_NUMBER        => p_ctr_item_associations_rec.object_version_number + 1
3938 	,p_LAST_UPDATE_DATE             => sysdate
3939 	,p_LAST_UPDATED_BY              => FND_GLOBAL.USER_ID
3940 	,p_LAST_UPDATE_LOGIN            => FND_GLOBAL.USER_ID
3941 	,p_CREATION_DATE                => p_ctr_item_associations_rec.creation_date
3942         ,p_CREATED_BY                   => p_ctr_item_associations_rec.created_by
3943         ,p_ATTRIBUTE1                   => p_ctr_item_associations_rec.attribute1
3944         ,p_ATTRIBUTE2                   => p_ctr_item_associations_rec.attribute2
3945         ,p_ATTRIBUTE3                   => p_ctr_item_associations_rec.attribute3
3946         ,p_ATTRIBUTE4                   => p_ctr_item_associations_rec.attribute4
3947         ,p_ATTRIBUTE5                   => p_ctr_item_associations_rec.attribute5
3948         ,p_ATTRIBUTE6                   => p_ctr_item_associations_rec.attribute6
3949         ,p_ATTRIBUTE7                   => p_ctr_item_associations_rec.attribute7
3950         ,p_ATTRIBUTE8                   => p_ctr_item_associations_rec.attribute8
3951         ,p_ATTRIBUTE9                   => p_ctr_item_associations_rec.attribute9
3952         ,p_ATTRIBUTE10                  => p_ctr_item_associations_rec.attribute10
3953         ,p_ATTRIBUTE11                  => p_ctr_item_associations_rec.attribute11
3954         ,p_ATTRIBUTE12                  => p_ctr_item_associations_rec.attribute12
3955         ,p_ATTRIBUTE13                  => p_ctr_item_associations_rec.attribute13
3956         ,p_ATTRIBUTE14                  => p_ctr_item_associations_rec.attribute14
3957         ,p_ATTRIBUTE15                  => p_ctr_item_associations_rec.attribute15
3958 	,p_ATTRIBUTE_CATEGORY           => p_ctr_item_associations_rec.attribute_category
3959 	,p_SECURITY_GROUP_ID            => p_ctr_item_associations_rec.security_group_id
3960 	,p_MIGRATED_FLAG                => p_ctr_item_associations_rec.migrated_flag
3961 	,p_COUNTER_ID                   => p_ctr_item_associations_rec.counter_id
3962         ,p_START_DATE_ACTIVE            => p_ctr_item_associations_rec.start_date_active
3963         ,p_END_DATE_ACTIVE              => p_ctr_item_associations_rec.end_date_active
3964         ,p_USAGE_RATE                   => p_ctr_item_associations_rec.usage_rate
3965         -- ,p_ASSOCIATION_TYPE             => p_ctr_item_associations_rec.association_type
3966         ,p_USE_PAST_READING             => p_ctr_item_associations_rec.use_past_reading
3967 	,p_ASSOCIATED_TO_GROUP          => p_ctr_item_associations_rec.associated_to_group
3968 	,p_MAINT_ORGANIZATION_ID        => NULL
3969 	,p_PRIMARY_FAILURE_FLAG         => p_ctr_item_associations_rec.primary_failure_flag
3970         );
3971 
3972    IF NOT (x_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
3973         ROLLBACK TO update_item_association_pvt;
3974         RETURN;
3975    END IF;
3976 
3977     /* End of API Body */
3978 
3979    IF FND_API.to_Boolean(nvl(p_commit,FND_API.G_FALSE)) THEN
3980       COMMIT WORK;
3981    END IF;
3982 
3983    -- Standard call to get message count and IF count is  get message info.
3984    FND_MSG_PUB.Count_And_Get
3985       ( p_count  =>  x_msg_count,
3986         p_data   =>  x_msg_data
3987       );
3988 EXCEPTION
3989    WHEN FND_API.G_EXC_ERROR THEN
3990       x_return_status := FND_API.G_RET_STS_ERROR ;
3991       ROLLBACK TO update_item_association_pvt;
3992       FND_MSG_PUB.Count_And_Get
3993          ( p_count => x_msg_count,
3994            p_data  => x_msg_data
3995          );
3996    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3997       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3998       ROLLBACK TO update_item_association_pvt;
3999       FND_MSG_PUB.Count_And_Get
4000          ( p_count => x_msg_count,
4001            p_data  => x_msg_data
4002          );
4003    WHEN OTHERS THEN
4004       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4005       ROLLBACK TO update_item_association_pvt;
4006       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4007          FND_MSG_PUB.Add_Exc_Msg
4008             ( G_PKG_NAME,
4009               l_api_name
4010             );
4011       END IF;
4012       FND_MSG_PUB.Count_And_Get
4013          ( p_count  => x_msg_count,
4014            p_data   => x_msg_data
4015          );
4016 END update_item_association;
4017 
4018 --|---------------------------------------------------
4019 --| procedure name: update_counter_template
4020 --| description :   procedure used to
4021 --|                 update counter template
4022 --|---------------------------------------------------
4023 
4024 PROCEDURE update_counter_template
4025  (
4026      p_api_version               IN     NUMBER
4027     ,p_commit                    IN     VARCHAR2
4028     ,p_init_msg_list             IN     VARCHAR2
4029     ,p_validation_level          IN     NUMBER
4030     ,p_counter_template_rec      IN OUT NOCOPY CSI_CTR_DATASTRUCTURES_PUB.counter_template_rec
4031     ,x_return_status                OUT NOCOPY VARCHAR2
4032     ,x_msg_count                    OUT NOCOPY NUMBER
4033     ,x_msg_data                     OUT NOCOPY VARCHAR2
4034  ) IS
4035 
4036    CURSOR cur_ctr_template_rec(p_counter_id IN NUMBER) IS
4037    SELECT counter_id,
4038           group_id,
4039           counter_type,
4040           initial_reading,
4041           initial_reading_date,
4042           step_value,
4043           tolerance_plus,
4044           tolerance_minus,
4045           uom_code,
4046           derive_counter_id,
4047           derive_function,
4048           valid_flag,
4049           formula_incomplete_flag,
4050           formula_text,
4051           rollover_last_reading,
4052           rollover_first_reading,
4053           usage_item_id,
4054           ctr_val_max_seq_no,
4055           start_date_active,
4056           end_date_active,
4057           attribute1,
4058           attribute2,
4059           attribute3,
4060           attribute4,
4061           attribute5,
4062           attribute6,
4063           attribute7,
4064           attribute8,
4065           attribute9,
4066           attribute10,
4067           attribute11,
4068           attribute12,
4069           attribute13,
4070           attribute14,
4071           attribute15,
4072           attribute16,
4073           attribute17,
4074           attribute18,
4075           attribute19,
4076           attribute20,
4077           attribute21,
4078           attribute22,
4079           attribute23,
4080           attribute24,
4081           attribute25,
4082           attribute26,
4083           attribute27,
4084           attribute28,
4085           attribute29,
4086           attribute30,
4087           attribute_category,
4088           customer_view,
4089           direction,
4090           filter_type,
4091           filter_reading_count,
4092           filter_time_uom,
4093           estimation_id,
4094           reading_type,
4095           automatic_rollover,
4096           default_usage_rate,
4097           use_past_reading,
4098           used_in_scheduling,
4099           defaulted_group_id,
4100           object_version_number,
4101           comments,
4102           association_type,
4103           time_based_manual_entry,
4104           eam_required_flag
4105    FROM   csi_counter_template_vl
4106    WHERE  counter_id = p_counter_id
4107    FOR UPDATE OF OBJECT_VERSION_NUMBER;
4108    l_old_counter_template_rec  cur_ctr_template_rec%ROWTYPE;
4109 
4110 
4111    CURSOR formula_ref_cur(p_counter_id number) IS
4112    SELECT relationship_id
4113    FROM   csi_counter_relationships
4114    WHERE  object_counter_id = p_counter_id
4115    AND    relationship_type_code = 'FORMULA';
4116 
4117    CURSOR derived_filters_cur(p_counter_id NUMBER) IS
4118    SELECT counter_derived_filter_id
4119    FROM   csi_counter_derived_filters
4120    WHERE  counter_id = p_counter_id;
4121 
4122    CURSOR target_counter_cur(p_counter_id NUMBER) IS
4123    SELECT relationship_id
4124    FROM   csi_counter_relationships
4125    WHERE  source_counter_id = p_counter_id
4126    AND    relationship_type_code = 'CONFIGURATION';
4127 
4128    CURSOR counter_readings_cur(p_counter_id  NUMBER) IS
4129    SELECT counter_value_id
4130    FROM   csi_counter_readings
4131    WHERE  counter_id = p_counter_id;
4132 
4133 
4134    l_api_name                      CONSTANT VARCHAR2(30)   := 'UPDATE_COUNTER_TEMPLATE';
4135    l_api_version                   CONSTANT NUMBER         := 1.0;
4136    l_msg_data                      VARCHAR2(2000);
4137    l_msg_index                     NUMBER;
4138    l_msg_count                     NUMBER;
4139    -- l_debug_level                   NUMBER;
4140 
4141    l_counter_template_rec            CSI_CTR_DATASTRUCTURES_PUB.counter_template_rec;
4142    l_formula_ref_count             NUMBER;
4143    l_der_filter_count              NUMBER;
4144    l_target_ctr_exist              NUMBER;
4145    l_rdg_exists                    NUMBER;
4146    l_return_status		   VARCHAR2(1);
4147 
4148 BEGIN
4149    -- Standard Start of API savepoint
4150    SAVEPOINT  update_counter_template_pvt;
4151 
4152    -- Standard call to check for call compatibility.
4153    IF NOT FND_API.Compatible_API_Call (l_api_version,
4154                                        p_api_version,
4155                                        l_api_name   ,
4156                                        G_PKG_NAME   ) THEN
4157       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4158    END IF;
4159 
4160    -- Initialize message list if p_init_msg_list is set to TRUE.
4161    IF FND_API.to_Boolean(nvl(p_init_msg_list,FND_API.G_FALSE)) THEN
4162       FND_MSG_PUB.initialize;
4163    END IF;
4164 
4165    --  Initialize API return status to success
4166    x_return_status := FND_API.G_RET_STS_SUCCESS;
4167 
4168    -- Read the debug profiles values in to global variable 7197402
4169    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
4170 
4171    --
4172    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
4173       csi_ctr_gen_utility_pvt.put_line( 'update_counter_template_pvt'           ||'-'||
4174                                      p_api_version                              ||'-'||
4175                                      nvl(p_commit,FND_API.G_FALSE)              ||'-'||
4176                                      nvl(p_init_msg_list,FND_API.G_FALSE)       ||'-'||
4177                                      nvl(p_validation_level,FND_API.G_VALID_LEVEL_FULL) );
4178    END IF;
4179 
4180    /* Start of API body */
4181    OPEN cur_ctr_template_rec(p_counter_template_rec.counter_id);
4182    FETCH cur_ctr_template_rec INTO l_old_counter_template_rec;
4183    IF  (l_old_counter_template_rec.object_version_number <> nvl(p_counter_template_rec.OBJECT_VERSION_NUMBER,0)) THEN
4184       FND_MESSAGE.Set_Name('CSI', 'CSI_API_OBJ_VER_MISMATCH');
4185       FND_MSG_PUB.ADD;
4186       RAISE FND_API.G_EXC_ERROR;
4187    END IF;
4188 
4189    CLOSE cur_ctr_template_rec;
4190 
4191    -- IF SQL%NOTFOUND THEN
4192    --   CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INVALID');
4193    -- END IF;
4194 
4195    -- Bug  8686933
4196    -- Assigning group id to defaulted group id column
4197    --
4198    IF (NVL(p_counter_template_rec.group_id, FND_API.G_MISS_NUM) <> FND_API.G_MISS_NUM) THEN
4199    p_counter_template_rec.defaulted_group_id := p_counter_template_rec.group_id;
4200    END IF;
4201 
4202    l_counter_template_rec := p_counter_template_rec;
4203 
4204    IF p_counter_template_rec.group_id  = NULL THEN
4205       l_counter_template_rec.group_id := l_old_counter_template_rec.group_id;
4206    ELSIF p_counter_template_rec.group_id = FND_API.G_MISS_NUM THEN
4207       l_counter_template_rec.group_id := NULL;
4208    END IF;
4209 
4210    IF p_counter_template_rec.counter_type  IS NULL THEN
4211       l_counter_template_rec.counter_type := l_old_counter_template_rec.counter_type;
4212    ELSIF p_counter_template_rec.counter_type = FND_API.G_MISS_CHAR THEN
4213       l_counter_template_rec.counter_type := NULL;
4214    END IF;
4215 
4216    IF p_counter_template_rec.initial_reading  IS NULL THEN
4217       l_counter_template_rec.initial_reading := l_old_counter_template_rec.initial_reading;
4218    ELSIF p_counter_template_rec.initial_reading = FND_API.G_MISS_NUM THEN
4219       l_counter_template_rec.initial_reading := NULL;
4220    END IF;
4221 
4222    IF p_counter_template_rec.step_value IS NULL THEN
4223       l_counter_template_rec.step_value := l_old_counter_template_rec.step_value;
4224    ELSIF p_counter_template_rec.step_value = FND_API.G_MISS_NUM THEN
4225       l_counter_template_rec.step_value := NULL;
4226    END IF;
4227 
4228    IF p_counter_template_rec.initial_reading_date IS NULL THEN
4229       l_counter_template_rec.initial_reading_date := l_old_counter_template_rec.initial_reading_date;
4230    ELSIF p_counter_template_rec.initial_reading_date = FND_API.G_MISS_DATE THEN
4231       l_counter_template_rec.initial_reading_date := NULL;
4232    END IF;
4233 
4234    IF p_counter_template_rec.tolerance_plus IS NULL THEN
4235       l_counter_template_rec.tolerance_plus := l_old_counter_template_rec.tolerance_plus;
4236    ELSIF p_counter_template_rec.tolerance_plus = FND_API.G_MISS_NUM THEN
4237       l_counter_template_rec.tolerance_plus := NULL;
4238    END IF;
4239 
4240    IF p_counter_template_rec.tolerance_minus IS NULL THEN
4241       l_counter_template_rec.tolerance_minus := l_old_counter_template_rec.tolerance_minus;
4242    ELSIF p_counter_template_rec.tolerance_minus = FND_API.G_MISS_NUM THEN
4243       l_counter_template_rec.tolerance_minus := NULL;
4244    END IF;
4245 
4246    IF p_counter_template_rec.uom_code IS NULL THEN
4247       l_counter_template_rec.uom_code := l_old_counter_template_rec.uom_code;
4248    ELSIF p_counter_template_rec.uom_code = FND_API.G_MISS_CHAR THEN
4249       l_counter_template_rec.uom_code := NULL;
4250    END IF;
4251 
4252    IF p_counter_template_rec.derive_counter_id IS NULL THEN
4253       l_counter_template_rec.derive_counter_id := l_old_counter_template_rec.derive_counter_id;
4254    ELSIF p_counter_template_rec.derive_counter_id = FND_API.G_MISS_NUM THEN
4255       l_counter_template_rec.derive_counter_id := NULL;
4256    END IF;
4257 
4258    IF p_counter_template_rec.derive_function IS NULL THEN
4259       l_counter_template_rec.derive_function := l_old_counter_template_rec.derive_function;
4260    ELSIF p_counter_template_rec.derive_function = FND_API.G_MISS_CHAR THEN
4261       l_counter_template_rec.derive_function := NULL;
4262    END IF;
4263 
4264    IF p_counter_template_rec.valid_flag IS NULL THEN
4265       l_counter_template_rec.valid_flag := l_old_counter_template_rec.valid_flag;
4266    ELSIF p_counter_template_rec.valid_flag = FND_API.G_MISS_CHAR THEN
4267       l_counter_template_rec.valid_flag := NULL;
4268    END IF;
4269 
4270    IF p_counter_template_rec.formula_incomplete_flag IS NULL THEN
4271       l_counter_template_rec.formula_incomplete_flag := l_old_counter_template_rec.formula_incomplete_flag;
4272    ELSIF p_counter_template_rec.formula_incomplete_flag = FND_API.G_MISS_CHAR THEN
4273       l_counter_template_rec.formula_incomplete_flag := NULL;
4274    END IF;
4275 
4276    IF p_counter_template_rec.formula_text IS NULL THEN
4277       l_counter_template_rec.formula_text := l_old_counter_template_rec.formula_text;
4278    ELSIF p_counter_template_rec.formula_text = FND_API.G_MISS_CHAR THEN
4279       l_counter_template_rec.formula_text := NULL;
4280    END IF;
4281 
4282    IF p_counter_template_rec.rollover_last_reading IS NULL THEN
4283       l_counter_template_rec.rollover_last_reading := l_old_counter_template_rec.rollover_last_reading;
4284    ELSIF p_counter_template_rec.rollover_last_reading = FND_API.G_MISS_NUM THEN
4285       l_counter_template_rec.rollover_last_reading := NULL;
4286    END IF;
4287 
4288    IF p_counter_template_rec.rollover_first_reading IS NULL THEN
4289       l_counter_template_rec.rollover_first_reading := l_old_counter_template_rec.rollover_first_reading;
4290    ELSIF p_counter_template_rec.rollover_first_reading = FND_API.G_MISS_NUM THEN
4291       l_counter_template_rec.rollover_first_reading := NULL;
4292    END IF;
4293 
4294    IF p_counter_template_rec.usage_item_id IS NULL THEN
4295       l_counter_template_rec.usage_item_id := l_old_counter_template_rec.usage_item_id;
4296    ELSIF p_counter_template_rec.usage_item_id = FND_API.G_MISS_NUM THEN
4297       l_counter_template_rec.usage_item_id := NULL;
4298    END IF;
4299 
4300    IF p_counter_template_rec.ctr_val_max_seq_no IS NULL THEN
4301       l_counter_template_rec.ctr_val_max_seq_no := l_old_counter_template_rec.ctr_val_max_seq_no;
4302    ELSIF p_counter_template_rec.ctr_val_max_seq_no = FND_API.G_MISS_NUM THEN
4303       l_counter_template_rec.ctr_val_max_seq_no := NULL;
4304    END IF;
4305 
4306    IF p_counter_template_rec.start_date_active IS NULL THEN
4307       l_counter_template_rec.start_date_active := l_old_counter_template_rec.start_date_active;
4308    ELSIF p_counter_template_rec.start_date_active = FND_API.G_MISS_DATE THEN
4309       l_counter_template_rec.start_date_active := NULL;
4310    END IF;
4311 
4312    IF p_counter_template_rec.end_date_active IS NULL THEN
4313       l_counter_template_rec.end_date_active := l_old_counter_template_rec.end_date_active;
4314    ELSIF p_counter_template_rec.end_date_active = FND_API.G_MISS_DATE THEN
4315       l_counter_template_rec.end_date_active := NULL;
4316    END IF;
4317 
4318    if l_counter_template_rec.end_date_active IS NOT NULL then
4319       if l_counter_template_rec.end_date_active < l_counter_template_rec.start_date_active then
4320          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_ALL_END_DATE');
4321       end if;
4322    end if;
4323 
4324 
4325    IF p_counter_template_rec.attribute1 IS NULL THEN
4326       l_counter_template_rec.attribute1 := l_old_counter_template_rec.attribute1;
4327    ELSIF p_counter_template_rec.attribute1 = FND_API.G_MISS_CHAR THEN
4328       l_counter_template_rec.attribute1 := NULL;
4329    END IF;
4330 
4331    IF p_counter_template_rec.attribute2 IS NULL THEN
4332       l_counter_template_rec.attribute2 := l_old_counter_template_rec.attribute2;
4333    ELSIF p_counter_template_rec.attribute2 = FND_API.G_MISS_CHAR THEN
4334       l_counter_template_rec.attribute2 := NULL;
4335    END IF;
4336 
4337    IF p_counter_template_rec.attribute3 IS NULL THEN
4338       l_counter_template_rec.attribute3 := l_old_counter_template_rec.attribute3;
4339    ELSIF p_counter_template_rec.attribute3 = FND_API.G_MISS_CHAR THEN
4340       l_counter_template_rec.attribute3 := NULL;
4341    END IF;
4342 
4343    IF p_counter_template_rec.attribute4 IS NULL THEN
4344       l_counter_template_rec.attribute4 := l_old_counter_template_rec.attribute4;
4345    ELSIF p_counter_template_rec.attribute4 = FND_API.G_MISS_CHAR THEN
4346       l_counter_template_rec.attribute4 := NULL;
4347    END IF;
4348 
4349    IF p_counter_template_rec.attribute5 IS NULL THEN
4350       l_counter_template_rec.attribute5 := l_old_counter_template_rec.attribute5;
4351    ELSIF p_counter_template_rec.attribute5 = FND_API.G_MISS_CHAR THEN
4352       l_counter_template_rec.attribute5 := NULL;
4353    END IF;
4354 
4355    IF p_counter_template_rec.attribute6 IS NULL THEN
4356       l_counter_template_rec.attribute6 := l_old_counter_template_rec.attribute6;
4357    ELSIF p_counter_template_rec.attribute6 = FND_API.G_MISS_CHAR THEN
4358       l_counter_template_rec.attribute6 := NULL;
4359    END IF;
4360 
4361    IF p_counter_template_rec.attribute7 IS NULL THEN
4362       l_counter_template_rec.attribute7 := l_old_counter_template_rec.attribute7;
4363    ELSIF p_counter_template_rec.attribute7 = FND_API.G_MISS_CHAR THEN
4364       l_counter_template_rec.attribute7 := NULL;
4365    END IF;
4366 
4367    IF p_counter_template_rec.attribute8 IS NULL THEN
4368       l_counter_template_rec.attribute8 := l_old_counter_template_rec.attribute8;
4369    ELSIF p_counter_template_rec.attribute8 = FND_API.G_MISS_CHAR THEN
4370       l_counter_template_rec.attribute8 := NULL;
4371    END IF;
4372 
4373    IF p_counter_template_rec.attribute9 IS NULL THEN
4374       l_counter_template_rec.attribute9 := l_old_counter_template_rec.attribute9;
4375    ELSIF p_counter_template_rec.attribute9 = FND_API.G_MISS_CHAR THEN
4376       l_counter_template_rec.attribute9 := NULL;
4377    END IF;
4378 
4379    IF p_counter_template_rec.attribute10 IS NULL THEN
4380       l_counter_template_rec.attribute10 := l_old_counter_template_rec.attribute10;
4381    ELSIF p_counter_template_rec.attribute10 = FND_API.G_MISS_CHAR THEN
4382       l_counter_template_rec.attribute10 := NULL;
4383    END IF;
4384 
4385    IF p_counter_template_rec.attribute11 IS NULL THEN
4386       l_counter_template_rec.attribute11 := l_old_counter_template_rec.attribute11;
4387    ELSIF p_counter_template_rec.attribute11 = FND_API.G_MISS_CHAR THEN
4388       l_counter_template_rec.attribute11 := NULL;
4389    END IF;
4390 
4391    IF p_counter_template_rec.attribute12 IS NULL THEN
4392       l_counter_template_rec.attribute12 := l_old_counter_template_rec.attribute12;
4393    ELSIF p_counter_template_rec.attribute12 = FND_API.G_MISS_CHAR THEN
4394       l_counter_template_rec.attribute12 := NULL;
4395    END IF;
4396 
4397    IF p_counter_template_rec.attribute13 IS NULL THEN
4398       l_counter_template_rec.attribute13 := l_old_counter_template_rec.attribute13;
4399    ELSIF p_counter_template_rec.attribute13 = FND_API.G_MISS_CHAR THEN
4400       l_counter_template_rec.attribute13 := NULL;
4401    END IF;
4402 
4403    IF p_counter_template_rec.attribute14 IS NULL THEN
4404       l_counter_template_rec.attribute14 := l_old_counter_template_rec.attribute14;
4405    ELSIF p_counter_template_rec.attribute14 = FND_API.G_MISS_CHAR THEN
4406       l_counter_template_rec.attribute14 := NULL;
4407    END IF;
4408 
4409    IF p_counter_template_rec.attribute15 IS NULL THEN
4410       l_counter_template_rec.attribute15 := l_old_counter_template_rec.attribute15;
4411    ELSIF p_counter_template_rec.attribute15 = FND_API.G_MISS_CHAR THEN
4412       l_counter_template_rec.attribute15 := NULL;
4413    END IF;
4414 
4415    IF p_counter_template_rec.attribute16 IS NULL THEN
4416       l_counter_template_rec.attribute16 := l_old_counter_template_rec.attribute16;
4417    ELSIF p_counter_template_rec.attribute16 = FND_API.G_MISS_CHAR THEN
4418       l_counter_template_rec.attribute16 := NULL;
4419    END IF;
4420 
4421    IF p_counter_template_rec.attribute17 IS NULL THEN
4422       l_counter_template_rec.attribute17 := l_old_counter_template_rec.attribute17;
4423    ELSIF p_counter_template_rec.attribute17 = FND_API.G_MISS_CHAR THEN
4424       l_counter_template_rec.attribute17 := NULL;
4425    END IF;
4426 
4427    IF p_counter_template_rec.attribute18 IS NULL THEN
4428       l_counter_template_rec.attribute18 := l_old_counter_template_rec.attribute18;
4429    ELSIF p_counter_template_rec.attribute18 = FND_API.G_MISS_CHAR THEN
4430       l_counter_template_rec.attribute18 := NULL;
4431    END IF;
4432 
4433    IF p_counter_template_rec.attribute19 IS NULL THEN
4434       l_counter_template_rec.attribute19 := l_old_counter_template_rec.attribute19;
4435    ELSIF p_counter_template_rec.attribute19 = FND_API.G_MISS_CHAR THEN
4436       l_counter_template_rec.attribute19 := NULL;
4437    END IF;
4438 
4439    IF p_counter_template_rec.attribute20 IS NULL THEN
4440       l_counter_template_rec.attribute20 := l_old_counter_template_rec.attribute20;
4441    ELSIF p_counter_template_rec.attribute20 = FND_API.G_MISS_CHAR THEN
4442       l_counter_template_rec.attribute20 := NULL;
4443    END IF;
4444 
4445    IF p_counter_template_rec.attribute21 IS NULL THEN
4446       l_counter_template_rec.attribute21 := l_old_counter_template_rec.attribute21;
4447    ELSIF p_counter_template_rec.attribute21 = FND_API.G_MISS_CHAR THEN
4448       l_counter_template_rec.attribute21 := NULL;
4449    END IF;
4450 
4451    IF p_counter_template_rec.attribute22 IS NULL THEN
4452       l_counter_template_rec.attribute22 := l_old_counter_template_rec.attribute22;
4453    ELSIF p_counter_template_rec.attribute22 = FND_API.G_MISS_CHAR THEN
4454       l_counter_template_rec.attribute22 := NULL;
4455    END IF;
4456 
4457    IF p_counter_template_rec.attribute23 IS NULL THEN
4458       l_counter_template_rec.attribute23 := l_old_counter_template_rec.attribute23;
4459    ELSIF p_counter_template_rec.attribute23 = FND_API.G_MISS_CHAR THEN
4460       l_counter_template_rec.attribute23 := NULL;
4461    END IF;
4462 
4463    IF p_counter_template_rec.attribute24 IS NULL THEN
4464       l_counter_template_rec.attribute24 := l_old_counter_template_rec.attribute24;
4465    ELSIF p_counter_template_rec.attribute24 = FND_API.G_MISS_CHAR THEN
4466       l_counter_template_rec.attribute24 := NULL;
4467    END IF;
4468 
4469    IF p_counter_template_rec.attribute25 IS NULL THEN
4470       l_counter_template_rec.attribute25 := l_old_counter_template_rec.attribute25;
4471    ELSIF p_counter_template_rec.attribute25 = FND_API.G_MISS_CHAR THEN
4472       l_counter_template_rec.attribute25 := NULL;
4473    END IF;
4474 
4475    IF p_counter_template_rec.attribute26 IS NULL THEN
4476       l_counter_template_rec.attribute26 := l_old_counter_template_rec.attribute26;
4477    ELSIF p_counter_template_rec.attribute26 = FND_API.G_MISS_CHAR THEN
4478       l_counter_template_rec.attribute26 := NULL;
4479    END IF;
4480 
4481    IF p_counter_template_rec.attribute27 IS NULL THEN
4482       l_counter_template_rec.attribute27 := l_old_counter_template_rec.attribute27;
4483    ELSIF p_counter_template_rec.attribute27 = FND_API.G_MISS_CHAR THEN
4484       l_counter_template_rec.attribute27 := NULL;
4485    END IF;
4486 
4487    IF p_counter_template_rec.attribute28 IS NULL THEN
4488       l_counter_template_rec.attribute28 := l_old_counter_template_rec.attribute28;
4489    ELSIF p_counter_template_rec.attribute28 = FND_API.G_MISS_CHAR THEN
4490       l_counter_template_rec.attribute28 := NULL;
4491    END IF;
4492 
4493    IF p_counter_template_rec.attribute29 IS NULL THEN
4494       l_counter_template_rec.attribute29 := l_old_counter_template_rec.attribute29;
4495    ELSIF p_counter_template_rec.attribute29 = FND_API.G_MISS_CHAR THEN
4496       l_counter_template_rec.attribute29 := NULL;
4497    END IF;
4498 
4499    IF p_counter_template_rec.attribute30 IS NULL THEN
4500       l_counter_template_rec.attribute30 := l_old_counter_template_rec.attribute30;
4501    ELSIF p_counter_template_rec.attribute30 = FND_API.G_MISS_CHAR THEN
4502       l_counter_template_rec.attribute30 := NULL;
4503    END IF;
4504 
4505    IF p_counter_template_rec.attribute_category IS NULL THEN
4506       l_counter_template_rec.attribute_category := l_old_counter_template_rec.attribute_category;
4507    ELSIF p_counter_template_rec.attribute_category = FND_API.G_MISS_CHAR THEN
4508       l_counter_template_rec.attribute_category := NULL;
4509    END IF;
4510 
4511    IF p_counter_template_rec.customer_view IS NULL THEN
4512       l_counter_template_rec.customer_view := l_old_counter_template_rec.customer_view;
4513    ELSIF p_counter_template_rec.customer_view = FND_API.G_MISS_CHAR THEN
4514       l_counter_template_rec.customer_view := NULL;
4515    END IF;
4516 
4517    IF p_counter_template_rec.direction IS NULL THEN
4518       l_counter_template_rec.direction := l_old_counter_template_rec.direction;
4519    ELSIF p_counter_template_rec.direction = FND_API.G_MISS_CHAR THEN
4520       l_counter_template_rec.direction := NULL;
4521    END IF;
4522 
4523    IF p_counter_template_rec.filter_type IS NULL THEN
4524       l_counter_template_rec.filter_type := l_old_counter_template_rec.filter_type;
4525    ELSIF p_counter_template_rec.filter_type = FND_API.G_MISS_CHAR THEN
4526       l_counter_template_rec.filter_type := NULL;
4527    END IF;
4528 
4529    IF p_counter_template_rec.filter_reading_count IS NULL THEN
4530       l_counter_template_rec.filter_reading_count := l_old_counter_template_rec.filter_reading_count;
4531    ELSIF p_counter_template_rec.filter_reading_count = FND_API.G_MISS_NUM THEN
4532       l_counter_template_rec.filter_reading_count := NULL;
4533    END IF;
4534 
4535    IF p_counter_template_rec.filter_time_uom IS NULL THEN
4536       l_counter_template_rec.filter_time_uom := l_old_counter_template_rec.filter_time_uom;
4537    ELSIF p_counter_template_rec.filter_time_uom = FND_API.G_MISS_CHAR THEN
4538       l_counter_template_rec.filter_time_uom := NULL;
4539    END IF;
4540 
4541    IF p_counter_template_rec.estimation_id IS NULL THEN
4542       l_counter_template_rec.estimation_id := l_old_counter_template_rec.estimation_id;
4543    ELSIF p_counter_template_rec.estimation_id = FND_API.G_MISS_NUM THEN
4544       l_counter_template_rec.estimation_id := NULL;
4545    END IF;
4546 
4547    IF p_counter_template_rec.association_type IS NULL THEN
4548       l_counter_template_rec.association_type := l_old_counter_template_rec.association_type;
4549    ELSIF p_counter_template_rec.association_type = FND_API.G_MISS_CHAR THEN
4550       l_counter_template_rec.association_type := NULL;
4551    END IF;
4552 
4553    IF p_counter_template_rec.reading_type IS NULL THEN
4554       l_counter_template_rec.reading_type := l_old_counter_template_rec.reading_type;
4555    ELSIF p_counter_template_rec.reading_type = FND_API.G_MISS_NUM THEN
4556       l_counter_template_rec.reading_type := NULL;
4557    END IF;
4558 
4559    IF p_counter_template_rec.automatic_rollover IS NULL THEN
4560       l_counter_template_rec.automatic_rollover := l_old_counter_template_rec.automatic_rollover;
4561    ELSIF p_counter_template_rec.automatic_rollover = FND_API.G_MISS_CHAR THEN
4562       l_counter_template_rec.automatic_rollover := NULL;
4563    END IF;
4564 
4565    IF p_counter_template_rec.default_usage_rate IS NULL THEN
4566       l_counter_template_rec.default_usage_rate := l_old_counter_template_rec.default_usage_rate;
4567    ELSIF p_counter_template_rec.default_usage_rate = FND_API.G_MISS_NUM THEN
4568       l_counter_template_rec.default_usage_rate := NULL;
4569    END IF;
4570 
4571    IF p_counter_template_rec.use_past_reading IS NULL THEN
4572       l_counter_template_rec.use_past_reading := l_old_counter_template_rec.use_past_reading;
4573    ELSIF p_counter_template_rec.use_past_reading = FND_API.G_MISS_NUM THEN
4574       l_counter_template_rec.use_past_reading := NULL;
4575    END IF;
4576 
4577    IF p_counter_template_rec.used_in_scheduling IS NULL THEN
4578       l_counter_template_rec.used_in_scheduling := l_old_counter_template_rec.used_in_scheduling;
4579    ELSIF p_counter_template_rec.used_in_scheduling = FND_API.G_MISS_CHAR THEN
4580       l_counter_template_rec.used_in_scheduling := NULL;
4581    END IF;
4582 
4583    IF p_counter_template_rec.time_based_manual_entry IS NULL THEN
4584       l_counter_template_rec.time_based_manual_entry := l_old_counter_template_rec.time_based_manual_entry;
4585    ELSIF p_counter_template_rec.time_based_manual_entry = FND_API.G_MISS_CHAR THEN
4586       l_counter_template_rec.time_based_manual_entry := NULL;
4587    END IF;
4588 
4589    IF l_old_counter_template_rec.used_in_scheduling = 'Y' AND l_counter_template_rec.used_in_scheduling = 'N' THEN
4590       Eam_Meters_Util.Validate_Used_In_Scheduling
4591       (
4592         p_meter_id         =>   p_counter_template_rec.counter_id,
4593         X_return_status    =>   l_return_status,
4594         X_msg_count        =>   l_msg_count,
4595         X_msg_data         =>   l_msg_data
4596      );
4597 
4598      IF NOT (l_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
4599         csi_ctr_gen_utility_pvt.put_line('used in scheduling cannot be updated');
4600         csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_USEDINSCHED_NOT_UPDT');
4601      END IF;
4602    END IF;
4603 
4604    csi_ctr_gen_utility_pvt.put_line('p_counter_template_rec.defaulted_group_id - ' || p_counter_template_rec.defaulted_group_id);
4605    IF p_counter_template_rec.defaulted_group_id IS NULL THEN
4606       l_counter_template_rec.defaulted_group_id := l_old_counter_template_rec.defaulted_group_id;
4607    ELSIF p_counter_template_rec.defaulted_group_id = FND_API.G_MISS_NUM THEN
4608       l_counter_template_rec.defaulted_group_id := NULL;
4609    END IF;
4610 
4611    IF p_counter_template_rec.comments IS NULL THEN
4612       l_counter_template_rec.comments := l_old_counter_template_rec.comments;
4613    ELSIF p_counter_template_rec.comments = FND_API.G_MISS_CHAR THEN
4614       l_counter_template_rec.comments := NULL;
4615    END IF;
4616 
4617    IF p_counter_template_rec.eam_required_flag IS NULL THEN
4618       l_counter_template_rec.eam_required_flag := l_old_counter_template_rec.eam_required_flag;
4619    ELSIF p_counter_template_rec.eam_required_flag = FND_API.G_MISS_CHAR THEN
4620       l_counter_template_rec.eam_required_flag := NULL;
4621    END IF;
4622 
4623    -- Counter group is not updateable
4624    IF l_counter_template_rec.counter_id <> l_old_counter_template_rec.counter_id    THEN
4625       CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_GRP_NOT_UPDATABLE');
4626    END IF;
4627 
4628    IF l_counter_template_rec.counter_type <> l_old_counter_template_rec.counter_type THEN
4629       IF l_old_counter_template_rec.counter_type = 'FORMULA' AND
4630          l_old_counter_template_rec.derive_function is null
4631       THEN
4632          OPEN  formula_ref_cur(p_counter_template_rec.counter_id);
4633          FETCH formula_ref_cur INTO l_formula_ref_count;
4634          CLOSE formula_ref_cur;
4635        IF l_formula_ref_count is not null then
4636          -- Formula references exist for this counter. You cannot
4637          -- change the type to something different.
4638          csi_ctr_gen_utility_pvt.put_line('Formula References exist for this counter. Cannot change counter type...');
4639          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CS_API_CTR_FMLA_REF_EXIST','CTR_NAME',l_counter_template_rec.name);
4640        END IF;
4641      ELSIF l_old_counter_template_rec.counter_type = 'FORMULA'
4642              and l_old_counter_template_rec.derive_function in ('SUM','COUNT') THEN
4643        OPEN derived_filters_cur(p_counter_template_rec.counter_id);
4644        FETCH derived_filters_cur INTO l_der_filter_count;
4645        CLOSE derived_filters_cur;
4646        IF l_der_filter_count is not null then
4647          -- Derived filters exist for this counter. You cannot
4648          -- change the type to something different.
4649          csi_ctr_gen_utility_pvt.put_line('Derived Filters exist for this counter. Cannot change counter type...');
4650          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_DER_FILTER_EXIST','CTR_NAME',l_counter_template_rec.name);
4651        END IF;
4652      ELSIF l_old_counter_template_rec.counter_type = 'REGULAR' THEN
4653        OPEN target_counter_cur(p_counter_template_rec.counter_id);
4654        FETCH target_counter_cur INTO l_target_ctr_exist;
4655        CLOSE target_counter_cur;
4656        IF l_target_ctr_exist is not null then
4657          -- Target counters exist for this counter. You cannot
4658          -- change the type to something different.
4659          csi_ctr_gen_utility_pvt.put_line('Target Counters exist for this counter. Cannot change counter type...');
4660          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_CONFIG_CTR_EXIST','CTR_NAME',l_counter_template_rec.name);
4661        END IF;
4662      END IF;
4663    END IF;
4664 
4665    -- Validate reading type. Reading type cannot be changed if readings exist
4666    IF l_counter_template_rec.reading_type <> l_old_counter_template_rec.reading_type THEN
4667       OPEN counter_readings_cur(p_counter_template_rec.counter_id);
4668       FETCH counter_readings_cur INTO l_rdg_exists;
4669       CLOSE counter_readings_cur;
4670       IF l_rdg_exists is not null then
4671          -- Counter readings exist for this counter. You cannot
4672          -- change the reading type to something different.counter.
4673          csi_ctr_gen_utility_pvt.put_line('Counter readings exist for this counter. Cannot change reading type...');
4674          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_RDGS_EXIST','CTR_NAME',l_counter_template_rec.name);
4675       END IF;
4676    END IF;
4677 
4678    -- Start add code for bug 6418952
4679    IF (l_counter_template_rec.name IS NULL AND l_old_counter_template_rec.counter_id IS NOT NULL) then
4680     BEGIN
4681       SELECT name
4682       INTO l_counter_template_rec.name
4683       FROM csi_counter_template_tl
4684       WHERE counter_id = l_old_counter_template_rec.counter_id;
4685     EXCEPTION
4686       WHEN OTHERS THEN NULL;
4687     END;
4688    END IF;
4689    -- End add code for bug 6418952
4690 
4691    -- Validate Counter
4692    validate_counter(l_counter_template_rec.group_id, l_counter_template_rec.name,
4693                     l_counter_template_rec.counter_type, l_counter_template_rec.uom_code,
4694                     l_counter_template_rec.usage_item_id, l_counter_template_rec.reading_type,
4695                     l_counter_template_rec.direction, l_counter_template_rec.estimation_id,
4696                     l_counter_template_rec.derive_function, l_counter_template_rec.formula_text,
4697                     l_counter_template_rec.derive_counter_id,l_counter_template_rec.filter_type,
4698                     l_counter_template_rec.filter_reading_count, l_counter_template_rec.filter_time_uom,
4699                     l_counter_template_rec.automatic_rollover, l_counter_template_rec.rollover_last_reading,
4700                     l_counter_template_rec.rollover_first_reading, l_counter_template_rec.tolerance_plus,
4701                     l_counter_template_rec.tolerance_minus, l_counter_template_rec.used_in_scheduling,
4702                     l_counter_template_rec.initial_reading, l_counter_template_rec.default_usage_rate,
4703                     l_counter_template_rec.use_past_reading, l_old_counter_template_rec.counter_id,
4704                     l_counter_template_rec.start_date_active, l_counter_template_rec.end_date_active, 'Y'
4705                    );
4706 
4707    -- call table handler here
4708 
4709    CSI_COUNTER_TEMPLATE_PKG.update_row
4710    (
4711      p_counter_id                  => p_counter_template_rec.counter_id
4712     ,p_group_id                    => p_counter_template_rec.group_id
4713     ,p_counter_type                => p_counter_template_rec.counter_type
4714     ,p_initial_reading             => p_counter_template_rec.initial_reading
4715     ,p_initial_reading_date        => p_counter_template_rec.initial_reading_date
4716     ,p_tolerance_plus              => p_counter_template_rec.tolerance_plus
4717     ,p_tolerance_minus             => p_counter_template_rec.tolerance_minus
4718     ,p_uom_code                    => p_counter_template_rec.uom_code
4719     ,p_derive_counter_id           => p_counter_template_rec.derive_counter_id
4720     ,p_derive_function             => p_counter_template_rec.derive_function
4721     ,p_derive_property_id          => p_counter_template_rec.derive_property_id
4722     ,p_valid_flag                  => p_counter_template_rec.valid_flag
4723     ,p_formula_incomplete_flag     => p_counter_template_rec.formula_incomplete_flag
4724     ,p_formula_text                => p_counter_template_rec.formula_text
4725     ,p_rollover_last_reading       => p_counter_template_rec.rollover_last_reading
4726     ,p_rollover_first_reading      => p_counter_template_rec.rollover_first_reading
4727     ,p_usage_item_id               => p_counter_template_rec.usage_item_id
4728     ,p_ctr_val_max_seq_no          => nvl(p_counter_template_rec.ctr_val_max_seq_no,1)
4729     ,p_start_date_active           => p_counter_template_rec.start_date_active
4730     ,p_end_date_active             => p_counter_template_rec.end_date_active
4731     ,p_object_version_number       => p_counter_template_rec.object_version_number
4732     ,p_last_update_date            => sysdate
4733     ,p_last_updated_by             => FND_GLOBAL.USER_ID
4734     ,p_creation_date               => p_counter_template_rec.creation_date
4735     ,p_created_by                  => p_counter_template_rec.created_by
4736     ,p_last_update_login           => FND_GLOBAL.USER_ID
4737     ,p_attribute1                  => p_counter_template_rec.attribute1
4738     ,p_attribute2                  => p_counter_template_rec.attribute2
4739     ,p_attribute3                  => p_counter_template_rec.attribute3
4740     ,p_attribute4                  => p_counter_template_rec.attribute4
4741     ,p_attribute5                  => p_counter_template_rec.attribute5
4742     ,p_attribute6                  => p_counter_template_rec.attribute6
4743     ,p_attribute7                  => p_counter_template_rec.attribute7
4744     ,p_attribute8                  => p_counter_template_rec.attribute8
4745     ,p_attribute9                  => p_counter_template_rec.attribute9
4746     ,p_attribute10                 => p_counter_template_rec.attribute10
4747     ,p_attribute11                 => p_counter_template_rec.attribute11
4748     ,p_attribute12                 => p_counter_template_rec.attribute12
4749     ,p_attribute13                 => p_counter_template_rec.attribute13
4750     ,p_attribute14                 => p_counter_template_rec.attribute14
4751     ,p_attribute15                 => p_counter_template_rec.attribute15
4752     ,p_attribute16                 => p_counter_template_rec.attribute16
4753     ,p_attribute17                 => p_counter_template_rec.attribute17
4754     ,p_attribute18                 => p_counter_template_rec.attribute18
4755     ,p_attribute19                 => p_counter_template_rec.attribute19
4756     ,p_attribute20                 => p_counter_template_rec.attribute20
4757     ,p_attribute21                 => p_counter_template_rec.attribute21
4758     ,p_attribute22                 => p_counter_template_rec.attribute22
4759     ,p_attribute23                 => p_counter_template_rec.attribute23
4760     ,p_attribute24                 => p_counter_template_rec.attribute24
4761     ,p_attribute25                 => p_counter_template_rec.attribute25
4762     ,p_attribute26                 => p_counter_template_rec.attribute26
4763     ,p_attribute27                 => p_counter_template_rec.attribute27
4764     ,p_attribute28                 => p_counter_template_rec.attribute28
4765     ,p_attribute29                 => p_counter_template_rec.attribute29
4766     ,p_attribute30                 => p_counter_template_rec.attribute30
4767     ,p_attribute_category          => p_counter_template_rec.attribute_category
4768     ,p_migrated_flag               => null
4769     ,p_customer_view               => p_counter_template_rec.customer_view
4770     ,p_direction                   => p_counter_template_rec.direction
4771     ,p_filter_type                 => p_counter_template_rec.filter_type
4772     ,p_filter_reading_count        => p_counter_template_rec.filter_reading_count
4773     ,p_filter_time_uom             => p_counter_template_rec.filter_time_uom
4774     ,p_estimation_id               => p_counter_template_rec.estimation_id
4775     ,p_association_type            => p_counter_template_rec.association_type
4776     ,p_reading_type                => p_counter_template_rec.reading_type
4777     ,p_automatic_rollover          => p_counter_template_rec.automatic_rollover
4778     ,p_default_usage_rate          => p_counter_template_rec.default_usage_rate
4779     ,p_use_past_reading            => p_counter_template_rec.use_past_reading
4780     ,p_used_in_scheduling          => p_counter_template_rec.used_in_scheduling
4781     ,p_defaulted_group_id          => p_counter_template_rec.defaulted_group_id
4782     ,p_SECURITY_GROUP_ID           => p_counter_template_rec.step_value
4783     ,p_STEP_VALUE                  => p_counter_template_rec.step_value
4784     ,p_name                        => p_counter_template_rec.name
4785     ,p_description                 => p_counter_template_rec.description
4786     ,p_time_based_manual_entry     => p_counter_template_rec.time_based_manual_entry
4787     ,p_eam_required_flag       => p_counter_template_rec.eam_required_flag
4788    );
4789 
4790 
4791    IF NOT (x_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
4792         ROLLBACK TO update_counter_template_pvt;
4793         RETURN;
4794    END IF;
4795 
4796    /* End of API Body */
4797 
4798    IF FND_API.to_Boolean(nvl(p_commit,FND_API.G_FALSE)) THEN
4799       COMMIT WORK;
4800    END IF;
4801 
4802    -- Standard call to get message count and IF count is  get message info.
4803    FND_MSG_PUB.Count_And_Get
4804       ( p_count  =>  x_msg_count,
4805         p_data   =>  x_msg_data
4806       );
4807 EXCEPTION
4808    WHEN FND_API.G_EXC_ERROR THEN
4809       x_return_status := FND_API.G_RET_STS_ERROR ;
4810       ROLLBACK TO update_counter_template_pvt;
4811       FND_MSG_PUB.Count_And_Get
4812          ( p_count => x_msg_count,
4813            p_data  => x_msg_data
4814          );
4815    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4816       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4817       ROLLBACK TO update_counter_template_pvt;
4818       FND_MSG_PUB.Count_And_Get
4819          ( p_count => x_msg_count,
4820            p_data  => x_msg_data
4821          );
4822    WHEN OTHERS THEN
4823       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4824       ROLLBACK TO update_counter_template_pvt;
4825       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4826          FND_MSG_PUB.Add_Exc_Msg
4827             ( G_PKG_NAME,
4828               l_api_name
4829             );
4830       END IF;
4831       FND_MSG_PUB.Count_And_Get
4832          ( p_count  => x_msg_count,
4833            p_data   => x_msg_data
4834          );
4835 END update_counter_template;
4836 
4837 --|---------------------------------------------------
4838 --| procedure name: update_ctr_property_template
4839 --| description :   procedure used to
4840 --|                 create counter properties
4841 --|---------------------------------------------------
4842 
4843 PROCEDURE update_ctr_property_template
4844  (
4845      p_api_version               IN     NUMBER
4846     ,p_commit                    IN     VARCHAR2
4847     ,p_init_msg_list             IN     VARCHAR2
4848     ,p_validation_level          IN     NUMBER
4849     ,p_ctr_property_template_rec IN OUT NOCOPY CSI_CTR_DATASTRUCTURES_PUB.ctr_property_template_rec
4850     ,x_return_status                OUT    NOCOPY VARCHAR2
4851     ,x_msg_count                    OUT    NOCOPY NUMBER
4852     ,x_msg_data                     OUT    NOCOPY VARCHAR2
4853  ) IS
4854 
4855    CURSOR cur_ctr_prop_rec(p_counter_property_id IN NUMBER) IS
4856    SELECT name
4857           ,description
4858           ,counter_id
4859           ,property_data_type
4860           ,is_nullable
4861           ,default_value
4862           ,minimum_value
4863           ,maximum_value
4864           ,uom_code
4865           ,start_date_active
4866           ,end_date_active
4867           ,object_version_number
4868           ,last_update_date
4869           ,last_updated_by
4870           ,creation_date
4871           ,created_by
4872           ,last_update_login
4873           ,attribute1
4874           ,attribute2
4875           ,attribute3
4876           ,attribute4
4877           ,attribute5
4878           ,attribute6
4879           ,attribute7
4880           ,attribute8
4881           ,attribute9
4882           ,attribute10
4883           ,attribute11
4884           ,attribute12
4885           ,attribute13
4886           ,attribute14
4887           ,attribute15
4888           ,attribute_category
4889           ,migrated_flag
4890           ,property_lov_type
4891           ,security_group_id
4892    FROM  csi_ctr_prop_template_vl
4893    WHERE counter_property_id = p_counter_property_id
4894    FOR UPDATE OF OBJECT_VERSION_NUMBER;
4895    l_old_ctr_property_tmpl_rec  cur_ctr_prop_rec%ROWTYPE;
4896 
4897 
4898    l_api_name                      CONSTANT VARCHAR2(30)   := 'UPDATE_CTR_PROPERTY_TEMPLATE';
4899    l_api_version                   CONSTANT NUMBER         := 1.0;
4900    l_msg_data                      VARCHAR2(2000);
4901    l_msg_index                     NUMBER;
4902    l_msg_count                     NUMBER;
4903    -- l_debug_level                   NUMBER;
4904 
4905    l_ctr_property_tmpl_rec         CSI_CTR_DATASTRUCTURES_PUB.ctr_property_template_rec;
4906 BEGIN
4907    -- Standard Start of API savepoint
4908    SAVEPOINT  update_ctr_property_tmpl_pvt;
4909 
4910    -- Standard call to check for call compatibility.
4911    IF NOT FND_API.Compatible_API_Call (l_api_version,
4912                                        p_api_version,
4913                                        l_api_name   ,
4914                                        G_PKG_NAME   ) THEN
4915       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4916    END IF;
4917 
4918    -- Initialize message list if p_init_msg_list is set to TRUE.
4919    IF FND_API.to_Boolean(nvl(p_init_msg_list,FND_API.G_FALSE)) THEN
4920       FND_MSG_PUB.initialize;
4921    END IF;
4922 
4923    --  Initialize API return status to success
4924    x_return_status := FND_API.G_RET_STS_SUCCESS;
4925 
4926    -- Read the debug profiles values in to global variable 7197402
4927    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
4928 
4929    --
4930    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
4931       csi_ctr_gen_utility_pvt.put_line( 'update_ctr_property_tmpl_pvt'         ||'-'||
4932                                      p_api_version                              ||'-'||
4933                                      nvl(p_commit,FND_API.G_FALSE)              ||'-'||
4934                                      nvl(p_init_msg_list,FND_API.G_FALSE)       ||'-'||
4935                                      nvl(p_validation_level,FND_API.G_VALID_LEVEL_FULL) );
4936    END IF;
4937 
4938    /* Start of API Body */
4939    OPEN cur_ctr_prop_rec(p_ctr_property_template_rec.counter_property_id);
4940    FETCH cur_ctr_prop_rec INTO l_old_ctr_property_tmpl_rec;
4941    IF  (l_old_ctr_property_tmpl_rec.object_version_number <> nvl(p_ctr_property_template_rec.OBJECT_VERSION_NUMBER,0)) THEN
4942       FND_MESSAGE.Set_Name('CSI', 'CSI_API_OBJ_VER_MISMATCH');
4943       FND_MSG_PUB.ADD;
4944       RAISE FND_API.G_EXC_ERROR;
4945    END IF;
4946 
4947    CLOSE cur_ctr_prop_rec;
4948 
4949    -- IF SQL%NOTFOUND THEN
4950    --     CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_GRP_NOTEXISTS');
4951    -- END IF;
4952 
4953    l_ctr_property_tmpl_rec := p_ctr_property_template_rec;
4954 
4955    IF p_ctr_property_template_rec.name  IS NULL THEN
4956       l_ctr_property_tmpl_rec.name := l_old_ctr_property_tmpl_rec.name;
4957    ELSIF p_ctr_property_template_rec.name = FND_API.G_MISS_CHAR THEN
4958       l_ctr_property_tmpl_rec.name := NULL;
4959    END IF;
4960 
4961    IF p_ctr_property_template_rec.description  IS NULL THEN
4962       l_ctr_property_tmpl_rec.description := l_old_ctr_property_tmpl_rec.description;
4963    ELSIF p_ctr_property_template_rec.description = FND_API.G_MISS_CHAR THEN
4964       l_ctr_property_tmpl_rec.description := NULL;
4965    END IF;
4966 
4967    IF p_ctr_property_template_rec.counter_id  IS NULL THEN
4968       l_ctr_property_tmpl_rec.counter_id := l_old_ctr_property_tmpl_rec.counter_id;
4969    ELSIF p_ctr_property_template_rec.counter_id = FND_API.G_MISS_NUM THEN
4970       l_ctr_property_tmpl_rec.counter_id := NULL;
4971    END IF;
4972 
4973    IF p_ctr_property_template_rec.start_date_active IS NULL THEN
4974       l_ctr_property_tmpl_rec.start_date_active := l_old_ctr_property_tmpl_rec.start_date_active;
4975    ELSIF p_ctr_property_template_rec.start_date_active = FND_API.G_MISS_DATE THEN
4976       l_ctr_property_tmpl_rec.start_date_active := NULL;
4977    END IF;
4978 
4979    IF p_ctr_property_template_rec.end_date_active IS NULL THEN
4980       l_ctr_property_tmpl_rec.end_date_active := l_old_ctr_property_tmpl_rec.end_date_active;
4981    ELSIF p_ctr_property_template_rec.end_date_active = FND_API.G_MISS_DATE THEN
4982       l_ctr_property_tmpl_rec.end_date_active := NULL;
4983    END IF;
4984 
4985    IF p_ctr_property_template_rec.attribute1 IS NULL THEN
4986       l_ctr_property_tmpl_rec.attribute1 := l_old_ctr_property_tmpl_rec.attribute1;
4987    ELSIF p_ctr_property_template_rec.attribute1 = FND_API.G_MISS_CHAR THEN
4988       l_ctr_property_tmpl_rec.attribute1 := NULL;
4989    END IF;
4990 
4991    IF p_ctr_property_template_rec.attribute2 IS NULL THEN
4992       l_ctr_property_tmpl_rec.attribute2 := l_old_ctr_property_tmpl_rec.attribute2;
4993    ELSIF p_ctr_property_template_rec.attribute2 = FND_API.G_MISS_CHAR THEN
4994       l_ctr_property_tmpl_rec.attribute2 := NULL;
4995    END IF;
4996 
4997    IF p_ctr_property_template_rec.attribute3 IS NULL THEN
4998       l_ctr_property_tmpl_rec.attribute3 := l_old_ctr_property_tmpl_rec.attribute3;
4999    ELSIF p_ctr_property_template_rec.attribute3 = FND_API.G_MISS_CHAR THEN
5000       l_ctr_property_tmpl_rec.attribute3 := NULL;
5001    END IF;
5002 
5003    IF p_ctr_property_template_rec.attribute4 IS NULL THEN
5004       l_ctr_property_tmpl_rec.attribute4 := l_old_ctr_property_tmpl_rec.attribute4;
5005    ELSIF p_ctr_property_template_rec.attribute4 = FND_API.G_MISS_CHAR THEN
5006       l_ctr_property_tmpl_rec.attribute4 := NULL;
5007    END IF;
5008 
5009    IF p_ctr_property_template_rec.attribute5 IS NULL THEN
5010       l_ctr_property_tmpl_rec.attribute5 := l_old_ctr_property_tmpl_rec.attribute5;
5011    ELSIF p_ctr_property_template_rec.attribute5 = FND_API.G_MISS_CHAR THEN
5012       l_ctr_property_tmpl_rec.attribute5 := NULL;
5013    END IF;
5014 
5015    IF p_ctr_property_template_rec.attribute6 IS NULL THEN
5016       l_ctr_property_tmpl_rec.attribute6 := l_old_ctr_property_tmpl_rec.attribute6;
5017    ELSIF p_ctr_property_template_rec.attribute6 = FND_API.G_MISS_CHAR THEN
5018       l_ctr_property_tmpl_rec.attribute6 := NULL;
5019    END IF;
5020 
5021    IF p_ctr_property_template_rec.attribute7 IS NULL THEN
5022       l_ctr_property_tmpl_rec.attribute7 := l_old_ctr_property_tmpl_rec.attribute7;
5023    ELSIF p_ctr_property_template_rec.attribute7 = FND_API.G_MISS_CHAR THEN
5024       l_ctr_property_tmpl_rec.attribute7 := NULL;
5025    END IF;
5026 
5027    IF p_ctr_property_template_rec.attribute8 IS NULL THEN
5028       l_ctr_property_tmpl_rec.attribute8 := l_old_ctr_property_tmpl_rec.attribute8;
5029    ELSIF p_ctr_property_template_rec.attribute8 = FND_API.G_MISS_CHAR THEN
5030       l_ctr_property_tmpl_rec.attribute8 := NULL;
5031    END IF;
5032 
5033    IF p_ctr_property_template_rec.attribute9 IS NULL THEN
5034       l_ctr_property_tmpl_rec.attribute9 := l_old_ctr_property_tmpl_rec.attribute9;
5035    ELSIF p_ctr_property_template_rec.attribute9 = FND_API.G_MISS_CHAR THEN
5036       l_ctr_property_tmpl_rec.attribute9 := NULL;
5037    END IF;
5038 
5039    IF p_ctr_property_template_rec.attribute10 IS NULL THEN
5040       l_ctr_property_tmpl_rec.attribute10 := l_old_ctr_property_tmpl_rec.attribute10;
5041    ELSIF p_ctr_property_template_rec.attribute10 = FND_API.G_MISS_CHAR THEN
5042       l_ctr_property_tmpl_rec.attribute10 := NULL;
5043    END IF;
5044 
5045    IF p_ctr_property_template_rec.attribute11 IS NULL THEN
5046       l_ctr_property_tmpl_rec.attribute11 := l_old_ctr_property_tmpl_rec.attribute11;
5047    ELSIF p_ctr_property_template_rec.attribute11 = FND_API.G_MISS_CHAR THEN
5048       l_ctr_property_tmpl_rec.attribute11 := NULL;
5049    END IF;
5050 
5051    IF p_ctr_property_template_rec.attribute12 IS NULL THEN
5052       l_ctr_property_tmpl_rec.attribute12 := l_old_ctr_property_tmpl_rec.attribute12;
5053    ELSIF p_ctr_property_template_rec.attribute12 = FND_API.G_MISS_CHAR THEN
5054       l_ctr_property_tmpl_rec.attribute12 := NULL;
5055    END IF;
5056 
5057    IF p_ctr_property_template_rec.attribute13 IS NULL THEN
5058       l_ctr_property_tmpl_rec.attribute13 := l_old_ctr_property_tmpl_rec.attribute13;
5059    ELSIF p_ctr_property_template_rec.attribute13 = FND_API.G_MISS_CHAR THEN
5060       l_ctr_property_tmpl_rec.attribute13 := NULL;
5061    END IF;
5062 
5063    IF p_ctr_property_template_rec.attribute14 IS NULL THEN
5064       l_ctr_property_tmpl_rec.attribute14 := l_old_ctr_property_tmpl_rec.attribute14;
5065    ELSIF p_ctr_property_template_rec.attribute14 = FND_API.G_MISS_CHAR THEN
5066       l_ctr_property_tmpl_rec.attribute14 := NULL;
5067    END IF;
5068 
5069    IF p_ctr_property_template_rec.attribute15 IS NULL THEN
5070       l_ctr_property_tmpl_rec.attribute15 := l_old_ctr_property_tmpl_rec.attribute15;
5071    ELSIF p_ctr_property_template_rec.attribute15 = FND_API.G_MISS_CHAR THEN
5072       l_ctr_property_tmpl_rec.attribute15 := NULL;
5073    END IF;
5074 
5075    IF p_ctr_property_template_rec.attribute_category IS NULL THEN
5076       l_ctr_property_tmpl_rec.attribute_category := l_old_ctr_property_tmpl_rec.attribute_category;
5077    ELSIF p_ctr_property_template_rec.attribute_category = FND_API.G_MISS_CHAR THEN
5078       l_ctr_property_tmpl_rec.attribute_category := NULL;
5079    END IF;
5080 
5081    IF p_ctr_property_template_rec.property_data_type IS NULL THEN
5082       l_ctr_property_tmpl_rec.property_data_type := l_old_ctr_property_tmpl_rec.property_data_type;
5083    ELSIF p_ctr_property_template_rec.property_data_type = FND_API.G_MISS_CHAR THEN
5084       l_ctr_property_tmpl_rec.property_data_type := NULL;
5085    END IF;
5086 
5087    IF p_ctr_property_template_rec.is_nullable IS NULL THEN
5088       l_ctr_property_tmpl_rec.is_nullable := l_old_ctr_property_tmpl_rec.is_nullable;
5089    ELSIF p_ctr_property_template_rec.is_nullable = FND_API.G_MISS_CHAR THEN
5090       l_ctr_property_tmpl_rec.is_nullable := NULL;
5091    END IF;
5092 
5093    IF p_ctr_property_template_rec.default_value IS NULL THEN
5094       l_ctr_property_tmpl_rec.default_value := l_old_ctr_property_tmpl_rec.default_value;
5095    ELSIF p_ctr_property_template_rec.default_value = FND_API.G_MISS_CHAR THEN
5096       l_ctr_property_tmpl_rec.default_value := NULL;
5097    END IF;
5098 
5099    IF p_ctr_property_template_rec.minimum_value IS NULL THEN
5100       l_ctr_property_tmpl_rec.minimum_value := l_old_ctr_property_tmpl_rec.minimum_value;
5101    ELSIF p_ctr_property_template_rec.minimum_value = FND_API.G_MISS_CHAR THEN
5102       l_ctr_property_tmpl_rec.minimum_value := NULL;
5103    END IF;
5104 
5105    IF p_ctr_property_template_rec.maximum_value IS NULL THEN
5106       l_ctr_property_tmpl_rec.maximum_value := l_old_ctr_property_tmpl_rec.maximum_value;
5107    ELSIF p_ctr_property_template_rec.maximum_value = FND_API.G_MISS_CHAR THEN
5108       l_ctr_property_tmpl_rec.maximum_value:= NULL;
5109    END IF;
5110 
5111    IF p_ctr_property_template_rec.uom_code IS NULL THEN
5112       l_ctr_property_tmpl_rec.uom_code := l_old_ctr_property_tmpl_rec.uom_code;
5113    ELSIF p_ctr_property_template_rec.uom_code = FND_API.G_MISS_CHAR THEN
5114       l_ctr_property_tmpl_rec.uom_code:= NULL;
5115    END IF;
5116 
5117    IF p_ctr_property_template_rec.property_lov_type IS NULL THEN
5118       l_ctr_property_tmpl_rec.property_lov_type := l_old_ctr_property_tmpl_rec.property_lov_type;
5119    ELSIF p_ctr_property_template_rec.property_lov_type = FND_API.G_MISS_CHAR THEN
5120       l_ctr_property_tmpl_rec.property_lov_type:= NULL;
5121    END IF;
5122 
5123    IF p_ctr_property_template_rec.migrated_flag IS NULL THEN
5124       l_ctr_property_tmpl_rec.migrated_flag := l_old_ctr_property_tmpl_rec.migrated_flag;
5125    ELSIF p_ctr_property_template_rec.migrated_flag = FND_API.G_MISS_CHAR THEN
5126       l_ctr_property_tmpl_rec.migrated_flag:= NULL;
5127    END IF;
5128 
5129    IF p_ctr_property_template_rec.security_group_id IS NULL THEN
5130       l_ctr_property_tmpl_rec.security_group_id := l_old_ctr_property_tmpl_rec.security_group_id;
5131    ELSIF p_ctr_property_template_rec.security_group_id = FND_API.G_MISS_NUM THEN
5132       l_ctr_property_tmpl_rec.security_group_id := NULL;
5133    END IF;
5134 
5135    -- Counter property name is not updateable
5136 
5137    IF l_ctr_property_tmpl_rec.name <> l_old_ctr_property_tmpl_rec.name THEN
5138        CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_PROP_NOT_UPDATABLE');
5139    END IF;
5140 
5141    -- Call the table handler
5142    validate_start_date(l_ctr_property_tmpl_rec.start_date_active);
5143    Validate_Data_Type(l_ctr_property_tmpl_rec.property_data_type, l_ctr_property_tmpl_rec.default_value, l_ctr_property_tmpl_rec.minimum_value, l_ctr_property_tmpl_rec.maximum_value);
5144 
5145    IF l_ctr_property_tmpl_rec.property_lov_type IS NOT NULL THEN
5146       validate_lookups('CSI_CTR_PROPERTY_LOV_TYPE',l_ctr_property_tmpl_rec.property_lov_type);
5147    END IF;
5148 
5149  csi_ctr_gen_utility_pvt.put_line(' Default Value = '||l_ctr_property_tmpl_rec.default_value);
5150    IF l_ctr_property_tmpl_rec.property_lov_type IS NOT NULL and l_ctr_property_tmpl_rec.default_value IS NOT NULL THEN
5151       validate_lookups(l_ctr_property_tmpl_rec.property_lov_type,l_ctr_property_tmpl_rec.default_value);
5152    END IF;
5153 
5154    IF l_ctr_property_tmpl_rec.uom_code IS NOT NULL THEN
5155       validate_uom(l_ctr_property_tmpl_rec.uom_code);
5156    END IF;
5157 
5158    /* Call the table Handler */
5159    CSI_CTR_PROPERTY_TEMPLATE_PKG.update_Row(
5160  	 p_COUNTER_PROPERTY_ID          => p_ctr_property_template_rec.counter_property_id
5161 	,p_COUNTER_ID                   => p_ctr_property_template_rec.counter_id
5162 	,p_PROPERTY_DATA_TYPE           => p_ctr_property_template_rec.property_data_type
5163 	,p_IS_NULLABLE                  => p_ctr_property_template_rec.is_nullable
5164 	,p_DEFAULT_VALUE                => p_ctr_property_template_rec.default_value
5165 	,p_MINIMUM_VALUE                => p_ctr_property_template_rec.minimum_value
5166 	,p_MAXIMUM_VALUE                => p_ctr_property_template_rec.maximum_value
5167 	,p_UOM_CODE                     => p_ctr_property_template_rec.uom_code
5168 	,p_START_DATE_ACTIVE            => p_ctr_property_template_rec.start_date_active
5169 	,p_END_DATE_ACTIVE              => p_ctr_property_template_rec.end_date_active
5170 	,p_OBJECT_VERSION_NUMBER        => p_ctr_property_template_rec.object_version_number + 1
5171         ,p_LAST_UPDATE_DATE             => sysdate
5172         ,p_LAST_UPDATED_BY              => FND_GLOBAL.USER_ID
5173         ,p_CREATION_DATE                => p_ctr_property_template_rec.creation_date
5174         ,p_CREATED_BY                   => p_ctr_property_template_rec.created_by
5175         ,p_LAST_UPDATE_LOGIN            => FND_GLOBAL.USER_ID
5176         ,p_ATTRIBUTE1                   => p_ctr_property_template_rec.attribute1
5177         ,p_ATTRIBUTE2                   => p_ctr_property_template_rec.attribute2
5178         ,p_ATTRIBUTE3                   => p_ctr_property_template_rec.attribute3
5179         ,p_ATTRIBUTE4                   => p_ctr_property_template_rec.attribute4
5180         ,p_ATTRIBUTE5                   => p_ctr_property_template_rec.attribute5
5181         ,p_ATTRIBUTE6                   => p_ctr_property_template_rec.attribute6
5182         ,p_ATTRIBUTE7                   => p_ctr_property_template_rec.attribute7
5183         ,p_ATTRIBUTE8                   => p_ctr_property_template_rec.attribute8
5184         ,p_ATTRIBUTE9                   => p_ctr_property_template_rec.attribute9
5185         ,p_ATTRIBUTE10                  => p_ctr_property_template_rec.attribute10
5186         ,p_ATTRIBUTE11                  => p_ctr_property_template_rec.attribute11
5187         ,p_ATTRIBUTE12                  => p_ctr_property_template_rec.attribute12
5188         ,p_ATTRIBUTE13                  => p_ctr_property_template_rec.attribute13
5189         ,p_ATTRIBUTE14                  => p_ctr_property_template_rec.attribute14
5190         ,p_ATTRIBUTE15                  => p_ctr_property_template_rec.attribute15
5191         ,p_ATTRIBUTE_CATEGORY           => p_ctr_property_template_rec.attribute_category
5192 	,p_MIGRATED_FLAG                => p_ctr_property_template_rec.migrated_flag
5193 	,p_PROPERTY_LOV_TYPE            => p_ctr_property_template_rec.property_lov_type
5194         ,p_SECURITY_GROUP_ID            => p_ctr_property_template_rec.security_group_id
5195         ,p_NAME	                        => p_ctr_property_template_rec.name
5196         ,p_DESCRIPTION                  => p_ctr_property_template_rec.description
5197         );
5198 
5199    IF NOT (x_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
5200         ROLLBACK TO update_ctr_property_tmpl_pvt;
5201         RETURN;
5202    END IF;
5203 
5204 
5205    /* End of API Body */
5206 
5207    IF FND_API.to_Boolean(nvl(p_commit,FND_API.G_FALSE)) THEN
5208       COMMIT WORK;
5209    END IF;
5210 
5211    -- Standard call to get message count and IF count is  get message info.
5212    FND_MSG_PUB.Count_And_Get
5213       ( p_count  =>  x_msg_count,
5214         p_data   =>  x_msg_data
5215       );
5216 EXCEPTION
5217    WHEN FND_API.G_EXC_ERROR THEN
5218       x_return_status := FND_API.G_RET_STS_ERROR ;
5219       ROLLBACK TO update_ctr_property_tmpl_pvt;
5220       FND_MSG_PUB.Count_And_Get
5221          ( p_count => x_msg_count,
5222            p_data  => x_msg_data
5223          );
5224    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5225       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5226       ROLLBACK TO update_ctr_property_tmpl_pvt;
5227       FND_MSG_PUB.Count_And_Get
5228          ( p_count => x_msg_count,
5229            p_data  => x_msg_data
5230          );
5231    WHEN OTHERS THEN
5232       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5233       ROLLBACK TO update_ctr_property_tmpl_pvt;
5234       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5235          FND_MSG_PUB.Add_Exc_Msg
5236             ( G_PKG_NAME,
5237               l_api_name
5238             );
5239       END IF;
5240       FND_MSG_PUB.Count_And_Get
5241          ( p_count  => x_msg_count,
5242            p_data   => x_msg_data
5243          );
5244 END update_ctr_property_template;
5245 
5246 --|---------------------------------------------------
5247 --| procedure name: update_counter_relationship
5248 --| description :   procedure used to
5249 --|                 update counter relationship
5250 --|---------------------------------------------------
5251 
5252 PROCEDURE update_counter_relationship
5253  (
5254      p_api_version               IN     NUMBER
5255     ,p_commit                    IN     VARCHAR2
5256     ,p_init_msg_list             IN     VARCHAR2
5257     ,p_validation_level          IN     NUMBER
5258     ,p_counter_relationships_rec IN OUT NOCOPY CSI_CTR_DATASTRUCTURES_PUB.counter_relationships_rec
5259     ,x_return_status                OUT    NOCOPY VARCHAR2
5260     ,x_msg_count                    OUT    NOCOPY NUMBER
5261     ,x_msg_data                     OUT    NOCOPY VARCHAR2
5262  ) IS
5263 
5264   CURSOR cur_rel_rec(p_relationship_id IN  NUMBER) IS
5265   SELECT ctr_association_id
5266           ,relationship_type_code
5267           ,source_counter_id
5268           ,object_counter_id
5269           ,active_start_date
5270           ,active_end_date
5271           ,object_version_number
5272           ,last_update_date
5273           ,last_updated_by
5274           ,creation_date
5275           ,created_by
5276           ,last_update_login
5277           ,attribute_category
5278           ,attribute1
5279           ,attribute2
5280           ,attribute3
5281           ,attribute4
5282           ,attribute5
5283           ,attribute6
5284           ,attribute7
5285           ,attribute8
5286           ,attribute9
5287           ,attribute10
5288           ,attribute11
5289           ,attribute12
5290           ,attribute13
5291           ,attribute14
5292           ,attribute15
5293           ,security_group_id
5294           ,migrated_flag
5295           ,bind_variable_name
5296           ,factor
5297    FROM  csi_ctr_relationships_v
5298    WHERE relationship_id = p_relationship_id
5299    FOR UPDATE OF OBJECT_VERSION_NUMBER;
5300    l_old_ctr_relationships_rec  cur_rel_rec%ROWTYPE;
5301 
5302 
5303    l_api_name                      CONSTANT VARCHAR2(30)   := 'UPDATE_COUNTER_RELATIONSHIP';
5304    l_api_version                   CONSTANT NUMBER         := 1.0;
5305    l_msg_data                      VARCHAR2(2000);
5306    l_msg_index                     NUMBER;
5307    l_msg_count                     NUMBER;
5308    -- l_debug_level                   NUMBER;
5309 
5310    l_ctr_relationships_rec         CSI_CTR_DATASTRUCTURES_PUB.counter_relationships_rec;
5311    l_source_direction              VARCHAR2(1);
5312    l_object_direction              VARCHAR2(1);
5313    l_valid_flag                    VARCHAR2(1);
5314    l_src_ctr_start_date            DATE;
5315    l_src_ctr_end_date              DATE;
5316    l_obj_ctr_start_date            DATE;
5317    l_obj_ctr_end_date              DATE;
5318    l_reading_date                  DATE;
5319 
5320    CURSOR c1(p_counter_id IN NUMBER) IS
5321    SELECT max(value_timestamp)
5322    FROM   csi_counter_readings
5323    WHERE  counter_id = p_counter_id;
5324 
5325 BEGIN
5326    -- Standard Start of API savepoint
5327    SAVEPOINT  update_ctr_relationship_pvt;
5328 
5329    -- Standard call to check for call compatibility.
5330    IF NOT FND_API.Compatible_API_Call (l_api_version,
5331                                        p_api_version,
5332                                        l_api_name   ,
5333                                        G_PKG_NAME   ) THEN
5334       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5335    END IF;
5336 
5337    -- Initialize message list if p_init_msg_list is set to TRUE.
5338    IF FND_API.to_Boolean(nvl(p_init_msg_list,FND_API.G_FALSE)) THEN
5339       FND_MSG_PUB.initialize;
5340    END IF;
5341 
5342    --  Initialize API return status to success
5343    x_return_status := FND_API.G_RET_STS_SUCCESS;
5344 
5345    -- Read the debug profiles values in to global variable 7197402
5346    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
5347 
5348    --
5349    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
5350       csi_ctr_gen_utility_pvt.put_line( 'update_ctr_relationship_pvt'           ||'-'||
5351                                      p_api_version                              ||'-'||
5352                                      nvl(p_commit,FND_API.G_FALSE)              ||'-'||
5353                                      nvl(p_init_msg_list,FND_API.G_FALSE)       ||'-'||
5354                                      nvl(p_validation_level,FND_API.G_VALID_LEVEL_FULL) );
5355    END IF;
5356 
5357    /* Start of API Body */
5358    OPEN cur_rel_rec(p_counter_relationships_rec.relationship_id);
5359    FETCH cur_rel_rec INTO l_old_ctr_relationships_rec;
5360 
5361    IF p_counter_relationships_rec.object_version_number  IS NULL THEN
5362       l_ctr_relationships_rec.object_version_number := l_old_ctr_relationships_rec.object_version_number;
5363    ELSIF p_counter_relationships_rec.object_version_number = FND_API.G_MISS_NUM THEN
5364       l_ctr_relationships_rec.object_version_number := NULL;
5365    END IF;
5366 
5367    IF  (l_old_ctr_relationships_rec.object_version_number <> nvl(p_counter_relationships_rec.OBJECT_VERSION_NUMBER,0)) THEN
5368       FND_MESSAGE.Set_Name('CSI', 'CSI_API_OBJ_VER_MISMATCH');
5369       FND_MSG_PUB.ADD;
5370       RAISE FND_API.G_EXC_ERROR;
5371    END IF;
5372 
5373    CLOSE cur_rel_rec;
5374 
5375    -- IF SQL%NOTFOUND THEN
5376    --    CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_GRP_NOTEXISTS');
5377    -- END IF;
5378 
5379    l_ctr_relationships_rec := p_counter_relationships_rec;
5380 
5381    IF p_counter_relationships_rec.ctr_association_id  IS NULL THEN
5382       l_ctr_relationships_rec.ctr_association_id := l_old_ctr_relationships_rec.ctr_association_id;
5383    ELSIF p_counter_relationships_rec.ctr_association_id = FND_API.G_MISS_NUM THEN
5384       l_ctr_relationships_rec.ctr_association_id := NULL;
5385    END IF;
5386 
5387    IF p_counter_relationships_rec.relationship_type_code IS NULL THEN
5388       l_ctr_relationships_rec.relationship_type_code := l_old_ctr_relationships_rec.relationship_type_code;
5389    ELSIF p_counter_relationships_rec.relationship_type_code = FND_API.G_MISS_CHAR THEN
5390       l_ctr_relationships_rec.relationship_type_code := NULL;
5391    END IF;
5392 
5393    IF p_counter_relationships_rec.source_counter_id  IS NULL THEN
5394       l_ctr_relationships_rec.source_counter_id := l_old_ctr_relationships_rec.source_counter_id;
5395    ELSIF p_counter_relationships_rec.source_counter_id = FND_API.G_MISS_NUM THEN
5396       l_ctr_relationships_rec.source_counter_id := NULL;
5397    END IF;
5398 
5399    IF p_counter_relationships_rec.object_counter_id  IS NULL THEN
5400       l_ctr_relationships_rec.object_counter_id := l_old_ctr_relationships_rec.object_counter_id;
5401    ELSIF p_counter_relationships_rec.object_counter_id = FND_API.G_MISS_NUM THEN
5402       l_ctr_relationships_rec.object_counter_id := NULL;
5403    END IF;
5404 
5405    IF p_counter_relationships_rec.active_start_date IS NULL THEN
5406       l_ctr_relationships_rec.active_start_date := l_old_ctr_relationships_rec.active_start_date;
5407    ELSIF p_counter_relationships_rec.active_start_date = FND_API.G_MISS_DATE THEN
5408       l_ctr_relationships_rec.active_start_date := NULL;
5409    END IF;
5410 
5411    IF p_counter_relationships_rec.active_end_date IS NULL THEN
5412       l_ctr_relationships_rec.active_end_date := l_old_ctr_relationships_rec.active_end_date;
5413    ELSIF p_counter_relationships_rec.active_end_date = FND_API.G_MISS_DATE THEN
5414       l_ctr_relationships_rec.active_end_date := NULL;
5415    END IF;
5416 
5417    IF p_counter_relationships_rec.attribute1 IS NULL THEN
5418       l_ctr_relationships_rec.attribute1 := l_old_ctr_relationships_rec.attribute1;
5419    ELSIF p_counter_relationships_rec.attribute1 = FND_API.G_MISS_CHAR THEN
5420       l_ctr_relationships_rec.attribute1 := NULL;
5421    END IF;
5422 
5423    IF p_counter_relationships_rec.attribute2 IS NULL THEN
5424       l_ctr_relationships_rec.attribute2 := l_old_ctr_relationships_rec.attribute2;
5425    ELSIF p_counter_relationships_rec.attribute2 = FND_API.G_MISS_CHAR THEN
5426       l_ctr_relationships_rec.attribute2 := NULL;
5427    END IF;
5428 
5429    IF p_counter_relationships_rec.attribute3 IS NULL THEN
5430       l_ctr_relationships_rec.attribute3 := l_old_ctr_relationships_rec.attribute3;
5431    ELSIF p_counter_relationships_rec.attribute3 = FND_API.G_MISS_CHAR THEN
5432       l_ctr_relationships_rec.attribute3 := NULL;
5433    END IF;
5434 
5435    IF p_counter_relationships_rec.attribute4 IS NULL THEN
5436       l_ctr_relationships_rec.attribute4 := l_old_ctr_relationships_rec.attribute4;
5437    ELSIF p_counter_relationships_rec.attribute4 = FND_API.G_MISS_CHAR THEN
5438       l_ctr_relationships_rec.attribute4 := NULL;
5439    END IF;
5440 
5441    IF p_counter_relationships_rec.attribute5 IS NULL THEN
5442       l_ctr_relationships_rec.attribute5 := l_old_ctr_relationships_rec.attribute5;
5443    ELSIF p_counter_relationships_rec.attribute5 = FND_API.G_MISS_CHAR THEN
5444       l_ctr_relationships_rec.attribute5 := NULL;
5445    END IF;
5446 
5447    IF p_counter_relationships_rec.attribute6 IS NULL THEN
5448       l_ctr_relationships_rec.attribute6 := l_old_ctr_relationships_rec.attribute6;
5449    ELSIF p_counter_relationships_rec.attribute6 = FND_API.G_MISS_CHAR THEN
5450       l_ctr_relationships_rec.attribute6 := NULL;
5451    END IF;
5452 
5453    IF p_counter_relationships_rec.attribute7 IS NULL THEN
5454       l_ctr_relationships_rec.attribute7 := l_old_ctr_relationships_rec.attribute7;
5455    ELSIF p_counter_relationships_rec.attribute7 = FND_API.G_MISS_CHAR THEN
5456       l_ctr_relationships_rec.attribute7 := NULL;
5457    END IF;
5458 
5459    IF p_counter_relationships_rec.attribute8 IS NULL THEN
5460       l_ctr_relationships_rec.attribute8 := l_old_ctr_relationships_rec.attribute8;
5461    ELSIF p_counter_relationships_rec.attribute8 = FND_API.G_MISS_CHAR THEN
5462       l_ctr_relationships_rec.attribute8 := NULL;
5463    END IF;
5464 
5465    IF p_counter_relationships_rec.attribute9 IS NULL THEN
5466       l_ctr_relationships_rec.attribute9 := l_old_ctr_relationships_rec.attribute9;
5467    ELSIF p_counter_relationships_rec.attribute9 = FND_API.G_MISS_CHAR THEN
5468       l_ctr_relationships_rec.attribute9 := NULL;
5469    END IF;
5470 
5471    IF p_counter_relationships_rec.attribute10 IS NULL THEN
5472       l_ctr_relationships_rec.attribute10 := l_old_ctr_relationships_rec.attribute10;
5473    ELSIF p_counter_relationships_rec.attribute10 = FND_API.G_MISS_CHAR THEN
5474       l_ctr_relationships_rec.attribute10 := NULL;
5475    END IF;
5476 
5477    IF p_counter_relationships_rec.attribute11 IS NULL THEN
5478       l_ctr_relationships_rec.attribute11 := l_old_ctr_relationships_rec.attribute11;
5479    ELSIF p_counter_relationships_rec.attribute11 = FND_API.G_MISS_CHAR THEN
5480       l_ctr_relationships_rec.attribute11 := NULL;
5481    END IF;
5482 
5483    IF p_counter_relationships_rec.attribute12 IS NULL THEN
5484       l_ctr_relationships_rec.attribute12 := l_old_ctr_relationships_rec.attribute12;
5485    ELSIF p_counter_relationships_rec.attribute12 = FND_API.G_MISS_CHAR THEN
5486       l_ctr_relationships_rec.attribute12 := NULL;
5487    END IF;
5488 
5489    IF p_counter_relationships_rec.attribute13 IS NULL THEN
5490       l_ctr_relationships_rec.attribute13 := l_old_ctr_relationships_rec.attribute13;
5491    ELSIF p_counter_relationships_rec.attribute13 = FND_API.G_MISS_CHAR THEN
5492       l_ctr_relationships_rec.attribute13 := NULL;
5493    END IF;
5494 
5495    IF p_counter_relationships_rec.attribute14 IS NULL THEN
5496       l_ctr_relationships_rec.attribute14 := l_old_ctr_relationships_rec.attribute14;
5497    ELSIF p_counter_relationships_rec.attribute14 = FND_API.G_MISS_CHAR THEN
5498       l_ctr_relationships_rec.attribute14 := NULL;
5499    END IF;
5500 
5501    IF p_counter_relationships_rec.attribute15 IS NULL THEN
5502       l_ctr_relationships_rec.attribute15 := l_old_ctr_relationships_rec.attribute15;
5503    ELSIF p_counter_relationships_rec.attribute15 = FND_API.G_MISS_CHAR THEN
5504       l_ctr_relationships_rec.attribute15 := NULL;
5505    END IF;
5506 
5507    IF p_counter_relationships_rec.attribute_category IS NULL THEN
5508       l_ctr_relationships_rec.attribute_category := l_old_ctr_relationships_rec.attribute_category;
5509    ELSIF p_counter_relationships_rec.attribute_category = FND_API.G_MISS_CHAR THEN
5510       l_ctr_relationships_rec.attribute_category := NULL;
5511    END IF;
5512 
5513    IF p_counter_relationships_rec.bind_variable_name IS NULL THEN
5514       l_ctr_relationships_rec.bind_variable_name := l_old_ctr_relationships_rec.bind_variable_name;
5515    ELSIF p_counter_relationships_rec.bind_variable_name = FND_API.G_MISS_CHAR THEN
5516       l_ctr_relationships_rec.bind_variable_name := NULL;
5517    END IF;
5518 
5519    IF p_counter_relationships_rec.migrated_flag IS NULL THEN
5520       l_ctr_relationships_rec.migrated_flag := l_old_ctr_relationships_rec.migrated_flag;
5521    ELSIF p_counter_relationships_rec.migrated_flag = FND_API.G_MISS_CHAR THEN
5522       l_ctr_relationships_rec.migrated_flag := NULL;
5523    END IF;
5524 
5525    IF p_counter_relationships_rec.factor IS NULL THEN
5526       l_ctr_relationships_rec.factor := l_old_ctr_relationships_rec.factor;
5527    ELSIF p_counter_relationships_rec.factor = FND_API.G_MISS_NUM THEN
5528       l_ctr_relationships_rec.factor := NULL;
5529    END IF;
5530 
5531    IF p_counter_relationships_rec.security_group_id IS NULL THEN
5532       l_ctr_relationships_rec.security_group_id := l_old_ctr_relationships_rec.security_group_id;
5533    ELSIF p_counter_relationships_rec.security_group_id = FND_API.G_MISS_NUM THEN
5534       l_ctr_relationships_rec.security_group_id := NULL;
5535    END IF;
5536 
5537 
5538    validate_start_date(l_ctr_relationships_rec.active_start_date);
5539    validate_lookups('CSI_CTR_RELATIONSHIP_TYPE_CODE', l_ctr_relationships_rec.relationship_type_code);
5540 
5541    csi_ctr_gen_utility_pvt.put_line(' type code = '||l_ctr_relationships_rec.relationship_type_code);
5542    IF l_ctr_relationships_rec.relationship_type_code = 'CONFIGURATION' THEN
5543       validate_ctr_relationship(l_ctr_relationships_rec.source_counter_id, l_source_direction,
5544                                 l_src_ctr_start_date, l_src_ctr_end_date);
5545       validate_ctr_relationship(l_ctr_relationships_rec.object_counter_id, l_object_direction,
5546                                 l_obj_ctr_start_date, l_obj_ctr_end_date);
5547       /* Validate direction */
5548       IF l_source_direction = 'B' and l_object_direction <> 'B'  THEN
5549          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_REL_DIR');
5550       END IF;
5551 
5552       IF l_object_direction = 'B' and l_source_direction <> 'B' THEN
5553          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_REL_DIR');
5554       END IF;
5555 
5556       IF p_counter_relationships_rec.active_start_date < l_src_ctr_start_date THEN
5557          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_SOURCE_ST_DATE');
5558       ELSIF p_counter_relationships_rec.active_start_date < l_obj_ctr_start_date THEN
5559          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_SOURCE_ST_DATE');
5560       END IF;
5561 
5562       IF l_src_ctr_end_date IS NOT NULL THEN
5563          IF p_counter_relationships_rec.active_start_date > l_src_ctr_end_date THEN
5564             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_SOURCE_ST_DATE');
5565          END IF;
5566       END IF;
5567 
5568       IF l_obj_ctr_end_date IS NOT NULL THEN
5569          IF p_counter_relationships_rec.active_start_date > l_obj_ctr_end_date THEN
5570             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_SOURCE_ST_DATE');
5571          END IF;
5572       END IF;
5573 
5574       OPEN c1(l_ctr_relationships_rec.source_counter_id);
5575       FETCH c1 into l_reading_date;
5576       IF l_reading_date IS NOT  NULL THEN
5577          IF p_counter_relationships_rec.active_start_date < l_reading_date THEN
5578             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_SOURCE_ST_DATE');
5579          END IF;
5580       END IF;
5581       CLOSE c1;
5582 
5583       OPEN c1(l_ctr_relationships_rec.object_counter_id);
5584       FETCH c1 into l_reading_date;
5585       IF l_reading_date IS NOT  NULL THEN
5586          IF p_counter_relationships_rec.active_start_date < l_reading_date THEN
5587             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_INV_SOURCE_ST_DATE');
5588          END IF;
5589       END IF;
5590       CLOSE c1;
5591 
5592       /* A source counter cannot be a target counter */
5593       BEGIN
5594          SELECT 'N'
5595          INTO   l_valid_flag
5596          FROM   csi_counter_relationships
5597          WHERE  relationship_type_code = 'CONFIGURATION'
5598          AND    source_counter_id = l_ctr_relationships_rec.object_counter_id;
5599 
5600          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_TARGET_CTR_EXIST');
5601 
5602       EXCEPTION
5603          WHEN NO_DATA_FOUND THEN NULL;
5604          WHEN TOO_MANY_ROWS THEN
5605             CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_TARGET_CTR_EXIST');
5606 
5607       END;
5608    ELSIF l_ctr_relationships_rec.relationship_type_code = 'FORMULA' THEN
5609       IF p_counter_relationships_rec.source_counter_id IS NULL THEN
5610          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_REQ_FORMULA_REF');
5611       END IF;
5612 
5613    END IF;
5614 
5615    /* Call the table Handler */
5616    CSI_COUNTER_RELATIONSHIP_PKG.update_Row(
5617 	p_RELATIONSHIP_ID              => p_counter_relationships_rec.relationship_id
5618   	,p_CTR_ASSOCIATION_ID           => p_counter_relationships_rec.ctr_association_id
5619   	,p_RELATIONSHIP_TYPE_CODE       => p_counter_relationships_rec.relationship_type_code
5620   	,p_SOURCE_COUNTER_ID            => p_counter_relationships_rec.source_counter_id
5621   	,p_OBJECT_COUNTER_ID            => p_counter_relationships_rec.object_counter_id
5622   	,p_ACTIVE_START_DATE            => p_counter_relationships_rec.active_start_date
5623   	,p_ACTIVE_END_DATE              => p_counter_relationships_rec.active_end_date
5624   	,p_OBJECT_VERSION_NUMBER        => p_counter_relationships_rec.object_version_number + 1
5625         ,p_LAST_UPDATE_DATE             => sysdate
5626         ,p_LAST_UPDATED_BY              => FND_GLOBAL.USER_ID
5627         ,p_CREATION_DATE                => p_counter_relationships_rec.creation_date
5628         ,p_CREATED_BY                   => p_counter_relationships_rec.created_by
5629         ,p_LAST_UPDATE_LOGIN            => FND_GLOBAL.USER_ID
5630   	,p_ATTRIBUTE_CATEGORY           => p_counter_relationships_rec.attribute_category
5631         ,p_ATTRIBUTE1                   => p_counter_relationships_rec.attribute1
5632         ,p_ATTRIBUTE2                   => p_counter_relationships_rec.attribute2
5633         ,p_ATTRIBUTE3                   => p_counter_relationships_rec.attribute3
5634         ,p_ATTRIBUTE4                   => p_counter_relationships_rec.attribute4
5635         ,p_ATTRIBUTE5                   => p_counter_relationships_rec.attribute5
5636         ,p_ATTRIBUTE6                   => p_counter_relationships_rec.attribute6
5637         ,p_ATTRIBUTE7                   => p_counter_relationships_rec.attribute7
5638         ,p_ATTRIBUTE8                   => p_counter_relationships_rec.attribute8
5639         ,p_ATTRIBUTE9                   => p_counter_relationships_rec.attribute9
5640         ,p_ATTRIBUTE10                  => p_counter_relationships_rec.attribute10
5641         ,p_ATTRIBUTE11                  => p_counter_relationships_rec.attribute11
5642         ,p_ATTRIBUTE12                  => p_counter_relationships_rec.attribute12
5643         ,p_ATTRIBUTE13                  => p_counter_relationships_rec.attribute13
5644         ,p_ATTRIBUTE14                  => p_counter_relationships_rec.attribute14
5645         ,p_ATTRIBUTE15                  => p_counter_relationships_rec.attribute15
5646         ,p_SECURITY_GROUP_ID            => p_counter_relationships_rec.security_group_id
5647 	,p_MIGRATED_FLAG                => p_counter_relationships_rec.migrated_flag
5648   	,p_BIND_VARIABLE_NAME           => p_counter_relationships_rec.bind_variable_name
5649   	,p_FACTOR                       => p_counter_relationships_rec.factor);
5650 
5651    IF NOT (x_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
5652         ROLLBACK TO update_ctr_relationship_pvt;
5653         RETURN;
5654    END IF;
5655 
5656 
5657    /* End of API Body */
5658 
5659    IF FND_API.to_Boolean(nvl(p_commit,FND_API.G_FALSE)) THEN
5660       COMMIT WORK;
5661    END IF;
5662 
5663    -- Standard call to get message count and IF count is  get message info.
5664    FND_MSG_PUB.Count_And_Get
5665       ( p_count  =>  x_msg_count,
5666         p_data   =>  x_msg_data
5667       );
5668 EXCEPTION
5669    WHEN FND_API.G_EXC_ERROR THEN
5670       x_return_status := FND_API.G_RET_STS_ERROR ;
5671       ROLLBACK TO update_ctr_relationship_pvt;
5672       FND_MSG_PUB.Count_And_Get
5673          ( p_count => x_msg_count,
5674            p_data  => x_msg_data
5675          );
5676    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5677       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5678       ROLLBACK TO update_ctr_relationship_pvt;
5679       FND_MSG_PUB.Count_And_Get
5680          ( p_count => x_msg_count,
5681            p_data  => x_msg_data
5682          );
5683    WHEN OTHERS THEN
5684       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5685       ROLLBACK TO update_ctr_relationship_pvt;
5686       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5687          FND_MSG_PUB.Add_Exc_Msg
5688             ( G_PKG_NAME,
5689               l_api_name
5690             );
5691       END IF;
5692       FND_MSG_PUB.Count_And_Get
5693          ( p_count  => x_msg_count,
5694            p_data   => x_msg_data
5695          );
5696 END update_counter_relationship;
5697 
5698 
5699 --|---------------------------------------------------
5700 --| procedure name: update_derived_filters
5701 --| description :   procedure used to
5702 --|                 update derived filters
5703 --|---------------------------------------------------
5704 
5705 PROCEDURE update_derived_filters
5706 (
5707    p_api_version	       IN     NUMBER
5708    ,p_commit                   IN     VARCHAR2
5709    ,p_init_msg_list            IN     VARCHAR2
5710    ,p_validation_level         IN     NUMBER
5711    ,p_ctr_derived_filters_tbl  IN OUT NOCOPY CSI_CTR_DATASTRUCTURES_PUB.ctr_derived_filters_tbl
5712    ,x_return_status            OUT    NOCOPY VARCHAR2
5713    ,x_msg_count                OUT    NOCOPY NUMBER
5714    ,x_msg_data                 OUT    NOCOPY VARCHAR2
5715 ) IS
5716 
5717    l_api_name			   CONSTANT VARCHAR2(30)   := 'UPDATE_DERIVED_FILTERS';
5718    l_api_version                   CONSTANT NUMBER         := 1.0;
5719    l_dummy			   VARCHAR2(1);
5720    l_found			   VARCHAR2(1);
5721    -- l_debug_level                   NUMBER;
5722    --l_flag                        VARCHAR2(1)             := 'N';
5723    l_msg_data                      VARCHAR2(2000);
5724    l_msg_index                     NUMBER;
5725    l_msg_count                     NUMBER;
5726 
5727    l_count                         NUMBER;
5728    l_return_message                VARCHAR2(100);
5729    l_ctr_derived_filters_rec	   CSI_CTR_DATASTRUCTURES_PUB.ctr_derived_filters_rec;
5730    l_old_ctr_derived_filters_rec   CSI_CTR_DATASTRUCTURES_PUB.ctr_derived_filters_rec;
5731    l_counter_id			   NUMBER;
5732    l_type			   VARCHAR2(30);
5733    l_name			   VARCHAR2(50);
5734    l_log_op			   VARCHAR2(30);
5735    l_desc_flex                     CSI_CTR_DATASTRUCTURES_PUB.dff_rec_type;
5736 
5737    l_return_status                 VARCHAR2(1);
5738    l_valid_flag                    VARCHAR2(1);
5739 
5740 BEGIN
5741    -- Standard Start of API savepoint
5742    SAVEPOINT  update_derived_filters;
5743 
5744    -- Check for freeze_flag in csi_install_parameters is set to 'Y'
5745 
5746    -- csi_ctr_gen_utility_pvt.check_ib_active;
5747 
5748    -- Standard call to check for call compatibility.
5749    IF NOT FND_API.Compatible_API_Call (l_api_version,
5750                                        p_api_version,
5751                                        l_api_name   ,
5752                                        G_PKG_NAME   )
5753    THEN
5754       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5755    END IF;
5756 
5757    -- Initialize message list if p_init_msg_list is set to TRUE.
5758    IF FND_API.to_Boolean( p_init_msg_list ) THEN
5759       FND_MSG_PUB.initialize;
5760    END IF;
5761 
5762    --  Initialize API return status to success
5763    x_return_status := FND_API.G_RET_STS_SUCCESS;
5764 
5765    -- Read the debug profiles values in to global variable 7197402
5766    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
5767 
5768    -- Check the profile option debug_level for debug message reporting
5769    -- l_debug_level:=fnd_profile.value('CSI_COUNTER_DEBUG_LEVEL');
5770 
5771    -- If debug_level = 1 then dump the procedure name
5772    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
5773       csi_ctr_gen_utility_pvt.put_line( 'update_derived_filters');
5774    END IF;
5775 
5776    -- If the debug level = 2 then dump all the parameters values.
5777    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 1) THEN
5778       csi_ctr_gen_utility_pvt.put_line( 'update_derived_filters'     ||
5779 					p_api_version           ||'-'||
5780 					p_commit                ||'-'||
5781 					p_init_msg_list         ||'-'||
5782 					p_validation_level );
5783       csi_ctr_gen_utility_pvt.dump_ctr_derived_filters_tbl(p_ctr_derived_filters_tbl);
5784    END IF;
5785 
5786    IF (p_ctr_derived_filters_tbl.count > 0) THEN
5787       FOR tab_row IN p_ctr_derived_filters_tbl.FIRST .. p_ctr_derived_filters_tbl.LAST
5788       LOOP
5789          IF p_ctr_derived_filters_tbl.EXISTS(tab_row) THEN
5790             IF ((p_ctr_derived_filters_tbl(tab_row).counter_derived_filter_id IS NULL)
5791                OR
5792                (p_ctr_derived_filters_tbl(tab_row).counter_derived_filter_id = FND_API.G_MISS_NUM))
5793             THEN
5794 		   create_derived_filters
5795 	           (p_api_version      => p_api_version
5796 	            ,p_commit           => fnd_api.g_false
5797 	            ,p_init_msg_list    => p_init_msg_list
5798 	            ,p_validation_level => p_validation_level
5799                   ,p_ctr_derived_filters_tbl => p_ctr_derived_filters_tbl
5800 	            ,x_return_status    => x_return_status
5801 	            ,x_msg_count        => x_msg_count
5802 	            ,x_msg_data         => x_msg_data
5803 	           );
5804 
5805  	          IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
5806 	            l_msg_index := 1;
5807 	            l_msg_count := x_msg_count;
5808 	            WHILE l_msg_count > 0 LOOP
5809 	               x_msg_data := FND_MSG_PUB.GET
5810 	                             (l_msg_index,
5811 	                              FND_API.G_FALSE);
5812 	               csi_ctr_gen_utility_pvt.put_line('Error from CSI_COUNTER_TEMPLATE_PVT.CREATE_DERIVED_FILTERS');
5813 	               csi_ctr_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
5814 	               l_msg_index := l_msg_index + 1;
5815 	               l_msg_count := l_msg_count - 1;
5816 	            END LOOP;
5817 	            RAISE FND_API.G_EXC_ERROR;
5818 	         END IF;
5819 
5820             ELSE
5821                SELECT	counter_id,
5822 		        counter_property_id,
5823                		attribute1,
5824              		attribute2,
5825             		attribute3,
5826  		        attribute4,
5827 		        attribute5,
5828 		        attribute6,
5829 		        attribute7,
5830 		        attribute8,
5831 		        attribute9,
5832             		attribute10,
5833 		        attribute11,
5834              		attribute12,
5835 		        attribute13,
5836 		        attribute14,
5837 		        attribute15,
5838 		        attribute_category
5839                  INTO	l_old_ctr_derived_filters_rec.counter_id,
5840           		l_old_ctr_derived_filters_rec.counter_property_id,
5841 			l_old_ctr_derived_filters_rec.attribute1,
5842 			l_old_ctr_derived_filters_rec.attribute2,
5843 			l_old_ctr_derived_filters_rec.attribute3,
5844 			l_old_ctr_derived_filters_rec.attribute4,
5845 			l_old_ctr_derived_filters_rec.attribute5,
5846 			l_old_ctr_derived_filters_rec.attribute6,
5847 			l_old_ctr_derived_filters_rec.attribute7,
5848 			l_old_ctr_derived_filters_rec.attribute8,
5849 			l_old_ctr_derived_filters_rec.attribute9,
5850 			l_old_ctr_derived_filters_rec.attribute10,
5851 			l_old_ctr_derived_filters_rec.attribute11,
5852 			l_old_ctr_derived_filters_rec.attribute12,
5853 			l_old_ctr_derived_filters_rec.attribute13,
5854 			l_old_ctr_derived_filters_rec.attribute14,
5855 			l_old_ctr_derived_filters_rec.attribute15,
5856 			l_old_ctr_derived_filters_rec.attribute_category
5857 		   FROM CSI_COUNTER_DERIVED_FILTERS
5858 		   -- WHERE   COUNTER_DERIVED_FILTER_ID = l_ctr_derived_filters_rec.COUNTER_DERIVED_FILTER_ID;
5859 		   WHERE  COUNTER_DERIVED_FILTER_ID = p_ctr_derived_filters_tbl(tab_row).counter_derived_filter_id;
5860 
5861 		   IF SQL%NOTFOUND THEN
5862 		      csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_DERIVED_FILTER_INVALID');
5863 		   END IF;
5864 
5865 		   IF NVL(l_old_ctr_derived_filters_rec.counter_id, -1) = FND_API.G_MISS_NUM THEN
5866 		      l_counter_id := NULL;
5867 		   ELSE
5868 		      l_counter_id := l_old_ctr_derived_filters_rec.counter_id;
5869 		   END IF;
5870 
5871 		   IF NVL(l_counter_id, -1) <> l_old_ctr_derived_filters_rec.counter_id THEN
5872 		      BEGIN
5873 		         -- validate all counters
5874 		         SELECT	NVL(type, 'REGULAR'), name
5875 		         INTO l_type, l_name
5876 		         FROM	csi_counters_bc_v
5877 		         WHERE	counter_id = p_ctr_derived_filters_tbl(tab_row).counter_id;
5878 		      EXCEPTION WHEN NO_DATA_FOUND THEN
5879 		         csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_INVALID');
5880 		      END;
5881 
5882 		      -- validate all counter types are GROUP type
5883 		      IF l_type <> 'FORMULA' THEN
5884 		         csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_INV_GROUP_CTR','CTR_NAME',l_name);
5885 		      END IF;
5886 		   END IF;
5887 
5888 		   -- validate all counter properties
5889 		   IF NVL(p_ctr_derived_filters_tbl(tab_row).counter_property_id, -1) <> FND_API.G_MISS_NUM AND
5890 		      NVL(p_ctr_derived_filters_tbl(tab_row).counter_property_id, -1) <> l_old_ctr_derived_filters_rec.counter_property_id THEN
5891 		      BEGIN
5892 		         SELECT	'x'
5893 	                 INTO   l_dummy
5894 		         FROM	csi_ctr_properties_bc_v
5895 		         WHERE	counter_property_id = l_ctr_derived_filters_rec.counter_property_id;
5896 		      EXCEPTION WHEN NO_DATA_FOUND THEN
5897 		         csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_PROP_INVALID');
5898 		      END;
5899 		   END IF;
5900 
5901 		   -- validate LEFT_PARENT
5902 		   IF p_ctr_derived_filters_tbl(tab_row).LEFT_PARENT NOT IN (FND_API.G_MISS_CHAR,'(', '((', '(((', '((((', '(((((') THEN
5903 		      csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_INV_DER_LEFT_PARENT',
5904 							     'PARM',l_ctr_derived_filters_rec.LEFT_PARENT);
5905 		   END IF;
5906 
5907 		   -- validate RIGHT_PARENT
5908 		   IF p_ctr_derived_filters_tbl(tab_row).RIGHT_PARENT NOT IN (FND_API.G_MISS_CHAR,')', '))', ')))', '))))', ')))))') THEN
5909 		      csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_INV_DER_RIGHT_PARENT',
5910 		   					     'PARM',l_ctr_derived_filters_rec.RIGHT_PARENT);
5911 		   END IF;
5912 
5913 		   -- validate RELATIONAL_OPERATOR
5914 		   IF p_ctr_derived_filters_tbl(tab_row).RELATIONAL_OPERATOR NOT IN (FND_API.G_MISS_CHAR,'=', '<', '<=', '>', '>=', '!=', '<>') THEN
5915 		      csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_INV_DER_REL_OPERATOR',
5916 							     'PARM',l_ctr_derived_filters_rec.RELATIONAL_OPERATOR);
5917 		   END IF;
5918 
5919 		   -- validate LOGICAL_OPERATOR
5920 		   -- l_log_op := upper(l_ctr_derived_filters_rec.LOGICAL_OPERATOR);
5921                    IF p_ctr_derived_filters_tbl(tab_row).LOGICAL_OPERATOR = FND_API.G_MISS_CHAR then
5922                       l_log_op := null;
5923                    ELSE
5924                       l_log_op := upper(p_ctr_derived_filters_tbl(tab_row).LOGICAL_OPERATOR);
5925                    END IF;
5926 
5927 		   IF l_log_op IS NOT NULL THEN
5928                       IF l_log_op NOT IN (FND_API.G_MISS_CHAR, 'AND', 'OR') THEN
5929 		         csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_INV_DER_LOG_OPERATOR', 'PARM',l_ctr_derived_filters_rec.LOGICAL_OPERATOR);
5930 		      END IF;
5931 		   END IF;
5932 
5933 		   -- initialize descritive flexfield
5934 		   -- csi_ctr_gen_utility_pvt.Initialize_Desc_Flex_For_Upd(p_ctr_derived_filters_rec,l_old_ctr_derived_filters_rec);
5935 
5936 		   -- validate SEQ_NO
5937                    IF p_ctr_derived_filters_tbl(tab_row).seq_no = FND_API.G_MISS_NUM then
5938                       p_ctr_derived_filters_tbl(tab_row).seq_no := null;
5939                    END IF;
5940 
5941 		   IF p_ctr_derived_filters_tbl(tab_row).SEQ_NO IS NULL THEN
5942 		      csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_DER_FILTER_NULL_SEQ');
5943 		   ELSE
5944 		      IF p_ctr_derived_filters_tbl(tab_row).SEQ_NO <> FND_API.G_MISS_NUM THEN
5945 		         BEGIN
5946 		            SELECT 'x'
5947 		            INTO   l_dummy
5948 		            FROM   CSI_COUNTER_DERIVED_FILTERS
5949 		            WHERE  counter_id = p_ctr_derived_filters_tbl(tab_row).counter_id
5950 		            AND    seq_no = p_ctr_derived_filters_tbl(tab_row).seq_no
5951 		            AND    counter_derived_filter_id <> p_ctr_derived_filters_tbl(tab_row).counter_derived_filter_id;
5952 
5953 			    -- this means that for this counter, there is one another
5954 			    -- derived filter record with the same sequence number.
5955 			    -- Raise error.
5956 			    csi_ctr_gen_utility_pvt.ExitWithErrMsg('CSI_API_CTR_DUP_DERFILTER_SEQNO',
5957 						   'SEQNO',to_char(p_ctr_derived_filters_tbl(tab_row).seq_no),
5958 						   'CTR_NAME',l_name);
5959 
5960 		         EXCEPTION WHEN NO_DATA_FOUND THEN
5961 		            -- good. you can proceed.
5962 			         NULL;
5963 		         END;
5964 		      END IF;
5965 		   END IF;
5966 
5967 		   -- call table handler here
5968 		   CSI_CTR_DERIVED_FILTERS_PKG.Update_Row
5969 		   (
5970 	      	     p_ctr_derived_filters_tbl(tab_row).COUNTER_DERIVED_FILTER_ID
5971 		,p_ctr_derived_filters_tbl(tab_row).COUNTER_ID
5972 		,p_ctr_derived_filters_tbl(tab_row).SEQ_NO
5973 		,p_ctr_derived_filters_tbl(tab_row).LEFT_PARENT
5974 		,p_ctr_derived_filters_tbl(tab_row).COUNTER_PROPERTY_ID
5975 		,p_ctr_derived_filters_tbl(tab_row).RELATIONAL_OPERATOR
5976 		,p_ctr_derived_filters_tbl(tab_row).RIGHT_VALUE
5977 		,p_ctr_derived_filters_tbl(tab_row).RIGHT_PARENT
5978 		,p_ctr_derived_filters_tbl(tab_row).LOGICAL_OPERATOR
5979 		,p_ctr_derived_filters_tbl(tab_row).START_DATE_ACTIVE
5980 		,p_ctr_derived_filters_tbl(tab_row).END_DATE_ACTIVE
5981 		,p_ctr_derived_filters_tbl(tab_row).OBJECT_VERSION_NUMBER +1
5982 		,sysdate
5983 		,FND_GLOBAL.USER_ID
5984 		,p_ctr_derived_filters_tbl(tab_row).CREATION_DATE
5985 		,p_ctr_derived_filters_tbl(tab_row).CREATED_BY
5986 		,FND_GLOBAL.USER_ID
5987 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE1
5988 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE2
5989 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE3
5990 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE4
5991 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE5
5992 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE6
5993 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE7
5994 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE8
5995 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE9
5996 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE10
5997 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE11
5998 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE12
5999 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE13
6000 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE14
6001 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE15
6002 		,p_ctr_derived_filters_tbl(tab_row).ATTRIBUTE_CATEGORY
6003 		,p_ctr_derived_filters_tbl(tab_row).SECURITY_GROUP_ID
6004 		,p_ctr_derived_filters_tbl(tab_row).MIGRATED_FLAG
6005 		);
6006 
6007 		   csi_ctr_gen_utility_pvt.Validate_GrpOp_ctr
6008 		   (
6009 		      p_api_version => 1.0,
6010 		      p_commit => FND_API.G_FALSE,
6011 		      p_validation_level => FND_API.G_VALID_LEVEL_NONE,
6012 		      x_return_status => l_return_status,
6013 		      x_msg_count	=> l_msg_count,
6014 		      x_msg_data => l_msg_data,
6015 		      p_counter_id => p_ctr_derived_filters_tbl(tab_row).COUNTER_ID,
6016 		      x_valid_flag => l_valid_flag
6017 		   );
6018 
6019 		   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6020 		      RAISE FND_API.G_EXC_ERROR;
6021 		   END IF;
6022 
6023 		   -- l_ctr_derived_filters_rec.OBJECT_VERSION_NUMBER := l_ctr_derived_filters_rec.OBJECT_VERSION_NUMBER+1;
6024                    csi_ctr_gen_utility_pvt.put_line('before updating csi_counters_vl');
6025                    BEGIN
6026                       SELECT 'x'
6027                       INTO   l_found
6028                       FROM   csi_counter_template_vl
6029                       WHERE  counter_id = p_ctr_derived_filters_tbl(tab_row).COUNTER_ID;
6030 
6031                       UPDATE  csi_counter_template_b
6032                       SET     valid_flag = decode(l_valid_flag, 'Y', 'Y', 'N')
6033                       WHERE   counter_id = p_ctr_derived_filters_tbl(tab_row).COUNTER_ID;
6034                       csi_ctr_gen_utility_pvt.put_line('after updating csi_counters_vl');
6035                    EXCEPTION
6036                       WHEN NO_DATA_FOUND THEN
6037 		         UPDATE csi_counters_b
6038 		         SET    valid_flag = decode(l_valid_flag, 'Y', 'Y', 'N')
6039 		         WHERE  counter_id = p_ctr_derived_filters_tbl(tab_row).COUNTER_ID;
6040                          csi_ctr_gen_utility_pvt.put_line('after updating csi_counters_vl');
6041                    END;
6042             END IF;
6043          END IF;
6044       END LOOP;
6045  END IF;
6046  -- End of API body
6047 
6048  -- Standard check of p_commit.
6049  IF FND_API.To_Boolean( p_commit ) THEN
6050     COMMIT WORK;
6051  END IF;
6052 
6053  FND_MSG_PUB.Count_And_Get
6054    (   p_count => x_msg_count,
6055        p_data  => x_msg_data
6056    );
6057 
6058 EXCEPTION
6059    WHEN FND_API.G_EXC_ERROR THEN
6060       x_return_status := FND_API.G_RET_STS_ERROR ;
6061       ROLLBACK TO update_derived_filters;
6062       FND_MSG_PUB.Count_And_Get
6063                 (p_count => x_msg_count,
6064                  p_data  => x_msg_data
6065                 );
6066 
6067    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6068       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6069       ROLLBACK TO update_derived_filters;
6070       FND_MSG_PUB.Count_And_Get
6071       		(p_count => x_msg_count,
6072              p_data  => x_msg_data
6073             );
6074    WHEN OTHERS THEN
6075       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6076       ROLLBACK TO update_derived_filters;
6077       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6078       THEN
6079          FND_MSG_PUB.Add_Exc_Msg
6080             (G_PKG_NAME,
6081              l_api_name
6082             );
6083        END IF;
6084        FND_MSG_PUB.Count_And_Get
6085             (p_count => x_msg_count,
6086              p_data  => x_msg_data
6087             );
6088 
6089 END update_derived_filters;
6090 
6091 
6092 PROCEDURE Create_Estimation_Method
6093 (
6094     p_api_version                IN      NUMBER
6095     ,p_init_msg_list             IN      VARCHAR2
6096     ,p_commit                    IN      VARCHAR2
6097     ,p_validation_level          IN      NUMBER
6098     ,x_return_status                 OUT NOCOPY     VARCHAR2
6099     ,x_msg_count                     OUT NOCOPY     NUMBER
6100     ,x_msg_data                      OUT NOCOPY     VARCHAR2
6101     ,p_ctr_estimation_rec        IN      CSI_CTR_DATASTRUCTURES_PUB.ctr_estimation_methods_rec
6102 ) IS
6103 
6104 
6105    l_api_name                     CONSTANT VARCHAR2(30)   := 'CREATE_ESTIMATION_METHOD';
6106    l_api_version                  CONSTANT NUMBER         := 1.0;
6107    l_msg_data                     VARCHAR2(2000);
6108    l_msg_index                    NUMBER;
6109    l_msg_count                    NUMBER;
6110    -- l_debug_level                  NUMBER;
6111    l_ESTIMATION_ID	          NUMBER;
6112    l_NAME                         VARCHAR2(50);
6113    l_DESCRIPTION                  VARCHAR2(240);
6114    l_ESTIMATION_TYPE              VARCHAR2(10);
6115    l_FIXED_VALUE                  NUMBER;
6116    l_USAGE_MARKUP                 NUMBER;
6117    l_DEFAULT_VALUE                NUMBER;
6118    l_COUNTER_ID                   NUMBER;
6119    l_ESTIMATION_AVG_TYPE          VARCHAR2(10);
6120    l_START_DATE_ACTIVE            DATE;
6121    l_END_DATE_ACTIVE              DATE;
6122    l_LAST_UPDATE_DATE             DATE;
6123    l_LAST_UPDATED_BY              NUMBER;
6124    l_CREATION_DATE                DATE;
6125    l_CREATED_BY                   NUMBER;
6126    l_LAST_UPDATE_LOGIN            NUMBER;
6127    l_ATTRIBUTE1                   VARCHAR2(150);
6128    l_ATTRIBUTE2                   VARCHAR2(150);
6129    l_ATTRIBUTE3                   VARCHAR2(150);
6130    l_ATTRIBUTE4                   VARCHAR2(150);
6131    l_ATTRIBUTE5                   VARCHAR2(150);
6132    l_ATTRIBUTE6                   VARCHAR2(150);
6133    l_ATTRIBUTE7                   VARCHAR2(150);
6134    l_ATTRIBUTE8                   VARCHAR2(150);
6135    l_ATTRIBUTE9                   VARCHAR2(150);
6136    l_ATTRIBUTE10                  VARCHAR2(150);
6137    l_ATTRIBUTE11                  VARCHAR2(150);
6138    l_ATTRIBUTE12                  VARCHAR2(150);
6139    l_ATTRIBUTE13                  VARCHAR2(150);
6140    l_ATTRIBUTE14                  VARCHAR2(150);
6141    l_ATTRIBUTE15                  VARCHAR2(150);
6142    l_ATTRIBUTE_CATEGORY           VARCHAR2(30);
6143    l_OBJECT_VERSION_NUMBER        NUMBER;
6144    l_MIGRATED_FLAG                VARCHAR2(1);
6145    l_dummy                        VARCHAR2(1);
6146 BEGIN
6147    -- Standard Start of API savepoint
6148    SAVEPOINT  create_estimation_method_pvt;
6149 
6150    -- Standard call to check for call compatibility.
6151    IF NOT FND_API.Compatible_API_Call (l_api_version,
6152                                        p_api_version,
6153                                        l_api_name   ,
6154                                        G_PKG_NAME   ) THEN
6155       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6156    END IF;
6157 
6158    -- Initialize message list if p_init_msg_list is set to TRUE.
6159    IF FND_API.to_Boolean(nvl(p_init_msg_list,FND_API.G_FALSE)) THEN
6160       FND_MSG_PUB.initialize;
6161    END IF;
6162 
6163    --  Initialize API return status to success
6164    x_return_status := FND_API.G_RET_STS_SUCCESS;
6165 
6166    -- Read the debug profiles values in to global variable 7197402
6167    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
6168 
6169    --
6170    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
6171       csi_ctr_gen_utility_pvt.put_line( 'create_estimation_method_pvt'          ||'-'||
6172                                      p_api_version                              ||'-'||
6173                                      nvl(p_commit,FND_API.G_FALSE)              ||'-'||
6174                                      nvl(p_init_msg_list,FND_API.G_FALSE)       ||'-'||
6175                                      nvl(p_validation_level,FND_API.G_VALID_LEVEL_FULL) );
6176    END IF;
6177 
6178 
6179    if p_ctr_estimation_rec.estimation_id = FND_API.G_MISS_NUM then
6180       l_estimation_id := null;
6181    else
6182       l_estimation_id := p_ctr_estimation_rec.estimation_id;
6183    end if;
6184 
6185    if p_ctr_estimation_rec.name = FND_API.G_MISS_CHAR then
6186       l_name := null;
6187    else
6188       l_name := p_ctr_estimation_rec.name;
6189    end if;
6190 
6191    if p_ctr_estimation_rec.description= FND_API.G_MISS_CHAR then
6192       l_description := null;
6193    else
6194       l_description := p_ctr_estimation_rec.description;
6195    end if;
6196 
6197    if p_ctr_estimation_rec.estimation_type= FND_API.G_MISS_CHAR then
6198       l_estimation_type := null;
6199    else
6200       l_estimation_type := p_ctr_estimation_rec.estimation_type;
6201    end if;
6202 
6203    if p_ctr_estimation_rec.estimation_avg_type = FND_API.G_MISS_CHAR then
6204       l_estimation_avg_type := null;
6205    else
6206       l_estimation_avg_type := p_ctr_estimation_rec.estimation_avg_type;
6207    end if;
6208 
6209    if p_ctr_estimation_rec.fixed_value = FND_API.G_MISS_NUM then
6210       l_fixed_value := null;
6211    else
6212       l_fixed_value := p_ctr_estimation_rec.fixed_value;
6213    end if;
6214 
6215    if p_ctr_estimation_rec.usage_markup = FND_API.G_MISS_NUM then
6216       l_usage_markup := null;
6217    else
6218       l_usage_markup := p_ctr_estimation_rec.usage_markup;
6219    end if;
6220 
6221    if p_ctr_estimation_rec.default_value = FND_API.G_MISS_NUM then
6222       l_default_value := null;
6223    else
6224       l_default_value := p_ctr_estimation_rec.default_value;
6225    end if;
6226 
6227    if nvl(p_ctr_estimation_rec.start_date_active,FND_API.G_MISS_DATE) = FND_API.G_MISS_DATE then
6228       l_start_date_active := sysdate;
6229    else
6230       l_start_date_active := p_ctr_estimation_rec.start_date_active;
6231    end if;
6232 
6233    if p_ctr_estimation_rec.end_date_active = FND_API.G_MISS_DATE then
6234       l_end_date_active := null;
6235    else
6236       l_end_date_active := p_ctr_estimation_rec.end_date_active;
6237    end if;
6238 
6239    if p_ctr_estimation_rec.attribute1 = FND_API.G_MISS_CHAR then
6240       l_attribute1 := null;
6241    else
6242       l_attribute1 := p_ctr_estimation_rec.attribute1;
6243    end if;
6244 
6245    if p_ctr_estimation_rec.attribute2 = FND_API.G_MISS_CHAR then
6246       l_attribute2 := null;
6247    else
6248       l_attribute2 := p_ctr_estimation_rec.attribute2;
6249    end if;
6250 
6251    if p_ctr_estimation_rec.attribute3 = FND_API.G_MISS_CHAR then
6252       l_attribute3 := null;
6253    else
6254       l_attribute3 := p_ctr_estimation_rec.attribute3;
6255    end if;
6256 
6257    if p_ctr_estimation_rec.attribute4 = FND_API.G_MISS_CHAR then
6258       l_attribute4 := null;
6259    else
6260       l_attribute4 := p_ctr_estimation_rec.attribute4;
6261    end if;
6262 
6263    if p_ctr_estimation_rec.attribute5 = FND_API.G_MISS_CHAR then
6264       l_attribute5 := null;
6265    else
6266       l_attribute5 := p_ctr_estimation_rec.attribute5;
6267    end if;
6268 
6269    if p_ctr_estimation_rec.attribute6 = FND_API.G_MISS_CHAR then
6270       l_attribute6 := null;
6271    else
6272       l_attribute6 := p_ctr_estimation_rec.attribute6;
6273    end if;
6274 
6275    if p_ctr_estimation_rec.attribute7 = FND_API.G_MISS_CHAR then
6276       l_attribute7 := null;
6277    else
6278       l_attribute7 := p_ctr_estimation_rec.attribute7;
6279    end if;
6280 
6281    if p_ctr_estimation_rec.attribute8 = FND_API.G_MISS_CHAR then
6282       l_attribute8 := null;
6283    else
6284       l_attribute8 := p_ctr_estimation_rec.attribute8;
6285    end if;
6286 
6287    if p_ctr_estimation_rec.attribute9 = FND_API.G_MISS_CHAR then
6288       l_attribute9 := null;
6289    else
6290       l_attribute9 := p_ctr_estimation_rec.attribute9;
6291    end if;
6292 
6293    if p_ctr_estimation_rec.attribute10 = FND_API.G_MISS_CHAR then
6294       l_attribute10 := null;
6295    else
6296       l_attribute10 := p_ctr_estimation_rec.attribute10;
6297    end if;
6298 
6299    if p_ctr_estimation_rec.attribute11 = FND_API.G_MISS_CHAR then
6300       l_attribute11 := null;
6301    else
6302       l_attribute11 := p_ctr_estimation_rec.attribute11;
6303    end if;
6304 
6305    if p_ctr_estimation_rec.attribute12 = FND_API.G_MISS_CHAR then
6306       l_attribute12 := null;
6307    else
6308       l_attribute12 := p_ctr_estimation_rec.attribute12;
6309    end if;
6310 
6311    if p_ctr_estimation_rec.attribute13 = FND_API.G_MISS_CHAR then
6312       l_attribute13 := null;
6313    else
6314       l_attribute13 := p_ctr_estimation_rec.attribute13;
6315    end if;
6316 
6317    if p_ctr_estimation_rec.attribute14 = FND_API.G_MISS_CHAR then
6318       l_attribute14 := null;
6319    else
6320       l_attribute14 := p_ctr_estimation_rec.attribute14;
6321    end if;
6322 
6323    if p_ctr_estimation_rec.attribute15 = FND_API.G_MISS_CHAR then
6324       l_attribute15 := null;
6325    else
6326       l_attribute15 := p_ctr_estimation_rec.attribute15;
6327    end if;
6328 
6329    if p_ctr_estimation_rec.attribute_category = FND_API.G_MISS_CHAR then
6330       l_attribute_category := null;
6331    else
6332       l_attribute_category := p_ctr_estimation_rec.attribute_category;
6333    end if;
6334 
6335    if p_ctr_estimation_rec.migrated_flag = FND_API.G_MISS_CHAR then
6336       l_migrated_flag := null;
6337    else
6338       l_migrated_flag := p_ctr_estimation_rec.migrated_flag;
6339    end if;
6340 
6341    -- validate_start_date(l_start_date_active);
6342 
6343    BEGIN
6344       SELECT 'X'
6345       INTO   l_dummy
6346       FROM   csi_ctr_estimate_methods_vl
6347       WHERE  name = l_name;
6348 
6349       CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_EST_DUP_NAME','CTR_EST_NAME',l_name);
6350    EXCEPTION
6351       WHEN NO_DATA_FOUND THEN
6352          NULL;
6353    END;
6354 
6355    validate_lookups('CSI_COUNTER_ESTIMATION_TYPE',l_estimation_type);
6356 
6357    IF l_estimation_avg_type not in ('DLY','MTH') THEN
6358       CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_EST_AVG_TYPE_INV','CTR_EST_AVG_TYPE',l_estimation_avg_type);
6359    END IF;
6360 
6361    IF l_estimation_type = 'FIXED' then
6362       IF l_fixed_value IS NULL THEN
6363          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_EST_INV_FIXED');
6364       END IF;
6365    -- ELSIF l_estimation_type = 'USAGE' then
6366      --  IF l_default_value IS NULL OR l_usage_markup IS NULL THEN
6367        --   CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_EST_INV_USAGE');
6368       -- END IF;
6369    END IF;
6370 
6371    /* Table Handler call */
6372    CSI_CTR_ESTIMATE_METHODS_PKG.Insert_Row(
6373 	px_ESTIMATION_ID                => l_estimation_id
6374  	,p_ESTIMATION_TYPE              => l_estimation_type
6375  	,p_FIXED_VALUE                  => l_fixed_value
6376  	,p_USAGE_MARKUP                 => l_usage_markup
6377  	,p_DEFAULT_VALUE                => l_default_value
6378  	,p_ESTIMATION_AVG_TYPE          => l_estimation_avg_type
6379 	,p_START_DATE_ACTIVE            => l_start_date_active
6380 	,p_END_DATE_ACTIVE              => l_end_date_active
6381         ,p_LAST_UPDATE_DATE             => sysdate
6382         ,p_LAST_UPDATED_BY              => FND_GLOBAL.USER_ID
6383         ,p_CREATION_DATE                => sysdate
6384         ,p_CREATED_BY                   => FND_GLOBAL.USER_ID
6385         ,p_LAST_UPDATE_LOGIN            => FND_GLOBAL.USER_ID
6386         ,p_ATTRIBUTE1                   => l_attribute1
6387         ,p_ATTRIBUTE2                   => l_attribute2
6388         ,p_ATTRIBUTE3                   => l_attribute3
6389         ,p_ATTRIBUTE4                   => l_attribute4
6390         ,p_ATTRIBUTE5                   => l_attribute5
6391         ,p_ATTRIBUTE6                   => l_attribute6
6392         ,p_ATTRIBUTE7                   => l_attribute7
6393         ,p_ATTRIBUTE8                   => l_attribute8
6394         ,p_ATTRIBUTE9                   => l_attribute9
6395         ,p_ATTRIBUTE10                  => l_attribute10
6396         ,p_ATTRIBUTE11                  => l_attribute11
6397         ,p_ATTRIBUTE12                  => l_attribute12
6398         ,p_ATTRIBUTE13                  => l_attribute13
6399         ,p_ATTRIBUTE14                  => l_attribute14
6400         ,p_ATTRIBUTE15                  => l_attribute15
6401         ,p_ATTRIBUTE_CATEGORY           => l_attribute_category
6402 	,p_OBJECT_VERSION_NUMBER        => 1
6403  	,p_MIGRATED_FLAG                => l_migrated_flag
6404         ,p_NAME	                        => l_name
6405         ,p_DESCRIPTION                  => l_description
6406         );
6407 
6408    IF NOT (x_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
6409         ROLLBACK TO create_estimation_method_pvt;
6410         RETURN;
6411    END IF;
6412 
6413    /* End of table handler call */
6414 
6415    IF FND_API.to_Boolean(nvl(p_commit,FND_API.G_FALSE)) THEN
6416       COMMIT WORK;
6417    END IF;
6418 
6419    -- Standard call to get message count and IF count is  get message info.
6420    FND_MSG_PUB.Count_And_Get
6421       ( p_count  =>  x_msg_count,
6422         p_data   =>  x_msg_data
6423       );
6424 EXCEPTION
6425    WHEN FND_API.G_EXC_ERROR THEN
6426       x_return_status := FND_API.G_RET_STS_ERROR ;
6427       ROLLBACK TO create_estimation_method_pvt;
6428       FND_MSG_PUB.Count_And_Get
6429          ( p_count => x_msg_count,
6430            p_data  => x_msg_data
6431          );
6432    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6433       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6434       ROLLBACK TO create_estimation_method_pvt;
6435       FND_MSG_PUB.Count_And_Get
6436          ( p_count => x_msg_count,
6437            p_data  => x_msg_data
6438          );
6439    WHEN OTHERS THEN
6440       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6441       ROLLBACK TO create_estimation_method_pvt;
6442       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6443          FND_MSG_PUB.Add_Exc_Msg
6444             ( G_PKG_NAME,
6445               l_api_name
6446             );
6447       END IF;
6448       FND_MSG_PUB.Count_And_Get
6449          ( p_count  => x_msg_count,
6450            p_data   => x_msg_data
6451          );
6452 END create_estimation_method;
6453 
6454 PROCEDURE Update_Estimation_Method
6455 (
6456     p_api_version                IN      NUMBER
6457     ,p_init_msg_list             IN      VARCHAR2
6458     ,p_commit                    IN      VARCHAR2
6459     ,p_validation_level          IN      NUMBER
6460     ,x_return_status                 OUT NOCOPY  VARCHAR2
6461     ,x_msg_count                     OUT NOCOPY  NUMBER
6462     ,x_msg_data                      OUT NOCOPY  VARCHAR2
6463     ,p_ctr_estimation_rec        IN      CSI_CTR_DATASTRUCTURES_PUB.ctr_estimation_methods_rec
6464 ) IS
6465 
6466    CURSOR cur_estimation_rec(p_estimation_id IN NUMBER) IS
6467    SELECT name
6468           ,description
6469           ,estimation_type
6470           ,fixed_value
6471           ,usage_markup
6472           ,default_value
6473           ,estimation_avg_type
6474           ,start_date_active
6475           ,end_date_active
6476           ,last_update_date
6477           ,last_updated_by
6478           ,creation_date
6479           ,created_by
6480           ,last_update_login
6481           ,attribute1
6482           ,attribute2
6483           ,attribute3
6484           ,attribute4
6485           ,attribute5
6486           ,attribute6
6487           ,attribute7
6488           ,attribute8
6489           ,attribute9
6490           ,attribute10
6491           ,attribute11
6492           ,attribute12
6493           ,attribute13
6494           ,attribute14
6495           ,attribute15
6496           ,attribute_category
6497           ,object_version_number
6498           ,migrated_flag
6499    FROM  csi_ctr_estimate_methods_vl
6500    WHERE estimation_id = p_estimation_id
6501    FOR UPDATE OF OBJECT_VERSION_NUMBER;
6502    l_old_ctr_estimation_rec  cur_estimation_rec%ROWTYPE;
6503 
6504    l_api_name                      CONSTANT VARCHAR2(30)   := 'UPDATE_ESTIMATION_METHOD';
6505    l_api_version                   CONSTANT NUMBER         := 1.0;
6506    l_msg_data                      VARCHAR2(2000);
6507    l_msg_index                     NUMBER;
6508    l_msg_count                     NUMBER;
6509    -- l_debug_level                   NUMBER;
6510 
6511    l_ctr_estimation_rec            CSI_CTR_DATASTRUCTURES_PUB.ctr_estimation_methods_rec;
6512 BEGIN
6513    -- Standard Start of API savepoint
6514    SAVEPOINT  update_estimation_method_pvt;
6515 
6516    -- Standard call to check for call compatibility.
6517    IF NOT FND_API.Compatible_API_Call (l_api_version,
6518                                        p_api_version,
6519                                        l_api_name   ,
6520                                        G_PKG_NAME   ) THEN
6521       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6522    END IF;
6523 
6524    -- Initialize message list if p_init_msg_list is set to TRUE.
6525    IF FND_API.to_Boolean(nvl(p_init_msg_list,FND_API.G_FALSE)) THEN
6526       FND_MSG_PUB.initialize;
6527    END IF;
6528 
6529    --  Initialize API return status to success
6530    x_return_status := FND_API.G_RET_STS_SUCCESS;
6531 
6532    -- Read the debug profiles values in to global variable 7197402
6533    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
6534 
6535    --
6536    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
6537       csi_ctr_gen_utility_pvt.put_line( 'update_estimation_method_pvt'          ||'-'||
6538            p_api_version                              ||'-'||
6539            nvl(p_commit,FND_API.G_FALSE)              ||'-'||
6540            nvl(p_init_msg_list,FND_API.G_FALSE)       ||'-'||
6541            nvl(p_validation_level,FND_API.G_VALID_LEVEL_FULL) );
6542    END IF;
6543 
6544    /* Start of API Body */
6545    OPEN cur_estimation_rec(p_ctr_estimation_rec.estimation_id);
6546    FETCH cur_estimation_rec INTO l_old_ctr_estimation_rec;
6547    IF  (l_old_ctr_estimation_rec.object_version_number <> nvl(p_ctr_estimation_rec.OBJECT_VERSION_NUMBER,0)) THEN
6548       FND_MESSAGE.Set_Name('CSI', 'CSI_API_OBJ_VER_MISMATCH');
6549       FND_MSG_PUB.ADD;
6550       RAISE FND_API.G_EXC_ERROR;
6551    END IF;
6552 
6553    CLOSE cur_estimation_rec;
6554 
6555    -- IF SQL%NOTFOUND THEN
6556    --    CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_ESTIM_NOTEXISTS');
6557    -- END IF;
6558 
6559    l_ctr_estimation_rec := p_ctr_estimation_rec;
6560 
6561 
6562    IF p_ctr_estimation_rec.name IS NULL THEN
6563       l_ctr_estimation_rec.name := l_old_ctr_estimation_rec.name;
6564    ELSIF p_ctr_estimation_rec.name = FND_API.G_MISS_CHAR THEN
6565       l_ctr_estimation_rec.name := NULL;
6566    END IF;
6567 
6568    IF p_ctr_estimation_rec.description IS NULL THEN
6569       l_ctr_estimation_rec.description := l_old_ctr_estimation_rec.description;
6570    ELSIF p_ctr_estimation_rec.description = FND_API.G_MISS_CHAR THEN
6571       l_ctr_estimation_rec.description := NULL;
6572    END IF;
6573 
6574    IF p_ctr_estimation_rec.estimation_type IS NULL THEN
6575       l_ctr_estimation_rec.estimation_type := l_old_ctr_estimation_rec.estimation_type;
6576    ELSIF p_ctr_estimation_rec.estimation_type = FND_API.G_MISS_CHAR THEN
6577       l_ctr_estimation_rec.estimation_type := NULL;
6578    END IF;
6579 
6580    IF p_ctr_estimation_rec.fixed_value IS NULL THEN
6581       l_ctr_estimation_rec.fixed_value := l_old_ctr_estimation_rec.fixed_value;
6582    ELSIF p_ctr_estimation_rec.fixed_value = FND_API.G_MISS_NUM THEN
6583       l_ctr_estimation_rec.fixed_value := NULL;
6584    END IF;
6585 
6586    IF p_ctr_estimation_rec.usage_markup IS NULL THEN
6587       l_ctr_estimation_rec.usage_markup := l_old_ctr_estimation_rec.usage_markup;
6588    ELSIF p_ctr_estimation_rec.usage_markup = FND_API.G_MISS_NUM THEN
6589       l_ctr_estimation_rec.usage_markup := NULL;
6590    END IF;
6591 
6592    IF p_ctr_estimation_rec.default_value IS NULL THEN
6593       l_ctr_estimation_rec.default_value := l_old_ctr_estimation_rec.default_value;
6594    ELSIF p_ctr_estimation_rec.default_value = FND_API.G_MISS_NUM THEN
6595       l_ctr_estimation_rec.default_value := NULL;
6596    END IF;
6597 
6598    IF p_ctr_estimation_rec.estimation_avg_type IS NULL THEN
6599       l_ctr_estimation_rec.estimation_avg_type := l_old_ctr_estimation_rec.estimation_avg_type;
6600    ELSIF p_ctr_estimation_rec.estimation_avg_type = FND_API.G_MISS_CHAR THEN
6601       l_ctr_estimation_rec.estimation_avg_type := NULL;
6602    END IF;
6603 
6604    IF p_ctr_estimation_rec.start_date_active IS NULL THEN
6605       l_ctr_estimation_rec.start_date_active := l_old_ctr_estimation_rec.start_date_active;
6606    ELSIF p_ctr_estimation_rec.start_date_active = FND_API.G_MISS_DATE THEN
6607       l_ctr_estimation_rec.start_date_active := NULL;
6608    END IF;
6609 
6610    IF p_ctr_estimation_rec.end_date_active IS NULL THEN
6611       l_ctr_estimation_rec.end_date_active := l_old_ctr_estimation_rec.end_date_active;
6612    ELSIF p_ctr_estimation_rec.end_date_active = FND_API.G_MISS_DATE THEN
6613       l_ctr_estimation_rec.end_date_active := NULL;
6614    END IF;
6615 
6616    IF p_ctr_estimation_rec.attribute_category IS NULL THEN
6617       l_ctr_estimation_rec.attribute_category := l_old_ctr_estimation_rec.attribute_category;
6618    ELSIF p_ctr_estimation_rec.attribute_category = FND_API.G_MISS_CHAR THEN
6619       l_ctr_estimation_rec.attribute_category := NULL;
6620    END IF;
6621 --
6622    IF p_ctr_estimation_rec.attribute1 IS NULL THEN
6623       l_ctr_estimation_rec.attribute1 := l_old_ctr_estimation_rec.attribute1;
6624    ELSIF p_ctr_estimation_rec.attribute1 = FND_API.G_MISS_CHAR THEN
6625       l_ctr_estimation_rec.attribute1 := NULL;
6626    END IF;
6627 
6628    IF p_ctr_estimation_rec.attribute2 IS NULL THEN
6629       l_ctr_estimation_rec.attribute2 := l_old_ctr_estimation_rec.attribute2;
6630    ELSIF p_ctr_estimation_rec.attribute2 = FND_API.G_MISS_CHAR THEN
6631       l_ctr_estimation_rec.attribute2 := NULL;
6632    END IF;
6633 
6634    IF p_ctr_estimation_rec.attribute3 IS NULL THEN
6635       l_ctr_estimation_rec.attribute3 := l_old_ctr_estimation_rec.attribute3;
6636    ELSIF p_ctr_estimation_rec.attribute3 = FND_API.G_MISS_CHAR THEN
6637       l_ctr_estimation_rec.attribute3 := NULL;
6638    END IF;
6639 
6640    IF p_ctr_estimation_rec.attribute4 IS NULL THEN
6641       l_ctr_estimation_rec.attribute4 := l_old_ctr_estimation_rec.attribute4;
6642    ELSIF p_ctr_estimation_rec.attribute4 = FND_API.G_MISS_CHAR THEN
6643       l_ctr_estimation_rec.attribute4 := NULL;
6644    END IF;
6645 
6646    IF p_ctr_estimation_rec.attribute5 IS NULL THEN
6647       l_ctr_estimation_rec.attribute5 := l_old_ctr_estimation_rec.attribute5;
6648    ELSIF p_ctr_estimation_rec.attribute5 = FND_API.G_MISS_CHAR THEN
6649       l_ctr_estimation_rec.attribute5 := NULL;
6650    END IF;
6651 
6652    IF p_ctr_estimation_rec.attribute6 IS NULL THEN
6653       l_ctr_estimation_rec.attribute6 := l_old_ctr_estimation_rec.attribute6;
6654    ELSIF p_ctr_estimation_rec.attribute6 = FND_API.G_MISS_CHAR THEN
6655       l_ctr_estimation_rec.attribute6 := NULL;
6656    END IF;
6657 
6658    IF p_ctr_estimation_rec.attribute7 IS NULL THEN
6659       l_ctr_estimation_rec.attribute7 := l_old_ctr_estimation_rec.attribute7;
6660    ELSIF p_ctr_estimation_rec.attribute7 = FND_API.G_MISS_CHAR THEN
6661       l_ctr_estimation_rec.attribute7 := NULL;
6662    END IF;
6663 
6664    IF p_ctr_estimation_rec.attribute8 IS NULL THEN
6665       l_ctr_estimation_rec.attribute8 := l_old_ctr_estimation_rec.attribute8;
6666    ELSIF p_ctr_estimation_rec.attribute8 = FND_API.G_MISS_CHAR THEN
6667       l_ctr_estimation_rec.attribute8 := NULL;
6668    END IF;
6669 
6670    IF p_ctr_estimation_rec.attribute9 IS NULL THEN
6671       l_ctr_estimation_rec.attribute9 := l_old_ctr_estimation_rec.attribute9;
6672    ELSIF p_ctr_estimation_rec.attribute9 = FND_API.G_MISS_CHAR THEN
6673       l_ctr_estimation_rec.attribute9 := NULL;
6674    END IF;
6675 
6676    IF p_ctr_estimation_rec.attribute10 IS NULL THEN
6677       l_ctr_estimation_rec.attribute10 := l_old_ctr_estimation_rec.attribute10;
6678    ELSIF p_ctr_estimation_rec.attribute10 = FND_API.G_MISS_CHAR THEN
6679       l_ctr_estimation_rec.attribute10 := NULL;
6680    END IF;
6681 
6682    IF p_ctr_estimation_rec.attribute11 IS NULL THEN
6683       l_ctr_estimation_rec.attribute11 := l_old_ctr_estimation_rec.attribute11;
6684    ELSIF p_ctr_estimation_rec.attribute11 = FND_API.G_MISS_CHAR THEN
6685       l_ctr_estimation_rec.attribute11 := NULL;
6686    END IF;
6687 
6688    IF p_ctr_estimation_rec.attribute12 IS NULL THEN
6689       l_ctr_estimation_rec.attribute12 := l_old_ctr_estimation_rec.attribute12;
6690    ELSIF p_ctr_estimation_rec.attribute12= FND_API.G_MISS_CHAR THEN
6691       l_ctr_estimation_rec.attribute12 := NULL;
6692    END IF;
6693 
6694    IF p_ctr_estimation_rec.attribute13 IS NULL THEN
6695       l_ctr_estimation_rec.attribute13 := l_old_ctr_estimation_rec.attribute13;
6696    ELSIF p_ctr_estimation_rec.attribute13 = FND_API.G_MISS_CHAR THEN
6697       l_ctr_estimation_rec.attribute13 := NULL;
6698    END IF;
6699 
6700    IF p_ctr_estimation_rec.attribute14 IS NULL THEN
6701       l_ctr_estimation_rec.attribute14 := l_old_ctr_estimation_rec.attribute14;
6702    ELSIF p_ctr_estimation_rec.attribute14 = FND_API.G_MISS_CHAR THEN
6703       l_ctr_estimation_rec.attribute14 := NULL;
6704    END IF;
6705 
6706    IF p_ctr_estimation_rec.attribute15 IS NULL THEN
6707       l_ctr_estimation_rec.attribute15 := l_old_ctr_estimation_rec.attribute15;
6708    ELSIF p_ctr_estimation_rec.attribute15 = FND_API.G_MISS_CHAR THEN
6709       l_ctr_estimation_rec.attribute15 := NULL;
6710    END IF;
6711 
6712    IF p_ctr_estimation_rec.migrated_flag IS NULL THEN
6713       l_ctr_estimation_rec.migrated_flag := l_old_ctr_estimation_rec.migrated_flag;
6714    ELSIF p_ctr_estimation_rec.migrated_flag = FND_API.G_MISS_CHAR THEN
6715       l_ctr_estimation_rec.migrated_flag := NULL;
6716    END IF;
6717 
6718    -- validate_start_date(l_ctr_estimation_rec.start_date_active);
6719    validate_lookups('CSI_COUNTER_ESTIMATION_TYPE',l_ctr_estimation_rec.estimation_type);
6720 
6721    /* IF l_ctr_estimation_rec.estimation_avg_type not in ('DLY','MTH') THEN
6722    CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_EST_AVG_TYPE_INV','CTR_EST_AVG_TYPE',l_ctr_estimation_rec.estimation_avg_type);
6723    END IF; */
6724 
6725    IF l_ctr_estimation_rec.estimation_type = 'FIXED' then
6726       IF l_ctr_estimation_rec.fixed_value IS NULL THEN
6727          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_EST_INV_FIXED');
6728       END IF;
6729    END IF;
6730 
6731    /* Table Handler call */
6732    CSI_CTR_ESTIMATE_METHODS_PKG.update_Row(
6733 	p_ESTIMATION_ID                 => p_ctr_estimation_rec.estimation_id
6734  	,p_ESTIMATION_TYPE              => p_ctr_estimation_rec.estimation_type
6735  	,p_FIXED_VALUE                  => p_ctr_estimation_rec.fixed_value
6736  	,p_USAGE_MARKUP                 => p_ctr_estimation_rec.usage_markup
6737  	,p_DEFAULT_VALUE                => p_ctr_estimation_rec.default_value
6738  	,p_ESTIMATION_AVG_TYPE          => p_ctr_estimation_rec.estimation_avg_type
6739 	,p_START_DATE_ACTIVE            => p_ctr_estimation_rec.start_date_active
6740 	,p_END_DATE_ACTIVE              => p_ctr_estimation_rec.end_date_active
6741         ,p_LAST_UPDATE_DATE             => sysdate
6742         ,p_LAST_UPDATED_BY              => FND_GLOBAL.USER_ID
6743         ,p_CREATION_DATE                => p_ctr_estimation_rec.creation_date
6744         ,p_CREATED_BY                   => p_ctr_estimation_rec.created_by
6745         ,p_LAST_UPDATE_LOGIN            => FND_GLOBAL.USER_ID
6746         ,p_ATTRIBUTE1                   => p_ctr_estimation_rec.attribute1
6747         ,p_ATTRIBUTE2                   => p_ctr_estimation_rec.attribute2
6748         ,p_ATTRIBUTE3                   => p_ctr_estimation_rec.attribute3
6749         ,p_ATTRIBUTE4                   => p_ctr_estimation_rec.attribute4
6750         ,p_ATTRIBUTE5                   => p_ctr_estimation_rec.attribute5
6751         ,p_ATTRIBUTE6                   => p_ctr_estimation_rec.attribute6
6752         ,p_ATTRIBUTE7                   => p_ctr_estimation_rec.attribute7
6753         ,p_ATTRIBUTE8                   => p_ctr_estimation_rec.attribute8
6754         ,p_ATTRIBUTE9                   => p_ctr_estimation_rec.attribute9
6755         ,p_ATTRIBUTE10                  => p_ctr_estimation_rec.attribute10
6756         ,p_ATTRIBUTE11                  => p_ctr_estimation_rec.attribute11
6757         ,p_ATTRIBUTE12                  => p_ctr_estimation_rec.attribute12
6758         ,p_ATTRIBUTE13                  => p_ctr_estimation_rec.attribute13
6759         ,p_ATTRIBUTE14                  => p_ctr_estimation_rec.attribute14
6760         ,p_ATTRIBUTE15                  => p_ctr_estimation_rec.attribute15
6761         ,p_ATTRIBUTE_CATEGORY           => p_ctr_estimation_rec.attribute_category
6762 	,p_OBJECT_VERSION_NUMBER        => p_ctr_estimation_rec.object_version_number + 1
6763  	,p_MIGRATED_FLAG                => p_ctr_estimation_rec.migrated_flag
6764         ,p_NAME	                        => p_ctr_estimation_rec.name
6765         ,p_DESCRIPTION                  => p_ctr_estimation_rec.description
6766         );
6767 
6768    IF NOT (x_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
6769         ROLLBACK TO update_estimation_method_pvt;
6770         RETURN;
6771    END IF;
6772 
6773    /* End of API Body */
6774 
6775    IF FND_API.to_Boolean(nvl(p_commit,FND_API.G_FALSE)) THEN
6776       COMMIT WORK;
6777    END IF;
6778 
6779    -- Standard call to get message count and IF count is  get message info.
6780    FND_MSG_PUB.Count_And_Get
6781       ( p_count  =>  x_msg_count,
6782         p_data   =>  x_msg_data
6783       );
6784 EXCEPTION
6785    WHEN FND_API.G_EXC_ERROR THEN
6786       x_return_status := FND_API.G_RET_STS_ERROR ;
6787       ROLLBACK TO update_estimation_method_pvt;
6788       FND_MSG_PUB.Count_And_Get
6789          ( p_count => x_msg_count,
6790            p_data  => x_msg_data
6791          );
6792    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6793       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6794       ROLLBACK TO update_estimation_method_pvt;
6795       FND_MSG_PUB.Count_And_Get
6796          ( p_count => x_msg_count,
6797            p_data  => x_msg_data
6798          );
6799    WHEN OTHERS THEN
6800       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6801       ROLLBACK TO update_estimation_method_pvt;
6802       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6803          FND_MSG_PUB.Add_Exc_Msg
6804             ( G_PKG_NAME,
6805               l_api_name
6806             );
6807       END IF;
6808       FND_MSG_PUB.Count_And_Get
6809          ( p_count  => x_msg_count,
6810            p_data   => x_msg_data
6811          );
6812 END update_estimation_method;
6813 
6814 PROCEDURE Instantiate_Counters
6815 (
6816    p_api_version		IN	NUMBER
6817    ,p_init_msg_list		IN	VARCHAR2
6818    ,p_commit			IN	VARCHAR2
6819    ,x_return_status		    OUT NOCOPY	VARCHAR2
6820    ,x_msg_count		    OUT NOCOPY	NUMBER
6821    ,x_msg_data			    OUT NOCOPY	VARCHAR2
6822    ,p_counter_id_template        	IN	NUMBER
6823    ,p_source_object_code_instance IN      VARCHAR2
6824    ,p_source_object_id_instance   IN	NUMBER
6825    ,x_ctr_id_template	    OUT NOCOPY	NUMBER
6826    ,x_ctr_id_instance	    OUT NOCOPY	NUMBER
6827    ,p_maint_org_id                IN   NUMBER
6828    ,p_primary_failure_flag        IN   VARCHAR2
6829 ) IS
6830 
6831 
6832    l_api_name                      CONSTANT VARCHAR2(30)   := 'INSTANTIATE_COUNTERS';
6833    l_api_version                   CONSTANT NUMBER         := 1.0;
6834    l_msg_data                      VARCHAR2(2000);
6835    l_msg_index                     NUMBER;
6836    l_msg_count                     NUMBER;
6837    -- l_debug_level                   NUMBER;
6838 
6839    l_ctr_grp_rec                   csi_ctr_datastructures_pub.counter_groups_rec;
6840    l_counter_instance_rec          csi_ctr_datastructures_pub.counter_instance_rec;
6841    l_ctr_properties_rec            csi_ctr_datastructures_pub.ctr_properties_rec;
6842    l_ctr_rec                       csi_ctr_datastructures_pub.ctr_rec_type;
6843    l_ctr_prop_rec                  csi_ctr_datastructures_pub.ctr_prop_rec_type;
6844    l_counter_relationships_rec     csi_ctr_datastructures_pub.counter_relationships_rec;
6845    l_ctr_derived_filters_tbl       csi_ctr_datastructures_pub.ctr_derived_filters_tbl;
6846    l_counter_associations_rec      csi_ctr_datastructures_pub.counter_associations_rec;
6847 
6848    l_COMMS_NL_TRACKABLE_FLAG       VARCHAR2(1);
6849    l_source_object_cd              VARCHAR2(30);
6850    l_source_object_id_instance     NUMBER;
6851    l_ctr_grp_id                    NUMBER;
6852    l_new_ctr_grp_id                NUMBER;
6853    l_new_ctr_id                    NUMBER;
6854    l_new_ctr_prop_id               NUMBER;
6855    l_new_mapped_Ctr_id             NUMBER;
6856    l_new_ctr_formula_bvar_id       NUMBER;
6857    l_new_ctr_der_filter_id         NUMBER;
6858    l_new_der_ctr_prop_id           NUMBER;
6859    l_ctr_grp_id_instance           NUMBER;
6860    l_maint_organization_id         NUMBER;
6861    l_desc_flex                     csi_ctr_datastructures_pub.dff_rec_type;
6862    l_item                          VARCHAR2(40);
6863    l_config_root_id                NUMBER;
6864    l_mapped_item_id                NUMBER;
6865    l_org_id                        NUMBER;
6866    l_reading_type                  NUMBER;
6867    l_ctr_grp_rec                   csi_ctr_datastructures_pub.counter_groups_rec;
6868    p_relationship_query_rec        CSI_DATASTRUCTURES_PUB.RELATIONSHIP_QUERY_REC;
6869    x_relationship_tbl              CSI_DATASTRUCTURES_PUB.II_RELATIONSHIP_TBL;
6870 
6871    l_validation_level              NUMBER;
6872    l_counter_id                    NUMBER;
6873    l_instance_association_id       NUMBER;
6874    l_rel_type                      VARCHAR2(30) := 'CONFIGURATION';
6875    l_c_ind_txn                     BINARY_INTEGER := 0;
6876    l_c_ind_rdg                     BINARY_INTEGER := 0;
6877    l_c_ind_prop                    BINARY_INTEGER := 0;
6878    l_transaction_type_id           NUMBER;
6879    l_new_derive_counter_id         NUMBER;
6880    l_transaction_tbl               csi_datastructures_pub.transaction_tbl;
6881    l_counter_readings_tbl          csi_ctr_datastructures_pub.counter_readings_tbl;
6882    l_ctr_property_readings_tbl     csi_ctr_datastructures_pub.ctr_property_readings_tbl;
6883 
6884   CURSOR DFLT_PROP_RDG(p_counter_id IN NUMBER) IS
6885     SELECT ccp.counter_property_id,ccp.default_value,ccp.property_data_type, ccp.is_nullable
6886     FROM CSI_COUNTER_PROPERTIES_B ccp
6887     WHERE ccp.counter_id = p_counter_id AND NVL(ccp.is_nullable,'N') = 'N'
6888     AND   NVL(end_date_active,(SYSDATE+1)) > SYSDATE;
6889 
6890    CURSOR ctr_cur(p_counter_id NUMBER) IS
6891    SELECT counter_id,
6892           name,
6893           description,
6894           counter_type,
6895           initial_reading,
6896           initial_reading_date,
6897           tolerance_plus,
6898           tolerance_minus,
6899           uom_code,
6900           derive_function,
6901           derive_counter_id,
6902           derive_property_id,
6903           valid_flag,
6904           formula_incomplete_flag,
6905           formula_text,
6906           rollover_last_reading,
6907           rollover_first_reading,
6908           comments,
6909           usage_item_id,
6910           ctr_val_max_seq_no,
6911           start_date_active,
6912           end_date_active ,
6913           customer_view,
6914           direction,
6915           filter_reading_count,
6916           filter_type,
6917           filter_time_uom,
6918           estimation_id,
6919           attribute1,
6920           attribute2,
6921           attribute3,
6922           attribute4,
6923           attribute5,
6924           attribute6,
6925           attribute7,
6926           attribute8,
6927           attribute9,
6928           attribute10,
6929           attribute11,
6930           attribute12,
6931           attribute13,
6932           attribute14,
6933           attribute15,
6934           attribute16,
6935           attribute17,
6936           attribute18,
6937           attribute19,
6938           attribute20,
6939           attribute21,
6940           attribute22,
6941           attribute23,
6942           attribute24,
6943           attribute25,
6944           attribute26,
6945           attribute27,
6946           attribute28,
6947           attribute29,
6948           attribute30,
6949           attribute_category,
6950           group_id,
6951           defaulted_group_id,
6952           reading_type,
6953           automatic_rollover,
6954           default_usage_rate,
6955           use_past_reading,
6956           used_in_scheduling,
6957           security_group_id,
6958           nvl(time_based_manual_entry, 'N') tm_based_manual_entry,
6959           eam_required_flag
6960    FROM   csi_counter_template_vl
6961    WHERE  counter_id = p_counter_id
6962    AND    NVL(end_date_active, SYSDATE+1) > SYSDATE;
6963 
6964    CURSOR fmla_ctr_cur(p_counter_id NUMBER) IS
6965    SELECT counter_id,
6966           name,
6967           description,
6968           counter_type,
6969           initial_reading,
6970           tolerance_plus,
6971           tolerance_minus,
6972           uom_code,
6973           derive_function,
6974           derive_counter_id,
6975           derive_property_id,
6976           valid_flag,
6977           formula_incomplete_flag,
6978           formula_text,
6979           rollover_last_reading,
6980           rollover_first_reading,
6981           comments,
6982           usage_item_id,
6983           ctr_val_max_seq_no,
6984           start_date_active,
6985           end_date_active,
6986           -- created_from_counter_tmpl_id,
6987           customer_view,
6988           filter_reading_count,
6989           filter_type,
6990           filter_time_uom,
6991           estimation_id,
6992           attribute1,
6993           attribute2,
6994           attribute3,
6995           attribute4,
6996           attribute5,
6997           attribute6,
6998           attribute7,
6999           attribute8,
7000           attribute9,
7001           attribute10,
7002           attribute11,
7003           attribute12,
7004           attribute13,
7005           attribute14,
7006           attribute15,
7007           attribute16,
7008           attribute17,
7009           attribute18,
7010           attribute19,
7011           attribute20,
7012           attribute21,
7013           attribute22,
7014           attribute23,
7015           attribute24,
7016           attribute25,
7017           attribute26,
7018           attribute27,
7019           attribute28,
7020           attribute29,
7021           attribute30,
7022           attribute_category,
7023           eam_required_flag
7024    FROM   csi_counters_vl
7025    WHERE  counter_id = p_counter_id
7026    AND    counter_type = 'FORMULA';
7027 
7028    CURSOR ctr_prop_cur(p_counter_id NUMBER) IS
7029    SELECT counter_property_id,
7030           name,
7031           description,
7032           property_data_type,
7033           is_nullable,
7034           default_value,
7035           minimum_value,
7036           maximum_value,
7037           uom_code,
7038           start_date_active,
7039           end_date_active ,
7040           property_lov_type,
7041           attribute1,
7042           attribute2,
7043           attribute3,
7044           attribute4,
7045           attribute5,
7046           attribute6,
7047           attribute7,
7048           attribute8,
7049           attribute9,
7050           attribute10,
7051           attribute11,
7052           attribute12,
7053           attribute13,
7054           attribute14,
7055           attribute15,
7056           attribute_category,
7057           security_group_id
7058    FROM   csi_ctr_prop_template_vl
7059    WHERE  counter_id = p_counter_id;
7060 
7061    CURSOR ctr_formula_bvars_cur(p_counter_id NUMBER) IS
7062    SELECT relationship_id,
7063           ctr_association_id,
7064           relationship_type_code,
7065           source_counter_id,
7066           object_counter_id,
7067           bind_variable_name,
7068           factor,
7069           active_start_date,
7070           active_end_date,
7071           attribute1,
7072           attribute2,
7073           attribute3,
7074           attribute4,
7075           attribute5,
7076           attribute6,
7077           attribute7,
7078           attribute8,
7079           attribute9,
7080           attribute10,
7081           attribute11,
7082           attribute12,
7083           attribute13,
7084           attribute14,
7085           attribute15,
7086           attribute_category,
7087           security_group_id
7088    FROM   csi_counter_relationships
7089    -- WHERE  source_counter_id = p_counter_id;
7090    WHERE  object_counter_id = p_counter_id;
7091 
7092    CURSOR ctr_der_filter_cur(p_counter_id NUMBER) IS
7093    SELECT counter_id,
7094           seq_no,
7095           left_parent,
7096           counter_property_id,
7097           relational_operator,
7098           right_value,
7099           right_parent,
7100           logical_operator,
7101           start_date_active,
7102           end_date_active,
7103           security_group_id,
7104           attribute1,
7105           attribute2,
7106           attribute3,
7107           attribute4,
7108           attribute5,
7109           attribute6,
7110           attribute7,
7111           attribute8,
7112           attribute9,
7113           attribute10,
7114           attribute11,
7115           attribute12,
7116           attribute13,
7117           attribute14,
7118           attribute15,
7119           attribute_category
7120    FROM   csi_counter_derived_filters
7121    WHERE  counter_id = p_counter_id;
7122 
7123 BEGIN
7124    -- Standard Start of API savepoint
7125    SAVEPOINT  instantiate_ctr_pvt;
7126 
7127    -- Standard call to check for call compatibility.
7128    IF NOT FND_API.Compatible_API_Call (l_api_version,
7129                                        p_api_version,
7130                                        l_api_name   ,
7131                                        G_PKG_NAME   ) THEN
7132       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7133    END IF;
7134 
7135    -- Initialize message list if p_init_msg_list is set to TRUE.
7136    IF FND_API.to_Boolean(nvl(p_init_msg_list,FND_API.G_FALSE)) THEN
7137       FND_MSG_PUB.initialize;
7138    END IF;
7139 
7140    --  Initialize API return status to success
7141    x_return_status := FND_API.G_RET_STS_SUCCESS;
7142 
7143    -- Read the debug profiles values in to global variable 7197402
7144    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
7145 
7146    --
7147    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
7148       csi_ctr_gen_utility_pvt.put_line(
7149             'instantiate_ctr_pvt'||'-'||
7150             p_api_version           ||'-'||
7151             nvl(p_commit,FND_API.G_FALSE) ||'-'||
7152             nvl(p_init_msg_list,FND_API.G_FALSE)||'-'||
7153             nvl(l_validation_level,FND_API.G_VALID_LEVEL_FULL) );
7154    END IF;
7155 
7156    l_source_object_cd := p_source_object_code_instance;
7157    l_source_object_id_instance := p_source_object_id_instance;
7158 
7159    l_counter_id := p_counter_id_template;
7160    csi_ctr_gen_utility_pvt.put_line('Template counter id = '||to_char(l_counter_id));
7161 
7162    FOR ctr_cur_rec IN ctr_cur(l_counter_id) LOOP
7163       IF ctr_cur_rec.group_id IS NOT NULL THEN
7164          l_counter_instance_rec.group_id := ctr_cur_rec.defaulted_group_id;
7165       END IF;
7166       l_counter_instance_rec.name := ctr_cur_rec.name;
7167       l_counter_instance_rec.description := ctr_cur_rec.description;
7168       l_counter_instance_rec.counter_type := ctr_cur_rec.counter_type;
7169       l_counter_instance_rec.initial_reading := ctr_cur_rec.initial_reading;
7170       l_counter_instance_rec.initial_reading_date := sysdate; /*commented for bug10114702 ctr_cur_rec.initial_reading_date;*/
7171       l_counter_instance_rec.created_from_counter_tmpl_id := ctr_cur_rec.counter_id;
7172       l_counter_instance_rec.tolerance_plus := ctr_cur_rec.tolerance_plus;
7173       l_counter_instance_rec.tolerance_minus := ctr_cur_rec.tolerance_minus;
7174       l_counter_instance_rec.uom_code := ctr_cur_rec.uom_code;
7175 
7176       IF ctr_cur_rec.derive_counter_id IS NOT NULL THEN
7177          BEGIN
7178 
7179             -- Bug 9068223 Fixing in both R12 and 12.1 code
7180             /*SELECT max(counter_id)
7181             INTO   l_new_derive_counter_id
7182             FROM   csi_counters_b
7183             WHERE  created_from_counter_tmpl_id = ctr_cur_rec.derive_counter_id;*/
7184 
7185             -- To fetch the maximum counter id
7186            SELECT counter_id INTO l_new_derive_counter_id FROM
7187            (SELECT counter_id
7188            FROM   csi_counters_b
7189            WHERE  created_from_counter_tmpl_id = ctr_cur_rec.derive_counter_id
7190            ORDER BY counter_id DESC)
7191            WHERE ROWNUM = 1;
7192 
7193          EXCEPTION
7194             WHEN NO_DATA_FOUND THEN
7195                csi_ctr_gen_utility_pvt.put_line( ' New counter id not found for deriver counte id '||to_char(ctr_cur_rec.derive_counter_id));
7196                NULL;
7197          END;
7198 
7199          l_counter_instance_rec.derive_counter_id := l_new_derive_counter_id;
7200       ELSE
7201          l_counter_instance_rec.derive_counter_id := ctr_cur_rec.derive_counter_id;
7202       END IF;
7203 
7204       l_counter_instance_rec.derive_function := ctr_cur_rec.derive_function;
7205       l_counter_instance_rec.derive_property_id := ctr_cur_rec.derive_property_id;
7206       l_counter_instance_rec.valid_flag := 'Y';
7207       l_counter_instance_rec.formula_text := ctr_cur_rec.formula_text;
7208       l_counter_instance_rec.rollover_last_reading := ctr_cur_rec.rollover_last_reading;
7209       l_counter_instance_rec.rollover_first_reading := ctr_cur_rec.rollover_first_reading;
7210       l_counter_instance_rec.usage_item_id := ctr_cur_rec.usage_item_id;
7211       l_counter_instance_rec.start_date_active := ctr_cur_rec.start_date_active;
7212       l_counter_instance_rec.end_date_active := ctr_cur_rec.end_date_active;
7213       l_counter_instance_rec.security_group_id := ctr_cur_rec.security_group_id;
7214       l_counter_instance_rec.object_version_number := 1;
7215       l_counter_instance_rec.last_update_date := sysdate;
7216       l_counter_instance_rec.last_Updated_by  := fnd_global.user_id;
7217       l_counter_instance_rec.creation_date := sysdate;
7218       l_counter_instance_rec.created_by := FND_GLOBAL.user_id;
7219       l_counter_instance_rec.last_update_login := FND_GLOBAL.user_id;
7220       l_counter_instance_rec.attribute_category := ctr_cur_rec.attribute_category;
7221       l_counter_instance_rec.attribute1 := ctr_cur_rec.attribute1;
7222       l_counter_instance_rec.attribute2 := ctr_cur_rec.attribute2;
7223       l_counter_instance_rec.attribute3 := ctr_cur_rec.attribute3;
7224       l_counter_instance_rec.attribute4 := ctr_cur_rec.attribute4;
7225       l_counter_instance_rec.attribute5 := ctr_cur_rec.attribute5;
7226       l_counter_instance_rec.attribute6 := ctr_cur_rec.attribute6;
7227       l_counter_instance_rec.attribute7 := ctr_cur_rec.attribute7;
7228       l_counter_instance_rec.attribute8 := ctr_cur_rec.attribute8;
7229       l_counter_instance_rec.attribute9 := ctr_cur_rec.attribute9;
7230       l_counter_instance_rec.attribute10 := ctr_cur_rec.attribute10;
7231       l_counter_instance_rec.attribute11 := ctr_cur_rec.attribute11;
7232       l_counter_instance_rec.attribute12 := ctr_cur_rec.attribute12;
7233       l_counter_instance_rec.attribute13 := ctr_cur_rec.attribute13;
7234       l_counter_instance_rec.attribute14 := ctr_cur_rec.attribute14;
7235       l_counter_instance_rec.attribute15 := ctr_cur_rec.attribute15;
7236       l_counter_instance_rec.attribute16 := ctr_cur_rec.attribute16;
7237       l_counter_instance_rec.attribute17 := ctr_cur_rec.attribute17;
7238       l_counter_instance_rec.attribute18 := ctr_cur_rec.attribute18;
7239       l_counter_instance_rec.attribute19 := ctr_cur_rec.attribute19;
7240       l_counter_instance_rec.attribute20 := ctr_cur_rec.attribute20;
7241       l_counter_instance_rec.attribute21 := ctr_cur_rec.attribute21;
7242       l_counter_instance_rec.attribute22 := ctr_cur_rec.attribute22;
7243       l_counter_instance_rec.attribute23 := ctr_cur_rec.attribute23;
7244       l_counter_instance_rec.attribute24 := ctr_cur_rec.attribute24;
7245       l_counter_instance_rec.attribute25 := ctr_cur_rec.attribute25;
7246       l_counter_instance_rec.attribute26 := ctr_cur_rec.attribute26;
7247       l_counter_instance_rec.attribute27 := ctr_cur_rec.attribute27;
7248       l_counter_instance_rec.attribute28 := ctr_cur_rec.attribute28;
7249       l_counter_instance_rec.attribute29 := ctr_cur_rec.attribute29;
7250       l_counter_instance_rec.attribute30 := ctr_cur_rec.attribute30;
7251       l_counter_instance_rec.customer_view := ctr_cur_rec.customer_view;
7252       l_counter_instance_rec.direction := ctr_cur_rec.direction;
7253       l_counter_instance_rec.filter_type := ctr_cur_rec.filter_type;
7254       l_counter_instance_rec.filter_reading_count := ctr_cur_rec.filter_reading_count;
7255       l_counter_instance_rec.filter_time_uom := ctr_cur_rec.filter_time_uom;
7256       l_counter_instance_rec.estimation_id := ctr_cur_rec.estimation_id;
7257       l_counter_instance_rec.reading_type := ctr_cur_rec.reading_type;
7258       l_counter_instance_rec.automatic_rollover := ctr_cur_rec.automatic_rollover;
7259       l_counter_instance_rec.default_usage_rate := ctr_cur_rec.default_usage_rate;
7260       l_counter_instance_rec.use_past_reading   := ctr_cur_rec.use_past_reading;
7261       l_counter_instance_rec.used_in_scheduling := ctr_cur_rec.used_in_scheduling;
7262       l_counter_instance_rec.defaulted_group_id := ctr_cur_rec.defaulted_group_id;
7263       l_new_ctr_grp_id := ctr_cur_rec.defaulted_group_id;
7264       l_counter_instance_rec.comments := ctr_cur_rec.comments;
7265       l_counter_instance_rec.time_based_manual_entry := ctr_cur_rec.tm_based_manual_entry;
7266       l_counter_instance_rec.eam_required_flag := ctr_cur_rec.eam_required_flag;
7267 
7268 csi_ctr_gen_utility_pvt.put_line('Calling csi_counter_pvt.create_counter....');
7269 
7270       CSI_COUNTER_PVT.CREATE_COUNTER
7271           (
7272             p_api_version           => p_api_version,
7273             p_init_msg_list         => p_init_msg_list,
7274             p_commit                => p_commit,
7275             p_validation_level      => l_validation_level,
7276             p_counter_instance_rec  => l_counter_instance_rec,
7277             x_return_status         => x_return_status,
7278             x_msg_count             => x_msg_count,
7279             x_msg_data              => x_msg_data,
7280             x_ctr_id                => l_new_ctr_id
7281           );
7282       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
7283           l_msg_index := 1;
7284           l_msg_count := x_msg_count;
7285 
7286           WHILE l_msg_count > 0 LOOP
7287              x_msg_data := FND_MSG_PUB.GET
7288                            (l_msg_index,
7289                             FND_API.G_FALSE );
7290              csi_ctr_gen_utility_pvt.put_line( ' Error from CSI_COUNTER_PVT.CREATE_COUNTER');
7291              csi_ctr_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
7292              l_msg_index := l_msg_index + 1;
7293              l_msg_count := l_msg_count - 1;
7294           END LOOP;
7295           RAISE FND_API.G_EXC_ERROR;
7296        END IF;
7297 
7298       -- x_ctr_id_instance := l_counter_instance_rec.group_id;
7299       -- Create Counter Associations
7300       l_counter_associations_rec.source_object_code := l_source_object_cd;
7301       l_counter_associations_rec.source_object_id   := l_source_object_id_instance;
7302       l_counter_associations_rec.counter_id         := l_new_ctr_id;
7303       l_counter_associations_rec.start_date_active  := sysdate;
7304       l_counter_associations_rec.maint_organization_id := p_maint_org_id;
7305       l_counter_associations_rec.primary_failure_flag := p_primary_failure_flag;
7306 
7307       csi_ctr_gen_utility_pvt.put_line( ' Maint organization id = '||to_char(p_maint_org_id));
7308 csi_ctr_gen_utility_pvt.put_line('Calling csi_counter_pvt.create_ctr_associations....');
7309       CSI_COUNTER_PVT.CREATE_CTR_ASSOCIATIONS
7310          (
7311            p_api_version            => p_api_version
7312            ,p_commit                => p_commit
7313            ,p_init_msg_list         => p_init_msg_list
7314            ,p_validation_level      => l_validation_level
7315            ,p_counter_associations_rec  => l_counter_associations_rec
7316            ,x_return_status         => x_return_status
7317            ,x_msg_count             => x_msg_count
7318            ,x_msg_data              => x_msg_data
7319            ,x_instance_association_id  => l_instance_association_id
7320          );
7321 
7322       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
7323           l_msg_index := 1;
7324           l_msg_count := x_msg_count;
7325 
7326           WHILE l_msg_count > 0 LOOP
7327              x_msg_data := FND_MSG_PUB.GET
7328                            (l_msg_index,
7329                             FND_API.G_FALSE );
7330              csi_ctr_gen_utility_pvt.put_line( ' Error from CSI_COUNTER_PVT.CREATE_CTR_ASSOCIATIONS');
7331              csi_ctr_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
7332              l_msg_index := l_msg_index + 1;
7333              l_msg_count := l_msg_count - 1;
7334           END LOOP;
7335           RAISE FND_API.G_EXC_ERROR;
7336        END IF;
7337 
7338        -- Insert initial reading by calling counter reading api.
7339        IF ctr_cur_rec.counter_type = 'REGULAR' AND
7340           (ctr_cur_rec.initial_reading is not null AND
7341           ctr_cur_rec.initial_reading <> FND_API.G_MISS_NUM) AND
7342           nvl(l_counter_relationships_rec.RELATIONSHIP_TYPE_CODE,'X') <>
7343           l_rel_type THEN
7344 
7345 csi_ctr_gen_utility_pvt.put_line('Inside the call reading api...');
7346 
7347           --create transaction record
7348           l_transaction_tbl(l_c_ind_txn)                    := NULL;
7349           l_transaction_tbl(l_c_ind_txn).TRANSACTION_ID     := NULL;
7350           l_transaction_tbl(l_c_ind_txn).TRANSACTION_DATE   := sysdate;
7351           l_transaction_tbl(l_c_ind_txn).SOURCE_TRANSACTION_DATE  := sysdate;
7352 
7353           if l_source_object_cd = 'CP' then
7354              l_transaction_type_id := 80;
7355           elsif l_source_object_cd = 'CONTRACT_LINE' then
7356              l_transaction_type_id := 81;
7357           end if;
7358 
7359           if l_transaction_type_id is null then
7360              l_transaction_type_id := 80;
7361           end if;
7362 
7363           l_transaction_tbl(l_c_ind_txn).TRANSACTION_TYPE_ID   := l_transaction_type_id;
7364           l_transaction_tbl(l_c_ind_txn).TXN_SUB_TYPE_ID       := NULL;
7365           l_transaction_tbl(l_c_ind_txn).SOURCE_GROUP_REF_ID   := NULL;
7366           l_transaction_tbl(l_c_ind_txn).SOURCE_GROUP_REF      := NULL;
7367           l_transaction_tbl(l_c_ind_txn).SOURCE_HEADER_REF_ID  := l_source_object_id_instance;
7368 
7369           -- create counter readings table
7370           l_counter_readings_tbl(l_c_ind_rdg).COUNTER_VALUE_ID :=  NULL;
7371           l_counter_readings_tbl(l_c_ind_rdg).COUNTER_ID       :=  l_new_ctr_id;
7372           l_counter_readings_tbl(l_c_ind_rdg).VALUE_TIMESTAMP  :=  sysdate; /*commented for bug 10114702 ctr_cur_rec.initial_reading_date;*/
7373           l_counter_readings_tbl(l_c_ind_rdg).COUNTER_READING  :=  ctr_cur_rec.initial_reading;
7374           l_counter_readings_tbl(l_c_ind_rdg).initial_reading_flag := 'Y';
7375           l_counter_readings_tbl(l_c_ind_rdg).DISABLED_FLAG    :=  'N';
7376           l_counter_readings_tbl(l_c_ind_rdg).COMMENTS         :=  'Initial Reading';
7377           l_counter_readings_tbl(l_c_ind_rdg).PARENT_TBL_INDEX :=  l_c_ind_txn;
7378 
7379           FOR dflt_rec IN DFLT_PROP_RDG(l_new_ctr_id)
7380           LOOP
7381              l_ctr_property_readings_tbl(l_c_ind_prop).COUNTER_PROP_VALUE_ID := NULL;
7382              l_ctr_property_readings_tbl(l_c_ind_prop).COUNTER_PROPERTY_ID   := dflt_rec.counter_property_id;
7383              IF dflt_rec.default_value IS NOT NULL THEN
7384                 l_ctr_property_readings_tbl(l_c_ind_prop).PROPERTY_VALUE := dflt_rec.default_value;
7385              ELSE
7386                 IF dflt_rec.property_data_type = 'CHAR' THEN
7387                    l_ctr_property_readings_tbl(l_c_ind_prop).PROPERTY_VALUE := 'Initial Reading';
7388                 ELSIF dflt_rec.property_data_type = 'DATE' THEN
7389                    l_ctr_property_readings_tbl(l_c_ind_prop).PROPERTY_VALUE := sysdate;
7390                 ELSE
7391                    l_ctr_property_readings_tbl(l_c_ind_prop).PROPERTY_VALUE := '1';
7392                 END IF;
7393              END IF;
7394 
7395              l_ctr_property_readings_tbl(l_c_ind_prop).VALUE_TIMESTAMP := sysdate;
7396              l_ctr_property_readings_tbl(l_c_ind_prop).PARENT_TBL_INDEX  := l_c_ind_rdg;
7397              l_c_ind_prop := l_c_ind_prop + 1;
7398           END LOOP;
7399 
7400 csi_ctr_gen_utility_pvt.put_line('Calling the capture counter reading...');
7401           csi_counter_readings_pub.capture_counter_reading(
7402              p_api_version      => 1.0,
7403              p_commit           => p_commit,
7404              p_init_msg_list    => p_init_msg_list,
7405              p_validation_level => l_validation_level,
7406              p_txn_tbl          => l_transaction_tbl,
7407              p_ctr_rdg_tbl      => l_counter_readings_tbl,
7408              p_ctr_prop_rdg_tbl => l_ctr_property_readings_tbl,
7409              x_return_status    => x_return_status,
7410              x_msg_count        => x_msg_count,
7411              x_msg_data         => x_msg_data
7412              );
7413 
7414           IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
7415              l_msg_index := 1;
7416              l_msg_count := x_msg_count;
7417 
7418              WHILE l_msg_count > 0 LOOP
7419                 x_msg_data := FND_MSG_PUB.GET
7420                         (l_msg_index,
7421                         FND_API.G_FALSE );
7422                 csi_ctr_gen_utility_pvt.put_line( ' Error from Instantiate-Capture Readings');
7423                 csi_ctr_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
7424                 l_msg_index := l_msg_index + 1;
7425                 l_msg_count := l_msg_count - 1;
7426              END LOOP;
7427              RAISE FND_API.G_EXC_ERROR;
7428           END IF;
7429       END IF;
7430 
7431       -- INstantiate all the properties for that counter.
7432       FOR ctr_prop_cur_rec IN ctr_prop_cur(ctr_cur_rec.counter_id) LOOP
7433 
7434          l_ctr_properties_rec.counter_id := l_new_ctr_id;
7435          l_ctr_properties_rec.name := ctr_prop_cur_rec.name;
7436          l_ctr_properties_rec.description := ctr_prop_cur_rec.description;
7437          l_ctr_properties_rec.property_data_type := ctr_prop_cur_rec.property_data_type;
7438          l_ctr_properties_rec.property_lov_type  :=  ctr_prop_cur_rec.property_lov_type;
7439          l_ctr_properties_rec.created_from_ctr_prop_tmpl_id := ctr_prop_cur_rec.counter_property_id;
7440          l_ctr_properties_rec.is_nullable := ctr_prop_cur_rec.is_nullable;
7441          l_ctr_properties_rec.default_value := ctr_prop_cur_rec.default_value;
7442          l_ctr_properties_rec.minimum_value := ctr_prop_cur_rec.minimum_value;
7443          l_ctr_properties_rec.maximum_value := ctr_prop_cur_rec.maximum_value;
7444          l_ctr_properties_rec.uom_code := ctr_prop_cur_rec.uom_code;
7445          l_ctr_properties_rec.start_date_active := ctr_prop_cur_rec.start_date_active;
7446          l_ctr_properties_rec.end_date_active := ctr_prop_cur_rec.end_date_active;
7447          l_ctr_properties_rec.security_group_id := ctr_prop_cur_rec.security_group_id;
7448          l_ctr_properties_rec.object_version_number := 1;
7449          l_ctr_properties_rec.last_update_date := sysdate;
7450          l_ctr_properties_rec.last_Updated_by  := fnd_global.user_id;
7451          l_ctr_properties_rec.creation_date := sysdate;
7452          l_ctr_properties_rec.created_by := FND_GLOBAL.user_id;
7453          l_ctr_properties_rec.last_update_login := FND_GLOBAL.user_id;
7454          l_ctr_properties_rec.attribute_category := ctr_prop_cur_rec.attribute_category;
7455          l_ctr_properties_rec.attribute1 := ctr_prop_cur_rec.attribute1;
7456          l_ctr_properties_rec.attribute2 := ctr_prop_cur_rec.attribute2;
7457          l_ctr_properties_rec.attribute3 := ctr_prop_cur_rec.attribute3;
7458          l_ctr_properties_rec.attribute4 := ctr_prop_cur_rec.attribute4;
7459          l_ctr_properties_rec.attribute5 := ctr_prop_cur_rec.attribute5;
7460          l_ctr_properties_rec.attribute6 := ctr_prop_cur_rec.attribute6;
7461          l_ctr_properties_rec.attribute7 := ctr_prop_cur_rec.attribute7;
7462          l_ctr_properties_rec.attribute8 := ctr_prop_cur_rec.attribute8;
7463          l_ctr_properties_rec.attribute9 := ctr_prop_cur_rec.attribute9;
7464          l_ctr_properties_rec.attribute10 := ctr_prop_cur_rec.attribute10;
7465          l_ctr_properties_rec.attribute11 := ctr_prop_cur_rec.attribute11;
7466          l_ctr_properties_rec.attribute12 := ctr_prop_cur_rec.attribute12;
7467          l_ctr_properties_rec.attribute13 := ctr_prop_cur_rec.attribute13;
7468          l_ctr_properties_rec.attribute14 := ctr_prop_cur_rec.attribute14;
7469          l_ctr_properties_rec.attribute15 := ctr_prop_cur_rec.attribute15;
7470 
7471 
7472          CSI_Counter_PVT.Create_Ctr_Property
7473             (
7474              p_api_version       => p_api_version
7475              ,p_commit           => p_commit
7476              ,p_init_msg_list    => p_init_msg_list
7477              ,p_validation_level => l_validation_level
7478              ,p_ctr_properties_rec => l_ctr_properties_rec
7479              ,x_return_status     => x_return_status
7480              ,x_msg_count         => x_msg_count
7481              ,x_msg_data          => x_msg_data
7482              ,x_ctr_property_id   => l_new_ctr_prop_id
7483           );
7484 
7485       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
7486           l_msg_index := 1;
7487           l_msg_count := x_msg_count;
7488 
7489           WHILE l_msg_count > 0 LOOP
7490              x_msg_data := FND_MSG_PUB.GET
7491                            (l_msg_index,
7492                             FND_API.G_FALSE );
7493              csi_ctr_gen_utility_pvt.put_line( ' Error from CSI_Counter_PVT.Create_Ctr_Property');
7494              csi_ctr_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
7495              l_msg_index := l_msg_index + 1;
7496              l_msg_count := l_msg_count - 1;
7497           END LOOP;
7498           RAISE FND_API.G_EXC_ERROR;
7499        END IF;
7500 
7501       END LOOP;
7502 
7503       -- All the counter properties belonging to this counter have been
7504       -- instantiated.
7505 
7506    END LOOP;
7507  csi_ctr_gen_utility_pvt.put_line('After the end loop ');
7508  csi_ctr_gen_utility_pvt.put_line('New counter id = '||to_char(l_new_ctr_id));
7509 
7510    FOR fmla_ctr_cur_rec IN fmla_ctr_cur(l_new_ctr_id) LOOP
7511       csi_ctr_gen_utility_pvt.put_line('Formula Counter Id = '||to_char(fmla_ctr_cur_rec.counter_id));
7512       FOR ctr_der_filter_cur_rec IN ctr_der_filter_cur(fmla_ctr_cur_rec.counter_id)
7513       LOOP
7514  csi_ctr_gen_utility_pvt.put_line('Inside the ctr_der_filter_cur_rec ');
7515 
7516          SELECT counter_property_id
7517          INTO   l_new_der_ctr_prop_id
7518          FROM   csi_counter_template_vl ctr,
7519                 csi_ctr_prop_template_vl ctrprop
7520          WHERE  ctrprop.counter_property_id = ctr_der_filter_cur_rec.counter_property_id
7521          AND    ctrprop.counter_id = l_new_ctr_id;
7522 
7523 /*         l_ctr_derived_filters_tbl.attribute1 := ctr_der_filter_cur_rec.attribute1;
7524          l_ctr_derived_filters_tbl.attribute2 := ctr_der_filter_cur_rec.attribute2;
7525          l_ctr_derived_filters_tbl.attribute3 := ctr_der_filter_cur_rec.attribute3;
7526          l_ctr_derived_filters_tbl.attribute4 := ctr_der_filter_cur_rec.attribute4;
7527          l_ctr_derived_filters_tbl.attribute5 := ctr_der_filter_cur_rec.attribute5;
7528          l_ctr_derived_filters_tbl.attribute6 := ctr_der_filter_cur_rec.attribute6;
7529          l_ctr_derived_filters_tbl.attribute7 := ctr_der_filter_cur_rec.attribute7;
7530          l_ctr_derived_filters_tbl.attribute8 := ctr_der_filter_cur_rec.attribute8;
7531          l_ctr_derived_filters_tbl.attribute9 := ctr_der_filter_cur_rec.attribute9;
7532          l_ctr_derived_filters_tbl.attribute10 := ctr_der_filter_cur_rec.attribute10;
7533          l_ctr_derived_filters_tbl.attribute11 := ctr_der_filter_cur_rec.attribute11;
7534          l_ctr_derived_filters_tbl.attribute12 := ctr_der_filter_cur_rec.attribute12;
7535          l_ctr_derived_filters_tbl.attribute13 := ctr_der_filter_cur_rec.attribute13;
7536          l_ctr_derived_filters_tbl.attribute14 := ctr_der_filter_cur_rec.attribute14;
7537          l_ctr_derived_filters_tbl.attribute15 := ctr_der_filter_cur_rec.attribute15;
7538          l_ctr_derived_filters_tbl.attribute_category := ctr_der_filter_cur_rec.attribute_category;
7539          l_ctr_derived_filters_tbl.security_group_id := ctr_der_filter_cur_rec.security_group_id;
7540          l_ctr_derived_filters_tbl.counter_id := ctr_der_filter_cur_rec.counter_id;
7541          l_ctr_derived_filters_tbl.left_parent := ctr_der_filter_cur_rec.left_parent;
7542          l_ctr_derived_filters_tbl.counter_property_id := l_new_der_ctr_prop_id;
7543          l_ctr_derived_filters_tbl.relational_operator := ctr_der_filter_cur_rec.relational_operator;
7544          l_ctr_derived_filters_tbl.right_value := ctr_der_filter_cur_rec.right_value;
7545          l_ctr_derived_filters_tbl.right_parent := ctr_der_filter_cur_rec.right_parent;
7546          l_ctr_derived_filters_tbl.logical_operator := ctr_der_filter_cur_rec.logical_operator;
7547          l_ctr_derived_filters_tbl.start_date_active := ctr_der_filter_cur_rec.start_date_active;
7548          l_ctr_derived_filters_tbl.end_date_active := ctr_der_filter_cur_rec.end_date_active;
7549          l_ctr_derived_filters_tbl.object_version_number := 1;
7550          l_ctr_derived_filters_tbl.last_update_date := sysdate;
7551          l_ctr_derived_filters_tbl.last_Updated_by  := fnd_global.user_id;
7552          l_ctr_derived_filters_tbl.creation_date := sysdate;
7553          l_ctr_derived_filters_tbl.created_by := FND_GLOBAL.user_id;
7554          l_ctr_derived_filters_tbl.last_update_login := FND_GLOBAL.user_id;
7555 */
7556          CSI_COUNTER_TEMPLATE_PVT.CREATE_DERIVED_FILTERS
7557             (
7558              p_api_version           => p_api_version
7559              ,p_commit               => p_commit
7560              ,p_init_msg_list        => p_init_msg_list
7561              ,p_validation_level     => l_validation_level
7562              ,p_ctr_derived_filters_tbl => l_ctr_derived_filters_tbl
7563              ,x_return_status         => x_return_status
7564              ,x_msg_count             => x_msg_count
7565              ,x_msg_data              => x_msg_data
7566             );
7567 
7568       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
7569           l_msg_index := 1;
7570           l_msg_count := x_msg_count;
7571 
7572           WHILE l_msg_count > 0 LOOP
7573              x_msg_data := FND_MSG_PUB.GET
7574                            (l_msg_index,
7575                             FND_API.G_FALSE );
7576              csi_ctr_gen_utility_pvt.put_line( ' Error from CSI_COUNTER_TEMPLATE_PVT.CREATE_DERIVED_FILTERS');
7577              csi_ctr_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
7578              l_msg_index := l_msg_index + 1;
7579              l_msg_count := l_msg_count - 1;
7580           END LOOP;
7581           RAISE FND_API.G_EXC_ERROR;
7582        END IF;
7583 
7584       END LOOP;
7585 
7586       -- FOR ctr_formula_bvars_cur_rec IN ctr_formula_bvars_cur(l_new_ctr_id)
7587       csi_ctr_gen_utility_pvt.put_line( 'Passed counter_id = '||to_char(l_counter_id));
7588       csi_ctr_gen_utility_pvt.put_line( 'Passed new grp id = '||to_char(l_new_ctr_grp_id));
7589       FOR ctr_formula_bvars_cur_rec IN ctr_formula_bvars_cur(l_counter_id)
7590       LOOP
7591          -- Bug 9068223 Fixing in both R12 and 12.1 code
7592          /*SELECT max(counter_id)
7593          INTO   l_new_mapped_ctr_id
7594          FROM   csi_counters_b
7595          WHERE  created_from_counter_tmpl_id = ctr_formula_bvars_cur_rec.source_counter_id;*/
7596 
7597          -- To fetch the maximum counter id
7598          SELECT counter_id INTO l_new_mapped_ctr_id FROM
7599          (SELECT counter_id
7600          FROM   csi_counters_b
7601          WHERE  created_from_counter_tmpl_id = ctr_formula_bvars_cur_rec.source_counter_id
7602          ORDER BY counter_id DESC)
7603          WHERE ROWNUM = 1;
7604 
7605          -- AND    defaulted_group_id = l_new_ctr_grp_id;
7606 
7607          IF ctr_formula_bvars_cur_rec.source_counter_id IS NOT NULL THEN
7608             -- THIS IS a configuration counter. I need to see if I can
7609             -- find out the CP to which the mapped_counter is attached.
7610             -- It may not be instantiated yet, so its possible that its
7611             -- not possible to get the mapped cp id. If i can't get it,
7612             -- set the formula_incompl_flag to 'N' for that counter.
7613 
7614             -- You cannot have configuration counters fro service items
7615             -- only for serviceable products. So, I can safely look at
7616             -- cs_customer_products and assume this is a product.
7617 
7618             FND_PROFILE.Get('ORG_ID',l_org_id);
7619 
7620             l_config_root_id := csi_generic_grp.config_root_node(p_source_object_id_instance,'COMPONENT-OF');
7621 
7622             BEGIN
7623                p_relationship_query_rec.RELATIONSHIP_ID := NULL;
7624                p_relationship_query_rec.RELATIONSHIP_TYPE_CODE := 'COMPONENT-OF';
7625                p_relationship_query_rec.OBJECT_ID := l_config_root_id;
7626                p_relationship_query_rec.SUBJECT_ID := NULL;
7627 
7628                -- Now call the stored program
7629                csi_ii_relationships_pub.get_relationships
7630                   (1.0,
7631                    '',
7632                    '',
7633                    NULL,
7634                    p_relationship_query_rec,
7635                    NULL,
7636                    NULL,
7637                    '',
7638                    x_relationship_tbl,
7639                    x_return_status,
7640                    x_msg_count,
7641                    x_msg_data);
7642 
7643 
7644                -- Output the results
7645                IF x_relationship_tbl IS NOT NULL THEN
7646                   IF x_relationship_tbl.count > 0 THEN
7647                      FOR i IN x_relationship_tbl.first..x_relationship_tbl.last LOOP
7648                         IF x_relationship_tbl.exists(i) THEN
7649                            --null; -- type of data not known
7650                            SELECT inventory_item_id
7651                            INTO   l_mapped_item_id
7652                            FROM   csi_item_instances
7653                            WHERE  instance_id = x_relationship_tbl(i).subject_id
7654                            AND    inv_master_organization_id = l_org_id;
7655 
7656                            --IF l_mapped_item_id = ctr_formula_bvars_cur_rec.mapped_inv_item_id then
7657                             --  exit;
7658                            --END IF;
7659                         END IF;
7660                      END LOOP;
7661                   END IF;
7662                END IF;
7663             EXCEPTION
7664                WHEN OTHERS THEN
7665                   -- l_mapped_item_id := ctr_formula_bvars_cur_rec.mapped_inv_item_id;
7666                   NULL;
7667             END;
7668 
7669          END IF;
7670 
7671          -- l_reading_type   := ctr_formula_bvars_cur_rec.reading_type;
7672          l_counter_relationships_rec.attribute1 := ctr_formula_bvars_cur_rec.attribute1;
7673          l_counter_relationships_rec.attribute2 := ctr_formula_bvars_cur_rec.attribute2;
7674          l_counter_relationships_rec.attribute3 := ctr_formula_bvars_cur_rec.attribute3;
7675          l_counter_relationships_rec.attribute4 := ctr_formula_bvars_cur_rec.attribute4;
7676          l_counter_relationships_rec.attribute5 := ctr_formula_bvars_cur_rec.attribute5;
7677          l_counter_relationships_rec.attribute6 := ctr_formula_bvars_cur_rec.attribute6;
7678          l_counter_relationships_rec.attribute7 := ctr_formula_bvars_cur_rec.attribute7;
7679          l_counter_relationships_rec.attribute8 := ctr_formula_bvars_cur_rec.attribute8;
7680          l_counter_relationships_rec.attribute9 := ctr_formula_bvars_cur_rec.attribute9;
7681          l_counter_relationships_rec.attribute10 := ctr_formula_bvars_cur_rec.attribute10;
7682          l_counter_relationships_rec.attribute11 := ctr_formula_bvars_cur_rec.attribute11;
7683          l_counter_relationships_rec.attribute12 := ctr_formula_bvars_cur_rec.attribute12;
7684          l_counter_relationships_rec.attribute13 := ctr_formula_bvars_cur_rec.attribute13;
7685          l_counter_relationships_rec.attribute14 := ctr_formula_bvars_cur_rec.attribute14;
7686          l_counter_relationships_rec.attribute15 := ctr_formula_bvars_cur_rec.attribute15;
7687          l_counter_relationships_rec.attribute_category := ctr_formula_bvars_cur_rec.attribute_category;
7688          l_counter_relationships_rec.security_group_id := ctr_formula_bvars_cur_rec.security_group_id;
7689          l_counter_relationships_rec.object_version_number := 1;
7690          l_counter_relationships_rec.last_update_date := sysdate;
7691          l_counter_relationships_rec.last_Updated_by  := fnd_global.user_id;
7692          l_counter_relationships_rec.creation_date := sysdate;
7693          l_counter_relationships_rec.created_by := FND_GLOBAL.user_id;
7694          l_counter_relationships_rec.last_update_login := FND_GLOBAL.user_id;
7695          l_counter_relationships_rec.ctr_association_id := ctr_formula_bvars_cur_rec.ctr_association_id;
7696          l_counter_relationships_rec.relationship_type_code := ctr_formula_bvars_cur_rec.relationship_type_code;
7697          -- l_counter_relationships_rec.source_counter_id := ctr_formula_bvars_cur_rec.source_counter_id;
7698          --l_counter_relationships_rec.object_counter_id := ctr_formula_bvars_cur_rec.object_counter_id;
7699          l_counter_relationships_rec.source_counter_id := l_new_mapped_ctr_id;
7700          l_counter_relationships_rec.object_counter_id := l_new_ctr_id;
7701          l_counter_relationships_rec.bind_variable_name := ctr_formula_bvars_cur_rec.bind_variable_name;
7702          l_counter_relationships_rec.factor := ctr_formula_bvars_cur_rec.factor;
7703          l_counter_relationships_rec.active_start_date := ctr_formula_bvars_cur_rec.active_start_date;
7704          l_counter_relationships_rec.active_end_date := ctr_formula_bvars_cur_rec.active_end_date;
7705 
7706          csi_counter_template_pvt.create_counter_relationship
7707             (
7708             p_api_version         => p_api_version
7709             ,p_commit             => fnd_api.g_false
7710             ,p_init_msg_list      => p_init_msg_list
7711             ,p_validation_level   => l_validation_level
7712             ,p_counter_relationships_rec => l_counter_relationships_rec
7713             ,x_return_status      => x_return_status
7714             ,x_msg_count          => x_msg_count
7715             ,x_msg_data           => x_msg_data
7716             );
7717 
7718        END LOOP;
7719    END LOOP;
7720 
7721    x_ctr_id_template := p_counter_id_template;
7722    -- x_ctr_id_instance := l_new_ctr_id;
7723 
7724    IF FND_API.to_Boolean(nvl(p_commit,FND_API.G_FALSE)) THEN
7725       COMMIT WORK;
7726    END IF;
7727 
7728    -- Standard call to get message count and IF count is  get message info.
7729    FND_MSG_PUB.Count_And_Get
7730       ( p_count  =>  x_msg_count,
7731         p_data   =>  x_msg_data
7732       );
7733 EXCEPTION
7734    WHEN FND_API.G_EXC_ERROR THEN
7735       x_return_status := FND_API.G_RET_STS_ERROR ;
7736       ROLLBACK TO instantiate_ctr_pvt;
7737       FND_MSG_PUB.Count_And_Get
7738          ( p_count => x_msg_count,
7739            p_data  => x_msg_data
7740          );
7741    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7742       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7743       ROLLBACK TO instantiate_ctr_pvt;
7744       FND_MSG_PUB.Count_And_Get
7745          ( p_count => x_msg_count,
7746            p_data  => x_msg_data
7747          );
7748    WHEN OTHERS THEN
7749       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7750       ROLLBACK TO instantiate_ctr_pvt;
7751       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
7752          FND_MSG_PUB.Add_Exc_Msg
7753             ( G_PKG_NAME,
7754               l_api_name
7755             );
7756       END IF;
7757       FND_MSG_PUB.Count_And_Get
7758          ( p_count  => x_msg_count,
7759            p_data   => x_msg_data
7760          );
7761 END Instantiate_Counters;
7762 
7763 PROCEDURE Instantiate_Grp_Counters
7764 (
7765    p_api_version		IN	NUMBER
7766    ,p_init_msg_list		IN	VARCHAR2
7767    ,p_commit			IN	VARCHAR2
7768    ,x_return_status		OUT NOCOPY VARCHAR2
7769    ,x_msg_count		        OUT NOCOPY NUMBER
7770    ,x_msg_data		        OUT NOCOPY VARCHAR2
7771    ,p_group_id_template        	IN	NUMBER
7772    ,p_source_object_code_instance IN    VARCHAR2
7773    ,p_source_object_id_instance   IN	NUMBER
7774    ,x_ctr_grp_id_instance	  OUT NOCOPY	NUMBER
7775    ,p_maint_org_id                IN    NUMBER
7776    ,p_primary_failure_flag        IN    VARCHAR2
7777 ) IS
7778 
7779    l_api_name                      CONSTANT VARCHAR2(30)   := 'INSTANTIATE_GRP_COUNTERS';
7780    l_api_version                   CONSTANT NUMBER         := 1.0;
7781    l_msg_data                      VARCHAR2(2000);
7782    l_msg_index                     NUMBER;
7783    l_msg_count                     NUMBER;
7784    -- l_debug_level                   NUMBER;
7785 
7786    l_ctr_grp_rec                   csi_ctr_datastructures_pub.counter_groups_rec;
7787    l_counter_instance_rec          csi_ctr_datastructures_pub.counter_instance_rec;
7788    l_ctr_properties_rec            csi_ctr_datastructures_pub.ctr_properties_rec;
7789    l_ctr_rec                       csi_ctr_datastructures_pub.ctr_rec_type;
7790    l_ctr_prop_rec                  csi_ctr_datastructures_pub.ctr_prop_rec_type;
7791    l_counter_relationships_rec     csi_ctr_datastructures_pub.counter_relationships_rec;
7792    l_ctr_derived_filters_tbl       csi_ctr_datastructures_pub.ctr_derived_filters_tbl;
7793    l_counter_associations_rec      csi_ctr_datastructures_pub.counter_associations_rec;
7794 
7795    l_COMMS_NL_TRACKABLE_FLAG       VARCHAR2(1);
7796    l_source_object_cd              VARCHAR2(30);
7797    l_source_object_id_instance     NUMBER;
7798    l_ctr_grp_id                    NUMBER;
7799    l_new_ctr_grp_id                NUMBER;
7800    l_new_ctr_id                    NUMBER;
7801    l_new_ctr_prop_id               NUMBER;
7802    l_new_mapped_Ctr_id             NUMBER;
7803    l_new_ctr_formula_bvar_id       NUMBER;
7804    l_new_ctr_der_filter_id         NUMBER;
7805    l_new_der_ctr_prop_id           NUMBER;
7806    l_counter_group_id              NUMBER;
7807    l_ctr_grp_id_instance           NUMBER;
7808    l_maint_organization_id         NUMBER;
7809    l_desc_flex                     csi_ctr_datastructures_pub.dff_rec_type;
7810    l_item                          VARCHAR2(40);
7811    l_config_root_id                NUMBER;
7812    l_mapped_item_id                NUMBER;
7813    l_org_id                        NUMBER;
7814    l_reading_type                  NUMBER;
7815    p_relationship_query_rec        CSI_DATASTRUCTURES_PUB.RELATIONSHIP_QUERY_REC;
7816    x_relationship_tbl              CSI_DATASTRUCTURES_PUB.II_RELATIONSHIP_TBL;
7817 
7818    l_validation_level              NUMBER;
7819    l_counter_id                    NUMBER;
7820    l_instance_association_id       NUMBER;
7821    l_new_derive_counter_id         NUMBER;
7822    l_created_map_id                NUMBER;
7823 
7824    CURSOR ctr_cur(p_counter_group_id NUMBER) IS
7825    SELECT counter_id,
7826           name,
7827           description,
7828           counter_type,
7829           initial_reading,
7830           initial_reading_date,
7831           tolerance_plus,
7832           tolerance_minus,
7833           uom_code,
7834           derive_function,
7835           derive_counter_id,
7836           derive_property_id,
7837           valid_flag,
7838           formula_incomplete_flag,
7839           formula_text,
7840           rollover_last_reading,
7841           rollover_first_reading,
7842           comments,
7843           usage_item_id,
7844           ctr_val_max_seq_no,
7845           start_date_active,
7846           end_date_active ,
7847           customer_view,
7848           direction,
7849           filter_reading_count,
7850           filter_type,
7851           filter_time_uom,
7852           estimation_id,
7853           attribute1,
7854           attribute2,
7855           attribute3,
7856           attribute4,
7857           attribute5,
7858           attribute6,
7859           attribute7,
7860           attribute8,
7861           attribute9,
7862           attribute10,
7863           attribute11,
7864           attribute12,
7865           attribute13,
7866           attribute14,
7867           attribute15,
7868           attribute16,
7869           attribute17,
7870           attribute18,
7871           attribute19,
7872           attribute20,
7873           attribute21,
7874           attribute22,
7875           attribute23,
7876           attribute24,
7877           attribute25,
7878           attribute26,
7879           attribute27,
7880           attribute28,
7881           attribute29,
7882           attribute30,
7883           attribute_category,
7884           group_id,
7885           defaulted_group_id,
7886           reading_type,
7887           automatic_rollover,
7888           default_usage_rate,
7889           use_past_reading,
7890           used_in_scheduling,
7891           security_group_id,
7892           nvl(time_based_manual_entry, 'N') tm_based_manual_entry,
7893           eam_required_flag
7894    FROM   csi_counter_template_vl
7895    WHERE  group_id = p_counter_group_id
7896    AND    NVL(end_date_active, SYSDATE+1) > SYSDATE; --Added for bug 9115470
7897 
7898    CURSOR fmla_ctr_cur(p_counter_group_id NUMBER) IS
7899    SELECT counter_id,
7900           name,
7901           description,
7902           counter_type,
7903           initial_reading,
7904           tolerance_plus,
7905           tolerance_minus,
7906           uom_code,
7907           derive_function,
7908           derive_counter_id,
7909           derive_property_id,
7910           valid_flag,
7911           formula_incomplete_flag,
7912           formula_text,
7913           rollover_last_reading,
7914           rollover_first_reading,
7915           comments,
7916           usage_item_id,
7917           ctr_val_max_seq_no,
7918           start_date_active,
7919           end_date_active,
7920           -- created_from_counter_tmpl_id,
7921           customer_view,
7922           filter_reading_count,
7923           filter_type,
7924           filter_time_uom,
7925           estimation_id,
7926           attribute1,
7927           attribute2,
7928           attribute3,
7929           attribute4,
7930           attribute5,
7931           attribute6,
7932           attribute7,
7933           attribute8,
7934           attribute9,
7935           attribute10,
7936           attribute11,
7937           attribute12,
7938           attribute13,
7939           attribute14,
7940           attribute15,
7941           attribute16,
7942           attribute17,
7943           attribute18,
7944           attribute19,
7945           attribute20,
7946           attribute21,
7947           attribute22,
7948           attribute23,
7949           attribute24,
7950           attribute25,
7951           attribute26,
7952           attribute27,
7953           attribute28,
7954           attribute29,
7955           attribute30,
7956           attribute_category,
7957           eam_required_flag
7958    FROM   csi_counters_vl
7959    WHERE  group_id = p_counter_group_id
7960    AND    counter_type = 'FORMULA';
7961 
7962    CURSOR ctr_prop_cur(p_counter_id NUMBER) IS
7963    SELECT counter_property_id,
7964           name,
7965           description,
7966           property_data_type,
7967           is_nullable,
7968           default_value,
7969           minimum_value,
7970           maximum_value,
7971           uom_code,
7972           start_date_active,
7973           end_date_active ,
7974           property_lov_type,
7975           attribute1,
7976           attribute2,
7977           attribute3,
7978           attribute4,
7979           attribute5,
7980           attribute6,
7981           attribute7,
7982           attribute8,
7983           attribute9,
7984           attribute10,
7985           attribute11,
7986           attribute12,
7987           attribute13,
7988           attribute14,
7989           attribute15,
7990           attribute_category,
7991           security_group_id
7992    FROM   csi_ctr_prop_template_vl
7993    WHERE  counter_id = p_counter_id;
7994 
7995    CURSOR ctr_formula_bvars_cur(p_counter_id NUMBER) IS
7996    SELECT relationship_id,
7997           ctr_association_id,
7998           relationship_type_code,
7999           source_counter_id,
8000           object_counter_id,
8001           bind_variable_name,
8002           factor,
8003           active_start_date,
8004           active_end_date,
8005           attribute1,
8006           attribute2,
8007           attribute3,
8008           attribute4,
8009           attribute5,
8010           attribute6,
8011           attribute7,
8012           attribute8,
8013           attribute9,
8014           attribute10,
8015           attribute11,
8016           attribute12,
8017           attribute13,
8018           attribute14,
8019           attribute15,
8020           attribute_category,
8021           security_group_id
8022    FROM   csi_counter_relationships
8023    -- WHERE  source_counter_id = p_counter_id;
8024    WHERE  object_counter_id = p_counter_id;
8025 
8026    CURSOR ctr_der_filter_cur(p_counter_id NUMBER) IS
8027    SELECT counter_id,
8028           seq_no,
8029           left_parent,
8030           counter_property_id,
8031           relational_operator,
8032           right_value,
8033           right_parent,
8034           logical_operator,
8035           start_date_active,
8036           end_date_active,
8037           security_group_id,
8038           attribute1,
8039           attribute2,
8040           attribute3,
8041           attribute4,
8042           attribute5,
8043           attribute6,
8044           attribute7,
8045           attribute8,
8046           attribute9,
8047           attribute10,
8048           attribute11,
8049           attribute12,
8050           attribute13,
8051           attribute14,
8052           attribute15,
8053           attribute_category
8054    FROM   csi_counter_derived_filters
8055    WHERE  counter_id = p_counter_id;
8056 
8057 BEGIN
8058    -- Standard Start of API savepoint
8059    SAVEPOINT  instantiate_grp_counters;
8060 
8061    -- Standard call to check for call compatibility.
8062    IF NOT FND_API.Compatible_API_Call (l_api_version,
8063                                        p_api_version,
8064                                        l_api_name   ,
8065                                        G_PKG_NAME   ) THEN
8066       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8067    END IF;
8068 
8069    -- Initialize message list if p_init_msg_list is set to TRUE.
8070    IF FND_API.to_Boolean(nvl(p_init_msg_list,FND_API.G_FALSE)) THEN
8071       FND_MSG_PUB.initialize;
8072    END IF;
8073 
8074    --  Initialize API return status to success
8075    x_return_status := FND_API.G_RET_STS_SUCCESS;
8076 
8077    -- Read the debug profiles values in to global variable 7197402
8078    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
8079 
8080    --
8081    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
8082       csi_ctr_gen_utility_pvt.put_line(
8083             'instantiate_grp_counters'||'-'||
8084             p_api_version           ||'-'||
8085             nvl(p_commit,FND_API.G_FALSE) ||'-'||
8086             nvl(p_init_msg_list,FND_API.G_FALSE)||'-'||
8087             nvl(l_validation_level,FND_API.G_VALID_LEVEL_FULL) );
8088    END IF;
8089 
8090    l_source_object_cd := p_source_object_code_instance;
8091    l_source_object_id_instance := p_source_object_id_instance;
8092    l_counter_group_id := p_group_id_template;
8093 
8094    BEGIN
8095       SELECT name,
8096              description,
8097              association_type,
8098              start_date_active,
8099              end_date_active ,
8100              attribute1,
8101              attribute2,
8102              attribute3,
8103              attribute4,
8104              attribute5,
8105              attribute6,
8106              attribute7,
8107              attribute8,
8108              attribute9,
8109              attribute10,
8110              attribute11,
8111              attribute12,
8112              attribute13,
8113              attribute14,
8114              attribute15,
8115              context,
8116              'N'
8117       INTO   l_ctr_grp_rec.name,
8118              l_ctr_grp_rec.description,
8119              l_ctr_grp_rec.association_type,
8120              l_ctr_grp_rec.start_date_active,
8121              l_ctr_grp_rec.end_date_active ,
8122              l_ctr_grp_rec.attribute1,
8123              l_ctr_grp_rec.attribute2,
8124              l_ctr_grp_rec.attribute3,
8125              l_ctr_grp_rec.attribute4,
8126              l_ctr_grp_rec.attribute5,
8127              l_ctr_grp_rec.attribute6,
8128              l_ctr_grp_rec.attribute7,
8129              l_ctr_grp_rec.attribute8,
8130              l_ctr_grp_rec.attribute9,
8131              l_ctr_grp_rec.attribute10,
8132              l_ctr_grp_rec.attribute11,
8133              l_ctr_grp_rec.attribute12,
8134              l_ctr_grp_rec.attribute13,
8135              l_ctr_grp_rec.attribute14,
8136              l_ctr_grp_rec.attribute15,
8137              l_ctr_grp_rec.context,
8138              l_ctr_grp_rec.template_flag
8139       FROM   cs_csi_counter_groups
8140       WHERE  counter_group_id = l_counter_group_id;
8141 
8142       csi_ctr_gen_utility_pvt.put_line('Counter group Id = '||to_char(l_counter_group_id));
8143       csi_ctr_gen_utility_pvt.put_line('Template Flag = '||l_ctr_grp_rec.template_flag);
8144       -- Added Bug 8510631
8145       l_ctr_grp_rec.source_object_code := l_source_object_cd;
8146       l_ctr_grp_rec.source_object_id := l_source_object_id_instance;
8147       l_ctr_grp_rec.source_counter_group_id := l_counter_group_id;
8148       -- End Addition Bug 8510631
8149       l_ctr_grp_rec.created_from_ctr_grp_tmpl_id := l_counter_group_id;
8150       create_counter_group
8151         (
8152          p_api_version        => p_api_version
8153          ,p_commit             => p_commit
8154          ,p_init_msg_list      => p_init_msg_list
8155          ,p_validation_level   => l_validation_level
8156          ,p_counter_groups_rec => l_ctr_grp_rec
8157          ,x_return_status      => x_return_status
8158          ,x_msg_count          => x_msg_count
8159          ,x_msg_data           => x_msg_data
8160         );
8161 
8162        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
8163           l_msg_index := 1;
8164           l_msg_count := x_msg_count;
8165 
8166           WHILE l_msg_count > 0 LOOP
8167              x_msg_data := FND_MSG_PUB.GET
8168                            (l_msg_index,
8169                             FND_API.G_FALSE );
8170              csi_ctr_gen_utility_pvt.put_line('Error from CSI_COUNTER_TEMPLATE_PVT.CREATE_COUNTER_GROUP');
8171              csi_ctr_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
8172              l_msg_index := l_msg_index + 1;
8173              l_msg_count := l_msg_count - 1;
8174           END LOOP;
8175           RAISE FND_API.G_EXC_ERROR;
8176       END IF;
8177 
8178       l_ctr_grp_id_instance := l_ctr_grp_rec.counter_group_id;
8179       x_ctr_grp_id_instance := l_ctr_grp_rec.counter_group_id;
8180    EXCEPTION
8181       WHEN OTHERS THEN
8182          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_GRP_INVALID');
8183    END;
8184 
8185    FOR ctr_cur_rec IN ctr_cur(l_counter_group_id) LOOP
8186       l_counter_instance_rec.group_id := l_ctr_grp_id_instance;
8187       l_counter_instance_rec.name := ctr_cur_rec.name;
8188       l_counter_instance_rec.description := ctr_cur_rec.description;
8189       l_counter_instance_rec.counter_type := ctr_cur_rec.counter_type;
8190       l_counter_instance_rec.initial_reading := ctr_cur_rec.initial_reading;
8191       l_counter_instance_rec.initial_reading_date := ctr_cur_rec.initial_reading_date;
8192       l_counter_instance_rec.created_from_counter_tmpl_id := ctr_cur_rec.counter_id;
8193       l_counter_instance_rec.tolerance_plus := ctr_cur_rec.tolerance_plus;
8194       l_counter_instance_rec.tolerance_minus := ctr_cur_rec.tolerance_minus;
8195       l_counter_instance_rec.uom_code := ctr_cur_rec.uom_code;
8196 
8197       IF ctr_cur_rec.derive_counter_id IS NOT NULL THEN
8198          BEGIN
8199             -- Bug 9068223 Fixing in both R12 and 12.1 code
8200             /*SELECT max(counter_id)
8201             INTO   l_new_derive_counter_id
8202             FROM   csi_counters_b
8203             WHERE  created_from_counter_tmpl_id = ctr_cur_rec.derive_counter_id;*/
8204 
8205             -- To fetch the maximum counter id
8206            SELECT counter_id INTO l_new_derive_counter_id FROM
8207            (SELECT counter_id
8208            FROM   csi_counters_b
8209            WHERE  created_from_counter_tmpl_id = ctr_cur_rec.derive_counter_id
8210            ORDER BY counter_id DESC)
8211            WHERE ROWNUM = 1;
8212 
8213          EXCEPTION
8214             WHEN NO_DATA_FOUND THEN
8215                csi_ctr_gen_utility_pvt.put_line( ' New counter id not found for deriver counte id '||to_char(ctr_cur_rec.derive_counter_id));
8216                NULL;
8217          END;
8218 
8219          l_counter_instance_rec.derive_counter_id := l_new_derive_counter_id;
8220       ELSE
8221          l_counter_instance_rec.derive_counter_id := ctr_cur_rec.derive_counter_id;
8222       END IF;
8223 
8224       l_counter_instance_rec.derive_function := ctr_cur_rec.derive_function;
8225       l_counter_instance_rec.derive_property_id := ctr_cur_rec.derive_property_id;
8226       l_counter_instance_rec.valid_flag := 'Y';
8227       l_counter_instance_rec.formula_text := ctr_cur_rec.formula_text;
8228       l_counter_instance_rec.rollover_last_reading := ctr_cur_rec.rollover_last_reading;
8229       l_counter_instance_rec.rollover_first_reading := ctr_cur_rec.rollover_first_reading;
8230       l_counter_instance_rec.usage_item_id := ctr_cur_rec.usage_item_id;
8231       l_counter_instance_rec.start_date_active := ctr_cur_rec.start_date_active;
8232       l_counter_instance_rec.end_date_active := ctr_cur_rec.end_date_active;
8233       l_counter_instance_rec.security_group_id := ctr_cur_rec.security_group_id;
8234       l_counter_instance_rec.object_version_number := 1;
8235       l_counter_instance_rec.last_update_date := sysdate;
8236       l_counter_instance_rec.last_Updated_by  := fnd_global.user_id;
8237       l_counter_instance_rec.creation_date := sysdate;
8238       l_counter_instance_rec.created_by := FND_GLOBAL.user_id;
8239       l_counter_instance_rec.last_update_login := FND_GLOBAL.user_id;
8240       l_counter_instance_rec.attribute_category := ctr_cur_rec.attribute_category;
8241       l_counter_instance_rec.attribute1 := ctr_cur_rec.attribute1;
8242       l_counter_instance_rec.attribute2 := ctr_cur_rec.attribute2;
8243       l_counter_instance_rec.attribute3 := ctr_cur_rec.attribute3;
8244       l_counter_instance_rec.attribute4 := ctr_cur_rec.attribute4;
8245       l_counter_instance_rec.attribute5 := ctr_cur_rec.attribute5;
8246       l_counter_instance_rec.attribute6 := ctr_cur_rec.attribute6;
8247       l_counter_instance_rec.attribute7 := ctr_cur_rec.attribute7;
8248       l_counter_instance_rec.attribute8 := ctr_cur_rec.attribute8;
8249       l_counter_instance_rec.attribute9 := ctr_cur_rec.attribute9;
8250       l_counter_instance_rec.attribute10 := ctr_cur_rec.attribute10;
8251       l_counter_instance_rec.attribute11 := ctr_cur_rec.attribute11;
8252       l_counter_instance_rec.attribute12 := ctr_cur_rec.attribute12;
8253       l_counter_instance_rec.attribute13 := ctr_cur_rec.attribute13;
8254       l_counter_instance_rec.attribute14 := ctr_cur_rec.attribute14;
8255       l_counter_instance_rec.attribute15 := ctr_cur_rec.attribute15;
8256       l_counter_instance_rec.attribute16 := ctr_cur_rec.attribute16;
8257       l_counter_instance_rec.attribute17 := ctr_cur_rec.attribute17;
8258       l_counter_instance_rec.attribute18 := ctr_cur_rec.attribute18;
8259       l_counter_instance_rec.attribute19 := ctr_cur_rec.attribute19;
8260       l_counter_instance_rec.attribute20 := ctr_cur_rec.attribute20;
8261       l_counter_instance_rec.attribute21 := ctr_cur_rec.attribute21;
8262       l_counter_instance_rec.attribute22 := ctr_cur_rec.attribute22;
8263       l_counter_instance_rec.attribute23 := ctr_cur_rec.attribute23;
8264       l_counter_instance_rec.attribute24 := ctr_cur_rec.attribute24;
8265       l_counter_instance_rec.attribute25 := ctr_cur_rec.attribute25;
8266       l_counter_instance_rec.attribute26 := ctr_cur_rec.attribute26;
8267       l_counter_instance_rec.attribute27 := ctr_cur_rec.attribute27;
8268       l_counter_instance_rec.attribute28 := ctr_cur_rec.attribute28;
8269       l_counter_instance_rec.attribute29 := ctr_cur_rec.attribute29;
8270       l_counter_instance_rec.attribute30 := ctr_cur_rec.attribute30;
8271       l_counter_instance_rec.customer_view := ctr_cur_rec.customer_view;
8272       l_counter_instance_rec.direction := ctr_cur_rec.direction;
8273       l_counter_instance_rec.filter_type := ctr_cur_rec.filter_type;
8274       l_counter_instance_rec.filter_reading_count := ctr_cur_rec.filter_reading_count;
8275       l_counter_instance_rec.filter_time_uom := ctr_cur_rec.filter_time_uom;
8276       l_counter_instance_rec.estimation_id := ctr_cur_rec.estimation_id;
8277       l_counter_instance_rec.reading_type := ctr_cur_rec.reading_type;
8278       l_counter_instance_rec.automatic_rollover := ctr_cur_rec.automatic_rollover;
8279       l_counter_instance_rec.default_usage_rate := ctr_cur_rec.default_usage_rate;
8280       l_counter_instance_rec.use_past_reading   := ctr_cur_rec.use_past_reading;
8281       l_counter_instance_rec.used_in_scheduling := ctr_cur_rec.used_in_scheduling;
8282       l_counter_instance_rec.defaulted_group_id := l_ctr_grp_id_instance;
8283       l_new_ctr_grp_id := l_ctr_grp_id_instance;
8284       l_counter_instance_rec.comments := ctr_cur_rec.comments;
8285       l_counter_instance_rec.time_based_manual_entry := ctr_cur_rec.tm_based_manual_entry;
8286       l_counter_instance_rec.eam_required_flag := ctr_cur_rec.eam_required_flag;
8287 
8288       CSI_COUNTER_PVT.CREATE_COUNTER
8289           (
8290             p_api_version           => p_api_version,
8291             p_init_msg_list         => p_init_msg_list,
8292             p_commit                => p_commit,
8293             p_validation_level      => l_validation_level,
8294             p_counter_instance_rec  => l_counter_instance_rec,
8295             x_return_status         => x_return_status,
8296             x_msg_count             => x_msg_count,
8297             x_msg_data              => x_msg_data,
8298             x_ctr_id                => l_new_ctr_id
8299           );
8300       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
8301           l_msg_index := 1;
8302           l_msg_count := x_msg_count;
8303 
8304           WHILE l_msg_count > 0 LOOP
8305              x_msg_data := FND_MSG_PUB.GET
8306                            (l_msg_index,
8307                             FND_API.G_FALSE );
8308              csi_ctr_gen_utility_pvt.put_line( ' Error from CSI_COUNTER_PVT.CREATE_COUNTER');
8309              csi_ctr_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
8310              l_msg_index := l_msg_index + 1;
8311              l_msg_count := l_msg_count - 1;
8312           END LOOP;
8313           RAISE FND_API.G_EXC_ERROR;
8314        END IF;
8315 
8316       -- x_ctr_id_instance := l_counter_instance_rec.group_id;
8317       -- Create Counter Associations
8318       l_counter_associations_rec.source_object_code := l_source_object_cd;
8319       l_counter_associations_rec.source_object_id   := l_source_object_id_instance;
8320       l_counter_associations_rec.counter_id         := l_new_ctr_id;
8321       l_counter_associations_rec.start_date_active  := sysdate;
8322       l_counter_associations_rec.maint_organization_id := p_maint_org_id;
8323       l_counter_associations_rec.primary_failure_flag := p_primary_failure_flag;
8324 
8325       CSI_COUNTER_PVT.CREATE_CTR_ASSOCIATIONS
8326          (
8327            p_api_version            => p_api_version
8328            ,p_commit                => p_commit
8329            ,p_init_msg_list         => p_init_msg_list
8330            ,p_validation_level      => l_validation_level
8331            ,p_counter_associations_rec  => l_counter_associations_rec
8332            ,x_return_status         => x_return_status
8333            ,x_msg_count             => x_msg_count
8334            ,x_msg_data              => x_msg_data
8335            ,x_instance_association_id  => l_instance_association_id
8336          );
8337 
8338       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
8339           l_msg_index := 1;
8340           l_msg_count := x_msg_count;
8341 
8342           WHILE l_msg_count > 0 LOOP
8343              x_msg_data := FND_MSG_PUB.GET
8344                            (l_msg_index,
8345                             FND_API.G_FALSE );
8346              csi_ctr_gen_utility_pvt.put_line( ' Error from CSI_COUNTER_PVT.CREATE_CTR_ASSOCIATIONS');
8347              csi_ctr_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
8348              l_msg_index := l_msg_index + 1;
8349              l_msg_count := l_msg_count - 1;
8350           END LOOP;
8351           RAISE FND_API.G_EXC_ERROR;
8352        END IF;
8353       -- INstantiate all the properties for that counter.
8354 
8355       FOR ctr_prop_cur_rec IN ctr_prop_cur(ctr_cur_rec.counter_id) LOOP
8356 
8357          l_ctr_properties_rec.counter_id := l_new_ctr_id;
8358          l_ctr_properties_rec.name := ctr_prop_cur_rec.name;
8359          l_ctr_properties_rec.description := ctr_prop_cur_rec.description;
8360          l_ctr_properties_rec.property_data_type := ctr_prop_cur_rec.property_data_type;
8361          l_ctr_properties_rec.property_lov_type  :=  ctr_prop_cur_rec.property_lov_type;
8362          l_ctr_properties_rec.created_from_ctr_prop_tmpl_id := ctr_prop_cur_rec.counter_property_id;
8363          l_ctr_properties_rec.is_nullable := ctr_prop_cur_rec.is_nullable;
8364          l_ctr_properties_rec.default_value := ctr_prop_cur_rec.default_value;
8365          l_ctr_properties_rec.minimum_value := ctr_prop_cur_rec.minimum_value;
8366          l_ctr_properties_rec.maximum_value := ctr_prop_cur_rec.maximum_value;
8367          l_ctr_properties_rec.uom_code := ctr_prop_cur_rec.uom_code;
8368          l_ctr_properties_rec.start_date_active := ctr_prop_cur_rec.start_date_active;
8369          l_ctr_properties_rec.end_date_active := ctr_prop_cur_rec.end_date_active;
8370          l_ctr_properties_rec.security_group_id := ctr_prop_cur_rec.security_group_id;
8371          l_ctr_properties_rec.object_version_number := 1;
8372          l_ctr_properties_rec.last_update_date := sysdate;
8373          l_ctr_properties_rec.last_Updated_by  := fnd_global.user_id;
8374          l_ctr_properties_rec.creation_date := sysdate;
8375          l_ctr_properties_rec.created_by := FND_GLOBAL.user_id;
8376          l_ctr_properties_rec.last_update_login := FND_GLOBAL.user_id;
8377          l_ctr_properties_rec.attribute_category := ctr_prop_cur_rec.attribute_category;
8378          l_ctr_properties_rec.attribute1 := ctr_prop_cur_rec.attribute1;
8379          l_ctr_properties_rec.attribute2 := ctr_prop_cur_rec.attribute2;
8380          l_ctr_properties_rec.attribute3 := ctr_prop_cur_rec.attribute3;
8381          l_ctr_properties_rec.attribute4 := ctr_prop_cur_rec.attribute4;
8382          l_ctr_properties_rec.attribute5 := ctr_prop_cur_rec.attribute5;
8383          l_ctr_properties_rec.attribute6 := ctr_prop_cur_rec.attribute6;
8384          l_ctr_properties_rec.attribute7 := ctr_prop_cur_rec.attribute7;
8385          l_ctr_properties_rec.attribute8 := ctr_prop_cur_rec.attribute8;
8386          l_ctr_properties_rec.attribute9 := ctr_prop_cur_rec.attribute9;
8387          l_ctr_properties_rec.attribute10 := ctr_prop_cur_rec.attribute10;
8388          l_ctr_properties_rec.attribute11 := ctr_prop_cur_rec.attribute11;
8389          l_ctr_properties_rec.attribute12 := ctr_prop_cur_rec.attribute12;
8390          l_ctr_properties_rec.attribute13 := ctr_prop_cur_rec.attribute13;
8391          l_ctr_properties_rec.attribute14 := ctr_prop_cur_rec.attribute14;
8392          l_ctr_properties_rec.attribute15 := ctr_prop_cur_rec.attribute15;
8393 
8394 
8395          CSI_Counter_PVT.Create_Ctr_Property
8396             (
8397              p_api_version       => p_api_version
8398              ,p_commit           => p_commit
8399              ,p_init_msg_list    => p_init_msg_list
8400              ,p_validation_level => l_validation_level
8401              ,p_ctr_properties_rec => l_ctr_properties_rec
8402              ,x_return_status     => x_return_status
8403              ,x_msg_count         => x_msg_count
8404              ,x_msg_data          => x_msg_data
8405              ,x_ctr_property_id   => l_new_ctr_prop_id
8406           );
8407 
8408       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
8409           l_msg_index := 1;
8410           l_msg_count := x_msg_count;
8411 
8412           WHILE l_msg_count > 0 LOOP
8413              x_msg_data := FND_MSG_PUB.GET
8414                            (l_msg_index,
8415                             FND_API.G_FALSE );
8416              csi_ctr_gen_utility_pvt.put_line( ' Error from CSI_Counter_PVT.Create_Ctr_Property');
8417              csi_ctr_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
8418              l_msg_index := l_msg_index + 1;
8419              l_msg_count := l_msg_count - 1;
8420           END LOOP;
8421           RAISE FND_API.G_EXC_ERROR;
8422        END IF;
8423 
8424       END LOOP;
8425 
8426       -- All the counter properties belonging to this counter have been
8427       -- instantiated.
8428 
8429    END LOOP;
8430    csi_ctr_gen_utility_pvt.put_line('After the end loop ');
8431    csi_ctr_gen_utility_pvt.put_line('Passed counter group id  = '||to_char(l_ctr_grp_rec.counter_group_id));
8432 
8433    FOR fmla_ctr_cur_rec IN fmla_ctr_cur(l_ctr_grp_rec.counter_group_id) LOOP
8434    csi_ctr_gen_utility_pvt.put_line('Formula Counter Id = '||to_char(fmla_ctr_cur_rec.counter_id));
8435       FOR ctr_der_filter_cur_rec IN ctr_der_filter_cur(fmla_ctr_cur_rec.counter_id)
8436       LOOP
8437  csi_ctr_gen_utility_pvt.put_line('Inside the ctr_der_filter_cur_rec ');
8438 
8439          SELECT counter_property_id
8440          INTO   l_new_der_ctr_prop_id
8441          FROM   csi_counter_template_vl ctr,
8442                 csi_ctr_prop_template_vl ctrprop
8443          WHERE  ctrprop.counter_property_id = ctr_der_filter_cur_rec.counter_property_id
8444          AND    ctrprop.counter_id = l_new_ctr_id;
8445 
8446          CSI_COUNTER_TEMPLATE_PVT.CREATE_DERIVED_FILTERS
8447             (
8448              p_api_version           => p_api_version
8449              ,p_commit               => p_commit
8450              ,p_init_msg_list        => p_init_msg_list
8451              ,p_validation_level     => l_validation_level
8452              ,p_ctr_derived_filters_tbl => l_ctr_derived_filters_tbl
8453              ,x_return_status         => x_return_status
8454              ,x_msg_count             => x_msg_count
8455              ,x_msg_data              => x_msg_data
8456             );
8457 
8458       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
8459           l_msg_index := 1;
8460           l_msg_count := x_msg_count;
8461 
8462           WHILE l_msg_count > 0 LOOP
8463              x_msg_data := FND_MSG_PUB.GET
8464                            (l_msg_index,
8465                             FND_API.G_FALSE );
8466              csi_ctr_gen_utility_pvt.put_line( ' Error from CSI_COUNTER_TEMPLATE_PVT.CREATE_DERIVED_FILTERS');
8467              csi_ctr_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
8468              l_msg_index := l_msg_index + 1;
8469              l_msg_count := l_msg_count - 1;
8470           END LOOP;
8471           RAISE FND_API.G_EXC_ERROR;
8472        END IF;
8473 
8474       END LOOP;
8475 
8476       csi_ctr_gen_utility_pvt.put_line('Created from ctr id = '||to_char(fmla_ctr_cur_rec.counter_id));
8477       BEGIN
8478          SELECT created_from_counter_tmpl_id
8479          INTO   l_created_map_id
8480          FROM   csi_counters_b
8481          WHERE  counter_id = fmla_ctr_cur_rec.counter_id;
8482       EXCEPTION
8483          WHEN OTHERS THEN NULL;
8484       END;
8485 
8486       -- FOR ctr_formula_bvars_cur_rec IN ctr_formula_bvars_cur(l_new_ctr_id)
8487       FOR ctr_formula_bvars_cur_rec IN ctr_formula_bvars_cur(l_created_map_id)
8488       LOOP
8489          -- Bug 9068223 Fixing in both R12 and 12.1 code
8490          --SELECT max(counter_id)
8491          --INTO   l_new_mapped_ctr_id
8492          --FROM   csi_counters_b
8493          --WHERE  created_from_counter_tmpl_id = ctr_formula_bvars_cur_rec.source_counter_id;
8494          -- AND    group_id = l_ctr_grp_rec.counter_group_id;
8495 
8496          -- To fetch the maximum counter id
8497          -- Bug 9864058 Fixing in both R12 and 12.1 code
8498 
8499          SELECT counter_id INTO l_new_mapped_ctr_id FROM
8500          (SELECT counter_id
8501          FROM   csi_counters_b
8502          WHERE  created_from_counter_tmpl_id = ctr_formula_bvars_cur_rec.source_counter_id
8503          AND group_id = l_ctr_grp_rec.counter_group_id
8504          ORDER BY counter_id DESC)
8505          WHERE ROWNUM = 1;
8506 
8507         -- Bug 11693116
8508        --The l_mapped_item_id derived in this part of code is not used anywhere & is causing performance issue.
8509        --so commenting the code
8510         /* IF ctr_formula_bvars_cur_rec.source_counter_id IS NOT NULL THEN
8511             -- THIS IS a configuration counter. I need to see if I can
8512             -- find out the CP to which the mapped_counter is attached.
8513             -- It may not be instantiated yet, so its possible that its
8514             -- not possible to get the mapped cp id. If i can't get it,
8515             -- set the formula_incompl_flag to 'N' for that counter.
8516 
8517             -- You cannot have configuration counters fro service items
8518             -- only for serviceable products. So, I can safely look at
8519             -- cs_customer_products and assume this is a product.
8520 
8521             FND_PROFILE.Get('ORG_ID',l_org_id);
8522 
8523             l_config_root_id := csi_generic_grp.config_root_node(p_source_object_id_instance,'COMPONENT-OF');
8524 
8525             BEGIN
8526                p_relationship_query_rec.RELATIONSHIP_ID := NULL;
8527                p_relationship_query_rec.RELATIONSHIP_TYPE_CODE := 'COMPONENT-OF';
8528                p_relationship_query_rec.OBJECT_ID := l_config_root_id;
8529                p_relationship_query_rec.SUBJECT_ID := NULL;
8530 
8531                -- Now call the stored program
8532                csi_ii_relationships_pub.get_relationships
8533                   (1.0,
8534                    '',
8535                    '',
8536                    NULL,
8537                    p_relationship_query_rec,
8538                    NULL,
8539                    NULL,
8540                    '',
8541                    x_relationship_tbl,
8542                    x_return_status,
8543                    x_msg_count,
8544                    x_msg_data);
8545 
8546 
8547                -- Output the results
8548                IF x_relationship_tbl IS NOT NULL THEN
8549                   IF x_relationship_tbl.count > 0 THEN
8550                      FOR i IN x_relationship_tbl.first..x_relationship_tbl.last LOOP
8551                         IF x_relationship_tbl.exists(i) THEN
8552                            --null; -- type of data not known
8553                            SELECT inventory_item_id
8554                            INTO   l_mapped_item_id
8555                            FROM   csi_item_instances
8556                            WHERE  instance_id = x_relationship_tbl(i).subject_id
8557                            AND    inv_master_organization_id = l_org_id;
8558 
8559                            --IF l_mapped_item_id = ctr_formula_bvars_cur_rec.mapped_inv_item_id then
8560                             --  exit;
8561                            --END IF;
8562                         END IF;
8563                      END LOOP;
8564                   END IF;
8565                END IF;
8566             EXCEPTION
8567                WHEN OTHERS THEN
8568                   -- l_mapped_item_id := ctr_formula_bvars_cur_rec.mapped_inv_item_id;
8569                   NULL;
8570             END;
8571 
8572          END IF;*/
8573 
8574          -- l_reading_type   := ctr_formula_bvars_cur_rec.reading_type;
8575          l_counter_relationships_rec.attribute1 := ctr_formula_bvars_cur_rec.attribute1;
8576          l_counter_relationships_rec.attribute2 := ctr_formula_bvars_cur_rec.attribute2;
8577          l_counter_relationships_rec.attribute3 := ctr_formula_bvars_cur_rec.attribute3;
8578          l_counter_relationships_rec.attribute4 := ctr_formula_bvars_cur_rec.attribute4;
8579          l_counter_relationships_rec.attribute5 := ctr_formula_bvars_cur_rec.attribute5;
8580          l_counter_relationships_rec.attribute6 := ctr_formula_bvars_cur_rec.attribute6;
8581          l_counter_relationships_rec.attribute7 := ctr_formula_bvars_cur_rec.attribute7;
8582          l_counter_relationships_rec.attribute8 := ctr_formula_bvars_cur_rec.attribute8;
8583          l_counter_relationships_rec.attribute9 := ctr_formula_bvars_cur_rec.attribute9;
8584          l_counter_relationships_rec.attribute10 := ctr_formula_bvars_cur_rec.attribute10;
8585          l_counter_relationships_rec.attribute11 := ctr_formula_bvars_cur_rec.attribute11;
8586          l_counter_relationships_rec.attribute12 := ctr_formula_bvars_cur_rec.attribute12;
8587          l_counter_relationships_rec.attribute13 := ctr_formula_bvars_cur_rec.attribute13;
8588          l_counter_relationships_rec.attribute14 := ctr_formula_bvars_cur_rec.attribute14;
8589          l_counter_relationships_rec.attribute15 := ctr_formula_bvars_cur_rec.attribute15;
8590          l_counter_relationships_rec.attribute_category := ctr_formula_bvars_cur_rec.attribute_category;
8591          l_counter_relationships_rec.security_group_id := ctr_formula_bvars_cur_rec.security_group_id;
8592          l_counter_relationships_rec.object_version_number := 1;
8593          l_counter_relationships_rec.last_update_date := sysdate;
8594          l_counter_relationships_rec.last_Updated_by  := fnd_global.user_id;
8595          l_counter_relationships_rec.creation_date := sysdate;
8596          l_counter_relationships_rec.created_by := FND_GLOBAL.user_id;
8597          l_counter_relationships_rec.last_update_login := FND_GLOBAL.user_id;
8598          l_counter_relationships_rec.ctr_association_id := ctr_formula_bvars_cur_rec.ctr_association_id;
8599          l_counter_relationships_rec.relationship_type_code := ctr_formula_bvars_cur_rec.relationship_type_code;
8600          -- l_counter_relationships_rec.source_counter_id := ctr_formula_bvars_cur_rec.source_counter_id;
8601          -- l_counter_relationships_rec.object_counter_id := ctr_formula_bvars_cur_rec.object_counter_id;
8602          l_counter_relationships_rec.source_counter_id := l_new_mapped_ctr_id;
8603          l_counter_relationships_rec.object_counter_id := fmla_ctr_cur_rec.counter_id;
8604          l_counter_relationships_rec.bind_variable_name := ctr_formula_bvars_cur_rec.bind_variable_name;
8605          l_counter_relationships_rec.factor := ctr_formula_bvars_cur_rec.factor;
8606          l_counter_relationships_rec.active_start_date := ctr_formula_bvars_cur_rec.active_start_date;
8607          l_counter_relationships_rec.active_end_date := ctr_formula_bvars_cur_rec.active_end_date;
8608 
8609          csi_counter_template_pvt.create_counter_relationship
8610             (
8611             p_api_version         => p_api_version
8612             ,p_commit             => fnd_api.g_false
8613             ,p_init_msg_list      => p_init_msg_list
8614             ,p_validation_level   => l_validation_level
8615             ,p_counter_relationships_rec => l_counter_relationships_rec
8616             ,x_return_status      => x_return_status
8617             ,x_msg_count          => x_msg_count
8618             ,x_msg_data           => x_msg_data
8619             );
8620 
8621        END LOOP;
8622    END LOOP;
8623 
8624    -- x_ctr_id_template := p_counter_id_template;
8625    -- x_ctr_id_instance := l_new_ctr_id;
8626 
8627    IF FND_API.to_Boolean(nvl(p_commit,FND_API.G_FALSE)) THEN
8628       COMMIT WORK;
8629    END IF;
8630 
8631    -- Standard call to get message count and IF count is  get message info.
8632    FND_MSG_PUB.Count_And_Get
8633       ( p_count  =>  x_msg_count,
8634         p_data   =>  x_msg_data
8635       );
8636 EXCEPTION
8637    WHEN FND_API.G_EXC_ERROR THEN
8638       x_return_status := FND_API.G_RET_STS_ERROR ;
8639       ROLLBACK TO instantiate_grp_counters;
8640       FND_MSG_PUB.Count_And_Get
8641          ( p_count => x_msg_count,
8642            p_data  => x_msg_data
8643          );
8644    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8645       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8646       ROLLBACK TO instantiate_grp_counters;
8647       FND_MSG_PUB.Count_And_Get
8648          ( p_count => x_msg_count,
8649            p_data  => x_msg_data
8650          );
8651    WHEN OTHERS THEN
8652       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8653       ROLLBACK TO  instantiate_grp_counters;
8654       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
8655          FND_MSG_PUB.Add_Exc_Msg
8656             ( G_PKG_NAME,
8657               l_api_name
8658             );
8659       END IF;
8660       FND_MSG_PUB.Count_And_Get
8661          ( p_count  => x_msg_count,
8662            p_data   => x_msg_data
8663          );
8664 END Instantiate_Grp_Counters;
8665 
8666 --|---------------------------------------------------
8667 --| procedure name: delete_item_association
8668 --| description :   procedure used to
8669 --|                 delete item association to
8670 --|                 counter group or counters
8671 --|---------------------------------------------------
8672 
8673 PROCEDURE delete_item_association
8674  (
8675      p_api_version               IN     NUMBER
8676     ,p_commit                    IN     VARCHAR2
8677     ,p_init_msg_list             IN     VARCHAR2
8678     ,p_validation_level          IN     NUMBER
8679     ,p_ctr_associations_id       IN     NUMBER
8680     ,x_return_status                OUT    NOCOPY VARCHAR2
8681     ,x_msg_count                    OUT    NOCOPY NUMBER
8682     ,x_msg_data                     OUT    NOCOPY VARCHAR2
8683  ) IS
8684    l_api_name                     CONSTANT VARCHAR2(30)   := 'DELETE_ITEM_ASSOCIATION';
8685    l_api_version                  CONSTANT NUMBER         := 1.0;
8686    l_msg_data                     VARCHAR2(2000);
8687    l_msg_index                    NUMBER;
8688    l_msg_count                    NUMBER;
8689    -- l_debug_level                  NUMBER;
8690 
8691    l_CTR_ASSOCIATIONS_ID	  NUMBER;
8692    l_instantiated_counter	  NUMBER;
8693    l_group_id	                  NUMBER;
8694    l_counter_id	                  NUMBER;
8695    l_inventory_item_id            NUMBER;
8696    l_associations_exists          VARCHAR2(1);
8697    l_associated_to_group          VARCHAR2(1);
8698 
8699    CURSOR get_item_details(p_group_id NUMBER,p_inv_id NUMBER) IS
8700    SELECT ctr_association_id, counter_id
8701    FROM   csi_ctr_item_associations
8702    WHERE  counter_id IS NOT NULL
8703    AND    associated_to_group = 'Y'
8704    AND    group_id  = p_group_id
8705    AND	  inventory_item_id = p_inv_id;
8706 
8707    CURSOR check_counters(p_counter_id NUMBER) IS
8708    SELECT counter_id instantiated_counter_id
8709    FROM   csi_counters_b
8710    WHERE  created_from_counter_tmpl_id = p_counter_id;
8711 BEGIN
8712    -- Standard Start of API savepoint
8713    SAVEPOINT  delete_item_association_pvt;
8714 
8715    -- Standard call to check for call compatibility.
8716    IF NOT FND_API.Compatible_API_Call (l_api_version,
8717                                        p_api_version,
8718                                        l_api_name   ,
8719                                        G_PKG_NAME   ) THEN
8720       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8721    END IF;
8722 
8723    -- Initialize message list if p_init_msg_list is set to TRUE.
8724    IF FND_API.to_Boolean(nvl(p_init_msg_list,FND_API.G_FALSE)) THEN
8725       FND_MSG_PUB.initialize;
8726    END IF;
8727 
8728    --  Initialize API return status to success
8729    x_return_status := FND_API.G_RET_STS_SUCCESS;
8730 
8731    -- Read the debug profiles values in to global variable 7197402
8732    CSI_CTR_GEN_UTILITY_PVT.read_debug_profiles;
8733 
8734    --
8735    IF (CSI_CTR_GEN_UTILITY_PVT.g_debug_level > 0) THEN
8736       csi_ctr_gen_utility_pvt.put_line
8737          ( 'delete_item_association_pvt'           ||'-'||
8738            p_api_version                              ||'-'||
8739            nvl(p_commit,FND_API.G_FALSE)              ||'-'||
8740            nvl(p_init_msg_list,FND_API.G_FALSE)       ||'-'||
8741            nvl(p_validation_level,FND_API.G_VALID_LEVEL_FULL) );
8742    END IF;
8743 
8744    if p_ctr_associations_id = FND_API.G_MISS_NUM then
8745       l_ctr_associations_id := null;
8746    else
8747       l_ctr_associations_id := p_ctr_associations_id;
8748    end if;
8749 
8750  csi_ctr_gen_utility_pvt.put_line('Inside delete Item association = '||to_char(l_ctr_associations_id));
8751    /* Start of validation */
8752    BEGIN
8753       SELECT associated_to_group, group_id, counter_id, inventory_item_id
8754       INTO   l_associated_to_group, l_group_id, l_counter_id, l_inventory_item_id
8755       FROM   csi_ctr_item_associations
8756       WHERE  ctr_association_id = l_ctr_associations_id;
8757 
8758       IF l_associated_to_group = 'Y' THEN
8759          FOR get_item_rec in get_item_details(l_group_id,l_inventory_item_id) LOOP
8760             FOR get_ctr_rec in check_counters(get_item_rec.counter_id) LOOP
8761                /* Verify if counter association exists */
8762                BEGIN
8763                   SELECT 'x'
8764                   INTO   l_associations_exists
8765                   FROM   csi_counter_associations cca,csi_item_instances cii
8766                   WHERE  cca.counter_id = get_ctr_rec.instantiated_counter_id
8767                   AND cca.SOURCE_OBJECT_ID=cii.instance_id
8768                   AND cii.inventory_item_id=l_inventory_item_id;
8769 
8770                   CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_ITEM_DEL_NOTALLOW');
8771                EXCEPTION
8772                   WHEN NO_DATA_FOUND THEN
8773                      NULL;
8774                   WHEN OTHERS THEN
8775                      CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_ITEM_DEL_NOTALLOW');
8776                END;
8777             END LOOP;
8778          END LOOP;
8779 
8780          /* Now Delete the data that was verified */
8781          FOR get_item_rec in get_item_details(l_group_id,l_inventory_item_id) LOOP
8782              /* Call the table Handler */
8783              CSI_CTR_ITEM_ASSOCIATIONS_PKG.Delete_Row
8784                  (p_CTR_ASSOCIATION_ID  => get_item_rec.ctr_association_id);
8785 
8786              IF NOT (x_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
8787                  ROLLBACK TO delete_item_association_pvt;
8788                  RETURN;
8789              END IF;
8790          END LOOP;
8791 
8792          /* Now Delete the main group-item association */
8793          /* Call the table Handler */
8794          CSI_CTR_ITEM_ASSOCIATIONS_PKG.Delete_Row
8795              (p_CTR_ASSOCIATION_ID  => l_ctr_associations_id);
8796 
8797          IF NOT (x_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
8798             ROLLBACK TO delete_item_association_pvt;
8799             RETURN;
8800          END IF;
8801       ELSE
8802          FOR get_ctr_rec in check_counters(l_counter_id) LOOP
8803             /* Verify if counter association exists */
8804             BEGIN
8805                SELECT 'x'
8806                INTO   l_associations_exists
8807                FROM   csi_counter_associations cca,csi_item_instances cii
8808                   WHERE  cca.counter_id = get_ctr_rec.instantiated_counter_id
8809                   AND cca.SOURCE_OBJECT_ID=cii.instance_id
8810                   AND cii.inventory_item_id=l_inventory_item_id;
8811                CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_ITEM_DEL_NOTALLOW');
8812             EXCEPTION
8813                WHEN NO_DATA_FOUND THEN
8814                   NULL;
8815                WHEN OTHERS THEN
8816                   CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_ITEM_DEL_NOTALLOW');
8817             END;
8818          END LOOP;
8819          /* Now Delete the data that was verified */
8820          /* Call the table Handler */
8821          CSI_CTR_ITEM_ASSOCIATIONS_PKG.Delete_Row
8822              (p_CTR_ASSOCIATION_ID  => l_ctr_associations_id);
8823 
8824          IF NOT (x_return_status = FND_API.G_RET_STS_SUCCESS ) THEN
8825             ROLLBACK TO delete_item_association_pvt;
8826             RETURN;
8827          END IF;
8828       END IF;
8829 
8830    EXCEPTION
8831       WHEN OTHERS THEN
8832          CSI_CTR_GEN_UTILITY_PVT.ExitWithErrMsg('CSI_API_CTR_ITEM_DEL_NOTALLOW');
8833    END;
8834 
8835    IF FND_API.to_Boolean(nvl(p_commit,FND_API.G_FALSE)) THEN
8836       COMMIT WORK;
8837    END IF;
8838 
8839    -- Standard call to get message count and IF count is  get message info.
8840    FND_MSG_PUB.Count_And_Get
8841       ( p_count  =>  x_msg_count,
8842         p_data   =>  x_msg_data
8843       );
8844 EXCEPTION
8845    WHEN FND_API.G_EXC_ERROR THEN
8846       x_return_status := FND_API.G_RET_STS_ERROR ;
8847       ROLLBACK TO delete_item_association_pvt;
8848       FND_MSG_PUB.Count_And_Get
8849          ( p_count => x_msg_count,
8850            p_data  => x_msg_data
8851          );
8852    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8853       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8854       ROLLBACK TO delete_item_association_pvt;
8855       FND_MSG_PUB.Count_And_Get
8856          ( p_count => x_msg_count,
8857            p_data  => x_msg_data
8858          );
8859    WHEN OTHERS THEN
8860       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8861       ROLLBACK TO create_item_association_pvt;
8862       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
8863          FND_MSG_PUB.Add_Exc_Msg
8864             ( G_PKG_NAME,
8865               l_api_name
8866             );
8867       END IF;
8868       FND_MSG_PUB.Count_And_Get
8869          ( p_count  => x_msg_count,
8870            p_data   => x_msg_data
8871          );
8872 END delete_item_association;
8873 
8874 END CSI_COUNTER_TEMPLATE_PVT;