DBA Data[Home] [Help]

PACKAGE BODY: APPS.INV_MAINTAIN_RESERVATION_PUB

Source


1 PACKAGE BODY INV_MAINTAIN_RESERVATION_PUB AS
2 /* $Header: INVPMRVB.pls 120.43.12020000.6 2013/02/27 04:54:31 tachen ship $*/
3 
4 g_dummy_sn_tbl       inv_reservation_global.serial_number_tbl_type;
5 
6 -- CodeReview.SU.03 Define a global variable for l_Debug
7 g_debug       Number := Nvl(Fnd_Profile.Value('INV_DEBUG_TRACE'),0);
8 
9 
10 g_pkg_name CONSTANT VARCHAR2(30) := 'INV_MAINTAIN_RESERVATION_PUB';
11 g_version_printed   BOOLEAN      := FALSE;
12 -- Global Constants for variables  used in FND Log
13 -- CodeReview.SU.01  : Commenting following constants
14 -- G_statement_level   constant number   := FND_LOG.LEVEL_STATEMENT;
15 -- G_procedure_level   constant number   := FND_LOG.LEVEL_PROCEDURE;
16 -- G_event_level       constant number   := FND_LOG.LEVEL_EVENT;
17 -- G_exception_level   constant number   := FND_LOG.LEVEL_EXCEPTION;
18 -- G_error_level       constant number   := FND_LOG.LEVEL_ERROR;
19 -- G_unexp_level       constant number   := FND_LOG.LEVEL_UNEXPECTED;
20 -- G_Current_Debug_Level Constant Number := Fnd_Log.G_CURRENT_RUNTIME_LEVEL;
21 
22 -- Define a cursor ref variable
23 TYPE query_cur_ref_type IS REF CURSOR;
24 
25 
26 PROCEDURE mydebug (p_message IN VARCHAR2
27                   ,p_module_name IN VARCHAR2
28                   ,p_level IN NUMBER)
29  IS
30 BEGIN
31       IF g_version_printed THEN
32         inv_log_util.TRACE ('$Header: INVPMRVB.pls 120.43.12020000.6 2013/02/27 04:54:31 tachen ship $',g_pkg_name||'.'||p_module_name, p_level);
33       END IF;
34       inv_log_util.TRACE (p_message,g_pkg_name||'.'||p_module_name, p_level);
35 
36 END mydebug;
37 
38 
39 /*-------------------------------------------------------------------------------------*/
40 /* Procedure Name: FND_LOG_DEBUG                                                       */
41 /* Description   : Logs the debug message using fnd_log API's                          */
42 /* Called from   : Called from API                                                     */
43 /*                                                                                     */
44 /* STANDARD PARAMETERS                                                                 */
45 /*   In Parameters :                                                                   */
46 /*      p_severity_level Required    Severity level                                    */
47 /*      p_module_name    Required    Module name                                       */
48 /*      p_message        Required    Debug message that needs to be logged             */
49 /*   Output Parameters:                                                                */
50 /* NON-STANDARD PARAMETERS                                                             */
51 /*   In Parameters                                                                     */
52 /*   Out parameters                                                                    */
53 /* Change Hist :                                                                       */
54 /*-------------------------------------------------------------------------------------*/
55 
56 
57 /*Procedure Fnd_Log_Debug
58     ( p_severity_level IN NUMBER,
59       p_module_name    IN VARCHAR2,
60       p_message        IN VARCHAR2) IS
61 BEGIN
62 -- CodeReview.SU.02 Replaced global variables with fnd_log constants
63 IF p_severity_level = Fnd_Log.Level_Statement THEN
64    IF ( Fnd_Log.Level_Statement >= Fnd_Log.G_Current_Runtime_Level) THEN
65       Fnd_Log.String(p_severity_level,p_module_name,p_message);
66    END IF;
67 ELSIF p_severity_level = Fnd_Log.Level_Procedure THEN
68   IF ( Fnd_Log.Level_procedure >= Fnd_Log.G_Current_Runtime_level) THEN
69       FND_LOG.STRING(p_severity_level,p_module_name,p_message);
70   END IF;
71 ELSIF p_severity_level = Fnd_Log.Level_Event THEN
72   IF ( Fnd_Log.Level_Event >= Fnd_Log.G_Current_Runtime_Level) THEN
73       FND_LOG.STRING(p_severity_level,p_module_name,p_message);
74   END IF;
75 ELSIF p_severity_level = Fnd_Log.Level_Exception THEN
76   IF ( Fnd_Log.Level_Exception >= Fnd_Log.G_current_Runtime_Level) Then
77       FND_LOG.STRING(p_severity_level,p_module_name,p_message);
78   END IF;
79 ELSIF p_severity_level = Fnd_Log.Level_Error THEN
80   IF ( Fnd_Log.Level_Error >= Fnd_Log.G_Current_Runtime_level) THEN
81       FND_LOG.STRING(p_severity_level,p_module_name,p_message);
82   END IF;
83 ELSIF p_severity_level = Fnd_Log.Level_UnExpected THEN
84   IF ( Fnd_Log.Level_Unexpected >= Fnd_Log.G_Current_Runtime_level) THEN
85       FND_LOG.STRING(p_severity_level,p_module_name,p_message);
86   END IF;
87 END IF;
88 
89 END Fnd_Log_Debug;*/
90 
91 -- Local procedures to check missing parameters
92 -- Check_Reqd_Param is overloaded procedures, one for each type of scalar variabls
93 -- like date, varchar2, number
94 PROCEDURE Check_Reqd_Param (
95  p_param_value IN NUMBER,
96  p_param_name  IN VARCHAR2,
97  p_api_name    IN VARCHAR2
98  )
99 IS
100   l_debug    NUMBER;
101   c_api_name CONSTANT VARCHAR2(30) := 'Check_Reqd_Param';
102 BEGIN
103   IF (g_debug IS NULL) THEN
104       g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
105   END IF;
106 
107   l_debug := g_debug;
108 
109   IF (l_debug = 1) THEN
110      mydebug('Checking API ' || p_api_name || ' , ' || p_param_name || ': ' || p_param_value, c_api_name, 9);
111   END IF;
112 
113   IF (NVL(p_param_value,FND_API.G_MISS_NUM) = FND_API.G_MISS_NUM) THEN
114      IF (l_debug = 1) THEN
115         mydebug('In ' || p_api_name ||', '|| p_param_name ||' is required parameter, value is g_miss_num', c_api_name, 9);
116      END IF;
117      FND_MESSAGE.SET_NAME('INV','INV_API_MISSING_PARAM');
118      FND_MESSAGE.SET_TOKEN('API_NAME',p_api_name);
119      FND_MESSAGE.SET_TOKEN('MISSING_PARAM',p_param_name);
120      FND_MSG_PUB.Add;
121      RAISE FND_API.G_EXC_ERROR;
122   END IF;
123 END Check_Reqd_Param;
124 
125 PROCEDURE Check_Reqd_Param (
126 p_param_value     IN VARCHAR2,
127 p_param_name  IN VARCHAR2,
128 p_api_name  IN VARCHAR2
129 )
130 IS
131   l_debug    NUMBER;
132   c_api_name CONSTANT VARCHAR2(30) := 'Check_Reqd_Param';
133 BEGIN
134   IF (g_debug IS NULL) THEN
135       g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
136   END IF;
137 
138   l_debug := g_debug;
139 
140   IF (l_debug = 1) THEN
141      mydebug('Checking API ' || p_api_name || ' , ' || p_param_name || ': ' || p_param_value, c_api_name, 9);
142   END IF;
143 
144   IF (NVL(p_param_value,FND_API.G_MISS_CHAR) = FND_API.G_MISS_CHAR) THEN
145      IF (l_debug = 1) THEN
146         mydebug('In ' || p_api_name || ', ' || p_param_name || ' is required parameter, value is g_miss_char', c_api_name, 9);
147      END IF;
148 
149      -- New Message - 001
150      -- $MISSING_PARAM is a required parameter.
151      FND_MESSAGE.SET_NAME('INV','INV_API_MISSING_PARAM');
152      FND_MESSAGE.SET_TOKEN('API_NAME',p_api_name);
153      FND_MESSAGE.SET_TOKEN('MISSING_PARAM',p_param_name);
154      FND_MSG_PUB.Add;
155      RAISE FND_API.G_EXC_ERROR;
156   END IF;
157 END Check_Reqd_Param;
158 
159 PROCEDURE Check_Reqd_Param (
160 p_param_value     IN DATE,
161 p_param_name  IN VARCHAR2,
162 p_api_name  IN VARCHAR2
163 )
164 IS
165   l_debug    NUMBER;
166   c_api_name CONSTANT VARCHAR2(30) := 'CHECK_REQD_PARAM';
167 BEGIN
168   IF (g_debug IS NULL) THEN
169       g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
170   END IF;
171 
172   l_debug := g_debug;
173 
174   IF (l_debug = 1) THEN
175      mydebug('Checking API ' || p_api_name || ' , ' || p_param_name || ': ' ||
176          TO_CHAR(p_param_value, 'YYYY-MM-DD:DD:SS'), c_api_name, 9);
177   END IF;
178 
179   IF (NVL(p_param_value,FND_API.G_MISS_DATE) = FND_API.G_MISS_DATE) THEN
180      IF (l_debug = 1) THEN
181         mydebug('In ' || p_api_name || ', ' || p_param_name || ' is required parameter, value is g_miss_date',
182             c_api_name, 9);
183      END IF;
184 
185      FND_MESSAGE.SET_NAME('INV','INV_API_MISSING_PARAM');
186      FND_MESSAGE.SET_TOKEN('API_NAME',p_api_name);
187      FND_MESSAGE.SET_TOKEN('MISSING_PARAM',p_param_name);
188      FND_MSG_PUB.Add;
189      RAISE FND_API.G_EXC_ERROR;
190   END IF;
191 END Check_Reqd_Param;
192 
193 
194    -- procedure
195    --   reduce_reservation
196    --
197    -- description
198    --   api will handle changes to the resevationl record based on the changes to the supply or
199    --   demand record changes.
200    --
201    -- Input Paramters
202    --   p_api_version_number   Number    API version number (current version is 1.0 Standard in parameter)
203    --   p_Init_Msg_lst         Varcahar2(1) (Flag for initialize message stack, standard input parameter)
204    --   p_Mtl_Maintain_Rsv_Tbl    Inv_Reservations_Global.mtl_Maintain_rsv_tbl_type
205    --   p_Delete_Flag          Varchar2(1)  Accepted values 'Y', 'N' and Null. Null value is equivalent to 'N'
206    --   p_Sort_By_Criteria     Number
207    --Out Parameters
208    --   x_Return_Status        Varchar2(1) (Return Status of API, Standard out parameter)
209    --   x_Msg_Count            Number (Message count from the stack, standard out parameter)
210    --   x_Msg_Data             Varchar2(255) (Message from message stack, standard out parameter)
211    --   x_Quantity_Modified    Number (Quantity that has been reduced or deleted by API)
212 
213 
214    Procedure reduce_reservation (
215         p_api_version_number   in   number,
216         p_init_msg_lst         in   varchar2,
217         x_return_status        out  nocopy varchar2,
218         x_msg_count            out  nocopy number,
219         x_msg_data             out  nocopy varchar2,
220         p_mtl_maintain_rsv_rec in   inv_reservation_global.mtl_maintain_rsv_rec_type,
221         p_delete_flag          in   varchar2,
222         p_sort_by_criteria     in   number,
223         x_quantity_modified    out  nocopy number ) is
224 
225        -- define constants for api version and api name
226        c_api_version_number constant number       := 1.0;
227        c_api_name           constant varchar2(30) := 'reduce_reservation';
228        c_module_name        constant varchar2(2000) := 'inv.plsql.inv_maintain_reservations.reduce_reservation';
229        c_debug_enabled      constant number := 1 ;
230        c_action_supply      constant number := 0;
231        c_action_demand      constant number := 1;
232        c_cancel_order_no    constant number := inv_reservation_global.g_cancel_order_no ;
233 
234        -- l_debug                         number      := nvl(fnd_profile.value('inv_debug_trace'), 0);
235        l_fnd_log_message               varchar2(2000);
236        l_rsv_rec                       inv_reservation_global.mtl_reservation_rec_type;
237        l_query_input                   inv_reservation_global.mtl_reservation_rec_type;
238        l_mtl_reservation_tbl           inv_reservation_global.mtl_reservation_tbl_type ;
239        l_mtl_reservation_tbl_count     number ;
240        l_sort_by_criteria              number ;
241        l_error_code                    number ;
242 
243        -- following variables for calling transfer reservation api
244        l_is_transfer_supply            varchar2(1);
245        l_original_rsv_rec              inv_reservation_global.mtl_reservation_rec_type;
246        -- convert_rsv_rec is used to convert expected_qty to primary_expected_uom_code
247        l_convert_rsv_rec               inv_reservation_global.mtl_reservation_rec_type;
248        l_to_rsv_rec                    inv_reservation_global.mtl_reservation_rec_type;
249        l_original_serial_number        inv_reservation_global.serial_number_tbl_type;
250        l_to_serial_number              inv_reservation_global.serial_number_tbl_type;
251        l_reservation_id                number ;
252        l_primary_uom_code              varchar2(10);
253        l_primary_expected_qty          number ;
254        l_to_primary_transaction_qty    number;
255        l_sum_primary_reservation_qty   number;
256        l_primary_need_reduced_qty      number ;
257        l_from_primary_transaction_qty  number;
258        l_msg_index_out                 number;
259        l_staged_rec_exists             varchar2(1) ;
260 
261        l_return_status     varchar2(1) := fnd_api.g_ret_sts_success;
262        l_msg_count         number;
263        l_msg_data          varchar2(1000);
264        l_wip_entity_type_id   number;
265        l_wip_job_type      varchar2(15);
266        l_source_header_id  number;
267 
268    begin
269       -- call fnd_log api in the begining of the api
270       l_fnd_log_message := 'begining of procedure :';
271       -- fnd_log_debug(fnd_log.level_procedure,c_module_name, l_fnd_log_message );
272 
273       -- log message in trace file
274       if g_debug= c_debug_enabled then
275          --codereview.su.04 : print package name once at the begning of api
276          g_version_printed := true ;
277          mydebug(l_fnd_log_message,c_api_name,9);
278          g_version_printed := false ;
279 
280       end if;
281 
282       -- initialize x_return_status variable to success in the begning of api
283       x_return_status := fnd_api.g_ret_sts_success;
284       --  standard call to check for call compatibility
285       if not fnd_api.compatible_api_call(c_api_version_number
286            , p_api_version_number
287            , c_api_name
288            , g_pkg_name
289            ) then
290           raise fnd_api.g_exc_unexpected_error;
291        end if;
292 
293        --  initialize message list. null value for p_init_msg_lst variable will be treated as 'n'
294        -- codereview.su.05  remove nvl function
295        if fnd_api.to_boolean(p_init_msg_lst) then
296           fnd_msg_pub.initialize;
297        end if;
298 
299        if (g_debug= c_debug_enabled) then
300           mydebug ('before checking for required parameters' ,c_api_name,1);
301        end if;
302 
303        -- check for required parameters
304        -- action column is a required column
305        check_reqd_param (
306           p_param_value =>  p_mtl_maintain_rsv_rec.action,
307           p_param_name =>  'p_mtl_maintain_rsv_rec.action',
308           p_api_name    =>  c_api_name );
309 
310        -- organization id is a required column
311        check_reqd_param (
312           p_param_value =>  p_mtl_maintain_rsv_rec.organization_id,
313           p_param_name =>  'p_mtl_maintain_rsv_rec.organization_id',
314           p_api_name    =>  c_api_name );
315 
316        -- inventory item id is a required column
317        check_reqd_param (
318           p_param_value =>  p_mtl_maintain_rsv_rec.inventory_item_id,
319           p_param_name =>  'p_mtl_maintain_rsv_rec.inventory_item_id',
320           p_api_name    =>  c_api_name );
321 
322 
323        -- get wip entity type if the supply is wip
324        if (p_mtl_maintain_rsv_rec.action = c_action_supply) then
325           l_source_header_id := p_mtl_maintain_rsv_rec.supply_source_header_id;
326        elsif (p_mtl_maintain_rsv_rec.action = c_action_demand) then
327           l_source_header_id := p_mtl_maintain_rsv_rec.demand_source_header_id;
328        end if;
329 
330        if (p_mtl_maintain_rsv_rec.supply_source_type_id = inv_reservation_global.g_source_type_wip) then
331            if (g_debug= c_debug_enabled) then
332               mydebug ('before calling get_wip_entity_type' , c_api_name, 9);
333            end if;
334            inv_reservation_pvt.get_wip_entity_type
335           (  p_api_version_number           => 1.0
336            , p_init_msg_lst                 => fnd_api.g_false
337            , x_return_status                => l_return_status
338            , x_msg_count                    => l_msg_count
339            , x_msg_data                     => l_msg_data
340            , p_organization_id              => p_mtl_maintain_rsv_rec.organization_id
341            , p_item_id                      => p_mtl_maintain_rsv_rec.inventory_item_id
342            , p_source_type_id               => null
343            , p_source_header_id             => l_source_header_id
344            , p_source_line_id               => null
345            , p_source_line_detail           => null
346            , x_wip_entity_type              => l_wip_entity_type_id
347            , x_wip_job_type                 => l_wip_job_type
348           );
349 
350            if (g_debug =  c_debug_enabled) then
351                mydebug('status return from get_wip_entity = ' || l_return_status, c_api_name, 9);
352                mydebug('l_wip_entity_type = ' || l_wip_entity_type_id, c_api_name, 9);
353            end if;
354            if (l_return_status = fnd_api.g_ret_sts_error) then
355                raise fnd_api.g_exc_error;
356            elsif (l_return_status = fnd_api.g_ret_sts_unexp_error) then
357                raise fnd_api.g_exc_unexpected_error;
358            end if;
359        end if;
360 
361        if (g_debug = c_debug_enabled) then
362           mydebug('p_delete_flag = '|| p_delete_flag ,c_api_name,1);
363           mydebug('action = ' || p_mtl_maintain_rsv_rec.action, c_api_name,1);
364        end if;
365 
366        if upper(nvl(p_delete_flag,'N')) = 'N' then
367           -- codereview.su.06. swap action types for sorting criteria
368           if p_mtl_maintain_rsv_rec.action = c_action_demand then
369             if p_sort_by_criteria is null then
370                l_sort_by_criteria := inv_reservation_global.g_query_supply_rcpt_date_asc;
371             else
372                l_sort_by_criteria := p_sort_by_criteria ;
373             end if;
374           elsif p_mtl_maintain_rsv_rec.action = c_action_supply then
375              if p_sort_by_criteria is null then
376                 l_sort_by_criteria := inv_reservation_global.g_query_demand_ship_date_desc;
377             else
378                l_sort_by_criteria := p_sort_by_criteria ;
379             end if;
380           end if;
381        elsif upper(nvl(p_delete_flag,'N')) = 'Y' then
382           -- if delete flag is 'y' sort by criteria will be same as what user has passed.
383           l_sort_by_criteria := p_sort_by_criteria;
384        end if;
385 
386        -- assign values to l_query_input record.
387        l_query_input.organization_id := p_mtl_maintain_rsv_rec.organization_id;
388        l_query_input.inventory_item_id := p_mtl_maintain_rsv_rec.inventory_item_id ;
389 
390        if (g_debug = c_debug_enabled) then
391           mydebug('checking action ' ,c_api_name,1);
392        end if;
393 
394        -- check if action to be performed on supply or demand
395        if p_mtl_maintain_rsv_rec.action = c_action_supply then
396           if (g_debug = c_debug_enabled) then
397              mydebug('action is supply' ,c_api_name,1);
398              mydebug('supply source type id = ' || p_mtl_maintain_rsv_rec.supply_source_type_id ,c_api_name,1);
399           end if;
400 
401           -- if action is supply then supply attributes should be passed
402           -- confirm with vishy that this logic is correct
403           -- supply source type is required column
404           check_reqd_param (
405              p_param_value =>  p_mtl_maintain_rsv_rec.supply_source_type_id,
406              p_param_name =>  'p_mtl_maintain_rsv_rec.supply_source_type_id',
407              p_api_name    =>  c_api_name);
408 
409           -- supply source header is required column
410           -- codereview.su.07. source_header_id is required for following source types
411           -- po, wip, int req, ext req, asn, intrasit
412           -- not required for inventory source type
413           if (p_mtl_maintain_rsv_rec.supply_source_type_id <> inv_reservation_global.g_source_type_inv) then
414              check_reqd_param (
415                 p_param_value =>  p_mtl_maintain_rsv_rec.supply_source_header_id,
416                 p_param_name =>  'p_mtl_maintain_rsv_rec.supply_source_header',
417                 p_api_name    =>  c_api_name );
418           end if; -- check for supply source header id
419 
420           -- suppply source line is required column
421           -- codereview.su.07. source_line_id is required for following source types
422           -- po, int req, ext req, asn, intrasit
423           -- not required for inventory,wip
424           if (p_mtl_maintain_rsv_rec.supply_source_type_id = inv_reservation_global.g_source_type_wip
425              and l_wip_entity_type_id in (inv_reservation_global.g_wip_source_type_batch
426                                          ,inv_reservation_global.g_wip_source_type_cmro)) then
427              check_reqd_param (
428                 p_param_value =>  p_mtl_maintain_rsv_rec.supply_source_line_id,
429                 p_param_name =>  'p_mtl_maintain_rsv_rec.supply_source_line_id',
430                 p_api_name    =>  c_api_name );
431           elsif (p_mtl_maintain_rsv_rec.supply_source_type_id
432           not in
433           (inv_reservation_global.g_source_type_inv,
434            inv_reservation_global.g_source_type_wip,
435            inv_reservation_global.g_source_type_account,
436            inv_reservation_global.g_source_type_account_alias)) then
437              check_reqd_param (
438                 p_param_value =>  p_mtl_maintain_rsv_rec.supply_source_line_id,
439                 p_param_name =>  'p_mtl_maintain_rsv_rec.supply_source_line_id',
440                 p_api_name    =>  c_api_name );
441 
442           end if; -- check for supply source line id
443 
444           -- suppply source line detail required column
445           -- codereview.su.07. source_line_detail is required for following source types
446           -- asn
447           if p_mtl_maintain_rsv_rec.supply_source_type_id = inv_reservation_global.g_source_type_asn then
448              check_reqd_param (
449                 p_param_value =>  p_mtl_maintain_rsv_rec.supply_source_line_detail,
450                 p_param_name =>  'p_mtl_maintain_rsv_rec.supply_source_line_detail',
451                 p_api_name    =>  c_api_name );
452           end if; -- check for supply source line id
453 
454           -- now assign values to l_query_input record
455           l_query_input.supply_source_type_id   := p_mtl_maintain_rsv_rec.supply_source_type_id ;
456           l_query_input.supply_source_header_id := p_mtl_maintain_rsv_rec.supply_source_header_id ;
457           l_query_input.supply_source_line_id   := p_mtl_maintain_rsv_rec.supply_source_line_id ;
458           -- codereview.su.08 : should pass supply source line detail value
459           l_query_input.supply_source_line_detail := p_mtl_maintain_rsv_rec.supply_source_line_detail ;
460 
461        elsif p_mtl_maintain_rsv_rec.action = c_action_demand then
462           -- if action is demand then demand attributes should be passed
463           -- demand source type is required column
464           -- dbms_output.put_line('action is demand');
465           if (g_debug = c_debug_enabled) then
466              mydebug('action is demand' ,c_api_name,1);
467           end if;
468 
469           check_reqd_param (
470              p_param_value =>  p_mtl_maintain_rsv_rec.demand_source_type_id,
471              p_param_name =>  'p_mtl_maintain_rsv_rec.demand_source_type_id',
472              p_api_name    =>  c_api_name );
473 
474           -- demand source header is required column
475           -- codereview.su.07. source_header_id is not required for inv source types
476           if p_mtl_maintain_rsv_rec.demand_source_type_id <> inv_reservation_global.g_source_type_inv then
477              check_reqd_param (
478                 p_param_value =>  p_mtl_maintain_rsv_rec.demand_source_header_id,
479                 p_param_name =>  'p_mtl_maintain_rsv_rec.demand_source_header',
480                 p_api_name    =>  c_api_name );
481           end if; -- check source header id
482 
483           -- demandy source line is required column
484           -- codereview.su.07. demand_source_line_id is not required for inv source types
485           if (p_mtl_maintain_rsv_rec.demand_source_type_id = inv_reservation_global.g_source_type_wip
486               and l_wip_entity_type_id in (inv_reservation_global.g_wip_source_type_cmro,
487                   inv_reservation_global.g_wip_source_type_batch)) then
488              check_reqd_param (
489                 p_param_value =>  p_mtl_maintain_rsv_rec.demand_source_line_id,
490                 p_param_name =>  'p_mtl_maintain_rsv_rec.demand_source_line_id',
491                 p_api_name    =>  c_api_name );
492           elsif (p_mtl_maintain_rsv_rec.demand_source_type_id not in
493                (inv_reservation_global.g_source_type_inv,inv_reservation_global.g_source_type_account,
494                 inv_reservation_global.g_source_type_account_alias)) then
495              check_reqd_param (
496                 p_param_value =>  p_mtl_maintain_rsv_rec.demand_source_line_id,
497                 p_param_name =>  'p_mtl_maintain_rsv_rec.demand_source_line_id',
498                 p_api_name    =>  c_api_name );
499           end if; -- check demand source line id
500 
501           -- now assign values to l_query_input record
502           l_query_input.demand_source_type_id   := p_mtl_maintain_rsv_rec.demand_source_type_id ;
503           l_query_input.demand_source_header_id := p_mtl_maintain_rsv_rec.demand_source_header_id ;
504           l_query_input.demand_source_line_id   := p_mtl_maintain_rsv_rec.demand_source_line_id ;
505           -- codereview.su.09 : should pass demand source line detail value
506           l_query_input.demand_source_line_detail := p_mtl_maintain_rsv_rec.demand_source_line_detail ;
507 
508        else
509           if (g_debug = c_debug_enabled) then
510              mydebug('invalid param value' ,c_api_name,1);
511           end if;
512           -- new message 002
513           -- "invalid value for parameter $param_name : $param_value
514           fnd_message.set_name('inv','inv_api_invalid_param_value');
515           fnd_message.set_token('param_name','action');
516           fnd_message.set_token('param_value',p_mtl_maintain_rsv_rec.action);
517           fnd_msg_pub.add;
518           raise fnd_api.g_exc_error;
519 
520        end if;
521        -- query the reservation records for supply based on sort by criteria passed by user.
522 
523        if (g_debug= c_debug_enabled) then
524           mydebug ('calling query reservation' ,c_api_name,1);
525        end if;
526 
527        IF (p_mtl_maintain_rsv_rec.project_id IS NOT NULL AND
528            p_mtl_maintain_rsv_rec.project_id <> fnd_api.g_miss_num) THEN
529             l_query_input.project_id := p_mtl_maintain_rsv_rec.project_id;
530        END IF;
531 
532        IF (p_mtl_maintain_rsv_rec.task_id IS NOT NULL AND
533            p_mtl_maintain_rsv_rec.task_id<> fnd_api.g_miss_num) THEN
534             l_query_input.task_id := p_mtl_maintain_rsv_rec.task_id;
535        END IF;
536 
537        inv_reservation_pvt.query_reservation(
538           p_api_version_number        => 1.0,
539           p_init_msg_lst              => p_init_msg_lst,
540           x_return_status             => x_return_status,
541           x_msg_count                 => x_msg_count,
542           x_msg_data                  => x_msg_data,
543           p_query_input               => l_query_input,
544           p_sort_by_req_date          => p_sort_by_criteria,
545           p_cancel_order_mode         => c_cancel_order_no,
546           x_mtl_reservation_tbl       => l_mtl_reservation_tbl,
547           x_mtl_reservation_tbl_count => l_mtl_reservation_tbl_count,
548           x_error_code                => l_error_code) ;
549 
550        if (g_debug = c_debug_enabled) then
551           mydebug('return status after calling query reservations'||x_return_status,c_api_name,1);
552           mydebug('totol number of records returned' || l_mtl_reservation_tbl_count, c_api_name,1);
553        end if;
554 
555        -- check if query reservation has raised any errors, if so raise exception
556        if x_return_status = fnd_api.g_ret_sts_error then
557           l_fnd_log_message := 'error while calling query_reservation api :';
558           -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
559 
560           if g_debug= c_debug_enabled then
561             mydebug(l_fnd_log_message, c_api_name,9);
562           end if;
563 
564           raise fnd_api.g_exc_error;
565        elsif x_return_status = fnd_api.g_ret_sts_unexp_error then
566           l_fnd_log_message := 'error while calling query_reservation api :';
567           -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
568 
569           if g_debug = c_debug_enabled then
570             mydebug(l_fnd_log_message, c_api_name,9);
571           end if;
572           raise fnd_api.g_exc_unexpected_error;
573        end if;
574 
575        -- check if there are any records returned by query_reservation
576        if l_mtl_reservation_tbl_count <= 0 then
577           l_fnd_log_message := 'there are no records returned by query_reservation api' ;
578           -- fnd_log_debug(fnd_log.level_procedure,c_module_name, l_fnd_log_message );
579           if g_debug = c_debug_enabled then
580             mydebug(l_fnd_log_message, c_api_name,9);
581           end if;
582           if p_mtl_maintain_rsv_rec.action = c_action_supply then
583              l_fnd_log_message := 'reservation action type is supply' ;
584              -- fnd_log_debug(fnd_log.level_procedure,c_module_name, l_fnd_log_message );
585              if g_debug = c_debug_enabled then
586                mydebug(l_fnd_log_message, c_api_name,9);
587              end if;
588              l_fnd_log_message := 'supply source type id is :' || p_mtl_maintain_rsv_rec.supply_source_type_id ;
589              -- fnd_log_debug(fnd_log.level_procedure,c_module_name, l_fnd_log_message );
590              if g_debug= c_debug_enabled then
591                mydebug(l_fnd_log_message, c_api_name,9);
592              end if;
593              l_fnd_log_message := 'supply source header id is:'||p_mtl_maintain_rsv_rec.supply_source_header_id;
594              -- fnd_log_debug(fnd_log.level_procedure,c_module_name, l_fnd_log_message );
595              if g_debug= c_debug_enabled then
596                mydebug(l_fnd_log_message, c_api_name,9);
597              end if;
598              l_fnd_log_message := 'supply source line id is:'||p_mtl_maintain_rsv_rec.supply_source_line_id;
599              -- fnd_log_debug(fnd_log.level_procedure,c_module_name, l_fnd_log_message );
600              if g_debug= c_debug_enabled then
601                mydebug(l_fnd_log_message, c_api_name,9);
602              end if;
603           else
604              l_fnd_log_message := 'reservation action type is demand' ;
605              -- fnd_log_debug(fnd_log.level_procedure,c_module_name, l_fnd_log_message );
606              if g_debug= c_debug_enabled then
607                mydebug(l_fnd_log_message, c_api_name,9);
608              end if;
609              l_fnd_log_message := 'demand source type id is :' || p_mtl_maintain_rsv_rec.demand_source_type_id ;
610              -- fnd_log_debug(fnd_log.level_procedure,c_module_name, l_fnd_log_message );
611              if g_debug= c_debug_enabled then
612                mydebug(l_fnd_log_message, c_api_name,9);
613              end if;
614              l_fnd_log_message := 'demand source header id is:'||p_mtl_maintain_rsv_rec.demand_source_header_id;
615              -- fnd_log_debug(fnd_log.level_procedure,c_module_name, l_fnd_log_message );
616              if g_debug= c_debug_enabled then
617                mydebug(l_fnd_log_message, c_api_name,9);
618              end if;
619              l_fnd_log_message := 'demand source line id is:'||p_mtl_maintain_rsv_rec.demand_source_line_id;
620              -- fnd_log_debug(fnd_log.level_procedure,c_module_name, l_fnd_log_message );
621              if g_debug= c_debug_enabled then
622                mydebug(l_fnd_log_message, c_api_name,9);
623              end if;
624           end if;
625           -- new message 003 . this is a warning not an error message
626           -- "query reservation api returns no records"
627           fnd_message.set_name('inv','inv_qry_rsv_api_rtns_no_rec');
628           fnd_msg_pub.add;
629           return ;
630        end if;
631        -- dbms_output.put_line('checking delete flag status');
632        if upper(nvl(p_delete_flag,'N')) = 'Y' then
633           -- check if there are any reservation records with staged_flag as 'y'
634           -- if so, throw error
635           if (g_debug= c_debug_enabled) then
636              mydebug ('Inside delete flag is Y' ,c_api_name,1);
637           end if;
638 
639           l_staged_rec_exists := 'N' ;
640           for i in 1..l_mtl_reservation_tbl_count loop
641              if l_mtl_reservation_tbl(i).supply_source_type_id
642                 = inv_reservation_global.g_source_type_inv
643              and nvl(l_mtl_reservation_tbl(i).staged_flag, 'N') = 'Y' then
644                 l_staged_rec_exists := 'Y' ;
645                 if (g_debug= c_debug_enabled) then
646                    mydebug ('Staged reservation exists' ,c_api_name,1);
647                 end if;
648                 exit ;
649              end if;
650           end loop ;
651           if l_staged_rec_exists = 'Y' then
652              -- new message 003 . this is a warning not an error message
653              -- "reservation can not be deleted as one or more reservation
654              -- records are in staged"
655              if (g_debug= c_debug_enabled) then
656                 mydebug ('Staged reservation exists. Error out' ,c_api_name,1);
657              end if;
658              fnd_message.set_name('INV','INV_RSV_REC_IN_STAGING');
659              fnd_msg_pub.add;
660              raise fnd_api.g_exc_error;
661           end if;
662 
663           -- dbms_output.put_line('delete flag is yes');
664           for i in 1..l_mtl_reservation_tbl_count loop
665              -- codereview.su.10  move it to begining of loop and assign record itself
666              l_original_rsv_rec := l_mtl_reservation_tbl(i) ;
667              if ( l_mtl_reservation_tbl(i).orig_supply_source_type_id
668                   <> l_mtl_reservation_tbl(i).supply_source_type_id )
669                and l_mtl_reservation_tbl(i).supply_source_type_id = inv_reservation_global.g_source_type_asn then
670                 l_original_rsv_rec.reservation_id :=
671                   l_mtl_reservation_tbl(i).reservation_id ;
672                 if (g_debug= c_debug_enabled) then
673                    mydebug ('ASN reservation. Need to transfer to the
674                             original supply' ,c_api_name,1);
675                 end if;
676                 if p_mtl_maintain_rsv_rec.action = c_action_supply then
677                    l_to_rsv_rec.supply_source_type_id      := inv_reservation_global.g_source_type_po ;
678                    l_to_rsv_rec.supply_source_header_id    := l_original_rsv_rec.supply_source_header_id;
679                    l_to_rsv_rec.supply_source_line_id      := l_original_rsv_rec.supply_source_line_id;
680                    --codereview.su.11 assign supply_source_line_detail value too
681                    l_to_rsv_rec.supply_source_line_detail  := l_original_rsv_rec.supply_source_line_detail;
682                    l_is_transfer_supply := fnd_api.g_true ;
683                    -- codereview.su.12 comment out else statement. not required
684                 /**************************
685                 else
686                    l_to_rsv_rec.demand_source_type_id      := inv_reservation_global.g_source_type_po ;
687                    l_to_rsv_rec.demand_source_header_id    := l_original_rsv_rec.demand_source_header_id;
688                    l_to_rsv_rec.demand_source_line_id      := l_original_rsv_rec.demand_source_line_id;
689                    l_to_rsv_rec.demand_source_delivery     := null;
690                    l_is_transfer_supply := fnd_api.g_false;
691                 ***************************/
692                 end if;
693                 -- ignoring serial numbers assingment since serial numbers are not changed
694                 -- reservation is transfered from one document type to another one.
695 
696                 -- call transfer reservation api and transfer reservation to po
697                 if (g_debug= c_debug_enabled) then
698                    mydebug ('Transferring reservation to the original supply' ,c_api_name,9);
699                    mydebug ('Header:Line:Line Detail :' || l_to_rsv_rec.supply_source_header_id ||': '||
700                         l_to_rsv_rec.supply_source_line_id ||': '|| l_to_rsv_rec.supply_source_line_detail,c_api_name,9);
701                 end if;
702 
703                 inv_reservation_pvt.transfer_reservation (
704                    p_api_version_number => 1.0 ,
705                    p_init_msg_lst      => fnd_api.g_false ,
706                    x_return_status      => x_return_status,
707                    x_msg_count          => x_msg_count,
708                    x_msg_data           => x_msg_data,
709                    --p_is_transfer_supply => l_is_transfer_supply,
710                    p_original_rsv_rec   => l_original_rsv_rec ,
711                    p_to_rsv_rec         => l_to_rsv_rec ,
712                    p_original_serial_number => l_original_serial_number,
713                    -- p_to_serial_number   => l_to_serial_number,
714                    p_validation_flag    => fnd_api.g_true  ,
715                    x_reservation_id     => l_reservation_id ) ;
716                  -- check if transfer reservation has raised any errors, if so raise exception
717                 if (g_debug= c_debug_enabled) then
718                    mydebug ('After calling transfer: ' || x_return_status ,c_api_name,1);
719                 end if;
720                 if x_return_status = fnd_api.g_ret_sts_error then
721                     l_fnd_log_message := 'error while calling transfer_reservation api :';
722                     -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
723                     if g_debug= c_debug_enabled then
724                       mydebug(l_fnd_log_message, c_api_name,9);
725                     end if;
726                     raise fnd_api.g_exc_error;
727                  elsif x_return_status = fnd_api.g_ret_sts_unexp_error then
728                     l_fnd_log_message := 'error while calling transfer_reservation api :';
729                     -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
730                     if g_debug= c_debug_enabled then
731                       mydebug(l_fnd_log_message, c_api_name,9);
732                     end if;
733                     raise fnd_api.g_exc_unexpected_error;
734                  elsif x_return_status = fnd_api.g_ret_sts_success then
735                     l_fnd_log_message := 'calling transfer_reservation api was successful:';
736                     -- fnd_log_debug(fnd_log.level_event,c_module_name, l_fnd_log_message );
737                     if g_debug= c_debug_enabled then
738                       mydebug(l_fnd_log_message, c_api_name,9);
739                     end if;
740                  end if;
741               ELSE -- not ASN
742                 if (g_debug= c_debug_enabled) then
743                    mydebug ('Deleting reservations, Delete flag: Y' ,c_api_name,1);
744                    mydebug ('Header:Line:Line Detail :' || l_original_rsv_rec.supply_source_header_id ||': '
745                         || l_original_rsv_rec.supply_source_line_id ||': '|| l_original_rsv_rec.supply_source_line_detail,c_api_name,1);
746                 end if;
747                 l_original_rsv_rec.reservation_id := l_mtl_reservation_tbl(i).reservation_id ;
748                 -- delete reservation record
749                 inv_reservation_pvt.delete_reservation(
750                    p_api_version_number => 1.0 ,
751                    p_init_msg_lst      => fnd_api.g_false ,
752                    x_return_status      => x_return_status,
753                    x_msg_count          => x_msg_count,
754                    x_msg_data           => x_msg_data,
755                    p_rsv_rec            => l_original_rsv_rec ,
756                    p_original_serial_number  => l_original_serial_number,
757                    p_validation_flag   => fnd_api.g_true);
758 
759                 if (g_debug= c_debug_enabled) then
760                    mydebug ('After calling delete: ' || x_return_status ,c_api_name,1);
761                 end if;
762                  -- check if delete reservation has raised any errors, if so raise exception
763                  if x_return_status = fnd_api.g_ret_sts_error then
764                     l_fnd_log_message := 'error while calling delete_reservation api :';
765                     -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
766                     if g_debug= c_debug_enabled then
767                       mydebug(l_fnd_log_message, c_api_name,9);
768                     end if;
769                     raise fnd_api.g_exc_error;
770                  elsif x_return_status = fnd_api.g_ret_sts_unexp_error then
771                     l_fnd_log_message := 'error while calling delete_reservation api :';
772                     -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
773                     if g_debug= c_debug_enabled then
774                       mydebug(l_fnd_log_message, c_api_name,9);
775                     end if;
776                     raise fnd_api.g_exc_unexpected_error;
777                  elsif x_return_status = fnd_api.g_ret_sts_success then
778                     l_fnd_log_message := 'calling delete_reservation api was successful:';
779                     -- fnd_log_debug(fnd_log.level_event,c_module_name, l_fnd_log_message );
780                     if g_debug= c_debug_enabled then
781                       mydebug(l_fnd_log_message, c_api_name,9);
782                     end if;
783                  end if; -- end of checking return status
784                  -- update quantity reduced variable
785                  x_quantity_modified := x_quantity_modified + l_mtl_reservation_tbl(i).reservation_quantity ;
786              end if; -- end of checking original_source_type_id and source_type_id
787           end loop;
788        elsif upper(nvl(p_delete_flag,'N')) = 'N' then
789           -- check for expected quantity value
790           -- dbms_output.put_line('delete flag is no');
791           if (g_debug= c_debug_enabled) then
792              mydebug ('Expected qty: ' ||  Nvl(p_mtl_maintain_rsv_rec.expected_quantity,0), c_api_name,1);
793              mydebug ('Expected qty uom: ' ||  p_mtl_maintain_rsv_rec.expected_quantity_uom, c_api_name,1);
794              mydebug ('To primary uom: ' || p_mtl_maintain_rsv_rec.to_primary_uom_code ,c_api_name,1);
795           end if;
796           if nvl(p_mtl_maintain_rsv_rec.expected_quantity,0) > 0 then
797              -- dbms_output.put_line('expected qty is > 0');
798              if p_mtl_maintain_rsv_rec.expected_quantity_uom is null or
799                 p_mtl_maintain_rsv_rec.expected_quantity_uom = fnd_api.g_miss_char then
800                 -- codereview.su.13. raise error
801                 check_reqd_param
802                   (
803                    p_param_value =>  p_mtl_maintain_rsv_rec.expected_quantity_uom,
804                    p_param_name =>  'p_mtl_maintain_rsv_rec.expected_quantity_uom',
805                    p_api_name    =>  c_api_name );
806                 -- dbms_output.put_line('raising error as expected qty uom is null');
807                 raise fnd_api.g_exc_error ;
808                 -- commenting following lines
809                 -- it is assumed that expected qty uom is same as primary qty uom
810                 -- l_primary_expected_qty := p_mtl_maintain_rsv_rec.expected_quantity ;
811              else -- expected_quantity_uom is not null
812                 -- dbms_output.put_line('expected qty uom is not null');
813                 if p_mtl_maintain_rsv_rec.to_primary_uom_code is null or
814                    p_mtl_maintain_rsv_rec.to_primary_uom_code = fnd_api.g_miss_char then
815                    -- in such cases we will take primary uom code of the first record
816                    -- and assuming that for all the records primary uom code will be same as
817                    -- inventory_item_id and organization_id will be same
818                    l_primary_uom_code := l_mtl_reservation_tbl(1).primary_uom_code ;
819                    -- codereview.su.14 comment out follwing line.
820                 else -- to_primary_uom_code is null
821                    l_primary_uom_code := p_mtl_maintain_rsv_rec.to_primary_uom_code ;
822                    -- su:06/15/2005
823                 end if; -- to_primary_uom_code is null
824                 if g_debug= c_debug_enabled then
825                    mydebug('primary uom code: ' || l_primary_uom_code, c_api_name,9);
826                    mydebug('primary expected qty : ' || l_primary_expected_qty, c_api_name,9);
827                 end if;
828                 IF p_mtl_maintain_rsv_rec.expected_quantity_uom <>
829                   l_primary_uom_code THEN
830                    -- dbms_output.put_line('expected qty uom is not equal to primary_uom_code');
831                    l_primary_expected_qty := inv_convert.inv_um_convert
832                      (
833                       item_id        => p_mtl_maintain_rsv_rec.inventory_item_id,
834                       precision      => null,
835                       from_quantity  => p_mtl_maintain_rsv_rec.expected_quantity,
836                       from_unit      => p_mtl_maintain_rsv_rec.expected_quantity_uom,
837                       to_unit        => p_mtl_maintain_rsv_rec.to_primary_uom_code,
838                       from_name      => null,
839                       to_name        => null );
840                    l_fnd_log_message := 'after calling api inv_convert.inv_um_convert:';
841                    -- fnd_log_debug(fnd_log.level_event,c_module_name, l_fnd_log_message );
842                    if g_debug= c_debug_enabled then
843                       mydebug(l_fnd_log_message, c_api_name,9);
844                       mydebug('primary expected qty: ' || l_primary_expected_qty, c_api_name,9);
845                    end if;
846 
847                 ELSE -- they are the same
848                    l_primary_expected_qty := p_mtl_maintain_rsv_rec.expected_quantity ;
849                 END IF; -- end of expected qty uom <> primary qty uom su:06/15
850                 -- get sum of all primary reservation quantity
851                 -- intialize sum of primary reservation qty
852                 l_sum_primary_reservation_qty := 0 ;
853                 for i in 1..l_mtl_reservation_tbl_count loop
854                    l_sum_primary_reservation_qty := l_sum_primary_reservation_qty +
855                      l_mtl_reservation_tbl(i).primary_reservation_quantity ;
856                 end loop;
857 
858                 l_primary_need_reduced_qty := l_sum_primary_reservation_qty - l_primary_expected_qty;
859 
860                 if g_debug= c_debug_enabled THEN
861                    mydebug('Expectd Qty is not null', c_api_name,9);
862                    mydebug('Total reserved qty: ' || l_sum_primary_reservation_qty, c_api_name,9);
863                    mydebug('To be reduced qty: ' || l_primary_need_reduced_qty, c_api_name,9);
864                 END IF;
865                 -- dbms_output.put_line('primary need reduced qty' || l_primary_need_reduced_qty );
866                 -- dbms_output.put_line('sum primary reservation qty' || l_sum_primary_reservation_qty );
867                 -- dbms_output.put_line('primary expected qty' || l_primary_expected_qty );
868                 -- end if; commented by satish
869                 -- end if; -- end of expected qty uom <> primary qty uom su:06/15
870              end if; -- end of expected_quantity_uom is not null
871           else  -- expected_qty is zero or null
872              if g_debug= c_debug_enabled THEN
873                 mydebug('Expectd Qty is null', c_api_name,9);
874                 mydebug('from primary txn qty: ' ||p_mtl_maintain_rsv_rec.from_primary_txn_quantity,c_api_name,9);
875                 mydebug('from primary uom code: ' ||p_mtl_maintain_rsv_rec.from_primary_uom_code,c_api_name,9);
876                 mydebug('from txn uom code: ' ||p_mtl_maintain_rsv_rec.from_transaction_uom_code,c_api_name,9);
877 
878              END IF;
879              if p_mtl_maintain_rsv_rec.from_primary_txn_quantity is null or
880                 p_mtl_maintain_rsv_rec.from_primary_txn_quantity = fnd_api.g_miss_num then
881                 --codereview.su.16 check if from_transaction_uom_code and from_primary_uom_code
882                 -- is not null, if they are then raise error
883 
884                 -- from_primary_uom_code column is a required column
885                 check_reqd_param
886                   (
887                    p_param_value =>  p_mtl_maintain_rsv_rec.from_primary_uom_code,
888                    p_param_name =>  'p_mtl_maintain_rsv_rec.from_primary_uom_code',
889                    p_api_name    =>  c_api_name );
890 
891                 -- from_transaction_uom_code column is a required column
892                 check_reqd_param
893                   (
894                    p_param_value =>  p_mtl_maintain_rsv_rec.from_transaction_uom_code,
895                    p_param_name =>  'p_mtl_maintain_rsv_rec.from_transaction_uom_code',
896                    p_api_name    =>  c_api_name );
897 
898                 if p_mtl_maintain_rsv_rec.from_transaction_uom_code <> p_mtl_maintain_rsv_rec.from_primary_uom_code then
899                    --codereview.su.17
900                    l_from_primary_transaction_qty :=
901                      inv_convert.inv_um_convert
902                      (
903                       item_id        => p_mtl_maintain_rsv_rec.inventory_item_id,
904                       precision      => null,
905                       from_quantity  => p_mtl_maintain_rsv_rec.from_transaction_quantity,
906                       from_unit      => p_mtl_maintain_rsv_rec.from_transaction_uom_code,
907                       to_unit        => p_mtl_maintain_rsv_rec.from_primary_uom_code,
908                       from_name      => null,
909                       to_name        => null );
910                    l_fnd_log_message := 'after calling api inv_convert.inv_um_convert 02:';
911                    -- fnd_log_debug(fnd_log.level_event,c_module_name, l_fnd_log_message );
912                    if g_debug= c_debug_enabled then
913                       mydebug(l_fnd_log_message, c_api_name,9);
914                       mydebug('from primary txn qty' || l_from_primary_transaction_qty, c_api_name,9);
915                    end if;
916 
917                 end if; --check for transaction uom code
918               else --
919                 if g_debug= c_debug_enabled then
920                    mydebug('txn uom same as primary uom', c_api_name,9);
921                 end if;
922                 l_from_primary_transaction_qty := p_mtl_maintain_rsv_rec.from_primary_txn_quantity ;
923              end if; -- from transaction uom code
924              -- now check to_transaction_qty
925              if g_debug= c_debug_enabled THEN
926                 mydebug('to primary txn qty: ' || p_mtl_maintain_rsv_rec.to_primary_txn_quantity, c_api_name,9);
927                 mydebug('to primary uom code: ' || p_mtl_maintain_rsv_rec.to_primary_uom_code, c_api_name,9);
928                 mydebug('to txn uom code: ' || p_mtl_maintain_rsv_rec.to_transaction_uom_code, c_api_name,9);
929 
930              END IF;
931              if p_mtl_maintain_rsv_rec.to_primary_txn_quantity is null or
932                p_mtl_maintain_rsv_rec.to_primary_txn_quantity = fnd_api.g_miss_num then
933                 -- to_primary_uom_code column is a required column
934                 check_reqd_param
935                   (
936                    p_param_value =>  p_mtl_maintain_rsv_rec.to_primary_uom_code,
937                    p_param_name =>  'p_mtl_maintain_rsv_rec.to_primary_uom_code',
938                    p_api_name    =>  c_api_name );
939 
940                 -- to_transaction_uom_code column is a required column
941                 check_reqd_param
942                   (
943                    p_param_value =>  p_mtl_maintain_rsv_rec.to_transaction_uom_code,
944                    p_param_name =>  'p_mtl_maintain_rsv_rec.to_transaction_uom_code',
945                    p_api_name    =>  c_api_name );
946 
947                 if p_mtl_maintain_rsv_rec.to_transaction_uom_code <>
948                   p_mtl_maintain_rsv_rec.to_primary_uom_code then
949                    l_to_primary_transaction_qty :=
950                      inv_convert.inv_um_convert
951                      (
952                       item_id        => p_mtl_maintain_rsv_rec.inventory_item_id,
953                       precision      => null,
954                       from_quantity  => p_mtl_maintain_rsv_rec.to_transaction_quantity,
955                       from_unit      => p_mtl_maintain_rsv_rec.to_transaction_uom_code,
956                       to_unit        => p_mtl_maintain_rsv_rec.to_primary_uom_code,
957                       from_name      => null,
958                       to_name        => null );
959                    l_fnd_log_message := 'after calling api inv_convert.inv_um_convert 03:';
960                    -- fnd_log_debug(fnd_log.level_event,c_module_name, l_fnd_log_message );
961                    if g_debug= c_debug_enabled then
962                       mydebug(l_fnd_log_message, c_api_name,9);
963                       mydebug('After convert to primary txn qty: '|| l_to_primary_transaction_qty, c_api_name,9);
964                    end if;
965 
966                  else -- to_transaction_uom_code <> to_primary_uom_code
967                    --codereview.su.20 added else statement
968                    if g_debug= c_debug_enabled then
969                       mydebug('to txn uom same as to primary uom', c_api_name,9);
970                    end if;
971                    l_to_primary_transaction_qty := p_mtl_maintain_rsv_rec.to_primary_txn_quantity ;
972                 end if; --to-transaction_uom_code <> to_primary_uom_code
973              end if; --check for transaction uom code
974 
975              l_primary_need_reduced_qty := l_to_primary_transaction_qty -
976                l_from_primary_transaction_qty;
977              if g_debug= c_debug_enabled then
978                 mydebug('From primary txn qty' || l_from_primary_transaction_qty, c_api_name,9);
979                 mydebug('to primary txn qty' || l_to_primary_transaction_qty, c_api_name,9);
980                 mydebug('Need to reduce qty' || l_primary_need_reduced_qty, c_api_name,9);
981              end if;
982           end if; -- end of checking expected_qty value
983 
984           x_quantity_modified := 0;
985           l_primary_need_reduced_qty := Nvl(l_primary_need_reduced_qty,0);
986           -- loop through all the records
987 
988           for i in 1..l_mtl_reservation_tbl_count loop
989              -- dbms_output.put_line('looping through reservation records');
990              if l_mtl_reservation_tbl(i).supply_source_type_id
991                = inv_reservation_global.g_source_type_inv
992                and nvl(l_mtl_reservation_tbl(i).staged_flag, 'N') = 'Y' then
993                 -- skip record
994                 -- dbms_output.put_line('skipping record');
995                 null;
996                 if g_debug= c_debug_enabled then
997                    mydebug('Skipped record as staged flag is null', c_api_name,9);
998                 END IF;
999               else -- supply_souce_type is inv
1000                 -- dbms_output.put_line('looping through reservation table');
1001                 if l_primary_need_reduced_qty = 0 then
1002                    exit;
1003                 end if;
1004                 -- dbms_output.put_line('primary need reduced qty := ' || l_primary_need_reduced_qty );
1005                 -- added following line : su:06/15/05
1006                 l_original_rsv_rec := l_mtl_reservation_tbl(i) ;
1007                 IF g_debug= c_debug_enabled then
1008                    mydebug('Reservation records primary reservation qty'||l_mtl_reservation_tbl(i).primary_reservation_quantity,c_api_name,9);
1009                    mydebug('l_primary_need_reduced_qty' || l_primary_need_reduced_qty, c_api_name,9);
1010                 END IF;
1011 
1012                 IF Nvl(l_mtl_reservation_tbl(i).primary_reservation_quantity,0) > l_primary_need_reduced_qty then
1013                    -- call update reservation api
1014                    -- dbms_output.put_line('calling update reservation api');
1015                    if p_mtl_maintain_rsv_rec.action = c_action_supply then
1016                       l_to_rsv_rec.supply_source_type_id := l_mtl_reservation_tbl(i).supply_source_type_id ;
1017                       l_to_rsv_rec.supply_source_header_id:= l_mtl_reservation_tbl(i).supply_source_header_id;
1018                       l_to_rsv_rec.supply_source_line_id:= l_mtl_reservation_tbl(i).supply_source_line_id;
1019                       l_to_rsv_rec.supply_source_line_detail:= l_mtl_reservation_tbl(i).supply_source_line_detail;
1020 
1021                       l_is_transfer_supply := fnd_api.g_true ;
1022                     else
1023                       l_to_rsv_rec.demand_source_type_id := l_mtl_reservation_tbl(i).demand_source_type_id ;
1024                       l_to_rsv_rec.demand_source_header_id:= l_mtl_reservation_tbl(i).demand_source_header_id;
1025                       l_to_rsv_rec.demand_source_line_id:= l_mtl_reservation_tbl(i).demand_source_line_id;
1026                       l_to_rsv_rec.demand_source_delivery     := null;
1027                       l_is_transfer_supply := fnd_api.g_false;
1028                    end if;
1029                    l_to_rsv_rec.primary_reservation_quantity := Nvl(l_mtl_reservation_tbl(i).primary_reservation_quantity,0)
1030                                                             - l_primary_need_reduced_qty;
1031                    IF g_debug= c_debug_enabled then
1032                       mydebug('Update reservation', c_api_name,9);
1033                       mydebug('Update qty' || l_primary_need_reduced_qty, c_api_name,9);
1034                    END IF;
1035                    inv_reservation_pvt.update_reservation
1036                      (
1037                       p_api_version_number =>  1.0,
1038                       p_init_msg_lst       =>  fnd_api.g_false,
1039                       x_return_status      =>  x_return_status,
1040                       x_msg_count          =>  x_msg_count,
1041                       x_msg_data           =>  x_msg_data ,
1042                       p_original_rsv_rec   =>  l_original_rsv_rec,
1043                       p_to_rsv_rec         =>  l_to_rsv_rec,
1044                       p_original_serial_number => l_original_serial_number,
1045                       p_to_serial_number   =>  l_to_serial_number,
1046                       p_validation_flag    =>  fnd_api.g_true ,
1047                       p_check_availability =>  fnd_api.g_false );
1048 
1049                    -- check if delete reservation has raised any errors, if so raise exception
1050                    if x_return_status = fnd_api.g_ret_sts_error then
1051                       l_fnd_log_message := 'error while calling update reservation api 02:';
1052                       -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1053                       if g_debug= c_debug_enabled then
1054                          mydebug(l_fnd_log_message, c_api_name,9);
1055                       end if;
1056                       raise fnd_api.g_exc_error;
1057                     elsif x_return_status = fnd_api.g_ret_sts_unexp_error then
1058                       l_fnd_log_message := 'error while calling update reservation api 02:';
1059                       -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1060                       if g_debug= c_debug_enabled then
1061                          mydebug(l_fnd_log_message, c_api_name,9);
1062                       end if;
1063                       raise fnd_api.g_exc_unexpected_error;
1064                     elsif x_return_status = fnd_api.g_ret_sts_success then
1065                       l_fnd_log_message := 'calling update reservation api was successful -02:';
1066                       -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1067                       if g_debug= c_debug_enabled then
1068                          mydebug(l_fnd_log_message, c_api_name,9);
1069                       end if;
1070                    end if;
1071                    x_quantity_modified := x_quantity_modified + l_primary_need_reduced_qty ;
1072                    l_primary_need_reduced_qty := 0;
1073 
1074                  ELSIF (Nvl(l_mtl_reservation_tbl(i).primary_reservation_quantity,0) <= l_primary_need_reduced_qty) THEN
1075                  -- reservation_qty < primary_need_reduced_qty
1076                    -- call delete reservation
1077                    IF g_debug= c_debug_enabled then
1078                       mydebug('Call delete reservation for reservation id' || l_mtl_reservation_tbl(i).reservation_id, c_api_name,9);
1079                    END IF;
1080                    l_original_rsv_rec.reservation_id := l_mtl_reservation_tbl(i).reservation_id ;
1081                    -- not assigning any values to l_original_serial_number as it does not matter
1082                    -- dbms_output.put_line('calling delete reservation api');
1083                    inv_reservation_pvt.delete_reservation
1084                      (
1085                       p_api_version_number =>  1.0,
1086                       p_init_msg_lst       =>  fnd_api.g_false,
1087                       x_return_status      =>  x_return_status,
1088                       x_msg_count          =>  x_msg_count,
1089                       x_msg_data           =>  x_msg_data ,
1090                       p_rsv_rec            =>  l_original_rsv_rec,
1091                       p_original_serial_number => l_original_serial_number,
1092                       p_validation_flag    =>  fnd_api.g_true  );
1093 
1094                    -- check if delete reservation has raised any errors, if so raise exception
1095                    if x_return_status = fnd_api.g_ret_sts_error then
1096                       l_fnd_log_message := 'error while calling delete reservation api 02:';
1097                       -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1098                       if g_debug= c_debug_enabled then
1099                          mydebug(l_fnd_log_message, c_api_name,9);
1100                       end if;
1101                       raise fnd_api.g_exc_error;
1102                     elsif x_return_status = fnd_api.g_ret_sts_unexp_error then
1103                       l_fnd_log_message := 'error while calling delete reservation api 02:';
1104                       ----  fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1105                       if g_debug= c_debug_enabled then
1106                          mydebug(l_fnd_log_message, c_api_name,9);
1107                       end if;
1108                       raise fnd_api.g_exc_unexpected_error;
1109                     elsif x_return_status = fnd_api.g_ret_sts_success then
1110                       l_fnd_log_message := 'calling delete reservation api was successful -02:';
1111                       -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1112                       if g_debug= c_debug_enabled then
1113                          mydebug(l_fnd_log_message, c_api_name,9);
1114                       end if;
1115                       l_primary_need_reduced_qty := Nvl(l_primary_need_reduced_qty,0) -
1116                         Nvl(l_mtl_reservation_tbl(i).primary_reservation_quantity,0) ;
1117                       x_quantity_modified := x_quantity_modified +
1118                         Nvl(l_mtl_reservation_tbl(i).primary_reservation_quantity,0) ;
1119                    end if; -- end of checking return status
1120                 end if; -- reservation_qty > primary_need_reduced_qty
1121              end if; -- if source type id = inv
1122           end loop ;
1123 
1124           -- dbms_output.put_line('end of looping through reservation table');
1125           /*******satish01**
1126         else
1127           l_to_primary_transaction_qty := p_mtl_maintain_rsv_rec.to_primary_txn_quantity ;
1128           end if;
1129           -- moving this line up
1130           -- end if; -- end of checking expected_qty value
1131           **************satish ***/
1132        end if; -- p_delete_flag = y
1133 
1134        -- call fnd_log api at the end of the api
1135        l_fnd_log_message := 'at the end of procedure :';
1136        -- -- fnd_log_debug(fnd_log.level_procedure,c_module_name, l_fnd_log_message );
1137 
1138        if g_debug= c_debug_enabled then
1139           mydebug(l_fnd_log_message, c_api_name,9);
1140        end if;
1141    exception
1142       when fnd_api.g_exc_error then
1143           x_return_status := fnd_api.g_ret_sts_error;
1144 
1145           --  get message count and data
1146           fnd_msg_pub.count_and_get
1147             (  p_count => x_msg_count
1148              , p_data  => x_msg_data
1149              );
1150 
1151            -- call fnd_log api at the end of the api
1152            l_fnd_log_message := 'when expected exception raised for procedure :' ;
1153            -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1154 
1155            -- log message in trace file
1156            if g_debug= c_debug_enabled then
1157                mydebug(l_fnd_log_message,c_api_name,9);
1158            end if;
1159 
1160            -- get messages from stack and log them in fnd tables
1161            if x_msg_count = 1 then
1162               -- fnd_log_debug(fnd_log.level_error,c_module_name, x_msg_data );
1163               -- log message in trace file
1164               if g_debug= c_debug_enabled then
1165                 mydebug(l_fnd_log_message,c_api_name,9);
1166               end if;
1167            elsif x_msg_count > 1 then
1168                for i in 1..x_msg_count loop
1169                  fnd_msg_pub.get
1170                   (p_msg_index     => i,
1171                    p_encoded       => 'f',
1172                    p_data          => l_fnd_log_message,
1173                    p_msg_index_out => l_msg_index_out );
1174 
1175                   -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1176 
1177                   -- log message in trace file
1178                   if g_debug= c_debug_enabled then
1179                       mydebug(l_fnd_log_message,c_api_name,9);
1180                   end if;
1181                end loop ;
1182            end if;
1183 
1184 
1185        when fnd_api.g_exc_unexpected_error then
1186             x_return_status := fnd_api.g_ret_sts_unexp_error ;
1187 
1188             --  get message count and data
1189             fnd_msg_pub.count_and_get
1190               (  p_count  => x_msg_count
1191                , p_data   => x_msg_data
1192                 );
1193             -- call fnd_log api at the end of the api
1194             l_fnd_log_message := 'when unexpected exception raised for procedure :';
1195             -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1196 
1197             -- log message in trace file
1198             if g_debug= c_debug_enabled then
1199                 mydebug(l_fnd_log_message,c_api_name,9);
1200             end if;
1201 
1202             -- get messages from stack and log them in fnd tables
1203             if x_msg_count = 1 then
1204               -- fnd_log_debug(fnd_log.level_error,c_module_name, x_msg_data );
1205 
1206               -- log message in trace file
1207               if g_debug= c_debug_enabled then
1208                 mydebug(l_fnd_log_message,c_api_name,9);
1209               end if;
1210             elsif x_msg_count > 1 then
1211                for i in 1..x_msg_count loop
1212                  fnd_msg_pub.get
1213                   (p_msg_index     => i,
1214                    p_encoded       => 'f',
1215                    p_data          => l_fnd_log_message,
1216                    p_msg_index_out => l_msg_index_out );
1217 
1218                   -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1219 
1220                   -- log message in trace file
1221                   if g_debug= c_debug_enabled then
1222                       mydebug(l_fnd_log_message,c_api_name,9);
1223                   end if;
1224                end loop ;
1225             end if;
1226 
1227         when others then
1228             x_return_status := fnd_api.g_ret_sts_unexp_error ;
1229 
1230             if fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
1231               then
1232                fnd_msg_pub.add_exc_msg
1233                  (  g_pkg_name
1234                   , c_api_name
1235                   );
1236             end if;
1237 
1238             --  get message count and data
1239             fnd_msg_pub.count_and_get
1240               (  p_count  => x_msg_count
1241                , p_data   => x_msg_data
1242                  );
1243 
1244             -- call fnd_log api at the end of the api
1245             l_fnd_log_message := 'when others exception raised for procedure :' || g_pkg_name || '.' || c_api_name ;
1246             -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1247 
1248             -- log message in trace file
1249             if g_debug= c_debug_enabled then
1250                 mydebug(l_fnd_log_message,c_api_name,9);
1251             end if;
1252 
1253            -- get messages from stack and log them in fnd tables
1254            if x_msg_count = 1 then
1255               -- fnd_log_debug(fnd_log.level_error,c_module_name, x_msg_data );
1256 
1257               -- log message in trace file
1258               if g_debug= c_debug_enabled then
1259                  mydebug(l_fnd_log_message,c_api_name,9);
1260                end if;
1261             elsif x_msg_count > 1 then
1262                 for i in 1..x_msg_count loop
1263                   fnd_msg_pub.get
1264                    (p_msg_index     => i,
1265                     p_encoded       => 'f',
1266                     p_data          => l_fnd_log_message,
1267                     p_msg_index_out => l_msg_index_out );
1268 
1269                    -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1270 
1271                    -- log message in trace file
1272                    if g_debug= c_debug_enabled then
1273                        mydebug(l_fnd_log_message,c_api_name,9);
1274                    end if;
1275                 end loop ;
1276             end if;
1277 
1278      end reduce_reservation;
1279 --this procedure initializes the reservation record - only done when a new requisition reservation
1280 --is created
1281 --
1282 procedure build_res_rec (x_reservation_rec out nocopy inv_reservation_global.mtl_reservation_rec_type)
1283 is
1284 
1285    -- define constants for api version and api name
1286    c_api_version_number       constant number       := 1.0;
1287    c_api_name                 constant varchar2(30) := 'build_res_rec';
1288    c_module_name              constant varchar2(2000) := 'inv.plsql.inv_maintain_reservation_pub';
1289    c_debug_enabled            constant number := 1 ;
1290    l_fnd_log_message          varchar2(2000);
1291 
1292    l_rsv      inv_reservation_global.mtl_reservation_rec_type;
1293 
1294 begin
1295    --this procedure initializes the mtl_reservation record
1296    l_fnd_log_message := 'begining of procedure :';
1297    -- dbms_output.put_line('begining of procedure');
1298    -- fnd_log_debug(fnd_log.level_procedure, c_module_name, l_fnd_log_message);
1299 
1300    -- log message in trace file
1301    if g_debug= c_debug_enabled then
1302       g_version_printed := true ;
1303       mydebug(l_fnd_log_message,c_api_name,9);
1304       g_version_printed := false ;
1305    end if;
1306 
1307    l_rsv.reservation_id                 := null;
1308    l_rsv.requirement_date               := null;
1309    l_rsv.organization_id                := null;       -- org id
1310    l_rsv.inventory_item_id              := null;       -- item id
1311    l_rsv.demand_source_type_id          :=
1312          inv_reservation_global.g_source_type_oe;   -- order entry
1313    l_rsv.demand_source_name             := null;
1314    l_rsv.demand_source_header_id        := null;     -- oe order number
1315    l_rsv.demand_source_line_id          := null;     -- oe order line number
1316    l_rsv.demand_source_delivery         := null;
1317    l_rsv.primary_uom_code               := null;  --10
1318    l_rsv.primary_uom_id                 := null;
1319    l_rsv.reservation_uom_code           := null;
1320    l_rsv.reservation_uom_id             := null;
1321    l_rsv.reservation_quantity           := null;
1322    l_rsv.primary_reservation_quantity   := null;        -- reservation quantity
1323 
1324    l_rsv.detailed_quantity              := null;
1325    l_rsv.secondary_uom_code             := null;
1326    l_rsv.secondary_uom_id               := null;
1327    l_rsv.secondary_reservation_quantity := null;
1328    l_rsv.secondary_detailed_quantity    := null;  --20
1329 
1330    l_rsv.autodetail_group_id            := null;
1331    l_rsv.external_source_code           := null;
1332    l_rsv.external_source_line_id        := null;
1333    l_rsv.supply_source_type_id          :=
1334          inv_reservation_global.g_source_type_req;
1335    l_rsv.supply_source_header_id        := null;       -- po req header id
1336    l_rsv.supply_source_line_id          := null;       -- po req line id
1337    l_rsv.supply_source_name             := null;
1338    l_rsv.supply_source_line_detail      := null;
1339    l_rsv.revision                       := null;
1340    l_rsv.subinventory_code              := null;  --30
1341    l_rsv.subinventory_id                := null;
1342    l_rsv.locator_id                     := null;
1343    l_rsv.lot_number                     := null;
1344    l_rsv.lot_number_id                  := null;
1345    l_rsv.pick_slip_number               := null;
1346    l_rsv.lpn_id                         := null;
1347    l_rsv.attribute_category             := null;
1348    l_rsv.attribute1                     := null;
1349    l_rsv.attribute2                     := null;
1350    l_rsv.attribute3                     := null;  --40
1351    l_rsv.attribute4                     := null;
1352    l_rsv.attribute5                     := null;
1353    l_rsv.attribute6                     := null;
1354    l_rsv.attribute7                     := null;
1355    l_rsv.attribute8                     := null;
1356    l_rsv.attribute9                     := null;
1357    l_rsv.attribute10                    := null;
1358    l_rsv.attribute11                    := null;
1359    l_rsv.attribute12                    := null;
1360    l_rsv.attribute13                    := null;  --50
1361    l_rsv.attribute14                    := null;
1362    l_rsv.attribute15                    := null;
1363    l_rsv.ship_ready_flag                := null;
1364    l_rsv.staged_flag                    := null;
1365 
1366    l_rsv.crossdock_flag                 := null;
1367    l_rsv.crossdock_criteria_id          := null;
1368    l_rsv.demand_source_line_detail      := null;
1369    l_rsv.serial_reservation_quantity    := null;
1370    l_rsv.supply_receipt_date            := null;
1371    l_rsv.demand_ship_date               := null;  --60
1372    l_rsv.project_id                     := null;
1373    l_rsv.task_id                        := null;
1374    l_rsv.orig_supply_source_type_id     := null;
1375    l_rsv.orig_supply_source_header_id   := null;
1376    l_rsv.orig_supply_source_line_id     := null;
1377    l_rsv.orig_supply_source_line_detail := null;
1378    l_rsv.orig_demand_source_type_id     := null;
1379    l_rsv.orig_demand_source_header_id   := null;
1380    l_rsv.orig_demand_source_line_id     := null;
1381    l_rsv.orig_demand_source_line_detail := null;  --70
1382    l_rsv.serial_number                  := null;  --71
1383 
1384    x_reservation_rec := l_rsv;
1385 
1386    -- call fnd_log api at the end of the api
1387    l_fnd_log_message := 'at the end of procedure :';
1388    -- -- fnd_log_debug(fnd_log.level_procedure,c_module_name, l_fnd_log_message );
1389 
1390    if g_debug= c_debug_enabled then
1391      mydebug(l_fnd_log_message, c_api_name,9);
1392    end if;
1393 
1394 exception
1395    when others then
1396         l_fnd_log_message := 'when others exception raised for procedure :' || g_pkg_name || '.' || c_api_name ;
1397         -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1398 
1399         -- log message in trace file
1400         if g_debug= c_debug_enabled then
1401              mydebug(l_fnd_log_message,c_api_name,9);
1402         end if;
1403 end;
1404 
1405 
1406 -- procedure create_res calls the inventory api create_reservation with the appropriate parameters
1407 --
1408 --
1409 procedure create_res
1410   (p_inventory_item_id        in number
1411    ,p_organization_id          in number
1412    ,p_demand_source_header_id  in number
1413    ,p_demand_source_line_id    in NUMBER
1414    ,p_supply_source_type_id    IN NUMBER
1415    ,p_supply_source_header_id  in number
1416    ,p_supply_source_line_id    in number
1417    ,p_requirement_date         in date
1418    ,p_reservation_quantity     in number
1419    ,p_reservation_uom_code     in varchar2
1420    ,p_project_id               in number
1421    ,p_task_id                  in number
1422    ,x_msg_count                out nocopy number
1423    ,x_msg_data                 out nocopy varchar2
1424    ,x_return_status            out nocopy varchar2)
1425   is
1426 
1427      -- define constants for api version and api name
1428      c_api_version_number       constant number       := 1.0;
1429      c_api_name                 constant varchar2(30) := 'create_res';
1430      c_module_name              constant varchar2(2000) := 'inv.plsql.inv_maintain_reservation_pub.maintain_reservation';
1431      c_debug_enabled            constant number := 1 ;
1432 
1433      --l_debug                    number := nvl(fnd_profile.value('inv_debug_trace'), 0);
1434      l_fnd_log_message          varchar2(2000);
1435      l_msg_index_out            number;
1436 
1437      l_rsv                      inv_reservation_global.mtl_reservation_rec_type;
1438      l_return_status            varchar2(1);
1439      --l_msg_count                number := 0;
1440      --l_msg_data                 varchar2(2000);
1441      l_qty                      number;
1442      l_second_qty               number;
1443      l_rsv_id                   number;
1444      l_demand_source_type_id    number;
1445 begin
1446      l_fnd_log_message := 'begining of procedure :';
1447      -- dbms_output.put_line('begining of procedure');
1448      -- fnd_log_debug(fnd_log.level_procedure, c_module_name, l_fnd_log_message);
1449 
1450      -- log message in trace file
1451      if g_debug= c_debug_enabled then
1452         g_version_printed := true ;
1453         mydebug(l_fnd_log_message,c_api_name,9);
1454         g_version_printed := false ;
1455      end if;
1456 
1457      -- bug 3600118: initialize return status variable
1458      l_return_status := fnd_api.g_ret_sts_success;
1459 
1460      build_res_rec(x_reservation_rec =>l_rsv);
1461 
1462      l_rsv.inventory_item_id       := p_inventory_item_id;
1463      l_rsv.organization_id         := p_organization_id;
1464      l_rsv.demand_source_header_id := p_demand_source_header_id;
1465      l_rsv.demand_source_line_id   := p_demand_source_line_id;
1466      l_rsv.supply_source_header_id := p_supply_source_header_id;
1467      l_rsv.supply_source_line_id   := p_supply_source_line_id;
1468      l_rsv.requirement_date        := p_requirement_date;
1469      l_rsv.reservation_quantity    := p_reservation_quantity;
1470      l_rsv.reservation_uom_code    := p_reservation_uom_code;
1471      l_rsv.project_id              := p_project_id;
1472      l_rsv.task_id                 := p_task_id;
1473      --
1474      --for internal sales order the demand source needs to be of type internal order
1475      --for creating a reservation.  we removed the call to cto_util to remove the
1476      --dependency between po and cto
1477      -- l_source_document_type_id :=
1478      --     cto_utility_pk.get_source_document_id (plineid => p_demand_source_line_id );
1479 
1480      if g_debug= c_debug_enabled then
1481          l_fnd_log_message := 'progress 100';
1482          mydebug(l_fnd_log_message,c_api_name,9);
1483      end if;
1484 
1485      select decode (h.source_document_type_id, 10,
1486                inv_reservation_global.g_source_type_internal_ord,
1487                inv_reservation_global.g_source_type_oe )
1488        into l_demand_source_type_id
1489        from oe_order_headers_all h, oe_order_lines_all l
1490       where h.header_id = l.header_id
1491         and l.line_id = p_demand_source_line_id;
1492 
1493      l_rsv.demand_source_type_id        := l_demand_source_type_id;
1494      l_rsv.supply_source_type_id        := p_supply_source_type_id;
1495 
1496      if g_debug= c_debug_enabled then
1497            l_fnd_log_message := 'progress 200';
1498            mydebug(l_fnd_log_message,c_api_name,9);
1499      end if;
1500 
1501      inv_reservation_pvt.create_reservation
1502         (
1503            p_api_version_number          => 1.0
1504          , p_init_msg_lst                => fnd_api.g_false
1505          , x_return_status               => l_return_status
1506          , x_msg_count                   => x_msg_count
1507          , x_msg_data                    => x_msg_data
1508          , p_rsv_rec                     => l_rsv
1509          , p_serial_number               => g_dummy_sn_tbl
1510          , x_serial_number               => g_dummy_sn_tbl
1511          , p_partial_reservation_flag    => fnd_api.g_true
1512          , p_force_reservation_flag      => fnd_api.g_false
1513          , p_validation_flag             => fnd_api.g_true
1514          --, p_validation_flag             => fnd_api.g_false
1515          , x_quantity_reserved           => l_qty
1516          , x_secondary_quantity_reserved => l_second_qty
1517          , x_reservation_id              => l_rsv_id
1518          );
1519 
1520      if g_debug= c_debug_enabled then
1521            l_fnd_log_message := 'l_return_status: '|| l_return_status;
1522            mydebug(l_fnd_log_message,c_api_name,9);
1523      end if;
1524 
1525      x_return_status := l_return_status;
1526 
1527      if x_return_status = fnd_api.g_ret_sts_success then
1528         l_fnd_log_message := 'calling create_reservation api was successful ';
1529         -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1530         if g_debug= c_debug_enabled then
1531            mydebug(l_fnd_log_message, c_api_name,9);
1532         end if;
1533      else
1534         l_fnd_log_message := 'error while calling create_reservation api ';
1535         -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1536         if g_debug= c_debug_enabled then
1537            mydebug(l_fnd_log_message, c_api_name,9);
1538         end if;
1539      end if;
1540 
1541      -- call fnd_log api at the end of the api
1542      l_fnd_log_message := 'at the end of procedure :';
1543      -- -- fnd_log_debug(fnd_log.level_procedure,c_module_name, l_fnd_log_message );
1544 
1545      if g_debug= c_debug_enabled then
1546            mydebug(l_fnd_log_message, c_api_name,9);
1547      end if;
1548 
1549 exception
1550    when fnd_api.g_exc_error then
1551         x_return_status := fnd_api.g_ret_sts_error;
1552 
1553         --  get message count and data
1554         fnd_msg_pub.count_and_get
1555           (  p_count => x_msg_count
1556            , p_data  => x_msg_data
1557            );
1558 
1559          -- call fnd_log api at the end of the api
1560          l_fnd_log_message := 'when expected exception raised for procedure :' ;
1561          -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1562 
1563          -- log message in trace file
1564          if g_debug= c_debug_enabled then
1565              mydebug(l_fnd_log_message,c_api_name,9);
1566          end if;
1567 
1568          -- get messages from stack and log them in fnd tables
1569          if x_msg_count = 1 then
1570             -- fnd_log_debug(fnd_log.level_error,c_module_name, x_msg_data );
1571             -- log message in trace file
1572             if g_debug= c_debug_enabled then
1573               mydebug(l_fnd_log_message,c_api_name,9);
1574             end if;
1575          elsif x_msg_count > 1 then
1576              for i in 1..x_msg_count loop
1577                fnd_msg_pub.get
1578                 (p_msg_index     => i,
1579                  p_encoded       => 'f',
1580                  p_data          => l_fnd_log_message,
1581                  p_msg_index_out => l_msg_index_out );
1582 
1583                 -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1584 
1585                 -- log message in trace file
1586                 if g_debug= c_debug_enabled then
1587                     mydebug(l_fnd_log_message,c_api_name,9);
1588                 end if;
1589              end loop ;
1590          end if;
1591 
1592 
1593      when fnd_api.g_exc_unexpected_error then
1594           x_return_status := fnd_api.g_ret_sts_unexp_error ;
1595 
1596           --  get message count and data
1597           fnd_msg_pub.count_and_get
1598             (  p_count  => x_msg_count
1599              , p_data   => x_msg_data
1600               );
1601           -- call fnd_log api at the end of the api
1602           l_fnd_log_message := 'when unexpected exception raised for procedure :';
1603           -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1604 
1605           -- log message in trace file
1606           if g_debug= c_debug_enabled then
1607               mydebug(l_fnd_log_message,c_api_name,9);
1608           end if;
1609 
1610           -- get messages from stack and log them in fnd tables
1611           if x_msg_count = 1 then
1612             -- fnd_log_debug(fnd_log.level_error,c_module_name, x_msg_data );
1613 
1614             -- log message in trace file
1615             if g_debug= c_debug_enabled then
1616               mydebug(l_fnd_log_message,c_api_name,9);
1617             end if;
1618           elsif x_msg_count > 1 then
1619              for i in 1..x_msg_count loop
1620                fnd_msg_pub.get
1621                 (p_msg_index     => i,
1622                  p_encoded       => 'f',
1623                  p_data          => l_fnd_log_message,
1624                  p_msg_index_out => l_msg_index_out );
1625 
1626                 -- fnd_log_debug(fnd_log.level_error,c_module_name, l_fnd_log_message );
1627 
1628                 -- log message in trace file
1629                 if g_debug= c_debug_enabled then
1630                     mydebug(l_fnd_log_message,c_api_name,9);
1631                 end if;
1632              end loop ;
1633           end if;
1634 
1635    when others then
1636         x_return_status := fnd_api.g_ret_sts_unexp_error ;
1637 
1638         if fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
1639           then
1640            fnd_msg_pub.add_exc_msg
1641              (  g_pkg_name
1642               , c_api_name
1643               );
1644         END IF;
1645 
1646         --  Get message count and data
1647         fnd_msg_pub.count_and_get
1648           (  p_count  => x_msg_count
1649            , p_data   => x_msg_data
1650            );
1651 
1652         -- Call fnd_log api at the end of the API
1653         l_Fnd_Log_message := 'When Others exception raised for procedure :' || G_Pkg_Name || '.' || C_API_Name ;
1654         -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
1655 
1656         -- Log message in trace file
1657         IF G_debug= C_Debug_Enabled THEN
1658             mydebug(l_Fnd_Log_Message,c_api_name,9);
1659         END IF;
1660 
1661         -- Get messages from stack and log them in fnd tables
1662         If X_Msg_Count = 1 Then
1663           -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
1664 
1665           -- Log message in trace file
1666           If G_debug= C_Debug_Enabled Then
1667              mydebug(l_Fnd_Log_Message,c_api_name,9);
1668            End If;
1669         Elsif X_Msg_Count > 1 Then
1670             For I In 1..X_Msg_Count Loop
1671               FND_MSG_PUB.Get
1672                (p_msg_index     => i,
1673                 p_encoded       => 'F',
1674                 p_data          => l_Fnd_Log_Message,
1675                 p_msg_index_out => l_msg_index_out );
1676 
1677                -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
1678 
1679                -- Log message in trace file
1680                IF G_debug= C_Debug_Enabled THEN
1681                    mydebug(l_Fnd_Log_Message,c_api_name,9);
1682                END IF;
1683             End Loop ;
1684         End If;
1685 END;
1686 
1687 --
1688 --this procedure calls inventory API query_reservation with the appropriate parameters
1689 --
1690 PROCEDURE QUERY_RES
1691     (p_supply_source_header_id  IN NUMBER DEFAULT NULL
1692     ,p_supply_source_line_id    IN NUMBER DEFAULT NULL
1693     ,p_supply_source_type_id    IN NUMBER
1694     ,p_project_id               IN NUMBER
1695     ,p_task_id                  IN NUMBER
1696     ,x_rsv_array                OUT NOCOPY inv_reservation_global.mtl_reservation_tbl_type
1697     ,x_record_count             OUT NOCOPY NUMBER
1698     ,x_msg_count                OUT NOCOPY NUMBER
1699     ,x_msg_data                 OUT NOCOPY VARCHAR2
1700     ,x_return_status            OUT NOCOPY VARCHAR2)
1701 IS
1702    -- Define Constants for API version and API name
1703    C_api_version_number       CONSTANT NUMBER       := 1.0;
1704    C_api_name                 CONSTANT VARCHAR2(30) := 'QUERY_RES';
1705    C_Module_Name              Constant Varchar2(2000) := 'inv.plsql.inv_maintain_reservation_pub.maintain_reservation';
1706    C_Debug_Enabled            Constant Number := 1 ;
1707 
1708    --l_debug                  NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1709    l_Fnd_Log_Message          VARCHAR2(2000);
1710    l_Msg_Index_Out            Number;
1711 
1712    l_rsv_rec           inv_reservation_global.mtl_reservation_rec_type;
1713    --l_msg_count         NUMBER;
1714    --l_msg_data          VARCHAR2(240);
1715    l_rsv_id            NUMBER;
1716    l_return_status     VARCHAR2(1);
1717    l_error_code        NUMBER;
1718    l_rsv_array         inv_reservation_global.mtl_reservation_tbl_type;
1719    l_record_count      NUMBER;
1720    l_error_text        VARCHAR2(2000);
1721 BEGIN
1722    l_Fnd_Log_message := 'Begining of procedure :';
1723    -- Dbms_output.Put_line('Begining Of Procedure');
1724    -- Fnd_Log_Debug(Fnd_Log.Level_Procedure, C_Module_name, l_Fnd_Log_Message);
1725 
1726    -- Log message in trace file
1727    IF G_debug= C_Debug_Enabled THEN
1728       G_Version_Printed := TRUE ;
1729       mydebug(l_Fnd_Log_Message,c_api_name,9);
1730       G_Version_Printed := FALSE ;
1731    END IF;
1732 
1733    -- Bug 3600118: Initialize return status variable
1734    l_return_status := FND_API.g_ret_sts_success;
1735 
1736    IF p_supply_source_header_id is not null THEN
1737        l_rsv_rec.supply_source_header_id := p_supply_source_header_id;   --supply header id
1738    END IF;
1739 
1740    IF p_supply_source_line_id is not null THEN
1741        l_rsv_rec.supply_source_line_id := p_supply_source_line_id;   -- supply line id
1742    END IF;
1743 
1744    IF p_supply_source_type_id IS NULL THEN
1745         FND_MESSAGE.SET_NAME('INV','INV_API_NULL_SOURCE_TYPE_ID');
1746         FND_MSG_PUB.Add;
1747         RAISE fnd_api.g_exc_unexpected_error;
1748    ELSIF p_supply_source_type_id NOT IN
1749          (inv_reservation_global.g_source_type_po,
1750           inv_reservation_global.g_source_type_internal_req,
1751           inv_reservation_global.g_source_type_req) THEN
1752         FND_MESSAGE.SET_NAME('INV','INV_API_INVALID_SOURCE_TYPE_ID');
1753         FND_MSG_PUB.Add;
1754         RAISE fnd_api.g_exc_unexpected_error;
1755    END IF;
1756 
1757    IF p_project_id is not null THEN
1758        l_rsv_rec.project_id       := p_project_id;   -- project id
1759    END IF;
1760 
1761    IF p_task_id is not null THEN
1762        l_rsv_rec.task_id          := p_task_id;   -- task id
1763    END IF;
1764 
1765    l_rsv_rec.supply_source_type_id := p_supply_source_type_id;
1766 
1767    inv_reservation_pvt.query_reservation
1768      (
1769         p_api_version_number        => 1.0
1770       , p_init_msg_lst              => fnd_api.g_false
1771       , x_return_status             => l_return_status
1772       , x_msg_count                 => x_msg_count
1773       , x_msg_data                  => x_msg_data
1774       , p_query_input               => l_rsv_rec
1775       , p_lock_records              => fnd_api.g_false
1776       , p_sort_by_req_date          => inv_reservation_global.g_query_demand_ship_date_asc
1777       , p_cancel_order_mode         => inv_reservation_global.g_cancel_order_no
1778       , x_mtl_reservation_tbl       => l_rsv_array
1779       , x_mtl_reservation_tbl_count => l_record_count
1780       , x_error_code                => l_error_code
1781       );
1782 
1783    If g_debug= C_Debug_Enabled Then
1784          l_Fnd_Log_Message := 'l_return_status: '|| l_return_status;
1785          mydebug(l_Fnd_Log_Message,c_api_name,9);
1786    End If;
1787 
1788    x_return_status := l_return_status;
1789 
1790    If x_return_status = fnd_api.g_ret_sts_success THEN
1791       l_Fnd_Log_message := 'Calling query_reservation API was successful ';
1792       -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
1793       x_rsv_array := l_rsv_array;
1794       x_record_count := l_record_count;
1795 
1796       IF G_debug= C_Debug_Enabled THEN
1797          mydebug(l_Fnd_Log_Message, c_api_name,9);
1798       END IF;
1799    else
1800       l_Fnd_Log_message := 'Error while calling query_reservation API ';
1801       -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
1802       x_record_count := -1;
1803       IF G_debug= C_Debug_Enabled THEN
1804          mydebug(l_Fnd_Log_Message, c_api_name,9);
1805       END IF;
1806    end if;
1807 
1808    -- Call fnd_log api at the end of the API
1809    l_Fnd_Log_message := 'At the end of procedure :';
1810    -- Fnd_Log_Debug(Fnd_Log.Level_Procedure,C_Module_name, l_Fnd_Log_Message );
1811 
1812    IF G_debug= C_Debug_Enabled THEN
1813          mydebug(l_Fnd_Log_Message, c_api_name,9);
1814    END IF;
1815 
1816 
1817 
1818 EXCEPTION
1819    WHEN fnd_api.g_exc_error THEN
1820         x_return_status := fnd_api.g_ret_sts_error;
1821         x_record_count := -1;
1822 
1823         --  Get message count and data
1824         fnd_msg_pub.count_and_get
1825           (  p_count => x_msg_count
1826            , p_data  => x_msg_data
1827            );
1828 
1829          -- Call fnd_log api at the end of the API
1830          l_Fnd_Log_message := 'When Expected exception raised for procedure :' ;
1831          -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
1832 
1833          -- Log message in trace file
1834          IF G_debug= C_Debug_Enabled THEN
1835              mydebug(l_Fnd_Log_Message,c_api_name,9);
1836          END IF;
1837 
1838          -- Get messages from stack and log them in fnd tables
1839          If X_Msg_Count = 1 Then
1840             -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
1841             -- Log message in trace file
1842             If G_debug= C_Debug_Enabled THEN
1843               mydebug(l_Fnd_Log_Message,c_api_name,9);
1844             End If;
1845          Elsif x_msg_count > 1 Then
1846              For I In 1..X_Msg_Count Loop
1847                FND_MSG_PUB.Get
1848                 (p_msg_index     => i,
1849                  p_encoded       => 'F',
1850                  p_data          => l_Fnd_Log_Message,
1851                  p_msg_index_out => l_msg_index_out );
1852 
1853                 -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
1854 
1855                 -- Log message in trace file
1856                 IF G_debug= C_Debug_Enabled THEN
1857                     mydebug(l_Fnd_Log_Message,c_api_name,9);
1858                 END IF;
1859              End Loop ;
1860          End If;
1861 
1862 
1863      WHEN fnd_api.g_exc_unexpected_error THEN
1864           x_return_status := fnd_api.g_ret_sts_unexp_error ;
1865           x_record_count := -1;
1866 
1867           --  Get message count and data
1868           fnd_msg_pub.count_and_get
1869             (  p_count  => x_msg_count
1870              , p_data   => x_msg_data
1871               );
1872           -- Call fnd_log api at the end of the API
1873           l_Fnd_Log_message := 'When unexpected exception raised for procedure :';
1874           -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
1875 
1876           -- Log message in trace file
1877           IF G_debug= C_Debug_Enabled THEN
1878               mydebug(l_Fnd_Log_Message,c_api_name,9);
1879           END IF;
1880 
1881           -- Get messages from stack and log them in fnd tables
1882           If X_Msg_Count = 1 Then
1883             -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
1884 
1885             -- Log message in trace file
1886             IF G_debug= C_Debug_Enabled THEN
1887               mydebug(l_Fnd_Log_Message,c_api_name,9);
1888             END IF;
1889           Elsif X_Msg_Count > 1 Then
1890              For I In 1..X_Msg_Count Loop
1891                FND_MSG_PUB.Get
1892                 (p_msg_index     => i,
1893                  p_encoded       => 'F',
1894                  p_data          => l_Fnd_Log_Message,
1895                  p_msg_index_out => l_msg_index_out );
1896 
1897                 -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
1898 
1899                 -- Log message in trace file
1900                 IF G_debug= C_Debug_Enabled THEN
1901                     mydebug(l_Fnd_Log_Message,c_api_name,9);
1902                 END IF;
1903              End Loop ;
1904           End If;
1905 
1906    WHEN OTHERS THEN
1907         x_return_status := fnd_api.g_ret_sts_unexp_error ;
1908         x_record_count := -1;
1909 
1910         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
1911           THEN
1912            fnd_msg_pub.add_exc_msg
1913              (  g_pkg_name
1914               , c_api_name
1915               );
1916         END IF;
1917 
1918         --  Get message count and data
1919         fnd_msg_pub.count_and_get
1920           (  p_count  => x_msg_count
1921            , p_data   => x_msg_data
1922            );
1923 
1924         -- Call fnd_log api at the end of the API
1925         l_Fnd_Log_message := 'When Others exception raised for procedure :' || G_Pkg_Name || '.' || C_API_Name ;
1926         -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
1927 
1928         -- Log message in trace file
1929         IF G_debug= C_Debug_Enabled THEN
1930             mydebug(l_Fnd_Log_Message,c_api_name,9);
1931         END IF;
1932 
1933         -- Get messages from stack and log them in fnd tables
1934         If X_Msg_Count = 1 Then
1935           -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
1936 
1937           -- Log message in trace file
1938           If G_debug= C_Debug_Enabled Then
1939              mydebug(l_Fnd_Log_Message,c_api_name,9);
1940            End If;
1941         Elsif X_Msg_Count > 1 Then
1942             For I In 1..X_Msg_Count Loop
1943               FND_MSG_PUB.Get
1944                (p_msg_index     => i,
1945                 p_encoded       => 'F',
1946                 p_data          => l_Fnd_Log_Message,
1947                 p_msg_index_out => l_msg_index_out );
1948 
1949                -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
1950 
1951                -- Log message in trace file
1952                IF G_debug= C_Debug_Enabled THEN
1953                    mydebug(l_Fnd_Log_Message,c_api_name,9);
1954                END IF;
1955             End Loop ;
1956      End If;
1957 END;
1958 
1959 
1960 --
1961 -- this procedure checks whether a reservation exists
1962 --
1963 FUNCTION EXISTS_RESERVATION( p_supply_source_header_id  IN NUMBER DEFAULT NULL
1964                            , p_supply_source_line_id    IN NUMBER DEFAULT NULL
1965                            , p_supply_source_type_id        IN NUMBER DEFAULT inv_reservation_global.g_source_type_po)
1966 
1967 RETURN BOOLEAN IS
1968      -- Define Constants for API version and API name
1969      C_api_version_number       CONSTANT NUMBER       := 1.0;
1970      C_api_name                 CONSTANT VARCHAR2(30) := 'EXISTS_RESERVATION';
1971      C_Module_Name              Constant Varchar2(2000) := 'inv.plsql.inv_maintain_reservation_pub.maintain_reservation';
1972      C_Debug_Enabled            Constant Number := 1 ;
1973 
1974      --l_debug                  NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1975      l_Fnd_Log_Message          VARCHAR2(2000);
1976      l_Msg_Index_Out            Number;
1977 
1978      l_rsv_array                inv_reservation_global.mtl_reservation_tbl_type;
1979      l_record_count             NUMBER;
1980      l_msg_count                NUMBER;
1981      l_msg_data                 VARCHAR2(2000);
1982      l_return_status            VARCHAR2(1);
1983 
1984 BEGIN
1985 
1986      l_Fnd_Log_message := 'Begining of procedure :';
1987      -- Dbms_output.Put_line('Begining Of Procedure');
1988      -- Fnd_Log_Debug(Fnd_Log.Level_Procedure, C_Module_name, l_Fnd_Log_Message);
1989 
1990      -- Log message in trace file
1991      IF G_debug= C_Debug_Enabled THEN
1992         G_Version_Printed := TRUE ;
1993         mydebug(l_Fnd_Log_Message,c_api_name,9);
1994         G_Version_Printed := FALSE ;
1995      END IF;
1996 
1997      QUERY_RES
1998         ( p_supply_source_header_id => p_supply_source_header_id
1999          ,p_supply_source_line_id   => p_supply_source_line_id
2000          ,p_supply_source_type_id   => p_supply_source_type_id
2001          ,p_project_id              => NULL
2002          ,p_task_id                 => NULL
2003          ,x_rsv_array               => l_rsv_array
2004          ,x_record_count            => l_record_count
2005          ,x_msg_count               => l_msg_count
2006          ,x_msg_data                => l_msg_data
2007          ,x_return_status           => l_return_status);
2008 
2009      If g_debug= C_Debug_Enabled Then
2010            l_Fnd_Log_Message := 'l_return_status: '|| l_return_status;
2011            mydebug(l_Fnd_Log_Message,c_api_name,9);
2012      End If;
2013 
2014 
2015      If l_return_status = fnd_api.g_ret_sts_success THEN
2016         l_Fnd_Log_message := 'Calling QUERY_RES API was successful ';
2017         -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2018         IF G_debug= C_Debug_Enabled THEN
2019            mydebug(l_Fnd_Log_Message, c_api_name,9);
2020         END IF;
2021 
2022         IF l_record_count > 0 THEN
2023                return TRUE;
2024         ELSE
2025                return FALSE;
2026         END IF;
2027         --?? what should be returned if l_return_status is not 'S'.
2028         -- should we return fales. the reason is
2029         -- if a reservation is existing, .... then we think it is not,
2030         --  then we were trying to transfer from req to PO, which will fail.
2031         -- if a reservation is not existing, then no impact..
2032         -- or if we returns TRUE.  THEN
2033         -- if a reservation exists, then no impact....
2034         -- if a reservation is not existing, then we will update the reservation...
2035      else
2036         l_Fnd_Log_message := 'Error while calling QUERY_RES API ';
2037         -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2038         IF G_debug= C_Debug_Enabled THEN
2039            mydebug(l_Fnd_Log_Message, c_api_name,9);
2040         END IF;
2041         return FALSE;
2042      end if;
2043 
2044      -- Call fnd_log api at the end of the API
2045      l_Fnd_Log_message := 'At the end of procedure :';
2046      -- Fnd_Log_Debug(Fnd_Log.Level_Procedure,C_Module_name, l_Fnd_Log_Message );
2047 
2048      IF G_debug= C_Debug_Enabled THEN
2049            mydebug(l_Fnd_Log_Message, c_api_name,9);
2050      END IF;
2051 
2052 
2053 EXCEPTION
2054     WHEN OTHERS THEN
2055         l_Fnd_Log_message := 'OTHER exception happens when calling QUERY_RES API :';
2056         -- Fnd_Log_Debug(Fnd_Log.Level_Procedure,C_Module_name, l_Fnd_Log_Message );
2057 
2058         IF G_debug= C_Debug_Enabled THEN
2059               mydebug(l_Fnd_Log_Message, c_api_name,9);
2060         END IF;
2061         return FALSE;
2062 END;
2063 
2064 
2065 -- this procedure finds the reservation on the requisition and returns the reservation records
2066 -- so far we should only have one record returned for gaven requisition_line_id.
2067 PROCEDURE GET_REQ_LINE_RES
2068 (
2069   p_req_line_id           IN  NUMBER
2070  ,p_project_id            IN  NUMBER
2071  ,p_task_id               IN  NUMBER
2072  ,x_res_array             OUT NOCOPY inv_reservation_global.mtl_reservation_tbl_type
2073  ,x_record_count          OUT NOCOPY NUMBER
2074 )
2075 IS
2076     -- Define Constants for API version and API name
2077     C_api_version_number       CONSTANT NUMBER       := 1.0;
2078     C_api_name                 CONSTANT VARCHAR2(30) := 'GET_REQ_LINE_RES';
2079     C_Module_Name              Constant Varchar2(2000) := 'inv.plsql.inv_maintain_reservation_pub.maintain_reservation';
2080     C_Debug_Enabled            Constant Number := 1 ;
2081 
2082     --l_debug                  NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2083     l_Fnd_Log_Message          VARCHAR2(2000);
2084     l_Msg_Index_Out            Number;
2085 
2086     l_msg_count                NUMBER;
2087     l_msg_data                 VARCHAR2(2000);
2088     l_record_count             NUMBER;
2089     l_return_status            VARCHAR2(1);
2090 
2091 BEGIN
2092       -- this procedure checks whether a reservation exists on the req line
2093       -- if so, it returns the reservation record
2094 
2095       l_Fnd_Log_message := 'Begining of procedure :';
2096       -- Dbms_output.Put_line('Begining Of Procedure');
2097       -- Fnd_Log_Debug(Fnd_Log.Level_Procedure, C_Module_name, l_Fnd_Log_Message);
2098 
2099       -- Log message in trace file
2100       IF G_debug= C_Debug_Enabled THEN
2101          G_Version_Printed := TRUE ;
2102          mydebug(l_Fnd_Log_Message,c_api_name,9);
2103          G_Version_Printed := FALSE ;
2104       END IF;
2105 
2106       -- Bug 3600118: Initialize return status variable
2107       --l_return_status := FND_API.g_ret_sts_success;
2108       --
2109       -- bug fix 2341308
2110       if p_req_line_id IS NULL then
2111          l_Fnd_Log_message := 'p_req_line_id is NULL :';
2112          IF G_debug= C_Debug_Enabled THEN
2113             mydebug(l_Fnd_Log_Message,c_api_name,9);
2114          END IF;
2115          x_record_count := -1;
2116          return;
2117       end if;
2118 
2119 
2120       QUERY_RES
2121           (p_supply_source_line_id   => p_req_line_id
2122           ,p_supply_source_type_id   => inv_reservation_global.g_source_type_req
2123           ,p_project_id              => p_project_id
2124           ,p_task_id                 => p_task_id
2125           ,x_rsv_array               => x_res_array
2126           ,x_record_count            => l_record_count
2127           ,x_msg_count               => l_msg_count
2128           ,x_msg_data                => l_msg_data
2129           ,x_return_status           => l_return_status);
2130 
2131 
2132       If g_debug= C_Debug_Enabled Then
2133             l_Fnd_Log_Message := 'l_return_status: '|| l_return_status;
2134             mydebug(l_Fnd_Log_Message,c_api_name,9);
2135       End If;
2136 
2137       If l_return_status = fnd_api.g_ret_sts_success THEN
2138          l_Fnd_Log_message := 'Calling QUERY_RES API was successful ';
2139          -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2140          IF G_debug= C_Debug_Enabled THEN
2141             mydebug(l_Fnd_Log_Message, c_api_name,9);
2142          END IF;
2143          x_record_count := l_record_count;
2144       else
2145          l_Fnd_Log_message := 'Error while calling QUERY_RES API ';
2146          -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2147          IF G_debug= C_Debug_Enabled THEN
2148             mydebug(l_Fnd_Log_Message, c_api_name,9);
2149          END IF;
2150          x_record_count := -1;
2151       end if;
2152 
2153       -- Call fnd_log api at the end of the API
2154       l_Fnd_Log_message := 'At the end of procedure :';
2155       -- -- Fnd_Log_Debug(Fnd_Log.Level_Procedure,C_Module_name, l_Fnd_Log_Message );
2156 
2157       IF G_debug= C_Debug_Enabled THEN
2158             mydebug(l_Fnd_Log_Message, c_api_name,9);
2159       END IF;
2160 
2161 EXCEPTION
2162     WHEN OTHERS THEN
2163        x_record_count := -1;
2164        l_Fnd_Log_message := ' OTHER exceptions happens ';
2165        IF G_debug= C_Debug_Enabled THEN
2166               mydebug(l_Fnd_Log_Message, c_api_name,9);
2167        END IF;
2168 END;
2169 
2170 
2171 --
2172 -- this procedure calls the inventory API update_reservation with the appropriate parameters
2173 --
2174 PROCEDURE UPDATE_RES
2175    (p_supply_source_header_id       IN NUMBER
2176    ,p_supply_source_line_id         IN NUMBER
2177    ,p_supply_source_type_id         IN NUMBER
2178    ,p_primary_uom_code              IN VARCHAR2 DEFAULT NULL
2179    ,p_primary_reservation_quantity  IN NUMBER
2180    ,p_reservation_id                IN NUMBER
2181    ,p_project_id                    IN NUMBER
2182    ,p_task_id                       IN NUMBER
2183    ,x_msg_count                     OUT NOCOPY NUMBER
2184    ,x_msg_data                      OUT NOCOPY VARCHAR2
2185    ,x_return_status                 OUT NOCOPY VARCHAR2)
2186 IS
2187 
2188    -- Define Constants for API version and API name
2189    C_api_version_number       CONSTANT NUMBER       := 1.0;
2190    C_api_name                 CONSTANT VARCHAR2(30) := 'UPDATE_RES';
2191    C_Module_Name              Constant Varchar2(2000) := 'inv.plsql.inv_maintain_reservation_pub.maintain_reservation';
2192    C_Debug_Enabled            Constant Number := 1 ;
2193 
2194    --l_debug                  NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2195    l_Fnd_Log_Message          VARCHAR2(2000);
2196    l_Msg_Index_Out            Number;
2197 
2198    l_rsv              inv_reservation_global.mtl_reservation_rec_type;
2199    l_rsv_new          inv_reservation_global.mtl_reservation_rec_type;
2200    --l_msg_count        NUMBER;
2201    --l_msg_data         VARCHAR2(240);
2202    l_rsv_id           NUMBER;
2203    l_return_status    VARCHAR2(1);
2204 BEGIN
2205    l_Fnd_Log_message := 'Begining of procedure :';
2206    -- Dbms_output.Put_line('Begining Of Procedure');
2207    -- Fnd_Log_Debug(Fnd_Log.Level_Procedure, C_Module_name, l_Fnd_Log_Message);
2208 
2209    -- Log message in trace file
2210    IF G_debug= C_Debug_Enabled THEN
2211       G_Version_Printed := TRUE ;
2212       mydebug(l_Fnd_Log_Message,c_api_name,9);
2213       G_Version_Printed := FALSE ;
2214    END IF;
2215 
2216    -- Bug 3600118: Initialize return status variable
2217    l_return_status := FND_API.g_ret_sts_success;
2218 
2219    -- find the existing reservation
2220    -- REQ or PO id's
2221    l_rsv.supply_source_header_id := p_supply_source_header_id;
2222    l_rsv.supply_source_line_id   := p_supply_source_line_id;
2223    l_rsv.supply_source_type_id   := p_supply_source_type_id;
2224    l_rsv.project_id              := p_project_id;
2225    l_rsv.task_id                 := p_task_id;
2226 
2227    IF p_reservation_id IS NOT NULL THEN
2228       l_rsv.reservation_id := p_reservation_id;
2229    END IF;
2230 
2231 -- specify the new values
2232    l_rsv_new.primary_reservation_quantity := p_primary_reservation_quantity;
2233 --
2234    inv_reservation_pub.update_reservation
2235      (
2236         p_api_version_number        => 1.0
2237       , p_init_msg_lst              => fnd_api.g_false
2238       , x_return_status             => l_return_status
2239       , x_msg_count                 => x_msg_count
2240       , x_msg_data                  => x_msg_data
2241       , p_original_rsv_rec          => l_rsv
2242       , p_to_rsv_rec                => l_rsv_new
2243       , p_original_serial_number    => g_dummy_sn_tbl -- no serial contorl
2244       , p_to_serial_number          => g_dummy_sn_tbl -- no serial control
2245       , p_validation_flag           => fnd_api.g_true
2246       );
2247 
2248    If g_debug= C_Debug_Enabled Then
2249          l_Fnd_Log_Message := 'l_return_status: '|| l_return_status;
2250          mydebug(l_Fnd_Log_Message,c_api_name,9);
2251    End If;
2252 
2253    x_return_status := l_return_status;
2254 
2255    If x_return_status = fnd_api.g_ret_sts_success THEN
2256       l_Fnd_Log_message := 'Calling update_reservation API was successful ';
2257       -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2258       IF G_debug= C_Debug_Enabled THEN
2259          mydebug(l_Fnd_Log_Message, c_api_name,9);
2260       END IF;
2261    else
2262       l_Fnd_Log_message := 'Error while calling update_reservation API ';
2263       -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2264       IF G_debug= C_Debug_Enabled THEN
2265          mydebug(l_Fnd_Log_Message, c_api_name,9);
2266       END IF;
2267    end if;
2268 
2269    -- Call fnd_log api at the end of the API
2270    l_Fnd_Log_message := 'At the end of procedure :';
2271    -- -- Fnd_Log_Debug(Fnd_Log.Level_Procedure,C_Module_name, l_Fnd_Log_Message );
2272 
2273    IF G_debug= C_Debug_Enabled THEN
2274          mydebug(l_Fnd_Log_Message, c_api_name,9);
2275    END IF;
2276 
2277 
2278 EXCEPTION
2279    WHEN fnd_api.g_exc_error THEN
2280         x_return_status := fnd_api.g_ret_sts_error;
2281 
2282         --  Get message count and data
2283         fnd_msg_pub.count_and_get
2284           (  p_count => x_msg_count
2285            , p_data  => x_msg_data
2286            );
2287 
2288          -- Call fnd_log api at the end of the API
2289          l_Fnd_Log_message := 'When Expected exception raised for procedure :' ;
2290          -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2291 
2292          -- Log message in trace file
2293          IF G_debug= C_Debug_Enabled THEN
2294              mydebug(l_Fnd_Log_Message,c_api_name,9);
2295          END IF;
2296 
2297          -- Get messages from stack and log them in fnd tables
2298          If X_Msg_Count = 1 Then
2299             -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
2300             -- Log message in trace file
2301             If G_debug= C_Debug_Enabled THEN
2302               mydebug(l_Fnd_Log_Message,c_api_name,9);
2303             End If;
2304          Elsif x_msg_count > 1 Then
2305              For I In 1..X_Msg_Count Loop
2306                FND_MSG_PUB.Get
2307                 (p_msg_index     => i,
2308                  p_encoded       => 'F',
2309                  p_data          => l_Fnd_Log_Message,
2310                  p_msg_index_out => l_msg_index_out );
2311 
2312                 -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2313 
2314                 -- Log message in trace file
2315                 IF G_debug= C_Debug_Enabled THEN
2316                     mydebug(l_Fnd_Log_Message,c_api_name,9);
2317                 END IF;
2318              End Loop ;
2319          End If;
2320 
2321 
2322      WHEN fnd_api.g_exc_unexpected_error THEN
2323           x_return_status := fnd_api.g_ret_sts_unexp_error ;
2324 
2325           --  Get message count and data
2326           fnd_msg_pub.count_and_get
2327             (  p_count  => x_msg_count
2328              , p_data   => x_msg_data
2329               );
2330           -- Call fnd_log api at the end of the API
2331           l_Fnd_Log_message := 'When unexpected exception raised for procedure :';
2332           -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2333 
2334           -- Log message in trace file
2335           IF G_debug= C_Debug_Enabled THEN
2336               mydebug(l_Fnd_Log_Message,c_api_name,9);
2337           END IF;
2338 
2339           -- Get messages from stack and log them in fnd tables
2340           If X_Msg_Count = 1 Then
2341             -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
2342 
2343             -- Log message in trace file
2344             IF G_debug= C_Debug_Enabled THEN
2345               mydebug(l_Fnd_Log_Message,c_api_name,9);
2346             END IF;
2347           Elsif X_Msg_Count > 1 Then
2348              For I In 1..X_Msg_Count Loop
2349                FND_MSG_PUB.Get
2350                 (p_msg_index     => i,
2351                  p_encoded       => 'F',
2352                  p_data          => l_Fnd_Log_Message,
2353                  p_msg_index_out => l_msg_index_out );
2354 
2355                 -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2356 
2357                 -- Log message in trace file
2358                 IF G_debug= C_Debug_Enabled THEN
2359                     mydebug(l_Fnd_Log_Message,c_api_name,9);
2360                 END IF;
2361              End Loop ;
2362           End If;
2363 
2364    WHEN OTHERS THEN
2365         x_return_status := fnd_api.g_ret_sts_unexp_error ;
2366 
2367         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
2368           THEN
2369            fnd_msg_pub.add_exc_msg
2370              (  g_pkg_name
2371               , c_api_name
2372               );
2373         END IF;
2374 
2375         --  Get message count and data
2376         fnd_msg_pub.count_and_get
2377           (  p_count  => x_msg_count
2378            , p_data   => x_msg_data
2379            );
2380 
2381         -- Call fnd_log api at the end of the API
2382         l_Fnd_Log_message := 'When Others exception raised for procedure :' || G_Pkg_Name || '.' || C_API_Name ;
2383         -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2384 
2385         -- Log message in trace file
2386         IF G_debug= C_Debug_Enabled THEN
2387             mydebug(l_Fnd_Log_Message,c_api_name,9);
2388         END IF;
2389 
2390         -- Get messages from stack and log them in fnd tables
2391         If X_Msg_Count = 1 Then
2392           -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
2393 
2394           -- Log message in trace file
2395           If G_debug= C_Debug_Enabled Then
2396              mydebug(l_Fnd_Log_Message,c_api_name,9);
2397            End If;
2398         Elsif X_Msg_Count > 1 Then
2399             For I In 1..X_Msg_Count Loop
2400               FND_MSG_PUB.Get
2401                (p_msg_index     => i,
2402                 p_encoded       => 'F',
2403                 p_data          => l_Fnd_Log_Message,
2404                 p_msg_index_out => l_msg_index_out );
2405 
2406                -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2407 
2408                -- Log message in trace file
2409                IF G_debug= C_Debug_Enabled THEN
2410                    mydebug(l_Fnd_Log_Message,c_api_name,9);
2411                END IF;
2412             End Loop ;
2413        End If;
2414 END;
2415 
2416 
2417 
2418 --
2419 -- this procedure calls the inventory API transfer_reservation with the appropriate parameters
2420 --
2421 PROCEDURE TRANSFER_RES
2422 (p_from_reservation_id       IN NUMBER    DEFAULT NULL
2423 ,p_from_source_header_id     IN NUMBER    DEFAULT NULL
2424 ,p_from_source_line_id       IN NUMBER    DEFAULT NULL
2425 ,p_supply_source_type_id     IN NUMBER    DEFAULT NULL
2426 ,p_to_source_header_id       IN NUMBER    DEFAULT NULL
2427 ,p_to_source_line_id         IN NUMBER    DEFAULT NULL
2428 ,p_to_supply_source_type_id  IN NUMBER    DEFAULT NULL
2429 ,p_subinventory_code         IN VARCHAR2  DEFAULT NULL
2430 ,p_locator_id                IN NUMBER    DEFAULT NULL
2431 ,p_lot_number                IN VARCHAR2  DEFAULT NULL
2432 ,p_revision                  IN VARCHAR2  DEFAULT NULL
2433 ,p_lpn_id                    IN VARCHAR2  DEFAULT NULL --#Bug3020166
2434 ,p_primary_uom_code          IN VARCHAR2  DEFAULT NULL
2435 ,p_primary_res_quantity      IN NUMBER    DEFAULT NULL
2436 ,p_secondary_uom_code        IN VARCHAR2  DEFAULT NULL
2437 ,p_secondary_res_quantity    IN NUMBER    DEFAULT NULL
2438 ,x_msg_count                OUT NOCOPY    NUMBER
2439 ,x_msg_data                 OUT NOCOPY    VARCHAR2
2440 ,x_return_status            OUT NOCOPY    VARCHAR2)
2441 IS
2442 
2443    -- Define Constants for API version and API name
2444    C_api_version_number       CONSTANT NUMBER       := 1.0;
2445    C_api_name                 CONSTANT VARCHAR2(30) := 'TRANSFER_RES';
2446    C_Module_Name              Constant Varchar2(2000) := 'inv.plsql.inv_maintain_reservation_pub.maintain_reservation';
2447    C_Debug_Enabled            Constant Number := 1 ;
2448 
2449    --l_debug                    NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2450    l_Fnd_Log_Message          VARCHAR2(2000);
2451    l_Msg_Index_Out            Number;
2452 
2453    l_rsv         inv_reservation_global.mtl_reservation_rec_type;
2454    l_rsv_new     inv_reservation_global.mtl_reservation_rec_type;
2455    --l_msg_count   NUMBER;
2456    --l_msg_data    VARCHAR2(240);
2457    l_rsv_id         NUMBER;
2458    l_return_status  VARCHAR2(1);
2459    l_new_rsv_id     NUMBER;
2460 BEGIN
2461    l_Fnd_Log_message := 'Begining of procedure :';
2462    -- Dbms_output.Put_line('Begining Of Procedure');
2463    -- Fnd_Log_Debug(Fnd_Log.Level_Procedure, C_Module_name, l_Fnd_Log_Message);
2464 
2465    -- Log message in trace file
2466    IF G_debug= C_Debug_Enabled THEN
2467       G_Version_Printed := TRUE ;
2468       mydebug(l_Fnd_Log_Message,c_api_name,9);
2469       G_Version_Printed := FALSE ;
2470    END IF;
2471 
2472    -- Bug 3600118: Initialize return status variable
2473    l_return_status := FND_API.g_ret_sts_success;
2474 
2475    -- print out IN parameter values
2476    IF g_debug= C_Debug_Enabled THEN
2477      l_Fnd_Log_Message := 'p_from_reservation_id:' || p_from_reservation_id;
2478      mydebug(l_Fnd_Log_Message, c_api_name,9);
2479       l_Fnd_Log_Message := 'p_from_source_header_id:' || p_from_source_header_id;
2480          mydebug(l_Fnd_Log_Message, c_api_name,9);
2481          l_Fnd_Log_Message := 'p_from_source_line_id: ' || p_from_source_line_id;
2482          mydebug(l_Fnd_Log_Message, c_api_name,9);
2483          l_Fnd_Log_Message := 'p_supply_source_type_id :'||p_supply_source_type_id;
2484          mydebug(l_Fnd_Log_Message, c_api_name,9);
2485          l_Fnd_Log_Message := 'p_to_source_header_id :'||p_to_source_header_id;
2486          mydebug(l_Fnd_Log_Message, c_api_name,9);
2487          l_Fnd_Log_Message := 'p_to_source_line_id: '||p_to_source_line_id;
2488          mydebug(l_Fnd_Log_Message, c_api_name,9);
2489          l_Fnd_Log_Message := 'p_to_supply_source_type_id:'||p_to_supply_source_type_id;
2490          mydebug(l_Fnd_Log_Message, c_api_name,9);
2491          l_Fnd_Log_Message := 'p_subinventory_code:'||p_subinventory_code;
2492          mydebug(l_Fnd_Log_Message, c_api_name,9);
2493          l_Fnd_Log_Message := 'p_locator_id:'||p_locator_id;
2494          mydebug(l_Fnd_Log_Message, c_api_name,9);
2495          l_Fnd_Log_Message := 'p_lot_number:'||p_lot_number;
2496          mydebug(l_Fnd_Log_Message, c_api_name,9);
2497          l_Fnd_Log_Message := 'p_revision:'||p_revision;
2498          mydebug(l_Fnd_Log_Message, c_api_name,9);
2499          l_Fnd_Log_Message := 'p_lpn_id:'||p_lpn_id;
2500          mydebug(l_Fnd_Log_Message, c_api_name,9);
2501          l_Fnd_Log_Message := 'p_primary_uom_code: ' || p_primary_uom_code;
2502          mydebug(l_Fnd_Log_Message, c_api_name,9);
2503          l_Fnd_Log_Message := 'p_primary_res_quantity : '|| p_primary_res_quantity;
2504          mydebug(l_Fnd_Log_Message, c_api_name,9);
2505          l_Fnd_Log_Message := 'p_secondary_uom_code: ' || p_secondary_uom_code;
2506          mydebug(l_Fnd_Log_Message, c_api_name,9);
2507          l_Fnd_Log_Message := 'p_secondary_res_quantity : '|| p_secondary_res_quantity;
2508          mydebug(l_Fnd_Log_Message, c_api_name,9);
2509    END IF;
2510 
2511    -- REQ or PO id's
2512 
2513    l_rsv.supply_source_header_id      := p_from_source_header_id;
2514    l_rsv.supply_source_line_id        := p_from_source_line_id ;
2515    l_rsv.supply_source_type_id        := p_supply_source_type_id;
2516 
2517    -- specify other new values  if any
2518    l_rsv_new.supply_source_header_id      := p_to_source_header_id;
2519    l_rsv_new.supply_source_line_id        := p_to_source_line_id ;
2520    l_rsv_new.supply_source_type_id        := p_to_supply_source_type_id;
2521    l_rsv_new.primary_reservation_quantity := p_primary_res_quantity;
2522 
2523    IF p_secondary_uom_code IS NOT NULL THEN
2524       l_rsv_new.secondary_reservation_quantity := p_secondary_res_quantity;
2525       l_rsv_new.secondary_uom_code             := p_secondary_uom_code;
2526    END IF;
2527 
2528 -- these values will be available for a transfer from PO to inventory (receipt)
2529 
2530    IF p_from_reservation_id IS NOT NULL THEN
2531       l_rsv.reservation_id := p_from_reservation_id;
2532    END IF;
2533 
2534    IF p_subinventory_code is not NULL THEN
2535       l_rsv_new.subinventory_code            := p_subinventory_code;
2536    END IF;
2537 
2538    IF p_locator_id is not NULL THEN
2539       l_rsv_new.locator_id                   := p_locator_id;
2540    END IF;
2541 
2542    IF p_lot_number is not NULL THEN
2543       l_rsv_new.lot_number                   := p_lot_number;
2544    END IF;
2545 
2546    IF p_revision is not NULL THEN
2547       l_rsv_new.revision                     := p_revision;
2548    END IF;
2549 
2550 /*Bug 3020166 Assigning p_lpn_id got from rcv_transactions to the lpn_id in the
2551   record type */
2552    IF p_lpn_id is not NULL THEN
2553       l_rsv_new.lpn_id                       := p_lpn_id;
2554    END IF;
2555 
2556    inv_reservation_pvt.transfer_reservation
2557      (
2558         p_api_version_number        => 1.0
2559       , p_init_msg_lst              => fnd_api.g_false
2560       , x_return_status             => l_return_status
2561       , x_msg_count                 => x_msg_count
2562       , x_msg_data                  => x_msg_data
2563       --, p_is_transfer_supply        => fnd_api.g_true
2564       , p_original_rsv_rec          => l_rsv
2565       , p_to_rsv_rec                => l_rsv_new
2566       , p_original_serial_number    => g_dummy_sn_tbl -- no serial contorl
2567      -- , p_to_serial_number          => g_dummy_sn_tbl -- no serial control
2568       , p_validation_flag           => fnd_api.g_true
2569       , x_reservation_id            => l_new_rsv_id
2570       );
2571 
2572    If g_debug= C_Debug_Enabled Then
2573           l_Fnd_Log_Message := 'l_return_status: '|| l_return_status;
2574           mydebug(l_Fnd_Log_Message,c_api_name,9);
2575    End If;
2576 
2577    x_return_status := l_return_status;
2578 
2579    If x_return_status = fnd_api.g_ret_sts_success THEN
2580       l_Fnd_Log_message := 'Calling transfer_reservation API was successful ';
2581       -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2582       IF G_debug= C_Debug_Enabled THEN
2583          mydebug(l_Fnd_Log_Message, c_api_name,9);
2584       END IF;
2585    else
2586       l_Fnd_Log_message := 'Error while calling transfer_reservation API ';
2587       -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2588       IF G_debug= C_Debug_Enabled THEN
2589          mydebug(l_Fnd_Log_Message, c_api_name,9);
2590       END IF;
2591    end if;
2592 
2593    -- Call fnd_log api at the end of the API
2594    l_Fnd_Log_message := 'At the end of procedure :';
2595    -- -- Fnd_Log_Debug(Fnd_Log.Level_Procedure,C_Module_name, l_Fnd_Log_Message );
2596 
2597    IF G_debug= C_Debug_Enabled THEN
2598          mydebug(l_Fnd_Log_Message, c_api_name,9);
2599    END IF;
2600 
2601 EXCEPTION
2602    WHEN fnd_api.g_exc_error THEN
2603        x_return_status := fnd_api.g_ret_sts_error;
2604 
2605        --  Get message count and data
2606        fnd_msg_pub.count_and_get
2607          (  p_count => x_msg_count
2608           , p_data  => x_msg_data
2609           );
2610 
2611         -- Call fnd_log api at the end of the API
2612         l_Fnd_Log_message := 'When Expected exception raised for procedure :' ;
2613         -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2614 
2615         -- Log message in trace file
2616         IF G_debug= C_Debug_Enabled THEN
2617             mydebug(l_Fnd_Log_Message,c_api_name,9);
2618         END IF;
2619 
2620         -- Get messages from stack and log them in fnd tables
2621         If X_Msg_Count = 1 Then
2622            -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
2623            -- Log message in trace file
2624            If G_debug= C_Debug_Enabled THEN
2625              mydebug(l_Fnd_Log_Message,c_api_name,9);
2626            End If;
2627         Elsif x_msg_count > 1 Then
2628             For I In 1..X_Msg_Count Loop
2629               FND_MSG_PUB.Get
2630                (p_msg_index     => i,
2631                 p_encoded       => 'F',
2632                 p_data          => l_Fnd_Log_Message,
2633                 p_msg_index_out => l_msg_index_out );
2634 
2635                -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2636 
2637                -- Log message in trace file
2638                IF G_debug= C_Debug_Enabled THEN
2639                    mydebug(l_Fnd_Log_Message,c_api_name,9);
2640                END IF;
2641             End Loop ;
2642         End If;
2643 
2644     WHEN fnd_api.g_exc_unexpected_error THEN
2645          x_return_status := fnd_api.g_ret_sts_unexp_error ;
2646 
2647          --  Get message count and data
2648          fnd_msg_pub.count_and_get
2649            (  p_count  => x_msg_count
2650             , p_data   => x_msg_data
2651              );
2652          -- Call fnd_log api at the end of the API
2653          l_Fnd_Log_message := 'When unexpected exception raised for procedure :';
2654          -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2655 
2656          -- Log message in trace file
2657          IF G_debug= C_Debug_Enabled THEN
2658              mydebug(l_Fnd_Log_Message,c_api_name,9);
2659          END IF;
2660 
2661          -- Get messages from stack and log them in fnd tables
2662          If X_Msg_Count = 1 Then
2663            -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
2664 
2665            -- Log message in trace file
2666            IF G_debug= C_Debug_Enabled THEN
2667              mydebug(l_Fnd_Log_Message,c_api_name,9);
2668            END IF;
2669          Elsif X_Msg_Count > 1 Then
2670             For I In 1..X_Msg_Count Loop
2671               FND_MSG_PUB.Get
2672                (p_msg_index     => i,
2673                 p_encoded       => 'F',
2674                 p_data          => l_Fnd_Log_Message,
2675                 p_msg_index_out => l_msg_index_out );
2676 
2677                -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2678 
2679                -- Log message in trace file
2680                IF G_debug= C_Debug_Enabled THEN
2681                    mydebug(l_Fnd_Log_Message,c_api_name,9);
2682                END IF;
2683             End Loop ;
2684          End If;
2685 
2686   WHEN OTHERS THEN
2687        x_return_status := fnd_api.g_ret_sts_unexp_error ;
2688 
2689        IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
2690          THEN
2691           fnd_msg_pub.add_exc_msg
2692             (  g_pkg_name
2693              , c_api_name
2694              );
2695        END IF;
2696 
2697        --  Get message count and data
2698        fnd_msg_pub.count_and_get
2699          (  p_count  => x_msg_count
2700           , p_data   => x_msg_data
2701           );
2702 
2703        -- Call fnd_log api at the end of the API
2704        l_Fnd_Log_message := 'When Others exception raised for procedure :' || G_Pkg_Name || '.' || C_API_Name ;
2705        -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2706 
2707        -- Log message in trace file
2708        IF G_debug= C_Debug_Enabled THEN
2709            mydebug(l_Fnd_Log_Message,c_api_name,9);
2710        END IF;
2711 
2712        -- Get messages from stack and log them in fnd tables
2713        If X_Msg_Count = 1 Then
2714          -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
2715 
2716          -- Log message in trace file
2717          If G_debug= C_Debug_Enabled Then
2718             mydebug(l_Fnd_Log_Message,c_api_name,9);
2719           End If;
2720        Elsif X_Msg_Count > 1 Then
2721            For I In 1..X_Msg_Count Loop
2722              FND_MSG_PUB.Get
2723               (p_msg_index     => i,
2724                p_encoded       => 'F',
2725                p_data          => l_Fnd_Log_Message,
2726                p_msg_index_out => l_msg_index_out );
2727 
2728               -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2729 
2730               -- Log message in trace file
2731               IF G_debug= C_Debug_Enabled THEN
2732                   mydebug(l_Fnd_Log_Message,c_api_name,9);
2733               END IF;
2734            End Loop ;
2735       End If;
2736 END;
2737 
2738 
2739 --
2740 -- this procedure calls inventory API delete_reservation with the appropriate parameters
2741 --
2742 PROCEDURE DELETE_RES
2743 (p_supply_source_header_id  IN NUMBER DEFAULT NULL
2744 ,p_supply_source_line_id    IN NUMBER DEFAULT NULL
2745 ,p_supply_source_type_id    IN NUMBER DEFAULT NULL
2746 ,x_msg_count               OUT NOCOPY NUMBER
2747 ,x_msg_data                OUT NOCOPY VARCHAR2
2748 ,x_return_status           OUT NOCOPY VARCHAR2)
2749 IS
2750    -- Define Constants for API version and API name
2751    C_api_version_number       CONSTANT NUMBER       := 1.0;
2752    C_api_name                 CONSTANT VARCHAR2(30) := 'DELETE_RES';
2753    C_Module_Name              Constant Varchar2(2000) := 'inv.plsql.inv_maintain_reservation_pub.maintain_reservation';
2754    C_Debug_Enabled            Constant Number := 1 ;
2755 
2756    --l_debug                    NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2757    l_Fnd_Log_Message          VARCHAR2(2000);
2758    l_Msg_Index_Out            Number;
2759 
2760    l_rsv       inv_reservation_global.mtl_reservation_rec_type;
2761    --l_msg_count NUMBER;
2762    --l_msg_data  VARCHAR2(240);
2763    l_rsv_id    NUMBER;
2764    --l_dummy_sn  inv_reservation_global.serial_number_tbl_type;
2765    l_return_status   VARCHAR2(1);
2766 BEGIN
2767    l_Fnd_Log_message := 'Begining of procedure :';
2768    -- Dbms_output.Put_line('Begining Of Procedure');
2769    -- Fnd_Log_Debug(Fnd_Log.Level_Procedure, C_Module_name, l_Fnd_Log_Message);
2770 
2771    -- Log message in trace file
2772    IF G_debug= C_Debug_Enabled THEN
2773       G_Version_Printed := TRUE ;
2774       mydebug(l_Fnd_Log_Message,c_api_name,9);
2775       G_Version_Printed := FALSE ;
2776    END IF;
2777 
2778    -- Bug 3600118: Initialize return status variable
2779    l_return_status := FND_API.g_ret_sts_success;
2780 
2781    -- print out IN parameter values
2782    IF G_debug= C_Debug_Enabled THEN
2783         l_Fnd_Log_Message := 'p_supply_source_header_id: '|| p_supply_source_header_id;
2784         mydebug(l_Fnd_Log_Message, c_api_name,9);
2785         l_Fnd_Log_Message := 'p_supply_source_line_id: '|| p_supply_source_line_id;
2786         mydebug(l_Fnd_Log_Message, c_api_name,9);
2787         l_Fnd_Log_Message := 'p_supply_source_type_id: '|| p_supply_source_type_id;
2788         mydebug(l_Fnd_Log_Message, c_api_name,9);
2789    END IF;
2790 
2791    -- REQ or PO id's
2792    l_rsv.supply_source_type_id := p_supply_source_type_id;
2793    IF p_supply_source_header_id IS NOT NULL THEN
2794      l_rsv.supply_source_header_id := p_supply_source_header_id;
2795    END IF;
2796 
2797    IF p_supply_source_line_id IS NOT NULL THEN
2798      l_rsv.supply_source_line_id  := p_supply_source_line_id;
2799    END IF;
2800 --
2801    inv_reservation_pub.delete_reservation
2802      (
2803         p_api_version_number        => 1.0
2804       , p_init_msg_lst              => fnd_api.g_false
2805       , x_return_status             => l_return_status
2806       , x_msg_count                 => x_msg_count
2807       , x_msg_data                  => x_msg_data
2808       , p_rsv_rec                   => l_rsv
2809       , p_serial_number             => g_dummy_sn_tbl
2810       );
2811 
2812    If g_debug= C_Debug_Enabled Then
2813           l_Fnd_Log_Message := 'l_return_status: '|| l_return_status;
2814           mydebug(l_Fnd_Log_Message,c_api_name,9);
2815    End If;
2816 
2817    x_return_status := l_return_status;
2818 
2819    If x_return_status = fnd_api.g_ret_sts_success THEN
2820       l_Fnd_Log_message := 'Calling delete_reservation API was successful ';
2821       -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2822       IF G_debug= C_Debug_Enabled THEN
2823          mydebug(l_Fnd_Log_Message, c_api_name,9);
2824       END IF;
2825    else
2826       l_Fnd_Log_message := 'Error while calling delete_reservation API ';
2827       -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2828       IF G_debug= C_Debug_Enabled THEN
2829          mydebug(l_Fnd_Log_Message, c_api_name,9);
2830       END IF;
2831    end if;
2832 
2833    -- Call fnd_log api at the end of the API
2834    l_Fnd_Log_message := 'At the end of procedure :';
2835    -- -- Fnd_Log_Debug(Fnd_Log.Level_Procedure,C_Module_name, l_Fnd_Log_Message );
2836 
2837    IF G_debug= C_Debug_Enabled THEN
2838          mydebug(l_Fnd_Log_Message, c_api_name,9);
2839    END IF;
2840 EXCEPTION
2841    WHEN fnd_api.g_exc_error THEN
2842        x_return_status := fnd_api.g_ret_sts_error;
2843 
2844        --  Get message count and data
2845        fnd_msg_pub.count_and_get
2846          (  p_count => x_msg_count
2847           , p_data  => x_msg_data
2848           );
2849 
2850         -- Call fnd_log api at the end of the API
2851         l_Fnd_Log_message := 'When Expected exception raised for procedure :' ;
2852         -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2853 
2854         -- Log message in trace file
2855         IF G_debug= C_Debug_Enabled THEN
2856             mydebug(l_Fnd_Log_Message,c_api_name,9);
2857         END IF;
2858 
2859         -- Get messages from stack and log them in fnd tables
2860         If X_Msg_Count = 1 Then
2861            -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
2862            -- Log message in trace file
2863            If G_debug= C_Debug_Enabled THEN
2864              mydebug(l_Fnd_Log_Message,c_api_name,9);
2865            End If;
2866         Elsif x_msg_count > 1 Then
2867             For I In 1..X_Msg_Count Loop
2868               FND_MSG_PUB.Get
2869                (p_msg_index     => i,
2870                 p_encoded       => 'F',
2871                 p_data          => l_Fnd_Log_Message,
2872                 p_msg_index_out => l_msg_index_out );
2873 
2874                -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2875 
2876                -- Log message in trace file
2877                IF G_debug= C_Debug_Enabled THEN
2878                    mydebug(l_Fnd_Log_Message,c_api_name,9);
2879                END IF;
2880             End Loop ;
2881         End If;
2882 
2883     WHEN fnd_api.g_exc_unexpected_error THEN
2884          x_return_status := fnd_api.g_ret_sts_unexp_error ;
2885 
2886          --  Get message count and data
2887          fnd_msg_pub.count_and_get
2888            (  p_count  => x_msg_count
2889             , p_data   => x_msg_data
2890              );
2891          -- Call fnd_log api at the end of the API
2892          l_Fnd_Log_message := 'When unexpected exception raised for procedure :';
2893          -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2894 
2895          -- Log message in trace file
2896          IF G_debug= C_Debug_Enabled THEN
2897              mydebug(l_Fnd_Log_Message,c_api_name,9);
2898          END IF;
2899 
2900          -- Get messages from stack and log them in fnd tables
2901          If X_Msg_Count = 1 Then
2902            -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
2903 
2904            -- Log message in trace file
2905            IF G_debug= C_Debug_Enabled THEN
2906              mydebug(l_Fnd_Log_Message,c_api_name,9);
2907            END IF;
2908          Elsif X_Msg_Count > 1 Then
2909             For I In 1..X_Msg_Count Loop
2910               FND_MSG_PUB.Get
2911                (p_msg_index     => i,
2912                 p_encoded       => 'F',
2913                 p_data          => l_Fnd_Log_Message,
2914                 p_msg_index_out => l_msg_index_out );
2915 
2916                -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2917 
2918                -- Log message in trace file
2919                IF G_debug= C_Debug_Enabled THEN
2920                    mydebug(l_Fnd_Log_Message,c_api_name,9);
2921                END IF;
2922             End Loop ;
2923          End If;
2924 
2925   WHEN OTHERS THEN
2926        x_return_status := fnd_api.g_ret_sts_unexp_error ;
2927 
2928        IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
2929          THEN
2930           fnd_msg_pub.add_exc_msg
2931             (  g_pkg_name
2932              , c_api_name
2933              );
2934        END IF;
2935 
2936        --  Get message count and data
2937        fnd_msg_pub.count_and_get
2938          (  p_count  => x_msg_count
2939           , p_data   => x_msg_data
2940           );
2941 
2942        -- Call fnd_log api at the end of the API
2943        l_Fnd_Log_message := 'When Others exception raised for procedure :' || G_Pkg_Name || '.' || C_API_Name ;
2944        -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2945 
2946        -- Log message in trace file
2947        IF G_debug= C_Debug_Enabled THEN
2948            mydebug(l_Fnd_Log_Message,c_api_name,9);
2949        END IF;
2950 
2951        -- Get messages from stack and log them in fnd tables
2952        If X_Msg_Count = 1 Then
2953          -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
2954 
2955          -- Log message in trace file
2956          If G_debug= C_Debug_Enabled Then
2957             mydebug(l_Fnd_Log_Message,c_api_name,9);
2958           End If;
2959        Elsif X_Msg_Count > 1 Then
2960            For I In 1..X_Msg_Count Loop
2961              FND_MSG_PUB.Get
2962               (p_msg_index     => i,
2963                p_encoded       => 'F',
2964                p_data          => l_Fnd_Log_Message,
2965                p_msg_index_out => l_msg_index_out );
2966 
2967               -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
2968 
2969               -- Log message in trace file
2970               IF G_debug= C_Debug_Enabled THEN
2971                   mydebug(l_Fnd_Log_Message,c_api_name,9);
2972               END IF;
2973            End Loop ;
2974        End If;
2975 END;
2976 
2977 --BUG#3497445
2978 --Procedure below converts the PO Quantity and the Reservation Quantity as per
2979 --the quantity and UOM changes done at the PO_level without altering the PO UOM and the Reservation UOM.
2980 
2981 PROCEDURE UOM_CONVERSION
2982 (   p_res_uom        IN  VARCHAR2 ,
2983     p_primary_uom    IN  VARCHAR2,
2984     p_po_qty         IN  NUMBER,
2985     p_po_line_id     IN  NUMBER,
2986     x_res_qty        IN OUT NOCOPY NUMBER,
2987     x_po_primary_qty    OUT NOCOPY NUMBER)
2988 IS
2989    -- Define Constants for API version and API name
2990    C_api_version_number CONSTANT NUMBER       := 1.0;
2991    C_api_name           CONSTANT VARCHAR2(30) := 'UOM_CONVERSION';
2992    C_Module_Name        Constant Varchar2(2000) := 'inv.plsql.inv_maintain_reservation_pub.maintain_reservation';
2993    C_Debug_Enabled      Constant Number := 1 ;
2994 
2995    --l_debug            NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2996    l_fnd_log_message    VARCHAR2(2000);
2997    l_Msg_Index_Out      Number;
2998 
2999    l_item_id            NUMBER ;
3000    l_po_uom             VARCHAR2(25);
3001    l_primary_meas       VARCHAR2(25);
3002    l_res_meas           VARCHAR2(25);
3003 
3004 --Get the Primary UOM and the Reservation UOM,and quantities in PO and Reservation  respectively.
3005 --Make comparisons of the three UOMs and calculate the primary_reservation_quantity and the reservation quantity.
3006 
3007 BEGIN
3008     l_Fnd_Log_message := 'Begining of procedure :';
3009     -- Dbms_output.Put_line('Begining Of Procedure');
3010     -- Fnd_Log_Debug(Fnd_Log.Level_Procedure, C_Module_name, l_Fnd_Log_Message);
3011 
3012     -- Log message in trace file
3013     IF G_debug= C_Debug_Enabled THEN
3014        G_Version_Printed := TRUE ;
3015        mydebug(l_Fnd_Log_Message,c_api_name,9);
3016        G_Version_Printed := FALSE ;
3017     END IF;
3018 
3019     BEGIN
3020         SELECT   item_id
3021                , unit_meas_lookup_code
3022           INTO   l_item_id
3023                , l_po_uom
3024           FROM po_lines_all
3025          WHERE po_line_id= p_po_line_id;
3026 
3027         SELECT unit_of_measure
3028           INTO l_primary_meas
3029           FROM mtl_units_of_measure
3030          WHERE uom_code = p_primary_uom;
3031 
3032         SELECT unit_of_measure
3033           INTO l_res_meas
3034           FROM mtl_units_of_measure
3035          WHERE uom_code = p_res_uom;
3036 
3037     EXCEPTION
3038         WHEN OTHERS THEN
3039         --po_message_s.sql_error('UOM_CONVERSION', '010', sqlcode);
3040         -- inv message....
3041            FND_MESSAGE.SET_NAME('INV','INV_RSV_UOM_CONVERSION');
3042            FND_MSG_PUB.Add;
3043            IF G_debug= C_Debug_Enabled THEN
3044               l_Fnd_Log_message := 'OTHERS exception happens when geting item_id, primary_uom, res_uom';
3045               mydebug(l_Fnd_Log_Message,c_api_name,9);
3046            END IF;
3047            RAISE;
3048     END;
3049 
3050     IF G_debug= C_Debug_Enabled THEN
3051            l_Fnd_Log_message := 'l_item_id: '|| l_item_id;
3052            mydebug(l_Fnd_Log_Message,c_api_name,9);
3053            l_Fnd_Log_message := 'l_primary_meas: '|| l_primary_meas;
3054            mydebug(l_Fnd_Log_Message,c_api_name,9);
3055            l_Fnd_Log_message := 'l_res_meas: '|| l_res_meas;
3056            mydebug(l_Fnd_Log_Message,c_api_name,9);
3057     END IF;
3058 
3059   --Comparing if all three UOMs are same.
3060 
3061    IF(l_po_uom=l_primary_meas  AND  l_res_meas = l_primary_meas )  THEN
3062        x_po_primary_qty:=p_po_qty;
3063        x_res_qty := p_po_qty;
3064        return;
3065    END IF;
3066 
3067 --Compare the PO and the primary_uom and if different convert the PO quantity in its primary UOM
3068 
3069    IF(l_po_uom <> l_primary_meas ) THEN
3070    -- remove the dependency on PO, changes to call inv_convert API
3071       /*po_uom_s.uom_convert(p_po_qty
3072                           ,l_po_uom
3073                           ,l_item_id
3074                           ,l_primary_meas
3075                           ,x_po_primary_qty);*/
3076 
3077       x_po_primary_qty := inv_convert.inv_um_convert
3078                (
3079                  item_id       => l_item_id    --number,
3080                 ,precision     => NULL         --number,
3081                 ,from_quantity => p_po_qty     --number,
3082                 ,from_unit     => l_po_uom     --varchar2,
3083                 ,to_unit       => l_primary_meas   --varchar2,
3084                 ,from_name     => NULL     --varchar2,
3085                 ,to_name       => NULL    --varchar2
3086                 );
3087    ELSE
3088       x_po_primary_qty := p_po_qty;
3089    END IF;
3090 
3091 --Compare the PO UOM and Reservation UOM and if different convert the PO quantity in the Reservation UOM.
3092 
3093    IF(l_po_uom <> l_res_meas) THEN
3094         --inv_convert.??
3095          /*po_uom_s.uom_convert(p_po_qty
3096                              ,l_po_uom
3097                              ,l_item_id
3098                              ,l_res_meas
3099                              ,x_res_qty);*/
3100       x_res_qty := inv_convert.inv_um_convert
3101               (
3102                 item_id       => l_item_id    --number,
3103                ,precision     => NULL         --number,
3104                ,from_quantity => p_po_qty     --number,
3105                ,from_unit     => l_po_uom     --varchar2,
3106                ,to_unit       => l_res_meas   --varchar2,
3107                ,from_name     => NULL     --varchar2,
3108                ,to_name       => NULL    --varchar2
3109                );
3110    ELSE
3111         x_res_qty := p_po_qty;
3112    END IF;
3113 
3114    IF G_debug= C_Debug_Enabled THEN
3115         l_Fnd_Log_message := 'x_po_primary_qty :'|| x_po_primary_qty;
3116         mydebug(l_Fnd_Log_Message, c_api_name,9);
3117         l_Fnd_Log_message := 'x_res_qty :'|| x_res_qty;
3118    END IF;
3119 
3120    -- Call fnd_log api at the end of the API
3121    l_Fnd_Log_message := 'At the end of procedure :';
3122    -- Fnd_Log_Debug(Fnd_Log.Level_Procedure,C_Module_name, l_Fnd_Log_Message );
3123 
3124    IF G_debug= C_Debug_Enabled THEN
3125         mydebug(l_Fnd_Log_Message, c_api_name,9);
3126    END IF;
3127 
3128 EXCEPTION
3129     WHEN OTHERS THEN
3130         IF G_debug= C_Debug_Enabled THEN
3131               l_Fnd_Log_message := 'OTHERS exception happens in uom_conversion';
3132               mydebug(l_Fnd_Log_Message,c_api_name,9);
3133         END IF;
3134 END UOM_CONVERSION;
3135 --BUG#3497445
3136 
3137 
3138 
3139 -- Procedure
3140 --   MAINTAIN_RESERVATION
3141 --
3142 -- Description
3143 --   API will handle changes to the resevation record based on the action code.
3144 --
3145 -- Input Paramters
3146 --   p_api_version_number   API version number (current version is 1.0 Standard in parameter)
3147 --   p_Init_Msg_lst         Flag to determine to initialize message stack for API, standard input parameter
3148 --   p_header_id            Purchase order header id or requisition header id
3149 --   p_line_id              Purchase order line id or requisition line id
3150 --   p_line_location_id     Purchase order shipment id
3151 --   p_distribution_id      Purchase order distribution_id
3152 --   p_transaction_id       Receiving transaction id
3153 --   p_ordered_quantity     Ordered quantity from order entry form
3154 --   p_ordered_uom          Ordered uom from order entry form
3155 --   p_action               different action codes for po approve/delete, requisition approve/delete
3156 -- Output Parameters
3157 --   x_Return_Status        Return Status of API, Standard out parameter
3158 --   x_Msg_Count            Message count from the stack, standard out parameter
3159 --   x_Msg_Data             Message from message stack, standard out parameter
3160 
3161 
3162 PROCEDURE MAINTAIN_RESERVATION
3163 (
3164   p_api_version_number        IN   NUMBER   DEFAULT 1.0
3165 , p_init_msg_lst              IN   VARCHAR2 DEFAULT fnd_api.g_false
3166 , p_header_id                 IN   NUMBER   DEFAULT NULL
3167 , p_line_id                   IN   NUMBER   DEFAULT NULL
3168 , p_line_location_id          IN   NUMBER   DEFAULT NULL
3169 , p_distribution_id           IN   NUMBER   DEFAULT NULL
3170 , p_transaction_id            IN   NUMBER   DEFAULT NULL
3171 , p_ordered_quantity          IN   NUMBER   DEFAULT NULL
3172 , p_ordered_uom               IN   VARCHAR2 DEFAULT NULL
3173 , p_action                    IN   VARCHAR2
3174 , x_return_status             OUT  NOCOPY   VARCHAR2
3175 , x_msg_count                 OUT  NOCOPY   NUMBER
3176 , x_msg_data                  OUT  NOCOPY   VARCHAR2
3177 )
3178 
3179 IS
3180 
3181   --get the records to be processed from the po_requiusitions_interface table (for create reservations)
3182   --this gets the records inserted into the interface table by req import
3183   --and creates a resevation on the req
3184   --
3185     /* Bug 5520620: Added UNION clause to handle the master tables along with interface data*/
3186     /* Bug 8524455. Get the unit of measure also. The quantity will be in
3187     unit of measure rather than uom code*/
3188    CURSOR get_interface_records IS
3189       SELECT requisition_header_id
3190            , requisition_line_id
3191            , interface_source_line_id
3192            , need_by_date
3193            , item_id
3194            , destination_organization_id
3195            , uom_code
3196            , quantity
3197            , project_id
3198            , task_id
3199            , source_type_code
3200            , unit_of_measure  -- Bug 8524455
3201       FROM   po_requisitions_interface
3202       WHERE  requisition_header_id = p_header_id
3203       AND    interface_source_code = 'CTO'
3204   UNION
3205    select  prl.requisition_header_id
3206         , prl.requisition_line_id
3207         , interface_source_line_id
3208         , need_by_date
3209         , item_id
3210         , destination_organization_id
3211         , uom_code
3212         , quantity
3213         , project_id
3214         , task_id
3215         , source_type_code
3216         , unit_of_measure  -- Bug 8524455
3217     from po_requisition_lines_all prl, po_requisition_headers_all prh , po_req_distributions_all prd, mtl_units_of_measure muom
3218    where prh.requisition_header_id = p_header_id
3219      and prh.REQUISITION_HEADER_ID = prl.REQUISITION_HEADER_ID
3220      and prd.requisition_line_id = prl.requisition_line_id
3221      and nvl(prl.modified_by_agent_flag,'N') <> 'Y'
3222      AND muom.unit_of_measure= unit_meas_lookup_code
3223      AND prh.interface_source_code = 'CTO'  -- Bug 13580940
3224      and not exists (select null
3225                   FROM po_requisitions_interface pri
3226                    where pri.requisition_header_id = prh.requisition_header_id
3227                    and   pri.interface_source_code = 'CTO');
3228 
3229   -- Bug 13580940 added a condition to check the interface_source_code for 'CTO'
3230   -- It is only for B2B flow where the interface source code is 'CTO',
3231   -- records should be selected from the cursor to create reservations,
3232   -- where restricting otherwise.
3233 
3234 
3235   --get the sales order header for the sales order line
3236   --the interface table does not have the sales order header information
3237   --
3238   /* Bug 2693130: Changed the table name in the following cursor
3239      from 'oe_order_lines' to 'oe_order_lines_all'  */
3240  /* Bug 8524455, get the unit of measure and uom code also */
3241    CURSOR get_sales_order_line(v_demand_line_id IN NUMBER) IS
3242       SELECT  oel.header_id
3243             , oel.ordered_quantity
3244             , oel.order_quantity_uom
3245             , muom.unit_of_measure
3246         FROM  oe_order_lines_all oel
3247             , mtl_units_of_measure muom
3248        WHERE  oel.line_id = v_demand_line_id
3249          AND  cancelled_flag = 'N'
3250          AND  oel.order_quantity_uom = muom.uom_code;
3251 
3252   --
3253   --get quantity that is already reserved against INV
3254   --this is subtracted from the sales order quantity which
3255   --will then be the quantity that is to be reserved
3256   --
3257 
3258   CURSOR get_inv_res_qty(v_demand_header_id IN NUMBER,
3259                          v_demand_line_id   IN NUMBER) IS
3260       --SELECT sum(nvl(reservation_quantity,0))
3261       SELECT sum(nvl(primary_reservation_quantity,0)) sum_pri_res_qty, primary_uom_code
3262         FROM mtl_reservations
3263        WHERE demand_source_header_id = v_demand_header_id
3264          AND demand_source_line_id = v_demand_line_id
3265          AND demand_source_type_id in (inv_reservation_global.g_source_type_oe,
3266                                        inv_reservation_global.g_source_type_internal_ord)
3267        GROUP BY primary_uom_code;
3268    --    AND supply_source_type_id = inv_reservation_global.g_source_type_inv;
3269    -- comment out last restriction supply_source_type_id = inv_reservation_global.g_source_type_inv
3270    -- since starting from r12, we will start to open manual reservations for WIP job....
3271 
3272 
3273   /* Bug# 3085721, This Cursor will check if the reservation is already
3274   created for the Requisitions */
3275 
3276   CURSOR get_res_exists(v_requisition_header_id IN NUMBER,
3277                         v_requisition_line_id   IN NUMBER) IS
3278       SELECT 'Exists'
3279         FROM mtl_reservations
3280        WHERE supply_source_header_id = v_requisition_header_id
3281          AND supply_source_line_id = v_requisition_line_id
3282          AND Supply_source_type_id=inv_reservation_global.g_source_type_req;
3283 
3284   CURSOR get_po_shipment(v_po_header_id IN NUMBER) IS
3285       SELECT  pll.po_header_id
3286              ,pll.po_line_id
3287              ,pll.line_location_id
3288              ,pll.ship_to_organization_id
3289              ,pl.item_id
3290         FROM  po_line_locations_all pll
3291              ,po_lines_all  pl
3292        WHERE  pll.po_header_id = v_po_header_id
3293          AND  pl.po_line_id = pll.po_line_id
3294 		 AND  pl.item_id IS NOT NULL;            /* Bug 16236492 exclude PO line locations for expense/one time item
3295 		                                            as it has no reservation to check against. */
3296 
3297 
3298   CURSOR get_req_line_of_po_shipment(v_po_shipment_id IN NUMBER) IS
3299       SELECT  b.requisition_line_id req_line_id
3300             , Nvl(b.project_id,-99) project_id
3301             , Nvl(b.task_id, -99) task_id
3302            -- , sum(a.quantity_ordered) quantity_ordered
3303         FROM  po_distributions_all a
3304             , po_req_distributions_all b
3305        WHERE  a.line_location_id = v_po_shipment_id
3306          AND  a.req_distribution_id = b.distribution_id
3307          AND  a.distribution_type <> 'AGREEMENT' --<Encumbrance FPJ>
3308     GROUP BY  b.requisition_line_id
3309             , b.project_id
3310             , b.task_id;
3311 
3312   CURSOR get_pt_count_po_shipment(v_po_shipment_id IN NUMBER) IS
3313       SELECT COUNT(min(po_distribution_id)) count
3314         FROM  po_distributions_all pd
3315        WHERE  pd.line_location_id = v_po_shipment_id
3316     GROUP BY project_id, task_id;
3317 
3318   CURSOR get_proj_task_of_po_shipment(v_po_shipment_id IN NUMBER) IS
3319         SELECT    Nvl(project_id, -99) project_id, Nvl(task_id, -99) task_id
3320           FROM    po_distributions_all
3321          WHERE    line_location_id = v_po_shipment_id
3322       GROUP BY    project_id,task_id;
3323 
3324   CURSOR get_po_res_qty(v_po_header_id        IN NUMBER
3325                        ,v_po_line_location_id IN NUMBER
3326                        ,v_project_id          IN NUMBER
3327                        ,v_task_id             IN NUMBER) IS
3328    SELECT  reservation_uom_code
3329          , primary_uom_code
3330          , sum(nvl(primary_reservation_quantity,0)) primary_reservation_quantity
3331      FROM  mtl_reservations
3332     WHERE  supply_source_header_id = v_po_header_id
3333       AND  supply_source_line_id = v_po_line_location_id
3334       AND  supply_source_type_id = inv_reservation_global.g_source_type_po
3335       AND  Nvl(project_id,-99) = nvl(v_project_id, -99)
3336       AND  Nvl(task_id,-99) = nvl(v_task_id, -99)
3337  GROUP BY  reservation_uom_code
3338          , primary_uom_code;
3339 
3340 
3341   CURSOR get_po_shipment_for_release(v_po_header_id IN NUMBER) IS
3342       SELECT  pll.po_header_id
3343             , pll.po_line_id
3344             , pll.line_location_id
3345             , pll.ship_to_organization_id    --?? is it correct org_id ??
3346             , pl.item_id
3347         FROM  po_line_locations_all  pll
3348              ,po_lines_all pl
3349        WHERE  pll.po_release_id = v_po_header_id
3350          AND  pll.po_line_id = pl.po_line_id
3351 	 AND  pl.item_id is not null; -- Bug # 12996777
3352 
3353  --
3354  -- get requisition lines for the req header
3355  -- to delete the reservation on the req (remove req supply)
3356  --
3357   CURSOR get_req_hdr_lines (v_po_req_header_id IN NUMBER) IS
3358       SELECT requisition_line_id, source_type_code
3359         FROM po_requisition_lines_all --<Shared Proc FPJ>
3360        WHERE requisition_header_id = v_po_req_header_id;
3361 
3362   --
3363   --get the PO header for the po line id that is cancelled
3364   --when po line is cancelled the reservations needs to be transferred back to the req or deleted
3365   --
3366   CURSOR get_po_header_id_line(v_po_line_id IN NUMBER) IS
3367      SELECT po_header_id
3368      FROM   po_lines_all --<Shared Proc FPJ>
3369    WHERE  po_line_id = v_po_line_id;
3370 
3371 
3372   --
3373   --get Distribution Records for the PO Line ID
3374   --when a PO line is cancelled the reservations needs to be transferred back to the req or deleted
3375   --
3376 
3377   --BUG#3497445.Added po_line_id in the cursor below.
3378 
3379   CURSOR get_line_loc_for_po_line (v_po_line_id IN NUMBER) IS
3380      SELECT pll.po_header_id
3381        , pll.po_line_id
3382        , pll.line_location_id
3383        , pll.ship_to_organization_id
3384        , pl.item_id
3385        , pll.quantity
3386        FROM po_line_locations_all pll, po_lines_all pl --<Shared Proc FPJ>
3387        WHERE pl.po_line_id = v_po_line_id
3388        AND pl.po_line_id = pll.po_line_id;
3389 
3390   --
3391   --get PO Header ID for the PO Line Location ID
3392   --to Check whether reservation exists for the PO
3393   --
3394   CURSOR get_po_header_id_shipment (v_po_line_location_id IN NUMBER)IS
3395       SELECT   po_header_id
3396         FROM   po_line_locations_all --<Shared Proc FPJ>
3397        WHERE   line_location_id = p_line_location_id;
3398 
3399 
3400   --
3401   --get Distribution Records for the PO Line Location ID
3402   --cancel shipment need to transfer reservation from PO to req or be deleted
3403   --
3404 
3405   --BUG#3497445.In cursor below the po_line_id was also fetched for fetching the quantity and UOM from the PO Lines.
3406 
3407   CURSOR get_line_loc_for_po_shipment (v_po_line_location_id IN NUMBER) IS
3408      SELECT  pll.po_header_id
3409        , pll.po_line_id
3410        , pll.line_location_id
3411        , pll.ship_to_organization_id
3412        , pl.item_id
3413        , pll.quantity
3414        FROM  po_line_locations_all pll, po_lines_all pl--<Shared Proc FPJ>
3415        WHERE  pll.line_location_id = v_po_line_location_id AND
3416        pl.po_line_id = pll.po_line_id;
3417 
3418   --
3419   --release ID will come as Header ID
3420   --get the po header id for the release
3421   --when a release is cancelled the reservations need to be transferred from the PO to the req or
3422   --deleted
3423   --
3424   CURSOR get_po_header_id_release (v_po_header_id IN NUMBER)IS
3425      SELECT   po_header_id
3426        FROM   po_releases_all --<Shared Proc FPJ>
3427        WHERE  po_release_id = v_po_header_id;
3428   --
3429   --get Distribution Records for the PO Release ID
3430   --
3431 
3432   --BUG#3497445.In cursor below the po_line_id was also fetched for fetching the quantity and UOM from the PO Lines.
3433 
3434   CURSOR get_distr_for_po_release (v_po_header_id IN NUMBER) IS
3435      SELECT  po_header_id
3436        , po_line_id
3437        , line_location_id
3438        , req_distribution_id
3439        , quantity_ordered
3440        FROM  po_distributions_all --<Shared Proc FPJ>
3441        WHERE  po_release_id = v_po_header_id;
3442 
3443   CURSOR get_source_doc_code (v_transaction_id IN NUMBER) IS
3444      SELECT source_document_code, organization_id FROM rcv_transactions
3445        WHERE transaction_id = v_transaction_id;
3446 
3447   CURSOR get_rcv_transaction (v_transaction_id IN NUMBER) IS
3448      SELECT  decode(a.source_document_code,'PO'
3449        , decode(b.asn_line_flag, 'Y', 'ASN', 'PO'), a.source_document_code)  supply_type
3450        , a.po_header_id, a.po_line_id
3451        , a.po_line_location_id
3452        , a.po_distribution_id
3453        , d.uom_code primary_unit_of_measure
3454        , a.primary_quantity
3455        , a.secondary_quantity
3456        , a.secondary_unit_of_measure
3457        , a.requisition_line_id
3458        , a.req_distribution_id
3459        , a.shipment_line_id
3460        , a.shipment_header_id
3461        , a.subinventory
3462        , a.locator_id
3463        , a.organization_id
3464        , a.lpn_id
3465        , b.item_revision
3466        , b.item_id
3467        , b.to_organization_id
3468        , c.project_id
3469        , c.task_id
3470        FROM  rcv_transactions a
3471        , rcv_shipment_lines b
3472        , po_distributions_all c
3473        , mtl_units_of_measure d
3474        WHERE  transaction_id = v_transaction_id
3475        AND  a.shipment_line_id = b.shipment_line_id
3476        AND  c.po_distribution_id = a.po_distribution_id
3477        AND  c.po_header_id = a.po_header_id
3478        AND  c.po_line_id = a.po_line_id
3479        AND  c.line_location_id = a.po_line_location_id
3480        AND d.unit_of_measure = a.primary_unit_of_measure;
3481 
3482   --Bug#3009495 Get the lot number for the delivered item
3483   CURSOR get_rcv_lot_number(v_shipment_line_id IN NUMBER) IS
3484      SELECT lot_num
3485           , primary_quantity
3486           , secondary_quantity
3487        FROM rcv_lot_transactions
3488       WHERE shipment_line_id = v_shipment_line_id;
3489 
3490   -- Bug 5611560
3491   CURSOR get_rcv_txn_lot_number(v_shipment_line_id IN NUMBER, v_transaction_id IN NUMBER) IS
3492      SELECT lot_num
3493           , primary_quantity
3494           , secondary_quantity
3495        FROM rcv_lot_transactions
3496       WHERE shipment_line_id = v_shipment_line_id
3497         AND transaction_id = v_transaction_id;
3498 
3499   CURSOR get_rcv_transaction_asn(v_transaction_id IN NUMBER) IS
3500      SELECT  a.po_header_id
3501        , a.po_line_id
3502        , a.po_line_location_id
3503        , a.po_distribution_id
3504        , d.uom_code primary_unit_of_measure
3505        , a.primary_quantity
3506        , a.secondary_quantity                   /* 15979250 */
3507        , a.secondary_unit_of_measure            /* 15979250 */
3508        , a.shipment_line_id
3509        , a.subinventory
3510        , a.locator_id
3511        , a.organization_id
3512        , a.lpn_id
3513        , b.item_revision
3514        , b.item_id
3515        , b.to_organization_id
3516        , c.project_id
3517        , c.task_id
3518        FROM  rcv_transactions a
3519        , rcv_shipment_lines b
3520        , po_distributions_all c
3521        , mtl_units_of_measure d
3522        WHERE a.transaction_id = v_transaction_id
3523        AND a.source_document_code = 'PO'
3524        AND b.shipment_line_id = a.shipment_line_id
3525        AND b.asn_line_flag = 'Y'
3526        AND c.po_distribution_id = a.po_distribution_id
3527        AND c.po_header_id = a.po_header_id
3528        AND c.po_line_id = a.po_line_id
3529        AND c.line_location_id = a.po_line_location_id
3530        AND d.unit_of_measure = a.primary_unit_of_measure;
3531 
3532 
3533   CURSOR get_rcv_txn_int_req(v_transaction_id IN NUMBER) IS
3534 
3535      /* Fix for Bug#8673423. Added secondary_quantity and secondary unit of measure */
3536 
3537      SELECT  a.requisition_line_id
3538        , e.uom_code primary_unit_of_measure
3539        , a.primary_quantity
3540        , a.secondary_quantity
3541        , a.secondary_unit_of_measure
3542        , a.shipment_line_id
3543        , a.subinventory
3544        , a.locator_id
3545        , a.organization_id
3546        , a.lpn_id
3547        , b.item_revision
3548        , b.item_id
3549        , b.to_organization_id
3550        , c.project_id
3551        , c.task_id
3552        , d.requisition_header_id
3553        FROM  rcv_transactions a
3554        , rcv_shipment_lines b
3555        , po_req_distributions_all c
3556        , po_requisition_lines_all d
3557        , mtl_units_of_measure e
3558        WHERE a.transaction_id = v_transaction_id
3559        AND a.source_document_code = 'REQ'
3560        AND b.shipment_line_id = a.shipment_line_id
3561        AND c.distribution_id = a.req_distribution_id
3562        AND c.requisition_line_id = a.requisition_line_id
3563        AND d.requisition_line_id = c.requisition_line_id
3564        AND e.unit_of_measure = a.primary_unit_of_measure;
3565 
3566 
3567       --are we sure the req_distribution_id is populated in rcv_transactions ???
3568 
3569   -- Define Constants for API version and API name
3570   C_api_version_number CONSTANT NUMBER       := 1.0;
3571   C_api_name           CONSTANT VARCHAR2(30) := 'Maintain_Reservation';
3572   C_Module_Name  Constant Varchar2(2000) := 'inv.plsql.inv_maintain_reservation.Maintain_Reservation';
3573   C_Debug_Enabled      Constant Number := 1 ;
3574 
3575   --l_debug                      NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3576   l_Fnd_Log_Message            VARCHAR2(2000);
3577 
3578   l_return_status              VARCHAR2(1);
3579   l_reservation_quantity       NUMBER;
3580   l_demand_header_id           NUMBER;
3581   l_ordered_quantity           NUMBER;
3582   l_exist_inv_res_qty          NUMBER;
3583   l_temp_res_qty               NUMBER;
3584   l_lead_time                  NUMBER; -- Bug# 2984835.
3585   l_res_exists                 VARCHAR2(10); -- Bug# 3085721.
3586 
3587   l_rsv_rec                    inv_reservation_global.mtl_reservation_rec_type;
3588   l_rsv_array                  inv_reservation_global.mtl_reservation_tbl_type;
3589 
3590   l_record_count               NUMBER;
3591   l_qty_avail_to_reserve       NUMBER;
3592   l_qty_avail                  NUMBER;
3593   l_po_primary_qty             NUMBER;
3594   l_primary_res_quantity       NUMBER;
3595   l_po_header_id               NUMBER;
3596   l_record_index               NUMBER;
3597   l_primary_quantity           NUMBER;
3598   l_supply_source_type_id      NUMBER;
3599   l_source_type_code           VARCHAR2(25);
3600 
3601   --l_msg_count                  NUMBER;
3602   --l_msg_data                   VARCHAR2(2000);
3603   l_msg_index_out              NUMBER;
3604   l_org_id                     NUMBER;
3605 
3606   l_item_revision              VARCHAR2(3);
3607   l_revision_control_code      NUMBER;
3608   l_lot_control_code           NUMBER;
3609 
3610   l_quantity_modified          NUMBER;
3611   l_secondary_uom_code         VARCHAR2(3); /* Fix for Bug#8673423 */
3612 
3613   --interface_record                get_interface_records%ROWTYPE;
3614   sales_order_record                get_sales_order_line%ROWTYPE;
3615   get_inv_res_qty_rec               get_inv_res_qty%ROWTYPE;
3616 
3617   get_po_shipment_rec               get_po_shipment%ROWTYPE;
3618   get_req_line_po_shipment_rec      get_req_line_of_po_shipment%ROWTYPE;
3619   get_pt_count_po_shipment_rec      get_pt_count_po_shipment%ROWTYPE;
3620   get_pt_po_shipment_rec            get_proj_task_of_po_shipment%ROWTYPE;
3621 
3622   l_mtl_maint_rsv_rec               inv_reservation_global.mtl_maintain_rsv_rec_type;
3623 
3624   get_po_res_qty_rec                get_po_res_qty%ROWTYPE;
3625   get_po_shipment_rel_rec           get_po_shipment_for_release%ROWTYPE;
3626   get_req_hdr_lines_rec             get_req_hdr_lines%ROWTYPE;
3627  -- get_distr_rec                     get_distr_for_po_line%ROWTYPE;
3628   get_line_loc_rec                  get_line_loc_for_po_line%ROWTYPE;
3629   get_rcv_lot_number_rec            GET_RCV_LOT_NUMBER%ROWTYPE;
3630 
3631   get_rcv_transaction_rec           get_rcv_transaction%ROWTYPE;
3632   get_rcv_transaction_asn_rec       get_rcv_transaction_asn%ROWTYPE;
3633   get_rcv_txn_int_req_rec           get_rcv_txn_int_req%ROWTYPE;
3634   p_mtl_maintain_rsv_rec            inv_reservation_global.mtl_maintain_rsv_rec_type;
3635   get_source_doc_code_rec           get_source_doc_code%ROWTYPE;
3636   l_delete_flag VARCHAR2(1);
3637   l_sort_by_criteria Number;
3638   --l_transaction_id NUMBER;
3639   --Bug 5253916: For update so qty
3640   l_organization_id NUMBER;
3641   l_inventory_item_id NUMBER;
3642   l_primary_res_qty NUMBER;
3643   l_req_unit_meas VARCHAR2(25);
3644   l_req_qty NUMBER;
3645   l_temp_uom_res_qty           NUMBER;              -- Bug 8524455
3646   l_consider_for_rsv           BOOLEAN;             -- Bug 9733377
3647 
3648     --Bug# 2984835.
3649     CURSOR get_lead_time (v_item_id IN NUMBER
3650                          ,v_org_id  IN NUMBER) IS
3651         SELECT  POSTPROCESSING_LEAD_TIME
3652           FROM  MTL_SYSTEM_ITEMS
3653          WHERE  INVENTORY_ITEM_ID = v_item_id
3654            AND  ORGANIZATION_ID = v_org_id;
3655 
3656 BEGIN
3657 
3658    l_Fnd_Log_message := 'Begining of procedure :';
3659 
3660 -- Log message in trace file
3661    IF g_debug= C_Debug_Enabled THEN
3662       g_Version_Printed := TRUE ;
3663       mydebug(l_Fnd_Log_Message,c_api_name,9);
3664       g_Version_Printed := FALSE ;
3665    END IF;
3666 
3667    -- Bug 3600118: Initialize return status variable
3668    l_return_status := FND_API.g_ret_sts_success;
3669 
3670    l_secondary_uom_code := null ; -- Fix for 8673423
3671 
3672    -- print out all the IN paramter values
3673    IF g_debug= C_Debug_Enabled THEN
3674      l_Fnd_Log_Message := 'p_header_id : '|| p_header_id ;
3675      mydebug(l_Fnd_Log_Message, c_api_name,9);
3676      l_Fnd_Log_Message := 'p_line_id : '|| p_line_id ;
3677      mydebug(l_Fnd_Log_Message, c_api_name,9);
3678      l_Fnd_Log_Message := 'p_line_location_id : '|| p_line_location_id ;
3679      mydebug(l_Fnd_Log_Message, c_api_name,9);
3680     -- l_Fnd_Log_Message := 'p_distribution_id : '|| p_distribution_id ;
3681      mydebug(l_Fnd_Log_Message, c_api_name,9);
3682      l_Fnd_Log_Message := 'p_transaction_id : '|| p_transaction_id ;
3683      mydebug(l_Fnd_Log_Message, c_api_name,9);
3684      l_Fnd_Log_Message := 'p_ordered_quantity : '|| p_ordered_quantity ;
3685      mydebug(l_Fnd_Log_Message, c_api_name,9);
3686      l_Fnd_Log_Message := 'p_ordered_uom : '|| p_ordered_uom ;
3687      mydebug(l_Fnd_Log_Message, c_api_name,9);
3688      l_Fnd_Log_Message := 'p_action : '|| p_action ;
3689      mydebug(l_Fnd_Log_Message, c_api_name,9);
3690    END IF;
3691 
3692    IF p_action IS NULL OR LTRIM(p_action) = '' THEN
3693       IF g_debug = C_Debug_Enabled THEN
3694         l_Fnd_Log_Message := 'Null action code';
3695         mydebug(l_Fnd_Log_Message, c_api_name,9);
3696       END IF;
3697       RAISE fnd_api.g_exc_unexpected_error;
3698    END IF;
3699 
3700    IF upper(p_action) = 'APPROVE_REQ_SUPPLY' THEN
3701       --replace with FOR Loops
3702       --OPEN get_interface_records;
3703       --null;
3704 
3705       IF g_debug = C_Debug_Enabled THEN
3706         l_Fnd_Log_Message := 'Approve Req Supply ';
3707         mydebug(l_Fnd_Log_Message, c_api_name,9);
3708       END IF;
3709 
3710       FOR interface_record_rec IN get_interface_records LOOP
3711 
3712 --       get the records after req import runs - to create reservation on the req
3713 
3714          --FETCH get_interface_records into interface_record;
3715          --EXIT WHEN get_interface_records%NOTFOUND;
3716 
3717          -- Bug 8742568, all further processing (create reservation) is required only if requisition
3718          --              is created corresponding to a sales order (back to back order). No need to
3719          --              do anything if interface_source_line_id is NULL
3720          IF interface_record_rec.interface_source_line_id IS NOT NULL THEN
3721 
3722             --Bug 9733377 Creation of Reservation should be happen only for B2B orders. So checking for the
3723             --  Existence of a SO line is required.
3724             l_consider_for_rsv := True;
3725 
3726             l_reservation_quantity   := interface_record_rec.quantity;
3727 
3728             OPEN get_sales_order_line(interface_record_rec.interface_source_line_id);
3729 
3730             IF g_debug= C_Debug_Enabled THEN
3731                     l_Fnd_Log_Message := 'interface_source_line_id: '||interface_record_rec.interface_source_line_id;
3732                     mydebug(l_Fnd_Log_Message, c_api_name,9);
3733             END IF;
3734 
3735             -- bug 8676222, no need to exit (no loop)
3736             FETCH get_sales_order_line INTO sales_order_record;
3737             --EXIT WHEN get_sales_order_line%NOTFOUND;
3738             --CLOSE get_sales_order_line;
3739 
3740             IF get_sales_order_line%NOTFOUND THEN
3741                 l_consider_for_rsv := False;
3742             END IF;
3743 
3744             --Bug 9733377
3745             IF l_consider_for_rsv THEN
3746 
3747                l_demand_header_id := oe_header_util.get_mtl_sales_order_id(sales_order_record.header_id);
3748                l_ordered_quantity := sales_order_record.ordered_quantity;
3749 
3750                IF g_debug= C_Debug_Enabled THEN
3751                        l_Fnd_Log_Message := 'l_ordered_quantity :'|| l_ordered_quantity;
3752                        mydebug(l_Fnd_Log_Message, c_api_name,9);
3753                END IF;
3754       --
3755                OPEN get_inv_res_qty(l_demand_header_id
3756                                   , interface_record_rec.interface_source_line_id);
3757       --
3758                --FETCH get_inv_res_qty INTO l_exist_inv_res_qty;
3759                FETCH get_inv_res_qty INTO get_inv_res_qty_rec;
3760                --CLOSE get_inv_res_qty;
3761                IF g_debug= C_Debug_Enabled THEN
3762                    l_Fnd_Log_Message := 'sales_order_record.order_quantity_uom: '||sales_order_record.order_quantity_uom;
3763                    mydebug(l_Fnd_Log_Message, c_api_name,9);
3764                    l_Fnd_Log_Message := 'l_ordered_quantity: '|| l_ordered_quantity;
3765                    mydebug(l_Fnd_Log_Message, c_api_name,9);
3766                END IF;
3767 
3768                -- bug 8676222: if get_inv_res_qty%NOTFOUND, quantity and UOM are retained from previous record
3769                --              Added IF get_inv_res_qty%FOUND .. ELSE .. END IF
3770                if get_inv_res_qty%FOUND then
3771 
3772                    IF g_debug= C_Debug_Enabled THEN
3773                            l_Fnd_Log_Message := 'get_inv_res_qty_rec.sum_pri_res_qty: '||get_inv_res_qty_rec.sum_pri_res_qty;
3774                            mydebug(l_Fnd_Log_Message, c_api_name,9);
3775                            l_Fnd_Log_Message := 'get_inv_res_qty_rec.primary_uom_code: '||get_inv_res_qty_rec.primary_uom_code;
3776                            mydebug(l_Fnd_Log_Message, c_api_name,9);
3777                    END IF;
3778 
3779                    IF  sales_order_record.order_quantity_uom = get_inv_res_qty_rec.primary_uom_code THEN
3780                         l_exist_inv_res_qty := get_inv_res_qty_rec.sum_pri_res_qty;
3781                    ELSE
3782                         l_exist_inv_res_qty := inv_convert.inv_um_convert
3783                                (
3784                                  item_id       => interface_record_rec.item_id    --number,
3785                                 ,precision     => NULL         --number,
3786                                 ,from_quantity => get_inv_res_qty_rec.sum_pri_res_qty     --number,
3787                                 ,from_unit     => get_inv_res_qty_rec.primary_uom_code     --varchar2,
3788                                 ,to_unit       => sales_order_record.order_quantity_uom   --varchar2,
3789                                 ,from_name     => NULL     --varchar2,
3790                                 ,to_name       => NULL    --varchar2
3791                                );
3792                    END IF;
3793                else
3794                   l_exist_inv_res_qty := 0;
3795                end if;
3796                if g_debug = c_debug_enabled then
3797                   mydebug('l_exist_inv_res_qty:'|| l_exist_inv_res_qty, c_api_name,9);
3798                end if;
3799 
3800                CLOSE get_inv_res_qty;
3801                CLOSE get_sales_order_line;
3802       --
3803       --       calculate the actual quantity to be reserved;
3804       --
3805               -- SELECT nvl((l_ordered_quantity - nvl(l_exist_inv_res_qty,0)),l_ordered_quantity)
3806                --INTO l_temp_res_qty
3807                --FROM DUAL;
3808 
3809                l_temp_res_qty := nvl((l_ordered_quantity - nvl(l_exist_inv_res_qty,0)),l_ordered_quantity);
3810 
3811                     --
3812                     /* Bug 8524455
3813                      l_temp_res_qty is in Reservation uom, which is same as sales order UOM.
3814                      l_reservation_quantity is from req lines which is in req uom.
3815                      Need to convert l_reservation_quantity before comparison. */
3816                     IF interface_record_rec.unit_of_measure <> sales_order_record.unit_of_measure THEN
3817                        po_uom_s.uom_convert( l_reservation_quantity,
3818                                              interface_record_rec.unit_of_measure,
3819                                              interface_record_rec.item_id,
3820                                              sales_order_record.unit_of_measure,
3821                                              l_temp_uom_res_qty);
3822 
3823                        l_reservation_quantity := l_temp_uom_res_qty;
3824 
3825                     END IF;
3826                     /* End of Bug 8524455. Now the l_reservation_quantity is in Sales Order UOM */
3827                     --
3828                IF nvl(l_temp_res_qty,0) < nvl(l_reservation_quantity,0) THEN
3829                   l_reservation_quantity := l_temp_res_qty;
3830                END IF;
3831       --
3832 
3833               /* Bug# 2984835,
3834                  In maintain reservation we need to pass need_by_date+post-process-lead-time
3835                  to the reservation routine. This is a part of the Bug fix done in
3836                  Bug# 2931808 */
3837 
3838                IF g_debug= C_Debug_Enabled THEN
3839                      l_Fnd_Log_Message := 'l_reservation_quantity: '|| l_reservation_quantity;
3840                      mydebug(l_Fnd_Log_Message, c_api_name,9);
3841                END IF;
3842 
3843       --
3844                IF interface_record_rec.item_id is not NULL then
3845                     OPEN  get_lead_time (interface_record_rec.item_id
3846                                          ,interface_record_rec.destination_organization_id);
3847                     FETCH get_lead_time INTO l_lead_time;
3848                     CLOSE get_lead_time;
3849                END IF;
3850       --
3851                /* Bug# 3085721, If Encumbrance is ON the supply Code will be called twice.
3852                   This creates 2 Resevations records. To avoid this we check if the
3853                   reservation already exists we do not create reservation */
3854 
3855 
3856                OPEN get_res_exists(interface_record_rec.requisition_header_id,
3857                                    interface_record_rec.requisition_line_id);
3858                FETCH get_res_exists INTO l_res_exists;
3859                IF g_debug= C_Debug_Enabled THEN
3860                        l_Fnd_Log_Message := 'progress 600 ';
3861                        mydebug(l_Fnd_Log_Message, c_api_name,9);
3862                END IF;
3863                IF get_res_exists%NOTFOUND THEN
3864                   IF g_debug= C_Debug_Enabled THEN
3865                         l_Fnd_Log_Message := 'reservation is not existing ';
3866                         mydebug(l_Fnd_Log_Message, c_api_name,9);
3867                   END IF;
3868                   -- Get the source_type_code to determine the requistion type
3869                   IF Upper(interface_record_rec.source_type_code) = 'INVENTORY' THEN
3870                      l_supply_source_type_id := inv_reservation_global.g_source_type_internal_req;
3871                    else
3872                      l_supply_source_type_id := inv_reservation_global.g_source_type_req;
3873                   END IF;
3874 
3875                   /* Bug# 8524455, changed interface_record_rec.uom_code to
3876                      sales_order_record.order_quantity_uom below */
3877                   CREATE_RES
3878                     ( p_inventory_item_id        => interface_record_rec.item_id
3879                       ,p_organization_id          => interface_record_rec.destination_organization_id
3880                       ,p_demand_source_header_id  => l_demand_header_id
3881                       ,p_demand_source_line_id    => interface_record_rec.interface_source_line_id
3882                       ,p_supply_source_type_id    => l_supply_source_type_id
3883                       ,p_supply_source_header_id  => p_header_id
3884                       ,p_supply_source_line_id    => interface_record_rec.requisition_line_id
3885                       ,p_requirement_date         => interface_record_rec.need_by_date + nvl(l_lead_time,0)
3886                       ,p_reservation_quantity     => l_reservation_quantity
3887                       ,p_reservation_uom_code     => sales_order_record.order_quantity_uom
3888                       ,p_project_id               => interface_record_rec.project_id
3889                       ,p_task_id                  => interface_record_rec.task_id
3890                       ,x_msg_count                => x_msg_count
3891                       ,x_msg_data                 => x_msg_data
3892                       ,x_return_status            => l_return_status);
3893 
3894                    IF g_debug= C_Debug_Enabled THEN
3895                           l_Fnd_Log_Message := 'calling create_res :l_return_status: '|| l_return_status;
3896                           mydebug(l_Fnd_Log_Message, c_api_name,9);
3897                    END IF;
3898 
3899                    x_return_status := l_return_status;
3900 
3901                    If x_return_status = fnd_api.g_ret_sts_success THEN
3902                       l_Fnd_Log_message := 'Calling CREATE_RES API was successful ';
3903                       -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
3904                       IF G_debug= C_Debug_Enabled THEN
3905                          mydebug(l_Fnd_Log_Message, c_api_name,9);
3906                       END IF;
3907                    else
3908                       l_Fnd_Log_message := 'Error while calling CREATE_RES API ';
3909                       -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
3910                       IF G_debug= C_Debug_Enabled THEN
3911                          mydebug(l_Fnd_Log_Message, c_api_name,9);
3912                       END IF;
3913                    end if;
3914 
3915                END IF;
3916                CLOSE get_res_exists;
3917 
3918             END IF; -- IF l_consider_for_rsv
3919 
3920             -- BUG 14706895
3921             IF get_sales_order_line%ISOPEN THEN
3922 
3923              	CLOSE get_sales_order_line;
3924 
3925             END IF;
3926 
3927          END IF;  --interface_record_rec.interface_source_line_id IS NOT NULL
3928 
3929       END LOOP;  -- get interface_records
3930       --CLOSE get_interface_records;
3931 
3932       IF g_debug= C_Debug_Enabled THEN
3933               l_Fnd_Log_Message := 'progress 800 ';
3934               mydebug(l_Fnd_Log_Message, c_api_name,9);
3935       END IF;
3936 
3937    ELSIF upper(p_action) = 'APPROVE_PO_SUPPLY' THEN
3938    --IF upper(p_action) = 'APPROVE_PO_SUPPLY' THEN
3939       IF g_debug= C_Debug_Enabled THEN
3940             l_Fnd_Log_Message := 'Inside of APPROVE_PO_SUPPLY ';
3941             mydebug(l_Fnd_Log_Message, c_api_name,9);
3942       END IF;
3943 
3944       IF NOT EXISTS_RESERVATION(p_supply_source_header_id        => p_header_id) THEN
3945          -- normal path taken when a PO is approved and there is no reservation on the PO
3946          --
3947 
3948          IF g_debug= C_Debug_Enabled THEN
3949             l_Fnd_Log_Message := 'Inside no reservations. p_header_id: '||p_header_id;
3950             mydebug(l_Fnd_Log_Message, c_api_name,9);
3951             l_Fnd_Log_Message := 'No reservation existing for PO, normal path taken when a PO is approved ';
3952             mydebug(l_Fnd_Log_Message, c_api_name,9);
3953          END IF;
3954 
3955          OPEN get_po_shipment(p_header_id);
3956          LOOP
3957             FETCH get_po_shipment INTO get_po_shipment_rec;
3958             EXIT WHEN get_po_shipment%NOTFOUND;
3959 
3960             IF g_debug= C_Debug_Enabled THEN
3961                l_Fnd_Log_Message := 'get_po_shipment_rec.line_location_id: '||get_po_shipment_rec.line_location_id;
3962                mydebug(l_Fnd_Log_Message, c_api_name,9);
3963             END IF;
3964 
3965             OPEN get_req_line_of_po_shipment(get_po_shipment_rec.line_location_id);
3966             LOOP
3967                FETCH get_req_line_of_po_shipment INTO get_req_line_po_shipment_rec;
3968                EXIT WHEN get_req_line_of_po_shipment%NOTFOUND;
3969 
3970                IF g_debug= C_Debug_Enabled THEN
3971                   l_Fnd_Log_Message := 'Before get_req_line_po_shipment_rec.project_id: '||get_req_line_po_shipment_rec.project_id;
3972                   mydebug(l_Fnd_Log_Message, c_api_name,9);
3973                   l_Fnd_Log_Message := 'before get_req_line_po_shipment_rec.task_id: '||get_req_line_po_shipment_rec.task_id;
3974                   mydebug(l_Fnd_Log_Message, c_api_name,9);
3975                END IF;
3976 
3977                IF (get_req_line_po_shipment_rec.project_id = -99) THEN
3978                   get_req_line_po_shipment_rec.project_id := NULL;
3979                END IF;
3980                IF (get_req_line_po_shipment_rec.task_id = -99) THEN
3981                   get_req_line_po_shipment_rec.task_id := NULL;
3982                END IF;
3983 
3984                IF g_debug= C_Debug_Enabled THEN
3985                   l_Fnd_Log_Message := 'after get_req_line_po_shipment_rec.project_id: '||get_req_line_po_shipment_rec.project_id;
3986                   mydebug(l_Fnd_Log_Message, c_api_name,9);
3987                   l_Fnd_Log_Message := 'after get_req_line_po_shipment_rec.task_id: '||get_req_line_po_shipment_rec.task_id;
3988                   mydebug(l_Fnd_Log_Message, c_api_name,9);
3989                END IF;
3990                --IF   (get_req_line_po_shipment_rec.project_id IS NOT NULL
3991                -- OR
3992                --  get_req_line_po_shipment_rec.task_id IS NOT NULL) THEN
3993 
3994                INV_RESERVATION_AVAIL_PVT.available_supply_to_reserve
3995                  (
3996                   x_return_status            =>l_return_status    --OUT  NOCOPY VARCHAR2
3997                   , x_msg_count                =>x_msg_count     --OUT     NOCOPY NUMBER
3998                   , x_msg_data                 =>x_msg_data     --OUT     NOCOPY VARCHAR2
3999                   , p_organization_id          =>get_po_shipment_rec.ship_to_organization_id--IN  NUMBER default null
4000                   , p_item_id                  =>get_po_shipment_rec.item_id --IN  NUMBER default null
4001                   , p_supply_source_type_id    =>inv_reservation_global.g_source_type_po --IN NUMBER
4002                   , p_supply_source_header_id  =>p_header_id --IN NUMBER
4003                   , p_supply_source_line_id    =>get_po_shipment_rec.line_location_id --IN NUMBER
4004                   , p_project_id               =>get_req_line_po_shipment_rec.project_id--IN NUMBER default null
4005                   , p_task_id                  =>get_req_line_po_shipment_rec.task_id --IN NUMBER default null
4006                   , x_qty_available_to_reserve =>l_qty_avail_to_reserve --OUT      NOCOPY NUMBER
4007                   , x_qty_available            =>l_qty_avail  --OUT      NOCOPY NUMBER
4008                  );
4009 
4010                IF g_debug= C_Debug_Enabled THEN
4011                   l_Fnd_Log_Message := 'l_qty_avail_to_reserve: '|| l_qty_avail_to_reserve;
4012                   mydebug(l_Fnd_Log_Message, c_api_name,9);
4013                   l_Fnd_Log_Message := 'l_qty_avail: '|| l_qty_avail;
4014                   mydebug(l_Fnd_Log_Message, c_api_name,9);
4015                END IF;
4016 
4017                -- check to see if reservation exist on the req line
4018                -- Under normal circumstances, there will be a reservation on a req-line
4019                      --
4020                get_req_line_res
4021                  (p_req_line_id  =>get_req_line_po_shipment_rec.req_line_id,
4022                   p_project_id   =>get_req_line_po_shipment_rec.project_id,
4023                   p_task_id      =>get_req_line_po_shipment_rec.task_id,
4024                   x_res_array    =>l_rsv_array,
4025                   x_record_count =>l_record_count);
4026 
4027                IF g_debug= C_Debug_Enabled THEN
4028                   l_Fnd_Log_Message := 'l_record_count: '|| l_record_count;
4029                   mydebug(l_Fnd_Log_Message, c_api_name,9);
4030                END IF;
4031 
4032                IF l_record_count > 0 THEN
4033                   -- we should not compare a req_line reservation_qty with po_shipment_order_qty
4034                   -- since po shipment can comparise of multi req_line....
4035                   -- also there is no manual reservation against external req.,
4036                   -- so available_to_reserve qty should be the same as ordered_qty
4037                   -- plus there is no reservation for this PO
4038                   -- this is first time for PO approve.  before PO approve, we can not
4039                   -- create manual reservation against non-approved PO
4040                   -- only when PO approved, system created reservation plus manual reservation
4041                   -- can be created against this approved PO.
4042                   l_rsv_rec := l_rsv_array(1);
4043                   -- since l_qty_avail_to_reserve is already in primary_uom, there is
4044                   -- no need to convert.
4045                   /*uom_conversion(l_rsv_rec.reservation_uom_code
4046                   ,l_rsv_rec.primary_uom_code
4047                     ,l_qty_avail_to_reserve
4048                     ,get_po_shipment_rec.po_line_id
4049                     ,l_reservation_quantity
4050                     ,l_po_primary_qty); */
4051 
4052                     if (l_rsv_rec.primary_reservation_quantity > l_qty_avail_to_reserve)  THEN
4053                        IF g_debug= C_Debug_Enabled THEN
4054                           l_Fnd_Log_Message := 'calling update_res API ';
4055                           mydebug(l_Fnd_Log_Message, c_api_name,9);
4056                        END IF;
4057 
4058                        update_res
4059                          (p_supply_source_header_id      => l_rsv_rec.supply_source_header_id
4060                           ,p_supply_source_line_id        => l_rsv_rec.supply_source_line_id
4061                           ,p_supply_source_type_id        => inv_reservation_global.g_source_type_req
4062                           ,p_primary_reservation_quantity => l_qty_avail_to_reserve
4063                           ,p_project_id                   => get_req_line_po_shipment_rec.project_id
4064                           ,p_task_id                      => get_req_line_po_shipment_rec.task_id
4065                           ,p_reservation_id               => l_rsv_rec.reservation_id
4066                           ,x_msg_count                    => x_msg_count
4067                           ,x_msg_data                     => x_msg_data
4068                           ,x_return_status                => l_return_status);
4069                     end if;
4070 
4071 
4072                     --BUG#3497445.The l_po_primary_qty calculated from the UOM_conversion call is used below.
4073 
4074                     l_primary_res_quantity  := least(l_rsv_rec.primary_reservation_quantity,
4075                                                      l_qty_avail_to_reserve);
4076                     IF g_debug= C_Debug_Enabled THEN
4077                        l_Fnd_Log_Message := 'l_primary_res_quantity: '|| l_primary_res_quantity;
4078                        mydebug(l_Fnd_Log_Message, c_api_name,9);
4079                     END IF;
4080 
4081                     IF g_debug= C_Debug_Enabled THEN
4082                        l_Fnd_Log_Message := 'calling transfer reservation for, From req line id: '||
4083                            l_rsv_rec.supply_source_line_id || ' to po shipment line id: ' || get_po_shipment_rec.line_location_id;
4084                        mydebug(l_Fnd_Log_Message, c_api_name,9);
4085                     END IF;
4086 
4087                     -- calling transfer_res with project_id, task_id
4088                     TRANSFER_RES
4089                       (p_from_reservation_id       =>l_rsv_rec.reservation_id
4090                        ,p_from_source_header_id    =>l_rsv_rec.supply_source_header_id
4091                        ,p_from_source_line_id      =>l_rsv_rec.supply_source_line_id
4092                        ,p_supply_source_type_id    =>inv_reservation_global.g_source_type_req
4093                        ,p_to_source_header_id      =>p_header_id
4094                        ,p_to_source_line_id        =>get_po_shipment_rec.line_location_id
4095                        ,p_to_supply_source_type_id =>inv_reservation_global.g_source_type_po
4096                        ,p_primary_uom_code         =>l_rsv_rec.primary_uom_code
4097                        ,p_primary_res_quantity     =>l_primary_res_quantity
4098                        ,x_msg_count                => x_msg_count
4099                        ,x_msg_data                 => x_msg_data
4100                        ,x_return_status            =>l_return_status);
4101 
4102                     IF g_debug= C_Debug_Enabled THEN
4103                        l_Fnd_Log_Message := 'after calling transfer_res. The l_return_status : '|| l_return_status;
4104                        mydebug(l_Fnd_Log_Message, c_api_name,9);
4105                     END IF;
4106 
4107                 ELSE -- if did not have any req reservation existing
4108                   FND_MESSAGE.SET_NAME('INV','INV_API_NO_RSV_EXIST');
4109                   FND_MSG_PUB.Add;
4110                   l_Fnd_Log_Message := 'calling get_req_line_res, no reservation records';
4111                   mydebug(l_Fnd_Log_Message, c_api_name,9);
4112                END IF; -- record_count
4113 
4114             END LOOP;
4115             CLOSE get_req_line_of_po_shipment;
4116 
4117          END LOOP;
4118          CLOSE get_po_shipment;
4119 
4120        ELSE
4121           -- reservation exists, means that
4122           -- new req lines were added to an already approved PO
4123           -- and therefore reservations need to be transferred from req-line to PO
4124           IF g_debug= C_Debug_Enabled THEN
4125              l_Fnd_Log_Message := 'reservation existing for PO  ';
4126              mydebug(l_Fnd_Log_Message, c_api_name,9);
4127           END IF;
4128           OPEN get_po_shipment(p_header_id);
4129           LOOP
4130              FETCH get_po_shipment INTO get_po_shipment_rec;
4131              EXIT WHEN get_po_shipment%NOTFOUND;
4132 
4133              IF g_debug= C_Debug_Enabled THEN
4134                 l_Fnd_Log_Message := 'get_po_shipment_rec.line_location_id: '||get_po_shipment_rec.line_location_id;
4135                 mydebug(l_Fnd_Log_Message, c_api_name,9);
4136              END IF;
4137              OPEN get_req_line_of_po_shipment(get_po_shipment_rec.line_location_id);
4138              LOOP
4139                 FETCH get_req_line_of_po_shipment INTO get_req_line_po_shipment_rec;
4140                 EXIT WHEN get_req_line_of_po_shipment%NOTFOUND;
4141 
4142                 -- Bug 10014752
4143                 IF (get_req_line_po_shipment_rec.project_id = -99) THEN
4144                   get_req_line_po_shipment_rec.project_id := NULL;
4145                 END IF;
4146                 IF (get_req_line_po_shipment_rec.task_id = -99) THEN
4147                   get_req_line_po_shipment_rec.task_id := NULL;
4148                 END IF;
4149 
4150                 IF g_debug= C_Debug_Enabled THEN
4151                   l_Fnd_Log_Message := 'after get_req_line_po_shipment_rec.project_id: '||get_req_line_po_shipment_rec.project_id;
4152                   mydebug(l_Fnd_Log_Message, c_api_name,9);
4153                   l_Fnd_Log_Message := 'after get_req_line_po_shipment_rec.task_id: '||get_req_line_po_shipment_rec.task_id;
4154                   mydebug(l_Fnd_Log_Message, c_api_name,9);
4155                 END IF;
4156                 -- End Bug 10014752
4157 
4158                 IF g_debug= C_Debug_Enabled THEN
4159                    l_Fnd_Log_Message := 'before calling get_req_line_res... ';
4160                    mydebug(l_Fnd_Log_Message, c_api_name,9);
4161                 END IF;
4162                 get_req_line_res(p_req_line_id   =>get_req_line_po_shipment_rec.req_line_id,
4163                                  p_project_id    =>get_req_line_po_shipment_rec.project_id,
4164                                  p_task_id       =>get_req_line_po_shipment_rec.task_id,
4165                                  x_res_array     =>l_rsv_array,
4166                                  x_record_count  =>l_record_count);
4167 
4168                 IF g_debug= C_Debug_Enabled THEN
4169                    l_Fnd_Log_Message := 'after calling get_req_line_res,l_record_count: '|| l_record_count;
4170                    mydebug(l_Fnd_Log_Message, c_api_name,9);
4171                 END IF;
4172 
4173                 IF l_record_count > 0 THEN
4174                    l_rsv_rec := l_rsv_array(1);
4175 
4176                    IF g_debug= C_Debug_Enabled THEN
4177                       l_Fnd_Log_Message := 'calling transfer_res with following IN values:';
4178                       mydebug(l_Fnd_Log_Message, c_api_name,9);
4179                    END IF;
4180                    TRANSFER_RES
4181                      (p_from_reservation_id       =>l_rsv_rec.reservation_id
4182                       ,p_from_source_header_id     =>l_rsv_rec.supply_source_header_id
4183                       ,p_from_source_line_id       =>l_rsv_rec.supply_source_line_id
4184                       ,p_supply_source_type_id     =>inv_reservation_global.g_source_type_req
4185                       ,p_to_source_header_id       =>p_header_id
4186                       ,p_to_source_line_id         =>get_po_shipment_rec.line_location_id
4187                       ,p_to_supply_source_type_id  =>inv_reservation_global.g_source_type_po
4188                       ,p_primary_uom_code          =>l_rsv_rec.primary_uom_code
4189                       ,p_primary_res_quantity      =>l_rsv_rec.primary_reservation_quantity
4190                       ,x_msg_count                 =>x_msg_count
4191                       ,x_msg_data                  =>x_msg_data
4192                       ,x_return_status             =>l_return_status);
4193                    IF g_debug= C_Debug_Enabled THEN
4194                       l_Fnd_Log_Message := 'after calling transfer_res. The l_return_status : '|| l_return_status;
4195                       mydebug(l_Fnd_Log_Message, c_api_name,9);
4196                    END IF;
4197                  ELSE
4198                    FND_MESSAGE.SET_NAME('INV','INV_API_NO_RSV_EXIST');
4199                    FND_MSG_PUB.Add;
4200                    l_Fnd_Log_Message := 'calling get_req_line_res, no reservation records';
4201                    mydebug(l_Fnd_Log_Message, c_api_name,9);
4202                 END IF;
4203 
4204              END LOOP;
4205              CLOSE get_req_line_of_po_shipment;
4206 
4207              OPEN  get_pt_count_po_shipment(get_po_shipment_rec.line_location_id);
4208              FETCH get_pt_count_po_shipment into get_pt_count_po_shipment_rec;
4209              CLOSE get_pt_count_po_shipment;
4210 
4211              IF g_debug= C_Debug_Enabled THEN
4212                 l_Fnd_Log_Message := ' get_pt_count_po_shipment_rec.count : '||get_pt_count_po_shipment_rec.count;
4213                 mydebug(l_Fnd_Log_Message, c_api_name,9);
4214              END IF;
4215 
4216              IF get_pt_count_po_shipment_rec.count > 1 THEN   -- multiple project/task
4217 
4218                 IF g_debug= C_Debug_Enabled THEN
4219                    l_Fnd_Log_Message := 'Multiple project/task...  ';
4220                    l_Fnd_Log_Message := 'get_po_shipment_rec.org_id:'||get_po_shipment_rec.ship_to_organization_id;
4221                    mydebug(l_Fnd_Log_Message, c_api_name,9);
4222                 END IF;
4223 
4224                 IF (inv_install.adv_inv_installed(get_po_shipment_rec.ship_to_organization_id))  THEN
4225                    -- is wms org
4226                    -- delete all the reservations for this shipment
4227                    -- log message
4228                    IF g_debug= C_Debug_Enabled THEN
4229                       l_Fnd_Log_Message := 'Organization is wms org. calling delete_res API with IN parameters';
4230                       mydebug(l_Fnd_Log_Message, c_api_name,9);
4231                    END IF;
4232                    -- Call reduce reservations instead of delete
4233                    -- reservations
4234                    -- Call the reduce reservations API by setting the
4235                    -- delete_flag to yes. delete all reservations for that
4236                    -- supply line.
4237                    -- calling reduce_reservation API
4238                    l_delete_flag := 'Y';
4239                    l_sort_by_criteria := inv_reservation_global.g_query_demand_ship_date_desc;
4240                    l_mtl_maint_rsv_rec.action := 0;--supply is reduced
4241                    l_mtl_maint_rsv_rec.organization_id := get_po_shipment_rec.ship_to_organization_id;
4242                    l_mtl_maint_rsv_rec.inventory_item_id := get_po_shipment_rec.item_id;
4243                    l_mtl_maint_rsv_rec.supply_source_type_id:= inv_reservation_global.g_source_type_po;
4244                    l_mtl_maint_rsv_rec.supply_source_header_id := p_header_id;
4245                    l_mtl_maint_rsv_rec.supply_source_line_id := get_po_shipment_rec.line_location_id;
4246                    --l_mtl_maint_rsv_rec.supply_source_line_detail := NULL;
4247 
4248                    reduce_reservation
4249                      (
4250                        p_api_version_number     => 1.0
4251                        , p_init_msg_lst           => fnd_api.g_false
4252                        , x_return_status          => l_return_status
4253                        , x_msg_count              => x_msg_count
4254                        , x_msg_data               => x_msg_data
4255                        , p_mtl_maintain_rsv_rec   => l_mtl_maint_rsv_rec
4256                        , p_delete_flag            => l_delete_flag
4257                        , p_sort_by_criteria       => l_sort_by_criteria
4258                        , x_quantity_modified      => l_quantity_modified);
4259                    IF g_debug= C_Debug_Enabled THEN
4260                       mydebug ('Return Status after calling reduce reservations: '|| l_return_status, c_api_name,9);
4261                    END IF;
4262 
4263                    IF l_return_status = fnd_api.g_ret_sts_error THEN
4264 
4265                       IF g_debug= C_Debug_Enabled THEN
4266                          mydebug('Raising expected error'|| l_return_status, c_api_name,9);
4267                       END IF;
4268                       RAISE fnd_api.g_exc_error;
4269 
4270                     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4271 
4272                       IF g_debug= C_Debug_Enabled THEN
4273                          mydebug('Rasing Unexpected error'|| l_return_status, c_api_name,9);
4274                       END IF;
4275                       RAISE fnd_api.g_exc_unexpected_error;
4276 
4277                    END IF;
4278                    /***** Call reduce reservations instead of delete reservations
4279                    DELETE_RES
4280                      (p_supply_source_header_id => p_header_id
4281                       ,p_supply_source_line_id   => get_po_shipment_rec.line_location_id
4282                       ,p_supply_source_type_id   => inv_reservation_global.g_source_type_po
4283                       ,x_msg_count               => x_msg_count
4284                       ,x_msg_data                => x_msg_data
4285                       ,x_return_status           => l_return_status);
4286                    IF g_debug= C_Debug_Enabled THEN
4287                       l_Fnd_Log_Message := 'l_return_status: '|| l_return_status;
4288                      END IF;
4289                      End comment*******/
4290                 END IF;
4291              END IF;
4292 
4293              IF g_debug= C_Debug_Enabled THEN
4294                    mydebug('Line location id' || get_po_shipment_rec.line_location_id, c_api_name,9);
4295                 END IF;
4296              OPEN get_proj_task_of_po_shipment(get_po_shipment_rec.line_location_id);
4297              LOOP
4298                 FETCH  get_proj_task_of_po_shipment INTO
4299                   get_pt_po_shipment_rec;
4300                 EXIT WHEN get_proj_task_of_po_shipment%NOTFOUND;
4301 
4302                 IF g_debug= C_Debug_Enabled THEN
4303                    mydebug('Inside project/task loop', c_api_name,9);
4304                 END IF;
4305                 IF g_debug= C_Debug_Enabled THEN
4306                    mydebug('Project Id: '|| get_pt_po_shipment_rec.project_id, c_api_name,9);
4307                    mydebug('Task Id: '|| get_pt_po_shipment_rec.task_id, c_api_name,9);
4308                 END IF;
4309 
4310 
4311                 IF (get_pt_po_shipment_rec.project_id = -99) THEN
4312                    get_pt_po_shipment_rec.project_id := NULL;
4313                 END IF;
4314                 IF (get_pt_po_shipment_rec.task_id = -99) THEN
4315                    get_pt_po_shipment_rec.task_id := NULL;
4316                 END IF;
4317                 INV_RESERVATION_AVAIL_PVT.available_supply_to_reserve
4318                   (
4319                    x_return_status                  => l_return_status
4320                    , x_msg_count                      => x_msg_count
4321                    , x_msg_data                       => x_msg_data
4322                    , p_organization_id                => get_po_shipment_rec.ship_to_organization_id
4323                    , p_item_id                        => get_po_shipment_rec.item_id
4324                    , p_supply_source_type_id          => inv_reservation_global.g_source_type_po
4325                    , p_supply_source_header_id        => p_header_id
4326                    , p_supply_source_line_id          => get_po_shipment_rec.line_location_id
4327                    , p_project_id                     => get_pt_po_shipment_rec.project_id
4328                    , p_task_id                        => get_pt_po_shipment_rec.task_id
4329                    , x_qty_available_to_reserve       => l_qty_avail_to_reserve
4330                    , x_qty_available                  => l_qty_avail
4331                    );
4332 
4333 
4334                 OPEN  get_po_res_qty
4335                   (p_header_id
4336                    ,get_po_shipment_rec.line_location_id
4337                    ,get_pt_po_shipment_rec.project_id
4338                    ,get_pt_po_shipment_rec.task_id);
4339                 FETCH get_po_res_qty INTO get_po_res_qty_rec ;
4340                 CLOSE get_po_res_qty ;
4341 
4342                 IF g_debug= C_Debug_Enabled THEN
4343                    mydebug('Qty available: '|| l_qty_avail, c_api_name,9);
4344                    mydebug('Qty available to reserve: '|| l_qty_avail_to_reserve, c_api_name,9);
4345                    mydebug('Qty reserved qty: '|| get_po_res_qty_rec.primary_reservation_quantity, c_api_name,9);
4346                 END IF;
4347                 IF  get_po_res_qty_rec.primary_reservation_quantity > 0 THEN
4348                    -- since l_qty_avail_to_reserve is already in primary uom,
4349                    -- there is no need to convert it again
4350                    -- comment out the following uom convertion
4351                    /*uom_conversion(get_po_res_qty_rec.reservation_uom_code
4352                    ,get_po_res_qty_rec.primary_uom_code
4353                      ,l_qty_avail_to_reserve
4354                      ,get_po_shipment_rec.po_line_id
4355                      ,l_reservation_quantity
4356                      ,l_po_primary_qty);*/
4357 
4358                      IF (get_po_res_qty_rec.primary_reservation_quantity > l_qty_avail)  THEN
4359                         -- calling reduce_reservation API
4360                         l_mtl_maint_rsv_rec.action := 0;--supply is reduced
4361                         l_sort_by_criteria := inv_reservation_global.g_query_demand_ship_date_desc;
4362                         l_mtl_maint_rsv_rec.organization_id := get_po_shipment_rec.ship_to_organization_id;
4363                         l_mtl_maint_rsv_rec.inventory_item_id := get_po_shipment_rec.item_id;
4364                         l_mtl_maint_rsv_rec.supply_source_type_id:= inv_reservation_global.g_source_type_po;
4365                         l_mtl_maint_rsv_rec.supply_source_header_id := p_header_id;
4366                         l_mtl_maint_rsv_rec.supply_source_line_id := get_po_shipment_rec.line_location_id;
4367                 --        l_mtl_maint_rsv_rec.supply_source_line_detail := NULL;
4368                         --l_mtl_maint_rsv_rec.expected_quantity := get_po_res_qty_rec.primary_reservation_quantity - l_qty_avail_to_reserve;
4369                         l_mtl_maint_rsv_rec.expected_quantity := l_qty_avail;
4370                         l_mtl_maint_rsv_rec.expected_quantity_uom := get_po_res_qty_rec.primary_uom_code;
4371                         l_mtl_maint_rsv_rec.project_id := get_pt_po_shipment_rec.project_id;
4372                         l_mtl_maint_rsv_rec.task_id := get_pt_po_shipment_rec.task_id;
4373 
4374                         reduce_reservation
4375                           (
4376                             p_api_version_number     => 1.0
4377                             , p_init_msg_lst           => fnd_api.g_false
4378                             , x_return_status          => l_return_status
4379                             , x_msg_count              => x_msg_count
4380                             , x_msg_data               => x_msg_data
4381                             , p_mtl_maintain_rsv_rec   => l_mtl_maint_rsv_rec
4382                             , p_delete_flag            => 'N'
4383                             , p_sort_by_criteria       => l_sort_by_criteria
4384                             , x_quantity_modified      => l_quantity_modified);
4385 
4386                         IF g_debug= C_Debug_Enabled THEN
4387                            mydebug ('Return Status after calling reduce reservations: '|| l_return_status, c_api_name,9);
4388                         END IF;
4389 
4390                         IF l_return_status = fnd_api.g_ret_sts_error THEN
4391 
4392                            IF g_debug= C_Debug_Enabled THEN
4393                               mydebug('Raising expected error'|| l_return_status, c_api_name,9);
4394                            END IF;
4395                            RAISE fnd_api.g_exc_error;
4396 
4397                          ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4398 
4399                            IF g_debug= C_Debug_Enabled THEN
4400                               mydebug('Rasing Unexpected error'|| l_return_status, c_api_name,9);
4401                            END IF;
4402                            RAISE fnd_api.g_exc_unexpected_error;
4403 
4404                         END IF;
4405 
4406                      END IF;
4407                 END IF;  -- > 0
4408 
4409              END LOOP;
4410              CLOSE  get_proj_task_of_po_shipment;
4411 
4412           END LOOP;
4413           CLOSE get_po_shipment;
4414       END IF;  -- if not_exist()
4415     ELSIF upper(p_action) = 'DELIVER_TO_INVENTORY' THEN
4416       IF g_debug= C_Debug_Enabled THEN
4417            l_Fnd_Log_Message := 'Deliver To Inventory. p_transaction_id : '||p_transaction_id;
4418            mydebug(l_Fnd_Log_Message, c_api_name,9);
4419       END IF;
4420 
4421         -- IF p_transaction_id IS NULL THEN
4422               -- l_transaction_id := p_header_id;
4423         --  ELSE
4424               -- l_transaction_id := p_transaction_id;
4425         -- END IF;
4426 
4427       IF (p_transaction_id IS NULL) THEN
4428          FND_MESSAGE.SET_NAME('INV','INV_INVALID_TRANSACTION_ID');
4429          FND_MSG_PUB.ADD;
4430          RAISE fnd_api.g_exc_error;
4431       END IF;
4432 
4433       OPEN get_source_doc_code(p_transaction_id);
4434       FETCH get_source_doc_code INTO get_source_doc_code_rec;
4435 
4436       --IF g_debug= C_Debug_Enabled THEN
4437       --         l_Fnd_Log_Message := 'p_transaction_id : '|| l_transaction_id;
4438       --         mydebug(l_Fnd_Log_Message, c_api_name,9);
4439       -- END IF;
4440 
4441       IF g_debug= C_Debug_Enabled THEN
4442          l_Fnd_Log_Message := 'get_source_doc_code_rec.organization_id : '||get_source_doc_code_rec.organization_id;
4443          mydebug(l_Fnd_Log_Message, c_api_name,9);
4444       END IF;
4445 
4446       IF (get_source_doc_code_rec.organization_id IS NULL) THEN
4447          IF g_debug= C_Debug_Enabled THEN
4448             mydebug('Could not find the organization for the transaction id:' || p_transaction_id, c_api_name,9);
4449          END IF;
4450          RAISE fnd_api.g_exc_error;
4451       END IF;
4452 
4453       IF (inv_install.adv_inv_installed(get_source_doc_code_rec.organization_id)) THEN
4454            -- wms org
4455            -- return
4456            IF g_debug= C_Debug_Enabled THEN
4457                 l_Fnd_Log_Message := 'it is WMS organization, return.';
4458                 mydebug(l_Fnd_Log_Message, c_api_name,9);
4459            END IF;
4460 
4461            IF get_source_doc_code%isopen THEN
4462               CLOSE get_source_doc_code;
4463            END if;
4464 
4465            RETURN;
4466        ELSE
4467 
4468          IF g_debug= C_Debug_Enabled THEN
4469             l_Fnd_Log_Message := 'it is Inventory organization';
4470             mydebug(l_Fnd_Log_Message, c_api_name,9);
4471             l_Fnd_Log_Message := 'get_source_doc_code_rec.source_document_code :'|| get_source_doc_code_rec.source_document_code;
4472             mydebug(l_Fnd_Log_Message, c_api_name,9);
4473          END IF;
4474 
4475          IF (get_source_doc_code_rec.source_document_code = 'PO') THEN
4476             OPEN  get_rcv_transaction(p_transaction_id);
4477             FETCH get_rcv_transaction into get_rcv_transaction_rec;
4478             --CLOSE get_rcv_transaction;
4479 
4480             IF get_rcv_transaction_rec.supply_type = 'PO' THEN
4481 
4482               IF g_debug= C_Debug_Enabled THEN
4483                    l_Fnd_Log_Message := 'DTI. supply_type is PO';
4484                    mydebug(l_Fnd_Log_Message, c_api_name,9);
4485               END IF;
4486               IF get_rcv_transaction%FOUND THEN
4487 
4488                     IF g_debug= C_Debug_Enabled THEN
4489                           l_Fnd_Log_Message := 'get_rcv_transaction_rec.item_revision: '||get_rcv_transaction_rec.item_revision;
4490                           mydebug(l_Fnd_Log_Message, c_api_name,9);
4491                     END IF;
4492                     IF get_rcv_transaction_rec.item_revision is NULL then
4493                        IF g_debug= C_Debug_Enabled THEN
4494                              l_Fnd_Log_Message := 'get_rcv_transaction_rec.item_revision is NULL ';
4495                              mydebug(l_Fnd_Log_Message, c_api_name,9);
4496                        END IF;
4497 
4498                        BEGIN
4499                            SELECT    max(mir.revision)
4500                              INTO    l_item_revision
4501                              FROM    mtl_system_items msi
4502                                    , mtl_item_revisions mir
4503                             WHERE    msi.inventory_item_id = get_rcv_transaction_rec.item_id
4504                               AND    msi.organization_id = get_rcv_transaction_rec.organization_id
4505                               AND    msi.revision_qty_control_code = 2
4506                               AND    mir.organization_id = msi.organization_id
4507                               AND    mir.inventory_item_id = msi.inventory_item_id  --Bug# 10365814
4508                               AND    mir.effectivity_date in
4509                                       (SELECT   MAX(mir2.effectivity_date)
4510                                          FROM   mtl_item_revisions mir2
4511                                         WHERE   mir2.organization_id = get_rcv_transaction_rec.organization_id
4512                                           AND   mir2.inventory_item_id = get_rcv_transaction_rec.item_id
4513                                           AND   mir2.effectivity_date <= SYSDATE
4514                                           AND   mir2.implementation_date is not NULL);
4515 
4516 
4517                            get_rcv_transaction_rec.item_revision := l_item_revision;
4518 
4519                        EXCEPTION
4520                           when others then
4521                              l_item_revision := NULL;
4522                              get_rcv_transaction_rec.item_revision := NULL;
4523                        END;
4524 
4525                        IF g_debug= C_Debug_Enabled THEN
4526                               l_Fnd_Log_Message := 'l_item_revision :'|| l_item_revision;
4527                               mydebug(l_Fnd_Log_Message, c_api_name,9);
4528                        END IF;
4529 
4530                     ELSE
4531                       /* Begin Bug 3688014 : If the rcv_shipment_lines contains a item revision
4532                       for a non-revision controlled item, we should pass a NULL
4533                       item revision to inventory so that supply gets updated accordingly.
4534                       */
4535 
4536                       IF g_debug= C_Debug_Enabled THEN
4537                            l_Fnd_Log_Message := 'get_rcv_transaction_rec.item_revision is NOT NULL ';
4538                            mydebug(l_Fnd_Log_Message, c_api_name,9);
4539                       END IF;
4540 
4541                       BEGIN
4542 
4543                           SELECT msi.revision_qty_control_code
4544                             INTO l_revision_control_code
4545                             FROM mtl_system_items_b msi
4546                            WHERE msi.inventory_item_id = get_rcv_transaction_rec.item_id
4547                              AND msi.organization_id = get_rcv_transaction_rec.organization_id;
4548 
4549                           IF  l_revision_control_code = 1 THEN
4550                                get_rcv_transaction_rec.item_revision := NULL;
4551                           END IF;
4552 
4553                       EXCEPTION
4554                           WHEN others THEN
4555                               get_rcv_transaction_rec.item_revision := NULL;
4556                       END;
4557 
4558                       IF g_debug= C_Debug_Enabled THEN
4559                              l_Fnd_Log_Message := 'l_revision_control_code : '|| l_revision_control_code;
4560                              mydebug(l_Fnd_Log_Message, c_api_name,9);
4561                       END IF;
4562 
4563                       /* End Bug 3688014 */
4564 
4565                     END IF;  --get_rcv_transaction_rec.item_revision is NULL
4566 
4567                     --Bug#3009495.If item is lot controlled then we will derive the lot number
4568                     --from the rcv_lot_transactions.
4569                     get_rcv_lot_number_rec.lot_num := NULL;
4570                     --IF get_shipment_lines%FOUND THEN
4571 
4572                     --Bug 7559682 Error performing deliver transaction for Direct Items in EAM workorder
4573                     --having destination type as shop floor. Modified the code block to include MSI query
4574                     BEGIN
4575                         select nvl(lot_control_code,1)
4576                           into   l_lot_control_code
4577                           from   mtl_system_items
4578                          where  organization_id = get_rcv_transaction_rec.to_organization_id
4579                            and  inventory_item_id = get_rcv_transaction_rec.item_id ;
4580 
4581                         IF (l_lot_control_code = 2) THEN
4582                            --Begin         --commented for bug 7559682
4583                               -- 5611560 New cursor is opened and fetcched.
4584                               OPEN  get_rcv_txn_lot_number(get_rcv_transaction_rec.shipment_line_id,p_transaction_id);
4585                               FETCH get_rcv_txn_lot_number INTO get_rcv_lot_number_rec;
4586                         END IF;
4587 
4588                     EXCEPTION
4589                       WHEN OTHERS THEN
4590                         get_rcv_lot_number_rec.lot_num := NULL;
4591                         l_lot_control_code := 1;
4592                           -- END;        --commented for bug 7559682
4593 
4594                         --End If;  --commented for bug 7559682
4595                     END;
4596                     -- End If; -- End Bug#3009495
4597 
4598                     IF g_debug= C_Debug_Enabled THEN
4599                           l_Fnd_Log_Message := 'Calling query_res with following IN paramter values: ';
4600                           mydebug(l_Fnd_Log_Message, c_api_name,9);
4601                           l_Fnd_Log_Message := 'p_supply_source_header_id :' || get_rcv_transaction_rec.po_header_id;
4602                           mydebug(l_Fnd_Log_Message, c_api_name,9);
4603                           l_Fnd_Log_Message := 'p_supply_source_line_id : '|| get_rcv_transaction_rec.po_line_location_id;
4604                           mydebug(l_Fnd_Log_Message, c_api_name,9);
4605                           l_Fnd_Log_Message := 'p_supply_source_type_id : '|| inv_reservation_global.g_source_type_po;
4606                           mydebug(l_Fnd_Log_Message, c_api_name,9);
4607                           l_Fnd_Log_Message := 'p_project_id : '|| get_rcv_transaction_rec.project_id;
4608                           mydebug(l_Fnd_Log_Message, c_api_name,9);
4609                           l_Fnd_Log_Message := 'p_task_id: '||get_rcv_transaction_rec.task_id;
4610                           mydebug(l_Fnd_Log_Message, c_api_name,9);
4611                     END IF;
4612                     query_res(p_supply_source_header_id  => get_rcv_transaction_rec.po_header_id
4613                              ,p_supply_source_line_id    => get_rcv_transaction_rec.po_line_location_id
4614                              ,p_supply_source_type_id    => inv_reservation_global.g_source_type_po
4615                              ,p_project_id               => get_rcv_transaction_rec.project_id
4616                              ,p_task_id                  => get_rcv_transaction_rec.task_id
4617                              ,x_rsv_array                => l_rsv_array
4618                              ,x_record_count             => l_record_count
4619                              ,x_msg_count                => x_msg_count
4620                              ,x_msg_data                 => x_msg_data
4621                              ,x_return_status            => l_return_status);
4622 
4623                     IF g_debug= C_Debug_Enabled THEN
4624                         l_Fnd_Log_Message := 'l_record_count: '|| l_record_count;
4625                         mydebug(l_Fnd_Log_Message, c_api_name,9);
4626                         l_Fnd_Log_Message := 'l_return_status: '|| l_return_status;
4627                         mydebug(l_Fnd_Log_Message, c_api_name,9);
4628                     END IF;
4629                     IF l_record_count > 0 THEN
4630                        l_record_index := 1;
4631 
4632                        /* Fix for Bug#8673423. Populated secondary_uom_code from cache and
4633                           added secondary_uom_code and secondary_res_quantity in transfer_res
4634                        */
4635 
4636                       IF (get_rcv_transaction_rec.secondary_unit_of_measure IS NOT NULL ) THEN
4637                        IF (INV_CACHE.set_item_rec(get_rcv_transaction_rec.to_organization_id,
4638                                                   get_rcv_transaction_rec.item_id)) THEN
4639                            l_secondary_uom_code := INV_CACHE.item_rec.secondary_uom_code ;
4640 
4641                        END IF ;
4642                       END IF ;
4643 
4644                        LOOP
4645                           EXIT WHEN get_rcv_transaction_rec.primary_quantity = 0;
4646                           IF g_debug= C_Debug_Enabled THEN
4647                              l_Fnd_Log_Message := 'get_rcv_transaction_rec.primary_quantity: '||get_rcv_transaction_rec.primary_quantity;
4648                              mydebug(l_Fnd_Log_Message, c_api_name,9);
4649                              l_Fnd_Log_Message := 'get_rcv_transaction_rec.secondary_quantity: '||get_rcv_transaction_rec.secondary_quantity;
4650                              mydebug(l_Fnd_Log_Message, c_api_name,9);
4651                           END IF;
4652                           IF l_record_index <= l_record_count THEN
4653                              IF g_debug= C_Debug_Enabled THEN
4654                                 l_Fnd_Log_Message := 'l_record_index: '|| l_record_index;
4655                                 mydebug(l_Fnd_Log_Message, c_api_name,9);
4656                                 l_Fnd_Log_Message := 'l_rsv_array(l_record_index).primary_reservation_quantity:'
4657                                                       || l_rsv_array(l_record_index).primary_reservation_quantity;
4658                                 mydebug(l_Fnd_Log_Message, c_api_name,9);
4659                                 l_Fnd_Log_Message := 'calling transfer_res with folling IN paramter values:';
4660                                 mydebug(l_Fnd_Log_Message, c_api_name,9);
4661 
4662                              END IF;
4663 
4664                                -- 4891711 use lot level quantities where lot control is appropriate
4665 
4666 
4667 
4668                              IF l_rsv_array(l_record_index).primary_reservation_quantity
4669                                > NVL(get_rcv_lot_number_rec.primary_quantity,get_rcv_transaction_rec.primary_quantity) THEN
4670 
4671 
4672                                  TRANSFER_RES
4673                                  (p_from_reservation_id       => l_rsv_array(l_record_index).reservation_id
4674                                   ,p_from_source_header_id    => get_rcv_transaction_rec.po_header_id
4675                                   ,p_from_source_line_id      => get_rcv_transaction_rec.po_line_location_id
4676                                   ,p_supply_source_type_id    => inv_reservation_global.g_source_type_po
4677                                   ,p_to_supply_source_type_id => inv_reservation_global.g_source_type_inv
4678                                   ,p_subinventory_code        => get_rcv_transaction_rec.subinventory
4679                                   ,p_locator_id               => get_rcv_transaction_rec.locator_id
4680                                   ,p_lot_number               => NVL(get_rcv_lot_number_rec.lot_num, NULL)
4681                                   ,p_revision                 => get_rcv_transaction_rec.item_revision
4682                                   ,p_lpn_id                   => get_rcv_transaction_rec.lpn_id
4683                                   ,p_primary_uom_code         => get_rcv_transaction_rec.primary_unit_of_measure
4684                                   ,p_primary_res_quantity     => NVL(get_rcv_lot_number_rec.primary_quantity,
4685                                                                      get_rcv_transaction_rec.primary_quantity)
4686                                   ,p_secondary_uom_code       => l_secondary_uom_code
4687                                   ,p_secondary_res_quantity   => NVL(get_rcv_lot_number_rec.secondary_quantity,
4688                                                                      get_rcv_transaction_rec.secondary_quantity)
4689                                   ,x_msg_count                => x_msg_count
4690                                   ,x_msg_data                 => x_msg_data
4691                                   ,x_return_status            => l_return_status);
4692 
4693                                   get_rcv_transaction_rec.primary_quantity := get_rcv_transaction_rec.primary_quantity
4694                                                                                  - NVL(get_rcv_lot_number_rec.primary_quantity,
4695                                                                                        get_rcv_transaction_rec.primary_quantity);
4696 
4697                                   -- Bug 5611560 *** Possible case of split reservation .
4698                                   -- Set the Remaining reservation Quantity which is still can be transferred.
4699                                   -- The Lot can be fully satisfied by the Reservation so Set the Lot qty as Zero.
4700 
4701                                   l_rsv_array(l_record_index).primary_reservation_quantity :=
4702                                        l_rsv_array(l_record_index).primary_reservation_quantity
4703                                        - NVL(get_rcv_lot_number_rec.primary_quantity,get_rcv_transaction_rec.primary_quantity);
4704 
4705                                    --bug9888420 secondary reservation qty should be recalculated as well
4706                                    l_rsv_array(l_record_index).secondary_reservation_quantity :=
4707                                        l_rsv_array(l_record_index).secondary_reservation_quantity
4708                                        - NVL(get_rcv_lot_number_rec.secondary_quantity,get_rcv_transaction_rec.secondary_quantity
4709                                               );
4710 
4711                                   IF (l_lot_control_code = 2) then
4712                                      get_rcv_lot_number_rec.primary_quantity := 0;
4713                                   END IF;
4714 
4715                                   IF g_debug= C_Debug_Enabled THEN
4716                                     l_Fnd_Log_Message := 'Remaining reserv Qty := '||l_rsv_array(l_record_index).primary_reservation_quantity;
4717                                     l_Fnd_Log_Message := 'Remaining sec reserv Qty := '||l_rsv_array(l_record_index).secondary_reservation_quantity;
4718                                     mydebug(l_Fnd_Log_Message, c_api_name,9);
4719                                   END IF;
4720 
4721                              ELSE
4722                                   TRANSFER_RES
4723                                   (p_from_reservation_id       => l_rsv_array(l_record_index).reservation_id
4724                                   ,p_from_source_header_id     => get_rcv_transaction_rec.po_header_id
4725                                   ,p_from_source_line_id       => get_rcv_transaction_rec.po_line_location_id
4726                                   ,p_supply_source_type_id     => inv_reservation_global.g_source_type_po
4727                                   ,p_to_supply_source_type_id  => inv_reservation_global.g_source_type_inv
4728                                   ,p_subinventory_code         => get_rcv_transaction_rec.subinventory
4729                                   ,p_locator_id                => get_rcv_transaction_rec.locator_id
4730                                   ,p_lot_number                => NVL(get_rcv_lot_number_rec.lot_num, NULL)
4731                                   ,p_revision                  => get_rcv_transaction_rec.item_revision
4732                                   ,p_lpn_id                    => get_rcv_transaction_rec.lpn_id
4733                                   ,p_primary_uom_code          => get_rcv_transaction_rec.primary_unit_of_measure
4734                                   ,p_primary_res_quantity      => l_rsv_array(l_record_index).primary_reservation_quantity
4735                                   ,p_secondary_uom_code        => l_secondary_uom_code
4736                                   ,p_secondary_res_quantity    => l_rsv_array(l_record_index).secondary_reservation_quantity
4737                                   ,x_msg_count                 => x_msg_count
4738                                   ,x_msg_data                  => x_msg_data
4739                                   ,x_return_status             => l_return_status);
4740 
4741                                   get_rcv_transaction_rec.primary_quantity :=
4742                                     get_rcv_transaction_rec.primary_quantity - l_rsv_array(l_record_index).primary_reservation_quantity;
4743 
4744                                   -- Bug 5611560. reservation is fully consumed. So Set the
4745                                   -- Remaining resesrvation for this row as zero .
4746                                   -- Also Reduce the Lot Qty by the Reservation Qty transferred to Inv the same way
4747                                   -- as done above for delivered qty.
4748 
4749                                   IF (l_lot_control_code = 2) then
4750                                      get_rcv_lot_number_rec.primary_quantity := get_rcv_lot_number_rec.primary_quantity
4751                                      - l_rsv_array(l_record_index).primary_reservation_quantity;
4752 
4753                                      IF g_debug= C_Debug_Enabled THEN
4754                                          l_Fnd_Log_Message := 'Remaining lot Qty := '||get_rcv_lot_number_rec.primary_quantity;
4755                                          mydebug(l_Fnd_Log_Message, c_api_name,9);
4756                                      END IF;
4757                                   END IF;
4758 
4759                                   l_rsv_array(l_record_index).primary_reservation_quantity := 0;
4760                                   l_rsv_array(l_record_index).secondary_reservation_quantity :=0;  --bug9888420
4761 
4762                                   IF g_debug= C_Debug_Enabled THEN
4763                                       l_Fnd_Log_Message := 'Reservation Transfered completely for this Line';
4764                                       mydebug(l_Fnd_Log_Message, c_api_name,9);
4765                                   END IF;
4766 
4767                                END IF;
4768 
4769                                IF g_debug= C_Debug_Enabled THEN
4770                                   l_Fnd_Log_Message := 'l_return_status:'|| l_return_status;
4771                                   mydebug(l_Fnd_Log_Message, c_api_name,9);
4772                                END IF;
4773                           ELSE
4774                              IF g_debug= C_Debug_Enabled THEN
4775                                 l_Fnd_Log_Message := 'l_record_index > l_record_count. more received than reserved.
4776                                   l_record_count :' || l_record_count || ' l_record_index: '|| l_record_index;
4777                                 mydebug(l_Fnd_Log_Message, c_api_name,9);
4778                              END IF;
4779                              EXIT;
4780                           END IF;   -- res_qty > received_qty
4781                           -- 4891711 BEGIN need lot level looping
4782 
4783                           -- 5611560
4784                           -- Changed the Logic so that when reservation qty is set as zero the index is increased
4785                           -- Also when there is no more Lot rows just exit from the Loop;
4786                           --
4787                           IF (l_lot_control_code = 2) then
4788                               Begin
4789                                    IF get_rcv_lot_number_rec.primary_quantity = 0 then
4790                                       FETCH get_rcv_txn_lot_number INTO get_rcv_lot_number_rec;
4791                                       IF get_rcv_txn_lot_number%NOTFOUND THEN
4792                                          CLOSE get_rcv_txn_lot_number;
4793                                          --l_record_index := l_record_index + 1;
4794                                          -- No Need to Loop again Just Return from here.
4795                                          EXIT;
4796                                       END IF;
4797                                    END IF;
4798                               END;
4799                           END IF;
4800 
4801                           -- Bug 5611560
4802                           -- Increase the Index when no more reservation qty to be transfered.
4803                           IF ( l_rsv_array(l_record_index).primary_reservation_quantity = 0 ) THEN
4804                             l_record_index := l_record_index + 1;
4805                           End If;
4806 
4807                           -- 4891711 END
4808 
4809                        END LOOP;
4810                     ELSE -- l_record_count = 0
4811                          -- there is no reservation existing
4812                          -- log messeage
4813                          IF g_debug= C_Debug_Enabled THEN
4814                                l_Fnd_Log_Message := 'l_return_status:'|| l_return_status;
4815                                mydebug(l_Fnd_Log_Message, c_api_name,9);
4816                          END IF;
4817 
4818                     END IF;  -- l_record_count > 0
4819 
4820               END IF; -- get_rcv_transaction%found
4821 
4822           ELSIF get_rcv_transaction_rec.supply_type = 'ASN' THEN
4823              IF g_debug= C_Debug_Enabled THEN
4824                 mydebug( 'DTI. ASN.', c_api_name,9);
4825              END IF;
4826              OPEN  get_rcv_transaction_asn(p_transaction_id);
4827              FETCH get_rcv_transaction_asn into get_rcv_transaction_asn_rec;
4828              --CLOSE get_rcv_transaction_asn;
4829 
4830              IF get_rcv_transaction_asn%FOUND THEN
4831 
4832                 IF get_rcv_transaction_asn_rec.item_revision is NULL then
4833 
4834                   BEGIN
4835                      SELECT    max(mir.revision)
4836                        INTO    l_item_revision
4837                        FROM    mtl_system_items msi
4838                        , mtl_item_revisions mir
4839                        WHERE    msi.inventory_item_id = get_rcv_transaction_asn_rec.item_id
4840                        AND    msi.organization_id = get_rcv_transaction_asn_rec.organization_id
4841                        AND    msi.revision_qty_control_code = 2
4842                        AND    mir.organization_id = msi.organization_id
4843                        AND    mir.inventory_item_id = msi.inventory_item_id  --Bug# 10365814
4844                        AND    mir.effectivity_date in
4845                        (SELECT   MAX(mir2.effectivity_date)
4846                         FROM   mtl_item_revisions mir2
4847                         WHERE   mir2.organization_id = get_rcv_transaction_asn_rec.organization_id
4848                         AND   mir2.inventory_item_id = get_rcv_transaction_asn_rec.item_id
4849                         AND   mir2.effectivity_date <= SYSDATE
4850                         AND   mir2.implementation_date is not NULL);
4851 
4852 
4853                         get_rcv_transaction_rec.item_revision := l_item_revision;
4854 
4855                   EXCEPTION when others then
4856                      l_item_revision := NULL;
4857                      get_rcv_transaction_rec.item_revision := NULL;
4858                   END;
4859 
4860                  ELSE
4861                      /* Begin Bug 3688014 : If the rcv_shipment_lines contains a item revision
4862                      for a non-revision controlled item, we should pass a NULL
4863                        item revision to inventory so that supply gets updated accordingly.
4864                        */
4865                        BEGIN
4866 
4867                           SELECT msi.revision_qty_control_code
4868                             INTO l_revision_control_code
4869                             FROM mtl_system_items_b msi
4870                             WHERE msi.inventory_item_id = get_rcv_transaction_asn_rec.item_id
4871                             AND msi.organization_id = get_rcv_transaction_asn_rec.organization_id;
4872 
4873                           IF  l_revision_control_code = 1 THEN
4874                              get_rcv_transaction_asn_rec.item_revision := NULL;
4875                           END IF;
4876 
4877                        EXCEPTION
4878                           WHEN others THEN
4879                              get_rcv_transaction_asn_rec.item_revision := NULL;
4880                        END;
4881 
4882                        /* End Bug 3688014 */
4883 
4884                 END IF;  --get_rcv_transaction_asn_rec.item_revision is NULL
4885 
4886 
4887                 --Bug#3009495.If item is lot controlled then we will derive the lot number
4888                 --from the rcv_lot_transactions.
4889                 get_rcv_lot_number_rec.lot_num := NULL;
4890                 --IF get_shipment_lines%FOUND THEN
4891 
4892                 --Bug 7559682 Error performing deliver transaction for Direct Items in EAM workorder
4893                 --having destination type as shop floor. Modified the code block to include MSI query
4894                 BEGIN
4895                     select nvl(lot_control_code,1)
4896                       into   l_lot_control_code
4897                       from   mtl_system_items
4898                      where  organization_id = get_rcv_transaction_asn_rec.to_organization_id
4899                        and  inventory_item_id = get_rcv_transaction_asn_rec.item_id ;
4900 
4901                     IF (l_lot_control_code = 2) THEN
4902                        --Begin         --commented for bug 7559682
4903                           -- 5611560 New cursor is opened and fetched.
4904                           OPEN  get_rcv_txn_lot_number(get_rcv_transaction_asn_rec.shipment_line_id,p_transaction_id);
4905                           FETCH get_rcv_txn_lot_number INTO get_rcv_lot_number_rec;
4906                     END IF;
4907 
4908                 EXCEPTION
4909                   WHEN OTHERS THEN
4910                     get_rcv_lot_number_rec.lot_num := NULL;
4911                     l_lot_control_code := 1;
4912                       -- END;        --commented for bug 7559682
4913 
4914                     --End If;  --commented for bug 7559682
4915                 END;
4916                 -- End If; -- End Bug#3009495
4917 
4918                 query_res
4919                   (p_supply_source_header_id  => get_rcv_transaction_asn_rec.po_header_id
4920                    ,p_supply_source_line_id    => get_rcv_transaction_asn_rec.po_line_location_id
4921                    ,p_supply_source_type_id    => inv_reservation_global.g_source_type_po
4922                    ,p_project_id               => get_rcv_transaction_asn_rec.project_id
4923                    ,p_task_id                  => get_rcv_transaction_asn_rec.task_id
4924                    ,x_rsv_array                => l_rsv_array
4925                    ,x_record_count             => l_record_count
4926                    ,x_msg_count                => x_msg_count
4927                    ,x_msg_data                 => x_msg_data
4928                    ,x_return_status            => l_return_status);
4929 
4930 
4931                 IF l_record_count > 0 THEN
4932                    IF (get_rcv_transaction_asn_rec.primary_unit_of_measure <> l_rsv_array(1).primary_uom_code) THEN
4933 
4934                       l_primary_quantity := inv_convert.inv_um_convert
4935                         (
4936                          item_id        => get_rcv_transaction_asn_rec.item_id,
4937                          precision      => NULL,
4938                          from_quantity  => get_rcv_transaction_asn_rec.primary_quantity,
4939                          from_unit      => get_rcv_transaction_asn_rec.primary_unit_of_measure,
4940                          to_unit        => l_rsv_array(1).primary_uom_code,
4941                          from_name      => NULL,
4942                          to_name        => NULL);
4943 
4944                       get_rcv_transaction_asn_rec.primary_quantity := l_primary_quantity;
4945 
4946                    END IF;
4947 
4948                    l_record_index := 1;
4949 
4950                    /* Added for 15979250 */
4951                    /* Start */
4952                    IF (get_rcv_transaction_rec.secondary_unit_of_measure IS NOT NULL ) THEN
4953                        IF (INV_CACHE.set_item_rec(get_rcv_transaction_asn_rec.to_organization_id,
4954                                                   get_rcv_transaction_asn_rec.item_id)) THEN
4955                            l_secondary_uom_code := INV_CACHE.item_rec.secondary_uom_code ;
4956 
4957                        END IF ;
4958                    END IF ;
4959                    /* end for 15979250 */
4960 
4961                    LOOP
4962 
4963                       EXIT WHEN get_rcv_transaction_asn_rec.primary_quantity = 0;
4964                           IF g_debug= C_Debug_Enabled THEN
4965                              l_Fnd_Log_Message := 'get_rcv_transaction_asn_rec.primary_quantity: '||get_rcv_transaction_asn_rec.primary_quantity;
4966                              mydebug(l_Fnd_Log_Message, c_api_name,9);
4967                           END IF;
4968 
4969                       IF l_record_index <= l_record_count THEN
4970                           IF g_debug= C_Debug_Enabled THEN
4971                                 l_Fnd_Log_Message := 'l_record_index: '|| l_record_index;
4972                                 mydebug(l_Fnd_Log_Message, c_api_name,9);
4973                                 l_Fnd_Log_Message := 'l_rsv_array(l_record_index).primary_reservation_quantity:'
4974                                                       || l_rsv_array(l_record_index).primary_reservation_quantity;
4975                                 mydebug(l_Fnd_Log_Message, c_api_name,9);
4976                                 l_Fnd_Log_Message := 'calling transfer_res with folling IN paramter values:';
4977                                 mydebug(l_Fnd_Log_Message, c_api_name,9);
4978 
4979                           END IF;
4980 
4981                          IF l_rsv_array(l_record_index).primary_reservation_quantity
4982                                  >  NVL(get_rcv_lot_number_rec.primary_quantity,get_rcv_transaction_asn_rec.primary_quantity) THEN
4983                             TRANSFER_RES
4984                               (p_from_reservation_id           => l_rsv_array(l_record_index).reservation_id
4985                                , p_from_source_header_id     => get_rcv_transaction_asn_rec.po_header_id
4986                                ,p_from_source_line_id       => get_rcv_transaction_asn_rec.po_line_location_id
4987                                ,p_supply_source_type_id     => inv_reservation_global.g_source_type_po
4988                                ,p_to_supply_source_type_id  => inv_reservation_global.g_source_type_inv
4989                                ,p_subinventory_code         => get_rcv_transaction_rec.subinventory
4990                                ,p_locator_id                => get_rcv_transaction_rec.locator_id
4991                                ,p_lot_number                => NVL(get_rcv_lot_number_rec.lot_num, NULL)
4992                                ,p_revision                  => get_rcv_transaction_asn_rec.item_revision
4993                                ,p_lpn_id                    => get_rcv_transaction_asn_rec.lpn_id
4994                                ,p_primary_uom_code          => get_rcv_transaction_asn_rec.primary_unit_of_measure
4995                                ,p_primary_res_quantity      => NVL(get_rcv_lot_number_rec.primary_quantity,
4996                                                                      get_rcv_transaction_asn_rec.primary_quantity)
4997                                --,p_action                    => l_action  it is never used. I remove it from procedure transfer_res
4998 
4999 		               ,p_secondary_uom_code       => l_secondary_uom_code /* 15979250 */
5000 			       ,p_secondary_res_quantity   => NVL(get_rcv_lot_number_rec.secondary_quantity,
5001                                                                         get_rcv_transaction_asn_rec.secondary_quantity) /* 15979250 */
5002                               ,x_msg_count                 => x_msg_count
5003                               ,x_msg_data                  => x_msg_data
5004                               ,x_return_status             => l_return_status);
5005 
5006                             -- Bug 12960837 *** Possible case of split reservation .
5007                             -- Set the Remaining reservation Quantity which is still can be transferred.
5008                             -- The Lot can be fully satisfied by the Reservation so Set the Lot qty as Zero.
5009 
5010                             get_rcv_transaction_asn_rec.primary_quantity := get_rcv_transaction_asn_rec.primary_quantity
5011                                                                                   - NVL(get_rcv_lot_number_rec.primary_quantity,
5012                                                                                      get_rcv_transaction_asn_rec.primary_quantity);
5013 
5014                             l_rsv_array(l_record_index).primary_reservation_quantity :=
5015                                        l_rsv_array(l_record_index).primary_reservation_quantity
5016                                        - NVL(get_rcv_lot_number_rec.primary_quantity,get_rcv_transaction_asn_rec.primary_quantity);
5017 
5018                             /* Start of 15979250 */
5019                             get_rcv_transaction_asn_rec.secondary_quantity := get_rcv_transaction_asn_rec.secondary_quantity
5020                                                                                   - NVL(get_rcv_lot_number_rec.secondary_quantity,
5021                                                                                      get_rcv_transaction_asn_rec.secondary_quantity);
5022 
5023                             l_rsv_array(l_record_index).secondary_reservation_quantity :=
5024                                        l_rsv_array(l_record_index).secondary_reservation_quantity
5025                                        - NVL(get_rcv_lot_number_rec.secondary_quantity,get_rcv_transaction_asn_rec.secondary_quantity);
5026                             /* End of 15979250 */
5027 
5028                             IF (l_lot_control_code = 2) then
5029                                 get_rcv_lot_number_rec.primary_quantity := 0;
5030                             END IF;
5031 
5032                             IF g_debug= C_Debug_Enabled THEN
5033                                 l_Fnd_Log_Message := 'Remaining reserv Qty := '||l_rsv_array(l_record_index).primary_reservation_quantity;
5034                                 mydebug(l_Fnd_Log_Message, c_api_name,9);
5035                             END IF;
5036 
5037                           ELSE
5038                             TRANSFER_RES
5039                               (p_from_reservation_id           => l_rsv_array(l_record_index).reservation_id
5040                                ,p_from_source_header_id     => get_rcv_transaction_asn_rec.po_header_id
5041                                ,p_from_source_line_id       => get_rcv_transaction_asn_rec.po_line_location_id
5042                                ,p_supply_source_type_id     => inv_reservation_global.g_source_type_po
5043                                ,p_to_supply_source_type_id  => inv_reservation_global.g_source_type_inv
5044                                ,p_subinventory_code         => get_rcv_transaction_asn_rec.subinventory
5045                                ,p_locator_id                => get_rcv_transaction_asn_rec.locator_id
5046                                ,p_lot_number                => NVL(get_rcv_lot_number_rec.lot_num, NULL)
5047                                ,p_revision                  => get_rcv_transaction_asn_rec.item_revision
5048                                ,p_lpn_id                    => get_rcv_transaction_asn_rec.lpn_id
5049                                ,p_primary_uom_code          => get_rcv_transaction_asn_rec.primary_unit_of_measure
5050                                ,p_primary_res_quantity      => l_rsv_array(l_record_index).primary_reservation_quantity
5051                                -- ,p_action                    IN VARCHAR2  DEFAULT NULL
5052 		               ,p_secondary_uom_code       => l_secondary_uom_code /* 15979250 */
5053 			       ,p_secondary_res_quantity   => nvl(get_rcv_lot_number_rec.secondary_quantity, l_rsv_array(l_record_index).secondary_reservation_quantity) /* 15979250 */
5054                               ,x_msg_count                 => x_msg_count
5055                               ,x_msg_data                  => x_msg_data
5056                               ,x_return_status             => l_return_status);
5057 
5058                             get_rcv_transaction_asn_rec.primary_quantity := get_rcv_transaction_asn_rec.primary_quantity
5059                               - l_rsv_array(l_record_index).primary_reservation_quantity;
5060 
5061                             -- Bug 12960837. reservation is fully consumed. So Set the Remaining resesrvation for this row as zero .
5062                             -- Also Reduce the Lot Qty by the Reservation Qty transferred to Inv the same way
5063                             -- as done above for delivered qty.
5064 
5065                             IF (l_lot_control_code = 2) then
5066                                 get_rcv_lot_number_rec.primary_quantity := get_rcv_lot_number_rec.primary_quantity
5067                                    - l_rsv_array(l_record_index).primary_reservation_quantity;
5068 
5069                                 IF g_debug= C_Debug_Enabled THEN
5070                                       l_Fnd_Log_Message := 'Remaining lot Qty := '||get_rcv_lot_number_rec.primary_quantity;
5071                                       mydebug(l_Fnd_Log_Message, c_api_name,9);
5072                                 END IF;
5073                             END IF;
5074 
5075                             l_rsv_array(l_record_index).primary_reservation_quantity := 0;
5076 
5077                             IF g_debug= C_Debug_Enabled THEN
5078                                     l_Fnd_Log_Message := 'Reservation Transfered completely for this Line';
5079                                     mydebug(l_Fnd_Log_Message, c_api_name,9);
5080                             END IF;
5081                          END IF;
5082                        ELSE
5083                          -- raise error: since received more qty than reserved qty ??
5084                          IF g_debug= C_Debug_Enabled THEN
5085                             l_Fnd_Log_Message := 'l_record_index > l_record_count. more received than reserved.
5086                             l_record_count :' || l_record_count || ' l_record_index: ' || l_record_index;
5087                             mydebug(l_Fnd_Log_Message, c_api_name,9);
5088                          END IF;
5089                          EXIT;
5090                       END IF;   -- res_qty > received_qty
5091                       -- 12960837
5092                       -- Changed the Logic so that when reservation qty is set as zero the index is increased
5093                       -- Also when there is no more Lot rows just exit from the Loop;
5094                       --
5095                       IF (l_lot_control_code = 2) then
5096                           Begin
5097                                IF get_rcv_lot_number_rec.primary_quantity = 0 then
5098                                   FETCH get_rcv_txn_lot_number INTO get_rcv_lot_number_rec;
5099                                   IF get_rcv_txn_lot_number%NOTFOUND THEN
5100                                      CLOSE get_rcv_txn_lot_number;
5101                                      --l_record_index := l_record_index + 1;
5102                                      -- No Need to Loop again Just Return from here.
5103                                      EXIT;
5104                                   END IF;
5105                                END IF;
5106                           END;
5107                       END IF;
5108 
5109                       -- Bug 12960837
5110                       -- Increase the Index when no more reservation qty to be transfered.
5111                       IF ( l_rsv_array(l_record_index).primary_reservation_quantity = 0 ) THEN
5112                         l_record_index := l_record_index + 1;
5113                       End If;
5114 
5115                    END LOOP;
5116                   ELSE -- l_record_count = 0
5117                          -- there is no reservation existing
5118                          -- log messeage
5119                          IF g_debug= C_Debug_Enabled THEN
5120                                l_Fnd_Log_Message := 'l_return_status:'|| l_return_status;
5121                                mydebug(l_Fnd_Log_Message, c_api_name,9);
5122                          END IF;
5123                 END IF;  -- l_record_count > 0
5124              END IF;  -- %FOUND
5125              CLOSE get_rcv_transaction_asn;
5126             END IF;
5127 
5128           ELSIF get_source_doc_code_rec.source_document_code = 'REQ' THEN
5129              IF g_debug= C_Debug_Enabled THEN
5130                 mydebug( 'DTI. Requisition.', c_api_name,9);
5131              END IF;
5132              OPEN  get_rcv_txn_int_req(p_transaction_id);
5133              FETCH get_rcv_txn_int_req into get_rcv_txn_int_req_rec;
5134              --CLOSE get_rcv_txn_int_req;
5135 
5136              IF get_rcv_txn_int_req%FOUND THEN
5137 
5138                 IF g_debug= C_Debug_Enabled THEN
5139                    mydebug( 'Item revision' || get_rcv_txn_int_req_rec.item_revision, c_api_name,9);
5140                 END IF;
5141                 IF get_rcv_txn_int_req_rec.item_revision is NULL then
5142 
5143                    BEGIN
5144                       SELECT    max(mir.revision)
5145                         INTO    l_item_revision
5146                         FROM    mtl_system_items msi
5147                         , mtl_item_revisions mir
5148                         WHERE    msi.inventory_item_id = get_rcv_txn_int_req_rec.item_id
5149                         AND    msi.organization_id = get_rcv_txn_int_req_rec.organization_id
5150                         AND    msi.revision_qty_control_code = 2
5151                         AND    mir.organization_id = msi.organization_id
5152                         AND    mir.inventory_item_id = msi.inventory_item_id  --Bug# 10365814
5153                         AND    mir.effectivity_date in
5154                         (SELECT   MAX(mir2.effectivity_date)
5155                          FROM   mtl_item_revisions mir2
5156                          WHERE   mir2.organization_id = get_rcv_txn_int_req_rec.organization_id
5157                          AND   mir2.inventory_item_id = get_rcv_txn_int_req_rec.item_id
5158                          AND   mir2.effectivity_date <= SYSDATE
5159                          AND   mir2.implementation_date is not NULL);
5160 
5161 
5162                          get_rcv_txn_int_req_rec.item_revision := l_item_revision;
5163 
5164                    EXCEPTION when others then
5165                       l_item_revision := NULL;
5166                    END;
5167 
5168                  ELSE
5169                       IF g_debug= C_Debug_Enabled THEN
5170                          mydebug( 'Inside rev not null', c_api_name,9);
5171                          mydebug( 'Item id :' || get_rcv_txn_int_req_rec.item_id, c_api_name,9);
5172                          mydebug( 'Org :' || get_rcv_txn_int_req_rec.organization_id, c_api_name,9);
5173                       END IF;
5174                       /* Begin Bug 3688014 : If the rcv_shipment_lines contains a item revision
5175                       for a non-revision controlled item, we should pass a NULL
5176                         item revision to inventory so that supply gets updated accordingly.
5177                         */
5178                         --Bug 5147013: Changed the cursor name to get the
5179                         -- item and org from the correct one.
5180                         BEGIN
5181                            SELECT msi.revision_qty_control_code
5182                              INTO l_revision_control_code
5183                              FROM mtl_system_items_b msi
5184                              WHERE msi.inventory_item_id = get_rcv_txn_int_req_rec.item_id
5185                              AND msi.organization_id = get_rcv_txn_int_req_rec.organization_id;
5186 
5187                            IF g_debug= C_Debug_Enabled THEN
5188                               mydebug( 'rev control is: ' || l_revision_control_code, c_api_name,9);
5189                            END IF;
5190                            IF  l_revision_control_code = 1 THEN
5191                               get_rcv_txn_int_req_rec.item_revision := NULL;
5192                            END IF;
5193 
5194                         EXCEPTION
5195                            WHEN no_data_found THEN
5196                               get_rcv_txn_int_req_rec.item_revision := NULL;
5197                            WHEN OTHERS THEN
5198                               get_rcv_txn_int_req_rec.item_revision := NULL;
5199                         END;
5200 
5201                         /* End Bug 3688014 */
5202 
5203                 END IF;  --get_rcv_txn_req_rec.item_revision is NULL
5204 
5205 
5206                --Bug#3009495.If item is lot controlled then we will derive the lot number
5207                 --from the rcv_lot_transactions.
5208                 get_rcv_lot_number_rec.lot_num := NULL;
5209                 --IF get_shipment_lines%FOUND THEN
5210 
5211                 --Bug 7559682 Error performing deliver transaction for Direct Items in EAM workorder
5212                 --having destination type as shop floor. Modified the code block to include MSI query
5213                 BEGIN
5214 
5215                     IF g_debug= C_Debug_Enabled THEN
5216                         mydebug( 'Org_id = '||get_rcv_txn_int_req_rec.to_organization_id, c_api_name,9);
5217                         mydebug( 'Item_id = '||get_rcv_txn_int_req_rec.item_id, c_api_name,9);
5218                     END IF;
5219 
5220                     select nvl(lot_control_code,1)
5221                       into   l_lot_control_code
5222                       from   mtl_system_items
5223                      where  organization_id = get_rcv_txn_int_req_rec.to_organization_id
5224                        and  inventory_item_id = get_rcv_txn_int_req_rec.item_id ;
5225 
5226                     IF g_debug= C_Debug_Enabled THEN
5227                         mydebug( 'Lot Control Code = '||l_lot_control_code, c_api_name,9);
5228                     END IF;
5229 
5230                     IF (l_lot_control_code = 2) THEN
5231                        --Begin         --commented for bug 7559682
5232                           -- 5611560 New cursor is opened and fetched.
5233 
5234                           IF g_debug= C_Debug_Enabled THEN
5235                                 mydebug( 'RSL_ID = '||get_rcv_txn_int_req_rec.shipment_line_id, c_api_name,9);
5236                           END IF;
5237                           OPEN  get_rcv_txn_lot_number(get_rcv_txn_int_req_rec.shipment_line_id,p_transaction_id);
5238                           FETCH get_rcv_txn_lot_number INTO get_rcv_lot_number_rec;
5239                     END IF;
5240 
5241                     IF g_debug= C_Debug_Enabled THEN
5242                         mydebug( 'get_rcv_lot_number_rec.lot_num = '||get_rcv_lot_number_rec.lot_num, c_api_name,9);
5243                     END IF;
5244 
5245                 EXCEPTION
5246                   WHEN OTHERS THEN
5247                     IF g_debug= C_Debug_Enabled THEN
5248                         mydebug( 'Fetch Lot Control Code Exc: '||SQLERRM, c_api_name,9);
5249                     END IF;
5250                     get_rcv_lot_number_rec.lot_num := NULL;
5251                     l_lot_control_code := 1;
5252                       -- END;        --commented for bug 7559682
5253 
5254                     --End If;  --commented for bug 7559682
5255                 END;
5256                 -- End If; -- End Bug#3009495
5257 
5258                 query_res
5259                   (p_supply_source_header_id  => get_rcv_txn_int_req_rec.requisition_header_id
5260                    ,p_supply_source_line_id    => get_rcv_txn_int_req_rec.requisition_line_id
5261                    ,p_supply_source_type_id    => inv_reservation_global.g_source_type_internal_req
5262                    ,p_project_id               => get_rcv_txn_int_req_rec.project_id
5263                    ,p_task_id                  => get_rcv_txn_int_req_rec.task_id
5264                    ,x_rsv_array                => l_rsv_array
5265                    ,x_record_count             => l_record_count
5266                    ,x_msg_count                => x_msg_count
5267                    ,x_msg_data                 => x_msg_data
5268                    ,x_return_status            => l_return_status);
5269 
5270                 IF l_record_count > 0 THEN
5271                    IF (get_rcv_txn_int_req_rec.primary_unit_of_measure <> l_rsv_array(1).primary_uom_code) THEN
5272                       l_primary_quantity := inv_convert.inv_um_convert
5273                         (
5274                          item_id        => get_rcv_txn_int_req_rec.item_id,
5275                          precision      => NULL,
5276                          from_quantity  => get_rcv_txn_int_req_rec.primary_quantity,
5277                          from_unit      => get_rcv_txn_int_req_rec.primary_unit_of_measure,
5278                          to_unit        => l_rsv_array(1).primary_uom_code,
5279                          from_name      => NULL,
5280                          to_name        => NULL);
5281 
5282                       get_rcv_txn_int_req_rec.primary_quantity := l_primary_quantity;
5283 
5284                    END IF;
5285 
5286                    l_record_index := 1;
5287 
5288                    /* Fix for Bug#8673423. Populated secondary_uom_code from cache and
5289                       added secondary_uom_code and secondary_res_quantity in transfer_res
5290                    */
5291 
5292                    IF (get_rcv_txn_int_req_rec.secondary_unit_of_measure IS NOT NULL ) THEN
5293                      IF (INV_CACHE.set_item_rec(get_rcv_txn_int_req_rec.to_organization_id,
5294                                              get_rcv_txn_int_req_rec.item_id)) THEN
5295                         l_secondary_uom_code := INV_CACHE.item_rec.secondary_uom_code ;
5296 
5297                      END IF ;
5298                    END IF ;
5299 
5300                    LOOP
5301 
5302                      EXIT WHEN get_rcv_txn_int_req_rec.primary_quantity = 0;
5303 
5304                      IF l_record_index <= l_record_count THEN
5305 
5306                         IF l_rsv_array(l_record_index).primary_reservation_quantity > get_rcv_txn_int_req_rec.primary_quantity THEN
5307 
5308                            /* Fix for Bug#8673423. Added secondary_uom_code and secondary_res_quantity */
5309 
5310                            TRANSFER_RES
5311                              (p_from_reservation_id           => l_rsv_array(l_record_index).reservation_id
5312                               ,p_from_source_header_id     => get_rcv_txn_int_req_rec.requisition_header_id
5313                               ,p_from_source_line_id       => get_rcv_txn_int_req_rec.requisition_line_id
5314                               ,p_supply_source_type_id     => inv_reservation_global.g_source_type_internal_req
5315                               ,p_to_supply_source_type_id  => inv_reservation_global.g_source_type_inv
5316                               ,p_subinventory_code         => get_rcv_txn_int_req_rec.subinventory
5317                               ,p_locator_id                => get_rcv_txn_int_req_rec.locator_id
5318                               ,p_lot_number                => NVL(get_rcv_lot_number_rec.lot_num, NULL)
5319                               ,p_revision                  => get_rcv_txn_int_req_rec.item_revision
5320                               ,p_lpn_id                    => get_rcv_txn_int_req_rec.lpn_id
5321                               ,p_primary_uom_code          => get_rcv_txn_int_req_rec.primary_unit_of_measure
5322                                             ,p_primary_res_quantity      => get_rcv_txn_int_req_rec.primary_quantity
5323                               --,p_action                    => l_action  it is never used. I remove it from procedure transfer_res
5324                              ,p_secondary_uom_code          => l_secondary_uom_code
5325                              ,p_secondary_res_quantity   => NVL(get_rcv_lot_number_rec.secondary_quantity,
5326                                                                 get_rcv_txn_int_req_rec.secondary_quantity)
5327                              ,x_msg_count                 => x_msg_count
5328                              ,x_msg_data                  => x_msg_data
5329                              ,x_return_status             => l_return_status);
5330                                   get_rcv_txn_int_req_rec.primary_quantity := 0;
5331                          ELSE
5332                            TRANSFER_RES
5333                              (p_from_reservation_id           => l_rsv_array(l_record_index).reservation_id
5334                               ,p_from_source_header_id     => get_rcv_txn_int_req_rec.requisition_header_id
5335                               ,p_from_source_line_id       => get_rcv_txn_int_req_rec.requisition_line_id
5336                                              ,p_supply_source_type_id     => inv_reservation_global.g_source_type_internal_req
5337                               ,p_to_supply_source_type_id  => inv_reservation_global.g_source_type_inv
5338                               ,p_subinventory_code         => get_rcv_txn_int_req_rec.subinventory
5339                               ,p_locator_id                => get_rcv_txn_int_req_rec.locator_id
5340                               ,p_lot_number                => NVL(get_rcv_lot_number_rec.lot_num, NULL)
5341                               ,p_revision                  => get_rcv_txn_int_req_rec.item_revision
5342                               ,p_lpn_id                    => get_rcv_txn_int_req_rec.lpn_id
5343                               ,p_primary_uom_code          => get_rcv_txn_int_req_rec.primary_unit_of_measure
5344                               ,p_primary_res_quantity      => l_rsv_array(l_record_index).primary_reservation_quantity
5345                               ,p_secondary_uom_code        => l_secondary_uom_code
5346                               ,p_secondary_res_quantity   => NVL(get_rcv_lot_number_rec.secondary_quantity,
5347                                                                 get_rcv_txn_int_req_rec.secondary_quantity)
5348                              -- ,p_action                    IN VARCHAR2  DEFAULT NULL
5349                              ,x_msg_count                 => x_msg_count
5350                              ,x_msg_data                  => x_msg_data
5351                              ,x_return_status             => l_return_status);
5352                            get_rcv_txn_int_req_rec.primary_quantity := get_rcv_txn_int_req_rec.primary_quantity
5353                              - l_rsv_array(l_record_index).primary_reservation_quantity;
5354                         END IF;
5355                       ELSE
5356                         -- raise error: since received more qty than reserved qty ??
5357                         IF g_debug= C_Debug_Enabled THEN
5358                            l_Fnd_Log_Message := 'l_record_index > l_record_count. more received than reserved.
5359                               l_record_count :' || l_record_count || ' l_record_index: '|| l_record_index;
5360                            mydebug(l_Fnd_Log_Message, c_api_name,9);
5361                         END IF;
5362                         EXIT;
5363                      END IF;   -- res_qty > received_qty
5364                      l_record_index := l_record_index + 1;
5365 
5366                   END LOOP;
5367                  ELSE -- l_record_count = 0
5368                                 -- there is no reservation existing
5369                                 -- log messeage
5370                         IF g_debug= C_Debug_Enabled THEN
5371                            l_Fnd_Log_Message := 'l_return_status:'|| l_return_status;
5372                            mydebug(l_Fnd_Log_Message, c_api_name,9);
5373                         END IF;
5374                 END IF;  -- l_record_count > 0
5375              END IF;  -- %FOUND
5376              CLOSE get_rcv_txn_int_req;
5377           ELSE
5378                    -- do nothing. DO not support reservations for In-transit shipments in inventory orgs
5379                    -- log message
5380                    IF g_debug= C_Debug_Enabled THEN
5381                       mydebug( 'Other source type. Do not support: ' || get_source_doc_code_rec.source_document_code, c_api_name,9);
5382                    END IF;
5383                    NULL;
5384          END IF;  -- if supply_type = 'PO'
5385 
5386       END IF;  -- if wms org
5387 
5388 
5389       -- 5611560
5390       IF get_rcv_txn_lot_number%ISOPEN THEN
5391          CLOSE get_rcv_txn_lot_number;
5392       END IF;
5393 
5394       IF get_rcv_lot_number%ISOPEN THEN
5395          CLOSE get_rcv_lot_number;
5396       END IF;
5397 
5398       IF get_rcv_transaction%ISOPEN THEN
5399          CLOSE get_rcv_transaction;
5400       END IF;
5401 
5402       IF get_source_doc_code%isopen THEN
5403          CLOSE get_source_doc_code;
5404       END IF;
5405 
5406    ELSIF upper(p_action) = 'APPROVE_BLANKET_RELEASE_SUPPLY'  THEN
5407       IF g_debug= C_Debug_Enabled THEN
5408          mydebug( 'Approve Blanket Release Supply.', c_api_name,9);
5409       END IF;
5410 
5411       OPEN get_po_shipment_for_release(p_header_id);
5412       LOOP
5413           FETCH get_po_shipment_for_release INTO get_po_shipment_rel_rec;
5414           EXIT WHEN get_po_shipment_for_release%NOTFOUND;
5415 
5416           IF g_debug= C_Debug_Enabled THEN
5417              mydebug( 'get_po_shipment_rel_rec.line_location_id: '||get_po_shipment_rel_rec.line_location_id, c_api_name,9);
5418           END IF;
5419 
5420           IF NOT EXISTS_RESERVATION(p_supply_source_line_id => get_po_shipment_rel_rec.line_location_id) THEN
5421 
5422              IF g_debug= C_Debug_Enabled THEN
5423                 mydebug( 'No reservations exist for blanket release', c_api_name,9);
5424              END IF;
5425 
5426              OPEN get_req_line_of_po_shipment(get_po_shipment_rel_rec.line_location_id);
5427              LOOP
5428                  FETCH get_req_line_of_po_shipment INTO get_req_line_po_shipment_rec;
5429                  EXIT WHEN get_req_line_of_po_shipment%NOTFOUND;
5430 
5431                  IF g_debug= C_Debug_Enabled THEN
5432                     l_Fnd_Log_Message := 'Before get_req_line_po_shipment_rec.project_id: '||get_req_line_po_shipment_rec.project_id;
5433                     mydebug(l_Fnd_Log_Message, c_api_name,9);
5434                     l_Fnd_Log_Message := 'before get_req_line_po_shipment_rec.task_id: '||get_req_line_po_shipment_rec.task_id;
5435                     mydebug(l_Fnd_Log_Message, c_api_name,9);
5436                  END IF;
5437 
5438                  IF (get_req_line_po_shipment_rec.project_id = -99) THEN
5439                     get_req_line_po_shipment_rec.project_id := NULL;
5440                  END IF;
5441                  IF (get_req_line_po_shipment_rec.task_id = -99) THEN
5442                     get_req_line_po_shipment_rec.task_id := NULL;
5443                  END IF;
5444 
5445                  IF g_debug= C_Debug_Enabled THEN
5446                     l_Fnd_Log_Message := 'after get_req_line_po_shipment_rec.project_id: '||get_req_line_po_shipment_rec.project_id;
5447                     mydebug(l_Fnd_Log_Message, c_api_name,9);
5448                     l_Fnd_Log_Message := 'after get_req_line_po_shipment_rec.task_id: '||get_req_line_po_shipment_rec.task_id;
5449                     mydebug(l_Fnd_Log_Message, c_api_name,9);
5450                  END IF;
5451 
5452                --  IF   (get_req_line_po_shipment_rec.project_id IS NOT NULL
5453                  --   OR
5454                    --    get_req_line_po_shipment_rec.task_id IS NOT NULL) THEN
5455 
5456                  IF g_debug= C_Debug_Enabled THEN
5457                     l_Fnd_Log_Message := 'get_req_line_po_shipment_rec.project_id or task_id is not null. Calling availibitly API';
5458                     mydebug(l_Fnd_Log_Message, c_api_name,9);
5459                  END IF;
5460 
5461                  INV_RESERVATION_AVAIL_PVT.available_supply_to_reserve
5462                    (
5463                     x_return_status            =>l_return_status
5464                     , x_msg_count                =>x_msg_count
5465                     , x_msg_data                 =>x_msg_data
5466                     , p_organization_id          =>get_po_shipment_rel_rec.ship_to_organization_id
5467                     , p_item_id                  =>get_po_shipment_rel_rec.item_id
5468                     , p_supply_source_type_id    =>inv_reservation_global.g_source_type_po
5469                     , p_supply_source_header_id  =>get_po_shipment_rel_rec.po_header_id
5470                     , p_supply_source_line_id    =>get_po_shipment_rel_rec.line_location_id
5471                     , p_project_id               =>get_req_line_po_shipment_rec.project_id
5472                     , p_task_id                  =>get_req_line_po_shipment_rec.task_id
5473                     , x_qty_available_to_reserve =>l_qty_avail_to_reserve
5474                     , x_qty_available            =>l_qty_avail
5475                     );
5476 
5477                  IF g_debug= C_Debug_Enabled THEN
5478                     l_Fnd_Log_Message := 'l_qty_avail_to_reserve: '|| l_qty_avail_to_reserve;
5479                     mydebug(l_Fnd_Log_Message, c_api_name,9);
5480                     l_Fnd_Log_Message := 'l_qty_avail: '|| l_qty_avail;
5481                     mydebug(l_Fnd_Log_Message, c_api_name,9);
5482                  END IF;
5483 
5484                  get_req_line_res
5485                    ( p_req_line_id  =>get_req_line_po_shipment_rec.req_line_id
5486                      ,p_project_id  =>get_req_line_po_shipment_rec.project_id
5487                      ,p_task_id     =>get_req_line_po_shipment_rec.task_id
5488                      ,x_res_array   =>l_rsv_array
5489                      ,x_record_count =>l_record_count);
5490 
5491                  IF g_debug= C_Debug_Enabled THEN
5492                     l_Fnd_Log_Message := 'l_record_count: '|| l_record_count;
5493                     mydebug(l_Fnd_Log_Message, c_api_name,9);
5494                  END IF;
5495 
5496                  IF l_record_count > 0 THEN
5497                     l_rsv_rec := l_rsv_array(1);
5498                     /*
5499                     uom_conversion(l_rsv_rec.reservation_uom_code
5500                       ,l_rsv_rec.primary_uom_code
5501                       ,get_req_line_po_shipment_rec.quantity_ordered
5502                       ,get_po_shipment_rel_rec.po_line_id
5503                       ,l_reservation_quantity
5504                       ,l_po_primary_qty); */
5505 
5506                       IF (l_rsv_rec.primary_reservation_quantity > l_qty_avail_to_reserve)  THEN
5507                          update_res
5508                            (p_supply_source_header_id => l_rsv_rec.supply_source_header_id
5509                             ,p_supply_source_line_id  => l_rsv_rec.supply_source_line_id
5510                             ,p_supply_source_type_id  => inv_reservation_global.g_source_type_req
5511                             ,p_primary_reservation_quantity => l_qty_avail_to_reserve
5512                             ,p_project_id  => get_req_line_po_shipment_rec.project_id
5513                             ,p_task_id                      => get_req_line_po_shipment_rec.task_id
5514                             ,p_reservation_id               => l_rsv_rec.reservation_id
5515                             ,x_msg_count                    => x_msg_count
5516                             ,x_msg_data                     => x_msg_data
5517                             ,x_return_status                => l_return_status);
5518                       END IF;
5519 
5520                       --BUG#3497445.The l_po_primary_qty calculated from the UOM_conversion call is used below.
5521 
5522                       l_primary_res_quantity  := least(l_rsv_rec.primary_reservation_quantity, l_qty_avail_to_reserve);
5523 
5524                       IF g_debug= C_Debug_Enabled THEN
5525                          l_Fnd_Log_Message := 'l_primary_res_quantity: '|| l_primary_res_quantity;
5526                          mydebug(l_Fnd_Log_Message, c_api_name,9);
5527                       END IF;
5528 
5529                       IF g_debug= C_Debug_Enabled THEN
5530                          l_Fnd_Log_Message := 'calling transfer reservation for, From req line id: '|| l_rsv_rec.supply_source_line_id
5531                            || ' to po shipment line id: ' || get_po_shipment_rel_rec.line_location_id;
5532                          mydebug(l_Fnd_Log_Message, c_api_name,9);
5533                       END IF;
5534 
5535                       TRANSFER_RES
5536                         (p_from_reservation_id        =>l_rsv_rec.reservation_id
5537                          ,p_from_source_header_id     =>l_rsv_rec.supply_source_header_id
5538                          ,p_from_source_line_id       =>l_rsv_rec.supply_source_line_id
5539                          ,p_supply_source_type_id     =>inv_reservation_global.g_source_type_req
5540                          ,p_to_source_header_id       =>get_po_shipment_rel_rec.po_header_id
5541                          ,p_to_source_line_id         =>get_po_shipment_rel_rec.line_location_id
5542                          ,p_to_supply_source_type_id  =>inv_reservation_global.g_source_type_po
5543                          ,p_primary_uom_code          =>l_rsv_rec.primary_uom_code
5544                          ,p_primary_res_quantity      =>l_primary_res_quantity
5545                          ,x_msg_count                 =>x_msg_count
5546                          ,x_msg_data                  =>x_msg_data
5547                          ,x_return_status             =>l_return_status);
5548                       IF g_debug= C_Debug_Enabled THEN
5549                          l_Fnd_Log_Message := 'after calling transfer_res. The l_return_status : '|| l_return_status;
5550                          mydebug(l_Fnd_Log_Message, c_api_name,9);
5551                       END IF;
5552 
5553                   ELSE -- if did not have any req reservation existing
5554                     IF g_debug= C_Debug_Enabled THEN
5555                        FND_MESSAGE.SET_NAME('INV','INV_API_NO_RSV_EXIST');
5556                        FND_MSG_PUB.Add;
5557                        l_Fnd_Log_Message := 'l_record_count < 0';
5558                        mydebug(l_Fnd_Log_Message, c_api_name,9);
5559                     END IF;
5560                  END IF; -- record_count
5561              END LOOP;
5562              CLOSE get_req_line_of_po_shipment;
5563 
5564           ELSE
5565              -- Reservation exists for Blanket PO - update reservation if quantity
5566              -- is decreased on the PO
5567              -- get the existing reservation quantity and compare it with the quantity on the PO
5568                     --
5569              IF g_debug= C_Debug_Enabled THEN
5570                 l_Fnd_Log_Message := 'Reservation existing for Blanket release  ';
5571                 mydebug(l_Fnd_Log_Message, c_api_name,9);
5572              END IF;
5573 
5574              OPEN  get_pt_count_po_shipment(get_po_shipment_rel_rec.line_location_id);
5575              FETCH get_pt_count_po_shipment INTO get_pt_count_po_shipment_rec;
5576              CLOSE get_pt_count_po_shipment;
5577 
5578              IF g_debug= C_Debug_Enabled THEN
5579                 l_Fnd_Log_Message := ' get_pt_count_po_shipment_rec.count : '||get_pt_count_po_shipment_rec.count;
5580                 mydebug(l_Fnd_Log_Message, c_api_name,9);
5581              END IF;
5582 
5583              IF get_pt_count_po_shipment_rec.count > 1 THEN   -- multiple project/task
5584 
5585                 IF g_debug= C_Debug_Enabled THEN
5586                    l_Fnd_Log_Message := 'Multiple project/task...  ';
5587                    l_Fnd_Log_Message := 'get_po_shipment_rel_rec.ship_to_org_id:'||get_po_shipment_rec.ship_to_organization_id;
5588                    mydebug(l_Fnd_Log_Message, c_api_name,9);
5589                 END IF;
5590 
5591                  IF (inv_install.adv_inv_installed(get_po_shipment_rel_rec.ship_to_organization_id))  THEN
5592                     -- is wms org
5593                     -- delete all the reservations for this shipment
5594                     -- log message
5595                     -- Commenting out the delete reservation call.  Call
5596                     -- reduce reservations instead
5597                     -- DELETE_RES
5598                     --   (p_supply_source_header_id  => get_po_shipment_rel_rec.po_header_id
5599                     -- ,p_supply_source_line_id    => get_po_shipment_rel_rec.line_location_id
5600                     --,p_supply_source_type_id    => inv_reservation_global.g_source_type_po
5601                     -- ,x_msg_count                => x_msg_count
5602                     -- ,x_msg_data                 => x_msg_data
5603                     -- ,x_return_status            => l_return_status);
5604                     -- Call reduce reservations instead of delete
5605                    -- reservations
5606                    -- Call the reduce reservations API by setting the
5607                    -- delete_flag to yes. delete all reservations for that
5608                    -- supply line.
5609                    -- calling reduce_reservation API
5610                     l_delete_flag := 'Y';
5611                     l_sort_by_criteria := inv_reservation_global.g_query_demand_ship_date_desc;
5612                     l_mtl_maint_rsv_rec.action := 0;--supply is reduced
5613                     l_mtl_maint_rsv_rec.organization_id := get_po_shipment_rel_rec.ship_to_organization_id;
5614                     l_mtl_maint_rsv_rec.inventory_item_id := get_po_shipment_rel_rec.item_id;
5615                     l_mtl_maint_rsv_rec.supply_source_type_id:= inv_reservation_global.g_source_type_po;
5616                     l_mtl_maint_rsv_rec.supply_source_header_id := get_po_shipment_rel_rec.po_header_id;
5617                     l_mtl_maint_rsv_rec.supply_source_line_id := get_po_shipment_rel_rec.line_location_id;
5618                  --   l_mtl_maint_rsv_rec.supply_source_line_detail := NULL;
5619 
5620                     reduce_reservation
5621                       (
5622                         p_api_version_number     => 1.0
5623                         , p_init_msg_lst           => fnd_api.g_false
5624                         , x_return_status          => l_return_status
5625                         , x_msg_count              => x_msg_count
5626                         , x_msg_data               => x_msg_data
5627                         , p_mtl_maintain_rsv_rec   => l_mtl_maint_rsv_rec
5628                         , p_delete_flag            => l_delete_flag
5629                         , p_sort_by_criteria       => l_sort_by_criteria
5630                         , x_quantity_modified      => l_quantity_modified);
5631                     IF g_debug= C_Debug_Enabled THEN
5632                        mydebug ('Return Status after calling reduce reservations: '|| l_return_status, c_api_name,9);
5633                     END IF;
5634 
5635                     IF l_return_status = fnd_api.g_ret_sts_error THEN
5636 
5637                        IF g_debug= C_Debug_Enabled THEN
5638                           mydebug('Raising expected error'|| l_return_status, c_api_name,9);
5639                        END IF;
5640                        RAISE fnd_api.g_exc_error;
5641 
5642                      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5643 
5644                        IF g_debug= C_Debug_Enabled THEN
5645                           mydebug('Rasing Unexpected error'|| l_return_status, c_api_name,9);
5646                        END IF;
5647                        RAISE fnd_api.g_exc_unexpected_error;
5648 
5649                     END IF;
5650                  END IF;
5651              END IF;
5652              -- ELSE   -- inv org
5653              -- working through each project and task
5654              OPEN get_proj_task_of_po_shipment(get_po_shipment_rel_rec.line_location_id);
5655              LOOP
5656                 FETCH  get_proj_task_of_po_shipment INTO
5657                   get_pt_po_shipment_rec;
5658 
5659                 EXIT WHEN  get_proj_task_of_po_shipment%NOTFOUND;
5660 
5661                 IF g_debug= C_Debug_Enabled THEN
5662                    mydebug('Inside project/task loop', c_api_name,9);
5663                 END IF;
5664                 IF g_debug= C_Debug_Enabled THEN
5665                    mydebug('Project Id: '|| get_pt_po_shipment_rec.project_id, c_api_name,9);
5666                    mydebug('Task Id: '|| get_pt_po_shipment_rec.task_id, c_api_name,9);
5667                 END IF;
5668 
5669                 IF (get_pt_po_shipment_rec.project_id = -99) THEN
5670                    get_pt_po_shipment_rec.project_id := NULL;
5671                 END IF;
5672                 IF (get_pt_po_shipment_rec.task_id = -99) THEN
5673                    get_pt_po_shipment_rec.task_id := NULL;
5674                 END IF;
5675 
5676                 INV_RESERVATION_AVAIL_PVT.available_supply_to_reserve
5677                   (
5678                    x_return_status            => l_return_status    --OUT  NOCOPY VARCHAR2
5679                    , x_msg_count                => x_msg_count     --OUT     NOCOPY NUMBER
5680                    , x_msg_data                 => x_msg_data     --OUT     NOCOPY VARCHAR2
5681                    , p_organization_id          => get_po_shipment_rel_rec.ship_to_organization_id--IN  NUMBER default null
5682                    , p_item_id                  => get_po_shipment_rel_rec.item_id--IN  NUMBER default null
5683                    , p_supply_source_type_id    => inv_reservation_global.g_source_type_po --IN NUMBER
5684                    , p_supply_source_header_id  => get_po_shipment_rel_rec.po_header_id --IN NUMBER ?? how do we query for blank PO
5685                    , p_supply_source_line_id    => get_po_shipment_rel_rec.line_location_id --IN NUMBER
5686                    , p_project_id               => get_pt_po_shipment_rec.project_id--IN NUMBER default null
5687                    , p_task_id                  => get_pt_po_shipment_rec.task_id --IN NUMBER default null
5688                    , x_qty_available_to_reserve => l_qty_avail_to_reserve --OUT      NOCOPY NUMBER
5689                   , x_qty_available            => l_qty_avail  --OUT      NOCOPY NUMBER
5690                   );
5691 
5692                 OPEN  get_po_res_qty
5693                   (get_po_shipment_rel_rec.po_header_id
5694                    ,get_po_shipment_rel_rec.line_location_id
5695                    ,get_pt_po_shipment_rec.project_id
5696                    ,get_pt_po_shipment_rec.task_id);
5697                 FETCH get_po_res_qty INTO get_po_res_qty_rec ;
5698                 CLOSE get_po_res_qty ;
5699 
5700                 IF g_debug= C_Debug_Enabled THEN
5701                    mydebug('Qty available: '|| l_qty_avail, c_api_name,9);
5702                    mydebug('Qty available to reserve: '|| l_qty_avail_to_reserve, c_api_name,9);
5703                    mydebug('Qty reserved qty: '|| get_po_res_qty_rec.primary_reservation_quantity, c_api_name,9);
5704                 END IF;
5705 
5706                 IF  get_po_res_qty_rec.primary_reservation_quantity > 0 THEN
5707                    /* uom_conversion(get_po_res_qty_rec.reservation_uom_code
5708                    ,get_po_res_qty_rec.primary_uom_code
5709                      ,l_qty_avail_to_reserve
5710                      ,get_po_shipment_rel_rec.po_line_id
5711                      ,l_reservation_quantity
5712                      ,l_po_primary_qty);*/
5713                      IF (get_po_res_qty_rec.primary_reservation_quantity > l_qty_avail) THEN
5714                         -- calling reduce_reservation API get_pt_po_shipment_rec
5715                         l_mtl_maint_rsv_rec.action := 0;--supply is reduced
5716                         l_sort_by_criteria := inv_reservation_global.g_query_demand_ship_date_desc;
5717                         l_mtl_maint_rsv_rec.organization_id
5718                           := get_po_shipment_rel_rec.ship_to_organization_id;
5719                         l_mtl_maint_rsv_rec.inventory_item_id := get_po_shipment_rel_rec.item_id;
5720 
5721                         l_mtl_maint_rsv_rec.supply_source_type_id:= inv_reservation_global.g_source_type_po;
5722                         l_mtl_maint_rsv_rec.supply_source_header_id := get_po_shipment_rel_rec.po_header_id;
5723                         l_mtl_maint_rsv_rec.supply_source_line_id := get_po_shipment_rel_rec.line_location_id;
5724                         --        l_mtl_maint_rsv_rec.supply_source_line_detail := NULL;
5725                         --l_mtl_maint_rsv_rec.expected_quantity :=
5726                         --        get_po_res_qty_rec.primary_reservation_quantity - l_qty_avail_to_reserve;
5727                         l_mtl_maint_rsv_rec.expected_quantity := l_qty_avail;
5728                         l_mtl_maint_rsv_rec.expected_quantity_uom := get_po_res_qty_rec.primary_uom_code;
5729                         l_mtl_maint_rsv_rec.project_id := get_pt_po_shipment_rec.project_id;
5730                         l_mtl_maint_rsv_rec.task_id := get_pt_po_shipment_rec.task_id;
5731 
5732                         reduce_reservation
5733                           (
5734                             p_API_Version_Number  => 1.0
5735                             , p_Init_Msg_Lst        => fnd_api.g_false
5736                             , x_Return_Status       => l_return_status
5737                             , x_Msg_Count           => x_msg_count
5738                             , x_Msg_Data            => x_msg_data
5739                             , p_Mtl_Maintain_Rsv_rec=> l_mtl_maint_rsv_rec
5740                             , p_Delete_Flag         => 'N'
5741                             , p_Sort_By_Criteria    => NULL
5742                             , x_Quantity_Modified   => l_quantity_modified);
5743 
5744                         IF g_debug= C_Debug_Enabled THEN
5745                            mydebug ('Return Status after calling reduce reservations: '|| l_return_status, c_api_name,9);
5746                         END IF;
5747 
5748                         IF l_return_status = fnd_api.g_ret_sts_error THEN
5749 
5750                            IF g_debug= C_Debug_Enabled THEN
5751                               mydebug('Raising expected error'|| l_return_status, c_api_name,9);
5752                            END IF;
5753                            RAISE fnd_api.g_exc_error;
5754 
5755                          ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5756 
5757                            IF g_debug= C_Debug_Enabled THEN
5758                               mydebug('Rasing Unexpected error'|| l_return_status, c_api_name,9);
5759                            END IF;
5760                            RAISE fnd_api.g_exc_unexpected_error;
5761 
5762                         END IF;
5763 
5764                      END IF;
5765                 END IF;
5766 
5767              END LOOP;
5768              CLOSE  get_proj_task_of_po_shipment;
5769           END IF;
5770       END LOOP;
5771       CLOSE get_po_shipment_for_release;
5772 
5773    ELSIF upper(p_action) = 'REMOVE_REQ_SUPPLY' THEN
5774            OPEN get_req_hdr_lines (p_header_id);
5775            --
5776            IF g_debug= C_Debug_Enabled THEN
5777               mydebug ('Remove req supply. req header:'|| p_header_id ,c_api_name,9);
5778            END IF;
5779            LOOP
5780               --
5781               FETCH get_req_hdr_lines INTO get_req_hdr_lines_rec;
5782               EXIT WHEN get_req_hdr_lines%NOTFOUND;
5783 
5784               IF g_debug= C_Debug_Enabled THEN
5785                  mydebug ('Removing req supply for req line :'|| get_req_hdr_lines_rec.requisition_line_id , c_api_name,9);
5786               END IF;
5787 
5788               IF Upper(get_req_hdr_lines_rec.source_type_code) = 'INVENTORY' THEN
5789                  l_supply_source_type_id := inv_reservation_global.g_source_type_internal_req;
5790                else
5791                  l_supply_source_type_id := inv_reservation_global.g_source_type_req;
5792               END IF;
5793 
5794               IF EXISTS_RESERVATION(p_supply_source_header_id        => p_header_id,
5795                                  p_supply_source_line_id        => get_req_hdr_lines_rec.requisition_line_id,
5796                                  p_supply_source_type_id        => l_supply_source_type_id) THEN
5797                 DELETE_RES
5798                 (p_supply_source_header_id  => p_header_id
5799                  ,p_supply_source_line_id    => get_req_hdr_lines_rec.requisition_line_id
5800                  ,p_supply_source_type_id    => l_supply_source_type_id
5801                  ,x_msg_count                => x_msg_count
5802                  ,x_msg_data                 => x_msg_data
5803                  ,x_return_status            => l_return_status);
5804               END IF;
5805            END LOOP; --get_req_hdr_lines
5806 
5807     ELSIF upper(p_action) = 'REMOVE_REQ_LINE_SUPPLY' THEN
5808                  --delete the reservation on the req
5809                  --
5810            IF g_debug= C_Debug_Enabled THEN
5811               mydebug ('Remove req line supply. req line:'|| p_line_id ,c_api_name,9);
5812            END IF;
5813 
5814            BEGIN
5815               SELECT source_type_code INTO l_source_type_code FROM
5816                 po_requisition_lines_all WHERE requisition_line_id = p_line_id;
5817 
5818            EXCEPTION
5819               WHEN no_data_found THEN
5820                  IF g_debug= C_Debug_Enabled THEN
5821                     mydebug ('Cannot find the source type code for req line' || p_line_id,c_api_name,9);
5822                  END IF;
5823            END;
5824 
5825 
5826            IF Upper(l_source_type_code) = 'INVENTORY' THEN
5827               l_supply_source_type_id := inv_reservation_global.g_source_type_internal_req;
5828             else
5829               l_supply_source_type_id := inv_reservation_global.g_source_type_req;
5830            END IF;
5831 
5832            IF EXISTS_RESERVATION(p_supply_source_line_id          => p_line_id,
5833                                  p_supply_source_type_id          => l_supply_source_type_id) THEN
5834              DELETE_RES
5835              (p_supply_source_line_id   => p_line_id
5836               ,p_supply_source_type_id   => l_supply_source_type_id
5837               ,x_msg_count               => x_msg_count
5838               ,x_msg_data                => x_msg_data
5839               ,x_return_status           => l_return_status);
5840            END IF;
5841 
5842    ELSIF upper(p_action) = 'CANCEL_PO_SUPPLY' THEN
5843 
5844                  -- If Reservation Exists, then delete them and never transfer back to requistion
5845                  -- since when PO is cancelled, associated req got
5846                  -- cancelled too.
5847         IF g_debug= C_Debug_Enabled THEN
5848            mydebug ('Cancel PO supply. Supply header: '|| p_header_id ,c_api_name,9);
5849         END IF;
5850         IF EXISTS_RESERVATION(p_supply_source_header_id          => p_header_id) THEN
5851 
5852             --reservation should be deleted
5853             OPEN get_po_shipment(p_header_id);
5854             LOOP
5855 
5856               FETCH get_po_shipment INTO get_po_shipment_rec;
5857               EXIT WHEN get_po_shipment%NOTFOUND;
5858 
5859               --DELETE_RES (p_supply_source_header_id => p_header_id
5860               --        ,p_supply_source_line_id   => get_po_shipment_rec.line_location_id
5861               --        ,p_supply_source_type_id   => inv_reservation_global.g_source_type_po
5862               --           ,x_msg_count               => x_msg_count
5863               --           ,x_msg_data                => x_msg_data
5864               --           ,x_return_status           => l_return_status);
5865               IF g_debug= C_Debug_Enabled THEN
5866                  mydebug ('Removing po line location. po line loc.:'|| get_po_shipment_rec.line_location_id,c_api_name,9);
5867               END IF;
5868               l_delete_flag := 'Y';
5869               l_sort_by_criteria := inv_reservation_global.g_query_demand_ship_date_desc;
5870               l_mtl_maint_rsv_rec.action := 0;--supply is reduced
5871               l_mtl_maint_rsv_rec.organization_id :=
5872                 get_po_shipment_rec.ship_to_organization_id;
5873               l_mtl_maint_rsv_rec.inventory_item_id :=
5874                 get_po_shipment_rec.item_id;
5875               l_mtl_maint_rsv_rec.supply_source_type_id:= inv_reservation_global.g_source_type_po;
5876               l_mtl_maint_rsv_rec.supply_source_header_id := p_header_id;
5877               l_mtl_maint_rsv_rec.supply_source_line_id :=
5878                 get_po_shipment_rec.line_location_id;
5879              -- l_mtl_maint_rsv_rec.supply_source_line_detail := NULL;
5880 
5881               reduce_reservation
5882                 (
5883                   p_api_version_number     => 1.0
5884                   , p_init_msg_lst           => fnd_api.g_false
5885                   , x_return_status          => l_return_status
5886                   , x_msg_count              => x_msg_count
5887                   , x_msg_data               => x_msg_data
5888                   , p_mtl_maintain_rsv_rec   => l_mtl_maint_rsv_rec
5889                   , p_delete_flag            => l_delete_flag
5890                   , p_sort_by_criteria       => l_sort_by_criteria
5891                   , x_quantity_modified      => l_quantity_modified);
5892               IF g_debug= C_Debug_Enabled THEN
5893                  mydebug ('Return Status after calling reduce reservations: '|| l_return_status, c_api_name,9);
5894               END IF;
5895 
5896               IF l_return_status = fnd_api.g_ret_sts_error THEN
5897 
5898                  IF g_debug= C_Debug_Enabled THEN
5899                     mydebug('Raising expected error'|| l_return_status, c_api_name,9);
5900                  END IF;
5901                  RAISE fnd_api.g_exc_error;
5902 
5903                ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5904 
5905                  IF g_debug= C_Debug_Enabled THEN
5906                     mydebug('Rasing Unexpected error'|| l_return_status, c_api_name,9);
5907                  END IF;
5908                  RAISE fnd_api.g_exc_unexpected_error;
5909 
5910               END IF;
5911             END LOOP; --get_distr
5912 
5913        END IF; --Reservation Exists
5914 
5915    ELSIF upper(p_action) = 'CANCEL_PO_LINE' THEN
5916        OPEN  get_po_header_id_line (p_line_id);
5917        FETCH get_po_header_id_line INTO l_po_header_id;
5918        CLOSE get_po_header_id_line;
5919 
5920        IF g_debug= C_Debug_Enabled THEN
5921           mydebug ('Cancel PO line. PO line.: '|| p_line_id,c_api_name,9);
5922        END IF;
5923        -- If Reservation Exists for the PO, then proceed to check whether
5924        -- the existing Reservation needs to be transferred to Requisition
5925        -- Since Reservation is on the PO and PO Distribution, PO Reservation is
5926        -- checked even when the Line is Cancelled
5927        IF EXISTS_RESERVATION(p_supply_source_header_id        => l_po_header_id) THEN
5928 
5929            --delete the reservation
5930            OPEN get_line_loc_for_po_line(p_line_id);
5931            LOOP
5932                FETCH get_line_loc_for_po_line INTO get_line_loc_rec;
5933                EXIT WHEN get_line_loc_for_po_line%NOTFOUND;
5934 
5935                --    DELETE_RES (p_supply_source_header_id => l_po_header_id
5936                --             ,p_supply_source_line_id   => get_distr_rec.line_location_id
5937                --           ,p_supply_source_type_id   => inv_reservation_global.g_source_type_po
5938                --         ,x_msg_count               => x_msg_count
5939                --       ,x_msg_data                => x_msg_data
5940                --     ,x_return_status           => l_return_status);
5941                IF g_debug= C_Debug_Enabled THEN
5942                   mydebug ('Cancel line.Removing po line location. po line loc.:'|| get_line_loc_rec.line_location_id,c_api_name,9);
5943                END IF;
5944 
5945                l_delete_flag := 'Y';
5946                l_sort_by_criteria := inv_reservation_global.g_query_demand_ship_date_desc;
5947                l_mtl_maint_rsv_rec.action := 0;--supply is reduced
5948                l_mtl_maint_rsv_rec.organization_id :=
5949                  get_line_loc_rec.ship_to_organization_id;
5950                l_mtl_maint_rsv_rec.inventory_item_id :=
5951                  get_line_loc_rec.item_id;
5952                l_mtl_maint_rsv_rec.supply_source_type_id:= inv_reservation_global.g_source_type_po;
5953                l_mtl_maint_rsv_rec.supply_source_header_id := l_po_header_id;
5954                l_mtl_maint_rsv_rec.supply_source_line_id :=
5955                  get_line_loc_rec.line_location_id;
5956                -- l_mtl_maint_rsv_rec.supply_source_line_detail := NULL;
5957 
5958                reduce_reservation
5959                  (
5960                    p_api_version_number     => 1.0
5961                    , p_init_msg_lst           => fnd_api.g_false
5962                    , x_return_status          => l_return_status
5963                    , x_msg_count              => x_msg_count
5964                    , x_msg_data               => x_msg_data
5965                    , p_mtl_maintain_rsv_rec   => l_mtl_maint_rsv_rec
5966                    , p_delete_flag            => l_delete_flag
5967                    , p_sort_by_criteria       => l_sort_by_criteria
5968                    , x_quantity_modified      => l_quantity_modified);
5969                IF g_debug= C_Debug_Enabled THEN
5970                   mydebug ('Return Status after calling reduce reservations: '|| l_return_status, c_api_name,9);
5971                END IF;
5972 
5973                IF l_return_status = fnd_api.g_ret_sts_error THEN
5974 
5975                   IF g_debug= C_Debug_Enabled THEN
5976                      mydebug('Raising expected error'|| l_return_status, c_api_name,9);
5977                   END IF;
5978                   RAISE fnd_api.g_exc_error;
5979 
5980                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5981 
5982                   IF g_debug= C_Debug_Enabled THEN
5983                      mydebug('Rasing Unexpected error'|| l_return_status, c_api_name,9);
5984                   END IF;
5985                   RAISE fnd_api.g_exc_unexpected_error;
5986 
5987                END IF;
5988            END LOOP;
5989        END IF; --Reservation Exists
5990 
5991    ELSIF upper(p_action) = 'CANCEL_PO_SHIPMENT' THEN
5992       --OPEN  get_po_header_id_shipment(p_line_location_id);
5993       --FETCH get_po_header_id_shipment INTO l_po_header_id;
5994       --CLOSE get_po_header_id_shipment;
5995 
5996      IF g_debug= C_Debug_Enabled THEN
5997         mydebug ('Cancel PO Shipment.Removing po line location. po line loc.:'|| p_line_location_id,c_api_name,9);
5998      END IF;
5999      -- If Reservation Exists for the PO, then proceed to check whether
6000      -- the existing Reservation needs to be transferred to Requisition
6001      -- Since Reservation is on the PO and PO Distribution, PO Reservation is
6002      -- checked even when the Line is Cancelled
6003      IF exists_reservation(p_supply_source_line_id => p_line_location_id) THEN
6004 
6005         -- OPEN get_distr_for_po_shipment(p_line_location_id);
6006         --
6007         --  LOOP
6008         --      FETCH get_distr_for_po_shipment INTO get_distr_rec;
6009         --      EXIT WHEN get_distr_for_po_shipment%NOTFOUND;
6010         --
6011         --      DELETE_RES (p_supply_source_header_id  => l_po_header_id
6012         --                 ,p_supply_source_line_id    => get_distr_rec.line_location_id
6013         --                 ,p_supply_source_type_id    => inv_reservation_global.g_source_type_po--
6014         --                 ,x_msg_count                => x_msg_count
6015         ----                 ,x_msg_data                 => x_msg_data
6016         --                 ,x_return_status            => l_return_status);
6017         --
6018          --  END LOOP; -- get_distr
6019 
6020         OPEN get_line_loc_for_po_shipment(p_line_location_id);
6021         FETCH get_line_loc_for_po_shipment INTO get_line_loc_rec;
6022         CLOSE get_line_loc_for_po_shipment;
6023 
6024         l_delete_flag := 'Y';
6025         l_sort_by_criteria := inv_reservation_global.g_query_demand_ship_date_desc;
6026         l_mtl_maint_rsv_rec.action := 0;--supply is reduced
6027         l_mtl_maint_rsv_rec.organization_id := get_line_loc_rec.ship_to_organization_id;
6028         l_mtl_maint_rsv_rec.inventory_item_id := get_line_loc_rec.item_id;
6029         l_mtl_maint_rsv_rec.supply_source_type_id:= inv_reservation_global.g_source_type_po;
6030         l_mtl_maint_rsv_rec.supply_source_header_id := get_line_loc_rec.po_header_id;
6031         l_mtl_maint_rsv_rec.supply_source_line_id := p_line_location_id;
6032         -- l_mtl_maint_rsv_rec.supply_source_line_detail := NULL;
6033 
6034         reduce_reservation
6035           (
6036             p_api_version_number     => 1.0
6037             , p_init_msg_lst           => fnd_api.g_false
6038             , x_return_status          => l_return_status
6039             , x_msg_count              => x_msg_count
6040             , x_msg_data               => x_msg_data
6041             , p_mtl_maintain_rsv_rec   => l_mtl_maint_rsv_rec
6042             , p_delete_flag            => l_delete_flag
6043             , p_sort_by_criteria       => l_sort_by_criteria
6044             , x_quantity_modified      => l_quantity_modified);
6045         IF g_debug= C_Debug_Enabled THEN
6046            mydebug ('Return Status after calling reduce reservations: '|| l_return_status, c_api_name,9);
6047         END IF;
6048 
6049         IF l_return_status = fnd_api.g_ret_sts_error THEN
6050 
6051            IF g_debug= C_Debug_Enabled THEN
6052               mydebug('Raising expected error'|| l_return_status, c_api_name,9);
6053            END IF;
6054            RAISE fnd_api.g_exc_error;
6055 
6056          ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6057 
6058            IF g_debug= C_Debug_Enabled THEN
6059               mydebug('Rasing Unexpected error'|| l_return_status, c_api_name,9);
6060            END IF;
6061            RAISE fnd_api.g_exc_unexpected_error;
6062 
6063         END IF;
6064 
6065      END IF; --Reservation Exists
6066 
6067    ELSIF upper(p_action) = 'CANCEL_BLANKET_RELEASE' THEN
6068        OPEN  get_po_header_id_release(p_header_id); -- p_header_id is po_release_id
6069        FETCH get_po_header_id_release INTO l_po_header_id;
6070        CLOSE get_po_header_id_release;
6071 
6072        IF g_debug= C_Debug_Enabled THEN
6073           mydebug ('Cancel Blanket Release. po header id. :'|| l_po_header_id,c_api_name,9);
6074        END IF;
6075        -- If Reservation Exists for the PO, then proceed to check whether
6076        -- the existing Reservation needs to be transferred to Requisition
6077        -- Since Reservation is on the PO and PO Distribution, PO Reservation is
6078        -- checked even when the Release is Cancelled
6079        --
6080        IF exists_reservation(p_supply_source_header_id  => l_po_header_id) THEN
6081 
6082           --delete the reservation
6083           -- OPEN get_distr_for_po_release(p_header_id);
6084           --LOOP
6085           --  FETCH get_distr_for_po_release INTO get_distr_rec;
6086           -- EXIT WHEN get_distr_for_po_release%NOTFOUND;
6087 
6088           --      DELETE_RES ( p_supply_source_header_id => l_po_header_id
6089           --                ,p_supply_source_line_id   => get_distr_rec.line_location_id
6090           --              ,p_supply_source_type_id   => inv_reservation_global.g_source_type_po
6091           --            ,x_msg_count               => x_msg_count
6092           --          ,x_msg_data                => x_msg_data
6093           --        ,x_return_status           => l_return_status);
6094 
6095           --END LOOP; --get_distr_for_po_release
6096 
6097           --reservation should be deleted
6098           --bug8578392 change get_po_shipment to get_po_shipment_for_release
6099           OPEN get_po_shipment_for_release(p_header_id);
6100           LOOP
6101                FETCH get_po_shipment_for_release INTO get_po_shipment_rel_rec;
6102                EXIT WHEN get_po_shipment_for_release%NOTFOUND;
6103 
6104                --DELETE_RES (p_supply_source_header_id => p_header_id
6105                --        ,p_supply_source_line_id   => get_po_shipment_rec.line_location_id
6106                --        ,p_supply_source_type_id   => inv_reservation_global.g_source_type_po
6107                --           ,x_msg_count               => x_msg_count
6108                --           ,x_msg_data                => x_msg_data
6109                --           ,x_return_status           => l_return_status);
6110                IF g_debug= C_Debug_Enabled THEN
6111                   mydebug ('Removing po line location. po line loc.:'|| get_po_shipment_rel_rec.line_location_id,c_api_name,9);
6112                END IF;
6113 
6114               l_delete_flag := 'Y';
6115               l_sort_by_criteria := inv_reservation_global.g_query_demand_ship_date_desc;
6116               l_mtl_maint_rsv_rec.action := 0;--supply is reduced
6117               --bug8578392 change get_po_shipment_rec.ship_to_organization_id to get_po_shipment_rel_rec.ship_to_organization_id
6118               l_mtl_maint_rsv_rec.organization_id := get_po_shipment_rel_rec.ship_to_organization_id;
6119               --bug8578392 change get_po_shipment_rec.item_id to get_po_shipment_rel_rec.item_id
6120               l_mtl_maint_rsv_rec.inventory_item_id := get_po_shipment_rel_rec.item_id;
6121               l_mtl_maint_rsv_rec.supply_source_type_id:= inv_reservation_global.g_source_type_po;
6122               l_mtl_maint_rsv_rec.supply_source_header_id := l_po_header_id;
6123               --bug8578392 change get_po_shipment_rec.line_location_id to get_po_shipment_rel_rec.line_location_id
6124               l_mtl_maint_rsv_rec.supply_source_line_id := get_po_shipment_rel_rec.line_location_id;
6125               -- l_mtl_maint_rsv_rec.supply_source_line_detail := NULL;
6126 
6127               reduce_reservation
6128                 (
6129                   p_api_version_number     => 1.0
6130                   , p_init_msg_lst           => fnd_api.g_false
6131                   , x_return_status          => l_return_status
6132                   , x_msg_count              => x_msg_count
6133                   , x_msg_data               => x_msg_data
6134                   , p_mtl_maintain_rsv_rec   => l_mtl_maint_rsv_rec
6135                   , p_delete_flag            => l_delete_flag
6136                   , p_sort_by_criteria       => l_sort_by_criteria
6137                   , x_quantity_modified      => l_quantity_modified);
6138               IF g_debug= C_Debug_Enabled THEN
6139                  mydebug ('Return Status after calling reduce reservations: '|| l_return_status, c_api_name,9);
6140               END IF;
6141 
6142               IF l_return_status = fnd_api.g_ret_sts_error THEN
6143 
6144                  IF g_debug= C_Debug_Enabled THEN
6145                     mydebug('Raising expected error'|| l_return_status, c_api_name,9);
6146                  END IF;
6147                  RAISE fnd_api.g_exc_error;
6148 
6149                ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6150 
6151                  IF g_debug= C_Debug_Enabled THEN
6152                     mydebug('Rasing Unexpected error'|| l_return_status, c_api_name,9);
6153                  END IF;
6154                  RAISE fnd_api.g_exc_unexpected_error;
6155 
6156               END IF;
6157             END LOOP; --get_distr
6158 
6159        END IF; --Reservation Exists
6160 --
6161    ELSIF upper(p_action) = 'CANCEL_BLANKET_SHIPMENT' THEN
6162 
6163       -- OPEN  get_po_header_id_shipment(p_line_location_id);
6164       -- FETCH get_po_header_id_shipment INTO l_po_header_id;
6165       -- CLOSE get_po_header_id_shipment;
6166 
6167       -- If Reservation Exists for the PO, then proceed to check whether
6168       -- the existing Reservation needs to be transferred to Requisition
6169       -- Since Reservation is on the PO and PO Distribution, PO Reservation is
6170       -- checked even when the Line is Cancelled
6171      IF g_debug= C_Debug_Enabled THEN
6172         mydebug ('Cancel Blanket Shipment. po line location id. :'|| p_line_location_id,c_api_name,9);
6173      END IF;
6174 
6175      IF exists_reservation(p_supply_source_line_id => p_line_location_id)  THEN
6176 
6177         --        OPEN get_distr_for_po_shipment(p_line_location_id);
6178         --        LOOP
6179         --           FETCH get_distr_for_po_shipment INTO get_distr_rec;
6180         --               EXIT WHEN get_distr_for_po_shipment%NOTFOUND;
6181 
6182         --               DELETE_RES (p_supply_source_header_id  => l_po_header_id
6183         --                          ,p_supply_source_line_id    => get_distr_rec.line_location_id
6184         --                          ,p_supply_source_type_id    => inv_reservation_global.g_source_type_po
6185         --                          ,x_msg_count                => x_msg_count
6186         --                          ,x_msg_data                 => x_msg_data
6187         --                          ,x_return_status            => l_return_status);
6188         --
6189         --           END LOOP; -- get_distr
6190 
6191         OPEN get_line_loc_for_po_shipment(p_line_location_id);
6192         FETCH get_line_loc_for_po_shipment INTO get_line_loc_rec;
6193         CLOSE get_line_loc_for_po_shipment;
6194 
6195         l_delete_flag := 'Y';
6196         l_sort_by_criteria := inv_reservation_global.g_query_demand_ship_date_desc;
6197         l_mtl_maint_rsv_rec.action := 0;--supply is reduced
6198         l_mtl_maint_rsv_rec.organization_id :=
6199           get_line_loc_rec.ship_to_organization_id;
6200         l_mtl_maint_rsv_rec.inventory_item_id :=
6201           get_line_loc_rec.item_id;
6202         l_mtl_maint_rsv_rec.supply_source_type_id:= inv_reservation_global.g_source_type_po;
6203         l_mtl_maint_rsv_rec.supply_source_header_id := get_line_loc_rec.po_header_id;
6204         l_mtl_maint_rsv_rec.supply_source_line_id := p_line_location_id;
6205         -- l_mtl_maint_rsv_rec.supply_source_line_detail := NULL;
6206 
6207         reduce_reservation
6208           (
6209             p_api_version_number     => 1.0
6210             , p_init_msg_lst           => fnd_api.g_false
6211             , x_return_status          => l_return_status
6212             , x_msg_count              => x_msg_count
6213             , x_msg_data               => x_msg_data
6214             , p_mtl_maintain_rsv_rec   => l_mtl_maint_rsv_rec
6215             , p_delete_flag            => l_delete_flag
6216             , p_sort_by_criteria       => l_sort_by_criteria
6217             , x_quantity_modified      => l_quantity_modified);
6218         IF g_debug= C_Debug_Enabled THEN
6219            mydebug ('Return Status after calling reduce reservations: '|| l_return_status, c_api_name,9);
6220         END IF;
6221 
6222         IF l_return_status = fnd_api.g_ret_sts_error THEN
6223 
6224            IF g_debug= C_Debug_Enabled THEN
6225               mydebug('Raising expected error'|| l_return_status, c_api_name,9);
6226            END IF;
6227            RAISE fnd_api.g_exc_error;
6228 
6229          ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6230 
6231            IF g_debug= C_Debug_Enabled THEN
6232               mydebug('Rasing Unexpected error'|| l_return_status, c_api_name,9);
6233            END IF;
6234            RAISE fnd_api.g_exc_unexpected_error;
6235 
6236         END IF;
6237 
6238      END IF; --Reservation Exists
6239 
6240      -- Bug 5253916: For update so quantity
6241 
6242    ELSIF upper(p_action) = 'UPDATE_SO_QUANTITY' THEN
6243      --reduce the reservation on the req by the ordered quantity
6244       IF g_debug= C_Debug_Enabled THEN
6245          mydebug ('Inside update so qty. req line:'|| p_line_id ,c_api_name,9);
6246       END IF;
6247 
6248       -- Check to see if there are any reservations for this requsition
6249       -- line.
6250 
6251       BEGIN
6252          SELECT Nvl(SUM(primary_reservation_quantity),0) INTO
6253            l_primary_res_qty FROM mtl_reservations
6254            WHERE supply_source_type_id =
6255            inv_reservation_global.g_source_type_internal_req AND
6256            supply_source_header_id = p_header_id AND
6257            supply_source_line_id = p_line_id;
6258       EXCEPTION
6259          WHEN no_data_found THEN
6260         IF g_debug= C_Debug_Enabled THEN
6261            mydebug ('No reservation records found for req line id. req line:'|| p_line_id ,c_api_name,9);
6262         END IF;
6263       END;
6264 
6265       IF l_primary_res_qty > 0 THEN
6266            -- reservations found
6267            -- get the req line information.
6268            BEGIN
6269              --bug #5498904 replaced org_id with destination_organization_id
6270               SELECT destination_organization_id, item_id, unit_meas_lookup_code, quantity INTO
6271             l_organization_id,
6272             l_inventory_item_id,
6273             l_req_unit_meas, l_req_qty  FROM
6274             po_requisition_lines_all WHERE requisition_line_id = p_line_id;
6275            EXCEPTION
6276               WHEN no_data_found THEN
6277                 IF g_debug= C_Debug_Enabled THEN
6278                     mydebug ('No records found for req line id. req line:'|| p_line_id ,c_api_name,9);
6279                 END IF;
6280            END;
6281 
6282            IF (p_ordered_quantity = 0) THEN
6283               -- delete all reservations for that req line
6284               -- calling reduce_reservation API
6285               l_delete_flag := 'Y';
6286               l_sort_by_criteria := inv_reservation_global.g_query_demand_ship_date_desc;
6287               l_mtl_maint_rsv_rec.action := 0;--supply is reduced
6288               l_mtl_maint_rsv_rec.organization_id := l_organization_id;
6289               l_mtl_maint_rsv_rec.inventory_item_id := l_inventory_item_id;
6290              --bug #5498904 populated l_mtl_maint_rsv_rec.supply_source_type_id
6291               l_mtl_maint_rsv_rec.supply_source_type_id := inv_reservation_global.g_source_type_internal_req;
6292               l_supply_source_type_id := inv_reservation_global.g_source_type_internal_req;
6293               l_mtl_maint_rsv_rec.supply_source_header_id := p_header_id;
6294               l_mtl_maint_rsv_rec.supply_source_line_id := p_line_id;
6295               -- l_mtl_maint_rsv_rec.supply_source_line_detail := NULL;
6296 
6297               reduce_reservation
6298                 (
6299                   p_api_version_number       => 1.0
6300                   , p_init_msg_lst           => fnd_api.g_false
6301                   , x_return_status          => l_return_status
6302                   , x_msg_count              => x_msg_count
6303                   , x_msg_data               => x_msg_data
6304                   , p_mtl_maintain_rsv_rec   => l_mtl_maint_rsv_rec
6305                   , p_delete_flag            => l_delete_flag
6306                   , p_sort_by_criteria       => l_sort_by_criteria
6307                   , x_quantity_modified      => l_quantity_modified);
6308               IF g_debug= C_Debug_Enabled THEN
6309                  mydebug ('Return Status after calling reduce reservations: '|| l_return_status, c_api_name,9);
6310                  mydebug ('Expected qty: '|| p_ordered_quantity, c_api_name,9);
6311                  mydebug ('Modified qty: '|| l_quantity_modified, c_api_name,9);
6312               END IF;
6313 
6314               IF l_return_status = fnd_api.g_ret_sts_error THEN
6315 
6316                  IF g_debug= C_Debug_Enabled THEN
6317                     mydebug('Raising expected error'|| l_return_status, c_api_name,9);
6318                  END IF;
6319                  RAISE fnd_api.g_exc_error;
6320 
6321               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6322 
6323                  IF g_debug= C_Debug_Enabled THEN
6324                     mydebug('Rasing Unexpected error'|| l_return_status, c_api_name,9);
6325                  END IF;
6326                  RAISE fnd_api.g_exc_unexpected_error;
6327 
6328               END IF;
6329 
6330            ELSE
6331               -- reduce the reservations to the ordered qty
6332               -- Set the source type as internal req
6333 
6334               l_mtl_maint_rsv_rec.action := 0;--supply is reduced
6335               l_sort_by_criteria := inv_reservation_global.g_query_demand_ship_date_desc;
6336               l_mtl_maint_rsv_rec.organization_id := l_organization_id;
6337               l_mtl_maint_rsv_rec.inventory_item_id := l_inventory_item_id;
6338               --bug #5498904 populated l_mtl_maint_rsv_rec.supply_source_type_id
6339               l_mtl_maint_rsv_rec.supply_source_type_id := inv_reservation_global.g_source_type_internal_req;
6340               l_supply_source_type_id := inv_reservation_global.g_source_type_internal_req;
6341               l_mtl_maint_rsv_rec.supply_source_header_id := p_header_id;
6342               l_mtl_maint_rsv_rec.supply_source_line_id := p_line_id;
6343               -- l_mtl_maint_rsv_rec.supply_source_line_detail := NULL;
6344               l_mtl_maint_rsv_rec.expected_quantity := p_ordered_quantity;
6345               l_mtl_maint_rsv_rec.expected_quantity_uom := p_ordered_uom;
6346 
6347               reduce_reservation
6348                 (
6349                   p_api_version_number     => 1.0
6350                   , p_init_msg_lst           => fnd_api.g_false
6351                   , x_return_status          => l_return_status
6352                   , x_msg_count              => x_msg_count
6353                   , x_msg_data               => x_msg_data
6354                   , p_mtl_maintain_rsv_rec   => l_mtl_maint_rsv_rec
6355                   , p_delete_flag            => 'N'
6356                   , p_sort_by_criteria       => l_sort_by_criteria
6357                   , x_quantity_modified      => l_quantity_modified);
6358 
6359               IF g_debug= C_Debug_Enabled THEN
6360                  mydebug ('Return Status after calling reduce reservations: '|| l_return_status, c_api_name,9);
6361                  mydebug ('Expected qty: '|| p_ordered_quantity, c_api_name,9);
6362                    mydebug ('Modified qty: '|| l_quantity_modified, c_api_name,9);
6363               END IF;
6364 
6365               IF l_return_status = fnd_api.g_ret_sts_error THEN
6366 
6367                  IF g_debug= C_Debug_Enabled THEN
6368                     mydebug('Raising expected error'|| l_return_status, c_api_name,9);
6369                  END IF;
6370                  RAISE fnd_api.g_exc_error;
6371 
6372                ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6373 
6374                  IF g_debug= C_Debug_Enabled THEN
6375                     mydebug('Rasing Unexpected error'|| l_return_status, c_api_name,9);
6376                  END IF;
6377                  RAISE fnd_api.g_exc_unexpected_error;
6378 
6379               END IF;
6380            END IF; -- ordered qty
6381 
6382       END IF; -- reservation records found
6383     ELSE
6384           -- all other actions do nothing ....for now
6385           NULL;
6386 
6387    END IF;
6388 
6389 
6390    If g_debug= C_Debug_Enabled Then
6391           l_Fnd_Log_Message := 'l_return_status: '|| l_return_status;
6392           mydebug(l_Fnd_Log_Message,c_api_name,9);
6393    End If;
6394 
6395    x_return_status := l_return_status;
6396 
6397    If x_return_status = fnd_api.g_ret_sts_success THEN
6398       l_Fnd_Log_message := 'Calling maintain_reservation API was successful ';
6399       -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
6400       IF G_debug= C_Debug_Enabled THEN
6401          mydebug(l_Fnd_Log_Message, c_api_name,9);
6402       END IF;
6403    else
6404       l_Fnd_Log_message := 'Error while calling maintain_reservation API ';
6405       -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
6406       IF G_debug= C_Debug_Enabled THEN
6407          mydebug(l_Fnd_Log_Message, c_api_name,9);
6408       END IF;
6409    end if;
6410 
6411    -- Call fnd_log api at the end of the API
6412    l_Fnd_Log_message := 'At the end of procedure :';
6413    -- Fnd_Log_Debug(Fnd_Log.Level_Procedure,C_Module_name, l_Fnd_Log_Message );
6414 
6415    IF G_debug= C_Debug_Enabled THEN
6416         mydebug(l_Fnd_Log_Message, c_api_name,9);
6417    END IF;
6418 
6419 --
6420 EXCEPTION
6421    WHEN fnd_api.g_exc_error THEN
6422        x_return_status := fnd_api.g_ret_sts_error;
6423 
6424        --  Get message count and data
6425        fnd_msg_pub.count_and_get
6426          (  p_count => x_msg_count
6427           , p_data  => x_msg_data
6428           );
6429 
6430         -- Call fnd_log api at the end of the API
6431         l_Fnd_Log_message := 'When Expected exception raised for procedure :' ;
6432         -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
6433 
6434         -- Log message in trace file
6435         IF G_debug= C_Debug_Enabled THEN
6436             mydebug(l_Fnd_Log_Message,c_api_name,9);
6437         END IF;
6438 
6439         -- Get messages from stack and log them in fnd tables
6440         If X_Msg_Count = 1 Then
6441            -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
6442            -- Log message in trace file
6443            If G_debug= C_Debug_Enabled THEN
6444              mydebug(l_Fnd_Log_Message,c_api_name,9);
6445            End If;
6446         Elsif x_msg_count > 1 Then
6447             For I In 1..X_Msg_Count Loop
6448               FND_MSG_PUB.Get
6449                (p_msg_index     => i,
6450                 p_encoded       => 'F',
6451                 p_data          => l_Fnd_Log_Message,
6452                 p_msg_index_out => l_msg_index_out );
6453 
6454                -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
6455 
6456                -- Log message in trace file
6457                IF G_debug= C_Debug_Enabled THEN
6458                    mydebug(l_Fnd_Log_Message,c_api_name,9);
6459                END IF;
6460             End Loop ;
6461         End If;
6462 
6463     WHEN fnd_api.g_exc_unexpected_error THEN
6464          x_return_status := fnd_api.g_ret_sts_unexp_error ;
6465 
6466          --  Get message count and data
6467          fnd_msg_pub.count_and_get
6468            (  p_count  => x_msg_count
6469             , p_data   => x_msg_data
6470              );
6471          -- Call fnd_log api at the end of the API
6472          l_Fnd_Log_message := 'When unexpected exception raised for procedure :';
6473          -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
6474 
6475          -- Log message in trace file
6476          IF G_debug= C_Debug_Enabled THEN
6477              mydebug(l_Fnd_Log_Message,c_api_name,9);
6478          END IF;
6479 
6480          -- Get messages from stack and log them in fnd tables
6481          If X_Msg_Count = 1 Then
6482            -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
6483 
6484            -- Log message in trace file
6485            IF G_debug= C_Debug_Enabled THEN
6486              mydebug(l_Fnd_Log_Message,c_api_name,9);
6487            END IF;
6488          Elsif X_Msg_Count > 1 Then
6489             For I In 1..X_Msg_Count Loop
6490               FND_MSG_PUB.Get
6491                (p_msg_index     => i,
6492                 p_encoded       => 'F',
6493                 p_data          => l_Fnd_Log_Message,
6494                 p_msg_index_out => l_msg_index_out );
6495 
6496                -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
6497 
6498                -- Log message in trace file
6499                IF G_debug= C_Debug_Enabled THEN
6500                    mydebug(l_Fnd_Log_Message,c_api_name,9);
6501                END IF;
6502             End Loop ;
6503          End If;
6504 
6505   WHEN OTHERS THEN
6506        x_return_status := fnd_api.g_ret_sts_unexp_error ;
6507 
6508        IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
6509          THEN
6510           fnd_msg_pub.add_exc_msg
6511             (  g_pkg_name
6512              , c_api_name
6513              );
6514        END IF;
6515 
6516        --  Get message count and data
6517        fnd_msg_pub.count_and_get
6518          (  p_count  => x_msg_count
6519           , p_data   => x_msg_data
6520           );
6521 
6522        -- Call fnd_log api at the end of the API
6523        l_Fnd_Log_message := 'When Others exception raised for procedure :' || G_Pkg_Name || '.' || C_API_Name ;
6524        -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
6525 
6526        -- Log message in trace file
6527        IF G_debug= C_Debug_Enabled THEN
6528            mydebug(l_Fnd_Log_Message,c_api_name,9);
6529        END IF;
6530 
6531        -- Get messages from stack and log them in fnd tables
6532        If X_Msg_Count = 1 Then
6533          -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, X_Msg_Data );
6534 
6535          -- Log message in trace file
6536          If G_debug= C_Debug_Enabled Then
6537             mydebug(l_Fnd_Log_Message,c_api_name,9);
6538           End If;
6539        Elsif X_Msg_Count > 1 Then
6540            For I In 1..X_Msg_Count Loop
6541              FND_MSG_PUB.Get
6542               (p_msg_index     => i,
6543                p_encoded       => 'F',
6544                p_data          => l_Fnd_Log_Message,
6545                p_msg_index_out => l_msg_index_out );
6546 
6547               -- Fnd_Log_Debug(fnd_log.Level_Error,C_Module_name, l_Fnd_Log_Message );
6548 
6549               -- Log message in trace file
6550               IF G_debug= C_Debug_Enabled THEN
6551                   mydebug(l_Fnd_Log_Message,c_api_name,9);
6552               END IF;
6553            End Loop ;
6554       End If;
6555 
6556 END MAINTAIN_RESERVATION;
6557 
6558 
6559 END INV_MAINTAIN_RESERVATION_PUB;