DBA Data[Home] [Help]

PACKAGE BODY: APPS.RLM_CUM_SV

Source


1 PACKAGE BODY RLM_CUM_SV AS
2 /* $Header: RLMCUMMB.pls 120.5.12010000.2 2008/07/30 12:54:08 sunilku ship $ */
3 
4 --
5 l_DEBUG NUMBER := NVL(fnd_profile.value('RLM_DEBUG_MODE'),-1);
6 --
7 /*=============================== rlm_cum_sv ===============================*/
8 
9 /*============================================================================
10 
11   PROCEDURE NAME:	CalculateCumKey
12 
13 =============================================================================*/
14 
15  PROCEDURE CalculateCumKey (
16         x_cum_key_record IN     RLM_CUM_SV.cum_key_attrib_rec_type,
17         x_cum_record     IN OUT NOCOPY RLM_CUM_SV.cum_rec_type)
18  IS
19 	v_rlm_setup_terms_record	rlm_setup_terms_sv.setup_terms_rec_typ;
20 	v_cum_control_code	 	rlm_cust_shipto_terms.cum_control_code%TYPE;
21 	v_cum_org_level_code	 	rlm_cust_shipto_terms.cum_org_level_code%TYPE;
22 	v_address_id		 	NUMBER;
23 	v_terms_level		 	VARCHAR2(20) DEFAULT NULL;
24 	v_setup_terms_status	 	BOOLEAN;
25 	v_start_date_num          	NUMBER;
26 	v_record_year_num         	NUMBER;
27 	v_setup_terms_msg		VARCHAR2(2000);
28 
29      /* These are variables used as switches when finding cum key ids.
30 	Initially, all the switches are turned OFF by setting each to NULL */
31 	p_ship_from_org_id		NUMBER DEFAULT NULL;
32 	p_ship_to_address_id		NUMBER DEFAULT NULL;
33 	p_intrmd_ship_to_address_id	NUMBER DEFAULT NULL;
34 	p_bill_to_address_id		NUMBER DEFAULT NULL;
35 	p_customer_item_id		NUMBER DEFAULT NULL;
36 	p_purchase_order_number 	RLM_CUST_ITEM_CUM_KEYS.PURCHASE_ORDER_NUMBER%TYPE DEFAULT NULL;
37 	p_cust_record_year		RLM_CUST_ITEM_CUM_KEYS.CUST_RECORD_YEAR%TYPE DEFAULT NULL;
38 	p_cum_start_date		DATE   DEFAULT NULL;
39 	E_UNEXPECTED    		EXCEPTION;
40 	v_ship_from_org_name		VARCHAR2(250) DEFAULT NULL;
41 	v_customer_name			VARCHAR2(360) DEFAULT NULL;
42 	v_ship_to_location		VARCHAR2(250) DEFAULT NULL;
43 	v_customer_item_number		VARCHAR2(250) DEFAULT NULL;
44     --
45     v_OrgId                         NUMBER;
46     --
47  BEGIN
48    --
49    -- DEBUGGING - INPUT Values
50    --
51    IF (l_debug <> -1) THEN
52       rlm_core_sv.dpush(C_SDEBUG, 'CalculateCumKey');
53       rlm_core_sv.dlog(C_DEBUG, '');
54       rlm_core_sv.dlog(C_DEBUG, 'HERE ARE THE INPUT VALUES:');
55       rlm_core_sv.dlog(C_DEBUG, '--------------------------');
56       rlm_core_sv.dlog(C_DEBUG, 'ship_to_address_id', x_cum_key_record.ship_to_address_id);
57       rlm_core_sv.dlog(C_DEBUG, 'ship_from_org_id', x_cum_key_record.ship_from_org_id);
58       rlm_core_sv.dlog(C_DEBUG, 'bill_to_address_id', x_cum_key_record.bill_to_address_id);
59       rlm_core_sv.dlog(C_DEBUG, 'intrmd_ship_to_address_id', x_cum_key_record.intrmd_ship_to_address_id);
60       rlm_core_sv.dlog(C_DEBUG, 'customer_item_id', x_cum_key_record.customer_item_id);
61       rlm_core_sv.dlog(C_DEBUG, 'purchase_order_number', x_cum_key_record.purchase_order_number);
62       rlm_core_sv.dlog(C_DEBUG, 'cum_start_date', x_cum_key_record.cum_start_date);
63       rlm_core_sv.dlog(C_DEBUG, 'cust_record_year', x_cum_key_record.cust_record_year);
64       rlm_core_sv.dlog(C_DEBUG, 'create_cum_key', x_cum_key_record.create_cum_key_flag);
65       rlm_core_sv.dlog(C_DEBUG, 'actual_shipment_date', x_cum_record.actual_shipment_date);
66       rlm_core_sv.dlog(C_DEBUG, 'shipped_quantity', x_cum_record.shipped_quantity);
67    END IF;
68    --
69    -- Get the current Org ID and raise error if the current Org is not set
70    -- This piece of code has been added as a fail-safe mechanism, we
71    -- always expect the current org context to be set before this API
72    -- is called
73    --
74    v_OrgId := MO_GLOBAL.get_current_org_id;
75    --
76    IF (l_debug <> -1) THEN
77     rlm_core_sv.dlog(C_DEBUG, 'Current Org ID', v_OrgId);
78    END IF;
79    --
80    IF v_OrgID IS NULL THEN
81     --
82     x_cum_record.msg_name := 'RLM_OU_CONTEXT_NOT_SET';
83     rlm_message_sv.get_msg_text(
84                  x_message_name  => x_cum_record.msg_name,
85                  x_text          => x_cum_record.msg_data);
86     --
87     RAISE e_Unexpected;
88     --
89    END IF;
90    --
91 -- Determine whether to use Ship To or Intermediate Ship To
92    --
93    IF x_cum_key_record.intrmd_ship_to_address_id IS NULL THEN
94 	--
95 	IF x_cum_key_record.ship_to_address_id IS NULL THEN
96 		--
97                 x_cum_record.msg_name := 'RLM_CUM_ADDRESS_REQUIRED';
98 		rlm_message_sv.get_msg_text(
99 	  		x_message_name	=> x_cum_record.msg_name,
100 	  		x_text		=> x_cum_record.msg_data);
101 		RAISE E_UNEXPECTED;
102 		--
103 	ELSE
104 		--
105 		v_address_id := x_cum_key_record.ship_to_address_id;
106 		--
107 	END IF;
108 	--
109    ELSE
110 	--
111 	IF x_cum_key_record.ship_to_address_id IS NULL THEN
112 		--
113 		v_address_id := x_cum_key_record.intrmd_ship_to_address_id;
114 		--
115 	ELSE
116 		--
117 		v_address_id := x_cum_key_record.ship_to_address_id;
118 		--
119 	END IF;
120 	--
121    END IF;
122    --
123    IF (l_debug <> -1) THEN
124       rlm_core_sv.dlog(C_DEBUG, 'address_id', v_address_id);
125    END IF;
126    --
127 -- Get the CUM Control Code from the setup terms table
128    --
129    RLM_TPA_SV.get_setup_terms(
130 	x_ship_from_org_id 		=> x_cum_key_record.ship_from_org_id,
131 	x_customer_id 			=> x_cum_key_record.customer_id,
132 	x_ship_to_address_id 		=> v_address_id,
133 	x_customer_item_id		=> x_cum_key_record.customer_item_id,
134 	x_terms_definition_level 	=> v_terms_level,
135 	x_terms_rec			=> v_rlm_setup_terms_record,
136 	x_return_message		=> v_setup_terms_msg,
137 	x_return_status			=> v_setup_terms_status);
138    --
139    IF v_setup_terms_status = FALSE THEN
140 		--
141 		v_ship_from_org_name := RLM_CORE_SV.get_ship_from(x_cum_key_record.ship_from_org_id);
142 		v_customer_name := RLM_CORE_SV.get_customer_name(x_cum_key_record.customer_id);
143 		v_ship_to_location := RLM_CORE_SV.get_ship_to(v_address_id);
144 		v_customer_item_number := RLM_CORE_SV.get_item_number(x_cum_key_record.customer_item_id);
145 		--
146                 x_cum_record.msg_name := 'RLM_CUM_SETUP_TERMS_REQUIRED';
147                 --
148 		rlm_message_sv.get_msg_text(
149 	  			x_message_name	=> x_cum_record.msg_name,
150 	  			x_text		=> x_cum_record.msg_data,
151 				x_token1	=> 'SF',
152 				x_value1	=> v_ship_from_org_name,
153 				x_token2	=> 'CUST',
154 				x_value2	=> v_customer_name,
155 				x_token3	=> 'ST',
156 				x_value3	=> v_ship_to_location,
157 				x_token4	=> 'CI',
158 				x_value4	=> v_customer_item_number);
159 		--
160         	RAISE E_UNEXPECTED;
161 		--
162    ELSE
163 	--
164         IF v_rlm_setup_terms_record.cum_control_code IS NULL THEN
165 			--
166                         x_cum_record.msg_name := 'RLM_CUM_CTRLCD_REQUIRED';
167                         --
168 			rlm_message_sv.get_msg_text(
169 	  				x_message_name	=> x_cum_record.msg_name,
170 	  				x_text		=> x_cum_record.msg_data);
171 			--
172            		RAISE E_UNEXPECTED;
173 			--
174         ELSE
175 	   --
176 	   IF v_terms_level = 'CUSTOMER_ITEM' THEN
177 	      --
178               IF v_rlm_setup_terms_record.calc_cum_flag <> 'Y'
179                  OR v_rlm_setup_terms_record.calc_cum_flag IS NULL THEN
180 			--
181 			v_ship_from_org_name := RLM_CORE_SV.get_ship_from(x_cum_key_record.ship_from_org_id);
182 			v_customer_name := RLM_CORE_SV.get_customer_name(x_cum_key_record.customer_id);
183 			v_ship_to_location := RLM_CORE_SV.get_ship_to(v_address_id);
184 		v_customer_item_number := RLM_CORE_SV.get_item_number(x_cum_key_record.customer_item_id);
185 			--
186                         x_cum_record.msg_name := 'RLM_CUM_NO_CALC_FLAG';
187                         --
188 			rlm_message_sv.get_msg_text(
189 	  				x_message_name	=> x_cum_record.msg_name,
190 	  				x_text		=> x_cum_record.msg_data,
191 					x_token1	=> 'CI',
192 					x_value1	=> v_customer_item_number);
193 			--
194                 	RAISE E_UNEXPECTED;
195 			--
196               END IF;
197 	      --
198            END IF;
199 	   --
200         END IF;
201 	--
202    END IF;
203    --
204 -- CUM Organization Level
205    --
206    v_cum_org_level_code := v_rlm_setup_terms_record.cum_org_level_code;
207    --
208 /* Customer Item Id is always populated in the CUM Key table regardless of
209    CUM control code or CUM Organization Level. Ship From Organization Id
210    is always populated in the CUM Key table except for the case of
211    ALL_SHIP_FROMS. Therefore, the switches below are turned ON and
212    assigned to the associated parameters passed by the calling program */
213    --
214    p_customer_item_id := x_cum_key_record.customer_item_id;
215    p_ship_from_org_id := x_cum_key_record.ship_from_org_id;
216    --
217    IF v_rlm_setup_terms_record.cum_control_code <> 'NO_CUM' THEN
218      --
219      IF (x_cum_key_record.cum_start_date
220                                            = rlm_manage_demand_sv.K_DNULL)
221         AND (NVL(v_rlm_setup_terms_record.cum_control_code,'NO_CUM')
222         IN ('CUM_BY_DATE_PO','CUM_BY_DATE_RECORD_YEAR','CUM_BY_DATE_ONLY'))
223      THEN
224        --
225        rlm_cum_sv.GetLatestCum(x_cum_key_record,
226                     v_rlm_setup_terms_record,
227                     x_cum_record);
228        --
229        IF x_cum_record.record_return_status = FALSE THEN
230          --
231          fnd_file.put_line(fnd_file.log,x_cum_record.msg_data);
232          --
233        END IF;
234        --
235        IF (l_debug <> -1) THEN
236           rlm_core_sv.dpop(C_DEBUG,'CalculateCumKey');
237        END IF;
238        --
239        RETURN;
240        --
241      END IF;
242      --
243  --Turn ON switches based on cum control code
244      --
245      IF v_rlm_setup_terms_record.cum_control_code = 'CUM_BY_PO_ONLY' THEN
246 	  --
247 	  IF (x_cum_key_record.purchase_order_number IS NULL) THEN
248 			--
249                         x_cum_record.msg_name := 'RLM_CUM_PO_REQUIRED';
250                         --
251 			rlm_message_sv.get_msg_text(
252 	  				x_message_name	=> x_cum_record.msg_name,
253 	  				x_text		=> x_cum_record.msg_data);
254 			--
255                    	RAISE E_UNEXPECTED;
256 			--
257 	  END IF;
258 		--
259 		p_purchase_order_number := x_cum_key_record.purchase_order_number;
260 		--
261      ELSIF v_rlm_setup_terms_record.cum_control_code = 'CUM_BY_DATE_ONLY'
262 	   OR v_rlm_setup_terms_record.cum_control_code = 'CUM_UNTIL_MANUAL_RESET' THEN
263 	--
264 	IF x_cum_key_record.cum_start_date IS NULL THEN
265 	--
266                   x_cum_record.msg_name := 'RLM_CUM_START_DT_REQUIRED';
267                   --
268                   rlm_message_sv.get_msg_text(
269 	  		x_message_name	=> x_cum_record.msg_name,
270 	  		x_text		=> x_cum_record.msg_data);
271                   --
272                 	RAISE E_UNEXPECTED;
273 			--
274 		END IF;
275 		--
276 		p_cum_start_date := x_cum_key_record.cum_start_date;
277 		--
278 	--
279      ELSIF v_rlm_setup_terms_record.cum_control_code = 'CUM_BY_DATE_RECORD_YEAR' THEN
280 	--
281         IF (x_cum_key_record.cum_start_date IS NULL)
282             OR (x_cum_key_record.cust_record_year IS NULL) THEN
283 		--
284                 x_cum_record.msg_name := 'RLM_CUM_REC_YEAR_REQUIRED';
285                 --
286 		rlm_message_sv.get_msg_text(
287   				x_message_name	=> x_cum_record.msg_name,
288   				x_text		=> x_cum_record.msg_data);
289 		--
290                 RAISE E_UNEXPECTED;
291 		--
292         END IF;
293 	--
294         p_cum_start_date := x_cum_key_record.cum_start_date;
295 	p_cust_record_year := x_cum_key_record.cust_record_year;
296 	--
297      ELSIF v_rlm_setup_terms_record.cum_control_code = 'CUM_BY_DATE_PO' THEN
298 	--
299 	IF (x_cum_key_record.cum_start_date IS NULL)
300             OR (x_cum_key_record.purchase_order_number IS NULL) THEN
301 		--
302                 x_cum_record.msg_name := 'RLM_CUM_DATE_PO_REQUIRED';
303                 --
304 		rlm_message_sv.get_msg_text(
305   				x_message_name	=> x_cum_record.msg_name,
306   				x_text		=> x_cum_record.msg_data);
307 		--
308             	RAISE E_UNEXPECTED;
309 		--
310 	END IF;
311 	--
312 	p_purchase_order_number := x_cum_key_record.purchase_order_number;
313         p_cum_start_date := x_cum_key_record.cum_start_date;
314 	--
315      ELSE
316 			--
317                         x_cum_record.msg_name := 'RLM_CUM_DATE_PO_REQUIRED';
318                         --
319 			rlm_message_sv.get_msg_text(
320 	  				x_message_name	=> x_cum_record.msg_name,
321 	  				x_text		=> x_cum_record.msg_data);
322 			--
323 	                RAISE E_UNEXPECTED;
324 			--
325      END IF;
326      --
327    ELSE
328 		--
329 		v_ship_from_org_name := RLM_CORE_SV.get_ship_from(x_cum_key_record.ship_from_org_id);
330 		v_customer_name := RLM_CORE_SV.get_customer_name(x_cum_key_record.customer_id);
331 		v_ship_to_location := RLM_CORE_SV.get_ship_to(v_address_id);
332 		v_customer_item_number := RLM_CORE_SV.get_item_number(x_cum_key_record.customer_item_id);
333 		--
334                 x_cum_record.msg_name := 'RLM_CUM_NO_CUM_CALC';
335                 --
336 		rlm_message_sv.get_msg_text(
337 	  			x_message_name	=> x_cum_record.msg_name,
338 	  			x_text		=> x_cum_record.msg_data,
339 				x_token1	=> 'CUST',
340 				x_value1	=> v_customer_name,
341 				x_token2	=> 'ST',
342 				x_value2	=> v_ship_to_location);
343 		--
344 	        RAISE E_UNEXPECTED;
345 		--
346    END IF;
347    --
348    IF (l_debug <> -1) THEN
349       rlm_core_sv.dlog(C_DEBUG, 'cum_control_code', v_rlm_setup_terms_record.cum_control_code);
350       rlm_core_sv.dlog(C_DEBUG, 'cum_org_level_code', v_cum_org_level_code);
351       rlm_core_sv.dlog(C_DEBUG, ' ');
352       rlm_core_sv.dlog(C_DEBUG, 'The setup terms were retrieved at this level: ', v_terms_level);
353    END IF;
354    --
355 -- Turn ON/OFF switches based on cum organization level code
356    --
357    IF v_cum_org_level_code = 'BILL_TO_SHIP_FROM' THEN
358 		--
359 		IF x_cum_key_record.bill_to_address_id IS NULL THEN
360 		       --
361                        x_cum_record.msg_name := 'RLM_CUM_BILL_TO_REQUIRED';
362                        --
363 		       rlm_message_sv.get_msg_text(
364 	  				x_message_name	=> x_cum_record.msg_name,
365 	  				x_text		=> x_cum_record.msg_data);
366 		       --
367                        RAISE E_UNEXPECTED;
368 		       --
369 		END IF;
370 		--
371 		p_bill_to_address_id := x_cum_key_record.bill_to_address_id;
372 		--
373    ELSIF v_cum_org_level_code = 'SHIP_TO_SHIP_FROM' THEN
374 		--
375 		IF x_cum_key_record.ship_to_address_id IS NULL THEN
376 		       --
377                        x_cum_record.msg_name := 'RLM_CUM_SHIP_TO_REQUIRED';
378                        --
379 		       rlm_message_sv.get_msg_text(
380 	  				x_message_name	=> x_cum_record.msg_name,
381 	  				x_text		=> x_cum_record.msg_data);
382 		       --
383                        RAISE E_UNEXPECTED;
384 		       --
385 		END IF;
386 		--
387 	        p_ship_to_address_id := x_cum_key_record.ship_to_address_id;
388 		--
389    ELSIF v_cum_org_level_code = 'INTRMD_SHIP_TO_SHIP_FROM' THEN
390 		--
391 		IF x_cum_key_record.intrmd_ship_to_address_id IS NULL THEN
392 		       --
393                        x_cum_record.msg_name := 'RLM_CUM_INTER_SHIP_TO_REQUIRED';
394                        --
395 		       rlm_message_sv.get_msg_text(
396 	  				x_message_name	=> x_cum_record.msg_name,
397 	  				x_text		=> x_cum_record.msg_data);
398 		       --
399                        RAISE E_UNEXPECTED;
400 		       --
401 		END IF;
402 		--
403 	        p_intrmd_ship_to_address_id := x_cum_key_record.intrmd_ship_to_address_id;
404    ELSIF v_cum_org_level_code = 'SHIP_TO_ALL_SHIP_FROMS' THEN
405 		p_ship_to_address_id := x_cum_key_record.ship_to_address_id;
406    		p_ship_from_org_id := NULL;
407 		--
408    ELSE
409 		       --
410                        x_cum_record.msg_name := 'RLM_CUM_UNKNOWN_ORG_LEVEL';
411                        --
412 		       rlm_message_sv.get_msg_text(
413 	  				x_message_name	=> x_cum_record.msg_name,
414 	  				x_text		=> x_cum_record.msg_data);
415 		       --
416 	               RAISE E_UNEXPECTED;
417 		       --
418    END IF;
419    --
420    IF (l_debug <> -1) THEN
421       rlm_core_sv.dlog(C_DEBUG, 'cust_record_year', x_cum_key_record.cust_record_year);
422       rlm_core_sv.dlog(C_DEBUG, '');
423       rlm_core_sv.dlog(C_DEBUG, 'ATTRIBUTES ACTUALLY USED TO FIND/CREATE THE KEY:');
424       rlm_core_sv.dlog(C_DEBUG, '------------------------------------------------');
425       rlm_core_sv.dlog(C_DEBUG, 'p_ship_from_org_id', p_ship_from_org_id);
426       rlm_core_sv.dlog(C_DEBUG, 'p_ship_to_address_id', p_ship_to_address_id);
427       rlm_core_sv.dlog(C_DEBUG, 'p_intrmd_ship_to_address_id', p_intrmd_ship_to_address_id);
428       rlm_core_sv.dlog(C_DEBUG, 'p_bill_to_address_id', p_bill_to_address_id);
429       rlm_core_sv.dlog(C_DEBUG, 'p_customer_item_id', p_customer_item_id);
430       rlm_core_sv.dlog(C_DEBUG, 'p_purchase_order_number', p_purchase_order_number);
431       rlm_core_sv.dlog(C_DEBUG, 'p_cust_record_year', p_cust_record_year);
432       rlm_core_sv.dlog(C_DEBUG, 'p_cum_start_date', p_cum_start_date);
433    END IF;
434    --
435    --Find the cum key id using the CUM RULES
436    --
437    IF v_cum_org_level_code <> 'SHIP_TO_ALL_SHIP_FROMS'THEN
438    --
439       	SELECT 	cum_key_id,
440 	    	cum_qty,
441      		cum_qty_to_be_accumulated,
442 		cum_qty_after_cutoff,
443      		last_cum_qty_update_date,
444      		cust_uom_code
445      		INTO  	x_cum_record.cum_key_id,
446      		x_cum_record.cum_qty,
447      		x_cum_record.cum_qty_to_be_accumulated,
448      		x_cum_record.cum_qty_after_cutoff,
449      		x_cum_record.last_cum_qty_update_date,
450      		x_cum_record.cust_uom_code
451      		FROM   RLM_CUST_ITEM_CUM_KEYS
452      		WHERE  NVL(ship_from_org_id,0)		= NVL(p_ship_from_org_id,0)
453       		AND    NVL(ship_to_address_id,0)     	= NVL(p_ship_to_address_id,0)
454       		AND    NVL(intrmd_ship_to_id,0) 	= NVL(p_intrmd_ship_to_address_id,0)
455       		AND    NVL(bill_to_address_id,0)     	= NVL(p_bill_to_address_id,0)
456       		AND    NVL(customer_item_id,0) 	   	= NVL(p_customer_item_id,0)
457       		AND    NVL(purchase_order_number,' ')	= NVL(p_purchase_order_number, ' ')
458       		AND    NVL(cust_record_year, ' ')	= NVL(p_cust_record_year, ' ')
459       		AND    NVL(TRUNC(cum_start_date), sysdate)
460                               = NVL(TRUNC(p_cum_start_date), sysdate)
461                 AND     NVL(inactive_flag,'N')          =  'N';
462 --truncation for bug 1667299
463 	--
464    ELSE
465 	--
466       	SELECT	cum_key_id,
467      		cum_qty,
468      		cum_qty_to_be_accumulated,
469 		cum_qty_after_cutoff,
470      		last_cum_qty_update_date,
471      		cust_uom_code
472      		INTO	x_cum_record.cum_key_id,
473      		x_cum_record.cum_qty,
474      		x_cum_record.cum_qty_to_be_accumulated,
475 		x_cum_record.cum_qty_after_cutoff,
476      		x_cum_record.last_cum_qty_update_date,
477      		x_cum_record.cust_uom_code
478      		FROM   RLM_CUST_ITEM_CUM_KEYS
479     		WHERE  ship_from_org_id IS NULL
480 		AND    NVL(ship_to_address_id, 0)	= NVL(p_ship_to_address_id,0)
481       		AND    NVL(intrmd_ship_to_id, 0) 	= NVL(p_intrmd_ship_to_address_id,0)
482       		AND    NVL(bill_to_address_id, 0)    	= NVL(p_bill_to_address_id,0)
483       		AND    NVL(customer_item_id, 0) 	= NVL(p_customer_item_id,0)
484       		AND    NVL(purchase_order_number,' ')	= NVL(p_purchase_order_number, ' ')
485       		AND    NVL(cust_record_year, ' ')	= NVL(p_cust_record_year, ' ')
486       		AND    NVL(TRUNC(cum_start_date), sysdate)
487                 	= NVL(TRUNC(p_cum_start_date), sysdate)
488                 AND     NVL(inactive_flag,'N')          =  'N';
489 --truncation for bug 1667299
490 		--
491    END IF;
492    --
493    x_cum_record.cum_start_date := p_cum_start_date;
494    x_cum_record.record_return_status := TRUE;
495    x_cum_record.cum_key_created_flag := FALSE;
496    x_cum_record.shipment_rule_code := v_rlm_setup_terms_record.cum_shipment_rule_code;
497 
498    IF x_cum_record.cum_qty IS NULL THEN
499       x_cum_record.cum_qty := 0;
500    END IF;
501 
502    IF x_cum_record.cum_qty_to_be_accumulated IS NULL THEN
503       x_cum_record.cum_qty_to_be_accumulated := 0;
504    END IF;
505 
506    IF x_cum_record.cum_qty_after_cutoff IS NULL THEN
507       x_cum_record.cum_qty_after_cutoff := 0;
508    END IF;
509    --
510    /* DEBUGGING - OUTPUT Values */
511   IF (l_debug <> -1) THEN
512       rlm_core_sv.dlog(C_DEBUG, '');
513       rlm_core_sv.dlog(C_DEBUG, 'HERE ARE THE OUTPUT VALUES:');
514       rlm_core_sv.dlog(C_DEBUG, '---------------------------');
515       rlm_core_sv.dlog(C_DEBUG, 'cum_key_id', x_cum_record.cum_key_id);
516       rlm_core_sv.dlog(C_DEBUG, 'cum_start_date', x_cum_record.cum_start_date);
517       rlm_core_sv.dlog(C_DEBUG, 'shipped_quantity', x_cum_record.shipped_quantity);
518       rlm_core_sv.dlog(C_DEBUG, 'actual_shipment_date', x_cum_record.actual_shipment_date);
519       rlm_core_sv.dlog(C_DEBUG, 'cum_key_created_flag', x_cum_record.cum_key_created_flag);
520       rlm_core_sv.dlog(C_DEBUG, 'cum_qty', x_cum_record.cum_qty);
521       rlm_core_sv.dlog(C_DEBUG, 'as_of_date_cum_qty', x_cum_record.as_of_date_cum_qty);
522       rlm_core_sv.dlog(C_DEBUG, 'cum_qty_to_be_accumulated', x_cum_record.cum_qty_to_be_accumulated);
523       rlm_core_sv.dlog(C_DEBUG, 'cum_qty_after_cutoff', x_cum_record.cum_qty_after_cutoff);
524       rlm_core_sv.dlog(C_DEBUG, 'last_cum_qty_update_date', x_cum_record.last_cum_qty_update_date);
525       rlm_core_sv.dlog(C_DEBUG, 'cust_uom_code', x_cum_record.cust_uom_code);
526       rlm_core_sv.dlog(C_DEBUG, 'shipment_rule_code', x_cum_record.shipment_rule_code);
527       rlm_core_sv.dlog(C_DEBUG, 'use_ship_incl_rule_flag', x_cum_record.use_ship_incl_rule_flag);
528       rlm_core_sv.dlog(C_DEBUG, 'record_return_status', x_cum_record.record_return_status);
529       rlm_core_sv.dpop(C_DEBUG, 'Cum key record is retrieved successfully');
530    END IF;
531    --
532    EXCEPTION
533 	--
534 	WHEN TOO_MANY_ROWS THEN
535 	   --
536            x_cum_record.msg_name := 'RLM_CUM_TOO_MANY_ROWS';
537            --
538 	   rlm_message_sv.get_msg_text(
539 	  		x_message_name	=> x_cum_record.msg_name,
540 	  		x_text		=> x_cum_record.msg_data);
541 	   --
542 	   x_cum_record.record_return_status := FALSE;
543 	   --
544            IF (l_debug <> -1) THEN
545    	     rlm_core_sv.dlog(C_DEBUG, 'msg_data', x_cum_record.msg_data);
546              rlm_core_sv.dpop(C_DEBUG, 'TOO_MANY_ROWS');
547 	   END IF;
548 	   --
549 	WHEN NO_DATA_FOUND THEN
550 	   --
551            IF (l_debug <> -1) THEN
552    	     rlm_core_sv.dlog(C_DEBUG, 'No cum key id is found');
553              rlm_core_sv.dlog(C_DEBUG, 'disable_create_cum_key_flag',v_rlm_setup_terms_record.disable_create_cum_key_flag);
554              rlm_core_sv.dlog(C_DEBUG, 'v_orgId ', v_orgId);
555 	   END IF;
556 	   --
557 	   --IF x_cum_key_record.create_cum_key_flag = 'Y' THEN
558            IF (nvl(v_rlm_setup_terms_record.disable_create_cum_key_flag,'N') = 'N'
559                 AND x_cum_key_record.create_cum_key_flag = 'Y' OR (g_manual_cum)) -- BugFix #4147544
560 	   THEN
561 
562 	     --------------------------------------------------------------------+
563 	     /* Insert a new cum key record if the record does not exist yet and
564 	    	if the calling progam sets the create cum key flag to 'Y' */
565 
566 		--
567             	BEGIN
568 		  --
569 		  x_cum_record.cum_qty := 0;
570 		  x_cum_record.cum_qty_to_be_accumulated := 0;
571 		  x_cum_record.cum_qty_after_cutoff := 0;
572 		  --
573 		  INSERT INTO rlm_cust_item_cum_keys_all(
574 			cum_key_id,
575 			cum_qty,
576 			cum_qty_to_be_accumulated,
577 			cum_qty_after_cutoff,
578 			customer_item_id,
579 			ship_to_address_id,
580 			bill_to_address_id,
581 			intrmd_ship_to_id,
582 			ship_from_org_id,
583 			cum_start_date,
584 			cust_record_year,
585 			purchase_order_number,
586 			last_cum_qty_update_date,
587 			last_updated_by,
588 			creation_date,
589 			created_by,
590 			last_update_date,
591 			cust_uom_code,
592                         org_id)
593 		  VALUES(
594 			rlm_cust_item_cum_keys_s.nextval,
595 			x_cum_record.cum_qty,
596 			x_cum_record.cum_qty_to_be_accumulated,
597 			x_cum_record.cum_qty_after_cutoff,
598 			p_customer_item_id,
599 			p_ship_to_address_id,
600 			p_bill_to_address_id,
601 			p_intrmd_ship_to_address_id,
602 			p_ship_from_org_id,
603 			p_cum_start_date,
604 			p_cust_record_year,
605 			p_purchase_order_number,
606 			sysdate,
607 			fnd_global.user_id,
608 			sysdate,
609 			fnd_global.user_id,
610 			sysdate,
611 			x_cum_record.cust_uom_code,
612                         v_OrgId);
613 		  --
614 		  SELECT	rlm_cust_item_cum_keys_s.currval
615 		  INTO		x_cum_record.cum_key_id
616 		  FROM		DUAL;
617 		  --
618 		  x_cum_record.cum_start_date := p_cum_start_date;
619 		  x_cum_record.cum_key_created_flag := TRUE;
620 		  x_cum_record.record_return_status := TRUE;
621 		  x_cum_record.last_cum_qty_update_date := SYSDATE;
622 		  --
623                   x_cum_record.msg_name := 'RLM_CUM_KEY_ID_CREATED';
624                   --
625 		  rlm_message_sv.get_msg_text(
626 	  			x_message_name	=> x_cum_record.msg_name,
627 	  			x_text		=> x_cum_record.msg_data);
628 		  --
629 	        --DEBUGGING - Output Values
630                   IF (l_debug <> -1) THEN
631    	   	    rlm_core_sv.dlog(C_DEBUG, 'HERE ARE THE NEWLY INSERTED RECORD VALUES');
632               	    rlm_core_sv.dlog(C_DEBUG, 'cum_key_id', x_cum_record.cum_key_id);
633               	    rlm_core_sv.dlog(C_DEBUG, 'cum_start_date', x_cum_record.cum_start_date);
634               	    rlm_core_sv.dlog(C_DEBUG, 'shipped_quantity', x_cum_record.shipped_quantity);
635               	    rlm_core_sv.dlog(C_DEBUG, 'actual_ship_date', x_cum_record.actual_shipment_date);
636               	    rlm_core_sv.dlog(C_DEBUG, 'cum_key_created', x_cum_record.cum_key_created_flag);
637               	    rlm_core_sv.dlog(C_DEBUG, 'cum_qty', x_cum_record.cum_qty);
638               	    rlm_core_sv.dlog(C_DEBUG, 'cust_uom_code', x_cum_record.cust_uom_code);
639               	    rlm_core_sv.dlog(C_DEBUG, 'record_return_status', x_cum_record.record_return_status);
640    	  	    rlm_core_sv.dpop(C_DEBUG, x_cum_record.msg_data);
641 	   	  END IF;
642 		  --
643                 EXCEPTION
644 		  --
645 		  WHEN OTHERS THEN
646 			--
647                         x_cum_record.msg_name := 'RLM_CUM_KEY_NOT_CREATED';
648                         --
649 			rlm_message_sv.get_msg_text(
650 	  			x_message_name	=> x_cum_record.msg_name,
651 	  			x_text		=> x_cum_record.msg_data,
652 				x_token1	=> 'SQLERRM',
653 				x_value1	=> SQLERRM);
654 			--
655 	             	x_cum_record.record_return_status := FALSE;
656 			--
657                         IF (l_debug <> -1) THEN
658    	             	  rlm_core_sv.dlog(C_DEBUG, x_cum_record.msg_data);
659    	             	  rlm_core_sv.dpop(C_DEBUG, SQLERRM);
660 	             	END IF;
661 			--
662                 END;
663                 ----------------------------------------------------------------+
664 
665 	   ELSE
666 		-- If create_cum_key_flag is 'N', set record return status to TRUE
667 		--
668                 x_cum_record.cum_key_id := NULL; -- Bug 4667349
669 		x_cum_record.record_return_status := TRUE;
670 		--
671                 IF (l_debug <> -1) THEN
672    	   	  rlm_core_sv.dpop(C_DEBUG, 'CUM Key Id is not found, and not created as create_cum_key_flag is set to N');
673 	   	END IF;
674 		--
675 	   END IF;
676 	   --
677         WHEN E_UNEXPECTED THEN
678 	    --
679 	    x_cum_record.record_return_status := FALSE;
680 	    --
681             IF (l_debug <> -1) THEN
682    	      rlm_core_sv.dlog(C_DEBUG,'msg_data',x_cum_record.msg_data);
683    	      rlm_core_sv.dlog(C_DEBUG,'record_return_status',x_cum_record.record_return_status);
684    	      rlm_core_sv.dpop(C_DEBUG,'E_UNEXPECTED');
685 	    END IF;
686 	    --
687 	WHEN OTHERS THEN
688 	    --
689 	    -- RLM_CUM_SQLERR
690 	    --
691 	    x_cum_record.record_return_status := FALSE;
692 	  --DEBUGGING - OUTPUT Values
693             IF (l_debug <> -1) THEN
694    	       rlm_core_sv.dlog(C_DEBUG, '');
695    	       rlm_core_sv.dlog(C_DEBUG, 'HERE ARE THE OUTPUT VALUES:');
696    	       rlm_core_sv.dlog(C_DEBUG, '---------------------------');
697                rlm_core_sv.dlog(C_DEBUG, 'cum_key_id', x_cum_record.cum_key_id);
698                rlm_core_sv.dlog(C_DEBUG, 'cum_start_date', x_cum_record.cum_start_date);
699                rlm_core_sv.dlog(C_DEBUG, 'shipped_quantity', x_cum_record.shipped_quantity);
700                rlm_core_sv.dlog(C_DEBUG, 'actual_ship_date', x_cum_record.actual_shipment_date);
701                rlm_core_sv.dlog(C_DEBUG, 'cum_key_created', x_cum_record.cum_key_created_flag);
702                rlm_core_sv.dlog(C_DEBUG, 'cum_qty', x_cum_record.cum_qty);
703                rlm_core_sv.dlog(C_DEBUG, 'cust_uom_code', x_cum_record.cust_uom_code);
704                rlm_core_sv.dlog(C_DEBUG, 'record_return_status', x_cum_record.record_return_status);
705    	       rlm_core_sv.dpop(C_DEBUG, SQLERRM);
706 	    END IF;
707 	    --
708    END CalculateCumKey;
709    --
710 /*============================================================================
711 
712   PROCEDURE NAME:	CalculateCumKeyClient
713 
714 =============================================================================*/
715 
716  PROCEDURE CalculateCumKeyClient (
717  -- Parameter definition was changed TCA obsolescence project.
718   x_customer_id			IN	HZ_CUST_ACCOUNTS.CUST_ACCOUNT_ID%TYPE,
719   x_customer_item_id		IN	RLM_CUST_ITEM_CUM_KEYS.CUSTOMER_ITEM_ID%TYPE,
720   x_ship_from_org_id		IN	RLM_CUST_ITEM_CUM_KEYS.SHIP_FROM_ORG_ID%TYPE,
721   x_intrmd_ship_to_address_id 	IN	RLM_CUST_ITEM_CUM_KEYS.INTRMD_SHIP_TO_ID%TYPE,
722   x_ship_to_address_id		IN	RLM_CUST_ITEM_CUM_KEYS.SHIP_TO_ADDRESS_ID%TYPE,
723   x_bill_to_address_id		IN	RLM_CUST_ITEM_CUM_KEYS.BILL_TO_ADDRESS_ID%TYPE,
724   x_purchase_order_number	IN	RLM_CUST_ITEM_CUM_KEYS.PURCHASE_ORDER_NUMBER%TYPE,
725   x_cust_record_year		IN	RLM_CUST_ITEM_CUM_KEYS.CUST_RECORD_YEAR%TYPE,
726   x_create_cum_key_flag		IN	VARCHAR2,
727   x_msg_data			IN OUT	NOCOPY VARCHAR2,
728   x_record_return_status	IN OUT	NOCOPY BOOLEAN,
729   x_cum_key_id			IN OUT	NOCOPY RLM_CUST_ITEM_CUM_KEYS.CUM_KEY_ID%TYPE,
730   x_cum_start_date		IN OUT	NOCOPY RLM_CUST_ITEM_CUM_KEYS.CUM_START_DATE%TYPE,
731   x_shipped_quantity		IN OUT	NOCOPY OE_ORDER_LINES.SHIPPED_QUANTITY%TYPE,
732   x_actual_shipment_date	IN OUT	NOCOPY OE_ORDER_LINES.ACTUAL_SHIPMENT_DATE%TYPE,
733   x_cum_key_created_flag	IN OUT	NOCOPY BOOLEAN,
734   x_cum_qty			IN OUT	NOCOPY RLM_CUST_ITEM_CUM_KEYS.CUM_QTY%TYPE,
735   x_as_of_date_cum_qty		IN OUT	NOCOPY RLM_CUST_ITEM_CUM_KEYS.CUM_QTY%TYPE,
736   x_cum_qty_to_be_accumulated	IN OUT	NOCOPY RLM_CUST_ITEM_CUM_KEYS.CUM_QTY_TO_BE_ACCUMULATED%TYPE,
737   x_cum_qty_after_cutoff	IN OUT	NOCOPY RLM_CUST_ITEM_CUM_KEYS.CUM_QTY_AFTER_CUTOFF%TYPE,
738   x_last_cum_qty_update_date	IN OUT	NOCOPY RLM_CUST_ITEM_CUM_KEYS.LAST_CUM_QTY_UPDATE_DATE%TYPE,
739   x_cust_uom_code		IN OUT	NOCOPY RLM_CUST_ITEM_CUM_KEYS.CUST_UOM_CODE%TYPE,
740   x_use_ship_incl_rule_flag	IN OUT	NOCOPY VARCHAR2,
741   x_shipment_rule_code		IN OUT	NOCOPY RLM_CUST_SHIPTO_TERMS.CUM_SHIPMENT_RULE_CODE%TYPE,
742   x_yesterday_time_cutoff	IN OUT	NOCOPY RLM_CUST_ITEM_CUM_KEYS.LAST_UPDATE_DATE%TYPE,
743   x_last_update_date		IN OUT	NOCOPY RLM_CUST_ITEM_CUM_KEYS.LAST_UPDATE_DATE%TYPE,
744   x_as_of_date_time		IN OUT	NOCOPY OE_ORDER_LINES.ACTUAL_SHIPMENT_DATE%TYPE)
745   --
746  IS
747      --
748      x_cum_key_record cum_key_attrib_rec_type;
749      x_cum_record     cum_rec_type;
750      --
751  BEGIN
752      --
753      IF (l_debug <> -1) THEN
754         rlm_core_sv.start_debug;
755         rlm_core_sv.dpush(C_SDEBUG, 'CalculateCumKeyClient');
756      END IF;
757 	--
758 	x_cum_key_record.customer_id      := x_customer_id;
759 	x_cum_key_record.customer_item_id := x_customer_item_id;
760 
761 	x_cum_key_record.ship_from_org_id := x_ship_from_org_id;
762 	x_cum_key_record.intrmd_ship_to_address_id := x_intrmd_ship_to_address_id;
763 	x_cum_key_record.ship_to_address_id := x_ship_to_address_id;
764 	x_cum_key_record.bill_to_address_id := x_bill_to_address_id;
765 	x_cum_key_record.purchase_order_number := x_purchase_order_number;
766 	x_cum_key_record.cust_record_year:= x_cust_record_year;
767 	x_cum_key_record.cum_start_date := x_cum_start_date;
768 	x_cum_key_record.create_cum_key_flag := x_create_cum_key_flag;
769 	x_cum_key_record.create_cum_key_flag := 'Y';
770 	x_cum_record.msg_data := x_msg_data;
771 	x_cum_record.record_return_status := x_record_return_status;
772 	x_cum_record.cum_key_id := x_cum_key_id;
773 	x_cum_record.cum_start_date := x_cum_start_date;
774 	x_cum_record.shipped_quantity := x_shipped_quantity;
775 	--
776         IF x_actual_shipment_date IS NOT NULL THEN
777 	   --
778 	   x_cum_record.actual_shipment_date := x_actual_shipment_date;
779 	   --
780         ELSE
781 	   --
782 	   SELECT SYSDATE
783 	   INTO   x_cum_record.actual_shipment_date
784 	   FROM   DUAL;
785 	   --
786         END IF;
787 	--
788 	x_cum_record.cum_key_created_flag := x_cum_key_created_flag;
789 	x_cum_record.cum_qty:= x_cum_qty;
790 	x_cum_record.as_of_date_cum_qty := x_as_of_date_cum_qty;
791 	x_cum_record.cum_qty_to_be_accumulated := x_cum_qty_to_be_accumulated;
792 	x_cum_record.cum_qty_after_cutoff := x_cum_qty_after_cutoff;
793 	x_cum_record.last_cum_qty_update_date:= x_last_cum_qty_update_date;
794 	x_cum_record.cust_uom_code := x_cust_uom_code;
795 	x_cum_record.use_ship_incl_rule_flag := x_use_ship_incl_rule_flag;
796 	x_cum_record.shipment_rule_code := x_shipment_rule_code;
797 	x_cum_record.yesterday_time_cutoff := x_yesterday_time_cutoff;
798 	x_cum_record.last_update_date := x_last_update_date;
799 	x_cum_record.as_of_date_time := x_as_of_date_time;
800 	--
801    -- Call calculate cum key api passing cum key records and cum records
802 	--
803 	g_manual_cum := TRUE; -- BugFix #4147544
804 	--
805    	RLM_TPA_SV.CalculateCumKey (x_cum_key_record => x_cum_key_record,
806 	                            x_cum_record     => x_cum_record);
807 	x_msg_data := x_cum_record.msg_data;
808 	x_record_return_status := x_cum_record.record_return_status;
809 	x_cum_key_id := x_cum_record.cum_key_id;
810 	x_cum_start_date := x_cum_record.cum_start_date;
811 	x_shipped_quantity := x_cum_record.shipped_quantity;
812  	x_actual_shipment_date := x_cum_record.actual_shipment_date;
813  	x_cum_key_created_flag := x_cum_record.cum_key_created_flag;
814  	x_cum_qty := x_cum_record.cum_qty;
815  	x_as_of_date_cum_qty := x_cum_record.as_of_date_cum_qty;
816  	x_cum_qty_to_be_accumulated := x_cum_record.cum_qty_to_be_accumulated;
817  	x_cum_qty_after_cutoff := x_cum_record.cum_qty_after_cutoff;
818  	x_last_cum_qty_update_date := x_cum_record.last_cum_qty_update_date;
819  	x_cust_uom_code := x_cum_record.cust_uom_code;
820  	x_use_ship_incl_rule_flag := x_cum_record.use_ship_incl_rule_flag;
821 	x_shipment_rule_code := x_cum_record.shipment_rule_code;
822 	x_yesterday_time_cutoff := x_cum_record.yesterday_time_cutoff;
823 	x_last_update_date := x_cum_record.last_update_date;
824 	x_as_of_date_time := x_cum_record.as_of_date_time;
825 	--
826 	g_manual_cum := FALSE; -- BugFix #4147544
827 	--
828         IF (l_debug <> -1) THEN
829           rlm_core_sv.stop_debug;
830         END IF;
831         --
832    EXCEPTION
833 	--
834 	WHEN OTHERS THEN
835           --
836           IF (l_debug <> -1) THEN
837    	    rlm_core_sv.dpop(C_DEBUG, SQLERRM);
838             rlm_core_sv.stop_debug;
839 	  END IF;
840           --
841    END CalculateCumKeyClient;
842    --
843 
844 /*============================================================================
845 
846   PROCEDURE NAME:	CalculateSupplierCum
847 
848 =============================================================================*/
849 
850  PROCEDURE CalculateSupplierCum (
851      x_new_ship_count        IN     RLM_CUM_SV.t_new_ship_count := RLM_CUM_SV.g_miss_new_ship_count,
852      x_cum_key_record        IN     RLM_CUM_SV.cum_key_attrib_rec_type,
853      x_cum_record            IN OUT NOCOPY RLM_CUM_SV.cum_rec_type)
854  IS
855 	--
856 	E_UNEXPECTED			EXCEPTION;
857 	v_terms_level			VARCHAR2(15) DEFAULT NULL;
858 	v_terms_level2			VARCHAR2(15) DEFAULT NULL;
859 	v_setup_terms_msg		VARCHAR2(2000);
860 	v_return_status			BOOLEAN DEFAULT FALSE;
861 	rlm_setup_record		rlm_setup_terms_sv.setup_terms_rec_typ;
862 	v_cutoff_date			DATE;
863 	v_ship_to_org_id		NUMBER;
864 	v_bill_to_org_id		NUMBER;
865 	adj_qty				NUMBER DEFAULT 0;
866 	v_ship_from_org_name		VARCHAR2(250);
867 	v_customer_name			VARCHAR2(360);
868 	v_ship_to_location		VARCHAR2(250);
869 	v_customer_item_number		VARCHAR2(250);
870 	--
871  BEGIN
872    --
873    /* DEBUGGING - INPUT Values */
874    IF (l_debug <> -1) THEN
875       rlm_core_sv.dpush(C_SDEBUG, 'CalculateSupplierCum');
876       rlm_core_sv.dlog(C_DEBUG, 'HERE ARE THE INPUT VALUES');
877       rlm_core_sv.dlog(C_DEBUG, 'ship_from_org_id', x_cum_key_record.ship_from_org_id);
878       rlm_core_sv.dlog(C_DEBUG, 'ship_to_address_id', x_cum_key_record.ship_to_address_id);
879       rlm_core_sv.dlog(C_DEBUG, 'inventory_item_id', x_cum_key_record.inventory_item_id);
880       rlm_core_sv.dlog(C_DEBUG, 'cum_qty', x_cum_record.cum_qty);
881       rlm_core_sv.dlog(C_DEBUG, 'cum_qty_to_be_accumulated', x_cum_record.cum_qty_to_be_accumulated);
882       rlm_core_sv.dlog(C_DEBUG, 'cum_qty_after_cutoff', x_cum_record.cum_qty_after_cutoff);
883       rlm_core_sv.dlog(C_DEBUG, 'shipped_quantity', x_cum_record.shipped_quantity);
884       rlm_core_sv.dlog(C_DEBUG, 'actual_shipment_date', x_cum_record.actual_shipment_date);
885       rlm_core_sv.dlog(C_DEBUG, 'use_ship_incl_rule_flag',	x_cum_record.use_ship_incl_rule_flag);
886       rlm_core_sv.dlog(C_DEBUG, 'record_return_status', x_cum_record.record_return_status);
887    END IF;
888    --
889    IF x_cum_record.record_return_status = FALSE THEN
890 	--
891         x_cum_record.msg_name := 'RLM_CUM_INVALID_RECORD';
892         --
893         rlm_message_sv.get_msg_text(
894 		x_message_name	=> x_cum_record.msg_name,
895 		x_text		=> x_cum_record.msg_data);
896 	--
897 	RAISE E_UNEXPECTED;
898 	--
899    END IF;
900    --
901    --
902    IF x_cum_record.use_ship_incl_rule_flag = 'N' THEN
903 
904 	/* CUM calculation will be done using as_of_date_time being
905            the cutoff date. This as_of_date_time is not necessarily
906 	   the cum_shipment_rule_code cutoff date. The as_of_date_time
907 	   could be any value between the cum_start_date and today */
908 	--
909         IF (l_debug <> -1) THEN
910    	  rlm_core_sv.dlog(C_DEBUG, 'Cum calculation is done using as_of_date_time');
911    	  rlm_core_sv.dlog(C_DEBUG, 'cum_key_id', x_cum_record.cum_key_id);
912    	  rlm_core_sv.dlog(C_DEBUG, 'cum_start_date', x_cum_record.cum_start_date);
913    	  rlm_core_sv.dlog(C_DEBUG, 'as_of_date_time', x_cum_record.as_of_date_time);
914 	END IF;
915 	--
916         SELECT	NVL((SUM(shipped_quantity)), 0)
917 	INTO	x_cum_record.as_of_date_cum_qty
918        	FROM	OE_ORDER_LINES
919        	WHERE   veh_cus_item_cum_key_id = x_cum_record.cum_key_id
920 	AND	actual_shipment_date >= x_cum_record.cum_start_date
921 	AND	actual_shipment_date <= x_cum_record.as_of_date_time
922 	AND	source_document_type_id = 5
923 	AND	open_flag = 'N'
924         AND     shipped_quantity IS NOT NULL
925         AND     inventory_item_id = x_cum_key_record.inventory_item_id;
926         --
927         SELECT  NVL(SUM(transaction_qty), 0)
928         INTO    adj_qty
929         FROM    rlm_cust_item_cum_adj
930         WHERE   cum_key_id = x_cum_record.cum_key_id
931         AND     transaction_date_time >= x_cum_record.cum_start_date
932         AND     transaction_date_time <= x_cum_record.as_of_date_time;
933 	--
934         x_cum_record.as_of_date_cum_qty := x_cum_record.as_of_date_cum_qty + adj_qty;
935         --
936         IF (l_debug <> -1) THEN
937    	  rlm_core_sv.dlog(C_DEBUG, 'as_of_date_cum_qty', x_cum_record.as_of_date_cum_qty);
938 	END IF;
939  	--
940    ELSIF x_cum_record.use_ship_incl_rule_flag = 'Y' THEN
941 
942 	/* Get cum_shipment_rule_code and cum_yesterd_time_cutoff */
943 	--
944 	RLM_TPA_SV.get_setup_terms(
945 		x_ship_from_org_id => x_cum_key_record.ship_from_org_id,
946 		x_customer_id => x_cum_key_record.customer_id,
947 		x_ship_to_address_id => x_cum_key_record.ship_to_address_id,
948 		x_customer_item_id => x_cum_key_record.customer_item_id,
949 		x_terms_definition_level => v_terms_level,
950 		x_terms_rec => rlm_setup_record,
951 		x_return_message => v_setup_terms_msg,
952 		x_return_status => v_return_status);
953 	--
954 	IF v_return_status = FALSE THEN
955 		--
956 		v_ship_from_org_name := RLM_CORE_SV.get_ship_from(x_cum_key_record.ship_from_org_id);
957 		v_customer_name := RLM_CORE_SV.get_customer_name(x_cum_key_record.customer_id);
958 		v_ship_to_location := RLM_CORE_SV.get_ship_to(x_cum_key_record.ship_to_address_id);
959 		v_customer_item_number := RLM_CORE_SV.get_item_number(x_cum_key_record.customer_item_id);
960 		--
961                 x_cum_record.msg_name := 'RLM_CUM_SETUP_TERMS_REQUIRED';
962                 --
963 		rlm_message_sv.get_msg_text(
964 	 		x_message_name	=> x_cum_record.msg_name,
965 	 		x_text		=> x_cum_record.msg_data,
966 			x_token1	=> 'SF',
967 			x_value1	=> v_ship_from_org_name,
968 			x_token2	=> 'CUST',
969 			x_value2	=> v_customer_name,
970 			x_token3	=> 'ST',
971 			x_value3	=> v_ship_to_location,
972 			x_token4	=> 'CI',
973 			x_value4	=> v_customer_item_number);
974 			--
975 			RAISE E_UNEXPECTED;
976 			--
977 	END IF;
978 	--
979 	--
980 	IF rlm_setup_record.cum_shipment_rule_code IS NOT NULL THEN
981 		--
982 		x_cum_record.shipment_rule_code := rlm_setup_record.cum_shipment_rule_code;
983 		--
984 	ELSE
985 		--
986 		-- RLM_CUM_NO_SHIP_RULE
987 		-- This message will not go to the end user
988 		x_cum_record.msg_data := 'Cum shipment rule code is not found. Proceed with "As of Current Shipment" rule code';
989                 IF (l_debug <> -1) THEN
990                    rlm_core_sv.dlog(C_DEBUG, x_cum_record.msg_data);
991                 END IF;
992 		--
993 	END IF;
994 	--
995 	IF x_cum_record.shipment_rule_code <> 'AS_OF_YESTERDAY' THEN
996 		--
997 		IF x_cum_record.shipment_rule_code = 'AS_OF_CURRENT' THEN
998 		   --
999 		   IF x_cum_record.as_of_date_time IS NULL THEN
1000 			--
1001 			x_cum_record.cum_qty := NVL(x_cum_record.cum_qty,0) + NVL(x_cum_record.cum_qty_to_be_accumulated,0) + NVL(x_cum_record.shipped_quantity,0);
1002 			--
1003 			x_cum_record.cum_qty_to_be_accumulated := 0;
1004 			--
1005 		   ELSE
1006 			--
1007 			v_cutoff_date := x_cum_record.as_of_date_time;
1008 			--
1009  		   END IF;
1010 		   --
1011 		ELSIF x_cum_record.shipment_rule_code = 'AS_OF_PRIOR' THEN
1012 		   --
1013 		   IF x_cum_record.as_of_date_time IS NULL THEN
1014 		     --
1015 		     IF x_new_ship_count(x_cum_record.cum_key_id) = 1 THEN
1016 			--
1017 			x_cum_record.cum_qty := NVL(x_cum_record.cum_qty,0) + NVL(x_cum_record.cum_qty_to_be_accumulated,0);
1018 			--
1019 			x_cum_record.cum_qty_to_be_accumulated := 0;
1020 			--
1021                      END IF;
1022 			--
1023 			x_cum_record.cum_qty_to_be_accumulated := NVL(x_cum_record.cum_qty_to_be_accumulated,0) + NVL(x_cum_record.shipped_quantity,0);
1024 			--
1025 		   ELSE
1026 			--
1027 			-- Get the last shipment date before the as_of_date_time
1028 			--
1029 			BEGIN
1030 				--
1031 				-- TODO: Verify this logic again
1032 				--
1033 				SELECT 	MAX(actual_shipment_date)
1034 				INTO	v_cutoff_date
1035 				FROM 	oe_order_lines
1036 				WHERE	veh_cus_item_cum_key_id = x_cum_record.cum_key_id
1037 				AND	source_document_type_id = 5
1038 				AND	open_flag = 'N'
1039                                 AND     shipped_quantity IS NOT NULL
1040 				AND	actual_shipment_date < x_cum_record.as_of_date_time
1041                                 AND     inventory_item_id = x_cum_key_record.inventory_item_id;
1042 				--
1043 			EXCEPTION
1044 				--
1045 				WHEN NO_DATA_FOUND THEN
1046 					--
1047                                         IF (l_debug <> -1) THEN
1048    					  rlm_core_sv.dlog(C_DEBUG, 'No data found');
1049 					END IF;
1050 					--
1051 			END;
1052 			--
1053 		   END IF;
1054 		   --
1055 		ELSE
1056 			--
1057                         x_cum_record.msg_name := 'RLM_CUM_UNKNOWN_SHIP_RULE';
1058                         --
1059 	        	rlm_message_sv.get_msg_text(
1060 				x_message_name	=> x_cum_record.msg_name,
1061 				x_text		=> x_cum_record.msg_data);
1062 			--
1063 			RAISE E_UNEXPECTED;
1064 			--
1065 		END IF;
1066 		--
1067 	ELSE
1068 		--
1069 		-- 'AS_OF_YESTERDAY'
1070 		--
1071 		IF x_cum_record.as_of_date_time IS NULL THEN
1072 			--
1073 			SELECT	TO_DATE(TO_CHAR(SYSDATE-1, 'DD/MM/YYYY')
1074         		  ||
1075           		DECODE(SIGN(rlm_setup_record.cum_yesterd_time_cutoff/1000-1),
1076                 		 -1, '0'||to_char(rlm_setup_record.cum_yesterd_time_cutoff),
1077                  		to_char(rlm_setup_record.cum_yesterd_time_cutoff)),
1078                  		'DD/MM/YYYY HH24MI')
1079 	        	INTO	x_cum_record.yesterday_time_cutoff
1080 			FROM	DUAL;
1081 			--
1082                         IF x_cum_key_record.called_by_reset_cum = 'N' THEN
1083                           --
1084 			  /* Calculate supplier_cum based on all shipments dated
1085 			     since CUM start date excluding the shipments since yesterday
1086 			     cutoff time. */
1087                 	  --
1088                           IF (SYSDATE-1) < (x_cum_record.yesterday_time_cutoff) THEN
1089                             --
1090                             IF TRUNC(x_cum_record.last_cum_qty_update_date) < TRUNC(SYSDATE) THEN
1091                               --
1092                               x_cum_record.cum_qty := NVL(x_cum_record.cum_qty,0) + NVL(x_cum_record.cum_qty_to_be_accumulated,0);
1093                               x_cum_record.cum_qty_to_be_accumulated := NVL(x_cum_record.shipped_quantity,0) + NVL(x_cum_record.cum_qty_after_cutoff,0);
1094                               x_cum_record.cum_qty_after_cutoff := 0;
1095                               --
1096                             ELSE
1097                               --
1098                               x_cum_record.cum_qty_to_be_accumulated := NVL(x_cum_record.cum_qty_to_be_accumulated,0) + NVL(x_cum_record.shipped_quantity,0);
1099                               --
1100                             END IF;
1101                 	    --
1102                           ELSE
1103                             --
1104                             IF TRUNC(x_cum_record.last_cum_qty_update_date) < TRUNC(SYSDATE) THEN
1105                               --
1106                               x_cum_record.cum_qty := NVL(x_cum_record.cum_qty,0) + NVL(x_cum_record.cum_qty_to_be_accumulated,0);
1107                               x_cum_record.cum_qty_to_be_accumulated := NVL(x_cum_record.cum_qty_after_cutoff,0);
1108                               x_cum_record.cum_qty_after_cutoff := 0;
1109                               --
1110                             END IF;
1111                             --
1112                             x_cum_record.cum_qty_after_cutoff := NVL(x_cum_record.shipped_quantity,0) + NVL(x_cum_record.cum_qty_after_cutoff,0);
1113                             --
1114                           END IF;
1115                           --
1116                         ELSE
1117                           --
1118                           IF x_cum_record.actual_shipment_date < x_cum_record.yesterday_time_cutoff THEN
1119                             --
1120                             x_cum_record.cum_qty := NVL(x_cum_record.cum_qty,0) + NVL(x_cum_record.shipped_quantity,0);
1121                           ELSE
1122                             --
1123                             IF TRUNC(x_cum_record.actual_shipment_date) < TRUNC(SYSDATE) THEN
1124                               --
1125                               x_cum_record.cum_qty_to_be_accumulated := NVL(x_cum_record.cum_qty_to_be_accumulated,0) + NVL(x_cum_record.shipped_quantity,0);
1126                               --
1127                             ELSE
1128                               --
1129                               IF x_cum_record.actual_shipment_date < (x_cum_record.yesterday_time_cutoff + 1) THEN
1130                                 --
1131                                 x_cum_record.cum_qty_to_be_accumulated := NVL(x_cum_record.cum_qty_to_be_accumulated,0) + NVL(x_cum_record.shipped_quantity,0);
1132                                 --
1133                               ELSE
1134                                 --
1135                                 x_cum_record.cum_qty_after_cutoff := NVL(x_cum_record.cum_qty_after_cutoff,0) + NVL(x_cum_record.shipped_quantity,0);
1136                                 --
1137                               END IF;
1138                               --
1139                             END IF;
1140                             --
1141                           END IF;
1142                           --
1143                         END IF;
1144                         --
1145 		ELSE
1146 			--
1147 			SELECT	TO_DATE(TO_CHAR(x_cum_record.as_of_date_time, 'DD/MM/YYYY')
1148         		  ||
1149           		DECODE(SIGN(rlm_setup_record.cum_yesterd_time_cutoff/1000-1),
1150                 		 -1, '0'||to_char(rlm_setup_record.cum_yesterd_time_cutoff),
1151                  		to_char(rlm_setup_record.cum_yesterd_time_cutoff)),
1152                  		'DD/MM/YYYY HH24MI')
1153 	        	INTO	x_cum_record.yesterday_time_cutoff
1154 			FROM	DUAL;
1155 			--
1156 			v_cutoff_date := x_cum_record.yesterday_time_cutoff;
1157 			--
1158 		END IF;
1159 		--
1160    	END IF;
1161 	--
1162 	--
1163 	IF x_cum_record.as_of_date_time IS NOT NULL THEN
1164 		--
1165 		IF x_cum_record.as_of_date_time <> x_cum_record.last_cum_qty_update_date THEN
1166 			--
1167 			BEGIN
1168 				--
1169       				SELECT	NVL(SUM(shipped_quantity), 0)
1170 				INTO	x_cum_record.as_of_date_cum_qty
1171 				FROM	OE_ORDER_LINES
1172    				WHERE   veh_cus_item_cum_key_id = x_cum_record.cum_key_id
1173 				AND	actual_shipment_date >= x_cum_record.cum_start_date
1174 				AND	actual_shipment_date <= v_cutoff_date
1175 				AND	source_document_type_id = 5
1176 				AND	open_flag = 'N'
1177                                 AND     shipped_quantity IS NOT NULL
1178                                 AND     inventory_item_id = x_cum_key_record.inventory_item_id;
1179 				--
1180 				SELECT 	NVL(SUM(transaction_qty), 0)
1181 				INTO	adj_qty
1182 				FROM	rlm_cust_item_cum_adj
1183 				WHERE	cum_key_id = x_cum_record.cum_key_id
1184 				AND	transaction_date_time >= x_cum_record.cum_start_date
1185 				AND	transaction_date_time <= v_cutoff_date;
1186 				--
1187 				x_cum_record.as_of_date_cum_qty := x_cum_record.as_of_date_cum_qty + adj_qty;
1188 				--
1189    			EXCEPTION
1190 				--
1191 				WHEN NO_DATA_FOUND THEN
1192 					--
1193                                         x_cum_record.msg_name := 'RLM_CUM_NO_AOD_SHIPMENT';
1194                                         --
1195 			        	rlm_message_sv.get_msg_text(
1196 						x_message_name	=> x_cum_record.msg_name,
1197 						x_text		=> x_cum_record.msg_data,
1198 						x_token1	=> 'CSD',
1199 						x_value1	=> TO_CHAR(x_cum_record.cum_start_date),
1200 						x_token2	=> 'CTD',
1201 						x_value2	=> TO_CHAR(v_cutoff_date));
1202 					--
1203                                         IF (l_debug <> -1) THEN
1204    					  rlm_core_sv.dlog(C_DEBUG, 'RLM_CUM_NO_AOD_SHIPMENT -- as_of_date_time not null');
1205 					END IF;
1206 					--
1207    			END;
1208 			--
1209 		ELSE
1210 			--
1211 			/* There is no intransit shipment. The previously stored quantity is the same */
1212 			--
1213 			x_cum_record.as_of_date_cum_qty := x_cum_record.cum_qty;
1214 			--
1215 		END IF;
1216 		--
1217 	END IF;
1218 	--
1219    END IF;
1220    --
1221    x_cum_record.record_return_status := TRUE;
1222    --
1223    /* DEBUGGING - OUTPUT Values */
1224    IF (l_debug <> -1) THEN
1225       rlm_core_sv.dlog(C_DEBUG, 'HERE ARE THE OUTPUT VALUES');
1226       rlm_core_sv.dlog(C_DEBUG, 'cum_qty', x_cum_record.cum_qty);
1227       rlm_core_sv.dlog(C_DEBUG, 'cum_qty_to_be_accumulated', x_cum_record.cum_qty_to_be_accumulated);
1228       rlm_core_sv.dlog(C_DEBUG, 'cum_qty_after_cutoff', x_cum_record.cum_qty_after_cutoff);
1229       rlm_core_sv.dlog(C_DEBUG, 'as_of_date_cum_qty', x_cum_record.as_of_date_cum_qty);
1230       rlm_core_sv.dlog(C_DEBUG, 'shipment_rule_code', x_cum_record.shipment_rule_code);
1231       rlm_core_sv.dlog(C_DEBUG, 'yesterday_time_cutoff', x_cum_record.yesterday_time_cutoff);
1232       rlm_core_sv.dlog(C_DEBUG, 'record_return_status', x_cum_record.record_return_status);
1233       rlm_core_sv.dpop(C_DEBUG, 'Terminate successfully');
1234    END IF;
1235    --
1236  EXCEPTION
1237 	--
1238 	WHEN E_UNEXPECTED THEN
1239 	   --
1240 	   x_cum_record.record_return_status := FALSE;
1241 	   --
1242 	   /* DEBUGGING - OUTPUT Values */
1243            IF (l_debug <> -1) THEN
1244    	     rlm_core_sv.dlog(C_DEBUG, 'HERE ARE THE OUTPUT VALUES');
1245    	     rlm_core_sv.dlog(C_DEBUG, 'cum_qty', x_cum_record.cum_qty);
1246    	     rlm_core_sv.dlog(C_DEBUG, 'cum_qty_to_be_accumulated', x_cum_record.cum_qty_to_be_accumulated);
1247    	     rlm_core_sv.dlog(C_DEBUG, 'cum_qty_after_cutoff', x_cum_record.cum_qty_after_cutoff);
1248    	     rlm_core_sv.dlog(C_DEBUG, 'shipment_rule_code', x_cum_record.shipment_rule_code);
1249    	     rlm_core_sv.dlog(C_DEBUG, 'yesterday_time_cutoff', x_cum_record.yesterday_time_cutoff);
1250    	     rlm_core_sv.dlog(C_DEBUG, 'record_return_status', x_cum_record.record_return_status);
1251    	     rlm_core_sv.dpop(C_DEBUG, x_cum_record.msg_data);
1252 	   END IF;
1253 	   --
1254 	WHEN NO_DATA_FOUND THEN
1255 	   --
1256 	   x_cum_record.record_return_status := FALSE;
1257 	   --
1258 	   /* DEBUGGING - OUTPUT Values */
1259            IF (l_debug <> -1) THEN
1260    	     rlm_core_sv.dlog(C_DEBUG, 'HERE ARE THE OUTPUT VALUES');
1261    	     rlm_core_sv.dlog(C_DEBUG, 'cum_qty', x_cum_record.cum_qty);
1262    	     rlm_core_sv.dlog(C_DEBUG, 'cum_qty_to_be_accumulated', x_cum_record.cum_qty_to_be_accumulated);
1263    	     rlm_core_sv.dlog(C_DEBUG, 'cum_qty_after_cutoff', x_cum_record.cum_qty_after_cutoff);
1264    	     rlm_core_sv.dlog(C_DEBUG, 'shipment_rule_code', x_cum_record.shipment_rule_code);
1265    	     rlm_core_sv.dlog(C_DEBUG, 'yesterday_time_cutoff', x_cum_record.yesterday_time_cutoff);
1266    	     rlm_core_sv.dlog(C_DEBUG, 'record_return_status', x_cum_record.record_return_status);
1267    	     rlm_core_sv.dpop(C_DEBUG, 'No record found using the set of input values provided');
1268 	   END IF;
1269 	   --
1270 	WHEN OTHERS THEN
1271 	   --
1272 	   x_cum_record.record_return_status := FALSE;
1273 	   --
1274 	   /* DEBUGGING - OUTPUT Values */
1275            IF (l_debug <> -1) THEN
1276    	     rlm_core_sv.dlog(C_DEBUG, 'HERE ARE THE OUTPUT VALUES');
1277    	     rlm_core_sv.dlog(C_DEBUG, 'cum_qty', x_cum_record.cum_qty);
1278    	     rlm_core_sv.dlog(C_DEBUG, 'cum_qty_to_be_accumulated', x_cum_record.cum_qty_to_be_accumulated);
1279    	     rlm_core_sv.dlog(C_DEBUG, 'cum_qty_after_cutoff', x_cum_record.cum_qty_after_cutoff);
1280    	     rlm_core_sv.dlog(C_DEBUG, 'shipment_rule_code', x_cum_record.shipment_rule_code);
1281    	     rlm_core_sv.dlog(C_DEBUG, 'yesterday_time_cutoff', x_cum_record.yesterday_time_cutoff);
1282    	     rlm_core_sv.dlog(C_DEBUG, 'record_return_status', x_cum_record.record_return_status);
1283 	     -- RLM_CUM_SQLERR
1284    	     rlm_core_sv.dpop(C_DEBUG, SQLERRM);
1285 	   END IF;
1286 	   --
1287  END CalculateSupplierCum;
1288 
1289 /*============================================================================
1290 
1291   PROCEDURE NAME:	CalculateSupplierCumClient
1292 
1293 =============================================================================*/
1294 
1295  PROCEDURE CalculateSupplierCumClient (
1296  -- Parameter definition was changed TCA obsolescence project.
1297   x_customer_id			IN HZ_CUST_ACCOUNTS.CUST_ACCOUNT_ID%TYPE,
1298   x_customer_item_id		IN RLM_CUST_ITEM_CUM_KEYS.CUSTOMER_ITEM_ID%TYPE,
1299   x_inventory_item_id           IN      OE_ORDER_LINES.INVENTORY_ITEM_ID%TYPE,
1300   x_ship_from_org_id		IN RLM_CUST_ITEM_CUM_KEYS.SHIP_FROM_ORG_ID%TYPE,
1301   x_intrmd_ship_to_address_id	IN RLM_CUST_ITEM_CUM_KEYS.INTRMD_SHIP_TO_ID%TYPE,
1302   x_ship_to_address_id		IN RLM_CUST_ITEM_CUM_KEYS.SHIP_TO_ADDRESS_ID%TYPE,
1303   x_bill_to_address_id		IN RLM_CUST_ITEM_CUM_KEYS.BILL_TO_ADDRESS_ID%TYPE,
1304   x_purchase_order_number	IN RLM_CUST_ITEM_CUM_KEYS.PURCHASE_ORDER_NUMBER%TYPE,
1305   x_cust_record_year		IN RLM_CUST_ITEM_CUM_KEYS.CUST_RECORD_YEAR%TYPE,
1306   x_create_cum_key_flag		IN VARCHAR2,
1307   x_msg_data			IN OUT NOCOPY VARCHAR2,
1308   x_record_return_status	IN OUT NOCOPY BOOLEAN,
1309   x_cum_key_id			IN OUT NOCOPY RLM_CUST_ITEM_CUM_KEYS.CUM_KEY_ID%TYPE,
1310   x_cum_start_date		IN OUT NOCOPY RLM_CUST_ITEM_CUM_KEYS.CUM_START_DATE%TYPE,
1311   x_shipped_quantity		IN OUT NOCOPY OE_ORDER_LINES.SHIPPED_QUANTITY%TYPE,
1312   x_actual_shipment_date	IN OUT NOCOPY OE_ORDER_LINES.ACTUAL_SHIPMENT_DATE%TYPE,
1313   x_cum_key_created_flag	IN OUT NOCOPY BOOLEAN,
1314   x_cum_qty			IN OUT NOCOPY RLM_CUST_ITEM_CUM_KEYS.CUM_QTY%TYPE,
1315   x_as_of_date_cum_qty		IN OUT NOCOPY RLM_CUST_ITEM_CUM_KEYS.CUM_QTY%TYPE,
1316   x_cum_qty_to_be_accumulated	IN OUT NOCOPY RLM_CUST_ITEM_CUM_KEYS.CUM_QTY_TO_BE_ACCUMULATED%TYPE,
1317   x_cum_qty_after_cutoff	IN OUT NOCOPY RLM_CUST_ITEM_CUM_KEYS.CUM_QTY_AFTER_CUTOFF%TYPE,
1318   x_last_cum_qty_update_date	IN OUT NOCOPY RLM_CUST_ITEM_CUM_KEYS.LAST_CUM_QTY_UPDATE_DATE%TYPE,
1319   x_cust_uom_code		IN OUT NOCOPY RLM_CUST_ITEM_CUM_KEYS.CUST_UOM_CODE%TYPE,
1320   x_use_ship_incl_rule_flag	IN OUT NOCOPY VARCHAR2,
1321   x_shipment_rule_code		IN OUT NOCOPY RLM_CUST_SHIPTO_TERMS.CUM_SHIPMENT_RULE_CODE%TYPE,
1322   x_yesterday_time_cutoff	IN OUT NOCOPY RLM_CUST_ITEM_CUM_KEYS.LAST_UPDATE_DATE%TYPE,
1323   x_last_update_date		IN OUT NOCOPY RLM_CUST_ITEM_CUM_KEYS.LAST_UPDATE_DATE%TYPE,
1324   x_as_of_date_time		IN OUT NOCOPY OE_ORDER_LINES.ACTUAL_SHIPMENT_DATE%TYPE)
1325 
1326  IS
1327      --
1328      x_cum_key_record cum_key_attrib_rec_type;
1329      x_cum_record     cum_rec_type;
1330      --
1331  BEGIN
1332    --
1333    IF (l_debug <> -1) THEN
1334       rlm_core_sv.start_debug;
1335       rlm_core_sv.dpush(C_SDEBUG, 'CalculateSupplierCumClient');
1336    END IF;
1337 	--
1338 	x_cum_key_record.customer_id      := x_customer_id;
1339 	x_cum_key_record.customer_item_id := x_customer_item_id;
1340         x_cum_key_record.inventory_item_id := x_inventory_item_id;
1341 	x_cum_key_record.ship_from_org_id := x_ship_from_org_id;
1342 	x_cum_key_record.intrmd_ship_to_address_id := x_intrmd_ship_to_address_id;
1343 	x_cum_key_record.ship_to_address_id := x_ship_to_address_id;
1344 	x_cum_key_record.bill_to_address_id := x_bill_to_address_id;
1345 	x_cum_key_record.purchase_order_number := x_purchase_order_number;
1346 	x_cum_key_record.cust_record_year:= x_cust_record_year;
1347 	x_cum_key_record.create_cum_key_flag := x_create_cum_key_flag;
1348 	x_cum_record.msg_data := x_msg_data;
1349 	x_cum_record.record_return_status := x_record_return_status;
1350 	x_cum_record.cum_key_id := x_cum_key_id;
1351 	x_cum_record.cum_start_date := x_cum_start_date;
1352 	x_cum_record.shipped_quantity := x_shipped_quantity;
1353 	x_cum_record.actual_shipment_date := x_actual_shipment_date;
1354 	x_cum_record.cum_key_created_flag := x_cum_key_created_flag;
1355 	x_cum_record.cum_qty:= x_cum_qty;
1356 	x_cum_record.as_of_date_cum_qty := x_as_of_date_cum_qty;
1357 	x_cum_record.cum_qty_to_be_accumulated := x_cum_qty_to_be_accumulated;
1358 	x_cum_record.cum_qty_after_cutoff := x_cum_qty_after_cutoff;
1359 	x_cum_record.last_cum_qty_update_date:= x_last_cum_qty_update_date;
1360 	x_cum_record.cust_uom_code := x_cust_uom_code;
1361 	x_cum_record.use_ship_incl_rule_flag := x_use_ship_incl_rule_flag;
1362 	x_cum_record.shipment_rule_code := x_shipment_rule_code;
1363 	x_cum_record.yesterday_time_cutoff := x_yesterday_time_cutoff;
1364 	x_cum_record.last_update_date := x_last_update_date;
1365 	x_cum_record.as_of_date_time := x_as_of_date_time;
1366 	--
1367    -- Call calculate cum key api passing cum key records and cum records
1368    	RLM_TPA_SV.CalculateSupplierCum (x_cum_key_record => x_cum_key_record,
1369 	                                 x_cum_record     => x_cum_record);
1370 	--
1371 	x_msg_data := x_cum_record.msg_data;
1372  	x_record_return_status := x_cum_record.record_return_status;
1373 	x_cum_key_id := x_cum_record.cum_key_id;
1374 	x_cum_start_date := x_cum_record.cum_start_date;
1375 	x_shipped_quantity := x_cum_record.shipped_quantity;
1376 	x_actual_shipment_date := x_cum_record.actual_shipment_date;
1377 	x_cum_key_created_flag := x_cum_record.cum_key_created_flag;
1378 	x_cum_qty := x_cum_record.cum_qty;
1379 	x_as_of_date_cum_qty := x_cum_record.as_of_date_cum_qty;
1380 	x_cum_qty_to_be_accumulated := x_cum_record.cum_qty_to_be_accumulated;
1381 	x_cum_qty_after_cutoff := x_cum_record.cum_qty_after_cutoff;
1382 	x_last_cum_qty_update_date := x_cum_record.last_cum_qty_update_date;
1383 	x_cust_uom_code := x_cum_record.cust_uom_code;
1384 	x_use_ship_incl_rule_flag := x_cum_record.use_ship_incl_rule_flag;
1385 	x_shipment_rule_code := x_cum_record.shipment_rule_code;
1386 	x_yesterday_time_cutoff := x_cum_record.yesterday_time_cutoff;
1387 	x_last_update_date := x_cum_record.last_update_date;
1388 	x_as_of_date_time := x_cum_record.as_of_date_time;
1389     	--
1390    IF (l_debug <> -1) THEN
1391       rlm_core_sv.dpop(C_SDEBUG, 'Completed successfully');
1392       rlm_core_sv.stop_debug;
1393    END IF;
1394    --
1395    EXCEPTION
1396 	--
1397 	WHEN OTHERS THEN
1398           --
1399           IF (l_debug <> -1) THEN
1400    	    rlm_core_sv.dpop(C_DEBUG,SQLERRM);
1401             rlm_core_sv.stop_debug;
1402 	  END IF;
1403           --
1404    END CalculateSupplierCumClient;
1405 
1406 
1407 /*============================================================================
1408 
1409   PROCEDURE NAME:	UpdateCumKey
1410 
1411 =============================================================================*/
1412 
1413  PROCEDURE UpdateCumKey (
1414 	x_trip_stop_id   IN  NUMBER,
1415 	x_return_status OUT NOCOPY BOOLEAN)
1416  IS
1417 	--
1418       --Local Variables
1419 	--
1420 	v_rlm_setup_terms_record  	rlm_setup_terms_sv.setup_terms_rec_typ;
1421 	v_cum_key_record	  	cum_key_attrib_rec_type;
1422 	v_cum_record		  	cum_rec_type;
1423 	v_delivery_id			NUMBER;
1424 	v_header_id		  	NUMBER;
1425 	v_line_id		  	NUMBER;
1426 	v_schedule_header_id		NUMBER;
1427 	v_schedule_line_id		NUMBER;
1428 	v_terms_level		  	VARCHAR2(80) 		DEFAULT NULL;
1429 	v_terms_level2		  	VARCHAR2(80) 		DEFAULT NULL;
1430 	v_setup_terms_msg	  	VARCHAR2(4000);
1431 	v_setup_terms_status	  	BOOLEAN;
1432 	v_veh_cus_item_cum_key_id 	NUMBER;
1433 	v_ship_to_site_use_id		NUMBER 			DEFAULT NULL;
1434 	v_intrmd_ship_to_site_use_id	NUMBER 			DEFAULT NULL;
1435 	v_bill_to_site_use_id		NUMBER			DEFAULT NULL;
1436 	v_level			  	VARCHAR2(80);
1437 	counter			  	NUMBER			DEFAULT 0;
1438    v_new_ship_count	  	t_new_ship_count;
1439 	v_msg_text			VARCHAR2(4000);
1440 	v_upd_indicator			BOOLEAN 		DEFAULT FALSE;
1441 	v_tmp_cust_record_year		VARCHAR2(240);
1442 	v_tmp_return_message		VARCHAR2(4000);
1443 	v_tmp_return_status		BOOLEAN;
1444 	v_oe_header			t_oe_header;
1445 	v_oe_header_id			NUMBER;
1446 	v_org_id			NUMBER;
1447 	hdr_count			NUMBER 			DEFAULT 0;
1448 	v_loop_count			NUMBER;
1449         k                               NUMBER;
1450 	--
1451       --Used by process_order api
1452 	x_oe_api_version		NUMBER 			DEFAULT 1;
1453 	l_return_status			VARCHAR2(1);
1454 	x_msg_count			NUMBER;
1455 	x_msg_data			VARCHAR2(4000);
1456 	--
1457 
1458       --Used for UOM conversion: Bug 4439006
1459         v_Primary_UOM_Code		wsh_delivery_details.requested_quantity_uom%TYPE;
1460         v_shipped_qty                   NUMBER;
1461 
1462       --Exception variables declaration
1463 	e_general_error			EXCEPTION;
1464 	e_no_shipment_line		EXCEPTION;
1465 	e_null_mandatory		EXCEPTION;
1466 	e_do_not_update			EXCEPTION;
1467 	e_cum_start_date		EXCEPTION;
1468         e_no_cum_key	                EXCEPTION;
1469 	--
1470       --Cursor to populate t_oe_header
1471 	--
1472 	CURSOR 	c_oe_header IS
1473 	SELECT 	DISTINCT oelines.header_id, oelines.org_id
1474 	FROM  	WSH_DELIVERY_LEGS wleg,
1475 		WSH_DELIVERY_ASSIGNMENTS_V wdass,
1476 		WSH_DELIVERY_DETAILS wdel,
1477 		OE_ORDER_LINES_ALL oelines
1478 	WHERE	wleg.pick_up_stop_id    		= x_trip_stop_id
1479 	AND	wdass.delivery_id 			= wleg.delivery_id
1480 	AND	wdel.delivery_detail_id 		= wdass.delivery_detail_id
1481 	AND	oelines.shipped_quantity 		IS NOT NULL
1482 	AND	oelines.line_id 			= wdel.source_line_id
1483         AND     wdel.container_flag                     = 'N'                      -- 4301944
1484 	AND	oelines.header_id 			= wdel.source_header_id    -- 4301944
1485 	AND	oelines.source_document_type_id		= 5;
1486 	--
1487         --Cursor to hold oe_order_lines rows of data
1488 	--
1489 	CURSOR 	c_oe_lines IS
1490 	SELECT 	oelines.line_id,
1491 		wleg.delivery_id,
1492 		oelines.header_id,
1493 		sum(wdel.shipped_quantity),
1494 		oelines.actual_shipment_date,
1495 		oelines.ordered_item_id,
1496                 oelines.inventory_item_id,
1497 		oelines.ship_to_org_id,
1498 		oelines.intmed_ship_to_org_id,
1499 		oelines.ship_from_org_id,
1500 		oelines.cust_po_number,
1501 		oelines.industry_attribute1,
1502 		oelines.invoice_to_org_id,
1503 		oelines.actual_shipment_date,
1504                 wdel.requested_quantity_uom, -- Bug 4439006
1505 		oelines.order_quantity_uom,
1506 		oelines.veh_cus_item_cum_key_id,
1507 		oelines.source_document_id,
1508 		oelines.source_document_line_id,
1509                 oelines.org_id
1510 	FROM  	WSH_DELIVERY_LEGS wleg,
1511 		WSH_DELIVERY_ASSIGNMENTS_V wdass,
1512 		WSH_DELIVERY_DETAILS wdel,
1513 		OE_ORDER_LINES_ALL oelines
1514 	WHERE	wleg.pick_up_stop_id    		= x_trip_stop_id
1515 	AND	wdass.delivery_id 			= wleg.delivery_id
1516 	AND	wdel.delivery_detail_id 		= wdass.delivery_detail_id
1517 	AND	oelines.header_id 			= v_oe_header_id
1518 	AND	oelines.shipped_quantity 		IS NOT NULL
1519 	AND	oelines.line_id 			= wdel.source_line_id
1520         AND     wdel.container_flag                     = 'N'                      -- 4301944
1521 	AND	oelines.header_id 			= wdel.source_header_id    -- 4301944
1522 	AND	oelines.source_document_type_id		= 5
1523         group by oelines.line_id,
1524                  wleg.delivery_id,
1525                  oelines.header_id,
1526                  oelines.actual_shipment_date,
1527                  oelines.ordered_item_id,
1528                  oelines.inventory_item_id,
1529                  oelines.ship_to_org_id,
1530                  oelines.intmed_ship_to_org_id,
1531                  oelines.ship_from_org_id,
1532                  oelines.cust_po_number,
1533                  oelines.industry_attribute1,
1534                  oelines.invoice_to_org_id,
1535                  oelines.actual_shipment_date,
1536                  wdel.requested_quantity_uom, -- Bug 4439006
1537                  oelines.order_quantity_uom,
1538                  oelines.veh_cus_item_cum_key_id,
1539                  oelines.source_document_id,
1540                  oelines.source_document_line_id,
1541                  oeLines.org_id;
1542         --
1543 	l_oe_line_tbl_out   oe_order_pub.line_tbl_type;
1544 	--
1545  BEGIN
1546    --
1547    IF (l_debug <> -1) THEN
1548       rlm_core_sv.start_debug;
1549       rlm_core_sv.dpush(C_SDEBUG, 'UpdateCumKey');
1550    END IF;
1551    --
1552    savepoint updatecumkey; --bug 3719088
1553 
1554 -- Check for mandatory input
1555    --
1556    IF x_trip_stop_id IS NULL THEN
1557 	--
1558 	RAISE e_null_mandatory;
1559 	--
1560    END IF;
1561    --
1562 -- Find out how many oe headers assigned to the trip stop
1563    --
1564    OPEN c_oe_header;
1565 	--
1566    	LOOP
1567 	   --
1568 	   FETCH c_oe_header INTO v_oe_header(hdr_count).header_id, v_oe_header(hdr_count).org_id;
1569 	   --
1570 	   EXIT WHEN c_oe_header%NOTFOUND;
1571 	   --
1572 	   hdr_count := hdr_count + 1;
1573 	   --
1574            IF (l_debug <> -1) THEN
1575    	     rlm_core_sv.dlog(C_DEBUG, 'Inside c_oe_header Loop');
1576 	   END IF;
1577 	   --
1578    	END LOOP;
1579 	--
1580    CLOSE c_oe_header;
1581    --
1582    IF (l_debug <> -1) THEN
1583       rlm_core_sv.dlog(C_DEBUG, 'Number of oe headers to update: ', hdr_count);
1584    END IF;
1585    --
1586 --
1587 IF hdr_count = 0 THEN
1588 	--
1589 	RAISE e_no_shipment_line;
1590 	--
1591 END IF;
1592 --
1593 ----------------------------------------------------------+
1594 FOR v_loop_count IN 0 .. (hdr_count-1) LOOP
1595 --
1596 -- LOOP to fetch distinct header ids
1597 --
1598    IF (l_debug <> -1) THEN
1599       rlm_core_sv.dlog(C_DEBUG, 'v_loop_count', v_loop_count);
1600       rlm_core_sv.dlog(C_DEBUG, 'Inside v_loop_count Loop');
1601    END IF;
1602    --
1603  --Reset counter
1604    counter := 0;
1605    g_oe_line_tbl := oe_order_pub.g_miss_line_tbl;
1606    --
1607  --Get the header_id for each loop iteration
1608    --
1609    v_oe_header_id := v_oe_header(v_loop_count).header_id;
1610    v_org_id := v_oe_header(v_loop_count).org_id;
1611    --
1612    IF (l_debug <> -1) THEN
1613       rlm_core_sv.dlog(C_DEBUG, 'v_oe_header_id', v_oe_header_id);
1614       rlm_core_sv.dlog(C_DEBUG, 'v_org_id', v_org_id);
1615    END IF;
1616    --
1617    MO_GLOBAL.set_policy_context(p_access_mode => 'S',
1618                                 p_org_id      => v_org_id);
1619    --
1620 /* For the supplied delivery detail id, find all shipment lines and
1621    attributes needed to calculate cum key */
1622    --
1623    ---------------+
1624    OPEN c_oe_lines;
1625    --
1626    LOOP --LOOP to fetch OE Order Lines
1627    	--DEBUGGING
1628 	--
1629         IF (l_debug <> -1) THEN
1630    	  rlm_core_sv.dlog(C_DEBUG, 'Inside c_oe_lines Loop');
1631 	END IF;
1632         --
1633    	v_msg_text := NULL;
1634         --
1635         -- Reset loop variables so as to not retain values from prior fetch
1636 	--
1637         v_line_id := NULL;
1638         v_delivery_id := NULL;
1639         v_header_id := NULL;
1640         v_cum_record.shipped_quantity := NULL;
1641         v_cum_record.actual_shipment_date := NULL;
1642         v_cum_key_record.customer_item_id := NULL;
1643         v_cum_key_record.inventory_item_id := NULL;
1644         v_ship_to_site_use_id := NULL;
1645         v_intrmd_ship_to_site_use_id := NULL;
1646         v_cum_key_record.ship_from_org_id := NULL;
1647         v_cum_key_record.purchase_order_number := NULL;
1648         v_cum_key_record.cust_record_year := NULL;
1649         v_bill_to_site_use_id := NULL;
1650         v_cum_record.as_of_date_time := NULL;
1651         v_Primary_UOM_Code := NULL;         -- Bug 4439006
1652         v_cum_record.cust_uom_code := NULL;
1653         v_veh_cus_item_cum_key_id := NULL;
1654         v_schedule_header_id := NULL;
1655         v_schedule_line_id := NULL;
1656         v_org_id := NULL;
1657         --
1658 	FETCH c_oe_lines INTO	v_line_id,
1659 				v_delivery_id,
1660 				v_header_id,
1661 				v_cum_record.shipped_quantity,
1662 				v_cum_record.actual_shipment_date,
1663 				v_cum_key_record.customer_item_id,
1664                                 v_cum_key_record.inventory_item_id,
1665 				v_ship_to_site_use_id,
1666 				v_intrmd_ship_to_site_use_id,
1667 				v_cum_key_record.ship_from_org_id,
1668 				v_cum_key_record.purchase_order_number,
1669 				v_cum_key_record.cust_record_year,
1670 				v_bill_to_site_use_id,
1671 				v_cum_record.as_of_date_time,
1672                                 v_Primary_UOM_Code,         -- Bug 4439006
1673 				v_cum_record.cust_uom_code,
1674 				v_veh_cus_item_cum_key_id,
1675 				v_schedule_header_id,
1676 				v_schedule_line_id,
1677                                 v_org_id;
1678 	--
1679 	EXIT WHEN c_oe_lines%NOTFOUND;
1680 	--
1681 	v_level := 'SHIP_TO_ADDRESS_ID';
1682 	--
1683      -- Find the ship_to_address_id
1684 	--
1685 	IF v_ship_to_site_use_id IS NOT NULL THEN
1686 		--
1687                 -- Following query is changed as per TCA obsolescence project.
1688 		SELECT  CUST_ACCT_SITE_ID
1689 		INTO 	v_cum_key_record.ship_to_address_id
1690 		FROM	HZ_CUST_SITE_USES
1691 		WHERE	site_use_id = v_ship_to_site_use_id
1692                 AND	site_use_code = 'SHIP_TO';
1693 		--
1694 	ELSE
1695 		--
1696 		v_cum_key_record.ship_to_address_id := NULL;
1697 		--
1698 	END IF;
1699 	--
1700 	v_level := 'INTRMD_SHIP_TO_ADDRESS_ID';
1701 	--
1702      -- Find the intrmd_ship_to_address_id
1703 	--
1704 	IF v_intrmd_ship_to_site_use_id IS NOT NULL THEN
1705 		--
1706                 -- Following query is changed as per TCA obsolescence project.
1707 		SELECT  CUST_ACCT_SITE_ID
1708 		INTO 	v_cum_key_record.intrmd_ship_to_address_id
1709 		FROM	HZ_CUST_SITE_USES
1710 		WHERE	site_use_id = v_intrmd_ship_to_site_use_id
1711                 AND	site_use_code = 'SHIP_TO';
1712 		--
1713 	ELSE
1714 		--
1715 		v_cum_key_record.intrmd_ship_to_address_id := NULL;
1716 		--
1717 	END IF;
1718 	--
1719 	v_level := 'BILL_TO_ADDRESS_ID';
1720 	--
1721      -- Find the bill_to_address_id
1722 	--
1723 	IF v_bill_to_site_use_id IS NOT NULL THEN
1724 		--
1725                 -- Following query is changed as per TCA obsolescence project.
1726 		SELECT  CUST_ACCT_SITE_ID
1727 		INTO 	  v_cum_key_record.bill_to_address_id
1728 		FROM	  HZ_CUST_SITE_USES
1729 		WHERE	  site_use_id = v_bill_to_site_use_id
1730 		AND	  site_use_code = 'BILL_TO';
1731 		--
1732 	ELSE
1733 		--
1734 		v_cum_key_record.bill_to_address_id := NULL;
1735 		--
1736 	END IF;
1737 	--
1738       --DEBUGGING - INPUT Values
1739         IF (l_debug <> -1) THEN
1740            rlm_core_sv.dlog(C_DEBUG, '');
1741            rlm_core_sv.dlog(C_DEBUG, 'HERE ARE THE INPUT VALUES:');
1742            rlm_core_sv.dlog(C_DEBUG, '--------------------------');
1743            rlm_core_sv.dlog(C_DEBUG, 'x_trip_stop_id', x_trip_stop_id);
1744            rlm_core_sv.dlog(C_DEBUG, 'v_delivery_id', v_delivery_id);
1745            rlm_core_sv.dlog(C_DEBUG, 'v_line_id', v_line_id);
1746            rlm_core_sv.dlog(C_DEBUG, 'ship_to_address_id', v_cum_key_record.ship_to_address_id);
1747            rlm_core_sv.dlog(C_DEBUG, 'ship_from_org_id', v_cum_key_record.ship_from_org_id);
1748            rlm_core_sv.dlog(C_DEBUG, 'bill_to_address_id', v_cum_key_record.bill_to_address_id);
1749            rlm_core_sv.dlog(C_DEBUG, 'intrmd_ship_to_address_id', v_cum_key_record.intrmd_ship_to_address_id);
1750            rlm_core_sv.dlog(C_DEBUG, 'customer_item_id', v_cum_key_record.customer_item_id);
1751            rlm_core_sv.dlog(C_DEBUG, 'purchase_order_number', v_cum_key_record.purchase_order_number);
1752            rlm_core_sv.dlog(C_DEBUG, 'cust_record_year', v_cum_key_record.cust_record_year);
1753            rlm_core_sv.dlog(C_DEBUG, 'create_cum_key', v_cum_key_record.create_cum_key_flag);
1754            rlm_core_sv.dlog(C_DEBUG, 'actual_shipment_date', v_cum_record.actual_shipment_date);
1755            rlm_core_sv.dlog(C_DEBUG, 'shipped_quantity', v_cum_record.shipped_quantity);
1756         END IF;
1757 	--
1758      -- User friendly message
1759      -- TODO: Make the message more user friendly
1760      --
1761 	rlm_message_sv.get_msg_text(
1762 	  		x_message_name	=> 'RLM_TRIP_DEL_INFO',
1763 	  		x_text		=> v_msg_text,
1764 			x_token1	=> 'TRIP_STOP',
1765 			x_value1	=> x_trip_stop_id,
1766 			x_token2	=> 'DELIVERY',
1767 			x_value2	=> v_delivery_id,
1768 			x_token3	=> 'OE_LINE',
1769 			x_value3	=> v_line_id,
1770 			x_token4	=> 'SHIP_DATE',
1771 			x_value4	=> v_cum_record.actual_shipment_date,
1772 			x_token5	=> 'SHIP_QTY',
1773 			x_value5	=> v_cum_record.shipped_quantity);
1774 	  --
1775 	  fnd_file.put_line(fnd_file.log, v_msg_text);
1776 	  --
1777       --Find customer_id, as it is a required input parameter for rlm_setup_terms
1778 	--
1779 	v_level := 'CUSTOMER_ID';
1780 	--
1781         -- Following query is changed as per TCA obsolescence project.
1782 /*		SELECT	DISTINCT ACCT_SITE.CUST_ACCOUNT_ID
1783 		INTO	v_cum_key_record.customer_id
1784 		FROM	HZ_CUST_ACCT_SITES	 ACCT_SITE
1785 		WHERE	ACCT_SITE.CUST_ACCT_SITE_ID = v_cum_key_record.ship_to_address_id;
1786 */
1787                 -- CR changes
1788                 SELECT  DISTINCT sold_to_org_id
1789                 INTO    v_cum_key_record.customer_id
1790                 FROM    oe_order_headers_all oeh
1791                 WHERE   oeh.header_id = v_oe_header_id;
1792 
1793 	--
1794       --DEBUGGING
1795 	--
1796         IF (l_debug <> -1) THEN
1797            rlm_core_sv.dlog(C_DEBUG, 'customer_id', v_cum_key_record.customer_id);
1798         END IF;
1799 	--
1800       --Get the CUM_CONTROL_CODE defined at CUSTOMER or ADDRESS level
1801 	--
1802 	RLM_TPA_SV.get_setup_terms(
1803 		x_ship_from_org_id 	     	=> v_cum_key_record.ship_from_org_id,
1804 		x_customer_id 	     		=> v_cum_key_record.customer_id,
1805 	    	x_ship_to_address_id     	=> v_cum_key_record.ship_to_address_id,
1806 	    	x_customer_item_id	     	=> v_cum_key_record.customer_item_id,
1807 	    	x_terms_definition_level	=> v_terms_level,
1808 	    	x_terms_rec	 	     	=> v_rlm_setup_terms_record,
1809 	    	x_return_message	     	=> v_setup_terms_msg,
1810 	    	x_return_status 	     	=> v_setup_terms_status);
1811 	--
1812       --DEBUGGING
1813         IF (l_debug <> -1) THEN
1814            rlm_core_sv.dlog(C_DEBUG, 'Setup terms x_return_status', v_setup_terms_status);
1815            rlm_core_sv.dlog(C_DEBUG, 'Terms level', v_terms_level);
1816            rlm_core_sv.dlog(C_DEBUG, 'cum_control_code', v_rlm_setup_terms_record.cum_control_code);
1817            rlm_core_sv.dlog(C_DEBUG, 'ship_to_address_id', v_cum_key_record.ship_to_address_id);
1818            rlm_core_sv.dlog(C_DEBUG, 'ship_from_org_id', v_cum_key_record.ship_from_org_id);
1819            rlm_core_sv.dlog(C_DEBUG, 'customer_id', v_cum_key_record.customer_id);
1820            rlm_core_sv.dlog(C_DEBUG, 'customer_item_id', v_cum_key_record.customer_item_id);
1821            rlm_core_sv.dlog(C_DEBUG, 'calc_cum_flag', v_rlm_setup_terms_record.calc_cum_flag);
1822         END IF;
1823 	--
1824      -- User friendly message
1825 	--
1826 	rlm_message_sv.get_msg_text(
1827 	  		x_message_name	=> 'RLM_TRIP_CTRLCD',
1828 	  		x_text		=> v_msg_text,
1829 			x_token1	=> 'CUM_CONTROL_CODE',
1830 			x_value1	=> v_rlm_setup_terms_record.cum_control_code);
1831 	--
1832 	fnd_file.put_line(fnd_file.log, v_msg_text);
1833 	--
1834       --Continue only if cum_control_code is found and not set to NO_CUM
1835 	--
1836 	IF v_setup_terms_status = TRUE AND
1837 	   v_rlm_setup_terms_record.cum_control_code <> 'NO_CUM' THEN
1838 	       --
1839                IF (v_terms_level = 'CUSTOMER_ITEM' AND
1840 		   v_rlm_setup_terms_record.calc_cum_flag = 'Y')
1841                    OR v_terms_level <> 'CUSTOMER_ITEM' THEN
1842 		      --
1843 	              --DEBUGGING
1844 			--
1845                         IF (l_debug <> -1) THEN
1846                            rlm_core_sv.dlog(C_DEBUG, 'Setup terms x_return_status at item level', v_setup_terms_status);
1847                         END IF;
1848 			--
1849 		      --Find the cum start date
1850 			--
1851 			GetCumStartDate(i_schedule_header_id => v_schedule_header_id,
1852 					i_schedule_line_id   => v_schedule_line_id,
1853 					o_cum_start_date     => v_cum_key_record.cum_start_date,
1854 					o_cust_record_year   => v_tmp_cust_record_year,
1855 					o_return_message     => v_tmp_return_message,
1856 					o_return_status	     => v_tmp_return_status);
1857 			--
1858 			IF v_tmp_return_status = FALSE THEN
1859 					--
1860 					RAISE e_cum_start_date;
1861 					--
1862 			END IF;
1863 			--
1864                         IF (l_debug <> -1) THEN
1865    		          rlm_core_sv.dlog(C_DEBUG, 'cum_start_date', v_cum_key_record.cum_start_date);
1866 		        END IF;
1867 			--
1868 		     /* Call calculate_cum_key procedure to search for the cum
1869 		        key id, and the cum quantity stored in the database as
1870 		        of the time of shipment */
1871 			--
1872 		   	RLM_TPA_SV.CalculateCumKey(
1873 					x_cum_key_record => v_cum_key_record,
1874 					x_cum_record     => v_cum_record);
1875 			--
1876 			IF v_cum_record.record_return_status <> FALSE THEN
1877 				     --
1878 				     -- User friendly message
1879 				     --
1880 			  		rlm_message_sv.get_msg_text(
1881 				  		x_message_name	=> 'RLM_TRIP_CUM_KEY_ID',
1882 				  		x_text		=> v_msg_text,
1883 						x_token1	=> 'CUM_KEY_ID',
1884 						x_value1	=> v_cum_record.cum_key_id);
1885 					--
1886 	      		  		fnd_file.put_line(fnd_file.log, v_msg_text);
1887 					--
1888 			ELSE
1889 				--
1890 				RAISE e_general_error;
1891 				--
1892 			END IF;
1893 
1894 			--
1895                         IF(v_cum_record.cum_key_id IS NULL) THEN
1896                           --
1897                           raise e_no_cum_key;
1898                           --
1899                         END IF;
1900 
1901 		      --Check if this delivery line has actually been updated
1902 			--
1903 		     /* Initialize v_new_ship_count. This variable is used to keep track of the
1904 		        shipment lines that have the same cum_key_id and
1905 		        shipment_rule_code = 'AS_OF_PRIOR' */
1906 		        ------------------------------------------------------------+
1907                         BEGIN
1908 			   --
1909 			   SELECT v_new_ship_count(v_cum_record.cum_key_id) + 1
1910 			   INTO v_new_ship_count(v_cum_record.cum_key_id)
1911 			   FROM DUAL;
1912 			   --
1913                         EXCEPTION
1914 			   --
1915 			   WHEN NO_DATA_FOUND THEN
1916 				--
1917 				v_new_ship_count(v_cum_record.cum_key_id) := 0;
1918 				--
1919 				SELECT v_new_ship_count(v_cum_record.cum_key_id) + 1
1920 			   	INTO v_new_ship_count(v_cum_record.cum_key_id)
1921 			   	FROM DUAL;
1922 				--
1923 	    	        END;
1924 		        ------------------------------------------------------------+
1925 			--
1926 		        IF NVL(v_veh_cus_item_cum_key_id,0) <> v_cum_record.cum_key_id THEN
1927 			  --
1928 			  /* Need to set this to 'Y' since we're going to include the
1929 			     current shipment into the supplier CUM calculation */
1930 			     v_cum_record.use_ship_incl_rule_flag := 'Y';
1931 			     v_cum_record.as_of_date_time := NULL;
1932 
1933 			  /* Bug 4439006: Start
1934                              While ship confirming, shipped qty is specified in Primary Uom,
1935                              where as the Uom attached with the CUM Key can be different.
1936                              The Cum key should be updated with qty specified in Cum key uom. */
1937                           --
1938                           IF (l_debug <> -1) THEN
1939                               rlm_core_sv.dlog(C_DEBUG, 'inventory_item_id', v_cum_key_record.inventory_item_id);
1940                               rlm_core_sv.dlog(C_DEBUG, 'ship_from_org_id', v_cum_key_record.ship_from_org_id);
1941                           END IF;
1942                           --
1943                           v_shipped_qty := v_cum_record.shipped_quantity;
1944                           --
1945                           IF (v_cum_record.cust_uom_code <> v_Primary_UOM_Code) THEN
1946                             RLM_FORECAST_SV.Convert_UOM (v_Primary_UOM_Code,
1947                                                          v_cum_record.cust_uom_code,
1948                                                          v_cum_record.shipped_quantity,
1949                                                          v_cum_key_record.inventory_item_id,
1950                                                          v_cum_key_record.ship_from_org_id);
1951                             --
1952                             IF (l_debug <> -1) THEN
1953                               rlm_core_sv.dlog(C_DEBUG, 'Primary Uom', v_Primary_UOM_Code);
1954                               rlm_core_sv.dlog(C_DEBUG, 'Cum Key Uom', v_cum_record.cust_uom_code);
1955                               rlm_core_sv.dlog(C_DEBUG, 'Shipd Qty.-Before Conversion to CUM Uom', v_shipped_qty);
1956                               rlm_core_sv.dlog(C_DEBUG, 'Shipd Qty.-After Conversion -CUM Uom', v_cum_record.shipped_quantity);
1957                               rlm_core_sv.dlog(C_DEBUG, 'Primary Uom and Cum Key Uom differ: Calling Convert_UOM');
1958                             END IF;
1959                             --
1960                           END IF;
1961 			  /* Bug 4439006: End */
1962 			  --
1963 		          /* Call calculate_supplier_cum procedure to calculate the
1964 			     total supplier cum by summing the current line shipped
1965 			     quantity and the stored quantity */
1966 			     --
1967 			     RLM_TPA_SV.CalculateSupplierCum(
1968 					x_new_ship_count => v_new_ship_count,
1969 					x_cum_key_record => v_cum_key_record,
1970 					x_cum_record     => v_cum_record);
1971 			     --
1972                              -- Bug 4439006
1973                              v_cum_record.shipped_quantity := v_shipped_qty;
1974 
1975 			   --DEBUGGING - OUTPUT Values
1976                              IF (l_debug <> -1) THEN
1977    			       rlm_core_sv.dlog(C_DEBUG, '');
1978    		   	       rlm_core_sv.dlog(C_DEBUG, 'HERE ARE THE OUTPUT VALUES:');
1979    		   	       rlm_core_sv.dlog(C_DEBUG, '---------------------------');
1980    		   	       rlm_core_sv.dlog(C_DEBUG, 'cum_key_id', v_cum_record.cum_key_id);
1981    		   	       rlm_core_sv.dlog(C_DEBUG, 'cum_start_date', v_cum_record.cum_start_date);
1982     		   	       rlm_core_sv.dlog(C_DEBUG, 'shipped_quantity', v_cum_record.shipped_quantity);
1983    		   	       rlm_core_sv.dlog(C_DEBUG, 'actual_shipment_date', v_cum_record.actual_shipment_date);
1984    		   	       rlm_core_sv.dlog(C_DEBUG, 'cum_key_created_flag', v_cum_record.cum_key_created_flag);
1985    			       rlm_core_sv.dlog(C_DEBUG, 'cum_qty', v_cum_record.cum_qty);
1986    		   	       rlm_core_sv.dlog(C_DEBUG, 'as_of_date_cum_qty', v_cum_record.as_of_date_cum_qty);
1987    		   	       rlm_core_sv.dlog(C_DEBUG, 'cum_qty_to_be_accumulated', v_cum_record.cum_qty_to_be_accumulated);
1988    		   	       rlm_core_sv.dlog(C_DEBUG, 'cum_qty_after_cutoff', v_cum_record.cum_qty_after_cutoff);
1989    		   	       rlm_core_sv.dlog(C_DEBUG, 'last_cum_qty_update_date', v_cum_record.last_cum_qty_update_date);
1990    		   	       rlm_core_sv.dlog(C_DEBUG, 'cust_uom_code', v_cum_record.cust_uom_code);
1991                                rlm_core_sv.dlog(C_DEBUG, 'Primary_uom_code', v_Primary_UOM_Code);
1992    		   	       rlm_core_sv.dlog(C_DEBUG, 'use_ship_incl_rule_flag', v_cum_record.use_ship_incl_rule_flag);
1993    		   	       rlm_core_sv.dlog(C_DEBUG, 'shipment_rule_code', v_cum_record.shipment_rule_code);
1994    		   	       rlm_core_sv.dlog(C_DEBUG, 'record_return_status', v_cum_record.record_return_status);
1995 			     END IF;
1996 			     --
1997                              --Bug 3688778 jckwok : Need to Lock the Cum Key
1998 			     --
1999 		   	     IF (LockCumKey(v_cum_record.cum_key_id) AND
2000                                            v_cum_record.record_return_status = TRUE) THEN
2001 
2002 				 /* Update the CUM related quantity in
2003 				    RLM_CUS_ITEM_CUM_KEY_ALL table */
2004 				    --
2005 				    UPDATE RLM_CUST_ITEM_CUM_KEYS_ALL
2006 				    SET    cum_qty = v_cum_record.cum_qty,
2007 			  	           cum_qty_to_be_accumulated = v_cum_record.cum_qty_to_be_accumulated,
2008 			  	           cum_qty_after_cutoff = v_cum_record.cum_qty_after_cutoff,
2009 			  		   last_cum_qty_update_date = sysdate,
2010 			  		   last_update_login = fnd_global.login_id,
2011 			  		   last_update_date = sysdate,
2012 			  		   last_updated_by = fnd_global.user_id
2013 				    WHERE  cum_key_id = v_cum_record.cum_key_id;
2014 				    --
2015  				    v_upd_indicator := TRUE;
2016 				    --
2017 		   	         /* Populate g_oe_line_tbl data structure with the
2018 		      	            calculated cum_key_id so it can be used when
2019                       	            oe_order_grp.process order api to update
2020                       	            oe_order_lines table */
2021 				    --
2022 			            counter := counter + 1;
2023 				    -- Bug# 1466909
2024 				    g_oe_line_tbl(counter) := OE_Order_PUB.G_MISS_LINE_REC;
2025 			            g_oe_line_tbl(counter).header_id := v_header_id;
2026 			            g_oe_line_tbl(counter).line_id := v_line_id;
2027 	  	   	            g_oe_line_tbl(counter).operation := oe_globals.G_OPR_UPDATE;
2028 		   	            g_oe_line_tbl(counter).veh_cus_item_cum_key_id := v_cum_record.cum_key_id;
2029 		   	            g_oe_line_tbl(counter).industry_attribute7 := TO_CHAR(v_cum_record.cum_qty);
2030 		   	            g_oe_line_tbl(counter).industry_attribute8 := v_cum_record.cust_uom_code;
2031                                     g_oe_line_tbl(counter).org_id := v_org_id;
2032 				    --
2033 			          --DEBUGGING
2034                                     IF (l_debug <> -1) THEN
2035            		              rlm_core_sv.dlog(C_DEBUG, 'counter', counter);
2036            		              rlm_core_sv.dlog(C_DEBUG, 'g_oe_line_tbl(counter).header_id',
2037 								g_oe_line_tbl(counter).header_id);
2038           			      rlm_core_sv.dlog(C_DEBUG, 'g_oe_line_tbl(counter).line_id',
2039 								g_oe_line_tbl(counter).line_id);
2040            		              rlm_core_sv.dlog(C_DEBUG,'g_oe_line_tbl(counter).veh_cus_item_cum_key_id',
2041 								g_oe_line_tbl(counter).veh_cus_item_cum_key_id);
2042            		              rlm_core_sv.dlog(C_DEBUG, 'g_oe_line_tbl(counter).industry_attribute7',
2043 								g_oe_line_tbl(counter).industry_attribute7);
2044            		              rlm_core_sv.dlog(C_DEBUG, 'g_oe_line_tbl(counter).industry_attribute8',
2045 								g_oe_line_tbl(counter).industry_attribute8);
2046            		              rlm_core_sv.dlog(C_DEBUG, 'g_oe_line_tbl(counter).org_id',
2047 								g_oe_line_tbl(counter).org_id);
2048         		            END IF;
2049 				    --
2050 			     ELSE
2051 				    --
2052 				    RAISE e_general_error;
2053 				    --
2054 		   	     END IF; 	-- record_return_status = TRUE
2055 			     --
2056 		      ELSE
2057 			     --
2058 			     x_return_status := TRUE;
2059 			     --
2060 			     v_msg_text := 'RLM_TRIP_NO_UPDATE';
2061 			     --
2062                              IF (l_debug <> -1) THEN
2063     		  	       rlm_core_sv.dlog(C_SDEBUG, v_msg_text);
2064  		  	     END IF;
2065 			     --
2066 		       	  -- User friendly message
2067 			     --
2068 			     rlm_message_sv.get_msg_text(
2069 				  x_message_name	=> 'RLM_TRIP_NO_UPDATE',
2070 				  x_text		=> v_msg_text);
2071 			     --
2072 			     fnd_file.put_line(fnd_file.log, v_msg_text);
2073 			     --
2074 			  -- Pls do not remove the line below
2075 			     --
2076 			     v_msg_text := 'RLM_TRIP_NO_UPDATE';
2077 
2078 		      END IF; 		-- cum key id
2079 		      --
2080 	       ELSE
2081 		      --
2082 		      x_return_status := TRUE;
2083 		      --
2084 	       END IF; 			-- v_calc_cum_flag = 'Y'
2085 	       --
2086 	ELSE
2087 	   --
2088 	   x_return_status := TRUE;
2089 	   --
2090 	   -- RLM_TRIP_NO_SETUP
2091 	   --
2092            IF (l_debug <> -1) THEN
2093     	       rlm_core_sv.dlog(C_SDEBUG, 'RLM_TRIP_NO_SETUP');
2094  	   END IF;
2095 	   --
2096 	   -- User friendly message
2097 			  --
2098 			  rlm_message_sv.get_msg_text(
2099 				  x_message_name	=> 'RLM_TRIP_NO_SETUP',
2100 				  x_text		=> v_msg_text);
2101 			  --
2102 	      		  fnd_file.put_line(fnd_file.log, v_msg_text);
2103 			  --
2104    	END IF; -- v_cum_control_code <> 'NO_CUM'
2105 	--
2106    END LOOP; -- END LOOP to fetch OE Order Lines
2107    --
2108    CLOSE c_oe_lines;
2109    ----------------+
2110    --
2111    IF (l_debug <> -1) THEN
2112       rlm_core_sv.dlog(C_DEBUG, 'v_msg_text', v_msg_text);
2113       rlm_core_sv.dlog(C_DEBUG, 'v_upd_indicator', v_upd_indicator);
2114    END IF;
2115    --
2116    IF v_upd_indicator = TRUE THEN
2117        --
2118        -----------------------------------------------------------------------+
2119        BEGIN
2120        /* Call OE_Order_GRP.Process_Order procedure to update OE_ORDER_LINES
2121   	  table by passing the g_oe_line_tbl structure that has been prepared
2122   	  inside the loop above */
2123 	--
2124         --Pass only g_oe_line_tbl. The rest uses default values
2125 	--
2126   	  OE_Order_GRP.Process_order(
2127 	  	p_api_version_number     => x_oe_api_version,
2128   		p_init_msg_list          => FND_API.G_TRUE,
2129   		p_return_values          => FND_API.G_FALSE,
2130   		--p_commit                 => FND_API.G_FALSE,
2131   		p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
2132   		x_return_status          => l_return_status,
2133   		x_msg_count              => x_msg_count,
2134  		x_msg_data               => x_msg_data,
2135 		------------------------------------------
2136   		p_line_tbl               => g_oe_line_tbl,
2137 		------------------------------------------
2138   		x_header_rec             => g_oe_header_out_rec,
2139   		x_header_val_rec         => g_oe_header_val_out_rec,
2140   		x_Header_Adj_tbl         => g_oe_Header_Adj_out_tbl,
2141   		x_Header_Adj_val_tbl	 => g_oe_Header_Adj_val_out_tbl,
2142    		x_Header_price_Att_tbl   => g_Header_price_Att_out_tbl,
2143     		x_Header_Adj_Att_tbl     => g_Header_Adj_Att_out_tbl,
2144     		x_Header_Adj_Assoc_tbl   => g_Header_Adj_Assoc_out_tbl,
2145   		x_Header_Scredit_tbl     => g_oe_Header_Scredit_out_tbl,
2146   		x_Header_Scredit_val_tbl => g_oe_Hdr_Scdt_val_out_tbl,
2147   		x_line_tbl               => l_oe_line_tbl_out,
2148   		x_line_val_tbl           => g_oe_line_val_out_tbl,
2149   		x_Line_Adj_tbl           => g_oe_line_Adj_out_tbl,
2150   		x_Line_Adj_val_tbl       => g_oe_line_Adj_val_out_tbl,
2151     		x_Line_price_Att_tbl     => g_Line_price_Att_out_tbl,
2152     		x_Line_Adj_Att_tbl       => g_Line_Adj_Att_out_tbl,
2153     		x_Line_Adj_Assoc_tbl     => g_Line_Adj_Assoc_out_tbl,
2154   		x_Line_Scredit_tbl       => g_oe_line_scredit_out_tbl,
2155   		x_Line_Scredit_val_tbl   => g_oe_line_scredit_val_out_tbl,
2156   		x_Lot_Serial_tbl         => g_oe_lot_serial_out_tbl,
2157   		x_Lot_Serial_val_tbl     => g_oe_lot_serial_val_out_tbl,
2158   		x_Action_Request_tbl     => g_oe_Action_Request_out_Tbl);
2159 	--
2160         --Handle the exceptions caused by the OE call
2161  	--
2162           IF (l_debug <> -1) THEN
2163            rlm_core_sv.dlog(C_SDEBUG, 'Input tbl count', g_oe_line_tbl.LAST);
2164            rlm_core_sv.dlog(C_SDEBUG, 'Output tbl count', l_oe_line_tbl_out.LAST);
2165           END IF;
2166 	  --
2167   	  IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2168 		--
2169 		RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2170 		--
2171   	  ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
2172 		--
2173 		RAISE FND_API.G_EXC_ERROR;
2174 		--
2175   	  ELSE
2176 		--
2177 		x_return_status := TRUE;
2178 		--
2179 	      --DEBUGGING
2180                 IF (l_debug <> -1) THEN
2181    		  rlm_core_sv.dlog(C_SDEBUG,'l_return_status', l_return_status);
2182    		  rlm_core_sv.dlog(C_SDEBUG, 'x_msg_data', x_msg_data);
2183    		  rlm_core_sv.dlog(C_SDEBUG, 'x_msg_count', x_msg_count);
2184    		  rlm_core_sv.dlog(C_SDEBUG, 'Process Order is completed succesfully');
2185 		END IF;
2186 		--
2187  	  END IF;
2188 	  --
2189        EXCEPTION
2190 	     --
2191 	     WHEN FND_API.G_EXC_ERROR THEN
2192 		      --
2193 		      --Get message count and data
2194 		      --
2195 			OE_MSG_PUB.Count_And_Get(
2196 				p_count	=> x_msg_count,
2197 				p_data  => x_msg_data);
2198 			--
2199 			x_return_status := FALSE;
2200 			--
2201 			ROLLBACK to updatecumkey;
2202 			--
2203 		     -- DEBUGGING
2204                         IF (l_debug <> -1) THEN
2205    			  rlm_core_sv.dlog(C_SDEBUG, 'G_EXC_ERROR');
2206    			  rlm_core_sv.dlog(C_SDEBUG, 'l_return_status', l_return_status);
2207    			  rlm_core_sv.dlog(C_SDEBUG, 'x_return_status', x_return_status);
2208    			  rlm_core_sv.dlog(C_SDEBUG, 'x_msg_count', x_msg_count);
2209    			  rlm_core_sv.dlog(C_SDEBUG, 'Main x_msg_data', x_msg_data);
2210 			END IF;
2211 			--
2212 			IF x_msg_count > 0 THEN
2213 			  --
2214 			  FOR k in 1 .. x_msg_count LOOP
2215 				--
2216         			x_msg_data := oe_msg_pub.get( p_msg_index => k,
2217                         				      p_encoded   => 'F');
2218 				--
2219 				fnd_file.put_line(fnd_file.log, x_msg_data);
2220 			        --
2221                                 IF (l_debug <> -1) THEN
2222    			          rlm_core_sv.dlog(C_SDEBUG, 'x_msg_data', x_msg_data);
2223 			        END IF;
2224 				--
2225                           END LOOP;
2226 			  --
2227 			END IF;
2228 			--
2229                         IF (l_debug <> -1) THEN
2230    			  rlm_core_sv.dpop(C_SDEBUG, 'Process Order Error');
2231 			END IF;
2232 			--
2233 		     --
2234 		     -- User friendly message
2235 		     --
2236 			rlm_message_sv.get_msg_text(
2237 				x_message_name	=> 'RLM_CUM_PROCESS_ORDER',
2238 				x_text		=> v_msg_text);
2239 			--
2240 			fnd_file.put_line(fnd_file.log, v_msg_text);
2241 			--
2242                         IF (l_debug <> -1) THEN
2243    			  rlm_core_sv.stop_debug;
2244 			END IF;
2245 			--
2246 			RAISE e_general_error;
2247 			--
2248 	     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2249 		     --
2250 		     -- Get message count and data
2251 		     --
2252 			OE_MSG_PUB.Count_And_Get(
2253 				p_count	=> x_msg_count,
2254 				p_data  => x_msg_data);
2255 			--
2256 			v_msg_text := x_msg_data;
2257 			--
2258 			x_return_status := FALSE;
2259 			--
2260 			ROLLBACK to updatecumkey;
2261 			--
2262 		     -- DEBUGGING
2263                         IF (l_debug <> -1) THEN
2264    			  rlm_core_sv.dlog(C_SDEBUG, 'G_EXC_UNEXPECTED_ERROR');
2265    			  rlm_core_sv.dlog(C_SDEBUG, 'x_msg_data', x_msg_data);
2266    			  rlm_core_sv.dlog(C_SDEBUG, 'l_return_status', l_return_status);
2267    			  rlm_core_sv.dlog(C_SDEBUG, 'x_return_status', x_return_status);
2268    			  rlm_core_sv.dlog(C_SDEBUG, 'x_msg_count', x_msg_count);
2269    			  rlm_core_sv.dlog(C_SDEBUG, 'Main x_msg_data', x_msg_data);
2270 			END IF;
2271 			--
2272 			IF x_msg_count > 0 THEN
2273 			  --
2274 			  FOR k in 1 .. x_msg_count LOOP
2275 				--
2276         			x_msg_data := oe_msg_pub.get( p_msg_index => k,
2277                         				      p_encoded   => 'F');
2278 				--
2279 				fnd_file.put_line(fnd_file.log, x_msg_data);
2280 				--
2281   				IF (l_debug <> -1) THEN
2282    			          rlm_core_sv.dlog(C_SDEBUG, 'x_msg_data', x_msg_data);
2283 			        END IF;
2284 				--
2285                           END LOOP;
2286 			  --
2287 			END IF;
2288 			--
2289   			IF (l_debug <> -1) THEN
2290    			  rlm_core_sv.dpop(C_SDEBUG, 'Process Order Error');
2291 			END IF;
2292 			--
2293 		     --
2294 		     -- User friendly message
2295 		     --
2296 			rlm_message_sv.get_msg_text(
2297 				x_message_name	=> 'RLM_CUM_PROCESS_ORDER',
2298 				x_text		=> v_msg_text);
2299 			--
2300 			fnd_file.put_line(fnd_file.log, v_msg_text);
2301 			--
2302   			IF (l_debug <> -1) THEN
2303    			  rlm_core_sv.stop_debug;
2304 			END IF;
2305 			--
2306 			RAISE e_general_error;
2307 			--
2308              WHEN OTHERS THEN
2309 		     --
2310 		     -- Get message count and data
2311 		     --
2312 			OE_MSG_PUB.Count_And_Get(
2313 				p_count	=> x_msg_count,
2314 				p_data  => x_msg_data);
2315 			--
2316 			v_msg_text := x_msg_data;
2317 			--
2318 			x_return_status := FALSE;
2319 			--
2320 			ROLLBACK to updatecumkey;
2321 			--
2322 		     -- DEBUGGING
2323   			IF (l_debug <> -1) THEN
2324    			  rlm_core_sv.dlog(C_SDEBUG, 'G_EXC_UNEXPECTED_ERROR');
2325    			  rlm_core_sv.dlog(C_SDEBUG, 'x_msg_data', x_msg_data);
2326    			  rlm_core_sv.dlog(C_SDEBUG, 'l_return_status', l_return_status);
2327    			  rlm_core_sv.dlog(C_SDEBUG, 'x_return_status', x_return_status);
2328    			  rlm_core_sv.dlog(C_SDEBUG, 'x_msg_count', x_msg_count);
2329 			END IF;
2330 			--
2331 			FOR k in 1 .. x_msg_count LOOP
2332 				--
2333         			x_msg_data := oe_msg_pub.get( p_msg_index => k,
2334                         				      p_encoded   => 'F');
2335 				--
2336 				fnd_file.put_line(fnd_file.log, x_msg_data);
2337 				--
2338   				IF (l_debug <> -1) THEN
2339    			          rlm_core_sv.dlog(C_SDEBUG, 'x_msg_data', x_msg_data);
2340 			        END IF;
2341 				--
2342                         END LOOP;
2343 			--
2344   			IF (l_debug <> -1) THEN
2345    			  rlm_core_sv.dpop(C_SDEBUG, 'Process Order Error. When Others');
2346 			END IF;
2347 			--
2348 			--
2349 		     -- User friendly message
2350 			--
2351 			rlm_message_sv.get_msg_text(
2352 				x_message_name	=> 'RLM_CUM_PROCESS_ORDER',
2353 				x_text		=> v_msg_text);
2354 			--
2355 			fnd_file.put_line(fnd_file.log, v_msg_text);
2356 			--
2357   			IF (l_debug <> -1) THEN
2358    			  rlm_core_sv.stop_debug;
2359 			END IF;
2360 			--
2361       END;
2362       --------------------------------------------------------------------------------+
2363    END IF;
2364    --
2365 END LOOP;			-- END LOOP to fetch distinct header ids
2366 --
2367  --DEBUGGING
2368    IF (l_debug <> -1) THEN
2369       rlm_core_sv.dlog(C_SDEBUG, 'x_return_status', x_return_status);
2370       rlm_core_sv.dpop(C_DEBUG, 'Completed successfully');
2371    END IF;
2372  --
2373  --User friendly message
2374  --
2375    rlm_message_sv.get_msg_text(
2376 		x_message_name	=> 'RLM_CUM_SUCCESS',
2377 		x_text		=> v_msg_text);
2378    --
2379    fnd_file.put_line(fnd_file.log, v_msg_text);
2380    --
2381    IF (l_debug <> -1) THEN
2382       rlm_core_sv.stop_debug;
2383    END IF;
2384    --
2385  EXCEPTION
2386    --
2387    WHEN NO_DATA_FOUND THEN
2388 	--
2389 	IF v_level = 'SHIP_TO_ADDRESS_ID' THEN
2390 		--
2391 		v_msg_text := 'No ship_to_address_id is associated with the ship_to_org_id';
2392 		--
2393 	ELSIF v_level = 'INTRMD_SHIP_TO_ADDRESS_ID' THEN
2394 		--
2395 		v_msg_text := 'No intrmd_ship_to_address_id is associated with the intmed_ship_to_org_id';
2396 		--
2397 	ELSIF v_level = 'CUSTOMER_ID' THEN
2398 		--
2399 		v_msg_text := 'No customer_id is associated with the address_id';
2400 		--
2401 	ELSIF v_level = 'BILL_TO_ADDRESS_ID' THEN
2402 		--
2403 		v_msg_text := 'No bill_to_address_id is associated with the bill_to_org_id';
2404 		--
2405 	END IF;
2406 	--
2407 	ROLLBACK to updatecumkey;
2408 	--
2409 	x_return_status := FALSE;
2410 	--
2411         IF (l_debug <> -1) THEN
2412           rlm_core_sv.dlog(C_SDEBUG, 'x_return_status', x_return_status);
2413       	  rlm_core_sv.dlog(C_SDEBUG, 'EXCEPTION: NO_DATA_FOUND');
2414       	  rlm_core_sv.dpop(C_SDEBUG, v_msg_text);
2415    	  rlm_core_sv.stop_debug;
2416 	END IF;
2417 	--
2418 	-- User-friendly message
2419 	--
2420 	IF v_level <> 'CUSTOMER_ID' THEN
2421 		--
2422 		rlm_message_sv.get_msg_text(
2423 			x_message_name	=> 'RLM_NO_ID_FOR_ORG',
2424 			x_text		=> v_msg_text);
2425 		--
2426 	ELSE
2427 		--
2428 		rlm_message_sv.get_msg_text(
2429 			x_message_name	=> 'RLM_NO_CUST_FOR_ADDRESS',
2430 			x_text		=> v_msg_text);
2431 		--
2432 	END IF;
2433 	--
2434 	fnd_file.put_line(fnd_file.log, v_msg_text);
2435 	--
2436    WHEN e_no_shipment_line THEN
2437         ----------------------------------------------------+
2438 	DECLARE
2439 		--
2440 		v_hr_location_code		hr_locations.location_code%TYPE;
2441 		--
2442         BEGIN
2443 		--
2444 		ROLLBACK to updatecumkey;
2445 		--
2446 		x_return_status := TRUE;
2447 		--
2448                 IF (l_debug <> -1) THEN
2449            	  rlm_core_sv.dlog(C_SDEBUG, 'x_return_status', x_return_status);
2450       		  rlm_core_sv.dlog(C_SDEBUG, 'EXCEPTION: e_no_shipment_line');
2451         	END IF;
2452 		--
2453 		SELECT 	location_code
2454 		INTO	v_hr_location_code
2455 		FROM 	hr_locations hr, wsh_trip_stops tstop
2456 		WHERE	tstop.stop_id	= x_trip_stop_id
2457 		AND	hr.location_id 	= tstop.stop_location_id;
2458 		--
2459  		rlm_message_sv.get_msg_text(
2460 				x_message_name	=> 'RLM_NO_SHIPMENT_LINE',
2461 				x_text		=> v_msg_text,
2462 				x_token1	=> 'TRIP_STOP',
2463 				x_value1	=> v_hr_location_code);
2464 		--
2465                 IF (l_debug <> -1) THEN
2466       		  rlm_core_sv.dpop(C_SDEBUG, v_msg_text);
2467    		END IF;
2468                 --
2469                 -- Bug 1306894: Shipping requests the removal of this
2470 		-- misleading message for non-RLM customers
2471 		--
2472 		-- fnd_file.put_line(fnd_file.log, v_msg_text);
2473 		--
2474                 IF (l_debug <> -1) THEN
2475    		  rlm_core_sv.stop_debug;
2476 		END IF;
2477 		--
2478 	EXCEPTION
2479 		--
2480 		WHEN OTHERS THEN
2481 			--
2482 	 		rlm_message_sv.get_msg_text(
2483 				x_message_name	=> 'RLM_NO_SHIPMENT_LINE',
2484 				x_text		=> v_msg_text,
2485 				x_token1	=> 'TRIP_STOP',
2486 				x_value1	=> NULL);
2487 			--
2488                         IF (l_debug <> -1) THEN
2489    	   		  rlm_core_sv.dpop(C_SDEBUG, v_msg_text);
2490    			  rlm_core_sv.stop_debug;
2491 			END IF;
2492 			--
2493 			fnd_file.put_line(fnd_file.log, v_msg_text);
2494 			--
2495 	END;
2496         -----------------------------------------------------------+
2497    WHEN e_null_mandatory THEN
2498 	--
2499 	ROLLBACK to updatecumkey;
2500 	--
2501 	x_return_status := FALSE;
2502 	--
2503 	rlm_message_sv.get_msg_text(
2504 		x_message_name	=> 'RLM_TRIP_STOP_REQUIRED',
2505 		x_text		=> v_msg_text);
2506 	--
2507         IF (l_debug <> -1) THEN
2508           rlm_core_sv.dlog(C_SDEBUG, 'x_return_status', x_return_status);
2509       	  rlm_core_sv.dlog(C_SDEBUG, 'EXCEPTION: e_null_mandatory');
2510       	  rlm_core_sv.dpop(C_SDEBUG, v_msg_text);
2511    	  rlm_core_sv.stop_debug;
2512         END IF;
2513         --
2514 	fnd_file.put_line(fnd_file.log, v_msg_text);
2515 	--
2516    WHEN e_cum_start_date THEN
2517 	--
2518 	ROLLBACK to updatecumkey;
2519 	--
2520 	x_return_status := FALSE;
2521 	--
2522         IF (l_debug <> -1) THEN
2523            rlm_core_sv.dlog(C_SDEBUG, 'x_return_status', x_return_status);
2524         END IF;
2525 	v_msg_text := v_tmp_return_message;
2526 	--
2527         IF (l_debug <> -1) THEN
2528       	  rlm_core_sv.dlog(C_SDEBUG, 'EXCEPTION: e_cum_start_date');
2529       	  rlm_core_sv.dpop(C_SDEBUG, v_msg_text);
2530    	  rlm_core_sv.stop_debug;
2531    	END IF;
2532 	--
2533 	fnd_file.put_line(fnd_file.log, v_msg_text);
2534 	--
2535    WHEN e_no_cum_key THEN
2536         ROLLBACK to updatecumkey;
2537         rlm_message_sv.get_msg_text(
2538 		x_message_name	=> 'RLM_NO_CUM_DISABLE_CUM_KEY',
2539 		x_text		=> v_msg_text);
2540 	--
2541 	x_return_status := FALSE;
2542 	--
2543         IF (l_debug <> -1) THEN
2544            rlm_core_sv.dlog(C_SDEBUG, 'x_return_status', x_return_status);
2545            rlm_core_sv.dlog(C_SDEBUG, 'EXCEPTION: e_no_cum_key');
2546       	   rlm_core_sv.dpop(C_SDEBUG);
2547    	   rlm_core_sv.stop_debug;
2548         END IF;
2549 	fnd_file.put_line(fnd_file.log, v_msg_text);
2550 
2551    WHEN e_general_error THEN
2552 	--
2553 	ROLLBACK to updatecumkey;
2554 	--
2555 	x_return_status := FALSE;
2556 	--
2557    WHEN OTHERS THEN
2558 	--
2559 	ROLLBACK to updatecumkey;
2560 	--
2561 	x_return_status := FALSE;
2562 	--
2563 	v_msg_text := SQLERRM;
2564 	--
2565         IF (l_debug <> -1) THEN
2566           rlm_core_sv.dlog(C_SDEBUG, 'x_return_status', x_return_status);
2567    	  rlm_core_sv.dlog(C_SDEBUG, 'EXCEPTION: OTHERS');
2568    	  rlm_core_sv.dpop(C_SDEBUG, v_msg_text);
2569    	  rlm_core_sv.stop_debug;
2570 	END IF;
2571 	--
2572 	fnd_file.put_line(fnd_file.log, v_msg_text);
2573 	--
2574  END UpdateCumKey;
2575 
2576 /*============================================================================
2577 
2578   PROCEDURE NAME:	UpdateCumKeyClient
2579 
2580 =============================================================================*/
2581 
2582  PROCEDURE UpdateCumKeyClient (
2583 	errbuf		OUT NOCOPY VARCHAR2,
2584 	retcode		OUT NOCOPY NUMBER,
2585 	x_trip_stop_id   IN  NUMBER)
2586  IS
2587    --
2588    v_return_status BOOLEAN;
2589    --
2590  BEGIN
2591   --
2592   IF (l_debug <> -1) THEN
2593     rlm_core_sv.start_debug;
2594   END IF;
2595   --
2596   RLM_TPA_SV.UpdateCumKey( x_trip_stop_id,
2597 		v_return_status);
2598   --
2599   IF (l_debug <> -1) THEN
2600     rlm_core_sv.stop_debug;
2601   END IF;
2602   --
2603  END UpdateCumKeyClient;
2604 
2605 /*============================================================================
2606 
2607   PROCEDURE NAME:	ResetCumClient
2608 
2609 =============================================================================*/
2610 
2611 PROCEDURE ResetCumClient (
2612 	errbuf				OUT NOCOPY VARCHAR2,
2613 	retcode				OUT NOCOPY NUMBER,
2614         p_org_id                        IN NUMBER,
2615 	x_ship_from_org_id  		IN NUMBER,
2616 	x_customer_id 			IN NUMBER,
2617 	x_ship_to_org_id		IN NUMBER,
2618 	x_intrmd_ship_to_org_id		IN NUMBER,
2619 	x_bill_to_org_id		IN NUMBER,
2620 	x_customer_item_id              IN NUMBER,
2621 	x_transaction_start_date	IN VARCHAR2,
2622 	x_transaction_end_date		IN VARCHAR2)
2623  IS
2624         --
2625 	v_return_status BOOLEAN;
2626 	v_transaction_start_date	DATE;
2627 	v_transaction_end_date		DATE;
2628 	--
2629  --
2630  BEGIN
2631   --
2632   IF (l_debug <> -1) THEN
2633     rlm_core_sv.start_debug;
2634   END IF;
2635 	--
2636         --4316744: Commented out the following code.
2637 
2638 /*	v_transaction_start_date := to_date(x_transaction_start_date, 'YYYY/MM/DD HH24:MI:SS');
2639 	v_transaction_end_date := to_date(x_transaction_end_date, 'YYYY/MM/DD HH24:MI:SS');*/
2640 	--
2641         --4316744: Timezone uptake in RLM.
2642         v_transaction_start_date := FND_DATE.canonical_to_date(x_transaction_start_date);
2643         v_transaction_end_date := FND_DATE.canonical_to_date(x_transaction_end_date);
2644 
2645 	ResetCum(
2646                 p_org_id,
2647 		x_ship_from_org_id,
2648 		x_customer_id,
2649 		x_ship_to_org_id,
2650 		x_intrmd_ship_to_org_id,
2651 		x_bill_to_org_id,
2652 		x_customer_item_id,
2653 		v_transaction_start_date,
2654 		v_transaction_end_date,
2655 		v_return_status);
2656 	--
2657   IF (l_debug <> -1) THEN
2658     rlm_core_sv.stop_debug;
2659   END IF;
2660   --
2661  END ResetCumClient;
2662 
2663 
2664 /*=============================================================================
2665   FUNCTION NAME:	GetCumControl
2666 
2667   DESCRIPTION:		This procedure will be called by Demand Status
2668                         Inquiry Report, to get the CUM Control Code
2669 			based on the setup made in the RLM Setup Terms Form.
2670 
2671   PARAMETERS:		x_ship_from_org_id	 IN NUMBER
2672                         x_customer_id            IN NUMBER,
2673                         x_ship_to_address_id     IN NUMBER,
2674                         x_customer_item_id       IN NUMBER
2675 
2676 
2677  ============================================================================*/
2678   FUNCTION GetCumControl(
2679                i_ship_from_org_id          IN NUMBER,
2680                i_customer_id               IN NUMBER,
2681                i_ship_to_address_id        IN NUMBER,
2682                i_customer_item_id          IN NUMBER
2683                             )
2684   RETURN VARCHAR2 IS
2685 
2686   v_temp                     VARCHAR2(10);
2687   v_terms_level              VARCHAR2(25);
2688   v_setup_terms_msg	     VARCHAR2(2000);
2689   v_return_status            BOOLEAN;
2690   v_rlm_setup_terms_record   rlm_setup_terms_sv.setup_terms_rec_typ;
2691 
2692   BEGIN
2693 
2694   v_terms_level := 'CUSTOMER_ITEM';
2695   RLM_TPA_SV.get_setup_terms(
2696                 x_ship_from_org_id              => i_ship_from_org_id,
2697                 x_customer_id                   => i_customer_id,
2698                 x_ship_to_address_id            => i_ship_to_address_id,
2699                 x_customer_item_id              => i_customer_item_id,
2700                 x_terms_definition_level        => v_terms_level,
2701                 x_terms_rec                     => v_rlm_setup_terms_record,
2702 		x_return_message		=> v_setup_terms_msg,
2703                 x_return_status                 => v_return_status);
2704 
2705   return v_rlm_setup_terms_record.cum_control_code;
2706 
2707   END GetCumControl;
2708 
2709 /*=============================================================================
2710   FUNCTION NAME:	get_cum_control
2711 
2712   DESCRIPTION:		This procedure will be called by Demand Status
2713                         Inquiry Report, to get the CUM Control Code
2714 			based on the setup made in the RLM Setup Terms Form.
2715 
2716   PARAMETERS:		x_ship_from_org_id	 IN NUMBER
2717                         x_customer_id            IN NUMBER,
2718                         x_ship_to_address_id     IN NUMBER,
2719                         x_customer_item_id       IN NUMBER
2720 
2721 
2722  ============================================================================*/
2723   FUNCTION get_cum_control(
2724                i_ship_from_org_id          IN NUMBER,
2725                i_customer_id               IN NUMBER,
2726                i_ship_to_address_id        IN NUMBER,
2727                i_customer_item_id          IN NUMBER
2728                             )
2729   RETURN VARCHAR2 IS
2730 
2731   v_temp                     VARCHAR2(10);
2732   v_terms_level              VARCHAR2(25);
2733   v_setup_terms_msg	     VARCHAR2(2000);
2734   v_return_status            BOOLEAN;
2735   v_rlm_setup_terms_record   rlm_setup_terms_sv.setup_terms_rec_typ;
2736 
2737   BEGIN
2738 
2739 --  v_terms_level := 'CUSTOMER';
2740 --  v_terms_level := 'ADDRESS';
2741   v_terms_level := 'CUSTOMER_ITEM';
2742   RLM_TPA_SV.get_setup_terms(
2743                 x_ship_from_org_id              => i_ship_from_org_id,
2744                 x_customer_id                   => i_customer_id,
2745                 x_ship_to_address_id            => i_ship_to_address_id,
2746                 x_customer_item_id              => i_customer_item_id,
2747                 x_terms_definition_level        => v_terms_level,
2748                 x_terms_rec                     => v_rlm_setup_terms_record,
2749 		x_return_message		=> v_setup_terms_msg,
2750                 x_return_status                 => v_return_status);
2751 
2752   return v_rlm_setup_terms_record.cum_control_code;
2753 
2754   END get_cum_control;
2755 
2756 /*=============================================================================
2757   PROCEDURE NAME:	GetCumManagement
2758 
2759   DESCRIPTION:		This procedure will be called by Release Workbench
2760                         to get the CUM Control Code and CUM Org Level Code
2761 			based on the setup made in the RLM Setup Terms Form.
2762 
2763   PARAMETERS:		x_ship_from_org_id	 IN NUMBER
2764                         x_customer_id            IN NUMBER,
2765                         x_ship_to_address_id     IN NUMBER,
2766                         x_customer_item_id       IN NUMBER,
2767 			o_cum_control_code	 IN VARCHAR2,
2768 			o_cum_org_level_code	 IN VARCHAR2
2769 
2770 
2771  ============================================================================*/
2772   PROCEDURE GetCumManagement(
2773                i_ship_from_org_id          IN NUMBER,
2774                i_ship_to_address_id        IN NUMBER,
2775                i_customer_item_id          IN NUMBER,
2776 	       o_cum_control_code	   OUT NOCOPY VARCHAR2,
2777 	       o_cum_org_level_code	   OUT NOCOPY VARCHAR2
2778                             )
2779   IS
2780   v_temp                     VARCHAR2(10);
2781   v_terms_level              VARCHAR2(25);
2782   v_setup_terms_msg	     VARCHAR2(2000);
2783   v_return_status            BOOLEAN;
2784   v_rlm_setup_terms_record   rlm_setup_terms_sv.setup_terms_rec_typ;
2785   v_customer_id		     NUMBER;
2786 
2787   BEGIN
2788   --
2789   IF (l_debug <> -1) THEN
2790      rlm_core_sv.start_debug;
2791      rlm_core_sv.dpush(C_SDEBUG, 'GetCumManagement');
2792   END IF;
2793   --
2794 
2795 	/* Find customer_id, as it is a required input parameter for
2796    	rlm_setup_terms */
2797         -- Following query is changed as per TCA obsolescence project.
2798         SELECT  DISTINCT ACCT_SITE.CUST_ACCOUNT_ID
2799         INTO    v_customer_id
2800         FROM    HZ_CUST_ACCT_SITES ACCT_SITE
2801         WHERE   ACCT_SITE.CUST_ACCT_SITE_ID = i_ship_to_address_id;
2802 
2803   RLM_TPA_SV.get_setup_terms(
2804                 x_ship_from_org_id              => i_ship_from_org_id,
2805                 x_customer_id                   => v_customer_id,
2806                 x_ship_to_address_id            => i_ship_to_address_id,
2807                 x_customer_item_id              => i_customer_item_id,
2808                 x_terms_definition_level        => v_terms_level,
2809                 x_terms_rec                     => v_rlm_setup_terms_record,
2810 	        x_return_message		=> v_setup_terms_msg,
2811                 x_return_status                 => v_return_status);
2812 
2813   o_cum_control_code   := v_rlm_setup_terms_record.cum_control_code;
2814   o_cum_org_level_code := v_rlm_setup_terms_record.cum_org_level_code;
2815   --
2816   IF (l_debug <> -1) THEN
2817      rlm_core_sv.dpop('Completed successfully');
2818      rlm_core_sv.stop_debug;
2819   END IF;
2820   --
2821   EXCEPTION
2822 	  WHEN NO_DATA_FOUND THEN
2823            --
2824            IF (l_debug <> -1) THEN
2825    		rlm_core_sv.dpop('no data found');
2826            END IF;
2827 	   --
2828 	  WHEN OTHERS THEN
2829            --
2830            IF (l_debug <> -1) THEN
2831    		rlm_core_sv.dpop('when others');
2832            END IF;
2833 
2834   END GetCumManagement;
2835 
2836 /*=============================================================================
2837   PROCEDURE NAME:	GetCumStartDate
2838 
2839   DESCRIPTION:		This procedure will be called by CalculateCumKey to
2840                         to get the CUM current start date and CUM current
2841 			record year from the schedule
2842 
2843   PARAMETERS:		i_schedule_header_id	IN NUMBER
2844                 	i_schedule_line_id	IN NUMBER
2845 			o_cum_start_date	OUT NOCOPY DATE
2846 			o_cust_record_year	OUT NOCOPY VARCHAR2
2847 			o_return_message	OUT NOCOPY VARCHAR2
2848 			o_return_status		OUT NOCOPY BOOLEAN
2849 
2850  ============================================================================*/
2851 
2852   PROCEDURE GetCumStartDate(
2853 			i_schedule_header_id	IN NUMBER,
2854                 	i_schedule_line_id	IN NUMBER,
2855 			o_cum_start_date	OUT NOCOPY DATE,
2856 			o_cust_record_year	OUT NOCOPY VARCHAR2,
2857 			o_return_message	OUT NOCOPY VARCHAR2,
2858 			o_return_status		OUT NOCOPY BOOLEAN
2859                             )
2860   IS
2861 	v_ship_from_org_id	NUMBER;
2862 	v_ship_to_address_id	NUMBER;
2863 	v_customer_item_id	NUMBER;
2864         v_customer_id           NUMBER;
2865         v_terms_level           VARCHAR2(20) DEFAULT NULL;
2866         v_rlm_setup_terms_record  rlm_setup_terms_sv.setup_terms_rec_typ;
2867         v_setup_terms_msg       VARCHAR2(2000);
2868         v_setup_terms_status    BOOLEAN;
2869         v_cust_po_number        VARCHAR2(50);  --Bugfix 7007638
2870         v_industry_attribute1   VARCHAR2(150); --Bugfix 7007638
2871 
2872 
2873   BEGIN
2874 	--
2875         IF (l_debug <> -1) THEN
2876    	  rlm_core_sv.dpush(C_SDEBUG, 'GetCumStartDate');
2877      	  rlm_core_sv.dlog(C_DEBUG, 'i_schedule_header_id', i_schedule_header_id);
2878      	  rlm_core_sv.dlog(C_DEBUG, 'i_schedule_line_id', i_schedule_line_id);
2879 	END IF;
2880 	--
2881 	SELECT	line.ship_from_org_id,
2882 		line.ship_to_address_id,
2883 		line.customer_item_id,
2884         header.customer_id,
2885         line.cust_po_number,     --Bugfix 7007638
2886         line.industry_attribute1 --Bugfix 7007638
2887 	INTO	v_ship_from_org_id,
2888 		v_ship_to_address_id,
2889 		v_customer_item_id,
2890                 v_customer_id,
2891                 v_cust_po_number,     --Bugfix 7007638
2892                 v_industry_attribute1 --Bugfix 7007638
2893 	FROM	rlm_schedule_lines_all line,
2894                 rlm_schedule_headers header
2895 	WHERE	line.header_id = i_schedule_header_id
2896 	AND	line.line_id = i_schedule_line_id
2897         AND     line.header_id = header.header_id;
2898 	--
2899         IF (l_debug <> -1) THEN
2900      	  rlm_core_sv.dlog(C_DEBUG, 'v_ship_from_org_id', v_ship_from_org_id);
2901      	  rlm_core_sv.dlog(C_DEBUG, 'v_ship_to_address_id', v_ship_to_address_id);
2902      	  rlm_core_sv.dlog(C_DEBUG, 'v_customer_item_id', v_customer_item_id);
2903      	  rlm_core_sv.dlog(C_DEBUG, 'customer_id', v_customer_id);
2904   	END IF;
2905 	--
2906         BEGIN
2907           --
2908   	  SELECT  start_date_time, industry_attribute1
2909   	  INTO	  o_cum_start_date, o_cust_record_year
2910   	  FROM    rlm_schedule_lines
2911 	  WHERE	 header_id = i_schedule_header_id
2912 	  AND	ship_from_org_id = v_ship_from_org_id
2913 	  AND	ship_to_address_id = v_ship_to_address_id
2914 	  AND	customer_item_id = v_customer_item_id
2915 	  AND   NVL(cust_po_number,' ')	= NVL(v_cust_po_number,' ')           --Bugfix 7007638
2916       AND   NVL(industry_attribute1,' ') = NVL(v_industry_attribute1,' ') --Bugfix 7007638
2917 	  AND	item_detail_type = '4'
2918 	  AND	item_detail_subtype = 'CUM';
2919           --
2920        EXCEPTION
2921          --
2922          WHEN NO_DATA_FOUND THEN
2923          --
2924          -- see if there are setup terms set for CUM management
2925          --
2926          RLM_TPA_SV.get_setup_terms(
2927           x_ship_from_org_id              => v_ship_from_org_id,
2928           x_customer_id                   => v_customer_id,
2929           x_ship_to_address_id            => v_ship_to_address_id,
2930           x_customer_item_id              => v_customer_item_id,
2931           x_terms_definition_level        => v_terms_level,
2932           x_terms_rec                     => v_rlm_setup_terms_record,
2933           x_return_message                => v_setup_terms_msg,
2934           x_return_status                 => v_setup_terms_status);
2935          --
2936          IF (l_debug <> -1) THEN
2937      	   rlm_core_sv.dlog(C_DEBUG, 'cum_control_code',
2938                             v_rlm_setup_terms_record.cum_control_code);
2939      	   rlm_core_sv.dlog(C_DEBUG, 'v_setup_terms_status',
2940                             v_setup_terms_status);
2941   	 END IF;
2942          --
2943          IF (v_setup_terms_status = FALSE)
2944          OR (NVL(v_rlm_setup_terms_record.cum_control_code,'NO_CUM') NOT IN
2945          ('CUM_BY_DATE_ONLY','CUM_BY_DATE_RECORD_YEAR','CUM_BY_DATE_PO'))
2946          THEN
2947            --
2948            RAISE NO_DATA_FOUND;
2949            --
2950          END IF;
2951          --
2952          --This is indicating that CalculateCumKey should get the latest Key
2953          o_cum_start_date := rlm_manage_demand_sv.k_DNULL;
2954          --
2955        END;
2956 
2957 	rlm_message_sv.get_msg_text(
2958 		x_message_name	=> 'RLM_CUM_SUCCESS',
2959 		x_text		=> o_return_message);
2960 
2961   	o_return_status := TRUE;
2962 	--
2963         IF (l_debug <> -1) THEN
2964      	  rlm_core_sv.dlog(C_DEBUG, 'o_cum_start_date', o_cum_start_date);
2965      	  rlm_core_sv.dlog(C_DEBUG, 'o_cust_record_year', o_cust_record_year);
2966      	  rlm_core_sv.dlog(C_DEBUG, 'o_return_status', o_return_status);
2967      	  rlm_core_sv.dpop('Completed successfully');
2968   	END IF;
2969 	--
2970   EXCEPTION
2971 	  WHEN NO_DATA_FOUND THEN
2972 		o_return_status := FALSE;
2973 		rlm_message_sv.get_msg_text(
2974 			x_message_name	=> 'RLM_CUM_CSD_NOT_FOUND',
2975 			x_text		=> o_return_message);
2976 		--
2977   		IF (l_debug <> -1) THEN
2978 		  --
2979    		  rlm_core_sv.dlog(C_DEBUG, 'o_cum_start_date', o_cum_start_date);
2980      		  rlm_core_sv.dlog(C_DEBUG, 'o_cust_record_year', o_cust_record_year);
2981      		  rlm_core_sv.dlog(C_DEBUG, 'o_return_message', o_return_message);
2982      		  rlm_core_sv.dlog(C_DEBUG, 'o_return_status', o_return_status);
2983    		  rlm_core_sv.dpop(C_DEBUG);
2984 		  --
2985 		END IF;
2986 
2987 	  WHEN OTHERS THEN
2988 		o_return_status := FALSE;
2989 		o_return_message := SQLERRM;
2990 		--
2991                 IF (l_debug <> -1) THEN
2992    		  rlm_core_sv.dlog(C_DEBUG, 'o_cum_start_date', o_cum_start_date);
2993      		  rlm_core_sv.dlog(C_DEBUG, 'o_cust_record_year', o_cust_record_year);
2994      		  rlm_core_sv.dlog(C_DEBUG, 'o_return_message', o_return_message);
2995      		  rlm_core_sv.dlog(C_DEBUG, 'o_return_status', o_return_status);
2996    		  rlm_core_sv.dpop(C_DEBUG);
2997 		END IF;
2998 
2999   END GetCumStartDate;
3000 
3001 /*=============================================================================
3002   PROCEDURE NAME:	GetTPContext
3003 
3004   DESCRIPTION:		This procedure returns the tpcontext using CUM Key
3005 			Record
3006 
3007   PARAMETERS:		x_cum_key_record		IN  RLM_CUM_SV.cum_key_attrib_rec_type
3008                        	x_customer_number 		OUT NOCOPY VARCHAR2
3009                        	x_ship_to_ece_locn_code 	OUT NOCOPY VARCHAR2
3010                        	x_bill_to_ece_locn_code 	OUT NOCOPY VARCHAR2
3011                        	x_inter_ship_to_ece_locn_code 	OUT NOCOPY VARCHAR2
3012                        	x_tp_group_code 		OUT NOCOPY VARCHAR2
3013 
3014  ============================================================================*/
3015   PROCEDURE GetTPContext(
3016 			x_cum_key_record		IN  RLM_CUM_SV.cum_key_attrib_rec_type,
3017                        	x_customer_number 		OUT NOCOPY VARCHAR2,
3018                        	x_ship_to_ece_locn_code 	OUT NOCOPY VARCHAR2,
3019                        	x_bill_to_ece_locn_code 	OUT NOCOPY VARCHAR2,
3020                        	x_inter_ship_to_ece_locn_code 	OUT NOCOPY VARCHAR2,
3021                        	x_tp_group_code 		OUT NOCOPY VARCHAR2)
3022   IS
3023 	v_level 	VARCHAR2(50) := 'NONE';
3024   BEGIN
3025   --
3026   IF (l_debug <> -1) THEN
3027      rlm_core_sv.dpush(C_DEBUG, 'GetTPContext');
3028   END IF;
3029   --
3030   IF x_cum_key_record.customer_id IS NOT NULL THEN
3031 
3032 	IF x_cum_key_record.ship_to_address_id IS NOT NULL THEN
3033 		v_level := 'SHIP_TO_ECE_LOCN_CODE';
3034                 -- Following query is changed as per TCA obsolescence project.
3035 		SELECT  acct_site.ece_tp_location_code,
3036 			c.tp_group_code
3037 		INTO	x_ship_to_ece_locn_code,
3038 			x_tp_group_code
3039 		FROM	HZ_CUST_ACCT_SITES ACCT_SITE,
3040 			ece_tp_headers b,
3041 			ece_tp_group c
3042 		WHERE	 ACCT_SITE.CUST_ACCOUNT_ID  = x_cum_key_record.customer_id
3043 		AND	ACCT_SITE.CUST_ACCT_SITE_ID = x_cum_key_record.ship_to_address_id
3044 		AND	b.tp_header_id = acct_site.tp_header_id
3045 		AND	c.tp_group_id = b.tp_group_id;
3046 	END IF;
3047 
3048 	IF x_cum_key_record.bill_to_address_id IS NOT NULL THEN
3049 		v_level := 'BILL_TO_ECE_LOCN_CODE';
3050                 -- Following query is changed as per TCA obsolescence project.
3051 		SELECT  acct_site.ece_tp_location_code
3052 		INTO	x_bill_to_ece_locn_code
3053 		FROM	HZ_CUST_ACCT_SITES ACCT_SITE
3054 		WHERE	ACCT_SITE.CUST_ACCT_SITE_ID = x_cum_key_record.bill_to_address_id
3055 		AND	ACCT_SITE.CUST_ACCOUNT_ID   = x_cum_key_record.customer_id;
3056 	END IF;
3057 
3058 	IF x_cum_key_record.intrmd_ship_to_address_id IS NOT NULL THEN
3059 		v_level := 'INTER_SHIP_TO_ECE_LOCN_CODE';
3060                 -- Following query is changed as per TCA obsolescence project.
3061 		SELECT	acct_site.ece_tp_location_code
3062 		INTO	x_inter_ship_to_ece_locn_code
3063 		FROM	HZ_CUST_ACCT_SITES ACCT_SITE
3064 		WHERE	ACCT_SITE.CUST_ACCT_SITE_ID = x_cum_key_record.intrmd_ship_to_address_id
3065 		AND	ACCT_SITE.CUST_ACCOUNT_ID   = x_cum_key_record.customer_id;
3066 	END IF;
3067 
3068 	IF x_cum_key_record.customer_id IS NOT NULL THEN
3069 		v_level := 'CUSTOMER_NUMBER';
3070                 -- Following query is changed as per TCA obsolescence project.
3071 		SELECT  CUST_ACCT.ACCOUNT_NUMBER
3072 		INTO	x_customer_number
3073 		FROM	HZ_CUST_ACCOUNTS CUST_ACCT
3074 		WHERE  	CUST_ACCT.CUST_ACCOUNT_ID = x_cum_key_record.customer_id;
3075   	END IF;
3076 
3077   END IF;
3078   --
3079   IF (l_debug <> -1) THEN
3080      rlm_core_sv.dlog(C_DEBUG, 'customer_number', x_customer_number);
3081      rlm_core_sv.dlog(C_DEBUG,'x_ship_to_ece_locn_code', x_ship_to_ece_locn_code);
3082      rlm_core_sv.dlog(C_DEBUG, 'x_bill_to_ece_locn_code', x_bill_to_ece_locn_code);
3083      rlm_core_sv.dlog(C_DEBUG, 'x_inter_ship_to_ece_locn_code', x_inter_ship_to_ece_locn_code);
3084      rlm_core_sv.dlog(C_DEBUG, 'x_tp_group_code',x_tp_group_code);
3085      rlm_core_sv.dpop(C_DEBUG, 'Successful');
3086   END IF;
3087   --
3088   EXCEPTION
3089   WHEN NO_DATA_FOUND THEN
3090 	IF v_level = 'SHIP_TO_ECE_LOCN_CODE' THEN
3091           --
3092   	  IF (l_debug <> -1) THEN
3093             rlm_core_sv.dlog(C_DEBUG, 'No Location Code Found for Ship-To Address ID',
3094 			x_cum_key_record.ship_to_address_id);
3095 	  END IF;
3096 	  --
3097 	  x_ship_to_ece_locn_code := NULL;
3098 	  --
3099 	ELSIF v_level = 'BILL_TO_ECE_LOCN_CODE' THEN
3100 	  --
3101   	  IF (l_debug <> -1) THEN
3102    	    rlm_core_sv.dlog(C_DEBUG, 'No Location Code Found for Bill-To Address ID',
3103 			x_cum_key_record.bill_to_address_id);
3104 	  END IF;
3105 	  --
3106 	  x_bill_to_ece_locn_code := NULL;
3107 	  --
3108 	ELSIF v_level = 'INTER_SHIP_TO_ECE_LOCN_CODE' THEN
3109           --
3110 	  IF (l_debug <> -1) THEN
3111    	    rlm_core_sv.dlog(C_DEBUG, 'No Location Code Found for Intermediate Ship-To Address ID',
3112 				x_cum_key_record.intrmd_ship_to_address_id);
3113 	  END IF;
3114 	  --
3115 	  x_inter_ship_to_ece_locn_code := NULL;
3116  	  --
3117 	ELSIF v_level = 'CUSTOMER_NUMBER' THEN
3118 	  --
3119   	  IF (l_debug <> -1) THEN
3120    	    rlm_core_sv.dlog(C_DEBUG, 'No Customer Number Found for Customer ID ',
3121 				x_cum_key_record.customer_id);
3122 	  END IF;
3123 	  --
3124 	  x_customer_number := NULL;
3125 	  --
3126 	END IF;
3127 	--
3128         IF (l_debug <> -1) THEN
3129    	  rlm_core_sv.dpop(C_DEBUG);
3130 	END IF;
3131 	--
3132   WHEN OTHERS THEN
3133 	--
3134         IF (l_debug <> -1) THEN
3135    	  rlm_core_sv.dlog(C_SDEBUG, 'Level: ', v_level);
3136    	  rlm_core_sv.dlog(C_SDEBUG, 'EXCEPTION: '||SUBSTR(SQLERRM,1,200));
3137    	  rlm_core_sv.dpop(C_DEBUG);
3138 	END IF;
3139 	--
3140 	RAISE;
3141 
3142   END GetTPContext;
3143 
3144 /*=============================================================================
3145   PROCEDURE NAME:	GetTPContext2
3146 
3147   DESCRIPTION:		This procedure returns the tpcontext using Trip Stop ID
3148 
3149   PARAMETERS:		x_trip_stop_id			IN  NUMBER
3150                        	x_customer_number 		OUT NOCOPY VARCHAR2
3151                        	x_ship_to_ece_locn_code 	OUT NOCOPY VARCHAR2
3152                        	x_bill_to_ece_locn_code 	OUT NOCOPY VARCHAR2
3153                        	x_inter_ship_to_ece_locn_code 	OUT NOCOPY VARCHAR2
3154                        	x_tp_group_code 		OUT NOCOPY VARCHAR2
3155 
3156  ============================================================================*/
3157   PROCEDURE GetTPContext2(
3158 			x_trip_stop_id			IN  NUMBER,
3159                        	x_customer_number 		OUT NOCOPY VARCHAR2,
3160                        	x_ship_to_ece_locn_code 	OUT NOCOPY VARCHAR2,
3161                        	x_bill_to_ece_locn_code 	OUT NOCOPY VARCHAR2,
3162                        	x_inter_ship_to_ece_locn_code 	OUT NOCOPY VARCHAR2,
3163                        	x_tp_group_code 		OUT NOCOPY VARCHAR2)
3164   IS
3165 	CURSOR c_oe_lines IS
3166 	SELECT 	oelines.sold_to_org_id,
3167 		oelines.ship_to_org_id,
3168 		oelines.intmed_ship_to_org_id,
3169 		oelines.invoice_to_org_id
3170 	FROM  	WSH_DELIVERY_LEGS wleg,
3171 		WSH_DELIVERY_ASSIGNMENTS_V wdass,
3172 		WSH_DELIVERY_DETAILS wdel,
3173 		OE_ORDER_LINES oelines
3174 	WHERE	wleg.pick_up_stop_id    = x_trip_stop_id
3175 	AND	wdass.delivery_id 	= wleg.delivery_id
3176 	AND	wdel.delivery_detail_id = wdass.delivery_detail_id
3177 	AND	oelines.shipped_quantity IS NOT NULL
3178 	AND	oelines.line_id 	= wdel.source_line_id
3179         AND     wdel.container_flag                     = 'N'                      -- 4301944
3180 	AND	oelines.header_id 			= wdel.source_header_id    -- 4301944
3181 	AND	oelines.source_document_type_id	= 5;
3182 
3183 	v_customer_id			NUMBER;
3184 	v_ship_to_org_id		NUMBER;
3185 	v_ship_to_address_id		NUMBER;
3186 	v_bill_to_org_id		NUMBER;
3187 	v_bill_to_address_id		NUMBER;
3188 	v_intmed_ship_to_org_id		NUMBER;
3189 	v_intrmd_ship_to_address_id	NUMBER;
3190 	v_level 			VARCHAR2(50) := 'NONE';
3191 	e_no_delivery_lines		EXCEPTION;
3192   BEGIN
3193   --
3194   IF (l_debug <> -1) THEN
3195      rlm_core_sv.dpush(C_DEBUG, 'GetTPContext2');
3196   END IF;
3197   --
3198   OPEN 	c_oe_lines;
3199   FETCH c_oe_lines
3200   INTO	v_customer_id,
3201 	v_ship_to_org_id,
3202 	v_intmed_ship_to_org_id,
3203 	v_bill_to_org_id;
3204   CLOSE c_oe_lines;
3205 
3206   IF c_oe_lines%NOTFOUND THEN
3207 	RAISE e_no_delivery_lines;
3208   END IF;
3209 
3210   IF v_customer_id IS NOT NULL THEN
3211 		v_level := 'CUSTOMER_NUMBER';
3212                 -- Following query is changed as per TCA obsolescence project.
3213 		SELECT  CUST_ACCT.ACCOUNT_NUMBER
3214 		INTO	x_customer_number
3215 		FROM	HZ_CUST_ACCOUNTS CUST_ACCT
3216 		WHERE  	CUST_ACCT.CUST_ACCOUNT_ID = v_customer_id;
3217   END IF;
3218 
3219   IF v_ship_to_org_id IS NOT NULL THEN
3220 		v_level := 'SHIP_TO_ADDRESS_ID';
3221                 -- Following query is changed as per TCA obsolescence project.
3222 		SELECT  CUST_ACCT_SITE_ID
3223 		INTO	v_ship_to_address_id
3224 		FROM	HZ_CUST_SITE_USES
3225 		WHERE	site_use_id = v_ship_to_org_id
3226 		AND	site_use_code = 'SHIP_TO';
3227 
3228 	IF v_ship_to_address_id IS NOT NULL THEN
3229 		v_level := 'SHIP_TO_ECE_LOCN_CODE';
3230                 -- Following query is changed as per TCA obsolescence project.
3231 		SELECT  acct_site.ece_tp_location_code,
3232 			c.tp_group_code
3233 		INTO	x_ship_to_ece_locn_code,
3234 			x_tp_group_code
3235 		FROM	HZ_CUST_ACCT_SITES	 ACCT_SITE,
3236 			ece_tp_headers b,
3237 			ece_tp_group c
3238 		WHERE	ACCT_SITE.CUST_ACCOUNT_ID = v_customer_id
3239 		AND	ACCT_SITE.CUST_ACCT_SITE_ID = v_ship_to_address_id
3240 		AND	b.tp_header_id = acct_site.tp_header_id
3241 		AND	c.tp_group_id = b.tp_group_id;
3242 	END IF;
3243   END IF;
3244 
3245   IF v_bill_to_org_id IS NOT NULL THEN
3246 		v_level := 'BILL_TO_ADDRESS_ID';
3247                 -- Following query is changed as per TCA obsolescence project.
3248 		SELECT	CUST_ACCT_SITE_ID
3249 		INTO	v_bill_to_address_id
3250 		FROM	HZ_CUST_SITE_USES
3251 		WHERE	site_use_id = v_bill_to_org_id
3252 		AND	site_use_code = 'BILL_TO';
3253 
3254 	IF v_bill_to_address_id IS NOT NULL THEN
3255 		v_level := 'BILL_TO_ECE_LOCN_CODE';
3256                 -- Following query is changed as per TCA obsolescence project.
3257 		SELECT 	ACCT_SITE.ece_tp_location_code
3258 		INTO	x_bill_to_ece_locn_code
3259 		FROM	HZ_CUST_ACCT_SITES ACCT_SITE
3260 		WHERE	ACCT_SITE.CUST_ACCOUNT_ID = v_customer_id
3261 		AND	ACCT_SITE.CUST_ACCT_SITE_ID = v_bill_to_address_id;
3262 
3263 	END IF;
3264 
3265   END IF;
3266 
3267   IF v_intmed_ship_to_org_id IS NOT NULL THEN
3268 		v_level := 'INTRMD_SHIP_TO_ADDRESS_ID';
3269                 -- Following query is changed as per TCA obsolescence project.
3270 		SELECT	CUST_ACCT_SITE_ID
3271 		INTO	v_intrmd_ship_to_address_id
3272 		FROM	HZ_CUST_SITE_USES
3273 		WHERE	site_use_id = v_intmed_ship_to_org_id
3274 		AND	site_use_code = 'SHIP_TO';
3275 
3276   	IF v_intrmd_ship_to_address_id IS NOT NULL THEN
3277 		v_level := 'INTER_SHIP_TO_ECE_LOCN_CODE';
3278                 -- Following query is changed as per TCA obsolescence project.
3279 		SELECT 	ACCT_SITE.ece_tp_location_code
3280 		INTO	x_inter_ship_to_ece_locn_code
3281 		FROM	HZ_CUST_ACCT_SITES ACCT_SITE
3282 		WHERE	ACCT_SITE.CUST_ACCOUNT_ID = v_customer_id
3283 		AND	ACCT_SITE.CUST_ACCT_SITE_ID = v_intrmd_ship_to_address_id;
3284 	END IF;
3285   END IF;
3286   --
3287   IF (l_debug <> -1) THEN
3288      rlm_core_sv.dlog(C_DEBUG, 'customer_number', x_customer_number);
3289      rlm_core_sv.dlog(C_DEBUG,'x_ship_to_ece_locn_code', x_ship_to_ece_locn_code);
3290      rlm_core_sv.dlog(C_DEBUG, 'x_bill_to_ece_locn_code', x_bill_to_ece_locn_code);
3291      rlm_core_sv.dlog(C_DEBUG, 'x_inter_ship_to_ece_locn_code', x_inter_ship_to_ece_locn_code);
3292      rlm_core_sv.dlog(C_DEBUG, 'x_tp_group_code',x_tp_group_code);
3293      rlm_core_sv.dpop(C_DEBUG, 'Successful');
3294   END IF;
3295   --
3296   EXCEPTION
3297   WHEN NO_DATA_FOUND THEN
3298 	--
3299 	IF v_level = 'SHIP_TO_ECE_LOCN_CODE' THEN
3300 	  --
3301   	  IF (l_debug <> -1) THEN
3302    	    rlm_core_sv.dlog(C_DEBUG, 'No Location Code Found for Ship-To Address ID', v_ship_to_address_id);
3303 	  END IF;
3304 	  --
3305 	  x_ship_to_ece_locn_code := NULL;
3306 	  --
3307 	ELSIF v_level = 'BILL_TO_ECE_LOCN_CODE' THEN
3308 	  --
3309   	  IF (l_debug <> -1) THEN
3310    	    rlm_core_sv.dlog(C_DEBUG, 'No Location Code Found for Bill-To Address ID', v_bill_to_address_id);
3311 	  END IF;
3312 	  --
3313 	  x_bill_to_ece_locn_code := NULL;
3314 	  --
3315 	ELSIF v_level = 'INTER_SHIP_TO_ECE_LOCN_CODE' THEN
3316 	  --
3317   	  IF (l_debug <> -1) THEN
3318    	   rlm_core_sv.dlog(C_DEBUG, 'No Location Code Found for Intermediate Ship-To Address ID',
3319 			v_intrmd_ship_to_address_id);
3320 	  END IF;
3321 	  --
3322 	  x_inter_ship_to_ece_locn_code := NULL;
3323 	  --
3324 	ELSIF v_level = 'CUSTOMER_NUMBER' THEN
3325 	  --
3326   	  IF (l_debug <> -1) THEN
3327             rlm_core_sv.dlog(C_DEBUG, 'No Customer Number Found for Customer ID ', v_customer_id);
3328 	  END IF;
3329 	  --
3330 	  x_customer_number := NULL;
3331 	  --
3332 	ELSIF v_level = 'SHIP_TO_ADDRESS_ID' THEN
3333 	  --
3334   	  IF (l_debug <> -1) THEN
3335    	    rlm_core_sv.dlog(C_DEBUG, 'No Address ID Found for Ship-To Org ID ', v_ship_to_org_id);
3336 	  END IF;
3337 	  --
3338 	ELSIF v_level = 'INTRMD_SHIP_TO_ADDRESS_ID' THEN
3339 	  --
3340   	  IF (l_debug <> -1) THEN
3341    	    rlm_core_sv.dlog(C_DEBUG, 'No Address ID Found for Intermediate Ship-To Org ID ',
3342 			 v_intmed_ship_to_org_id);
3343 	  END IF;
3344 	  --
3345 	ELSIF v_level = 'BILL_TO_ADDRESS_ID' THEN
3346 	  --
3347   	  IF (l_debug <> -1) THEN
3348    	    rlm_core_sv.dlog(C_DEBUG, 'No Address ID Found for Bill-To Org ID ', v_bill_to_org_id);
3349 	  END IF;
3350 	  --
3351 	END IF;
3352 	--
3353   	IF (l_debug <> -1) THEN
3354    	  rlm_core_sv.dpop(C_DEBUG);
3355 	END IF;
3356   	--
3357   WHEN e_no_delivery_lines THEN
3358 	--
3359 	IF (l_debug <> -1) THEN
3360    	  rlm_core_sv.dlog(C_DEBUG, 'No delivery line is associated with Trip Stop ID ', x_trip_stop_id);
3361 	END IF;
3362  	--
3363   WHEN OTHERS THEN
3364 	--
3365 	IF (l_debug <> -1) THEN
3366    	  rlm_core_sv.dlog(C_SDEBUG, 'Level: ', v_level);
3367    	  rlm_core_sv.dlog(C_SDEBUG, 'EXCEPTION: '||SUBSTR(SQLERRM,1,200));
3368    	  rlm_core_sv.dpop(C_DEBUG);
3369 	END IF;
3370 	--
3371 	RAISE;
3372 
3373   END GetTPContext2;
3374 
3375 -- The procedure GetNameForId is removed as it was commented out.
3376 
3377 
3378 /*============================================================================
3379 
3380   PROCEDURE NAME:	ResetCum
3381 
3382   Flow:  Get the setupterms based on the parameters passed, then call GetCums
3383    which will return 3 tables.  One table is the new cums created to reset the
3384    old ones, the other table is the old cums and the third table is used to
3385    relate the first and second table.  For each cum in the old table, call
3386    SetSupplierCum which calls CalculateSupplierCum for all the shipments
3387    for the given Cum Key.  It populates oe global tables and it calculates
3388    the cum qty .
3389    Then call the process_order once for each header_id.  If process_order
3390    completes without error then call UpdateOldKey to recalculate the old
3391    cums which have been reset.
3392 
3393 =============================================================================*/
3394 
3395  PROCEDURE ResetCum (
3396         p_org_id                        IN NUMBER,
3397 	x_ship_from_org_id  		IN NUMBER,
3398 	x_customer_id 			IN NUMBER,
3399 	x_ship_to_org_id		IN NUMBER,
3400 	x_intrmd_ship_to_org_id		IN NUMBER,
3401 	x_bill_to_org_id		IN NUMBER,
3402 	x_customer_item_id		IN NUMBER,
3403 	x_transaction_start_date	IN DATE,
3404 	x_transaction_end_date		IN DATE,
3405 	x_return_status			OUT NOCOPY BOOLEAN)
3406  IS
3407 	--
3408 	v_rlm_setup_terms_rec	   	rlm_setup_terms_sv.setup_terms_rec_typ;
3409         v_index                         NUMBER;
3410         v_index2                        NUMBER;
3411         v_index3                        NUMBER;
3412         v_num                           NUMBER;
3413         v_start_num                     NUMBER DEFAULT 0;
3414         v_end_num                       NUMBER DEFAULT 0;
3415 	v_cum_key_record	   	cum_key_attrib_rec_type;
3416         v_old_cum_counter               RLM_CUM_SV.t_new_ship_count;
3417 	p_ship_from_org_id	   	NUMBER DEFAULT NULL;
3418 	p_ship_to_org_id	   	NUMBER DEFAULT NULL;
3419 	p_intmed_ship_to_org_id    	NUMBER DEFAULT NULL;
3420         p_bill_to_org_id           	NUMBER DEFAULT NULL;
3421         p_cum_key_id                    NUMBER DEFAULT NULL;
3422 	v_ship_to_address_id	   	NUMBER;
3423 	v_bill_to_address_id	   	NUMBER;
3424 	v_intrmd_ship_to_address_id 	NUMBER;
3425 	v_terms_level		   	VARCHAR2(25) DEFAULT NULL;
3426 	v_setup_terms_msg	   	VARCHAR2(5000);
3427 	v_setup_terms_status	   	BOOLEAN;
3428 	v_level			   	VARCHAR2(60);
3429 	counter			   	NUMBER DEFAULT 1;
3430         counter2                        NUMBER DEFAULT 0;
3431         v_reset_counter                 NUMBER DEFAULT 0;
3432 	error_count			NUMBER DEFAULT 0;
3433 	v_trail1			VARCHAR2(5000);
3434 	v_trail2			VARCHAR2(5000);
3435 	v_trail3			VARCHAR2(5000);
3436 	v_trail4			VARCHAR2(5000);
3437 	v_trail5			VARCHAR2(5000);
3438 	v_trail6			VARCHAR2(5000);
3439 	--v_old_cum_table			t_old_cum;
3440         v_old_cum_records               RLM_CUM_SV.t_cums;
3441         v_tmp_old_table                 t_old_cum;
3442 	----
3443      ---- Used by Process Order API
3444 	--
3445 	x_oe_api_version		NUMBER DEFAULT 1;
3446 	l_return_status			VARCHAR2(1);
3447         v_return_status                 BOOLEAN;
3448         v_return_stat                   NUMBER DEFAULT 0 ;
3449 	v_msg_data			VARCHAR2(2000);
3450         v_adjustment_ids                t_new_ship_count;
3451         v_adjustment_date               DATE DEFAULT NULL;
3452         v_header_id                     NUMBER;
3453         v_cum_records                   RLM_CUM_SV.t_cums;
3454         v_msg_count                     NUMBER;
3455         adj_qty                         NUMBER DEFAULT 0;
3456         l_file_val                      VARCHAR2(80);
3457         v_om_dbg_dir                    VARCHAR2(80);
3458         v_end_date_time                 date DEFAULT NULL;
3459         tmp_char                        VARCHAR2(50);
3460         v_shipLineCounter               NUMBER;
3461 	--
3462         e_NoCum                          EXCEPTION;
3463         e_NoShipment                     EXCEPTION;
3464         v_visited                        BOOLEAN;
3465 	--
3466 	l_oe_line_tbl_out              oe_order_pub.line_tbl_type;
3467 	--
3468  BEGIN
3469    --
3470   IF (l_debug <> -1) THEN
3471       rlm_core_sv.dpush(C_SDEBUG, 'ResetCum');
3472    END IF;
3473    --
3474    x_return_status := TRUE;
3475    --
3476    MO_GLOBAL.set_policy_context(p_access_mode => 'S',
3477                                 p_org_id      => p_org_id);
3478    --
3479    IF (l_debug <> -1) THEN
3480       rlm_core_sv.dlog(C_SDEBUG, 'p_org_id',p_org_id);
3481       rlm_core_sv.dlog(C_SDEBUG, 'x_ship_from_org_id',x_ship_from_org_id);
3482       rlm_core_sv.dlog(C_SDEBUG, 'x_customer_id',x_customer_id);
3483       rlm_core_sv.dlog(C_SDEBUG, 'x_customer_item_id',x_customer_item_id);
3484       rlm_core_sv.dlog(C_SDEBUG, 'x_transaction_start_date',
3485                                   x_transaction_start_date);
3486    END IF;
3487    --
3488    -- add time stamp
3489    --
3490    --4316744: Commented out the following code.
3491    /*IF x_transaction_end_date IS NOT NULL THEN
3492      tmp_char := to_char(x_transaction_end_date ,'DD-MM-YYYY') || ' 23:59:59';
3493      v_end_date_time := to_date(tmp_char,'DD-MM-YYYY HH24:MI:SS');
3494    END IF;*/
3495    --
3496    --4316744: Timezone uptake in RLM.
3497    IF x_transaction_end_date IS NOT NULL THEN
3498      v_end_date_time := x_transaction_end_date;
3499    END IF;
3500 
3501    IF (l_debug <> -1) THEN
3502       rlm_core_sv.dlog(C_SDEBUG, 'v_end_date_time',
3503                                      v_end_date_time);
3504    END IF;
3505 --
3506 -- Start logging the ResetCum trail into the fnd log file here!
3507 --
3508    fnd_file.put_line(fnd_file.log, 'User Input Parameters: ');
3509    fnd_file.put_line(fnd_file.log, 'Operating Unit: '	                	||p_org_id);
3510    fnd_file.put_line(fnd_file.log, 'Ship-From Organization ID: '		||x_ship_from_org_id);
3511    fnd_file.put_line(fnd_file.log, 'Ship-To Organization ID: '			||x_ship_to_org_id);
3512    fnd_file.put_line(fnd_file.log, 'Intermediate Ship-To Organization ID: '	||x_intrmd_ship_to_org_id);
3513    fnd_file.put_line(fnd_file.log, 'Bill-To Organization ID: '			||x_bill_to_org_id);
3514    fnd_file.put_line(fnd_file.log, 'Customer Item ID: '				||x_customer_item_id);
3515    fnd_file.put_line(fnd_file.log, 'Transaction Start Date: '			||x_transaction_start_date);
3516    fnd_file.put_line(fnd_file.log, 'Transaction End Date: '			||NVL(v_end_date_time, SYSDATE));
3517    fnd_file.put_line(fnd_file.log, ' ');
3518 --
3519 -- v_level is used to keep track of where in the program flow the exception occurs
3520 -- Find the ship_to_address_id
3521 --
3522    v_level := 'SHIP_TO_ADDRESS_ID';
3523    --
3524    IF (l_debug <> -1) THEN
3525       rlm_core_sv.dlog(C_SDEBUG, 'x_ship_to_org_id',x_ship_to_org_id);
3526    END IF;
3527    --
3528    --IF x_ship_to_org_id IS NOT NULL THEN
3529    --
3530    -- Following query is changed as per TCA obsolescence project.
3531        SELECT CUST_ACCT_SITE_ID
3532        INTO   v_ship_to_address_id
3533        FROM   HZ_CUST_SITE_USES
3534        WHERE  site_use_id = x_ship_to_org_id
3535        AND    site_use_code = 'SHIP_TO';
3536 
3537 -- Find the intrmd_ship_to_address_id
3538    --
3539    v_level := 'INTRMD_SHIP_TO_ADDRESS_ID';
3540    --
3541   IF (l_debug <> -1) THEN
3542       rlm_core_sv.dlog(C_SDEBUG,'x_intrmd_ship_to_org_id',x_intrmd_ship_to_org_id);
3543    END IF;
3544    --
3545    IF x_intrmd_ship_to_org_id IS NOT NULL THEN
3546 	--
3547         -- Following query is changed as per TCA obsolescence project.
3548 	SELECT	CUST_ACCT_SITE_ID
3549 	INTO	v_intrmd_ship_to_address_id
3550 	FROM	HZ_CUST_SITE_USES
3551 	WHERE	site_use_id = x_intrmd_ship_to_org_id
3552 	AND	site_use_code = 'SHIP_TO';
3553 	--
3554    ELSE
3555      --
3556      -- There is no intermediate ship-to address id associated with that int shipto org id
3557      --
3558 	v_intrmd_ship_to_address_id := NULL;
3559    END IF;
3560    --
3561    IF (l_debug <> -1) THEN
3562       rlm_core_sv.dlog(C_SDEBUG,'v_intrmd_ship_to_address_id',
3563                                 v_intrmd_ship_to_address_id);
3564    END IF;
3565    --
3566 -- Find the bill_to_address_id
3567    --
3568    v_level := 'BILL_TO_ADDRESS_ID';
3569    --
3570    IF (l_debug <> -1) THEN
3571       rlm_core_sv.dlog(C_SDEBUG,'x_bill_to_org_id',x_bill_to_org_id);
3572    END IF;
3573    --
3574    IF x_bill_to_org_id IS NOT NULL THEN
3575 	--
3576         -- Following query is changed as per TCA obsolescence project.
3577 	SELECT	CUST_ACCT_SITE_ID
3578 	INTO	v_bill_to_address_id
3579 	FROM	HZ_CUST_SITE_USES
3580 	WHERE	site_use_id = x_bill_to_org_id
3581 	AND	site_use_code = 'BILL_TO';
3582 	--
3583    ELSE
3584      --
3585      -- There is no bill-to address id associated with that bill-to org id
3586 	--
3587 	v_bill_to_address_id := NULL;
3588 	--
3589    END IF;
3590    --
3591    IF (l_debug <> -1) THEN
3592       rlm_core_sv.dlog(C_SDEBUG,'v_bill_to_address_id',v_bill_to_address_id);
3593    END IF;
3594    --
3595 -- Get the setup terms at the appropriate/lowest level so that
3596    --
3597    RLM_TPA_SV.get_setup_terms(
3598 	x_ship_from_org_id 	  => x_ship_from_org_id,
3599 	x_customer_id 		  => x_customer_id,
3600 	x_ship_to_address_id 	  => v_ship_to_address_id,
3601 	x_customer_item_id 	  => x_customer_item_id,
3602 	x_terms_definition_level  => v_terms_level,
3603 	x_terms_rec	 	  => v_rlm_setup_terms_rec,
3604 	x_return_message	  => v_setup_terms_msg,
3605 	x_return_status 	  => v_setup_terms_status);
3606    --
3607 -- Continue only if the records return status is TRUE
3608 -- Continue only if the cum control code is NO_CUM
3609    --
3610    IF v_setup_terms_status = TRUE THEN --{
3611      --
3612      IF (nvl(v_rlm_setup_terms_rec.cum_control_code,'NO_CUM') <> 'NO_CUM'
3613 	AND v_rlm_setup_terms_rec.cum_control_code <> 'CUM_BY_PO_ONLY') THEN
3614 	  --{
3615 	  IF (v_terms_level = 'CUSTOMER_ITEM'
3616               AND v_rlm_setup_terms_rec.calc_cum_flag = 'Y')
3617               OR v_terms_level <> 'CUSTOMER_ITEM' THEN --{
3618 	     --
3619  	     -- Determine the select criteria based on the cum org level code
3620 	     --
3621              --
3622              IF v_rlm_setup_terms_rec.cum_org_level_code = 'BILL_TO_SHIP_FROM'
3623              THEN
3624              --
3625                 p_ship_from_org_id 	:= x_ship_from_org_id;
3626                 p_bill_to_org_id	:= x_bill_to_org_id;
3627 			--
3628              ELSIF v_rlm_setup_terms_rec.cum_org_level_code =
3629               'SHIP_TO_SHIP_FROM'
3630              THEN
3631              --
3632 		p_ship_from_org_id	:= x_ship_from_org_id;
3633 		p_ship_to_org_id	:= x_ship_to_org_id;
3634 			--
3635              ELSIF v_rlm_setup_terms_rec.cum_org_level_code =
3636               'INTRMD_SHIP_TO_SHIP_FROM'
3637              THEN
3638 		--
3639 		p_ship_from_org_id	:= x_ship_from_org_id;
3640 		p_intmed_ship_to_org_id	:= x_ship_to_org_id;
3641 		--
3642              ELSIF v_rlm_setup_terms_rec.cum_org_level_code =
3643               'SHIP_TO_ALL_SHIP_FROMS'
3644              THEN
3645              --
3646 		p_ship_to_org_id	:= x_ship_to_org_id;
3647 		--
3648 	     END IF;
3649           --
3650           IF (l_debug <> -1) THEN
3651              rlm_core_sv.dlog(C_SDEBUG, 'The criteria used to select the shipment ' ||'lines');
3652              rlm_core_sv.dlog(C_SDEBUG, 'p_ship_from_org_id', p_ship_from_org_id);
3653              rlm_core_sv.dlog(C_SDEBUG, 'p_ship_to_org_id', p_ship_to_org_id);
3654              rlm_core_sv.dlog(C_SDEBUG, 'p_intmed_ship_to_org_id',
3655                        p_intmed_ship_to_org_id);
3656              rlm_core_sv.dlog(C_SDEBUG, 'p_bill_to_org_id', p_bill_to_org_id);
3657              rlm_core_sv.dlog(C_SDEBUG, 'x_customer_item_id', x_customer_item_id);
3658              rlm_core_sv.dlog(C_SDEBUG, 'x_transaction_start_date',
3659                        x_transaction_start_date);
3660              rlm_core_sv.dlog(C_SDEBUG, 'v_end_date_time',
3661                        v_end_date_time);
3662           END IF;
3663           --
3664           fnd_file.put_line(fnd_file.log, 'p_ship_from_org_id: '
3665    		||p_ship_from_org_id);
3666           fnd_file.put_line(fnd_file.log, 'p_ship_to_org_id: '
3667                 ||p_ship_to_org_id);
3668           fnd_file.put_line(fnd_file.log, 'p_intmed_ship_to_org_id: '
3669         	||p_intmed_ship_to_org_id);
3670           fnd_file.put_line(fnd_file.log, 'p_bill_to_org_id: '
3671  		||p_bill_to_org_id);
3672           fnd_file.put_line(fnd_file.log, ' ');
3673           fnd_file.put_line(fnd_file.log,
3674                 'Loop to find all qualified shipment lines ');
3675           fnd_file.put_line(fnd_file.log, ' ');
3676           --
3677 	  --
3678 
3679            v_cum_key_record.customer_id := x_customer_id;
3680            v_cum_key_record.customer_item_id := x_customer_item_id;
3681            v_cum_key_record.ship_from_org_id := x_ship_from_org_id;
3682            v_cum_key_record.intrmd_ship_to_address_id:=
3683                                        v_intrmd_ship_to_address_id;
3684            v_cum_key_record.ship_to_address_id := v_ship_to_address_id;
3685            v_cum_key_record.bill_to_address_id := v_bill_to_address_id;
3686 
3687 
3688            rlm_cum_sv.GetCums(v_rlm_setup_terms_rec
3689                                  ,v_terms_level
3690                                  ,v_cum_key_record
3691                                  ,x_transaction_start_date
3692                                  ,v_end_date_time
3693                                  ,p_ship_from_org_id
3694                                  ,p_ship_to_org_id
3695                                  ,p_intmed_ship_to_org_id
3696                                  ,p_bill_to_org_id
3697                                  ,v_cum_records
3698                                  ,v_old_cum_records
3699                                  ,v_old_cum_counter
3700                                  ,v_return_stat);
3701           --
3702   	  IF (l_debug <> -1) THEN
3703              rlm_core_sv.dlog(C_DEBUG,'v_return_stat',v_return_stat);
3704           END IF;
3705           --
3706           IF v_return_stat = 0 THEN
3707           --
3708   	     IF (l_debug <> -1) THEN
3709                 rlm_core_sv.dlog(C_DEBUG,'Could not find any CUM to reset');
3710              END IF;
3711              --
3712              raise e_NoCum;
3713           --
3714           END IF;
3715 
3716           v_num := v_old_cum_counter.count;
3717           --
3718   	  IF (l_debug <> -1) THEN
3719              rlm_core_sv.dlog(C_DEBUG,'v_num',v_num);
3720           END IF;
3721           --
3722           IF (l_debug <> -1) THEN
3723              rlm_core_sv.dlog(C_DEBUG,'v_cum_records.count',v_cum_records.count);
3724              rlm_core_sv.dlog(C_DEBUG,'v_old_cum_records.count',
3725                                                      v_old_cum_records.count);
3726           END IF;
3727           --
3728           g_oe_tmp_line_tbl := oe_order_pub.g_miss_line_tbl;
3729           --
3730           FOR v_index IN 1..v_num LOOP --{
3731           --
3732             SAVEPOINT s_reset_counter;
3733             --
3734             --v_reset_counter is used to undo cum_keys processed with error
3735             --
3736             v_reset_counter := counter;
3737             --
3738   	    IF (l_debug <> -1) THEN
3739                rlm_core_sv.dlog(C_DEBUG,'v_reset_counter',v_reset_counter);
3740             END IF;
3741             --
3742             /* these indexes are used to get the relationships between
3743              v_cum_records, v_old_cum_records, and v_old_cum_counter
3744             */
3745             --
3746             v_start_num := v_end_num + 1;
3747             --
3748   	    IF (l_debug <> -1) THEN
3749                rlm_core_sv.dlog(C_DEBUG,'v_start_num',v_start_num);
3750             END IF;
3751             --
3752             v_end_num := v_start_num + v_old_cum_counter(v_index) -1 ;
3753             --
3754   	    IF (l_debug <> -1) THEN
3755                rlm_core_sv.dlog(C_DEBUG,'v_end_num',v_end_num);
3756             END IF;
3757             --
3758             v_index2 := v_end_num;
3759             --
3760   	    IF (l_debug <> -1) THEN
3761                rlm_core_sv.dlog(C_DEBUG,'v_index2',v_index2);
3762             END IF;
3763             --
3764             /* following are set for CalculateSuplierCum*/
3765             v_cum_records(v_index).cum_qty := 0;
3766             --
3767             v_cum_records(v_index).cum_qty_to_be_accumulated := 0;
3768             v_cum_records(v_index).cum_qty_after_cutoff := 0;
3769             --
3770             -- Set shipment inclusion rule and as of date time
3771             --
3772             v_cum_records(v_index).use_ship_incl_rule_flag := 'Y';
3773             --
3774             v_cum_records(v_index).as_of_date_time := NULL;
3775             --
3776             --set record_return_status to TRUE used in CalculateSuplierCum
3777             --
3778             v_cum_records(v_index).record_return_status := TRUE;
3779             --
3780             v_adjustment_date := NULL;
3781             --
3782             v_tmp_old_table.DELETE;
3783             g_cum_oe_lines.DELETE;
3784             --
3785             v_shipLineCounter  := 1;
3786             v_visited := FALSE;
3787 
3788             LOOP --{
3789             --
3790              p_cum_key_id := v_old_cum_records(v_index2).cum_key_id;
3791              --
3792 
3793              --calculate inventory_item_id from customer_item_id once for the old cum group
3794 
3795              IF (v_visited = FALSE) THEN
3796              --
3797                v_old_cum_records(v_index2).inventory_item_id := GetInventoryItemId(v_old_cum_records(v_index2).customer_item_id);
3798 
3799                --inventory_item_id populated in v_cum_key_record to improve queries (bug 1893220)
3800 
3801                v_cum_key_record.inventory_item_id := v_old_cum_records(v_index2).inventory_item_id;
3802                --
3803   	       IF (l_debug <> -1) THEN
3804                   rlm_core_sv.dlog(C_DEBUG,'customer_item_id',v_old_cum_records(v_index2).customer_item_id);
3805                   rlm_core_sv.dlog(C_DEBUG,'inventory_item_id',v_cum_key_record.inventory_item_id);
3806                END IF;
3807 
3808                v_visited := TRUE;
3809              --
3810 
3811              ELSE
3812 
3813                v_old_cum_records(v_index2).inventory_item_id :=v_cum_key_record.inventory_item_id;
3814 
3815              END IF;
3816 
3817 
3818       /* For each new key, put the old one in table v_tmp_old_table
3819          If SetSupplierCum returned false then keep this table so that
3820          we can delete these keys from the table passed to UpdateOldKey
3821          so that these keys won't get processed.  If everything goes well
3822          then delete this table
3823       */
3824              v_tmp_old_table(v_index2)   := p_cum_key_id;
3825              --
3826              rlm_cum_sv.GetShippLines(
3827                         x_cum_key_id             => p_cum_key_id,
3828                         x_ship_from_org_id       => p_ship_from_org_id,
3829                         x_ship_to_org_id         => p_ship_to_org_id,
3830                         x_intmed_ship_to_org_id  => p_intmed_ship_to_org_id,
3831                         x_bill_to_org_id         => p_bill_to_org_id,
3832                         x_customer_item_id       => x_customer_item_id,
3833                         x_inventory_item_id      => v_cum_key_record.inventory_item_id,
3834                         x_transaction_start_date => x_transaction_start_date,
3835                         x_transaction_end_date   => v_end_date_time,
3836                         x_index                  => v_shipLineCounter);
3837              --
3838             --set the cum_key_id from adjustment to be the new cum_key_id
3839 
3840             UPDATE rlm_cust_item_cum_adj
3841             SET cum_key_id = v_cum_records(v_index).cum_key_id
3842             WHERE cum_key_id = p_cum_key_id
3843             AND transaction_date_time <= nvl(v_end_date_time,sysdate)
3844             AND transaction_date_time >= x_transaction_start_date;
3845             --
3846 
3847              v_index2 := v_index2 -1;
3848              --
3849              IF(v_index2 < v_start_num) THEN
3850              --
3851                 EXIT;
3852              --
3853              END IF;
3854             --
3855             END LOOP; --}
3856             --
3857             --take care off the shipment for the new cum
3858             p_cum_key_id := v_cum_records(v_index).cum_key_id;
3859             --
3860             --
3861             -- if the end date is smaller than sysdate then we should
3862             --increase it to today's date to calculate the qty for
3863             --new cum correctly.
3864             --
3865             v_end_date_time := SYSDATE;
3866             --
3867             rlm_cum_sv.GetShippLines(
3868                        x_cum_key_id             => p_cum_key_id,
3869                        x_ship_from_org_id       => p_ship_from_org_id,
3870                        x_ship_to_org_id         => p_ship_to_org_id,
3871                        x_intmed_ship_to_org_id  => p_intmed_ship_to_org_id,
3872                        x_bill_to_org_id         => p_bill_to_org_id,
3873                        x_customer_item_id       => x_customer_item_id,
3874                        x_inventory_item_id      => v_cum_key_record.inventory_item_id,
3875                        x_transaction_start_date => NULL,
3876                        x_transaction_end_date   => v_end_date_time,
3877                        x_index                  => v_shipLineCounter);
3878              --
3879              IF g_cum_oe_lines.COUNT > 0 THEN
3880              --
3881                  rlm_cum_sv.quicksort(g_cum_oe_lines.FIRST,
3882                                       g_cum_oe_lines.LAST,
3883                                       C_cum_oe_lines);
3884              --
3885              END IF;
3886              --
3887             -- Need to set called_by_reset_cum to 'Y'
3888             v_cum_key_record.called_by_reset_cum := 'Y';
3889             --
3890 
3891             rlm_cum_sv.SetSupplierCum(
3892                        x_index                  => v_index,
3893                        x_cum_key_record         => v_cum_key_record,
3894                        x_transaction_start_date => x_transaction_start_date,
3895                        x_cum_records            => v_cum_records,
3896                        x_return_status          => v_return_status,
3897                        x_counter                => counter,
3898                        x_adjustment_date        => v_adjustment_date);
3899              --
3900   	     IF (l_debug <> -1) THEN
3901                 rlm_core_sv.dlog(C_DEBUG,'v_return_status',v_return_status);
3902              END IF;
3903              --
3904              IF(v_return_status = FALSE)
3905                OR (v_cum_records(v_index).record_return_status = FALSE)
3906              THEN
3907                  -- For this Cum clear the OE table and the updates in
3908                  -- adjustment table.  Also mark the old_table so the procedure
3909                  --does not recalculate the cum for them
3910                  ROLLBACK TO s_reset_counter;
3911                  --
3912                  IF counter > v_reset_counter THEN
3913                  --
3914                      FOR i in v_reset_counter..(counter-1) LOOP
3915                      --
3916                        g_oe_tmp_line_tbl.DELETE(i);
3917                      --
3918                      END LOOP;
3919                  --
3920                  END IF;
3921                  --
3922                  counter := v_reset_counter;
3923                  --
3924                  v_index3 := v_tmp_old_table.FIRST;
3925                  --
3926                  LOOP
3927                  --
3928                     v_old_cum_records(v_index3).cum_key_id := NULL;
3929                     --
3930                     EXIT WHEN v_index3 = v_tmp_old_table.LAST;
3931                     --
3932                     v_index3 := v_tmp_old_table.NEXT(v_index3);
3933                  --
3934                  END LOOP;
3935                  --
3936                  v_tmp_old_table.DELETE;
3937                  --
3938              ELSE --if there are no problems then update the CUM table
3939                  --
3940                  -- get all adjustments after the last shipment
3941                  IF v_adjustment_date IS NULL THEN
3942                  --
3943                    SELECT  SUM(transaction_qty)
3944                    INTO    adj_qty
3945                    FROM    rlm_cust_item_cum_adj
3946                    WHERE   cum_key_id = v_cum_records(v_index).cum_key_id
3947                    AND     transaction_date_time <= sysdate;
3948                  --
3949                  ELSE
3950                  --
3951                    SELECT  SUM(transaction_qty)
3952                    INTO    adj_qty
3953                    FROM    rlm_cust_item_cum_adj
3954                    WHERE   cum_key_id = v_cum_records(v_index).cum_key_id
3955                    AND     transaction_date_time >= v_adjustment_date
3956                    AND     transaction_date_time <= sysdate;
3957                  --
3958                  END IF;
3959 		 --
3960   		 IF (l_debug <> -1) THEN
3961                     rlm_core_sv.dlog(C_DEBUG,'v_adjustment_date',
3962                                                        v_adjustment_date);
3963                  END IF;
3964                  --
3965                  IF adj_qty IS NULL THEN
3966                  --
3967                    adj_qty := 0;
3968                    --
3969                  --
3970                  END IF;
3971                  --
3972   		 IF (l_debug <> -1) THEN
3973                     rlm_core_sv.dlog(C_DEBUG, 'adj_qty',adj_qty);
3974                     rlm_core_sv.dlog(C_DEBUG,'v_index',v_index);
3975                  END IF;
3976 
3977                  v_cum_records(v_index).cum_qty :=
3978                                     v_cum_records(v_index).cum_qty + adj_qty;
3979                  --
3980                  UPDATE rlm_cust_item_cum_keys
3981                  SET cum_qty                   = v_cum_records(v_index).cum_qty,
3982                      cum_qty_to_be_accumulated =
3983                               v_cum_records(v_index).cum_qty_to_be_accumulated,
3984                      cum_qty_after_cutoff =
3985                               v_cum_records(v_index).cum_qty_after_cutoff,
3986                      last_cum_qty_update_date  = SYSDATE,
3987                      last_update_login         = FND_GLOBAL.LOGIN_ID,
3988                      last_update_date          = SYSDATE,
3989                      last_updated_by           = FND_GLOBAL.USER_ID
3990                      WHERE   cum_key_id = v_cum_records(v_index).cum_key_id
3991                      AND     NVL(inactive_flag,'N')          =  'N';
3992                      --
3993   		 IF (l_debug <> -1) THEN
3994                     rlm_core_sv.dlog(C_DEBUG,'v_cum_records(v_index).cum_key_id',
3995                                             v_cum_records(v_index).cum_key_id);
3996                     rlm_core_sv.dlog(C_DEBUG,'v_cum_records(v_index).cum_qty',
3997                                             v_cum_records(v_index).cum_qty);
3998                  END IF;
3999                  --
4000              END IF;
4001              --
4002           END LOOP; --}
4003           --  group the g_oe_tmp_line_tbl by header_id
4004           IF g_oe_tmp_line_tbl.COUNT = 0 THEN
4005           --
4006   	     IF (l_debug <> -1) THEN
4007                 rlm_core_sv.dlog(C_DEBUG,'There are no order lines to process');
4008              END IF;
4009              --
4010              RAISE e_NoShipment;
4011           --
4012           END IF;
4013           --
4014           rlm_cum_sv.quicksort(g_oe_tmp_line_tbl.FIRST,
4015                                g_oe_tmp_line_tbl.LAST,
4016                                RLM_CUM_SV.C_line_table_type);
4017           --
4018           v_index3 := g_oe_tmp_line_tbl.FIRST;
4019           --
4020           IF (l_debug <> -1) THEN
4021              rlm_core_sv.dlog(C_DEBUG, 'v_index3',v_index3);
4022           END IF;
4023           --
4024           v_index2 := 1;
4025           --
4026           v_header_id := g_oe_tmp_line_tbl(v_index3).header_id;
4027           --
4028           IF (l_debug <> -1) THEN
4029              rlm_core_sv.dlog(C_DEBUG, 'v_header_id',v_header_id);
4030           END IF;
4031           --
4032           g_oe_line_tbl := oe_order_pub.g_miss_line_tbl;
4033           --
4034           --g_oe_tmp_line_tbl := oe_order_pub.g_miss_line_tbl;
4035           --
4036           BEGIN --{
4037           --
4038           fnd_profile.get('ECE_OUT_FILE_PATH',v_om_dbg_dir);
4039           --
4040           fnd_profile.put('OE_DEBUG_LOG_DIRECTORY',v_om_dbg_dir);
4041           --
4042           l_file_val      := OE_DEBUG_PUB.Set_Debug_Mode('FILE');
4043           --
4044           --oe_Debug_pub.setdebuglevel(5);
4045           --
4046           IF (l_debug <> -1) THEN
4047              rlm_core_sv.dlog(C_DEBUG, 'l_file_val',l_file_val);
4048           END IF;
4049           --
4050           LOOP --{
4051           /* Call OE_Order_GRP.Process_Order procedure to update OE_ORDER_LINES
4052              table by passing the g_oe_line_tbl structure that has been prepared
4053              this loop calls the process_order API once per each header_id,
4054              since the table is sorted by header_id then this loop
4055              calls the process_order once the header_id is changed  */
4056              --
4057              IF g_oe_tmp_line_tbl(v_index3).header_id = v_header_id THEN --{
4058              --
4059                 g_oe_line_tbl(v_index2) := g_oe_tmp_line_tbl(v_index3);
4060                 --
4061                 v_index2 := v_index2 + 1;
4062                 --
4063                 IF v_index3 = g_oe_tmp_line_tbl.LAST THEN --{
4064                    -- process order for the last time
4065                    OE_Order_GRP.Process_order(
4066                      p_api_version_number     => x_oe_api_version,
4067                      p_init_msg_list          => FND_API.G_TRUE,
4068                      p_return_values          => FND_API.G_FALSE,
4072                      x_msg_data               => v_msg_data,
4069                      p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
4070                      x_return_status          => l_return_status,
4071                      x_msg_count              => v_msg_count,
4073                      ------------------------------------------
4074                      p_line_tbl               => g_oe_line_tbl,
4075                      ------------------------------------------
4076                      x_header_rec             => g_oe_header_out_rec,
4077                      x_header_val_rec         => g_oe_header_val_out_rec,
4078                      x_Header_Adj_tbl         => g_oe_Header_Adj_out_tbl,
4079                      x_Header_Adj_val_tbl     => g_oe_Header_Adj_val_out_tbl,
4080                      x_Header_price_Att_tbl   => g_Header_price_Att_out_tbl,
4081                      x_Header_Adj_Att_tbl     => g_Header_Adj_Att_out_tbl,
4082                      x_Header_Adj_Assoc_tbl   => g_Header_Adj_Assoc_out_tbl,
4083                      x_Header_Scredit_tbl     => g_oe_Header_Scredit_out_tbl,
4084                      x_Header_Scredit_val_tbl => g_oe_Hdr_Scdt_val_out_tbl,
4085                      x_line_tbl               => l_oe_line_tbl_out,
4086                      x_line_val_tbl           => g_oe_line_val_out_tbl,
4087                      x_Line_Adj_tbl           => g_oe_line_Adj_out_tbl,
4088                      x_Line_Adj_val_tbl       => g_oe_line_Adj_val_out_tbl,
4089                      x_Line_price_Att_tbl     => g_Line_price_Att_out_tbl,
4090                      x_Line_Adj_Att_tbl       => g_Line_Adj_Att_out_tbl,
4091                      x_Line_Adj_Assoc_tbl     => g_Line_Adj_Assoc_out_tbl,
4092                      x_Line_Scredit_tbl       => g_oe_line_scredit_out_tbl,
4093                      x_Line_Scredit_val_tbl   => g_oe_line_scredit_val_out_tbl,
4094                      x_Lot_Serial_tbl         => g_oe_lot_serial_out_tbl,
4095                      x_Lot_Serial_val_tbl     => g_oe_lot_serial_val_out_tbl,
4096                      x_Action_Request_tbl     => g_oe_Action_Request_out_Tbl) ;
4097                      --
4098   		     IF (l_debug <> -1) THEN
4099                         rlm_core_sv.dlog(C_DEBUG,'G_FILE',OE_DEBUG_PUB.G_FILE);
4100                         rlm_core_sv.dlog(C_DEBUG, 'Input tbl count',
4101                                          g_oe_line_tbl.LAST);
4102                         rlm_core_sv.dlog(C_DEBUG, 'Output tbl count',
4103                                          l_oe_line_tbl_out.LAST);
4104                      END IF;
4105                      --
4106                   -- Handle the exceptions caused by the OE call
4107                      --
4108 
4109                    IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN --{
4110                             --
4111                             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4112                             --}
4113                     ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN --{
4114                             --
4115                             RAISE FND_API.G_EXC_ERROR;
4116                             --}
4117                     ELSE    --{
4118                             --
4119                             counter2 := counter2 + 1;
4120                             x_return_status := TRUE;
4121                             --
4122   			    IF (l_debug <> -1) THEN
4123                                rlm_core_sv.dlog(C_DEBUG,
4124                                 'Order line is updated successfully');
4125                             END IF;
4126                             --
4127                             rlm_message_sv.get_msg_text(
4128                                     x_message_name  => 'RLM_CUM_RESET_TRAIL4',
4129                                     x_text          => v_trail4);
4130                             --
4131                             fnd_file.put_line(fnd_file.output, v_trail1);
4132                             fnd_file.put_line(fnd_file.output, v_trail2);
4133                             fnd_file.put_line(fnd_file.output, v_trail3);
4134                             fnd_file.put_line(fnd_file.output, v_trail4);
4135                             --
4136                     END IF; --}
4137                    EXIT;
4138                 --}
4139                 --{ if not the last element
4140                 ELSE
4141                 --
4142                    v_index3 := g_oe_tmp_line_tbl.NEXT(v_index3);
4143                    --
4144                 END IF; --}
4145              --}
4146              --{ if header_id has changed
4147              ELSE
4148                 --
4149                 v_header_id := g_oe_tmp_line_tbl(v_index3).header_id;
4150                 --
4151                 v_index2 := 1;
4152                 --
4153 
4154                 OE_Order_GRP.Process_order(
4155                      p_api_version_number     => x_oe_api_version,
4156                      p_init_msg_list          => FND_API.G_TRUE,
4157                      p_return_values          => FND_API.G_FALSE,
4158                      p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
4159                      x_return_status          => l_return_status,
4160                      x_msg_count              => v_msg_count,
4161                      x_msg_data               => v_msg_data,
4162                      ------------------------------------------
4163                      p_line_tbl               => g_oe_line_tbl,
4164                      ------------------------------------------
4165                      x_header_rec             => g_oe_header_out_rec,
4169                      x_Header_price_Att_tbl   => g_Header_price_Att_out_tbl,
4166                      x_header_val_rec         => g_oe_header_val_out_rec,
4167                      x_Header_Adj_tbl         => g_oe_Header_Adj_out_tbl,
4168                      x_Header_Adj_val_tbl     => g_oe_Header_Adj_val_out_tbl,
4170                      x_Header_Adj_Att_tbl     => g_Header_Adj_Att_out_tbl,
4171                      x_Header_Adj_Assoc_tbl   => g_Header_Adj_Assoc_out_tbl,
4172                      x_Header_Scredit_tbl     => g_oe_Header_Scredit_out_tbl,
4173                      x_Header_Scredit_val_tbl => g_oe_Hdr_Scdt_val_out_tbl,
4174                      x_line_tbl               => l_oe_line_tbl_out,
4175                      x_line_val_tbl           => g_oe_line_val_out_tbl,
4176                      x_Line_Adj_tbl           => g_oe_line_Adj_out_tbl,
4177                      x_Line_Adj_val_tbl       => g_oe_line_Adj_val_out_tbl,
4178                      x_Line_price_Att_tbl     => g_Line_price_Att_out_tbl,
4179                      x_Line_Adj_Att_tbl       => g_Line_Adj_Att_out_tbl,
4180                      x_Line_Adj_Assoc_tbl     => g_Line_Adj_Assoc_out_tbl,
4181                      x_Line_Scredit_tbl       => g_oe_line_scredit_out_tbl,
4182                      x_Line_Scredit_val_tbl   => g_oe_line_scredit_val_out_tbl,
4183                      x_Lot_Serial_tbl         => g_oe_lot_serial_out_tbl,
4184                      x_Lot_Serial_val_tbl     => g_oe_lot_serial_val_out_tbl,
4185                      x_Action_Request_tbl     => g_oe_Action_Request_out_Tbl) ;
4186                      --
4187                   -- Handle the exceptions caused by the OE call
4188                      --
4189   		     IF (l_debug <> -1) THEN
4190                         rlm_core_sv.dlog(C_DEBUG,'G_FILE',OE_DEBUG_PUB.G_FILE);
4191                         rlm_core_sv.dlog(C_DEBUG, 'Input tbl count',
4192                                          g_oe_line_tbl.LAST);
4193                         rlm_core_sv.dlog(C_DEBUG, 'Output tbl count',
4194                                          l_oe_line_tbl_out.LAST);
4195                      END IF;
4196                      --
4197                      IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN --{
4198                              --
4199                              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4200                              --}
4201                      ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN --{
4202                              --
4203                              RAISE FND_API.G_EXC_ERROR;
4204                              --}
4205                      ELSE    --{
4206                              --
4207                              counter2 := counter2 + 1;
4208                              x_return_status := TRUE;
4209                              --
4210   			     IF (l_debug <> -1) THEN
4211                                 rlm_core_sv.dlog(C_DEBUG,
4212                                  'Order line is updated successfully');
4213                              END IF;
4214                              --
4215                              rlm_message_sv.get_msg_text(
4216                                      x_message_name  => 'RLM_CUM_RESET_TRAIL4',
4217                                      x_text          => v_trail4);
4218                              --
4219                              fnd_file.put_line(fnd_file.output, v_trail1);
4220                              fnd_file.put_line(fnd_file.output, v_trail2);
4221                              fnd_file.put_line(fnd_file.output, v_trail3);
4222                              fnd_file.put_line(fnd_file.output, v_trail4);
4223                              --
4224                      END IF; --}
4225                      --
4226                      g_oe_line_tbl.DELETE;
4227                      --
4228                      g_oe_line_tbl := oe_order_pub.g_miss_line_tbl;
4229                      --
4230                      g_oe_line_tbl(v_index2) := g_oe_tmp_line_tbl(v_index3);
4231                      --
4232                   END IF; --} if the header_id has not changed
4233                   --
4234             END LOOP; --}
4235             --
4236             EXCEPTION
4237                   --
4238                 WHEN FND_API.G_EXC_ERROR THEN --{
4239                       --
4240                       x_return_status := FALSE;
4241                       --
4242                    -- Get message count and data
4243                       --
4244                       OE_MSG_PUB.Count_And_Get(
4245                                       p_count => v_msg_count,
4246                                       p_data  => v_msg_data);
4247                       --
4248   		      IF (l_debug <> -1) THEN
4249                          rlm_core_sv.dlog(C_SDEBUG, 'G_EXC_ERROR');
4250                          rlm_core_sv.dlog(C_SDEBUG, 'l_return_status',
4251                                                               l_return_status);
4252                          rlm_core_sv.dlog(C_SDEBUG, 'x_return_status',
4253                                                               x_return_status);
4254                          rlm_core_sv.dlog(C_SDEBUG, 'v_msg_count', v_msg_count);
4255                          rlm_core_sv.dlog(C_SDEBUG, 'Main v_msg_data', v_msg_data);
4256                       END IF;
4257                       --
4258                       fnd_file.put_line(fnd_file.log, 'Process Order Error: '
4259                                                                   ||v_msg_data);
4260                       fnd_file.put_line(fnd_file.log, ' ');
4261                       --
4262                       IF v_msg_count > 0 THEN  --{
4263                         --
4264                         FOR k in 1 .. v_msg_count LOOP --{
4265                               --
4266                               v_msg_data := oe_msg_pub.get( p_msg_index => k,
4267                                                             p_encoded=> 'F');
4268                               --
4269                               fnd_file.put_line(fnd_file.log, v_msg_data);
4270 			      --
4271   			      IF (l_debug <> -1) THEN
4272                                  rlm_core_sv.dlog(C_SDEBUG, 'v_msg_data',
4273                                                                    v_msg_data);
4274                               END IF;
4275                               --
4276                         END LOOP; --}
4277                         --
4278                       END IF; --}
4279                       --
4280   		      IF (l_debug <> -1) THEN
4281                          rlm_core_sv.dpop(C_SDEBUG,
4282                                             'Process Order Error. Rollback');
4283                       END IF;
4284                       --
4285                       --
4286                       rlm_message_sv.get_msg_text(
4287                               x_message_name  => 'RLM_CUM_RESET_TRAIL5',
4288                               x_text          => v_trail5);
4289                       --
4290                       fnd_file.put_line(fnd_file.output, v_trail1);
4291                       fnd_file.put_line(fnd_file.output, v_trail2);
4292                       fnd_file.put_line(fnd_file.output, v_trail3);
4293                       fnd_file.put_line(fnd_file.output, v_trail4);
4294                       fnd_file.put_line(fnd_file.output, v_trail5);
4295                       --
4296                       error_count := error_count+1;
4297                       --
4298                       ROLLBACK;
4299                       --}
4300 
4301                 WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4302                       --{
4303                       x_return_status := FALSE;
4304                       --
4305                       --
4306                    -- Get message count and data
4307                       --
4308                       OE_MSG_PUB.Count_And_Get(
4309                                       p_count => v_msg_count,
4310                                       p_data  => v_msg_data);
4311                       --
4312   		      IF (l_debug <> -1) THEN
4313                          rlm_core_sv.dlog(C_SDEBUG, 'G_EXC_UNEXPECTED_ERROR');
4314                          rlm_core_sv.dlog(C_SDEBUG, 'l_return_status',
4315                                                             l_return_status);
4316                          rlm_core_sv.dlog(C_SDEBUG, 'x_return_status',
4317                                                             x_return_status);
4318                          rlm_core_sv.dlog(C_SDEBUG, 'v_msg_count', v_msg_count);
4319                          rlm_core_sv.dlog(C_SDEBUG, 'Main v_msg_data', v_msg_data);
4320                       END IF;
4321                       --
4322                       fnd_file.put_line(fnd_file.log, 'Process Order Error: '
4323                                                                ||v_msg_data);
4324                       fnd_file.put_line(fnd_file.log, ' ');
4325                       IF v_msg_count > 0 THEN   --{
4326                       --
4327                         FOR k in 1 .. v_msg_count LOOP  --{
4328                               --
4329                               v_msg_data := oe_msg_pub.get( p_msg_index => k,
4330                                                             p_encoded=> 'F');
4331                               --
4332                               fnd_file.put_line(fnd_file.log, v_msg_data);
4333 			      --
4334   			      IF (l_debug <> -1) THEN
4335                                  rlm_core_sv.dlog(C_SDEBUG, 'v_msg_data',
4336                                                                    v_msg_data);
4337                               END IF;
4338                               --
4339                         END LOOP; --}
4340                         --
4341                       END IF; --}
4342                       --
4343   		      IF (l_debug <> -1) THEN
4344                          rlm_core_sv.dpop(C_SDEBUG,
4345                                              'Process Order Error. Rollback');
4346                       END IF;
4347                       --
4348                       --
4349                       fnd_file.put_line(fnd_file.log, 'Process Order Error: '
4350                                                                   ||v_msg_data);
4351                       fnd_file.put_line(fnd_file.log, ' ');
4352                       --
4353                       rlm_message_sv.get_msg_text(
4354                               x_message_name  => 'RLM_CUM_RESET_TRAIL5',
4355                               x_text          => v_trail5);
4356                       --
4357                       fnd_file.put_line(fnd_file.output, v_trail1);
4358                       fnd_file.put_line(fnd_file.output, v_trail2);
4359                       fnd_file.put_line(fnd_file.output, v_trail3);
4360                       fnd_file.put_line(fnd_file.output, v_trail4);
4361                       fnd_file.put_line(fnd_file.output, v_trail5);
4362                       --
4363                       error_count := error_count+1;
4364                       --
4365                       ROLLBACK;
4366                       --}
4367                 WHEN OTHERS THEN
4368                       --{
4369                       x_return_status := FALSE;
4370                       --
4371                    -- Get message count and data
4372                       --
4373                       OE_MSG_PUB.Count_And_Get(
4374                                       p_count => v_msg_count,
4375                                       p_data  => v_msg_data);
4376                       --
4377   		      IF (l_debug <> -1) THEN
4378                          rlm_core_sv.dlog(C_SDEBUG, 'WHEN OTHERS');
4379                          rlm_core_sv.dlog(C_SDEBUG, 'l_return_status',
4380                                                                l_return_status);
4381                          rlm_core_sv.dlog(C_SDEBUG, 'x_return_status',
4382                                                                x_return_status);
4383                          rlm_core_sv.dlog(C_SDEBUG, 'v_msg_count', v_msg_count);
4384                          rlm_core_sv.dlog(C_SDEBUG, 'Main v_msg_data', v_msg_data);
4385                       END IF;
4386                       --
4387                       fnd_file.put_line(fnd_file.log, 'Process Order Error: '
4388                                                                ||v_msg_data);
4389                       fnd_file.put_line(fnd_file.log, ' ');
4390                       --
4391                       IF v_msg_count > 0 THEN  --{
4392                       --
4393                         FOR k in 1 .. v_msg_count LOOP  --{
4394                               --
4395                               v_msg_data := oe_msg_pub.get( p_msg_index => k,
4396                                                             p_encoded=> 'F');
4397                               --
4398                               fnd_file.put_line(fnd_file.log, v_msg_data);
4399 			      --
4400   			      IF (l_debug <> -1) THEN
4401                                  rlm_core_sv.dlog(C_SDEBUG, 'v_msg_data',
4402                                                                   v_msg_data);
4403                               END IF;
4404                               --
4405                         END LOOP;   --}
4406                         --
4407                       END IF; --}
4408                       --
4409   		      IF (l_debug <> -1) THEN
4410                          rlm_core_sv.dpop(C_SDEBUG,
4411                                               'Process Order Error. Rollback');
4412                       END IF;
4413                       --
4414                       fnd_file.put_line(fnd_file.log, 'Process Order Error: '
4415                                                                 || v_msg_data);
4416                       fnd_file.put_line(fnd_file.log, ' ');
4417                       --
4418                       rlm_message_sv.get_msg_text(
4419                               x_message_name  => 'RLM_CUM_RESET_TRAIL5',
4420                               x_text          => v_trail5);
4421                       --
4422                       fnd_file.put_line(fnd_file.output, v_trail1);
4423                       fnd_file.put_line(fnd_file.output, v_trail2);
4424                       fnd_file.put_line(fnd_file.output, v_trail3);
4425                       fnd_file.put_line(fnd_file.output, v_trail4);
4426                       fnd_file.put_line(fnd_file.output, v_trail5);
4427                       --
4428                       error_count := error_count+1;
4429                       --
4430                       ROLLBACK;
4431                       --}
4432           END;  --}
4433    -- DEBUGGING
4434    --
4435        END IF;		-- v_terms_level }
4436        --}
4437      ELSE --{
4438     --
4439     v_msg_data := 'Adjust CUM Transactions is not processed against NO_CUM and CUM_BY_PO_ONLY CUM management types';
4440     --
4441            IF (l_debug <> -1) THEN
4442               rlm_core_sv.dlog(C_DEBUG, v_msg_data);
4443            END IF;
4444     --
4445     fnd_file.put_line(fnd_file.log, v_msg_data);
4446     --
4447      END IF; 		-- cum_control_code }
4448      --}
4449    ELSE --{
4450     --
4451     v_msg_data := 'Release Management setup terms are not found';
4452     --
4453     IF (l_debug <> -1) THEN
4454        rlm_core_sv.dlog(C_DEBUG, v_msg_data);
4455     END IF;
4456      --
4457     fnd_file.put_line(fnd_file.log, v_msg_data);
4458     --
4459    END IF;		-- setup_terms_status }
4460    --
4461    v_msg_data := 'Number of lines eligible for adjustment: ';
4462    --
4463    IF (l_debug <> -1) THEN
4464       rlm_core_sv.dlog(C_DEBUG, v_msg_data, counter -1);
4465    END IF;
4466    --
4467    --fnd_file.put_line(fnd_file.log, v_msg_data ||counter2);
4468    --
4469    IF error_count > 0 THEN --{
4470 	--
4471 	v_msg_data := 'Completed with errors';
4472 	--
4473         IF (l_debug <> -1) THEN
4474    	  rlm_core_sv.dpop(C_DEBUG, v_msg_data);
4475 	END IF;
4476 	--
4477    	fnd_file.put_line(fnd_file.log, v_msg_data);
4478 	-- }
4479    ELSE
4480 	-- {
4481         IF (l_debug <> -1) THEN
4482    	  rlm_core_sv.dlog(C_DEBUG, 'v_old_cum_records.COUNT',
4483                                           v_old_cum_records.COUNT);
4484 	END IF;
4485 	--
4486 	IF v_old_cum_records.COUNT > 0 THEN
4487 		--
4488 		UpdateOldKey(	v_old_cum_records,
4489 				v_cum_records(1).shipment_rule_code,
4490 				v_cum_records(1).yesterday_time_cutoff,
4491                                 v_cum_key_record,
4492                                 p_ship_from_org_id,
4493                                 p_ship_to_org_id,
4494                                 p_intmed_ship_to_org_id,
4495                                 p_bill_to_org_id,
4496                                 x_customer_item_id,
4497 				v_return_status);
4498 		--
4499 		IF v_return_status THEN
4500 			--
4501 			v_msg_data := 'Completed successfully';
4502 			--
4503 	   		COMMIT;
4504 			--
4505 		ELSE
4506 			--
4507 			v_msg_data := 'Update old CUM key error. Rollback';
4508 			--
4509 			ROLLBACK;
4510 			--
4511 		END IF;
4512 		--
4513 	ELSE
4514 		--
4515 		v_msg_data := 'Completed successfully';
4516 		--
4517 	END IF;
4518 	--
4519         IF (l_debug <> -1) THEN
4520    	  rlm_core_sv.dpop(C_DEBUG, v_msg_data);
4521 	END IF;
4522 	--
4523 	fnd_file.put_line(fnd_file.log, v_msg_data);
4524 	--
4525    END IF;--} if error_count = 0
4526    --
4527  EXCEPTION
4528      --
4529      WHEN NO_DATA_FOUND THEN
4530 	--
4531 	IF v_level = 'SHIP_TO_ADDRESS_ID' THEN
4532            --
4533            IF (l_debug <> -1) THEN
4534               rlm_core_sv.dlog(C_SDEBUG,
4535                'No ship_to_address_id is associated with the ship_to_org_id');
4536            END IF;
4537            --
4538 	ELSIF v_level = 'INTRMD_SHIP_TO_ADDRESS_ID' THEN
4539            --
4540   	   IF (l_debug <> -1) THEN
4541               rlm_core_sv.dlog(C_SDEBUG,
4542               'No intrmd_ship_to_address_id is associated with the'
4543                ||'  intmed_ship_to_org_id');
4544            END IF;
4545            --
4546 	ELSIF v_level = 'CUSTOMER_ID' THEN
4547            --
4548   	   IF (l_debug <> -1) THEN
4549               rlm_core_sv.dlog(C_SDEBUG,
4550               'No customer_id is associated with the address_id');
4551            END IF;
4552            --
4553 	ELSIF v_level = 'BILL_TO_ADDRESS_ID' THEN
4554            --
4555   	   IF (l_debug <> -1) THEN
4556               rlm_core_sv.dlog(C_SDEBUG,
4557                'No bill_to_address_id is associated with the bill_to_org_id');
4558            END IF;
4559            --
4560          ELSIF v_level = 'SHIP_TO_ADDRESS_ID2' THEN
4561            --
4562   	   IF (l_debug <> -1) THEN
4563               rlm_core_sv.dlog(C_SDEBUG,
4564                 'Second time: No ship_to_address_id is associated with the'
4565                 ||'  ship_to_org_id');
4566            END IF;
4567            --
4568 	ELSIF v_level = 'INTRMD_SHIP_TO_ADDRESS_ID2' THEN
4569            --
4570   	   IF (l_debug <> -1) THEN
4571               rlm_core_sv.dlog(C_SDEBUG,
4572                 'Second time: No intrmd_ship_to_address_id is associated '
4573                 ||'with the intmed_ship_to_org_id');
4574            END IF;
4575            --
4576 	ELSIF v_level = 'BILL_TO_ADDRESS_ID2' THEN
4577            --
4578   	   IF (l_debug <> -1) THEN
4579               rlm_core_sv.dlog(C_SDEBUG,
4580                  'Second time: No bill_to_address_id is associated with the'
4581                  || '  bill_to_org_id');
4582            END IF;
4583            --
4584 	END IF;
4585 	--
4586         ROLLBACK;
4587         --
4588         IF (l_debug <> -1) THEN
4589    	  rlm_core_sv.dpop(C_SDEBUG, 'No data found');
4590 	END IF;
4591 	--
4592      WHEN e_NoCum THEN
4593         --
4594         --ROLLBACK;
4595         COMMIT;
4596         --
4597         v_msg_data := 'There are no qualifying shipments.';
4598         --
4599         fnd_file.put_line(fnd_file.log,v_msg_data);
4600         --
4601   	IF (l_debug <> -1) THEN
4602            rlm_core_sv.dpop(C_SDEBUG,'ResetCum:e_NoCum');
4603         END IF;
4604         --
4605      WHEN e_NoShipment THEN
4606         --
4607         v_msg_data := 'There are no qualifying shipments.  Manual adjustments'
4608           ||' would be processed';
4609         --
4610         fnd_file.put_line(fnd_file.log,v_msg_data);
4611         --
4612         BEGIN
4613         --
4614   	IF (l_debug <> -1) THEN
4615    	  rlm_core_sv.dlog(C_DEBUG, 'v_old_cum_records.COUNT',
4616                                           v_old_cum_records.COUNT);
4617 	END IF;
4618 	--
4619 	IF v_old_cum_records.COUNT > 0 THEN
4620 		--
4621                 UpdateOldKey(  v_old_cum_records,
4622                                 v_cum_records(1).shipment_rule_code,
4623                                 v_cum_records(1).yesterday_time_cutoff,
4624                                 v_cum_key_record,
4625                                 p_ship_from_org_id,
4626                                 p_ship_to_org_id,
4627                                 p_intmed_ship_to_org_id,
4628                                 p_bill_to_org_id,
4629                                 x_customer_item_id,
4630                                 v_return_status);
4631 		--
4632 		IF v_return_status THEN
4633 			--
4634 			v_msg_data := 'Completed successfully';
4635 			--
4636 	   		COMMIT;
4637 			--
4638 		ELSE
4639 			--
4640 			v_msg_data := 'Update old CUM key error. Rollback';
4641 			--
4642 			ROLLBACK;
4643 			--
4644 		END IF;
4645                 --
4646                 fnd_file.put_line(fnd_file.log,v_msg_data);
4647 		--
4648 	ELSE
4649 		--
4650 		v_msg_data := 'Completed successfully';
4651                 --
4652                 fnd_file.put_line(fnd_file.log,v_msg_data);
4653 		--
4654 	END IF;
4655 	--
4656         COMMIT;
4657         --
4658         EXCEPTION
4659           WHEN OTHERS THEN
4660 	    --
4661   	    IF (l_debug <> -1) THEN
4662                rlm_core_sv.dlog(C_DEBUG, 'when others::',substr(SQLERRM,1,200));
4663             END IF;
4664 	    --
4665             fnd_file.put_line(fnd_file.log, substr(SQLERRM,1,200));
4666              --
4667              x_return_status := FALSE;
4668         END;
4669 	--
4670   	IF (l_debug <> -1) THEN
4671            rlm_core_sv.dpop(C_SDEBUG,'ResetCum:e_NoShipment');
4672         END IF;
4673         --
4674      WHEN OTHERS THEN
4675 	--
4676   	IF (l_debug <> -1) THEN
4677    	  rlm_core_sv.dpop(C_SDEBUG, substr(SQLERRM,1,200));
4678 	END IF;
4679 	--
4680 	fnd_file.put_line(fnd_file.log, substr(SQLERRM,1,200));
4681         --
4682         x_return_status := FALSE;
4683         --
4684         ROLLBACK;
4685 	--
4686  END ResetCum;
4687 
4688 
4689 
4690 /*============================================================================
4691 
4692   PROCEDURE NAME:       GetCums
4693 
4694 
4695   This procedure is called by reset cum.  According to the parameters and
4696   the setup terms for the cum, it gets all the cum records from
4697   rlm_cust_item_cum_keys.  From the records returned, the program filters
4698   out those recors which do not have any manual adjustments within the period
4699   nor any shipments(if there is any shipment the field last_cum_qty_update_date
4700   would be within the time frame).  These records would be sorted, so that all
4701   corresponding Cums are in sorted together(for example same customer items
4702   may be sorted together).  The program recognizes the first record of each
4703   group as the newly created CUM and the reset of the group as the CUMs which
4704   are being adjusted.  It then puts all the new cums in one table and all the
4705   old one in a different table.
4706 
4707 
4708   PARAMETERS:
4709 
4710    **  x_rlm_setup_terms_record      IN
4711        This is setupterms generated from the resetCums parameters
4712    **  x_terms_level                 IN
4713        This is the same as parameter x_terms_definition_level of
4714        rlm_setup_terms_sv.get_setup_terms
4715    **  x_cum_key_record              IN
4716        This records would containe the followings if it could be derived in
4717        resetcum
4718        bill_to_address_id, ship_to_address_id, intrmd_ship_to_address_id,
4719        ship_from_org_id,customer_item_id,customer_id
4720    **  x_transaction_start_date      IN
4721        same as parameter in resetcum
4722    **  x_transaction_end_date        IN
4723        same as parameter in resetcum would be defaulted to sysdate
4724    **  x_cum_records                 OUT NOCOPY
4725        These are records of all new cum_keys created
4726    **  x_old_cum_records             OUT NOCOPY
4727        These are all old_cums which either have shipment or manual adjustments
4728    **  x_counter                     OUT NOCOPY
4729        This is a table that indicates the relation ship between x_cum_records
4730        and x_old_cum_records.  For example, if x_cum_records(3) has
4731        2 records in the old cums table x_old_cum_records, then
4732        x_counter(3) would have the value 2
4733    **  x_return_status               OUT NOCOPY
4734        1 if any cums found to be adjusted, 0 if no cum
4735 
4736 =============================================================================*/
4737 
4738  PROCEDURE GetCums (
4739         x_rlm_setup_terms_record  IN  rlm_setup_terms_sv.setup_terms_rec_typ,
4740         x_terms_level             IN  VARCHAR2,
4741         x_cum_key_record          IN  OUT NOCOPY rlm_cum_sv.cum_key_attrib_rec_type,
4742         x_transaction_start_date  IN  DATE,
4743         x_transaction_end_date    IN  DATE ,
4744         x_ship_from_org_id        IN  NUMBER,
4745         x_ship_to_org_id          IN  NUMBER,
4746         x_intmed_ship_to_org_id   IN  NUMBER,
4747         x_bill_to_org_id          IN  NUMBER,
4748         x_cum_records             OUT NOCOPY RLM_CUM_SV.t_cums,
4749         x_old_cum_records         OUT NOCOPY RLM_CUM_SV.t_cums,
4750         x_counter                 OUT NOCOPY RLM_CUM_SV.t_new_ship_count,
4751         x_return_status           OUT NOCOPY NUMBER)
4752  IS
4753   v_cum_control_code              rlm_cust_shipto_terms.cum_control_code%TYPE;
4754   v_cum_org_level_code            rlm_cust_shipto_terms.cum_org_level_code%TYPE;
4755   v_tmp_cum_record                rlm_cum_sv.cum_rec_type;
4756   v_address_id                    NUMBER;
4757   v_cum_rec_ctr                   NUMBER DEFAULT 0;
4758   v_old_cum_ctr                   NUMBER DEFAULT 0;
4759   v_counter_ctr                   NUMBER DEFAULT 0;
4760   v_exist                         NUMBER;
4761   v_new_customer_item_id          NUMBER DEFAULT 0;
4762   v_old_customer_item_id         NUMBER DEFAULT 0;
4763   p_ship_from_org_id              NUMBER DEFAULT NULL;
4764   p_ship_to_address_id            NUMBER DEFAULT NULL;
4765   p_intrmd_ship_to_address_id     NUMBER DEFAULT NULL;
4766   p_bill_to_address_id            NUMBER DEFAULT NULL;
4767   p_customer_item_id              NUMBER DEFAULT NULL;
4768   msg_data                        VARCHAR2(2500);
4769   msg_name                        VARCHAR2(30) DEFAULT NULL;
4770   v_new_purchase_order_number     RLM_CUST_ITEM_CUM_KEYS.PURCHASE_ORDER_NUMBER%TYPE DEFAULT NULL;
4771   v_new_cust_record_year          RLM_CUST_ITEM_CUM_KEYS.CUST_RECORD_YEAR%TYPE
4772                                   DEFAULT NULL;
4773   v_ship_from_org_name            VARCHAR2(250);
4774   v_customer_name                 VARCHAR2(360);
4775   v_ship_to_location              VARCHAR2(250);
4776   v_customer_item_number          VARCHAR2(250);
4777   v_new_cum_start_date            DATE DEFAULT NULL;
4778 
4779 /* these strings will create the cursor that contains the cum keys*/
4780 
4781   v_select                        VARCHAR2(3600);
4782   v_where_clause                  VARCHAR2(3600);
4783   v_statment                      VARCHAR2(3600);
4784   v_shipment_count                NUMBER;
4785 
4786   E_UNEXPECTED                    EXCEPTION;
4787 
4788   TYPE ref_cum_t    IS REF CURSOR;
4789   c_cum_keys        ref_cum_t;
4790   --
4791 
4792 
4793  BEGIN --{
4794  --
4795    IF (l_debug <> -1) THEN
4796       rlm_core_sv.dpush(C_SDEBUG, 'GetCums');
4797       rlm_core_sv.dlog(C_DEBUG, 'ship_from_org_id',
4798                                  x_cum_key_record.ship_from_org_id);
4799       rlm_core_sv.dlog(C_DEBUG, 'customer_item_id',
4800                                  x_cum_key_record.customer_item_id);
4801       rlm_core_sv.dlog(C_DEBUG,'Ship_to_address_id',
4802                                  x_cum_key_record.ship_to_address_id);
4803       rlm_core_sv.dlog(C_DEBUG,'intrmd_ship_to_address_id',
4804                                  x_cum_key_record.intrmd_ship_to_address_id);
4805       rlm_core_sv.dlog(C_DEBUG,'bill_to_address_id',
4806 			      x_cum_key_record.bill_to_address_id);
4807       rlm_core_sv.dlog(C_DEBUG,'x_terms_level'
4808                                   ,x_terms_level);
4809    END IF;
4810    --
4811    --return status of zero indicates that procedure did not find any
4812    --cum records, return status of 1 tells there are some records found
4813    x_return_status := 1;
4814 
4815 -- Determine whether to use Ship To or Intermediate Ship To
4816    --
4817    IF x_cum_key_record.intrmd_ship_to_address_id IS NULL THEN --{
4818         --
4819         IF x_cum_key_record.ship_to_address_id IS NULL THEN --{
4820                 --
4821                 msg_name := 'RLM_CUM_ADDRESS_REQUIRED';
4822                 rlm_message_sv.get_msg_text(
4823                         x_message_name  => msg_name,
4824                         x_text          => msg_data);
4825                 --
4826                 RAISE E_UNEXPECTED;
4827                 --
4828         ELSE
4829                 --
4830                 v_address_id := x_cum_key_record.ship_to_address_id;
4831                 --
4832         END IF; --}
4833         --
4834    ELSE
4835         --
4836         IF x_cum_key_record.ship_to_address_id IS NULL THEN
4837                 --
4838                 v_address_id := x_cum_key_record.intrmd_ship_to_address_id;
4839                 --
4840         ELSE
4841                 --
4842                 v_address_id := x_cum_key_record.ship_to_address_id;
4843                 --
4844         END IF;
4845         --
4846    END IF; --}
4847    --
4848    IF (l_debug <> -1) THEN
4849       rlm_core_sv.dlog(C_DEBUG, 'address_id', v_address_id);
4850    END IF;
4851    --
4852 -- Get the CUM Control Code from the setup terms table
4853    --
4854    v_cum_control_code := x_rlm_setup_terms_record.cum_control_code;
4855    --
4856    IF (l_debug <> -1) THEN
4857       rlm_core_sv.dlog(C_DEBUG, 'v_cum_control_code',v_cum_control_code);
4858    END IF;
4859    --
4860    IF x_rlm_setup_terms_record.cum_control_code IS NULL THEN --{
4861       --
4862       msg_name := 'RLM_CUM_CTRLCD_REQUIRED';
4863       --
4864       rlm_message_sv.get_msg_text(
4865                       x_message_name  => msg_name,
4866                       x_text          => msg_data);
4867       --
4868       RAISE E_UNEXPECTED;
4869       --}
4870 /*
4871    ELSE
4872       --{
4873 
4874       IF (x_terms_level = 'CUSTOMER_ITEM')
4875         AND (nvl(x_rlm_setup_terms_record.calc_cum_flag,'N') <> 'Y') THEN --{
4876  we do not need this check anymore.  We can get all setup information for
4877  cum at the address level.  calc_cum_flag is the only field that could be
4878  different at the customer item level.  However, we check later on that
4879  all items should have a new cum key and also shipment/adjustments during
4880  the transactions
4881              --
4882 	     v_ship_from_org_name := RLM_CORE_SV.get_ship_from(x_cum_key_record.ship_from_org_id);
4883 	     v_customer_name := RLM_CORE_SV.get_customer_name(x_cum_key_record.customer_id);
4884 	     v_ship_to_location := RLM_CORE_SV.get_ship_to(v_address_id);
4885 	     v_customer_item_number := RLM_CORE_SV.get_item_number(x_cum_key_record.customer_item_id);
4886 	     --
4887              msg_name := 'RLM_CUM_NO_CALC_FLAG';
4888              --
4889              rlm_message_sv.get_msg_text(
4890                    x_message_name  => msg_name,
4891                    x_text          => msg_data,
4892                    x_token1        => 'CI',
4893                    x_value1        => v_customer_item_number);
4894              --
4895              RAISE E_UNEXPECTED;
4896                         --
4897       END IF; --}
4898 */
4899       --
4900    END IF; --}
4901 
4902    --
4903 -- CUM Organization Level
4904    --
4905    v_cum_org_level_code := x_rlm_setup_terms_record.cum_org_level_code;
4906    --
4907 /* Customer Item Id is always populated in the CUM Key table regardless of
4908    CUM control code or CUM Organization Level. Ship From Organization Id
4909    is always populated in the CUM Key table except for the case of
4910    ALL_SHIP_FROMS. Therefore, the switches below are turned ON and
4911    assigned to the associated parameters passed by the calling program */
4912    --
4913    p_customer_item_id := x_cum_key_record.customer_item_id;
4914    p_ship_from_org_id := x_cum_key_record.ship_from_org_id;
4915    --
4916  --Turn ON switches based on cum control code
4917    --
4918    IF (l_debug <> -1) THEN
4919       rlm_core_sv.dlog(C_DEBUG, 'cum_control_code',
4920        x_rlm_setup_terms_record.cum_control_code);
4921    END IF;
4922    --
4923    IF v_cum_org_level_code = 'BILL_TO_SHIP_FROM' THEN
4924       --
4925       IF x_cum_key_record.bill_to_address_id IS NULL THEN
4926              --
4927              msg_name := 'RLM_CUM_BILL_TO_REQUIRED';
4928              --
4929              rlm_message_sv.get_msg_text(
4930                               x_message_name  => msg_name,
4931                               x_text          => msg_data);
4932              --
4933              RAISE E_UNEXPECTED;
4934              --
4935       END IF;
4936       --
4937       p_bill_to_address_id := x_cum_key_record.bill_to_address_id;
4938       --
4939    ELSIF v_cum_org_level_code = 'SHIP_TO_SHIP_FROM' THEN
4940       --
4941       IF x_cum_key_record.ship_to_address_id IS NULL THEN
4942              --
4943              msg_name := 'RLM_CUM_SHIP_TO_REQUIRED';
4944              --
4945              rlm_message_sv.get_msg_text(
4946                               x_message_name  => msg_name,
4947                               x_text          => msg_data);
4948              --
4949              RAISE E_UNEXPECTED;
4950              --
4951       END IF;
4952       --
4953       p_ship_to_address_id := x_cum_key_record.ship_to_address_id;
4954       --
4955    ELSIF v_cum_org_level_code = 'INTRMD_SHIP_TO_SHIP_FROM' THEN
4956       --
4957       IF x_cum_key_record.intrmd_ship_to_address_id IS NULL THEN
4958              --
4959              msg_name := 'RLM_CUM_INTER_SHIP_TO_REQUIRED' ;
4960              --
4961              rlm_message_sv.get_msg_text(
4962                               x_message_name  => msg_name,
4963                               x_text          => msg_data);
4964              --
4965              RAISE E_UNEXPECTED;
4966              --
4967       END IF;
4968       --
4969       p_intrmd_ship_to_address_id := x_cum_key_record.intrmd_ship_to_address_id;
4970       --
4971    ELSIF v_cum_org_level_code = 'SHIP_TO_ALL_SHIP_FROMS' THEN
4972       --
4973       p_ship_to_address_id := x_cum_key_record.ship_to_address_id;
4974       p_ship_from_org_id := NULL;
4975                 --
4976    ELSE
4977              --
4978              msg_name := 'RLM_CUM_UNKNOWN_ORG_LEVEL';
4979              --
4980              rlm_message_sv.get_msg_text(
4981                               x_message_name  => msg_name,
4982                               x_text          => msg_data);
4983              --
4984              RAISE E_UNEXPECTED;
4985              --
4986    END IF;
4987    --
4988     --
4989     --Build the cursor dynamicly
4990     --
4991      v_select := 'SELECT cum_key_id, cum_qty, cum_qty_to_be_accumulated, cum_qty_after_cutoff,
4992                   last_cum_qty_update_date,cust_uom_code,cum_start_date,
4993                   cust_record_year,purchase_order_number ,customer_item_id
4994                   FROM RLM_CUST_ITEM_CUM_KEYS
4995 ';
4996 
4997      IF x_rlm_setup_terms_record.cum_control_code = 'CUM_BY_DATE_ONLY'
4998         OR x_rlm_setup_terms_record.cum_control_code = 'CUM_UNTIL_MANUAL_RESET'
4999      THEN
5000      --
5001         IF v_cum_org_level_code <> 'SHIP_TO_ALL_SHIP_FROMS' THEN
5002              --
5003     /* ---------------------------------------------
5004       This cursor is used when the proc is called by ResetCum and the cum
5005       control code is cum by date only and the cum organization level is not
5006       for all ship froms ------------------------------------ */
5007 
5008             v_where_clause := '
5009             WHERE NVL(ship_from_org_id,0)= NVL('''||p_ship_from_org_id||''',0)
5010             AND NVL(ship_to_address_id,0)= NVL('''||p_ship_to_address_id||''',0)
5011             AND     NVL(intrmd_ship_to_id,0)
5012                            = NVL('''||p_intrmd_ship_to_address_id||''',0)
5013             AND NVL(bill_to_address_id,0)= NVL('''||p_bill_to_address_id||''',0)
5014             AND     ((customer_item_id = '''||p_customer_item_id||''') OR
5015               ('''||p_customer_item_id||''' IS NULL))
5016             AND     cum_start_date               IS NOT NULL
5017             AND     cum_start_date               < SYSDATE
5018             AND     purchase_order_number        IS NULL
5019             AND     cust_record_year             IS NULL
5020             AND     NVL(inactive_flag,''N'')     =  ''N''
5021             ORDER BY customer_item_id,creation_date DESC,cum_start_date DESC';
5022         ELSE
5023              --
5024      /*-------------------------------------------------------------------
5025       This cursor is used when the proc is called by ResetCum and the cum
5026       control code is cum by date only and the cum organization level is for
5027       all ship froms ----------------------------------------------------*/
5028 
5029             v_where_clause:='
5030             WHERE   ship_from_org_id                IS NULL
5031             AND NVL(ship_to_address_id,0)= NVL('''||p_ship_to_address_id||''',0)
5032             AND     NVL(intrmd_ship_to_id,0)
5033                         = NVL('''||p_intrmd_ship_to_address_id||''',0)
5034             AND NVL(bill_to_address_id,0)= NVL('''||p_bill_to_address_id||''',0)
5035             AND     ((customer_item_id = '''||p_customer_item_id||''') OR
5036               ('''||p_customer_item_id||''' IS NULL))
5037             AND     cum_start_date               IS NOT NULL
5038             AND     cum_start_date               < SYSDATE
5039             AND     purchase_order_number        IS NULL
5040             AND     cust_record_year             IS NULL
5041             AND     NVL(inactive_flag,''N'')     =  ''N''
5042             ORDER BY customer_item_id,creation_date DESC,cum_start_date DESC';
5043              --
5044         END IF;
5045      --
5046     ELSIF x_rlm_setup_terms_record.cum_control_code = 'CUM_BY_DATE_RECORD_YEAR'
5047     THEN
5048         --
5049         IF v_cum_org_level_code <> 'SHIP_TO_ALL_SHIP_FROMS' THEN
5050              --
5051      /* --------------------------------------------------------------------
5052         This cursor is used when the proc is called by ResetCum and the cum
5053         control code is cum by date/record year and the cum organization level
5054         is not for all ship froms -----------------------------------------*/
5055 
5056             v_where_clause:='
5057             WHERE NVL(ship_from_org_id,0)  = NVL('''||p_ship_from_org_id||''',0)
5058             AND NVL(ship_to_address_id,0)= NVL('''||p_ship_to_address_id||''',0)
5059             AND     NVL(intrmd_ship_to_id,0)
5060                      = NVL('''||p_intrmd_ship_to_address_id||''',0)
5061             AND NVL(bill_to_address_id,0)= NVL('''||p_bill_to_address_id||''',0)
5062             AND     ((customer_item_id = '''||p_customer_item_id||''') OR
5063               ('''||p_customer_item_id||''' IS NULL))
5064             AND     cum_start_date               IS NOT NULL
5065             AND     cum_start_date               < SYSDATE
5066             AND     purchase_order_number        IS NULL
5067             AND     cust_record_year             IS NOT NULL
5068             AND     NVL(inactive_flag,''N'')     =  ''N''
5069             ORDER BY customer_item_id,creation_date DESC,cum_start_date DESC,cust_record_year DESC';
5070              --
5071         ELSE
5072              --
5073      /* ---------------------------------------------------------------------
5074         This cursor is used when the proc is called by ResetCum and the cum
5075         control code is cum by date/record year and the cum organization level
5076         is for all ship froms -----------------------------------------------*/
5077 
5078             v_where_clause :='
5079             WHERE   ship_from_org_id                IS NULL
5080             AND NVL(ship_to_address_id,0)= NVL('''||p_ship_to_address_id||''',0)
5081             AND     NVL(intrmd_ship_to_id,0)
5082                          = NVL('''||p_intrmd_ship_to_address_id||''',0)
5083             AND NVL(bill_to_address_id,0)= NVL('''||p_bill_to_address_id||''',0)
5084             AND     ((customer_item_id = '''||p_customer_item_id||''') OR
5085               ('''||p_customer_item_id||''' IS NULL))
5086             AND     cum_start_date               IS NOT NULL
5087             AND     cum_start_date               < SYSDATE
5088             AND     purchase_order_number        IS NULL
5089             AND     cust_record_year             IS NOT NULL
5090             AND     NVL(inactive_flag,''N'')     =  ''N''
5091             ORDER BY customer_item_id,creation_date DESC,cum_start_date DESC,cust_record_year DESC';
5092              --
5093         END IF;
5094         --
5095     ELSIF x_rlm_setup_terms_record.cum_control_code = 'CUM_BY_DATE_PO' THEN
5096         --
5097         IF v_cum_org_level_code <> 'SHIP_TO_ALL_SHIP_FROMS' THEN
5098              --
5099      /* ---------------------------------------------------------------------
5100        This cursor is used when the proc is called by ResetCum and the cum
5101        control code is cum by date/po and the cum organization level is not for
5102        all ship froms -------------------------------------------------------*/
5103 
5104             v_where_clause :='
5105             WHERE NVL(ship_from_org_id,0)  = NVL('''||p_ship_from_org_id||''',0)
5106             AND NVL(ship_to_address_id,0)= NVL('''||p_ship_to_address_id||''',0)
5107             AND     NVL(intrmd_ship_to_id,0)
5108                            = NVL('''||p_intrmd_ship_to_address_id||''',0)
5109             AND NVL(bill_to_address_id,0)= NVL('''||p_bill_to_address_id||''',0)
5110             AND     ((customer_item_id = '''||p_customer_item_id||''') OR
5111               ('''||p_customer_item_id||''' IS NULL))
5112             AND     cum_start_date               IS NOT NULL
5113             AND     cum_start_date               < SYSDATE
5114             AND     purchase_order_number        IS NOT NULL
5115             AND     cust_record_year             IS NULL
5116             AND     NVL(inactive_flag,''N'')     =  ''N''
5117             ORDER BY customer_item_id,creation_date DESC, cum_start_date DESC, purchase_order_number DESC ';
5118              --
5119         ELSE
5120              --
5121      /*------------------------------------------------------------------------
5122         This cursor is used when the proc is called by ResetCum and the cum
5123         control code is cum by date/po and the cum organization level is for
5124         all ship froms ------------------------------------------------------*/
5125 
5126             v_where_clause := '
5127             WHERE   ship_from_org_id                IS NULL
5128             AND NVL(ship_to_address_id,0)= NVL('''||p_ship_to_address_id||''',0)
5129             AND     NVL(intrmd_ship_to_id,0)
5130                         = NVL('''||p_intrmd_ship_to_address_id||''',0)
5131             AND NVL(bill_to_address_id,0)= NVL('''||p_bill_to_address_id||''',0)
5132             AND     ((customer_item_id = '''||p_customer_item_id||''') OR
5133               ('''||p_customer_item_id||''' IS NULL))
5134             AND     cum_start_date               IS NOT NULL
5135             AND     cum_start_date               < SYSDATE
5136             AND     purchase_order_number        IS NOT NULL
5137             AND     cust_record_year             IS NULL
5138             AND     NVL(inactive_flag,''N'')     =  ''N''
5139             ORDER BY customer_item_id,creation_date DESC,cum_start_date DESC, purchase_order_number DESC';
5140 
5141         END IF;
5142         --
5143      END IF;
5144      --
5145      v_statment := v_select ||  v_where_clause;
5146      --
5147      IF (l_debug <> -1) THEN
5148         rlm_core_sv.dlog(C_DEBUG,v_statment);
5149      END IF;
5150      --
5151      OPEN c_cum_keys FOR v_statment;
5152      LOOP --{
5153        FETCH c_cum_keys
5154        INTO          v_tmp_cum_record.cum_key_id,
5155                      v_tmp_cum_record.cum_qty,
5156                      v_tmp_cum_record.cum_qty_to_be_accumulated,
5157                      v_tmp_cum_record.cum_qty_after_cutoff,
5158                      v_tmp_cum_record.last_cum_qty_update_date,
5159                      v_tmp_cum_record.cust_uom_code,
5160                      v_tmp_cum_record.cum_start_date,
5161                      v_new_cust_record_year,
5162                      v_new_purchase_order_number,
5163                      v_new_customer_item_id;
5164        --
5165        EXIT WHEN c_cum_keys%NOTFOUND;
5166        --
5167        BEGIN --{
5168          v_exist := 1;
5169 
5170          v_tmp_cum_record.customer_item_id := v_new_customer_item_id;
5171          --
5172          v_new_cum_start_date := v_tmp_cum_record.cum_start_date;
5173          --
5174          IF (v_new_customer_item_id <> v_old_customer_item_id) THEN --{
5175          --
5176             v_old_customer_item_id := v_new_customer_item_id;
5177             v_cum_rec_ctr := v_cum_rec_ctr +1;
5178             --
5179             IF(v_cum_rec_ctr - v_counter_ctr) <> 1 THEN --{
5180               --
5181               IF x_cum_records.EXISTS(v_cum_rec_ctr-1) THEN
5182                    fnd_file.put_line(fnd_file.log,'Could not find '
5183                     ||'corresponding CumKey for cum_key_id: ' ||
5184                     x_cum_records(v_cum_rec_ctr-1).cum_key_id);
5185                    --
5186   		   IF (l_debug <> -1) THEN
5187                       rlm_core_sv.dlog(C_DEBUG,'Could not find correspounding'
5188                     ||  ' CumKey for cum_key_id: ',
5189                      x_cum_records(v_cum_rec_ctr-1).cum_key_id);
5190                    END IF;
5191 		   --
5192               ELSE
5193 		   --
5194   		   IF (l_debug <> -1) THEN
5195                       rlm_core_sv.dlog(C_DEBUG,'Could not find correspounding'
5196                     || ' CumKey for cum_key_id: ',v_tmp_cum_record.cum_key_id);
5197                    END IF;
5198 		   --
5199               END IF;
5200               --
5201               v_cum_rec_ctr := v_cum_rec_ctr -1;
5202             --
5203             END IF; --}
5204             --
5205             x_cum_records(v_cum_rec_ctr).cum_key_id :=
5206                   v_tmp_cum_record.cum_key_id;
5207 	    --
5208 	    IF (l_debug <> -1) THEN
5209                rlm_core_sv.dlog(C_SDEBUG,'x_cum_records(v_cum_rec_ctr).cum_key_id',
5210 				x_cum_records(v_cum_rec_ctr).cum_key_id);
5211             END IF;
5212             --
5213             x_counter(v_cum_rec_ctr) := 0;
5214             --
5215             x_cum_records(v_cum_rec_ctr).cum_qty := v_tmp_cum_record.cum_qty;
5216             --
5217             x_cum_records(v_cum_rec_ctr).cum_qty_to_be_accumulated :=
5218                   v_tmp_cum_record.cum_qty_to_be_accumulated;
5219             x_cum_records(v_cum_rec_ctr).cum_qty_after_cutoff :=
5220                   v_tmp_cum_record.cum_qty_after_cutoff;
5221             x_cum_records(v_cum_rec_ctr).last_cum_qty_update_date :=
5222                   v_tmp_cum_record.last_cum_qty_update_date;
5223             x_cum_records(v_cum_rec_ctr).cust_uom_code :=
5224                   v_tmp_cum_record.cust_uom_code;
5225             x_cum_records(v_cum_rec_ctr).cum_start_date :=
5226                    v_tmp_cum_record.cum_start_date;
5227             x_cum_records(v_cum_rec_ctr).customer_item_id :=
5228                    v_tmp_cum_record.customer_item_id;
5229 
5230          --}
5231 /*
5232     else if the customer_item_id has not changed see if there are
5233     old cum with shipment or manual adjustment for it
5234  */
5235          ELSE --{
5236          --
5237            SELECT COUNT(*)
5238            INTO v_shipment_count
5239            FROM oe_order_lines
5240            WHERE (ship_from_org_id               = x_ship_from_org_id
5241                      OR x_ship_from_org_id           IS NULL)
5242              AND     (ship_to_org_id                 = x_ship_to_org_id
5243                      OR x_ship_to_org_id             IS NULL)
5244              AND     (intmed_ship_to_org_id          = x_intmed_ship_to_org_id
5245                      OR x_intmed_ship_to_org_id      IS NULL)
5246              AND     (invoice_to_org_id              = x_bill_to_org_id
5247                      OR x_bill_to_org_id             IS NULL)
5248              AND     ordered_item_id       =       v_new_customer_item_id
5249              AND     actual_shipment_date           >= x_transaction_start_date
5250              AND     actual_shipment_date           <= NVL(x_transaction_end_date, SYSDATE)
5251              AND     open_flag = 'N'
5252              AND     shipped_quantity IS NOT NULL
5253              AND     source_document_type_id         = 5
5254              AND     veh_cus_item_cum_key_id     = v_tmp_cum_record.cum_key_id;
5255 
5256            --
5257            IF nvl(v_shipment_count,0) = 0 THEN --{
5258             --see if there are adjustments
5259              SELECT COUNT(*)
5260              INTO v_exist
5261              FROM rlm_cust_item_cum_adj
5262              WHERE cum_key_id = v_tmp_cum_record.cum_key_id
5263              AND transaction_date_time <= nvl(x_transaction_end_date,sysdate)
5264              AND transaction_date_time >= x_transaction_start_date;
5265            --
5266            END IF; --}
5267            --
5268            IF (l_debug <> -1) THEN
5269             rlm_core_sv.dlog(C_DEBUG, 'v_shipment_count', v_shipment_count);
5270             rlm_core_sv.dlog(C_DEBUG, 'Adjustments', v_exist);
5271            END IF;
5272            IF nvl(v_exist,0) > 0 THEN
5273            --
5274              v_old_cum_ctr := v_old_cum_ctr + 1;
5275              --
5276              x_counter(v_cum_rec_ctr) := x_counter(v_cum_rec_ctr) + 1;
5277              --
5278              v_counter_ctr := v_cum_rec_ctr;
5279              --
5280              --x_counter(v_cum_rec_ctr) := v_old_cum_ctr;
5281              --
5282   	     IF (l_debug <> -1) THEN
5283                 rlm_core_sv.dlog(C_SDEBUG,'v_cum_rec_ctr',v_cum_rec_ctr);
5284                 rlm_core_sv.dlog(C_SDEBUG,'x_counter',x_counter(v_cum_rec_ctr));
5285                 rlm_core_sv.dlog(C_SDEBUG,'v_old_cum_ctr',v_old_cum_ctr);
5286              END IF;
5287              --
5288              x_old_cum_records(v_old_cum_ctr).cum_key_id :=
5289                   v_tmp_cum_record.cum_key_id;
5290 	     --
5291   	     IF (l_debug <> -1) THEN
5292                 rlm_core_sv.dlog(C_SDEBUG,
5293                'x_old_cum_records(v_old_cum_ctr).cum_key_id',
5294                 x_old_cum_records(v_old_cum_ctr).cum_key_id);
5295              END IF;
5296              --
5297              x_old_cum_records(v_old_cum_ctr).cum_qty :=
5298                   v_tmp_cum_record.cum_qty;
5299              x_old_cum_records(v_old_cum_ctr).cum_qty_to_be_accumulated :=
5300                   v_tmp_cum_record.cum_qty_to_be_accumulated;
5301              x_old_cum_records(v_old_cum_ctr).cum_qty_after_cutoff :=
5302                   v_tmp_cum_record.cum_qty_after_cutoff;
5303              x_old_cum_records(v_old_cum_ctr).last_cum_qty_update_date :=
5304                   v_tmp_cum_record.last_cum_qty_update_date;
5305              x_old_cum_records(v_old_cum_ctr).cust_uom_code :=
5306                   v_tmp_cum_record.cust_uom_code;
5307              x_old_cum_records(v_old_cum_ctr).cum_start_date :=
5308                    v_tmp_cum_record.cum_start_date;
5309              x_old_cum_records(v_old_cum_ctr).customer_item_id :=
5310                    v_tmp_cum_record.customer_item_id;
5311            --
5312            END IF;
5313          --
5314          END IF; --}
5315        --
5316        END; --}
5317        --
5318      END LOOP; --}
5319      --
5320      CLOSE c_cum_keys;
5321      --
5322      /* If the last new_cum did not have any old_cum then delete it from the
5323         table */
5324 
5325      IF Not (x_counter.EXISTS(v_cum_rec_ctr)) THEN
5326      --
5327        IF (l_debug <> -1) THEN
5328           rlm_core_sv.dlog(C_SDEBUG,'could not find old cums for cum key id: ',
5329            x_cum_records(v_cum_rec_ctr).cum_key_id);
5330        END IF;
5331        --
5332        x_cum_records.DELETE(v_cum_rec_ctr);
5333        --
5334        v_cum_rec_ctr := v_cum_rec_ctr -1 ;
5335        --
5336      ELSE
5337      --
5338        IF x_counter(v_cum_rec_ctr) = 0 THEN
5339        --
5340          IF x_cum_records.EXISTS(v_cum_rec_ctr) THEN
5341          --
5342             IF (l_debug <> -1) THEN
5343                rlm_core_sv.dlog(C_SDEBUG,'could not find old cums for cum key id:',
5344                 x_cum_records(v_cum_rec_ctr).cum_key_id);
5345             END IF;
5346             --
5347             x_cum_records.DELETE(v_cum_rec_ctr);
5348          --
5349          END IF;
5350          --
5351          x_counter.DELETE(v_cum_rec_ctr);
5352          --
5353          v_cum_rec_ctr := v_cum_rec_ctr -1 ;
5354        --
5355        END IF;
5356        --
5357        IF (l_debug <> -1) THEN
5358           rlm_core_sv.dlog(C_DEBUG,'v_cum_rec_ctr',v_cum_rec_ctr);
5359        END IF;
5360        --
5361      END IF;
5362      --
5363      /* if there are no cum found then return 0 */
5364      IF (v_cum_rec_ctr = 0 ) THEN
5365         x_return_status := 0;
5366 	--
5367   	IF (l_debug <> -1) THEN
5368           rlm_core_sv.dpop(C_DEBUG,
5369                 'GetCums:Cum key record could not be retrieved');
5370         END IF;
5371        --
5372      ELSE
5373        x_return_status := 1;
5374        --
5375        IF (l_debug <> -1) THEN
5376           rlm_core_sv.dpop(C_DEBUG,
5377                 'GetCums:Cum key record is retrieved successfully');
5378        END IF;
5379        --
5380      END IF;
5381    --
5382  EXCEPTION
5383       --
5384       WHEN E_UNEXPECTED THEN
5385           --
5386           x_return_status := 0;
5387           --
5388   	  IF (l_debug <> -1) THEN
5389              rlm_core_sv.dlog(C_DEBUG,'msg_data',msg_data);
5390              rlm_core_sv.dpop(C_DEBUG,'GetCums E_UNEXPECTED');
5391           END IF;
5392           --
5393       WHEN NO_DATA_FOUND THEN
5394           --
5395           x_return_status := 0;
5396           --
5397   	  IF (l_debug <> -1) THEN
5398              rlm_core_sv.dlog(C_DEBUG,'msg_data',msg_data);
5399              rlm_core_sv.dpop(C_DEBUG,'GetCums NO_DATA_FOUND');
5400           END IF;
5401           --
5402       WHEN OTHERS THEN
5403           --
5404           -- RLM_CUM_SQLERR
5405           --
5406   	  IF (l_debug <> -1) THEN
5407              rlm_core_sv.dpop(C_DEBUG,'GetCums' ||  substr(SQLERRM,1,100));
5408           END IF;
5409           --
5410           RAISE;
5411  END GetCums; --}
5412 
5413 /*============================================================================
5414 
5415   PROCEDURE NAME:       SetSupplierCum
5416 
5417     This procedure calls the CalculateSupplierCum for all the shipments
5418     for the given Cum Key.  It populates oe global tables and it calculates
5419     the cum qty
5420 
5421   PARAMETERS:
5422 
5423      ** x_index                   IN              NUMBER
5424         This is the index for table x_cum_records
5425      ** x_cum_key_record          IN              cum_key_attrib_rec_type
5426         contains information about address ids
5427      ** x_transaction_start_date  IN              DATE
5428      ** x_cum_records             IN OUT NOCOPY          RLM_CUM_SV.t_cums
5429         Using the x_index we get the new cum record which needs to be rest
5430      ** x_return_status           OUT NOCOPY          BOOLEAN
5431         This will return FALSE if unexpected error happens
5432      ** x_counter                 IN OUT NOCOPY          NUMBER
5433         Shows the number of order lines modified
5434 
5435 =============================================================================*/
5436 
5437  PROCEDURE SetSupplierCum (
5438         x_index                   IN              NUMBER,
5439         x_cum_key_record          IN              cum_key_attrib_rec_type,
5440         x_transaction_start_date  IN              DATE,
5441         x_cum_records             IN OUT NOCOPY          RLM_CUM_SV.t_cums,
5442         x_return_status           OUT NOCOPY             BOOLEAN,
5443         x_counter                 IN OUT NOCOPY          NUMBER,
5444         x_adjustment_date         IN OUT NOCOPY          DATE )
5445  IS
5446         v_new_ship_count          t_new_ship_count;
5447         tmp_shipment_date         DATE   DEFAULT TO_DATE('01/01/1500', 'DD/MM/YYYY');
5448         adj_qty                   NUMBER DEFAULT 0;
5449         v_header_id               NUMBER;
5450         v_line_id                 NUMBER;
5451         v_index                   NUMBER;
5452 
5453         e_Unexpected              EXCEPTION;
5454 
5455  BEGIN
5456 
5457     IF (l_debug <> -1) THEN
5458        rlm_core_sv.dpush(C_SDEBUG,'SetSupplierCum');
5459     END IF;
5460     --
5461     v_new_ship_count(x_cum_records(x_index).cum_key_id) := 0;
5462     --
5463     IF (l_debug <> -1) THEN
5464        rlm_core_sv.dlog(C_DEBUG,'cum_key_id', x_cum_records(x_index).cum_key_id);
5465     END IF;
5466     --
5467     x_return_status := FALSE;
5468     --
5469     IF x_cum_records(x_index).record_return_status = FALSE THEN
5470         --
5471         IF (l_debug <> -1) THEN
5472            rlm_core_sv.dlog(C_DEBUG,'CalculateSupplierCum returned false.');
5473         END IF;
5474         --
5475         RAISE e_Unexpected;
5476         --
5477     END IF;
5478     --
5479     IF g_cum_oe_lines.COUNT > 0 THEN  --{
5480       v_index := g_cum_oe_lines.FIRST;
5481       --
5482       LOOP --{
5483       --
5484         v_header_id := g_cum_oe_lines(v_index).header_id;
5485         v_line_id :=   g_cum_oe_lines(v_index).line_id;
5486         x_cum_records(x_index).shipped_quantity :=
5487                        g_cum_oe_lines(v_index).shipped_quantity;
5488         x_cum_records(x_index).actual_shipment_date :=
5489                        g_cum_oe_lines(v_index).actual_shipment_date;
5490         x_cum_records(x_index).cust_uom_code :=
5491                        g_cum_oe_lines(v_index).order_quantity_uom;
5492         --
5493         IF (l_debug <> -1) THEN
5494            rlm_core_sv.dlog(C_DEBUG,'shipment_date',
5495                         x_cum_records(x_index).actual_shipment_date);
5496         END IF;
5497 	--
5498         IF x_cum_records(x_index).actual_shipment_date
5499           = tmp_shipment_date
5500         THEN
5501             v_new_ship_count(x_cum_records(x_index).cum_key_id) :=
5502                 v_new_ship_count(x_cum_records(x_index).cum_key_id) + 1;
5503         ELSE
5504             v_new_ship_count(x_cum_records(x_index).cum_key_id) := 1;
5505             tmp_shipment_date :=
5506                          x_cum_records(x_index).actual_shipment_date;
5507         END IF;
5508         --
5509   	IF (l_debug <> -1) THEN
5510            rlm_core_sv.dlog(C_DEBUG,'shipped_quantity',
5511                                   x_cum_records(x_index).shipped_quantity);
5512         END IF;
5513         --
5514         RLM_TPA_SV.CalculateSupplierCum(
5515               x_new_ship_count => v_new_ship_count,
5516               x_cum_key_record => x_cum_key_record,
5517               x_cum_record     => x_cum_records(x_index));
5518         --
5519 
5520         IF x_cum_records(x_index).record_return_status = FALSE THEN
5521             --
5522   	    IF (l_debug <> -1) THEN
5523                rlm_core_sv.dlog(C_DEBUG,'CalculateSupplierCum returned false.');
5524             END IF;
5525             --
5526             RAISE e_Unexpected;
5527             --
5528         END IF;
5529         --
5530         IF x_adjustment_date IS NULL THEN
5531         --
5532           SELECT  SUM(transaction_qty)
5533           INTO    adj_qty
5534           FROM    rlm_cust_item_cum_adj
5535           WHERE   cum_key_id = x_cum_records(x_index).cum_key_id
5536           AND     transaction_date_time <
5537                      x_cum_records(x_index).actual_shipment_date;
5538         --
5539         ELSE
5540         --
5541           SELECT  SUM(transaction_qty)
5542           INTO    adj_qty
5543           FROM    rlm_cust_item_cum_adj
5544           WHERE   cum_key_id = x_cum_records(x_index).cum_key_id
5545           AND     transaction_date_time >= x_adjustment_date
5546           AND     transaction_date_time <
5547                      x_cum_records(x_index).actual_shipment_date;
5548 
5549         --
5550         END IF;
5551         --
5552         IF (l_debug <> -1) THEN
5553            rlm_core_sv.dlog(C_DEBUG,'x_adjustment_date',x_adjustment_date);
5554         END IF;
5555         --
5556   	IF (l_debug <> -1) THEN
5557            rlm_core_sv.dlog(C_DEBUG,'actual_shipment_date',
5558                      x_cum_records(x_index).actual_shipment_date);
5559         END IF;
5560 	--
5561         IF adj_qty IS NULL THEN
5562         --
5563           adj_qty := 0;
5564           --
5565   	  IF (l_debug <> -1) THEN
5566              rlm_core_sv.dlog(C_DEBUG, 'No adjustment found.');
5567           END IF;
5568         --
5569         END IF;
5570         --
5571   	IF (l_debug <> -1) THEN
5572            rlm_core_sv.dlog(C_DEBUG,'adj_qty',adj_qty);
5573         END IF;
5574         --
5575         x_adjustment_date := x_cum_records(x_index).actual_shipment_date;
5576         --
5577         x_cum_records(x_index).cum_qty :=
5578                            x_cum_records(x_index).cum_qty + adj_qty;
5579 	--
5580   	IF (l_debug <> -1) THEN
5581            rlm_core_sv.dlog(C_DEBUG,'cum qty',
5582                                     x_cum_records(x_index).cum_qty);
5583         END IF;
5584 	--
5585         --g_oe_line_tbl := oe_order_pub.g_miss_line_tbl;
5586         --
5587         g_oe_tmp_line_tbl(x_counter) := OE_Order_PUB.G_MISS_LINE_REC;
5588         --
5589         g_oe_tmp_line_tbl(x_counter).header_id      := v_header_id;
5590         --
5591         g_oe_tmp_line_tbl(x_counter).line_id        := v_line_id;
5592         --
5593         g_oe_tmp_line_tbl(x_counter).operation      := oe_globals.G_OPR_UPDATE;
5594         --
5595         g_oe_tmp_line_tbl(x_counter).veh_cus_item_cum_key_id  :=
5596                                            x_cum_records(x_index).cum_key_id;
5597         g_oe_tmp_line_tbl(x_counter).industry_attribute7      :=
5598                                                 x_cum_records(x_index).cum_qty;
5599         g_oe_tmp_line_tbl(x_counter).industry_attribute8      :=
5600                                           x_cum_records(x_index).cust_uom_code;
5601         --
5602   	IF (l_debug <> -1) THEN
5603            rlm_core_sv.dlog(C_DEBUG, 'x_counter', x_counter);
5604            rlm_core_sv.dlog(C_DEBUG, 'g_oe_tmp_line_tbl(x_counter).header_id',
5605                                         g_oe_tmp_line_tbl(x_counter).header_id);
5606            rlm_core_sv.dlog(C_DEBUG, 'g_oe_tmp_line_tbl(x_counter).line_id',
5607                                           g_oe_tmp_line_tbl(x_counter).line_id);
5608            rlm_core_sv.dlog(C_DEBUG,
5609                          'g_oe_tmp_line_tbl(x_counter).veh_cus_item_cum_key_id',
5610                           g_oe_tmp_line_tbl(x_counter).veh_cus_item_cum_key_id);
5611            rlm_core_sv.dlog(C_DEBUG,'g_oe_tmp_line_tbl(counter).industry_attribute7',
5612                              g_oe_tmp_line_tbl(x_counter).industry_attribute7);
5613            rlm_core_sv.dlog(C_DEBUG,'g_oe_tmp_line_tbl(counter).industry_attribute8',
5614                               g_oe_tmp_line_tbl(x_counter).industry_attribute8);
5615         END IF;
5616         --
5617         x_counter := x_counter + 1;
5618         --
5619         EXIT WHEN v_index = g_cum_oe_lines.LAST;
5620         --
5621         v_index := g_cum_oe_lines.NEXT(v_index);
5622         --
5623       END LOOP; --}
5624     --
5625     END IF; --}
5626     --
5627     x_return_status := TRUE;
5628     --
5629     IF (l_debug <> -1) THEN
5630        rlm_core_sv.dpop(C_DEBUG,'SetSupplierCum');
5631     END IF;
5632     --
5633  EXCEPTION
5634  --
5635     WHEN e_Unexpected THEN
5636       --
5637       IF (l_debug <> -1) THEN
5638         rlm_core_sv.dpop(C_DEBUG,'SetSupplierCum: e_unexpected');
5639       END IF;
5640       --
5641     WHEN OTHERS THEN
5642       --
5643       IF (l_debug <> -1) THEN
5644         rlm_core_sv.dpop(C_DEBUG,'SetSupplierCum' ||  substr(SQLERRM,1,200));
5645       END IF;
5646       --
5647       RAISE;
5648 
5649  END SetSupplierCum;
5650 
5651 
5652 /*===========================================================================
5653 
5654 PROCEDURE NAME:    QuickSort
5655 
5656 ===========================================================================*/
5657 
5658 PROCEDURE QuickSort(first    IN NUMBER,
5659                     last     IN NUMBER,
5660                     sortType IN NUMBER)
5661 IS
5662 
5663   Low           NUMBER;
5664   High          NUMBER;
5665   Pivot_d       DATE;
5666   Pivot_n       NUMBER;
5667   v_Progress    VARCHAR2(3) := '010';
5668 
5669 BEGIN
5670   --
5671   IF (l_debug <> -1) THEN
5672      rlm_core_sv.dpush(C_SDEBUG,'QuickSort');
5673   END IF;
5674   --
5675   low := First;
5676   high := Last;
5677 
5678   /* Select an element from the middle. */
5679   IF sortType = RLM_CUM_SV.C_line_table_type THEN
5680     pivot_n :=  g_oe_tmp_line_tbl(TRUNC((First + Last) / 2)).header_id;
5681     LOOP
5682       /* Find lowest element that is >= Pivot */
5683       WHILE g_oe_tmp_line_tbl(Low).header_id < Pivot_n LOOP
5684         Low := Low + 1;
5685       END LOOP;
5686       /* Find highest element that is <= Pivot */
5687       WHILE g_oe_tmp_line_tbl(High).header_id > Pivot_n LOOP
5688         High := High - 1;
5689       END LOOP;
5690       /*  swap the elements */
5691       IF Low <= High THEN
5692         Swap(High, Low,sortType);
5693         Low := Low + 1;
5694         High := High - 1;
5695       End IF;
5696       EXIT WHEN Low > High;
5697     END LOOP ;
5698   ELSE
5699     pivot_d :=  g_cum_oe_lines(TRUNC((First + Last) / 2)).actual_shipment_date;
5700     LOOP
5701       /* Find lowest element that is >= Pivot */
5702       WHILE g_cum_oe_lines(Low).actual_shipment_date < Pivot_d LOOP
5703         Low := Low + 1;
5704       END LOOP;
5705       /* Find highest element that is <= Pivot */
5706       WHILE g_cum_oe_lines(High).actual_shipment_date > Pivot_d LOOP
5707         High := High - 1;
5708       END LOOP;
5709       /*  swap the elements */
5710       IF Low <= High THEN
5711         Swap(High, Low,sortType);
5712         Low := Low + 1;
5713         High := High - 1;
5714       End IF;
5715       EXIT WHEN Low > High;
5716     END LOOP ;
5717   END IF;
5718   --
5719   IF (First < High) THEN
5720       Quicksort(First, High,sortType);
5721   END IF;
5722   IF (Low < Last) THEN
5723      Quicksort(Low, Last,sortType);
5724   END IF;
5725   --
5726   IF (l_debug <> -1) THEN
5727      rlm_core_sv.dpop(C_DEBUG);
5728   END IF;
5729   --
5730 EXCEPTION
5731 
5732   WHEN OTHERS THEN
5733     rlm_message_sv.sql_error('rlm_cum_sv.QuickSort', v_Progress);
5734     --
5735     IF (l_debug <> -1) THEN
5736        rlm_core_sv.dlog(C_DEBUG,'progress',v_Progress);
5737        rlm_core_sv.dpop(C_DEBUG,'EXCEPTION: '||SUBSTR(SQLERRM,1,200));
5738     END IF;
5739     --
5740     raise;
5741     --
5742 END QuickSort;
5743 
5744 /*===========================================================================
5745 
5746 PROCEDURE NAME:    Swap
5747 
5748 ===========================================================================*/
5749 PROCEDURE Swap( i        IN NUMBER,
5750                 j        IN NUMBER,
5751                 sortType IN NUMBER)
5752 IS
5753 
5754   T             oe_order_pub.Line_Rec_Type ;
5755   T2            rlm_cum_sv.cum_oe_lines_type;
5756   v_Progress    VARCHAR2(3) := '010';
5757 
5758 BEGIN
5759 
5760 --  rlm_core_sv.dpush(C_SDEBUG,'Swap');
5761 
5762   IF sortType = rlm_cum_sv.C_line_table_type THEN
5763     T := g_oe_tmp_line_tbl(i);
5764     g_oe_tmp_line_tbl(i) := g_oe_tmp_line_tbl(j);
5765     g_oe_tmp_line_tbl(j) := T;
5766   ELSE
5767     T2 := g_cum_oe_lines(i);
5768     g_cum_oe_lines(i) := g_cum_oe_lines(j);
5769     g_cum_oe_lines(j) := T2;
5770   END IF;
5771 
5772 --  rlm_core_sv.dpop(C_SDEBUG);
5773 
5774 EXCEPTION
5775 
5776   WHEN OTHERS THEN
5777     --rlm_message_sv.sql_error('rlm_cum_sv.Swap', v_Progress);
5778     --rlm_core_sv.dlog(C_DEBUG,'progress',v_Progress);
5779     --rlm_core_sv.dpop(C_SDEBUG,'EXCEPTION: '||SUBSTR(SQLERRM,1,200));
5780     raise;
5781 
5782 END Swap;
5783 
5784  PROCEDURE GetShippLines (
5785         x_cum_key_id              IN              NUMBER,
5786         x_ship_from_org_id        IN              NUMBER,
5787         x_ship_to_org_id          IN              NUMBER,
5788         x_intmed_ship_to_org_id   IN              NUMBER,
5789         x_bill_to_org_id          IN              NUMBER,
5790         x_customer_item_id        IN              NUMBER,
5791         x_inventory_item_id       IN              NUMBER,
5792         x_transaction_start_date  IN              DATE,
5793         x_transaction_end_date    IN              DATE,
5794         x_index                   IN  OUT NOCOPY         NUMBER )
5795  IS
5796         CURSOR c_oe_lines IS
5797              SELECT  header_id,
5798                      line_id,
5799                      shipped_quantity,
5800                      actual_shipment_date,
5801                      order_quantity_uom,
5802                      org_id
5803              FROM    oe_order_lines_all
5804              WHERE   (ship_from_org_id               = x_ship_from_org_id
5805                      OR x_ship_from_org_id           IS NULL)
5806              AND     (ship_to_org_id                 = x_ship_to_org_id
5807                      OR x_ship_to_org_id             IS NULL)
5808              AND     (intmed_ship_to_org_id          = x_intmed_ship_to_org_id
5809                      OR x_intmed_ship_to_org_id      IS NULL)
5810              AND     (invoice_to_org_id              = x_bill_to_org_id
5811                      OR x_bill_to_org_id             IS NULL)
5812              AND     (ordered_item_id                = x_customer_item_id
5813                      OR x_customer_item_id           IS NULL)
5814              AND     (actual_shipment_date           >= x_transaction_start_date
5815                      OR x_transaction_start_date     IS NULL)
5816              AND     (actual_shipment_date           <= NVL(x_transaction_end_date, SYSDATE))
5817              AND     shipped_quantity                IS NOT NULL
5818              AND     open_flag                       = 'N'
5819              AND     source_document_type_id         = 5
5820              AND     veh_cus_item_cum_key_id         = x_cum_key_id
5821              AND     inventory_item_id               = x_inventory_item_id
5822              ORDER BY actual_shipment_date;
5823 
5824 
5825  BEGIN
5826     --
5827     IF (l_debug <> -1) THEN
5828        rlm_core_sv.dpush(C_SDEBUG,'GetShippLines');
5829        rlm_core_sv.dlog(C_DEBUG,'cum_key_id',x_cum_key_id);
5830        rlm_core_sv.dlog(C_DEBUG,'x_ship_from_org_id',x_ship_from_org_id);
5831        rlm_core_sv.dlog(C_DEBUG,'x_ship_to_org_id',x_ship_to_org_id);
5832        rlm_core_sv.dlog(C_DEBUG,'x_intmed_ship_to_org_id',x_intmed_ship_to_org_id);
5833        rlm_core_sv.dlog(C_DEBUG,'x_bill_to_org_id',x_bill_to_org_id);
5834        rlm_core_sv.dlog(C_DEBUG,'x_customer_item_id',x_customer_item_id);
5835        rlm_core_sv.dlog(C_DEBUG,'x_inventory_item_id',x_inventory_item_id);
5836        rlm_core_sv.dlog(C_DEBUG,'x_transaction_start_date',x_transaction_start_date);
5837        rlm_core_sv.dlog(C_DEBUG,'x_transaction_end_date',x_transaction_end_date);
5838        rlm_core_sv.dlog(C_DEBUG,'x_index',x_index);
5839     END IF;
5840     --
5841     OPEN c_oe_lines ;
5842     --
5843     LOOP --{
5844     --
5845       FETCH c_oe_lines INTO
5846            g_cum_oe_lines(x_index).header_id,
5847            g_cum_oe_lines(x_index).line_id,
5848            g_cum_oe_lines(x_index).shipped_quantity,
5849            g_cum_oe_lines(x_index).actual_shipment_date,
5850            g_cum_oe_lines(x_index).order_quantity_uom,
5851            g_cum_oe_lines(x_index).org_id;
5852       --
5853       EXIT WHEN c_oe_lines%NOTFOUND;
5854       --
5855       IF (l_debug <> -1) THEN
5856          rlm_core_sv.dlog(C_DEBUG,'shipped_quantity',
5857                                 g_cum_oe_lines(x_index).shipped_quantity);
5858       END IF;
5859       --
5860       x_index := x_index + 1;
5861       --
5862     END LOOP; --}
5863     --
5864     CLOSE c_oe_Lines;
5865     --
5866     IF (l_debug <> -1) THEN
5867        rlm_core_sv.dpop(C_DEBUG,'GetShippLines');
5868     END IF;
5869     --
5870  EXCEPTION
5871     --
5872     WHEN OTHERS THEN
5873         --
5874         IF c_oe_lines%ISOPEN THEN
5875         --
5876            CLOSE c_oe_lines;
5877         --
5878         END IF;
5879         --
5880   	IF (l_debug <> -1) THEN
5881            rlm_core_sv.dpop(C_DEBUG,'GetShippLines' ||  substr(SQLERRM,1,200));
5882         END IF;
5883         --
5884         RAISE;
5885 
5886  END GetShippLines;
5887 
5888 /*=============================================================================
5889   PROCEDURE NAME: UpdateOldKey
5890 
5891   DESCRIPTION:    This procedure will be called to update CUM Key ID record(s) t
5892 hat
5893                   are previously attached to order lines
5894 
5895   PARAMETERS:     x_old_cum_table               IN t_old_cum
5896 
5897  ============================================================================*/
5898  --{
5899  PROCEDURE UpdateOldKey(x_old_cum_records        IN OUT NOCOPY RLM_CUM_SV.t_cums,
5900                          x_shipment_rule_code     IN VARCHAR2,
5901                          x_cutoff_time            IN DATE,
5902                          x_cum_key_record         IN OUT NOCOPY cum_key_attrib_rec_type,
5903                          x_ship_from_org_id       IN NUMBER,
5904                          x_ship_to_org_id         IN NUMBER,
5905                          x_intmed_ship_to_org_id  IN NUMBER,
5906                          x_bill_to_org_id         IN NUMBER,
5907                          x_customer_item_id       IN NUMBER,
5908                          x_return_status          OUT NOCOPY BOOLEAN)
5909  IS
5910         v_index                 NUMBER;
5911         v_adjustment_date       DATE;
5912         counter                 NUMBER DEFAULT 1;
5913         cum_records_counter     NUMBER;
5914         adj_qty                 NUMBER DEFAULT 0;
5915         v_return_status         BOOLEAN;
5916         v_line_idx              NUMBER;
5917         v_tmp_line_idx          NUMBER;
5918         v_header_id             NUMBER;
5919         l_file_val              VARCHAR2(80);
5920         v_om_dbg_dir            VARCHAR2(80);
5921         x_oe_api_version        NUMBER                  DEFAULT 1;
5922         l_return_status         VARCHAR2(1);
5923         v_msg_count             NUMBER;
5924         v_msg_data              VARCHAR2(2000);
5925         e_noshipment            EXCEPTION;
5926         e_SetSupplierCum        EXCEPTION;
5927         --
5928 	l_oe_line_tbl_out	oe_order_pub.line_tbl_type;
5929 	--
5930  BEGIN
5931   --
5932   IF (l_debug <> -1) THEN
5933      rlm_core_sv.dpush(C_DEBUG, 'UpdateOldKey');
5934   END IF;
5935   --
5936   g_oe_tmp_line_tbl.DELETE;
5937   g_oe_tmp_line_tbl := oe_order_pub.g_miss_line_tbl;
5938   g_oe_line_tbl.DELETE;
5939   g_oe_line_tbl := oe_order_pub.g_miss_line_tbl;
5940   --
5941   x_return_status := TRUE;
5942   --
5943   FOR cum_records_counter in 1..x_old_cum_records.COUNT LOOP
5944     --return if the table is empty
5945     IF x_old_cum_records(cum_records_counter).cum_key_id IS NOT NULL THEN --{
5946     --
5947       g_cum_oe_lines.DELETE;
5948       v_index := 1;
5949       v_adjustment_date := NULL;
5950       v_return_status := TRUE;
5951       --
5952 
5953       --for each old cum record x_cum_key_record needs to be updated with correct inventory_item_id
5954 
5955       x_cum_key_record.inventory_item_id := x_old_cum_records(cum_records_counter).inventory_item_id;
5956       --
5957       IF (l_debug <> -1) THEN
5958          rlm_core_sv.dlog(C_DEBUG,'inventory_item_id', x_cum_key_record.inventory_item_id);
5959       END IF;
5960       --
5961       rlm_cum_sv.GetShippLines(
5962                  x_cum_key_id             => x_old_cum_records(cum_records_counter).cum_key_id,
5963                  x_ship_from_org_id       => x_ship_from_org_id,
5964                  x_ship_to_org_id         => x_ship_to_org_id,
5965                  x_intmed_ship_to_org_id  => x_intmed_ship_to_org_id,
5966                  x_bill_to_org_id         => x_bill_to_org_id,
5967                  x_customer_item_id       => x_customer_item_id,
5968                  x_inventory_item_id      => x_cum_key_record.inventory_item_id,
5969                  x_transaction_start_date => NULL,
5970                  x_transaction_end_date   => SYSDATE,
5971                  x_index                  => v_index);
5972       --
5973       IF g_cum_oe_lines.COUNT > 0 THEN
5974       --
5975           rlm_cum_sv.quicksort(g_cum_oe_lines.FIRST,
5976                                g_cum_oe_lines.LAST,
5977                                C_cum_oe_lines);
5978       --
5979       END IF;
5980       --
5981       x_old_cum_records(cum_records_counter).record_return_status := TRUE;
5982       x_old_cum_records(cum_records_counter).cum_qty := 0;
5983       --
5984       x_old_cum_records(cum_records_counter).cum_qty_to_be_accumulated := 0;
5985       --
5986       x_old_cum_records(cum_records_counter).cum_qty_after_cutoff := 0;
5987       --
5988       x_old_cum_records(cum_records_counter).use_ship_incl_rule_flag := 'Y';
5989       --
5990       x_old_cum_records(cum_records_counter).as_of_date_time := NULL;
5991 
5992       -- Need to set called_by_reset_cum to 'Y'
5993       x_cum_key_record.called_by_reset_cum := 'Y';
5994       --
5995 
5996       rlm_cum_sv.SetSupplierCum(
5997                  x_index                  => cum_records_counter,
5998                  x_cum_key_record         => x_cum_key_record,
5999                  x_transaction_start_date => NULL,
6000                  x_cum_records            => x_old_cum_records,
6001                  x_return_status          => v_return_status,
6002                  x_counter                => counter,
6003                  x_adjustment_date        => v_adjustment_date);
6004       --
6005       IF (l_debug <> -1) THEN
6006          rlm_core_sv.dlog(C_DEBUG,'v_return_status',v_return_status);
6007       END IF;
6008       --
6009       IF v_return_status = FALSE THEN
6010       --
6011         x_return_status := FALSE;
6012 	--
6013         IF (l_debug <> -1) THEN
6014            rlm_core_sv.dlog(C_DEBUG,'SetSupplierCum failed');
6015         END IF;
6016 	--
6017         fnd_file.put_line(fnd_file.log,'Failed to update Old CUMs, rolling back');
6018         RAISE e_SetSupplierCum;
6019       --
6020       ELSE --{
6021       --
6022       -- get all adjustments after the last shipment
6023         SELECT  SUM(transaction_qty)
6024         INTO    adj_qty
6025         FROM    rlm_cust_item_cum_adj
6026         WHERE   cum_key_id = x_old_cum_records(cum_records_counter).cum_key_id
6027         AND     transaction_date_time <= sysdate
6028         AND     ((transaction_date_time >= v_adjustment_date)
6029              OR (v_adjustment_date IS NULL));
6030 	--
6031 	IF (l_debug <> -1) THEN
6032            rlm_core_sv.dlog(C_DEBUG,'v_adjustment_date',
6033                                               v_adjustment_date);
6034            rlm_core_sv.dlog(C_DEBUG, 'adj_qty',adj_qty);
6035         END IF;
6036         --
6037         x_old_cum_records(cum_records_counter).cum_qty :=
6038                            x_old_cum_records(cum_records_counter).cum_qty +
6039                            nvl(adj_qty,0);
6040         --
6041         UPDATE rlm_cust_item_cum_keys
6042         SET cum_qty           = x_old_cum_records(cum_records_counter).cum_qty,
6043             cum_qty_to_be_accumulated =
6044                x_old_cum_records(cum_records_counter).cum_qty_to_be_accumulated,
6045             cum_qty_after_cutoff =
6046                x_old_cum_records(cum_records_counter).cum_qty_after_cutoff,
6047             last_cum_qty_update_date  = SYSDATE,
6048             last_update_login         = FND_GLOBAL.LOGIN_ID,
6049             last_update_date          = SYSDATE,
6050             last_updated_by           = FND_GLOBAL.USER_ID
6051             WHERE  cum_key_id=x_old_cum_records(cum_records_counter).cum_key_id
6052             AND NVL(inactive_flag,'N')          =  'N';
6053             --
6054   	IF (l_debug <> -1) THEN
6055            rlm_core_sv.dlog(C_DEBUG,'x_old_cum_records.cum_key_id',
6056                              x_old_cum_records(cum_records_counter).cum_key_id);
6057            rlm_core_sv.dlog(C_DEBUG,'x_old_cum_records.cum_qty',
6058                            x_old_cum_records(cum_records_counter).cum_qty);
6059         END IF;
6060         --
6061 
6062       END IF; --}
6063     END IF; --}
6064   --
6065   END LOOP;
6066   --
6067 
6068    --  group the g_oe_tmp_line_tbl by header_id
6069    IF g_oe_tmp_line_tbl.COUNT = 0 THEN
6070    --
6071       IF (l_debug <> -1) THEN
6072          rlm_core_sv.dlog(C_DEBUG,'There are no order lines to process in ' ||
6073        'update old cum');
6074       END IF;
6075       --
6076       RAISE e_NoShipment;
6077    --
6078    END IF;
6079    --
6080    rlm_cum_sv.quicksort(g_oe_tmp_line_tbl.FIRST,
6081                         g_oe_tmp_line_tbl.LAST,
6082                         RLM_CUM_SV.C_line_table_type);
6083    --
6084    v_tmp_line_idx := g_oe_tmp_line_tbl.FIRST;
6085    --
6086    IF (l_debug <> -1) THEN
6087       rlm_core_sv.dlog(C_DEBUG, 'v_tmp_line_idx',v_tmp_line_idx);
6088    END IF;
6089    --
6090    v_line_idx := 1;
6091    --
6092    v_header_id := g_oe_tmp_line_tbl(v_tmp_line_idx).header_id;
6093    --
6094    IF (l_debug <> -1) THEN
6095       rlm_core_sv.dlog(C_DEBUG, 'v_header_id',v_header_id);
6096    END IF;
6097    --
6098    g_oe_line_tbl := oe_order_pub.g_miss_line_tbl;
6099    --
6100    BEGIN --{
6101    --
6102    fnd_profile.get('ECE_OUT_FILE_PATH',v_om_dbg_dir);
6103    --
6104    fnd_profile.put('OE_DEBUG_LOG_DIRECTORY',v_om_dbg_dir);
6105    --
6106    l_file_val      := OE_DEBUG_PUB.Set_Debug_Mode('FILE');
6107    --
6108    IF (l_debug <> -1) THEN
6109       rlm_core_sv.dlog(C_DEBUG, 'l_file_val',l_file_val);
6110    END IF;
6111    --
6112    LOOP --{
6113    /* Call OE_Order_GRP.Process_Order procedure to update OE_ORDER_LINES
6114       table by passing the g_oe_line_tbl structure that has been prepared
6115       this loop calls the process_order API once per each header_id,
6116       since the table is sorted by header_id then this loop
6117       calls the process_order once the header_id is changed  */
6118       --
6119       IF (l_debug <> -1) THEN
6120          rlm_core_sv.dlog(C_DEBUG,'processing header_id',v_header_id);
6121       END IF;
6122       --
6123       IF g_oe_tmp_line_tbl(v_tmp_line_idx).header_id = v_header_id THEN --{
6124       --
6125          g_oe_line_tbl(v_line_idx) := g_oe_tmp_line_tbl(v_tmp_line_idx);
6126          --
6127          v_line_idx := v_line_idx + 1;
6128          --
6129          IF v_tmp_line_idx = g_oe_tmp_line_tbl.LAST THEN --{
6130             -- process order for the last time
6131             OE_Order_GRP.Process_order(
6132               p_api_version_number     => x_oe_api_version,
6133               p_init_msg_list          => FND_API.G_TRUE,
6134               p_return_values          => FND_API.G_FALSE,
6135               p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
6136               x_return_status          => l_return_status,
6137               x_msg_count              => v_msg_count,
6138               x_msg_data               => v_msg_data,
6139               ------------------------------------------
6140               p_line_tbl               => g_oe_line_tbl,
6141               ------------------------------------------
6142               x_header_rec             => g_oe_header_out_rec,
6143               x_header_val_rec         => g_oe_header_val_out_rec,
6144               x_Header_Adj_tbl         => g_oe_Header_Adj_out_tbl,
6145               x_Header_Adj_val_tbl     => g_oe_Header_Adj_val_out_tbl,
6146               x_Header_price_Att_tbl   => g_Header_price_Att_out_tbl,
6147               x_Header_Adj_Att_tbl     => g_Header_Adj_Att_out_tbl,
6148               x_Header_Adj_Assoc_tbl   => g_Header_Adj_Assoc_out_tbl,
6149               x_Header_Scredit_tbl     => g_oe_Header_Scredit_out_tbl,
6150               x_Header_Scredit_val_tbl => g_oe_Hdr_Scdt_val_out_tbl,
6151               x_line_tbl               => l_oe_line_tbl_out,
6152               x_line_val_tbl           => g_oe_line_val_out_tbl,
6153               x_Line_Adj_tbl           => g_oe_line_Adj_out_tbl,
6154               x_Line_Adj_val_tbl       => g_oe_line_Adj_val_out_tbl,
6155               x_Line_price_Att_tbl     => g_Line_price_Att_out_tbl,
6156               x_Line_Adj_Att_tbl       => g_Line_Adj_Att_out_tbl,
6157               x_Line_Adj_Assoc_tbl     => g_Line_Adj_Assoc_out_tbl,
6158               x_Line_Scredit_tbl       => g_oe_line_scredit_out_tbl,
6159               x_Line_Scredit_val_tbl   => g_oe_line_scredit_val_out_tbl,
6160               x_Lot_Serial_tbl         => g_oe_lot_serial_out_tbl,
6161               x_Lot_Serial_val_tbl     => g_oe_lot_serial_val_out_tbl,
6162               x_Action_Request_tbl     => g_oe_Action_Request_out_Tbl) ;
6163               --
6164   	      IF (l_debug <> -1) THEN
6165                rlm_core_sv.dlog(C_DEBUG,'G_FILE',OE_DEBUG_PUB.G_FILE);
6166                rlm_core_sv.dlog(C_DEBUG, 'Input tbl count', g_oe_line_tbl.LAST);
6167                rlm_core_sv.dlog(C_DEBUG, 'Output tbl count',
6168                                        l_oe_line_tbl_out.LAST);
6169               END IF;
6170               --
6171            -- Handle the exceptions caused by the OE call
6172               --
6173 
6174             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN --{
6175                      --
6176                      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6177                      --}
6178              ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN --{
6179                      --
6180                      RAISE FND_API.G_EXC_ERROR;
6181                      --}
6182              ELSE    --{
6183                      --
6184                      x_return_status := TRUE;
6185                      --
6186   		     IF (l_debug <> -1) THEN
6187                         rlm_core_sv.dlog(C_DEBUG,
6188                          'Order line is updated successfully');
6189                      END IF;
6190                      --
6191              END IF; --}
6192             EXIT;
6193          --}
6194          --{ if not the last element
6195          ELSE
6196          --
6197             v_tmp_line_idx := g_oe_tmp_line_tbl.NEXT(v_tmp_line_idx);
6198             --
6199          END IF; --}
6200       --}
6201       --{ if header_id has changed
6202       ELSE
6203          --
6204          v_header_id := g_oe_tmp_line_tbl(v_tmp_line_idx).header_id;
6205          --
6206          v_line_idx := 1;
6207          --
6208 
6209          OE_Order_GRP.Process_order(
6210               p_api_version_number     => x_oe_api_version,
6211               p_init_msg_list          => FND_API.G_TRUE,
6212               p_return_values          => FND_API.G_FALSE,
6213               p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
6214               x_return_status          => l_return_status,
6215               x_msg_count              => v_msg_count,
6216               x_msg_data               => v_msg_data,
6217               ------------------------------------------
6218               p_line_tbl               => g_oe_line_tbl,
6219               ------------------------------------------
6220               x_header_rec             => g_oe_header_out_rec,
6221               x_header_val_rec         => g_oe_header_val_out_rec,
6222               x_Header_Adj_tbl         => g_oe_Header_Adj_out_tbl,
6223               x_Header_Adj_val_tbl     => g_oe_Header_Adj_val_out_tbl,
6224               x_Header_price_Att_tbl   => g_Header_price_Att_out_tbl,
6225               x_Header_Adj_Att_tbl     => g_Header_Adj_Att_out_tbl,
6226               x_Header_Adj_Assoc_tbl   => g_Header_Adj_Assoc_out_tbl,
6227               x_Header_Scredit_tbl     => g_oe_Header_Scredit_out_tbl,
6228               x_Header_Scredit_val_tbl => g_oe_Hdr_Scdt_val_out_tbl,
6229               x_line_tbl               => l_oe_line_tbl_out,
6230               x_line_val_tbl           => g_oe_line_val_out_tbl,
6231               x_Line_Adj_tbl           => g_oe_line_Adj_out_tbl,
6232               x_Line_Adj_val_tbl       => g_oe_line_Adj_val_out_tbl,
6233               x_Line_price_Att_tbl     => g_Line_price_Att_out_tbl,
6234               x_Line_Adj_Att_tbl       => g_Line_Adj_Att_out_tbl,
6235               x_Line_Adj_Assoc_tbl     => g_Line_Adj_Assoc_out_tbl,
6236               x_Line_Scredit_tbl       => g_oe_line_scredit_out_tbl,
6237               x_Line_Scredit_val_tbl   => g_oe_line_scredit_val_out_tbl,
6238               x_Lot_Serial_tbl         => g_oe_lot_serial_out_tbl,
6239               x_Lot_Serial_val_tbl     => g_oe_lot_serial_val_out_tbl,
6240               x_Action_Request_tbl     => g_oe_Action_Request_out_Tbl) ;
6241               --
6242            -- Handle the exceptions caused by the OE call
6243               --
6244   	      IF (l_debug <> -1) THEN
6245                rlm_core_sv.dlog(C_DEBUG,'G_FILE',OE_DEBUG_PUB.G_FILE);
6246                rlm_core_sv.dlog(C_DEBUG, 'Input tbl count', g_oe_line_tbl.LAST);               rlm_core_sv.dlog(C_DEBUG, 'Output tbl count',
6247                                        l_oe_line_tbl_out.LAST);
6248               END IF;
6249               --
6250               IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN --{
6251                       --
6252                       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6253                       --}
6254               ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN --{
6255                       --
6256                       RAISE FND_API.G_EXC_ERROR;
6257                       --}
6258               ELSE    --{
6259                       --
6260                       x_return_status := TRUE;
6261                       --
6262   		      IF (l_debug <> -1) THEN
6263                          rlm_core_sv.dlog(C_DEBUG,
6264                           'Order line is updated successfully');
6265                       END IF;
6266                       --
6267               END IF; --}
6268               --
6269               g_oe_line_tbl.DELETE;
6270               --
6271               g_oe_line_tbl := oe_order_pub.g_miss_line_tbl;
6272               --
6273               g_oe_line_tbl(v_line_idx) := g_oe_tmp_line_tbl(v_tmp_line_idx);
6274               --
6275            END IF; --} if the header_id has not changed
6276            --
6277      END LOOP; --}
6278   END ; --}
6279   --
6280   IF (l_debug <> -1) THEN
6281      rlm_core_sv.dpop(C_DEBUG);
6282   END IF;
6283   --
6284  EXCEPTION
6285    WHEN e_noshipment THEN
6286      --
6287      IF (l_debug <> -1) THEN
6288         rlm_core_sv.dpop(C_DEBUG, 'UpdateOldKey e_noshipment ');
6289      END IF;
6290      --
6291    WHEN e_SetSupplierCum THEN
6292      --
6293      x_return_status := FALSE;
6294      --
6295      IF (l_debug <> -1) THEN
6296         rlm_core_sv.dpop(C_DEBUG, 'UpdateOldKey e_SetSupplierCum');
6297      END IF;
6298      --
6299    WHEN FND_API.G_EXC_ERROR THEN
6300      --
6301      x_return_status := FALSE;
6302       --
6303       --Get message count and data
6304       --
6305 	OE_MSG_PUB.Count_And_Get(
6306 		p_count	=> v_msg_count,
6307 		p_data  => v_msg_data);
6308 	--
6309         rlm_message_sv.get_msg_text(
6310                 x_message_name  => 'RLM_CUM_PROCESS_ORDER',
6311                 x_text          => v_msg_data);
6312         --
6313 	fnd_file.put_line(fnd_file.log, v_msg_data);
6314 	--
6315      -- DEBUGGING
6316   	IF (l_debug <> -1) THEN
6317    	  rlm_core_sv.dlog(C_SDEBUG, 'G_EXC_ERROR');
6318    	  rlm_core_sv.dlog(C_SDEBUG, 'l_return_status', l_return_status);
6319    	  rlm_core_sv.dlog(C_SDEBUG, 'x_return_status', x_return_status);
6320    	  rlm_core_sv.dlog(C_SDEBUG, 'v_msg_count', v_msg_count);
6321    	  rlm_core_sv.dlog(C_SDEBUG, 'Main v_msg_data', v_msg_data);
6322 	END IF;
6323 	--
6324 	IF v_msg_count > 0 THEN
6325 	  --
6326 	  FOR k in 1 .. v_msg_count LOOP
6327 		--
6328        		v_msg_data := oe_msg_pub.get( p_msg_index => k,
6329                       				      p_encoded   => 'F');
6330 		--
6331 		fnd_file.put_line(fnd_file.log, v_msg_data);
6332 		--
6333   		IF (l_debug <> -1) THEN
6334    	          rlm_core_sv.dlog(C_SDEBUG, 'v_msg_data', v_msg_data);
6335 	        END IF;
6336 		--
6337           END LOOP;
6338 	  --
6339 	END IF;
6340 	--
6341 	--
6342      --
6343      -- User friendly message
6344      --
6345      IF (l_debug <> -1) THEN
6346         rlm_core_sv.dpop(C_DEBUG, 'UpdateOldKey G_EXC_ERROR');
6347      END IF;
6348      --
6349    WHEN  FND_API.G_EXC_UNEXPECTED_ERROR THEN
6350      x_return_status := FALSE;
6351       --
6352       --Get message count and data
6353       --
6354 	OE_MSG_PUB.Count_And_Get(
6355 		p_count	=> v_msg_count,
6356 		p_data  => v_msg_data);
6357 	--
6358         rlm_message_sv.get_msg_text(
6359                 x_message_name  => 'RLM_CUM_PROCESS_ORDER',
6360                 x_text          => v_msg_data);
6361         --
6362 	fnd_file.put_line(fnd_file.log, v_msg_data);
6363 	--
6364      -- DEBUGGING
6365         IF (l_debug <> -1) THEN
6366    	  rlm_core_sv.dlog(C_SDEBUG, 'G_EXC_UNEXPECTED_ERROR');
6367    	  rlm_core_sv.dlog(C_SDEBUG, 'l_return_status', l_return_status);
6368    	  rlm_core_sv.dlog(C_SDEBUG, 'x_return_status', x_return_status);
6369    	  rlm_core_sv.dlog(C_SDEBUG, 'v_msg_count', v_msg_count);
6370    	  rlm_core_sv.dlog(C_SDEBUG, 'Main v_msg_data', v_msg_data);
6371 	END IF;
6372 	--
6373 	IF v_msg_count > 0 THEN
6374 	  --
6375 	  FOR k in 1 .. v_msg_count LOOP
6376 		--
6377        		v_msg_data := oe_msg_pub.get( p_msg_index => k,
6378                       				      p_encoded   => 'F');
6379 		--
6380 		fnd_file.put_line(fnd_file.log, v_msg_data);
6381 	        --
6382   		IF (l_debug <> -1) THEN
6383    	          rlm_core_sv.dlog(C_SDEBUG, 'v_msg_data', v_msg_data);
6384 	        END IF;
6385 		--
6386           END LOOP;
6387 	  --
6388 	END IF;
6389 	--
6390      --
6391      -- User friendly message
6392      --
6393      IF (l_debug <> -1) THEN
6394         rlm_core_sv.dpop(C_DEBUG, 'UpdateOldKey G_EXC_UNEXPECTED_ERROR');
6395      END IF;
6396 
6397    WHEN OTHERS THEN
6398      x_return_status := FALSE;
6399      --
6400      IF (l_debug <> -1) THEN
6401         rlm_core_sv.dpop(C_DEBUG, 'UpdateOldKey others: '|| substr(SQLERRM,1,200));
6402      END IF;
6403  --
6404  END UpdateOldKey;--}
6405 
6406 
6407 FUNCTION GetInventoryItemId(x_customer_item_id IN NUMBER)
6408 RETURN NUMBER
6409 IS
6410 
6411 v_inventory_item_id             NUMBER          :=      NULL;
6412 Temp_Master_Organization_Id	Number		:=	NULL;
6413 Temp_Inventory_Item_Id		Number		:=	NULL;
6414 Temp_Inactive_Flag		Varchar2(1)	:=	NULL;
6415 
6416 CURSOR	CI_XREF_Cur IS
6417 SELECT	Master_Organization_Id, Inventory_Item_Id, Inactive_Flag
6418 FROM	MTL_CUSTOMER_ITEM_XREFS
6419 WHERE	Customer_Item_Id = x_customer_item_id
6420 ORDER BY  Preference_Number ASC;
6421 
6422 BEGIN
6423   --
6424   IF (l_debug <> -1) THEN
6425      rlm_core_sv.dpush(C_SDEBUG, 'GetInventoryItemId');
6426   END IF;
6427   --
6428   OPEN CI_XREF_Cur;
6429   FETCH CI_XREF_Cur INTO Temp_Master_Organization_Id,
6430 		         Temp_Inventory_Item_Id,
6431 		         Temp_Inactive_Flag;
6432 
6433   IF (CI_XREF_Cur%NOTFOUND) THEN
6434     --
6435     IF (l_debug <> -1) THEN
6436        rlm_core_sv.dlog(C_SDEBUG, 'Inventory Item Id could not be derived');
6437        rlm_core_sv.dpop(C_DEBUG, 'GetInventoryItemId');
6438     END IF;
6439     --
6440     return null;
6441     --
6442   ELSIF (Temp_Inactive_Flag = 'Y') THEN
6443 
6444     LOOP
6445 
6446       FETCH CI_XREF_Cur INTO Temp_Master_Organization_Id,
6447 	  	             Temp_Inventory_Item_Id,
6448 	                     Temp_Inactive_Flag;
6449 
6450       IF (CI_XREF_Cur%NOTFOUND) THEN
6451         --
6452         IF (l_debug <> -1) THEN
6453            rlm_core_sv.dlog(C_SDEBUG, 'Inventory Item Id could not be derived');
6454            rlm_core_sv.dpop(C_DEBUG, 'GetInventoryItemId');
6455         END IF;
6456 	--
6457         return null;
6458 	--
6459       ELSIF (Temp_Inactive_Flag = 'N') THEN
6460 	--
6461         v_inventory_item_id:=Temp_Inventory_Item_Id;
6462 	--
6463       END IF;
6464       --
6465       EXIT WHEN ((CI_XREF_Cur%NOTFOUND) OR (Temp_Inactive_Flag = 'N'));
6466       --
6467     END LOOP;
6468 
6469   ELSE
6470 
6471     v_inventory_item_id:=Temp_Inventory_Item_Id;
6472 
6473   END IF;
6474 
6475   CLOSE CI_XREF_Cur;
6476 
6477   --CURSOR
6478 
6479   IF (l_debug <> -1) THEN
6480      rlm_core_sv.dlog(C_SDEBUG, 'inventory_item_id', v_inventory_item_id);
6481      rlm_core_sv.dpop(C_DEBUG, 'GetInventoryItemId');
6482   END IF;
6483   --
6484   return v_inventory_item_id;
6485   --
6486 EXCEPTION
6487 
6488 when others then
6489   --
6490   IF (l_debug <> -1) THEN
6491      rlm_core_sv.dlog(C_SDEBUG, 'Inventory Item Id could not be derived');
6492      rlm_core_sv.dpop(C_DEBUG, 'GetInventoryItemId');
6493   END IF;
6494   --
6495   return null;
6496   --
6497 END;
6498 
6499 
6500 /*===========================================================================
6501 
6502 Procedure GetLatestCum
6503 Parameters:       x_cum_key_record:  Has the information needed to retrive the
6504                                      cum key
6505                   x_rlm_setup_terms_record: It has the setup terms info
6506                   x_cum_record            : returns the latest cum record
6507 
6508 ============================================================================*/
6509 
6510 
6511 
6512  PROCEDURE GetLatestCum (
6513         x_cum_key_record IN     RLM_CUM_SV.cum_key_attrib_rec_type,
6514         x_rlm_setup_terms_record IN rlm_setup_terms_sv.setup_terms_rec_typ,
6515         x_cum_record     IN OUT NOCOPY RLM_CUM_SV.cum_rec_type,
6516         x_called_from_vd IN NUMBER)
6517  IS
6518 	v_cum_org_level_code	 	rlm_cust_shipto_terms.cum_org_level_code%TYPE;
6519 	p_ship_from_org_id		NUMBER DEFAULT NULL;
6520 	p_ship_to_address_id		NUMBER DEFAULT NULL;
6521 	p_intrmd_ship_to_address_id	NUMBER DEFAULT NULL;
6522 	p_bill_to_address_id		NUMBER DEFAULT NULL;
6523 	p_customer_item_id		NUMBER DEFAULT NULL;
6524 	p_purchase_order_number 	RLM_CUST_ITEM_CUM_KEYS.PURCHASE_ORDER_NUMBER%TYPE DEFAULT NULL;
6525 	p_cust_record_year		RLM_CUST_ITEM_CUM_KEYS.CUST_RECORD_YEAR%TYPE DEFAULT NULL;
6526 	p_cum_start_date		DATE   DEFAULT NULL;
6527 	E_UNEXPECTED    		EXCEPTION;
6528 
6529 
6530      /* The purpose of the following cursors is to get the cum key id,
6531         cum qty, cum start date, cust record year, po number, etc. that
6532         already exist in rlm_cust_item_cum_keys table. The cum record of
6533         interest is the one that got created last and which cum start date is
6534         no later than sysdate. */
6535 
6536      /* This cursor is used when the proc is called by  to get the latest cum
6537         and the cum control code is cum by date only and the cum organization
6538         level is not for all ship froms */
6539 
6540 	--
6541 	CURSOR	c_cum_by_date1 IS
6542 		SELECT 	cum_key_id,
6543 	 		cum_qty,
6544 	     		cum_qty_to_be_accumulated,
6545 			cum_qty_after_cutoff,
6546 	     		last_cum_qty_update_date,
6547 	     		cust_uom_code,
6548 			cum_start_date,
6549 			cust_record_year,
6550 			purchase_order_number
6551       		FROM   	RLM_CUST_ITEM_CUM_KEYS
6552                 WHERE   NVL(ship_from_org_id,0)	= NVL(p_ship_from_org_id,0)
6553       		AND    	NVL(ship_to_address_id,0) = NVL(p_ship_to_address_id,0)
6554       		AND    	NVL(intrmd_ship_to_id,0)=
6555                                               NVL(p_intrmd_ship_to_address_id,0)
6556       		AND    	NVL(bill_to_address_id,0)= NVL(p_bill_to_address_id,0)
6557       		AND    	NVL(customer_item_id,0)	= NVL(p_customer_item_id,0)
6558       		AND    	cum_start_date			IS NOT NULL
6559 		AND    	(cum_start_date			< SYSDATE
6560                          OR x_called_from_vd     = rlm_cum_sv.k_CalledByVD
6561                          )
6562 		AND    	purchase_order_number 		IS NULL
6563 		AND    	cust_record_year 		IS NULL
6564                 AND     NVL(inactive_flag,'N')          =  'N'
6565 		ORDER BY creation_date DESC;
6566 	--
6567 
6568      /* This cursor is used when the proc is called by to get the latest cum
6569         and the cum control code is cum by date only and the cum organization
6570         level is for all ship froms */
6571 	--
6572 	CURSOR 	c_cum_by_date2 IS
6573 		SELECT 	cum_key_id,
6574 	 		cum_qty,
6575 	     		cum_qty_to_be_accumulated,
6576 	     		cum_qty_after_cutoff,
6577 	     		last_cum_qty_update_date,
6578 	     		cust_uom_code,
6579 			cum_start_date,
6580 			cust_record_year,
6581 			purchase_order_number
6582       		FROM   	RLM_CUST_ITEM_CUM_KEYS
6583       		WHERE  	ship_from_org_id		IS NULL
6584       		AND    	NVL(ship_to_address_id,0)= NVL(p_ship_to_address_id,0)
6585       		AND    	NVL(intrmd_ship_to_id,0) =
6586                                               NVL(p_intrmd_ship_to_address_id,0)
6587       		AND    	NVL(bill_to_address_id,0) = NVL(p_bill_to_address_id,0)
6588       		AND    	NVL(customer_item_id,0)= NVL(p_customer_item_id,0)
6589       		AND    	cum_start_date     		IS NOT NULL
6590 		AND    	(cum_start_date			< SYSDATE
6591                          OR x_called_from_vd     = rlm_cum_sv.k_CalledByVD
6592                          )
6593 		AND    	purchase_order_number 		IS NULL
6594 		AND    	cust_record_year 		IS NULL
6595                 AND     NVL(inactive_flag,'N')          =  'N'
6596 		ORDER BY creation_date DESC;
6597 	--
6598 
6599      /* This cursor is used when the proc is called by to get the latest cum
6600         and the cum control code is cum by date/record year and the cum
6601         organization level is not for all ship froms */
6602 	--
6603 	CURSOR 	c_cum_by_date_record_year1 IS
6604 		SELECT 	cum_key_id,
6605 	 		cum_qty,
6606 	     		cum_qty_to_be_accumulated,
6607 	     		cum_qty_after_cutoff,
6608 	     		last_cum_qty_update_date,
6609 	     		cust_uom_code,
6610 			cum_start_date,
6611 			cust_record_year,
6612 			purchase_order_number
6613       		FROM   	RLM_CUST_ITEM_CUM_KEYS
6614       		WHERE  	NVL(ship_from_org_id,0)= NVL(p_ship_from_org_id,0)
6615       		AND    	NVL(ship_to_address_id,0)= NVL(p_ship_to_address_id,0)
6616       		AND    	NVL(intrmd_ship_to_id,0)
6617                                            = NVL(p_intrmd_ship_to_address_id,0)
6618       		AND    	NVL(bill_to_address_id,0)= NVL(p_bill_to_address_id,0)
6619       		AND    	NVL(customer_item_id,0)= NVL(p_customer_item_id,0)
6620       		AND    	cum_start_date     		IS NOT NULL
6621 		AND    	(cum_start_date			< SYSDATE
6622                          OR x_called_from_vd     = rlm_cum_sv.k_CalledByVD
6623                          )
6624 		AND    	purchase_order_number 		IS NULL
6625 		AND    	cust_record_year 		IS NOT NULL
6626                 AND     (cust_record_year         = p_cust_record_year
6627                          OR p_cust_record_year IS NULL
6628                          )
6629                 AND     NVL(inactive_flag,'N')          =  'N'
6630 		ORDER BY creation_date DESC;
6631 	--
6632 
6633      /* This cursor is used when the proc is called by to get the latest cum
6634         and the cum control code is cum by date/record year and the cum
6635         organization level is for all ship froms */
6636 	--
6637 	CURSOR 	c_cum_by_date_record_year2 IS
6638 		SELECT 	cum_key_id,
6639 	 		cum_qty,
6640 	     		cum_qty_to_be_accumulated,
6641 	     		cum_qty_after_cutoff,
6642 	     		last_cum_qty_update_date,
6643 	     		cust_uom_code,
6644 			cum_start_date,
6645 			cust_record_year,
6646 			purchase_order_number
6647       		FROM   	RLM_CUST_ITEM_CUM_KEYS
6648       		WHERE  	ship_from_org_id		IS NULL
6649       		AND    	NVL(ship_to_address_id,0) = NVL(p_ship_to_address_id,0)
6650       		AND    	NVL(intrmd_ship_to_id,0)
6651                                             = NVL(p_intrmd_ship_to_address_id,0)
6652       		AND    	NVL(bill_to_address_id,0) = NVL(p_bill_to_address_id,0)
6653       		AND    	NVL(customer_item_id,0)	= NVL(p_customer_item_id,0)
6654       		AND    	cum_start_date     		IS NOT NULL
6655 		AND    	(cum_start_date 		< SYSDATE
6656                          OR x_called_from_vd     = rlm_cum_sv.k_CalledByVD
6657                          )
6658 		AND    	purchase_order_number 		IS NULL
6659 		AND    	cust_record_year 		IS NOT NULL
6660                 AND     (cust_record_year         = p_cust_record_year
6661                          OR p_cust_record_year IS NULL
6662                          )
6663                 AND     NVL(inactive_flag,'N')          =  'N'
6664 		ORDER BY creation_date DESC;
6665 	--
6666 
6667      /* This cursor is used when the proc is called by to get the latest cum
6668         and the cum control code is cum by date/po and the cum
6669         organization level is not for all ship froms */
6670 	--
6671 	CURSOR 	c_cum_by_date_po1 IS
6672 		SELECT 	cum_key_id,
6673 	 		cum_qty,
6674 	     		cum_qty_to_be_accumulated,
6675 	     		cum_qty_after_cutoff,
6676 	     		last_cum_qty_update_date,
6677 	     		cust_uom_code,
6678 			cum_start_date,
6679 			cust_record_year,
6680 			purchase_order_number
6681       		FROM   	RLM_CUST_ITEM_CUM_KEYS
6682       		WHERE  	NVL(ship_from_org_id,0)= NVL(p_ship_from_org_id,0)
6683       		AND    	NVL(ship_to_address_id,0)= NVL(p_ship_to_address_id,0)
6684       		AND    	NVL(intrmd_ship_to_id,0)
6685                          	= NVL(p_intrmd_ship_to_address_id,0)
6686       		AND    	NVL(bill_to_address_id,0)= NVL(p_bill_to_address_id,0)
6687       		AND    	NVL(customer_item_id,0)	= NVL(p_customer_item_id,0)
6688       		AND    	cum_start_date     		IS NOT NULL
6689 		AND    	(cum_start_date 		< SYSDATE
6690                          OR x_called_from_vd     = rlm_cum_sv.k_CalledByVD
6691                          )
6692 		AND    	purchase_order_number 		IS NOT NULL
6693                 AND     (purchase_order_number = p_purchase_order_number
6694                          OR p_purchase_order_number IS NULL
6695                          )
6696 		AND    	cust_record_year 		IS NULL
6697                 AND     NVL(inactive_flag,'N')          =  'N'
6698 		ORDER BY creation_date DESC;
6699 	--
6700 
6701      /* This cursor is used when the proc is called by to get the latest cum
6702         and the cum control code is cum by date/po and the cum organization
6703         level is for all ship froms */
6704 	--
6705 	CURSOR c_cum_by_date_po2 IS
6706 		SELECT 	cum_key_id,
6707 	 		cum_qty,
6708 	     		cum_qty_to_be_accumulated,
6709 	     		cum_qty_after_cutoff,
6710 	     		last_cum_qty_update_date,
6711 	     		cust_uom_code,
6712 			cum_start_date,
6713 			cust_record_year,
6714 			purchase_order_number
6715       		FROM   	RLM_CUST_ITEM_CUM_KEYS
6716       		WHERE  	ship_from_org_id		IS NULL
6717       		AND    	NVL(ship_to_address_id,0) = NVL(p_ship_to_address_id,0)
6718       		AND    	NVL(intrmd_ship_to_id,0)
6719                                             = NVL(p_intrmd_ship_to_address_id,0)
6720       		AND    	NVL(bill_to_address_id,0) = NVL(p_bill_to_address_id,0)
6721       		AND    	NVL(customer_item_id,0)= NVL(p_customer_item_id,0)
6722       		AND    	cum_start_date     		IS NOT NULL
6723 		AND    	(cum_start_date			< SYSDATE
6724                          OR x_called_from_vd     = rlm_cum_sv.k_CalledByVD
6725                          )
6726 
6727 		AND    	purchase_order_number 		IS NOT NULL
6728                 AND     (purchase_order_number = p_purchase_order_number
6729                          OR p_purchase_order_number IS NULL
6730                          )
6731 		AND    	cust_record_year 		IS NULL
6732                 AND     NVL(inactive_flag,'N')          =  'N'
6733 		ORDER BY creation_date DESC;
6734 	--
6735  BEGIN
6736    --
6737    IF (l_debug <> -1) THEN
6738       rlm_core_sv.dpush(C_SDEBUG, 'GetLatestCum');
6739       rlm_core_sv.dlog(C_DEBUG, 'ship_to_address_id',
6740                                    x_cum_key_record.ship_to_address_id);
6741       rlm_core_sv.dlog(C_DEBUG, 'ship_from_org_id',
6742                                    x_cum_key_record.ship_from_org_id);
6743       rlm_core_sv.dlog(C_DEBUG, 'bill_to_address_id',
6744                                    x_cum_key_record.bill_to_address_id);
6745       rlm_core_sv.dlog(C_DEBUG, 'intrmd_ship_to_address_id',
6746                                    x_cum_key_record.intrmd_ship_to_address_id);
6747       rlm_core_sv.dlog(C_DEBUG, 'customer_item_id',
6748                                    x_cum_key_record.customer_item_id);
6749       rlm_core_sv.dlog(C_DEBUG, 'purchase_order_number',
6750                                    x_cum_key_record.purchase_order_number);
6751       rlm_core_sv.dlog(C_DEBUG, 'cum_start_date', x_cum_key_record.cum_start_date);
6752       rlm_core_sv.dlog(C_DEBUG, 'cust_record_year',
6753                                    x_cum_key_record.cust_record_year);
6754    END IF;
6755    --
6756    v_cum_org_level_code := x_rlm_setup_terms_record.cum_org_level_code;
6757    --
6758    p_customer_item_id := x_cum_key_record.customer_item_id;
6759    p_ship_from_org_id := x_cum_key_record.ship_from_org_id;
6760    --
6761    IF (l_debug <> -1) THEN
6762       rlm_core_sv.dlog(C_DEBUG, 'cum_control_code',
6763                                  x_rlm_setup_terms_record.cum_control_code);
6764       rlm_core_sv.dlog(C_DEBUG, 'cum_org_level_code', v_cum_org_level_code);
6765    END IF;
6766    --
6767    x_cum_record.cum_key_id := NULL;
6768    --
6769    IF v_cum_org_level_code = 'BILL_TO_SHIP_FROM' THEN--{
6770 	--
6771 	IF x_cum_key_record.bill_to_address_id IS NULL THEN--{
6772 	       --
6773   	       IF (l_debug <> -1) THEN
6774                   rlm_core_sv.dlog(C_DEBUG, 'Error:',
6775                                                     'RLM_CUM_BILL_TO_REQUIRED');
6776                END IF;
6777                --
6778                RAISE E_UNEXPECTED;
6779 	       --
6780 	END IF;--}
6781 	--
6782 	p_bill_to_address_id := x_cum_key_record.bill_to_address_id;
6783 		--
6784    ELSIF v_cum_org_level_code = 'SHIP_TO_SHIP_FROM' THEN--}{
6785 	--
6786 	IF x_cum_key_record.ship_to_address_id IS NULL THEN--{
6787                --
6788   	       IF (l_debug <> -1) THEN
6789                   rlm_core_sv.dlog(C_DEBUG, 'Error:', 'RLM_CUM_SHIP_TO_REQUIRED');
6790                END IF;
6791                --
6792                RAISE E_UNEXPECTED;
6793 	       --
6794 	END IF;--}
6795 	--
6796         p_ship_to_address_id := x_cum_key_record.ship_to_address_id;
6797 	--
6798    ELSIF v_cum_org_level_code = 'INTRMD_SHIP_TO_SHIP_FROM' THEN--}{
6799 		--
6800          IF x_cum_key_record.intrmd_ship_to_address_id IS NULL THEN--{
6801 	       --
6802   	       IF (l_debug <> -1) THEN
6803                   rlm_core_sv.dlog(C_DEBUG, 'Error:',
6804                                           'RLM_CUM_INTER_SHIP_TO_REQUIRED');
6805                END IF;
6806 	       --
6807                RAISE E_UNEXPECTED;
6808                --
6809 	END IF;--}
6810 	--
6811         p_intrmd_ship_to_address_id :=
6812                                   x_cum_key_record.intrmd_ship_to_address_id;
6813    ELSIF v_cum_org_level_code = 'SHIP_TO_ALL_SHIP_FROMS' THEN		--}{
6814 		p_ship_to_address_id := x_cum_key_record.ship_to_address_id;
6815    		p_ship_from_org_id := NULL;
6816 		--
6817    ELSE--}{
6818 	       --
6819   	       IF (l_debug <> -1) THEN
6820                   rlm_core_sv.dlog(C_DEBUG, 'Error:', 'RLM_CUM_UNKNOWN_ORG_LEVEL');
6821                END IF;
6822                --
6823                RAISE E_UNEXPECTED;
6824 	       --
6825    END IF;--}
6826    --
6827    --Open the cursors previously defined and fetch only the first row of records
6828    --
6829    IF x_rlm_setup_terms_record.cum_control_code = 'CUM_BY_DATE_ONLY'
6830    OR x_rlm_setup_terms_record.cum_control_code = 'CUM_UNTIL_MANUAL_RESET' THEN--{
6831         --
6832         IF v_cum_org_level_code <> 'SHIP_TO_ALL_SHIP_FROMS' THEN--{
6833            --
6834            OPEN 	c_cum_by_date1;
6835            --
6836            FETCH 	c_cum_by_date1
6837            INTO	x_cum_record.cum_key_id,
6838 	   	x_cum_record.cum_qty,
6839 	     	x_cum_record.cum_qty_to_be_accumulated,
6840 	     	x_cum_record.cum_qty_after_cutoff,
6841 	     	x_cum_record.last_cum_qty_update_date,
6842 	     	x_cum_record.cust_uom_code,
6843 		p_cum_start_date,
6844 		p_cust_record_year,
6845 		p_purchase_order_number;
6846            --
6847            CLOSE c_cum_by_date1;
6848          ELSE--}{
6849             --
6850             OPEN 	c_cum_by_date2;
6851             FETCH 	c_cum_by_date2
6852             INTO	x_cum_record.cum_key_id,
6853 	   	x_cum_record.cum_qty,
6854 	     	x_cum_record.cum_qty_to_be_accumulated,
6855 	     	x_cum_record.cum_qty_after_cutoff,
6856 	     	x_cum_record.last_cum_qty_update_date,
6857 	     	x_cum_record.cust_uom_code,
6858 		p_cum_start_date,
6859 		p_cust_record_year,
6860 		p_purchase_order_number;
6861             --
6862             CLOSE c_cum_by_date2;
6863           END IF;--}
6864           --
6865    ELSIF x_rlm_setup_terms_record.cum_control_code = 'CUM_BY_DATE_RECORD_YEAR' THEN--}{
6866       --
6867       p_cust_record_year := x_cum_key_record.cust_record_year;
6868       --
6869       IF v_cum_org_level_code <> 'SHIP_TO_ALL_SHIP_FROMS' THEN--{
6870          --
6871          OPEN 	c_cum_by_date_record_year1;
6872          --
6873          FETCH 	c_cum_by_date_record_year1
6874          INTO	x_cum_record.cum_key_id,
6875 		x_cum_record.cum_qty,
6876 	        x_cum_record.cum_qty_to_be_accumulated,
6877 	        x_cum_record.cum_qty_after_cutoff,
6878 	        x_cum_record.last_cum_qty_update_date,
6879 	     	x_cum_record.cust_uom_code,
6880 		p_cum_start_date,
6881 		p_cust_record_year,
6882 		p_purchase_order_number;
6883          --
6884          CLOSE c_cum_by_date_record_year1;
6885       ELSE--}{
6886          --
6887          OPEN 	c_cum_by_date_record_year2;
6888             --
6889             FETCH 	c_cum_by_date_record_year2
6890             INTO	x_cum_record.cum_key_id,
6891 	   	x_cum_record.cum_qty,
6892 	     	x_cum_record.cum_qty_to_be_accumulated,
6893 	     	x_cum_record.cum_qty_after_cutoff,
6894 	     	x_cum_record.last_cum_qty_update_date,
6895 	     	x_cum_record.cust_uom_code,
6896 		p_cum_start_date,
6897 		p_cust_record_year,
6898 		p_purchase_order_number;
6899          CLOSE c_cum_by_date_record_year2;
6900          --
6901       END IF;--}
6902       --
6903    ELSIF x_rlm_setup_terms_record.cum_control_code = 'CUM_BY_DATE_PO' THEN--}{
6904       --
6905       p_purchase_order_number := x_cum_key_record.purchase_order_number;
6906       --
6907       IF v_cum_org_level_code <> 'SHIP_TO_ALL_SHIP_FROMS' THEN--{
6908          --
6909          OPEN 	c_cum_by_date_po1;
6910          --
6911          FETCH 	c_cum_by_date_po1
6912          INTO	x_cum_record.cum_key_id,
6913 		x_cum_record.cum_qty,
6914 	     	x_cum_record.cum_qty_to_be_accumulated,
6915 	     	x_cum_record.cum_qty_after_cutoff,
6916 	     	x_cum_record.last_cum_qty_update_date,
6917 	     	x_cum_record.cust_uom_code,
6918 		p_cum_start_date,
6919 		p_cust_record_year,
6920 		p_purchase_order_number;
6921          --
6922          CLOSE c_cum_by_date_po1;
6923          --
6924       ELSE--}{
6925          --
6926          OPEN 	c_cum_by_date_po2;
6927          --
6928          FETCH 	c_cum_by_date_po2
6929          INTO	x_cum_record.cum_key_id,
6930 		x_cum_record.cum_qty,
6931 	     	x_cum_record.cum_qty_to_be_accumulated,
6932 	     	x_cum_record.cum_qty_after_cutoff,
6933 	     	x_cum_record.last_cum_qty_update_date,
6934 	     	x_cum_record.cust_uom_code,
6935 		p_cum_start_date,
6936 		p_cust_record_year,
6937 		p_purchase_order_number;
6938          --
6939          CLOSE c_cum_by_date_po2;
6940          --
6941       END IF;--}
6942       --
6943 --Bug 4307505 uncommented the code
6944    ELSIF x_rlm_setup_terms_record.cum_control_code = 'CUM_BY_PO_ONLY' THEN--}{
6945 
6946     p_purchase_order_number := x_cum_key_record.purchase_order_number;
6947 
6948       IF v_cum_org_level_code <> 'SHIP_TO_ALL_SHIP_FROMS' THEN--{
6949          --
6950          SELECT	cum_key_id,
6951 	    	cum_qty,
6952 		cum_qty_to_be_accumulated,
6953 		cum_qty_after_cutoff,
6954 	     	last_cum_qty_update_date,
6955 	     	cust_uom_code
6956          INTO  	x_cum_record.cum_key_id,
6957   		x_cum_record.cum_qty,
6958 		x_cum_record.cum_qty_to_be_accumulated,
6959 		x_cum_record.cum_qty_after_cutoff,
6960 	   	x_cum_record.last_cum_qty_update_date,
6961 	   	x_cum_record.cust_uom_code
6962          FROM 	RLM_CUST_ITEM_CUM_KEYS
6963          WHERE NVL(ship_from_org_id,0)	      = NVL(p_ship_from_org_id,0)
6964          AND    	NVL(ship_to_address_id,0)
6965                                             = NVL(p_ship_to_address_id,0)
6966          AND    	NVL(intrmd_ship_to_id,0)
6967                                       = NVL(p_intrmd_ship_to_address_id,0)
6968          AND    	NVL(bill_to_address_id,0)
6969                                              = NVL(p_bill_to_address_id,0)
6970          AND    	NVL(customer_item_id,0)
6971                                                = NVL(p_customer_item_id,0)
6972          AND    	purchase_order_number = NVL(p_purchase_order_number, ' ')
6973          AND    	NVL(cust_record_year, ' ')
6974                                             = NVL(p_cust_record_year, ' ')
6975          AND    	NVL(cum_start_date, sysdate)
6976                                           = NVL(p_cum_start_date, sysdate)
6977          AND            purchase_order_number IS NOT NULL
6978          AND     NVL(inactive_flag,'N')          =  'N'
6979          ORDER BY creation_date desc ;
6980          --
6981       ELSE--}{
6982          --
6983          SELECT	cum_key_id,
6984 		cum_qty,
6985 		cum_qty_to_be_accumulated,
6986 		cum_qty_after_cutoff,
6987 		last_cum_qty_update_date,
6988 		cust_uom_code
6989          INTO	x_cum_record.cum_key_id,
6990 		x_cum_record.cum_qty,
6991 	     	x_cum_record.cum_qty_to_be_accumulated,
6992 	     	x_cum_record.cum_qty_after_cutoff,
6993 	     	x_cum_record.last_cum_qty_update_date,
6994 	     	x_cum_record.cust_uom_code
6995          FROM   	RLM_CUST_ITEM_CUM_KEYS
6996          WHERE  	ship_from_org_id IS NULL
6997          AND    	NVL(ship_to_address_id, 0)
6998                                                  = NVL(p_ship_to_address_id,0)
6999          AND    	NVL(intrmd_ship_to_id, 0)
7000                                           = NVL(p_intrmd_ship_to_address_id,0)
7001          AND    	NVL(bill_to_address_id, 0)
7002                                                  = NVL(p_bill_to_address_id,0)
7003          AND    	NVL(customer_item_id, 0)
7004                                                    = NVL(p_customer_item_id,0)
7005          AND    	purchase_order_number = NVL(p_purchase_order_number, ' ')
7006          AND    	NVL(cust_record_year, ' ')
7007                                                 = NVL(p_cust_record_year, ' ')
7008          AND    	NVL(cum_start_date, sysdate)
7009                                               = NVL(p_cum_start_date, sysdate)
7010          AND          NVL(inactive_flag,'N')          =  'N'
7011          AND       purchase_order_number IS NOT NULL
7012          ORDER BY creation_date desc ;
7013          --
7014       END IF;--}
7015       --
7016 
7017    END IF;--}
7018    --
7019   IF (l_debug <> -1) THEN
7020       rlm_core_sv.dlog(C_DEBUG, 'p_ship_from_org_id', p_ship_from_org_id);
7021       rlm_core_sv.dlog(C_DEBUG, 'p_ship_to_address_id', p_ship_to_address_id);
7022       rlm_core_sv.dlog(C_DEBUG, 'p_intrmd_ship_to_address_id', p_intrmd_ship_to_address_id);
7023       rlm_core_sv.dlog(C_DEBUG, 'p_bill_to_address_id', p_bill_to_address_id);
7024       rlm_core_sv.dlog(C_DEBUG, 'p_customer_item_id', p_customer_item_id);
7025       rlm_core_sv.dlog(C_DEBUG, 'p_purchase_order_number', p_purchase_order_number);
7026       rlm_core_sv.dlog(C_DEBUG, 'p_cust_record_year', p_cust_record_year);
7027       rlm_core_sv.dlog(C_DEBUG, 'p_cum_start_date', p_cum_start_date);
7028    END IF;
7029    --
7030    x_cum_record.cum_start_date := p_cum_start_date;
7031    --
7032    IF x_cum_record.cum_key_id IS NOT NULL THEN
7033       --
7034       x_cum_record.record_return_status := TRUE;
7035       --
7036       IF (x_called_from_vd = rlm_cum_sv.k_CalledByVD )
7037       AND NVL(x_cum_record.cum_start_date,SYSDATE) > SYSDATE
7038       THEN
7039         --
7040         x_cum_record.msg_name := 'RLM_CUM_START_FUTURE';
7041         --
7042   	IF (l_debug <> -1) THEN
7043            rlm_core_sv.dlog(C_DEBUG, 'RLM_CUM_START_FUTURE');
7044         END IF;
7045         --
7046       END IF;
7047       --
7048    ELSE
7049       --
7050       x_cum_record.record_return_status := FALSE;
7051       --
7052       x_cum_record.msg_name := 'RLM_NO_ACTIVE_CUM_SP';
7053       --
7054       rlm_message_sv.get_msg_text(
7055              x_message_name  => x_cum_record.msg_name ,
7056              x_text          => x_cum_record.msg_data);
7057 
7058       --
7059    END IF;
7060    --
7061    IF x_cum_record.cum_qty IS NULL THEN
7062       x_cum_record.cum_qty := 0;
7063    END IF;
7064 
7065    IF x_cum_record.cum_qty_to_be_accumulated IS NULL THEN
7066       x_cum_record.cum_qty_to_be_accumulated := 0;
7067    END IF;
7068 
7069    IF x_cum_record.cum_qty_after_cutoff IS NULL THEN
7070       x_cum_record.cum_qty_after_cutoff := 0;
7071    END IF;
7072 
7073    IF (l_debug <> -1) THEN
7074       rlm_core_sv.dlog(C_DEBUG, 'cum_key_id', x_cum_record.cum_key_id);
7075       rlm_core_sv.dlog(C_DEBUG, 'cum_start_date', x_cum_record.cum_start_date);
7076       rlm_core_sv.dlog(C_DEBUG, 'cum_qty', x_cum_record.cum_qty);
7077       rlm_core_sv.dlog(C_DEBUG, 'record_return_status',
7078                              x_cum_record.record_return_status);
7079       rlm_core_sv.dpop(C_DEBUG,'GetLatestCum');
7080    END IF;
7081    --
7082    EXCEPTION
7083 	--
7084         WHEN E_UNEXPECTED THEN
7085 	    --
7086 	    x_cum_record.record_return_status := FALSE;
7087 	    --
7088             x_cum_record.msg_name := 'RLM_NO_ACTIVE_CUM_SP';
7089             --
7090             rlm_message_sv.get_msg_text(
7091                    x_message_name  => x_cum_record.msg_name ,
7092                    x_text          => x_cum_record.msg_data);
7093 
7094             --
7095   	    IF (l_debug <> -1) THEN
7096    	      rlm_core_sv.dpop(C_DEBUG,'E_UNEXPECTED');
7097 	    END IF;
7098 	    --
7099 	WHEN OTHERS THEN
7100 	    --
7101 	    x_cum_record.record_return_status := FALSE;
7102             --
7103             x_cum_record.msg_name := 'RLM_NO_ACTIVE_CUM_SP';
7104             --
7105             rlm_message_sv.get_msg_text(
7106                    x_message_name  => x_cum_record.msg_name ,
7107                    x_text          => x_cum_record.msg_data);
7108 
7109             --
7110   	    IF (l_debug <> -1) THEN
7111               rlm_core_sv.dlog(C_DEBUG, 'when others');
7112    	      rlm_core_sv.dpop(C_DEBUG, substr(SQLERRM,1,200));
7113 	    END IF;
7114 	    --
7115    END GetLatestCum;
7116 
7117 
7118 /*=========================================================================
7119 
7120 PROCEDURE NAME:       LockCumKey
7121 
7122 Parameter: x_CumKeyId  IN NUMBER
7123 
7124 Created by: jckwok
7125 
7126 Creation Date: June 15, 2004
7127 
7128 History: Created due to Bug 3688778
7129 
7130 ===========================================================================*/
7131 
7132 FUNCTION LockCumKey (x_CumKeyId  IN NUMBER)
7133 RETURN BOOLEAN
7134 IS
7135    --
7136    CURSOR c IS
7137      SELECT *
7138      FROM   RLM_CUST_ITEM_CUM_KEYS_ALL
7139      WHERE  cum_key_id = x_CumKeyId
7140      FOR UPDATE NOWAIT;
7141    --
7142 
7143 BEGIN
7144    --
7145    IF (l_debug <> -1) THEN
7146      rlm_core_sv.dpush(C_SDEBUG,'LockCumKey');
7147      rlm_core_sv.dlog(C_DEBUG,'Locking RLM_CUST_ITEM_CUM_KEYS_ALL');
7148    END IF;
7149    --
7150    OPEN  c;
7151    CLOSE c;
7152    --
7153    IF (l_debug <> -1) THEN
7154      rlm_core_sv.dpop(C_SDEBUG);
7155    END IF;
7156    --
7157    RETURN(TRUE);
7158    --
7159 EXCEPTION
7160    --
7161    WHEN APP_EXCEPTION.RECORD_LOCK_EXCEPTION THEN
7162      --
7163      IF (l_debug <> -1) THEN
7164        rlm_core_sv.dlog(C_DEBUG,'Cum Key Record in RLM_CUST_ITEM_CUM_KEYS_ALL cannot be locked');
7165        rlm_core_sv.dpop(C_SDEBUG);
7166      END IF;
7167      --
7168      RETURN(FALSE);
7169      --
7170    WHEN OTHERS THEN
7171      --
7172      rlm_message_sv.sql_error('Locking Cum Key Failed', 'rlm_cum_sv.LockCumKey');
7173      --
7174      IF (l_debug <> -1) THEN
7175        rlm_core_sv.dpop(C_SDEBUG,'EXCEPTION: OTHER - sql error');
7176      END IF;
7177      --
7178      RAISE;
7179      --
7180 END LockCumKey;
7181 
7182 
7183 END RLM_CUM_SV;