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