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