DBA Data[Home] [Help]

PACKAGE BODY: APPS.OZF_ALLOCATION_ENGINE_PVT

Source


1 PACKAGE BODY OZF_ALLOCATION_ENGINE_PVT AS
2 /* $Header: ozfvaegb.pls 120.6 2006/03/08 02:40:54 inanaiah ship $  */
3 -- g_version    CONSTANT CHAR(80) := '$Header: ozfvaegb.pls 120.6 2006/03/08 02:40:54 inanaiah ship $';
4    g_pkg_name   CONSTANT VARCHAR2(30):='OZF_ALLOCATION_ENGINE_PVT';
5 
6    OZF_DEBUG_HIGH_ON CONSTANT BOOLEAN   := FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_debug_high);
7    OZF_DEBUG_MEDIUM_ON CONSTANT BOOLEAN := FND_MSG_PUB.CHECK_MSG_LEVEL(FND_MSG_PUB.G_MSG_LVL_DEBUG_MEDIUM);
8    OZF_DEBUG_LOW_ON CONSTANT BOOLEAN    := FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_debug_low);
9    G_DEBUG_LEVEL BOOLEAN := FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_debug_high);
10 
11    g_phase           VARCHAR2(500);
12    g_user_id         NUMBER;
13    g_login_id        NUMBER;
14 
15    TYPE G_GenericCurType IS REF CURSOR;
16 
17    OZF_TP_INVALID_PARAM          EXCEPTION;
18    OZF_TP_BLANK_PERIOD_TBL       EXCEPTION;
19    OZF_TP_DIFF_TIME_SPREAD       EXCEPTION;
20    OZF_TP_OPER_NOT_ALLOWED       EXCEPTION;
21    OZF_TP_CHG_PS_NOT_ALLOWED     EXCEPTION;
22    OZF_TP_ADDITEM_NOT_ALLOWED    EXCEPTION;
23    OZF_TP_DELITEM_NOT_ALLOWED    EXCEPTION;
24 
25 
26 -- ------------------------
27 -- Private Function
28 -- ------------------------
29 -- ------------------------------------------------------------------
30 -- Name: Generate new Time allocation ID from SEQ
31 -- Desc:
32 --
33 --
34 --
35 -- -----------------------------------------------------------------
36  FUNCTION get_time_allocation_id
37    RETURN NUMBER IS
38    l_api_version   CONSTANT NUMBER       := 1.0;
39    l_api_name      CONSTANT VARCHAR2(30) := 'get_time_allocation_id';
40    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
41 
42    CURSOR time_seq_csr IS
43           SELECT  ozf_time_allocations_s.NEXTVAL
44           FROM DUAL;
45 
46    CURSOR time_alloc_count_csr(p_time_alloc_id in number) IS
47           SELECT count(t.time_allocation_id)
48           FROM   ozf_time_allocations t
49           WHERE  t.time_allocation_id = p_time_alloc_id;
50 
51    l_count number := -1;
52    l_time_alloc_id  number := -1;
53 
54   BEGIN
55 
56    --OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- start');
57 
58    OPEN time_seq_csr;
59    FETCH time_seq_csr INTO l_time_alloc_id;
60    CLOSE time_seq_csr;
61 
62    LOOP
63     OPEN time_alloc_count_csr(l_time_alloc_id);
64     FETCH time_alloc_count_csr into l_count;
65     CLOSE time_alloc_count_csr;
66 
67     EXIT WHEN l_count = 0;
68 
69     OPEN time_seq_csr;
70     FETCH time_seq_csr INTO l_time_alloc_id;
71     CLOSE time_seq_csr;
72 
73    END LOOP;
74 
75    return l_time_alloc_id;
76 
77    EXCEPTION
78      WHEN OTHERS THEN
79           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
80   END get_time_allocation_id;
81 
82 
83 
84 -- ------------------------
85 -- Private Function
86 -- ------------------------
87 -- ------------------------------------------------------------------
88 -- Name: Generate new Product allocation ID from SEQ
89 -- Desc:
90 --
91 --
92 --
93 -- -----------------------------------------------------------------
94  FUNCTION get_product_allocation_id
95    RETURN NUMBER IS
96    l_api_version   CONSTANT NUMBER       := 1.0;
97    l_api_name      CONSTANT VARCHAR2(30) := 'get_product_allocation_id';
98    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
99 
100    CURSOR product_seq_csr IS
101           SELECT  ozf_product_allocations_s.NEXTVAL
102           FROM DUAL;
103 
104    CURSOR product_alloc_count_csr(p_product_alloc_id in number) IS
105           SELECT count(p.product_allocation_id)
106           FROM   ozf_product_allocations p
107           WHERE  p.product_allocation_id = p_product_alloc_id;
108 
109    l_count number := -1;
110    l_product_alloc_id  number := -1;
111 
112   BEGIN
113 
114    --OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- start');
115 
116    OPEN product_seq_csr;
117    FETCH product_seq_csr INTO l_product_alloc_id;
118    CLOSE product_seq_csr;
119 
120    LOOP
121     OPEN product_alloc_count_csr(l_product_alloc_id);
122     FETCH product_alloc_count_csr into l_count;
123     CLOSE product_alloc_count_csr;
124 
125     EXIT WHEN l_count = 0;
126 
127     OPEN product_seq_csr;
128     FETCH product_seq_csr INTO l_product_alloc_id;
129     CLOSE product_seq_csr;
130 
131    END LOOP;
132 
133    return l_product_alloc_id;
134 
135    EXCEPTION
136      WHEN OTHERS THEN
137           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
138   END get_product_allocation_id;
139 
140 
141 
142 
143 -- ------------------------
144 -- Private Function
145 -- ------------------------
146 -- ------------------------------------------------------------------
147 -- Name: Generate new Account allocation ID from SEQ
148 -- Desc:
149 --
150 --
151 --
152 -- -----------------------------------------------------------------
153  FUNCTION get_account_allocation_id
154    RETURN NUMBER IS
155    l_api_version   CONSTANT NUMBER       := 1.0;
156    l_api_name      CONSTANT VARCHAR2(30) := 'get_account_allocation_id';
157    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
158 
159    CURSOR account_seq_csr IS
160           SELECT  ozf_account_allocations_s.NEXTVAL
161           FROM DUAL;
162 
163    CURSOR account_alloc_count_csr(p_account_alloc_id in number) IS
164           SELECT count(account_allocation_id)
165           FROM   ozf_account_allocations
166           WHERE  account_allocation_id = p_account_alloc_id;
167 
168    l_count number := -1;
169    l_account_alloc_id  number := -1;
170 
171   BEGIN
172 
173    --OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- start');
174 
175    OPEN account_seq_csr;
176    FETCH account_seq_csr INTO l_account_alloc_id;
177    CLOSE account_seq_csr;
178 
179    LOOP
180     OPEN account_alloc_count_csr(l_account_alloc_id);
181     FETCH account_alloc_count_csr into l_count;
182     CLOSE account_alloc_count_csr;
183 
184     EXIT WHEN l_count = 0;
185 
186     OPEN account_seq_csr;
187     FETCH account_seq_csr INTO l_account_alloc_id;
188     CLOSE account_seq_csr;
189 
190    END LOOP;
191 
192    return l_account_alloc_id;
193 
194    EXCEPTION
195      WHEN OTHERS THEN
196           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
197   END get_account_allocation_id;
198 
199 
200 -- ------------------------
201 -- Public Function
202 -- ------------------------
203 -- ------------------------------------------------------------------
204 -- Name: get_LYSP_Sales
205 -- Desc: Called from Product Spread UI and from private apis.
206 --       This function will calculate and return LYSP sales of the newly
207 --       added Product or Category on the Product Spread UI for ROOT fund
208 --       for or any ShipTo Customer
209 -- Note: Distinct object types are = { ROOT, CUST }
210 -- ------------------------------------------------------------------
211  FUNCTION GET_SALES
212  (
213     p_object_type        IN          VARCHAR2,
214     p_object_id          IN          NUMBER,
215     p_item_id            IN          NUMBER,
216     p_item_type          IN          VARCHAR2,
217     p_time_id            IN          NUMBER
218  ) RETURN NUMBER IS
219 
220    l_api_version   CONSTANT NUMBER       := 1.0;
221    l_api_name      CONSTANT VARCHAR2(30) := 'get_lysp_sales';
222    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
223    l_return_status          VARCHAR2(1);
224 
225    l_object_type            VARCHAR2(100);
226    l_object_id              NUMBER;
227    l_item_id                NUMBER;
228    l_item_type              VARCHAR2(100);
229    l_time_id                NUMBER;
230 
231    l_fund_id                NUMBER;
232    l_site_use_id            NUMBER;
233    l_territory_id           NUMBER;
234    l_lysp_sales             NUMBER;
235 
236 
237 CURSOR account_spread_csr
238        (l_account_allocation_id  number) IS
239    SELECT
240        a.allocation_for_id,
241        a.site_use_id
242    FROM
243        ozf_account_allocations a
244    WHERE
245        a.account_allocation_id = l_account_allocation_id;
246 
247 l_account_spread_rec account_spread_csr%ROWTYPE;
248 
249 CURSOR territory_csr_1 (l_fund_id  NUMBER) IS
250  SELECT
251   fund.node_id territory_id
252  FROM
253   ozf_funds_all_vl fund
254  WHERE
255      fund.fund_id = l_fund_id;
256 
257 CURSOR territory_csr (l_fund_id  NUMBER) IS
258  SELECT
259   j.terr_id territory_id
260  FROM
261   ozf_funds_all_vl fund, jtf_terr_rsc_all j, jtf_terr_rsc_access_all j2
262  WHERE
263      fund.fund_id = l_fund_id
264  AND j.resource_id = fund.owner
265 -- AND j.primary_contact_flag = 'Y' ;
266  AND j2.terr_rsc_id = j.terr_rsc_id
267  AND j2.access_type = 'OFFER'
268  AND j2.trans_access_code = 'PRIMARY_CONTACT';
269 
270 CURSOR product_lysp_sales_csr
271                         (l_product_id    NUMBER,
272              l_territory_id  NUMBER,
273              l_site_use_id   NUMBER,
274              l_time_id       NUMBER) IS
275  SELECT
276    SUM(bsmv.sales) sales
277  FROM
278    ozf_order_sales_v bsmv,
279    ams_party_market_segments a
280  WHERE
281      a.market_qualifier_reference = l_territory_id
282  AND a.market_qualifier_type='TERRITORY'
283  AND a.site_use_id = NVL(l_site_use_id, a.site_use_id)
284  AND bsmv.ship_to_site_use_id = a.site_use_id
285  AND bsmv.inventory_item_id = l_product_id
286  AND bsmv.time_id = l_time_id;
287 
288 CURSOR fund_category_lysp_sales_csr
289                                (l_category_id    NUMBER,
290                                 l_territory_id   NUMBER,
291                                 l_time_id        NUMBER,
292                                 l_fund_id        NUMBER) IS
293  SELECT
294   SUM(bsmv.sales) sales
295  FROM
296   ozf_order_sales_v bsmv,
297   ams_party_market_segments a
298  WHERE
299      a.market_qualifier_reference = l_territory_id
300  AND a.market_qualifier_type='TERRITORY'
301  AND bsmv.ship_to_site_use_id = a.site_use_id
302  AND bsmv.time_id = l_time_id
303  AND bsmv.inventory_item_id IN
304                              ( SELECT DISTINCT MIC.INVENTORY_ITEM_ID
305                                FROM   MTL_ITEM_CATEGORIES     MIC,
306                                       ENI_PROD_DENORM_HRCHY_V DENORM
307                                WHERE  MIC.CATEGORY_SET_ID  = DENORM.CATEGORY_SET_ID
308                                 AND   MIC.CATEGORY_ID      = DENORM.CHILD_ID
309                                 AND   DENORM.PARENT_ID     = l_category_id
310                               MINUS
311                               SELECT a.inventory_item_id
312                               FROM  ams_act_products a
313                               WHERE act_product_used_by_id = l_fund_id
314                                AND arc_act_product_used_by = 'FUND'
315                                AND level_type_code = 'PRODUCT'
316                                AND excluded_flag IN  ('Y', 'N')
317                             );
318 
319 CURSOR cust_category_lysp_sales_csr
320                          (l_category_id    NUMBER,
321               l_territory_id   NUMBER,
322               l_site_use_id    NUMBER,
323               l_time_id        NUMBER,
324               l_fund_id        NUMBER) IS
325  SELECT
326    SUM(bsmv.sales) sales
327  FROM
328    ozf_order_sales_v bsmv,
329    ams_party_market_segments a
330  WHERE
331      a.market_qualifier_reference = l_territory_id
332  AND a.market_qualifier_type='TERRITORY'
333  AND a.site_use_id = l_site_use_id
334  AND bsmv.ship_to_site_use_id = a.site_use_id
335  AND bsmv.time_id = l_time_id
336  AND bsmv.inventory_item_id IN
337                       (SELECT DISTINCT MIC.INVENTORY_ITEM_ID
338                        FROM   MTL_ITEM_CATEGORIES     MIC,
339                               ENI_PROD_DENORM_HRCHY_V DENORM
340                        WHERE  MIC.CATEGORY_SET_ID  = DENORM.CATEGORY_SET_ID
341                         AND   MIC.CATEGORY_ID      = DENORM.CHILD_ID
342                         AND   DENORM.PARENT_ID     = l_category_id
343                MINUS
344                SELECT p.item_id
345                FROM   ozf_product_allocations p
346                WHERE  p.fund_id = l_fund_id
347               AND p.item_type = 'PRICING_ATTRIBUTE1'
348              );
349 
350 CURSOR fund_others_lysp_sales_csr
351                              (l_territory_id   NUMBER,
352                               l_time_id        NUMBER,
353                               l_fund_id        NUMBER) IS
354  SELECT
355   SUM(bsmv.sales) sales
356  FROM
357    ozf_order_sales_v bsmv,
358    ams_party_market_segments a
359  WHERE
360       a.market_qualifier_reference = l_territory_id
361   AND a.market_qualifier_type='TERRITORY'
362   AND bsmv.ship_to_site_use_id = a.site_use_id
363   AND bsmv.time_id = l_time_id
364   AND NOT EXISTS
365   (
366   ( SELECT prod.inventory_item_id
367     FROM ams_act_products prod
368     WHERE
369         prod.level_type_code = 'PRODUCT'
370     AND prod.arc_act_product_used_by = 'FUND'
371     AND prod.act_product_used_by_id = l_fund_id
372     AND prod.excluded_flag = 'N'
373     AND prod.inventory_item_id = bsmv.inventory_item_id
374     UNION ALL
375     SELECT MIC.INVENTORY_ITEM_ID
376     FROM   MTL_ITEM_CATEGORIES MIC,
377            ENI_PROD_DENORM_HRCHY_V DENORM,
378            AMS_ACT_PRODUCTS prod
379     WHERE
380           prod.level_type_code = 'FAMILY'
381       AND prod.arc_act_product_used_by = 'FUND'
382       AND prod.act_product_used_by_id = l_fund_id
383       AND prod.excluded_flag = 'N'
384       AND prod.category_id = DENORM.PARENT_ID
385       AND MIC.CATEGORY_SET_ID = DENORM.CATEGORY_SET_ID
386       AND MIC.CATEGORY_ID = DENORM.CHILD_ID
387       AND MIC.INVENTORY_ITEM_ID = bsmv.inventory_item_id
388   )
389   MINUS
390   SELECT prod.inventory_item_id
391   FROM ams_act_products prod
392   where
393       prod.level_type_code = 'PRODUCT'
394   AND prod.arc_act_product_used_by = 'FUND'
395   AND prod.act_product_used_by_id = l_fund_id
396   AND prod.excluded_flag = 'Y'
397   AND prod.inventory_item_id = bsmv.inventory_item_id
398   );
399 
400 
401   CURSOR cust_others_lysp_sales_csr
402                                (l_territory_id   NUMBER,
403                                 l_site_use_id    NUMBER,
404                                 l_time_id        NUMBER,
405                                 l_fund_id        NUMBER) IS
406   SELECT
407    SUM(bsmv.sales) sales
408   FROM
409    ozf_order_sales_v bsmv,
410    ams_party_market_segments a
411   WHERE
412       a.market_qualifier_reference = l_territory_id
413   AND a.market_qualifier_type='TERRITORY'
414   AND a.site_use_id = l_site_use_id
415   AND bsmv.ship_to_site_use_id = a.site_use_id
416   AND bsmv.time_id = l_time_id
417   AND NOT EXISTS
418   (
419   ( SELECT p.item_id
420     FROM ozf_product_allocations p
421     WHERE
422         p.fund_id = l_fund_id
423     AND p.item_type = 'PRICING_ATTRIBUTE1'
424     AND p.item_id = bsmv.inventory_item_id
425     UNION ALL
426     SELECT MIC.INVENTORY_ITEM_ID
427     FROM   MTL_ITEM_CATEGORIES MIC,
428            ENI_PROD_DENORM_HRCHY_V DENORM,
429            OZF_PRODUCT_ALLOCATIONS p
430     WHERE p.FUND_ID = l_fund_id
431       AND p.ITEM_TYPE = 'PRICING_ATTRIBUTE2'
432       AND p.ITEM_ID = DENORM.PARENT_ID
433       AND MIC.CATEGORY_SET_ID = DENORM.CATEGORY_SET_ID
434       AND MIC.CATEGORY_ID = DENORM.CHILD_ID
435       AND MIC.INVENTORY_ITEM_ID = bsmv.inventory_item_id
436   )
437   MINUS
438   SELECT prod.inventory_item_id
439   FROM ams_act_products prod
440   where
441       prod.level_type_code = 'PRODUCT'
442   AND prod.arc_act_product_used_by = 'FUND'
443   AND prod.act_product_used_by_id = l_fund_id
444   AND prod.excluded_flag = 'Y'
445   AND prod.inventory_item_id = bsmv.inventory_item_id
446   );
447 
448  BEGIN
449 
450    l_object_type := p_object_type;
451    l_object_id := p_object_id;
452    l_item_id := p_item_id;
453    l_item_type := p_item_type;
454    l_time_id := p_time_id;
455 
456 /*
457    OZF_UTILITY_PVT.debug_message('API Parameters For:---->: ' || l_full_api_name);
458    OZF_UTILITY_PVT.debug_message('1. l_object_type------->: ' || l_object_type);
459    OZF_UTILITY_PVT.debug_message('2. l_object_id--------->: ' || l_object_id);
460    OZF_UTILITY_PVT.debug_message('3. l_item_id----------->: ' || l_item_id);
461    OZF_UTILITY_PVT.debug_message('4. l_item_type--------->: ' || l_item_type);
462    OZF_UTILITY_PVT.debug_message('5. l_time_id----------->: ' || l_time_id);
463 */
464 
465 
466    IF     l_object_type IN ('ROOT', 'CUST')
467       AND l_object_id > 0
468       AND l_item_type IN ('PRICING_ATTRIBUTE1','PRICING_ATTRIBUTE2', 'OTHERS')
469       AND l_time_id > 0
470    THEN
471      NULL;
472    ELSE
473      RAISE OZF_TP_INVALID_PARAM;
474    END IF;
475 
476    IF l_object_type = 'CUST'
477    THEN
478 
479      OPEN account_spread_csr(l_object_id);
480      FETCH account_spread_csr INTO l_account_spread_rec;
481      CLOSE account_spread_csr;
482      l_fund_id := l_account_spread_rec.allocation_for_id;
483      l_site_use_id := l_account_spread_rec.site_use_id;
484    ELSE
485      l_fund_id := l_object_id;
486      l_site_use_id := NULL;
487    END IF;
488 
489    OPEN territory_csr_1(l_fund_id);
490    FETCH territory_csr_1 INTO l_territory_id;
491    CLOSE territory_csr_1 ;
492 
493    IF l_territory_id IS NULL THEN
494       OPEN territory_csr(l_fund_id);
495       FETCH territory_csr INTO l_territory_id;
496       CLOSE territory_csr ;
497    END IF;
498 
499    l_lysp_sales := 0;
500 
501    IF l_object_type = 'ROOT'
502    THEN
503 
504        IF l_item_type = 'PRICING_ATTRIBUTE1'
505        THEN
506            OPEN product_lysp_sales_csr(l_item_id,
507                                        l_territory_id,
508                                        l_site_use_id,
509                                        l_time_id
510                                       );
511            FETCH product_lysp_sales_csr INTO l_lysp_sales;
512            CLOSE product_lysp_sales_csr;
513        ELSIF l_item_type = 'PRICING_ATTRIBUTE2'
514        THEN
515            OPEN fund_category_lysp_sales_csr
516                                        (l_item_id,
517                                         l_territory_id,
518                                         l_time_id,
519                                         l_fund_id
520                                        );
521            FETCH fund_category_lysp_sales_csr INTO l_lysp_sales;
522            CLOSE fund_category_lysp_sales_csr;
523        ELSIF l_item_type = 'OTHERS'
524        THEN
525         OPEN fund_others_lysp_sales_csr
526                                   (l_territory_id,
527                                    l_time_id,
528                                    l_fund_id
529                                   );
530         FETCH fund_others_lysp_sales_csr INTO l_lysp_sales;
531         CLOSE fund_others_lysp_sales_csr;
532        END IF;
533    ELSIF l_object_type = 'CUST'
534    THEN
535 
536        IF l_item_type = 'PRICING_ATTRIBUTE1'
537        THEN
538            OPEN product_lysp_sales_csr(l_item_id,
539                                        l_territory_id,
540                                        l_site_use_id,
541                                        l_time_id
542                                       );
543            FETCH product_lysp_sales_csr INTO l_lysp_sales;
544            CLOSE product_lysp_sales_csr;
545        ELSIF l_item_type = 'PRICING_ATTRIBUTE2'
546        THEN
547            OPEN cust_category_lysp_sales_csr
548                                        (l_item_id,
549                                         l_territory_id,
550                                         l_site_use_id,
551                                         l_time_id,
552                                         l_fund_id
553                                        );
554            FETCH cust_category_lysp_sales_csr INTO l_lysp_sales;
555            CLOSE cust_category_lysp_sales_csr;
556        ELSIF l_item_type = 'OTHERS'
557        THEN
558         OPEN cust_others_lysp_sales_csr
559                                   (l_territory_id,
560                                    l_site_use_id,
561                                    l_time_id,
562                                    l_fund_id
563                                   );
564         FETCH cust_others_lysp_sales_csr INTO l_lysp_sales;
565         CLOSE cust_others_lysp_sales_csr;
566        END IF;
567    END IF;
568 
569    l_lysp_sales := NVL(l_lysp_sales, 0);
570 
571    RETURN l_lysp_sales;
572 
573  EXCEPTION
574      WHEN OTHERS THEN
575           RETURN NULL;
576  END GET_SALES;
577 
578 
579 
580 -- ------------------------
581 -- Private Procedure
582 -- ------------------------
583 -- ------------------------------------------------------------------
584 -- Name: raise_business_event
585 -- Desc: This prodcedure will fix RAISE Business events for Targets.
586 --       1.
587 --       2.
588 --       3.
589 -- -----------------------------------------------------------------
590 PROCEDURE raise_business_event(p_object_id IN NUMBER)
591  IS
592 
593   l_item_key        varchar2(30);
594   l_event_name      varchar2(80);
595   l_parameter_list  wf_parameter_list_t;
596 
597  BEGIN
598   l_item_key       := p_object_id ||'_'|| TO_CHAR(SYSDATE,'DDMMRRRRHH24MISS');
599   l_parameter_list := WF_PARAMETER_LIST_T();
600   l_event_name     :=  'oracle.apps.ozf.quota.TargetApproval';
601 
602   ozf_utility_pvt.debug_message('-> Raising Bussiness EVENT for Account Allocation Id == '||p_object_id||' ; ' );
603   ozf_utility_pvt.debug_message('-> ITEM KEY == '||l_item_key||' ; ' );
604 
605   wf_event.AddParameterToList(p_name           => 'P_ACCOUNT_ALLOCATION_ID',
606                               p_value          => p_object_id,
607                               p_parameterlist  => l_parameter_list);
608 
609   wf_event.raise( p_event_name => l_event_name,
610                   p_event_key  => l_item_key,
611                   p_parameters => l_parameter_list);
612 
613 
614  EXCEPTION
615    WHEN OTHERS THEN
616         RAISE Fnd_Api.g_exc_error;
617         ozf_utility_pvt.debug_message('Exception in raising business event for Target Approval');
618 END;
619 
620 
621 
622 
623 
624 
625 
626 
627 -- ------------------------
628 -- Private Procedure
629 -- ------------------------
630 -- ------------------------------------------------------------------
631 -- Name: fix product rounding err
632 -- Desc: This prodcedure will fix product rounding error for
633 --       1. Root Product Spread
634 --       2. Fact Product Spread
635 --       3. Ship-To Product Spread
636 -- -----------------------------------------------------------------
637  PROCEDURE fix_product_rounding_err
638  (
639     p_object_type        IN          VARCHAR2,
640     p_object_id          IN          NUMBER,
641     p_diff_target        IN          NUMBER
642  ) IS
643 
644    l_api_name      CONSTANT VARCHAR2(30) := 'fix_product_rounding_err';
645    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
646 
647    l_object_type            VARCHAR2(30) := null;
648    l_object_id              NUMBER;
649    l_diff_target            NUMBER;
650    l_temp_product_allocation_id NUMBER;
651 
652 
653  BEGIN
654 
655    l_object_type := p_object_type;
656    l_object_id   := p_object_id;
657    l_diff_target := p_diff_target;
658 
659 
660    IF     l_object_type IN ('FACT', 'CUST', 'FUND')
661       AND l_object_id > 0
662       AND l_diff_target <> 0
663    THEN
664 
665 
666             l_temp_product_allocation_id := 0;
667 
668             UPDATE OZF_TIME_ALLOCATIONS t
669                 SET t.TARGET = t.TARGET + l_diff_target,
670                     t.object_version_number = t.object_version_number + 1,
671                     t.last_update_date = SYSDATE,
672                     t.last_updated_by = FND_GLOBAL.USER_ID,
673                     t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
674               WHERE t.time_allocation_id = (SELECT max(x.time_allocation_id)
675                                             FROM OZF_TIME_ALLOCATIONS x
676                                             WHERE  x.allocation_for = 'PROD'
677                                             AND x.allocation_for_id IN ( SELECT max(p.product_allocation_id)
678                                                                          FROM  OZF_PRODUCT_ALLOCATIONS p
679                                                                          WHERE p.allocation_for = l_object_type
680                                                                            AND p.allocation_for_id = l_object_id
681                                                                            AND p.target =
682                                                                               (SELECT max(xz.target)
683                                                                                FROM OZF_PRODUCT_ALLOCATIONS xz
684                                                                                WHERE xz.allocation_for = l_object_type
685                                                                                AND xz.allocation_for_id = l_object_id
686                                                                                )
687 
688                                                                          )
689                                            AND x.target =
690                                                (SELECT max(zx.target)
691                                                 FROM OZF_TIME_ALLOCATIONS zx
692                                                 WHERE  zx.allocation_for = 'PROD'
693                                                 AND zx.allocation_for_id IN (SELECT max(pz.product_allocation_id)
694                                                                              FROM  OZF_PRODUCT_ALLOCATIONS pz
695                                                                              WHERE pz.allocation_for = l_object_type
696                                                                                AND pz.allocation_for_id = l_object_id
697                                                                                AND pz.target =
698                                                                                 (SELECT max(xz.target)
699                                                                                  FROM OZF_PRODUCT_ALLOCATIONS xz
700                                                                                 WHERE xz.allocation_for = l_object_type
701                                                                                 AND xz.allocation_for_id = l_object_id
702                                                                                 )
703 
704                                                                              )
705                                                )
706                                            )
707               RETURNING t.allocation_for_id INTO l_temp_product_allocation_id;
708 
709 
710               IF (SQL%NOTFOUND) THEN
711                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
712                     fnd_message.set_name ('OZF', 'OZF_TP_ROUNDING_TIME_ERR');
713                     fnd_message.set_token('OZF_TP_OBJECT_TYPE_TOKEN',l_object_type);
714                     fnd_message.set_token('OZF_TP_OBJECT_ID_TOKEN',l_object_id);
715                     fnd_msg_pub.ADD;
716                  END IF;
717                  RAISE fnd_api.g_exc_unexpected_error;
718               END IF;
719 
720          UPDATE OZF_PRODUCT_ALLOCATIONS p
721                 SET p.TARGET = p.TARGET + l_diff_target,
722                     p.object_version_number = p.object_version_number + 1,
723                     p.last_update_date = SYSDATE,
724                     p.last_updated_by = FND_GLOBAL.USER_ID,
725                     p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
726               WHERE p.product_allocation_id = l_temp_product_allocation_id;
727 
728               IF (SQL%NOTFOUND) THEN
729                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
730                     fnd_message.set_name ('OZF', 'OZF_TP_ROUNDING_PROD_ERR');
731                     fnd_message.set_token('OZF_TP_OBJECT_TYPE_TOKEN',l_object_type);
732                     fnd_message.set_token('OZF_TP_OBJECT_ID_TOKEN',l_object_id);
733                     fnd_msg_pub.ADD;
734                  END IF;
735                  RAISE fnd_api.g_exc_unexpected_error;
736               END IF;
737 
738 /*---------------------------------------------------------------------------------------------------
739             UPDATE OZF_TIME_ALLOCATIONS t
740                 SET t.TARGET = t.TARGET + l_diff_target,
741                     t.object_version_number = t.object_version_number + 1,
742                     t.last_update_date = SYSDATE,
743                     t.last_updated_by = FND_GLOBAL.USER_ID,
744                     t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
745               WHERE t.time_allocation_id = (SELECT max(x.time_allocation_id) from OZF_TIME_ALLOCATIONS x
746                                             WHERE  x.allocation_for = 'PROD'
747                                             AND x.allocation_for_id IN ( SELECT p.product_allocation_id
748                                                                          FROM  OZF_PRODUCT_ALLOCATIONS p
749                                                                          WHERE p.allocation_for = 'CUST'
750                                                                            AND p.allocation_for_id = l_account_allocation_id
751                                                                            AND p.item_id = -9999 )
752                                            );
753 
754 
755               IF (SQL%NOTFOUND) THEN
756                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
757                     fnd_message.set_name ('OZF', 'OZF_TP_TIMEALLOC_NOT_FOUND_TXT');
758                     fnd_msg_pub.ADD;
759                  END IF;
760                  RAISE fnd_api.g_exc_unexpected_error;
761               END IF;
762 
763          UPDATE OZF_PRODUCT_ALLOCATIONS p
764                 SET p.TARGET = p.TARGET + l_diff_target,
765                     p.object_version_number = p.object_version_number + 1,
766                     p.last_update_date = SYSDATE,
767                     p.last_updated_by = FND_GLOBAL.USER_ID,
768                     p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
769               WHERE p.allocation_for = 'CUST'
770                 AND p.allocation_for_id = l_account_allocation_id
771         AND p.item_id = -9999;
772 
773               IF (SQL%NOTFOUND) THEN
774                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
775                     fnd_message.set_name ('OZF', 'OZF_TP_PRODALLOC_NOT_FOUND_TXT');
776                     fnd_msg_pub.ADD;
777                  END IF;
778                  RAISE fnd_api.g_exc_unexpected_error;
779               END IF;
780 ---------------------------------------------------------------------------------------------------
781 */
782 
783 
784 
785    ELSE
786      null;
787    END IF;
788  END fix_product_rounding_err;
789 
790 
791 
792 
793 
794 
795 -- ------------------------
796 -- Private Procedure
797 -- ------------------------
798 -- ------------------------------------------------------------------
799 -- Name: create fact product spread
800 -- Desc: C O M M E N T I N G on May 17th, 2004.
801 --       Check the new Prodedure below.
802 --       Uncomment this if Root's Product Spread is to be considered
803 --       as the basis of creating Facts Product Spread.
804 -- -----------------------------------------------------------------
805  PROCEDURE create_old_fact_product_spread
806  (
807     p_api_version        IN          NUMBER,
808     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
809     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
810     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
811     x_return_status      OUT NOCOPY  VARCHAR2,
812     x_error_number       OUT NOCOPY  NUMBER,
813     x_error_message      OUT NOCOPY  VARCHAR2,
814     p_fact_id            IN          NUMBER
815  ) IS
816 
817    l_api_version   CONSTANT NUMBER       := 1.0;
818    l_api_name      CONSTANT VARCHAR2(30) := 'create_old_fact_product_spread';
819    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
820    l_return_status          VARCHAR2(1);
821 
822    l_object_version_number  NUMBER := 1;
823    l_org_id                 NUMBER := TO_NUMBER(SUBSTRB(USERENV('CLIENT_INFO'), 1, 10));
824    l_fact_id                NUMBER;
825    l_fund_id                NUMBER;
826    l_start_date             VARCHAR2(30) := null;
827    l_end_date               VARCHAR2(30) := null;
828    l_period_type_id         NUMBER;
829    l_in_clause              VARCHAR2(1000) := null;
830    l_lysp_in_clause         VARCHAR2(1000) := null;
831    l_total_target           NUMBER;
832    l_territory_id           NUMBER;
833    l_resource_id            NUMBER;
834    l_lysp_sales             NUMBER;
835    l_total_lysp_sales       NUMBER;
836    l_total_root_quota       NUMBER;
837    l_time_quota             NUMBER;
838    l_total_quota            NUMBER;
839    l_multiplying_factor     NUMBER;
840    l_product_allocation_id  NUMBER;
841    l_time_allocation_id     NUMBER;
842    l_diff_target            NUMBER;
843    l_diff_target_1          NUMBER;
844    l_diff_target_2          NUMBER;
845 
846    l_period_tbl          OZF_TIME_API_PVT.G_period_tbl_type;
847    l_lysp_period_tbl     OZF_TIME_API_PVT.G_period_tbl_type;
848 
849    p_prod_alloc_rec      ozf_product_allocations%ROWTYPE;
850    p_time_alloc_rec      ozf_time_allocations%ROWTYPE;
851 
852 
853   CURSOR fact_csr
854          (l_fact_id        number) IS
855   SELECT
856       activity_metric_fact_id,
857       act_metric_used_by_id,
858       arc_act_metric_used_by,
859       activity_metric_id,
860       hierarchy_id,
861       hierarchy_type,
862       node_id,
863       previous_fact_id,
864       recommend_total_amount,
865       status_code
866   FROM
867       OZF_ACT_METRIC_FACTS_ALL
868   WHERE
869       activity_metric_fact_id = l_fact_id;
870 
871   l_fact_rec      fact_csr%ROWTYPE;
872 
873   CURSOR allocation_csr
874          (l_allocation_id        number) IS
875   SELECT
876      activity_metric_id,
877      arc_act_metric_used_by,
878      act_metric_used_by_id,
879      product_spread_time_id  period_type_id,  -- (eg.. 32 for monthly, 64 for quarterly),
880      published_flag,
881      status_code,
882      start_period_name,
883      end_period_name,
884      from_date,
885      to_date
886   FROM
887       OZF_ACT_METRICS_ALL
888   WHERE
889       activity_metric_id = l_allocation_id;
890 
891   l_allocation_rec      allocation_csr%ROWTYPE;
892 
893 
894   CURSOR fund_csr (l_fund_id  NUMBER) IS
895    SELECT
896     owner,
897     start_period_id,
898     end_period_id,
899     start_date_active,
900     end_date_active,
901     status_code,
902     original_budget,
903     transfered_in_amt,
904     transfered_out_amt,
905     node_id, -- (=territory id)
906     product_spread_time_id period_type_id -- (= minor_scale_id i.e. qtrly or monthly)
907    FROM
908     ozf_funds_all_vl
909    WHERE
910     fund_id = l_fund_id;
911 
912   l_fund_rec    fund_csr%ROWTYPE;
913 
914 /*
915   CURSOR get_total_target_csr
916         (l_fund_id   NUMBER,
917      l_in_clause VARCHAR2) IS
918    SELECT SUM(t.target)
919    FROM
920        ozf_time_allocations t,
921        ozf_product_allocations p
922    WHERE
923        p.fund_id = l_fund_id
924    AND t.allocation_for_id   = p.product_allocation_id
925    AND t.allocation_for      = 'PROD'
926    AND t.time_id IN (l_in_clause);
927 */
928 
929   l_get_total_target_sql VARCHAR2(30000) :=
930    ' SELECT SUM(t.target) '||
931    ' FROM '||
932    '     ozf_time_allocations t,'||
933    '     ozf_product_allocations p'||
934    ' WHERE'||
935    '     p.fund_id = :l_fund_id'||
936    ' AND t.allocation_for_id   = p.product_allocation_id'||
937    ' AND t.allocation_for      = ''PROD'' '||
938    ' AND t.time_id IN (';
939 --l_in_clause);
940 
941   get_total_target_csr  G_GenericCurType;  --cursor variable (processed like a PL/SQL variable)
942 
943 
944    CURSOR root_product_spread_csr
945          (l_fund_id        number) IS
946     SELECT
947        p.product_allocation_id,
948        p.item_id,
949        p.item_type
950     FROM
951        ozf_product_allocations p
952     WHERE
953        p.fund_id = l_fund_id;
954 
955    l_root_product_rec     root_product_spread_csr%rowtype;
956 
957    CURSOR root_time_spread_csr
958          (l_product_allocation_id       number,
959           l_time_id                     number) IS
960    SELECT t.target
961    FROM
962        ozf_time_allocations t
963    WHERE
964        t.allocation_for_id = l_product_allocation_id
965    AND t.allocation_for = 'PROD'
966    AND t.time_id = l_time_id;
967 
968    l_root_time_rec     root_time_spread_csr%rowtype;
969 
970 
971   CURSOR product_lysp_sales_csr (l_product_id    NUMBER,
972                                  l_territory_id  NUMBER,
973                                  l_time_id       NUMBER) IS
974    SELECT
975     SUM(bsmv.sales) sales
976    FROM
977      ozf_order_sales_v bsmv,
978      ams_party_market_segments a
979    WHERE
980         a.market_qualifier_reference = l_territory_id
981     AND a.market_qualifier_type='TERRITORY'
982     AND a.site_use_id = bsmv.ship_to_site_use_id
983     AND bsmv.inventory_item_id = l_product_id
984     AND bsmv.time_id = l_time_id;
985 
986 
987   CURSOR category_lysp_sales_csr (l_category_id    NUMBER,
988                                   l_territory_id   NUMBER,
989                                   l_time_id        NUMBER,
990                                   l_fund_id        NUMBER) IS
991   SELECT
992    SUM(bsmv.sales) sales
993   FROM
994    ozf_order_sales_v bsmv,
995    ams_party_market_segments a
996   WHERE
997       a.market_qualifier_reference = l_territory_id
998   AND a.market_qualifier_type='TERRITORY'
999   AND bsmv.ship_to_site_use_id = a.site_use_id
1000   AND bsmv.time_id = l_time_id
1001   AND bsmv.inventory_item_id IN
1002                              ( SELECT DISTINCT MIC.INVENTORY_ITEM_ID
1003                                FROM   MTL_ITEM_CATEGORIES     MIC,
1004                                       ENI_PROD_DENORM_HRCHY_V DENORM
1005                                WHERE  MIC.CATEGORY_SET_ID  = DENORM.CATEGORY_SET_ID
1006                                 AND   MIC.CATEGORY_ID      = DENORM.CHILD_ID
1007                                 AND   DENORM.PARENT_ID     = l_category_id
1008                                MINUS
1009                                SELECT p.item_id
1010                                FROM   ozf_product_allocations p
1011                                WHERE  p.fund_id = l_fund_id
1012                                   AND p.item_type = 'PRICING_ATTRIBUTE1'
1013                              );
1014 
1015 
1016   CURSOR others_lysp_sales_csr (l_territory_id   NUMBER,
1017                                 l_time_id        NUMBER,
1018                                 l_fund_id        NUMBER) IS
1019   SELECT
1020    SUM(bsmv.sales) sales
1021   FROM
1022    ozf_order_sales_v bsmv,
1023    ams_party_market_segments a
1024   WHERE
1025       a.market_qualifier_reference = l_territory_id
1026   AND a.market_qualifier_type='TERRITORY'
1027   AND bsmv.ship_to_site_use_id = a.site_use_id
1028   AND bsmv.time_id = l_time_id
1029   AND NOT EXISTS
1030   (
1031   ( SELECT p.item_id
1032     FROM ozf_product_allocations p
1033     WHERE
1034         p.fund_id = l_fund_id
1035     AND p.item_type = 'PRICING_ATTRIBUTE1'
1036     AND p.item_id = bsmv.inventory_item_id
1037     UNION ALL
1038     SELECT MIC.INVENTORY_ITEM_ID
1039     FROM   MTL_ITEM_CATEGORIES MIC,
1040            ENI_PROD_DENORM_HRCHY_V DENORM,
1041            OZF_PRODUCT_ALLOCATIONS p
1042     WHERE p.FUND_ID = l_fund_id
1043       AND p.ITEM_TYPE = 'PRICING_ATTRIBUTE2'
1044       AND p.ITEM_ID = DENORM.PARENT_ID
1045       AND MIC.CATEGORY_SET_ID = DENORM.CATEGORY_SET_ID
1046       AND MIC.CATEGORY_ID = DENORM.CHILD_ID
1047       AND MIC.INVENTORY_ITEM_ID = bsmv.inventory_item_id
1048   )
1049   MINUS
1050   SELECT prod.inventory_item_id
1051   FROM ams_act_products prod
1052   where
1053       prod.level_type_code = 'PRODUCT'
1054   AND prod.arc_act_product_used_by = 'FUND'
1055   AND prod.act_product_used_by_id = l_fund_id
1056   AND prod.excluded_flag = 'Y'
1057   AND prod.inventory_item_id = bsmv.inventory_item_id
1058   );
1059 
1060 
1061 
1062  BEGIN
1063 
1064    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- start');
1065 
1066    -- Standard Start of API savepoint
1067    SAVEPOINT create_old_fact_product_spread;
1068 
1069    -- Standard call to check for call compatibility.
1070    IF NOT FND_API.compatible_api_call(l_api_version,
1071                                       p_api_version,
1072                                       l_api_name,
1073                                       g_pkg_name)
1074    THEN
1075      RAISE FND_API.g_exc_unexpected_error;
1076    END IF;
1077 
1078    -- Initialize message list if p_init_msg_list is set to TRUE.
1079    IF FND_API.to_Boolean( p_init_msg_list ) THEN
1080       FND_MSG_PUB.initialize;
1081    END IF;
1082 
1083    -- Initialize API return status to SUCCESS
1084    x_return_status := FND_API.g_ret_sts_success;
1085 
1086    l_fact_id := p_fact_id;
1087 
1088    OPEN fact_csr(l_fact_id);
1089    FETCH fact_csr INTO l_fact_rec;
1090    CLOSE fact_csr ;
1091 
1092    l_territory_id := l_fact_rec.node_id;
1093 
1094    OPEN allocation_csr(l_fact_rec.activity_metric_id);
1095    FETCH allocation_csr INTO l_allocation_rec;
1096    CLOSE allocation_csr ;
1097 
1098    l_fund_id := l_allocation_rec.act_metric_used_by_id; -- this is ROOT's Fund_id
1099    l_period_type_id := l_allocation_rec.period_type_id;
1100 
1101    OPEN fund_csr(l_fund_id);
1102    FETCH fund_csr INTO l_fund_rec;
1103    CLOSE fund_csr ;
1104 
1105    IF l_period_type_id <> l_fund_rec.period_type_id THEN
1106       RAISE OZF_TP_DIFF_TIME_SPREAD;
1107    END IF;
1108 
1109    l_start_date := to_char(l_allocation_rec.from_date, 'YYYY/MM/DD');
1110    l_end_date   := to_char(l_allocation_rec.to_date, 'YYYY/MM/DD');
1111 
1112    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Getting Time_ids Between '
1113                                                  ||l_start_date||' AND '||l_end_date||' ; '
1114                                                  ||' Period_Type_id = '||l_period_type_id||' ; ');
1115 
1116    IF l_start_date IS NULL OR
1117       l_end_date IS NULL OR
1118       l_period_type_id IS NULL
1119    THEN
1120       RAISE OZF_TP_BLANK_PERIOD_TBL;
1121    END IF;
1122 
1123    l_period_tbl := OZF_TIME_API_PVT.get_period_tbl
1124                                     (l_start_date,
1125                                      l_end_date,
1126                                      l_period_type_id);
1127 
1128 
1129    IF l_period_tbl IS NULL OR l_period_tbl.COUNT <= 0 THEN
1130       RAISE OZF_TP_BLANK_PERIOD_TBL;
1131    END IF;
1132 
1133    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Getting Time_ids Between '||l_start_date
1134                                          ||' AND '||l_end_date||' ; ');
1135    IF l_period_tbl IS NOT NULL THEN
1136     IF l_period_tbl.COUNT > 0 THEN
1137       FOR l_idx IN l_period_tbl.first..l_period_tbl.last
1138       LOOP
1139         IF l_period_tbl.exists(l_idx) THEN
1140 
1141          l_lysp_period_tbl(l_idx) := OZF_TIME_API_PVT.get_lysp_id (l_period_tbl(l_idx), l_period_type_id);
1142 
1143          OZF_UTILITY_PVT.debug_message(SubStr('l_period_tbl('||TO_CHAR(l_idx)||') = '
1144                                ||TO_CHAR(l_period_tbl(l_idx)), 1,255));
1145          OZF_UTILITY_PVT.debug_message(SubStr('l_lysp_period_tbl('||TO_CHAR(l_idx)||') = '
1146                                ||TO_CHAR(l_lysp_period_tbl(l_idx)), 1,255));
1147         END IF;
1148       END LOOP;
1149     END IF;
1150    END IF;
1151 
1152    --OZF_UTILITY_PVT.debug_message(' out of  lysp period table');
1153 
1154    --l_in_clause := '(';
1155    l_in_clause := NULL;
1156    IF l_period_tbl IS NOT NULL THEN
1157       FOR l_idx IN l_period_tbl.first..l_period_tbl.last
1158       LOOP
1159           IF l_in_clause IS NULL THEN
1160              l_in_clause := LTRIM(' '||l_period_tbl(l_idx));
1161           ELSE
1162              l_in_clause := l_in_clause ||','|| l_period_tbl(l_idx);
1163           END IF;
1164       END LOOP;
1165    END IF;
1166    --l_in_clause := l_in_clause||')';
1167 
1168    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': l_in_clause == '||l_in_clause);
1169 
1170    --l_lysp_in_clause := '(';
1171    l_lysp_in_clause := NULL;
1172    IF l_lysp_period_tbl IS NOT NULL THEN
1173       FOR l_idx IN l_lysp_period_tbl.first..l_lysp_period_tbl.last
1174       LOOP
1175           IF l_lysp_in_clause IS NULL THEN
1176              l_lysp_in_clause := LTRIM(' '||l_lysp_period_tbl(l_idx));
1177           ELSE
1178              l_lysp_in_clause := l_lysp_in_clause ||','|| l_lysp_period_tbl(l_idx);
1179           END IF;
1180       END LOOP;
1181    END IF;
1182    --l_lysp_in_clause := l_lysp_in_clause||')';
1183 
1184    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': l_lysp_in_clause == '||l_lysp_in_clause);
1185 
1186    l_get_total_target_sql := l_get_total_target_sql||l_in_clause ||')';
1187 
1188    OPEN get_total_target_csr FOR l_get_total_target_sql USING l_fund_id;
1189    FETCH get_total_target_csr INTO l_total_root_quota;
1190    CLOSE get_total_target_csr ;
1191 
1192 
1193    l_total_root_quota := NVL(l_total_root_quota, 0);
1194 
1195    IF l_total_root_quota > 0 THEN
1196       l_multiplying_factor :=  NVL(l_fact_rec.recommend_total_amount, 0) / l_total_root_quota;
1197    ELSE
1198       l_multiplying_factor := 0;
1199    END IF;
1200 
1201 
1202    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Multiplying Factor is '||  l_multiplying_factor);
1203 
1204 
1205 ------- Insert rows for PRODUCT and TIME Allocation Records for given FACT ------------------------------
1206 
1207 
1208    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Populating Product and Time Allocations Records'
1209                                          || 'FOR Fact_id = '|| l_fact_id || ' ; ');
1210 
1211    FOR root_product_rec IN root_product_spread_csr(l_fund_id)
1212    LOOP
1213 
1214        p_prod_alloc_rec := NULL;
1215 
1216        l_product_allocation_id := get_product_allocation_id;
1217 
1218        p_prod_alloc_rec.allocation_for := 'FACT';
1219        p_prod_alloc_rec.allocation_for_id := l_fact_id;
1220        p_prod_alloc_rec.item_type := root_product_rec.item_type;
1221        p_prod_alloc_rec.item_id := root_product_rec.item_id;
1222        p_prod_alloc_rec.selected_flag := 'N';
1223        p_prod_alloc_rec.target := 0;
1224        p_prod_alloc_rec.lysp_sales := 0;
1225 
1226 
1227        Ozf_Product_Allocations_Pkg.Insert_Row(
1228           px_product_allocation_id  => l_product_allocation_id,
1229           p_allocation_for  => p_prod_alloc_rec.allocation_for,
1230           p_allocation_for_id  => p_prod_alloc_rec.allocation_for_id,
1231           p_fund_id  => p_prod_alloc_rec.fund_id,
1232           p_item_type  => p_prod_alloc_rec.item_type,
1233           p_item_id  => p_prod_alloc_rec.item_id,
1234           p_selected_flag  => p_prod_alloc_rec.selected_flag,
1235           p_target  => NVL(p_prod_alloc_rec.target, 0),
1236           p_lysp_sales  => NVL(p_prod_alloc_rec.lysp_sales, 0),
1237           p_parent_product_allocation_id  => p_prod_alloc_rec.parent_product_allocation_id,
1238           px_object_version_number  => l_object_version_number,
1239           p_creation_date  => SYSDATE,
1240           p_created_by  => FND_GLOBAL.USER_ID,
1241           p_last_update_date  => SYSDATE,
1242           p_last_updated_by  => FND_GLOBAL.USER_ID,
1243           p_last_update_login  => FND_GLOBAL.conc_login_id,
1244           p_attribute_category  => p_prod_alloc_rec.attribute_category,
1245           p_attribute1  => p_prod_alloc_rec.attribute1,
1246           p_attribute2  => p_prod_alloc_rec.attribute2,
1247           p_attribute3  => p_prod_alloc_rec.attribute3,
1248           p_attribute4  => p_prod_alloc_rec.attribute4,
1249           p_attribute5  => p_prod_alloc_rec.attribute5,
1250           p_attribute6  => p_prod_alloc_rec.attribute6,
1251           p_attribute7  => p_prod_alloc_rec.attribute7,
1252           p_attribute8  => p_prod_alloc_rec.attribute8,
1253           p_attribute9  => p_prod_alloc_rec.attribute9,
1254           p_attribute10  => p_prod_alloc_rec.attribute10,
1255           p_attribute11  => p_prod_alloc_rec.attribute11,
1256           p_attribute12  => p_prod_alloc_rec.attribute12,
1257           p_attribute13  => p_prod_alloc_rec.attribute13,
1258           p_attribute14  => p_prod_alloc_rec.attribute14,
1259           p_attribute15  => p_prod_alloc_rec.attribute15,
1260           px_org_id  => l_org_id
1261         );
1262 
1263 
1264        l_total_lysp_sales := 0;
1265        l_total_quota := 0;
1266 
1267 
1268        FOR l_idx IN l_period_tbl.first..l_period_tbl.last
1269        LOOP
1270         IF l_period_tbl.exists(l_idx) THEN
1271 
1272            p_time_alloc_rec := NULL;
1273 
1274            OPEN root_time_spread_csr(root_product_rec.product_allocation_id, l_period_tbl(l_idx));
1275            FETCH root_time_spread_csr INTO l_root_time_rec;
1276            CLOSE root_time_spread_csr ;
1277 
1278 
1279            l_root_time_rec.target := NVL(l_root_time_rec.target, 0);
1280 
1281            l_time_quota := ROUND( (NVL(l_root_time_rec.target, 0) * l_multiplying_factor), 0);
1282            l_total_quota := l_total_quota + l_time_quota;
1283 
1284            l_lysp_sales := 0;
1285 
1286            IF root_product_rec.item_type = 'PRICING_ATTRIBUTE1' THEN
1287               OPEN product_lysp_sales_csr(root_product_rec.item_id,
1288                                           l_territory_id,
1289                                           l_lysp_period_tbl(l_idx)
1290                                          );
1291               FETCH product_lysp_sales_csr INTO l_lysp_sales;
1292               CLOSE product_lysp_sales_csr;
1293            ELSIF root_product_rec.item_type = 'PRICING_ATTRIBUTE2' THEN
1294                OPEN category_lysp_sales_csr(root_product_rec.item_id,
1295                                             l_territory_id,
1296                                             l_lysp_period_tbl(l_idx),
1297                                             l_fund_id
1298                                            );
1299                FETCH category_lysp_sales_csr INTO l_lysp_sales;
1300                CLOSE category_lysp_sales_csr;
1301            ELSIF root_product_rec.item_type = 'OTHERS' THEN
1302                OPEN others_lysp_sales_csr(l_territory_id,
1303                                           l_lysp_period_tbl(l_idx),
1304                                           l_fund_id
1305                                          );
1306                FETCH others_lysp_sales_csr INTO l_lysp_sales;
1307                CLOSE others_lysp_sales_csr;
1308            END IF;
1309 
1310            l_lysp_sales := NVL(l_lysp_sales, 0);
1311            l_total_lysp_sales := l_total_lysp_sales + l_lysp_sales;
1312 
1313            l_time_allocation_id := get_time_allocation_id;
1314 
1315            p_time_alloc_rec.allocation_for := 'PROD';
1316            p_time_alloc_rec.allocation_for_id := l_product_allocation_id;
1317            p_time_alloc_rec.time_id := l_period_tbl(l_idx);
1318            p_time_alloc_rec.period_type_id := l_period_type_id;
1319            p_time_alloc_rec.target := NVL(l_time_quota, 0);
1320            p_time_alloc_rec.lysp_sales := NVL(l_lysp_sales, 0);
1321 
1322 
1323            Ozf_Time_Allocations_Pkg.Insert_Row(
1324               px_time_allocation_id  => l_time_allocation_id,
1325               p_allocation_for  => p_time_alloc_rec.allocation_for,
1326               p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
1327               p_time_id  => p_time_alloc_rec.time_id,
1328               p_period_type_id => p_time_alloc_rec.period_type_id,
1329               p_target  => NVL(p_time_alloc_rec.target, 0),
1330               p_lysp_sales  => NVL(p_time_alloc_rec.lysp_sales, 0),
1331               px_object_version_number  => l_object_version_number,
1332               p_creation_date  => SYSDATE,
1333               p_created_by  => FND_GLOBAL.USER_ID,
1334               p_last_update_date  => SYSDATE,
1335               p_last_updated_by  => FND_GLOBAL.USER_ID,
1336               p_last_update_login  => FND_GLOBAL.conc_login_id,
1337               p_attribute_category  => p_time_alloc_rec.attribute_category,
1338               p_attribute1  => p_time_alloc_rec.attribute1,
1339               p_attribute2  => p_time_alloc_rec.attribute2,
1340               p_attribute3  => p_time_alloc_rec.attribute3,
1341               p_attribute4  => p_time_alloc_rec.attribute4,
1342               p_attribute5  => p_time_alloc_rec.attribute5,
1343               p_attribute6  => p_time_alloc_rec.attribute6,
1344               p_attribute7  => p_time_alloc_rec.attribute7,
1345               p_attribute8  => p_time_alloc_rec.attribute8,
1346               p_attribute9  => p_time_alloc_rec.attribute9,
1347               p_attribute10  => p_time_alloc_rec.attribute10,
1348               p_attribute11  => p_time_alloc_rec.attribute11,
1349               p_attribute12  => p_time_alloc_rec.attribute12,
1350               p_attribute13  => p_time_alloc_rec.attribute13,
1351               p_attribute14  => p_time_alloc_rec.attribute14,
1352               p_attribute15  => p_time_alloc_rec.attribute15,
1353               px_org_id  => l_org_id
1354             );
1355 
1356 
1357          END IF;
1358        END LOOP;
1359 
1360        UPDATE OZF_PRODUCT_ALLOCATIONS p
1361        SET p.lysp_sales = l_total_lysp_sales,
1362            p.target = ROUND( l_total_quota, 0),
1363            p.object_version_number = p.object_version_number + 1,
1364            p.last_update_date = SYSDATE,
1365            p.last_updated_by = FND_GLOBAL.USER_ID,
1366            p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
1367        WHERE p.product_allocation_id = l_product_allocation_id;
1368 
1369    END LOOP;
1370 
1371 
1372 -------BEGIN: ORIGINAL FIX for difference due to ROUNDING --------------------------------------------------|
1373 
1374          l_diff_target   := 0;
1375          l_diff_target_1 := 0;
1376 
1377      BEGIN
1378 
1379 
1380               SELECT SUM(p.TARGET) INTO l_diff_target_1
1381               FROM OZF_PRODUCT_ALLOCATIONS p
1382                WHERE p.allocation_for = 'FACT'
1383                  AND p.allocation_for_id = l_fact_id;
1384 
1385              l_diff_target := ROUND((NVL(l_fact_rec.recommend_total_amount, 0) - NVL(l_diff_target_1, 0)), 0);
1386 
1387          EXCEPTION
1388             WHEN OTHERS THEN
1389                  l_diff_target := 0;
1390          END;
1391 
1392      IF l_diff_target <> 0 THEN
1393 
1394             UPDATE OZF_TIME_ALLOCATIONS t
1395                 SET t.TARGET = t.TARGET + l_diff_target,
1396                     t.object_version_number = t.object_version_number + 1,
1397                     t.last_update_date = SYSDATE,
1398                     t.last_updated_by = FND_GLOBAL.USER_ID,
1399                     t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
1400               WHERE t.time_allocation_id = (SELECT max(x.time_allocation_id) from OZF_TIME_ALLOCATIONS x
1401                                             WHERE  x.allocation_for = 'PROD'
1402                                             AND x.allocation_for_id IN ( SELECT p.product_allocation_id
1403                                                                          FROM  OZF_PRODUCT_ALLOCATIONS p
1404                                                                          WHERE p.allocation_for = 'FACT'
1405                                                                            AND p.allocation_for_id = l_fact_id
1406                                                                            AND p.item_id = -9999 )
1407                                            );
1408 
1409              IF (SQL%NOTFOUND) THEN
1410                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
1411                     fnd_message.set_name ('OZF', 'OZF_TP_ROUNDING_TIME_ERR');
1412                     fnd_message.set_token('OZF_TP_OBJECT_TYPE_TOKEN','FACT');
1413                     fnd_message.set_token('OZF_TP_OBJECT_ID_TOKEN',l_fact_id);
1414                     fnd_msg_pub.ADD;
1415                  END IF;
1416                  RAISE fnd_api.g_exc_unexpected_error;
1417              END IF;
1418 
1419 
1420          UPDATE OZF_PRODUCT_ALLOCATIONS p
1421                 SET p.TARGET = p.TARGET + l_diff_target,
1422                     p.object_version_number = p.object_version_number + 1,
1423                     p.last_update_date = SYSDATE,
1424                     p.last_updated_by = FND_GLOBAL.USER_ID,
1425                     p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
1426               WHERE p.allocation_for = 'FACT'
1427                 AND p.allocation_for_id = l_fact_id
1428         AND p.item_id = -9999;
1429 
1430               IF (SQL%NOTFOUND) THEN
1431                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
1432                     fnd_message.set_name ('OZF', 'OZF_TP_ROUNDING_PROD_ERR');
1433                     fnd_message.set_token('OZF_TP_OBJECT_TYPE_TOKEN','FACT');
1434                     fnd_message.set_token('OZF_TP_OBJECT_ID_TOKEN',l_fact_id);
1435                     fnd_msg_pub.ADD;
1436                  END IF;
1437                  RAISE fnd_api.g_exc_unexpected_error;
1438               END IF;
1439 
1440       END IF;
1441 
1442 --------END: ORIGINAL FIX for difference due to ROUNDING --------------------------------------------------|
1443 
1444 
1445 
1446    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Populating Product and Time Allocations Records'
1447                                          || 'FOR Fact_id = '|| l_fact_id || ' ; ');
1448 
1449    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- end');
1450 
1451  EXCEPTION
1452      WHEN OZF_TP_DIFF_TIME_SPREAD THEN
1453           ROLLBACK TO create_old_fact_product_spread;
1454           FND_MESSAGE.set_name('OZF', 'OZF_TP_DIFF_TIME_SPREAD_TXT');
1455           FND_MSG_PUB.add;
1456           x_return_status := FND_API.g_ret_sts_error ;
1457           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
1458                                      p_data    => x_error_message);
1459           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : TIME SPREAD MISMATCH EXCEPTION = '||sqlerrm(sqlcode));
1460           OZF_UTILITY_PVT.debug_message(fnd_message.get_string('OZF', 'OZF_TP_DIFF_TIME_SPREAD_TXT'));
1461 
1462      WHEN OZF_TP_BLANK_PERIOD_TBL THEN
1463           ROLLBACK TO create_old_fact_product_spread;
1464           FND_MESSAGE.set_name('OZF', 'OZF_TP_BLANK_PERIOD_TBL_TXT');
1465           FND_MSG_PUB.add;
1466           x_return_status := FND_API.g_ret_sts_error ;
1467           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
1468                                      p_data    => x_error_message);
1469           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : BLANK PERIOD TABLE EXCEPTION = '||sqlerrm(sqlcode));
1470           OZF_UTILITY_PVT.debug_message(fnd_message.get_string('OZF', 'OZF_TP_BLANK_PERIOD_TBL_TXT'));
1471 
1472      WHEN FND_API.G_EXC_ERROR THEN
1473           ROLLBACK TO create_old_fact_product_spread;
1474           x_return_status := FND_API.g_ret_sts_error ;
1475           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
1476                                      p_data    => x_error_message);
1477 
1478      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1479           ROLLBACK TO create_old_fact_product_spread;
1480           x_return_status := FND_API.g_ret_sts_unexp_error ;
1481           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
1482                                      p_data  => x_error_message);
1483 
1484      WHEN OTHERS THEN
1485           ROLLBACK TO create_old_fact_product_spread;
1486           x_return_status := FND_API.g_ret_sts_unexp_error ;
1487           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
1488              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
1489           END IF;
1490           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
1491                                      p_data  => x_error_message);
1492           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
1493 
1494  END create_old_fact_product_spread;
1495 
1496 
1497 
1498 
1499 -- ------------------------
1500 -- Private Procedure
1501 -- ------------------------
1502 -- ------------------------------------------------------------------
1503 -- Name: create fact product spread
1504 -- Desc: Setup product spread for a given Fact
1505 --       This Allocation WorkSheet Fact emerged from an Allocation done
1506 --       by a Root Node or Creator Node
1507 --       Updated on May17th,2004 -
1508 --          Per new requirements in Bug 3594874.
1509 --          Facts Product Spread should not be based on Root's Product Spread.
1510 --          Instead, it should be based on that Territories LYSP sales.
1511 -- -----------------------------------------------------------------
1512  PROCEDURE create_fact_product_spread
1513  (
1514     p_api_version        IN          NUMBER,
1515     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
1516     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
1517     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
1518     x_return_status      OUT NOCOPY  VARCHAR2,
1519     x_error_number       OUT NOCOPY  NUMBER,
1520     x_error_message      OUT NOCOPY  VARCHAR2,
1521     p_fact_id            IN          NUMBER
1522  ) IS
1523 
1524    l_api_version   CONSTANT NUMBER       := 1.0;
1525    l_api_name      CONSTANT VARCHAR2(30) := 'create_fact_product_spread';
1526    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
1527    l_return_status          VARCHAR2(1);
1528 
1529    l_object_version_number  NUMBER := 1;
1530    l_org_id                 NUMBER := TO_NUMBER(SUBSTRB(USERENV('CLIENT_INFO'), 1, 10));
1531    l_fact_id                NUMBER;
1532    l_fund_id                NUMBER;
1533    l_start_date             VARCHAR2(30) := null;
1534    l_end_date               VARCHAR2(30) := null;
1535    l_period_type_id         NUMBER;
1536    l_in_clause              VARCHAR2(1000) := null;
1537    l_lysp_in_clause         VARCHAR2(1000) := null;
1538    l_total_target           NUMBER;
1539    l_territory_id           NUMBER;
1540    l_resource_id            NUMBER;
1541    l_lysp_sales             NUMBER;
1542    l_total_lysp_sales       NUMBER;
1543    l_grand_total_lysp_sales NUMBER;
1544    l_denominator            NUMBER;
1545    l_total_root_quota       NUMBER;
1546    l_time_quota             NUMBER;
1547    l_total_quota            NUMBER;
1548    l_multiplying_factor     NUMBER;
1549    l_product_allocation_id  NUMBER;
1550    l_time_allocation_id     NUMBER;
1551    l_diff_target            NUMBER;
1552    l_diff_target_1          NUMBER;
1553    l_diff_target_2          NUMBER;
1554 
1555    l_period_tbl          OZF_TIME_API_PVT.G_period_tbl_type;
1556    l_lysp_period_tbl     OZF_TIME_API_PVT.G_period_tbl_type;
1557 
1558    p_prod_alloc_rec      ozf_product_allocations%ROWTYPE;
1559    p_time_alloc_rec      ozf_time_allocations%ROWTYPE;
1560 
1561 
1562   CURSOR fact_csr
1563          (l_fact_id        number) IS
1564   SELECT
1565       activity_metric_fact_id,
1566       act_metric_used_by_id,
1567       arc_act_metric_used_by,
1568       activity_metric_id,
1569       hierarchy_id,
1570       hierarchy_type,
1571       node_id,
1572       previous_fact_id,
1573       recommend_total_amount,
1574       status_code
1575   FROM
1576       OZF_ACT_METRIC_FACTS_ALL
1577   WHERE
1578       activity_metric_fact_id = l_fact_id;
1579 
1580   l_fact_rec      fact_csr%ROWTYPE;
1581 
1582   CURSOR allocation_csr
1583          (l_allocation_id        number) IS
1584   SELECT
1585      activity_metric_id,
1586      arc_act_metric_used_by,
1587      act_metric_used_by_id,
1588      product_spread_time_id  period_type_id,  -- (eg.. 32 for monthly, 64 for quarterly),
1589      published_flag,
1590      status_code,
1591      start_period_name,
1592      end_period_name,
1593      from_date,
1594      to_date
1595   FROM
1596       OZF_ACT_METRICS_ALL
1597   WHERE
1598       activity_metric_id = l_allocation_id;
1599 
1600   l_allocation_rec      allocation_csr%ROWTYPE;
1601 
1602 
1603   CURSOR fund_csr (l_fund_id  NUMBER) IS
1604    SELECT
1605     owner,
1606     start_period_id,
1607     end_period_id,
1608     start_date_active,
1609     end_date_active,
1610     status_code,
1611     original_budget,
1612     transfered_in_amt,
1613     transfered_out_amt,
1614     node_id, -- (=territory id)
1615     product_spread_time_id period_type_id -- (= minor_scale_id i.e. qtrly or monthly)
1616    FROM
1617     ozf_funds_all_vl
1618    WHERE
1619     fund_id = l_fund_id;
1620 
1621   l_fund_rec    fund_csr%ROWTYPE;
1622 
1623 /*
1624   CURSOR get_total_target_csr
1625         (l_fund_id   NUMBER,
1626      l_in_clause VARCHAR2) IS
1627    SELECT SUM(t.target)
1628    FROM
1629        ozf_time_allocations t,
1630        ozf_product_allocations p
1631    WHERE
1632        p.fund_id = l_fund_id
1633    AND t.allocation_for_id   = p.product_allocation_id
1634    AND t.allocation_for      = 'PROD'
1635    AND t.time_id IN (l_in_clause);
1636 */
1637 
1638   l_get_total_target_sql VARCHAR2(30000) :=
1639    ' SELECT SUM(t.target) '||
1640    ' FROM '||
1641    '     ozf_time_allocations t,'||
1642    '     ozf_product_allocations p'||
1643    ' WHERE'||
1644    '     p.fund_id = :l_fund_id'||
1645    ' AND t.allocation_for_id   = p.product_allocation_id'||
1646    ' AND t.allocation_for      = ''PROD'' '||
1647    ' AND t.time_id IN (';
1648 --l_in_clause);
1649 
1650   get_total_target_csr  G_GenericCurType;  --cursor variable (processed like a PL/SQL variable)
1651 
1652 
1653    CURSOR root_product_spread_csr
1654          (l_fund_id        number) IS
1655     SELECT
1656        p.product_allocation_id,
1657        p.item_id,
1658        p.item_type
1659     FROM
1660        ozf_product_allocations p
1661     WHERE
1662        p.fund_id = l_fund_id;
1663 
1664    l_root_product_rec     root_product_spread_csr%rowtype;
1665 
1666    CURSOR root_time_spread_csr
1667          (l_product_allocation_id       number,
1668           l_time_id                     number) IS
1669    SELECT t.target
1670    FROM
1671        ozf_time_allocations t
1672    WHERE
1673        t.allocation_for_id = l_product_allocation_id
1674    AND t.allocation_for = 'PROD'
1675    AND t.time_id = l_time_id;
1676 
1677    l_root_time_rec     root_time_spread_csr%rowtype;
1678 
1679 
1680   CURSOR product_lysp_sales_csr (l_product_id    NUMBER,
1681                                  l_territory_id  NUMBER,
1682                                  l_time_id       NUMBER) IS
1683    SELECT
1684     SUM(bsmv.sales) sales
1685    FROM
1686      ozf_order_sales_v bsmv,
1687      ams_party_market_segments a
1688    WHERE
1689         a.market_qualifier_reference = l_territory_id
1690     AND a.market_qualifier_type='TERRITORY'
1691     AND a.site_use_id = bsmv.ship_to_site_use_id
1692     AND bsmv.inventory_item_id = l_product_id
1693     AND bsmv.time_id = l_time_id;
1694 
1695 
1696   CURSOR category_lysp_sales_csr (l_category_id    NUMBER,
1697                                   l_territory_id   NUMBER,
1698                                   l_time_id        NUMBER,
1699                                   l_fund_id        NUMBER) IS
1700   SELECT
1701    SUM(bsmv.sales) sales
1702   FROM
1703    ozf_order_sales_v bsmv,
1704    ams_party_market_segments a
1705   WHERE
1706       a.market_qualifier_reference = l_territory_id
1707   AND a.market_qualifier_type='TERRITORY'
1708   AND bsmv.ship_to_site_use_id = a.site_use_id
1709   AND bsmv.time_id = l_time_id
1710   AND bsmv.inventory_item_id IN
1711                              ( SELECT DISTINCT MIC.INVENTORY_ITEM_ID
1712                                FROM   MTL_ITEM_CATEGORIES     MIC,
1713                                       ENI_PROD_DENORM_HRCHY_V DENORM
1714                                WHERE  MIC.CATEGORY_SET_ID  = DENORM.CATEGORY_SET_ID
1715                                 AND   MIC.CATEGORY_ID      = DENORM.CHILD_ID
1716                                 AND   DENORM.PARENT_ID     = l_category_id
1717                                MINUS
1718                                SELECT p.item_id
1719                                FROM   ozf_product_allocations p
1720                                WHERE  p.fund_id = l_fund_id
1721                                   AND p.item_type = 'PRICING_ATTRIBUTE1'
1722                              );
1723 
1724 
1725   CURSOR others_lysp_sales_csr (l_territory_id   NUMBER,
1726                                 l_time_id        NUMBER,
1727                                 l_fund_id        NUMBER) IS
1728   SELECT
1729    SUM(bsmv.sales) sales
1730   FROM
1731    ozf_order_sales_v bsmv,
1732    ams_party_market_segments a
1733   WHERE
1734       a.market_qualifier_reference = l_territory_id
1735   AND a.market_qualifier_type='TERRITORY'
1736   AND bsmv.ship_to_site_use_id = a.site_use_id
1737   AND bsmv.time_id = l_time_id
1738   AND NOT EXISTS
1739   (
1740   ( SELECT p.item_id
1741     FROM ozf_product_allocations p
1742     WHERE
1743         p.fund_id = l_fund_id
1744     AND p.item_type = 'PRICING_ATTRIBUTE1'
1745     AND p.item_id = bsmv.inventory_item_id
1746     UNION ALL
1747     SELECT MIC.INVENTORY_ITEM_ID
1748     FROM   MTL_ITEM_CATEGORIES MIC,
1749            ENI_PROD_DENORM_HRCHY_V DENORM,
1750            OZF_PRODUCT_ALLOCATIONS p
1751     WHERE p.FUND_ID = l_fund_id
1752       AND p.ITEM_TYPE = 'PRICING_ATTRIBUTE2'
1753       AND p.ITEM_ID = DENORM.PARENT_ID
1754       AND MIC.CATEGORY_SET_ID = DENORM.CATEGORY_SET_ID
1755       AND MIC.CATEGORY_ID = DENORM.CHILD_ID
1756       AND MIC.INVENTORY_ITEM_ID = bsmv.inventory_item_id
1757   )
1758   MINUS
1759   SELECT prod.inventory_item_id
1760   FROM ams_act_products prod
1761   where
1762       prod.level_type_code = 'PRODUCT'
1763   AND prod.arc_act_product_used_by = 'FUND'
1764   AND prod.act_product_used_by_id = l_fund_id
1765   AND prod.excluded_flag = 'Y'
1766   AND prod.inventory_item_id = bsmv.inventory_item_id
1767   );
1768 
1769 
1770 
1771  BEGIN
1772 
1773    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- start');
1774 
1775    -- Standard Start of API savepoint
1776    SAVEPOINT create_fact_product_spread;
1777 
1778    -- Standard call to check for call compatibility.
1779    IF NOT FND_API.compatible_api_call(l_api_version,
1780                                       p_api_version,
1781                                       l_api_name,
1782                                       g_pkg_name)
1783    THEN
1784      RAISE FND_API.g_exc_unexpected_error;
1785    END IF;
1786 
1787    -- Initialize message list if p_init_msg_list is set to TRUE.
1788    IF FND_API.to_Boolean( p_init_msg_list ) THEN
1789       FND_MSG_PUB.initialize;
1790    END IF;
1791 
1792    -- Initialize API return status to SUCCESS
1793    x_return_status := FND_API.g_ret_sts_success;
1794 
1795    l_fact_id := p_fact_id;
1796 
1797    OPEN fact_csr(l_fact_id);
1798    FETCH fact_csr INTO l_fact_rec;
1799    CLOSE fact_csr ;
1800 
1801    l_territory_id := l_fact_rec.node_id;
1802 
1803    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Territory Id => '
1804                                                  || l_territory_id ||' ; ');
1805 
1806    OPEN allocation_csr(l_fact_rec.activity_metric_id);
1807    FETCH allocation_csr INTO l_allocation_rec;
1808    CLOSE allocation_csr ;
1809 
1810    l_fund_id := l_allocation_rec.act_metric_used_by_id; -- this is ROOT's Fund_id
1811    l_period_type_id := l_allocation_rec.period_type_id;
1812 
1813    OPEN fund_csr(l_fund_id);
1814    FETCH fund_csr INTO l_fund_rec;
1815    CLOSE fund_csr ;
1816 
1817    IF l_period_type_id <> l_fund_rec.period_type_id THEN
1818       RAISE OZF_TP_DIFF_TIME_SPREAD;
1819    END IF;
1820 
1821    l_start_date := to_char(l_allocation_rec.from_date, 'YYYY/MM/DD');
1822    l_end_date   := to_char(l_allocation_rec.to_date, 'YYYY/MM/DD');
1823 
1824    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Getting Time_ids Between '
1825                                                  ||l_start_date||' AND '||l_end_date||' ; '
1826                                                  ||' Period_Type_id = '||l_period_type_id||' ; ');
1827 
1828    IF l_start_date IS NULL OR
1829       l_end_date IS NULL OR
1830       l_period_type_id IS NULL
1831    THEN
1832       RAISE OZF_TP_BLANK_PERIOD_TBL;
1833    END IF;
1834 
1835    l_period_tbl := OZF_TIME_API_PVT.get_period_tbl
1836                                     (l_start_date,
1837                                      l_end_date,
1838                                      l_period_type_id);
1839 
1840 
1841    IF l_period_tbl IS NULL OR l_period_tbl.COUNT <= 0 THEN
1842       RAISE OZF_TP_BLANK_PERIOD_TBL;
1843    END IF;
1844 
1845    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Getting Time_ids Between '||l_start_date
1846                                          ||' AND '||l_end_date||' ; ');
1847    IF l_period_tbl IS NOT NULL THEN
1848     IF l_period_tbl.COUNT > 0 THEN
1849       FOR l_idx IN l_period_tbl.first..l_period_tbl.last
1850       LOOP
1851         IF l_period_tbl.exists(l_idx) THEN
1852 
1853          l_lysp_period_tbl(l_idx) := OZF_TIME_API_PVT.get_lysp_id (l_period_tbl(l_idx), l_period_type_id);
1854 
1855          OZF_UTILITY_PVT.debug_message(SubStr('l_period_tbl('||TO_CHAR(l_idx)||') = '
1856                                ||TO_CHAR(l_period_tbl(l_idx)), 1,255));
1857          OZF_UTILITY_PVT.debug_message(SubStr('l_lysp_period_tbl('||TO_CHAR(l_idx)||') = '
1858                                ||TO_CHAR(l_lysp_period_tbl(l_idx)), 1,255));
1859         END IF;
1860       END LOOP;
1861     END IF;
1862    END IF;
1863 
1864    --OZF_UTILITY_PVT.debug_message(' out of  lysp period table');
1865 
1866    --l_in_clause := '(';
1867    l_in_clause := NULL;
1868    IF l_period_tbl IS NOT NULL THEN
1869       FOR l_idx IN l_period_tbl.first..l_period_tbl.last
1870       LOOP
1871           IF l_in_clause IS NULL THEN
1872              l_in_clause := LTRIM(' '||l_period_tbl(l_idx));
1873           ELSE
1874              l_in_clause := l_in_clause ||','|| l_period_tbl(l_idx);
1875           END IF;
1876       END LOOP;
1877    END IF;
1878    --l_in_clause := l_in_clause||')';
1879 
1880    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': l_in_clause == '||l_in_clause);
1881 
1882    --l_lysp_in_clause := '(';
1883    l_lysp_in_clause := NULL;
1884    IF l_lysp_period_tbl IS NOT NULL THEN
1885       FOR l_idx IN l_lysp_period_tbl.first..l_lysp_period_tbl.last
1886       LOOP
1887           IF l_lysp_in_clause IS NULL THEN
1888              l_lysp_in_clause := LTRIM(' '||l_lysp_period_tbl(l_idx));
1889           ELSE
1890              l_lysp_in_clause := l_lysp_in_clause ||','|| l_lysp_period_tbl(l_idx);
1891           END IF;
1892       END LOOP;
1893    END IF;
1894    --l_lysp_in_clause := l_lysp_in_clause||')';
1895 
1896    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': l_lysp_in_clause == '||l_lysp_in_clause);
1897 
1898 --//00000 May17th,2004 - as per new requirements in Bug 3594874 000000000000000000000000000000000000
1899 /*
1900    l_get_total_target_sql := l_get_total_target_sql||l_in_clause ||')';
1901 
1902    OPEN get_total_target_csr FOR l_get_total_target_sql USING l_fund_id;
1903    FETCH get_total_target_csr INTO l_total_root_quota;
1904    CLOSE get_total_target_csr ;
1905 
1906 
1907    l_total_root_quota := NVL(l_total_root_quota, 0);
1908 
1909    IF l_total_root_quota > 0 THEN
1910       l_multiplying_factor :=  NVL(l_fact_rec.recommend_total_amount, 0) / l_total_root_quota;
1911    ELSE
1912       l_multiplying_factor := 0;
1913    END IF;
1914 
1915    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Multiplying Factor is '||  l_multiplying_factor);
1916 */
1917 --//0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1918 
1919 
1920 ------- Insert rows for PRODUCT and TIME Allocation Records for given FACT ------------------------------
1921 
1922 
1923    l_grand_total_lysp_sales := 0;
1924 
1925    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Populating Product and Time Allocations Records'
1926                                          || 'FOR Fact_id = '|| l_fact_id || ' ; ');
1927 
1928    FOR root_product_rec IN root_product_spread_csr(l_fund_id)
1929    LOOP
1930 
1931        p_prod_alloc_rec := NULL;
1932 
1933        l_product_allocation_id := get_product_allocation_id;
1934 
1935        p_prod_alloc_rec.allocation_for := 'FACT';
1936        p_prod_alloc_rec.allocation_for_id := l_fact_id;
1937        p_prod_alloc_rec.item_type := root_product_rec.item_type;
1938        p_prod_alloc_rec.item_id := root_product_rec.item_id;
1939        p_prod_alloc_rec.selected_flag := 'N';
1940        p_prod_alloc_rec.target := 0;
1941        p_prod_alloc_rec.lysp_sales := 0;
1942 
1943 
1944        Ozf_Product_Allocations_Pkg.Insert_Row(
1945           px_product_allocation_id  => l_product_allocation_id,
1946           p_allocation_for  => p_prod_alloc_rec.allocation_for,
1947           p_allocation_for_id  => p_prod_alloc_rec.allocation_for_id,
1948           p_fund_id  => p_prod_alloc_rec.fund_id,
1949           p_item_type  => p_prod_alloc_rec.item_type,
1950           p_item_id  => p_prod_alloc_rec.item_id,
1951           p_selected_flag  => p_prod_alloc_rec.selected_flag,
1952           p_target  => NVL(p_prod_alloc_rec.target, 0),
1953           p_lysp_sales  => NVL(p_prod_alloc_rec.lysp_sales, 0),
1954           p_parent_product_allocation_id  => p_prod_alloc_rec.parent_product_allocation_id,
1955           px_object_version_number  => l_object_version_number,
1956           p_creation_date  => SYSDATE,
1957           p_created_by  => FND_GLOBAL.USER_ID,
1958           p_last_update_date  => SYSDATE,
1959           p_last_updated_by  => FND_GLOBAL.USER_ID,
1960           p_last_update_login  => FND_GLOBAL.conc_login_id,
1961           p_attribute_category  => p_prod_alloc_rec.attribute_category,
1962           p_attribute1  => p_prod_alloc_rec.attribute1,
1963           p_attribute2  => p_prod_alloc_rec.attribute2,
1964           p_attribute3  => p_prod_alloc_rec.attribute3,
1965           p_attribute4  => p_prod_alloc_rec.attribute4,
1966           p_attribute5  => p_prod_alloc_rec.attribute5,
1967           p_attribute6  => p_prod_alloc_rec.attribute6,
1968           p_attribute7  => p_prod_alloc_rec.attribute7,
1969           p_attribute8  => p_prod_alloc_rec.attribute8,
1970           p_attribute9  => p_prod_alloc_rec.attribute9,
1971           p_attribute10  => p_prod_alloc_rec.attribute10,
1972           p_attribute11  => p_prod_alloc_rec.attribute11,
1973           p_attribute12  => p_prod_alloc_rec.attribute12,
1974           p_attribute13  => p_prod_alloc_rec.attribute13,
1975           p_attribute14  => p_prod_alloc_rec.attribute14,
1976           p_attribute15  => p_prod_alloc_rec.attribute15,
1977           px_org_id  => l_org_id
1978         );
1979 
1980 
1981        l_total_lysp_sales := 0;
1982        l_total_quota := 0;
1983 
1984 
1985        FOR l_idx IN l_period_tbl.first..l_period_tbl.last
1986        LOOP
1987         IF l_period_tbl.exists(l_idx) THEN
1988 
1989            p_time_alloc_rec := NULL;
1990 
1991 --//00000 May17th,2004 - as per new requirements in Bug 3594874 000000000000000000000000000000000000
1992 /*
1993            OPEN root_time_spread_csr(root_product_rec.product_allocation_id, l_period_tbl(l_idx));
1994            FETCH root_time_spread_csr INTO l_root_time_rec;
1995            CLOSE root_time_spread_csr ;
1996 
1997 
1998            l_root_time_rec.target := NVL(l_root_time_rec.target, 0);
1999 
2000            l_time_quota := ROUND( (NVL(l_root_time_rec.target, 0) * l_multiplying_factor), 0);
2001            l_total_quota := l_total_quota + l_time_quota;
2002 */
2003 --//000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
2004 
2005            l_lysp_sales := 0;
2006 
2007            IF root_product_rec.item_type = 'PRICING_ATTRIBUTE1' THEN
2008               OPEN product_lysp_sales_csr(root_product_rec.item_id,
2009                                           l_territory_id,
2010                                           l_lysp_period_tbl(l_idx)
2011                                          );
2012               FETCH product_lysp_sales_csr INTO l_lysp_sales;
2013               CLOSE product_lysp_sales_csr;
2014            ELSIF root_product_rec.item_type = 'PRICING_ATTRIBUTE2' THEN
2015                OPEN category_lysp_sales_csr(root_product_rec.item_id,
2016                                             l_territory_id,
2017                                             l_lysp_period_tbl(l_idx),
2018                                             l_fund_id
2019                                            );
2020                FETCH category_lysp_sales_csr INTO l_lysp_sales;
2021                CLOSE category_lysp_sales_csr;
2022            ELSIF root_product_rec.item_type = 'OTHERS' THEN
2023                OPEN others_lysp_sales_csr(l_territory_id,
2024                                           l_lysp_period_tbl(l_idx),
2025                                           l_fund_id
2026                                          );
2027                FETCH others_lysp_sales_csr INTO l_lysp_sales;
2028                CLOSE others_lysp_sales_csr;
2029            END IF;
2030 
2031            l_lysp_sales := NVL(l_lysp_sales, 0);
2032            l_total_lysp_sales := l_total_lysp_sales + l_lysp_sales;
2033 
2034            l_time_allocation_id := get_time_allocation_id;
2035 
2036            p_time_alloc_rec.allocation_for := 'PROD';
2037            p_time_alloc_rec.allocation_for_id := l_product_allocation_id;
2038            p_time_alloc_rec.time_id := l_period_tbl(l_idx);
2039            p_time_alloc_rec.period_type_id := l_period_type_id;
2040            p_time_alloc_rec.target := 0;  --------NVL(l_time_quota, 0);
2041            p_time_alloc_rec.lysp_sales := NVL(l_lysp_sales, 0);
2042 
2043 
2044            Ozf_Time_Allocations_Pkg.Insert_Row(
2045               px_time_allocation_id  => l_time_allocation_id,
2046               p_allocation_for  => p_time_alloc_rec.allocation_for,
2047               p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
2048               p_time_id  => p_time_alloc_rec.time_id,
2049               p_period_type_id => p_time_alloc_rec.period_type_id,
2050               p_target  => NVL(p_time_alloc_rec.target, 0),
2051               p_lysp_sales  => NVL(p_time_alloc_rec.lysp_sales, 0),
2052               px_object_version_number  => l_object_version_number,
2053               p_creation_date  => SYSDATE,
2054               p_created_by  => FND_GLOBAL.USER_ID,
2055               p_last_update_date  => SYSDATE,
2056               p_last_updated_by  => FND_GLOBAL.USER_ID,
2057               p_last_update_login  => FND_GLOBAL.conc_login_id,
2058               p_attribute_category  => p_time_alloc_rec.attribute_category,
2059               p_attribute1  => p_time_alloc_rec.attribute1,
2060               p_attribute2  => p_time_alloc_rec.attribute2,
2061               p_attribute3  => p_time_alloc_rec.attribute3,
2062               p_attribute4  => p_time_alloc_rec.attribute4,
2063               p_attribute5  => p_time_alloc_rec.attribute5,
2064               p_attribute6  => p_time_alloc_rec.attribute6,
2065               p_attribute7  => p_time_alloc_rec.attribute7,
2066               p_attribute8  => p_time_alloc_rec.attribute8,
2067               p_attribute9  => p_time_alloc_rec.attribute9,
2068               p_attribute10  => p_time_alloc_rec.attribute10,
2069               p_attribute11  => p_time_alloc_rec.attribute11,
2070               p_attribute12  => p_time_alloc_rec.attribute12,
2071               p_attribute13  => p_time_alloc_rec.attribute13,
2072               p_attribute14  => p_time_alloc_rec.attribute14,
2073               p_attribute15  => p_time_alloc_rec.attribute15,
2074               px_org_id  => l_org_id
2075             );
2076 
2077 
2078          END IF;
2079        END LOOP;
2080 
2081        UPDATE OZF_PRODUCT_ALLOCATIONS p
2082        SET p.lysp_sales = l_total_lysp_sales,
2083            p.object_version_number = p.object_version_number + 1,
2084            p.last_update_date = SYSDATE,
2085            p.last_updated_by = FND_GLOBAL.USER_ID,
2086            p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
2087        WHERE p.product_allocation_id = l_product_allocation_id;
2088 
2089        l_grand_total_lysp_sales := l_grand_total_lysp_sales + l_total_lysp_sales;
2090 
2091    END LOOP;
2092 
2093 
2094 ------ Updating Target ----------------------------------------------------------------
2095 
2096    l_multiplying_factor := 0;
2097    IF l_grand_total_lysp_sales > 0 THEN
2098       l_multiplying_factor := NVL(l_fact_rec.recommend_total_amount, 0) / l_grand_total_lysp_sales;
2099    ELSE
2100       l_multiplying_factor := 0;
2101    END IF;
2102 
2103    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Multiplying Factor is '||  l_multiplying_factor);
2104 
2105    UPDATE OZF_TIME_ALLOCATIONS t
2106    SET t.TARGET = ROUND((NVL(t.LYSP_SALES, 0) * l_multiplying_factor), 0),
2107        t.object_version_number = t.object_version_number + 1,
2108        t.last_update_date = SYSDATE,
2109        t.last_updated_by = FND_GLOBAL.USER_ID,
2110        t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
2111    WHERE  t.allocation_for = 'PROD'
2112    AND t.allocation_for_id IN ( SELECT p.product_allocation_id
2113                                  FROM  OZF_PRODUCT_ALLOCATIONS p
2114                                  WHERE p.allocation_for = 'FACT'
2115                                    AND p.allocation_for_id = l_fact_id );
2116 
2117    IF (SQL%NOTFOUND) THEN
2118       IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
2119          fnd_message.set_name ('OZF', 'OZF_TP_TIMEALLOC_NOT_FOUND_TXT');
2120          fnd_msg_pub.ADD;
2121       END IF;
2122 
2123       RAISE fnd_api.g_exc_unexpected_error;
2124    END IF;
2125 
2126    UPDATE OZF_PRODUCT_ALLOCATIONS p
2127    SET p.TARGET = (SELECT SUM(ti.TARGET)
2128                      FROM OZF_TIME_ALLOCATIONS ti
2129                     WHERE ti.ALLOCATION_FOR = 'PROD'
2130                       AND ti.ALLOCATION_FOR_ID = p.PRODUCT_ALLOCATION_ID),
2131        p.object_version_number = p.object_version_number + 1,
2132        p.last_update_date = SYSDATE,
2133        p.last_updated_by = FND_GLOBAL.USER_ID,
2134        p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
2135    WHERE p.allocation_for = 'FACT'
2136      AND p.allocation_for_id = l_fact_id;
2137 
2138    IF (SQL%NOTFOUND) THEN
2139       IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
2140          fnd_message.set_name ('OZF', 'OZF_TP_PRODALLOC_NOT_FOUND_TXT');
2141          fnd_msg_pub.ADD;
2142       END IF;
2143 
2144       RAISE fnd_api.g_exc_unexpected_error;
2145    END IF;
2146 
2147 
2148 
2149    -- Handling Corner Case : If all products including others have ZERO lysp sales, then
2150    --                        OTHERS get all the Quota. It is equally distributed in all
2151    --                        time periods.
2152 
2153    IF l_multiplying_factor = 0 THEN
2154 
2155       l_denominator := l_period_tbl.COUNT;
2156 
2157 /*
2158       UPDATE OZF_PRODUCT_ALLOCATIONS p
2159       SET p.TARGET = ROUND(NVL(l_fact_rec.recommend_total_amount, 0),0),
2160           p.object_version_number = p.object_version_number + 1,
2161           p.last_update_date = SYSDATE,
2162           p.last_updated_by = FND_GLOBAL.USER_ID,
2163           p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
2164       WHERE p.allocation_for = 'FACT'
2165         AND p.allocation_for_id = l_fact_id
2166         AND p.item_id = -9999;
2167 
2168       IF (SQL%NOTFOUND) THEN
2169          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
2170             fnd_message.set_name ('OZF', 'OZF_TP_PRODALLOC_NOT_FOUND_TXT');
2171             fnd_msg_pub.ADD;
2172          END IF;
2173 
2174          RAISE fnd_api.g_exc_unexpected_error;
2175       END IF;
2176 */
2177       UPDATE OZF_TIME_ALLOCATIONS t
2178       SET t.TARGET = ROUND((NVL(l_fact_rec.recommend_total_amount, 0) / l_denominator), 0),
2179           t.object_version_number = t.object_version_number + 1,
2180           t.last_update_date = SYSDATE,
2181           t.last_updated_by = FND_GLOBAL.USER_ID,
2182           t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
2183       WHERE  t.allocation_for = 'PROD'
2184       AND t.allocation_for_id IN ( SELECT p.product_allocation_id
2185                                     FROM  OZF_PRODUCT_ALLOCATIONS p
2186                                     WHERE p.allocation_for = 'FACT'
2187                                       AND p.allocation_for_id = l_fact_id
2188                                       AND p.item_id = -9999 );
2189 
2190       IF (SQL%NOTFOUND) THEN
2191          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
2192             fnd_message.set_name ('OZF', 'OZF_TP_TIMEALLOC_NOT_FOUND_TXT');
2193             fnd_msg_pub.ADD;
2194          END IF;
2195          RAISE fnd_api.g_exc_unexpected_error;
2196       END IF;
2197 
2198       UPDATE OZF_PRODUCT_ALLOCATIONS p
2199       SET p.TARGET = (SELECT SUM(ti.TARGET)
2200                         FROM OZF_TIME_ALLOCATIONS ti
2201                        WHERE ti.ALLOCATION_FOR = 'PROD'
2202                          AND ti.ALLOCATION_FOR_ID = p.PRODUCT_ALLOCATION_ID),
2203           p.object_version_number = p.object_version_number + 1,
2204           p.last_update_date = SYSDATE,
2205           p.last_updated_by = FND_GLOBAL.USER_ID,
2206           p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
2207       WHERE p.allocation_for = 'FACT'
2208         AND p.allocation_for_id = l_fact_id
2209         AND p.item_id = -9999;
2210 
2211       IF (SQL%NOTFOUND) THEN
2212          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
2213             fnd_message.set_name ('OZF', 'OZF_TP_PRODALLOC_NOT_FOUND_TXT');
2214             fnd_msg_pub.ADD;
2215          END IF;
2216 
2217          RAISE fnd_api.g_exc_unexpected_error;
2218       END IF;
2219 
2220 
2221    END IF;
2222 
2223 
2224 
2225 ---BEGIN: ORIGINAL FIX for difference due to ROUNDING --------------------------------------------------|
2226 
2227    l_diff_target   := 0;
2228    l_diff_target_1 := 0;
2229 
2230    BEGIN
2231 
2232        SELECT SUM(p.TARGET) INTO l_diff_target_1
2233        FROM OZF_PRODUCT_ALLOCATIONS p
2234        WHERE p.allocation_for = 'FACT'
2235        AND p.allocation_for_id = l_fact_id;
2236 
2237        l_diff_target := (NVL(l_fact_rec.recommend_total_amount, 0) - NVL(l_diff_target_1, 0));
2238 
2239    EXCEPTION
2240        WHEN OTHERS THEN
2241             l_diff_target := 0;
2242    END;
2243 
2244    IF ABS(l_diff_target) >= 1 THEN
2245 
2246       IF SIGN(l_diff_target) = -1 THEN
2247          l_diff_target := CEIL(l_diff_target); -- (So, -1.5 will become -1 )
2248       ELSE
2249          l_diff_target := FLOOR(l_diff_target); -- (So, +1.5 will become +1)
2250       END IF;
2251 
2252       fix_product_rounding_err('FACT', l_fact_id, l_diff_target);
2253 
2254 /*
2255             UPDATE OZF_TIME_ALLOCATIONS t
2256                 SET t.TARGET = t.TARGET + l_diff_target,
2257                     t.object_version_number = t.object_version_number + 1,
2258                     t.last_update_date = SYSDATE,
2259                     t.last_updated_by = FND_GLOBAL.USER_ID,
2260                     t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
2261               WHERE t.time_allocation_id = (SELECT max(x.time_allocation_id) from OZF_TIME_ALLOCATIONS x
2262                                             WHERE  x.allocation_for = 'PROD'
2263                                             AND x.allocation_for_id IN ( SELECT p.product_allocation_id
2264                                                                          FROM  OZF_PRODUCT_ALLOCATIONS p
2265                                                                          WHERE p.allocation_for = 'FACT'
2266                                                                            AND p.allocation_for_id = l_fact_id
2267                                                                            AND p.item_id = -9999 )
2268                                            );
2269 
2270 
2271               IF (SQL%NOTFOUND) THEN
2272                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
2273                     fnd_message.set_name ('OZF', 'OZF_TP_TIMEALLOC_NOT_FOUND_TXT');
2274                     fnd_msg_pub.ADD;
2275                  END IF;
2276                  RAISE fnd_api.g_exc_unexpected_error;
2277               END IF;
2278 
2279          UPDATE OZF_PRODUCT_ALLOCATIONS p
2280                 SET p.TARGET = p.TARGET + l_diff_target,
2281                     p.object_version_number = p.object_version_number + 1,
2282                     p.last_update_date = SYSDATE,
2283                     p.last_updated_by = FND_GLOBAL.USER_ID,
2284                     p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
2285               WHERE p.allocation_for = 'FACT'
2286                 AND p.allocation_for_id = l_fact_id
2287         AND p.item_id = -9999;
2288 
2289               IF (SQL%NOTFOUND) THEN
2290                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
2291                     fnd_message.set_name ('OZF', 'OZF_TP_PRODALLOC_NOT_FOUND_TXT');
2292                     fnd_msg_pub.ADD;
2293                  END IF;
2294                  RAISE fnd_api.g_exc_unexpected_error;
2295               END IF;
2296 */
2297 
2298 
2299    END IF;
2300 
2301 ---END: ORIGINAL FIX for difference due to ROUNDING --------------------------------------------------|
2302 
2303 
2304 
2305    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Populating Product and Time Allocations Records'
2306                                          || 'FOR Fact_id = '|| l_fact_id || ' ; ');
2307 
2308    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- end');
2309 
2310  EXCEPTION
2311      WHEN OZF_TP_DIFF_TIME_SPREAD THEN
2312           ROLLBACK TO create_fact_product_spread;
2313           FND_MESSAGE.set_name('OZF', 'OZF_TP_DIFF_TIME_SPREAD_TXT');
2314           FND_MSG_PUB.add;
2315           x_return_status := FND_API.g_ret_sts_error ;
2316           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
2317                                      p_data    => x_error_message);
2318           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : TIME SPREAD MISMATCH EXCEPTION = '||sqlerrm(sqlcode));
2319           OZF_UTILITY_PVT.debug_message(fnd_message.get_string('OZF', 'OZF_TP_DIFF_TIME_SPREAD_TXT'));
2320 
2321      WHEN OZF_TP_BLANK_PERIOD_TBL THEN
2322           ROLLBACK TO create_fact_product_spread;
2323           FND_MESSAGE.set_name('OZF', 'OZF_TP_BLANK_PERIOD_TBL_TXT');
2324           FND_MSG_PUB.add;
2325           x_return_status := FND_API.g_ret_sts_error ;
2326           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
2327                                      p_data    => x_error_message);
2328           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : BLANK PERIOD TABLE EXCEPTION = '||sqlerrm(sqlcode));
2329           OZF_UTILITY_PVT.debug_message(fnd_message.get_string('OZF', 'OZF_TP_BLANK_PERIOD_TBL_TXT'));
2330 
2331      WHEN FND_API.G_EXC_ERROR THEN
2332           ROLLBACK TO create_fact_product_spread;
2333           x_return_status := FND_API.g_ret_sts_error ;
2334           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
2335                                      p_data    => x_error_message);
2336 
2337      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2338           ROLLBACK TO create_fact_product_spread;
2339           x_return_status := FND_API.g_ret_sts_unexp_error ;
2340           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
2341                                      p_data  => x_error_message);
2342 
2343      WHEN OTHERS THEN
2344           ROLLBACK TO create_fact_product_spread;
2345           x_return_status := FND_API.g_ret_sts_unexp_error ;
2346           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
2347              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
2348           END IF;
2349           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
2350                                      p_data  => x_error_message);
2351           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
2352 
2353  END create_fact_product_spread;
2354 
2355 
2356 
2357 
2358 
2359 
2360 
2361 -- ------------------------
2362 -- Private Procedure
2363 -- ------------------------
2364 -- ------------------------------------------------------------------
2365 -- Name: create root product spread
2366 -- Desc: Setup product spread for Root Node or any Creator Node
2367 -- -----------------------------------------------------------------
2368  PROCEDURE create_root_product_spread
2369  (
2370     p_api_version        IN          NUMBER,
2371     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
2372     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
2373     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
2374     x_return_status      OUT NOCOPY  VARCHAR2,
2375     x_error_number       OUT NOCOPY  NUMBER,
2376     x_error_message      OUT NOCOPY  VARCHAR2,
2377     p_fund_id            IN          NUMBER
2378  ) IS
2379 
2380    l_api_version   CONSTANT NUMBER       := 1.0;
2381    l_api_name      CONSTANT VARCHAR2(30) := 'create_root_product_spread';
2382    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
2383    l_return_status          VARCHAR2(1);
2384 
2385    l_object_version_number  NUMBER := 1;
2386    l_org_id                 NUMBER := TO_NUMBER(SUBSTRB(USERENV('CLIENT_INFO'), 1, 10));
2387    l_fund_id                NUMBER;
2388    l_start_date             VARCHAR2(30) := null;
2389    l_end_date               VARCHAR2(30) := null;
2390    l_period_type_id         NUMBER;
2391    l_in_clause              VARCHAR2(1000) := null;
2392    l_territory_id           NUMBER;
2393    l_resource_id            NUMBER;
2394    l_lysp_sales             NUMBER;
2395    l_total_lysp_sales       NUMBER;
2396    l_grand_total_lysp_sales NUMBER;
2397    l_total_root_quota       NUMBER;
2398    l_multiplying_factor     NUMBER;
2399    l_product_allocation_id  NUMBER;
2400    l_time_allocation_id     NUMBER;
2401    l_denominator            NUMBER;
2402    l_diff_target            NUMBER;
2403    l_diff_target_1          NUMBER;
2404    l_diff_target_2          NUMBER;
2405 
2406    l_period_tbl          OZF_TIME_API_PVT.G_period_tbl_type;
2407    l_lysp_period_tbl     OZF_TIME_API_PVT.G_period_tbl_type;
2408 
2409    p_prod_alloc_rec      ozf_product_allocations%ROWTYPE;
2410    p_time_alloc_rec      ozf_time_allocations%ROWTYPE;
2411 
2412 
2413 CURSOR fund_csr (l_fund_id  NUMBER) IS
2414  SELECT
2415   owner,
2416   start_period_id,
2417   end_period_id,
2418   start_date_active,
2419   end_date_active,
2420   status_code,
2421   original_budget,
2422   transfered_in_amt,
2423   transfered_out_amt,
2424   node_id, -- (=territory id)
2425   product_spread_time_id period_type_id -- (= minor_scale_id i.e. qtrly or monthly)
2426  FROM
2427   ozf_funds_all_vl
2428  WHERE
2429   fund_id = l_fund_id;
2430 
2431 l_fund_rec    fund_csr%ROWTYPE;
2432 
2433 CURSOR territory_csr (l_resource_id NUMBER) IS
2434  SELECT
2435   j.terr_id territory_id
2436  FROM
2437   jtf_terr_rsc_all j, jtf_terr_rsc_access_all j2
2438  WHERE
2439      j.resource_id = l_resource_id
2440 -- AND j.primary_contact_flag = 'Y' ;
2441  AND j2.terr_rsc_id = j.terr_rsc_id
2442  AND j2.access_type = 'OFFER'
2443  AND j2.trans_access_code = 'PRIMARY_CONTACT';
2444 
2445 l_territory_rec territory_csr%ROWTYPE;
2446 
2447 CURSOR product_elig_csr (l_fund_id NUMBER) IS
2448  SELECT
2449   inventory_item_id
2450  FROM
2451   ams_act_products
2452  WHERE
2453      act_product_used_by_id = l_fund_id
2454  AND arc_act_product_used_by = 'FUND'
2455  AND level_type_code = 'PRODUCT'
2456  AND NVL(excluded_flag,'N') = 'N';
2457 
2458 CURSOR category_elig_csr (l_fund_id NUMBER) IS
2459  SELECT
2460   category_id
2461  FROM
2462   ams_act_products
2463  WHERE
2464      act_product_used_by_id = l_fund_id
2465  AND arc_act_product_used_by = 'FUND'
2466  AND level_type_code = 'FAMILY'
2467  AND NVL(excluded_flag,'N') = 'N';
2468 
2469 
2470 CURSOR excluded_product_elig_csr (l_fund_id NUMBER) IS
2471  SELECT
2472   inventory_item_id
2473  FROM
2474   ams_act_products
2475  WHERE
2476      act_product_used_by_id = l_fund_id
2477  AND arc_act_product_used_by = 'FUND'
2478  AND level_type_code = 'PRODUCT'
2479  AND excluded_flag ='Y';
2480 
2481 CURSOR excluded_category_elig_csr (l_fund_id NUMBER) IS
2482  SELECT
2483   category_id
2484  FROM
2485   ams_act_products
2486  WHERE
2487      act_product_used_by_id = l_fund_id
2488  AND arc_act_product_used_by = 'FUND'
2489  AND level_type_code = 'FAMILY'
2490  AND excluded_flag ='Y';
2491 
2492 
2493 CURSOR product_lysp_sales_csr (l_product_id    NUMBER,
2494                                l_territory_id  NUMBER,
2495                                l_time_id       NUMBER) IS
2496  SELECT
2497   SUM(bsmv.sales) sales
2498  FROM
2499    ozf_order_sales_v bsmv,
2500    ams_party_market_segments a
2501  WHERE
2502       a.market_qualifier_reference = l_territory_id
2503   AND a.market_qualifier_type='TERRITORY'
2504   AND bsmv.ship_to_site_use_id = a.site_use_id
2505   AND bsmv.inventory_item_id = l_product_id
2506   AND bsmv.time_id = l_time_id;
2507 
2508 
2509 CURSOR category_lysp_sales_csr (l_category_id    NUMBER,
2510                                 l_territory_id   NUMBER,
2511                                 l_time_id        NUMBER,
2512                                 l_fund_id        NUMBER) IS
2513 SELECT
2514  SUM(bsmv.sales) sales
2515 FROM
2516  ozf_order_sales_v bsmv,
2517  ams_party_market_segments a
2518 WHERE
2519     a.market_qualifier_reference = l_territory_id
2520 AND a.market_qualifier_type='TERRITORY'
2521 AND bsmv.ship_to_site_use_id = a.site_use_id
2522 AND bsmv.time_id = l_time_id
2523 AND bsmv.inventory_item_id IN
2524                            ( SELECT DISTINCT MIC.INVENTORY_ITEM_ID
2525                              FROM   MTL_ITEM_CATEGORIES     MIC,
2526                                     ENI_PROD_DENORM_HRCHY_V DENORM
2527                              WHERE  MIC.CATEGORY_SET_ID  = DENORM.CATEGORY_SET_ID
2528                               AND   MIC.CATEGORY_ID      = DENORM.CHILD_ID
2529                               AND   DENORM.PARENT_ID     = l_category_id
2530                              MINUS
2531                              SELECT a.inventory_item_id
2532                              FROM  ams_act_products a
2533                              WHERE act_product_used_by_id = l_fund_id
2534                               AND arc_act_product_used_by = 'FUND'
2535                               AND level_type_code = 'PRODUCT'
2536                               AND excluded_flag IN  ('Y', 'N')
2537                            );
2538 
2539 
2540 CURSOR others_lysp_sales_csr (l_territory_id   NUMBER,
2541                               l_time_id        NUMBER,
2542                               l_fund_id        NUMBER) IS
2543 SELECT
2544  SUM(bsmv.sales) sales
2545 FROM
2546  ozf_order_sales_v bsmv,
2547  ams_party_market_segments a
2548 WHERE
2549     a.market_qualifier_reference = l_territory_id
2550 AND a.market_qualifier_type='TERRITORY'
2551 AND bsmv.ship_to_site_use_id = a.site_use_id
2552 AND bsmv.time_id = l_time_id
2553 AND NOT EXISTS
2554 (
2555 ( SELECT prod.inventory_item_id
2556   FROM ams_act_products prod
2557   WHERE
2558       prod.level_type_code = 'PRODUCT'
2559   AND prod.arc_act_product_used_by = 'FUND'
2560   AND prod.act_product_used_by_id = l_fund_id
2561   AND prod.excluded_flag = 'N'
2562   AND prod.inventory_item_id = bsmv.inventory_item_id
2563   UNION ALL
2564   SELECT MIC.INVENTORY_ITEM_ID
2565   FROM   MTL_ITEM_CATEGORIES MIC,
2566          ENI_PROD_DENORM_HRCHY_V DENORM,
2567          AMS_ACT_PRODUCTS prod
2568   WHERE
2569     prod.level_type_code = 'FAMILY'
2570 AND prod.arc_act_product_used_by = 'FUND'
2571 AND prod.act_product_used_by_id = l_fund_id
2572 AND prod.excluded_flag = 'N'
2573 AND prod.category_id = DENORM.PARENT_ID
2574 AND MIC.CATEGORY_SET_ID = DENORM.CATEGORY_SET_ID
2575 AND MIC.CATEGORY_ID = DENORM.CHILD_ID
2576 AND MIC.INVENTORY_ITEM_ID = bsmv.inventory_item_id
2577 )
2578 MINUS
2579 SELECT prod.inventory_item_id
2580 FROM ams_act_products prod
2581 where
2582     prod.level_type_code = 'PRODUCT'
2583 AND prod.arc_act_product_used_by = 'FUND'
2584 AND prod.act_product_used_by_id = l_fund_id
2585 AND prod.excluded_flag = 'Y'
2586 AND prod.inventory_item_id = bsmv.inventory_item_id
2587 );
2588 
2589 
2590  BEGIN
2591 
2592    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- start');
2593 
2594    -- Standard Start of API savepoint
2595    SAVEPOINT create_root_product_spread;
2596 
2597    -- Standard call to check for call compatibility.
2598    IF NOT FND_API.compatible_api_call(l_api_version,
2599                                       p_api_version,
2600                                       l_api_name,
2601                                       g_pkg_name)
2602    THEN
2603      RAISE FND_API.g_exc_unexpected_error;
2604    END IF;
2605 
2606    -- Initialize message list if p_init_msg_list is set to TRUE.
2607    IF FND_API.to_Boolean( p_init_msg_list ) THEN
2608       FND_MSG_PUB.initialize;
2609    END IF;
2610 
2611    -- Initialize API return status to SUCCESS
2612    x_return_status := FND_API.g_ret_sts_success;
2613 
2614    l_fund_id := p_fund_id;
2615 
2616    OPEN fund_csr(l_fund_id);
2617    FETCH fund_csr INTO l_fund_rec;
2618    CLOSE fund_csr ;
2619 
2620    l_resource_id := l_fund_rec.owner;
2621    l_territory_id := l_fund_rec.node_id;
2622 
2623    IF l_territory_id IS NULL THEN
2624       OPEN territory_csr(l_resource_id);
2625       FETCH territory_csr INTO l_territory_id;
2626       CLOSE territory_csr ;
2627    END IF;
2628 
2629    l_start_date := to_char(l_fund_rec.start_date_active, 'YYYY/MM/DD');
2630    l_end_date   := to_char(l_fund_rec.end_date_active, 'YYYY/MM/DD');
2631    l_period_type_id := l_fund_rec.period_type_id;
2632 
2633    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Getting Time_ids Between '
2634                                                  ||l_start_date||' AND '||l_end_date||' ; '
2635                                                  ||' Period_Type_id = '||l_period_type_id||' ; ');
2636 
2637    IF l_start_date IS NULL OR
2638       l_end_date IS NULL OR
2639       l_period_type_id IS NULL
2640    THEN
2641       RAISE OZF_TP_BLANK_PERIOD_TBL;
2642    END IF;
2643 
2644    l_period_tbl := OZF_TIME_API_PVT.get_period_tbl
2645                                     (l_start_date,
2646                                      l_end_date,
2647                                      l_period_type_id);
2648 
2649    IF l_period_tbl IS NULL OR l_period_tbl.COUNT <= 0 THEN
2650       RAISE OZF_TP_BLANK_PERIOD_TBL;
2651    END IF;
2652 
2653    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Getting Time_ids Between '||l_start_date
2654                                          ||' AND '||l_end_date||' ; ');
2655    IF l_period_tbl IS NOT NULL THEN
2656     IF l_period_tbl.COUNT > 0 THEN
2657       FOR l_idx IN l_period_tbl.first..l_period_tbl.last
2658       LOOP
2659         IF l_period_tbl.exists(l_idx) THEN
2660 
2661          l_lysp_period_tbl(l_idx) := OZF_TIME_API_PVT.get_lysp_id (l_period_tbl(l_idx), l_period_type_id);
2662 
2663          OZF_UTILITY_PVT.debug_message(SubStr('l_period_tbl('||TO_CHAR(l_idx)||') = '
2664                                ||TO_CHAR(l_period_tbl(l_idx)), 1,255));
2665          OZF_UTILITY_PVT.debug_message(SubStr('l_lysp_period_tbl('||TO_CHAR(l_idx)||') = '
2666                                ||TO_CHAR(l_lysp_period_tbl(l_idx)), 1,255));
2667         END IF;
2668       END LOOP;
2669     END IF;
2670    END IF;
2671 
2672 
2673    l_in_clause := '(';
2674    IF l_lysp_period_tbl IS NOT NULL THEN
2675       FOR l_idx IN l_lysp_period_tbl.first..l_lysp_period_tbl.last
2676       LOOP
2677         IF l_lysp_period_tbl.exists(l_idx) THEN
2678           IF l_in_clause = '(' THEN
2679              l_in_clause := l_in_clause || l_lysp_period_tbl(l_idx);
2680           ELSE
2681              l_in_clause := l_in_clause ||','|| l_lysp_period_tbl(l_idx);
2682           END IF;
2683         END IF;
2684       END LOOP;
2685    END IF;
2686    l_in_clause := l_in_clause||')';
2687 
2688    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': l_in_clause == '||l_in_clause);
2689 
2690 ------- Insert rows for PRODUCTS  ------------------------------------------------
2691 
2692    l_grand_total_lysp_sales := 0;
2693 
2694    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Populating Product and Time Allocations Records'
2695                                          || 'FOR Fund_id = '|| l_fund_id || ' ; ');
2696 
2697    FOR product_rec IN product_elig_csr(l_fund_id)
2698    LOOP
2699        p_prod_alloc_rec := NULL;
2700 
2701        l_product_allocation_id := get_product_allocation_id;
2702 
2703        p_prod_alloc_rec.allocation_for := 'FUND';
2704        p_prod_alloc_rec.allocation_for_id := l_fund_id;
2705        p_prod_alloc_rec.fund_id := l_fund_id;
2706        p_prod_alloc_rec.item_type := 'PRICING_ATTRIBUTE1';
2707        p_prod_alloc_rec.item_id := product_rec.inventory_item_id;
2708        p_prod_alloc_rec.selected_flag := 'N';
2709        p_prod_alloc_rec.target := 0;
2710        p_prod_alloc_rec.lysp_sales := 0;
2711 
2712 
2713        Ozf_Product_Allocations_Pkg.Insert_Row(
2714           px_product_allocation_id  => l_product_allocation_id,
2715           p_allocation_for  => p_prod_alloc_rec.allocation_for,
2716           p_allocation_for_id  => p_prod_alloc_rec.allocation_for_id,
2717           p_fund_id  => p_prod_alloc_rec.fund_id,
2718           p_item_type  => p_prod_alloc_rec.item_type,
2719           p_item_id  => p_prod_alloc_rec.item_id,
2720           p_selected_flag  => p_prod_alloc_rec.selected_flag,
2721           p_target  => NVL(p_prod_alloc_rec.target, 0),
2722           p_lysp_sales  => NVL(p_prod_alloc_rec.lysp_sales, 0),
2723           p_parent_product_allocation_id  => p_prod_alloc_rec.parent_product_allocation_id,
2724           px_object_version_number  => l_object_version_number,
2725           p_creation_date  => SYSDATE,
2726           p_created_by  => FND_GLOBAL.USER_ID,
2727           p_last_update_date  => SYSDATE,
2728           p_last_updated_by  => FND_GLOBAL.USER_ID,
2729           p_last_update_login  => FND_GLOBAL.conc_login_id,
2730           p_attribute_category  => p_prod_alloc_rec.attribute_category,
2731           p_attribute1  => p_prod_alloc_rec.attribute1,
2732           p_attribute2  => p_prod_alloc_rec.attribute2,
2733           p_attribute3  => p_prod_alloc_rec.attribute3,
2734           p_attribute4  => p_prod_alloc_rec.attribute4,
2735           p_attribute5  => p_prod_alloc_rec.attribute5,
2736           p_attribute6  => p_prod_alloc_rec.attribute6,
2737           p_attribute7  => p_prod_alloc_rec.attribute7,
2738           p_attribute8  => p_prod_alloc_rec.attribute8,
2739           p_attribute9  => p_prod_alloc_rec.attribute9,
2740           p_attribute10  => p_prod_alloc_rec.attribute10,
2741           p_attribute11  => p_prod_alloc_rec.attribute11,
2742           p_attribute12  => p_prod_alloc_rec.attribute12,
2743           p_attribute13  => p_prod_alloc_rec.attribute13,
2744           p_attribute14  => p_prod_alloc_rec.attribute14,
2745           p_attribute15  => p_prod_alloc_rec.attribute15,
2746           px_org_id  => l_org_id
2747         );
2748 
2749 
2750        l_total_lysp_sales := 0;
2751 
2752        FOR l_idx IN l_period_tbl.first..l_period_tbl.last
2753        LOOP
2754         IF l_period_tbl.exists(l_idx) THEN
2755 
2756            p_time_alloc_rec := NULL;
2757 
2758            l_lysp_sales := 0;
2759            OPEN product_lysp_sales_csr(product_rec.inventory_item_id,
2760                                        l_territory_id,
2761                                        l_lysp_period_tbl(l_idx)
2762                                       );
2763            FETCH product_lysp_sales_csr INTO l_lysp_sales;
2764            CLOSE product_lysp_sales_csr;
2765 
2766            l_lysp_sales := NVL(l_lysp_sales, 0);
2767 
2768            l_total_lysp_sales := l_total_lysp_sales + l_lysp_sales;
2769 
2770            l_time_allocation_id := get_time_allocation_id;
2771 
2772            p_time_alloc_rec.allocation_for := 'PROD';
2773            p_time_alloc_rec.allocation_for_id := l_product_allocation_id;
2774            p_time_alloc_rec.time_id := l_period_tbl(l_idx);
2775            p_time_alloc_rec.period_type_id := l_period_type_id;
2776            p_time_alloc_rec.target := 0;
2777            p_time_alloc_rec.lysp_sales := l_lysp_sales;
2778 
2779 
2780            Ozf_Time_Allocations_Pkg.Insert_Row(
2781               px_time_allocation_id  => l_time_allocation_id,
2782               p_allocation_for  => p_time_alloc_rec.allocation_for,
2783               p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
2784               p_time_id  => p_time_alloc_rec.time_id,
2785               p_period_type_id => p_time_alloc_rec.period_type_id,
2786               p_target  =>NVL( p_time_alloc_rec.target, 0),
2787               p_lysp_sales  => NVL(p_time_alloc_rec.lysp_sales, 0),
2788               px_object_version_number  => l_object_version_number,
2789               p_creation_date  => SYSDATE,
2790               p_created_by  => FND_GLOBAL.USER_ID,
2791               p_last_update_date  => SYSDATE,
2792               p_last_updated_by  => FND_GLOBAL.USER_ID,
2793               p_last_update_login  => FND_GLOBAL.conc_login_id,
2794               p_attribute_category  => p_time_alloc_rec.attribute_category,
2795               p_attribute1  => p_time_alloc_rec.attribute1,
2796               p_attribute2  => p_time_alloc_rec.attribute2,
2797               p_attribute3  => p_time_alloc_rec.attribute3,
2798               p_attribute4  => p_time_alloc_rec.attribute4,
2799               p_attribute5  => p_time_alloc_rec.attribute5,
2800               p_attribute6  => p_time_alloc_rec.attribute6,
2801               p_attribute7  => p_time_alloc_rec.attribute7,
2802               p_attribute8  => p_time_alloc_rec.attribute8,
2803               p_attribute9  => p_time_alloc_rec.attribute9,
2804               p_attribute10  => p_time_alloc_rec.attribute10,
2805               p_attribute11  => p_time_alloc_rec.attribute11,
2806               p_attribute12  => p_time_alloc_rec.attribute12,
2807               p_attribute13  => p_time_alloc_rec.attribute13,
2808               p_attribute14  => p_time_alloc_rec.attribute14,
2809               p_attribute15  => p_time_alloc_rec.attribute15,
2810               px_org_id  => l_org_id
2811             );
2812 
2813 
2814          END IF;
2815        END LOOP;
2816 
2817        UPDATE OZF_PRODUCT_ALLOCATIONS p
2818        SET p.lysp_sales = NVL(l_total_lysp_sales,0),
2819            p.object_version_number = p.object_version_number + 1,
2820            p.last_update_date = SYSDATE,
2821            p.last_updated_by = FND_GLOBAL.USER_ID,
2822            p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
2823        WHERE p.product_allocation_id = l_product_allocation_id;
2824 
2825        l_grand_total_lysp_sales := l_grand_total_lysp_sales + l_total_lysp_sales;
2826    END LOOP;
2827 
2828 
2829 ------- Insert rows for CATEGORIES  ----------------------------------------------
2830 
2831 
2832    FOR category_rec IN category_elig_csr(l_fund_id)
2833    LOOP
2834 
2835        p_prod_alloc_rec := NULL;
2836 
2837        l_product_allocation_id := get_product_allocation_id;
2838 
2839 
2840        p_prod_alloc_rec.allocation_for := 'FUND';
2841        p_prod_alloc_rec.allocation_for_id := l_fund_id;
2842        p_prod_alloc_rec.fund_id := l_fund_id;
2843        p_prod_alloc_rec.item_type := 'PRICING_ATTRIBUTE2';
2844        p_prod_alloc_rec.item_id := category_rec.category_id;
2845        p_prod_alloc_rec.selected_flag := 'N';
2846        p_prod_alloc_rec.target := 0;
2847        p_prod_alloc_rec.lysp_sales := 0;
2848 
2849 
2850        Ozf_Product_Allocations_Pkg.Insert_Row(
2851           px_product_allocation_id  => l_product_allocation_id,
2852           p_allocation_for  => p_prod_alloc_rec.allocation_for,
2853           p_allocation_for_id  => p_prod_alloc_rec.allocation_for_id,
2854           p_fund_id  => p_prod_alloc_rec.fund_id,
2855           p_item_type  => p_prod_alloc_rec.item_type,
2856           p_item_id  => p_prod_alloc_rec.item_id,
2857           p_selected_flag  => p_prod_alloc_rec.selected_flag,
2858           p_target  => NVL(p_prod_alloc_rec.target,0),
2859           p_lysp_sales  => NVL(p_prod_alloc_rec.lysp_sales, 0),
2860           p_parent_product_allocation_id  => p_prod_alloc_rec.parent_product_allocation_id,
2861           px_object_version_number  => l_object_version_number,
2862           p_creation_date  => SYSDATE,
2863           p_created_by  => FND_GLOBAL.USER_ID,
2864           p_last_update_date  => SYSDATE,
2865           p_last_updated_by  => FND_GLOBAL.USER_ID,
2866           p_last_update_login  => FND_GLOBAL.conc_login_id,
2867           p_attribute_category  => p_prod_alloc_rec.attribute_category,
2868           p_attribute1  => p_prod_alloc_rec.attribute1,
2869           p_attribute2  => p_prod_alloc_rec.attribute2,
2870           p_attribute3  => p_prod_alloc_rec.attribute3,
2871           p_attribute4  => p_prod_alloc_rec.attribute4,
2872           p_attribute5  => p_prod_alloc_rec.attribute5,
2873           p_attribute6  => p_prod_alloc_rec.attribute6,
2874           p_attribute7  => p_prod_alloc_rec.attribute7,
2875           p_attribute8  => p_prod_alloc_rec.attribute8,
2876           p_attribute9  => p_prod_alloc_rec.attribute9,
2877           p_attribute10  => p_prod_alloc_rec.attribute10,
2878           p_attribute11  => p_prod_alloc_rec.attribute11,
2879           p_attribute12  => p_prod_alloc_rec.attribute12,
2880           p_attribute13  => p_prod_alloc_rec.attribute13,
2881           p_attribute14  => p_prod_alloc_rec.attribute14,
2882           p_attribute15  => p_prod_alloc_rec.attribute15,
2883           px_org_id  => l_org_id
2884         );
2885 
2886        l_total_lysp_sales := 0;
2887 
2888        FOR l_idx IN l_period_tbl.first..l_period_tbl.last
2889        LOOP
2890         IF l_period_tbl.exists(l_idx) THEN
2891 
2892            p_time_alloc_rec := NULL;
2893 
2894            l_lysp_sales := 0;
2895            OPEN category_lysp_sales_csr(category_rec.category_id,
2896                                         l_territory_id,
2897                                         l_lysp_period_tbl(l_idx),
2898                                         l_fund_id
2899                                        );
2900            FETCH category_lysp_sales_csr INTO l_lysp_sales;
2901            CLOSE category_lysp_sales_csr;
2902 
2903            l_lysp_sales := NVL(l_lysp_sales, 0);
2904 
2905            l_total_lysp_sales := l_total_lysp_sales + l_lysp_sales;
2906 
2907            l_time_allocation_id := get_time_allocation_id;
2908 
2909            p_time_alloc_rec.allocation_for := 'PROD';
2910            p_time_alloc_rec.allocation_for_id := l_product_allocation_id;
2911            p_time_alloc_rec.time_id := l_period_tbl(l_idx);
2912            p_time_alloc_rec.period_type_id := l_period_type_id;
2913            p_time_alloc_rec.target := 0;
2914            p_time_alloc_rec.lysp_sales := l_lysp_sales;
2915 
2916 
2917            Ozf_Time_Allocations_Pkg.Insert_Row(
2918               px_time_allocation_id  => l_time_allocation_id,
2919               p_allocation_for  => p_time_alloc_rec.allocation_for,
2920               p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
2921               p_time_id  => p_time_alloc_rec.time_id,
2922               p_period_type_id => p_time_alloc_rec.period_type_id,
2923               p_target  => NVL(p_time_alloc_rec.target, 0),
2924               p_lysp_sales  => NVL(p_time_alloc_rec.lysp_sales, 0),
2925               px_object_version_number  => l_object_version_number,
2926               p_creation_date  => SYSDATE,
2927               p_created_by  => FND_GLOBAL.USER_ID,
2928               p_last_update_date  => SYSDATE,
2929               p_last_updated_by  => FND_GLOBAL.USER_ID,
2930               p_last_update_login  => FND_GLOBAL.conc_login_id,
2931               p_attribute_category  => p_time_alloc_rec.attribute_category,
2932               p_attribute1  => p_time_alloc_rec.attribute1,
2933               p_attribute2  => p_time_alloc_rec.attribute2,
2934               p_attribute3  => p_time_alloc_rec.attribute3,
2935               p_attribute4  => p_time_alloc_rec.attribute4,
2936               p_attribute5  => p_time_alloc_rec.attribute5,
2937               p_attribute6  => p_time_alloc_rec.attribute6,
2938               p_attribute7  => p_time_alloc_rec.attribute7,
2939               p_attribute8  => p_time_alloc_rec.attribute8,
2940               p_attribute9  => p_time_alloc_rec.attribute9,
2941               p_attribute10  => p_time_alloc_rec.attribute10,
2942               p_attribute11  => p_time_alloc_rec.attribute11,
2943               p_attribute12  => p_time_alloc_rec.attribute12,
2944               p_attribute13  => p_time_alloc_rec.attribute13,
2945               p_attribute14  => p_time_alloc_rec.attribute14,
2946               p_attribute15  => p_time_alloc_rec.attribute15,
2947               px_org_id  => l_org_id
2948             );
2949 
2950 
2951          END IF;
2952        END LOOP;
2953 
2954        UPDATE OZF_PRODUCT_ALLOCATIONS p
2955        SET p.lysp_sales = NVL(l_total_lysp_sales, 0),
2956            p.object_version_number = p.object_version_number + 1,
2957            p.last_update_date = SYSDATE,
2958            p.last_updated_by = FND_GLOBAL.USER_ID,
2959            p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
2960        WHERE p.product_allocation_id = l_product_allocation_id;
2961 
2962        l_grand_total_lysp_sales := l_grand_total_lysp_sales + l_total_lysp_sales;
2963    END LOOP;
2964 
2965 
2966 
2967 ------- Insert rows for OTHERS ---------------------------------------------------
2968 
2969        p_prod_alloc_rec := NULL;
2970 
2971        l_product_allocation_id := get_product_allocation_id;
2972 
2973        p_prod_alloc_rec.allocation_for := 'FUND';
2974        p_prod_alloc_rec.allocation_for_id := l_fund_id;
2975        p_prod_alloc_rec.fund_id := l_fund_id;
2976        p_prod_alloc_rec.item_type := 'OTHERS';
2977        p_prod_alloc_rec.item_id := -9999;
2978        p_prod_alloc_rec.selected_flag := 'N';
2979        p_prod_alloc_rec.target := 0;
2980        p_prod_alloc_rec.lysp_sales := 0;
2981 
2982 
2983        Ozf_Product_Allocations_Pkg.Insert_Row(
2984           px_product_allocation_id  => l_product_allocation_id,
2985           p_allocation_for  => p_prod_alloc_rec.allocation_for,
2986           p_allocation_for_id  => p_prod_alloc_rec.allocation_for_id,
2987           p_fund_id  => p_prod_alloc_rec.fund_id,
2988           p_item_type  => p_prod_alloc_rec.item_type,
2989           p_item_id  => p_prod_alloc_rec.item_id,
2990           p_selected_flag  => p_prod_alloc_rec.selected_flag,
2991           p_target  => NVL(p_prod_alloc_rec.target, 0),
2992           p_lysp_sales  => NVL(p_prod_alloc_rec.lysp_sales, 0),
2993           p_parent_product_allocation_id  => p_prod_alloc_rec.parent_product_allocation_id,
2994           px_object_version_number  => l_object_version_number,
2995           p_creation_date  => SYSDATE,
2996           p_created_by  => FND_GLOBAL.USER_ID,
2997           p_last_update_date  => SYSDATE,
2998           p_last_updated_by  => FND_GLOBAL.USER_ID,
2999           p_last_update_login  => FND_GLOBAL.conc_login_id,
3000           p_attribute_category  => p_prod_alloc_rec.attribute_category,
3001           p_attribute1  => p_prod_alloc_rec.attribute1,
3002           p_attribute2  => p_prod_alloc_rec.attribute2,
3003           p_attribute3  => p_prod_alloc_rec.attribute3,
3004           p_attribute4  => p_prod_alloc_rec.attribute4,
3005           p_attribute5  => p_prod_alloc_rec.attribute5,
3006           p_attribute6  => p_prod_alloc_rec.attribute6,
3007           p_attribute7  => p_prod_alloc_rec.attribute7,
3008           p_attribute8  => p_prod_alloc_rec.attribute8,
3009           p_attribute9  => p_prod_alloc_rec.attribute9,
3010           p_attribute10  => p_prod_alloc_rec.attribute10,
3011           p_attribute11  => p_prod_alloc_rec.attribute11,
3012           p_attribute12  => p_prod_alloc_rec.attribute12,
3013           p_attribute13  => p_prod_alloc_rec.attribute13,
3014           p_attribute14  => p_prod_alloc_rec.attribute14,
3015           p_attribute15  => p_prod_alloc_rec.attribute15,
3016           px_org_id  => l_org_id
3017         );
3018    l_total_lysp_sales := 0;
3019 
3020    FOR l_idx IN l_period_tbl.first..l_period_tbl.last
3021    LOOP
3022      IF l_period_tbl.exists(l_idx) THEN
3023 
3024         p_time_alloc_rec := NULL;
3025 
3026         l_lysp_sales := 0;
3027         OPEN others_lysp_sales_csr(l_territory_id,
3028                                    l_lysp_period_tbl(l_idx),
3029                                    l_fund_id
3030                                   );
3031         FETCH others_lysp_sales_csr INTO l_lysp_sales;
3032         CLOSE others_lysp_sales_csr;
3033 
3034         l_lysp_sales := NVL(l_lysp_sales, 0);
3035 
3036         l_total_lysp_sales := l_total_lysp_sales + l_lysp_sales;
3037 
3038         l_time_allocation_id := get_time_allocation_id;
3039 
3040         p_time_alloc_rec.allocation_for := 'PROD';
3041         p_time_alloc_rec.allocation_for_id := l_product_allocation_id;
3042         p_time_alloc_rec.time_id := l_period_tbl(l_idx);
3043         p_time_alloc_rec.period_type_id := l_period_type_id;
3044         p_time_alloc_rec.target := 0;
3045         p_time_alloc_rec.lysp_sales := l_lysp_sales;
3046 
3047 
3048         Ozf_Time_Allocations_Pkg.Insert_Row(
3049            px_time_allocation_id  => l_time_allocation_id,
3050            p_allocation_for  => p_time_alloc_rec.allocation_for,
3051            p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
3052            p_time_id  => p_time_alloc_rec.time_id,
3053            p_period_type_id => p_time_alloc_rec.period_type_id,
3054            p_target  => NVL(p_time_alloc_rec.target, 0),
3055            p_lysp_sales  => NVL(p_time_alloc_rec.lysp_sales, 0),
3056            px_object_version_number  => l_object_version_number,
3057            p_creation_date  => SYSDATE,
3058            p_created_by  => FND_GLOBAL.USER_ID,
3059            p_last_update_date  => SYSDATE,
3060            p_last_updated_by  => FND_GLOBAL.USER_ID,
3061            p_last_update_login  => FND_GLOBAL.conc_login_id,
3062            p_attribute_category  => p_time_alloc_rec.attribute_category,
3063            p_attribute1  => p_time_alloc_rec.attribute1,
3064            p_attribute2  => p_time_alloc_rec.attribute2,
3065            p_attribute3  => p_time_alloc_rec.attribute3,
3066            p_attribute4  => p_time_alloc_rec.attribute4,
3067            p_attribute5  => p_time_alloc_rec.attribute5,
3068            p_attribute6  => p_time_alloc_rec.attribute6,
3069            p_attribute7  => p_time_alloc_rec.attribute7,
3070            p_attribute8  => p_time_alloc_rec.attribute8,
3071            p_attribute9  => p_time_alloc_rec.attribute9,
3072            p_attribute10  => p_time_alloc_rec.attribute10,
3073            p_attribute11  => p_time_alloc_rec.attribute11,
3074            p_attribute12  => p_time_alloc_rec.attribute12,
3075            p_attribute13  => p_time_alloc_rec.attribute13,
3076            p_attribute14  => p_time_alloc_rec.attribute14,
3077            p_attribute15  => p_time_alloc_rec.attribute15,
3078            px_org_id  => l_org_id
3079          );
3080 
3081 
3082       END IF;
3083     END LOOP;
3084 
3085     UPDATE OZF_PRODUCT_ALLOCATIONS p
3086     SET p.lysp_sales = NVL(l_total_lysp_sales, 0),
3087         p.object_version_number = p.object_version_number + 1,
3088         p.last_update_date = SYSDATE,
3089         p.last_updated_by = FND_GLOBAL.USER_ID,
3090         p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
3091     WHERE p.product_allocation_id = l_product_allocation_id;
3092 
3093     l_grand_total_lysp_sales := l_grand_total_lysp_sales + l_total_lysp_sales;
3094 
3095 
3096 
3097    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Populating Product and Time Allocations Records'
3098                                          || 'FOR Fund_id = '|| l_fund_id || ' ; ');
3099 
3100 ------ Updating Target ----------------------------------------------------------------
3101 
3102    l_total_root_quota := 0;
3103    l_total_root_quota := NVL(l_fund_rec.original_budget, 0) + NVL(l_fund_rec.transfered_in_amt, 0);
3104 
3105    l_total_root_quota := NVL(l_total_root_quota, 0);
3106 
3107    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Total Root Quota'
3108                                          || 'FOR Fund_id = '|| l_fund_id || 'is == ' ||l_total_root_quota||' ;');
3109 
3110    l_multiplying_factor := 0;
3111    IF l_grand_total_lysp_sales > 0 THEN
3112       l_multiplying_factor := l_total_root_quota / l_grand_total_lysp_sales;
3113    ELSE
3114       l_multiplying_factor := 0;
3115    END IF;
3116 
3117    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Multiplying Factor == '
3118                                          ||l_multiplying_factor||' ;');
3119 /*
3120    UPDATE OZF_PRODUCT_ALLOCATIONS p
3121    SET p.TARGET = ROUND( (NVL(p.LYSP_SALES, 0) * l_multiplying_factor), 0),
3122        p.object_version_number = p.object_version_number + 1,
3123        p.last_update_date = SYSDATE,
3124        p.last_updated_by = FND_GLOBAL.USER_ID,
3125        p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
3126    WHERE p.allocation_for = 'FUND'
3127      AND p.allocation_for_id = l_fund_id;
3128 
3129    IF (SQL%NOTFOUND) THEN
3130       IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
3131          fnd_message.set_name ('OZF', 'OZF_TP_PRODALLOC_NOT_FOUND_TXT');
3132          fnd_msg_pub.ADD;
3133       END IF;
3134 
3135       RAISE fnd_api.g_exc_unexpected_error;
3136    END IF;
3137 */
3138 
3139    UPDATE OZF_TIME_ALLOCATIONS t
3140    SET t.TARGET = ROUND((NVL(t.LYSP_SALES, 0) * l_multiplying_factor), 0),
3141        t.object_version_number = t.object_version_number + 1,
3142        t.last_update_date = SYSDATE,
3143        t.last_updated_by = FND_GLOBAL.USER_ID,
3144        t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
3145    WHERE  t.allocation_for = 'PROD'
3146    AND t.allocation_for_id IN ( SELECT p.product_allocation_id
3147                                  FROM  OZF_PRODUCT_ALLOCATIONS p
3148                                  WHERE p.allocation_for = 'FUND'
3149                                    AND p.allocation_for_id = l_fund_id );
3150 
3151    IF (SQL%NOTFOUND) THEN
3152       IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
3153          fnd_message.set_name ('OZF', 'OZF_TP_TIMEALLOC_NOT_FOUND_TXT');
3154          fnd_msg_pub.ADD;
3155       END IF;
3156 
3157       RAISE fnd_api.g_exc_unexpected_error;
3158    END IF;
3159 
3160 
3161    UPDATE OZF_PRODUCT_ALLOCATIONS p
3162    SET p.TARGET = (SELECT SUM(ti.TARGET)
3163                      FROM OZF_TIME_ALLOCATIONS ti
3164                     WHERE ti.ALLOCATION_FOR = 'PROD'
3165                       AND ti.ALLOCATION_FOR_ID = p.PRODUCT_ALLOCATION_ID),
3166        p.object_version_number = p.object_version_number + 1,
3167        p.last_update_date = SYSDATE,
3168        p.last_updated_by = FND_GLOBAL.USER_ID,
3169        p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
3170    WHERE p.allocation_for = 'FUND'
3171      AND p.allocation_for_id = l_fund_id;
3172 
3173    IF (SQL%NOTFOUND) THEN
3174       IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
3175          fnd_message.set_name ('OZF', 'OZF_TP_PRODALLOC_NOT_FOUND_TXT');
3176          fnd_msg_pub.ADD;
3177       END IF;
3178 
3179       RAISE fnd_api.g_exc_unexpected_error;
3180    END IF;
3181 
3182 
3183 
3184 
3185 
3186    -- Handling Corner Case : If all products including others have ZERO lysp sales, then
3187    --                        OTHERS get all the Quota. It is equally distributed in all
3188    --                        time periods.
3189 
3190    IF l_multiplying_factor = 0 THEN
3191 
3192       l_denominator := l_period_tbl.COUNT;
3193 /*
3194       UPDATE OZF_PRODUCT_ALLOCATIONS p
3195       SET p.TARGET = ROUND( l_total_root_quota, 0),
3196           p.object_version_number = p.object_version_number + 1,
3197           p.last_update_date = SYSDATE,
3198           p.last_updated_by = FND_GLOBAL.USER_ID,
3199           p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
3200       WHERE p.allocation_for = 'FUND'
3201         AND p.allocation_for_id = l_fund_id
3202         AND p.item_id = -9999;
3203 
3204       IF (SQL%NOTFOUND) THEN
3205          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
3206             fnd_message.set_name ('OZF', 'OZF_TP_PRODALLOC_NOT_FOUND_TXT');
3207             fnd_msg_pub.ADD;
3208          END IF;
3209 
3210          RAISE fnd_api.g_exc_unexpected_error;
3211       END IF;
3212 */
3213       UPDATE OZF_TIME_ALLOCATIONS t
3214       SET t.TARGET = ROUND((l_total_root_quota / l_denominator), 0),
3215           t.object_version_number = t.object_version_number + 1,
3216           t.last_update_date = SYSDATE,
3217           t.last_updated_by = FND_GLOBAL.USER_ID,
3218           t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
3219       WHERE  t.allocation_for = 'PROD'
3220       AND t.allocation_for_id IN ( SELECT p.product_allocation_id
3221                                     FROM  OZF_PRODUCT_ALLOCATIONS p
3222                                     WHERE p.allocation_for = 'FUND'
3223                                       AND p.allocation_for_id = l_fund_id
3224                                       AND p.item_id = -9999 );
3225 
3226       IF (SQL%NOTFOUND) THEN
3227          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
3228             fnd_message.set_name ('OZF', 'OZF_TP_TIMEALLOC_NOT_FOUND_TXT');
3229             fnd_msg_pub.ADD;
3230          END IF;
3231          RAISE fnd_api.g_exc_unexpected_error;
3232       END IF;
3233 
3234 
3235       UPDATE OZF_PRODUCT_ALLOCATIONS p
3236       SET p.TARGET = (SELECT SUM(ti.TARGET)
3237                         FROM OZF_TIME_ALLOCATIONS ti
3238                        WHERE ti.ALLOCATION_FOR = 'PROD'
3239                          AND ti.ALLOCATION_FOR_ID = p.PRODUCT_ALLOCATION_ID),
3240           p.object_version_number = p.object_version_number + 1,
3241           p.last_update_date = SYSDATE,
3242           p.last_updated_by = FND_GLOBAL.USER_ID,
3243           p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
3244       WHERE p.allocation_for = 'FUND'
3245         AND p.allocation_for_id = l_fund_id
3246         AND p.item_id = -9999;
3247 
3248       IF (SQL%NOTFOUND) THEN
3249          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
3250             fnd_message.set_name ('OZF', 'OZF_TP_PRODALLOC_NOT_FOUND_TXT');
3251             fnd_msg_pub.ADD;
3252          END IF;
3253 
3254          RAISE fnd_api.g_exc_unexpected_error;
3255       END IF;
3256 
3257 
3258 -------BEGIN: ORIGINAL FIX for difference due to ROUNDING --------------------------------------------------|
3259 
3260 
3261 /*
3262       IF (MOD(l_total_root_quota,l_denominator) <> 0) THEN
3263 
3264          l_diff_target := 0;
3265 
3266          BEGIN
3267 
3268               SELECT p.TARGET INTO l_diff_target_1
3269               FROM OZF_PRODUCT_ALLOCATIONS p
3270                WHERE p.allocation_for = 'FUND'
3271                  AND p.allocation_for_id = l_fund_id
3272                  AND p.item_id = -9999;
3273 
3274 
3275               SELECT SUM(t.TARGET) INTO l_diff_target_2
3276               FROM OZF_TIME_ALLOCATIONS t
3277               WHERE  t.allocation_for = 'PROD'
3278                  AND t.allocation_for_id IN ( SELECT p.product_allocation_id
3279                                               FROM  OZF_PRODUCT_ALLOCATIONS p
3280                                               WHERE p.allocation_for = 'FUND'
3281                                                 AND p.allocation_for_id = l_fund_id
3282                                                 AND p.item_id = -9999 );
3283 
3284              l_diff_target := NVL(l_diff_target_1, 0) - NVL(l_diff_target_2, 0);
3285 
3286          EXCEPTION
3287             WHEN OTHERS THEN
3288                  l_diff_target := 0;
3289          END;
3290 */
3291    END IF;
3292 
3293 
3294 /*
3295 -------THIS Works---------------------------------------------------------------------
3296             SELECT (a.target-b.target) INTO l_diff_target
3297             FROM
3298             (
3299               SELECT p.TARGET target
3300               FROM OZF_PRODUCT_ALLOCATIONS p
3301                WHERE p.allocation_for = 'FUND'
3302                  AND p.allocation_for_id = l_fund_id
3303                  AND p.item_id = -9999
3304             ) a,
3305             (
3306               SELECT SUM(t.TARGET) target
3307               FROM OZF_TIME_ALLOCATIONS t
3308               WHERE  t.allocation_for = 'PROD'
3309                  AND t.allocation_for_id IN ( SELECT p.product_allocation_id
3310                                               FROM  OZF_PRODUCT_ALLOCATIONS p
3311                                               WHERE p.allocation_for = 'FUND'
3312                                                 AND p.allocation_for_id = l_fund_id
3313                                                 AND p.item_id = -9999 )
3314             ) b;
3315 
3316 */
3317 
3318 /*
3319          BEGIN
3320             SELECT
3321             (
3322               SELECT p.TARGET
3323               FROM OZF_PRODUCT_ALLOCATIONS p
3324                WHERE p.allocation_for = 'FUND'
3325                  AND p.allocation_for_id = l_fund_id
3326                  AND p.item_id = -9999
3327             )
3328             -
3329             (
3330               SELECT SUM(t.TARGET)
3331               FROM OZF_TIME_ALLOCATIONS t
3332               WHERE  t.allocation_for = 'PROD'
3333                  AND t.allocation_for_id IN ( SELECT p.product_allocation_id
3334                                               FROM  OZF_PRODUCT_ALLOCATIONS p
3335                                               WHERE p.allocation_for = 'FUND'
3336                                                 AND p.allocation_for_id = l_fund_id
3337                                                 AND p.item_id = -9999 )
3338             ) diff_target INTO l_diff_target
3339             FROM DUAL;
3340          EXCEPTION
3341             WHEN OTHERS THEN
3342                  l_diff_target := 0;
3343          END;
3344 
3345          OPEN fix_rounding_csr(fund_id);
3346          FETCH fix_rounding_csr INTO l_diff_target;
3347          CLOSE fix_rounding_csr;
3348          l_diff_target := NVL(l_diff_target, 0);
3349 */
3350 
3351 
3352 
3353        l_diff_target   := 0;
3354        l_diff_target_1 := 0;
3355 
3356        BEGIN
3357 
3358            SELECT SUM(p.TARGET) INTO l_diff_target_1
3359            FROM OZF_PRODUCT_ALLOCATIONS p
3360            WHERE p.allocation_for = 'FUND'
3361            AND p.allocation_for_id = l_fund_id;
3362 
3363            l_diff_target := (NVL(l_total_root_quota, 0) - NVL(l_diff_target_1, 0));
3364 
3365        EXCEPTION
3366            WHEN OTHERS THEN
3367                 l_diff_target := 0;
3368        END;
3369 
3370 
3371        IF ABS(l_diff_target) >= 1 THEN
3372 
3373           IF SIGN(l_diff_target) = -1 THEN
3374              l_diff_target := CEIL(l_diff_target); -- (So, -1.5 will become -1 )
3375           ELSE
3376              l_diff_target := FLOOR(l_diff_target); -- (So, +1.5 will become +1)
3377           END IF;
3378 
3379           fix_product_rounding_err('FUND', l_fund_id, l_diff_target);
3380 
3381        END IF;
3382 
3383 --------END: ORIGINAL FIX for difference due to ROUNDING --------------------------------------------------|
3384 
3385 
3386 /*
3387 --------BEGIN : NEW FIX for difference due to ROUNDING --------------------------------------------------|
3388 
3389       l_diff_target := 0;
3390       l_diff_target := MOD(l_total_root_quota,l_denominator);
3391 
3392       IF l_diff_target <> 0 THEN
3393           UPDATE OZF_TIME_ALLOCATIONS t
3394             SET t.TARGET = t.TARGET + l_diff_target,
3395                 t.object_version_number = t.object_version_number + 1,
3396                 t.last_update_date = SYSDATE,
3397                 t.last_updated_by = FND_GLOBAL.USER_ID,
3398                 t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
3399           WHERE t.time_allocation_id = (SELECT max(x.time_allocation_id) from OZF_TIME_ALLOCATIONS x
3400                                         WHERE  x.allocation_for = 'PROD'
3401                                         AND x.allocation_for_id IN ( SELECT p.product_allocation_id
3402                                                                      FROM  OZF_PRODUCT_ALLOCATIONS p
3403                                                                      WHERE p.allocation_for = 'FUND'
3404                                                                        AND p.allocation_for_id = l_fund_id
3405                                                                        AND p.item_id = -9999 )
3406                                        );
3407 
3408           IF (SQL%NOTFOUND) THEN
3409              IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
3410                 fnd_message.set_name ('OZF', 'OZF_TP_TIMEALLOC_NOT_FOUND_TXT');
3411                 fnd_msg_pub.ADD;
3412              END IF;
3413              RAISE fnd_api.g_exc_unexpected_error;
3414           END IF;
3415       END IF;
3416 
3417 --------END : NEW FIX for difference due to ROUNDING --------------------------------------------------|
3418 */
3419 
3420 
3421 
3422    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- end');
3423 
3424  EXCEPTION
3425      WHEN OZF_TP_BLANK_PERIOD_TBL THEN
3426           ROLLBACK TO create_root_product_spread;
3427           FND_MESSAGE.set_name('OZF', 'OZF_TP_BLANK_PERIOD_TBL_TXT');
3428           FND_MSG_PUB.add;
3429           x_return_status := FND_API.g_ret_sts_error ;
3430           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
3431                                      p_data    => x_error_message);
3432           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : BLANK PERIOD TABLE EXCEPTION = '||sqlerrm(sqlcode));
3433           OZF_UTILITY_PVT.debug_message(fnd_message.get_string('OZF', 'OZF_TP_BLANK_PERIOD_TBL_TXT'));
3434 
3435      WHEN FND_API.G_EXC_ERROR THEN
3436           ROLLBACK TO create_root_product_spread;
3437           x_return_status := FND_API.g_ret_sts_error ;
3438           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
3439                                      p_data    => x_error_message);
3440 
3441      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3442           ROLLBACK TO create_root_product_spread;
3443           x_return_status := FND_API.g_ret_sts_unexp_error ;
3444           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
3445                                      p_data  => x_error_message);
3446 
3447      WHEN OTHERS THEN
3448           ROLLBACK TO create_root_product_spread;
3449           x_return_status := FND_API.g_ret_sts_unexp_error ;
3450           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
3451              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
3452           END IF;
3453           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
3454                                      p_data  => x_error_message);
3455           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
3456 
3457  END create_root_product_spread;
3458 
3459 
3460 
3461 
3462 
3463 
3464 
3465 
3466 -- ------------------------
3467 -- Private Procedure
3468 -- ------------------------
3469 -- ------------------------------------------------------------------
3470 -- Name: publish fact product spread
3471 -- Desc: Update all Product allocation records of given fact_id with new Fund_id
3472 --       >  when Fact is Published (first time)   OR
3473 --       >  when Fact is activated (subsequently)
3474 -- -----------------------------------------------------------------
3475  PROCEDURE publish_fact_product_spread
3476  (
3477     p_api_version        IN          NUMBER,
3478     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
3479     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
3480     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
3481     x_return_status      OUT NOCOPY  VARCHAR2,
3482     x_error_number       OUT NOCOPY  NUMBER,
3483     x_error_message      OUT NOCOPY  VARCHAR2,
3484     p_fact_id            IN          NUMBER
3485  ) IS
3486 
3487    l_api_version   CONSTANT NUMBER       := 1.0;
3488    l_api_name      CONSTANT VARCHAR2(30) := 'publish_fact_product_spread';
3489    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
3490    l_return_status          VARCHAR2(1);
3491 
3492    l_fact_id                 NUMBER;
3493    l_fund_id                 NUMBER;
3494    l_status_code             OZF_ACT_METRIC_FACTS_ALL.status_code%TYPE;
3495 
3496 
3497   CURSOR fact_csr
3498        (l_fact_id        number) IS
3499   SELECT
3500       activity_metric_fact_id,
3501       act_metric_used_by_id,
3502       arc_act_metric_used_by,
3503       activity_metric_id,
3504       hierarchy_id,
3505       hierarchy_type,
3506       node_id,
3507       previous_fact_id,
3508       recommend_total_amount,
3509       status_code
3510   FROM
3511       OZF_ACT_METRIC_FACTS_ALL
3512   WHERE
3513       activity_metric_fact_id = l_fact_id;
3514 
3515   l_fact_rec      fact_csr%ROWTYPE;
3516 
3517  BEGIN
3518 
3519    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- start');
3520 
3521    -- Standard Start of API savepoint
3522    SAVEPOINT publish_fact_product_spread;
3523 
3524    -- Standard call to check for call compatibility.
3525    IF NOT FND_API.compatible_api_call(l_api_version,
3526                                       p_api_version,
3527                                       l_api_name,
3528                                       g_pkg_name)
3529    THEN
3530      RAISE FND_API.g_exc_unexpected_error;
3531    END IF;
3532 
3533    -- Initialize message list if p_init_msg_list is set to TRUE.
3534    IF FND_API.to_Boolean( p_init_msg_list ) THEN
3535       FND_MSG_PUB.initialize;
3536    END IF;
3537 
3538    -- Initialize API return status to SUCCESS
3539    x_return_status := FND_API.g_ret_sts_success;
3540 
3541    l_fact_id := p_fact_id;
3542 
3543    OPEN fact_csr(l_fact_id);
3544    FETCH fact_csr INTO l_fact_rec;
3545    CLOSE fact_csr ;
3546 
3547    l_status_code := l_fact_rec.status_code;
3548 
3549    IF l_status_code IN ('ACTIVE', 'PLANNED') THEN
3550       null;
3551 
3552 --=========>>> Can do some more validation here to check if the caller JTT Quota page is not calling this by mistake
3553 --=========>>> It should call this only after Updating the FACT record with the Newly created FUND id
3554 
3555    ELSE
3556       OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': STATUS of Fact Number : ' ||l_fact_id|| ' is : '||
3557                            l_status_code);
3558       RAISE OZF_TP_OPER_NOT_ALLOWED;
3559    END IF;
3560 
3561 
3562 ------- Start Publishing Product Allocation Records for allocations of l_fact_id -----------------------
3563 
3564    l_fund_id := l_fact_rec.act_metric_used_by_id;
3565 
3566    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Publishing Product Allocations Records'
3567                                          || 'FOR Fact_id = '|| l_fact_id || ' with NEW Fund_id = '||l_fund_id||' ; ');
3568 
3569    UPDATE ozf_product_allocations p
3570    SET p.fund_id = l_fund_id,
3571        p.object_version_number = p.object_version_number + 1,
3572        p.last_update_date = SYSDATE,
3573        p.last_updated_by = FND_GLOBAL.USER_ID,
3574        p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
3575    WHERE p.allocation_for = 'FACT'
3576      AND p.allocation_for_id = l_fact_id;
3577 
3578 
3579 /*
3580 ***** LATER: Check here  ======>  DO YOU ALSO NEED to create account spread for this newly addON-ed fact_id
3581                                   to the existing fund_id (for the newly added period)
3582 
3583 */
3584 
3585 
3586    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Publishing Product Allocations Records'
3587                                          || 'FOR Fact_id = '|| l_fact_id || ' with NEW Fund_id = '||l_fund_id||' ; ');
3588 
3589    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- end');
3590 
3591  EXCEPTION
3592      WHEN OZF_TP_OPER_NOT_ALLOWED THEN
3593           ROLLBACK TO publish_fact_product_spread;
3594           FND_MESSAGE.set_name('OZF', 'OZF_TP_OPER_NOT_ALLOWED_TXT');
3595           FND_MSG_PUB.add;
3596           x_return_status := FND_API.g_ret_sts_error ;
3597           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
3598                                      p_data    => x_error_message);
3599           OZF_UTILITY_PVT.debug_message(fnd_message.get_string('OZF', 'OZF_TP_OPER_NOT_ALLOWED_TXT'));
3600 
3601      WHEN FND_API.G_EXC_ERROR THEN
3602           ROLLBACK TO publish_fact_product_spread;
3603           x_return_status := FND_API.g_ret_sts_error ;
3604           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
3605                                      p_data    => x_error_message);
3606 
3607      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3608           ROLLBACK TO publish_fact_product_spread;
3609           x_return_status := FND_API.g_ret_sts_unexp_error ;
3610           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
3611                                      p_data  => x_error_message);
3612 
3613      WHEN OTHERS THEN
3614           ROLLBACK TO publish_fact_product_spread;
3615           x_return_status := FND_API.g_ret_sts_unexp_error ;
3616           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
3617              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
3618           END IF;
3619           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
3620                                      p_data  => x_error_message);
3621           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
3622 
3623  END publish_fact_product_spread;
3624 
3625 
3626 -- ------------------------
3627 -- Private Procedure
3628 -- ------------------------
3629 -- ------------------------------------------------------------------------
3630 -- Name: delete fact product spread
3631 -- Desc: Delete all Product and Time allocation records for a given fact_id
3632 -- ------------------------------------------------------------------------
3633  PROCEDURE delete_fact_product_spread
3634  (
3635     p_api_version        IN          NUMBER,
3636     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
3637     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
3638     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
3639     x_return_status      OUT NOCOPY  VARCHAR2,
3640     x_error_number       OUT NOCOPY  NUMBER,
3641     x_error_message      OUT NOCOPY  VARCHAR2,
3642     p_fact_id            IN          NUMBER
3643  ) IS
3644 
3645    l_api_version   CONSTANT NUMBER       := 1.0;
3646    l_api_name      CONSTANT VARCHAR2(30) := 'delete_fact_product_spread';
3647    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
3648    l_return_status          VARCHAR2(1);
3649    l_fact_id                NUMBER;
3650 
3651    CURSOR fact_product_spread_csr
3652          (l_fact_id        number) IS
3653    SELECT DISTINCT
3654        p.product_allocation_id
3655    FROM
3656        ozf_product_allocations p
3657    WHERE
3658        p.allocation_for  = 'FACT'
3659    AND p.allocation_for_id = l_fact_id;
3660 
3661 
3662  BEGIN
3663 
3664    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- start');
3665 
3666    -- Standard Start of API savepoint
3667    SAVEPOINT delete_fact_product_spread;
3668 
3669    -- Standard call to check for call compatibility.
3670    IF NOT FND_API.compatible_api_call(l_api_version,
3671                                       p_api_version,
3672                                       l_api_name,
3673                                       g_pkg_name)
3674    THEN
3675      RAISE FND_API.g_exc_unexpected_error;
3676    END IF;
3677 
3678    -- Initialize message list if p_init_msg_list is set to TRUE.
3679    IF FND_API.to_Boolean( p_init_msg_list ) THEN
3680       FND_MSG_PUB.initialize;
3681    END IF;
3682 
3683    -- Initialize API return status to SUCCESS
3684    x_return_status := FND_API.g_ret_sts_success;
3685 
3686    l_fact_id := p_fact_id;
3687 
3688 ------- Start Deleting Product and Time Allocation Records -----------------------
3689 
3690    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Deleting Product and Time Allocations Records'
3691                                          || 'FOR Fact_id = '|| l_fact_id || ' ; ');
3692 
3693    FOR fact_product_spread_rec IN fact_product_spread_csr(l_fact_id)
3694    LOOP
3695       DELETE ozf_time_allocations t
3696       WHERE t.allocation_for_id = fact_product_spread_rec.product_allocation_id
3697         AND t.allocation_for = 'PROD';
3698    END LOOP;
3699 
3700    DELETE ozf_product_allocations p
3701    WHERE  p.allocation_for  = 'FACT'
3702       AND p.allocation_for_id = l_fact_id;
3703 
3704 
3705    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Deleting Product and Time Allocations Records'
3706                                          || 'FOR Fact_id = '|| l_fact_id || ' ; ');
3707    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- end');
3708 
3709  EXCEPTION
3710      WHEN FND_API.G_EXC_ERROR THEN
3711           ROLLBACK TO delete_fact_product_spread;
3712           x_return_status := FND_API.g_ret_sts_error ;
3713           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
3714                                      p_data    => x_error_message);
3715 
3716      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3717           ROLLBACK TO delete_fact_product_spread;
3718           x_return_status := FND_API.g_ret_sts_unexp_error ;
3719           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
3720                                      p_data  => x_error_message);
3721 
3722      WHEN OTHERS THEN
3723           ROLLBACK TO delete_fact_product_spread;
3724           x_return_status := FND_API.g_ret_sts_unexp_error ;
3725           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
3726              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
3727           END IF;
3728           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
3729                                      p_data  => x_error_message);
3730           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
3731 
3732  END delete_fact_product_spread;
3733 
3734 
3735 -- ------------------------
3736 -- Private Procedure
3737 -- ------------------------
3738 -- --------------------------------------------------------------------
3739 -- Name: delete cascade product spread
3740 -- Desc: Delete all Product and Time Allocation records when Root Node
3741 --       changes its Product_Spread_Time_Id (Monthly, Quarterly etc)
3742 -- --------------------------------------------------------------------
3743  PROCEDURE delete_cascade_product_spread
3744  (
3745     p_api_version        IN          NUMBER,
3746     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
3747     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
3748     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
3749     x_return_status      OUT NOCOPY  VARCHAR2,
3750     x_error_number       OUT NOCOPY  NUMBER,
3751     x_error_message      OUT NOCOPY  VARCHAR2,
3752     p_fund_id            IN          NUMBER
3753  ) IS
3754 
3755    l_api_version   CONSTANT NUMBER       := 1.0;
3756    l_api_name      CONSTANT VARCHAR2(30) := 'delete_cascade_product_spread';
3757    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
3758    l_return_status          VARCHAR2(1);
3759 
3760    l_fund_id                NUMBER;
3761    l_territory_id           NUMBER;
3762    l_resource_id            NUMBER;
3763    l_status_code            OZF_FUNDS_ALL_VL.status_code%TYPE;
3764 
3765 
3766   CURSOR fund_csr(l_fund_id  NUMBER) IS
3767    SELECT
3768     owner,
3769     start_period_id,
3770     end_period_id,
3771     start_date_active,
3772     end_date_active,
3773     status_code,
3774     original_budget,
3775     transfered_in_amt,
3776     transfered_out_amt,
3777     node_id, -- (=territory id)
3778     product_spread_time_id period_type_id -- (= minor_scale_id i.e. qtrly or monthly)
3779    FROM
3780     ozf_funds_all_vl
3781    WHERE
3782     fund_id = l_fund_id;
3783 
3784   l_fund_rec    fund_csr%ROWTYPE;
3785 
3786   CURSOR allocation_csr
3787         (l_fund_id        number) IS
3788   SELECT
3789      activity_metric_id,
3790      arc_act_metric_used_by,
3791      act_metric_used_by_id,
3792      product_spread_time_id  period_type_id,  -- (eg.. 32 for monthly, 64 for quarterly),
3793      published_flag,
3794      status_code,
3795      start_period_name,
3796      end_period_name,
3797      from_date,
3798      to_date
3799   FROM
3800       OZF_ACT_METRICS_ALL
3801   WHERE
3802       arc_act_metric_used_by = 'FUND'
3803   AND act_metric_used_by_id = l_fund_id;
3804 
3805 
3806   CURSOR fact_csr
3807        (l_allocation_id        number) IS
3808   SELECT
3809       activity_metric_fact_id,
3810       act_metric_used_by_id,
3811       arc_act_metric_used_by,
3812       activity_metric_id,
3813       hierarchy_id,
3814       hierarchy_type,
3815       node_id,
3816       previous_fact_id,
3817       recommend_total_amount,
3818       status_code
3819   FROM
3820       OZF_ACT_METRIC_FACTS_ALL
3821   WHERE
3822       activity_metric_id = l_allocation_id;
3823 
3824   CURSOR fund_product_spread_csr
3825          (l_fund_id        number) IS
3826    SELECT DISTINCT
3827        p.product_allocation_id
3828    FROM
3829        ozf_product_allocations p
3830    WHERE
3831        p.allocation_for  = 'FUND'
3832    AND p.allocation_for_id = l_fund_id
3833    AND p.fund_id = l_fund_id;
3834 
3835 
3836  BEGIN
3837 
3838    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- start');
3839 
3840    -- Standard Start of API savepoint
3841    SAVEPOINT delete_cascade_product_spread;
3842 
3843    -- Standard call to check for call compatibility.
3844    IF NOT FND_API.compatible_api_call(l_api_version,
3845                                       p_api_version,
3846                                       l_api_name,
3847                                       g_pkg_name)
3848    THEN
3849      RAISE FND_API.g_exc_unexpected_error;
3850    END IF;
3851 
3852    -- Initialize message list if p_init_msg_list is set to TRUE.
3853    IF FND_API.to_Boolean( p_init_msg_list ) THEN
3854       FND_MSG_PUB.initialize;
3855    END IF;
3856 
3857    -- Initialize API return status to SUCCESS
3858    x_return_status := FND_API.g_ret_sts_success;
3859 
3860    l_fund_id := p_fund_id;
3861 
3862    OPEN fund_csr(l_fund_id);
3863    FETCH fund_csr INTO l_fund_rec;
3864    CLOSE fund_csr ;
3865 
3866    l_status_code := l_fund_rec.status_code;
3867 
3868 
3869    IF l_status_code IN ('DRAFT', 'PLANNING') THEN
3870       null;
3871    ELSE
3872       OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': STATUS of Root Fund Number : ' ||l_fund_id|| ' is : '||
3873                            l_status_code);
3874       RAISE OZF_TP_CHG_PS_NOT_ALLOWED;
3875    END IF;
3876 
3877 
3878 ------- Start Deleting Product and Time Allocation Records for allocations of l_fund_id -----------------------
3879 
3880    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Deleting Product and Time Allocations Records'
3881                                          || 'FOR Allocations of Fund_id = '|| l_fund_id || ' ; ');
3882 
3883    FOR allocation_rec IN allocation_csr(l_fund_id)
3884    LOOP
3885        FOR fact_rec IN fact_csr(allocation_rec.activity_metric_id)
3886        LOOP
3887            delete_fact_product_spread
3888                          (p_api_version        => p_api_version,
3889                           x_return_status      => x_return_status,
3890                           x_error_number       => x_error_number,
3891                           x_error_message      => x_error_message,
3892                           p_fact_id            => fact_rec.activity_metric_fact_id
3893                          );
3894        END LOOP;
3895    END LOOP;
3896 
3897    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Deleting Product and Time Allocations Records'
3898                                          || 'FOR Allocations of Fund_id = '|| l_fund_id || ' ; ');
3899 
3900 ------- Start Deleting Product and Time Allocation Records for ROOT i.e. l_fund_id ----------------------
3901 
3902 
3903    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Deleting Product and Time Allocations Records'
3904                                          || 'FOR Fund_id = '|| l_fund_id || ' ; ');
3905 
3906    FOR fund_product_spread_rec IN fund_product_spread_csr(l_fund_id)
3907    LOOP
3908       DELETE ozf_time_allocations t
3909       WHERE t.allocation_for_id = fund_product_spread_rec.product_allocation_id
3910         AND t.allocation_for = 'PROD';
3911    END LOOP;
3912 
3913    DELETE ozf_product_allocations p
3914    WHERE  p.allocation_for  = 'FUND'
3915       AND p.allocation_for_id = l_fund_id
3916       AND p.fund_id = l_fund_id;
3917 
3918 
3919    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Deleting Product and Time Allocations Records'
3920                                          || 'FOR Fund_id = '|| l_fund_id || ' ; ');
3921 
3922 
3923    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- end');
3924 
3925  EXCEPTION
3926      WHEN OZF_TP_CHG_PS_NOT_ALLOWED THEN
3927           ROLLBACK TO delete_cascade_product_spread;
3928           FND_MESSAGE.set_name('OZF', 'OZF_TP_CHG_PS_NOT_ALLOWED_TXT');
3929           FND_MSG_PUB.add;
3930           x_return_status := FND_API.g_ret_sts_error ;
3931           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
3932                                      p_data    => x_error_message);
3933           OZF_UTILITY_PVT.debug_message(fnd_message.get_string('OZF', 'OZF_TP_CHG_PS_NOT_ALLOWED_TXT'));
3934 
3935      WHEN FND_API.G_EXC_ERROR THEN
3936           ROLLBACK TO delete_cascade_product_spread;
3937           x_return_status := FND_API.g_ret_sts_error ;
3938           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
3939                                      p_data    => x_error_message);
3940 
3941      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3942           ROLLBACK TO delete_cascade_product_spread;
3943           x_return_status := FND_API.g_ret_sts_unexp_error ;
3944           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
3945                                      p_data  => x_error_message);
3946 
3947      WHEN OTHERS THEN
3948           ROLLBACK TO delete_cascade_product_spread;
3949           x_return_status := FND_API.g_ret_sts_unexp_error ;
3950           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
3951              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
3952           END IF;
3953           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
3954                                      p_data  => x_error_message);
3955           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
3956 
3957  END delete_cascade_product_spread;
3958 
3959 
3960 -- ------------------------
3961 -- Private Procedure
3962 -- ------------------------
3963 -- ------------------------------------------------------------------
3964 -- Name: Add cascade product spread
3965 -- Desc: When Root Node adds a Product to his Product Spread, cascade new product
3966 --       to all facts for all his allocations. Use that facts
3967 --       Product Spread Time Id (Monthly, Quarterly etc)
3968 --       Note : Products for the Root Node will be added from OA UI.
3969 -- -----------------------------------------------------------------
3970  PROCEDURE add_cascade_product_spread
3971  (
3972     p_api_version        IN          NUMBER,
3973     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
3974     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
3975     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
3976     x_return_status      OUT NOCOPY  VARCHAR2,
3977     x_error_number       OUT NOCOPY  NUMBER,
3978     x_error_message      OUT NOCOPY  VARCHAR2,
3979     p_fund_id            IN          NUMBER,
3980     p_item_id            IN          NUMBER,
3981     p_item_type          IN          VARCHAR2
3982  ) IS
3983 
3984    l_api_version   CONSTANT NUMBER       := 1.0;
3985    l_api_name      CONSTANT VARCHAR2(30) := 'add_cascade_product_spread';
3986    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
3987    l_return_status          VARCHAR2(1);
3988 
3989    l_fund_id                NUMBER;
3990    l_item_id                NUMBER;
3991    l_item_type              VARCHAR2(30);
3992    l_fact_id                NUMBER;
3993    l_territory_id           NUMBER;
3994    l_status_code            OZF_FUNDS_ALL_VL.status_code%TYPE;
3995    l_lysp_sales             NUMBER;
3996    l_total_lysp_sales       NUMBER;
3997    l_product_allocation_id  NUMBER;
3998    l_time_allocation_id     NUMBER;
3999    l_lysp_time_id           NUMBER;
4000    l_object_version_number  NUMBER := 1;
4001    l_org_id                 NUMBER := TO_NUMBER(SUBSTRB(USERENV('CLIENT_INFO'), 1, 10));
4002 
4003    p_prod_alloc_rec      ozf_product_allocations%ROWTYPE;
4004    p_time_alloc_rec      ozf_time_allocations%ROWTYPE;
4005 
4006 
4007   CURSOR fund_csr(l_fund_id  NUMBER) IS
4008    SELECT
4009     owner,
4010     start_period_id,
4011     end_period_id,
4012     start_date_active,
4013     end_date_active,
4014     status_code,
4015     original_budget,
4016     transfered_in_amt,
4017     transfered_out_amt,
4018     node_id, -- (=territory id)
4019     product_spread_time_id period_type_id -- (= minor_scale_id i.e. qtrly or monthly)
4020    FROM
4021     ozf_funds_all_vl
4022    WHERE
4023     fund_id = l_fund_id;
4024 
4025   l_fund_rec    fund_csr%ROWTYPE;
4026 
4027   CURSOR allocation_csr
4028         (l_fund_id        number) IS
4029   SELECT
4030      activity_metric_id,
4031      arc_act_metric_used_by,
4032      act_metric_used_by_id,
4033      product_spread_time_id  period_type_id,  -- (eg.. 32 for monthly, 64 for quarterly),
4034      published_flag,
4035      status_code,
4036      start_period_name,
4037      end_period_name,
4038      from_date,
4039      to_date
4040   FROM
4041       OZF_ACT_METRICS_ALL
4042   WHERE
4043       arc_act_metric_used_by = 'FUND'
4044   AND act_metric_used_by_id = l_fund_id;
4045 
4046 
4047   CURSOR fact_csr
4048        (l_allocation_id        number) IS
4049   SELECT
4050       activity_metric_fact_id,
4051       act_metric_used_by_id,
4052       arc_act_metric_used_by,
4053       activity_metric_id,
4054       hierarchy_id,
4055       hierarchy_type,
4056       node_id,  ---  this is territory_id of this FACT ******* confirm this *********
4057       previous_fact_id,
4058       recommend_total_amount,
4059       status_code
4060   FROM
4061       OZF_ACT_METRIC_FACTS_ALL
4062   WHERE
4063       activity_metric_id = l_allocation_id;
4064 
4065 
4066    CURSOR prod_alloc_csr
4067        (l_fact_id        number) IS
4068    SELECT product_allocation_id,
4069           allocation_for,
4070           allocation_for_id,
4071           fund_id,
4072           item_id,
4073           item_type,
4074           target,
4075           lysp_sales
4076    FROM OZF_PRODUCT_ALLOCATIONS
4077    WHERE allocation_for = 'FACT'
4078     AND allocation_for_id = l_fact_id
4079     AND item_id = -9999;
4080 
4081    l_prod_alloc_rec  prod_alloc_csr%ROWTYPE;
4082 
4083    CURSOR time_alloc_csr
4084        (l_prod_alloc_id    number) IS
4085    SELECT time_allocation_id,
4086           allocation_for,
4087           allocation_for_id,
4088           time_id,
4089           period_type_id,
4090           target,
4091           lysp_sales
4092    FROM OZF_TIME_ALLOCATIONS
4093    WHERE allocation_for = 'PROD'
4094     AND allocation_for_id = l_prod_alloc_id;
4095 
4096    l_time_alloc_rec  time_alloc_csr%ROWTYPE;
4097 
4098 
4099    CURSOR product_lysp_sales_csr (l_product_id    NUMBER,
4100                                   l_territory_id  NUMBER,
4101                                   l_time_id       NUMBER) IS
4102     SELECT
4103      SUM(bsmv.sales) sales
4104     FROM
4105       ozf_order_sales_v bsmv,
4106       ams_party_market_segments a
4107     WHERE
4108          a.market_qualifier_reference = l_territory_id
4109      AND a.market_qualifier_type='TERRITORY'
4110      AND bsmv.ship_to_site_use_id = a.site_use_id
4111      AND bsmv.inventory_item_id = l_product_id
4112      AND bsmv.time_id = l_time_id;
4113 
4114 
4115    CURSOR category_lysp_sales_csr (l_category_id    NUMBER,
4116                                    l_territory_id   NUMBER,
4117                                    l_time_id        NUMBER,
4118                                    l_fund_id        NUMBER) IS
4119    SELECT
4120     SUM(bsmv.sales) sales
4121    FROM
4122     ozf_order_sales_v bsmv,
4123     ams_party_market_segments a
4124    WHERE
4125        a.market_qualifier_reference = l_territory_id
4126    AND a.market_qualifier_type='TERRITORY'
4127    AND bsmv.ship_to_site_use_id = a.site_use_id
4128    AND bsmv.time_id = l_time_id
4129    AND bsmv.inventory_item_id IN
4130                              (  SELECT DISTINCT MIC.INVENTORY_ITEM_ID
4131                                 FROM   MTL_ITEM_CATEGORIES     MIC,
4132                                        ENI_PROD_DENORM_HRCHY_V DENORM
4133                                 WHERE  MIC.CATEGORY_SET_ID  = DENORM.CATEGORY_SET_ID
4134                                  AND   MIC.CATEGORY_ID      = DENORM.CHILD_ID
4135                                  AND   DENORM.PARENT_ID     = l_category_id
4136                                 MINUS
4137                                 SELECT a.inventory_item_id
4138                                 FROM  ams_act_products a
4139                                 WHERE act_product_used_by_id = l_fund_id
4140                                  AND arc_act_product_used_by = 'FUND'
4141                                  AND level_type_code = 'PRODUCT'
4142                                  AND excluded_flag IN  ('Y', 'N')
4143                               );
4144 
4145 
4146  BEGIN
4147 
4148    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- start');
4149 
4150    -- Standard Start of API savepoint
4151    SAVEPOINT add_cascade_product_spread;
4152 
4153    -- Standard call to check for call compatibility.
4154    IF NOT FND_API.compatible_api_call(l_api_version,
4155                                       p_api_version,
4156                                       l_api_name,
4157                                       g_pkg_name)
4158    THEN
4159      RAISE FND_API.g_exc_unexpected_error;
4160    END IF;
4161 
4162    -- Initialize message list if p_init_msg_list is set to TRUE.
4163    IF FND_API.to_Boolean( p_init_msg_list ) THEN
4164       FND_MSG_PUB.initialize;
4165    END IF;
4166 
4167    -- Initialize API return status to SUCCESS
4168    x_return_status := FND_API.g_ret_sts_success;
4169 
4170    l_fund_id := p_fund_id;
4171 
4172    OPEN fund_csr(l_fund_id);
4173    FETCH fund_csr INTO l_fund_rec;
4174    CLOSE fund_csr ;
4175 
4176    l_status_code := l_fund_rec.status_code;
4177 
4178 
4179    IF l_status_code IN ('DRAFT', 'PLANNING') THEN
4180       null;
4181    ELSE
4182       OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': STATUS of Root Fund Number : ' ||l_fund_id|| ' is : '||
4183                            l_status_code);
4184       RAISE OZF_TP_ADDITEM_NOT_ALLOWED;
4185    END IF;
4186 
4187 
4188 ------- Start Adding Product and Time Allocation Records for allocations of l_fund_id -----------------------
4189 
4190    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Adding Product and Time Allocations Records'
4191                                          || 'FOR Allocations of Fund_id = '|| l_fund_id || ' ; ');
4192 
4193    FOR allocation_rec IN allocation_csr(l_fund_id)
4194    LOOP
4195        FOR fact_rec IN fact_csr(allocation_rec.activity_metric_id)
4196        LOOP
4197 
4198            l_fact_id := fact_rec.activity_metric_fact_id;
4199            l_territory_id := fact_rec.node_id;
4200 
4201            OPEN prod_alloc_csr(l_fact_id);
4202            FETCH prod_alloc_csr INTO l_prod_alloc_rec;
4203            IF prod_alloc_csr%NOTFOUND THEN
4204               CLOSE prod_alloc_csr;
4205               OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': NO Product and Time Allocations Records exist'
4206                                                     || 'FOR Fact_id = '|| l_fact_id || ' ; ');
4207               GOTO next_fact_iteration;
4208            END IF;
4209            CLOSE prod_alloc_csr;
4210 
4211             OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Adding Product and Time Allocations Records'
4212                                                   || 'FOR Fact_ID = '|| l_fact_id || ' ; ');
4213 
4214             p_prod_alloc_rec := NULL;
4215 
4216             l_product_allocation_id := get_product_allocation_id;
4217 
4218             p_prod_alloc_rec.allocation_for := l_prod_alloc_rec.allocation_for;  -- same as 'FACT'
4219             p_prod_alloc_rec.allocation_for_id := l_prod_alloc_rec.allocation_for_id;  -- same as l_fact_id
4220             p_prod_alloc_rec.fund_id := l_prod_alloc_rec.fund_id;
4221             p_prod_alloc_rec.item_type := l_item_type;
4222             p_prod_alloc_rec.item_id := l_item_id;
4223             p_prod_alloc_rec.selected_flag := 'N';
4224             p_prod_alloc_rec.target := 0;
4225             p_prod_alloc_rec.lysp_sales := 0;
4226 
4227 
4228             Ozf_Product_Allocations_Pkg.Insert_Row(
4229                px_product_allocation_id  => l_product_allocation_id,
4230                p_allocation_for  => p_prod_alloc_rec.allocation_for,
4231                p_allocation_for_id  => p_prod_alloc_rec.allocation_for_id,
4232                p_fund_id  => p_prod_alloc_rec.fund_id,
4233                p_item_type  => p_prod_alloc_rec.item_type,
4234                p_item_id  => p_prod_alloc_rec.item_id,
4235                p_selected_flag  => p_prod_alloc_rec.selected_flag,
4236                p_target  => NVL(p_prod_alloc_rec.target, 0),
4237                p_lysp_sales  => NVL(p_prod_alloc_rec.lysp_sales, 0),
4238                p_parent_product_allocation_id  => p_prod_alloc_rec.parent_product_allocation_id,
4239                px_object_version_number  => l_object_version_number,
4240                p_creation_date  => SYSDATE,
4241                p_created_by  => FND_GLOBAL.USER_ID,
4242                p_last_update_date  => SYSDATE,
4243                p_last_updated_by  => FND_GLOBAL.USER_ID,
4244                p_last_update_login  => FND_GLOBAL.conc_login_id,
4245                p_attribute_category  => p_prod_alloc_rec.attribute_category,
4246                p_attribute1  => p_prod_alloc_rec.attribute1,
4247                p_attribute2  => p_prod_alloc_rec.attribute2,
4248                p_attribute3  => p_prod_alloc_rec.attribute3,
4249                p_attribute4  => p_prod_alloc_rec.attribute4,
4250                p_attribute5  => p_prod_alloc_rec.attribute5,
4251                p_attribute6  => p_prod_alloc_rec.attribute6,
4252                p_attribute7  => p_prod_alloc_rec.attribute7,
4253                p_attribute8  => p_prod_alloc_rec.attribute8,
4254                p_attribute9  => p_prod_alloc_rec.attribute9,
4255                p_attribute10  => p_prod_alloc_rec.attribute10,
4256                p_attribute11  => p_prod_alloc_rec.attribute11,
4257                p_attribute12  => p_prod_alloc_rec.attribute12,
4258                p_attribute13  => p_prod_alloc_rec.attribute13,
4259                p_attribute14  => p_prod_alloc_rec.attribute14,
4260                p_attribute15  => p_prod_alloc_rec.attribute15,
4261                px_org_id  => l_org_id
4262              );
4263 
4264 
4265             l_total_lysp_sales := 0;
4266 
4267             FOR time_alloc_rec IN time_alloc_csr(l_prod_alloc_rec.product_allocation_id)
4268             LOOP
4269                 p_time_alloc_rec := NULL;
4270 
4271                 l_lysp_time_id :=  OZF_TIME_API_PVT.get_lysp_id(time_alloc_rec.time_id, time_alloc_rec.period_type_id);
4272 
4273                 l_lysp_sales := 0;
4274                 IF l_item_type = 'PRICING_ATTRIBUTE1' THEN
4275                    OPEN product_lysp_sales_csr(l_item_id,
4276                                                l_territory_id,
4277                                                l_lysp_time_id
4278                                               );
4279                    FETCH product_lysp_sales_csr INTO l_lysp_sales;
4280                 ELSIF l_item_type = 'PRICING_ATTRIBUTE2' THEN
4281                    OPEN category_lysp_sales_csr(l_item_id,
4282                                                 l_territory_id,
4283                                                 l_lysp_time_id,
4284                                                 l_fund_id
4285                                                );
4286                    FETCH category_lysp_sales_csr INTO l_lysp_sales;
4287                    CLOSE category_lysp_sales_csr;
4288                 END IF;
4289 
4290                 l_total_lysp_sales := l_total_lysp_sales + l_lysp_sales;
4291 
4292                 l_time_allocation_id := get_time_allocation_id;
4293 
4294                 p_time_alloc_rec.allocation_for := time_alloc_rec.allocation_for;
4295                 p_time_alloc_rec.allocation_for_id := l_product_allocation_id;
4296                 p_time_alloc_rec.time_id := time_alloc_rec.time_id;
4297                 p_time_alloc_rec.period_type_id := time_alloc_rec.period_type_id;
4298                 p_time_alloc_rec.target := 0;
4299                 p_time_alloc_rec.lysp_sales := l_lysp_sales;
4300 
4301 
4302                 Ozf_Time_Allocations_Pkg.Insert_Row(
4303                    px_time_allocation_id  => l_time_allocation_id,
4304                    p_allocation_for  => p_time_alloc_rec.allocation_for,
4305                    p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
4306                    p_time_id  => p_time_alloc_rec.time_id,
4307                    p_period_type_id => p_time_alloc_rec.period_type_id,
4308                    p_target  => NVL(p_time_alloc_rec.target, 0),
4309                    p_lysp_sales  => NVL(p_time_alloc_rec.lysp_sales, 0),
4310                    px_object_version_number  => l_object_version_number,
4311                    p_creation_date  => SYSDATE,
4312                    p_created_by  => FND_GLOBAL.USER_ID,
4313                    p_last_update_date  => SYSDATE,
4314                    p_last_updated_by  => FND_GLOBAL.USER_ID,
4315                    p_last_update_login  => FND_GLOBAL.conc_login_id,
4316                    p_attribute_category  => p_time_alloc_rec.attribute_category,
4317                    p_attribute1  => p_time_alloc_rec.attribute1,
4318                    p_attribute2  => p_time_alloc_rec.attribute2,
4319                    p_attribute3  => p_time_alloc_rec.attribute3,
4320                    p_attribute4  => p_time_alloc_rec.attribute4,
4321                    p_attribute5  => p_time_alloc_rec.attribute5,
4322                    p_attribute6  => p_time_alloc_rec.attribute6,
4323                    p_attribute7  => p_time_alloc_rec.attribute7,
4324                    p_attribute8  => p_time_alloc_rec.attribute8,
4325                    p_attribute9  => p_time_alloc_rec.attribute9,
4326                    p_attribute10  => p_time_alloc_rec.attribute10,
4327                    p_attribute11  => p_time_alloc_rec.attribute11,
4328                    p_attribute12  => p_time_alloc_rec.attribute12,
4329                    p_attribute13  => p_time_alloc_rec.attribute13,
4330                    p_attribute14  => p_time_alloc_rec.attribute14,
4331                    p_attribute15  => p_time_alloc_rec.attribute15,
4332                    px_org_id  => l_org_id
4333                  );
4334 
4335 
4336             END LOOP;
4337 
4338             UPDATE OZF_PRODUCT_ALLOCATIONS p
4339             SET p.lysp_sales = l_total_lysp_sales,
4340                 p.object_version_number = p.object_version_number + 1,
4341                 p.last_update_date = SYSDATE,
4342                 p.last_updated_by = FND_GLOBAL.USER_ID,
4343                 p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
4344             WHERE p.product_allocation_id = l_product_allocation_id;
4345 
4346             OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Adding Product and Time Allocations Records'
4347                                                   || 'FOR Fact_ID = '|| l_fact_id || ' ; ');
4348 
4349             <<next_fact_iteration>>
4350             NULL;
4351        END LOOP;
4352    END LOOP;
4353 
4354    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Adding Product and Time Allocations Records'
4355                                          || 'FOR Allocations of Fund_id = '|| l_fund_id || ' ; ');
4356 
4357    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- end');
4358 
4359  EXCEPTION
4360      WHEN OZF_TP_ADDITEM_NOT_ALLOWED THEN
4361           ROLLBACK TO add_cascade_product_spread;
4362           FND_MESSAGE.set_name('OZF', 'OZF_TP_ADDITEM_NOT_ALLOWED_TXT');
4363           FND_MSG_PUB.add;
4364           x_return_status := FND_API.g_ret_sts_error ;
4365           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
4366                                      p_data    => x_error_message);
4367           OZF_UTILITY_PVT.debug_message(fnd_message.get_string('OZF', 'OZF_TP_ADDITEM_NOT_ALLOWED_TXT'));
4368 
4369      WHEN FND_API.G_EXC_ERROR THEN
4370           ROLLBACK TO add_cascade_product_spread;
4371           x_return_status := FND_API.g_ret_sts_error ;
4372           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
4373                                      p_data    => x_error_message);
4374 
4375      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4376           ROLLBACK TO add_cascade_product_spread;
4377           x_return_status := FND_API.g_ret_sts_unexp_error ;
4378           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
4379                                      p_data  => x_error_message);
4380 
4381      WHEN OTHERS THEN
4382           ROLLBACK TO add_cascade_product_spread;
4383           x_return_status := FND_API.g_ret_sts_unexp_error ;
4384           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4385              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
4386           END IF;
4387           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
4388                                      p_data  => x_error_message);
4389           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
4390 
4391  END add_cascade_product_spread;
4392 
4393 
4394 
4395 -- ------------------------
4396 -- Private Procedure
4397 -- ------------------------
4398 -- ------------------------------------------------------------------
4399 -- Name: Delete Single Product
4400 -- Desc: When Root Node (= p_fund_id) deletes a product from his Product Spread,
4401 --       cascade the Deletion to all facts for all his allocations.
4402 --       So, delete that Product from everywhere and INCREMENT Quota
4403 --       for OTHERS for that FACT_id
4404 --       Note: Deletion of Product from Root and incrementing Root-OTHERS
4405 --             will be done in OA UI.
4406 -- -----------------------------------------------------------------
4407  PROCEDURE delete_single_product
4408  (
4409     p_api_version        IN          NUMBER,
4410     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
4411     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
4412     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
4413     x_return_status      OUT NOCOPY  VARCHAR2,
4414     x_error_number       OUT NOCOPY  NUMBER,
4415     x_error_message      OUT NOCOPY  VARCHAR2,
4416     p_fund_id            IN          NUMBER,
4417     p_item_id            IN          NUMBER,
4418     p_item_type          IN          VARCHAR2
4419  ) IS
4420 
4421    l_api_version   CONSTANT NUMBER       := 1.0;
4422    l_api_name      CONSTANT VARCHAR2(30) := 'delete_single_product';
4423    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
4424    l_return_status          VARCHAR2(1);
4425 
4426    l_fund_id                NUMBER;
4427    l_item_id                NUMBER;
4428    l_item_type              VARCHAR2(30);
4429    l_fact_id                NUMBER;
4430    l_territory_id           NUMBER;
4431    l_status_code            OZF_FUNDS_ALL_VL.status_code%TYPE;
4432    l_lysp_sales             NUMBER;
4433    l_total_lysp_sales       NUMBER;
4434    l_product_allocation_id  NUMBER;
4435    l_time_allocation_id     NUMBER;
4436    l_lysp_time_id           NUMBER;
4437    l_object_version_number  NUMBER := 1;
4438    l_org_id                 NUMBER := TO_NUMBER(SUBSTRB(USERENV('CLIENT_INFO'), 1, 10));
4439 
4440    p_prod_alloc_rec      ozf_product_allocations%ROWTYPE;
4441    p_time_alloc_rec      ozf_time_allocations%ROWTYPE;
4442 
4443   CURSOR fund_csr(l_fund_id  NUMBER) IS
4444    SELECT
4445     owner,
4446     start_period_id,
4447     end_period_id,
4448     start_date_active,
4449     end_date_active,
4450     status_code,
4451     original_budget,
4452     transfered_in_amt,
4453     transfered_out_amt,
4454     node_id, -- (=territory id)
4455     product_spread_time_id period_type_id -- (= minor_scale_id i.e. qtrly or monthly)
4456    FROM
4457     ozf_funds_all_vl
4458    WHERE
4459     fund_id = l_fund_id;
4460 
4461   l_fund_rec    fund_csr%ROWTYPE;
4462 
4463   CURSOR allocation_csr
4464         (l_fund_id        number) IS
4465   SELECT
4466      activity_metric_id,
4467      arc_act_metric_used_by,
4468      act_metric_used_by_id,
4469      product_spread_time_id  period_type_id,  -- (eg.. 32 for monthly, 64 for quarterly),
4470      published_flag,
4471      status_code,
4472      start_period_name,
4473      end_period_name,
4474      from_date,
4475      to_date
4476   FROM
4477       OZF_ACT_METRICS_ALL
4478   WHERE
4479       arc_act_metric_used_by = 'FUND'
4480   AND act_metric_used_by_id = l_fund_id;
4481 
4482 
4483   CURSOR fact_csr
4484        (l_allocation_id        number) IS
4485   SELECT
4486       activity_metric_fact_id,
4487       act_metric_used_by_id,
4488       arc_act_metric_used_by,
4489       activity_metric_id,
4490       hierarchy_id,
4491       hierarchy_type,
4492       node_id,  ---  this is territory_id of this FACT ******* confirm this *********
4493       previous_fact_id,
4494       recommend_total_amount,
4495       status_code
4496   FROM
4497       OZF_ACT_METRIC_FACTS_ALL
4498   WHERE
4499       activity_metric_id = l_allocation_id;
4500 
4501 
4502    CURSOR prod_alloc_csr
4503        (l_fact_id        number,
4504         l_item_id        number,
4505         l_item_type      varchar2 ) IS
4506    SELECT product_allocation_id,
4507           allocation_for,
4508           allocation_for_id,
4509           fund_id,
4510           item_id,
4511           item_type,
4512           target,
4513           lysp_sales
4514    FROM OZF_PRODUCT_ALLOCATIONS
4515    WHERE allocation_for = 'FACT'
4516     AND allocation_for_id = l_fact_id
4517     AND item_id = l_item_id
4518     AND item_type = l_item_type;
4519 
4520    l_prod_alloc_rec         prod_alloc_csr%ROWTYPE;
4521    l_others_prod_alloc_rec  prod_alloc_csr%ROWTYPE;
4522 
4523    CURSOR time_alloc_csr
4524        (l_prod_alloc_id    number) IS
4525    SELECT time_allocation_id,
4526           allocation_for,
4527           allocation_for_id,
4528           time_id,
4529           period_type_id,
4530           target,
4531           lysp_sales
4532    FROM OZF_TIME_ALLOCATIONS
4533    WHERE allocation_for = 'PROD'
4534     AND allocation_for_id = l_prod_alloc_id;
4535 
4536    l_time_alloc_rec  time_alloc_csr%ROWTYPE;
4537 
4538 
4539    CURSOR product_lysp_sales_csr (l_product_id    NUMBER,
4540                                   l_territory_id  NUMBER,
4541                                   l_time_id       NUMBER) IS
4542     SELECT
4543      SUM(bsmv.sales) sales
4544     FROM
4545       ozf_order_sales_v bsmv,
4546       ams_party_market_segments a
4547     WHERE
4548          a.market_qualifier_reference = l_territory_id
4549      AND a.market_qualifier_type='TERRITORY'
4550      AND bsmv.ship_to_site_use_id = a.site_use_id
4551      AND bsmv.inventory_item_id = l_product_id
4552      AND bsmv.time_id = l_time_id;
4553 
4554 
4555    CURSOR category_lysp_sales_csr (l_category_id    NUMBER,
4556                                    l_territory_id   NUMBER,
4557                                    l_time_id        NUMBER,
4558                                    l_fund_id        NUMBER) IS
4559    SELECT
4560     SUM(bsmv.sales) sales
4561    FROM
4562     ozf_order_sales_v bsmv,
4563     ams_party_market_segments a
4564    WHERE
4565        a.market_qualifier_reference = l_territory_id
4566    AND a.market_qualifier_type='TERRITORY'
4567    AND bsmv.ship_to_site_use_id = a.site_use_id
4568    AND bsmv.time_id = l_time_id
4569    AND bsmv.inventory_item_id IN
4570                              ( SELECT DISTINCT MIC.INVENTORY_ITEM_ID
4571                                FROM   MTL_ITEM_CATEGORIES     MIC,
4572                                       ENI_PROD_DENORM_HRCHY_V DENORM
4573                                WHERE  MIC.CATEGORY_SET_ID  = DENORM.CATEGORY_SET_ID
4574                                 AND   MIC.CATEGORY_ID      = DENORM.CHILD_ID
4575                                 AND   DENORM.PARENT_ID     = l_category_id
4576                                 MINUS
4577                                 SELECT a.inventory_item_id
4578                                 FROM  ams_act_products a
4579                                 WHERE act_product_used_by_id = l_fund_id
4580                                  AND arc_act_product_used_by = 'FUND'
4581                                  AND level_type_code = 'PRODUCT'
4582                                  AND excluded_flag IN  ('Y', 'N')
4583                               );
4584 
4585 
4586  BEGIN
4587 
4588    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- start');
4589 
4590    -- Standard Start of API savepoint
4591    SAVEPOINT delete_single_product;
4592 
4593    -- Standard call to check for call compatibility.
4594    IF NOT FND_API.compatible_api_call(l_api_version,
4595                                       p_api_version,
4596                                       l_api_name,
4597                                       g_pkg_name)
4598    THEN
4599      RAISE FND_API.g_exc_unexpected_error;
4600    END IF;
4601 
4602    -- Initialize message list if p_init_msg_list is set to TRUE.
4603    IF FND_API.to_Boolean( p_init_msg_list ) THEN
4604       FND_MSG_PUB.initialize;
4605    END IF;
4606 
4607    -- Initialize API return status to SUCCESS
4608    x_return_status := FND_API.g_ret_sts_success;
4609 
4610    l_fund_id := p_fund_id;
4611 
4612    OPEN fund_csr(l_fund_id);
4613    FETCH fund_csr INTO l_fund_rec;
4614    CLOSE fund_csr ;
4615 
4616    l_status_code := l_fund_rec.status_code;
4617 
4618 
4619    IF l_status_code IN ('DRAFT', 'PLANNING') THEN
4620       null;
4621    ELSE
4622       OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': STATUS of Root Fund Number : '
4623                                             ||l_fund_id|| ' is : '||l_status_code);
4624       RAISE OZF_TP_DELITEM_NOT_ALLOWED;
4625    END IF;
4626 
4627 
4628 
4629 ------- Start Deleting Product and Time Allocation Records for allocations of l_fact_id -----------------------
4630 
4631    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Deleting Product and Time Allocations Records'
4632                                          || 'FOR Allocations of Fund_id = '|| l_fund_id || ' ; ');
4633 
4634    FOR allocation_rec IN allocation_csr(l_fund_id)
4635    LOOP
4636        FOR fact_rec IN fact_csr(allocation_rec.activity_metric_id)
4637        LOOP
4638 
4639            l_fact_id := fact_rec.activity_metric_fact_id;
4640            l_territory_id := fact_rec.node_id;
4641 
4642            OPEN prod_alloc_csr(l_fact_id, l_item_id, l_item_type);
4643            FETCH prod_alloc_csr INTO l_prod_alloc_rec;
4644            IF prod_alloc_csr%NOTFOUND THEN
4645               CLOSE prod_alloc_csr;
4646               OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': NO Product and Time Allocations Records exist'
4647                                                     || 'FOR Fact_id = '|| l_fact_id || ' ; ');
4648               GOTO next_fact_iteration;
4649            END IF;
4650            CLOSE prod_alloc_csr;
4651 
4652            OPEN prod_alloc_csr(l_fact_id, -9999, 'OTHERS');
4653            FETCH prod_alloc_csr INTO l_others_prod_alloc_rec;
4654            IF prod_alloc_csr%NOTFOUND THEN
4655               CLOSE prod_alloc_csr;
4656               OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': NO OTHERS Product and Time Allocations Records exist'
4657                                                     || 'FOR Fact_id = '|| l_fact_id || ' ; ');
4658               GOTO next_fact_iteration;
4659            END IF;
4660            CLOSE prod_alloc_csr;
4661 
4662 
4663             OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name ||
4664                                   ': Begin - Updating OTHERS Product and Time Allocations Records'
4665                                   || 'FOR Fact_ID = '|| l_fact_id || ' ; ');
4666 
4667             FOR time_alloc_rec IN time_alloc_csr(l_prod_alloc_rec.product_allocation_id)
4668             LOOP
4669 
4670                 UPDATE ozf_time_allocations t -- Update Others Quota for Jul03, Aug03, Sep03 etc
4671                    SET t.target = t.target + NVL(time_alloc_rec.target, 0),
4672                        t.object_version_number = t.object_version_number + 1,
4673                        t.last_update_date = SYSDATE,
4674                        t.last_updated_by = FND_GLOBAL.USER_ID,
4675                        t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
4676                  WHERE
4677                        t.time_id = time_alloc_rec.time_id
4678                    AND t.allocation_for_id = l_others_prod_alloc_rec.product_allocation_id
4679                    AND t.allocation_for = 'PROD';
4680 
4681             END LOOP;
4682 
4683             UPDATE ozf_product_allocations p -- Update Others Quota for Q3-03 etc
4684             SET p.target = p.target + NVL(l_prod_alloc_rec.target, 0),
4685                 p.object_version_number = p.object_version_number + 1,
4686                 p.last_update_date = SYSDATE,
4687                 p.last_updated_by = FND_GLOBAL.USER_ID,
4688                 p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
4689             WHERE p.product_allocation_id = l_others_prod_alloc_rec.product_allocation_id;
4690 
4691 
4692             OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Updating OTHERS Product and Time Allocations Records'
4693                                                   || 'FOR Fact_ID = '|| l_fact_id || ' ; ');
4694 
4695 
4696             DELETE ozf_time_allocations t
4697             WHERE t.allocation_for_id = l_prod_alloc_rec.product_allocation_id
4698               AND t.allocation_for = 'PROD';
4699 
4700             DELETE ozf_product_allocations p
4701             WHERE  p.product_allocation_id = l_prod_alloc_rec.product_allocation_id;
4702 
4703 
4704             <<next_fact_iteration>>
4705             NULL;
4706        END LOOP;
4707    END LOOP;
4708 
4709    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Deleting Product and Time Allocations Records'
4710                                          || 'FOR Allocations of Fund_id = '|| l_fund_id || ' ; ');
4711 
4712    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- end');
4713 
4714  EXCEPTION
4715      WHEN OZF_TP_DELITEM_NOT_ALLOWED THEN
4716           ROLLBACK TO delete_single_product;
4717           FND_MESSAGE.set_name('OZF', 'OZF_TP_DELITEM_NOT_ALLOWED_TXT');
4718           FND_MSG_PUB.add;
4719           x_return_status := FND_API.g_ret_sts_error ;
4720           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
4721                                      p_data    => x_error_message);
4722           OZF_UTILITY_PVT.debug_message(fnd_message.get_string('OZF', 'OZF_TP_DELITEM_NOT_ALLOWED_TXT'));
4723 
4724      WHEN FND_API.G_EXC_ERROR THEN
4725           ROLLBACK TO delete_single_product;
4726           x_return_status := FND_API.g_ret_sts_error ;
4727           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
4728                                      p_data    => x_error_message);
4729 
4730      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4731           ROLLBACK TO delete_single_product;
4732           x_return_status := FND_API.g_ret_sts_unexp_error ;
4733           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
4734                                      p_data  => x_error_message);
4735 
4736      WHEN OTHERS THEN
4737           ROLLBACK TO delete_single_product;
4738           x_return_status := FND_API.g_ret_sts_unexp_error ;
4739           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4740              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
4741           END IF;
4742           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
4743                                      p_data  => x_error_message);
4744           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
4745 
4746  END delete_single_product;
4747 
4748 -- ------------------------
4749 -- Private Procedure
4750 -- ------------------------
4751 -- ------------------------------------------------------------------------
4752 -- Name: delete target allocation
4753 -- Desc: Delete all Account, Product and Time allocation records
4754 --       for a given fund_id
4755 --       Note that this will wipe all targets for a given fund_id.
4756 -- ------------------------------------------------------------------------
4757  PROCEDURE delete_target_allocation
4758  (
4759     p_api_version        IN          NUMBER,
4760     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
4761     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
4762     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
4763     x_return_status      OUT NOCOPY  VARCHAR2,
4764     x_error_number       OUT NOCOPY  NUMBER,
4765     x_error_message      OUT NOCOPY  VARCHAR2,
4766     p_fund_id            IN          NUMBER
4767  ) IS
4768 
4769    l_api_version   CONSTANT NUMBER       := 1.0;
4770    l_api_name      CONSTANT VARCHAR2(30) := 'delete_target_allocation';
4771    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
4772    l_return_status          VARCHAR2(1);
4773    l_fund_id                NUMBER;
4774 
4775    CURSOR fund_account_spread_csr
4776          (l_fund_id        number) IS
4777    SELECT DISTINCT
4778        p.account_allocation_id
4779    FROM
4780        ozf_account_allocations p
4781    WHERE
4782        p.allocation_for  = 'FUND'
4783    AND p.allocation_for_id = l_fund_id;
4784 
4785    CURSOR acct_product_spread_csr
4786          (l_acct_allocation_id        number) IS
4787    SELECT DISTINCT
4788        p.product_allocation_id
4789    FROM
4790        ozf_product_allocations p
4791    WHERE
4792        p.allocation_for  = 'CUST'
4793    AND p.allocation_for_id = l_acct_allocation_id;
4794 
4795 
4796  BEGIN
4797 
4798    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- start');
4799 
4800    -- Standard Start of API savepoint
4801    SAVEPOINT delete_target_allocation;
4802 
4803    -- Standard call to check for call compatibility.
4804    IF NOT FND_API.compatible_api_call(l_api_version,
4805                                       p_api_version,
4806                                       l_api_name,
4807                                       g_pkg_name)
4808    THEN
4809      RAISE FND_API.g_exc_unexpected_error;
4810    END IF;
4811 
4812    -- Initialize message list if p_init_msg_list is set to TRUE.
4813    IF FND_API.to_Boolean( p_init_msg_list ) THEN
4814       FND_MSG_PUB.initialize;
4815    END IF;
4816 
4817    -- Initialize API return status to SUCCESS
4818    x_return_status := FND_API.g_ret_sts_success;
4819 
4820    l_fund_id := p_fund_id;
4821 
4822 ------- Start Deleting Product and Time Allocation Records -----------------------
4823 
4824    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name ||
4825                          ': Begin - Deleting Account, Product and Time Allocations Records'
4826                          || 'FOR Fund_id = '|| l_fund_id || ' ; ');
4827 
4828    FOR account_rec IN fund_account_spread_csr(l_fund_id)
4829    LOOP
4830       DELETE ozf_time_allocations t
4831       WHERE t.allocation_for_id = account_rec.account_allocation_id
4832         AND t.allocation_for = 'CUST';
4833 
4834       FOR product_rec IN acct_product_spread_csr(account_rec.account_allocation_id)
4835       LOOP
4836          DELETE ozf_time_allocations t
4837          WHERE t.allocation_for_id = product_rec.product_allocation_id
4838            AND t.allocation_for = 'PROD';
4839       END LOOP;
4840 
4841       DELETE ozf_product_allocations p
4842       WHERE  p.allocation_for  = 'CUST'
4843          AND p.allocation_for_id = account_rec.account_allocation_id;
4844 
4845    END LOOP;
4846 
4847    DELETE ozf_account_allocations p
4848    WHERE  p.allocation_for  = 'FUND'
4849       AND p.allocation_for_id = l_fund_id;
4850 
4851 
4852    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name ||
4853                          ': End - Deleting Account, Product and Time Allocations Records'
4854                          || 'FOR Fund_id = '|| l_fund_id || ' ; ');
4855 
4856    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- end');
4857 
4858  EXCEPTION
4859      WHEN FND_API.G_EXC_ERROR THEN
4860           ROLLBACK TO delete_target_allocation;
4861           x_return_status := FND_API.g_ret_sts_error ;
4862           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
4863                                      p_data    => x_error_message);
4864 
4865      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4866           ROLLBACK TO delete_target_allocation;
4867           x_return_status := FND_API.g_ret_sts_unexp_error ;
4868           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
4869                                      p_data  => x_error_message);
4870 
4871      WHEN OTHERS THEN
4872           ROLLBACK TO delete_target_allocation;
4873           x_return_status := FND_API.g_ret_sts_unexp_error ;
4874           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4875              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
4876           END IF;
4877           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
4878                                      p_data  => x_error_message);
4879           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
4880 
4881  END delete_target_allocation;
4882 
4883 
4884 
4885 -- ------------------------
4886 -- ------------------------
4887 -- Private Procedure
4888 -- ------------------------
4889 -- ------------------------------------------------------------------
4890 -- Name: OLD ALLOCATE TARGET FIRST TIME
4891 -- Desc: 1. Allocate Target across Accounts and Products for Sales Rep
4892 --          when the Fact is published for the first time and a fund_id
4893 --          is created.
4894 --
4895 --  This is obsoleted as of May17th, 2004.
4896 --  and has been renamed old_old_allocate_target_first_time.
4897 -- -----------------------------------------------------------------
4898 PROCEDURE old_allocate_target_first_time
4899  (
4900     p_api_version        IN          NUMBER,
4901     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
4902     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
4903     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
4904     x_return_status      OUT NOCOPY  VARCHAR2,
4905     x_error_number       OUT NOCOPY  NUMBER,
4906     x_error_message      OUT NOCOPY  VARCHAR2,
4907     p_fund_id            IN          NUMBER
4908 ) IS
4909    l_api_version   CONSTANT NUMBER       := 1.0;
4910    l_api_name      CONSTANT VARCHAR2(30) := 'old_allocate_target_first_time';
4911    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
4912    l_return_status          VARCHAR2(1);
4913 
4914    l_fund_id                NUMBER;
4915 
4916    l_object_version_number  NUMBER := 1;
4917    l_org_id                 NUMBER := TO_NUMBER(SUBSTRB(USERENV('CLIENT_INFO'), 1, 10));
4918 
4919    l_start_date             VARCHAR2(30) := null;
4920    l_end_date               VARCHAR2(30) := null;
4921    l_period_type_id         NUMBER;
4922    l_lysp_in_clause         VARCHAR2(1000) := null;
4923    l_in_clause              VARCHAR2(1000) := null;
4924    l_territory_id           NUMBER;
4925    l_resource_id            NUMBER;
4926 
4927    l_total_lysp_sales       NUMBER;
4928 
4929    l_total_account_sales    NUMBER;
4930    l_account_sales          NUMBER;
4931    l_total_account_target   NUMBER;
4932    l_account_target         NUMBER;
4933 
4934    l_total_product_sales    NUMBER;
4935    l_product_sales          NUMBER;
4936    l_total_product_target   NUMBER;
4937    l_product_target         NUMBER;
4938 
4939    l_total_root_quota       NUMBER;
4940    l_total_fund_quota       NUMBER;
4941    l_time_fund_target       NUMBER;
4942 
4943    l_multiplying_factor     NUMBER;
4944    l_prod_mltply_factor     NUMBER;
4945 
4946    l_account_allocation_id  NUMBER;
4947    l_product_allocation_id  NUMBER;
4948    l_time_allocation_id     NUMBER;
4949 
4950    l_denominator            NUMBER;
4951    l_total_target_unalloc   NUMBER;
4952    l_time_target_unalloc    NUMBER;
4953 
4954    l_diff_target            NUMBER;
4955    l_diff_target_1          NUMBER;
4956    l_diff_target_2          NUMBER;
4957 
4958    l_period_tbl          OZF_TIME_API_PVT.G_period_tbl_type;
4959    l_lysp_period_tbl     OZF_TIME_API_PVT.G_period_tbl_type;
4960 
4961    p_acct_alloc_rec      ozf_account_allocations%ROWTYPE;
4962    p_prod_alloc_rec      ozf_product_allocations%ROWTYPE;
4963    p_time_alloc_rec      ozf_time_allocations%ROWTYPE;
4964 
4965 
4966 
4967   CURSOR fund_csr (l_fund_id  NUMBER)
4968   IS
4969    SELECT
4970     owner,
4971     start_period_id,
4972     end_period_id,
4973     start_date_active,
4974     end_date_active,
4975     status_code,
4976     original_budget,
4977     transfered_in_amt,
4978     transfered_out_amt,
4979     node_id, -- (=territory id)
4980     product_spread_time_id period_type_id -- (= minor_scale_id i.e. qtrly or monthly)
4981    FROM
4982     ozf_funds_all_vl
4983    WHERE
4984     fund_id = l_fund_id;
4985 
4986   l_fund_rec    fund_csr%ROWTYPE;
4987 
4988   CURSOR territory_csr (l_resource_id NUMBER) IS
4989    SELECT
4990     j.terr_id territory_id
4991    FROM
4992     jtf_terr_rsc_all j, jtf_terr_rsc_access_all j2
4993    WHERE
4994        j.resource_id = l_resource_id
4995   -- AND j.primary_contact_flag = 'Y' ;
4996    AND j2.terr_rsc_id = j.terr_rsc_id
4997    AND j2.access_type = 'OFFER'
4998    AND j2.trans_access_code = 'PRIMARY_CONTACT';
4999 
5000 /*
5001   CURSOR total_lysp_sales_csr (l_territory_id  NUMBER,
5002                                l_in_clause     VARCHAR2)
5003    IS
5004    SELECT
5005     SUM(bsmv.sales) sales
5006    FROM
5007      ozf_order_sales_v bsmv,
5008      ams_party_market_segments a
5009    WHERE
5010         a.market_qualifier_reference = l_territory_id
5011     AND a.market_qualifier_type='TERRITORY'
5012     AND bsmv.ship_to_site_use_id = a.site_use_id
5013     AND bsmv.time_id IN (l_in_clause);
5014 */
5015 
5016   l_total_lysp_sales_sql varchar2(30000) :=
5017    ' SELECT'||
5018    '  SUM(bsmv.sales) sales'||
5019    ' FROM'||
5020    '   ozf_order_sales_v bsmv,'||
5021    '   ams_party_market_segments a'||
5022    ' WHERE'||
5023    '      a.market_qualifier_reference = :l_territory_id    '||
5024    '  AND a.market_qualifier_type=''TERRITORY''  '||
5025    '  AND bsmv.ship_to_site_use_id = a.site_use_id'||
5026    '  AND bsmv.time_id IN (';
5027 
5028   total_lysp_sales_csr  G_GenericCurType;  --cursor variable (processed like a PL/SQL variable)
5029 
5030 
5031   CURSOR account_csr
5032         (l_territory_id    number)
5033   IS
5034   SELECT
5035     a.cust_account_id                                        cust_account_id,
5036     a.site_use_id                                            site_use_id,
5037     a.site_use_code                                          site_use_code,
5038     OZF_LOCATION_PVT.get_location_id(a.site_use_id)          location_id,
5039     NVL(a.bill_to_site_use_id, -9996)                        bill_to_site_use_id,
5040     OZF_LOCATION_PVT.get_location_id(a.bill_to_site_use_id)  bill_to_location_id,
5041     a.party_id                                               parent_party_id,
5042     NVL(a.rollup_party_id, a.party_id)                       rollup_party_id
5043   FROM
5044     ams_party_market_segments a
5045   WHERE
5046        a.market_qualifier_reference = l_territory_id
5047    AND a.market_qualifier_type='TERRITORY'
5048    AND a.site_use_code = 'SHIP_TO'
5049    AND a.party_id IS NOT NULL
5050    AND a.site_use_id IS NOT NULL;
5051 
5052 /*
5053   SELECT
5054     a.cust_account_id                                        cust_account_id,
5055     a.site_use_id                                            site_use_id,
5056     a.site_use_code                                          site_use_code,
5057     OZF_LOCATION_PVT.get_location_id(a.site_use_id)          location_id,
5058     a.bill_to_site_use_id                                    bill_to_site_use_id,
5059     OZF_LOCATION_PVT.get_location_id(a.bill_to_site_use_id)  bill_to_location_id,
5060     a.party_id                                               parent_party_id,
5061     a.rollup_party_id                                        rollup_party_id
5062   FROM
5063     ams_party_market_segments a
5064   WHERE
5065        a.market_qualifier_reference = l_territory_id
5066    AND a.market_qualifier_type='TERRITORY';
5067 */
5068 
5069 /*
5070   CURSOR account_total_sales_csr
5071          (l_site_use_id         number,
5072           l_in_clause           varchar2)
5073   IS
5074   SELECT
5075       SUM(bsmv.sales) account_sales
5076   FROM
5077       ozf_order_sales_v bsmv
5078   WHERE
5079       bsmv.ship_to_site_use_id = l_site_use_id
5080   AND bsmv.time_id IN (l_in_clause);
5081 
5082 */
5083 
5084   l_account_total_sales_sql varchar2(30000) :=
5085   ' SELECT '||
5086   '     SUM(bsmv.sales) account_sales'||
5087   ' FROM '||
5088   '     ozf_order_sales_v bsmv'||
5089   ' WHERE '||
5090   '     bsmv.ship_to_site_use_id = :l_site_use_id'||
5091   ' AND bsmv.time_id IN (' ;
5092   --l_in_clause);
5093 
5094   account_total_sales_csr  G_GenericCurType;  --cursor variable (processed like a PL/SQL variable
5095 
5096 
5097   CURSOR account_sales_csr
5098          (l_site_use_id         number,
5099           l_time_id             number)
5100   IS
5101   SELECT
5102       SUM(bsmv.sales) account_sales
5103   FROM
5104       ozf_order_sales_v bsmv
5105   WHERE
5106       bsmv.ship_to_site_use_id = l_site_use_id
5107   AND bsmv.time_id = l_time_id;
5108 
5109 /*
5110   CURSOR get_total_target_csr
5111         (l_fund_id   NUMBER,
5112      l_in_clause VARCHAR2) IS
5113    SELECT SUM(t.target)
5114    FROM
5115        ozf_time_allocations t,
5116        ozf_product_allocations p
5117    WHERE
5118        p.fund_id = l_fund_id
5119    AND t.allocation_for_id   = p.product_allocation_id
5120    AND t.allocation_for      = 'PROD'
5121    AND t.time_id IN (l_in_clause);
5122 */
5123 
5124   l_get_total_target_sql VARCHAR2(30000) :=
5125    ' SELECT SUM(t.target) '||
5126    ' FROM '||
5127    '     ozf_time_allocations t,'||
5128    '     ozf_product_allocations p'||
5129    ' WHERE'||
5130    '     p.fund_id = :l_fund_id'||
5131    ' AND t.allocation_for_id   = p.product_allocation_id'||
5132    ' AND t.allocation_for      = ''PROD'' '||
5133    ' AND t.time_id IN (';
5134 --l_in_clause);
5135 
5136   get_total_target_csr  G_GenericCurType;  --cursor variable (processed like a PL/SQL variable)
5137 
5138 
5139   CURSOR product_lysp_sales_csr (l_product_id    NUMBER,
5140                                  l_territory_id  NUMBER,
5141                                  l_site_use_id   NUMBER,
5142                                  l_time_id       NUMBER) IS
5143    SELECT
5144     SUM(bsmv.sales) sales
5145    FROM
5146      ozf_order_sales_v bsmv,
5147      ams_party_market_segments a
5148    WHERE
5149         a.market_qualifier_reference = l_territory_id
5150     AND a.market_qualifier_type='TERRITORY'
5151     AND a.site_use_id = l_site_use_id
5152     AND bsmv.ship_to_site_use_id = a.site_use_id
5153     AND bsmv.inventory_item_id = l_product_id
5154     AND bsmv.time_id = l_time_id;
5155 
5156 
5157   CURSOR category_lysp_sales_csr (l_category_id    NUMBER,
5158                                   l_territory_id   NUMBER,
5159                                   l_site_use_id    NUMBER,
5160                                   l_time_id        NUMBER,
5161                                   l_fund_id        NUMBER) IS
5162   SELECT
5163    SUM(bsmv.sales) sales
5164   FROM
5165    ozf_order_sales_v bsmv,
5166    ams_party_market_segments a
5167   WHERE
5168       a.market_qualifier_reference = l_territory_id
5169   AND a.market_qualifier_type='TERRITORY'
5170   AND a.site_use_id = l_site_use_id
5171   AND bsmv.ship_to_site_use_id = a.site_use_id
5172   AND bsmv.time_id = l_time_id
5173   AND bsmv.inventory_item_id IN
5174                              ( SELECT DISTINCT MIC.INVENTORY_ITEM_ID
5175                                FROM   MTL_ITEM_CATEGORIES     MIC,
5176                                       ENI_PROD_DENORM_HRCHY_V DENORM
5177                                WHERE  MIC.CATEGORY_SET_ID  = DENORM.CATEGORY_SET_ID
5178                                 AND   MIC.CATEGORY_ID      = DENORM.CHILD_ID
5179                                 AND   DENORM.PARENT_ID     = l_category_id
5180                                MINUS
5181                                SELECT p.item_id
5182                                FROM   ozf_product_allocations p
5183                                WHERE  p.fund_id = l_fund_id
5184                                   AND p.item_type = 'PRICING_ATTRIBUTE1'
5185                              );
5186 
5187 
5188   CURSOR others_lysp_sales_csr (l_territory_id   NUMBER,
5189                                 l_site_use_id    NUMBER,
5190                                 l_time_id        NUMBER,
5191                                 l_fund_id        NUMBER) IS
5192   SELECT
5193    SUM(bsmv.sales) sales
5194   FROM
5195    ozf_order_sales_v bsmv,
5196    ams_party_market_segments a
5197   WHERE
5198       a.market_qualifier_reference = l_territory_id
5199   AND a.market_qualifier_type='TERRITORY'
5200   AND a.site_use_id = l_site_use_id
5201   AND bsmv.ship_to_site_use_id = a.site_use_id
5202   AND bsmv.time_id = l_time_id
5203   AND NOT EXISTS
5204   (
5205   ( SELECT p.item_id
5206     FROM ozf_product_allocations p
5207     WHERE
5208         p.fund_id = l_fund_id
5209     AND p.item_type = 'PRICING_ATTRIBUTE1'
5210     AND p.item_id = bsmv.inventory_item_id
5211     UNION ALL
5212     SELECT MIC.INVENTORY_ITEM_ID
5213     FROM   MTL_ITEM_CATEGORIES MIC,
5214            ENI_PROD_DENORM_HRCHY_V DENORM,
5215            OZF_PRODUCT_ALLOCATIONS p
5216     WHERE p.FUND_ID = l_fund_id
5217       AND p.ITEM_TYPE = 'PRICING_ATTRIBUTE2'
5218       AND p.ITEM_ID = DENORM.PARENT_ID
5219       AND MIC.CATEGORY_SET_ID = DENORM.CATEGORY_SET_ID
5220       AND MIC.CATEGORY_ID = DENORM.CHILD_ID
5221       AND MIC.INVENTORY_ITEM_ID = bsmv.inventory_item_id
5222   )
5223   MINUS
5224   SELECT prod.inventory_item_id
5225   FROM ams_act_products prod
5226   where
5227       prod.level_type_code = 'PRODUCT'
5228   AND prod.arc_act_product_used_by = 'FUND'
5229   AND prod.act_product_used_by_id = l_fund_id
5230   AND prod.excluded_flag = 'Y'
5231   AND prod.inventory_item_id = bsmv.inventory_item_id
5232   );
5233 
5234   CURSOR fund_product_spread_csr
5235          (l_fund_id        number) IS
5236     SELECT
5237        p.product_allocation_id,
5238        p.item_id,
5239        p.item_type,
5240        p.target
5241     FROM
5242        ozf_product_allocations p
5243     WHERE
5244        p.fund_id = l_fund_id;
5245 
5246    l_fund_product_rec     fund_product_spread_csr%rowtype;
5247 
5248    CURSOR fund_time_spread_csr
5249          (l_product_allocation_id       number,
5250           l_time_id                     number) IS
5251    SELECT t.target
5252    FROM
5253        ozf_time_allocations t
5254    WHERE
5255        t.allocation_for_id = l_product_allocation_id
5256    AND t.allocation_for = 'PROD'
5257    AND t.time_id = l_time_id;
5258 
5259 
5260  BEGIN
5261 
5262    -- Standard Start of API savepoint
5263    SAVEPOINT old_allocate_target_first_time;
5264 
5265 
5266    -- Standard call to check for call compatibility.
5267    IF NOT FND_API.compatible_api_call(l_api_version,
5268                                       p_api_version,
5269                                       l_api_name,
5270                                       g_pkg_name)
5271    THEN
5272      RAISE FND_API.g_exc_unexpected_error;
5273    END IF;
5274 
5275    -- Initialize message list if p_init_msg_list is set to TRUE.
5276    IF FND_API.to_Boolean( p_init_msg_list ) THEN
5277       FND_MSG_PUB.initialize;
5278    END IF;
5279 
5280    -- Initialize API return status to SUCCESS
5281    x_return_status := FND_API.g_ret_sts_success;
5282 
5283    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- start');
5284 
5285    l_fund_id := p_fund_id;
5286 
5287    OPEN fund_csr(l_fund_id);
5288    FETCH fund_csr INTO l_fund_rec;
5289    CLOSE fund_csr ;
5290 
5291    l_resource_id := l_fund_rec.owner;
5292    l_territory_id := l_fund_rec.node_id;
5293 
5294    IF l_territory_id IS NULL THEN
5295       OPEN territory_csr(l_resource_id);
5296       FETCH territory_csr INTO l_territory_id;
5297       CLOSE territory_csr ;
5298    END IF;
5299 
5300    l_start_date := to_char(l_fund_rec.start_date_active, 'YYYY/MM/DD');
5301    l_end_date   := to_char(l_fund_rec.end_date_active, 'YYYY/MM/DD');
5302    l_period_type_id := l_fund_rec.period_type_id;
5303 
5304    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Getting Time_ids Between '
5305                                                  ||l_start_date||' AND '||l_end_date||' ; '
5306                                                  ||' Period_Type_id = '||l_period_type_id||' ; ');
5307 
5308    IF l_start_date IS NULL OR
5309       l_end_date IS NULL OR
5310       l_period_type_id IS NULL
5311    THEN
5312       RAISE OZF_TP_BLANK_PERIOD_TBL;
5313    END IF;
5314 
5315    l_period_tbl := OZF_TIME_API_PVT.get_period_tbl
5316                                     (l_start_date,
5317                                      l_end_date,
5318                                      l_period_type_id);
5319 
5320    IF l_period_tbl IS NULL OR l_period_tbl.COUNT <= 0 THEN
5321       RAISE OZF_TP_BLANK_PERIOD_TBL;
5322    END IF;
5323 
5324    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Getting Time_ids Between '||l_start_date
5325                                          ||' AND '||l_end_date||' ; ');
5326    IF l_period_tbl IS NOT NULL THEN
5327     IF l_period_tbl.COUNT > 0 THEN
5328       FOR l_idx IN l_period_tbl.first..l_period_tbl.last
5329       LOOP
5330         IF l_period_tbl.exists(l_idx) THEN
5331 
5332          l_lysp_period_tbl(l_idx) := OZF_TIME_API_PVT.get_lysp_id (l_period_tbl(l_idx), l_period_type_id);
5333 
5334          OZF_UTILITY_PVT.debug_message(SubStr('l_period_tbl('||TO_CHAR(l_idx)||') = '
5335                                ||TO_CHAR(l_period_tbl(l_idx)), 1,255));
5336         END IF;
5337       END LOOP;
5338     END IF;
5339    END IF;
5340 
5341    --l_in_clause := '(';
5342    l_in_clause := NULL;
5343    IF l_period_tbl IS NOT NULL THEN
5344       FOR l_idx IN l_period_tbl.first..l_period_tbl.last
5345       LOOP
5346           IF l_in_clause IS NULL THEN
5347              l_in_clause := LTRIM(' '||l_period_tbl(l_idx));
5348           ELSE
5349              l_in_clause := l_in_clause ||','|| l_period_tbl(l_idx);
5350           END IF;
5351       END LOOP;
5352    END IF;
5353    --l_in_clause := l_in_clause||')';
5354 
5355    l_lysp_in_clause := NULL;
5356    IF l_lysp_period_tbl IS NOT NULL THEN
5357       FOR l_idx IN l_lysp_period_tbl.first..l_lysp_period_tbl.last
5358       LOOP
5359           IF l_lysp_in_clause IS NULL THEN
5360              l_lysp_in_clause := LTRIM(' '||l_lysp_period_tbl(l_idx));
5361           ELSE
5362              l_lysp_in_clause := l_lysp_in_clause ||','|| l_lysp_period_tbl(l_idx);
5363           END IF;
5364       END LOOP;
5365    END IF;
5366    --l_lysp_in_clause := l_lysp_in_clause||')';
5367 
5368 
5369    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Time_ids between '||l_start_date
5370                                          ||' AND '||l_end_date||' ARE : '||l_in_clause||' ; ');
5371    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': LYSP-Time_ids between '||l_start_date
5372                                          ||' AND '||l_end_date||' ARE : '||l_lysp_in_clause||' ; ');
5373 
5374 
5375    l_total_lysp_sales_sql := l_total_lysp_sales_sql||l_lysp_in_clause||')';
5376 
5377    OPEN total_lysp_sales_csr FOR l_total_lysp_sales_sql USING l_territory_id;
5378 --   OPEN total_lysp_sales_csr(l_territory_id,l_lysp_in_clause);
5379    FETCH total_lysp_sales_csr INTO l_total_lysp_sales;
5380    CLOSE total_lysp_sales_csr;
5381    l_total_lysp_sales := NVL(l_total_lysp_sales, 0);
5382 
5383    l_total_root_quota := NVL(l_fund_rec.original_budget,0) + NVL(l_fund_rec.transfered_in_amt,0);
5384 
5385    l_total_target_unalloc := 0;
5386    l_time_target_unalloc := 0;
5387    l_multiplying_factor := 0;
5388 
5389    IF l_total_lysp_sales > 0 THEN
5390       l_multiplying_factor := l_total_root_quota / l_total_lysp_sales;
5391    ELSE
5392       l_multiplying_factor := 0;
5393       l_denominator := l_period_tbl.COUNT;
5394 
5395       l_total_target_unalloc := l_total_root_quota;
5396       l_time_target_unalloc := l_total_root_quota / l_denominator;
5397    END IF;
5398 
5399    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Multiplying Factor = '
5400                                                  || l_multiplying_factor || ' ; ');
5401 
5402 ------ for product allocations: based upon Funds product spread ------------------
5403 
5404    l_get_total_target_sql := l_get_total_target_sql||l_in_clause ||')';
5405 
5406    OPEN get_total_target_csr FOR l_get_total_target_sql USING l_fund_id;
5407    FETCH get_total_target_csr INTO l_total_fund_quota;
5408    CLOSE get_total_target_csr ;
5409 
5410    l_total_fund_quota := NVL(l_total_fund_quota, 0);
5411 
5412 ------- Insert rows for ACCOUNTS  ------------------------------------------------
5413 
5414    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name
5415                               || ': Begin - Populating Account and Time Allocations Records'
5416                               || ' FOR Fund_id = '|| l_fund_id
5417                               || ' AND Territory_id = '|| l_territory_id
5418                               || ' ; ');
5419 
5420 
5421    l_account_total_sales_sql := l_account_total_sales_sql||l_lysp_in_clause ||')';
5422 
5423    <<account_loop>>
5424    FOR account_rec IN account_csr(l_territory_id)
5425    LOOP
5426        p_acct_alloc_rec := NULL;
5427 
5428        OPEN account_total_sales_csr FOR l_account_total_sales_sql USING account_rec.site_use_id;
5429 --       OPEN account_total_sales_csr (account_rec.site_use_id, l_lysp_in_clause);
5430        FETCH account_total_sales_csr into l_total_account_sales;
5431        CLOSE account_total_sales_csr;
5432 
5433        l_total_account_sales := NVL(l_total_account_sales, 0);
5434        l_total_account_target := ROUND( (l_total_account_sales * l_multiplying_factor), 0);
5435 
5436        p_acct_alloc_rec.allocation_for := 'FUND';
5437        p_acct_alloc_rec.allocation_for_id := l_fund_id;
5438        p_acct_alloc_rec.cust_account_id := account_rec.cust_account_id;
5439        p_acct_alloc_rec.site_use_id := account_rec.site_use_id;
5440        p_acct_alloc_rec.site_use_code := account_rec.site_use_code;
5441        p_acct_alloc_rec.location_id := account_rec.location_id;
5442        p_acct_alloc_rec.bill_to_site_use_id := account_rec.bill_to_site_use_id;
5443        p_acct_alloc_rec.bill_to_location_id := account_rec.bill_to_location_id;
5444        p_acct_alloc_rec.parent_party_id := account_rec.parent_party_id;
5445        p_acct_alloc_rec.rollup_party_id := account_rec.rollup_party_id;
5446        p_acct_alloc_rec.selected_flag := 'Y';
5447        p_acct_alloc_rec.target := l_total_account_target;
5448        p_acct_alloc_rec.lysp_sales := l_total_account_sales;
5449 
5450        l_account_allocation_id := get_account_allocation_id;
5451 
5452        Ozf_Account_Allocations_Pkg.Insert_Row(
5453           px_Account_allocation_id        => l_account_allocation_id,
5454           p_allocation_for                => p_acct_alloc_rec.allocation_for,
5455           p_allocation_for_id             => p_acct_alloc_rec.allocation_for_id,
5456           p_cust_account_id               => p_acct_alloc_rec.cust_account_id,
5457           p_site_use_id                   => p_acct_alloc_rec.site_use_id,
5458           p_site_use_code                 => p_acct_alloc_rec.site_use_code,
5459           p_location_id                   => p_acct_alloc_rec.location_id,
5460           p_bill_to_site_use_id           => p_acct_alloc_rec.bill_to_site_use_id,
5461           p_bill_to_location_id           => p_acct_alloc_rec.bill_to_location_id,
5462           p_parent_party_id               => p_acct_alloc_rec.parent_party_id,
5463           p_rollup_party_id               => p_acct_alloc_rec.rollup_party_id,
5464           p_selected_flag                 => p_acct_alloc_rec.selected_flag,
5465           p_target                        => p_acct_alloc_rec.target,
5466           p_lysp_sales                    => p_acct_alloc_rec.lysp_sales,
5467           p_parent_Account_allocation_id  => p_acct_alloc_rec.parent_Account_allocation_id,
5468           px_object_version_number        => l_object_version_number,
5469           p_creation_date                 => SYSDATE,
5470           p_created_by                    => FND_GLOBAL.USER_ID,
5471           p_last_update_date              => SYSDATE,
5472           p_last_updated_by               => FND_GLOBAL.USER_ID,
5473           p_last_update_login             => FND_GLOBAL.conc_login_id,
5474           p_attribute_category            => p_acct_alloc_rec.attribute_category,
5475           p_attribute1                    => p_acct_alloc_rec.attribute1,
5476           p_attribute2                    => p_acct_alloc_rec.attribute2,
5477           p_attribute3                    => p_acct_alloc_rec.attribute3,
5478           p_attribute4                    => p_acct_alloc_rec.attribute4,
5479           p_attribute5                    => p_acct_alloc_rec.attribute5,
5480           p_attribute6                    => p_acct_alloc_rec.attribute6,
5481           p_attribute7                    => p_acct_alloc_rec.attribute7,
5482           p_attribute8                    => p_acct_alloc_rec.attribute8,
5483           p_attribute9                    => p_acct_alloc_rec.attribute9,
5484           p_attribute10                   => p_acct_alloc_rec.attribute10,
5485           p_attribute11                   => p_acct_alloc_rec.attribute11,
5486           p_attribute12                   => p_acct_alloc_rec.attribute12,
5487           p_attribute13                   => p_acct_alloc_rec.attribute13,
5488           p_attribute14                   => p_acct_alloc_rec.attribute14,
5489           p_attribute15                   => p_acct_alloc_rec.attribute15,
5490           px_org_id                       => l_org_id
5491         );
5492 
5493        <<account_time_loop>>
5494        FOR l_idx IN l_period_tbl.first..l_period_tbl.last
5495        LOOP
5496         IF l_period_tbl.exists(l_idx) THEN
5497 
5498            p_time_alloc_rec := NULL;
5499            l_account_sales := 0;
5500 
5501            OPEN account_sales_csr (account_rec.site_use_id, l_lysp_period_tbl(l_idx));
5502            FETCH account_sales_csr into l_account_sales;
5503            CLOSE account_sales_csr;
5504 
5505            l_account_sales := NVL(l_account_sales, 0);
5506            l_account_target := ROUND((l_account_sales * l_multiplying_factor), 0);
5507 
5508 
5509            p_time_alloc_rec.allocation_for := 'CUST';
5510            p_time_alloc_rec.allocation_for_id := l_account_allocation_id;
5511            p_time_alloc_rec.time_id := l_period_tbl(l_idx);
5512            p_time_alloc_rec.period_type_id := l_period_type_id;
5513            p_time_alloc_rec.target := l_account_target;
5514            p_time_alloc_rec.lysp_sales := l_account_sales;
5515 
5516            l_time_allocation_id := get_time_allocation_id;
5517 
5518            Ozf_Time_Allocations_Pkg.Insert_Row(
5519               px_time_allocation_id  => l_time_allocation_id,
5520               p_allocation_for  => p_time_alloc_rec.allocation_for,
5521               p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
5522               p_time_id  => p_time_alloc_rec.time_id,
5523               p_period_type_id => p_time_alloc_rec.period_type_id,
5524               p_target  => p_time_alloc_rec.target,
5525               p_lysp_sales  => p_time_alloc_rec.lysp_sales,
5526               px_object_version_number  => l_object_version_number,
5527               p_creation_date  => SYSDATE,
5528               p_created_by  => FND_GLOBAL.USER_ID,
5529               p_last_update_date  => SYSDATE,
5530               p_last_updated_by  => FND_GLOBAL.USER_ID,
5531               p_last_update_login  => FND_GLOBAL.conc_login_id,
5532               p_attribute_category  => p_time_alloc_rec.attribute_category,
5533               p_attribute1  => p_time_alloc_rec.attribute1,
5534               p_attribute2  => p_time_alloc_rec.attribute2,
5535               p_attribute3  => p_time_alloc_rec.attribute3,
5536               p_attribute4  => p_time_alloc_rec.attribute4,
5537               p_attribute5  => p_time_alloc_rec.attribute5,
5538               p_attribute6  => p_time_alloc_rec.attribute6,
5539               p_attribute7  => p_time_alloc_rec.attribute7,
5540               p_attribute8  => p_time_alloc_rec.attribute8,
5541               p_attribute9  => p_time_alloc_rec.attribute9,
5542               p_attribute10  => p_time_alloc_rec.attribute10,
5543               p_attribute11  => p_time_alloc_rec.attribute11,
5544               p_attribute12  => p_time_alloc_rec.attribute12,
5545               p_attribute13  => p_time_alloc_rec.attribute13,
5546               p_attribute14  => p_time_alloc_rec.attribute14,
5547               p_attribute15  => p_time_alloc_rec.attribute15,
5548               px_org_id  => l_org_id
5549             );
5550 
5551 
5552          END IF;
5553        END LOOP account_time_loop;
5554 
5555 ------- Insert rows for PRODUCT and TIME Allocation Records for given ACCOUNT ---------------------------
5556 
5557 
5558        OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name ||
5559                              ': Begin - Populating Product and Time Allocations Records'
5560                               || 'FOR site_use_id = '|| account_rec.site_use_id || ' ; ');
5561 
5562 
5563        l_total_fund_quota := NVL(l_total_fund_quota, 0);
5564 
5565        IF l_total_fund_quota > 0 THEN
5566           l_prod_mltply_factor := l_total_account_target / l_total_fund_quota;
5567        ELSE
5568           l_prod_mltply_factor := 0;
5569        END IF;
5570 
5571        <<account_product_loop>>
5572        FOR fund_product_rec IN fund_product_spread_csr(l_fund_id)
5573        LOOP
5574 
5575            p_prod_alloc_rec := NULL;
5576            l_total_product_target := ROUND( (NVL(fund_product_rec.target ,0) * l_prod_mltply_factor), 0);
5577 
5578            p_prod_alloc_rec.allocation_for := 'CUST';
5579            p_prod_alloc_rec.allocation_for_id := l_account_allocation_id;
5580            p_prod_alloc_rec.item_type := fund_product_rec.item_type;
5581            p_prod_alloc_rec.item_id := fund_product_rec.item_id;
5582            p_prod_alloc_rec.selected_flag := 'N';
5583            p_prod_alloc_rec.target := l_total_product_target;
5584            p_prod_alloc_rec.lysp_sales := 0;
5585 
5586            l_product_allocation_id := get_product_allocation_id;
5587 
5588            Ozf_Product_Allocations_Pkg.Insert_Row(
5589               px_product_allocation_id  => l_product_allocation_id,
5590               p_allocation_for  => p_prod_alloc_rec.allocation_for,
5591               p_allocation_for_id  => p_prod_alloc_rec.allocation_for_id,
5592               p_fund_id  => p_prod_alloc_rec.fund_id,
5593               p_item_type  => p_prod_alloc_rec.item_type,
5594               p_item_id  => p_prod_alloc_rec.item_id,
5595               p_selected_flag  => p_prod_alloc_rec.selected_flag,
5596               p_target  => p_prod_alloc_rec.target,
5597               p_lysp_sales  => p_prod_alloc_rec.lysp_sales,
5598               p_parent_product_allocation_id  => p_prod_alloc_rec.parent_product_allocation_id,
5599               px_object_version_number  => l_object_version_number,
5600               p_creation_date  => SYSDATE,
5601               p_created_by  => FND_GLOBAL.USER_ID,
5602               p_last_update_date  => SYSDATE,
5603               p_last_updated_by  => FND_GLOBAL.USER_ID,
5604               p_last_update_login  => FND_GLOBAL.conc_login_id,
5605               p_attribute_category  => p_prod_alloc_rec.attribute_category,
5606               p_attribute1  => p_prod_alloc_rec.attribute1,
5607               p_attribute2  => p_prod_alloc_rec.attribute2,
5608               p_attribute3  => p_prod_alloc_rec.attribute3,
5609               p_attribute4  => p_prod_alloc_rec.attribute4,
5610               p_attribute5  => p_prod_alloc_rec.attribute5,
5611               p_attribute6  => p_prod_alloc_rec.attribute6,
5612               p_attribute7  => p_prod_alloc_rec.attribute7,
5613               p_attribute8  => p_prod_alloc_rec.attribute8,
5614               p_attribute9  => p_prod_alloc_rec.attribute9,
5615               p_attribute10  => p_prod_alloc_rec.attribute10,
5616               p_attribute11  => p_prod_alloc_rec.attribute11,
5617               p_attribute12  => p_prod_alloc_rec.attribute12,
5618               p_attribute13  => p_prod_alloc_rec.attribute13,
5619               p_attribute14  => p_prod_alloc_rec.attribute14,
5620               p_attribute15  => p_prod_alloc_rec.attribute15,
5621               px_org_id  => l_org_id
5622             );
5623 
5624 
5625            l_total_product_sales := 0;
5626 
5627            <<account_product_time_loop>>
5628            FOR l_idx IN l_period_tbl.first..l_period_tbl.last
5629            LOOP
5630             IF l_period_tbl.exists(l_idx) THEN
5631 
5632                p_time_alloc_rec := NULL;
5633 
5634 
5635                OPEN fund_time_spread_csr(fund_product_rec.product_allocation_id, l_period_tbl(l_idx));
5636                FETCH fund_time_spread_csr INTO l_time_fund_target;
5637                CLOSE fund_time_spread_csr ;
5638 
5639                l_time_fund_target := NVL(l_time_fund_target, 0);
5640 
5641                l_product_target := ROUND( (l_time_fund_target * l_prod_mltply_factor), 0 );
5642 
5643                l_product_sales := 0;
5644 
5645                IF fund_product_rec.item_type = 'PRICING_ATTRIBUTE1' THEN
5646                   OPEN product_lysp_sales_csr(fund_product_rec.item_id,
5647                                               l_territory_id,
5648                                               account_rec.site_use_id,
5649                                               l_lysp_period_tbl(l_idx)
5650                                              );
5651                   FETCH product_lysp_sales_csr INTO l_product_sales;
5652                   CLOSE product_lysp_sales_csr;
5653                ELSIF fund_product_rec.item_type = 'PRICING_ATTRIBUTE2' THEN
5654                    OPEN category_lysp_sales_csr(fund_product_rec.item_id,
5655                                                 l_territory_id,
5656                                                 account_rec.site_use_id,
5657                                                 l_lysp_period_tbl(l_idx),
5658                                                 l_fund_id
5659                                                );
5660                    FETCH category_lysp_sales_csr INTO l_product_sales;
5661                    CLOSE category_lysp_sales_csr;
5662                ELSIF fund_product_rec.item_type = 'OTHERS' THEN
5663                    OPEN others_lysp_sales_csr(l_territory_id,
5664                                               account_rec.site_use_id,
5665                                               l_lysp_period_tbl(l_idx),
5666                                               l_fund_id
5667                                              );
5668                    FETCH others_lysp_sales_csr INTO l_product_sales;
5669                    CLOSE others_lysp_sales_csr;
5670                END IF;
5671 
5672                l_product_sales := NVL(l_product_sales, 0);
5673                l_total_product_sales := l_total_product_sales + l_product_sales;
5674 
5675                p_time_alloc_rec.allocation_for := 'PROD';
5676                p_time_alloc_rec.allocation_for_id := l_product_allocation_id;
5677                p_time_alloc_rec.time_id := l_period_tbl(l_idx);
5678                p_time_alloc_rec.period_type_id := l_period_type_id;
5679                p_time_alloc_rec.target := l_product_target;
5680                p_time_alloc_rec.lysp_sales := l_product_sales;
5681 
5682                l_time_allocation_id := get_time_allocation_id;
5683 
5684                Ozf_Time_Allocations_Pkg.Insert_Row(
5685                   px_time_allocation_id  => l_time_allocation_id,
5686                   p_allocation_for  => p_time_alloc_rec.allocation_for,
5687                   p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
5688                   p_time_id  => p_time_alloc_rec.time_id,
5689                   p_period_type_id => p_time_alloc_rec.period_type_id,
5690                   p_target  => p_time_alloc_rec.target,
5691                   p_lysp_sales  => p_time_alloc_rec.lysp_sales,
5692                   px_object_version_number  => l_object_version_number,
5693                   p_creation_date  => SYSDATE,
5694                   p_created_by  => FND_GLOBAL.USER_ID,
5695                   p_last_update_date  => SYSDATE,
5696                   p_last_updated_by  => FND_GLOBAL.USER_ID,
5697                   p_last_update_login  => FND_GLOBAL.conc_login_id,
5698                   p_attribute_category  => p_time_alloc_rec.attribute_category,
5699                   p_attribute1  => p_time_alloc_rec.attribute1,
5700                   p_attribute2  => p_time_alloc_rec.attribute2,
5701                   p_attribute3  => p_time_alloc_rec.attribute3,
5702                   p_attribute4  => p_time_alloc_rec.attribute4,
5703                   p_attribute5  => p_time_alloc_rec.attribute5,
5704                   p_attribute6  => p_time_alloc_rec.attribute6,
5705                   p_attribute7  => p_time_alloc_rec.attribute7,
5706                   p_attribute8  => p_time_alloc_rec.attribute8,
5707                   p_attribute9  => p_time_alloc_rec.attribute9,
5708                   p_attribute10  => p_time_alloc_rec.attribute10,
5709                   p_attribute11  => p_time_alloc_rec.attribute11,
5710                   p_attribute12  => p_time_alloc_rec.attribute12,
5711                   p_attribute13  => p_time_alloc_rec.attribute13,
5712                   p_attribute14  => p_time_alloc_rec.attribute14,
5713                   p_attribute15  => p_time_alloc_rec.attribute15,
5714                   px_org_id  => l_org_id
5715                 );
5716 
5717 
5718              END IF;
5719            END LOOP account_product_time_loop;
5720 
5721            UPDATE OZF_PRODUCT_ALLOCATIONS p
5722            SET p.lysp_sales = l_total_product_sales,
5723                p.object_version_number = p.object_version_number + 1,
5724                p.last_update_date = SYSDATE,
5725                p.last_updated_by = FND_GLOBAL.USER_ID,
5726                p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
5727            WHERE p.product_allocation_id = l_product_allocation_id;
5728 
5729        END LOOP account_product_loop;
5730 
5731 
5732 
5733 
5734 
5735 
5736 
5737 
5738 
5739 
5740 
5741 
5742 
5743 
5744 
5745 
5746 
5747 
5748 
5749 
5750 
5751 
5752 
5753        OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name ||
5754                              ': End - Populating Product and Time Allocations Records'
5755                              || 'FOR site_use_id = '|| account_rec.site_use_id || ' ; ');
5756 
5757 
5758    END LOOP account_loop;
5759 
5760 
5761    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Populating UNALLOCATED Allocations Records'
5762                                          || 'FOR FUND_Id = '|| l_fund_id || ' ; ');
5763 
5764    p_acct_alloc_rec := NULL;
5765 
5766    p_acct_alloc_rec.allocation_for := 'FUND';
5767    p_acct_alloc_rec.allocation_for_id := l_fund_id;
5768    p_acct_alloc_rec.cust_account_id := -9999;
5769    p_acct_alloc_rec.site_use_id := -9999;
5770    p_acct_alloc_rec.bill_to_site_use_id := -9999;
5771    p_acct_alloc_rec.parent_party_id := -9999;
5772    p_acct_alloc_rec.rollup_party_id := -9999;
5773    p_acct_alloc_rec.selected_flag := 'N';
5774    p_acct_alloc_rec.target := ROUND( (NVL(l_total_target_unalloc, 0)), 0);
5775    p_acct_alloc_rec.lysp_sales := 0;
5776 
5777    l_account_allocation_id := get_account_allocation_id;
5778 
5779    Ozf_Account_Allocations_Pkg.Insert_Row(
5780       px_Account_allocation_id        => l_account_allocation_id,
5781       p_allocation_for                => p_acct_alloc_rec.allocation_for,
5782       p_allocation_for_id             => p_acct_alloc_rec.allocation_for_id,
5783       p_cust_account_id               => p_acct_alloc_rec.cust_account_id,
5784       p_site_use_id                   => p_acct_alloc_rec.site_use_id,
5785       p_site_use_code                 => p_acct_alloc_rec.site_use_code,
5786       p_location_id                   => p_acct_alloc_rec.location_id,
5787       p_bill_to_site_use_id           => p_acct_alloc_rec.bill_to_site_use_id,
5788       p_bill_to_location_id           => p_acct_alloc_rec.bill_to_location_id,
5789       p_parent_party_id               => p_acct_alloc_rec.parent_party_id,
5790       p_rollup_party_id               => p_acct_alloc_rec.rollup_party_id,
5791       p_selected_flag                 => p_acct_alloc_rec.selected_flag,
5792       p_target                        => p_acct_alloc_rec.target,
5793       p_lysp_sales                    => p_acct_alloc_rec.lysp_sales,
5794       p_parent_Account_allocation_id  => p_acct_alloc_rec.parent_Account_allocation_id,
5795       px_object_version_number        => l_object_version_number,
5796       p_creation_date                 => SYSDATE,
5797       p_created_by                    => FND_GLOBAL.USER_ID,
5798       p_last_update_date              => SYSDATE,
5799       p_last_updated_by               => FND_GLOBAL.USER_ID,
5800       p_last_update_login             => FND_GLOBAL.conc_login_id,
5801       p_attribute_category            => p_acct_alloc_rec.attribute_category,
5802       p_attribute1                    => p_acct_alloc_rec.attribute1,
5803       p_attribute2                    => p_acct_alloc_rec.attribute2,
5804       p_attribute3                    => p_acct_alloc_rec.attribute3,
5805       p_attribute4                    => p_acct_alloc_rec.attribute4,
5806       p_attribute5                    => p_acct_alloc_rec.attribute5,
5807       p_attribute6                    => p_acct_alloc_rec.attribute6,
5808       p_attribute7                    => p_acct_alloc_rec.attribute7,
5809       p_attribute8                    => p_acct_alloc_rec.attribute8,
5810       p_attribute9                    => p_acct_alloc_rec.attribute9,
5811       p_attribute10                   => p_acct_alloc_rec.attribute10,
5812       p_attribute11                   => p_acct_alloc_rec.attribute11,
5813       p_attribute12                   => p_acct_alloc_rec.attribute12,
5814       p_attribute13                   => p_acct_alloc_rec.attribute13,
5815       p_attribute14                   => p_acct_alloc_rec.attribute14,
5816       p_attribute15                   => p_acct_alloc_rec.attribute15,
5817       px_org_id                       => l_org_id
5818     );
5819 
5820    <<unalloc_time_loop>>
5821    FOR l_idx IN l_period_tbl.first..l_period_tbl.last
5822    LOOP
5823     IF l_period_tbl.exists(l_idx) THEN
5824 
5825        p_prod_alloc_rec := NULL;
5826 
5827        p_time_alloc_rec.allocation_for := 'CUST';
5828        p_time_alloc_rec.allocation_for_id := l_account_allocation_id;
5829        p_time_alloc_rec.time_id := l_period_tbl(l_idx);
5830        p_time_alloc_rec.period_type_id := l_period_type_id;
5831        p_time_alloc_rec.target := ROUND( (NVL(l_time_target_unalloc, 0)), 0);
5832        p_time_alloc_rec.lysp_sales := 0;
5833 
5834        l_time_allocation_id := get_time_allocation_id;
5835 
5836        Ozf_Time_Allocations_Pkg.Insert_Row(
5837           px_time_allocation_id  => l_time_allocation_id,
5838           p_allocation_for  => p_time_alloc_rec.allocation_for,
5839           p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
5840           p_time_id  => p_time_alloc_rec.time_id,
5841           p_period_type_id => p_time_alloc_rec.period_type_id,
5842           p_target  => p_time_alloc_rec.target,
5843           p_lysp_sales  => p_time_alloc_rec.lysp_sales,
5844           px_object_version_number  => l_object_version_number,
5845           p_creation_date  => SYSDATE,
5846           p_created_by  => FND_GLOBAL.USER_ID,
5847           p_last_update_date  => SYSDATE,
5848           p_last_updated_by  => FND_GLOBAL.USER_ID,
5849           p_last_update_login  => FND_GLOBAL.conc_login_id,
5850           p_attribute_category  => p_time_alloc_rec.attribute_category,
5851           p_attribute1  => p_time_alloc_rec.attribute1,
5852           p_attribute2  => p_time_alloc_rec.attribute2,
5853           p_attribute3  => p_time_alloc_rec.attribute3,
5854           p_attribute4  => p_time_alloc_rec.attribute4,
5855           p_attribute5  => p_time_alloc_rec.attribute5,
5856           p_attribute6  => p_time_alloc_rec.attribute6,
5857           p_attribute7  => p_time_alloc_rec.attribute7,
5858           p_attribute8  => p_time_alloc_rec.attribute8,
5859           p_attribute9  => p_time_alloc_rec.attribute9,
5860           p_attribute10  => p_time_alloc_rec.attribute10,
5861           p_attribute11  => p_time_alloc_rec.attribute11,
5862           p_attribute12  => p_time_alloc_rec.attribute12,
5863           p_attribute13  => p_time_alloc_rec.attribute13,
5864           p_attribute14  => p_time_alloc_rec.attribute14,
5865           p_attribute15  => p_time_alloc_rec.attribute15,
5866           px_org_id  => l_org_id
5867         );
5868 
5869 
5870      END IF;
5871    END LOOP unalloc_time_loop;
5872 
5873 -------BEGIN: FIX for difference due to ROUNDING --------------------------------------------------|
5874 
5875    IF (MOD(l_total_target_unalloc, l_denominator) <> 0) THEN
5876 
5877          l_diff_target := 0;
5878 
5879          BEGIN
5880 
5881               SELECT a.TARGET INTO l_diff_target_1
5882               FROM OZF_ACCOUNT_ALLOCATIONS a
5883                WHERE a.allocation_for = 'FUND'
5884                  AND a.allocation_for_id = l_fund_id
5885                  AND a.parent_party_id = -9999;
5886 
5887               SELECT SUM(t.TARGET) INTO l_diff_target_2
5888               FROM OZF_TIME_ALLOCATIONS t
5889               WHERE  t.allocation_for = 'CUST'
5890                  AND t.allocation_for_id IN ( SELECT a.account_allocation_id
5891                                               FROM  OZF_ACCOUNT_ALLOCATIONS a
5892                                               WHERE a.allocation_for = 'FUND'
5893                                                 AND a.allocation_for_id = l_fund_id
5894                                                 AND a.parent_party_id = -9999 );
5895 
5896              l_diff_target := NVL(l_diff_target_1, 0) - NVL(l_diff_target_2, 0);
5897 
5898          EXCEPTION
5899             WHEN OTHERS THEN
5900                  l_diff_target := 0;
5901          END;
5902 
5903 
5904          IF l_diff_target <> 0 THEN
5905 
5906             UPDATE OZF_TIME_ALLOCATIONS t
5907                 SET t.TARGET = t.TARGET + l_diff_target,
5908                     t.object_version_number = t.object_version_number + 1,
5909                     t.last_update_date = SYSDATE,
5910                     t.last_updated_by = FND_GLOBAL.USER_ID,
5911                     t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
5912               WHERE t.time_allocation_id = (SELECT max(x.time_allocation_id) from OZF_TIME_ALLOCATIONS x
5913                                             WHERE  x.allocation_for = 'CUST'
5914                                             AND x.allocation_for_id IN ( SELECT a.account_allocation_id
5915                                                                          FROM  OZF_ACCOUNT_ALLOCATIONS a
5916                                                                          WHERE a.allocation_for = 'FUND'
5917                                                                            AND a.allocation_for_id = l_fund_id
5918                                                                            AND a.parent_party_id = -9999 )
5919                                            );
5920 
5921              IF (SQL%NOTFOUND) THEN
5922                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
5923                     fnd_message.set_name ('OZF', 'OZF_TP_ROUNDING_TIME_ERR');
5924                     fnd_message.set_token('OZF_TP_OBJECT_TYPE_TOKEN','UNALLOCATED FUND');
5925                     fnd_message.set_token('OZF_TP_OBJECT_ID_TOKEN',l_fund_id);
5926                     fnd_msg_pub.ADD;
5927                  END IF;
5928                  RAISE fnd_api.g_exc_unexpected_error;
5929              END IF;
5930 
5931          END IF;
5932    END IF;
5933 
5934 -------END: FIX for difference due to ROUNDING --------------------------------------------------|
5935 
5936    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Populating UNALLOCATED Allocations Records'
5937                                          || 'FOR FUND_Id = '|| l_fund_id || ' ; ');
5938 
5939 
5940 
5941 
5942 
5943    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- end');
5944 
5945  EXCEPTION
5946 
5947      WHEN OZF_TP_INVALID_PARAM THEN
5948           ROLLBACK TO old_allocate_target_first_time;
5949           FND_MESSAGE.set_name('OZF', 'OZF_TP_INVALID_PARAM_TXT');
5950           FND_MSG_PUB.add;
5951           x_return_status := FND_API.g_ret_sts_error ;
5952           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
5953                                      p_data    => x_error_message);
5954           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : INVALID PARAMETER EXCEPTION = '||sqlerrm(sqlcode));
5955           OZF_UTILITY_PVT.debug_message(fnd_message.get_string('OZF', 'OZF_TP_INVALID_PARAM_TXT'));
5956 
5957      WHEN FND_API.G_EXC_ERROR THEN
5958           ROLLBACK TO old_allocate_target_first_time;
5959           x_return_status := FND_API.g_ret_sts_error ;
5960           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
5961                                      p_data    => x_error_message);
5962 
5963      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5964           ROLLBACK TO old_allocate_target_first_time;
5965           x_return_status := FND_API.g_ret_sts_unexp_error ;
5966           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
5967                                      p_data  => x_error_message);
5968 
5969      WHEN OTHERS THEN
5970           ROLLBACK TO old_allocate_target_first_time;
5971 
5972           FND_MESSAGE.set_name('OZF', 'OZF_TP_OTHERS_ERROR_TXT');
5973           FND_MESSAGE.set_token('OZF_TP_SQLERRM_TOKEN',SQLERRM);
5974           FND_MESSAGE.set_token('OZF_TP_SQLCODE_TOKEN',SQLCODE);
5975           FND_MSG_PUB.add;
5976 
5977           x_return_status := FND_API.g_ret_sts_unexp_error ;
5978           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5979              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
5980           END IF;
5981           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
5982                                      p_data  => x_error_message);
5983           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
5984 
5985  END old_allocate_target_first_time;
5986 
5987 
5988 
5989 -- ------------------------
5990 -- ------------------------
5991 -- Private Procedure
5992 -- ------------------------
5993 -- ------------------------------------------------------------------
5994 -- Name: ALLOCATE TARGET FIRST TIME
5995 -- Desc: 1. Allocate Target across Accounts and Products for Sales Rep
5996 --          when the Fact is published for the first time and a fund_id
5997 --          is created.
5998 --          May 18th, 2004:  The Product Spread for each Site is now
5999 --                           based on its Year Ago Sales.
6000 --                           (and Not on that Leaf Funds Product Spread).
6001 -- -----------------------------------------------------------------
6002 PROCEDURE allocate_target_first_time
6003  (
6004     p_api_version        IN          NUMBER,
6005     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
6006     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
6007     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
6008     x_return_status      OUT NOCOPY  VARCHAR2,
6009     x_error_number       OUT NOCOPY  NUMBER,
6010     x_error_message      OUT NOCOPY  VARCHAR2,
6011     p_fund_id            IN          NUMBER
6012 ) IS
6013    l_api_version   CONSTANT NUMBER       := 1.0;
6014    l_api_name      CONSTANT VARCHAR2(30) := 'allocate_target_first_time';
6015    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
6016    l_return_status          VARCHAR2(1);
6017 
6018    l_fund_id                NUMBER;
6019 
6020    l_object_version_number  NUMBER := 1;
6021    l_org_id                 NUMBER := TO_NUMBER(SUBSTRB(USERENV('CLIENT_INFO'), 1, 10));
6022 
6023    l_start_date             VARCHAR2(30) := null;
6024    l_end_date               VARCHAR2(30) := null;
6025    l_period_type_id         NUMBER;
6026    l_lysp_in_clause         VARCHAR2(1000) := null;
6027    l_in_clause              VARCHAR2(1000) := null;
6028    l_territory_id           NUMBER;
6029    l_resource_id            NUMBER;
6030 
6031    l_total_lysp_sales       NUMBER;
6032 
6033    l_total_account_sales    NUMBER;
6034    l_account_sales          NUMBER;
6035    l_total_account_target   NUMBER;
6036    l_account_target         NUMBER;
6037 
6038    l_total_product_sales    NUMBER;
6039    l_product_sales          NUMBER;
6040    l_total_product_target   NUMBER;
6041    l_product_target         NUMBER;
6042    l_p_grand_total_lysp_sales NUMBER;
6043    l_p_denominator            NUMBER;
6044 
6045    l_total_root_quota       NUMBER;
6046    l_total_fund_quota       NUMBER;
6047    l_time_fund_target       NUMBER;
6048 
6049    l_multiplying_factor     NUMBER;
6050    l_prod_mltply_factor     NUMBER;
6051 
6052    l_account_allocation_id  NUMBER;
6053    l_product_allocation_id  NUMBER;
6054    l_time_allocation_id     NUMBER;
6055 
6056    l_denominator            NUMBER;
6057    l_total_target_unalloc   NUMBER;
6058    l_time_target_unalloc    NUMBER;
6059 
6060    l_diff_target            NUMBER;
6061    l_diff_target_1          NUMBER;
6062    l_diff_target_2          NUMBER;
6063 
6064    l_temp_account_allocation_id NUMBER;
6065    l_temp_product_allocation_id NUMBER;
6066 
6067    l_period_tbl          OZF_TIME_API_PVT.G_period_tbl_type;
6068    l_lysp_period_tbl     OZF_TIME_API_PVT.G_period_tbl_type;
6069 
6070    p_acct_alloc_rec      ozf_account_allocations%ROWTYPE;
6071    p_prod_alloc_rec      ozf_product_allocations%ROWTYPE;
6072    p_time_alloc_rec      ozf_time_allocations%ROWTYPE;
6073 
6074 
6075 
6076   CURSOR fund_csr (l_fund_id  NUMBER)
6077   IS
6078    SELECT
6079     owner,
6080     start_period_id,
6081     end_period_id,
6082     start_date_active,
6083     end_date_active,
6084     status_code,
6085     original_budget,
6086     transfered_in_amt,
6087     transfered_out_amt,
6088     node_id, -- (=territory id)
6089     product_spread_time_id period_type_id -- (= minor_scale_id i.e. qtrly or monthly)
6090    FROM
6091     ozf_funds_all_vl
6092    WHERE
6093     fund_id = l_fund_id;
6094 
6095   l_fund_rec    fund_csr%ROWTYPE;
6096 
6097   CURSOR territory_csr (l_resource_id NUMBER) IS
6098    SELECT
6099     j.terr_id territory_id
6100    FROM
6101     jtf_terr_rsc_all j, jtf_terr_rsc_access_all j2
6102    WHERE
6103        j.resource_id = l_resource_id
6104   -- AND j.primary_contact_flag = 'Y' ;
6105    AND j2.terr_rsc_id = j.terr_rsc_id
6106    AND j2.access_type = 'OFFER'
6107    AND j2.trans_access_code = 'PRIMARY_CONTACT';
6108 
6109 
6110 /*
6111   CURSOR total_lysp_sales_csr (l_territory_id  NUMBER,
6112                                l_in_clause     VARCHAR2)
6113    IS
6114    SELECT
6115     SUM(bsmv.sales) sales
6116    FROM
6117      ozf_order_sales_v bsmv,
6118      ams_party_market_segments a
6119    WHERE
6120         a.market_qualifier_reference = l_territory_id
6121     AND a.market_qualifier_type='TERRITORY'
6122     AND bsmv.ship_to_site_use_id = a.site_use_id
6123     AND bsmv.time_id IN (l_in_clause);
6124 */
6125 
6126   l_total_lysp_sales_sql varchar2(30000) :=
6127    ' SELECT'||
6128    '  SUM(bsmv.sales) sales'||
6129    ' FROM'||
6130    '   ozf_order_sales_v bsmv,'||
6131    '   ams_party_market_segments a'||
6132    ' WHERE'||
6133    '      a.market_qualifier_reference = :l_territory_id    '||
6134    '  AND a.market_qualifier_type=''TERRITORY''  '||
6135    '  AND bsmv.ship_to_site_use_id = a.site_use_id'||
6136    '  AND bsmv.time_id IN (';
6137 
6138   total_lysp_sales_csr  G_GenericCurType;  --cursor variable (processed like a PL/SQL variable)
6139 
6140 
6141   CURSOR account_csr
6142         (l_territory_id    number)
6143   IS
6144   SELECT
6145     a.cust_account_id                                        cust_account_id,
6146     a.site_use_id                                            site_use_id,
6147     a.site_use_code                                          site_use_code,
6148     OZF_LOCATION_PVT.get_location_id(a.site_use_id)          location_id,
6149     NVL(a.bill_to_site_use_id, -9996)                        bill_to_site_use_id,
6150     OZF_LOCATION_PVT.get_location_id(a.bill_to_site_use_id)  bill_to_location_id,
6151     a.party_id                                               parent_party_id,
6152     NVL(a.rollup_party_id, a.party_id)                       rollup_party_id
6153   FROM
6154     ams_party_market_segments a
6155   WHERE
6156        a.market_qualifier_reference = l_territory_id
6157    AND a.market_qualifier_type='TERRITORY'
6158    AND a.site_use_code = 'SHIP_TO'
6159    AND a.party_id IS NOT NULL
6160    AND a.site_use_id IS NOT NULL;
6161 
6162 /*
6163   SELECT
6164     a.cust_account_id                                        cust_account_id,
6165     a.site_use_id                                            site_use_id,
6166     a.site_use_code                                          site_use_code,
6167     OZF_LOCATION_PVT.get_location_id(a.site_use_id)          location_id,
6168     a.bill_to_site_use_id                                    bill_to_site_use_id,
6169     OZF_LOCATION_PVT.get_location_id(a.bill_to_site_use_id)  bill_to_location_id,
6170     a.party_id                                               parent_party_id,
6171     a.rollup_party_id                                        rollup_party_id
6172   FROM
6173     ams_party_market_segments a
6174   WHERE
6175        a.market_qualifier_reference = l_territory_id
6176    AND a.market_qualifier_type='TERRITORY';
6177 */
6178 
6179 /*
6180   CURSOR account_total_sales_csr
6181          (l_site_use_id         number,
6182           l_in_clause           varchar2)
6183   IS
6184   SELECT
6185       SUM(bsmv.sales) account_sales
6186   FROM
6187       ozf_order_sales_v bsmv
6188   WHERE
6189       bsmv.ship_to_site_use_id = l_site_use_id
6190   AND bsmv.time_id IN (l_in_clause);
6191 
6192 */
6193 
6194   l_account_total_sales_sql varchar2(30000) :=
6195   ' SELECT '||
6196   '     SUM(bsmv.sales) account_sales'||
6197   ' FROM '||
6198   '     ozf_order_sales_v bsmv'||
6199   ' WHERE '||
6200   '     bsmv.ship_to_site_use_id = :l_site_use_id'||
6201   ' AND bsmv.time_id IN (' ;
6202   --l_in_clause);
6203 
6204   account_total_sales_csr  G_GenericCurType;  --cursor variable (processed like a PL/SQL variable
6205 
6206 
6207   CURSOR account_sales_csr
6208          (l_site_use_id         number,
6209           l_time_id             number)
6210   IS
6211   SELECT
6212       SUM(bsmv.sales) account_sales
6213   FROM
6214       ozf_order_sales_v bsmv
6215   WHERE
6216       bsmv.ship_to_site_use_id = l_site_use_id
6217   AND bsmv.time_id = l_time_id;
6218 
6219 /*
6220   CURSOR get_total_target_csr
6221         (l_fund_id   NUMBER,
6222      l_in_clause VARCHAR2) IS
6223    SELECT SUM(t.target)
6224    FROM
6225        ozf_time_allocations t,
6226        ozf_product_allocations p
6227    WHERE
6228        p.fund_id = l_fund_id
6229    AND t.allocation_for_id   = p.product_allocation_id
6230    AND t.allocation_for      = 'PROD'
6231    AND t.time_id IN (l_in_clause);
6232 */
6233 
6234   l_get_total_target_sql VARCHAR2(30000) :=
6235    ' SELECT SUM(t.target) '||
6236    ' FROM '||
6237    '     ozf_time_allocations t,'||
6238    '     ozf_product_allocations p'||
6239    ' WHERE'||
6240    '     p.fund_id = :l_fund_id'||
6241    ' AND t.allocation_for_id   = p.product_allocation_id'||
6242    ' AND t.allocation_for      = ''PROD'' '||
6243    ' AND t.time_id IN (';
6244 --l_in_clause);
6245 
6246   get_total_target_csr  G_GenericCurType;  --cursor variable (processed like a PL/SQL variable)
6247 
6248 
6249   CURSOR product_lysp_sales_csr (l_product_id    NUMBER,
6250                                  l_territory_id  NUMBER,
6251                                  l_site_use_id   NUMBER,
6252                                  l_time_id       NUMBER) IS
6253    SELECT
6254     SUM(bsmv.sales) sales
6255    FROM
6256      ozf_order_sales_v bsmv,
6257      ams_party_market_segments a
6258    WHERE
6259         a.market_qualifier_reference = l_territory_id
6260     AND a.market_qualifier_type='TERRITORY'
6261     AND a.site_use_id = l_site_use_id
6262     AND bsmv.ship_to_site_use_id = a.site_use_id
6263     AND bsmv.inventory_item_id = l_product_id
6264     AND bsmv.time_id = l_time_id;
6265 
6266 
6267   CURSOR category_lysp_sales_csr (l_category_id    NUMBER,
6268                                   l_territory_id   NUMBER,
6269                                   l_site_use_id    NUMBER,
6270                                   l_time_id        NUMBER,
6271                                   l_fund_id        NUMBER) IS
6272   SELECT
6273    SUM(bsmv.sales) sales
6274   FROM
6275    ozf_order_sales_v bsmv,
6276    ams_party_market_segments a
6277   WHERE
6278       a.market_qualifier_reference = l_territory_id
6279   AND a.market_qualifier_type='TERRITORY'
6280   AND a.site_use_id = l_site_use_id
6281   AND bsmv.ship_to_site_use_id = a.site_use_id
6282   AND bsmv.time_id = l_time_id
6283   AND bsmv.inventory_item_id IN
6284                              ( SELECT DISTINCT MIC.INVENTORY_ITEM_ID
6285                                FROM   MTL_ITEM_CATEGORIES     MIC,
6286                                       ENI_PROD_DENORM_HRCHY_V DENORM
6287                                WHERE  MIC.CATEGORY_SET_ID  = DENORM.CATEGORY_SET_ID
6288                                 AND   MIC.CATEGORY_ID      = DENORM.CHILD_ID
6289                                 AND   DENORM.PARENT_ID     = l_category_id
6290                                MINUS
6291                                SELECT p.item_id
6292                                FROM   ozf_product_allocations p
6293                                WHERE  p.fund_id = l_fund_id
6294                                   AND p.item_type = 'PRICING_ATTRIBUTE1'
6295                              );
6296 
6297 
6298   CURSOR others_lysp_sales_csr (l_territory_id   NUMBER,
6299                                 l_site_use_id    NUMBER,
6300                                 l_time_id        NUMBER,
6301                                 l_fund_id        NUMBER) IS
6302   SELECT
6303    SUM(bsmv.sales) sales
6304   FROM
6305    ozf_order_sales_v bsmv,
6306    ams_party_market_segments a
6307   WHERE
6308       a.market_qualifier_reference = l_territory_id
6309   AND a.market_qualifier_type='TERRITORY'
6310   AND a.site_use_id = l_site_use_id
6311   AND bsmv.ship_to_site_use_id = a.site_use_id
6312   AND bsmv.time_id = l_time_id
6313   AND NOT EXISTS
6314   (
6315   ( SELECT p.item_id
6316     FROM ozf_product_allocations p
6317     WHERE
6318         p.fund_id = l_fund_id
6319     AND p.item_type = 'PRICING_ATTRIBUTE1'
6320     AND p.item_id = bsmv.inventory_item_id
6321     UNION ALL
6322     SELECT MIC.INVENTORY_ITEM_ID
6323     FROM   MTL_ITEM_CATEGORIES MIC,
6324            ENI_PROD_DENORM_HRCHY_V DENORM,
6325            OZF_PRODUCT_ALLOCATIONS p
6326     WHERE p.FUND_ID = l_fund_id
6327       AND p.ITEM_TYPE = 'PRICING_ATTRIBUTE2'
6328       AND p.ITEM_ID = DENORM.PARENT_ID
6329       AND MIC.CATEGORY_SET_ID = DENORM.CATEGORY_SET_ID
6330       AND MIC.CATEGORY_ID = DENORM.CHILD_ID
6331       AND MIC.INVENTORY_ITEM_ID = bsmv.inventory_item_id
6332   )
6333   MINUS
6334   SELECT prod.inventory_item_id
6335   FROM ams_act_products prod
6336   where
6337       prod.level_type_code = 'PRODUCT'
6338   AND prod.arc_act_product_used_by = 'FUND'
6339   AND prod.act_product_used_by_id = l_fund_id
6340   AND prod.excluded_flag = 'Y'
6341   AND prod.inventory_item_id = bsmv.inventory_item_id
6342   );
6343 
6344   CURSOR fund_product_spread_csr
6345          (l_fund_id        number) IS
6346     SELECT
6347        p.product_allocation_id,
6348        p.item_id,
6349        p.item_type,
6350        p.target
6351     FROM
6352        ozf_product_allocations p
6353     WHERE
6354        p.fund_id = l_fund_id;
6355 
6356    l_fund_product_rec     fund_product_spread_csr%rowtype;
6357 
6358    CURSOR fund_time_spread_csr
6359          (l_product_allocation_id       number,
6360           l_time_id                     number) IS
6361    SELECT t.target
6362    FROM
6363        ozf_time_allocations t
6364    WHERE
6365        t.allocation_for_id = l_product_allocation_id
6366    AND t.allocation_for = 'PROD'
6367    AND t.time_id = l_time_id;
6368 
6369 
6370  BEGIN
6371 
6372    -- Standard Start of API savepoint
6373    SAVEPOINT allocate_target_first_time;
6374 
6375 
6376    -- Standard call to check for call compatibility.
6377    IF NOT FND_API.compatible_api_call(l_api_version,
6378                                       p_api_version,
6379                                       l_api_name,
6380                                       g_pkg_name)
6381    THEN
6382      RAISE FND_API.g_exc_unexpected_error;
6383    END IF;
6384 
6385    -- Initialize message list if p_init_msg_list is set to TRUE.
6386    IF FND_API.to_Boolean( p_init_msg_list ) THEN
6387       FND_MSG_PUB.initialize;
6388    END IF;
6389 
6390    -- Initialize API return status to SUCCESS
6391    x_return_status := FND_API.g_ret_sts_success;
6392 
6393    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- start');
6394 
6395    l_fund_id := p_fund_id;
6396 
6397    OPEN fund_csr(l_fund_id);
6398    FETCH fund_csr INTO l_fund_rec;
6399    CLOSE fund_csr ;
6400 
6401    l_resource_id := l_fund_rec.owner;
6402    l_territory_id := l_fund_rec.node_id;
6403 
6404    IF l_territory_id IS NULL THEN
6405       OPEN territory_csr(l_resource_id);
6406       FETCH territory_csr INTO l_territory_id;
6407       CLOSE territory_csr ;
6408    END IF;
6409 
6410    l_start_date := to_char(l_fund_rec.start_date_active, 'YYYY/MM/DD');
6411    l_end_date   := to_char(l_fund_rec.end_date_active, 'YYYY/MM/DD');
6412    l_period_type_id := l_fund_rec.period_type_id;
6413 
6414    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Getting Time_ids Between '
6415                                                  ||l_start_date||' AND '||l_end_date||' ; '
6416                                                  ||' Period_Type_id = '||l_period_type_id||' ; ');
6417 
6418    IF l_start_date IS NULL OR
6419       l_end_date IS NULL OR
6420       l_period_type_id IS NULL
6421    THEN
6422       RAISE OZF_TP_BLANK_PERIOD_TBL;
6423    END IF;
6424 
6425    l_period_tbl := OZF_TIME_API_PVT.get_period_tbl
6426                                     (l_start_date,
6427                                      l_end_date,
6428                                      l_period_type_id);
6429 
6430    IF l_period_tbl IS NULL OR l_period_tbl.COUNT <= 0 THEN
6431       RAISE OZF_TP_BLANK_PERIOD_TBL;
6432    END IF;
6433 
6434    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Getting Time_ids Between '||l_start_date
6435                                          ||' AND '||l_end_date||' ; ');
6436    IF l_period_tbl IS NOT NULL THEN
6437     IF l_period_tbl.COUNT > 0 THEN
6438       FOR l_idx IN l_period_tbl.first..l_period_tbl.last
6439       LOOP
6440         IF l_period_tbl.exists(l_idx) THEN
6441 
6442          l_lysp_period_tbl(l_idx) := OZF_TIME_API_PVT.get_lysp_id (l_period_tbl(l_idx), l_period_type_id);
6443 
6444          OZF_UTILITY_PVT.debug_message(SubStr('l_period_tbl('||TO_CHAR(l_idx)||') = '
6445                                ||TO_CHAR(l_period_tbl(l_idx)), 1,255));
6446         END IF;
6447       END LOOP;
6448     END IF;
6449    END IF;
6450 
6451    --l_in_clause := '(';
6452    l_in_clause := NULL;
6453    IF l_period_tbl IS NOT NULL THEN
6454       FOR l_idx IN l_period_tbl.first..l_period_tbl.last
6455       LOOP
6456           IF l_in_clause IS NULL THEN
6457              l_in_clause := LTRIM(' '||l_period_tbl(l_idx));
6458           ELSE
6459              l_in_clause := l_in_clause ||','|| l_period_tbl(l_idx);
6460           END IF;
6461       END LOOP;
6462    END IF;
6463    --l_in_clause := l_in_clause||')';
6464 
6465    l_lysp_in_clause := NULL;
6466    IF l_lysp_period_tbl IS NOT NULL THEN
6467       FOR l_idx IN l_lysp_period_tbl.first..l_lysp_period_tbl.last
6468       LOOP
6469           IF l_lysp_in_clause IS NULL THEN
6470              l_lysp_in_clause := LTRIM(' '||l_lysp_period_tbl(l_idx));
6471           ELSE
6472              l_lysp_in_clause := l_lysp_in_clause ||','|| l_lysp_period_tbl(l_idx);
6473           END IF;
6474       END LOOP;
6475    END IF;
6476    --l_lysp_in_clause := l_lysp_in_clause||')';
6477 
6478 
6479    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Time_ids between '||l_start_date
6480                                          ||' AND '||l_end_date||' ARE : '||l_in_clause||' ; ');
6481    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': LYSP-Time_ids between '||l_start_date
6482                                          ||' AND '||l_end_date||' ARE : '||l_lysp_in_clause||' ; ');
6483 
6484 
6485    l_total_lysp_sales_sql := l_total_lysp_sales_sql||l_lysp_in_clause||')';
6486 
6487    OPEN total_lysp_sales_csr FOR l_total_lysp_sales_sql USING l_territory_id;
6488 --   OPEN total_lysp_sales_csr(l_territory_id,l_lysp_in_clause);
6489    FETCH total_lysp_sales_csr INTO l_total_lysp_sales;
6490    CLOSE total_lysp_sales_csr;
6491    l_total_lysp_sales := NVL(l_total_lysp_sales, 0);
6492 
6493    l_total_root_quota := NVL(l_fund_rec.original_budget,0) + NVL(l_fund_rec.transfered_in_amt,0);
6494 
6495    l_total_target_unalloc := 0;
6496    l_time_target_unalloc := 0;
6497    l_multiplying_factor := 0;
6498 
6499    IF l_total_lysp_sales > 0 THEN
6500       l_multiplying_factor := l_total_root_quota / l_total_lysp_sales;
6501    ELSE
6502       l_multiplying_factor := 0;
6503       l_denominator := l_period_tbl.COUNT;
6504 
6505       l_total_target_unalloc := l_total_root_quota;
6506       l_time_target_unalloc := l_total_root_quota / l_denominator;
6507    END IF;
6508 
6509    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Multiplying Factor = '
6510                                                  || l_multiplying_factor || ' ; ');
6511 
6512 ------ for product allocations: based upon Funds product spread ------------------
6513 
6514    l_get_total_target_sql := l_get_total_target_sql||l_in_clause ||')';
6515 
6516    OPEN get_total_target_csr FOR l_get_total_target_sql USING l_fund_id;
6517    FETCH get_total_target_csr INTO l_total_fund_quota;
6518    CLOSE get_total_target_csr ;
6519 
6520    l_total_fund_quota := NVL(l_total_fund_quota, 0);
6521 
6522 ------- Insert rows for ACCOUNTS  ------------------------------------------------
6523 
6524    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name
6525                               || ': Begin - Populating Account and Time Allocations Records'
6526                               || ' FOR Fund_id = '|| l_fund_id
6527                               || ' AND Territory_id = '|| l_territory_id
6528                               || ' ; ');
6529 
6530 
6531    l_account_total_sales_sql := l_account_total_sales_sql||l_lysp_in_clause ||')';
6532 
6533    <<account_loop>>
6534    FOR account_rec IN account_csr(l_territory_id)
6535    LOOP
6536        p_acct_alloc_rec := NULL;
6537 
6538        OPEN account_total_sales_csr FOR l_account_total_sales_sql USING account_rec.site_use_id;
6539 --       OPEN account_total_sales_csr (account_rec.site_use_id, l_lysp_in_clause);
6540        FETCH account_total_sales_csr into l_total_account_sales;
6541        CLOSE account_total_sales_csr;
6542 
6543        l_total_account_sales := NVL(l_total_account_sales, 0);
6544        l_total_account_target := ROUND( (l_total_account_sales * l_multiplying_factor), 0);
6545 
6546        p_acct_alloc_rec.allocation_for := 'FUND';
6547        p_acct_alloc_rec.allocation_for_id := l_fund_id;
6548        p_acct_alloc_rec.cust_account_id := account_rec.cust_account_id;
6549        p_acct_alloc_rec.site_use_id := account_rec.site_use_id;
6550        p_acct_alloc_rec.site_use_code := account_rec.site_use_code;
6551        p_acct_alloc_rec.location_id := account_rec.location_id;
6552        p_acct_alloc_rec.bill_to_site_use_id := account_rec.bill_to_site_use_id;
6553        p_acct_alloc_rec.bill_to_location_id := account_rec.bill_to_location_id;
6554        p_acct_alloc_rec.parent_party_id := account_rec.parent_party_id;
6555        p_acct_alloc_rec.rollup_party_id := account_rec.rollup_party_id;
6556        p_acct_alloc_rec.selected_flag := 'Y';
6557        p_acct_alloc_rec.target := l_total_account_target;
6558        p_acct_alloc_rec.lysp_sales := l_total_account_sales;
6559 
6560        l_account_allocation_id := get_account_allocation_id;
6561 
6562        Ozf_Account_Allocations_Pkg.Insert_Row(
6563           px_Account_allocation_id        => l_account_allocation_id,
6564           p_allocation_for                => p_acct_alloc_rec.allocation_for,
6565           p_allocation_for_id             => p_acct_alloc_rec.allocation_for_id,
6566           p_cust_account_id               => p_acct_alloc_rec.cust_account_id,
6567           p_site_use_id                   => p_acct_alloc_rec.site_use_id,
6568           p_site_use_code                 => p_acct_alloc_rec.site_use_code,
6569           p_location_id                   => p_acct_alloc_rec.location_id,
6570           p_bill_to_site_use_id           => p_acct_alloc_rec.bill_to_site_use_id,
6571           p_bill_to_location_id           => p_acct_alloc_rec.bill_to_location_id,
6572           p_parent_party_id               => p_acct_alloc_rec.parent_party_id,
6573           p_rollup_party_id               => p_acct_alloc_rec.rollup_party_id,
6574           p_selected_flag                 => p_acct_alloc_rec.selected_flag,
6575           p_target                        => p_acct_alloc_rec.target,
6576           p_lysp_sales                    => p_acct_alloc_rec.lysp_sales,
6577           p_parent_Account_allocation_id  => p_acct_alloc_rec.parent_Account_allocation_id,
6578           px_object_version_number        => l_object_version_number,
6579           p_creation_date                 => SYSDATE,
6580           p_created_by                    => FND_GLOBAL.USER_ID,
6581           p_last_update_date              => SYSDATE,
6582           p_last_updated_by               => FND_GLOBAL.USER_ID,
6583           p_last_update_login             => FND_GLOBAL.conc_login_id,
6584           p_attribute_category            => p_acct_alloc_rec.attribute_category,
6585           p_attribute1                    => p_acct_alloc_rec.attribute1,
6586           p_attribute2                    => p_acct_alloc_rec.attribute2,
6587           p_attribute3                    => p_acct_alloc_rec.attribute3,
6588           p_attribute4                    => p_acct_alloc_rec.attribute4,
6589           p_attribute5                    => p_acct_alloc_rec.attribute5,
6590           p_attribute6                    => p_acct_alloc_rec.attribute6,
6591           p_attribute7                    => p_acct_alloc_rec.attribute7,
6592           p_attribute8                    => p_acct_alloc_rec.attribute8,
6593           p_attribute9                    => p_acct_alloc_rec.attribute9,
6594           p_attribute10                   => p_acct_alloc_rec.attribute10,
6595           p_attribute11                   => p_acct_alloc_rec.attribute11,
6596           p_attribute12                   => p_acct_alloc_rec.attribute12,
6597           p_attribute13                   => p_acct_alloc_rec.attribute13,
6598           p_attribute14                   => p_acct_alloc_rec.attribute14,
6599           p_attribute15                   => p_acct_alloc_rec.attribute15,
6600           px_org_id                       => l_org_id
6601         );
6602 
6603        <<account_time_loop>>
6604        FOR l_idx IN l_period_tbl.first..l_period_tbl.last
6605        LOOP
6606         IF l_period_tbl.exists(l_idx) THEN
6607 
6608            p_time_alloc_rec := NULL;
6609            l_account_sales := 0;
6610 
6611            OPEN account_sales_csr (account_rec.site_use_id, l_lysp_period_tbl(l_idx));
6612            FETCH account_sales_csr into l_account_sales;
6613            CLOSE account_sales_csr;
6614 
6615            l_account_sales := NVL(l_account_sales, 0);
6616            l_account_target := ROUND((l_account_sales * l_multiplying_factor), 0);
6617 
6618 
6619            p_time_alloc_rec.allocation_for := 'CUST';
6620            p_time_alloc_rec.allocation_for_id := l_account_allocation_id;
6621            p_time_alloc_rec.time_id := l_period_tbl(l_idx);
6622            p_time_alloc_rec.period_type_id := l_period_type_id;
6623            p_time_alloc_rec.target := l_account_target;
6624            p_time_alloc_rec.lysp_sales := l_account_sales;
6625 
6626            l_time_allocation_id := get_time_allocation_id;
6627 
6628            Ozf_Time_Allocations_Pkg.Insert_Row(
6629               px_time_allocation_id  => l_time_allocation_id,
6630               p_allocation_for  => p_time_alloc_rec.allocation_for,
6631               p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
6632               p_time_id  => p_time_alloc_rec.time_id,
6633               p_period_type_id => p_time_alloc_rec.period_type_id,
6634               p_target  => p_time_alloc_rec.target,
6635               p_lysp_sales  => p_time_alloc_rec.lysp_sales,
6636               px_object_version_number  => l_object_version_number,
6637               p_creation_date  => SYSDATE,
6638               p_created_by  => FND_GLOBAL.USER_ID,
6639               p_last_update_date  => SYSDATE,
6640               p_last_updated_by  => FND_GLOBAL.USER_ID,
6641               p_last_update_login  => FND_GLOBAL.conc_login_id,
6642               p_attribute_category  => p_time_alloc_rec.attribute_category,
6643               p_attribute1  => p_time_alloc_rec.attribute1,
6644               p_attribute2  => p_time_alloc_rec.attribute2,
6645               p_attribute3  => p_time_alloc_rec.attribute3,
6646               p_attribute4  => p_time_alloc_rec.attribute4,
6647               p_attribute5  => p_time_alloc_rec.attribute5,
6648               p_attribute6  => p_time_alloc_rec.attribute6,
6649               p_attribute7  => p_time_alloc_rec.attribute7,
6650               p_attribute8  => p_time_alloc_rec.attribute8,
6651               p_attribute9  => p_time_alloc_rec.attribute9,
6652               p_attribute10  => p_time_alloc_rec.attribute10,
6653               p_attribute11  => p_time_alloc_rec.attribute11,
6654               p_attribute12  => p_time_alloc_rec.attribute12,
6655               p_attribute13  => p_time_alloc_rec.attribute13,
6656               p_attribute14  => p_time_alloc_rec.attribute14,
6657               p_attribute15  => p_time_alloc_rec.attribute15,
6658               px_org_id  => l_org_id
6659             );
6660 
6661 
6662          END IF;
6663        END LOOP account_time_loop;
6664 
6665 
6666    -- For a Given ShipTo, Account Level Target SHOULD be exactly equal to SUM of its Time Level Targets
6667    --                     Basically, get rid of any Rounding error.
6668 
6669    BEGIN
6670 
6671       SELECT SUM(ti.TARGET) into l_total_account_target
6672        FROM  OZF_TIME_ALLOCATIONS ti
6673       WHERE  ti.ALLOCATION_FOR = 'CUST'
6674         AND  ti.ALLOCATION_FOR_ID = l_account_allocation_id;
6675 
6676       UPDATE OZF_ACCOUNT_ALLOCATIONS a
6677       SET a.TARGET = l_total_account_target,
6678           a.object_version_number = a.object_version_number + 1,
6679           a.last_update_date = SYSDATE,
6680           a.last_updated_by = FND_GLOBAL.USER_ID,
6681           a.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
6682       WHERE a.account_allocation_id = l_account_allocation_id;
6683 
6684       IF (SQL%NOTFOUND) THEN
6685          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
6686             fnd_message.set_name ('OZF', 'OZF_TP_TIMEALLOC_NOT_FOUND_TXT');
6687             fnd_msg_pub.ADD;
6688          END IF;
6689 
6690          RAISE fnd_api.g_exc_unexpected_error;
6691       END IF;
6692 
6693    EXCEPTION
6694      WHEN OTHERS THEN
6695           null;
6696    END;
6697 
6698 
6699 
6700 ------- Insert rows for PRODUCT and TIME Allocation Records for given ACCOUNT ---------------------------
6701 
6702 
6703        OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name ||
6704                              ': Begin - Populating Product and Time Allocations Records'
6705                               || 'FOR site_use_id = '|| account_rec.site_use_id || ' ; ');
6706 
6707 
6708 --//00000 May17th,2004 - as per new requirements in Bug 3594874 000000000000000000000000000000000000
6709 /*
6710        l_total_fund_quota := NVL(l_total_fund_quota, 0);
6711 
6712        IF l_total_fund_quota > 0 THEN
6713           l_prod_mltply_factor := l_total_account_target / l_total_fund_quota;
6714        ELSE
6715           l_prod_mltply_factor := 0;
6716        END IF;
6717 */
6718 --//0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
6719 
6720        l_p_grand_total_lysp_sales := 0;
6721 
6722        <<account_product_loop>>
6723        FOR fund_product_rec IN fund_product_spread_csr(l_fund_id)
6724        LOOP
6725 
6726            p_prod_alloc_rec := NULL;
6727 
6728            --l_total_product_target := ROUND( (NVL(fund_product_rec.target ,0) * l_prod_mltply_factor), 0);
6729 
6730            p_prod_alloc_rec.allocation_for := 'CUST';
6731            p_prod_alloc_rec.allocation_for_id := l_account_allocation_id;
6732            p_prod_alloc_rec.item_type := fund_product_rec.item_type;
6733            p_prod_alloc_rec.item_id := fund_product_rec.item_id;
6734            p_prod_alloc_rec.selected_flag := 'N';
6735            p_prod_alloc_rec.target := 0; --- l_total_product_target;
6736            p_prod_alloc_rec.lysp_sales := 0;
6737 
6738            l_product_allocation_id := get_product_allocation_id;
6739 
6740            Ozf_Product_Allocations_Pkg.Insert_Row(
6741               px_product_allocation_id  => l_product_allocation_id,
6742               p_allocation_for  => p_prod_alloc_rec.allocation_for,
6743               p_allocation_for_id  => p_prod_alloc_rec.allocation_for_id,
6744               p_fund_id  => p_prod_alloc_rec.fund_id,
6745               p_item_type  => p_prod_alloc_rec.item_type,
6746               p_item_id  => p_prod_alloc_rec.item_id,
6747               p_selected_flag  => p_prod_alloc_rec.selected_flag,
6748               p_target  => p_prod_alloc_rec.target,
6749               p_lysp_sales  => p_prod_alloc_rec.lysp_sales,
6750               p_parent_product_allocation_id  => p_prod_alloc_rec.parent_product_allocation_id,
6751               px_object_version_number  => l_object_version_number,
6752               p_creation_date  => SYSDATE,
6753               p_created_by  => FND_GLOBAL.USER_ID,
6754               p_last_update_date  => SYSDATE,
6755               p_last_updated_by  => FND_GLOBAL.USER_ID,
6756               p_last_update_login  => FND_GLOBAL.conc_login_id,
6757               p_attribute_category  => p_prod_alloc_rec.attribute_category,
6758               p_attribute1  => p_prod_alloc_rec.attribute1,
6759               p_attribute2  => p_prod_alloc_rec.attribute2,
6760               p_attribute3  => p_prod_alloc_rec.attribute3,
6761               p_attribute4  => p_prod_alloc_rec.attribute4,
6762               p_attribute5  => p_prod_alloc_rec.attribute5,
6763               p_attribute6  => p_prod_alloc_rec.attribute6,
6764               p_attribute7  => p_prod_alloc_rec.attribute7,
6765               p_attribute8  => p_prod_alloc_rec.attribute8,
6766               p_attribute9  => p_prod_alloc_rec.attribute9,
6767               p_attribute10  => p_prod_alloc_rec.attribute10,
6768               p_attribute11  => p_prod_alloc_rec.attribute11,
6769               p_attribute12  => p_prod_alloc_rec.attribute12,
6770               p_attribute13  => p_prod_alloc_rec.attribute13,
6771               p_attribute14  => p_prod_alloc_rec.attribute14,
6772               p_attribute15  => p_prod_alloc_rec.attribute15,
6773               px_org_id  => l_org_id
6774             );
6775 
6776 
6777            l_total_product_sales := 0;
6778 
6779            <<account_product_time_loop>>
6780            FOR l_idx IN l_period_tbl.first..l_period_tbl.last
6781            LOOP
6782             IF l_period_tbl.exists(l_idx) THEN
6783 
6784                p_time_alloc_rec := NULL;
6785 
6786 --//00000 May17th,2004 - as per new requirements in Bug 3594874 000000000000000000000000000000000000
6787 /*
6788                OPEN fund_time_spread_csr(fund_product_rec.product_allocation_id, l_period_tbl(l_idx));
6789                FETCH fund_time_spread_csr INTO l_time_fund_target;
6790                CLOSE fund_time_spread_csr ;
6791 
6792                l_time_fund_target := NVL(l_time_fund_target, 0);
6793 
6794                l_product_target := ROUND( (l_time_fund_target * l_prod_mltply_factor), 0 );
6795 */
6796 --//00000 May17th,2004 - as per new requirements in Bug 3594874 000000000000000000000000000000000000
6797 
6798                l_product_sales := 0;
6799 
6800                IF fund_product_rec.item_type = 'PRICING_ATTRIBUTE1' THEN
6801                   OPEN product_lysp_sales_csr(fund_product_rec.item_id,
6802                                               l_territory_id,
6803                                               account_rec.site_use_id,
6804                                               l_lysp_period_tbl(l_idx)
6805                                              );
6806                   FETCH product_lysp_sales_csr INTO l_product_sales;
6807                   CLOSE product_lysp_sales_csr;
6808                ELSIF fund_product_rec.item_type = 'PRICING_ATTRIBUTE2' THEN
6809                    OPEN category_lysp_sales_csr(fund_product_rec.item_id,
6810                                                 l_territory_id,
6811                                                 account_rec.site_use_id,
6812                                                 l_lysp_period_tbl(l_idx),
6813                                                 l_fund_id
6814                                                );
6815                    FETCH category_lysp_sales_csr INTO l_product_sales;
6816                    CLOSE category_lysp_sales_csr;
6817                ELSIF fund_product_rec.item_type = 'OTHERS' THEN
6818                    OPEN others_lysp_sales_csr(l_territory_id,
6819                                               account_rec.site_use_id,
6820                                               l_lysp_period_tbl(l_idx),
6821                                               l_fund_id
6822                                              );
6823                    FETCH others_lysp_sales_csr INTO l_product_sales;
6824                    CLOSE others_lysp_sales_csr;
6825                END IF;
6826 
6827                l_product_sales := NVL(l_product_sales, 0);
6828                l_total_product_sales := l_total_product_sales + l_product_sales;
6829 
6830                p_time_alloc_rec.allocation_for := 'PROD';
6831                p_time_alloc_rec.allocation_for_id := l_product_allocation_id;
6832                p_time_alloc_rec.time_id := l_period_tbl(l_idx);
6833                p_time_alloc_rec.period_type_id := l_period_type_id;
6834                p_time_alloc_rec.target := 0;  ---------l_product_target;
6835                p_time_alloc_rec.lysp_sales := l_product_sales;
6836 
6837                l_time_allocation_id := get_time_allocation_id;
6838 
6839                Ozf_Time_Allocations_Pkg.Insert_Row(
6840                   px_time_allocation_id  => l_time_allocation_id,
6841                   p_allocation_for  => p_time_alloc_rec.allocation_for,
6842                   p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
6843                   p_time_id  => p_time_alloc_rec.time_id,
6844                   p_period_type_id => p_time_alloc_rec.period_type_id,
6845                   p_target  => p_time_alloc_rec.target,
6846                   p_lysp_sales  => p_time_alloc_rec.lysp_sales,
6847                   px_object_version_number  => l_object_version_number,
6848                   p_creation_date  => SYSDATE,
6849                   p_created_by  => FND_GLOBAL.USER_ID,
6850                   p_last_update_date  => SYSDATE,
6851                   p_last_updated_by  => FND_GLOBAL.USER_ID,
6852                   p_last_update_login  => FND_GLOBAL.conc_login_id,
6853                   p_attribute_category  => p_time_alloc_rec.attribute_category,
6854                   p_attribute1  => p_time_alloc_rec.attribute1,
6855                   p_attribute2  => p_time_alloc_rec.attribute2,
6856                   p_attribute3  => p_time_alloc_rec.attribute3,
6857                   p_attribute4  => p_time_alloc_rec.attribute4,
6858                   p_attribute5  => p_time_alloc_rec.attribute5,
6859                   p_attribute6  => p_time_alloc_rec.attribute6,
6860                   p_attribute7  => p_time_alloc_rec.attribute7,
6861                   p_attribute8  => p_time_alloc_rec.attribute8,
6862                   p_attribute9  => p_time_alloc_rec.attribute9,
6863                   p_attribute10  => p_time_alloc_rec.attribute10,
6864                   p_attribute11  => p_time_alloc_rec.attribute11,
6865                   p_attribute12  => p_time_alloc_rec.attribute12,
6866                   p_attribute13  => p_time_alloc_rec.attribute13,
6867                   p_attribute14  => p_time_alloc_rec.attribute14,
6868                   p_attribute15  => p_time_alloc_rec.attribute15,
6869                   px_org_id  => l_org_id
6870                 );
6871 
6872 
6873              END IF;
6874            END LOOP account_product_time_loop;
6875 
6876            UPDATE OZF_PRODUCT_ALLOCATIONS p
6877            SET p.lysp_sales = l_total_product_sales,
6878                p.object_version_number = p.object_version_number + 1,
6879                p.last_update_date = SYSDATE,
6880                p.last_updated_by = FND_GLOBAL.USER_ID,
6881                p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
6882            WHERE p.product_allocation_id = l_product_allocation_id;
6883 
6884            l_p_grand_total_lysp_sales := l_p_grand_total_lysp_sales + l_total_product_sales;
6885 
6886        END LOOP account_product_loop;
6887 
6888 
6889 ------ Updating Target --Added on May 18th, 2004 --------------------------------------
6890 ------------------------ for each ShipTos Product Spread ------------------------------
6891 
6892    l_prod_mltply_factor := 0;
6893    IF l_p_grand_total_lysp_sales > 0 THEN
6894       l_prod_mltply_factor := NVL(l_total_account_target, 0) / l_p_grand_total_lysp_sales;
6895    ELSE
6896       l_prod_mltply_factor := 0;
6897    END IF;
6898 
6899 -- OZF_UTILITY_PVT.debug_message('1. mkothari --'||' -- l_prod_mltply_factor==> '||l_prod_mltply_factor || ' ; ');
6900 -- OZF_UTILITY_PVT.debug_message('2. mkothari --'||' l_total_account_target===> '||l_total_account_target);
6901 -- OZF_UTILITY_PVT.debug_message('3. mkothari --'||' l_p_grand_total_lysp_sales='||l_p_grand_total_lysp_sales);
6902 
6903    UPDATE OZF_TIME_ALLOCATIONS t
6904    SET t.TARGET = ROUND((NVL(t.LYSP_SALES, 0) * l_prod_mltply_factor), 0),
6905        t.object_version_number = t.object_version_number + 1,
6906        t.last_update_date = SYSDATE,
6907        t.last_updated_by = FND_GLOBAL.USER_ID,
6908        t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
6909    WHERE  t.allocation_for = 'PROD'
6910    AND t.allocation_for_id IN ( SELECT p.product_allocation_id
6911                                  FROM  OZF_PRODUCT_ALLOCATIONS p
6912                                  WHERE p.allocation_for = 'CUST'
6913                                    AND p.allocation_for_id = l_account_allocation_id);
6914 
6915    IF (SQL%NOTFOUND) THEN
6916       IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
6917          fnd_message.set_name ('OZF', 'OZF_TP_TIMEALLOC_NOT_FOUND_TXT');
6918          fnd_msg_pub.ADD;
6919       END IF;
6920 
6921       RAISE fnd_api.g_exc_unexpected_error;
6922    END IF;
6923 
6924    UPDATE OZF_PRODUCT_ALLOCATIONS p
6925    SET p.TARGET = (SELECT SUM(ti.TARGET)
6926                      FROM OZF_TIME_ALLOCATIONS ti
6927                     WHERE ti.ALLOCATION_FOR = 'PROD'
6928                       AND ti.ALLOCATION_FOR_ID = p.PRODUCT_ALLOCATION_ID),
6929        p.object_version_number = p.object_version_number + 1,
6930        p.last_update_date = SYSDATE,
6931        p.last_updated_by = FND_GLOBAL.USER_ID,
6932        p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
6933    WHERE p.allocation_for = 'CUST'
6934      AND p.allocation_for_id = l_account_allocation_id;
6935 
6936    IF (SQL%NOTFOUND) THEN
6937       IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
6938          fnd_message.set_name ('OZF', 'OZF_TP_PRODALLOC_NOT_FOUND_TXT');
6939          fnd_msg_pub.ADD;
6940       END IF;
6941 
6942       RAISE fnd_api.g_exc_unexpected_error;
6943    END IF;
6944 
6945 
6946 
6947    -- Handling Corner Case : If all products including others have ZERO lysp sales, then
6948    --                        OTHERS get all the Quota. It is equally distributed in all
6949    --                        time periods.
6950 
6951    IF l_prod_mltply_factor = 0 THEN
6952 
6953       l_p_denominator := l_period_tbl.COUNT;
6954 
6955 /*
6956       UPDATE OZF_PRODUCT_ALLOCATIONS p
6957       SET p.TARGET = ROUND(NVL(l_total_account_target, 0),0),
6958           p.object_version_number = p.object_version_number + 1,
6959           p.last_update_date = SYSDATE,
6960           p.last_updated_by = FND_GLOBAL.USER_ID,
6961           p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
6962       WHERE p.allocation_for = 'CUST'
6963         AND p.allocation_for_id = l_account_allocation_id
6964         AND p.item_id = -9999;
6965 
6966       IF (SQL%NOTFOUND) THEN
6967          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
6968             fnd_message.set_name ('OZF', 'OZF_TP_PRODALLOC_NOT_FOUND_TXT');
6969             fnd_msg_pub.ADD;
6970          END IF;
6971 
6972          RAISE fnd_api.g_exc_unexpected_error;
6973       END IF;
6974 */
6975 
6976       UPDATE OZF_TIME_ALLOCATIONS t
6977       SET t.TARGET = ROUND((NVL(l_total_account_target, 0) / l_p_denominator), 0),
6978           t.object_version_number = t.object_version_number + 1,
6979           t.last_update_date = SYSDATE,
6980           t.last_updated_by = FND_GLOBAL.USER_ID,
6981           t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
6982       WHERE  t.allocation_for = 'PROD'
6983       AND t.allocation_for_id IN ( SELECT p.product_allocation_id
6984                                     FROM  OZF_PRODUCT_ALLOCATIONS p
6985                                     WHERE p.allocation_for = 'CUST'
6986                                       AND p.allocation_for_id = l_account_allocation_id
6987                                       AND p.item_id = -9999 );
6988 
6989       IF (SQL%NOTFOUND) THEN
6990          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
6991             fnd_message.set_name ('OZF', 'OZF_TP_TIMEALLOC_NOT_FOUND_TXT');
6992             fnd_msg_pub.ADD;
6993          END IF;
6994          RAISE fnd_api.g_exc_unexpected_error;
6995       END IF;
6996 
6997       UPDATE OZF_PRODUCT_ALLOCATIONS p
6998       SET p.TARGET = (SELECT SUM(ti.TARGET)
6999                         FROM OZF_TIME_ALLOCATIONS ti
7000                        WHERE ti.ALLOCATION_FOR = 'PROD'
7001                          AND ti.ALLOCATION_FOR_ID = p.PRODUCT_ALLOCATION_ID),
7002           p.object_version_number = p.object_version_number + 1,
7003           p.last_update_date = SYSDATE,
7004           p.last_updated_by = FND_GLOBAL.USER_ID,
7005           p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
7006       WHERE p.allocation_for = 'CUST'
7007         AND p.allocation_for_id = l_account_allocation_id
7008         AND p.item_id = -9999;
7009 
7010       IF (SQL%NOTFOUND) THEN
7011          IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
7012             fnd_message.set_name ('OZF', 'OZF_TP_PRODALLOC_NOT_FOUND_TXT');
7013             fnd_msg_pub.ADD;
7014          END IF;
7015 
7016          RAISE fnd_api.g_exc_unexpected_error;
7017       END IF;
7018 
7019 
7020 
7021    END IF;
7022 
7023 
7024 -------BEGIN: FIX for difference due to ROUNDING FOR each ShipTos Product Spread ------------------|
7025 
7026          l_diff_target   := 0;
7027          l_diff_target_1 := 0;
7028 
7029      BEGIN
7030 
7031 
7032               SELECT SUM(p.TARGET) INTO l_diff_target_1
7033               FROM OZF_PRODUCT_ALLOCATIONS p
7034                WHERE p.allocation_for = 'CUST'
7035                  AND p.allocation_for_id = l_account_allocation_id;
7036 
7037              l_diff_target := (NVL(l_total_account_target, 0) - NVL(l_diff_target_1, 0));
7038 
7039 -- OZF_UTILITY_PVT.debug_message('mkothari -- l_diff_target => '||l_diff_target || ' ; ');
7040 
7041          EXCEPTION
7042             WHEN OTHERS THEN
7043                  l_diff_target := 0;
7044          END;
7045 
7046      IF ABS(l_diff_target) >= 1 THEN
7047 
7048             IF SIGN(l_diff_target) = -1 THEN
7049                l_diff_target := CEIL(l_diff_target); -- (So, -1.5 will become -1 )
7050             ELSE
7051                l_diff_target := FLOOR(l_diff_target); -- (So, +1.5 will become +1)
7052             END IF;
7053 
7054             fix_product_rounding_err('CUST', l_account_allocation_id, l_diff_target);
7055 
7056 
7057 /*
7058             l_temp_product_allocation_id := 0;
7059 
7060             UPDATE OZF_TIME_ALLOCATIONS t
7061                 SET t.TARGET = t.TARGET + l_diff_target,
7062                     t.object_version_number = t.object_version_number + 1,
7063                     t.last_update_date = SYSDATE,
7064                     t.last_updated_by = FND_GLOBAL.USER_ID,
7065                     t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
7066               WHERE t.time_allocation_id = (SELECT max(x.time_allocation_id)
7067                                             FROM OZF_TIME_ALLOCATIONS x
7068                                             WHERE  x.allocation_for = 'PROD'
7069                                             AND x.allocation_for_id IN ( SELECT max(p.product_allocation_id)
7070                                                                          FROM  OZF_PRODUCT_ALLOCATIONS p
7071                                                                          WHERE p.allocation_for = 'CUST'
7072                                                                            AND p.allocation_for_id = l_account_allocation_id
7073                                                                            AND p.target =
7074                                                                               (SELECT max(xz.target)
7075                                                                                FROM OZF_PRODUCT_ALLOCATIONS xz
7076                                                                                WHERE xz.allocation_for = 'CUST'
7077                                                                                AND xz.allocation_for_id = l_account_allocation_id
7078                                                                                )
7079 
7080                                                                          )
7081                                            AND x.target =
7082                                                (SELECT max(zx.target)
7083                                                 FROM OZF_TIME_ALLOCATIONS zx
7084                                                 WHERE  zx.allocation_for = 'PROD'
7085                                                 AND zx.allocation_for_id IN (SELECT max(pz.product_allocation_id)
7086                                                                              FROM  OZF_PRODUCT_ALLOCATIONS pz
7087                                                                              WHERE pz.allocation_for = 'CUST'
7088                                                                                AND pz.allocation_for_id = l_account_allocation_id
7089                                                                                AND pz.target =
7090                                                                                 (SELECT max(xz.target)
7091                                                                                  FROM OZF_PRODUCT_ALLOCATIONS xz
7092                                                                                 WHERE xz.allocation_for = 'CUST'
7093                                                                                 AND xz.allocation_for_id = l_account_allocation_id
7094                                                                                 )
7095 
7096                                                                              )
7097                                                )
7098                                            )
7099               RETURNING t.allocation_for_id INTO l_temp_product_allocation_id;
7100 
7101 
7102               IF (SQL%NOTFOUND) THEN
7103                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
7104                     fnd_message.set_name ('OZF', 'OZF_TP_TIMEALLOC_NOT_FOUND_TXT');
7105                     fnd_msg_pub.ADD;
7106                  END IF;
7107                  RAISE fnd_api.g_exc_unexpected_error;
7108               END IF;
7109 
7110          UPDATE OZF_PRODUCT_ALLOCATIONS p
7111                 SET p.TARGET = p.TARGET + l_diff_target,
7112                     p.object_version_number = p.object_version_number + 1,
7113                     p.last_update_date = SYSDATE,
7114                     p.last_updated_by = FND_GLOBAL.USER_ID,
7115                     p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
7116               WHERE p.product_allocation_id = l_temp_product_allocation_id;
7117 
7118               IF (SQL%NOTFOUND) THEN
7119                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
7120                     fnd_message.set_name ('OZF', 'OZF_TP_PRODALLOC_NOT_FOUND_TXT');
7121                     fnd_msg_pub.ADD;
7122                  END IF;
7123                  RAISE fnd_api.g_exc_unexpected_error;
7124               END IF;
7125 */
7126 
7127 
7128 /*---------------------------------------------------------------------------------------------------
7129             UPDATE OZF_TIME_ALLOCATIONS t
7130                 SET t.TARGET = t.TARGET + l_diff_target,
7131                     t.object_version_number = t.object_version_number + 1,
7132                     t.last_update_date = SYSDATE,
7133                     t.last_updated_by = FND_GLOBAL.USER_ID,
7134                     t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
7135               WHERE t.time_allocation_id = (SELECT max(x.time_allocation_id) from OZF_TIME_ALLOCATIONS x
7136                                             WHERE  x.allocation_for = 'PROD'
7137                                             AND x.allocation_for_id IN ( SELECT p.product_allocation_id
7138                                                                          FROM  OZF_PRODUCT_ALLOCATIONS p
7139                                                                          WHERE p.allocation_for = 'CUST'
7140                                                                            AND p.allocation_for_id = l_account_allocation_id
7141                                                                            AND p.item_id = -9999 )
7142                                            );
7143 
7144 
7145               IF (SQL%NOTFOUND) THEN
7146                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
7147                     fnd_message.set_name ('OZF', 'OZF_TP_TIMEALLOC_NOT_FOUND_TXT');
7148                     fnd_msg_pub.ADD;
7149                  END IF;
7150                  RAISE fnd_api.g_exc_unexpected_error;
7151               END IF;
7152 
7153          UPDATE OZF_PRODUCT_ALLOCATIONS p
7154                 SET p.TARGET = p.TARGET + l_diff_target,
7155                     p.object_version_number = p.object_version_number + 1,
7156                     p.last_update_date = SYSDATE,
7157                     p.last_updated_by = FND_GLOBAL.USER_ID,
7158                     p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
7159               WHERE p.allocation_for = 'CUST'
7160                 AND p.allocation_for_id = l_account_allocation_id
7161         AND p.item_id = -9999;
7162 
7163               IF (SQL%NOTFOUND) THEN
7164                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
7165                     fnd_message.set_name ('OZF', 'OZF_TP_PRODALLOC_NOT_FOUND_TXT');
7166                     fnd_msg_pub.ADD;
7167                  END IF;
7168                  RAISE fnd_api.g_exc_unexpected_error;
7169               END IF;
7170 ---------------------------------------------------------------------------------------------------
7171 */
7172 
7173       END IF;
7174 
7175 --------END: FIX for difference due to ROUNDING FOR each ShipTos Product Spread ------------------|
7176 
7177 
7178        OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name ||
7179                              ': End - Populating Product and Time Allocations Records'
7180                              || 'FOR site_use_id = '|| account_rec.site_use_id || ' ; ');
7181 
7182 
7183    END LOOP account_loop;
7184 
7185 
7186    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Begin - Populating UNALLOCATED Allocations Records'
7187                                          || 'FOR FUND_Id = '|| l_fund_id || ' ; ');
7188 
7189    p_acct_alloc_rec := NULL;
7190 
7191    p_acct_alloc_rec.allocation_for := 'FUND';
7192    p_acct_alloc_rec.allocation_for_id := l_fund_id;
7193    p_acct_alloc_rec.cust_account_id := -9999;
7194    p_acct_alloc_rec.site_use_id := -9999;
7195    p_acct_alloc_rec.bill_to_site_use_id := -9999;
7196    p_acct_alloc_rec.parent_party_id := -9999;
7197    p_acct_alloc_rec.rollup_party_id := -9999;
7198    p_acct_alloc_rec.selected_flag := 'N';
7199    p_acct_alloc_rec.target := ROUND( (NVL(l_total_target_unalloc, 0)), 0);
7200    p_acct_alloc_rec.lysp_sales := 0;
7201 
7202    l_account_allocation_id := get_account_allocation_id;
7203 
7204    Ozf_Account_Allocations_Pkg.Insert_Row(
7205       px_Account_allocation_id        => l_account_allocation_id,
7206       p_allocation_for                => p_acct_alloc_rec.allocation_for,
7207       p_allocation_for_id             => p_acct_alloc_rec.allocation_for_id,
7208       p_cust_account_id               => p_acct_alloc_rec.cust_account_id,
7209       p_site_use_id                   => p_acct_alloc_rec.site_use_id,
7210       p_site_use_code                 => p_acct_alloc_rec.site_use_code,
7211       p_location_id                   => p_acct_alloc_rec.location_id,
7212       p_bill_to_site_use_id           => p_acct_alloc_rec.bill_to_site_use_id,
7213       p_bill_to_location_id           => p_acct_alloc_rec.bill_to_location_id,
7214       p_parent_party_id               => p_acct_alloc_rec.parent_party_id,
7215       p_rollup_party_id               => p_acct_alloc_rec.rollup_party_id,
7216       p_selected_flag                 => p_acct_alloc_rec.selected_flag,
7217       p_target                        => p_acct_alloc_rec.target,
7218       p_lysp_sales                    => p_acct_alloc_rec.lysp_sales,
7219       p_parent_Account_allocation_id  => p_acct_alloc_rec.parent_Account_allocation_id,
7220       px_object_version_number        => l_object_version_number,
7221       p_creation_date                 => SYSDATE,
7222       p_created_by                    => FND_GLOBAL.USER_ID,
7223       p_last_update_date              => SYSDATE,
7224       p_last_updated_by               => FND_GLOBAL.USER_ID,
7225       p_last_update_login             => FND_GLOBAL.conc_login_id,
7226       p_attribute_category            => p_acct_alloc_rec.attribute_category,
7227       p_attribute1                    => p_acct_alloc_rec.attribute1,
7228       p_attribute2                    => p_acct_alloc_rec.attribute2,
7229       p_attribute3                    => p_acct_alloc_rec.attribute3,
7230       p_attribute4                    => p_acct_alloc_rec.attribute4,
7231       p_attribute5                    => p_acct_alloc_rec.attribute5,
7232       p_attribute6                    => p_acct_alloc_rec.attribute6,
7233       p_attribute7                    => p_acct_alloc_rec.attribute7,
7234       p_attribute8                    => p_acct_alloc_rec.attribute8,
7235       p_attribute9                    => p_acct_alloc_rec.attribute9,
7236       p_attribute10                   => p_acct_alloc_rec.attribute10,
7237       p_attribute11                   => p_acct_alloc_rec.attribute11,
7238       p_attribute12                   => p_acct_alloc_rec.attribute12,
7239       p_attribute13                   => p_acct_alloc_rec.attribute13,
7240       p_attribute14                   => p_acct_alloc_rec.attribute14,
7241       p_attribute15                   => p_acct_alloc_rec.attribute15,
7242       px_org_id                       => l_org_id
7243     );
7244 
7245    <<unalloc_time_loop>>
7246    FOR l_idx IN l_period_tbl.first..l_period_tbl.last
7247    LOOP
7248     IF l_period_tbl.exists(l_idx) THEN
7249 
7250        p_prod_alloc_rec := NULL;
7251 
7252        p_time_alloc_rec.allocation_for := 'CUST';
7253        p_time_alloc_rec.allocation_for_id := l_account_allocation_id;
7254        p_time_alloc_rec.time_id := l_period_tbl(l_idx);
7255        p_time_alloc_rec.period_type_id := l_period_type_id;
7256        p_time_alloc_rec.target := ROUND( (NVL(l_time_target_unalloc, 0)), 0);
7257        p_time_alloc_rec.lysp_sales := 0;
7258 
7259        l_time_allocation_id := get_time_allocation_id;
7260 
7261        Ozf_Time_Allocations_Pkg.Insert_Row(
7262           px_time_allocation_id  => l_time_allocation_id,
7263           p_allocation_for  => p_time_alloc_rec.allocation_for,
7264           p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
7265           p_time_id  => p_time_alloc_rec.time_id,
7266           p_period_type_id => p_time_alloc_rec.period_type_id,
7267           p_target  => p_time_alloc_rec.target,
7268           p_lysp_sales  => p_time_alloc_rec.lysp_sales,
7269           px_object_version_number  => l_object_version_number,
7270           p_creation_date  => SYSDATE,
7271           p_created_by  => FND_GLOBAL.USER_ID,
7272           p_last_update_date  => SYSDATE,
7273           p_last_updated_by  => FND_GLOBAL.USER_ID,
7274           p_last_update_login  => FND_GLOBAL.conc_login_id,
7275           p_attribute_category  => p_time_alloc_rec.attribute_category,
7276           p_attribute1  => p_time_alloc_rec.attribute1,
7277           p_attribute2  => p_time_alloc_rec.attribute2,
7278           p_attribute3  => p_time_alloc_rec.attribute3,
7279           p_attribute4  => p_time_alloc_rec.attribute4,
7280           p_attribute5  => p_time_alloc_rec.attribute5,
7281           p_attribute6  => p_time_alloc_rec.attribute6,
7282           p_attribute7  => p_time_alloc_rec.attribute7,
7283           p_attribute8  => p_time_alloc_rec.attribute8,
7284           p_attribute9  => p_time_alloc_rec.attribute9,
7285           p_attribute10  => p_time_alloc_rec.attribute10,
7286           p_attribute11  => p_time_alloc_rec.attribute11,
7287           p_attribute12  => p_time_alloc_rec.attribute12,
7288           p_attribute13  => p_time_alloc_rec.attribute13,
7289           p_attribute14  => p_time_alloc_rec.attribute14,
7290           p_attribute15  => p_time_alloc_rec.attribute15,
7291           px_org_id  => l_org_id
7292         );
7293 
7294 
7295      END IF;
7296    END LOOP unalloc_time_loop;
7297 
7298 -------BEGIN: FIX for difference due to ROUNDING in Unallocated Rows only ------------------------|
7299 
7300    IF (MOD(l_total_target_unalloc, l_denominator) <> 0) THEN
7301 
7302          l_diff_target := 0;
7303 
7304          BEGIN
7305 
7306               SELECT a.TARGET INTO l_diff_target_1
7307               FROM OZF_ACCOUNT_ALLOCATIONS a
7308                WHERE a.allocation_for = 'FUND'
7309                  AND a.allocation_for_id = l_fund_id
7310                  AND a.parent_party_id = -9999;
7311 
7312               SELECT SUM(t.TARGET) INTO l_diff_target_2
7313               FROM OZF_TIME_ALLOCATIONS t
7314               WHERE  t.allocation_for = 'CUST'
7315                  AND t.allocation_for_id IN ( SELECT a.account_allocation_id
7316                                               FROM  OZF_ACCOUNT_ALLOCATIONS a
7317                                               WHERE a.allocation_for = 'FUND'
7318                                                 AND a.allocation_for_id = l_fund_id
7319                                                 AND a.parent_party_id = -9999 );
7320 
7321              l_diff_target := NVL(l_diff_target_1, 0) - NVL(l_diff_target_2, 0);
7322 
7323 -- OZF_UTILITY_PVT.debug_message('mkothari --- UNALLOCATED l_diff_target => '||l_diff_target || ' ; ');
7324 
7325          EXCEPTION
7326             WHEN OTHERS THEN
7327                  l_diff_target := 0;
7328          END;
7329 
7330 
7331          IF ABS(l_diff_target) >= 1 THEN
7332 
7333             IF SIGN(l_diff_target) = -1 THEN
7334                l_diff_target := CEIL(l_diff_target); -- (So, -1.5 will become -1 )
7335             ELSE
7336                l_diff_target := FLOOR(l_diff_target); -- (So, +1.5 will become +1)
7337             END IF;
7338 
7339             UPDATE OZF_TIME_ALLOCATIONS t
7340                 SET t.TARGET = t.TARGET + l_diff_target,
7341                     t.object_version_number = t.object_version_number + 1,
7342                     t.last_update_date = SYSDATE,
7343                     t.last_updated_by = FND_GLOBAL.USER_ID,
7344                     t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
7345               WHERE t.time_allocation_id = (SELECT max(x.time_allocation_id) from OZF_TIME_ALLOCATIONS x
7346                                             WHERE  x.allocation_for = 'CUST'
7347                                             AND x.allocation_for_id IN ( SELECT a.account_allocation_id
7348                                                                          FROM  OZF_ACCOUNT_ALLOCATIONS a
7349                                                                          WHERE a.allocation_for = 'FUND'
7350                                                                            AND a.allocation_for_id = l_fund_id
7351                                                                            AND a.parent_party_id = -9999 )
7352                                            );
7353 
7354               IF (SQL%NOTFOUND) THEN
7355                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
7356                     fnd_message.set_name ('OZF', 'OZF_TP_ROUNDING_TIME_ERR');
7357                     fnd_message.set_token('OZF_TP_OBJECT_TYPE_TOKEN','UNALLOCATED FUND');
7358                     fnd_message.set_token('OZF_TP_OBJECT_ID_TOKEN',l_fund_id);
7359                     fnd_msg_pub.ADD;
7360                  END IF;
7361                  RAISE fnd_api.g_exc_unexpected_error;
7362               END IF;
7363 
7364          END IF;
7365    END IF;
7366 
7367 -------END: FIX for difference due to ROUNDING --------------------------------------------------|
7368 
7369    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': End - Populating UNALLOCATED Allocations Records'
7370                                          || 'FOR FUND_Id = '|| l_fund_id || ' ; ');
7371 
7372 
7373 
7374 
7375 
7376 -------BEGIN: FIX for difference due to ROUNDING for ALL Targets taken together -------------------|
7377 
7378 
7379    l_diff_target   := 0;
7380    l_diff_target_1 := 0;
7381 
7382    BEGIN
7383 
7384         SELECT SUM(a.TARGET) INTO l_diff_target_1
7385         FROM OZF_ACCOUNT_ALLOCATIONS a
7386         WHERE a.allocation_for = 'FUND'
7387           AND a.allocation_for_id = l_fund_id;
7388 
7389         l_diff_target := (NVL(l_total_root_quota, 0) - NVL(l_diff_target_1, 0));
7390 
7391  -- OZF_UTILITY_PVT.debug_message('mkothari-- ALL TARGETS l_diff_target => '||l_diff_target || ' ; ');
7392 
7393    EXCEPTION
7394       WHEN OTHERS THEN
7395            l_diff_target := 0;
7396    END;
7397 
7398 /*
7399          IF l_diff_target > 0 THEN
7400 
7401             UPDATE OZF_TIME_ALLOCATIONS t
7402                 SET t.TARGET = t.TARGET + l_diff_target,
7403                     t.object_version_number = t.object_version_number + 1,
7404                     t.last_update_date = SYSDATE,
7405                     t.last_updated_by = FND_GLOBAL.USER_ID,
7406                     t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
7407               WHERE t.time_allocation_id = (SELECT max(x.time_allocation_id) from OZF_TIME_ALLOCATIONS x
7408                                             WHERE  x.allocation_for = 'CUST'
7409                                             AND x.allocation_for_id IN ( SELECT a.account_allocation_id
7410                                                                          FROM  OZF_ACCOUNT_ALLOCATIONS a
7411                                                                          WHERE a.allocation_for = 'FUND'
7412                                                                            AND a.allocation_for_id = l_fund_id
7413                                                                            AND a.parent_party_id = -9999)
7414                                            );
7415 
7416               IF (SQL%NOTFOUND) THEN
7417                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
7418                     fnd_message.set_name ('OZF', 'OZF_TP_TIMEALLOC_NOT_FOUND_TXT');
7419                     fnd_msg_pub.ADD;
7420                  END IF;
7421                  RAISE fnd_api.g_exc_unexpected_error;
7422               END IF;
7423 
7424 
7425               UPDATE OZF_ACCOUNT_ALLOCATIONS a
7426               SET a.TARGET = a.TARGET + l_diff_target,
7427                   a.object_version_number = a.object_version_number + 1,
7428                   a.last_update_date = SYSDATE,
7429                   a.last_updated_by = FND_GLOBAL.USER_ID,
7430                   a.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
7431               WHERE a.allocation_for = 'FUND'
7432                 AND a.allocation_for_id = l_fund_id
7433                 AND a.parent_party_id = -9999;
7434 
7435               IF (SQL%NOTFOUND) THEN
7436                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
7437                     fnd_message.set_name ('OZF', 'OZF_TP_TIMEALLOC_NOT_FOUND_TXT');
7438                     fnd_msg_pub.ADD;
7439                  END IF;
7440 
7441                  RAISE fnd_api.g_exc_unexpected_error;
7442               END IF;
7443 */
7444 
7445 
7446    ----Fixing for Account Targets -----------------------
7447    IF ABS(l_diff_target) >= 1 THEN
7448 
7449             IF SIGN(l_diff_target) = -1 THEN
7450                l_diff_target := CEIL(l_diff_target); -- (So, -1.5 will become -1 )
7451             ELSE
7452                l_diff_target := FLOOR(l_diff_target); -- (So, +1.5 will become +1)
7453             END IF;
7454 
7455 -- OZF_UTILITY_PVT.debug_message('222. mkothari-- ALL TARGETS l_diff_target => '||l_diff_target || ' ; ');
7456 
7457             l_temp_account_allocation_id := 0;
7458 
7459             UPDATE OZF_TIME_ALLOCATIONS t
7460                 SET t.TARGET = t.TARGET + l_diff_target,
7461                     t.object_version_number = t.object_version_number + 1,
7462                     t.last_update_date = SYSDATE,
7463                     t.last_updated_by = FND_GLOBAL.USER_ID,
7464                     t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
7465               WHERE t.time_allocation_id = (SELECT MAX(x.time_allocation_id)
7466                                             FROM OZF_TIME_ALLOCATIONS x
7467                                             WHERE  x.allocation_for = 'CUST'
7468                                             AND x.allocation_for_id IN ( SELECT MAX(a.account_allocation_id)
7469                                                                          FROM  OZF_ACCOUNT_ALLOCATIONS a
7470                                                                          WHERE a.allocation_for = 'FUND'
7471                                                                            AND a.allocation_for_id = l_fund_id
7472                                                                            AND a.target = (SELECT MAX(xyz.target)
7473                                                                                             FROM  OZF_ACCOUNT_ALLOCATIONS xyz
7474                                                                                             WHERE xyz.allocation_for = 'FUND'
7475                                                                                               AND xyz.allocation_for_id = l_fund_id)
7476                                                                         )
7477                                             AND x.target = (SELECT MAX(xyz2.target)
7478                                                             FROM  OZF_TIME_ALLOCATIONS xyz2
7479                                                             WHERE xyz2.allocation_for = 'CUST'
7480                                                               AND xyz2.allocation_for_id IN
7481                                                                                     ( SELECT MAX(ax.account_allocation_id)
7482                                                                                       FROM  OZF_ACCOUNT_ALLOCATIONS ax
7483                                                                                       WHERE ax.allocation_for = 'FUND'
7484                                                                                         AND ax.allocation_for_id = l_fund_id
7485                                                                                         AND ax.target =
7486                                                                                             (SELECT MAX(yz.target)
7487                                                                                              FROM  OZF_ACCOUNT_ALLOCATIONS yz
7488                                                                                              WHERE yz.allocation_for = 'FUND'
7489                                                                                                AND yz.allocation_for_id = l_fund_id)
7490                                                                                     )
7491                                                            )
7492                                            )
7493               RETURNING t.allocation_for_id INTO l_temp_account_allocation_id;
7494 
7495              IF (SQL%NOTFOUND) THEN
7496                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
7497                     fnd_message.set_name ('OZF', 'OZF_TP_ROUNDING_TIME_ERR');
7498                     fnd_message.set_token('OZF_TP_OBJECT_TYPE_TOKEN','QUOTA');
7499                     fnd_message.set_token('OZF_TP_OBJECT_ID_TOKEN',l_fund_id);
7500                     fnd_msg_pub.ADD;
7501                  END IF;
7502                  RAISE fnd_api.g_exc_unexpected_error;
7503              END IF;
7504 
7505 
7506              -- OZF_UTILITY_PVT.debug_message('4.mkothari--'||'ALL TARGETS AccAllocID=> '||l_temp_account_allocation_id|| ';');
7507 
7508               UPDATE OZF_ACCOUNT_ALLOCATIONS a
7509               SET a.TARGET = a.TARGET + l_diff_target,
7510                   a.object_version_number = a.object_version_number + 1,
7511                   a.last_update_date = SYSDATE,
7512                   a.last_updated_by = FND_GLOBAL.USER_ID,
7513                   a.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
7514               WHERE a.account_allocation_id = l_temp_account_allocation_id;
7515 
7516              IF (SQL%NOTFOUND) THEN
7517                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
7518                     fnd_message.set_name ('OZF', 'OZF_TP_ROUNDING_ACCT_ERR');
7519                     fnd_message.set_token('OZF_TP_OBJECT_TYPE_TOKEN', 'ACCOUNT ALLOCATION');
7520                     fnd_message.set_token('OZF_TP_OBJECT_ID_TOKEN', l_temp_account_allocation_id);
7521                     fnd_msg_pub.ADD;
7522                  END IF;
7523                  RAISE fnd_api.g_exc_unexpected_error;
7524              END IF;
7525 
7526 
7527 
7528      -------Now fix the corresponding Product Spread for Rounding Adjustment.--------------------
7529 
7530       fix_product_rounding_err('CUST', l_temp_account_allocation_id, l_diff_target);
7531 
7532 
7533 /*
7534             l_temp_product_allocation_id := 0;
7535 
7536             UPDATE OZF_TIME_ALLOCATIONS t
7537                 SET t.TARGET = t.TARGET + l_diff_target,
7538                     t.object_version_number = t.object_version_number + 1,
7539                     t.last_update_date = SYSDATE,
7540                     t.last_updated_by = FND_GLOBAL.USER_ID,
7541                     t.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
7542               WHERE t.time_allocation_id = (SELECT max(x.time_allocation_id)
7543                                             FROM OZF_TIME_ALLOCATIONS x
7544                                             WHERE  x.allocation_for = 'PROD'
7545                                             AND x.allocation_for_id IN ( SELECT max(p.product_allocation_id)
7546                                                                          FROM  OZF_PRODUCT_ALLOCATIONS p
7547                                                                          WHERE p.allocation_for = 'CUST'
7548                                                                            AND p.allocation_for_id = l_temp_account_allocation_id
7549                                                                            AND p.target =
7550                                                                               (SELECT max(xz.target)
7551                                                                                FROM OZF_PRODUCT_ALLOCATIONS xz
7552                                                                                WHERE xz.allocation_for = 'CUST'
7553                                                                                AND xz.allocation_for_id = l_temp_account_allocation_id
7554                                                                                )
7555 
7556                                                                          )
7557                                            AND x.target =
7558                                                (SELECT max(zx.target)
7559                                                 FROM OZF_TIME_ALLOCATIONS zx
7560                                                 WHERE  zx.allocation_for = 'PROD'
7561                                                 AND zx.allocation_for_id IN (SELECT max(pz.product_allocation_id)
7562                                                                              FROM  OZF_PRODUCT_ALLOCATIONS pz
7563                                                                              WHERE pz.allocation_for = 'CUST'
7564                                                                                AND pz.allocation_for_id = l_temp_account_allocation_id
7565                                                                                AND pz.target =
7566                                                                                 (SELECT max(xz.target)
7567                                                                                  FROM OZF_PRODUCT_ALLOCATIONS xz
7568                                                                                 WHERE xz.allocation_for = 'CUST'
7569                                                                                 AND xz.allocation_for_id = l_temp_account_allocation_id
7570                                                                                 )
7571 
7572                                                                              )
7573                                                )
7574                                            )
7575               RETURNING t.allocation_for_id INTO l_temp_product_allocation_id;
7576 
7577 
7578               IF (SQL%NOTFOUND) THEN
7579                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
7580                     fnd_message.set_name ('OZF', 'OZF_TP_TIMEALLOC_NOT_FOUND_TXT');
7581                     fnd_msg_pub.ADD;
7582                  END IF;
7583                  RAISE fnd_api.g_exc_unexpected_error;
7584               END IF;
7585 
7586          UPDATE OZF_PRODUCT_ALLOCATIONS p
7587                 SET p.TARGET = p.TARGET + l_diff_target,
7588                     p.object_version_number = p.object_version_number + 1,
7589                     p.last_update_date = SYSDATE,
7590                     p.last_updated_by = FND_GLOBAL.USER_ID,
7591                     p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
7592               WHERE p.product_allocation_id = l_temp_product_allocation_id;
7593 
7594               IF (SQL%NOTFOUND) THEN
7595                  IF fnd_msg_pub.check_msg_level (fnd_msg_pub.g_msg_lvl_error) THEN
7596                     fnd_message.set_name ('OZF', 'OZF_TP_PRODALLOC_NOT_FOUND_TXT');
7597                     fnd_msg_pub.ADD;
7598                  END IF;
7599                  RAISE fnd_api.g_exc_unexpected_error;
7600               END IF;
7601 
7602 */
7603 
7604 
7605    END IF; -- end of "IF l_diff_target <> 0 THEN"
7606 
7607 
7608 -------END: FIX for difference due to ROUNDING for ALL Targets taken together--------------------|
7609 
7610    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- end');
7611 
7612  EXCEPTION
7613 
7614      WHEN OZF_TP_INVALID_PARAM THEN
7615           ROLLBACK TO allocate_target_first_time;
7616           FND_MESSAGE.set_name('OZF', 'OZF_TP_INVALID_PARAM_TXT');
7617           FND_MSG_PUB.add;
7618           x_return_status := FND_API.g_ret_sts_error ;
7619           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
7620                                      p_data    => x_error_message);
7621           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : INVALID PARAMETER EXCEPTION = '||sqlerrm(sqlcode));
7622           OZF_UTILITY_PVT.debug_message(fnd_message.get_string('OZF', 'OZF_TP_INVALID_PARAM_TXT'));
7623 
7624      WHEN FND_API.G_EXC_ERROR THEN
7625           ROLLBACK TO allocate_target_first_time;
7626           x_return_status := FND_API.g_ret_sts_error ;
7627           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
7628                                      p_data    => x_error_message);
7629 
7630      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7631           ROLLBACK TO allocate_target_first_time;
7632           x_return_status := FND_API.g_ret_sts_unexp_error ;
7633           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
7634                                      p_data  => x_error_message);
7635 
7636      WHEN OTHERS THEN
7637           ROLLBACK TO allocate_target_first_time;
7638 
7639           FND_MESSAGE.set_name('OZF', 'OZF_TP_OTHERS_ERROR_TXT');
7640           FND_MESSAGE.set_token('OZF_TP_SQLERRM_TOKEN',SQLERRM);
7641           FND_MESSAGE.set_token('OZF_TP_SQLCODE_TOKEN',SQLCODE);
7642           FND_MSG_PUB.add;
7643 
7644           x_return_status := FND_API.g_ret_sts_unexp_error ;
7645           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
7646              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
7647           END IF;
7648           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
7649                                      p_data  => x_error_message);
7650           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
7651 
7652  END allocate_target_first_time;
7653 
7654 
7655 -- ------------------------
7656 -- ------------------------
7657 -- Private Procedure
7658 -- ------------------------
7659 -- ------------------------------------------------------------------
7660 -- Name: ALLOCATE TARGET ADDON
7661 -- Desc: 1. When activating an ADD-ON Fact_id, call this API for each
7662 --          Fact_id/Fund_id combination. Target Allocation records
7663 --          are to be created for the newly added period which is
7664 --          calculated by :
7665 --          (time-ids from old-start-date to new-end-date) minus (pre-exisiting-time_ids)
7666 --          Note : 1. Period_type_ids has to be the same.
7667 --             2. The new-start-date can be anyDate greater then old-start-date
7668 --                 3. The new end-date can be any date greater then old-end-date.
7669 --
7670 --   R E M E M B E R to chnage this one too for May17-18, 2004 Chnages
7671 --
7672 -- -----------------------------------------------------------------
7673 PROCEDURE allocate_target_addon
7674  (
7675     p_api_version        IN          NUMBER,
7676     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
7677     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
7678     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
7679     x_return_status      OUT NOCOPY  VARCHAR2,
7680     x_error_number       OUT NOCOPY  NUMBER,
7681     x_error_message      OUT NOCOPY  VARCHAR2,
7682     p_fund_id            IN          NUMBER,
7683     p_old_start_date     IN          DATE,
7684     p_new_end_date       IN          DATE,
7685     p_addon_fact_id      IN          NUMBER,
7686     p_addon_amount       IN          NUMBER
7687 ) IS
7688    l_api_version   CONSTANT NUMBER       := 1.0;
7689    l_api_name      CONSTANT VARCHAR2(30) := 'allocate_target_addon';
7690    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
7691    l_return_status          VARCHAR2(1);
7692 
7693    l_fund_id                NUMBER;
7694    l_old_start_date         DATE;
7695    l_new_end_date           DATE;
7696    l_addon_fact_id          NUMBER;
7697    l_addon_amount           NUMBER;
7698 
7699    l_object_version_number  NUMBER := 1;
7700    l_org_id                 NUMBER := TO_NUMBER(SUBSTRB(USERENV('CLIENT_INFO'), 1, 10));
7701 
7702    l_start_date             VARCHAR2(30) := null;
7703    l_end_date               VARCHAR2(30) := null;
7704 
7705    l_period_type_id         NUMBER;
7706    l_lysp_in_clause         VARCHAR2(1000) := null;
7707    l_in_clause              VARCHAR2(1000) := null;
7708    l_territory_id           NUMBER;
7709    l_resource_id            NUMBER;
7710    l_index                  NUMBER := 0;
7711 
7712    l_total_lysp_sales       NUMBER;
7713 
7714    l_total_account_sales    NUMBER;
7715    l_account_sales          NUMBER;
7716    l_total_account_target   NUMBER;
7717    l_account_target         NUMBER;
7718 
7719    l_total_product_sales    NUMBER;
7720    l_product_sales          NUMBER;
7721    l_total_product_target   NUMBER;
7722    l_product_target         NUMBER;
7723 
7724    l_total_root_quota       NUMBER;
7725    l_total_fund_quota       NUMBER;
7726    l_time_fund_target       NUMBER;
7727 
7728    l_multiplying_factor     NUMBER;
7729    l_prod_mltply_factor     NUMBER;
7730 
7731    l_account_allocation_id  NUMBER;
7732    l_product_allocation_id  NUMBER;
7733    l_time_allocation_id     NUMBER;
7734 
7735    l_denominator            NUMBER;
7736    l_total_target_unalloc   NUMBER;
7737    l_time_target_unalloc    NUMBER;
7738    l_fund_product_rec_exists BOOLEAN;
7739 
7740    l_period_tbl          OZF_TIME_API_PVT.G_period_tbl_type;
7741    l_new_period_tbl      OZF_TIME_API_PVT.G_period_tbl_type;
7742    l_lysp_period_tbl     OZF_TIME_API_PVT.G_period_tbl_type;
7743 
7744    l_new_ozf_period_tbl  OZF_PERIOD_TBL_TYPE;
7745    l_old_ozf_period_tbl  OZF_PERIOD_TBL_TYPE;
7746 
7747    p_acct_alloc_rec      ozf_account_allocations%ROWTYPE;
7748    p_prod_alloc_rec      ozf_product_allocations%ROWTYPE;
7749    p_time_alloc_rec      ozf_time_allocations%ROWTYPE;
7750 
7751 
7752 
7753   CURSOR fund_csr (l_fund_id  NUMBER)
7754   IS
7755    SELECT
7756     owner,
7757     start_period_id,
7758     end_period_id,
7759     start_date_active,
7760     end_date_active,
7761     status_code,
7762     original_budget,
7763     transfered_in_amt,
7764     transfered_out_amt,
7765     node_id, -- (=territory id)
7766     product_spread_time_id period_type_id -- (= minor_scale_id i.e. qtrly or monthly)
7767    FROM
7768     ozf_funds_all_vl
7769    WHERE
7770     fund_id = l_fund_id;
7771 
7772   l_fund_rec    fund_csr%ROWTYPE;
7773 
7774   CURSOR territory_csr (l_resource_id NUMBER) IS
7775    SELECT
7776     j.terr_id territory_id
7777    FROM
7778     jtf_terr_rsc_all j, jtf_terr_rsc_access_all j2
7779    WHERE
7780        j.resource_id = l_resource_id
7781   -- AND j.primary_contact_flag = 'Y' ;
7782    AND j2.terr_rsc_id = j.terr_rsc_id
7783    AND j2.access_type = 'OFFER'
7784    AND j2.trans_access_code = 'PRIMARY_CONTACT';
7785 
7786   CURSOR total_lysp_sales_csr (l_fund_id       NUMBER,
7787                                l_in_clause     VARCHAR2)
7788    IS
7789    SELECT
7790     SUM(bsmv.sales) sales
7791    FROM
7792      ozf_order_sales_v bsmv,
7793      ozf_account_allocations a
7794    WHERE
7795         a.allocation_for = 'FUND'
7796     AND a.allocation_for_id = l_fund_id
7797     AND bsmv.ship_to_site_use_id = a.site_use_id
7798     AND bsmv.time_id IN (l_in_clause);
7799 
7800 
7801   CURSOR account_csr
7802         (l_fund_id    number)
7803   IS
7804   SELECT
7805     a.account_allocation_id account_allocation_id,
7806     a.cust_account_id       cust_account_id,
7807     a.site_use_id           site_use_id,
7808     a.site_use_code         site_use_code,
7809     a.location_id           location_id,
7810     a.bill_to_site_use_id   bill_to_site_use_id,
7811     a.bill_to_location_id   bill_to_location_id,
7812     a.parent_party_id       parent_party_id,
7813     a.rollup_party_id       rollup_party_id
7814   FROM
7815     ozf_account_allocations a
7816   WHERE
7817         a.allocation_for = 'FUND'
7818     AND a.allocation_for_id = l_fund_id;
7819 
7820 
7821   CURSOR account_total_sales_csr
7822          (l_site_use_id         number,
7823           l_in_clause           varchar2)
7824   IS
7825   SELECT
7826       SUM(bsmv.sales) account_sales
7827   FROM
7828       ozf_order_sales_v bsmv
7829   WHERE
7830       bsmv.ship_to_site_use_id = l_site_use_id
7831   AND bsmv.time_id IN (l_in_clause);
7832 
7833 
7834   CURSOR account_sales_csr
7835          (l_site_use_id         number,
7836           l_time_id             number)
7837   IS
7838   SELECT
7839       SUM(bsmv.sales) account_sales
7840   FROM
7841       ozf_order_sales_v bsmv
7842   WHERE
7843       bsmv.ship_to_site_use_id = l_site_use_id
7844   AND bsmv.time_id = l_time_id;
7845 
7846 
7847 /*
7848   CURSOR get_total_target_csr
7849         (l_fund_id   NUMBER,
7850      l_in_clause VARCHAR2) IS
7851    SELECT SUM(t.target)
7852    FROM
7853        ozf_time_allocations t,
7854        ozf_product_allocations p
7855    WHERE
7856        p.fund_id = l_fund_id
7857    AND t.allocation_for_id   = p.product_allocation_id
7858    AND t.allocation_for      = 'PROD'
7859    AND t.time_id IN (l_in_clause);
7860 */
7861 
7862   l_get_total_target_sql VARCHAR2(30000) :=
7863    ' SELECT SUM(t.target) '||
7864    ' FROM '||
7865    '     ozf_time_allocations t,'||
7866    '     ozf_product_allocations p'||
7867    ' WHERE'||
7868    '     p.fund_id = :l_fund_id'||
7869    ' AND t.allocation_for_id   = p.product_allocation_id'||
7870    ' AND t.allocation_for      = ''PROD'' '||
7871    ' AND t.time_id IN (';
7872 --l_in_clause);
7873 
7874   get_total_target_csr  G_GenericCurType;  --cursor variable (processed like a PL/SQL variable)
7875 
7876   CURSOR product_lysp_sales_csr (l_product_id    NUMBER,
7877                                  l_territory_id  NUMBER,
7878                                  l_site_use_id   NUMBER,
7879                                  l_time_id       NUMBER) IS
7880    SELECT
7881     SUM(bsmv.sales) sales
7882    FROM
7883      ozf_order_sales_v bsmv,
7884      ams_party_market_segments a
7885    WHERE
7886         a.market_qualifier_reference = l_territory_id
7887     AND a.market_qualifier_type='TERRITORY'
7888     AND a.site_use_id = l_site_use_id
7889     AND bsmv.ship_to_site_use_id = a.site_use_id
7890     AND bsmv.inventory_item_id = l_product_id
7891     AND bsmv.time_id = l_time_id;
7892 
7893 
7894   CURSOR category_lysp_sales_csr (l_category_id    NUMBER,
7895                                   l_territory_id   NUMBER,
7896                                   l_site_use_id    NUMBER,
7897                                   l_time_id        NUMBER,
7898                                   l_fund_id        NUMBER) IS
7899   SELECT
7900    SUM(bsmv.sales) sales
7901   FROM
7902    ozf_order_sales_v bsmv,
7903    ams_party_market_segments a
7904   WHERE
7905       a.market_qualifier_reference = l_territory_id
7906   AND a.market_qualifier_type='TERRITORY'
7907   AND a.site_use_id = l_site_use_id
7908   AND bsmv.ship_to_site_use_id = a.site_use_id
7909   AND bsmv.time_id = l_time_id
7910   AND bsmv.inventory_item_id IN
7911                              ( SELECT  mtl.inventory_item_id
7912                                FROM    mtl_item_categories mtl
7913                                WHERE   mtl.category_id = l_category_id
7914                                MINUS
7915                                SELECT p.item_id
7916                                FROM   ozf_product_allocations p
7917                                WHERE  p.fund_id = l_fund_id
7918                                   AND p.item_type = 'PRICING_ATTRIBUTE1'
7919                              );
7920 
7921 
7922   CURSOR others_lysp_sales_csr (l_territory_id   NUMBER,
7923                                 l_site_use_id    NUMBER,
7924                                 l_time_id        NUMBER,
7925                                 l_fund_id        NUMBER) IS
7926   SELECT
7927    SUM(bsmv.sales) sales
7928   FROM
7929    ozf_order_sales_v bsmv,
7930    ams_party_market_segments a
7931   WHERE
7932       a.market_qualifier_reference = l_territory_id
7933   AND a.market_qualifier_type='TERRITORY'
7934   AND a.site_use_id = l_site_use_id
7935   AND bsmv.ship_to_site_use_id = a.site_use_id
7936   AND bsmv.time_id = l_time_id
7937   AND NOT EXISTS
7938   (
7939   ( SELECT p.item_id
7940     FROM ozf_product_allocations p
7941     WHERE
7942         p.fund_id = l_fund_id
7943     AND p.item_type = 'PRICING_ATTRIBUTE1'
7944     AND p.item_id = bsmv.inventory_item_id
7945     UNION ALL
7946     SELECT mtl.inventory_item_id
7947     FROM ozf_product_allocations p,
7948          mtl_item_categories mtl
7949     WHERE
7950       p.fund_id = l_fund_id
7951   AND p.item_type = 'PRICING_ATTRIBUTE2'
7952   AND p.item_id = mtl.category_id
7953   AND mtl.inventory_item_id = bsmv.inventory_item_id
7954   )
7955   MINUS
7956   SELECT prod.inventory_item_id
7957   FROM ams_act_products prod
7958   where
7959       prod.level_type_code = 'PRODUCT'
7960   AND prod.arc_act_product_used_by = 'FUND'
7961   AND prod.act_product_used_by_id = l_fund_id
7962   AND prod.excluded_flag = 'Y'
7963   AND prod.inventory_item_id = bsmv.inventory_item_id
7964   );
7965 
7966   CURSOR account_product_spread_csr
7967        (l_account_allocation_id        number) IS
7968     SELECT
7969        p.product_allocation_id,
7970        p.item_id,
7971        p.item_type,
7972        p.target
7973     FROM
7974        ozf_product_allocations p
7975     WHERE
7976          p.allocation_for = 'CUST'
7977      AND p.allocation_for_id = l_account_allocation_id;
7978 
7979    l_account_product_rec     account_product_spread_csr%rowtype;
7980 
7981    CURSOR account_time_spread_csr
7982          (l_product_allocation_id       number,
7983           l_time_id                     number) IS
7984    SELECT t.target
7985    FROM
7986        ozf_time_allocations t
7987    WHERE
7988        t.allocation_for_id = l_product_allocation_id
7989    AND t.allocation_for = 'PROD'
7990    AND t.time_id = l_time_id;
7991 
7992   CURSOR fund_product_spread_csr
7993          (l_fund_id        number,
7994           l_addon_fact_id  number,
7995           l_item_id        number,
7996           l_item_type      varchar2) IS
7997     SELECT
7998        p.product_allocation_id,
7999        p.target
8000     FROM
8001        ozf_product_allocations p
8002     WHERE
8003         p.fund_id = l_fund_id
8004     AND p.allocation_for = 'FACT'
8005     AND p.allocation_for_id = l_addon_fact_id
8006     AND p.item_id = l_item_id
8007     AND p.item_type = l_item_type;
8008 
8009    l_fund_product_rec     fund_product_spread_csr%rowtype;
8010 
8011    CURSOR fund_time_spread_csr
8012          (l_product_allocation_id       number,
8013           l_time_id                     number) IS
8014    SELECT t.target
8015    FROM
8016        ozf_time_allocations t
8017    WHERE
8018        t.allocation_for_id = l_product_allocation_id
8019    AND t.allocation_for = 'PROD'
8020    AND t.time_id = l_time_id;
8021 
8022    CURSOR get_old_period_csr
8023           (l_fund_id   NUMBER) IS
8024       SELECT DISTINCT t.time_id
8025       FROM
8026           ozf_time_allocations t,
8027           ozf_account_allocations a
8028       WHERE
8029           a.allocation_for = 'FUND'
8030       AND a.allocation_for_id = l_fund_id
8031       AND t.allocation_for_id = a.account_allocation_id
8032       AND t.allocation_for    = 'CUST';
8033 
8034 
8035  BEGIN
8036 
8037    -- Standard Start of API savepoint
8038    SAVEPOINT allocate_target_addon;
8039 
8040 
8041    -- Standard call to check for call compatibility.
8042    IF NOT FND_API.compatible_api_call(l_api_version,
8043                                       p_api_version,
8044                                       l_api_name,
8045                                       g_pkg_name)
8046    THEN
8047      RAISE FND_API.g_exc_unexpected_error;
8048    END IF;
8049 
8050    -- Initialize message list if p_init_msg_list is set to TRUE.
8051    IF FND_API.to_Boolean( p_init_msg_list ) THEN
8052       FND_MSG_PUB.initialize;
8053    END IF;
8054 
8055    -- Initialize API return status to SUCCESS
8056    x_return_status := FND_API.g_ret_sts_success;
8057 
8058    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- start');
8059 
8060    l_fund_id          := p_fund_id;
8061    l_old_start_date   := p_old_start_date;
8062    l_new_end_date     := p_new_end_date;
8063    l_addon_fact_id    := p_addon_fact_id;
8064    l_addon_amount     := p_addon_amount;
8065 
8066    OPEN fund_csr(l_fund_id);
8067    FETCH fund_csr INTO l_fund_rec;
8068    CLOSE fund_csr ;
8069 
8070    l_resource_id := l_fund_rec.owner;
8071    l_territory_id := l_fund_rec.node_id;
8072 
8073    IF l_territory_id IS NULL THEN
8074       OPEN territory_csr(l_resource_id);
8075       FETCH territory_csr INTO l_territory_id;
8076       CLOSE territory_csr ;
8077    END IF;
8078 
8079    l_start_date := to_char(l_old_start_date, 'YYYY/MM/DD');
8080    l_end_date   := to_char(l_new_end_date, 'YYYY/MM/DD');
8081    l_period_type_id := 32; -- l_fund_rec.period_type_id;
8082 
8083    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name ||
8084                          ': Begin - Getting ADDON Time_ids Between '
8085                         ||l_start_date||' AND '||l_end_date||' ; ');
8086 
8087    l_new_period_tbl := OZF_TIME_API_PVT.get_period_tbl
8088                                     (l_start_date,
8089                                      l_end_date,
8090                                      l_period_type_id);
8091    FOR i IN l_new_period_tbl.FIRST..l_new_period_tbl.LAST
8092    LOOP
8093       l_new_ozf_period_tbl.EXTEND;
8094       l_new_ozf_period_tbl(i) :=  l_new_period_tbl(i);
8095    END LOOP;
8096 
8097    l_index := 1;
8098    FOR old_period_rec in get_old_period_csr(l_fund_id)
8099    LOOP
8100       l_old_ozf_period_tbl.EXTEND;
8101       l_old_ozf_period_tbl(l_index) := old_period_rec.time_id;
8102       l_index := l_index + 1;
8103    END LOOP;
8104 
8105    BEGIN
8106      SELECT * BULK COLLECT INTO l_period_tbl
8107      FROM
8108          ( SELECT * FROM TABLE(CAST(l_new_ozf_period_tbl as OZF_PERIOD_TBL_TYPE))
8109            MINUS
8110            SELECT * FROM TABLE(CAST(l_old_ozf_period_tbl as OZF_PERIOD_TBL_TYPE))
8111           );
8112      EXCEPTION
8113         WHEN OTHERS THEN
8114              ROLLBACK TO allocate_target_addon;
8115              x_return_status := FND_API.g_ret_sts_unexp_error ;
8116              IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
8117                 FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
8118              END IF;
8119              FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
8120                                         p_data  => x_error_message);
8121              OZF_UTILITY_PVT.debug_message(l_full_api_name||' : COLLECTION UNNESTING: OTHERS EXCEPTION = '||sqlerrm(sqlcode));
8122 
8123              IF l_period_tbl IS NULL OR l_period_tbl.COUNT <= 0 THEN
8124                 RAISE OZF_TP_BLANK_PERIOD_TBL;
8125              END IF;
8126    END;
8127 
8128 
8129    IF l_period_tbl IS NULL OR l_period_tbl.COUNT <= 0 THEN
8130       RAISE OZF_TP_BLANK_PERIOD_TBL;
8131    END IF;
8132 
8133    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name ||
8134                          ': End - Getting ADDON Time_ids Between '
8135                         ||l_start_date||' AND '||l_end_date||' ; ');
8136 
8137 
8138    IF l_period_tbl IS NOT NULL THEN
8139     IF l_period_tbl.COUNT > 0 THEN
8140       FOR l_idx IN l_period_tbl.first..l_period_tbl.last
8141       LOOP
8142         IF l_period_tbl.exists(l_idx) THEN
8143 
8144          l_lysp_period_tbl(l_idx) := OZF_TIME_API_PVT.get_lysp_id (l_period_tbl(l_idx), l_period_type_id);
8145 
8146          OZF_UTILITY_PVT.debug_message(SubStr('l_period_tbl('||TO_CHAR(l_idx)||') = '
8147                                ||TO_CHAR(l_period_tbl(l_idx)), 1,255));
8148         END IF;
8149       END LOOP;
8150     END IF;
8151    END IF;
8152 
8153    --l_in_clause := '(';
8154    l_in_clause := NULL;
8155    IF l_period_tbl IS NOT NULL THEN
8156       FOR l_idx IN l_period_tbl.first..l_period_tbl.last
8157       LOOP
8158           IF l_in_clause IS NULL THEN
8159              l_in_clause := LTRIM(' '||l_period_tbl(l_idx));
8160           ELSE
8161              l_in_clause := l_in_clause ||','|| l_period_tbl(l_idx);
8162           END IF;
8163       END LOOP;
8164    END IF;
8165    --l_in_clause := l_in_clause||')';
8166 
8167    l_lysp_in_clause := NULL;
8168    IF l_lysp_period_tbl IS NOT NULL THEN
8169       FOR l_idx IN l_lysp_period_tbl.first..l_lysp_period_tbl.last
8170       LOOP
8171           IF l_lysp_in_clause IS NULL THEN
8172              l_lysp_in_clause := LTRIM(' '||l_lysp_period_tbl(l_idx));
8173           ELSE
8174              l_lysp_in_clause := l_lysp_in_clause ||','|| l_lysp_period_tbl(l_idx);
8175           END IF;
8176       END LOOP;
8177    END IF;
8178    --l_lysp_in_clause := l_lysp_in_clause||')';
8179 
8180 
8181    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': Time_ids between '||l_start_date
8182                                          ||' AND '||l_end_date||' ARE : '||l_in_clause||' ; ');
8183    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ': LYSP-Time_ids between '||l_start_date
8184                                          ||' AND '||l_end_date||' ARE : '||l_lysp_in_clause||' ; ');
8185 
8186    OPEN total_lysp_sales_csr(l_fund_id,l_lysp_in_clause);
8187    FETCH total_lysp_sales_csr INTO l_total_lysp_sales;
8188    CLOSE total_lysp_sales_csr;
8189 
8190    l_total_root_quota := l_addon_amount;
8191 
8192    l_total_target_unalloc := 0;
8193    l_time_target_unalloc := 0;
8194    l_multiplying_factor := 0;
8195 
8196    IF l_total_lysp_sales > 0 THEN
8197       l_multiplying_factor := l_total_root_quota / l_total_lysp_sales;
8198    ELSE
8199       l_multiplying_factor := 0;
8200       l_denominator := l_period_tbl.COUNT;
8201 
8202       l_total_target_unalloc := ROUND(l_total_root_quota, 0);
8203       l_time_target_unalloc := ROUND ((l_total_root_quota / l_denominator), 0);
8204    END IF;
8205 
8206 ------ for product allocations: based upon Funds product spread ------------------
8207    l_get_total_target_sql := l_get_total_target_sql||l_in_clause ||')';
8208 
8209    OPEN get_total_target_csr FOR l_get_total_target_sql USING l_fund_id;
8210    FETCH get_total_target_csr INTO l_total_fund_quota;
8211    CLOSE get_total_target_csr ;
8212 
8213 
8214 ------- Insert rows for ACCOUNT-TIME Allocations and Update rows for Account Allocations ---------
8215 
8216    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name ||
8217                          ': Begin - Updating Account and Populating Time Allocations Records'
8218                          || 'FOR Fund_id = '|| l_fund_id || ' ; ');
8219    <<account_loop>>
8220    FOR account_rec IN account_csr(l_fund_id)
8221    LOOP
8222 
8223        l_account_allocation_id := account_rec.account_allocation_id;
8224 
8225        OPEN account_total_sales_csr (account_rec.site_use_id, l_lysp_in_clause);
8226        FETCH account_total_sales_csr into l_total_account_sales;
8227        CLOSE account_total_sales_csr;
8228 
8229        l_total_account_sales := NVL(l_total_account_sales, 0);
8230        l_total_account_target := ROUND( (l_total_account_sales * l_multiplying_factor), 0);
8231 
8232        UPDATE ozf_account_allocations a
8233        SET a.target = a.target + l_total_account_target,
8234            a.lysp_sales = a.lysp_sales + l_total_account_sales,
8235            a.object_version_number = a.object_version_number + 1,
8236            a.last_update_date = SYSDATE,
8237            a.last_updated_by = FND_GLOBAL.USER_ID,
8238            a.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
8239        WHERE a.account_allocation_id = l_account_allocation_id;
8240 
8241        <<account_time_loop>>
8242        FOR l_idx IN l_period_tbl.first..l_period_tbl.last
8243        LOOP
8244         IF l_period_tbl.exists(l_idx) THEN
8245 
8246            p_time_alloc_rec := NULL;
8247            l_account_sales := 0;
8248 
8249            OPEN account_sales_csr (account_rec.site_use_id, l_lysp_period_tbl(l_idx));
8250            FETCH account_sales_csr into l_account_sales;
8251            CLOSE account_sales_csr;
8252 
8253            l_account_target := ROUND( (l_account_sales * l_multiplying_factor), 0);
8254 
8255            p_time_alloc_rec.allocation_for := 'CUST';
8256            p_time_alloc_rec.allocation_for_id := l_account_allocation_id;
8257            p_time_alloc_rec.time_id := l_period_tbl(l_idx);
8258            p_time_alloc_rec.period_type_id := l_period_type_id;
8259            p_time_alloc_rec.target := NVL(l_account_target, 0);
8260            p_time_alloc_rec.lysp_sales := NVL(l_account_sales, 0);
8261 
8262            l_time_allocation_id := get_time_allocation_id;
8263 
8264            Ozf_Time_Allocations_Pkg.Insert_Row(
8265               px_time_allocation_id  => l_time_allocation_id,
8266               p_allocation_for  => p_time_alloc_rec.allocation_for,
8267               p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
8268               p_time_id  => p_time_alloc_rec.time_id,
8269               p_period_type_id => p_time_alloc_rec.period_type_id,
8270               p_target  => p_time_alloc_rec.target,
8271               p_lysp_sales  => p_time_alloc_rec.lysp_sales,
8272               px_object_version_number  => l_object_version_number,
8273               p_creation_date  => SYSDATE,
8274               p_created_by  => FND_GLOBAL.USER_ID,
8275               p_last_update_date  => SYSDATE,
8276               p_last_updated_by  => FND_GLOBAL.USER_ID,
8277               p_last_update_login  => FND_GLOBAL.conc_login_id,
8278               p_attribute_category  => p_time_alloc_rec.attribute_category,
8279               p_attribute1  => p_time_alloc_rec.attribute1,
8280               p_attribute2  => p_time_alloc_rec.attribute2,
8281               p_attribute3  => p_time_alloc_rec.attribute3,
8282               p_attribute4  => p_time_alloc_rec.attribute4,
8283               p_attribute5  => p_time_alloc_rec.attribute5,
8284               p_attribute6  => p_time_alloc_rec.attribute6,
8285               p_attribute7  => p_time_alloc_rec.attribute7,
8286               p_attribute8  => p_time_alloc_rec.attribute8,
8287               p_attribute9  => p_time_alloc_rec.attribute9,
8288               p_attribute10  => p_time_alloc_rec.attribute10,
8289               p_attribute11  => p_time_alloc_rec.attribute11,
8290               p_attribute12  => p_time_alloc_rec.attribute12,
8291               p_attribute13  => p_time_alloc_rec.attribute13,
8292               p_attribute14  => p_time_alloc_rec.attribute14,
8293               p_attribute15  => p_time_alloc_rec.attribute15,
8294               px_org_id  => l_org_id
8295             );
8296 
8297 
8298          END IF;
8299        END LOOP account_time_loop;
8300 
8301 
8302 ------- Insert rows for Product-Time Allocations and Update rows for Account Allocations ---------
8303 
8304 
8305        OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name ||
8306                              ': Begin - Updating Product and Populating Time Allocations Records'
8307                              || 'FOR site_use_id = '|| account_rec.site_use_id || ' ; ');
8308 
8309 
8310        IF l_total_fund_quota > 0 THEN
8311           l_prod_mltply_factor := l_total_account_target / l_total_fund_quota;
8312        ELSE
8313           l_prod_mltply_factor := 0;
8314        END IF;
8315 
8316        <<account_product_loop>>
8317        FOR account_product_rec IN account_product_spread_csr(l_account_allocation_id)
8318        LOOP
8319 
8320            l_product_allocation_id := account_product_rec.product_allocation_id;
8321 
8322            OPEN fund_product_spread_csr(l_fund_id,
8323                                         l_addon_fact_id,
8324                                         account_product_rec.item_id,
8325                                         account_product_rec.item_type);
8326            FETCH fund_product_spread_csr INTO l_fund_product_rec;
8327            IF fund_product_spread_csr%FOUND THEN
8328               l_fund_product_rec_exists := TRUE;
8329            END IF;
8330            CLOSE fund_product_spread_csr;
8331 
8332            l_total_product_target := 0;
8333            l_total_product_sales := 0;
8334 
8335            <<account_product_time_loop>>
8336            FOR l_idx IN l_period_tbl.first..l_period_tbl.last
8337            LOOP
8338             IF l_period_tbl.exists(l_idx) THEN
8339 
8340                p_time_alloc_rec := NULL;
8341 
8342                l_time_fund_target := 0;
8343                IF l_fund_product_rec_exists = TRUE THEN
8344                   OPEN fund_time_spread_csr(l_fund_product_rec.product_allocation_id, l_period_tbl(l_idx));
8345                   FETCH fund_time_spread_csr INTO l_time_fund_target;
8346                   CLOSE fund_time_spread_csr ;
8347                END IF;
8348 
8349                l_product_target := ROUND( (l_time_fund_target * l_prod_mltply_factor), 0);
8350                l_total_product_target := l_total_product_target + l_product_target;
8351 
8352                l_product_sales := 0;
8353 
8354                IF account_product_rec.item_type = 'PRODUCT' THEN
8355                   OPEN product_lysp_sales_csr(account_product_rec.item_id,
8356                                               l_territory_id,
8357                                               account_rec.site_use_id,
8358                                               l_lysp_period_tbl(l_idx)
8359                                              );
8360                   FETCH product_lysp_sales_csr INTO l_product_sales;
8361                   CLOSE product_lysp_sales_csr;
8362                ELSIF account_product_rec.item_type = 'PRICING_ATTRIBUTE2' THEN
8363                    OPEN category_lysp_sales_csr(account_product_rec.item_id,
8364                                                 l_territory_id,
8365                                                 account_rec.site_use_id,
8366                                                 l_lysp_period_tbl(l_idx),
8367                                                 l_fund_id
8368                                                );
8369                    FETCH category_lysp_sales_csr INTO l_product_sales;
8370                    CLOSE category_lysp_sales_csr;
8371                ELSIF account_product_rec.item_type = 'OTHERS' THEN
8372                    OPEN others_lysp_sales_csr(l_territory_id,
8373                                               account_rec.site_use_id,
8374                                               l_lysp_period_tbl(l_idx),
8375                                               l_fund_id
8376                                              );
8377                    FETCH others_lysp_sales_csr INTO l_product_sales;
8378                    CLOSE others_lysp_sales_csr;
8379                END IF;
8380 
8381                l_total_product_sales := l_total_product_sales + l_product_sales;
8382 
8383                p_time_alloc_rec.allocation_for := 'PROD';
8384                p_time_alloc_rec.allocation_for_id := l_product_allocation_id;
8385                p_time_alloc_rec.time_id := l_period_tbl(l_idx);
8386                p_time_alloc_rec.period_type_id := l_period_type_id;
8387                p_time_alloc_rec.target := NVL(l_product_target, 0);
8388                p_time_alloc_rec.lysp_sales := NVL(l_product_sales, 0);
8389 
8390                l_time_allocation_id := get_time_allocation_id;
8391 
8392                Ozf_Time_Allocations_Pkg.Insert_Row(
8393                   px_time_allocation_id  => l_time_allocation_id,
8394                   p_allocation_for  => p_time_alloc_rec.allocation_for,
8395                   p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
8396                   p_time_id  => p_time_alloc_rec.time_id,
8397                   p_period_type_id => p_time_alloc_rec.period_type_id,
8398                   p_target  => p_time_alloc_rec.target,
8399                   p_lysp_sales  => p_time_alloc_rec.lysp_sales,
8400                   px_object_version_number  => l_object_version_number,
8401                   p_creation_date  => SYSDATE,
8402                   p_created_by  => FND_GLOBAL.USER_ID,
8403                   p_last_update_date  => SYSDATE,
8404                   p_last_updated_by  => FND_GLOBAL.USER_ID,
8405                   p_last_update_login  => FND_GLOBAL.conc_login_id,
8406                   p_attribute_category  => p_time_alloc_rec.attribute_category,
8407                   p_attribute1  => p_time_alloc_rec.attribute1,
8408                   p_attribute2  => p_time_alloc_rec.attribute2,
8409                   p_attribute3  => p_time_alloc_rec.attribute3,
8410                   p_attribute4  => p_time_alloc_rec.attribute4,
8411                   p_attribute5  => p_time_alloc_rec.attribute5,
8412                   p_attribute6  => p_time_alloc_rec.attribute6,
8413                   p_attribute7  => p_time_alloc_rec.attribute7,
8414                   p_attribute8  => p_time_alloc_rec.attribute8,
8415                   p_attribute9  => p_time_alloc_rec.attribute9,
8416                   p_attribute10  => p_time_alloc_rec.attribute10,
8417                   p_attribute11  => p_time_alloc_rec.attribute11,
8418                   p_attribute12  => p_time_alloc_rec.attribute12,
8419                   p_attribute13  => p_time_alloc_rec.attribute13,
8420                   p_attribute14  => p_time_alloc_rec.attribute14,
8421                   p_attribute15  => p_time_alloc_rec.attribute15,
8422                   px_org_id  => l_org_id
8423                 );
8424 
8425 
8426              END IF;
8427            END LOOP account_product_time_loop;
8428 
8429            UPDATE OZF_PRODUCT_ALLOCATIONS p
8430            SET p.lysp_sales = p.lysp_sales + l_total_product_sales,
8431                p.target = p.target + l_total_product_target,
8432                p.object_version_number = p.object_version_number + 1,
8433                p.last_update_date = SYSDATE,
8434                p.last_updated_by = FND_GLOBAL.USER_ID,
8435                p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
8436            WHERE p.product_allocation_id = l_product_allocation_id;
8437 
8438        END LOOP account_product_loop;
8439 
8440 
8441 
8442        OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name ||
8443                              ': End - Updating Product and Populating Time Allocations Records'
8444                              || 'FOR site_use_id = '|| account_rec.site_use_id || ' ; ');
8445 
8446 
8447    END LOOP account_loop;
8448 
8449 
8450 ------- Insert rows for Unalloc-Time Allocations and Update rows for Unalloc Allocations ---------
8451 
8452 
8453    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name ||
8454                          ': Begin - Updating UNALLOCATED Account and Populating Time Allocations Records'
8455                          || 'FOR FUND_Id = '|| l_fund_id || ' ; ');
8456 
8457    UPDATE ozf_account_allocations a
8458    SET a.target = a.target + l_total_target_unalloc,
8459        a.object_version_number = a.object_version_number + 1,
8460        a.last_update_date = SYSDATE,
8461        a.last_updated_by = FND_GLOBAL.USER_ID,
8462        a.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
8463    WHERE a.allocation_for = 'FUND'
8464     AND a.allocation_for_id = l_fund_id
8465     AND a.site_use_id = -9999
8466    RETURNING account_allocation_id INTO l_account_allocation_id;
8467 
8468    <<unalloc_time_loop>>
8469    FOR l_idx IN l_period_tbl.first..l_period_tbl.last
8470    LOOP
8471     IF l_period_tbl.exists(l_idx) THEN
8472 
8473        p_time_alloc_rec := NULL;
8474 
8475        p_time_alloc_rec.allocation_for := 'CUST';
8476        p_time_alloc_rec.allocation_for_id := l_account_allocation_id;
8477        p_time_alloc_rec.time_id := l_period_tbl(l_idx);
8478        p_time_alloc_rec.period_type_id := l_period_type_id;
8479        p_time_alloc_rec.target := NVL(l_time_target_unalloc, 0);
8480        p_time_alloc_rec.lysp_sales := 0;
8481 
8482        l_time_allocation_id := get_time_allocation_id;
8483 
8484        Ozf_Time_Allocations_Pkg.Insert_Row(
8485           px_time_allocation_id  => l_time_allocation_id,
8486           p_allocation_for  => p_time_alloc_rec.allocation_for,
8487           p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
8488           p_time_id  => p_time_alloc_rec.time_id,
8489           p_period_type_id => p_time_alloc_rec.period_type_id,
8490           p_target  => p_time_alloc_rec.target,
8491           p_lysp_sales  => p_time_alloc_rec.lysp_sales,
8492           px_object_version_number  => l_object_version_number,
8493           p_creation_date  => SYSDATE,
8494           p_created_by  => FND_GLOBAL.USER_ID,
8495           p_last_update_date  => SYSDATE,
8496           p_last_updated_by  => FND_GLOBAL.USER_ID,
8497           p_last_update_login  => FND_GLOBAL.conc_login_id,
8498           p_attribute_category  => p_time_alloc_rec.attribute_category,
8499           p_attribute1  => p_time_alloc_rec.attribute1,
8500           p_attribute2  => p_time_alloc_rec.attribute2,
8501           p_attribute3  => p_time_alloc_rec.attribute3,
8502           p_attribute4  => p_time_alloc_rec.attribute4,
8503           p_attribute5  => p_time_alloc_rec.attribute5,
8504           p_attribute6  => p_time_alloc_rec.attribute6,
8505           p_attribute7  => p_time_alloc_rec.attribute7,
8506           p_attribute8  => p_time_alloc_rec.attribute8,
8507           p_attribute9  => p_time_alloc_rec.attribute9,
8508           p_attribute10  => p_time_alloc_rec.attribute10,
8509           p_attribute11  => p_time_alloc_rec.attribute11,
8510           p_attribute12  => p_time_alloc_rec.attribute12,
8511           p_attribute13  => p_time_alloc_rec.attribute13,
8512           p_attribute14  => p_time_alloc_rec.attribute14,
8513           p_attribute15  => p_time_alloc_rec.attribute15,
8514           px_org_id  => l_org_id
8515         );
8516 
8517 
8518      END IF;
8519    END LOOP unalloc_time_loop;
8520 
8521 
8522    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name ||
8523                          ': End - Updating UNALLOCATED Account and Populating Time Allocations Records'
8524                          || 'FOR FUND_Id = '|| l_fund_id || ' ; ');
8525 
8526 
8527 
8528    OZF_UTILITY_PVT.debug_message('Private API: ' || l_full_api_name || ' -- end');
8529 
8530  EXCEPTION
8531 
8532      WHEN OZF_TP_INVALID_PARAM THEN
8533           ROLLBACK TO allocate_target_addon;
8534           FND_MESSAGE.set_name('OZF', 'OZF_TP_INVALID_PARAM_TXT');
8535           FND_MSG_PUB.add;
8536           x_return_status := FND_API.g_ret_sts_error ;
8537           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
8538                                      p_data    => x_error_message);
8539           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : INVALID PARAMETER EXCEPTION = '||sqlerrm(sqlcode));
8540           OZF_UTILITY_PVT.debug_message(fnd_message.get_string('OZF', 'OZF_TP_INVALID_PARAM_TXT'));
8541 
8542      WHEN FND_API.G_EXC_ERROR THEN
8543           ROLLBACK TO allocate_target_addon;
8544           x_return_status := FND_API.g_ret_sts_error ;
8545           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
8546                                      p_data    => x_error_message);
8547 
8548      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8549           ROLLBACK TO allocate_target_addon;
8550           x_return_status := FND_API.g_ret_sts_unexp_error ;
8551           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
8552                                      p_data  => x_error_message);
8553 
8554      WHEN OTHERS THEN
8555           ROLLBACK TO allocate_target_addon;
8556 
8557           FND_MESSAGE.set_name('OZF', 'OZF_TP_OTHERS_ERROR_TXT');
8558           FND_MESSAGE.set_token('OZF_TP_SQLERRM_TOKEN',SQLERRM);
8559           FND_MESSAGE.set_token('OZF_TP_SQLCODE_TOKEN',SQLCODE);
8560           FND_MSG_PUB.add;
8561 
8562           x_return_status := FND_API.g_ret_sts_unexp_error ;
8563           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
8564              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
8565           END IF;
8566           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
8567                                      p_data  => x_error_message);
8568           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
8569 
8570  END allocate_target_addon;
8571 
8572 
8573 
8574 
8575 
8576 
8577 
8578 
8579 -- ------------------------
8580 -- Public Function
8581 -- ------------------------
8582 -- ------------------------------------------------------------------
8583 -- Name: Called from Quota Create APIs
8584 -- Desc: This is for checking if product allocation for a particular
8585 --       fund is already done.
8586 --
8587 -- -----------------------------------------------------------------
8588 
8589  FUNCTION GET_PROD_ALLOC_COUNT
8590     (p_fund_id  IN number)
8591   RETURN NUMBER IS
8592 
8593  l_count number;
8594  CURSOR prod_alloc_count_csr (l_fund_id NUMBER)
8595    IS
8596    select count(product_allocation_id)
8597    from ozf_product_allocations
8598    where fund_id = l_fund_id;
8599 
8600  BEGIN
8601 
8602    OPEN prod_alloc_count_csr (p_fund_id);
8603    FETCH prod_alloc_count_csr into l_count;
8604    CLOSE prod_alloc_count_csr;
8605 
8606    return NVL(l_count, 0);
8607 
8608  EXCEPTION
8609    WHEN OTHERS THEN
8610      return 0;
8611  END;
8612 
8613 
8614 
8615 
8616 ----------------------------------------------------------------------------------------------------------
8617 ----------------------------------------------------------------------------------------------------------
8618 
8619 -- ------------------------
8620 -- Public Procedure
8621 -- ------------------------
8622 -- ------------------------------------------------------------------
8623 -- Name: SETUP PRODUCT SPREAD
8624 -- Desc: 1. Setup product spread for Root Node, Normal Node and Facts
8625 --          in the Worksheet.
8626 --       2. Update or Delete the product spread
8627 --       3. Add-on Quota on subsequent call
8628 -- -----------------------------------------------------------------
8629  PROCEDURE setup_product_spread
8630  (
8631     p_api_version        IN          NUMBER,
8632     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
8633     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
8634     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
8635     x_return_status      OUT NOCOPY  VARCHAR2,
8636     x_error_number       OUT NOCOPY  NUMBER,
8637     x_error_message      OUT NOCOPY  VARCHAR2,
8638     p_mode               IN          VARCHAR2,
8639     p_obj_id             IN          NUMBER,
8640     p_context            IN          VARCHAR2
8641  ) IS
8642 
8643    l_api_version   CONSTANT NUMBER       := 1.0;
8644    l_api_name      CONSTANT VARCHAR2(30) := 'setup_product_spread';
8645    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
8646    l_return_status          VARCHAR2(1);
8647    l_mode                   VARCHAR2(30);
8648    l_obj_id                 NUMBER;
8649    l_context                VARCHAR2(30);
8650 
8651  BEGIN
8652 
8653    -- Standard Start of API savepoint
8654    SAVEPOINT setup_product_spread;
8655 
8656 
8657    -- Standard call to check for call compatibility.
8658    IF NOT FND_API.compatible_api_call(l_api_version,
8659                                       p_api_version,
8660                                       l_api_name,
8661                                       g_pkg_name)
8662    THEN
8663      RAISE FND_API.g_exc_unexpected_error;
8664    END IF;
8665 
8666    -- Initialize message list if p_init_msg_list is set to TRUE.
8667    IF FND_API.to_Boolean( p_init_msg_list ) THEN
8668       FND_MSG_PUB.initialize;
8669    END IF;
8670 
8671    -- Initialize API return status to SUCCESS
8672    x_return_status := FND_API.g_ret_sts_success;
8673 
8674    OZF_UTILITY_PVT.debug_message('Public API: ' || l_full_api_name || ' -- start');
8675 
8676    l_mode := p_mode;
8677    l_obj_id := p_obj_id;
8678    l_context:= p_context;
8679 
8680    OZF_UTILITY_PVT.debug_message('API Parameters For:---->: ' || l_full_api_name);
8681    OZF_UTILITY_PVT.debug_message('1. l_mode ------------->: ' || l_mode);
8682    OZF_UTILITY_PVT.debug_message('2. l_Object_id--------->: ' || l_obj_id);
8683    OZF_UTILITY_PVT.debug_message('3. l_context----------->: ' || l_context);
8684 
8685 
8686    IF     l_mode IN ('CREATE', 'DELETE', 'PUBLISH', 'ADD')
8687       AND l_obj_id > 0
8688       AND l_context IN ('ROOT', 'FACT')
8689    THEN
8690      NULL;
8691    ELSE
8692      RAISE OZF_TP_INVALID_PARAM;
8693    END IF;
8694 
8695 
8696    IF l_mode = 'CREATE' AND l_context = 'ROOT' THEN
8697 
8698       IF (GET_PROD_ALLOC_COUNT(l_obj_id) <= 0) THEN
8699          create_root_product_spread
8700                              (p_api_version        => p_api_version,
8701                               x_return_status      => x_return_status,
8702                               x_error_number       => x_error_number,
8703                               x_error_message      => x_error_message,
8704                               p_fund_id            => l_obj_id
8705                              );
8706       END IF;
8707 
8708    ELSIF l_mode = 'CREATE' AND l_context = 'FACT' THEN
8709 
8710      create_fact_product_spread
8711                          (p_api_version        => p_api_version,
8712                           x_return_status      => x_return_status,
8713                           x_error_number       => x_error_number,
8714                           x_error_message      => x_error_message,
8715                           p_fact_id            => l_obj_id
8716                          );
8717 
8718    ELSIF l_mode = 'DELETE' AND l_context = 'ROOT' THEN
8719 
8720      delete_cascade_product_spread
8721                          (p_api_version        => p_api_version,
8722                           x_return_status      => x_return_status,
8723                           x_error_number       => x_error_number,
8724                           x_error_message      => x_error_message,
8725                           p_fund_id            => l_obj_id
8726                          );
8727 
8728    ELSIF l_mode = 'DELETE' AND l_context = 'FACT' THEN
8729 
8730      delete_fact_product_spread
8731                          (p_api_version        => p_api_version,
8732                           x_return_status      => x_return_status,
8733                           x_error_number       => x_error_number,
8734                           x_error_message      => x_error_message,
8735                           p_fact_id            => l_obj_id
8736                          );
8737 
8738    ELSIF l_mode = 'PUBLISH' AND l_context = 'FACT' THEN
8739 
8740      publish_fact_product_spread
8741                          (p_api_version        => p_api_version,
8742                           x_return_status      => x_return_status,
8743                           x_error_number       => x_error_number,
8744                           x_error_message      => x_error_message,
8745                           p_fact_id            => l_obj_id
8746                          );
8747    END IF;
8748 
8749 
8750    -- If any errors happen abort API.
8751    IF    x_return_status = FND_API.g_ret_sts_error THEN
8752          RAISE FND_API.g_exc_error;
8753    ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
8754          RAISE FND_API.g_exc_unexpected_error;
8755    END IF;
8756 
8757    OZF_UTILITY_PVT.debug_message('Public API: ' || l_full_api_name || ' -- end');
8758 
8759  EXCEPTION
8760 
8761      WHEN OZF_TP_INVALID_PARAM THEN
8762           ROLLBACK TO setup_product_spread;
8763           FND_MESSAGE.set_name('OZF', 'OZF_TP_INVALID_PARAM_TXT');
8764           FND_MSG_PUB.add;
8765           x_return_status := FND_API.g_ret_sts_error ;
8766           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
8767                                      p_data    => x_error_message);
8768           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : INVALID PARAMETER EXCEPTION = '||sqlerrm(sqlcode));
8769           OZF_UTILITY_PVT.debug_message(fnd_message.get_string('OZF', 'OZF_TP_INVALID_PARAM_TXT'));
8770 
8771      WHEN FND_API.G_EXC_ERROR THEN
8772           ROLLBACK TO setup_product_spread;
8773           x_return_status := FND_API.g_ret_sts_error ;
8774           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
8775                                      p_data    => x_error_message);
8776 
8777      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8778           ROLLBACK TO setup_product_spread;
8779           x_return_status := FND_API.g_ret_sts_unexp_error ;
8780           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
8781                                      p_data  => x_error_message);
8782 
8783      WHEN OTHERS THEN
8784           ROLLBACK TO setup_product_spread;
8785 
8786           FND_MESSAGE.set_name('OZF', 'OZF_TP_OTHERS_ERROR_TXT');
8787           FND_MESSAGE.set_token('OZF_TP_SQLERRM_TOKEN',SQLERRM);
8788           FND_MESSAGE.set_token('OZF_TP_SQLCODE_TOKEN',SQLCODE);
8789           FND_MSG_PUB.add;
8790 
8791           x_return_status := FND_API.g_ret_sts_unexp_error ;
8792           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
8793              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
8794           END IF;
8795           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
8796                                      p_data  => x_error_message);
8797           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
8798 
8799  END setup_product_spread;
8800 
8801 
8802 
8803 
8804 
8805 ----------------------------------------------------------------------------------------------------------
8806 
8807 -- ------------------------
8808 -- Public Procedure
8809 -- ------------------------
8810 -- ------------------------------------------------------------------
8811 -- Name: CASCADE PRODUCT SPREAD
8812 -- Desc: 1. Cascade product spread for Creator Node to all other Nodes
8813 --          who are part of same hierarchy.
8814 --       2. This will be called only when a SINGLE product is added or deleted
8815 --          in the root product spread.
8816 -- -----------------------------------------------------------------
8817 PROCEDURE cascade_product_spread
8818  (
8819     p_api_version        IN          NUMBER,
8820     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
8821     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
8822     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
8823     x_return_status      OUT NOCOPY  VARCHAR2,
8824     x_error_number       OUT NOCOPY  NUMBER,
8825     x_error_message      OUT NOCOPY  VARCHAR2,
8826     p_mode               IN          VARCHAR2,
8827     p_fund_id            IN          NUMBER,
8828     p_item_id            IN          NUMBER,
8829     p_item_type          IN          VARCHAR2
8830 ) IS
8831 
8832    l_api_version   CONSTANT NUMBER       := 1.0;
8833    l_api_name      CONSTANT VARCHAR2(30) := 'cascade_product_spread';
8834    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
8835    l_return_status          VARCHAR2(1);
8836    l_mode                   VARCHAR2(30);
8837    l_fund_id                NUMBER;
8838    l_item_id                NUMBER;
8839    l_item_type              VARCHAR2(30);
8840 
8841  BEGIN
8842 
8843    -- Standard Start of API savepoint
8844    SAVEPOINT cascade_product_spread;
8845 
8846 
8847    -- Standard call to check for call compatibility.
8848    IF NOT FND_API.compatible_api_call(l_api_version,
8849                                       p_api_version,
8850                                       l_api_name,
8851                                       g_pkg_name)
8852    THEN
8853      RAISE FND_API.g_exc_unexpected_error;
8854    END IF;
8855 
8856    -- Initialize message list if p_init_msg_list is set to TRUE.
8857    IF FND_API.to_Boolean( p_init_msg_list ) THEN
8858       FND_MSG_PUB.initialize;
8859    END IF;
8860 
8861    -- Initialize API return status to SUCCESS
8862    x_return_status := FND_API.g_ret_sts_success;
8863 
8864    OZF_UTILITY_PVT.debug_message('Public API: ' || l_full_api_name || ' -- start');
8865 
8866    l_mode := p_mode;
8867    l_fund_id := p_fund_id;
8868    l_item_id := p_item_id;
8869    l_item_type := p_item_type;
8870 
8871    OZF_UTILITY_PVT.debug_message('API Parameters For:---->: ' || l_full_api_name);
8872    OZF_UTILITY_PVT.debug_message('1. l_mode ------------->: ' || l_mode);
8873    OZF_UTILITY_PVT.debug_message('2. l_fund_id ---------->: ' || l_fund_id);
8874    OZF_UTILITY_PVT.debug_message('3. l_item_id ---------->: ' || l_item_id);
8875    OZF_UTILITY_PVT.debug_message('4. l_item_type--------->: ' || l_item_type);
8876 
8877    IF     l_mode IN ('ADD', 'DELETE')
8878       AND l_fund_id > 0
8879       AND l_item_id > 0
8880       AND l_item_type IN ('PRICING_ATTRIBUTE1', 'PRICING_ATTRIBUTE2')
8881    THEN
8882      NULL;
8883    ELSE
8884      RAISE OZF_TP_INVALID_PARAM;
8885    END IF;
8886 
8887 
8888    IF l_mode = 'ADD' THEN
8889 
8890      add_cascade_product_spread
8891                          (p_api_version        => p_api_version,
8892                           x_return_status      => x_return_status,
8893                           x_error_number       => x_error_number,
8894                           x_error_message      => x_error_message,
8895                           p_fund_id            => l_fund_id,
8896                           p_item_id            => l_item_id,
8897                           p_item_type          => l_item_type
8898                          );
8899 
8900    ELSIF l_mode = 'DELETE' THEN  -- this will remove ONE product only and adjust the OTHERS quota for all FACTS
8901 
8902      delete_single_product
8903                          (p_api_version        => p_api_version,
8904                           x_return_status      => x_return_status,
8905                           x_error_number       => x_error_number,
8906                           x_error_message      => x_error_message,
8907                           p_fund_id            => l_fund_id,
8908                           p_item_id            => l_item_id,
8909                           p_item_type          => l_item_type
8910                          );
8911 
8912    END IF;
8913 
8914 
8915    -- If any errors happen abort API.
8916    IF    x_return_status = FND_API.g_ret_sts_error THEN
8917          RAISE FND_API.g_exc_error;
8918    ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
8919          RAISE FND_API.g_exc_unexpected_error;
8920    END IF;
8921 
8922    OZF_UTILITY_PVT.debug_message('Public API: ' || l_full_api_name || ' -- end');
8923 
8924  EXCEPTION
8925 
8926      WHEN OZF_TP_INVALID_PARAM THEN
8927           ROLLBACK TO cascade_product_spread;
8928           FND_MESSAGE.set_name('OZF', 'OZF_TP_INVALID_PARAM_TXT');
8929           FND_MSG_PUB.add;
8930           x_return_status := FND_API.g_ret_sts_error ;
8931           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
8932                                      p_data    => x_error_message);
8933           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : INVALID PARAMETER EXCEPTION = '||sqlerrm(sqlcode));
8934           OZF_UTILITY_PVT.debug_message(fnd_message.get_string('OZF', 'OZF_TP_INVALID_PARAM_TXT'));
8935 
8936      WHEN FND_API.G_EXC_ERROR THEN
8937           ROLLBACK TO cascade_product_spread;
8938           x_return_status := FND_API.g_ret_sts_error ;
8939           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
8940                                      p_data    => x_error_message);
8941 
8942      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8943           ROLLBACK TO cascade_product_spread;
8944           x_return_status := FND_API.g_ret_sts_unexp_error ;
8945           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
8946                                      p_data  => x_error_message);
8947 
8948      WHEN OTHERS THEN
8949           ROLLBACK TO cascade_product_spread;
8950 
8951           FND_MESSAGE.set_name('OZF', 'OZF_TP_OTHERS_ERROR_TXT');
8952           FND_MESSAGE.set_token('OZF_TP_SQLERRM_TOKEN',SQLERRM);
8953           FND_MESSAGE.set_token('OZF_TP_SQLCODE_TOKEN',SQLCODE);
8954           FND_MSG_PUB.add;
8955 
8956           x_return_status := FND_API.g_ret_sts_unexp_error ;
8957           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
8958              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
8959           END IF;
8960           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
8961                                      p_data  => x_error_message);
8962           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
8963 
8964  END cascade_product_spread;
8965 
8966 
8967 
8968 ----- Target Allocation APIs -------------------------------------------------------------------------------
8969 -- ------------------------
8970 -- Public Procedure
8971 -- ------------------------
8972 -- ------------------------------------------------------------------
8973 -- Name: ALLOCATE TARGET
8974 -- Desc: 1. Allocate Target across Accounts and Products for Sales Rep
8975 --       2. Add-on Target on subsequent call
8976 -- -----------------------------------------------------------------
8977 PROCEDURE allocate_target
8978  (
8979     p_api_version        IN          NUMBER,
8980     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
8981     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
8982     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
8983     x_return_status      OUT NOCOPY  VARCHAR2,
8984     x_error_number       OUT NOCOPY  NUMBER,
8985     x_error_message      OUT NOCOPY  VARCHAR2,
8986     p_mode               IN          VARCHAR2,
8987     p_fund_id            IN          NUMBER,
8988     p_old_start_date     IN          DATE,
8989     p_new_end_date       IN          DATE,
8990     p_addon_fact_id      IN          NUMBER,
8991     p_addon_amount       IN          NUMBER
8992 ) IS
8993    l_api_version   CONSTANT NUMBER       := 1.0;
8994    l_api_name      CONSTANT VARCHAR2(30) := 'allocate_target';
8995    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
8996    l_return_status          VARCHAR2(1);
8997    l_mode                   VARCHAR2(30);
8998    l_fund_id                NUMBER;
8999    l_old_start_date         DATE;
9000    l_new_end_date           DATE;
9001    l_addon_fact_id          NUMBER;
9002    l_addon_amount           NUMBER;
9003 
9004 
9005  CURSOR acct_alloc_bes_csr (l_fund_id NUMBER)
9006    IS
9007    select account_allocation_id
9008    from ozf_account_allocations
9009    where allocation_for = 'FUND'
9010      AND allocation_for_id = l_fund_id
9011      AND parent_party_id > 0;
9012 
9013  BEGIN
9014 
9015    -- Standard Start of API savepoint
9016    SAVEPOINT allocate_target;
9017 
9018 
9019    -- Standard call to check for call compatibility.
9020    IF NOT FND_API.compatible_api_call(l_api_version,
9021                                       p_api_version,
9022                                       l_api_name,
9023                                       g_pkg_name)
9024    THEN
9025      RAISE FND_API.g_exc_unexpected_error;
9026    END IF;
9027 
9028    -- Initialize message list if p_init_msg_list is set to TRUE.
9029    IF FND_API.to_Boolean( p_init_msg_list ) THEN
9030       FND_MSG_PUB.initialize;
9031    END IF;
9032 
9033    -- Initialize API return status to SUCCESS
9034    x_return_status := FND_API.g_ret_sts_success;
9035 
9036    OZF_UTILITY_PVT.debug_message('Public API: ' || l_full_api_name || ' -- start');
9037 
9038    l_mode := p_mode;
9039    l_fund_id := p_fund_id;
9040    l_old_start_date := p_old_start_date;
9041    l_new_end_date := p_new_end_date;
9042    l_addon_fact_id:= p_addon_fact_id;
9043    l_addon_amount:= p_addon_amount;
9044 
9045    OZF_UTILITY_PVT.debug_message('API Parameters For:---->: ' || l_full_api_name);
9046    OZF_UTILITY_PVT.debug_message('1. l_mode ------------->: ' || l_mode);
9047    OZF_UTILITY_PVT.debug_message('2. l_fund_id----------->: ' || l_fund_id);
9048    OZF_UTILITY_PVT.debug_message('3. l_old_start_date---->: ' || to_char(l_old_start_date, 'YYYY/MM/DD'));
9049    OZF_UTILITY_PVT.debug_message('4. l_new_end_date------>: ' || to_char(l_new_end_date, 'YYYY/MM/DD'));
9050    OZF_UTILITY_PVT.debug_message('5. l_addon_fact_id----->: ' || l_addon_fact_id);
9051    OZF_UTILITY_PVT.debug_message('6. l_addon_amount------>: ' || l_addon_amount);
9052 
9053    IF l_mode IN ('FIRSTTIME', 'ADDON', 'DELETE') AND l_fund_id > 0 THEN
9054       NULL;
9055    ELSE
9056       RAISE OZF_TP_INVALID_PARAM;
9057    END IF;
9058 
9059    IF l_mode = 'ADDON' THEN
9060       IF l_addon_fact_id > 0 THEN
9061          NULL;
9062       ELSE
9063          RAISE OZF_TP_INVALID_PARAM;
9064       END IF;
9065    END IF;
9066 
9067 
9068    IF l_mode = 'FIRSTTIME' THEN
9069 
9070      allocate_target_first_time
9071                          (p_api_version        => p_api_version,
9072                           x_return_status      => x_return_status,
9073                           x_error_number       => x_error_number,
9074                           x_error_message      => x_error_message,
9075                           p_fund_id            => l_fund_id
9076                          );
9077 
9078 
9079      FOR acct_alloc_bes_rec IN acct_alloc_bes_csr(l_fund_id)
9080      LOOP
9081         raise_business_event(acct_alloc_bes_rec.account_allocation_id);
9082      END LOOP;
9083 
9084 
9085    ELSIF l_mode = 'ADDON' THEN
9086 
9087      allocate_target_addon
9088                          (p_api_version        => p_api_version,
9089                           x_return_status      => x_return_status,
9090                           x_error_number       => x_error_number,
9091                           x_error_message      => x_error_message,
9092                           p_fund_id            => l_fund_id,
9093                           p_old_start_date     => l_old_start_date,
9094                           p_new_end_date       => l_new_end_date,
9095                           p_addon_fact_id      => l_addon_fact_id,
9096                           p_addon_amount       => l_addon_amount
9097                          );
9098 
9099    ELSIF l_mode = 'DELETE' THEN
9100 
9101      delete_target_allocation
9102                          (p_api_version        => p_api_version,
9103                           x_return_status      => x_return_status,
9104                           x_error_number       => x_error_number,
9105                           x_error_message      => x_error_message,
9106                           p_fund_id            => l_fund_id
9107                          );
9108 
9109    END IF;
9110 
9111 
9112    -- If any errors happen abort API.
9113    IF    x_return_status = FND_API.g_ret_sts_error THEN
9114          RAISE FND_API.g_exc_error;
9115    ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
9116          RAISE FND_API.g_exc_unexpected_error;
9117    END IF;
9118 
9119    OZF_UTILITY_PVT.debug_message('Public API: ' || l_full_api_name || ' -- end');
9120 
9121  EXCEPTION
9122 
9123      WHEN OZF_TP_INVALID_PARAM THEN
9124           ROLLBACK TO allocate_target;
9125           FND_MESSAGE.set_name('OZF', 'OZF_TP_INVALID_PARAM_TXT');
9126           FND_MSG_PUB.add;
9127           x_return_status := FND_API.g_ret_sts_error ;
9128           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
9129                                      p_data    => x_error_message);
9130           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : INVALID PARAMETER EXCEPTION = '||sqlerrm(sqlcode));
9131           OZF_UTILITY_PVT.debug_message(fnd_message.get_string('OZF', 'OZF_TP_INVALID_PARAM_TXT'));
9132 
9133      WHEN FND_API.G_EXC_ERROR THEN
9134           ROLLBACK TO allocate_target;
9135           x_return_status := FND_API.g_ret_sts_error ;
9136           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
9137                                      p_data    => x_error_message);
9138 
9139      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
9140           ROLLBACK TO allocate_target;
9141           x_return_status := FND_API.g_ret_sts_unexp_error ;
9142           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
9143                                      p_data  => x_error_message);
9144 
9145      WHEN OTHERS THEN
9146           ROLLBACK TO allocate_target;
9147 
9148           FND_MESSAGE.set_name('OZF', 'OZF_TP_OTHERS_ERROR_TXT');
9149           FND_MESSAGE.set_token('OZF_TP_SQLERRM_TOKEN',SQLERRM);
9150           FND_MESSAGE.set_token('OZF_TP_SQLCODE_TOKEN',SQLCODE);
9151           FND_MSG_PUB.add;
9152 
9153           x_return_status := FND_API.g_ret_sts_unexp_error ;
9154           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
9155              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
9156           END IF;
9157           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
9158                                      p_data  => x_error_message);
9159           OZF_UTILITY_PVT.debug_message(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
9160 
9161  END allocate_target;
9162 
9163 
9164 
9165 ----------------------------------------------------------------------------------------------------------
9166 ----------------------------------------------------------------------------------------------------------
9167 
9168 -- ------------------------
9169 -- Public Function
9170 -- ------------------------
9171 -- ------------------------------------------------------------------
9172 -- Name: Called from Account Spread and Product Spread UI
9173 -- Desc: This is part of the tweaking to swap DB rows to UI columns
9174 --       after brainstorming with ATG, Performance, Arch teams.
9175 -- Note: Distinct allocation_for are = { CUST, PROD }
9176 --
9177 --
9178 --
9179 -- -----------------------------------------------------------------
9180 
9181  FUNCTION GET_TARGET
9182     (p_allocation_for_id      IN number,
9183      p_time_id                IN number,
9184      p_allocation_for         IN varchar2 DEFAULT 'PROD'
9185     ) RETURN NUMBER IS
9186 
9187  l_target  number;
9188 
9189  BEGIN
9190 
9191    IF p_time_id = null or p_time_id < 0 THEN
9192       return -1;
9193    ELSE
9194      SELECT
9195        target into l_target
9196      FROM
9197        OZF_TIME_ALLOCATIONS
9198      WHERE
9199            allocation_for = p_allocation_for
9200        AND allocation_for_id = p_allocation_for_id
9201        AND time_id = p_time_id;
9202 
9203      return l_target;
9204    END IF;
9205 
9206  EXCEPTION
9207    WHEN OTHERS THEN
9208      return null;
9209  END;
9210 
9211 
9212 -- ------------------------
9213 -- Public Function
9214 -- ------------------------
9215 -- ------------------------------------------------------------------
9216 -- Name: Called from Account Spread and Product Spread UI
9217 -- Desc: This is part of the tweaking to swap DB rows to UI columns
9218 --       after brainstorming with ATG, Performance, Arch teams.
9219 -- Note: Distinct allocation_for are = { CUST, PROD }
9220 --
9221 --
9222 --
9223 -- -----------------------------------------------------------------
9224 
9225  FUNCTION GET_TARGET_PKEY
9226     (p_allocation_for_id      IN number,
9227      p_time_id                IN number,
9228      p_allocation_for         IN varchar2 DEFAULT 'PROD'
9229     ) RETURN NUMBER IS
9230 
9231  l_time_allocation_id  number;
9232 
9233  BEGIN
9234 
9235    IF p_time_id = null or p_time_id < 0 THEN
9236       return -1;
9237    ELSE
9238      SELECT
9239        time_allocation_id into l_time_allocation_id
9240      FROM
9241        OZF_TIME_ALLOCATIONS
9242      WHERE
9243            allocation_for = p_allocation_for
9244        AND allocation_for_id = p_allocation_for_id
9245        AND time_id = p_time_id;
9246 
9247      return l_time_allocation_id;
9248    END IF;
9249 
9250  EXCEPTION
9251    WHEN OTHERS THEN
9252      return null;
9253  END;
9254 
9255 -- ------------------------
9256 -- Public Function
9257 -- ------------------------
9258 -- ------------------------------------------------------------------
9259 -- Name: Called from Account Spread and Product Spread UI
9260 -- Desc: This is part of the tweaking to swap DB rows to UI columns
9261 --       after brainstorming with ATG, Performance, Arch teams.
9262 -- Note: Distinct allocation_for are = { CUST, PROD }
9263 --
9264 --
9265 --
9266 -- -----------------------------------------------------------------
9267 
9268  FUNCTION GET_SALES
9269     (p_allocation_for_id      IN number,
9270      p_time_id                IN number,
9271      p_allocation_for         IN varchar2 DEFAULT 'PROD'
9272     ) RETURN NUMBER IS
9273 
9274  l_lysp_sales number;
9275 
9276  BEGIN
9277 
9278    IF p_time_id = null or p_time_id < 0 THEN
9279       return -1;
9280    ELSE
9281      SELECT
9282        lysp_sales into l_lysp_sales
9283      FROM
9284        OZF_TIME_ALLOCATIONS
9285      WHERE
9286            allocation_for = p_allocation_for
9287        AND allocation_for_id = p_allocation_for_id
9288        AND time_id = p_time_id;
9289 
9290      return l_lysp_sales;
9291    END IF;
9292 
9293  EXCEPTION
9294    WHEN OTHERS THEN
9295      return null;
9296  END;
9297 
9298 
9299 -- ------------------------
9300 -- ------------------------
9301 -- Private  Procedure
9302 -- ------------------------
9303 -- ------------------------------------------------------------------
9304 -- Name: adjust_target_for_acct_added
9305 -- Desc: 1. Create new target allocation records,
9306 --          when an account is newly assigned to a territory
9307 --       2. Note that FACT Product Spread is NOT effected due to xFer of ship_to
9308 -- -----------------------------------------------------------------
9309 PROCEDURE adjust_target_for_acct_added
9310  (
9311     p_api_version        IN          NUMBER,
9312     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
9313     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
9314     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
9315     x_return_status      OUT NOCOPY  VARCHAR2,
9316     x_error_number       OUT NOCOPY  NUMBER,
9317     x_error_message      OUT NOCOPY  VARCHAR2,
9318     p_fund_id              IN          NUMBER,
9319     p_corr_fund_id         IN          NUMBER,
9320     p_terr_id              IN          NUMBER,
9321     p_ship_to_site_use_id  IN          NUMBER
9322 ) IS
9323    l_api_version   CONSTANT NUMBER       := 1.0;
9324    l_api_name      CONSTANT VARCHAR2(30) := 'adjust_target_for_acct_added';
9325    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
9326    l_return_status          VARCHAR2(1);
9327 
9328    l_object_version_number  NUMBER := 1;
9329    l_org_id                 NUMBER := TO_NUMBER(SUBSTRB(USERENV('CLIENT_INFO'), 1, 10));
9330 
9331    l_start_date             VARCHAR2(30) := null;
9332    l_end_date               VARCHAR2(30) := null;
9333    l_period_type_id         NUMBER;
9334    l_lysp_in_clause         VARCHAR2(1000) := null;
9335    l_in_clause              VARCHAR2(1000) := null;
9336 
9337    l_total_lysp_sales       NUMBER;
9338    l_total_product_sales    NUMBER;
9339    l_product_sales          NUMBER;
9340 
9341    l_account_allocation_id  NUMBER;
9342    l_product_allocation_id  NUMBER;
9343    l_time_allocation_id     NUMBER;
9344 
9345    l_period_tbl          OZF_TIME_API_PVT.G_period_tbl_type;
9346    l_lysp_period_tbl     OZF_TIME_API_PVT.G_period_tbl_type;
9347 
9348    p_acct_alloc_rec      ozf_account_allocations%ROWTYPE;
9349    p_prod_alloc_rec      ozf_product_allocations%ROWTYPE;
9350    p_time_alloc_rec      ozf_time_allocations%ROWTYPE;
9351 
9352 
9353   CURSOR fund_csr
9354   IS
9355    SELECT
9356     owner,
9357     start_period_id,
9358     end_period_id,
9359     start_date_active,
9360     end_date_active,
9361     status_code,
9362     original_budget,
9363     transfered_in_amt,
9364     transfered_out_amt,
9365     node_id, -- (=territory id)
9366     product_spread_time_id period_type_id -- (= minor_scale_id i.e. qtrly or monthly)
9367    FROM
9368     ozf_funds_all_vl
9369    WHERE
9370     fund_id = p_fund_id;
9371 
9372   l_fund_rec    fund_csr%ROWTYPE;
9373 
9374 
9375   CURSOR product_lysp_sales_csr (l_product_id    NUMBER,
9376                                  l_territory_id  NUMBER,
9377                                  l_site_use_id   NUMBER,
9378                                  l_time_id       NUMBER) IS
9379    SELECT
9380     SUM(bsmv.sales) sales
9381    FROM
9382      ozf_order_sales_v bsmv,
9383      ams_party_market_segments a
9384    WHERE
9385         a.market_qualifier_reference = l_territory_id
9386     AND a.market_qualifier_type='TERRITORY'
9387     AND a.site_use_id = l_site_use_id
9388     AND bsmv.ship_to_site_use_id = a.site_use_id
9389     AND bsmv.inventory_item_id = l_product_id
9390     AND bsmv.time_id = l_time_id;
9391 
9392 
9393   CURSOR category_lysp_sales_csr (l_category_id    NUMBER,
9394                                   l_territory_id   NUMBER,
9395                                   l_site_use_id    NUMBER,
9396                                   l_time_id        NUMBER,
9397                                   l_fund_id        NUMBER) IS
9398   SELECT
9399    SUM(bsmv.sales) sales
9400   FROM
9401    ozf_order_sales_v bsmv,
9402    ams_party_market_segments a
9403   WHERE
9404       a.market_qualifier_reference = l_territory_id
9405   AND a.market_qualifier_type='TERRITORY'
9406   AND a.site_use_id = l_site_use_id
9407   AND bsmv.ship_to_site_use_id = a.site_use_id
9408   AND bsmv.time_id = l_time_id
9409   AND bsmv.inventory_item_id IN
9410                              ( SELECT DISTINCT MIC.INVENTORY_ITEM_ID
9411                                FROM   MTL_ITEM_CATEGORIES     MIC,
9412                                       ENI_PROD_DENORM_HRCHY_V DENORM
9413                                WHERE  MIC.CATEGORY_SET_ID  = DENORM.CATEGORY_SET_ID
9414                                 AND   MIC.CATEGORY_ID      = DENORM.CHILD_ID
9415                                 AND   DENORM.PARENT_ID     = l_category_id
9416                                MINUS
9417                                SELECT p.item_id
9418                                FROM   ozf_product_allocations p
9419                                WHERE  p.fund_id = l_fund_id
9420                                   AND p.item_type = 'PRICING_ATTRIBUTE1'
9421                              );
9422 
9423 
9424   CURSOR others_lysp_sales_csr (l_territory_id   NUMBER,
9425                                 l_site_use_id    NUMBER,
9426                                 l_time_id        NUMBER,
9427                                 l_fund_id        NUMBER) IS
9428   SELECT
9429    SUM(bsmv.sales) sales
9430   FROM
9431    ozf_order_sales_v bsmv,
9432    ams_party_market_segments a
9433   WHERE
9434       a.market_qualifier_reference = l_territory_id
9435   AND a.market_qualifier_type='TERRITORY'
9436   AND a.site_use_id = l_site_use_id
9437   AND bsmv.ship_to_site_use_id = a.site_use_id
9438   AND bsmv.time_id = l_time_id
9439   AND NOT EXISTS
9440   (
9441   ( SELECT p.item_id
9442     FROM ozf_product_allocations p
9443     WHERE
9444         p.fund_id = l_fund_id
9445     AND p.item_type = 'PRICING_ATTRIBUTE1'
9446     AND p.item_id = bsmv.inventory_item_id
9447     UNION ALL
9448     SELECT MIC.INVENTORY_ITEM_ID
9449     FROM   MTL_ITEM_CATEGORIES MIC,
9450            ENI_PROD_DENORM_HRCHY_V DENORM,
9451            OZF_PRODUCT_ALLOCATIONS p
9452     WHERE p.FUND_ID = l_fund_id
9453       AND p.ITEM_TYPE = 'PRICING_ATTRIBUTE2'
9454       AND p.ITEM_ID = DENORM.PARENT_ID
9455       AND MIC.CATEGORY_SET_ID = DENORM.CATEGORY_SET_ID
9456       AND MIC.CATEGORY_ID = DENORM.CHILD_ID
9457       AND MIC.INVENTORY_ITEM_ID = bsmv.inventory_item_id
9458   )
9459   MINUS
9460   SELECT prod.inventory_item_id
9461   FROM ams_act_products prod
9462   where
9463       prod.level_type_code = 'PRODUCT'
9464   AND prod.arc_act_product_used_by = 'FUND'
9465   AND prod.act_product_used_by_id = l_fund_id
9466   AND prod.excluded_flag = 'Y'
9467   AND prod.inventory_item_id = bsmv.inventory_item_id
9468   );
9469 
9470   CURSOR fund_product_spread_csr
9471          (l_fund_id        number) IS
9472     SELECT
9473        p.product_allocation_id,
9474        p.item_id,
9475        p.item_type,
9476        p.target
9477     FROM
9478        ozf_product_allocations p
9479     WHERE
9480        p.fund_id = l_fund_id;
9481 
9482    l_fund_product_rec     fund_product_spread_csr%rowtype;
9483 
9484 
9485   CURSOR corr_time_alloc_rec (l_alloc_for_id NUMBER) IS
9486   SELECT *
9487   FROM  ozf_time_allocations tt
9488   WHERE tt.allocation_for = 'CUST'
9489     AND tt.allocation_for_id = l_alloc_for_id; -- p_acct_alloc_rec.account_allocation_id;
9490 
9491 
9492   CURSOR corr_prod_alloc_rec
9493          (l_acct_alloc_id        number) IS
9494     SELECT *
9495     FROM
9496        ozf_product_allocations p
9497     WHERE
9498        p.allocation_for = 'CUST'
9499    AND p.allocation_for_id = l_acct_alloc_id; -- p_acct_alloc_rec.account_allocation_id;
9500 
9501 
9502   CURSOR corr_prod_time_alloc_rec (l_alloc_for_id NUMBER) IS
9503   SELECT *
9504   FROM  ozf_time_allocations tt
9505   WHERE tt.allocation_for = 'PROD'
9506     AND tt.allocation_for_id = l_alloc_for_id; -- p_prod_alloc_rec.product_allocation_id;
9507 
9508 
9509  BEGIN
9510 
9511    -- Standard Start of API savepoint
9512    SAVEPOINT adjust_target_for_acct_added;
9513 
9514 
9515    -- Standard call to check for call compatibility.
9516    IF NOT FND_API.compatible_api_call(l_api_version,
9517                                       p_api_version,
9518                                       l_api_name,
9519                                       g_pkg_name)
9520    THEN
9521      RAISE FND_API.g_exc_unexpected_error;
9522    END IF;
9523 
9524    -- Initialize message list if p_init_msg_list is set to TRUE.
9525    IF FND_API.to_Boolean( p_init_msg_list ) THEN
9526       FND_MSG_PUB.initialize;
9527    END IF;
9528 
9529    -- Initialize API return status to SUCCESS
9530    x_return_status := FND_API.g_ret_sts_success;
9531 
9532    Ozf_Utility_pvt.write_conc_log('Private API: ' || l_full_api_name || ' -- start');
9533 
9534    Ozf_Utility_pvt.write_conc_log('- Parameter - p_fund_id ==> '      || p_fund_id);
9535    Ozf_Utility_pvt.write_conc_log('- Parameter - p_corr_fund_id ==> ' || p_corr_fund_id);
9536    Ozf_Utility_pvt.write_conc_log('- Parameter - p_terr_id ==> '      || p_terr_id);
9537    Ozf_Utility_pvt.write_conc_log('- Parameter - p_ship_to_site_use_id ==> ' || p_ship_to_site_use_id);
9538 
9539 
9540    OPEN fund_csr;
9541    FETCH fund_csr INTO l_fund_rec;
9542    CLOSE fund_csr ;
9543 
9544    l_start_date := to_char(l_fund_rec.start_date_active, 'YYYY/MM/DD');
9545    l_end_date   := to_char(l_fund_rec.end_date_active, 'YYYY/MM/DD');
9546    l_period_type_id := l_fund_rec.period_type_id;
9547 
9548    Ozf_Utility_pvt.write_conc_log('Private API: ' || l_full_api_name || ': Begin - Getting Time_ids Between '
9549                                                  ||l_start_date||' AND '||l_end_date||' ; '
9550                                                  ||' Period_Type_id = '||l_period_type_id||' ; ');
9551 
9552    IF l_start_date IS NULL OR
9553       l_end_date IS NULL OR
9554       l_period_type_id IS NULL
9555    THEN
9556       RAISE OZF_TP_BLANK_PERIOD_TBL;
9557    END IF;
9558 
9559    l_period_tbl := OZF_TIME_API_PVT.get_period_tbl
9560                                     (l_start_date,
9561                                      l_end_date,
9562                                      l_period_type_id);
9563 
9564    IF l_period_tbl IS NULL OR l_period_tbl.COUNT <= 0 THEN
9565       RAISE OZF_TP_BLANK_PERIOD_TBL;
9566    END IF;
9567 
9568    Ozf_Utility_pvt.write_conc_log('Private API: ' || l_full_api_name || ': End - Getting Time_ids Between '||l_start_date
9569                                          ||' AND '||l_end_date||' ; ');
9570    IF l_period_tbl IS NOT NULL THEN
9571     IF l_period_tbl.COUNT > 0 THEN
9572       FOR l_idx IN l_period_tbl.first..l_period_tbl.last
9573       LOOP
9574         IF l_period_tbl.exists(l_idx) THEN
9575 
9576          l_lysp_period_tbl(l_idx) := OZF_TIME_API_PVT.get_lysp_id (l_period_tbl(l_idx), l_period_type_id);
9577 
9578          Ozf_Utility_pvt.write_conc_log(SubStr('l_period_tbl('||TO_CHAR(l_idx)||') = '
9579                                ||TO_CHAR(l_period_tbl(l_idx)), 1,255));
9580         END IF;
9581       END LOOP;
9582     END IF;
9583    END IF;
9584 
9585    --l_in_clause := '(';
9586    l_in_clause := NULL;
9587    IF l_period_tbl IS NOT NULL THEN
9588       FOR l_idx IN l_period_tbl.first..l_period_tbl.last
9589       LOOP
9590           IF l_in_clause IS NULL THEN
9591              l_in_clause := LTRIM(' '||l_period_tbl(l_idx));
9592           ELSE
9593              l_in_clause := l_in_clause ||','|| l_period_tbl(l_idx);
9594           END IF;
9595       END LOOP;
9596    END IF;
9597    --l_in_clause := l_in_clause||')';
9598 
9599    l_lysp_in_clause := NULL;
9600    IF l_lysp_period_tbl IS NOT NULL THEN
9601       FOR l_idx IN l_lysp_period_tbl.first..l_lysp_period_tbl.last
9602       LOOP
9603           IF l_lysp_in_clause IS NULL THEN
9604              l_lysp_in_clause := LTRIM(' '||l_lysp_period_tbl(l_idx));
9605           ELSE
9606              l_lysp_in_clause := l_lysp_in_clause ||','|| l_lysp_period_tbl(l_idx);
9607           END IF;
9608       END LOOP;
9609    END IF;
9610    --l_lysp_in_clause := l_lysp_in_clause||')';
9611 
9612 
9613    Ozf_Utility_pvt.write_conc_log('Private API: ' || l_full_api_name || ': Time_ids between '||l_start_date
9614                                          ||' AND '||l_end_date||' ARE : '||l_in_clause||' ; ');
9615    Ozf_Utility_pvt.write_conc_log('Private API: ' || l_full_api_name || ': LYSP-Time_ids between '||l_start_date
9616                                          ||' AND '||l_end_date||' ARE : '||l_lysp_in_clause||' ; ');
9617 
9618 
9619 -- 1. CREATING NEW SHIPTO ACCOUNT HEADER RECORD ....
9620 
9621 p_acct_alloc_rec := null;
9622 
9623 IF p_corr_fund_id IS NOT NULL
9624 THEN
9625 
9626 ---read the corr funds existing account rows -----
9627   SELECT * INTO p_acct_alloc_rec
9628   FROM ozf_account_allocations aa
9629   WHERE aa.allocation_for = 'FUND'
9630     and allocation_for_id = p_corr_fund_id
9631     and site_use_code = 'SHIP_TO'
9632     and site_use_id = p_ship_to_site_use_id;
9633 ELSE
9634 
9635 -- read the unallocated row for p_fund_id
9636   SELECT * INTO p_acct_alloc_rec
9637   FROM ozf_account_allocations aa
9638   WHERE aa.allocation_for = 'FUND'
9639     and allocation_for_id = p_fund_id
9640     and site_use_id = -9999;
9641 
9642 ---then get the newly added account for p_terr_id and new_ship_to_id
9643   SELECT
9644     a.cust_account_id                                        cust_account_id,
9645     a.site_use_id                                            site_use_id,
9646     a.site_use_code                                          site_use_code,
9647     OZF_LOCATION_PVT.get_location_id(a.site_use_id)          location_id,
9648     NVL(a.bill_to_site_use_id, -9996)                        bill_to_site_use_id,
9649     OZF_LOCATION_PVT.get_location_id(a.bill_to_site_use_id)  bill_to_location_id,
9650     a.party_id                                               parent_party_id,
9651     NVL(a.rollup_party_id, a.party_id)                       rollup_party_id
9652   INTO
9653     p_acct_alloc_rec.cust_account_id,
9654     p_acct_alloc_rec.site_use_id,
9655     p_acct_alloc_rec.site_use_code,
9656     p_acct_alloc_rec.location_id,
9657     p_acct_alloc_rec.bill_to_site_use_id,
9658     p_acct_alloc_rec.bill_to_location_id,
9659     p_acct_alloc_rec.parent_party_id,
9660     p_acct_alloc_rec.rollup_party_id
9661   FROM
9662     ams_party_market_segments a
9663   WHERE
9664        a.market_qualifier_reference = p_terr_id
9665    AND a.market_qualifier_type='TERRITORY'
9666    AND a.site_use_code = 'SHIP_TO'
9667    AND a.party_id IS NOT NULL
9668    AND a.site_use_id IS NOT NULL
9669    AND a.site_use_id = p_ship_to_site_use_id;
9670 
9671 
9672   p_acct_alloc_rec.selected_flag                 := 'Y';
9673   p_acct_alloc_rec.target                        := 0;
9674   p_acct_alloc_rec.lysp_sales                    := 0;
9675   p_acct_alloc_rec.parent_Account_allocation_id  := NULL;
9676 
9677 
9678 END IF;
9679 
9680 l_account_allocation_id := get_account_allocation_id;
9681 
9682 Ozf_Account_Allocations_Pkg.Insert_Row(
9683   px_Account_allocation_id        => l_account_allocation_id,
9684   p_allocation_for                => p_acct_alloc_rec.allocation_for,
9685   p_allocation_for_id             => p_fund_id,
9686   p_cust_account_id               => p_acct_alloc_rec.cust_account_id,
9687   p_site_use_id                   => p_acct_alloc_rec.site_use_id,
9688   p_site_use_code                 => p_acct_alloc_rec.site_use_code,
9689   p_location_id                   => p_acct_alloc_rec.location_id,
9690   p_bill_to_site_use_id           => p_acct_alloc_rec.bill_to_site_use_id,
9691   p_bill_to_location_id           => p_acct_alloc_rec.bill_to_location_id,
9692   p_parent_party_id               => p_acct_alloc_rec.parent_party_id,
9693   p_rollup_party_id               => p_acct_alloc_rec.rollup_party_id,
9694   p_selected_flag                 => p_acct_alloc_rec.selected_flag,
9695   p_target                        => 0,
9696   p_lysp_sales                    => p_acct_alloc_rec.lysp_sales,
9697   p_parent_Account_allocation_id  => p_acct_alloc_rec.parent_account_allocation_id,
9698   px_object_version_number        => l_object_version_number,
9699   p_creation_date                 => SYSDATE,
9700   p_created_by                    => FND_GLOBAL.USER_ID,
9701   p_last_update_date              => SYSDATE,
9702   p_last_updated_by               => FND_GLOBAL.USER_ID,
9703   p_last_update_login             => FND_GLOBAL.conc_login_id,
9704   p_attribute_category            => p_acct_alloc_rec.attribute_category,
9705   p_attribute1                    => p_acct_alloc_rec.attribute1,
9706   p_attribute2                    => p_acct_alloc_rec.attribute2,
9707   p_attribute3                    => p_acct_alloc_rec.attribute3,
9708   p_attribute4                    => p_acct_alloc_rec.attribute4,
9709   p_attribute5                    => p_acct_alloc_rec.attribute5,
9710   p_attribute6                    => p_acct_alloc_rec.attribute6,
9711   p_attribute7                    => p_acct_alloc_rec.attribute7,
9712   p_attribute8                    => p_acct_alloc_rec.attribute8,
9713   p_attribute9                    => p_acct_alloc_rec.attribute9,
9714   p_attribute10                   => p_acct_alloc_rec.attribute10,
9715   p_attribute11                   => p_acct_alloc_rec.attribute11,
9716   p_attribute12                   => p_acct_alloc_rec.attribute12,
9717   p_attribute13                   => p_acct_alloc_rec.attribute13,
9718   p_attribute14                   => p_acct_alloc_rec.attribute14,
9719   p_attribute15                   => p_acct_alloc_rec.attribute15,
9720   px_org_id                       => l_org_id
9721 );
9722 
9723 
9724 -- 2. CREATING NEW SHIPTO ACCOUNT - TIME RECORDS ....
9725 
9726 FOR p_time_alloc_rec IN corr_time_alloc_rec (p_acct_alloc_rec.account_allocation_id)
9727 LOOP
9728            l_time_allocation_id := get_time_allocation_id;
9729            Ozf_Time_Allocations_Pkg.Insert_Row(
9730               px_time_allocation_id  => l_time_allocation_id,
9731               p_allocation_for  => p_time_alloc_rec.allocation_for,
9732               p_allocation_for_id  => l_account_allocation_id,
9733               p_time_id  => p_time_alloc_rec.time_id,
9734               p_period_type_id => p_time_alloc_rec.period_type_id,
9735               p_target  => p_time_alloc_rec.target,
9736               p_lysp_sales  => p_time_alloc_rec.lysp_sales,
9737               px_object_version_number  => l_object_version_number,
9738               p_creation_date  => SYSDATE,
9739               p_created_by  => FND_GLOBAL.USER_ID,
9740               p_last_update_date  => SYSDATE,
9741               p_last_updated_by  => FND_GLOBAL.USER_ID,
9742               p_last_update_login  => FND_GLOBAL.conc_login_id,
9743               p_attribute_category  => p_time_alloc_rec.attribute_category,
9744               p_attribute1  => p_time_alloc_rec.attribute1,
9745               p_attribute2  => p_time_alloc_rec.attribute2,
9746               p_attribute3  => p_time_alloc_rec.attribute3,
9747               p_attribute4  => p_time_alloc_rec.attribute4,
9748               p_attribute5  => p_time_alloc_rec.attribute5,
9749               p_attribute6  => p_time_alloc_rec.attribute6,
9750               p_attribute7  => p_time_alloc_rec.attribute7,
9751               p_attribute8  => p_time_alloc_rec.attribute8,
9752               p_attribute9  => p_time_alloc_rec.attribute9,
9753               p_attribute10  => p_time_alloc_rec.attribute10,
9754               p_attribute11  => p_time_alloc_rec.attribute11,
9755               p_attribute12  => p_time_alloc_rec.attribute12,
9756               p_attribute13  => p_time_alloc_rec.attribute13,
9757               p_attribute14  => p_time_alloc_rec.attribute14,
9758               p_attribute15  => p_time_alloc_rec.attribute15,
9759               px_org_id  => l_org_id
9760             );
9761 END LOOP; -- end of account - time records
9762 
9763 
9764 IF p_corr_fund_id IS NOT NULL THEN
9765 
9766 -- 3. UPDATING NEW SHIPTO ACCOUNT TIME RECORDS  - PAST periods....
9767 
9768   -- CURRENT AND FUTURE TARGETS ARE carried with the account (already done above)
9769 
9770   -- NOW, MAKE PAST TARGETS ZERO
9771   update  ozf_time_allocations tt
9772   set tt.target = 0
9773   WHERE tt.allocation_for = 'CUST'
9774     AND tt.allocation_for_id = l_account_allocation_id
9775     and EXISTS
9776             (select 'x'
9777             from ozf_time_ent_period period
9778             where period.ent_period_id = tt.time_id
9779             and period.end_date < trunc(sysdate)
9780             and tt.period_type_id = 32
9781             UNION
9782             select 'x'
9783             from ozf_time_ent_qtr qtr
9784             where qtr.ent_qtr_id = tt.time_id
9785             and qtr.end_date < trunc(sysdate)
9786             and tt.period_type_id = 64
9787             );
9788 
9789 -- 4. UPDATING UNALLOCATED TIME RECORDS  - CURR and FUTURE periods....
9790 
9791   -- PAST UNALLOCATED remains unchanged
9792   -- Adjust CURRRENT AND FUTURE Targets for UNALLOCATED row
9793   update  ozf_time_allocations tta
9794   set tta.target = tta.target - (
9795                                 SELECT ttb.target
9796                                 FROM ozf_time_allocations ttb
9797                                 WHERE ttb.allocation_for = 'CUST'
9798                                 AND ttb.allocation_for_id = l_account_allocation_id
9799                                 AND ttb.time_id = tta.time_id
9800                                 )
9801   where
9802         tta.allocation_for = 'CUST'
9803     AND tta.allocation_for_id = ( SELECT aa.account_allocation_id
9804                                 FROM ozf_account_allocations aa
9805                                 WHERE aa.allocation_for = 'FUND'
9806                                 and aa.allocation_for_id = p_fund_id
9807                                 and aa.site_use_id = -9999)
9808     and EXISTS
9809             (select 'x'
9810             from ozf_time_ent_period period
9811             where period.ent_period_id = tta.time_id
9812             and period.end_date >= trunc(sysdate)
9813             and tta.period_type_id = 32
9814             UNION
9815             select 'x'
9816             from ozf_time_ent_qtr qtr
9817             where qtr.ent_qtr_id = tta.time_id
9818             and qtr.end_date >= trunc(sysdate)
9819             and tta.period_type_id = 64
9820             );
9821 
9822 /*
9823   update  ozf_time_allocations tta,  ozf_time_allocations ttb
9824   set tta.target = tta.target - ttb.target
9825   where ttb.allocation_for = 'CUST'
9826     AND ttb.allocation_for_id = l_account_allocation_id
9827     AND tta.time_id = ttb.time_id
9828     AND tta.allocation_for = 'CUST'
9829     AND tta.allocation_for_id ( SELECT aa.account_allocation_id
9830                                 FROM ozf_account_allocations aa
9831                                 WHERE aa.allocation_for = 'FUND'
9832                                 and aa.allocation_for_id = p_fund_id
9833                                 and aa.site_use_id = -9999)
9834     and EXISTS
9835             (select 'x'
9836             from ozf_time_ent_period period
9837             where period.ent_period_id = tta.time_id
9838             and period.start_date >= trunc(sysdate)
9839             and tta.period_type_id = 32
9840             UNION
9841             select 'x'
9842             from ozf_time_ent_qtr qtr
9843             where qtr.ent_qtr_id = tta.time_id
9844             and qtr.start_date >= trunc(sysdate)
9845             and tta.period_type_id = 64
9846             );
9847 */
9848 
9849 ELSE
9850 
9851 -- When corr quota is not found  i.e. this is brand new account
9852 
9853 -- 5. UPDATING NEW SHIPTO ACCOUNT TIME RECORDS  - All periods....
9854 
9855   update  ozf_time_allocations tt
9856   set tt.target = 0,
9857   tt.lysp_sales = (
9858                   SELECT SUM(bsmv.sales)
9859                   FROM ozf_order_sales_v bsmv
9860                   WHERE bsmv.ship_to_site_use_id = p_ship_to_site_use_id
9861                   AND bsmv.time_id = OZF_TIME_API_PVT.get_lysp_id(tt.time_id, tt.period_type_id)
9862                   )
9863   WHERE tt.allocation_for = 'CUST'
9864     AND tt.allocation_for_id = l_account_allocation_id;
9865 
9866 
9867 END IF;
9868 
9869 
9870 -- Rollup the time targets to the acount target level for 2 rows
9871 --   i) for newly created row for l_ship_to
9872 --  (ii) for unallocated row
9873 
9874 -- 6. UPDATING NEW SHIPTO and UNALLOCATED ACCOUNT Header RECORDS  ....
9875 
9876   UPDATE OZF_ACCOUNT_ALLOCATIONS aa
9877   SET (aa.TARGET, aa.LYSP_SALES) = (
9878                   SELECT SUM(ti.TARGET), SUM(ti.lysp_sales)
9879                   FROM  OZF_TIME_ALLOCATIONS ti
9880                   WHERE  ti.ALLOCATION_FOR = 'CUST'
9881                   AND  ti.ALLOCATION_FOR_ID = aa.account_allocation_id
9882                  ),
9883     aa.object_version_number = aa.object_version_number + 1,
9884     aa.last_update_date = SYSDATE,
9885     aa.last_updated_by = FND_GLOBAL.USER_ID,
9886     aa.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
9887   WHERE
9888     (aa.account_allocation_id = l_account_allocation_id)
9889     OR
9890     (
9891         aa.allocation_for = 'FUND'
9892     and aa.allocation_for_id = p_fund_id
9893     and aa.site_use_id = -9999
9894     );
9895 
9896 
9897 -- 7. UPDATING SHIPTOs Product Allocation Records  ...
9898 
9899 
9900     -- IF There is corr QUOTA THEN move its product allocation for l_ship_to
9901   IF p_corr_fund_id IS NOT NULL THEN
9902 
9903   -- 8. CREATING SHIPTOs Product Spread Header Records ....
9904 
9905     FOR p_prod_alloc_rec IN corr_prod_alloc_rec (p_acct_alloc_rec.account_allocation_id)
9906     LOOP
9907 
9908            l_product_allocation_id := get_product_allocation_id;
9909 
9910            Ozf_Product_Allocations_Pkg.Insert_Row(
9911               px_product_allocation_id  => l_product_allocation_id,
9912               p_allocation_for  => p_prod_alloc_rec.allocation_for,
9913               p_allocation_for_id  => l_account_allocation_id,
9914               p_fund_id  => p_fund_id,
9915               p_item_type  => p_prod_alloc_rec.item_type,
9916               p_item_id  => p_prod_alloc_rec.item_id,
9917               p_selected_flag  => p_prod_alloc_rec.selected_flag,
9918               p_target  => p_prod_alloc_rec.target,
9919               p_lysp_sales  => p_prod_alloc_rec.lysp_sales,
9920               p_parent_product_allocation_id  => p_prod_alloc_rec.parent_product_allocation_id,
9921               px_object_version_number  => l_object_version_number,
9922               p_creation_date  => SYSDATE,
9923               p_created_by  => FND_GLOBAL.USER_ID,
9924               p_last_update_date  => SYSDATE,
9925               p_last_updated_by  => FND_GLOBAL.USER_ID,
9926               p_last_update_login  => FND_GLOBAL.conc_login_id,
9927               p_attribute_category  => p_prod_alloc_rec.attribute_category,
9928               p_attribute1  => p_prod_alloc_rec.attribute1,
9929               p_attribute2  => p_prod_alloc_rec.attribute2,
9930               p_attribute3  => p_prod_alloc_rec.attribute3,
9931               p_attribute4  => p_prod_alloc_rec.attribute4,
9932               p_attribute5  => p_prod_alloc_rec.attribute5,
9933               p_attribute6  => p_prod_alloc_rec.attribute6,
9934               p_attribute7  => p_prod_alloc_rec.attribute7,
9935               p_attribute8  => p_prod_alloc_rec.attribute8,
9936               p_attribute9  => p_prod_alloc_rec.attribute9,
9937               p_attribute10  => p_prod_alloc_rec.attribute10,
9938               p_attribute11  => p_prod_alloc_rec.attribute11,
9939               p_attribute12  => p_prod_alloc_rec.attribute12,
9940               p_attribute13  => p_prod_alloc_rec.attribute13,
9941               p_attribute14  => p_prod_alloc_rec.attribute14,
9942               p_attribute15  => p_prod_alloc_rec.attribute15,
9943               px_org_id  => l_org_id
9944             );
9945 
9946   -- 9. CREATING SHIPTOs Product Spread - TIME  Records ....
9947         FOR p_time_alloc_rec IN corr_prod_time_alloc_rec (p_prod_alloc_rec.product_allocation_id)
9948         LOOP
9949                    l_time_allocation_id := get_time_allocation_id;
9950                    Ozf_Time_Allocations_Pkg.Insert_Row(
9951                       px_time_allocation_id  => l_time_allocation_id,
9952                       p_allocation_for  => p_time_alloc_rec.allocation_for,
9953                       p_allocation_for_id  => l_product_allocation_id,
9954                       p_time_id  => p_time_alloc_rec.time_id,
9955                       p_period_type_id => p_time_alloc_rec.period_type_id,
9956                       p_target  => p_time_alloc_rec.target,
9957                       p_lysp_sales  => p_time_alloc_rec.lysp_sales,
9958                       px_object_version_number  => l_object_version_number,
9959                       p_creation_date  => SYSDATE,
9960                       p_created_by  => FND_GLOBAL.USER_ID,
9961                       p_last_update_date  => SYSDATE,
9962                       p_last_updated_by  => FND_GLOBAL.USER_ID,
9963                       p_last_update_login  => FND_GLOBAL.conc_login_id,
9964                       p_attribute_category  => p_time_alloc_rec.attribute_category,
9965                       p_attribute1  => p_time_alloc_rec.attribute1,
9966                       p_attribute2  => p_time_alloc_rec.attribute2,
9967                       p_attribute3  => p_time_alloc_rec.attribute3,
9968                       p_attribute4  => p_time_alloc_rec.attribute4,
9969                       p_attribute5  => p_time_alloc_rec.attribute5,
9970                       p_attribute6  => p_time_alloc_rec.attribute6,
9971                       p_attribute7  => p_time_alloc_rec.attribute7,
9972                       p_attribute8  => p_time_alloc_rec.attribute8,
9973                       p_attribute9  => p_time_alloc_rec.attribute9,
9974                       p_attribute10  => p_time_alloc_rec.attribute10,
9975                       p_attribute11  => p_time_alloc_rec.attribute11,
9976                       p_attribute12  => p_time_alloc_rec.attribute12,
9977                       p_attribute13  => p_time_alloc_rec.attribute13,
9978                       p_attribute14  => p_time_alloc_rec.attribute14,
9979                       p_attribute15  => p_time_alloc_rec.attribute15,
9980                       px_org_id  => l_org_id
9981                     );
9982         END LOOP; -- end of product - time records
9983 
9984 
9985   -- 10. Updating SHIPTOs Product Spread - TIME  Records ....PAST PERIODS....
9986 
9987     -- CURRENT AND FUTURE TARGETS ARE carried with the product (above)
9988     -- MAKE PAST TARGETS ZERO
9989       update  ozf_time_allocations tt
9990       set tt.target = 0
9991       WHERE tt.allocation_for = 'PROD'
9992         AND tt.allocation_for_id = l_product_allocation_id
9993         and EXISTS
9994                 (select 'x'
9995                 from ozf_time_ent_period
9996                 where ent_period_id = tt.time_id
9997                 and end_date < trunc(sysdate)
9998                 and tt.period_type_id = 32
9999                 UNION
10000                 select 'x'
10001                 from ozf_time_ent_qtr
10002                 where ent_qtr_id = tt.time_id
10003                 and end_date < trunc(sysdate)
10004                 and tt.period_type_id = 64
10005                 );
10006 
10007   --  Unallocated is automatically shown in the header of the product spread
10008 
10009   -- 11. Updating SHIPTOs Product Spread - Header Records ....
10010 
10011   -- Rollup the target numbers to the product record
10012      UPDATE OZF_PRODUCT_ALLOCATIONS p
10013      SET p.TARGET = (SELECT SUM(ti.TARGET)
10014                        FROM OZF_TIME_ALLOCATIONS ti
10015                       WHERE ti.ALLOCATION_FOR = 'PROD'
10016                         AND ti.ALLOCATION_FOR_ID = p.product_allocation_id),
10017          p.object_version_number = p.object_version_number + 1,
10018          p.last_update_date = SYSDATE,
10019          p.last_updated_by = FND_GLOBAL.USER_ID,
10020          p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
10021      WHERE p.product_allocation_id = l_product_allocation_id;
10022 
10023     END LOOP; --- FOR p_prod_alloc_rec IN corr_prod_alloc_rec
10024 
10025 
10026   ELSE
10027 
10028   -- if there is NO corr QUOTA THEN create brand new product allocation rows
10029 
10030   -- 13. CREATING SHIPTOs Product Spread - Header Records ....
10031 
10032          <<account_product_loop>>
10033          FOR fund_product_rec IN fund_product_spread_csr(p_fund_id)
10034          LOOP
10035 
10036              p_prod_alloc_rec := NULL;
10037              p_prod_alloc_rec.allocation_for := 'CUST';
10038              p_prod_alloc_rec.allocation_for_id := l_account_allocation_id;
10039              p_prod_alloc_rec.item_type := fund_product_rec.item_type;
10040              p_prod_alloc_rec.item_id := fund_product_rec.item_id;
10041              p_prod_alloc_rec.selected_flag := 'N';
10042              p_prod_alloc_rec.target := 0; --- l_total_product_target;
10043              p_prod_alloc_rec.lysp_sales := 0;
10044 
10045              l_product_allocation_id := get_product_allocation_id;
10046 
10047              Ozf_Product_Allocations_Pkg.Insert_Row(
10048                 px_product_allocation_id  => l_product_allocation_id,
10049                 p_allocation_for  => p_prod_alloc_rec.allocation_for,
10050                 p_allocation_for_id  => p_prod_alloc_rec.allocation_for_id,
10051                 p_fund_id  => p_fund_id,
10052                 p_item_type  => p_prod_alloc_rec.item_type,
10053                 p_item_id  => p_prod_alloc_rec.item_id,
10054                 p_selected_flag  => p_prod_alloc_rec.selected_flag,
10055                 p_target  => p_prod_alloc_rec.target,
10056                 p_lysp_sales  => p_prod_alloc_rec.lysp_sales,
10057                 p_parent_product_allocation_id  => p_prod_alloc_rec.parent_product_allocation_id,
10058                 px_object_version_number  => l_object_version_number,
10059                 p_creation_date  => SYSDATE,
10060                 p_created_by  => FND_GLOBAL.USER_ID,
10061                 p_last_update_date  => SYSDATE,
10062                 p_last_updated_by  => FND_GLOBAL.USER_ID,
10063                 p_last_update_login  => FND_GLOBAL.conc_login_id,
10064                 p_attribute_category  => p_prod_alloc_rec.attribute_category,
10065                 p_attribute1  => p_prod_alloc_rec.attribute1,
10066                 p_attribute2  => p_prod_alloc_rec.attribute2,
10067                 p_attribute3  => p_prod_alloc_rec.attribute3,
10068                 p_attribute4  => p_prod_alloc_rec.attribute4,
10069                 p_attribute5  => p_prod_alloc_rec.attribute5,
10070                 p_attribute6  => p_prod_alloc_rec.attribute6,
10071                 p_attribute7  => p_prod_alloc_rec.attribute7,
10072                 p_attribute8  => p_prod_alloc_rec.attribute8,
10073                 p_attribute9  => p_prod_alloc_rec.attribute9,
10074                 p_attribute10  => p_prod_alloc_rec.attribute10,
10075                 p_attribute11  => p_prod_alloc_rec.attribute11,
10076                 p_attribute12  => p_prod_alloc_rec.attribute12,
10077                 p_attribute13  => p_prod_alloc_rec.attribute13,
10078                 p_attribute14  => p_prod_alloc_rec.attribute14,
10079                 p_attribute15  => p_prod_alloc_rec.attribute15,
10080                 px_org_id  => l_org_id
10081               );
10082 
10083 
10084              l_total_product_sales := 0;
10085 
10086   -- 14. CREATING SHIPTOs Product Spread - TIME Records ....
10087 
10088              <<account_product_time_loop>>
10089              FOR l_idx IN l_period_tbl.first..l_period_tbl.last
10090              LOOP
10091               IF l_period_tbl.exists(l_idx) THEN
10092 
10093                  p_time_alloc_rec := NULL;
10094                  l_product_sales := 0;
10095 
10096                  IF fund_product_rec.item_type = 'PRICING_ATTRIBUTE1' THEN
10097                     OPEN product_lysp_sales_csr(fund_product_rec.item_id,
10098                                                 p_terr_id,
10099                                                 p_ship_to_site_use_id,
10100                                                 l_lysp_period_tbl(l_idx)
10101                                                );
10102                     FETCH product_lysp_sales_csr INTO l_product_sales;
10103                     CLOSE product_lysp_sales_csr;
10104                  ELSIF fund_product_rec.item_type = 'PRICING_ATTRIBUTE2' THEN
10105                      OPEN category_lysp_sales_csr(fund_product_rec.item_id,
10106                                                   p_terr_id,
10107                                                   p_ship_to_site_use_id,
10108                                                   l_lysp_period_tbl(l_idx),
10109                                                   p_fund_id
10110                                                  );
10111                      FETCH category_lysp_sales_csr INTO l_product_sales;
10112                      CLOSE category_lysp_sales_csr;
10113                  ELSIF fund_product_rec.item_type = 'OTHERS' THEN
10114                      OPEN others_lysp_sales_csr(p_terr_id,
10115                                                 p_ship_to_site_use_id,
10116                                                 l_lysp_period_tbl(l_idx),
10117                                                 p_fund_id
10118                                                );
10119                      FETCH others_lysp_sales_csr INTO l_product_sales;
10120                      CLOSE others_lysp_sales_csr;
10121                  END IF;
10122 
10123                  l_product_sales := NVL(l_product_sales, 0);
10124                  l_total_product_sales := l_total_product_sales + l_product_sales;
10125 
10126                  p_time_alloc_rec.allocation_for := 'PROD';
10127                  p_time_alloc_rec.allocation_for_id := l_product_allocation_id;
10128                  p_time_alloc_rec.time_id := l_period_tbl(l_idx);
10129                  p_time_alloc_rec.period_type_id := l_period_type_id;
10130                  p_time_alloc_rec.target := 0;  ---------l_product_target;
10131                  p_time_alloc_rec.lysp_sales := l_product_sales;
10132 
10133                  l_time_allocation_id := get_time_allocation_id;
10134 
10135                  Ozf_Time_Allocations_Pkg.Insert_Row(
10136                     px_time_allocation_id  => l_time_allocation_id,
10137                     p_allocation_for  => p_time_alloc_rec.allocation_for,
10138                     p_allocation_for_id  => p_time_alloc_rec.allocation_for_id,
10139                     p_time_id  => p_time_alloc_rec.time_id,
10140                     p_period_type_id => p_time_alloc_rec.period_type_id,
10141                     p_target  => p_time_alloc_rec.target,
10142                     p_lysp_sales  => p_time_alloc_rec.lysp_sales,
10143                     px_object_version_number  => l_object_version_number,
10144                     p_creation_date  => SYSDATE,
10145                     p_created_by  => FND_GLOBAL.USER_ID,
10146                     p_last_update_date  => SYSDATE,
10147                     p_last_updated_by  => FND_GLOBAL.USER_ID,
10148                     p_last_update_login  => FND_GLOBAL.conc_login_id,
10149                     p_attribute_category  => p_time_alloc_rec.attribute_category,
10150                     p_attribute1  => p_time_alloc_rec.attribute1,
10151                     p_attribute2  => p_time_alloc_rec.attribute2,
10152                     p_attribute3  => p_time_alloc_rec.attribute3,
10153                     p_attribute4  => p_time_alloc_rec.attribute4,
10154                     p_attribute5  => p_time_alloc_rec.attribute5,
10155                     p_attribute6  => p_time_alloc_rec.attribute6,
10156                     p_attribute7  => p_time_alloc_rec.attribute7,
10157                     p_attribute8  => p_time_alloc_rec.attribute8,
10158                     p_attribute9  => p_time_alloc_rec.attribute9,
10159                     p_attribute10  => p_time_alloc_rec.attribute10,
10160                     p_attribute11  => p_time_alloc_rec.attribute11,
10161                     p_attribute12  => p_time_alloc_rec.attribute12,
10162                     p_attribute13  => p_time_alloc_rec.attribute13,
10163                     p_attribute14  => p_time_alloc_rec.attribute14,
10164                     p_attribute15  => p_time_alloc_rec.attribute15,
10165                     px_org_id  => l_org_id
10166                   );
10167 
10168 
10169                END IF;
10170              END LOOP account_product_time_loop;
10171 
10172 
10173   -- 15. UPDATING SHIPTOs Product Spread - Header Record ....
10174 
10175              UPDATE OZF_PRODUCT_ALLOCATIONS p
10176              SET p.lysp_sales = l_total_product_sales,
10177                  p.object_version_number = p.object_version_number + 1,
10178                  p.last_update_date = SYSDATE,
10179                  p.last_updated_by = FND_GLOBAL.USER_ID,
10180                  p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
10181              WHERE p.product_allocation_id = l_product_allocation_id;
10182 
10183          END LOOP account_product_loop;
10184 
10185   END IF;
10186 
10187    Ozf_Utility_pvt.write_conc_log('Private API: ' || l_full_api_name || ' -- end');
10188 
10189  EXCEPTION
10190 
10191      WHEN OZF_TP_INVALID_PARAM THEN
10192           ROLLBACK TO adjust_target_for_acct_added;
10193           FND_MESSAGE.set_name('OZF', 'OZF_TP_INVALID_PARAM_TXT');
10194           FND_MSG_PUB.add;
10195           x_return_status := FND_API.g_ret_sts_error ;
10196           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
10197                                      p_data    => x_error_message);
10198           Ozf_Utility_pvt.write_conc_log(l_full_api_name||' : INVALID PARAMETER EXCEPTION = '||sqlerrm(sqlcode));
10199           Ozf_Utility_pvt.write_conc_log(fnd_message.get_string('OZF', 'OZF_TP_INVALID_PARAM_TXT'));
10200 
10201      WHEN FND_API.G_EXC_ERROR THEN
10202           ROLLBACK TO adjust_target_for_acct_added;
10203           x_return_status := FND_API.g_ret_sts_error ;
10204           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
10205                                      p_data    => x_error_message);
10206 
10207      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10208           ROLLBACK TO adjust_target_for_acct_added;
10209           x_return_status := FND_API.g_ret_sts_unexp_error ;
10210           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
10211                                      p_data  => x_error_message);
10212 
10213      WHEN OTHERS THEN
10214           ROLLBACK TO adjust_target_for_acct_added;
10215 
10216           FND_MESSAGE.set_name('OZF', 'OZF_TP_OTHERS_ERROR_TXT');
10217           FND_MESSAGE.set_token('OZF_TP_SQLERRM_TOKEN',SQLERRM);
10218           FND_MESSAGE.set_token('OZF_TP_SQLCODE_TOKEN',SQLCODE);
10219           FND_MSG_PUB.add;
10220 
10221           x_return_status := FND_API.g_ret_sts_unexp_error ;
10222           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
10223              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
10224           END IF;
10225           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
10226                                      p_data  => x_error_message);
10227           Ozf_Utility_pvt.write_conc_log(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
10228 
10229  END adjust_target_for_acct_added;
10230 
10231 -- ------------------------
10232 -- ------------------------
10233 -- Private  Procedure
10234 -- ------------------------
10235 -- ------------------------------------------------------------------
10236 -- Name: adjust_target_for_acct_deleted
10237 -- Desc: 1. Adjust Old target allocation records,
10238 --          when an account is moved away from a territory
10239 --       2. Note that FACT Product Spread is NOT effected due to xFer of ship_to
10240 -- -----------------------------------------------------------------
10241 PROCEDURE adjust_target_for_acct_deleted
10242  (
10243     p_api_version        IN          NUMBER,
10244     p_init_msg_list      IN          VARCHAR2   := FND_API.G_FALSE,
10245     p_commit             IN          VARCHAR2   := FND_API.G_FALSE,
10246     p_validation_level   IN          NUMBER     := FND_API.G_VALID_LEVEL_FULL,
10247     x_return_status      OUT NOCOPY  VARCHAR2,
10248     x_error_number       OUT NOCOPY  NUMBER,
10249     x_error_message      OUT NOCOPY  VARCHAR2,
10250     p_fund_id              IN          NUMBER,
10251     p_ship_to_site_use_id  IN          NUMBER
10252 ) IS
10253    l_api_version   CONSTANT NUMBER       := 1.0;
10254    l_api_name      CONSTANT VARCHAR2(30) := 'adjust_target_for_acct_deleted';
10255    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
10256    l_return_status          VARCHAR2(1);
10257    l_object_version_number  NUMBER := 1;
10258    l_org_id                 NUMBER := TO_NUMBER(SUBSTRB(USERENV('CLIENT_INFO'), 1, 10));
10259    l_account_allocation_id  NUMBER;
10260    l_unalloc_acct_alloc_id  NUMBER;
10261 
10262  BEGIN
10263 
10264    -- Standard Start of API savepoint
10265    SAVEPOINT adjust_target_for_acct_deleted;
10266 
10267 
10268    -- Standard call to check for call compatibility.
10269    IF NOT FND_API.compatible_api_call(l_api_version,
10270                                       p_api_version,
10271                                       l_api_name,
10272                                       g_pkg_name)
10273    THEN
10274      RAISE FND_API.g_exc_unexpected_error;
10275    END IF;
10276 
10277    -- Initialize message list if p_init_msg_list is set to TRUE.
10278    IF FND_API.to_Boolean( p_init_msg_list ) THEN
10279       FND_MSG_PUB.initialize;
10280    END IF;
10281 
10282    -- Initialize API return status to SUCCESS
10283    x_return_status := FND_API.g_ret_sts_success;
10284 
10285    Ozf_Utility_pvt.write_conc_log('Private API: ' || l_full_api_name || ' -- start');
10286 
10287    IF p_fund_id IS NULL OR p_ship_to_site_use_id IS NULL
10288    THEN
10289       RAISE OZF_TP_INVALID_PARAM;
10290    END IF;
10291 
10292 
10293   BEGIN
10294 
10295   SELECT aa.account_allocation_id INTO l_account_allocation_id
10296   FROM ozf_account_allocations aa
10297   WHERE aa.allocation_for = 'FUND'
10298     and aa.allocation_for_id = p_fund_id
10299     and aa.site_use_code = 'SHIP_TO'
10300     and aa.site_use_id = p_ship_to_site_use_id;
10301 
10302 
10303   SELECT aa.account_allocation_id INTO l_unalloc_acct_alloc_id
10304   FROM ozf_account_allocations aa
10305   WHERE aa.allocation_for = 'FUND'
10306     and aa.allocation_for_id = p_fund_id
10307     and aa.site_use_id = -9999;
10308 
10309   EXCEPTION
10310    WHEN NO_DATA_FOUND THEN
10311        GOTO end_of_atfad;
10312   END;
10313 
10314    IF l_account_allocation_id IS NULL OR l_unalloc_acct_alloc_id IS NULL
10315    THEN
10316       RAISE OZF_TP_INVALID_PARAM;
10317    END IF;
10318 
10319 
10320 -- 1. ADJUSTING  UNALLOCATED Time records....
10321 
10322    BEGIN
10323    Ozf_Utility_pvt.write_conc_log(' - '||l_full_api_name|| ' - 1. ADJUSTING  UNALLOCATED Time records....');
10324 
10325   -- Increase the Unallocated for Current and Future Periods
10326   update  ozf_time_allocations tta
10327   set tta.target = tta.target + (
10328                                 SELECT ttb.target
10329                                 FROM ozf_time_allocations ttb
10330                                 WHERE ttb.allocation_for = 'CUST'
10331                                 AND ttb.allocation_for_id = l_account_allocation_id
10332                                 AND ttb.time_id = tta.time_id
10333                                 )
10334   where
10335         tta.allocation_for = 'CUST'
10336     AND tta.allocation_for_id = l_unalloc_acct_alloc_id
10337     and EXISTS
10338             (select 'x'
10339             from ozf_time_ent_period period
10340             where period.ent_period_id = tta.time_id
10341             and period.end_date >= trunc(sysdate)
10342             and tta.period_type_id = 32
10343             UNION
10344             select 'x'
10345             from ozf_time_ent_qtr qtr
10346             where qtr.ent_qtr_id = tta.time_id
10347             and qtr.end_date >= trunc(sysdate)
10348             and tta.period_type_id = 64
10349             );
10350    END;
10351 
10352 -- 1(b). ADJUSTING  UNALLOCATED Account record ....
10353    BEGIN
10354    Ozf_Utility_pvt.write_conc_log(' - '||l_full_api_name|| ' - 1(b). ADJUSTING  UNALLOCATED Account record....');
10355 
10356   -- Rollup the total targets to the unallocated records
10357   UPDATE OZF_ACCOUNT_ALLOCATIONS aa
10358   SET (aa.TARGET, aa.LYSP_SALES) = (
10359                   SELECT SUM(ti.TARGET), SUM(ti.lysp_sales)
10360                   FROM  OZF_TIME_ALLOCATIONS ti
10361                   WHERE  ti.ALLOCATION_FOR = 'CUST'
10362                   AND  ti.ALLOCATION_FOR_ID = aa.account_allocation_id
10363                  ),
10364     aa.object_version_number = aa.object_version_number + 1,
10365     aa.last_update_date = SYSDATE,
10366     aa.last_updated_by = FND_GLOBAL.USER_ID,
10367     aa.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
10368   WHERE aa.account_allocation_id = l_unalloc_acct_alloc_id;
10369   END;
10370 
10371 -- 2. ADJUSTING  SHIP TO  Time records. - Curr and Future Periods ...
10372 
10373    BEGIN
10374    Ozf_Utility_pvt.write_conc_log(' - '||l_full_api_name|| ' - 2. ADJUSTING SHIP TO Time records....');
10375 
10376   -- Past Targets stay with the shipTo
10377   -- Make ShipTos Current and Future Periods Targets ZERO
10378   update  ozf_time_allocations ttb
10379   set ttb.target = 0,
10380       ttb.account_status = 'D'
10381   WHERE ttb.allocation_for = 'CUST'
10382   AND ttb.allocation_for_id = l_account_allocation_id
10383   AND EXISTS
10384             (select 'x'
10385             from ozf_time_ent_period period
10386             where period.ent_period_id = ttb.time_id
10387             and period.end_date >= trunc(sysdate)
10388             and ttb.period_type_id = 32
10389             UNION
10390             select 'x'
10391             from ozf_time_ent_qtr qtr
10392             where qtr.ent_qtr_id = ttb.time_id
10393             and qtr.end_date >= trunc(sysdate)
10394             and ttb.period_type_id = 64
10395             );
10396    END;
10397 
10398 -- 2(b). ADJUSTING  SHIP TO  Account record ....
10399    BEGIN
10400    Ozf_Utility_pvt.write_conc_log(' - '||l_full_api_name|| ' - 2(b). ADJUSTING SHIP TO Account record....');
10401 
10402   -- Rollup the total targets to the shipTo record
10403   UPDATE OZF_ACCOUNT_ALLOCATIONS aa
10404   SET (aa.TARGET, aa.LYSP_SALES) = (
10405                   SELECT SUM(ti.TARGET), SUM(ti.lysp_sales)
10406                   FROM  OZF_TIME_ALLOCATIONS ti
10407                   WHERE  ti.ALLOCATION_FOR = 'CUST'
10408                   AND  ti.ALLOCATION_FOR_ID = aa.account_allocation_id
10409                  ),
10410     aa.account_status = 'D',
10411     aa.object_version_number = aa.object_version_number + 1,
10412     aa.last_update_date = SYSDATE,
10413     aa.last_updated_by = FND_GLOBAL.USER_ID,
10414     aa.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
10415   WHERE
10416     aa.account_allocation_id = l_account_allocation_id;
10417   END;
10418 
10419 -- 3. ADJUSTING  ShipTos PRODUCT SPREAD Time records ....
10420 
10421    BEGIN
10422    Ozf_Utility_pvt.write_conc_log(' - '||l_full_api_name|| ' - 3. ADJUSTING  ShipTos Product Spread Time Records....');
10423 
10424 -- CURRENT AND FUTURE TARGETS of this Shiptos products are made ZERO
10425     update  ozf_time_allocations tt
10426     set tt.target = 0,
10427         tt.account_status = 'D'
10428     WHERE tt.allocation_for = 'PROD'
10429       AND tt.allocation_for_id IN (
10430                                   SELECT pp.product_allocation_id
10431                                   FROM ozf_product_allocations pp
10432                                   WHERE pp.allocation_for = 'CUST'
10433                                     AND pp.allocation_for_id = l_account_allocation_id
10434                                   )
10435       and EXISTS
10436               (select 'x'
10437               from ozf_time_ent_period
10438               where ent_period_id = tt.time_id
10439               and end_date >= trunc(sysdate)
10440               and tt.period_type_id = 32
10441               UNION
10442               select 'x'
10443               from ozf_time_ent_qtr
10444               where ent_qtr_id = tt.time_id
10445               and end_date >= trunc(sysdate)
10446               and tt.period_type_id = 64
10447               );
10448    END;
10449 --  Unallocated is automatically shown in the header of the product spread
10450 
10451 -- 5. ADJUSTING  ShipTos PRODUCT SPREAD Header records ....
10452    BEGIN
10453    Ozf_Utility_pvt.write_conc_log(' - '||l_full_api_name|| ' - 5. ADJUSTING  ShipTos Product Spread Header Records....');
10454 
10455 -- Rollup the target numbers to the product record
10456    UPDATE OZF_PRODUCT_ALLOCATIONS p
10457    SET p.TARGET = (SELECT SUM(ti.TARGET)
10458                      FROM OZF_TIME_ALLOCATIONS ti
10459                     WHERE ti.ALLOCATION_FOR = 'PROD'
10460                       AND ti.ALLOCATION_FOR_ID = p.product_allocation_id),
10461        p.account_status = 'D',
10462        p.object_version_number = p.object_version_number + 1,
10463        p.last_update_date = SYSDATE,
10464        p.last_updated_by = FND_GLOBAL.USER_ID,
10465        p.last_update_login = FND_GLOBAL.CONC_LOGIN_ID
10466    WHERE p.product_allocation_id IN (
10467                                     SELECT pp.product_allocation_id
10468                                     FROM ozf_product_allocations pp
10469                                     WHERE pp.allocation_for = 'CUST'
10470                                       AND pp.allocation_for_id = l_account_allocation_id
10471                                     );
10472    END;
10473 
10474 
10475 
10476    <<end_of_atfad>>
10477 
10478    Ozf_Utility_pvt.write_conc_log('Private API: ' || l_full_api_name || ' -- end');
10479 
10480  EXCEPTION
10481 
10482      WHEN OZF_TP_INVALID_PARAM THEN
10483           ROLLBACK TO adjust_target_for_acct_deleted;
10484           FND_MESSAGE.set_name('OZF', 'OZF_TP_INVALID_PARAM_TXT');
10485           FND_MSG_PUB.add;
10486           x_return_status := FND_API.g_ret_sts_error ;
10487           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
10488                                      p_data    => x_error_message);
10489           Ozf_Utility_pvt.write_conc_log(l_full_api_name||' : INVALID PARAMETER EXCEPTION = '||sqlerrm(sqlcode));
10490           Ozf_Utility_pvt.write_conc_log(fnd_message.get_string('OZF', 'OZF_TP_INVALID_PARAM_TXT'));
10491 
10492      WHEN FND_API.G_EXC_ERROR THEN
10493           ROLLBACK TO adjust_target_for_acct_deleted;
10494           x_return_status := FND_API.g_ret_sts_error ;
10495           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
10496                                      p_data    => x_error_message);
10497 
10498      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10499           ROLLBACK TO adjust_target_for_acct_deleted;
10500           x_return_status := FND_API.g_ret_sts_unexp_error ;
10501           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
10502                                      p_data  => x_error_message);
10503 
10504      WHEN OTHERS THEN
10505           ROLLBACK TO adjust_target_for_acct_deleted;
10506 
10507           FND_MESSAGE.set_name('OZF', 'OZF_TP_OTHERS_ERROR_TXT');
10508           FND_MESSAGE.set_token('OZF_TP_SQLERRM_TOKEN',SQLERRM);
10509           FND_MESSAGE.set_token('OZF_TP_SQLCODE_TOKEN',SQLCODE);
10510           FND_MSG_PUB.add;
10511 
10512           x_return_status := FND_API.g_ret_sts_unexp_error ;
10513           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
10514              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
10515           END IF;
10516           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
10517                                      p_data  => x_error_message);
10518           Ozf_Utility_pvt.write_conc_log(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
10519 
10520  END adjust_target_for_acct_deleted;
10521 
10522 
10523 -- ------------------------
10524 -- ------------------------
10525 -- Public  Procedure
10526 -- ------------------------
10527 -- ------------------------------------------------------------------
10528 -- Name: ADJUST_ACCOUNT_TARGETS
10529 -- Desc: 1. Create new target allocation records,
10530 --          when an account is newly assigned to a territory
10531 --       2. Adjust old target allocation records,
10532 --          when an account is moved away from a territory
10533 -- History
10534 --   09-SEP-05       mkothari    created
10535 --
10536 -- -----------------------------------------------------------------
10537 PROCEDURE adjust_account_targets
10538 (
10539     x_error_number       OUT NOCOPY  NUMBER,
10540     x_error_message      OUT NOCOPY  VARCHAR2,
10541     p_terr_id            IN          NUMBER := NULL
10542 ) IS
10543    p_api_version             CONSTANT NUMBER       := 1.0;
10544    x_return_status           VARCHAR2(1) ;
10545    l_init_msg_list           VARCHAR2(10)  := FND_API.G_FALSE;
10546    l_api_version   CONSTANT NUMBER       := 1.0;
10547    l_api_name      CONSTANT VARCHAR2(30) := 'adjust_account_targets';
10548    l_full_api_name CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
10549    l_object_version_number  NUMBER := 1;
10550    l_org_id                 NUMBER := TO_NUMBER(SUBSTRB(USERENV('CLIENT_INFO'), 1, 10));
10551    l_corr_quota_id          NUMBER;
10552    l_node_id                NUMBER;
10553    l_previous_fact_id       NUMBER;
10554    l_activity_metric_fact_id  NUMBER;
10555 
10556 CURSOR terr_list_csr
10557 IS
10558 SELECT DISTINCT
10559  FF.NODE_ID
10560 FROM OZF_FUNDS_ALL_b FF
10561 WHERE
10562       FF.FUND_TYPE = 'QUOTA'
10563   AND FF.STATUS_CODE <> 'CANCELLED'
10564   AND FF.NODE_ID = NVL(p_terr_id, FF.NODE_ID)
10565   AND EXISTS
10566        (SELECT 'x'
10567         FROM OZF_ACCOUNT_ALLOCATIONS AA
10568         WHERE AA.ALLOCATION_FOR = 'FUND'
10569           AND AA.ALLOCATION_FOR_ID = FF.FUND_ID
10570        );
10571 
10572 
10573 /*
10574 
10575 -- Use following cursor later ------------------------------------------------------
10576 -- also change terr denorm such that adjust_acct is called only from LOAD_ procedure
10577 ------------------------------------------------------------------------------------
10578 
10579 SELECT DISTINCT FF.NODE_ID
10580 FROM OZF_FUNDS_ALL_b FF
10581 WHERE
10582       FF.FUND_TYPE = 'QUOTA'
10583   AND FF.STATUS_CODE <> 'CANCELLED'
10584   AND FF.NODE_ID = NVL(p_terr_id, FF.NODE_ID)
10585   AND EXISTS
10586        (SELECT 'x'
10587         FROM OZF_ACCOUNT_ALLOCATIONS AA
10588         WHERE AA.ALLOCATION_FOR = 'FUND'
10589           AND AA.ALLOCATION_FOR_ID = FF.FUND_ID
10590        )
10591 ----------
10592 UNION ALL
10593 ----------
10594 SELECT DISTINCT FF.NODE_ID
10595 FROM OZF_FUNDS_ALL_b FF
10596 WHERE
10597       FF.FUND_TYPE = 'QUOTA'
10598   AND FF.STATUS_CODE <> 'CANCELLED'
10599   AND EXISTS
10600        (SELECT 'x'
10601         FROM OZF_ACCOUNT_ALLOCATIONS AA
10602         WHERE AA.ALLOCATION_FOR = 'FUND'
10603           AND AA.ALLOCATION_FOR_ID = FF.FUND_ID
10604        )
10605 AND FF.NODE_ID IN
10606 (
10607 select  distinct node_id
10608 from    ozf_funds_all_b outer
10609 where   not exists (select 'x' from ozf_funds_all_b inner where inner.parent_fund_id = outer.fund_id)
10610 connect by prior fund_id = parent_fund_id
10611 start with parent_fund_id = (select inner2.fund_id
10612                              from ozf_funds_all_b inner2
10613                             where inner2.node_id = p_terr_id -- 3104
10614                               and inner2.fund_type = 'QUOTA'
10615                               and inner2.status_code <> 'CANCELLED'
10616                               and rownum = 1
10617                             )
10618 );
10619 
10620 Very Important Note:
10621 Make sure that the first terr_node which is processed is the one
10622 where accounts are added and not the one which is loosing the accounts
10623 Otherwise, new added accounts will have zero targets. other stuff will work fine.
10624 -------------------------------------------------------------
10625 */
10626 
10627 
10628 
10629 
10630 
10631 
10632 CURSOR added_accounts_csr (l_territory_id    number)
10633 IS
10634 SELECT
10635     'A'                                                       account_status_code,
10636     ---a.cust_account_id                                        cust_account_id,
10637     a.site_use_id                                            site_use_id
10638     ---a.site_use_code                                          site_use_code,
10639     ---OZF_LOCATION_PVT.get_location_id(a.site_use_id)          location_id,
10640     ---NVL(a.bill_to_site_use_id, -9996)                        bill_to_site_use_id,
10641     ---OZF_LOCATION_PVT.get_location_id(a.bill_to_site_use_id)  bill_to_location_id,
10642     ---a.party_id                                               parent_party_id,
10643     ---NVL(a.rollup_party_id, a.party_id)                       rollup_party_id
10644   FROM
10645     ams_party_market_segments a
10646   WHERE
10647        a.market_qualifier_reference = l_territory_id
10648    AND a.market_qualifier_type='TERRITORY'
10649    AND a.site_use_code = 'SHIP_TO'
10650    AND a.party_id IS NOT NULL
10651    AND a.site_use_id IS NOT NULL
10652  ------------
10653   MINUS
10654  ------------
10655    SELECT
10656     'A'                                                       account_status_code,
10657     ---a.cust_account_id                                        cust_account_id,
10658     a.site_use_id                                            site_use_id
10659     ---a.site_use_code                                          site_use_code,
10660     ---OZF_LOCATION_PVT.get_location_id(a.site_use_id)          location_id,
10661     ---NVL(a.bill_to_site_use_id, -9996)                        bill_to_site_use_id,
10662     ---OZF_LOCATION_PVT.get_location_id(a.bill_to_site_use_id)  bill_to_location_id,
10663     ---a.party_id                                               parent_party_id,
10664     ---NVL(a.rollup_party_id, a.party_id)                       rollup_party_id
10665   FROM
10666     ozf_party_market_segments_t a
10667   WHERE
10668        a.market_qualifier_reference = l_territory_id
10669    AND a.market_qualifier_type='TERRITORY'
10670    AND a.site_use_code = 'SHIP_TO'
10671    AND a.party_id IS NOT NULL
10672    AND a.site_use_id IS NOT NULL;
10673 
10674 
10675  CURSOR deleted_accounts_csr (l_territory_id    number)
10676  IS
10677  SELECT
10678     'D'                                                       account_status_code,
10679     ---a.cust_account_id                                        cust_account_id,
10680     a.site_use_id                                            site_use_id
10681     ---a.site_use_code                                          site_use_code,
10682     ---OZF_LOCATION_PVT.get_location_id(a.site_use_id)          location_id,
10683     ---NVL(a.bill_to_site_use_id, -9996)                        bill_to_site_use_id,
10684     ---OZF_LOCATION_PVT.get_location_id(a.bill_to_site_use_id)  bill_to_location_id,
10685     ---a.party_id                                               parent_party_id,
10686     ---NVL(a.rollup_party_id, a.party_id)                       rollup_party_id
10687   FROM
10688     ozf_party_market_segments_t a
10689   WHERE
10690        a.market_qualifier_reference = l_territory_id
10691    AND a.market_qualifier_type='TERRITORY'
10692    AND a.site_use_code = 'SHIP_TO'
10693    AND a.party_id IS NOT NULL
10694    AND a.site_use_id IS NOT NULL
10695  ------------
10696   MINUS
10697  ------------
10698    SELECT
10699     'D'                                                       account_status_code,
10700     ---a.cust_account_id                                        cust_account_id,
10701     a.site_use_id                                            site_use_id
10702     ---a.site_use_code                                          site_use_code,
10703     ---OZF_LOCATION_PVT.get_location_id(a.site_use_id)          location_id,
10704     ---NVL(a.bill_to_site_use_id, -9996)                        bill_to_site_use_id,
10705     ---OZF_LOCATION_PVT.get_location_id(a.bill_to_site_use_id)  bill_to_location_id,
10706     ---a.party_id                                               parent_party_id,
10707     ---NVL(a.rollup_party_id, a.party_id)                       rollup_party_id
10708   FROM
10709     ams_party_market_segments a
10710   WHERE
10711        a.market_qualifier_reference = l_territory_id
10712    AND a.market_qualifier_type='TERRITORY'
10713    AND a.site_use_code = 'SHIP_TO'
10714    AND a.party_id IS NOT NULL
10715    AND a.site_use_id IS NOT NULL;
10716 
10717 
10718 CURSOR add_quota_list_csr (l_terr_id NUMBER, l_ship_to_id NUMBER) IS
10719 SELECT
10720  FF.FUND_ID
10721 FROM OZF_FUNDS_ALL_b FF
10722 WHERE
10723       FF.FUND_TYPE = 'QUOTA'
10724   AND FF.STATUS_CODE <> 'CANCELLED'
10725   AND FF.NODE_ID = l_terr_id
10726   AND EXISTS
10727        (SELECT 'x'
10728         FROM OZF_ACCOUNT_ALLOCATIONS AA
10729         WHERE AA.ALLOCATION_FOR = 'FUND'
10730           AND AA.ALLOCATION_FOR_ID = FF.FUND_ID
10731           AND AA.site_use_id <> l_ship_to_id -- newly added to the FUND
10732        );
10733 
10734 CURSOR del_quota_list_csr (l_terr_id NUMBER, l_ship_to_id NUMBER) IS
10735 SELECT
10736  FF.FUND_ID
10737 FROM OZF_FUNDS_ALL_b FF
10738 WHERE
10739       FF.FUND_TYPE = 'QUOTA'
10740   AND FF.STATUS_CODE <> 'CANCELLED'
10741   AND FF.NODE_ID = l_terr_id
10742   AND EXISTS
10743        (SELECT 'x'
10744         FROM OZF_ACCOUNT_ALLOCATIONS AA
10745         WHERE AA.ALLOCATION_FOR = 'FUND'
10746           AND AA.ALLOCATION_FOR_ID = FF.FUND_ID
10747           AND AA.site_use_id = l_ship_to_id -- the one deleted from the FUND targets
10748        );
10749 
10750 CURSOR allocation_list_csr (l_fund_id NUMBER)
10751 IS
10752 SELECT
10753 a.activity_metric_id,
10754 a.level_depth,
10755 a.node_id,
10756 a.previous_fact_id,
10757 a.activity_metric_fact_id
10758 from ozf_act_metric_facts_all a
10759 where a.arc_act_metric_used_by = 'FUND'
10760 and a.act_metric_used_by_id = l_fund_id
10761 order by a.activity_metric_id desc;
10762 
10763 
10764 CURSOR corr_quota_csr
10765       (l_allocation_id        NUMBER,
10766        l_ship_to_site_use_id  NUMBER
10767       )
10768 IS
10769 SELECT
10770 a.act_metric_used_by_id,
10771 a.node_id,
10772 a.previous_fact_id,
10773 a.activity_metric_fact_id
10774 from ozf_act_metric_facts_all a, OZF_FUNDS_ALL_b FF
10775 where a.arc_act_metric_used_by = 'FUND'
10776 and a.activity_metric_id = l_allocation_id
10777 --and a.level_depth = l_level_depth
10778 and a.node_id = FF.NODE_ID
10779 AND FF.FUND_TYPE = 'QUOTA'
10780 AND FF.STATUS_CODE <> 'CANCELLED'
10781 AND FF.FUND_ID =  a.act_metric_used_by_id
10782 AND EXISTS
10783      (SELECT 'x'
10784       FROM OZF_ACCOUNT_ALLOCATIONS AA
10785       WHERE AA.ALLOCATION_FOR = 'FUND'
10786         AND AA.ALLOCATION_FOR_ID = FF.FUND_ID
10787         AND AA.site_use_id = l_ship_to_site_use_id -- new added
10788      )
10789 AND EXISTS
10790 (SELECT  'x'
10791  FROM
10792  (
10793  SELECT
10794     'D' account_status_code,
10795     a.market_qualifier_reference territory_id,
10796     a.site_use_id site_use_id
10797   FROM
10798     ozf_party_market_segments_t a
10799   WHERE
10800        a.market_qualifier_type='TERRITORY'
10801    AND a.site_use_code = 'SHIP_TO'
10802    AND a.party_id IS NOT NULL
10803    AND a.site_use_id = l_ship_to_site_use_id
10804  ------------
10805   MINUS
10806  ------------
10807    SELECT
10808     'D' account_status_code,
10809     a.market_qualifier_reference territory_id,
10810     a.site_use_id site_use_id
10811   FROM
10812     ams_party_market_segments a
10813   WHERE
10814        a.market_qualifier_type='TERRITORY'
10815    AND a.site_use_code = 'SHIP_TO'
10816    AND a.party_id IS NOT NULL
10817    AND a.site_use_id = l_ship_to_site_use_id
10818  )
10819 );
10820 
10821  BEGIN
10822 
10823    -- Standard Start of API savepoint
10824    SAVEPOINT adjust_account_targets;
10825 
10826    -- Standard call to check for call compatibility.
10827    IF NOT FND_API.compatible_api_call(l_api_version,
10828                                       p_api_version,
10829                                       l_api_name,
10830                                       g_pkg_name)
10831    THEN
10832      RAISE FND_API.g_exc_unexpected_error;
10833    END IF;
10834 
10835    -- Initialize message list if l_init_msg_list is set to TRUE.
10836    IF FND_API.to_Boolean( l_init_msg_list ) THEN
10837       FND_MSG_PUB.initialize;
10838    END IF;
10839 
10840    -- Initialize API return status to SUCCESS
10841    x_return_status := FND_API.g_ret_sts_success;
10842 
10843    Ozf_Utility_pvt.write_conc_log('Private API: ' || l_full_api_name || ' -- start');
10844 
10845    FOR terr IN terr_list_csr
10846    LOOP
10847     ---1. PROCESS ADDITION OF ACCOUNTS
10848      FOR new_accounts IN added_accounts_csr (terr.node_id)
10849      LOOP
10850 
10851        FOR quota in add_quota_list_csr(terr.node_id, new_accounts.site_use_id)
10852        LOOP
10853 
10854          FOR quota_allocation in allocation_list_csr (quota.fund_id)
10855          LOOP
10856 
10857            IF G_DEBUG_LEVEL THEN
10858             Ozf_Utility_pvt.write_conc_log('- '||l_full_api_name||'- Process Added Accounts for Quota Id =>'||quota.fund_id);
10859            END IF;
10860            -- FIND the corr_quota, such that there exists a corr_terr which lost that ship_to
10861            OPEN corr_quota_csr (quota_allocation.activity_metric_id, new_accounts.site_use_id);
10862            FETCH corr_quota_csr INTO l_corr_quota_id, l_node_id, l_previous_fact_id, l_activity_metric_fact_id;
10863            CLOSE corr_quota_csr;
10864 
10865 --        -> Create account_allocation records for new ship_to based upon the old quota id
10866           adjust_target_for_acct_added (
10867                                       p_api_version        => p_api_version,
10868                                       x_return_status      => x_return_status,
10869                                       x_error_number       => x_error_number,
10870                                       x_error_message      => x_error_message,
10871                                       p_fund_id            => quota.fund_id,
10872                                       p_corr_fund_id       => l_corr_quota_id,
10873                                       p_terr_id            => terr.node_id,
10874                                       p_ship_to_site_use_id => new_accounts.site_use_id
10875                                       );
10876 
10877            IF    x_return_status = FND_API.g_ret_sts_error
10878            THEN
10879                  RAISE FND_API.g_exc_error;
10880            ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
10881                  RAISE FND_API.g_exc_unexpected_error;
10882            END IF;
10883 
10884          END LOOP; -- allocation_list_csr(quota.fund_id)
10885 
10886        END LOOP; -- add_quota_list_csr(terr.node_id, new_accounts.site_use_id)
10887 
10888      END LOOP; --add_quota_list_csr(terr.node_id, new_accounts.site_use_id)
10889 
10890 
10891     --2. PROCESS DELETION OF ACCOUNTS
10892 
10893      FOR del_accounts IN deleted_accounts_csr (terr.node_id)
10894      LOOP
10895 
10896        FOR quota IN del_quota_list_csr(terr.node_id, del_accounts.site_use_id)
10897        LOOP
10898          IF G_DEBUG_LEVEL THEN
10899            Ozf_Utility_pvt.write_conc_log('- '||l_full_api_name||'- Process Deleted Accounts for Site Use Id   =>'||del_accounts.site_use_id);
10900          END IF;
10901          adjust_target_for_acct_deleted (
10902                                       p_api_version        => p_api_version,
10903                                       x_return_status      => x_return_status,
10904                                       x_error_number       => x_error_number,
10905                                       x_error_message      => x_error_message,
10906                                       p_fund_id            => quota.fund_id,
10907                                       p_ship_to_site_use_id => del_accounts.site_use_id
10908                                       );
10909 
10910          IF    x_return_status = FND_API.g_ret_sts_error
10911          THEN
10912                RAISE FND_API.g_exc_error;
10913          ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
10914                RAISE FND_API.g_exc_unexpected_error;
10915          END IF;
10916 
10917        END LOOP; -- del_quota_list_csr(terr.node_id, del_accounts.site_use_id)
10918 
10919      END LOOP; -- deleted_accounts_csr (terr.node_id)
10920 
10921 -- 3. UPDATE ACCOUNT ALLOCATION account details based upon latest information from Terr (i.e from TCA )
10922      IF G_DEBUG_LEVEL THEN
10923         Ozf_Utility_pvt.write_conc_log('- '||l_full_api_name||'- UPDATE ACCOUNT ALLOCATION Table''s account details based upon latest information from Terr (i.e from TCA).');
10924      END IF;
10925 
10926 
10927 --- Jan 5th, 2005
10928 --- Uncomment following later so that TCA's latest shipto-partyid info can be updated in TP allocation tables
10929 /*
10930     UPDATE
10931       (SELECT
10932           FF.NODE_ID territory_id,
10933           aa.account_allocation_id,
10934           aa.site_use_id,
10935           aa.site_use_code,
10936           aa.cust_account_id,
10937           aa.location_id,
10938           aa.bill_to_site_use_id,
10939           aa.bill_to_location_id,
10940           aa.parent_party_id,
10941           aa.rollup_party_id
10942       FROM ozf_account_allocations aa, ozf_funds_all_b ff
10943       WHERE FF.FUND_TYPE = 'QUOTA'
10944         AND FF.STATUS_CODE <> 'CANCELLED'
10945         AND aa.allocation_for = 'FUND'
10946         AND aa.allocation_for_id = FF.FUND_ID
10947         AND FF.NODE_ID = terr.node_id
10948         AND aa.parent_party_id IS NOT NULL
10949         AND aa.parent_party_id > 0
10950       ) alloc
10951     SET
10952       (
10953         alloc.cust_account_id,
10954         alloc.location_id,
10955         alloc.bill_to_site_use_id,
10956         alloc.bill_to_location_id,
10957         alloc.parent_party_id,
10958         alloc.rollup_party_id
10959       )
10960       = (
10961          SELECT
10962             a.cust_account_id                                        cust_account_id,
10963             OZF_LOCATION_PVT.get_location_id(a.site_use_id)          location_id,
10964             NVL(a.bill_to_site_use_id, -9996)                        bill_to_site_use_id,
10965             OZF_LOCATION_PVT.get_location_id(a.bill_to_site_use_id)  bill_to_location_id,
10966             a.party_id                                               parent_party_id,
10967             NVL(a.rollup_party_id, a.party_id)                       rollup_party_id
10968          FROM
10969             ams_party_market_segments a
10970          WHERE
10971             a.market_qualifier_reference = alloc.territory_id
10972             AND a.market_qualifier_reference = terr.node_id
10973             AND a.market_qualifier_type='TERRITORY'
10974             AND a.site_use_code = 'SHIP_TO'
10975             AND a.party_id IS NOT NULL
10976             AND a.site_use_id IS NOT NULL
10977             AND a.site_use_id = alloc.site_use_id
10978             AND a.site_use_code = alloc.site_use_code
10979         );
10980 
10981 */
10982 
10983 
10984    END LOOP; -- terr_list_csr
10985 
10986    Ozf_Utility_pvt.write_conc_log('Private API: ' || l_full_api_name || ' -- end');
10987 
10988  EXCEPTION
10989 
10990      WHEN FND_API.G_EXC_ERROR THEN
10991           ROLLBACK TO adjust_account_targets;
10992           x_return_status := FND_API.g_ret_sts_error ;
10993           FND_MSG_PUB.count_and_get (p_count   => x_error_number,
10994                                      p_data    => x_error_message);
10995 
10996      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10997           ROLLBACK TO adjust_account_targets;
10998           x_return_status := FND_API.g_ret_sts_unexp_error ;
10999           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
11000                                      p_data  => x_error_message);
11001 
11002      WHEN OTHERS THEN
11003           ROLLBACK TO adjust_account_targets;
11004 
11005           FND_MESSAGE.set_name('OZF', 'OZF_TP_OTHERS_ERROR_TXT');
11006           FND_MESSAGE.set_token('OZF_TP_SQLERRM_TOKEN',SQLERRM);
11007           FND_MESSAGE.set_token('OZF_TP_SQLCODE_TOKEN',SQLCODE);
11008           FND_MSG_PUB.add;
11009 
11010           x_return_status := FND_API.g_ret_sts_unexp_error ;
11011           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
11012              FND_MSG_PUB.Add_Exc_Msg (g_pkg_name, l_api_name);
11013           END IF;
11014           FND_MSG_PUB.Count_And_Get (p_count => x_error_number,
11015                                      p_data  => x_error_message);
11016           Ozf_Utility_pvt.write_conc_log(l_full_api_name||' : OTHERS EXCEPTION = '||sqlerrm(sqlcode));
11017 
11018  END adjust_account_targets;
11019 
11020 
11021 ----------------------------------------------------------------------------------------------------------
11022 ----------------------------------------------------------------------------------------------------------
11023 
11024 END OZF_ALLOCATION_ENGINE_PVT;