DBA Data[Home] [Help]

PACKAGE BODY: APPS.MST_REPORTS_PKG

Source


1 PACKAGE BODY MST_REPORTS_PKG AS
2 /* $Header: MSTREPOB.pls 115.15 2004/07/02 19:25:33 bramacha noship $ */
3 
4   --define/declare package level variables here
5   -- g_release_type                number; -- being used in MSTEXCEP.pld
6 
7   --declare internal functions/procedures here
8   --
9 
10   --start procedure/function definition here
11   --
12 
13 
14 -- p_report_for    =>   0 -> Entire Plan ;  1 -> My Facility ;  2 -> Customer ;  4 -> Supplier
15 -- p_report_for_id =>   My Facility ID / Customer ID / Carrier ID / Supplier ID
16 function get_plan_order_count (p_plan_id       in number
17                              , p_report_for    in number
18 			     , p_report_for_id in number)
19 return number is
20   l_plan_order_count number;
21 
22   cursor cur_plan_orders (l_plan_id in number) is
23   select nvl(mp.total_orders,0)
24   from mst_plans mp
25   where mp.plan_id = l_plan_id;
26 
27 
28   cursor cur_plan_orders_myfac (l_plan_id  in number
29                             , l_myfac_id in number) is
30   select count(distinct mdd.source_header_number)
31   from mst_delivery_details mdd
32      , mst_deliveries md
33      , mst_delivery_assignments mda
34   where md.plan_id = mda.plan_id
35   and md.delivery_id = mda.delivery_id
36   and md.delivery_id in
37         (select mdl.delivery_id
38          from mst_trips t
39             , mst_trip_stops ts
40             , mst_delivery_legs mdl
41             , fte_location_parameters flp
42          where mdl.plan_id = md.plan_id
43          and ts.plan_id  = mdl.plan_id
44          and ts.stop_id  = mdl.pick_up_stop_id
45          and ts.stop_location_id = flp.location_id
46 	 and flp.facility_id = l_myfac_id
47          and ts.plan_id  = t.plan_id
48          and ts.trip_id  = t.trip_id)
49   and   mda.plan_id = mdd.plan_id
50   and   mda.delivery_detail_id = mdd.delivery_detail_id
51   and   md.plan_id = l_plan_id
52   and   mdd.container_flag = 2;
53 --  and   mdd.split_from_delivery_detail_id is null;
54 
55 
56   --considering both assigned and unassigned deliveries
57   cursor cur_plan_orders_c_s (l_plan_id      in number
58                             , l_c_s_ident    in number
59                             , l_cust_supp_id in number) is
60   select count(distinct dd.source_header_number)
61   from (
62         select mdd.source_header_number
63         from mst_delivery_details mdd
64            , mst_deliveries md
65            , mst_delivery_assignments mda
66         where md.plan_id = mda.plan_id
67         and   md.delivery_id = mda.delivery_id
68         and   mda.plan_id = mdd.plan_id
69         and   mda.delivery_detail_id = mdd.delivery_detail_id
70         and   md.plan_id = l_plan_id
71         and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
72         and   md.delivery_id in
73                     (select mdl.delivery_id
74                      from  mst_delivery_legs mdl
75                          , mst_trip_stops mts
76                      where mdl.plan_id = md.plan_id
77                      and   mdl.plan_id = mts.plan_id
78                      and ( mdl.pick_up_stop_id = mts.stop_id
79                           or mdl.drop_off_stop_id = mts.stop_id))
80   union all
81   select mdd.source_header_number
82   from mst_delivery_details mdd
83      , mst_deliveries md
84      , mst_delivery_assignments mda
85   where md.plan_id = mda.plan_id
86   and   md.delivery_id = mda.delivery_id
87   and   mda.plan_id = mdd.plan_id
88   and   mda.delivery_detail_id = mdd.delivery_detail_id
89   and   md.plan_id = l_plan_id
90   and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
91   and   not exists (select 1 from mst_delivery_legs mdl
92                     where mdl.plan_id=md.plan_id
93                     and   mdl.delivery_id = md.delivery_id)) dd;
94 
95   cursor cur_plan_orders_carr (l_plan_id      in number
96                              , l_carrier_id   in number) is
97   select count(distinct nvl(mdd.split_from_delivery_detail_id, mdd.delivery_detail_id))
98   from mst_delivery_details mdd
99   , mst_delivery_assignments mda
100   , mst_deliveries md
101   where mdd.plan_id = l_plan_id
102   and mdd.plan_id = mda.plan_id
103   and mdd.delivery_detail_id = mda.delivery_detail_id
104   and mda.parent_delivery_detail_id is null
105   and md.plan_id = mda.plan_id
106   and md.delivery_id = mda.delivery_id
107   and md.delivery_id in (select distinct mdl.delivery_id
108                          from mst_delivery_legs mdl
109 			 , mst_trips mt
110 			 where mdl.plan_id = md.plan_id
111 			 and mt.plan_id = mdl.plan_id
112 			 and mt.trip_id = mdl.trip_id
113 			 and mt.carrier_id = l_carrier_id);
114 
115 begin
116   if p_report_for = 0 then
117     open cur_plan_orders (p_plan_id);
118     fetch cur_plan_orders into l_plan_order_count;
119     close cur_plan_orders;
120   elsif p_report_for = 1 then
121     open cur_plan_orders_myfac (p_plan_id, p_report_for_id);
122     fetch cur_plan_orders_myfac into l_plan_order_count;
123     close cur_plan_orders_myfac;
124   elsif (p_report_for = 2 OR p_report_for = 4) then
125     open cur_plan_orders_c_s (p_plan_id, p_report_for, p_report_for_id);
126     fetch cur_plan_orders_c_s into l_plan_order_count;
127     close cur_plan_orders_c_s;
128   elsif p_report_for = 3 then
129     open cur_plan_orders_carr (p_plan_id, p_report_for_id);
130     fetch cur_plan_orders_carr into l_plan_order_count;
131     close cur_plan_orders_carr;
132   end if;
133 
134   return l_plan_order_count;
135 exception
136 when others then
137 	 return 0;
138 end get_plan_order_count;
139 
140 
141 -- Logical Order Groups KPI
142 function get_order_group_count (p_plan_id       in number
143                              , p_report_for     in number
144 			     , p_report_for_id  in number)
145 return number is
146   l_order_groups number;
147 
148   cursor cur_order_groups (l_plan_id in number) is
149   select count(*)
150   from mst_deliveries md
151   where md.plan_id = l_plan_id;
152 
153 
154   cursor cur_order_groups_myfac (l_plan_id  in number
155                                , l_myfac_id in number) is
156   select count(*)
157   from mst_deliveries md
158   , fte_location_parameters flp
159   where md.plan_id = l_plan_id
160   and (md.pickup_location_id = flp.location_id
161       or md.dropoff_location_id = flp.location_id)
162   and flp.facility_id = l_myfac_id;
163 
164 
165   cursor cur_order_groups_c_s (l_plan_id      in number
166                              , l_c_s_ident    in number
167     	                     , l_cust_supp_id in number) is
168   select count(*)
169   from mst_deliveries md
170   where md.plan_id = l_plan_id
171   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
172 
173   cursor cur_order_groups_carr (l_plan_id    in number
174                               , l_carrier_id in number) is
175   select count(*)
176   from mst_deliveries md
177   where md.plan_id = l_plan_id
178   and md.delivery_id in (select distinct mdl.delivery_id
179                          from mst_delivery_legs mdl
180 			 , mst_trips mt
181 			 where mdl.plan_id = md.plan_id
182 			 and mt.plan_id = mdl.plan_id
183 			 and mt.trip_id = mdl.trip_id
184 			 and mt.carrier_id = l_carrier_id);
185 begin
186   if p_report_for = 0 then
187     open cur_order_groups (p_plan_id);
188     fetch cur_order_groups into l_order_groups;
189     close cur_order_groups;
190   elsif p_report_for = 1 then
191     open cur_order_groups_myfac (p_plan_id, p_report_for_id);
192     fetch cur_order_groups_myfac into l_order_groups;
193     close cur_order_groups_myfac;
194   elsif (p_report_for = 2 OR p_report_for = 4) then
195     open cur_order_groups_c_s (p_plan_id, p_report_for, p_report_for_id);
196     fetch cur_order_groups_c_s into l_order_groups;
197     close cur_order_groups_c_s;
198   elsif p_report_for = 3 then
199     open cur_order_groups_carr (p_plan_id, p_report_for_id);
200     fetch cur_order_groups_carr into l_order_groups;
201     close cur_order_groups_carr;
202   end if;
203 
204 return l_order_groups;
205 exception
206 when others then
207 	 return 0;
208 end get_order_group_count;
209 
210 
211 -- Weight KPI
212 function get_weight (p_plan_id       in number
213                    , p_report_for    in number
214 		   , p_report_for_id in number)
215 return number is
216   l_weight number;
217 
218   cursor cur_weight (l_plan_id in number) is
219   select nvl(mp.total_weight,0)
220   from mst_plans mp
221   where mp.plan_id = l_plan_id;
222 
223 
224   cursor cur_weight_myfac (l_plan_id  in number
225                          , l_myfac_id in number) is
226   select nvl(sum(nvl(md.gross_weight,0)),0)
227   from mst_deliveries md
228      , fte_location_parameters flp
229   where md.plan_id = l_plan_id
230   and (md.pickup_location_id = flp.location_id
231       or md.dropoff_location_id = flp.location_id)
232   and flp.facility_id = l_myfac_id;
233 
234 
235   cursor cur_weight_c_s (l_plan_id      in number
236                        , l_c_s_ident    in number
237   		       , l_cust_supp_id in number) is
238   select nvl(sum(nvl(md.gross_weight,0)),0)
239   from mst_deliveries md
240   where md.plan_id = l_plan_id
241   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
242 
243   cursor cur_weight_carr (l_plan_id       in number
244                         , l_carrier_id    in number) is
245   select nvl(sum(nvl(md.gross_weight,0)),0)
246   from mst_deliveries md
247   where md.plan_id = l_plan_id
248   and md.delivery_id in (select distinct mdl.delivery_id
249                          from mst_delivery_legs mdl
250 			 , mst_trips mt
251 			 where mdl.plan_id = md.plan_id
252 			 and mt.plan_id = mdl.plan_id
253 			 and mt.trip_id = mdl.trip_id
254 			 and mt.carrier_id = l_carrier_id);
255 begin
256   if p_report_for = 0 then
257     open cur_weight (p_plan_id);
258     fetch cur_weight into l_weight;
259     close cur_weight;
260   elsif p_report_for = 1 then
261     open cur_weight_myfac (p_plan_id, p_report_for_id);
262     fetch cur_weight_myfac into l_weight;
263     close cur_weight_myfac;
264   elsif (p_report_for = 2 OR p_report_for = 4) then
265     open cur_weight_c_s (p_plan_id, p_report_for, p_report_for_id);
266     fetch cur_weight_c_s into l_weight;
267     close cur_weight_c_s;
268   elsif p_report_for = 3 then
269     open cur_weight_carr (p_plan_id, p_report_for_id);
270     fetch cur_weight_carr into l_weight;
271     close cur_weight_carr;
272   end if;
273 
274 return l_weight;
275 exception
276 when others then
277 	 return 0;
278 end get_weight;
279 
280 
281 -- Volums KPI
282 function get_volume (p_plan_id       in number
283                    , p_report_for    in number
284 		   , p_report_for_id in number)
285 return number is
286   l_volume number;
287 
288   cursor cur_volume (l_plan_id in number) is
289   select nvl(mp.total_volume,0)
290   from mst_plans mp
291   where mp.plan_id = l_plan_id;
292 
293 
294   cursor cur_volume_myfac (l_plan_id  in number
295                          , l_myfac_id in number) is
296   select nvl(sum(nvl(md.volume,0)),0)
297   from mst_deliveries md
298      , fte_location_parameters flp
299   where md.plan_id = l_plan_id
300   and (md.pickup_location_id = flp.location_id
301       or md.dropoff_location_id = flp.location_id)
302   and flp.facility_id = l_myfac_id;
303 
304 
305   cursor cur_volume_c_s (l_plan_id      in number
306                        , l_c_s_ident    in number
307   		       , l_cust_supp_id in number) is
308   select nvl(sum(nvl(md.volume,0)),0)
309   from mst_deliveries md
310   where md.plan_id = l_plan_id
311   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
312 
313   cursor cur_volume_carr (l_plan_id       in number
314                         , l_carrier_id    in number) is
315   select nvl(sum(nvl(md.volume,0)),0)
316   from mst_deliveries md
317   where md.plan_id = l_plan_id
318   and md.delivery_id in (select distinct mdl.delivery_id
319                          from mst_delivery_legs mdl
320 			 , mst_trips mt
321 			 where mdl.plan_id = md.plan_id
322 			 and mt.plan_id = mdl.plan_id
323 			 and mt.trip_id = mdl.trip_id
324 			 and mt.carrier_id = l_carrier_id);
325 begin
326   if p_report_for = 0 then
327     open cur_volume (p_plan_id);
328     fetch cur_volume into l_volume;
329     close cur_volume;
330   elsif p_report_for = 1 then
331     open cur_volume_myfac (p_plan_id, p_report_for_id);
332     fetch cur_volume_myfac into l_volume;
333     close cur_volume_myfac;
334   elsif (p_report_for = 2 OR p_report_for = 4) then
338   elsif p_report_for = 3 then
335     open cur_volume_c_s (p_plan_id, p_report_for, p_report_for_id);
336     fetch cur_volume_c_s into l_volume;
337     close cur_volume_c_s;
339     open cur_volume_carr (p_plan_id, p_report_for_id);
340     fetch cur_volume_carr into l_volume;
341     close cur_volume_carr;
342   end if;
343 
344 return l_volume;
345 exception
346 when others then
347 	 return 0;
348 end get_volume;
349 
350 
351 -- Pieces KPI
352 function get_pieces (p_plan_id       in number
353                    , p_report_for    in number
354 		   , p_report_for_id in number)
355 return number is
356   l_pieces number;
357 
358   cursor cur_pieces (l_plan_id in number) is
359   select nvl(mp.total_pieces,0)
360   from mst_plans mp
361   where mp.plan_id = l_plan_id;
362 
363 
364   cursor cur_pieces_myfac (l_plan_id  in number
365                          , l_myfac_id in number) is
366   select nvl(sum(nvl(md.number_of_pieces,0)),0)
367   from mst_deliveries md
368      , fte_location_parameters flp
369   where md.plan_id = l_plan_id
370   and (md.pickup_location_id = flp.location_id
371       or md.dropoff_location_id = flp.location_id)
372   and flp.facility_id = l_myfac_id;
373 
374 
375   cursor cur_pieces_c_s (l_plan_id      in number
376                        , l_c_s_ident    in number
377   		       , l_cust_supp_id in number) is
378   select nvl(sum(nvl(md.number_of_pieces,0)),0)
379   from mst_deliveries md
380   where md.plan_id = l_plan_id
381   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
382 
383   cursor cur_pieces_carr (l_plan_id       in number
384                         , l_carrier_id    in number) is
385   select nvl(sum(nvl(md.number_of_pieces,0)),0)
386   from mst_deliveries md
387   where md.plan_id = l_plan_id
388   and md.delivery_id in (select distinct mdl.delivery_id
389                          from mst_delivery_legs mdl
390 			 , mst_trips mt
391 			 where mdl.plan_id = md.plan_id
392 			 and mt.plan_id = mdl.plan_id
393 			 and mt.trip_id = mdl.trip_id
394 			 and mt.carrier_id = l_carrier_id);
395 begin
396   if p_report_for = 0 then
397     open cur_pieces (p_plan_id);
398     fetch cur_pieces into l_pieces;
399     close cur_pieces;
400   elsif p_report_for = 1 then
401     open cur_pieces_myfac (p_plan_id, p_report_for_id);
402     fetch cur_pieces_myfac into l_pieces;
403     close cur_pieces_myfac;
404   elsif (p_report_for = 2 OR p_report_for = 4) then
405     open cur_pieces_c_s (p_plan_id, p_report_for, p_report_for_id);
406     fetch cur_pieces_c_s into l_pieces;
407     close cur_pieces_c_s;
408   elsif p_report_for = 3 then
409     open cur_pieces_carr (p_plan_id, p_report_for_id);
410     fetch cur_pieces_carr into l_pieces;
411     close cur_pieces_carr;
412   end if;
413 
414 return l_pieces;
415 exception
416 when others then
417 	 return 0;
418 end get_pieces;
419 
420 
421 function get_plan_value (p_plan_id in number
422                        , p_report_for    in number
423 		       , p_report_for_id in number)
424 return number is
425   l_plan_value number;
426 
427   cursor cur_plan_value (l_plan_id in number) is
428   select sum(nvl(mdd.unit_price,0)* nvl(mdd.requested_quantity,0))
429   from mst_delivery_details mdd
430   , mst_delivery_assignments mda
431   , mst_deliveries md
432   where mdd.plan_id = l_plan_id
433   and mda.plan_id = mdd.plan_id
434   and mda.delivery_detail_id = mdd.delivery_detail_id
435   and mda.parent_delivery_detail_id is null
436   and md.plan_id = mda.plan_id
437   and md.delivery_id = mda.delivery_id;
438 
439   cursor cur_plan_value_myfac (l_plan_id  in number
440                              , l_myfac_id in number) is
441   select sum(nvl(mdd.unit_price,0)* nvl(mdd.requested_quantity,0))
442   from mst_delivery_details mdd
443   , mst_delivery_assignments mda
444   , mst_deliveries md
445   , fte_location_parameters flp
446   where mdd.plan_id = l_plan_id
447   and mda.plan_id = mdd.plan_id
448   and mda.delivery_detail_id = mdd.delivery_detail_id
449   and mda.parent_delivery_detail_id is null
450   and md.plan_id = mda.plan_id
451   and md.delivery_id = mda.delivery_id
452   and (md.pickup_location_id = flp.location_id
453       or md.dropoff_location_id = flp.location_id)
454   and flp.facility_id = l_myfac_id;
455 
456   cursor cur_plan_value_c_s (l_plan_id in number
457                            , l_c_s_ident    in number
458      		           , l_cust_supp_id in number) is
459   select sum(nvl(mdd.unit_price,0)* nvl(mdd.requested_quantity,0))
460   from mst_delivery_details mdd
461   , mst_delivery_assignments mda
462   , mst_deliveries md
463   where mdd.plan_id = l_plan_id
464   and mda.plan_id = mdd.plan_id
465   and mda.delivery_detail_id = mdd.delivery_detail_id
466   and mda.parent_delivery_detail_id is null
467   and md.plan_id = mda.plan_id
468   and md.delivery_id = mda.delivery_id
469   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
470 
471   cursor cur_plan_value_carr (l_plan_id      in number
472                             , l_carrier_id   in number) is
473   select sum(nvl(mdd.unit_price,0)* nvl(mdd.requested_quantity,0))
474   from mst_delivery_details mdd
475   , mst_delivery_assignments mda
476   , mst_deliveries md
477   where mdd.plan_id = l_plan_id
478   and mdd.plan_id = mda.plan_id
479   and mdd.delivery_detail_id = mda.delivery_detail_id
480   and mda.parent_delivery_detail_id is null
481   and md.plan_id = mda.plan_id
482   and md.delivery_id = mda.delivery_id
483   and md.delivery_id in (select distinct mdl.delivery_id
487 			 and mt.plan_id = mdl.plan_id
484                          from mst_delivery_legs mdl
485 			 , mst_trips mt
486 			 where mdl.plan_id = md.plan_id
488 			 and mt.trip_id = mdl.trip_id
489 			 and mt.carrier_id = l_carrier_id);
490 begin
491   if p_report_for = 0 then
492     open cur_plan_value (p_plan_id);
493     fetch cur_plan_value into l_plan_value;
494     close cur_plan_value;
495   elsif p_report_for = 1 then
496     open cur_plan_value_myfac (p_plan_id, p_report_for_id);
497     fetch cur_plan_value_myfac into l_plan_value;
498     close cur_plan_value_myfac;
499   elsif (p_report_for = 2 OR p_report_for = 4) then
500     open cur_plan_value_c_s (p_plan_id, p_report_for, p_report_for_id);
501     fetch cur_plan_value_c_s into l_plan_value;
502     close cur_plan_value_c_s;
503   elsif p_report_for = 3 then
504     open cur_plan_value_carr (p_plan_id, p_report_for_id);
505     fetch cur_plan_value_carr into l_plan_value;
506     close cur_plan_value_carr;
507   end if;
508 
509    return l_plan_value;
510 exception
511 when others then
512 	 return 0;
513 end get_plan_value;
514 
515 
516 function get_trips_per_mode (p_plan_id       in number
517                            , p_report_for    in number
518 		           , p_report_for_id in number
519 			   , p_mode          in varchar2)
520 return number is
521   l_trips_per_mode number;
522 
523   cursor cur_trips (l_plan_id in number
524                   , l_mode    in varchar2) is
525   select count(*)
526   from mst_trips mt
527   where mt.plan_id = l_plan_id
528   and mt.mode_of_transport = l_mode;
529 
530   cursor cur_trips_myfac (l_plan_id  in number
531                         , l_myfac_id in number
532 			, l_mode     in varchar2) is
533   select count(*)
534   from mst_trips mt
535   , fte_location_parameters flp
536   where mt.plan_id = l_plan_id
537   and mt.mode_of_transport = l_mode
538   and flp.location_id in (select mts.stop_location_id
539                           from mst_trip_stops mts
540 			  where mts.plan_id = mt.plan_id
541 			  and mts.trip_id = mt.trip_id)
542   and flp.facility_id = l_myfac_id;
543 
544   cursor cur_trips_c_s (l_plan_id      in number
545                       , l_c_s_ident    in number
546                       , l_cust_supp_id in number
547 		      , l_mode         in varchar2) is
548   select count(mt.trip_id)
549   from mst_trips mt
550   where mt.plan_id = l_plan_id
551   and   mt.trip_id in
552                    (select distinct mts.trip_id
553                     from mst_trip_stops mts
554                     , mst_delivery_legs mdl
555                     , mst_deliveries md
556                     where md.plan_id = mt.plan_id
557                     and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
558                     and mts.plan_id = md.plan_id
559                     and mts.stop_location_id = md.dropoff_location_id
560                     and mdl.plan_id = md.plan_id
561                     and mdl.delivery_id = md.delivery_id
562                     and mdl.trip_id = mts.trip_id
563                     and mdl.drop_off_stop_id = mts.stop_id)
564   and   mt.mode_of_transport = l_mode
565   and   EXISTS
566        (select ts.trip_id
567         from mst_trip_stops ts
568         where ts.plan_id = mt.plan_id
569         and   ts.trip_id = mt.trip_id
570         having count(ts.stop_id) >= 2
571         group by ts.trip_id);
572 
573   cursor cur_trips_carr (l_plan_id in number
574                        , l_carrier_id in number
575                        , l_mode    in varchar2) is
576   select count(*)
577   from mst_trips mt
578   where mt.plan_id = l_plan_id
579   and mt.carrier_id = l_carrier_id
580   and mt.mode_of_transport = l_mode;
581 
582 begin
583   if p_report_for = 0 then
584     open cur_trips (p_plan_id, p_mode);
585     fetch cur_trips into l_trips_per_mode;
586     close cur_trips;
587   elsif p_report_for = 1 then
588     open cur_trips_myfac (p_plan_id, p_report_for_id, p_mode);
589     fetch cur_trips_myfac into l_trips_per_mode;
590     close cur_trips_myfac;
591   elsif (p_report_for = 2 OR p_report_for = 4) then
592     open cur_trips_c_s (p_plan_id, p_report_for, p_report_for_id, p_mode);
593     fetch cur_trips_c_s into l_trips_per_mode;
594     close cur_trips_c_s;
595   elsif p_report_for = 3 then
596     open cur_trips_carr (p_plan_id, p_report_for_id, p_mode);
597     fetch cur_trips_carr into l_trips_per_mode;
598     close cur_trips_carr;
599   end if;
600 
601    return l_trips_per_mode;
602 exception
603 when others then
604 	 return 0;
605 end get_trips_per_mode;
606 
607 
608 function get_trans_cost_per_mode (p_plan_id       in number
609                                 , p_report_for    in number
610 		                , p_report_for_id in number
611 			        , p_mode          in varchar2)
612 return number is
613   l_trans_cost_per_mode number;
614 
615   cursor cur_trans_cost (l_plan_id in number
616                        , l_mode    in varchar2) is
617   select decode(l_mode,'TRUCK',nvl(mp.total_tl_cost,0)
618                       ,'LTL'  ,nvl(mp.total_ltl_cost,0)
619 		              ,nvl(mp.total_parcel_cost,0))
620   from mst_plans mp
621   where mp.plan_id = l_plan_id;
622 
623   cursor cur_trans_cost_myfac (l_plan_id  in number
624                              , l_myfac_id in number
625 			     , l_mode     in varchar2) is
626 /*
627   select nvl(sum(nvl(mdl.allocated_transport_cost,0)
628                + nvl(mdl.allocated_fac_loading_cost,0)
629                + nvl(mdl.allocated_fac_unloading_cost,0)),0)
630   from mst_delivery_legs mdl
634                from  mst_delivery_legs mdl1
631   where mdl.plan_id = l_plan_id
632   and mdl.delivery_id in
633              ( select md.delivery_id
635                    , mst_deliveries md
636                    , mst_trips mt
637                    , mst_trip_stops mts
638 		   , fte_location_parameters flp
639                 where mt.plan_id = mdl1.plan_id
640                 and   mt.trip_id  = mdl1.trip_id
641                 and   mt.mode_of_transport = l_mode
642                 and   mt.plan_id = mts.plan_id
643                 and   mt.trip_id = mts.trip_id
644                 and   mts.stop_location_id = flp.location_id
645                 and   flp.facility_id = l_myfac_id
646                 and   md.plan_id = mdl1.plan_id
647                 and   md.delivery_id  = mdl1.delivery_id
648                 and   md.plan_id = mdl.plan_id
649                 and   (md.pickup_location_id = flp.location_id
650                        or md.dropoff_location_id = flp.location_id));
651 */
652 --Bug_Fix for 3696518 - II
653   ( select nvl(sum(nvl(mdl.allocated_transport_cost,0)
654                + nvl(mdl.allocated_fac_loading_cost,0)
655                + nvl(mdl.allocated_fac_unloading_cost,0)),0)
656   from mst_deliveries md
657   , mst_delivery_legs mdl
658   , mst_trips mt
659   , fte_location_parameters flp
660   where mt.plan_id = l_plan_id
661   and mt.mode_of_transport = l_mode
662   and mt.plan_id = mdl.plan_id
663   and mt.trip_id = mdl.trip_id
664   and mdl.plan_id = md.plan_id
665   and mdl.delivery_id = md.delivery_id
666   and flp.facility_id = l_myfac_id
667   and ( md.pickup_location_id = flp.location_id
668         or md.dropoff_location_id = flp.location_id ) );
669 
670 
671   cursor cur_trans_cost_c_s (l_plan_id      in number
672                            , l_c_s_ident    in number
673                            , l_cust_supp_id in number
674 		           , l_mode         in varchar2) is
675 /*
676   select nvl(sum(nvl(mdl.allocated_transport_cost,0)
677                + nvl(mdl.allocated_fac_loading_cost,0)
678                + nvl(mdl.allocated_fac_unloading_cost,0)),0)
679   from mst_delivery_legs mdl
680   where mdl.plan_id = l_plan_id
681   and mdl.delivery_id in
682              ( select md.delivery_id
683                from  mst_delivery_legs mdl1
684                    , mst_deliveries md
685                    , mst_trips mt
686                    , mst_trip_stops mts
687                 where mt.plan_id = mdl1.plan_id
688                 and   mt.trip_id  = mdl1.trip_id
689                 and   mt.mode_of_transport = l_mode
690                 and   mt.plan_id = mts.plan_id
691                 and   mt.trip_id = mts.trip_id
692                 and   md.plan_id = mdl1.plan_id
693                 and   md.delivery_id  = mdl1.delivery_id
694                 and   md.plan_id = mdl.plan_id
695                 and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
696                 and   (md.pickup_location_id = mts.stop_location_id
697 		       or md.dropoff_location_id = mts.stop_location_id));
698 */
699 --Bug_Fix for 3696518 - II
700   ( select nvl(sum(nvl(mdl.allocated_transport_cost,0)
701                + nvl(mdl.allocated_fac_loading_cost,0)
702                + nvl(mdl.allocated_fac_unloading_cost,0)),0)
703   from mst_deliveries md
704   , mst_delivery_legs mdl
705   , mst_trips mt
706   where mt.plan_id = l_plan_id
707   and mt.mode_of_transport = l_mode
708   and mt.plan_id = mdl.plan_id
709   and mt.trip_id = mdl.trip_id
710   and mdl.plan_id = md.plan_id
711   and mdl.delivery_id = md.delivery_id
712 --  and decode ( l_report_for, 2, md.customer_id, 4, md.supplier_id, 0 ) = decode ( l_report_for, 2, l_report_for_id, 4, l_report_for_id, 0 )
713   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id );
714 
715   cursor cur_trans_cost_carr (l_plan_id  in number
716                             , l_carrier_id in number
717 			    , l_mode     in varchar2) is
718   select nvl(sum(decode(mt.mode_of_transport
719                        , 'TRUCK', (nvl(mt.total_basic_transport_cost,0)
720                                  + nvl(mt.total_stop_cost,0)
721 		                 + nvl(mt.total_load_unload_cost,0)
722 			         + nvl(mt.total_layover_cost,0)
723 			         + nvl(mt.total_accessorial_cost,0))
724 		       , (nvl(mt.total_basic_transport_cost,0)
725 		       + nvl(mt.total_accessorial_cost,0)))),0)
726   from mst_trips mt
727   where mt.plan_id = l_plan_id
728   and mt.carrier_id = l_carrier_id
729   and mt.mode_of_transport = l_mode;
730 --  and mt.continuous_move_id is null;
731 
732 begin
733   if p_report_for = 0 then
734     open cur_trans_cost (p_plan_id, p_mode);
735     fetch cur_trans_cost into l_trans_cost_per_mode;
736     close cur_trans_cost;
737   elsif p_report_for = 1 then
738     open cur_trans_cost_myfac (p_plan_id, p_report_for_id, p_mode);
739     fetch cur_trans_cost_myfac into l_trans_cost_per_mode;
740     close cur_trans_cost_myfac;
741   elsif (p_report_for = 2 OR p_report_for = 4) then
742     open cur_trans_cost_c_s (p_plan_id, p_report_for, p_report_for_id, p_mode);
743     fetch cur_trans_cost_c_s into l_trans_cost_per_mode;
744     close cur_trans_cost_c_s;
745   elsif p_report_for = 3 then
746     open cur_trans_cost_carr (p_plan_id, p_report_for_id, p_mode);
747     fetch cur_trans_cost_carr into l_trans_cost_per_mode;
748     close cur_trans_cost_carr;
749   end if;
750 
751    return l_trans_cost_per_mode;
752 exception
753 when others then
754 	 return 0;
755 end get_trans_cost_per_mode;
756 
757 function get_handl_cost_per_mode (p_plan_id       in number
758                                 , p_report_for    in number
759 		                , p_report_for_id in number
760 		                , p_mode         in varchar2)
764   cursor cur_handl_cost (l_plan_id in number
761 return number is
762   l_handl_cost_per_mode number;
763 
765                        , l_mode    in varchar2) is
766   select nvl(sum(nvl(mdl.allocated_fac_shp_hand_cost,0)
767                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
768   from mst_plans mp
769   , mst_delivery_legs mdl
770   where mp.plan_id = mdl.plan_id
771   and mdl.trip_id in (select mt.trip_id
772                       from mst_trips mt
773 		      where mt.plan_id = mp.plan_id
774 		      and   mt.mode_of_transport = l_mode)
775   and mdl.plan_id = l_plan_id;
776 
777   cursor cur_handl_cost_myfac (l_plan_id  in number
778                              , l_myfac_id in number
779 			     , l_mode     in varchar2) is
780 /*
781   select nvl(sum(nvl(mdl.allocated_fac_shp_hand_cost,0)
782                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
783   from mst_delivery_legs mdl
784   where mdl.plan_id = l_plan_id
785   and mdl.delivery_id in
786              ( select md.delivery_id
787                from  mst_delivery_legs mdl1
788                    , mst_deliveries md
789                    , mst_trips mt
790                    , mst_trip_stops mts
791 		   , fte_location_parameters flp
792                 where mt.plan_id = mdl1.plan_id
793                 and   mt.trip_id  = mdl1.trip_id
794                 and   mt.mode_of_transport = l_mode
795                 and   mt.plan_id = mts.plan_id
796                 and   mt.trip_id = mts.trip_id
797                 and   mts.stop_location_id = flp.location_id
798                 and   flp.facility_id = l_myfac_id
799                 and   md.plan_id = mdl1.plan_id
800                 and   md.delivery_id  = mdl1.delivery_id
801                 and   md.plan_id = mdl.plan_id
802                 and   (md.pickup_location_id = flp.location_id
803                        or md.dropoff_location_id = flp.location_id));
804 */
805 --Bug_Fix for 3696518 - II
806   ( select nvl(sum(nvl(mdl.allocated_fac_shp_hand_cost,0)
807                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
808   from mst_deliveries md
809   , mst_delivery_legs mdl
810   , mst_trips mt
811   , fte_location_parameters flp
812   where mt.plan_id = l_plan_id
813   and mt.mode_of_transport = l_mode
814   and mt.plan_id = mdl.plan_id
815   and mt.trip_id = mdl.trip_id
816   and mdl.plan_id = md.plan_id
817   and mdl.delivery_id = md.delivery_id
818   and flp.facility_id = l_myfac_id
819   and ( md.pickup_location_id = flp.location_id
820         or md.dropoff_location_id = flp.location_id ) );
821 
822   cursor cur_handl_cost_c_s (l_plan_id      in number
823                            , l_c_s_ident    in number
824                            , l_cust_supp_id in number
825 			   , l_mode         in varchar2) is
826 /*
827   select nvl(sum(nvl(mdl.allocated_fac_shp_hand_cost,0)
828                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
829   from mst_delivery_legs mdl
830   where mdl.plan_id = l_plan_id
831   and mdl.delivery_id in
832              ( select md.delivery_id
833                from  mst_delivery_legs mdl1
834                    , mst_deliveries md
835                    , mst_trips mt
836                    , mst_trip_stops mts
837                 where mt.plan_id = mdl1.plan_id
838                 and   mt.trip_id  = mdl1.trip_id
839                 and   mt.mode_of_transport = l_mode
840                 and   mt.plan_id = mts.plan_id
841                 and   mt.trip_id = mts.trip_id
842                 and   md.plan_id = mdl1.plan_id
843                 and   md.delivery_id  = mdl1.delivery_id
844                 and   md.plan_id = mdl.plan_id
845                 and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
846                 and   (md.pickup_location_id = mts.stop_location_id
847 		       or md.dropoff_location_id = mts.stop_location_id));
848 */
849 --Bug_Fix for 3696518 - II
850   (select nvl(sum(nvl(mdl.allocated_fac_shp_hand_cost,0)
851                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
852   from mst_deliveries md
853   , mst_delivery_legs mdl
854   , mst_trips mt
855   where mt.plan_id = l_plan_id
856   and mt.mode_of_transport = l_mode
857   and mt.plan_id = mdl.plan_id
858   and mt.trip_id = mdl.trip_id
859   and mdl.plan_id = md.plan_id
860   and mdl.delivery_id = md.delivery_id
861 --  and decode ( l_report_for, 2, md.customer_id, 4, md.supplier_id, 0 ) = decode ( l_report_for, 2, l_report_for_id, 4, l_report_for_id, 0 )
862   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id );
863 
864 
865   cursor cur_handl_cost_carr (l_plan_id  in number
866                             , l_carrier_id in number
867 			    , l_mode     in varchar2) is
868   select nvl(sum(decode(mt.mode_of_transport
869                        , 'TRUCK', (nvl(mt.total_handling_cost,0))
870 		       , (nvl(mt.total_handling_cost,0)))),0)
871   from mst_trips mt
872   where mt.plan_id = l_plan_id
873   and mt.carrier_id = l_carrier_id
874   and mt.mode_of_transport = l_mode;
875 --  and mt.continuous_move_id is null;
876 
877 begin
878   if p_report_for = 0 then
879     open cur_handl_cost (p_plan_id, p_mode);
880     fetch cur_handl_cost into l_handl_cost_per_mode;
881     close cur_handl_cost;
882   elsif p_report_for = 1 then
883     open cur_handl_cost_myfac (p_plan_id, p_report_for_id, p_mode);
884     fetch cur_handl_cost_myfac into l_handl_cost_per_mode;
885     close cur_handl_cost_myfac;
886   elsif (p_report_for = 2 OR p_report_for = 4) then
887     open cur_handl_cost_c_s (p_plan_id, p_report_for, p_report_for_id, p_mode);
888     fetch cur_handl_cost_c_s into l_handl_cost_per_mode;
889     close cur_handl_cost_c_s;
890   elsif p_report_for = 3 then
891     open cur_handl_cost_carr (p_plan_id, p_report_for_id, p_mode);
895 
892     fetch cur_handl_cost_carr into l_handl_cost_per_mode;
893     close cur_handl_cost_carr;
894   end if;
896    return l_handl_cost_per_mode;
897 exception
898 when others then
899 	 return 0;
900 end get_handl_cost_per_mode;
901 
902 function get_total_cost_per_mode (p_plan_id       in number
903                                 , p_report_for    in number
904       		                , p_report_for_id in number
905 		                , p_mode          in varchar2)
906 return number is
907   l_total_cost_per_mode number;
908 
909   cursor cur_total_cost (l_plan_id in number
910                        , l_mode    in varchar2) is
911   select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
912                + nvl(mdl.allocated_fac_unloading_cost,0)
913 	       + nvl(mdl.allocated_transport_cost,0)
914 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
915                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
916   from mst_plans mp
917   , mst_delivery_legs mdl
918   where mp.plan_id = mdl.plan_id
919   and mdl.trip_id in (select mt.trip_id
920                       from mst_trips mt
921 		      where mt.plan_id = mp.plan_id
922 		      and   mt.mode_of_transport = l_mode)
923   and mdl.plan_id = l_plan_id;
924 
925   cursor cur_total_cost_myfac (l_plan_id  in number
926                              , l_myfac_id in number
927                              , l_mode     in varchar2) is
928 /*
929   select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
930                + nvl(mdl.allocated_fac_unloading_cost,0)
931 	       + nvl(mdl.allocated_transport_cost,0)
932 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
933                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
934   from mst_delivery_legs mdl
935   where mdl.plan_id = l_plan_id
936   and mdl.delivery_id in
937              ( select md.delivery_id
938                from  mst_delivery_legs mdl1
939                    , mst_deliveries md
940                    , mst_trips mt
941                    , mst_trip_stops mts
942 		   , fte_location_parameters flp
943                 where mt.plan_id = mdl1.plan_id
944                 and   mt.trip_id  = mdl1.trip_id
945                 and   mt.mode_of_transport = l_mode
946                 and   mt.plan_id = mts.plan_id
947                 and   mt.trip_id = mts.trip_id
948                 and   mts.stop_location_id = flp.location_id
949                 and   flp.facility_id = l_myfac_id
950                 and   md.plan_id = mdl1.plan_id
951                 and   md.delivery_id  = mdl1.delivery_id
952                 and   md.plan_id = mdl.plan_id
953                 and   (md.pickup_location_id = flp.location_id
954                        or md.dropoff_location_id = flp.location_id));
955 */
956 --Bug_Fix for 3696518 - II
957   ( select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
958                + nvl(mdl.allocated_fac_unloading_cost,0)
959 	       + nvl(mdl.allocated_transport_cost,0)
960 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
961                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
962   from mst_deliveries md
963   , mst_delivery_legs mdl
964   , mst_trips mt
965   , fte_location_parameters flp
966   where mt.plan_id = l_plan_id
967   and mt.mode_of_transport = l_mode
968   and mt.plan_id = mdl.plan_id
969   and mt.trip_id = mdl.trip_id
970   and mdl.plan_id = md.plan_id
971   and mdl.delivery_id = md.delivery_id
972   and flp.facility_id = l_myfac_id
973   and ( md.pickup_location_id = flp.location_id
974         or md.dropoff_location_id = flp.location_id ) );
975 
976   cursor cur_total_cost_c_s (l_plan_id      in number
977                            , l_c_s_ident    in number
978                            , l_cust_supp_id in number
979 			   , l_mode         in varchar2) is
980 /*
981   select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
982                + nvl(mdl.allocated_fac_unloading_cost,0)
983 	       + nvl(mdl.allocated_transport_cost,0)
984 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
985                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
986   from mst_delivery_legs mdl
987   where mdl.plan_id = l_plan_id
988   and mdl.delivery_id in
989              ( select md.delivery_id
990                from  mst_delivery_legs mdl1
991                    , mst_deliveries md
992                    , mst_trips mt
993                    , mst_trip_stops mts
994                 where mt.plan_id = mdl1.plan_id
995                 and   mt.trip_id  = mdl1.trip_id
996                 and   mt.mode_of_transport = l_mode
997                 and   mt.plan_id = mts.plan_id
998                 and   mt.trip_id = mts.trip_id
999                 and   md.plan_id = mdl1.plan_id
1000                 and   md.delivery_id  = mdl1.delivery_id
1001                 and   md.plan_id = mdl.plan_id
1002                 and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
1003                 and   (md.pickup_location_id = mts.stop_location_id
1004 		       or md.dropoff_location_id = mts.stop_location_id));
1005 */
1006 --Bug_Fix for 3696518 - II
1007   (select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
1008                + nvl(mdl.allocated_fac_unloading_cost,0)
1009 	       + nvl(mdl.allocated_transport_cost,0)
1010 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
1011                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
1012   from mst_deliveries md
1013   , mst_delivery_legs mdl
1014   , mst_trips mt
1015   where mt.plan_id = l_plan_id
1016   and mt.mode_of_transport = l_mode
1017   and mt.plan_id = mdl.plan_id
1018   and mt.trip_id = mdl.trip_id
1019   and mdl.plan_id = md.plan_id
1020   and mdl.delivery_id = md.delivery_id
1021 --  and decode ( l_report_for, 2, md.customer_id, 4, md.supplier_id, 0 ) = decode ( l_report_for, 2, l_report_for_id, 4, l_report_for_id, 0 )
1025                             , l_carrier_id in number
1022   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id );
1023 
1024   cursor cur_total_cost_carr (l_plan_id  in number
1026 			    , l_mode     in varchar2) is
1027   select nvl(sum(decode(mt.mode_of_transport
1028                        , 'TRUCK', (nvl(mt.total_basic_transport_cost,0)
1029                                  + nvl(mt.total_stop_cost,0)
1030 		                 + nvl(mt.total_load_unload_cost,0)
1031 			         + nvl(mt.total_layover_cost,0)
1032 			         + nvl(mt.total_accessorial_cost,0)
1033 			         + nvl(mt.total_handling_cost,0))
1034 		       , (nvl(mt.total_basic_transport_cost,0)
1035 		        + nvl(mt.total_accessorial_cost,0)))),0)
1036   from mst_trips mt
1037   where mt.plan_id = l_plan_id
1038   and mt.carrier_id = l_carrier_id
1039   and mt.mode_of_transport = l_mode;
1040 --  and mt.continuous_move_id is null;
1041 
1042 begin
1043   if p_report_for = 0 then
1044     open cur_total_cost (p_plan_id, p_mode);
1045     fetch cur_total_cost into l_total_cost_per_mode;
1046     close cur_total_cost;
1047   elsif p_report_for = 1 then
1048     open cur_total_cost_myfac (p_plan_id, p_report_for_id, p_mode);
1049     fetch cur_total_cost_myfac into l_total_cost_per_mode;
1050     close cur_total_cost_myfac;
1051   elsif (p_report_for = 2 OR p_report_for = 4) then
1052     open cur_total_cost_c_s (p_plan_id, p_report_for, p_report_for_id, p_mode);
1053     fetch cur_total_cost_c_s into l_total_cost_per_mode;
1054     close cur_total_cost_c_s;
1055   elsif p_report_for = 3 then
1056     open cur_total_cost_carr (p_plan_id, p_report_for_id, p_mode);
1057     fetch cur_total_cost_carr into l_total_cost_per_mode;
1058     close cur_total_cost_carr;
1059   end if;
1060 
1061    return l_total_cost_per_mode;
1062 exception
1063 when others then
1064 	 return 0;
1065 end get_total_cost_per_mode;
1066 
1067 
1068 function get_stops_per_load (p_plan_id       in number
1069                            , p_report_for    in number
1070       		           , p_report_for_id in number)
1071 return number is
1072   l_TL_stops_per_load number;
1073   l_TL_stops          number;
1074   l_TLs               number;
1075   l_denom             number;
1076 
1077   cursor cur_TL_stops (l_plan_id in number) is
1078   select count(*)
1079   from mst_trip_stops mts
1080   , mst_trips mt
1081   where mt.plan_id = mts.plan_id
1082   and mt.trip_id = mts.trip_id
1083   and mt.mode_of_transport = 'TRUCK'
1084   and mts.plan_id = l_plan_id;
1085 
1086   cursor cur_TL_stops_myfac (l_plan_id  in number
1087                            , l_myfac_id in number) is
1088 /*
1089   select count(*)
1090   from mst_trip_stops mts
1091   , mst_trips mt
1092   , fte_location_parameters flp
1093   where mt.plan_id = mts.plan_id
1094   and mt.trip_id = mts.trip_id
1095   and mt.mode_of_transport = 'TRUCK'
1096   and flp.location_id = mts.stop_location_id
1097   and flp.facility_id = l_myfac_id
1098   and mts.plan_id = l_plan_id;
1099 */
1100 /*
1101 --Bug_Fix for 3693925
1102   select count(*)
1103   from mst_trip_stops mts
1104   where mts.plan_id = l_plan_id
1105   and mts.trip_id in ( select distinct mt.trip_id
1106                     from mst_trips mt
1107                     , mst_trip_stops mts1
1108                     , fte_location_parameters flp
1109                     where mt.plan_id = mts.plan_id
1110                     and mt.trip_id = mts.trip_id
1111                     and mt.mode_of_transport = 'TRUCK'
1112                     and mts1.plan_id = mt.plan_id
1113                     and mts1.trip_id = mt.trip_id
1114                     and flp.facility_id = l_myfac_id
1115                     and flp.location_id = mts1.stop_location_id );
1116 */
1117 --Bug_Fix for 3696518 - II
1118   select count ( * )
1119   from mst_trips mt
1120   , mst_trip_stops mts
1121   where mts.plan_id = l_plan_id
1122   and mts.trip_id = mt.trip_id
1123   and mt.plan_id= mts.plan_id
1124   and mt.mode_of_transport = 'TRUCK'
1125   and mt.trip_id in ( select mdl.trip_id
1126                       from mst_deliveries md
1127 					  , mst_delivery_legs mdl
1128 					  , fte_location_parameters flp
1129 					  where md.plan_id = l_plan_id
1130 					  and flp.facility_id = l_myfac_id
1131 					  and ( md.dropoff_location_id = flp.location_id
1132 					        or md.pickup_location_id = flp.location_id )
1133 					  and mdl.plan_id = md.plan_id
1134 					  and mdl.delivery_id = md.delivery_id );
1135 
1136   cursor cur_TL_stops_c_s (l_plan_id      in number
1137                          , l_c_s_ident    in number
1138                          , l_cust_supp_id in number) is
1139   select count(*)
1140   from mst_trip_stops mts
1141   , mst_trips mt
1142   where mt.plan_id = mts.plan_id
1143   and mt.trip_id = mts.trip_id
1144   and mt.mode_of_transport = 'TRUCK'
1145   and mts.plan_id = l_plan_id
1146   and mt.trip_id in (select distinct mdl.trip_id
1147                      from mst_delivery_legs mdl
1148 		     , mst_deliveries md
1149 		     where mdl.plan_id = mt.plan_id
1150 		     and md.plan_id = mdl.plan_id
1151 		     and md.delivery_id = mdl.delivery_id
1152                      and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id);
1153 
1154   cursor cur_TL_stops_carr (l_plan_id    in number
1155                           , l_carrier_id in number) is
1156   select count(*)
1157   from mst_trip_stops mts
1158   , mst_trips mt
1159   where mt.plan_id = mts.plan_id
1160   and mt.trip_id = mts.trip_id
1161   and mt.mode_of_transport = 'TRUCK'
1162   and mt.carrier_id = l_carrier_id
1163   and mts.plan_id = l_plan_id;
1164 
1165   cursor cur_TLs (l_plan_id in number) is
1166   select count(*)
1167   from mst_trips mt
1171   cursor cur_TLs_myfac (l_plan_id  in number
1168   where mt.plan_id = l_plan_id
1169   and mt.mode_of_transport = 'TRUCK';
1170 
1172                       , l_myfac_id in number) is
1173 /*
1174   select count(*)
1175   from mst_trips mt
1176   where mt.plan_id = l_plan_id
1177   and mt.mode_of_transport = 'TRUCK'
1178   and mt.trip_id in (select distinct mts.trip_id
1179                      from mst_trip_stops mts
1180 		     , fte_location_parameters flp
1181 		     where mts.plan_id = mt.plan_id
1182 		     and mts.stop_location_id = flp.location_id
1183 		     and flp.facility_id = l_myfac_id);
1184 */
1185 --Bug_Fix for 3696518 - II
1186   select count ( * )
1187   from mst_trips mt
1188   where mt.plan_id= l_plan_id
1189   and mt.mode_of_transport = 'TRUCK'
1190   and mt.trip_id in ( select mdl.trip_id
1191                       from mst_deliveries md
1192 					  , mst_delivery_legs mdl
1193 					  , fte_location_parameters flp
1194 					  where md.plan_id = l_plan_id
1195 					  and flp.facility_id = l_myfac_id
1196 					  and ( md.dropoff_location_id = flp.location_id
1197 					        or md.pickup_location_id = flp.location_id )
1198 					  and mdl.plan_id = md.plan_id
1199 					  and mdl.delivery_id = md.delivery_id );
1200 
1201   cursor cur_TLs_c_s (l_plan_id      in number
1202                     , l_c_s_ident    in number
1203                     , l_cust_supp_id in number) is
1204 /*
1205   select count(*)
1206   from mst_trips mt
1207   where mt.plan_id = l_plan_id
1208   and mt.mode_of_transport = 'TRUCK'
1209   and mt.trip_id in (select distinct mdl.trip_id
1210                      from mst_trip_stops mts
1211 		     , mst_delivery_legs mdl
1212 		     , mst_deliveries md
1213                     where md.plan_id = mt.plan_id
1214                     and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
1215                     and mts.plan_id = md.plan_id
1216                     and mts.stop_location_id = md.dropoff_location_id
1217                     and mdl.plan_id = md.plan_id
1218                     and mdl.delivery_id = md.delivery_id
1219                     and mdl.trip_id = mts.trip_id
1220                     and mdl.drop_off_stop_id = mts.stop_id)
1221   and   EXISTS
1222        (select ts.trip_id
1223         from mst_trip_stops ts
1224         where ts.plan_id = mt.plan_id
1225         and   ts.trip_id = mt.trip_id
1226         having count(ts.stop_id) >= 2
1227         group by ts.trip_id);
1228 */
1229 --Bug_Fix for 3696518 - II
1230   select count ( * )
1231   from mst_trips mt
1232   where mt.plan_id= l_plan_id
1233   and mt.mode_of_transport = 'TRUCK'
1234   and mt.trip_id in ( select mdl.trip_id
1235                       from mst_deliveries md
1236 					  , mst_delivery_legs mdl
1237 					  where md.plan_id = l_plan_id
1238 					  and mdl.plan_id = md.plan_id
1239 					  and mdl.delivery_id = md.delivery_id
1240 					  and decode ( l_c_s_ident, 2, md.customer_id, md.supplier_id ) = l_cust_supp_id );
1241 
1242   cursor cur_TLs_carr (l_plan_id in number
1243                      , l_carrier_id in number) is
1244   select count(*)
1245   from mst_trips mt
1246   where mt.plan_id = l_plan_id
1247   and mt.mode_of_transport = 'TRUCK'
1248   and mt.carrier_id = l_carrier_id;
1249 
1250 begin
1251   if p_report_for = 0 then
1252     open cur_TL_stops (p_plan_id);
1253     fetch cur_TL_stops into l_TL_stops;
1254     close cur_TL_stops;
1255 
1256     open cur_TLs (p_plan_id);
1257     fetch cur_TLs into l_TLs;
1258     close cur_TLs;
1259 
1260   elsif p_report_for = 1 then
1261     open cur_TL_stops_myfac (p_plan_id, p_report_for_id);
1262     fetch cur_TL_stops_myfac into l_TL_stops;
1263     close cur_TL_stops_myfac;
1264 
1265     open cur_TLs_myfac (p_plan_id, p_report_for_id);
1266     fetch cur_TLs_myfac into l_TLs;
1267     close cur_TLs_myfac;
1268 
1269   elsif (p_report_for = 2 OR p_report_for = 4) then
1270     open cur_TL_stops_c_s (p_plan_id, p_report_for, p_report_for_id);
1271     fetch cur_TL_stops_c_s into l_TL_stops;
1272     close cur_TL_stops_c_s;
1273 
1274     open cur_TLs_c_s (p_plan_id, p_report_for, p_report_for_id);
1275     fetch cur_TLs_c_s into l_TLs;
1276     close cur_TLs_c_s;
1277 
1278   elsif p_report_for = 3 then
1279     open cur_TL_stops_carr (p_plan_id, p_report_for_id);
1280     fetch cur_TL_stops_carr into l_TL_stops;
1281     close cur_TL_stops_carr;
1282 
1283     open cur_TLs_carr (p_plan_id, p_report_for_id);
1284     fetch cur_TLs_carr into l_TLs;
1285     close cur_TLs_carr;
1286 
1287   end if;
1288 
1289 --handling the divide by zero problem
1290   if nvl(l_TLs,0) = 0 then
1291      l_denom := 1;
1292      return 0;
1293   else
1294      l_denom := l_TLs;
1295   end if;
1296 
1297 --actual calculation of the KPI
1298   l_TL_stops_per_load := (l_TL_stops - l_TLs) / l_denom;
1299 
1300 --resetting negative KPI value to zero
1301   if nvl(l_TL_stops,0) < nvl(l_TLs,0) then
1302     l_TL_stops_per_load := 0;
1303   end if;
1304 
1305  return l_TL_stops_per_load;
1306 exception
1307 when others then
1308 	 return 0;
1309 end get_stops_per_load;
1310 
1311 
1312 function get_TL_distance (p_plan_id       in number
1313                         , p_report_for    in number
1314                         , p_report_for_id in number)
1315 return number is
1316   l_TL_distance number;
1317 
1318   cursor cur_TL_distance (l_plan_id in number) is
1319   select nvl(sum(mt.total_trip_distance),0)
1320   from mst_trips mt
1321   where mt.plan_id= l_plan_id
1322   and mt.mode_of_transport = 'TRUCK';
1323 
1324   cursor cur_TL_distance_myfac (l_plan_id  in number
1328   from mst_trips mt
1325                               , l_myfac_id in number) is
1326 /*
1327   select nvl(sum(mt.total_trip_distance),0)
1329   where mt.plan_id= l_plan_id
1330   and mt.mode_of_transport = 'TRUCK'
1331   and mt.trip_id in (select distinct mts.trip_id
1332                      from mst_trip_stops mts
1333 		     , fte_location_parameters flp
1334 		     where mts.plan_id = mt.plan_id
1335 		     and mts.stop_location_id = flp.location_id
1336 		     and flp.facility_id = l_myfac_id);
1337 */
1338 --Bug_Fix for 3696518 - II
1339   select nvl(sum(nvl(mt.total_trip_distance,0)),0)
1340   from mst_trips mt
1341   where mt.plan_id= l_plan_id
1342   and mt.mode_of_transport = 'TRUCK'
1343   and mt.trip_id in ( select mdl.trip_id
1344                       from mst_deliveries md
1345 					  , mst_delivery_legs mdl
1346 					  , fte_location_parameters flp
1347 					  where md.plan_id = l_plan_id
1348 					  and flp.facility_id = l_myfac_id
1349 					  and ( md.dropoff_location_id = flp.location_id
1350 					        or md.pickup_location_id = flp.location_id )
1351 					  and mdl.plan_id = md.plan_id
1352 					  and mdl.delivery_id = md.delivery_id );
1353 
1354 
1355   cursor cur_TL_distance_c_s (l_plan_id      in number
1356                            , l_c_s_ident    in number
1357                            , l_cust_supp_id in number) is
1358 /*
1359   select nvl(sum(mt.total_trip_distance),0)
1360   from mst_trips mt
1361   where mt.plan_id = l_plan_id
1362   and mt.mode_of_transport = 'TRUCK'
1363   and mt.trip_id in (select distinct mdl.trip_id
1364                      from mst_trip_stops mts
1365 		     , mst_delivery_legs mdl
1366 		     , mst_deliveries md
1367                     where md.plan_id = mt.plan_id
1368                     and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
1369                     and mts.plan_id = md.plan_id
1370                     and mts.stop_location_id = md.dropoff_location_id
1371                     and mdl.plan_id = md.plan_id
1372                     and mdl.delivery_id = md.delivery_id
1373                     and mdl.trip_id = mts.trip_id
1374                     and mdl.drop_off_stop_id = mts.stop_id)
1375   and   EXISTS
1376        (select ts.trip_id
1377         from mst_trip_stops ts
1378         where ts.plan_id = mt.plan_id
1379         and   ts.trip_id = mt.trip_id
1380         having count(ts.stop_id) >= 2
1381         group by ts.trip_id);
1382 */
1383 --Bug_Fix for 3696518 - II
1384   select nvl(sum(nvl(mt.total_trip_distance,0)),0)
1385   from mst_trips mt
1386   where mt.plan_id= l_plan_id
1387   and mt.mode_of_transport = 'TRUCK'
1388   and mt.trip_id in ( select mdl.trip_id
1389                       from mst_deliveries md
1390 					  , mst_delivery_legs mdl
1391 					  where md.plan_id = l_plan_id
1392 					  and mdl.plan_id = md.plan_id
1393 					  and mdl.delivery_id = md.delivery_id
1394 					  and decode ( l_c_s_ident, 2, md.customer_id, md.supplier_id ) = l_cust_supp_id );
1395 
1396   cursor cur_TL_distance_carr (l_plan_id in number
1397                              , l_carrier_id in number) is
1398   select nvl(sum(mt.total_trip_distance),0)
1399   from mst_trips mt
1400   where mt.plan_id= l_plan_id
1401   and mt.mode_of_transport = 'TRUCK'
1402   and mt.carrier_id = l_carrier_id;
1403 
1404 begin
1405   if p_report_for = 0 then
1406     open cur_TL_distance (p_plan_id);
1407     fetch cur_TL_distance into l_TL_distance;
1408     close cur_TL_distance;
1409   elsif p_report_for = 1 then
1410     open cur_TL_distance_myfac (p_plan_id, p_report_for_id);
1411     fetch cur_TL_distance_myfac into l_TL_distance;
1412     close cur_TL_distance_myfac;
1413   elsif (p_report_for = 2 OR p_report_for = 4) then
1414     open cur_TL_distance_c_s (p_plan_id, p_report_for, p_report_for_id);
1415     fetch cur_TL_distance_c_s into l_TL_distance;
1416     close cur_TL_distance_c_s;
1417   elsif p_report_for = 3 then
1418     open cur_TL_distance_carr (p_plan_id, p_report_for_id);
1419     fetch cur_TL_distance_carr into l_TL_distance;
1420     close cur_TL_distance_carr;
1421   end if;
1422 
1423    return l_TL_distance;
1424 exception
1425 when others then
1426 	 return 0;
1427 end get_TL_distance;
1428 
1429 
1430 function get_carr_movements(p_plan_id       in number
1431                           , p_report_for    in number
1432                           , p_report_for_id in number
1433 			  , p_carrier_id    in number)
1434 return number is
1435   l_carr_moves number;
1436 
1437   cursor cur_carr_moves (l_plan_id in number
1438                        , l_carrier_id in number) is
1439   select count(*)
1440   from mst_trips mt
1441   where mt.plan_id = l_plan_id
1442   and mt.carrier_id = l_carrier_id;
1443 
1444   cursor cur_carr_moves_myfac (l_plan_id    in number
1445                              , l_myfac_id   in number
1446 			     , l_carrier_id in number) is
1447 /*
1448   select count(*)
1449   from mst_trips mt
1450   , mst_trip_stops mts
1451   , fte_location_parameters flp
1452   where mt.plan_id = l_plan_id
1453   and mt.carrier_id = l_carrier_id
1454   and mts.plan_id = mt.plan_id
1455   and mts.trip_id = mt.trip_id
1456   and mts.stop_location_id = flp.location_id
1457   and flp.facility_id = l_myfac_id;
1458 */
1459 --Bug_Fix for 3696518 - II
1460   select count ( distinct mt.trip_id )
1461   from mst_deliveries md
1462   , fte_location_parameters flp
1463   , mst_delivery_legs mdl
1464   , mst_trips mt
1465   where md.plan_id = l_plan_id
1466   and flp.facility_id = l_myfac_id
1467   and ( md.dropoff_location_id = flp.location_id
1468         or md.pickup_location_id = flp.location_id )
1469   and mdl.plan_id = md.plan_id
1470   and mdl.delivery_id = md.delivery_id
1474 
1471   and mt.plan_id = mdl.plan_id
1472   and mt.trip_id = mdl.trip_id
1473   and mt.carrier_id = l_carrier_id;
1475   cursor cur_carr_moves_c_s (l_plan_id      in number
1476                            , l_c_s_ident    in number
1477 			   , l_cust_supp_id in number
1478 			   , l_carrier_id   in number) is
1479 /*
1480   select count(mt.trip_id)
1481   from mst_trips mt
1482   where mt.plan_id = l_plan_id
1483   and   mt.carrier_id = l_carrier_id
1484   and   mt.trip_id in
1485                    (select distinct mts.trip_id
1486                     from mst_trip_stops mts
1487                     , mst_delivery_legs mdl
1488                     , mst_deliveries md
1489                     where md.plan_id = mt.plan_id
1490                     and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
1491                     and mts.plan_id = md.plan_id
1492                     and mts.stop_location_id = md.dropoff_location_id
1493                     and mdl.plan_id = md.plan_id
1494                     and mdl.delivery_id = md.delivery_id
1495                     and mdl.trip_id = mts.trip_id
1496                     and mdl.drop_off_stop_id = mts.stop_id)
1497   and   EXISTS
1498        (select ts.trip_id
1499         from mst_trip_stops ts
1500         where ts.plan_id = mt.plan_id
1501         and   ts.trip_id = mt.trip_id
1502         having count(ts.stop_id) >= 2
1503         group by ts.trip_id);
1504 */
1505 --Bug_Fix for 3696518 - II
1506   select count ( distinct mt.trip_id )
1507   from mst_deliveries md
1508   , mst_delivery_legs mdl
1509   , mst_trips mt
1510   where md.plan_id = l_plan_id
1511   and decode ( l_c_s_ident, 2, md.customer_id, md.supplier_id ) = l_cust_supp_id
1512   and mdl.plan_id = md.plan_id
1513   and mdl.delivery_id = md.delivery_id
1514   and mt.plan_id = mdl.plan_id
1515   and mt.trip_id = mdl.trip_id
1516   and mt.carrier_id = l_carrier_id;
1517 
1518 begin
1519   if (p_report_for = 0 OR p_report_for = 3) then
1520     open cur_carr_moves (p_plan_id, p_carrier_id);
1521     fetch cur_carr_moves into l_carr_moves;
1522     close cur_carr_moves;
1523   elsif p_report_for = 1 then
1524     open cur_carr_moves_myfac (p_plan_id, p_report_for_id, p_carrier_id);
1525     fetch cur_carr_moves_myfac into l_carr_moves;
1526     close cur_carr_moves_myfac;
1527   elsif (p_report_for = 2 OR p_report_for = 4) then
1528     open cur_carr_moves_c_s (p_plan_id, p_report_for, p_report_for_id, p_carrier_id);
1529     fetch cur_carr_moves_c_s into l_carr_moves;
1530     close cur_carr_moves_c_s;
1531   end if;
1532 
1533    return l_carr_moves;
1534 exception
1535 when others then
1536 	 return 0;
1537 end get_carr_movements;
1538 
1539 
1540 function get_carr_cost(p_plan_id       in number
1541                      , p_report_for    in number
1542                      , p_report_for_id in number
1543 		     , p_carrier_id    in number)
1544 return number is
1545   l_carr_cost number;
1546 
1547   cursor cur_carr_cost (l_plan_id in number
1548                       , l_carrier_id in number) is
1549   select nvl(sum(nvl(mdl.allocated_transport_cost,0)),0)
1550   from mst_plans mp
1551   , mst_delivery_legs mdl
1552   where mp.plan_id = mdl.plan_id
1553   and mdl.trip_id in (select mt.trip_id
1554                       from mst_trips mt
1555 		      where mt.plan_id = mp.plan_id
1556 		      and   mt.carrier_id = l_carrier_id)
1557   and mdl.plan_id = l_plan_id;
1558 
1559   cursor cur_carr_cost_myfac (l_plan_id    in number
1560                             , l_myfac_id   in number
1561 			    , l_carrier_id in number) is
1562 /*
1563   select nvl(sum(nvl(mdl.allocated_transport_cost,0)),0)
1564   from mst_trips mt
1565      , mst_trip_stops mts
1566      , mst_delivery_legs mdl
1567      , fte_location_parameters flp
1568   where mt.plan_id = l_plan_id
1569   and   mt.plan_id = mts.plan_id
1570   and   mt.trip_id = mts.trip_id
1571   and   mt.carrier_id = l_carrier_id
1572   and   mts.plan_id = mdl.plan_id
1573 --  and   mdl.pick_up_stop_id = mts.stop_id
1574 -- Bug_Fix for 3694008
1575   and ( mdl.pick_up_stop_id = mts.stop_id
1576       or mdl.drop_off_stop_id = mts.stop_id )
1577   and   mts.stop_location_id = flp.location_id
1578   and   flp.facility_id = l_myfac_id;
1579 */
1580 --Bug_Fix for 3696518 - II
1581   select sum ( nvl ( mdl.allocated_transport_cost, 0 ) )
1582   from mst_deliveries md
1583   , fte_location_parameters flp
1584   , mst_delivery_legs mdl
1585   , mst_trips mt
1586   where md.plan_id = l_plan_id
1587   and flp.facility_id = l_myfac_id
1588   and ( md.dropoff_location_id = flp.location_id
1589         or md.pickup_location_id = flp.location_id )
1590   and mdl.plan_id = md.plan_id
1591   and mdl.delivery_id = md.delivery_id
1592   and mt.plan_id = mdl.plan_id
1593   and mt.trip_id = mdl.trip_id
1594   and mt.carrier_id = l_carrier_id;
1595 
1596   cursor cur_carr_cost_c_s (l_plan_id      in number
1597                           , l_c_s_ident    in number
1598 			  , l_cust_supp_id in number
1599 			  , l_carrier_id   in number) is
1600 /*
1601   select nvl(sum(nvl(mdl.allocated_transport_cost,0)),0)
1602   from  mst_deliveries md
1603   , mst_delivery_legs mdl
1604   , mst_trips mt
1605   where md.plan_id = l_plan_id
1606   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
1607   and   md.plan_id = mdl.plan_id
1608   and   md.delivery_id = mdl.delivery_id
1609   and   mt.plan_id = mdl.plan_id
1610   and   mt.trip_id = mdl.trip_id
1611   and   mt.carrier_id = l_carrier_id;
1612 */
1613 --Bug_Fix for 3696518 - II
1614   select sum ( nvl ( mdl.allocated_transport_cost, 0 ) )
1615   from mst_deliveries md
1616   , mst_delivery_legs mdl
1617   , mst_trips mt
1618   where md.plan_id = l_plan_id
1622   and mt.plan_id = mdl.plan_id
1619   and decode ( l_c_s_ident, 2, md.customer_id, md.supplier_id ) = l_cust_supp_id
1620   and mdl.plan_id = md.plan_id
1621   and mdl.delivery_id = md.delivery_id
1623   and mt.trip_id = mdl.trip_id
1624   and mt.carrier_id = l_carrier_id;
1625 
1626 begin
1627   if (p_report_for = 0 OR p_report_for = 3) then
1628     open cur_carr_cost (p_plan_id, p_carrier_id);
1629     fetch cur_carr_cost into l_carr_cost;
1630     close cur_carr_cost;
1631   elsif p_report_for = 1 then
1632     open cur_carr_cost_myfac (p_plan_id, p_report_for_id, p_carrier_id);
1633     fetch cur_carr_cost_myfac into l_carr_cost;
1634     close cur_carr_cost_myfac;
1635   elsif (p_report_for = 2 OR p_report_for = 4) then
1636     open cur_carr_cost_c_s (p_plan_id, p_report_for, p_report_for_id, p_carrier_id);
1637     fetch cur_carr_cost_c_s into l_carr_cost;
1638     close cur_carr_cost_c_s;
1639   end if;
1640 
1641    return l_carr_cost;
1642 exception
1643 when others then
1644 	 return 0;
1645 end get_carr_cost;
1646 
1647 
1648 --originwise summary functions
1649 --
1650 function get_orders_orig(p_plan_id       in number
1651                        , p_report_for    in number
1652                        , p_report_for_id in number
1653 		       , p_orig_state    in varchar2)
1654 return number is
1655   l_orders_orig number;
1656 
1657   cursor cur_orders_orig (l_plan_id    in number
1658                         , l_orig_state in varchar2) is
1659 --  select count(distinct nvl(mdd.split_from_delivery_detail_id, mdd.delivery_detail_id))
1660   select count(distinct mdd.source_header_number)
1661   from mst_delivery_details mdd
1662   , mst_delivery_assignments mda
1663   , mst_deliveries md
1664   , wsh_locations wl
1665   where md.plan_id = mda.plan_id
1666   and   md.delivery_id = mda.delivery_id
1667   and   md.pickup_location_id = wl.wsh_location_id
1668   and   wl.state = l_orig_state
1669   and   mdd.plan_id = mda.plan_id
1670   and   mdd.delivery_detail_id = mda.delivery_detail_id
1671   and   mda.parent_delivery_detail_id is null
1672   and   md.plan_id = l_plan_id;
1673 
1674   cursor cur_orders_orig_myfac (l_plan_id    in number
1675                               , l_myfac_id   in number
1676 			      , l_orig_state in varchar2) is
1677 /*
1678 --  select count(distinct nvl(mdd.split_from_delivery_detail_id, mdd.delivery_detail_id))
1679   select count(distinct mdd.source_header_number)
1680   from mst_delivery_details mdd
1681   , mst_delivery_assignments mda
1682   , mst_deliveries md
1683   , fte_location_parameters flp
1684   , wsh_locations wl
1685   where md.plan_id = mda.plan_id
1686   and   md.delivery_id = mda.delivery_id
1687   and   md.pickup_location_id = wl.wsh_location_id
1688   and   wl.state = l_orig_state
1689   and   flp.location_id = md.pickup_location_id
1690   and   flp.facility_id = l_myfac_id
1691   and   mdd.plan_id = mda.plan_id
1692   and   mdd.delivery_detail_id = mda.delivery_detail_id
1693   and   mda.parent_delivery_detail_id is null
1694   and   md.plan_id = l_plan_id;
1695 */
1696 --Bug_Fix for 3696518 - II
1697   select count(distinct mdd.source_header_number)
1698   from mst_delivery_assignments mda
1699   , mst_delivery_details mdd
1700   where mdd.plan_id = l_plan_id
1701   and mdd.plan_id = mda.plan_id
1702   and mdd.delivery_detail_id = mda.delivery_detail_id
1703   and mda.delivery_id in (select mdl.delivery_id
1704                          from mst_deliveries md
1705                          , mst_delivery_legs mdl
1706                          , mst_trip_stops mts
1707                          , fte_location_parameters flp
1708                          , wsh_locations wl
1709                          where mdl.plan_id = l_plan_id
1710                          and mdl.plan_id = mts.plan_id
1711                          and mdl.trip_id = mts.trip_id
1712                          and ( mdl.pick_up_stop_id = mts.stop_id
1713                                or mdl.drop_off_stop_id = mts.stop_id )
1714                          and mts.stop_location_id = flp.location_id
1715                          and flp.facility_id = l_myfac_id
1716                          and mdl.plan_id = md.plan_id
1717                          and mdl.delivery_id = md.delivery_id
1718                          and md.pickup_location_id = wl.wsh_location_id
1719                          and wl.state = l_orig_state);
1720 
1721   cursor cur_orders_orig_c_s (l_plan_id      in number
1722                             , l_c_s_ident    in number
1723 			    , l_cust_supp_id in number
1724 			    , l_orig_state   in varchar2) is
1725 --  select count(distinct nvl(mdd.split_from_delivery_detail_id, mdd.delivery_detail_id))
1726   select count(distinct mdd.source_header_number)
1727   from mst_delivery_details mdd
1728   , mst_delivery_assignments mda
1729   , mst_deliveries md
1730   , wsh_locations wl
1731   where md.plan_id = mda.plan_id
1732   and   md.delivery_id = mda.delivery_id
1733   and   md.pickup_location_id = wl.wsh_location_id
1734   and   wl.state = l_orig_state
1735   and   mdd.plan_id = mda.plan_id
1736   and   mdd.delivery_detail_id = mda.delivery_detail_id
1737   and   mda.parent_delivery_detail_id is null
1738   and   md.plan_id = l_plan_id
1739   and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
1740 
1741 
1742   cursor cur_orders_orig_carr (l_plan_id  in number
1743                              , l_carrier_id in number
1744 			     , l_orig_state in varchar2) is
1745   select count(distinct mdd.source_header_number)
1746   from mst_delivery_details mdd
1747      , mst_deliveries md
1748      , mst_delivery_assignments mda
1749   where md.plan_id = mda.plan_id
1750   and md.delivery_id = mda.delivery_id
1751   and md.delivery_id in
1752         (select mdl.delivery_id
1753          from mst_trips t
1754          , mst_trip_stops ts
1758          and ts.plan_id  = mdl.plan_id
1755          , mst_delivery_legs mdl
1756          , wsh_locations wl
1757          where mdl.plan_id = md.plan_id
1759          and ts.stop_id  = mdl.pick_up_stop_id
1760 	 and wl.wsh_location_id = md.pickup_location_id
1761          and ts.stop_location_id = wl.wsh_location_id
1762 	 and wl.state = l_orig_state
1763          and ts.plan_id  = t.plan_id
1764          and ts.trip_id  = t.trip_id
1765 	 and t.carrier_id = l_carrier_id)
1766   and   mda.plan_id = mdd.plan_id
1767   and   mda.delivery_detail_id = mdd.delivery_detail_id
1768   and   md.plan_id = l_plan_id
1769   and   mdd.container_flag = 2;
1770 
1771 begin
1772   if p_report_for = 0 then
1773     open cur_orders_orig (p_plan_id, p_orig_state);
1774     fetch cur_orders_orig into l_orders_orig;
1775     close cur_orders_orig;
1776   elsif p_report_for = 1 then
1777     open cur_orders_orig_myfac (p_plan_id, p_report_for_id, p_orig_state);
1778     fetch cur_orders_orig_myfac into l_orders_orig;
1779     close cur_orders_orig_myfac;
1780   elsif (p_report_for = 2 OR p_report_for = 4) then
1781     open cur_orders_orig_c_s (p_plan_id, p_report_for, p_report_for_id, p_orig_state);
1782     fetch cur_orders_orig_c_s into l_orders_orig;
1783     close cur_orders_orig_c_s;
1784   elsif p_report_for = 3 then
1785     open cur_orders_orig_carr (p_plan_id, p_report_for_id, p_orig_state);
1786     fetch cur_orders_orig_carr into l_orders_orig;
1787     close cur_orders_orig_carr;
1788   end if;
1789 
1790    return l_orders_orig;
1791 exception
1792 when others then
1793 	 return 0;
1794 end get_orders_orig;
1795 
1796 
1797 function get_weight_orig(p_plan_id       in number
1798                        , p_report_for    in number
1799                        , p_report_for_id in number
1800 		       , p_orig_state    in varchar2)
1801 return number is
1802   l_weight_orig number;
1803 
1804   cursor cur_weight_orig (l_plan_id in number
1805                         , l_orig_state in varchar2) is
1806   select nvl(sum(nvl(md.gross_weight,0)),0)
1807   from mst_deliveries md
1808   , wsh_locations wl
1809   where md.plan_id = l_plan_id
1810   and md.pickup_location_id = wl.wsh_location_id
1811   and wl.state = l_orig_state;
1812 
1813   cursor cur_weight_orig_myfac (l_plan_id    in number
1814                               , l_myfac_id   in number
1815 			      , l_orig_state in varchar2) is
1816 /*
1817   select nvl(sum(nvl(md.gross_weight,0)),0)
1818   from mst_deliveries md
1819   , fte_location_parameters flp
1820   , wsh_locations wl
1821   where md.plan_id = l_plan_id
1822   and md.pickup_location_id = wl.wsh_location_id
1823   and wl.state = l_orig_state
1824   and flp.location_id = md.pickup_location_id
1825   and flp.facility_id = l_myfac_id;
1826 */
1827 --Bug_Fix for 3696518 - II
1828   select nvl(sum(nvl(md.gross_weight,0)),0)
1829   from mst_deliveries md
1830   where md.plan_id = l_plan_id
1831   and md.delivery_id in (select mdl.delivery_id
1832                          from mst_deliveries md
1833                          , mst_delivery_legs mdl
1834                          , mst_trip_stops mts
1835                          , fte_location_parameters flp
1836                          , wsh_locations wl
1837                          where mdl.plan_id = l_plan_id
1838                          and mdl.plan_id = mts.plan_id
1839                          and mdl.trip_id = mts.trip_id
1840                          and ( mdl.pick_up_stop_id = mts.stop_id
1841                                or mdl.drop_off_stop_id = mts.stop_id )
1842                          and mts.stop_location_id = flp.location_id
1843                          and flp.facility_id = l_myfac_id
1844                          and mdl.plan_id = md.plan_id
1845                          and mdl.delivery_id = md.delivery_id
1846                          and md.pickup_location_id = wl.wsh_location_id
1847                          and wl.state = l_orig_state);
1848 
1849   cursor cur_weight_orig_c_s (l_plan_id      in number
1850                             , l_c_s_ident    in number
1851 			    , l_cust_supp_id in number
1852 			    , l_orig_state   in varchar2) is
1853   select nvl(sum(nvl(md.gross_weight,0)),0)
1854   from mst_deliveries md
1855   , wsh_locations wl
1856   where md.plan_id = l_plan_id
1857   and md.pickup_location_id = wl.wsh_location_id
1858   and wl.state = l_orig_state
1859   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
1860 
1861   cursor cur_weight_orig_carr (l_plan_id       in number
1862                              , l_carrier_id    in number
1863 			     , l_orig_state    in varchar2) is
1864   select nvl(sum(nvl(md.gross_weight,0)),0)
1865   from mst_deliveries md
1866   , wsh_locations wl
1867   where md.plan_id = l_plan_id
1868   and md.pickup_location_id = wl.wsh_location_id
1869   and wl.state = l_orig_state
1870   and md.delivery_id in (select distinct mdl.delivery_id
1871                          from mst_delivery_legs mdl
1872 			 , mst_trips mt
1873 			 where mdl.plan_id = md.plan_id
1874 			 and mt.plan_id = mdl.plan_id
1875 			 and mt.trip_id = mdl.trip_id
1876 			 and mt.carrier_id = l_carrier_id);
1877 begin
1878   if p_report_for = 0 then
1879     open cur_weight_orig (p_plan_id, p_orig_state);
1880     fetch cur_weight_orig into l_weight_orig;
1881     close cur_weight_orig;
1882   elsif p_report_for = 1 then
1883     open cur_weight_orig_myfac (p_plan_id, p_report_for_id, p_orig_state);
1884     fetch cur_weight_orig_myfac into l_weight_orig;
1885     close cur_weight_orig_myfac;
1886   elsif (p_report_for = 2 OR p_report_for = 4) then
1887     open cur_weight_orig_c_s (p_plan_id, p_report_for, p_report_for_id, p_orig_state);
1888     fetch cur_weight_orig_c_s into l_weight_orig;
1889     close cur_weight_orig_c_s;
1890   elsif p_report_for = 3 then
1894   end if;
1891     open cur_weight_orig_carr (p_plan_id, p_report_for_id, p_orig_state);
1892     fetch cur_weight_orig_carr into l_weight_orig;
1893     close cur_weight_orig_carr;
1895 
1896    return l_weight_orig;
1897 exception
1898 when others then
1899 	 return 0;
1900 end get_weight_orig;
1901 
1902 
1903 function get_volume_orig(p_plan_id       in number
1904                        , p_report_for    in number
1905                        , p_report_for_id in number
1906 		       , p_orig_state    in varchar2)
1907 return number is
1908   l_volume_orig number;
1909 
1910   cursor cur_volume_orig (l_plan_id in number
1911                         , l_orig_state in varchar2) is
1912   select nvl(sum(nvl(md.volume,0)),0)
1913   from mst_deliveries md
1914   , wsh_locations wl
1915   where md.plan_id = l_plan_id
1916   and md.pickup_location_id = wl.wsh_location_id
1917   and wl.state = l_orig_state;
1918 
1919   cursor cur_volume_orig_myfac (l_plan_id    in number
1920                               , l_myfac_id   in number
1921 			      , l_orig_state in varchar2) is
1922 /*
1923   select nvl(sum(nvl(md.volume,0)),0)
1924   from mst_deliveries md
1925   , fte_location_parameters flp
1926   , wsh_locations wl
1927   where md.plan_id = l_plan_id
1928   and md.pickup_location_id = wl.wsh_location_id
1929   and wl.state = l_orig_state
1930   and flp.location_id = md.pickup_location_id
1931   and flp.facility_id = l_myfac_id;
1932 */
1933 --Bug_Fix for 3693518
1934   select nvl(sum(nvl(md.volume,0)),0)
1935   from mst_deliveries md
1936   where md.plan_id = l_plan_id
1937   and md.delivery_id in (select mdl.delivery_id
1938                          from mst_deliveries md
1939                          , mst_delivery_legs mdl
1940                          , mst_trip_stops mts
1941                          , fte_location_parameters flp
1942                          , wsh_locations wl
1943                          where mdl.plan_id = l_plan_id
1944                          and mdl.plan_id = mts.plan_id
1945                          and mdl.trip_id = mts.trip_id
1946                          and ( mdl.pick_up_stop_id = mts.stop_id
1947                                or mdl.drop_off_stop_id = mts.stop_id )
1948                          and mts.stop_location_id = flp.location_id
1949                          and flp.facility_id = l_myfac_id
1950                          and mdl.plan_id = md.plan_id
1951                          and mdl.delivery_id = md.delivery_id
1952                          and md.pickup_location_id = wl.wsh_location_id
1953                          and wl.state = l_orig_state);
1954 
1955   cursor cur_volume_orig_c_s (l_plan_id      in number
1956                             , l_c_s_ident    in number
1957 			    , l_cust_supp_id in number
1958 			    , l_orig_state   in varchar2) is
1959   select nvl(sum(nvl(md.volume,0)),0)
1960   from mst_deliveries md
1961   , wsh_locations wl
1962   where md.plan_id = l_plan_id
1963   and md.pickup_location_id = wl.wsh_location_id
1964   and wl.state = l_orig_state
1965   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
1966 
1967   cursor cur_volume_orig_carr (l_plan_id       in number
1968                              , l_carrier_id    in number
1969 			     , l_orig_state    in varchar2) is
1970   select nvl(sum(nvl(md.volume,0)),0)
1971   from mst_deliveries md
1972   , wsh_locations wl
1973   where md.plan_id = l_plan_id
1974   and md.pickup_location_id = wl.wsh_location_id
1975   and wl.state = l_orig_state
1976   and md.delivery_id in (select distinct mdl.delivery_id
1977                          from mst_delivery_legs mdl
1978 			 , mst_trips mt
1979 			 where mdl.plan_id = md.plan_id
1980 			 and mt.plan_id = mdl.plan_id
1981 			 and mt.trip_id = mdl.trip_id
1982 			 and mt.carrier_id = l_carrier_id);
1983 begin
1984   if p_report_for = 0 then
1985     open cur_volume_orig (p_plan_id, p_orig_state);
1986     fetch cur_volume_orig into l_volume_orig;
1987     close cur_volume_orig;
1988   elsif p_report_for = 1 then
1989     open cur_volume_orig_myfac (p_plan_id, p_report_for_id, p_orig_state);
1990     fetch cur_volume_orig_myfac into l_volume_orig;
1991     close cur_volume_orig_myfac;
1992   elsif (p_report_for = 2 OR p_report_for = 4) then
1993     open cur_volume_orig_c_s (p_plan_id, p_report_for, p_report_for_id, p_orig_state);
1994     fetch cur_volume_orig_c_s into l_volume_orig;
1995     close cur_volume_orig_c_s;
1996   elsif p_report_for = 3 then
1997     open cur_volume_orig_carr (p_plan_id, p_report_for_id, p_orig_state);
1998     fetch cur_volume_orig_carr into l_volume_orig;
1999     close cur_volume_orig_carr;
2000   end if;
2001 
2002    return l_volume_orig;
2003 exception
2004 when others then
2005 	 return 0;
2006 end get_volume_orig;
2007 
2008 
2009 function get_pieces_orig(p_plan_id       in number
2010                        , p_report_for    in number
2011                        , p_report_for_id in number
2012 		       , p_orig_state    in varchar2)
2013 return number is
2014   l_pieces_orig number;
2015 
2016   cursor cur_pieces_orig (l_plan_id in number
2017                         , l_orig_state in varchar2) is
2018   select nvl(sum(nvl(md.number_of_pieces,0)),0)
2019   from mst_deliveries md
2020   , wsh_locations wl
2021   where md.plan_id = l_plan_id
2022   and md.pickup_location_id = wl.wsh_location_id
2023   and wl.state = l_orig_state;
2024 
2025   cursor cur_pieces_orig_myfac (l_plan_id    in number
2026                               , l_myfac_id   in number
2027 			      , l_orig_state in varchar2) is
2028 /*
2029   select nvl(sum(nvl(md.number_of_pieces,0)),0)
2030   from mst_deliveries md
2031   , fte_location_parameters flp
2032   , wsh_locations wl
2033   where md.plan_id = l_plan_id
2037   and flp.facility_id = l_myfac_id;
2034   and md.pickup_location_id = wl.wsh_location_id
2035   and wl.state = l_orig_state
2036   and flp.location_id = md.pickup_location_id
2038 */
2039 --Bug_Fix for 3696518
2040   select nvl(sum(nvl(md.number_of_pieces,0)),0)
2041   from mst_deliveries md
2042   where md.plan_id = l_plan_id
2043   and md.delivery_id in (select mdl.delivery_id
2044                          from mst_deliveries md
2045                          , mst_delivery_legs mdl
2046                          , mst_trip_stops mts
2047                          , fte_location_parameters flp
2048                          , wsh_locations wl
2049                          where mdl.plan_id = l_plan_id
2050                          and mdl.plan_id = mts.plan_id
2051                          and mdl.trip_id = mts.trip_id
2052                          and ( mdl.pick_up_stop_id = mts.stop_id
2053                                or mdl.drop_off_stop_id = mts.stop_id )
2054                          and mts.stop_location_id = flp.location_id
2055                          and flp.facility_id = l_myfac_id
2056                          and mdl.plan_id = md.plan_id
2057                          and mdl.delivery_id = md.delivery_id
2058                          and md.pickup_location_id = wl.wsh_location_id
2059                          and wl.state = l_orig_state);
2060 
2061   cursor cur_pieces_orig_c_s (l_plan_id      in number
2062                             , l_c_s_ident    in number
2063 			    , l_cust_supp_id in number
2064 			    , l_orig_state   in varchar2) is
2065   select nvl(sum(nvl(md.number_of_pieces,0)),0)
2066   from mst_deliveries md
2067   , wsh_locations wl
2068   where md.plan_id = l_plan_id
2069   and md.pickup_location_id = wl.wsh_location_id
2070   and wl.state = l_orig_state
2071   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
2072 
2073   cursor cur_pieces_orig_carr (l_plan_id       in number
2074                              , l_carrier_id    in number
2075 			     , l_orig_state    in varchar2) is
2076   select nvl(sum(nvl(md.number_of_pieces,0)),0)
2077   from mst_deliveries md
2078   , wsh_locations wl
2079   where md.plan_id = l_plan_id
2080   and md.pickup_location_id = wl.wsh_location_id
2081   and wl.state = l_orig_state
2082   and md.delivery_id in (select distinct mdl.delivery_id
2083                          from mst_delivery_legs mdl
2084 			 , mst_trips mt
2085 			 where mdl.plan_id = md.plan_id
2086 			 and mt.plan_id = mdl.plan_id
2087 			 and mt.trip_id = mdl.trip_id
2088 			 and mt.carrier_id = l_carrier_id);
2089 begin
2090   if p_report_for = 0 then
2091     open cur_pieces_orig (p_plan_id, p_orig_state);
2092     fetch cur_pieces_orig into l_pieces_orig;
2093     close cur_pieces_orig;
2094   elsif p_report_for = 1 then
2095     open cur_pieces_orig_myfac (p_plan_id, p_report_for_id, p_orig_state);
2096     fetch cur_pieces_orig_myfac into l_pieces_orig;
2097     close cur_pieces_orig_myfac;
2098   elsif (p_report_for = 2 OR p_report_for = 4) then
2099     open cur_pieces_orig_c_s (p_plan_id, p_report_for, p_report_for_id, p_orig_state);
2100     fetch cur_pieces_orig_c_s into l_pieces_orig;
2101     close cur_pieces_orig_c_s;
2102   elsif p_report_for = 3 then
2103     open cur_pieces_orig_carr (p_plan_id, p_report_for_id, p_orig_state);
2104     fetch cur_pieces_orig_carr into l_pieces_orig;
2105     close cur_pieces_orig_carr;
2106   end if;
2107 
2108    return l_pieces_orig;
2109 exception
2110 when others then
2111 	 return 0;
2112 end get_pieces_orig;
2113 
2114 
2115 function get_MTL_orig(p_plan_id       in number
2116                     , p_report_for    in number
2117                     , p_report_for_id in number
2118 		    , p_orig_state    in varchar2)
2119 return number is
2120   l_MTL_orig number;
2121 
2122   cursor cur_MTL_orig (l_plan_id in number
2123                      , l_orig_state in varchar2) is
2124   select count(*)
2125   from (select distinct mt.trip_id, count(*) num_stops
2126         from mst_trips mt
2127         , mst_trip_stops mts
2128 	where mt.plan_id = l_plan_id
2129 	and mt.trip_id in (select distinct mdl.trip_id
2130 	                   from mst_deliveries md
2131 			   , mst_delivery_legs mdl
2132 			   , mst_trip_stops mts1
2133 			   , wsh_locations wl
2134 			   where md.plan_id = mt.plan_id
2135 			   and mdl.plan_id = md.plan_id
2136 			   and mdl.delivery_id = md.delivery_id
2137                            and mts1.plan_id = mdl.plan_id
2138 			   and mts1.trip_id = mdl.trip_id
2139 			   and mts1.stop_id = mdl.pick_up_stop_id
2140 			   and mts1.stop_location_id = wl.wsh_location_id
2141 			   and mts1.stop_location_id = md.pickup_location_id
2142 			   and wl.state = l_orig_state)
2143 	and mt.mode_of_transport = 'TRUCK'
2144 	and mts.plan_id = mt.plan_id
2145 	and mts.trip_id = mt.trip_id
2146 	group by mt.trip_id) temp
2147   where temp.num_stops > 2;
2148 
2149   cursor cur_MTL_orig_myfac (l_plan_id    in number
2150                            , l_myfac_id   in number
2151 		           , l_orig_state in varchar2) is
2152   select count(*)
2153   from (select distinct mt.trip_id, count(*) num_stops
2154         from mst_trips mt
2155         , mst_trip_stops mts
2156 	where mt.plan_id = l_plan_id
2157 	and mt.trip_id in (select distinct mdl.trip_id
2158 	                   from mst_deliveries md
2159 			   , mst_delivery_legs mdl
2160 			   , mst_trip_stops mts1
2161                            , fte_location_parameters flp
2162 			   , wsh_locations wl
2163 			   where md.plan_id = mt.plan_id
2164 			   and mdl.plan_id = md.plan_id
2165 			   and mdl.delivery_id = md.delivery_id
2166                            and mts1.plan_id = mdl.plan_id
2167 			   and mts1.trip_id = mdl.trip_id
2168 			   and mts1.stop_id = mdl.pick_up_stop_id
2169 			   and mts1.stop_location_id = wl.wsh_location_id
2173                            and flp.facility_id = l_myfac_id)
2170 			   and mts1.stop_location_id = md.pickup_location_id
2171 			   and wl.state = l_orig_state
2172                            and flp.location_id = mts1.stop_location_id
2174 	and mt.mode_of_transport = 'TRUCK'
2175 	and mts.plan_id = mt.plan_id
2176 	and mts.trip_id = mt.trip_id
2177 	group by mt.trip_id) temp
2178   where temp.num_stops > 2;
2179 
2180   cursor cur_MTL_orig_c_s (l_plan_id      in number
2181                          , l_c_s_ident    in number
2182 		         , l_cust_supp_id in number
2183 			 , l_orig_state   in varchar2) is
2184   select count(*)
2185   from (select distinct mt.trip_id, count(*) num_stops
2186         from mst_trips mt
2187         , mst_trip_stops mts
2188 	where mt.plan_id = l_plan_id
2189 	and mt.mode_of_transport = 'TRUCK'
2190 	and mts.plan_id = mt.plan_id
2191 	and mts.trip_id = mt.trip_id
2192 	and mt.trip_id in
2193 	          (select distinct mdl.trip_id
2194 	           from mst_deliveries md
2195 		   , mst_delivery_legs mdl
2196 		   , mst_trip_stops mts1
2197 		   , wsh_locations wl
2198 		   where mdl.plan_id = mt.plan_id
2199 		   and mdl.pick_up_stop_id = mts1.stop_id
2200 		   and mdl.trip_id = mts1.trip_id
2201 		   and mts1.plan_id = mt.plan_id
2202 		   and mts1.stop_location_id = wl.wsh_location_id
2203 		   and mts1.stop_location_id = md.pickup_location_id
2204 		   and wl.state = l_orig_state
2205 		   and md.plan_id = mdl.plan_id
2206 		   and md.delivery_id = mdl.delivery_id
2207                    and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id)
2208 	group by mt.trip_id) temp
2209   where temp.num_stops > 2;
2210 
2211   cursor cur_MTL_orig_carr (l_plan_id in number
2212                           , l_carrier_id in number
2213                           , l_orig_state in varchar2) is
2214   select count(*)
2215   from (select distinct mt.trip_id, count(*) num_stops
2216         from mst_trips mt
2217         , mst_trip_stops mts
2218 	where mt.plan_id = l_plan_id
2219 	and mt.carrier_id = l_carrier_id
2220 	and mt.trip_id in (select distinct mdl.trip_id
2221 	                   from mst_deliveries md
2222 			   , mst_delivery_legs mdl
2223 			   , mst_trip_stops mts1
2224 			   , wsh_locations wl
2225 			   where md.plan_id = mt.plan_id
2226 			   and mdl.plan_id = md.plan_id
2227 			   and mdl.delivery_id = md.delivery_id
2228                            and mts1.plan_id = mdl.plan_id
2229 			   and mts1.trip_id = mdl.trip_id
2230 			   and mts1.stop_id = mdl.pick_up_stop_id
2231 			   and mts1.stop_location_id = wl.wsh_location_id
2232 			   and mts1.stop_location_id = md.pickup_location_id
2233 			   and wl.state = l_orig_state)
2234 	and mt.mode_of_transport = 'TRUCK'
2235 	and mts.plan_id = mt.plan_id
2236 	and mts.trip_id = mt.trip_id
2237 	group by mt.trip_id) temp
2238   where temp.num_stops > 2;
2239 
2240 begin
2241   if p_report_for = 0 then
2242     open cur_MTL_orig (p_plan_id, p_orig_state);
2243     fetch cur_MTL_orig into l_MTL_orig;
2244     close cur_MTL_orig;
2245   elsif p_report_for = 1 then
2246     open cur_MTL_orig_myfac (p_plan_id, p_report_for_id, p_orig_state);
2247     fetch cur_MTL_orig_myfac into l_MTL_orig;
2248     close cur_MTL_orig_myfac;
2249   elsif (p_report_for = 2 OR p_report_for = 4) then
2250     open cur_MTL_orig_c_s (p_plan_id, p_report_for, p_report_for_id, p_orig_state);
2251     fetch cur_MTL_orig_c_s into l_MTL_orig;
2252     close cur_MTL_orig_c_s;
2253   elsif p_report_for = 3 then
2254     open cur_MTL_orig_carr (p_plan_id, p_report_for_id, p_orig_state);
2255     fetch cur_MTL_orig_carr into l_MTL_orig;
2256     close cur_MTL_orig_carr;
2257   end if;
2258 
2259    return l_MTL_orig;
2260 exception
2261 when others then
2262 	 return 0;
2263 end get_MTL_orig;
2264 
2265 
2266 function get_DTL_orig(p_plan_id       in number
2267                     , p_report_for    in number
2268                     , p_report_for_id in number
2269 		    , p_orig_state    in varchar2)
2270 return number is
2271   l_DTL_orig number;
2272 
2273   cursor cur_DTL_orig (l_plan_id in number
2274                      , l_orig_state in varchar2) is
2275   select count(*)
2276   from (select distinct mt.trip_id, count(*) num_stops
2277         from mst_trips mt
2278         , mst_trip_stops mts
2279 	where mt.plan_id = l_plan_id
2280 	and mt.trip_id in (select distinct mdl.trip_id
2281 	                   from mst_deliveries md
2282 			   , mst_delivery_legs mdl
2283 			   , mst_trip_stops mts1
2284 			   , wsh_locations wl
2285 			   where md.plan_id = mt.plan_id
2286 			   and mdl.plan_id = md.plan_id
2287 			   and mdl.delivery_id = md.delivery_id
2288                            and mts1.plan_id = mdl.plan_id
2289 			   and mts1.trip_id = mdl.trip_id
2290 			   and mts1.stop_id = mdl.pick_up_stop_id
2291 			   and mts1.stop_location_id = wl.wsh_location_id
2292 			   and mts1.stop_location_id = md.pickup_location_id
2293 			   and wl.state = l_orig_state)
2294 	and mt.mode_of_transport = 'TRUCK'
2295 	and mts.plan_id = mt.plan_id
2296 	and mts.trip_id = mt.trip_id
2297 	group by mt.trip_id) temp
2298   where temp.num_stops = 2;
2299 
2300   cursor cur_DTL_orig_myfac (l_plan_id    in number
2301                            , l_myfac_id   in number
2302 		           , l_orig_state in varchar2) is
2303   select count(*)
2304   from (select distinct mt.trip_id, count(*) num_stops
2305         from mst_trips mt
2306         , mst_trip_stops mts
2307 	where mt.plan_id = l_plan_id
2308 	and mt.trip_id in (select distinct mdl.trip_id
2309 	                   from mst_deliveries md
2310 			   , mst_delivery_legs mdl
2311 			   , mst_trip_stops mts1
2312                            , fte_location_parameters flp
2313 			   , wsh_locations wl
2317                            and mts1.plan_id = mdl.plan_id
2314 			   where md.plan_id = mt.plan_id
2315 			   and mdl.plan_id = md.plan_id
2316 			   and mdl.delivery_id = md.delivery_id
2318 			   and mts1.trip_id = mdl.trip_id
2319 			   and mts1.stop_id = mdl.pick_up_stop_id
2320 			   and mts1.stop_location_id = wl.wsh_location_id
2321 			   and mts1.stop_location_id = md.pickup_location_id
2322 			   and wl.state = l_orig_state
2323                            and flp.location_id = mts1.stop_location_id
2324                            and flp.facility_id = l_myfac_id)
2325 	and mt.mode_of_transport = 'TRUCK'
2326 	and mts.plan_id = mt.plan_id
2327 	and mts.trip_id = mt.trip_id
2328 	group by mt.trip_id) temp
2329   where temp.num_stops = 2;
2330 
2331   cursor cur_DTL_orig_c_s (l_plan_id      in number
2332                          , l_c_s_ident    in number
2333 		         , l_cust_supp_id in number
2334 			 , l_orig_state   in varchar2) is
2335   select count(*)
2336   from (select distinct mt.trip_id, count(*) num_stops
2337         from mst_trips mt
2338         , mst_trip_stops mts
2339 	where mt.plan_id = l_plan_id
2340 	and mt.mode_of_transport = 'TRUCK'
2341 	and mts.plan_id = mt.plan_id
2342 	and mts.trip_id = mt.trip_id
2343 	and mt.trip_id in
2344 	          (select distinct mdl.trip_id
2345 	           from mst_deliveries md
2346 		   , mst_delivery_legs mdl
2347 		   , mst_trip_stops mts1
2348 		   , wsh_locations wl
2349 		   where mdl.plan_id = mt.plan_id
2350 		   and mdl.pick_up_stop_id = mts1.stop_id
2351 		   and mdl.trip_id = mts1.trip_id
2352 		   and mts1.plan_id = mt.plan_id
2353 		   and mts1.stop_location_id = wl.wsh_location_id
2354 		   and mts1.stop_location_id = md.pickup_location_id
2355 		   and wl.state = l_orig_state
2356 		   and md.plan_id = mdl.plan_id
2357 		   and md.delivery_id = mdl.delivery_id
2358                    and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id)
2359 	group by mt.trip_id) temp
2360   where temp.num_stops = 2;
2361 
2362   cursor cur_DTL_orig_carr (l_plan_id in number
2363                           , l_carrier_id in number
2364                           , l_orig_state in varchar2) is
2365   select count(*)
2366   from (select distinct mt.trip_id, count(*) num_stops
2367         from mst_trips mt
2368         , mst_trip_stops mts
2369 	where mt.plan_id = l_plan_id
2370 	and mt.carrier_id = l_carrier_id
2371 	and mt.trip_id in (select distinct mdl.trip_id
2372 	                   from mst_deliveries md
2373 			   , mst_delivery_legs mdl
2374 			   , mst_trip_stops mts1
2375 			   , wsh_locations wl
2376 			   where md.plan_id = mt.plan_id
2377 			   and mdl.plan_id = md.plan_id
2378 			   and mdl.delivery_id = md.delivery_id
2379                            and mts1.plan_id = mdl.plan_id
2380 			   and mts1.trip_id = mdl.trip_id
2381 			   and mts1.stop_id = mdl.pick_up_stop_id
2382 			   and mts1.stop_location_id = wl.wsh_location_id
2383 			   and mts1.stop_location_id = md.pickup_location_id
2384 			   and wl.state = l_orig_state)
2385 	and mt.mode_of_transport = 'TRUCK'
2386 	and mts.plan_id = mt.plan_id
2387 	and mts.trip_id = mt.trip_id
2388 	group by mt.trip_id) temp
2389   where temp.num_stops = 2;
2390 
2391 begin
2392   if p_report_for = 0 then
2393     open cur_DTL_orig (p_plan_id, p_orig_state);
2394     fetch cur_DTL_orig into l_DTL_orig;
2395     close cur_DTL_orig;
2396   elsif p_report_for = 1 then
2397     open cur_DTL_orig_myfac (p_plan_id, p_report_for_id, p_orig_state);
2398     fetch cur_DTL_orig_myfac into l_DTL_orig;
2399     close cur_DTL_orig_myfac;
2400   elsif (p_report_for = 2 OR p_report_for = 4) then
2401     open cur_DTL_orig_c_s (p_plan_id, p_report_for, p_report_for_id, p_orig_state);
2402     fetch cur_DTL_orig_c_s into l_DTL_orig;
2403     close cur_DTL_orig_c_s;
2404   elsif p_report_for = 3 then
2405     open cur_DTL_orig_carr (p_plan_id, p_report_for_id, p_orig_state);
2406     fetch cur_DTL_orig_carr into l_DTL_orig;
2407     close cur_DTL_orig_carr;
2408   end if;
2409 
2410    return l_DTL_orig;
2411 exception
2412 when others then
2413 	 return 0;
2414 end get_DTL_orig;
2415 
2416 
2417 function get_LTL_orig(p_plan_id       in number
2418                     , p_report_for    in number
2419                     , p_report_for_id in number
2420 		    , p_orig_state    in varchar2)
2421 return number is
2422   l_LTL_orig number;
2423 
2424   cursor cur_LTL_orig (l_plan_id in number
2425                      , l_orig_state in varchar2) is
2426   select count(*)
2427   from mst_trips mt
2428   where mt.plan_id = l_plan_id
2429   and mt.mode_of_transport = 'LTL'
2430   and mt.trip_id in (select distinct mdl.trip_id
2431                      from mst_deliveries md
2432 		     , mst_delivery_legs mdl
2433                      , mst_trip_stops mts1
2434 		     , wsh_locations wl
2435 		     where md.plan_id = mt.plan_id
2436 		     and mdl.plan_id = md.plan_id
2437 		     and mdl.delivery_id = md.delivery_id
2438                      and mts1.plan_id = mdl.plan_id
2439 		     and mts1.trip_id = mdl.trip_id
2440 		     and mts1.stop_id = mdl.pick_up_stop_id
2441  	 	     and mts1.stop_location_id = wl.wsh_location_id
2442 		     and mts1.stop_location_id = md.pickup_location_id
2443 		     and wl.state = l_orig_state);
2444 
2445   cursor cur_LTL_orig_myfac (l_plan_id    in number
2446                            , l_myfac_id   in number
2447 		           , l_orig_state in varchar2) is
2448   select count(*)
2449   from mst_trips mt
2450   where mt.plan_id = l_plan_id
2451   and mt.mode_of_transport = 'LTL'
2452   and mt.trip_id in (select distinct mdl.trip_id
2453                      from mst_deliveries md
2454 		     , mst_delivery_legs mdl
2455                      , mst_trip_stops mts1
2456 		     , fte_location_parameters flp
2457 		     , wsh_locations wl
2458 		     where md.plan_id = mt.plan_id
2459 		     and mdl.plan_id = md.plan_id
2460 		     and mdl.delivery_id = md.delivery_id
2461                      and mts1.plan_id = mdl.plan_id
2462 		     and mts1.trip_id = mdl.trip_id
2463 		     and mts1.stop_id = mdl.pick_up_stop_id
2464 	 	     and mts1.stop_location_id = wl.wsh_location_id
2465 		     and mts1.stop_location_id = md.pickup_location_id
2466 		     and wl.state = l_orig_state
2467 		     and flp.location_id = mts1.stop_location_id
2468 		     and flp.facility_id = l_myfac_id);
2469 
2470   cursor cur_LTL_orig_c_s (l_plan_id      in number
2471                          , l_c_s_ident    in number
2472 		         , l_cust_supp_id in number
2473 			 , l_orig_state   in varchar2) is
2474   select count(*)
2475   from mst_trips mt
2476   where mt.plan_id = l_plan_id
2477   and mt.mode_of_transport = 'LTL'
2478   and mt.trip_id in
2479 	          (select distinct mdl.trip_id
2480 	           from mst_deliveries md
2481 		   , mst_delivery_legs mdl
2482 		   , mst_trip_stops mts1
2483 		   , wsh_locations wl
2484 		   where mdl.plan_id = mt.plan_id
2485 		   and mdl.pick_up_stop_id = mts1.stop_id
2486 		   and mdl.trip_id = mts1.trip_id
2487 		   and mts1.plan_id = mt.plan_id
2488 		   and mts1.stop_location_id = wl.wsh_location_id
2489 		   and mts1.stop_location_id = md.pickup_location_id
2490 		   and wl.state = l_orig_state
2491 		   and md.plan_id = mdl.plan_id
2492 		   and md.delivery_id = mdl.delivery_id
2493                    and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id);
2494 
2495   cursor cur_LTL_orig_carr (l_plan_id in number
2496                           , l_carrier_id in number
2497                           , l_orig_state in varchar2) is
2498   select count(*)
2499   from mst_trips mt
2500   where mt.plan_id = l_plan_id
2501   and mt.mode_of_transport = 'LTL'
2502   and mt.carrier_id = l_carrier_id
2503   and mt.trip_id in (select distinct mdl.trip_id
2504                      from mst_deliveries md
2505 		     , mst_delivery_legs mdl
2506                      , mst_trip_stops mts1
2507 		     , wsh_locations wl
2508 		     where md.plan_id = mt.plan_id
2509 		     and mdl.plan_id = md.plan_id
2510 		     and mdl.delivery_id = md.delivery_id
2511                      and mts1.plan_id = mdl.plan_id
2512 		     and mts1.trip_id = mdl.trip_id
2513 		     and mts1.stop_id = mdl.pick_up_stop_id
2514  	 	     and mts1.stop_location_id = wl.wsh_location_id
2515 		     and mts1.stop_location_id = md.pickup_location_id
2516 		     and wl.state = l_orig_state);
2517 begin
2518   if p_report_for = 0 then
2522   elsif p_report_for = 1 then
2519     open cur_LTL_orig (p_plan_id, p_orig_state);
2520     fetch cur_LTL_orig into l_LTL_orig;
2521     close cur_LTL_orig;
2523     open cur_LTL_orig_myfac (p_plan_id, p_report_for_id, p_orig_state);
2524     fetch cur_LTL_orig_myfac into l_LTL_orig;
2525     close cur_LTL_orig_myfac;
2526   elsif (p_report_for = 2 OR p_report_for = 4) then
2527     open cur_LTL_orig_c_s (p_plan_id, p_report_for, p_report_for_id, p_orig_state);
2528     fetch cur_LTL_orig_c_s into l_LTL_orig;
2529     close cur_LTL_orig_c_s;
2530   elsif p_report_for = 3 then
2531     open cur_LTL_orig_carr (p_plan_id, p_report_for_id, p_orig_state);
2532     fetch cur_LTL_orig_carr into l_LTL_orig;
2533     close cur_LTL_orig_carr;
2534   end if;
2535 
2536    return l_LTL_orig;
2537 exception
2538 when others then
2539 	 return 0;
2540 end get_LTL_orig;
2541 
2542 
2543 function get_PCL_orig(p_plan_id       in number
2544                     , p_report_for    in number
2545                     , p_report_for_id in number
2546 		    , p_orig_state    in varchar2)
2547 return number is
2548   l_PCL_orig number;
2549 
2550   cursor cur_PCL_orig (l_plan_id in number
2551                      , l_orig_state in varchar2) is
2552   select count(*)
2553   from mst_trips mt
2554   where mt.plan_id = l_plan_id
2555   and mt.mode_of_transport = 'PARCEL'
2556   and mt.trip_id in (select distinct mdl.trip_id
2557                      from mst_deliveries md
2558 		     , mst_delivery_legs mdl
2559                      , mst_trip_stops mts1
2560 		     , wsh_locations wl
2561 		     where md.plan_id = mt.plan_id
2562 		     and mdl.plan_id = md.plan_id
2563 		     and mdl.delivery_id = md.delivery_id
2564                      and mts1.plan_id = mdl.plan_id
2565 		     and mts1.trip_id = mdl.trip_id
2566 		     and mts1.stop_id = mdl.pick_up_stop_id
2567 		     and mts1.stop_location_id = wl.wsh_location_id
2568 		     and mts1.stop_location_id = md.pickup_location_id
2569 		     and wl.state = l_orig_state);
2570 
2571   cursor cur_PCL_orig_myfac (l_plan_id    in number
2572                            , l_myfac_id   in number
2573 		           , l_orig_state in varchar2) is
2574   select count(*)
2575   from mst_trips mt
2576   where mt.plan_id = l_plan_id
2577   and mt.mode_of_transport = 'PARCEL'
2578   and mt.trip_id in (select distinct mdl.trip_id
2579                      from mst_deliveries md
2580 		     , mst_delivery_legs mdl
2581                      , mst_trip_stops mts1
2582 		     , fte_location_parameters flp
2583 		     , wsh_locations wl
2584 		     where md.plan_id = mt.plan_id
2585 		     and mdl.plan_id = md.plan_id
2586 		     and mdl.delivery_id = md.delivery_id
2587                      and mts1.plan_id = mdl.plan_id
2588 		     and mts1.trip_id = mdl.trip_id
2589 		     and mts1.stop_id = mdl.pick_up_stop_id
2590 		     and mts1.stop_location_id = wl.wsh_location_id
2591 		     and mts1.stop_location_id = md.pickup_location_id
2592 		     and wl.state = l_orig_state
2593 		     and flp.location_id = mts1.stop_location_id
2594 		     and flp.facility_id = l_myfac_id);
2595 
2596   cursor cur_PCL_orig_c_s (l_plan_id      in number
2597                          , l_c_s_ident    in number
2598 		         , l_cust_supp_id in number
2599 			 , l_orig_state   in varchar2) is
2600   select count(*)
2601   from mst_trips mt
2602   where mt.plan_id = l_plan_id
2603   and mt.mode_of_transport = 'PARCEL'
2604   and mt.trip_id in
2605 	          (select distinct mdl.trip_id
2606 	           from mst_deliveries md
2607 		   , mst_delivery_legs mdl
2608 		   , mst_trip_stops mts1
2609 		   , wsh_locations wl
2610 		   where mdl.plan_id = mt.plan_id
2611 		   and mdl.pick_up_stop_id = mts1.stop_id
2612 		   and mdl.trip_id = mts1.trip_id
2613 		   and mts1.plan_id = mt.plan_id
2614 		   and mts1.stop_location_id = wl.wsh_location_id
2615 		   and mts1.stop_location_id = md.pickup_location_id
2616 		   and wl.state = l_orig_state
2617 		   and md.plan_id = mdl.plan_id
2618 		   and md.delivery_id = mdl.delivery_id
2619                    and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id);
2620 
2621   cursor cur_PCL_orig_carr (l_plan_id in number
2622                           , l_carrier_id in number
2623                           , l_orig_state in varchar2) is
2624   select count(*)
2625   from mst_trips mt
2626   where mt.plan_id = l_plan_id
2627   and mt.mode_of_transport = 'PARCEL'
2628   and mt.carrier_id = l_carrier_id
2629   and mt.trip_id in (select distinct mdl.trip_id
2630                      from mst_deliveries md
2631 		     , mst_delivery_legs mdl
2632                      , mst_trip_stops mts1
2633 		     , wsh_locations wl
2634 		     where md.plan_id = mt.plan_id
2635 		     and mdl.plan_id = md.plan_id
2636 		     and mdl.delivery_id = md.delivery_id
2637                      and mts1.plan_id = mdl.plan_id
2638 		     and mts1.trip_id = mdl.trip_id
2639 		     and mts1.stop_id = mdl.pick_up_stop_id
2640 		     and mts1.stop_location_id = wl.wsh_location_id
2641 		     and mts1.stop_location_id = md.pickup_location_id
2642 		     and wl.state = l_orig_state);
2643 begin
2644   if p_report_for = 0 then
2645     open cur_PCL_orig (p_plan_id, p_orig_state);
2646     fetch cur_PCL_orig into l_PCL_orig;
2647     close cur_PCL_orig;
2648   elsif p_report_for = 1 then
2649     open cur_PCL_orig_myfac (p_plan_id, p_report_for_id, p_orig_state);
2650     fetch cur_PCL_orig_myfac into l_PCL_orig;
2651     close cur_PCL_orig_myfac;
2652   elsif (p_report_for = 2 OR p_report_for = 4) then
2653     open cur_PCL_orig_c_s (p_plan_id, p_report_for, p_report_for_id, p_orig_state);
2654     fetch cur_PCL_orig_c_s into l_PCL_orig;
2655     close cur_PCL_orig_c_s;
2656   elsif p_report_for = 3 then
2660   end if;
2657     open cur_PCL_orig_carr (p_plan_id, p_report_for_id, p_orig_state);
2658     fetch cur_PCL_orig_carr into l_PCL_orig;
2659     close cur_PCL_orig_carr;
2661 
2662    return l_PCL_orig;
2663 exception
2664 when others then
2665 	 return 0;
2666 end get_PCL_orig;
2667 
2668 
2669 function get_total_cost_mode_orig (p_plan_id       in number
2670                                  , p_report_for    in number
2671       		                 , p_report_for_id in number
2672 		                 , p_mode          in varchar2
2673 				 , p_orig_state     in varchar2)
2674 return number is
2675   l_total_cost_per_mode number;
2676 
2677   cursor cur_total_cost (l_plan_id   in number
2678                        , l_mode      in varchar2
2679 		       , l_orig_state in varchar2) is
2680 /*
2681   select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
2682                + nvl(mdl.allocated_fac_unloading_cost,0)
2683 	       + nvl(mdl.allocated_transport_cost,0)
2684 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
2685                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
2686   from mst_delivery_legs mdl
2687   where mdl.plan_id = l_plan_id
2688   and mdl.delivery_id in
2689              ( select md.delivery_id
2690                from  mst_delivery_legs mdl1
2691                    , mst_deliveries md
2692                    , mst_trips mt
2693                    , mst_trip_stops mts
2694 		   , wsh_locations wl
2695                 where mt.plan_id = mdl1.plan_id
2696                 and   mt.trip_id  = mdl1.trip_id
2697                 and   mt.mode_of_transport = l_mode
2698                 and   mt.plan_id = mts.plan_id
2699                 and   mt.trip_id = mts.trip_id
2700 	        and   mts.stop_location_id = wl.wsh_location_id
2701 	        and   wl.state = l_orig_state
2702                 and   md.plan_id = mdl1.plan_id
2703                 and   md.delivery_id  = mdl1.delivery_id
2704                 and   md.plan_id = mdl.plan_id
2705                 and   md.pickup_location_id = mts.stop_location_id);
2706 */
2707 --Bug_Fix for 3696518
2708   (select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
2709                + nvl(mdl.allocated_fac_unloading_cost,0)
2710 	       + nvl(mdl.allocated_transport_cost,0)
2711 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
2712                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
2713   from mst_deliveries md
2714   , mst_delivery_legs mdl
2715   , mst_trips mt
2716 --  , mst_trip_stops mts
2717   , wsh_locations wl
2718   where mt.plan_id = l_plan_id
2719   and mt.mode_of_transport = l_mode
2720   and mt.plan_id = mdl.plan_id
2721   and mt.trip_id = mdl.trip_id
2722   and mdl.plan_id = md.plan_id
2723   and mdl.delivery_id = md.delivery_id
2724   and wl.state = l_orig_state
2725   and md.pickup_location_id = wl.wsh_location_id );
2726 /*
2727   and mts.stop_location_id = wl.wsh_location_id
2728   and wl.state = l_orig_state
2729   and mt.plan_id = mts.plan_id
2730   and mt.trip_id = mts.trip_id
2731   and md.pickup_location_id = mts.stop_location_id);
2732 */
2733 
2734 
2735   cursor cur_total_cost_myfac (l_plan_id   in number
2736                              , l_myfac_id  in number
2737                              , l_mode      in varchar2
2738 			     , l_orig_state in varchar2) is
2739 /*
2740   select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
2741                + nvl(mdl.allocated_fac_unloading_cost,0)
2742 	       + nvl(mdl.allocated_transport_cost,0)
2743 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
2744                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
2745   from mst_delivery_legs mdl
2746   where mdl.plan_id = l_plan_id
2747   and mdl.delivery_id in
2748              ( select md.delivery_id
2749                from  mst_delivery_legs mdl1
2750                    , mst_deliveries md
2751                    , mst_trips mt
2752                    , mst_trip_stops mts
2753 		   , fte_location_parameters flp
2754 		   , wsh_locations wl
2755                 where mt.plan_id = mdl1.plan_id
2756                 and   mt.trip_id  = mdl1.trip_id
2757                 and   mt.mode_of_transport = l_mode
2758                 and   mt.plan_id = mts.plan_id
2759                 and   mt.trip_id = mts.trip_id
2760                 and   mts.stop_location_id = flp.location_id
2761                 and   flp.facility_id = l_myfac_id
2762                 and   md.plan_id = mdl1.plan_id
2763                 and   md.delivery_id  = mdl1.delivery_id
2764                 and   md.plan_id = mdl.plan_id
2765                 and   md.pickup_location_id = flp.location_id
2766 	        and   flp.location_id = wl.wsh_location_id
2767 	        and   wl.state = l_orig_state);
2768 */
2769 /*
2770 --Bug_Fix for 3696518
2771   (select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
2772                + nvl(mdl.allocated_fac_unloading_cost,0)
2773 	       + nvl(mdl.allocated_transport_cost,0)
2774 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
2775                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
2776   from mst_deliveries md
2777   , mst_delivery_legs mdl
2778   , mst_trips mt
2779 --  , mst_trip_stops mts
2780   , wsh_locations wl
2781   , fte_location_parameters flp
2782   where mt.plan_id = l_plan_id
2783   and mt.mode_of_transport = l_mode
2784   and mt.plan_id = mdl.plan_id
2785   and mt.trip_id = mdl.trip_id
2786   and mdl.plan_id = md.plan_id
2787   and mdl.delivery_id = md.delivery_id
2788 --  and mts.stop_location_id = wl.wsh_location_id
2789   and md.pickup_location_id = wl.wsh_location_id
2790   and wl.state = l_orig_state
2791 --  and mt.plan_id = mts.plan_id
2792 --  and mt.trip_id = mts.trip_id
2793   and flp.facility_id = l_myfac_id
2794   and md.pickup_location_id = flp.location_id );
2795 --  and md.pickup_location_id = mts.stop_location_id );
2796 */
2800 	       + nvl(mdl.allocated_transport_cost,0)
2797 --Bug_Fix for 3696518 - II
2798   select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
2799                + nvl(mdl.allocated_fac_unloading_cost,0)
2801 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
2802                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
2803   from mst_delivery_legs mdl
2804   , mst_trips mt
2805   where mt.plan_id = l_plan_id
2806   and mt.mode_of_transport = l_mode
2807   and mt.plan_id = mdl.plan_id
2808   and mt.trip_id = mdl.trip_id
2809   and mdl.delivery_id in (select mdl.delivery_id
2810                          from mst_deliveries md
2811                          , mst_delivery_legs mdl
2812                          , mst_trip_stops mts
2813                          , fte_location_parameters flp
2814                          , wsh_locations wl
2815                          where mdl.plan_id = l_plan_id
2816                          and mdl.plan_id = mts.plan_id
2817                          and mdl.trip_id = mts.trip_id
2818                          and ( mdl.pick_up_stop_id = mts.stop_id
2819                                or mdl.drop_off_stop_id = mts.stop_id )
2820                          and mts.stop_location_id = flp.location_id
2821                          and flp.facility_id = l_myfac_id
2822                          and mdl.plan_id = md.plan_id
2823                          and mdl.delivery_id = md.delivery_id
2824                          and md.pickup_location_id = wl.wsh_location_id
2825                          and wl.state = l_orig_state);
2826 
2827   cursor cur_total_cost_c_s (l_plan_id      in number
2828                            , l_c_s_ident    in number
2829                            , l_cust_supp_id in number
2830 			   , l_mode         in varchar2
2831 			   , l_orig_state    in varchar2) is
2832 /*
2833   select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
2834                + nvl(mdl.allocated_fac_unloading_cost,0)
2835 	       + nvl(mdl.allocated_transport_cost,0)
2836 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
2837                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
2838   from mst_delivery_legs mdl
2839   where mdl.plan_id = l_plan_id
2840   and mdl.delivery_id in
2841              ( select md.delivery_id
2842                from mst_trips mt
2843                 , mst_trip_stops mts
2844                 , mst_delivery_legs mdl1
2845                 , mst_deliveries md
2846 		, wsh_locations wl
2847                 where mt.plan_id = mdl1.plan_id
2848                 and   mt.trip_id  = mdl1.trip_id
2849                 and   mt.mode_of_transport = l_mode
2850                 and   mt.plan_id = mts.plan_id
2851                 and   mt.trip_id = mts.trip_id
2852                 and   mts.stop_location_id = wl.wsh_location_id
2853 	        and   wl.state = l_orig_state
2854                 and   md.plan_id = mdl1.plan_id
2855                 and   md.delivery_id  = mdl1.delivery_id
2856                 and   md.plan_id = mdl.plan_id
2857                 and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
2858                 and   md.pickup_location_id = mts.stop_location_id);
2859 */
2860 --Bug_Fix for 3696518
2861   (select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
2862                + nvl(mdl.allocated_fac_unloading_cost,0)
2863 	       + nvl(mdl.allocated_transport_cost,0)
2864 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
2865                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
2866   from mst_deliveries md
2867   , mst_delivery_legs mdl
2868   , mst_trips mt
2869 --  , mst_trip_stops mts
2870   , wsh_locations wl
2871   where mt.plan_id = l_plan_id
2872   and mt.mode_of_transport = l_mode
2873   and mt.plan_id = mdl.plan_id
2874   and mt.trip_id = mdl.trip_id
2875   and mdl.plan_id = md.plan_id
2876   and mdl.delivery_id = md.delivery_id
2877 --  and mts.stop_location_id = wl.wsh_location_id
2878   and md.pickup_location_id = wl.wsh_location_id
2879   and wl.state = l_orig_state
2880 --  and mt.plan_id = mts.plan_id
2881 --  and mt.trip_id = mts.trip_id
2882   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id );
2883 --  and md.pickup_location_id = mts.stop_location_id );
2884 
2885 
2886   cursor cur_total_cost_carr (l_plan_id   in number
2887                             , l_carrier_id in number
2888                             , l_mode      in varchar2
2889 		            , l_orig_state in varchar2) is
2890 /*
2891   select nvl(sum(decode(mt.mode_of_transport
2892                        , 'TRUCK', (nvl(mt.total_basic_transport_cost,0)
2893                                  + nvl(mt.total_stop_cost,0)
2894 		                 + nvl(mt.total_load_unload_cost,0)
2895 			         + nvl(mt.total_layover_cost,0)
2896 			         + nvl(mt.total_accessorial_cost,0)
2897 			         + nvl(mt.total_handling_cost,0))
2898 		       , (nvl(mt.total_basic_transport_cost,0)
2899 		        + nvl(mt.total_accessorial_cost,0)))),0)
2900   from mst_trips mt
2901   where mt.plan_id = l_plan_id
2902   and mt.carrier_id = l_carrier_id
2903   and mt.mode_of_transport = l_mode
2904 --  and mt.continuous_move_id is null --check
2905   and   mt.trip_id in
2906                    (select distinct mts.trip_id
2907                     from mst_trip_stops mts
2908                     , mst_delivery_legs mdl
2909                     , mst_deliveries md
2910                     , wsh_locations wl
2911                     where md.plan_id = mt.plan_id
2912                     and mts.plan_id = md.plan_id
2913                     and mdl.plan_id = md.plan_id
2914                     and mdl.delivery_id = md.delivery_id
2915                     and mdl.trip_id = mts.trip_id
2916 		    and mts.stop_id = mdl.pick_up_stop_id
2917                     and mts.stop_location_id = md.pickup_location_id
2918 		    and wl.wsh_location_id = mts.stop_location_id
2919 		    and wl.state = l_orig_state);
2920 */
2924                   + nvl(mdl.allocated_fac_rec_hand_cost,0)
2921 	select  nvl ( sum(nvl(mdl.allocated_fac_loading_cost,0)
2922         	  + nvl(mdl.allocated_fac_unloading_cost,0)
2923 	          + nvl(mdl.allocated_fac_shp_hand_cost,0)
2925     	  	  + nvl(mdl.allocated_transport_cost,0)), 0 )
2926 	from  mst_deliveries md
2927 	, mst_delivery_legs mdl
2928 	, mst_trips mt
2929 	, wsh_locations wl
2930 	, mst_trip_stops mts
2931 	where md.plan_id = l_plan_id
2932 	and md.plan_id = mt.plan_id
2933 	and md.plan_id = mdl.plan_id
2934 	and md.plan_id = mts.plan_id
2935 	and md.delivery_id = mdl.delivery_id
2936 	and mt.trip_id = mdl.trip_id
2937 	and mt.carrier_id = l_carrier_id
2938 	and mt.mode_of_transport = l_mode
2939 	and mts.trip_id = mt.trip_id
2940 	and mts.stop_location_id = wl.wsh_location_id
2941 	and mdl.pick_up_stop_id = mts.stop_id
2942 	and wl.state = l_orig_state;
2943 begin
2944   if p_report_for = 0 then
2945     open cur_total_cost (p_plan_id, p_mode, p_orig_state);
2946     fetch cur_total_cost into l_total_cost_per_mode;
2947     close cur_total_cost;
2948   elsif p_report_for = 1 then
2949     open cur_total_cost_myfac (p_plan_id, p_report_for_id, p_mode, p_orig_state);
2950     fetch cur_total_cost_myfac into l_total_cost_per_mode;
2951     close cur_total_cost_myfac;
2952   elsif (p_report_for = 2 OR p_report_for = 4) then
2953     open cur_total_cost_c_s (p_plan_id, p_report_for, p_report_for_id, p_mode, p_orig_state);
2954     fetch cur_total_cost_c_s into l_total_cost_per_mode;
2955     close cur_total_cost_c_s;
2956   elsif p_report_for = 3 then
2957     open cur_total_cost_carr (p_plan_id, p_report_for_id, p_mode, p_orig_state);
2958     fetch cur_total_cost_carr into l_total_cost_per_mode;
2959     close cur_total_cost_carr;
2960   end if;
2961 
2962    return nvl ( l_total_cost_per_mode, 0 );
2963 exception
2964 when others then
2965 	 return 0;
2966 end get_total_cost_mode_orig;
2967 
2968 
2969 function get_orders_dest(p_plan_id           in number
2970                        , p_report_for        in number
2971                        , p_report_for_id     in number
2972 		       , p_destination_state    in varchar2)
2973 return number is
2974   l_orders_dest number;
2975 
2976   cursor cur_orders_dest (l_plan_id        in number
2977                         , l_destination_state in varchar2) is
2978 --  select count(distinct nvl(mdd.split_from_delivery_detail_id, mdd.delivery_detail_id))
2979   select count(distinct mdd.source_header_number)
2980   from mst_delivery_details mdd
2981   , mst_delivery_assignments mda
2982   , mst_deliveries md
2983   , wsh_locations wl
2984   where md.plan_id = mda.plan_id
2985   and   md.delivery_id = mda.delivery_id
2986   and   md.dropoff_location_id = wl.wsh_location_id
2987   and   wl.state = l_destination_state
2988   and   mdd.plan_id = mda.plan_id
2989   and   mdd.delivery_detail_id = mda.delivery_detail_id
2990   and   mda.parent_delivery_detail_id is null
2991   and   md.plan_id = l_plan_id;
2992 
2993   cursor cur_orders_dest_myfac (l_plan_id        in number
2994                               , l_myfac_id       in number
2995 			      , l_destination_state in varchar2) is
2996 /*
2997 --  select count(distinct nvl(mdd.split_from_delivery_detail_id, mdd.delivery_detail_id))
2998   select count(distinct mdd.source_header_number)
2999   from mst_delivery_details mdd
3000   , mst_delivery_assignments mda
3001   , mst_deliveries md
3002   , fte_location_parameters flp
3003   , wsh_locations wl
3004   where md.plan_id = mda.plan_id
3005   and   md.delivery_id = mda.delivery_id
3006   and   md.dropoff_location_id = wl.wsh_location_id
3007   and   wl.state = l_destination_state
3008   and   flp.location_id = md.dropoff_location_id
3009   and   flp.facility_id = l_myfac_id
3010   and   mdd.plan_id = mda.plan_id
3011   and   mdd.delivery_detail_id = mda.delivery_detail_id
3012   and   mda.parent_delivery_detail_id is null
3013   and   md.plan_id = l_plan_id;
3014 */
3015 --Bug_Fix for 3696518 - II
3016   select count(distinct mdd.source_header_number)
3017   from mst_delivery_assignments mda
3018   , mst_delivery_details mdd
3019   where mdd.plan_id = l_plan_id
3020   and mdd.plan_id = mda.plan_id
3021   and mdd.delivery_detail_id = mda.delivery_detail_id
3022   and mda.delivery_id in (select mdl.delivery_id
3023                          from mst_deliveries md
3024                          , mst_delivery_legs mdl
3025                          , mst_trip_stops mts
3026                          , fte_location_parameters flp
3027                          , wsh_locations wl
3028                          where mdl.plan_id = l_plan_id
3029                          and mdl.plan_id = mts.plan_id
3030                          and mdl.trip_id = mts.trip_id
3031                          and ( mdl.pick_up_stop_id = mts.stop_id
3032                                or mdl.drop_off_stop_id = mts.stop_id )
3033                          and mts.stop_location_id = flp.location_id
3034                          and flp.facility_id = l_myfac_id
3035                          and mdl.plan_id = md.plan_id
3036                          and mdl.delivery_id = md.delivery_id
3037                          and md.dropoff_location_id = wl.wsh_location_id
3038                          and wl.state = l_destination_state);
3039 
3040   cursor cur_orders_dest_c_s (l_plan_id          in number
3041                             , l_c_s_ident        in number
3042 			    , l_cust_supp_id     in number
3043 			    , l_destination_state   in varchar2) is
3044 --  select count(distinct nvl(mdd.split_from_delivery_detail_id, mdd.delivery_detail_id))
3045   select count(distinct mdd.source_header_number)
3046   from mst_delivery_details mdd
3047   , mst_delivery_assignments mda
3048   , mst_deliveries md
3049   , wsh_locations wl
3050   where md.plan_id = mda.plan_id
3051   and   md.delivery_id = mda.delivery_id
3055   and   mdd.delivery_detail_id = mda.delivery_detail_id
3052   and   md.dropoff_location_id = wl.wsh_location_id
3053   and   wl.state = l_destination_state
3054   and   mdd.plan_id = mda.plan_id
3056   and   mda.parent_delivery_detail_id is null
3057   and   md.plan_id = l_plan_id
3058   and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
3059 
3060   cursor cur_orders_dest_carr (l_plan_id  in number
3061                              , l_carrier_id in number
3062 			     , l_destination_state in varchar2) is
3063   select count(distinct mdd.source_header_number)
3064   from mst_delivery_details mdd
3065      , mst_deliveries md
3066      , mst_delivery_assignments mda
3067   where md.plan_id = mda.plan_id
3068   and md.delivery_id = mda.delivery_id
3069   and md.delivery_id in
3070         (select mdl.delivery_id
3071          from mst_trips t
3072          , mst_trip_stops ts
3073          , mst_delivery_legs mdl
3074          , wsh_locations wl
3075          where mdl.plan_id = md.plan_id
3076          and ts.plan_id  = mdl.plan_id
3077          and ts.stop_id  = mdl.drop_off_stop_id
3078 	 and wl.wsh_location_id = md.dropoff_location_id
3079          and ts.stop_location_id = wl.wsh_location_id
3080 	 and wl.state = l_destination_state
3081          and ts.plan_id  = t.plan_id
3082          and ts.trip_id  = t.trip_id
3083 	 and t.carrier_id = l_carrier_id)
3084   and   mda.plan_id = mdd.plan_id
3085   and   mda.delivery_detail_id = mdd.delivery_detail_id
3086   and   md.plan_id = l_plan_id
3087   and   mdd.container_flag = 2;
3088 
3089 begin
3090   if p_report_for = 0 then
3091     open cur_orders_dest (p_plan_id, p_destination_state);
3092     fetch cur_orders_dest into l_orders_dest;
3093     close cur_orders_dest;
3094   elsif p_report_for = 1 then
3095     open cur_orders_dest_myfac (p_plan_id, p_report_for_id, p_destination_state);
3096     fetch cur_orders_dest_myfac into l_orders_dest;
3097     close cur_orders_dest_myfac;
3098   elsif (p_report_for = 2 OR p_report_for = 4) then
3099     open cur_orders_dest_c_s (p_plan_id, p_report_for, p_report_for_id, p_destination_state);
3100     fetch cur_orders_dest_c_s into l_orders_dest;
3101     close cur_orders_dest_c_s;
3102   elsif p_report_for = 3 then
3103     open cur_orders_dest_carr (p_plan_id, p_report_for_id, p_destination_state);
3104     fetch cur_orders_dest_carr into l_orders_dest;
3105     close cur_orders_dest_carr;
3106   end if;
3107 
3108    return l_orders_dest;
3109 exception
3110 when others then
3111 	 return 0;
3112 end get_orders_dest;
3113 
3114 
3115 function get_weight_dest(p_plan_id           in number
3116                        , p_report_for        in number
3117                        , p_report_for_id     in number
3118 		       , p_destination_state    in varchar2)
3119 return number is
3120   l_weight_dest number;
3121 
3122   cursor cur_weight_dest (l_plan_id        in number
3123                         , l_destination_state in varchar2) is
3124   select nvl(sum(nvl(md.gross_weight,0)),0)
3125   from mst_deliveries md
3126   , wsh_locations wl
3127   where md.plan_id = l_plan_id
3128   and   md.dropoff_location_id = wl.wsh_location_id
3129   and   wl.state = l_destination_state;
3130 
3131   cursor cur_weight_dest_myfac (l_plan_id        in number
3132                               , l_myfac_id       in number
3133 			      , l_destination_state in varchar2) is
3134 /*
3135   select nvl(sum(nvl(md.gross_weight,0)),0)
3136   from mst_deliveries md
3137   , fte_location_parameters flp
3138   , wsh_locations wl
3139   where md.plan_id = l_plan_id
3140   and   md.dropoff_location_id = wl.wsh_location_id
3141   and   wl.state = l_destination_state
3142   and flp.location_id = md.dropoff_location_id
3143   and flp.facility_id = l_myfac_id;
3144 */
3145 --Bug_Fix for 3696518 - II
3146   select nvl(sum(nvl(md.gross_weight,0)),0)
3147   from mst_deliveries md
3148   where md.plan_id = l_plan_id
3149   and md.delivery_id in (select mdl.delivery_id
3150                          from mst_deliveries md
3151                          , mst_delivery_legs mdl
3152                          , mst_trip_stops mts
3153                          , fte_location_parameters flp
3154                          , wsh_locations wl
3155                          where mdl.plan_id = l_plan_id
3156                          and mdl.plan_id = mts.plan_id
3157                          and mdl.trip_id = mts.trip_id
3158                          and ( mdl.pick_up_stop_id = mts.stop_id
3159                                or mdl.drop_off_stop_id = mts.stop_id )
3160                          and mts.stop_location_id = flp.location_id
3161                          and flp.facility_id = l_myfac_id
3162                          and mdl.plan_id = md.plan_id
3163                          and mdl.delivery_id = md.delivery_id
3164                          and md.dropoff_location_id = wl.wsh_location_id
3165                          and wl.state = l_destination_state);
3166 
3167   cursor cur_weight_dest_c_s (l_plan_id          in number
3168                             , l_c_s_ident        in number
3169 			    , l_cust_supp_id     in number
3170 			    , l_destination_state   in varchar2) is
3171   select nvl(sum(nvl(md.gross_weight,0)),0)
3172   from mst_deliveries md
3173   , wsh_locations wl
3174   where md.plan_id = l_plan_id
3175   and   md.dropoff_location_id = wl.wsh_location_id
3176   and   wl.state = l_destination_state
3177   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
3178 
3179   cursor cur_weight_dest_carr (l_plan_id       in number
3180                              , l_carrier_id    in number
3181 			     , l_destination_state    in varchar2) is
3182   select nvl(sum(nvl(md.gross_weight,0)),0)
3183   from mst_deliveries md
3184   , wsh_locations wl
3185   where md.plan_id = l_plan_id
3189                          from mst_delivery_legs mdl
3186   and md.dropoff_location_id = wl.wsh_location_id
3187   and wl.state = l_destination_state
3188   and md.delivery_id in (select distinct mdl.delivery_id
3190 			 , mst_trips mt
3191 			 where mdl.plan_id = md.plan_id
3192 			 and mt.plan_id = mdl.plan_id
3193 			 and mt.trip_id = mdl.trip_id
3194 			 and mt.carrier_id = l_carrier_id);
3195 begin
3196   if p_report_for = 0 then
3197     open cur_weight_dest (p_plan_id, p_destination_state);
3198     fetch cur_weight_dest into l_weight_dest;
3199     close cur_weight_dest;
3200   elsif p_report_for = 1 then
3201     open cur_weight_dest_myfac (p_plan_id, p_report_for_id, p_destination_state);
3202     fetch cur_weight_dest_myfac into l_weight_dest;
3203     close cur_weight_dest_myfac;
3204   elsif (p_report_for = 2 OR p_report_for = 4) then
3205     open cur_weight_dest_c_s (p_plan_id, p_report_for, p_report_for_id, p_destination_state);
3206     fetch cur_weight_dest_c_s into l_weight_dest;
3207     close cur_weight_dest_c_s;
3208   elsif p_report_for = 3 then
3209     open cur_weight_dest_carr (p_plan_id, p_report_for_id, p_destination_state);
3210     fetch cur_weight_dest_carr into l_weight_dest;
3211     close cur_weight_dest_carr;
3212   end if;
3213 
3214    return l_weight_dest;
3215 exception
3216 when others then
3217 	 return 0;
3218 end get_weight_dest;
3219 
3220 
3221 function get_volume_dest(p_plan_id           in number
3222                        , p_report_for        in number
3223                        , p_report_for_id     in number
3224 		       , p_destination_state    in varchar2)
3225 return number is
3226   l_volume_dest number;
3227 
3228   cursor cur_volume_dest (l_plan_id        in number
3229                         , l_destination_state in varchar2) is
3230   select nvl(sum(nvl(md.volume,0)),0)
3231   from mst_deliveries md
3232   , wsh_locations wl
3233   where md.plan_id = l_plan_id
3234   and   md.dropoff_location_id = wl.wsh_location_id
3235   and   wl.state = l_destination_state;
3236 
3237   cursor cur_volume_dest_myfac (l_plan_id        in number
3238                               , l_myfac_id       in number
3239 			      , l_destination_state in varchar2) is
3240 /*
3241   select nvl(sum(nvl(md.volume,0)),0)
3242   from mst_deliveries md
3243   , fte_location_parameters flp
3244   , wsh_locations wl
3245   where md.plan_id = l_plan_id
3246   and   md.dropoff_location_id = wl.wsh_location_id
3247   and   wl.state = l_destination_state
3248   and flp.location_id = md.dropoff_location_id
3249   and flp.facility_id = l_myfac_id;
3250 */
3251 --Bug_Fix for 3696518 - II
3252   select nvl(sum(nvl(md.volume,0)),0)
3253   from mst_deliveries md
3254   where md.plan_id = l_plan_id
3255   and md.delivery_id in (select mdl.delivery_id
3256                          from mst_deliveries md
3257                          , mst_delivery_legs mdl
3258                          , mst_trip_stops mts
3259                          , fte_location_parameters flp
3260                          , wsh_locations wl
3261                          where mdl.plan_id = l_plan_id
3262                          and mdl.plan_id = mts.plan_id
3263                          and mdl.trip_id = mts.trip_id
3264                          and ( mdl.pick_up_stop_id = mts.stop_id
3265                                or mdl.drop_off_stop_id = mts.stop_id )
3266                          and mts.stop_location_id = flp.location_id
3267                          and flp.facility_id = l_myfac_id
3268                          and mdl.plan_id = md.plan_id
3269                          and mdl.delivery_id = md.delivery_id
3270                          and md.dropoff_location_id = wl.wsh_location_id
3271                          and wl.state = l_destination_state);
3272 
3273   cursor cur_volume_dest_c_s (l_plan_id          in number
3274                             , l_c_s_ident        in number
3275 			    , l_cust_supp_id     in number
3276 			    , l_destination_state   in varchar2) is
3277   select nvl(sum(nvl(md.volume,0)),0)
3278   from mst_deliveries md
3279   , wsh_locations wl
3280   where md.plan_id = l_plan_id
3281   and   md.dropoff_location_id = wl.wsh_location_id
3282   and   wl.state = l_destination_state
3283   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
3284 
3285   cursor cur_volume_dest_carr (l_plan_id       in number
3286                              , l_carrier_id    in number
3287 			     , l_destination_state    in varchar2) is
3288   select nvl(sum(nvl(md.volume,0)),0)
3289   from mst_deliveries md
3290   , wsh_locations wl
3291   where md.plan_id = l_plan_id
3292   and md.dropoff_location_id = wl.wsh_location_id
3293   and wl.state = l_destination_state
3294   and md.delivery_id in (select distinct mdl.delivery_id
3295                          from mst_delivery_legs mdl
3296 			 , mst_trips mt
3297 			 where mdl.plan_id = md.plan_id
3298 			 and mt.plan_id = mdl.plan_id
3299 			 and mt.trip_id = mdl.trip_id
3300 			 and mt.carrier_id = l_carrier_id);
3301 begin
3302   if p_report_for = 0 then
3303     open cur_volume_dest (p_plan_id, p_destination_state);
3304     fetch cur_volume_dest into l_volume_dest;
3305     close cur_volume_dest;
3306   elsif p_report_for = 1 then
3307     open cur_volume_dest_myfac (p_plan_id, p_report_for_id, p_destination_state);
3308     fetch cur_volume_dest_myfac into l_volume_dest;
3309     close cur_volume_dest_myfac;
3310   elsif (p_report_for = 2 OR p_report_for = 4) then
3311     open cur_volume_dest_c_s (p_plan_id, p_report_for, p_report_for_id, p_destination_state);
3312     fetch cur_volume_dest_c_s into l_volume_dest;
3313     close cur_volume_dest_c_s;
3314   elsif p_report_for = 3 then
3315     open cur_volume_dest_carr (p_plan_id, p_report_for_id, p_destination_state);
3316     fetch cur_volume_dest_carr into l_volume_dest;
3317     close cur_volume_dest_carr;
3321 exception
3318   end if;
3319 
3320    return l_volume_dest;
3322 when others then
3323 	 return 0;
3324 end get_volume_dest;
3325 
3326 
3327 function get_pieces_dest(p_plan_id           in number
3328                        , p_report_for        in number
3329                        , p_report_for_id     in number
3330 		       , p_destination_state    in varchar2)
3331 return number is
3332   l_pieces_dest number;
3333 
3334   cursor cur_pieces_dest (l_plan_id        in number
3335                         , l_destination_state in varchar2) is
3336   select nvl(sum(nvl(md.number_of_pieces,0)),0)
3337   from mst_deliveries md
3338   , wsh_locations wl
3339   where md.plan_id = l_plan_id
3340   and   md.dropoff_location_id = wl.wsh_location_id
3341   and   wl.state = l_destination_state;
3342 
3343   cursor cur_pieces_dest_myfac (l_plan_id        in number
3344                               , l_myfac_id       in number
3345 			      , l_destination_state in varchar2) is
3346 /*
3347   select nvl(sum(nvl(md.number_of_pieces,0)),0)
3348   from mst_deliveries md
3349   , fte_location_parameters flp
3350   , wsh_locations wl
3351   where md.plan_id = l_plan_id
3352   and   md.dropoff_location_id = wl.wsh_location_id
3353   and   wl.state = l_destination_state
3354   and flp.location_id = md.dropoff_location_id
3355   and flp.facility_id = l_myfac_id;
3356 */
3357 --Bug_Fix for 3696518 - II
3358   select nvl(sum(nvl(md.number_of_pieces,0)),0)
3359   from mst_deliveries md
3360   where md.plan_id = l_plan_id
3361   and md.delivery_id in (select mdl.delivery_id
3362                          from mst_deliveries md
3363                          , mst_delivery_legs mdl
3364                          , mst_trip_stops mts
3365                          , fte_location_parameters flp
3366                          , wsh_locations wl
3367                          where mdl.plan_id = l_plan_id
3368                          and mdl.plan_id = mts.plan_id
3369                          and mdl.trip_id = mts.trip_id
3370                          and ( mdl.pick_up_stop_id = mts.stop_id
3371                                or mdl.drop_off_stop_id = mts.stop_id )
3372                          and mts.stop_location_id = flp.location_id
3373                          and flp.facility_id = l_myfac_id
3374                          and mdl.plan_id = md.plan_id
3375                          and mdl.delivery_id = md.delivery_id
3376                          and md.dropoff_location_id = wl.wsh_location_id
3377                          and wl.state = l_destination_state);
3378 
3379   cursor cur_pieces_dest_c_s (l_plan_id          in number
3380                             , l_c_s_ident        in number
3381 			    , l_cust_supp_id     in number
3382 			    , l_destination_state   in varchar2) is
3383   select nvl(sum(nvl(md.number_of_pieces,0)),0)
3384   from mst_deliveries md
3385   , wsh_locations wl
3386   where md.plan_id = l_plan_id
3387   and   md.dropoff_location_id = wl.wsh_location_id
3388   and   wl.state = l_destination_state
3389   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
3390 
3391   cursor cur_pieces_dest_carr (l_plan_id       in number
3392                              , l_carrier_id    in number
3393 			     , l_destination_state    in varchar2) is
3394   select nvl(sum(nvl(md.number_of_pieces,0)),0)
3395   from mst_deliveries md
3396   , wsh_locations wl
3397   where md.plan_id = l_plan_id
3398   and md.dropoff_location_id = wl.wsh_location_id
3399   and wl.state = l_destination_state
3400   and md.delivery_id in (select distinct mdl.delivery_id
3401                          from mst_delivery_legs mdl
3402 			 , mst_trips mt
3403 			 where mdl.plan_id = md.plan_id
3404 			 and mt.plan_id = mdl.plan_id
3405 			 and mt.trip_id = mdl.trip_id
3406 			 and mt.carrier_id = l_carrier_id);
3407 begin
3408   if p_report_for = 0 then
3409     open cur_pieces_dest (p_plan_id, p_destination_state);
3410     fetch cur_pieces_dest into l_pieces_dest;
3411     close cur_pieces_dest;
3412   elsif p_report_for = 1 then
3413     open cur_pieces_dest_myfac (p_plan_id, p_report_for_id, p_destination_state);
3414     fetch cur_pieces_dest_myfac into l_pieces_dest;
3415     close cur_pieces_dest_myfac;
3416   elsif (p_report_for = 2 OR p_report_for = 4) then
3417     open cur_pieces_dest_c_s (p_plan_id, p_report_for, p_report_for_id, p_destination_state);
3418     fetch cur_pieces_dest_c_s into l_pieces_dest;
3419     close cur_pieces_dest_c_s;
3420   elsif p_report_for = 3 then
3421     open cur_pieces_dest_carr (p_plan_id, p_report_for_id, p_destination_state);
3422     fetch cur_pieces_dest_carr into l_pieces_dest;
3423     close cur_pieces_dest_carr;
3424   end if;
3425 
3426    return l_pieces_dest;
3427 exception
3428 when others then
3429 	 return 0;
3430 end get_pieces_dest;
3431 
3432 
3433 function get_MTL_dest(p_plan_id           in number
3434                     , p_report_for        in number
3435                     , p_report_for_id     in number
3436 		    , p_destination_state    in varchar2)
3437 return number is
3438   l_MTL_dest number;
3439 
3440   cursor cur_MTL_dest (l_plan_id        in number
3441                      , l_destination_state in varchar2) is
3442   select count(*)
3443   from (select distinct mt.trip_id, count(*) num_stops
3444         from mst_trips mt
3445         , mst_trip_stops mts
3446 	where mt.plan_id = l_plan_id
3447 	and mt.trip_id in (select distinct mdl.trip_id
3448 	                   from mst_deliveries md
3449 			   , mst_delivery_legs mdl
3450 			   , mst_trip_stops mts1
3451 			   , wsh_locations wl
3452 			   where md.plan_id = mt.plan_id
3453 			   and mdl.plan_id = md.plan_id
3454 			   and mdl.delivery_id = md.delivery_id
3455                            and mts1.plan_id = mdl.plan_id
3456 			   and mts1.trip_id = mdl.trip_id
3460 			   and wl.state = l_destination_state)
3457 			   and mts1.stop_id = mdl.drop_off_stop_id
3458 			   and mts1.stop_location_id = wl.wsh_location_id
3459 			   and mts1.stop_location_id = md.dropoff_location_id
3461 	and mt.mode_of_transport = 'TRUCK'
3462 	and mts.plan_id = mt.plan_id
3463 	and mts.trip_id = mt.trip_id
3464 	group by mt.trip_id) temp
3465   where temp.num_stops > 2;
3466 
3467   cursor cur_MTL_dest_myfac (l_plan_id        in number
3468                            , l_myfac_id       in number
3469 		           , l_destination_state in varchar2) is
3470   select count(*)
3471   from (select distinct mt.trip_id, count(*) num_stops
3472         from mst_trips mt
3473         , mst_trip_stops mts
3474 	where mt.plan_id = l_plan_id
3475 	and mt.trip_id in (select distinct mdl.trip_id
3476 	                   from mst_deliveries md
3477 			   , mst_delivery_legs mdl
3478 			   , mst_trip_stops mts1
3479                            , fte_location_parameters flp
3480 			   , wsh_locations wl
3481 			   where md.plan_id = mt.plan_id
3482 			   and mdl.plan_id = md.plan_id
3483 			   and mdl.delivery_id = md.delivery_id
3484                            and mts1.plan_id = mdl.plan_id
3485 			   and mts1.trip_id = mdl.trip_id
3486 			   and mts1.stop_id = mdl.drop_off_stop_id
3487 			   and mts1.stop_location_id = wl.wsh_location_id
3488 			   and mts1.stop_location_id = md.dropoff_location_id
3489 			   and wl.state = l_destination_state
3490                            and flp.location_id = mts1.stop_location_id
3491                            and flp.facility_id = l_myfac_id)
3492 	and mt.mode_of_transport = 'TRUCK'
3493 	and mts.plan_id = mt.plan_id
3494 	and mts.trip_id = mt.trip_id
3495 	group by mt.trip_id) temp
3496   where temp.num_stops > 2;
3497 
3498   cursor cur_MTL_dest_c_s (l_plan_id          in number
3499                          , l_c_s_ident        in number
3500 		         , l_cust_supp_id     in number
3501 			 , l_destination_state   in varchar2) is
3502   select count(*)
3503   from (select distinct mt.trip_id, count(*) num_stops
3504         from mst_trips mt
3505         , mst_trip_stops mts
3506 	where mt.plan_id = l_plan_id
3507 	and mt.mode_of_transport = 'TRUCK'
3508 	and mts.plan_id = mt.plan_id
3509 	and mts.trip_id = mt.trip_id
3510 	and mt.trip_id in
3511 	          (select distinct mdl.trip_id
3512 	           from mst_deliveries md
3513 		   , mst_delivery_legs mdl
3514 		   , mst_trip_stops mts1
3515 		   , wsh_locations wl
3516 		   where mdl.plan_id = mt.plan_id
3517 		   and mdl.drop_off_stop_id = mts1.stop_id
3518 		   and mdl.trip_id = mts1.trip_id
3519 		   and mts1.plan_id = mt.plan_id
3520 		   and mts1.stop_location_id = wl.wsh_location_id
3521 		   and mts1.stop_location_id = md.dropoff_location_id
3522 		   and wl.state = l_destination_state
3523 		   and md.plan_id = mdl.plan_id
3524 		   and md.delivery_id = mdl.delivery_id
3525                    and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id)
3526 	group by mt.trip_id) temp
3527   where temp.num_stops > 2;
3528 
3529   cursor cur_MTL_dest_carr (l_plan_id        in number
3530                           , l_carrier_id     in number
3531                           , l_destination_state in varchar2) is
3532   select count(*)
3533   from (select distinct mt.trip_id, count(*) num_stops
3534         from mst_trips mt
3535         , mst_trip_stops mts
3536 	where mt.plan_id = l_plan_id
3537 	and mt.carrier_id = l_carrier_id
3538 	and mt.trip_id in (select distinct mdl.trip_id
3539 	                   from mst_deliveries md
3540 			   , mst_delivery_legs mdl
3541 			   , mst_trip_stops mts1
3542 			   , wsh_locations wl
3543 			   where md.plan_id = mt.plan_id
3544 			   and mdl.plan_id = md.plan_id
3545 			   and mdl.delivery_id = md.delivery_id
3546                            and mts1.plan_id = mdl.plan_id
3547 			   and mts1.trip_id = mdl.trip_id
3548 			   and mts1.stop_id = mdl.drop_off_stop_id
3549 			   and mts1.stop_location_id = wl.wsh_location_id
3550 			   and mts1.stop_location_id = md.dropoff_location_id
3551 			   and wl.state = l_destination_state)
3552 	and mt.mode_of_transport = 'TRUCK'
3553 	and mts.plan_id = mt.plan_id
3554 	and mts.trip_id = mt.trip_id
3555 	group by mt.trip_id) temp
3556   where temp.num_stops > 2;
3557 begin
3558   if p_report_for = 0 then
3559     open cur_MTL_dest (p_plan_id, p_destination_state);
3560     fetch cur_MTL_dest into l_MTL_dest;
3561     close cur_MTL_dest;
3562   elsif p_report_for = 1 then
3563     open cur_MTL_dest_myfac (p_plan_id, p_report_for_id, p_destination_state);
3564     fetch cur_MTL_dest_myfac into l_MTL_dest;
3565     close cur_MTL_dest_myfac;
3566   elsif (p_report_for = 2 OR p_report_for = 4) then
3567     open cur_MTL_dest_c_s (p_plan_id, p_report_for, p_report_for_id, p_destination_state);
3568     fetch cur_MTL_dest_c_s into l_MTL_dest;
3569     close cur_MTL_dest_c_s;
3570   elsif p_report_for = 3 then
3571     open cur_MTL_dest_carr (p_plan_id, p_report_for_id, p_destination_state);
3572     fetch cur_MTL_dest_carr into l_MTL_dest;
3573     close cur_MTL_dest_carr;
3574   end if;
3575 
3576    return l_MTL_dest;
3577 exception
3578 when others then
3579 	 return 0;
3580 end get_MTL_dest;
3581 
3582 
3583 function get_DTL_dest(p_plan_id           in number
3584                     , p_report_for        in number
3585                     , p_report_for_id     in number
3586 		    , p_destination_state    in varchar2)
3587 return number is
3588   l_DTL_dest number;
3589 
3590   cursor cur_DTL_dest (l_plan_id        in number
3591                      , l_destination_state in varchar2) is
3592   select count(*)
3593   from (select distinct mt.trip_id, count(*) num_stops
3594         from mst_trips mt
3595         , mst_trip_stops mts
3596 	where mt.plan_id = l_plan_id
3600 			   , mst_trip_stops mts1
3597 	and mt.trip_id in (select distinct mdl.trip_id
3598 	                   from mst_deliveries md
3599 			   , mst_delivery_legs mdl
3601 			   , wsh_locations wl
3602 			   where md.plan_id = mt.plan_id
3603 			   and mdl.plan_id = md.plan_id
3604 			   and mdl.delivery_id = md.delivery_id
3605                            and mts1.plan_id = mdl.plan_id
3606 			   and mts1.trip_id = mdl.trip_id
3607 			   and mts1.stop_id = mdl.drop_off_stop_id
3608 			   and mts1.stop_location_id = wl.wsh_location_id
3609 			   and mts1.stop_location_id = md.dropoff_location_id
3610 			   and wl.state = l_destination_state)
3611 	and mt.mode_of_transport = 'TRUCK'
3612 	and mts.plan_id = mt.plan_id
3613 	and mts.trip_id = mt.trip_id
3614 	group by mt.trip_id) temp
3615   where temp.num_stops = 2;
3616 
3617   cursor cur_DTL_dest_myfac (l_plan_id        in number
3618                            , l_myfac_id       in number
3619 		           , l_destination_state in varchar2) is
3620   select count(*)
3621   from (select distinct mt.trip_id, count(*) num_stops
3622         from mst_trips mt
3623         , mst_trip_stops mts
3624 	where mt.plan_id = l_plan_id
3625 	and mt.trip_id in (select distinct mdl.trip_id
3626 	                   from mst_deliveries md
3627 			   , mst_delivery_legs mdl
3628 			   , mst_trip_stops mts1
3629                            , fte_location_parameters flp
3630 			   , wsh_locations wl
3631 			   where md.plan_id = mt.plan_id
3632 			   and mdl.plan_id = md.plan_id
3633 			   and mdl.delivery_id = md.delivery_id
3634                            and mts1.plan_id = mdl.plan_id
3635 			   and mts1.trip_id = mdl.trip_id
3636 			   and mts1.stop_id = mdl.drop_off_stop_id
3637 			   and mts1.stop_location_id = wl.wsh_location_id
3638 			   and mts1.stop_location_id = md.dropoff_location_id
3639 			   and wl.state = l_destination_state
3640                            and flp.location_id = mts1.stop_location_id
3641                            and flp.facility_id = l_myfac_id)
3642 	and mt.mode_of_transport = 'TRUCK'
3643 	and mts.plan_id = mt.plan_id
3644 	and mts.trip_id = mt.trip_id
3645 	group by mt.trip_id) temp
3646   where temp.num_stops = 2;
3647 
3648   cursor cur_DTL_dest_c_s (l_plan_id          in number
3649                          , l_c_s_ident        in number
3650 		         , l_cust_supp_id     in number
3651 			 , l_destination_state   in varchar2) is
3652   select count(*)
3653   from (select distinct mt.trip_id, count(*) num_stops
3654         from mst_trips mt
3655         , mst_trip_stops mts
3656 	where mt.plan_id = l_plan_id
3657 	and mt.mode_of_transport = 'TRUCK'
3658 	and mts.plan_id = mt.plan_id
3659 	and mts.trip_id = mt.trip_id
3660 	and mt.trip_id in
3661 	          (select distinct mdl.trip_id
3662 	           from mst_deliveries md
3663 		   , mst_delivery_legs mdl
3667 		   and mdl.drop_off_stop_id = mts1.stop_id
3664 		   , mst_trip_stops mts1
3665 		   , wsh_locations wl
3666 		   where mdl.plan_id = mt.plan_id
3668 		   and mdl.trip_id = mts1.trip_id
3669 		   and mts1.plan_id = mt.plan_id
3670 		   and mts1.stop_location_id = wl.wsh_location_id
3671 		   and mts1.stop_location_id = md.dropoff_location_id
3672 		   and wl.state = l_destination_state
3673 		   and md.plan_id = mdl.plan_id
3674 		   and md.delivery_id = mdl.delivery_id
3675                    and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id)
3676 	group by mt.trip_id) temp
3677   where temp.num_stops = 2;
3678 
3679   cursor cur_DTL_dest_carr (l_plan_id        in number
3680                           , l_carrier_id in number
3681                           , l_destination_state in varchar2) is
3682   select count(*)
3683   from (select distinct mt.trip_id, count(*) num_stops
3684         from mst_trips mt
3685         , mst_trip_stops mts
3686 	where mt.plan_id = l_plan_id
3687 	and mt.carrier_id = l_carrier_id
3688 	and mt.trip_id in (select distinct mdl.trip_id
3689 	                   from mst_deliveries md
3690 			   , mst_delivery_legs mdl
3691 			   , mst_trip_stops mts1
3692 			   , wsh_locations wl
3693 			   where md.plan_id = mt.plan_id
3694 			   and mdl.plan_id = md.plan_id
3695 			   and mdl.delivery_id = md.delivery_id
3696                            and mts1.plan_id = mdl.plan_id
3697 			   and mts1.trip_id = mdl.trip_id
3698 			   and mts1.stop_id = mdl.drop_off_stop_id
3699 			   and mts1.stop_location_id = wl.wsh_location_id
3700 			   and mts1.stop_location_id = md.dropoff_location_id
3701 			   and wl.state = l_destination_state)
3702 	and mt.mode_of_transport = 'TRUCK'
3703 	and mts.plan_id = mt.plan_id
3704 	and mts.trip_id = mt.trip_id
3705 	group by mt.trip_id) temp
3706   where temp.num_stops = 2;
3707 
3708 begin
3709   if p_report_for = 0 then
3710     open cur_DTL_dest (p_plan_id, p_destination_state);
3711     fetch cur_DTL_dest into l_DTL_dest;
3712     close cur_DTL_dest;
3713   elsif p_report_for = 1 then
3714     open cur_DTL_dest_myfac (p_plan_id, p_report_for_id, p_destination_state);
3715     fetch cur_DTL_dest_myfac into l_DTL_dest;
3716     close cur_DTL_dest_myfac;
3717   elsif (p_report_for = 2 OR p_report_for = 4) then
3718     open cur_DTL_dest_c_s (p_plan_id, p_report_for, p_report_for_id, p_destination_state);
3719     fetch cur_DTL_dest_c_s into l_DTL_dest;
3720     close cur_DTL_dest_c_s;
3721   elsif p_report_for = 3 then
3722     open cur_DTL_dest_carr (p_plan_id, p_report_for_id, p_destination_state);
3723     fetch cur_DTL_dest_carr into l_DTL_dest;
3724     close cur_DTL_dest_carr;
3725   end if;
3726 
3727    return l_DTL_dest;
3728 exception
3729 when others then
3730 	 return 0;
3731 end get_DTL_dest;
3732 
3733 
3734 function get_LTL_dest(p_plan_id           in number
3735                     , p_report_for        in number
3736                     , p_report_for_id     in number
3737 		    , p_destination_state    in varchar2)
3738 return number is
3739   l_LTL_dest number;
3740 
3741   cursor cur_LTL_dest (l_plan_id        in number
3742                      , l_destination_state in varchar2) is
3743   select count(*)
3744   from mst_trips mt
3745   where mt.plan_id = l_plan_id
3746   and mt.mode_of_transport = 'LTL'
3747   and mt.trip_id in (select distinct mdl.trip_id
3748                      from mst_deliveries md
3749 		     , mst_delivery_legs mdl
3750                      , mst_trip_stops mts1
3751 		     , wsh_locations wl
3752 		     where md.plan_id = mt.plan_id
3753 		     and mdl.plan_id = md.plan_id
3754 		     and mdl.delivery_id = md.delivery_id
3755                      and mts1.plan_id = mdl.plan_id
3756 		     and mts1.trip_id = mdl.trip_id
3757 		     and mts1.stop_id = mdl.drop_off_stop_id
3758 		     and mts1.stop_location_id = wl.wsh_location_id
3759 		     and mts1.stop_location_id = md.dropoff_location_id
3760 		     and wl.state = l_destination_state);
3761 
3762   cursor cur_LTL_dest_myfac (l_plan_id        in number
3763                            , l_myfac_id       in number
3764 		           , l_destination_state in varchar2) is
3765   select count(*)
3766   from mst_trips mt
3767   where mt.plan_id = l_plan_id
3768   and mt.mode_of_transport = 'LTL'
3769   and mt.trip_id in (select distinct mdl.trip_id
3770                      from mst_deliveries md
3771 		     , mst_delivery_legs mdl
3772                      , mst_trip_stops mts1
3776 		     and mdl.plan_id = md.plan_id
3773 		     , fte_location_parameters flp
3774 		     , wsh_locations wl
3775 		     where md.plan_id = mt.plan_id
3777 		     and mdl.delivery_id = md.delivery_id
3778                      and mts1.plan_id = mdl.plan_id
3779 		     and mts1.trip_id = mdl.trip_id
3780 		     and mts1.stop_id = mdl.drop_off_stop_id
3781 		     and mts1.stop_location_id = wl.wsh_location_id
3782 		     and mts1.stop_location_id = md.dropoff_location_id
3783 		     and wl.state = l_destination_state
3784 		     and flp.location_id = mts1.stop_location_id
3785 		     and flp.facility_id = l_myfac_id);
3786 
3787   cursor cur_LTL_dest_c_s (l_plan_id          in number
3788                          , l_c_s_ident        in number
3789 		         , l_cust_supp_id     in number
3790 			 , l_destination_state   in varchar2) is
3791   select count(*)
3792   from mst_trips mt
3793   where mt.plan_id = l_plan_id
3794   and mt.mode_of_transport = 'LTL'
3795   and mt.trip_id in
3796 	          (select distinct mdl.trip_id
3797 	           from mst_deliveries md
3798 		   , mst_delivery_legs mdl
3799 		   , mst_trip_stops mts1
3800 		   , wsh_locations wl
3801 		   where mdl.plan_id = mt.plan_id
3802 		   and mdl.drop_off_stop_id = mts1.stop_id
3803 		   and mdl.trip_id = mts1.trip_id
3804 		   and mts1.plan_id = mt.plan_id
3805 		   and mts1.stop_location_id = wl.wsh_location_id
3806 		   and mts1.stop_location_id = md.dropoff_location_id
3807 		   and wl.state = l_destination_state
3808 		   and md.plan_id = mdl.plan_id
3809 		   and md.delivery_id = mdl.delivery_id
3810                    and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id);
3811 
3812   cursor cur_LTL_dest_carr (l_plan_id        in number
3813                           , l_carrier_id in number
3814                           , l_destination_state in varchar2) is
3815   select count(*)
3816   from mst_trips mt
3817   where mt.plan_id = l_plan_id
3818   and mt.mode_of_transport = 'LTL'
3819   and mt.carrier_id = l_carrier_id
3820   and mt.trip_id in (select distinct mdl.trip_id
3821                      from mst_deliveries md
3822 		     , mst_delivery_legs mdl
3823                      , mst_trip_stops mts1
3824 		     , wsh_locations wl
3825 		     where md.plan_id = mt.plan_id
3826 		     and mdl.plan_id = md.plan_id
3827 		     and mdl.delivery_id = md.delivery_id
3828                      and mts1.plan_id = mdl.plan_id
3829 		     and mts1.trip_id = mdl.trip_id
3830 		     and mts1.stop_id = mdl.drop_off_stop_id
3831 		     and mts1.stop_location_id = wl.wsh_location_id
3832 		     and mts1.stop_location_id = md.dropoff_location_id
3833 		     and wl.state = l_destination_state);
3834 begin
3835   if p_report_for = 0 then
3836     open cur_LTL_dest (p_plan_id, p_destination_state);
3837     fetch cur_LTL_dest into l_LTL_dest;
3838     close cur_LTL_dest;
3839   elsif p_report_for = 1 then
3840     open cur_LTL_dest_myfac (p_plan_id, p_report_for_id, p_destination_state);
3841     fetch cur_LTL_dest_myfac into l_LTL_dest;
3842     close cur_LTL_dest_myfac;
3843   elsif (p_report_for = 2 OR p_report_for = 4) then
3844     open cur_LTL_dest_c_s (p_plan_id, p_report_for, p_report_for_id, p_destination_state);
3845     fetch cur_LTL_dest_c_s into l_LTL_dest;
3846     close cur_LTL_dest_c_s;
3847   elsif p_report_for = 3 then
3848     open cur_LTL_dest_carr (p_plan_id, p_report_for_id, p_destination_state);
3849     fetch cur_LTL_dest_carr into l_LTL_dest;
3850     close cur_LTL_dest_carr;
3851   end if;
3852 
3853    return l_LTL_dest;
3854 exception
3855 when others then
3856 	 return 0;
3857 end get_LTL_dest;
3858 
3859 
3860 function get_PCL_dest(p_plan_id           in number
3861                     , p_report_for        in number
3862                     , p_report_for_id     in number
3863 		    , p_destination_state    in varchar2)
3864 return number is
3865   l_PCL_dest number;
3866 
3867   cursor cur_PCL_dest (l_plan_id        in number
3868                      , l_destination_state in varchar2) is
3869   select count(*)
3870   from mst_trips mt
3871   where mt.plan_id = l_plan_id
3872   and mt.mode_of_transport = 'PARCEL'
3873   and mt.trip_id in (select distinct mdl.trip_id
3874                      from mst_deliveries md
3875 		     , mst_delivery_legs mdl
3876                      , mst_trip_stops mts1
3877 		     , wsh_locations wl
3878 		     where md.plan_id = mt.plan_id
3879 		     and mdl.plan_id = md.plan_id
3880 		     and mdl.delivery_id = md.delivery_id
3881                      and mts1.plan_id = mdl.plan_id
3882 		     and mts1.trip_id = mdl.trip_id
3883 		     and mts1.stop_id = mdl.drop_off_stop_id
3884 		     and mts1.stop_location_id = wl.wsh_location_id
3885 		     and mts1.stop_location_id = md.dropoff_location_id
3886 		     and wl.state = l_destination_state);
3887 
3888   cursor cur_PCL_dest_myfac (l_plan_id        in number
3889                            , l_myfac_id       in number
3890 		           , l_destination_state in varchar2) is
3891   select count(*)
3892   from mst_trips mt
3893   where mt.plan_id = l_plan_id
3894   and mt.mode_of_transport = 'PARCEL'
3895   and mt.trip_id in (select distinct mdl.trip_id
3896                      from mst_deliveries md
3897 		     , mst_delivery_legs mdl
3898                      , mst_trip_stops mts1
3899 		     , fte_location_parameters flp
3900 		     , wsh_locations wl
3901 		     where md.plan_id = mt.plan_id
3902  	             and mdl.plan_id = md.plan_id
3903   		     and mdl.delivery_id = md.delivery_id
3904                      and mts1.plan_id = mdl.plan_id
3905 		     and mts1.trip_id = mdl.trip_id
3906 		     and mts1.stop_id = mdl.drop_off_stop_id
3907 		     and mts1.stop_location_id = wl.wsh_location_id
3908 		     and mts1.stop_location_id = md.dropoff_location_id
3909 		     and wl.state = l_destination_state
3913   cursor cur_PCL_dest_c_s (l_plan_id          in number
3910 		     and flp.location_id = mts1.stop_location_id
3911 		     and flp.facility_id = l_myfac_id);
3912 
3914                          , l_c_s_ident        in number
3915 		         , l_cust_supp_id     in number
3916 			 , l_destination_state   in varchar2) is
3917   select count(*)
3918   from mst_trips mt
3919   where mt.plan_id = l_plan_id
3920   and mt.mode_of_transport = 'PARCEL'
3921   and mt.trip_id in
3922 	          (select distinct mdl.trip_id
3923 	           from mst_deliveries md
3924 		   , mst_delivery_legs mdl
3925 		   , mst_trip_stops mts1
3926 		   , wsh_locations wl
3927 		   where mdl.plan_id = mt.plan_id
3928 		   and mdl.drop_off_stop_id = mts1.stop_id
3929 		   and mdl.trip_id = mts1.trip_id
3930 		   and mts1.plan_id = mt.plan_id
3931 		   and mts1.stop_location_id = wl.wsh_location_id
3932 		   and mts1.stop_location_id = md.dropoff_location_id
3933 		   and wl.state = l_destination_state
3934 		   and md.plan_id = mdl.plan_id
3935 		   and md.delivery_id = mdl.delivery_id
3936                    and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id);
3937 
3938   cursor cur_PCL_dest_carr (l_plan_id        in number
3939                           , l_carrier_id     in number
3940                           , l_destination_state in varchar2) is
3941   select count(*)
3942   from mst_trips mt
3943   where mt.plan_id = l_plan_id
3944   and mt.mode_of_transport = 'PARCEL'
3945   and mt.carrier_id = l_carrier_id
3946   and mt.trip_id in (select distinct mdl.trip_id
3947                      from mst_deliveries md
3948 		     , mst_delivery_legs mdl
3949                      , mst_trip_stops mts1
3950 		     , wsh_locations wl
3951 		     where md.plan_id = mt.plan_id
3952 		     and mdl.plan_id = md.plan_id
3953 		     and mdl.delivery_id = md.delivery_id
3954                      and mts1.plan_id = mdl.plan_id
3955 		     and mts1.trip_id = mdl.trip_id
3956 		     and mts1.stop_id = mdl.drop_off_stop_id
3957 		     and mts1.stop_location_id = wl.wsh_location_id
3958 		     and mts1.stop_location_id = md.dropoff_location_id
3959 		     and wl.state = l_destination_state);
3960 begin
3961   if p_report_for = 0 then
3962     open cur_PCL_dest (p_plan_id, p_destination_state);
3963     fetch cur_PCL_dest into l_PCL_dest;
3964     close cur_PCL_dest;
3965   elsif p_report_for = 1 then
3966     open cur_PCL_dest_myfac (p_plan_id, p_report_for_id, p_destination_state);
3967     fetch cur_PCL_dest_myfac into l_PCL_dest;
3968     close cur_PCL_dest_myfac;
3969   elsif (p_report_for = 2 OR p_report_for = 4) then
3970     open cur_PCL_dest_c_s (p_plan_id, p_report_for, p_report_for_id, p_destination_state);
3971     fetch cur_PCL_dest_c_s into l_PCL_dest;
3972     close cur_PCL_dest_c_s;
3973   elsif p_report_for = 3 then
3974     open cur_PCL_dest_carr (p_plan_id, p_report_for_id, p_destination_state);
3975     fetch cur_PCL_dest_carr into l_PCL_dest;
3976     close cur_PCL_dest_carr;
3977   end if;
3978 
3979    return l_PCL_dest;
3980 exception
3981 when others then
3982 	 return 0;
3983 end get_PCL_dest;
3984 
3985 
3986 function get_total_cost_mode_dest (p_plan_id            in number
3987                                  , p_report_for         in number
3988       		                 , p_report_for_id      in number
3989 		                 , p_mode               in varchar2
3990 				 , p_destination_state     in varchar2)
3991 return number is
3992   l_total_cost_per_mode number;
3993 
3994   cursor cur_total_cost (l_plan_id        in number
3995                        , l_mode           in varchar2
3996 		       , l_destination_state in varchar2) is
3997 /*
3998   select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
3999                + nvl(mdl.allocated_fac_unloading_cost,0)
4000 	       + nvl(mdl.allocated_transport_cost,0)
4001 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
4002                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
4003   from mst_delivery_legs mdl
4004   where mdl.plan_id = l_plan_id
4005   and mdl.delivery_id in
4006              ( select md.delivery_id
4007                from  mst_delivery_legs mdl1
4008                    , mst_deliveries md
4009                    , mst_trips mt
4010                    , mst_trip_stops mts
4011 		   , wsh_locations wl
4012                 where mt.plan_id = mdl1.plan_id
4013                 and   mt.trip_id  = mdl1.trip_id
4014                 and   mt.mode_of_transport = l_mode
4015                 and   mt.plan_id = mts.plan_id
4016                 and   mt.trip_id = mts.trip_id
4017 	        and   mts.stop_location_id = wl.wsh_location_id
4018 	        and   wl.state = l_destination_state
4019                 and   md.plan_id = mdl1.plan_id
4020                 and   md.delivery_id  = mdl1.delivery_id
4021                 and   md.plan_id = mdl.plan_id
4022                 and   md.dropoff_location_id = mts.stop_location_id);
4023 */
4024 --Bug_Fix for 3696518
4025   (select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
4026                + nvl(mdl.allocated_fac_unloading_cost,0)
4027 	       + nvl(mdl.allocated_transport_cost,0)
4028 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
4029                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
4030   from mst_deliveries md
4031   , mst_delivery_legs mdl
4032   , mst_trips mt
4033 --  , mst_trip_stops mts
4034   , wsh_locations wl
4035   where mt.plan_id = l_plan_id
4036   and mt.mode_of_transport = l_mode
4037   and mt.plan_id = mdl.plan_id
4038   and mt.trip_id = mdl.trip_id
4039   and mdl.plan_id = md.plan_id
4040   and mdl.delivery_id = md.delivery_id
4044   and mts.stop_location_id = wl.wsh_location_id
4041   and wl.state = l_destination_state
4042   and md.dropoff_location_id = wl.wsh_location_id );
4043 /*
4045   and wl.state = l_destination_state
4046   and mt.plan_id = mts.plan_id
4047   and mt.trip_id = mts.trip_id
4048   and md.dropoff_location_id = mts.stop_location_id);
4049 */
4050 
4051   cursor cur_total_cost_myfac (l_plan_id        in number
4052                              , l_myfac_id       in number
4053                              , l_mode           in varchar2
4054 			     , l_destination_state in varchar2) is
4055 /*
4056   select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
4057                + nvl(mdl.allocated_fac_unloading_cost,0)
4058 	       + nvl(mdl.allocated_transport_cost,0)
4059 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
4060                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
4061   from mst_delivery_legs mdl
4062   where mdl.plan_id = l_plan_id
4063   and mdl.delivery_id in
4064              ( select md.delivery_id
4065                from  mst_delivery_legs mdl1
4066                    , mst_deliveries md
4067                    , mst_trips mt
4068                    , mst_trip_stops mts
4069 		   , fte_location_parameters flp
4070 		   , wsh_locations wl
4071                 where mt.plan_id = mdl1.plan_id
4072                 and   mt.trip_id  = mdl1.trip_id
4073                 and   mt.mode_of_transport = l_mode
4074                 and   mt.plan_id = mts.plan_id
4075                 and   mt.trip_id = mts.trip_id
4076                 and   mts.stop_location_id = flp.location_id
4077                 and   flp.facility_id = l_myfac_id
4078                 and   md.plan_id = mdl1.plan_id
4079                 and   md.delivery_id  = mdl1.delivery_id
4080                 and   md.plan_id = mdl.plan_id
4081                 and   md.dropoff_location_id = flp.location_id
4082 		and   flp.location_id = wl.wsh_location_id
4083 		and   wl.state = l_destination_state);
4084 */
4085 /*
4086 --Bug_Fix for 3696518
4087   (select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
4088                + nvl(mdl.allocated_fac_unloading_cost,0)
4089 	       + nvl(mdl.allocated_transport_cost,0)
4090 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
4091                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
4092   from mst_deliveries md
4093   , mst_delivery_legs mdl
4094   , mst_trips mt
4095 --  , mst_trip_stops mts
4096   , wsh_locations wl
4097   , fte_location_parameters flp
4098   where mt.plan_id = l_plan_id
4099   and mt.mode_of_transport = l_mode
4100   and mt.plan_id = mdl.plan_id
4101   and mt.trip_id = mdl.trip_id
4102   and mdl.plan_id = md.plan_id
4103   and mdl.delivery_id = md.delivery_id
4104 --  and mts.stop_location_id = wl.wsh_location_id
4105   and wl.state = l_destination_state
4106   and md.dropoff_location_id = wsh_location_id
4107 --  and mt.plan_id = mts.plan_id
4108 --  and mt.trip_id = mts.trip_id
4109   and flp.facility_id = l_myfac_id
4110   and md.dropoff_location_id = flp.location_id );
4111 --  and md.dropoff_location_id = mts.stop_location_id );
4112 */
4113 --Bug_Fix for 3696518 - II
4114   select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
4115                + nvl(mdl.allocated_fac_unloading_cost,0)
4116 	       + nvl(mdl.allocated_transport_cost,0)
4117 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
4118                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
4119   from mst_delivery_legs mdl
4120   , mst_trips mt
4121   where mt.plan_id = l_plan_id
4122   and mt.mode_of_transport = l_mode
4123   and mt.plan_id = mdl.plan_id
4124   and mt.trip_id = mdl.trip_id
4125   and mdl.delivery_id in (select mdl.delivery_id
4126                          from mst_deliveries md
4127                          , mst_delivery_legs mdl
4128                          , mst_trip_stops mts
4129                          , fte_location_parameters flp
4130                          , wsh_locations wl
4131                          where mdl.plan_id = l_plan_id
4132                          and mdl.plan_id = mts.plan_id
4133                          and mdl.trip_id = mts.trip_id
4134                          and ( mdl.pick_up_stop_id = mts.stop_id
4135                                or mdl.drop_off_stop_id = mts.stop_id )
4136                          and mts.stop_location_id = flp.location_id
4137                          and flp.facility_id = l_myfac_id
4138                          and mdl.plan_id = md.plan_id
4139                          and mdl.delivery_id = md.delivery_id
4140                          and md.dropoff_location_id = wl.wsh_location_id
4141                          and wl.state = l_destination_state);
4142 
4143   cursor cur_total_cost_c_s (l_plan_id      in number
4144                            , l_c_s_ident         in number
4145                            , l_cust_supp_id      in number
4146 			   , l_mode              in varchar2
4147 			   , l_destination_state    in varchar2) is
4148 /*
4149   select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
4150                + nvl(mdl.allocated_fac_unloading_cost,0)
4151 	       + nvl(mdl.allocated_transport_cost,0)
4152 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
4153                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
4154   from mst_delivery_legs mdl
4155   where mdl.plan_id = l_plan_id
4156   and mdl.delivery_id in
4157              ( select md.delivery_id
4158                from mst_trips mt
4162 		, wsh_locations wl
4159                 , mst_trip_stops mts
4160                 , mst_delivery_legs mdl1
4161                 , mst_deliveries md
4163                 where mt.plan_id = mdl1.plan_id
4164                 and   mt.trip_id  = mdl1.trip_id
4165                 and   mt.mode_of_transport = l_mode
4166                 and   mt.plan_id = mts.plan_id
4167                 and   mt.trip_id = mts.trip_id
4168                 and   mts.stop_location_id = wl.wsh_location_id
4169 	        and   wl.state = l_destination_state
4170                 and   md.plan_id = mdl1.plan_id
4171                 and   md.delivery_id  = mdl1.delivery_id
4172                 and   md.plan_id = mdl.plan_id
4173                 and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
4174                 and   md.dropoff_location_id = mts.stop_location_id);
4175 */
4176 --Bug_Fix for 3696518
4177   (select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
4178                + nvl(mdl.allocated_fac_unloading_cost,0)
4179 	       + nvl(mdl.allocated_transport_cost,0)
4180 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
4181                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
4182   from mst_deliveries md
4183   , mst_delivery_legs mdl
4184   , mst_trips mt
4185 --  , mst_trip_stops mts
4186   , wsh_locations wl
4187   where mt.plan_id = l_plan_id
4188   and mt.mode_of_transport = l_mode
4189   and mt.plan_id = mdl.plan_id
4190   and mt.trip_id = mdl.trip_id
4191   and mdl.plan_id = md.plan_id
4192   and mdl.delivery_id = md.delivery_id
4193 --  and mts.stop_location_id = wl.wsh_location_id
4194   and wl.state = l_destination_state
4195   and md.dropoff_location_id = wl.wsh_location_id
4196 --  and mt.plan_id = mts.plan_id
4197 --  and mt.trip_id = mts.trip_id
4198   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id );
4199 --  and md.dropoff_location_id = mts.stop_location_id );
4200 
4201 
4202   cursor cur_total_cost_carr (l_plan_id        in number
4203                             , l_carrier_id     in number
4204                             , l_mode           in varchar2
4205 		            , l_destination_state in varchar2) is
4206 /*
4207   select nvl(sum(decode(mt.mode_of_transport
4208                        , 'TRUCK', (nvl(mt.total_basic_transport_cost,0)
4209                                  + nvl(mt.total_stop_cost,0)
4210 		                 + nvl(mt.total_load_unload_cost,0)
4211 			         + nvl(mt.total_layover_cost,0)
4212 			         + nvl(mt.total_accessorial_cost,0)
4213 			         + nvl(mt.total_handling_cost,0))
4214 		       , (nvl(mt.total_basic_transport_cost,0)
4215 		        + nvl(mt.total_accessorial_cost,0)))),0)
4216   from mst_trips mt
4217   where mt.plan_id = l_plan_id
4218   and mt.carrier_id = l_carrier_id
4219   and mt.mode_of_transport = l_mode
4220 --  and mt.continuous_move_id is null --check
4221   and   mt.trip_id in
4222                    (select distinct mts.trip_id
4223                     from mst_trip_stops mts
4224                     , mst_delivery_legs mdl
4225                     , mst_deliveries md
4226                     , wsh_locations wl
4227                     where md.plan_id = mt.plan_id
4228                     and mts.plan_id = md.plan_id
4229                     and mdl.plan_id = md.plan_id
4230                     and mdl.delivery_id = md.delivery_id
4231                     and mdl.trip_id = mts.trip_id
4232                     and mts.stop_id = mdl.drop_off_stop_id
4233                     and mts.stop_location_id = md.dropoff_location_id
4234 		    and wl.wsh_location_id = mts.stop_location_id
4235 		    and wl.state = l_destination_state);
4236 */
4237 	select  nvl ( sum(nvl(mdl.allocated_fac_loading_cost,0)
4238         	+ nvl(mdl.allocated_fac_unloading_cost,0)
4239                 + nvl(mdl.allocated_fac_shp_hand_cost,0)
4240 	        + nvl(mdl.allocated_fac_rec_hand_cost,0)
4241     	        + nvl(mdl.allocated_transport_cost,0)), 0 )
4242 	from  mst_deliveries md
4243 	, mst_delivery_legs mdl
4244 	, mst_trips mt
4245 	, wsh_locations wl
4246 	, mst_trip_stops mts
4247 	where md.plan_id = l_plan_id
4248 	and md.plan_id = mt.plan_id
4249 	and md.plan_id = mdl.plan_id
4250 	and md.plan_id = mts.plan_id
4251 	and md.delivery_id = mdl.delivery_id
4252 	and mt.trip_id = mdl.trip_id
4253 	and mt.carrier_id = l_carrier_id
4254 	and mt.mode_of_transport = l_mode
4255 	and mts.trip_id = mt.trip_id
4256 	and mts.stop_location_id = wl.wsh_location_id
4257 	and mdl.drop_off_stop_id = mts.stop_id
4258 	and wl.state = l_destination_state;
4259 begin
4260   if p_report_for = 0 then
4264   elsif p_report_for = 1 then
4261     open cur_total_cost (p_plan_id, p_mode, p_destination_state);
4262     fetch cur_total_cost into l_total_cost_per_mode;
4263     close cur_total_cost;
4265     open cur_total_cost_myfac (p_plan_id, p_report_for_id, p_mode, p_destination_state);
4266     fetch cur_total_cost_myfac into l_total_cost_per_mode;
4267     close cur_total_cost_myfac;
4268   elsif (p_report_for = 2 OR p_report_for = 4) then
4269     open cur_total_cost_c_s (p_plan_id, p_report_for, p_report_for_id, p_mode, p_destination_state);
4270     fetch cur_total_cost_c_s into l_total_cost_per_mode;
4271     close cur_total_cost_c_s;
4272   elsif p_report_for = 3 then
4273     open cur_total_cost_carr (p_plan_id, p_report_for_id, p_mode, p_destination_state);
4274     fetch cur_total_cost_carr into l_total_cost_per_mode;
4275     close cur_total_cost_carr;
4276   end if;
4277 
4278    return nvl ( l_total_cost_per_mode, 0 );
4279 exception
4280 when others then
4281 	 return 0;
4282 end get_total_cost_mode_dest;
4283 
4284 
4285 -- Myfac related functions
4286 
4287 function get_orders_myfac (p_plan_id       in number
4288                          , p_report_for    in number
4289 			 , p_report_for_id in number
4290 			 , p_myfac_id      in number)
4291 return number is
4292   l_orders number;
4293 
4294   cursor cur_plan_orders_myfac (l_plan_id  in number
4295                               , l_myfac_id in number) is
4296   select count(distinct mdd.source_header_number)
4297   from mst_delivery_details mdd
4298      , mst_deliveries md
4299      , mst_delivery_assignments mda
4300   where md.plan_id = mda.plan_id
4301   and md.delivery_id = mda.delivery_id
4302   and md.delivery_id in
4303         (select mdl.delivery_id
4304          from -- mst_trips t -- Removing the join with mst_trips
4305             mst_trip_stops ts
4306             , mst_delivery_legs mdl
4307             , fte_location_parameters flp
4308          where mdl.plan_id = md.plan_id
4309          and ts.plan_id  = mdl.plan_id
4310 --         and ts.stop_id  = mdl.pick_up_stop_id
4311 -- Bug_Fix for 3693945
4312          and ( ts.stop_id = mdl.pick_up_stop_id
4313              or ts.stop_id = mdl.drop_off_stop_id )
4314          and ts.stop_location_id = flp.location_id
4315 	 and flp.facility_id = l_myfac_id ) -- ending the subquery here, with the join with mst_trips removed
4316 --         and ts.plan_id  = t.plan_id
4317 --         and ts.trip_id  = t.trip_id)
4318   and   mda.plan_id = mdd.plan_id
4319   and   mda.delivery_detail_id = mdd.delivery_detail_id
4320   and   md.plan_id = l_plan_id
4321   and   mdd.container_flag = 2;
4322 --  and   mdd.split_from_delivery_detail_id is null;
4323 
4324 
4325   --considering both assigned and unassigned deliveries
4326   cursor cur_plan_orders_c_s (l_plan_id      in number
4327                             , l_myfac_id     in number
4328                             , l_c_s_ident    in number
4329                             , l_cust_supp_id in number) is
4330   select count(distinct dd.source_header_number)
4331   from (
4332         select mdd.source_header_number
4333         from mst_delivery_details mdd
4334            , mst_deliveries md
4335            , mst_delivery_assignments mda
4336         where md.plan_id = mda.plan_id
4337         and   md.delivery_id = mda.delivery_id
4338         and   mda.plan_id = mdd.plan_id
4339         and   mda.delivery_detail_id = mdd.delivery_detail_id
4340         and   md.plan_id = l_plan_id
4341         and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
4342         and   md.delivery_id in
4343                     (select mdl.delivery_id
4347                      where mdl.plan_id = md.plan_id
4344                      from  mst_delivery_legs mdl
4345                          , mst_trip_stops mts
4346 			 , fte_location_parameters flp
4348                      and   mdl.plan_id = mts.plan_id
4349                      and ( mdl.pick_up_stop_id = mts.stop_id
4350                           or mdl.drop_off_stop_id = mts.stop_id)
4351 		     and   flp.location_id = mts.stop_location_id
4352 		     and   flp.facility_id = l_myfac_id)
4353   union all
4354   select mdd.source_header_number
4355   from mst_delivery_details mdd
4356      , mst_deliveries md
4357      , mst_delivery_assignments mda
4358   where md.plan_id = mda.plan_id
4359   and   md.delivery_id = mda.delivery_id
4360   and   mda.plan_id = mdd.plan_id
4361   and   mda.delivery_detail_id = mdd.delivery_detail_id
4362   and   md.plan_id = l_plan_id
4363   and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
4364   and   not exists (select 1 from mst_delivery_legs mdl
4365                     where mdl.plan_id=md.plan_id
4366                     and   mdl.delivery_id = md.delivery_id)) dd;
4367 
4368   cursor cur_plan_orders_carr (l_plan_id  in number
4369                              , l_myfac_id in number
4370                              , l_carrier_id in number) is
4371   select count(distinct mdd.source_header_number)
4372   from mst_delivery_details mdd
4373      , mst_deliveries md
4374      , mst_delivery_assignments mda
4375   where md.plan_id = mda.plan_id
4376   and md.delivery_id = mda.delivery_id
4377   and md.delivery_id in
4378         (select mdl.delivery_id
4379          from mst_trips t
4380             , mst_trip_stops ts
4381             , mst_delivery_legs mdl
4382             , fte_location_parameters flp
4383          where mdl.plan_id = md.plan_id
4384          and ts.plan_id  = mdl.plan_id
4385 --         and ts.stop_id  = mdl.pick_up_stop_id
4386 -- Bug_Fix for 3693945
4387          and ( ts.stop_id = mdl.pick_up_stop_id
4388              or ts.stop_id = mdl.drop_off_stop_id )
4389          and ts.stop_location_id = flp.location_id
4390 	 and flp.facility_id = l_myfac_id
4391          and ts.plan_id  = t.plan_id
4392          and ts.trip_id  = t.trip_id
4393 	 and t.carrier_id = l_carrier_id)
4394   and   mda.plan_id = mdd.plan_id
4395   and   mda.delivery_detail_id = mdd.delivery_detail_id
4396   and   md.plan_id = l_plan_id
4397   and   mdd.container_flag = 2;
4398 
4399 begin
4400   if (p_report_for = 0 or p_report_for = 1) then
4401     open cur_plan_orders_myfac (p_plan_id, p_myfac_id);
4402     fetch cur_plan_orders_myfac into l_orders;
4403     close cur_plan_orders_myfac;
4404   elsif (p_report_for = 2 OR p_report_for = 4) then
4405     open cur_plan_orders_c_s (p_plan_id, p_myfac_id, p_report_for, p_report_for_id);
4406     fetch cur_plan_orders_c_s into l_orders;
4407     close cur_plan_orders_c_s;
4408   elsif p_report_for = 3 then
4409     open cur_plan_orders_carr (p_plan_id, p_myfac_id, p_report_for_id);
4410     fetch cur_plan_orders_carr into l_orders;
4411     close cur_plan_orders_carr;
4412   end if;
4413 
4414   return l_orders;
4415 exception
4416 when others then
4417 	 return 0;
4418 end get_orders_myfac;
4419 
4420 
4421 -- Weight KPI
4422 function get_weight_myfac (p_plan_id       in number
4423                          , p_report_for    in number
4424    	  	         , p_report_for_id in number
4425 		         , p_myfac_id      in number)
4426 return number is
4427   l_weight number;
4428 
4429   cursor cur_weight_myfac (l_plan_id  in number
4430                          , l_myfac_id in number) is
4431   select nvl(sum(nvl(md.gross_weight,0)),0)
4432   from mst_deliveries md
4433      , fte_location_parameters flp
4434   where md.plan_id = l_plan_id
4435   and (md.pickup_location_id = flp.location_id
4436       or md.dropoff_location_id = flp.location_id)
4437   and flp.facility_id = l_myfac_id;
4438 
4439 
4440   cursor cur_weight_c_s (l_plan_id      in number
4441                        , l_myfac_id     in number
4442                        , l_c_s_ident    in number
4443   		       , l_cust_supp_id in number) is
4444   select nvl(sum(nvl(md.gross_weight,0)),0)
4445   from mst_deliveries md
4446   , fte_location_parameters flp
4447   where md.plan_id = l_plan_id
4448   and (md.pickup_location_id = flp.location_id
4449       or md.dropoff_location_id = flp.location_id)
4450   and flp.facility_id = l_myfac_id
4451   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
4452 
4453   cursor cur_weight_carr (l_plan_id       in number
4454                         , l_myfac_id      in number
4455                         , l_carrier_id    in number) is
4456   select nvl(sum(nvl(md.gross_weight,0)),0)
4457   from mst_deliveries md
4458   , fte_location_parameters flp
4459   where md.plan_id = l_plan_id
4460   and (md.pickup_location_id = flp.location_id
4461       or md.dropoff_location_id = flp.location_id)
4462   and flp.facility_id = l_myfac_id
4463   and md.delivery_id in (select distinct mdl.delivery_id
4464                          from mst_delivery_legs mdl
4465 			 , mst_trips mt
4466 			 where mdl.plan_id = md.plan_id
4467 			 and mt.plan_id = mdl.plan_id
4468 			 and mt.trip_id = mdl.trip_id
4469 			 and mt.carrier_id = l_carrier_id);
4470 begin
4471   if (p_report_for = 0 or p_report_for =1) then
4472     open cur_weight_myfac (p_plan_id, p_myfac_id);
4473     fetch cur_weight_myfac into l_weight;
4474     close cur_weight_myfac;
4475   elsif (p_report_for = 2 OR p_report_for = 4) then
4476     open cur_weight_c_s (p_plan_id, p_myfac_id, p_report_for, p_report_for_id);
4477     fetch cur_weight_c_s into l_weight;
4478     close cur_weight_c_s;
4479   elsif p_report_for = 3 then
4480     open cur_weight_carr (p_plan_id, p_myfac_id, p_report_for_id);
4484 
4481     fetch cur_weight_carr into l_weight;
4482     close cur_weight_carr;
4483   end if;
4485 return l_weight;
4486 exception
4487 when others then
4488 	 return 0;
4489 end get_weight_myfac;
4490 
4491 
4492 -- Volums KPI
4493 function get_volume_myfac (p_plan_id       in number
4494                          , p_report_for    in number
4495 		         , p_report_for_id in number
4496 			 , p_myfac_id      in number)
4497 return number is
4498   l_volume number;
4499 
4500   cursor cur_volume_myfac (l_plan_id  in number
4501                          , l_myfac_id in number) is
4502   select nvl(sum(nvl(md.volume,0)),0)
4503   from mst_deliveries md
4504      , fte_location_parameters flp
4505   where md.plan_id = l_plan_id
4506   and (md.pickup_location_id = flp.location_id
4507       or md.dropoff_location_id = flp.location_id)
4508   and flp.facility_id = l_myfac_id;
4509 
4510 
4511   cursor cur_volume_c_s (l_plan_id      in number
4512                        , l_myfac_id     in number
4513                        , l_c_s_ident    in number
4514   		       , l_cust_supp_id in number) is
4515   select nvl(sum(nvl(md.volume,0)),0)
4516   from mst_deliveries md
4517   , fte_location_parameters flp
4518   where md.plan_id = l_plan_id
4519   and (md.pickup_location_id = flp.location_id
4520       or md.dropoff_location_id = flp.location_id)
4521   and flp.facility_id = l_myfac_id
4522   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
4523 
4524   cursor cur_volume_carr (l_plan_id       in number
4525                         , l_myfac_id      in number
4526                         , l_carrier_id    in number) is
4527   select nvl(sum(nvl(md.volume,0)),0)
4528   from mst_deliveries md
4529   , fte_location_parameters flp
4530   where md.plan_id = l_plan_id
4531   and (md.pickup_location_id = flp.location_id
4532       or md.dropoff_location_id = flp.location_id)
4533   and flp.facility_id = l_myfac_id
4534   and md.delivery_id in (select distinct mdl.delivery_id
4535                          from mst_delivery_legs mdl
4536 			 , mst_trips mt
4537 			 where mdl.plan_id = md.plan_id
4538 			 and mt.plan_id = mdl.plan_id
4539 			 and mt.trip_id = mdl.trip_id
4540 			 and mt.carrier_id = l_carrier_id);
4541 begin
4542   if (p_report_for = 0 OR p_report_for = 1) then
4543     open cur_volume_myfac (p_plan_id, p_myfac_id);
4544     fetch cur_volume_myfac into l_volume;
4545     close cur_volume_myfac;
4546   elsif (p_report_for = 2 OR p_report_for = 4) then
4547     open cur_volume_c_s (p_plan_id, p_myfac_id, p_report_for, p_report_for_id);
4548     fetch cur_volume_c_s into l_volume;
4549     close cur_volume_c_s;
4550   elsif p_report_for = 3 then
4551     open cur_volume_carr (p_plan_id, p_myfac_id, p_report_for_id);
4552     fetch cur_volume_carr into l_volume;
4553     close cur_volume_carr;
4554   end if;
4555 
4556 return l_volume;
4557 exception
4558 when others then
4559 	 return 0;
4560 end get_volume_myfac;
4561 
4562 
4563 -- Pieces KPI
4564 function get_pieces_myfac (p_plan_id       in number
4565                          , p_report_for    in number
4566 		         , p_report_for_id in number
4567 			 , p_myfac_id      in number)
4568 return number is
4569   l_pieces number;
4570 
4571   cursor cur_pieces_myfac (l_plan_id  in number
4572                          , l_myfac_id in number) is
4573   select nvl(sum(nvl(md.number_of_pieces,0)),0)
4574   from mst_deliveries md
4575      , fte_location_parameters flp
4576   where md.plan_id = l_plan_id
4577   and (md.pickup_location_id = flp.location_id
4578       or md.dropoff_location_id = flp.location_id)
4579   and flp.facility_id = l_myfac_id;
4580 
4581 
4582   cursor cur_pieces_c_s (l_plan_id      in number
4583                        , l_myfac_id     in number
4584                        , l_c_s_ident    in number
4585   		       , l_cust_supp_id in number) is
4586   select nvl(sum(nvl(md.number_of_pieces,0)),0)
4587   from mst_deliveries md
4588   , fte_location_parameters flp
4589   where md.plan_id = l_plan_id
4590   and (md.pickup_location_id = flp.location_id
4591       or md.dropoff_location_id = flp.location_id)
4592   and flp.facility_id = l_myfac_id
4593   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
4594 
4598   select nvl(sum(nvl(md.number_of_pieces,0)),0)
4595   cursor cur_pieces_carr (l_plan_id       in number
4596                         , l_myfac_id      in number
4597                         , l_carrier_id    in number) is
4599   from mst_deliveries md
4600   , fte_location_parameters flp
4601   where md.plan_id = l_plan_id
4602   and (md.pickup_location_id = flp.location_id
4603       or md.dropoff_location_id = flp.location_id)
4604   and flp.facility_id = l_myfac_id
4605   and md.delivery_id in (select distinct mdl.delivery_id
4606                          from mst_delivery_legs mdl
4607 			 , mst_trips mt
4608 			 where mdl.plan_id = md.plan_id
4609 			 and mt.plan_id = mdl.plan_id
4610 			 and mt.trip_id = mdl.trip_id
4611 			 and mt.carrier_id = l_carrier_id);
4612 begin
4613   if (p_report_for = 0 OR p_report_for = 1) then
4614     open cur_pieces_myfac (p_plan_id, p_myfac_id);
4615     fetch cur_pieces_myfac into l_pieces;
4616     close cur_pieces_myfac;
4617   elsif (p_report_for = 2 OR p_report_for = 4) then
4618     open cur_pieces_c_s (p_plan_id, p_myfac_id, p_report_for, p_report_for_id);
4619     fetch cur_pieces_c_s into l_pieces;
4620     close cur_pieces_c_s;
4621   elsif p_report_for = 3 then
4622     open cur_pieces_carr (p_plan_id, p_myfac_id, p_report_for_id);
4623     fetch cur_pieces_carr into l_pieces;
4624     close cur_pieces_carr;
4625   end if;
4626 
4627 return l_pieces;
4628 exception
4629 when others then
4630 	 return 0;
4631 end get_pieces_myfac;
4632 
4633 
4634 function get_trips_per_mode_myfac (p_plan_id       in number
4635                                  , p_report_for    in number
4636 		                 , p_report_for_id in number
4637 			         , p_mode          in varchar2
4638 			         , p_myfac_id      in number)
4639 return number is
4640   l_trips_per_mode number;
4641 
4642   cursor cur_trips_myfac (l_plan_id  in number
4643                         , l_myfac_id in number
4644 			, l_mode     in varchar2) is
4645   select count(*)
4646   from mst_trips mt
4647   , fte_location_parameters flp
4648   where mt.plan_id = l_plan_id
4649   and mt.mode_of_transport = l_mode
4650   and flp.location_id in (select mts.stop_location_id
4651                           from mst_trip_stops mts
4652 			  where mts.plan_id = mt.plan_id
4653 			  and mts.trip_id = mt.trip_id)
4654   and flp.facility_id = l_myfac_id;
4655 
4656   cursor cur_trips_c_s (l_plan_id      in number
4657                       , l_myfac_id     in number
4658                       , l_c_s_ident    in number
4659                       , l_cust_supp_id in number
4660 		      , l_mode         in varchar2) is
4661   select count(mt.trip_id)
4662   from mst_trips mt
4663   where mt.plan_id = l_plan_id
4664   and   mt.trip_id in
4665                    (select distinct mts.trip_id
4666                     from mst_trip_stops mts
4667                     , mst_delivery_legs mdl
4668                     , mst_deliveries md
4669 		    , fte_location_parameters flp
4670                     where md.plan_id = mt.plan_id
4671                     and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
4672                     and mts.plan_id = md.plan_id
4673                     and mts.stop_location_id = md.dropoff_location_id
4674 		    and flp.location_id = mts.stop_location_id
4675 		    and flp.facility_id = l_myfac_id
4676                     and mdl.plan_id = md.plan_id
4677                     and mdl.delivery_id = md.delivery_id
4678                     and mdl.trip_id = mts.trip_id
4679                     and mdl.drop_off_stop_id = mts.stop_id)
4680   and   mt.mode_of_transport = l_mode
4681   and   EXISTS
4682        (select ts.trip_id
4686         having count(ts.stop_id) >= 2
4683         from mst_trip_stops ts
4684         where ts.plan_id = mt.plan_id
4685         and   ts.trip_id = mt.trip_id
4687         group by ts.trip_id);
4688 
4689   cursor cur_trips_carr (l_plan_id  in number
4690                        , l_myfac_id in number
4691 		       , l_carrier_id in number
4692 		       , l_mode     in varchar2) is
4693   select count(*)
4694   from mst_trips mt
4695   , fte_location_parameters flp
4696   where mt.plan_id = l_plan_id
4697   and mt.mode_of_transport = l_mode
4698   and mt.carrier_id = l_carrier_id
4699   and flp.location_id in (select mts.stop_location_id
4700                           from mst_trip_stops mts
4701 			  where mts.plan_id = mt.plan_id
4702 			  and mts.trip_id = mt.trip_id)
4703   and flp.facility_id = l_myfac_id;
4704 begin
4705   if (p_report_for = 0 OR p_report_for = 1) then
4706     open cur_trips_myfac (p_plan_id, p_myfac_id, p_mode);
4707     fetch cur_trips_myfac into l_trips_per_mode;
4708     close cur_trips_myfac;
4709   elsif (p_report_for = 2 OR p_report_for = 4) then
4710     open cur_trips_c_s (p_plan_id, p_myfac_id, p_report_for, p_report_for_id, p_mode);
4711     fetch cur_trips_c_s into l_trips_per_mode;
4712     close cur_trips_c_s;
4713   elsif p_report_for = 3 then
4714     open cur_trips_carr (p_plan_id, p_myfac_id, p_report_for_id, p_mode);
4715     fetch cur_trips_carr into l_trips_per_mode;
4716     close cur_trips_carr;
4717   end if;
4718 
4719    return l_trips_per_mode;
4720 exception
4721 when others then
4722 	 return 0;
4723 end get_trips_per_mode_myfac;
4724 
4725 
4726 function get_cost_per_mode_myfac (p_plan_id         in number
4727                                   , p_report_for    in number
4728          	                  , p_report_for_id in number
4729 		                  , p_mode          in varchar2
4730 				  , p_myfac_id      in number)
4731 return number is
4732   l_total_cost_per_mode number;
4733   l_total_cost_per_mode_cd number;
4734   l_total_cost number;
4735   l_loc_id number;
4736 
4737   cursor cur_total_cost_myfac (l_plan_id  in number
4738                              , l_myfac_id in number
4739                              , l_mode     in varchar2) is
4740 /*
4741   select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
4742                + nvl(mdl.allocated_fac_unloading_cost,0)
4743 	       + nvl(mdl.allocated_transport_cost,0)
4744 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
4745                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
4746   from mst_delivery_legs mdl
4747   where mdl.plan_id = l_plan_id
4748   and mdl.delivery_id in
4749              ( select md.delivery_id
4750                from  mst_delivery_legs mdl1
4751                    , mst_deliveries md
4752                    , mst_trips mt
4753                    , mst_trip_stops mts
4754 		   , fte_location_parameters flp
4755                 where mt.plan_id = mdl1.plan_id
4756                 and   mt.trip_id  = mdl1.trip_id
4757                 and   mt.mode_of_transport = l_mode
4758                 and   mt.plan_id = mts.plan_id
4759                 and   mt.trip_id = mts.trip_id
4760                 and   mts.stop_location_id = flp.location_id
4761                 and   flp.facility_id = l_myfac_id
4762                 and   md.plan_id = mdl1.plan_id
4763                 and   md.delivery_id  = mdl1.delivery_id
4764                 and   md.plan_id = mdl.plan_id
4765                 and   (md.pickup_location_id = flp.location_id
4766                        or md.dropoff_location_id = flp.location_id));
4767 */
4768 --Bug_Fix for 3696518
4772 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
4769   (select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
4770                + nvl(mdl.allocated_fac_unloading_cost,0)
4771 	       + nvl(mdl.allocated_transport_cost,0)
4773                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
4774   from mst_deliveries md
4775   , mst_delivery_legs mdl
4776   , mst_trips mt
4777   , fte_location_parameters flp
4778   where mt.plan_id = l_plan_id
4779   and mt.mode_of_transport = l_mode
4780   and mt.plan_id = mdl.plan_id
4781   and mt.trip_id = mdl.trip_id
4782   and mdl.plan_id = md.plan_id
4783   and mdl.delivery_id = md.delivery_id
4784   and flp.facility_id = l_myfac_id
4785   and ( md.pickup_location_id = flp.location_id
4786         or md.dropoff_location_id = flp.location_id ) );
4787 
4788   cursor cur_total_cost_c_s (l_plan_id      in number
4789                            , l_myfac_id     in number
4790                            , l_c_s_ident    in number
4791                            , l_cust_supp_id in number
4792 			   , l_mode         in varchar2) is
4793 /*
4794   select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
4795                + nvl(mdl.allocated_fac_unloading_cost,0)
4796 	       + nvl(mdl.allocated_transport_cost,0)
4797 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
4798                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
4799   from mst_delivery_legs mdl
4800   where mdl.plan_id = l_plan_id
4801   and mdl.delivery_id in
4802              ( select md.delivery_id
4803                from  mst_delivery_legs mdl1
4804                    , mst_deliveries md
4805                    , mst_trips mt
4806                    , mst_trip_stops mts
4807 		   , fte_location_parameters flp
4808                 where mt.plan_id = mdl1.plan_id
4809                 and   mt.trip_id  = mdl1.trip_id
4810                 and   mt.mode_of_transport = l_mode
4811                 and   mt.plan_id = mts.plan_id
4812                 and   mt.trip_id = mts.trip_id
4813                 and   mts.stop_location_id = flp.location_id
4814 		and   flp.facility_id = l_myfac_id
4815                 and   md.plan_id = mdl1.plan_id
4816                 and   md.delivery_id  = mdl1.delivery_id
4817                 and   md.plan_id = mdl.plan_id
4818                 and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
4819                 and   (md.pickup_location_id = flp.location_id
4820 		       or md.dropoff_location_id = flp.location_id));
4821 */
4822 --Bug_Fix for 3696518
4823   (select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
4824                + nvl(mdl.allocated_fac_unloading_cost,0)
4825 	       + nvl(mdl.allocated_transport_cost,0)
4826 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
4827                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
4828   from mst_deliveries md
4829   , mst_delivery_legs mdl
4830   , mst_trips mt
4831   , fte_location_parameters flp
4832   where mt.plan_id = l_plan_id
4833   and mt.mode_of_transport = l_mode
4834   and mt.plan_id = mdl.plan_id
4835   and mt.trip_id = mdl.trip_id
4836   and mdl.plan_id = md.plan_id
4837   and mdl.delivery_id = md.delivery_id
4838   and flp.facility_id = l_myfac_id
4839   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
4840   and ( md.pickup_location_id = flp.location_id
4841         or md.dropoff_location_id = flp.location_id ) );
4842 
4843 /*  cursor cur_total_cost_carr (l_plan_id      in number
4844                             , l_myfac_id     in number
4845                             , l_carrier_id   in number
4846 			    , l_mode         in varchar2) is
4847   select nvl(sum(decode(mt.mode_of_transport
4848                        , 'TRUCK', (nvl(mt.total_basic_transport_cost,0)
4849                                  + nvl(mt.total_stop_cost,0)
4850 		                 + nvl(mt.total_load_unload_cost,0)
4851 			         + nvl(mt.total_layover_cost,0)
4852 			         + nvl(mt.total_accessorial_cost,0)
4853 			         + nvl(mt.total_handling_cost,0))
4854 		       , (nvl(mt.total_basic_transport_cost,0)
4855 		        + nvl(mt.total_accessorial_cost,0)))),0)
4856   from mst_trips mt
4857   where mt.plan_id = l_plan_id
4858   and mt.carrier_id = l_carrier_id
4859   and mt.mode_of_transport = l_mode
4860 --  and mt.continuous_move_id is null  --check
4861   and mt.trip_id in
4862                    (select distinct mts.trip_id
4863                     from mst_trip_stops mts
4864                     , mst_delivery_legs mdl
4865                     , mst_deliveries md
4866                     , fte_location_parameters flp
4867                     where md.plan_id = mt.plan_id
4868                     and mts.plan_id = md.plan_id
4869                     and mdl.plan_id = md.plan_id
4870                     and mdl.delivery_id = md.delivery_id
4871                     and mdl.trip_id = mts.trip_id
4872                     and mts.stop_location_id = flp.location_id
4873 		    and (md.pickup_location_id = flp.location_id
4874 		        or md.dropoff_location_id = flp.location_id)
4875 		    and flp.facility_id = l_myfac_id);
4876 */
4877 cursor cur_total_cost_carr (l_plan_id      in number
4878                             , l_location_id     in number
4879                             , l_carrier_id   in number
4880 			    , l_mode         in varchar2) is
4881 /*
4882  select sum(nvl(mdl.allocated_fac_loading_cost,0)  +
4883             nvl(mdl.allocated_transport_cost,0)    +
4884             nvl(mdl.allocated_fac_shp_hand_cost,0)  )
4885  from mst_delivery_legs mdl
4886  where mdl.plan_id = l_plan_id
4887  and mdl.delivery_id in
4888              ( select md.delivery_id
4889                from  mst_delivery_legs mdl1,
4890                      mst_deliveries md,
4891                      mst_trips mt,
4892                      mst_trip_stops mts
4893                 where mt.plan_id = mdl1.plan_id
4894                 and   mt.trip_id  = mdl1.trip_id
4898                 and   mt.carrier_id = l_carrier_id
4895                 and   mt.mode_of_transport = l_mode
4896                 and   mt.plan_id = mts.plan_id
4897                 and   mt.trip_id = mts.trip_id
4899                 and   mts.stop_location_id = l_location_id
4900                 and   md.plan_id = mdl1.plan_id
4901                 and   md.delivery_id  = mdl1.delivery_id
4902                 and   md.plan_id = mdl.plan_id
4903                 and  (  md.pickup_location_id = mts.stop_location_id
4904                      or md.dropoff_location_id = mts.stop_location_id));
4905 */
4906 --Bug_Fix for 3696518
4907   (select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
4908                + nvl(mdl.allocated_fac_unloading_cost,0)
4909 	       + nvl(mdl.allocated_transport_cost,0)
4910 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
4911                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
4912   from mst_deliveries md
4913   , mst_delivery_legs mdl
4914   , mst_trips mt
4915   , mst_trip_stops mts
4916   where mt.plan_id = l_plan_id
4917   and mt.mode_of_transport = l_mode
4918   and mt.plan_id = mdl.plan_id
4919   and mt.trip_id = mdl.trip_id
4920   and mt.carrier_id = l_carrier_id
4921   and mts.stop_location_id = l_location_id
4922   and mdl.plan_id = md.plan_id
4923   and mdl.delivery_id = md.delivery_id
4924   and mt.plan_id = mts.plan_id
4925   and mt.trip_id = mts.trip_id
4926   and ( md.pickup_location_id = mts.stop_location_id
4927         or md.dropoff_location_id = mts.stop_location_id ) );
4928 
4929 -- for cross dock cost calculation
4930   cursor cur_total_cost_carr_cd (l_plan_id      in number
4931                             , l_location_id     in number
4932                             , l_carrier_id   in number
4933 			    , l_mode         in varchar2) is
4934 /*
4935   select sum(nvl(mdl.allocated_fac_loading_cost,0)  +
4936              nvl(mdl.allocated_transport_cost,0)    +
4937              nvl(mdl.allocated_fac_shp_hand_cost,0)  )
4938   from mst_delivery_legs mdl
4939   where mdl.plan_id = l_plan_id
4940   and mdl.delivery_id in
4941              ( select md.delivery_id
4942                from  mst_delivery_legs mdl1,
4943                      mst_deliveries md,
4944                      mst_trips mt,
4945                      mst_trip_stops mts
4946                 where mt.plan_id = mdl1.plan_id
4947                 and   mt.trip_id  = mdl1.trip_id
4948                 and   mt.mode_of_transport = l_mode
4949                 and   mt.plan_id = mts.plan_id
4950                 and   mt.trip_id = mts.trip_id
4951 		and   mt.carrier_id = l_carrier_id
4952                 and   mts.stop_location_id = l_location_id
4953                 and   (mts.stop_id = mdl.pick_up_stop_id
4954                       or mts.stop_id = mdl.drop_off_stop_id)
4955                 and   md.plan_id = mdl1.plan_id
4956                 and   md.delivery_id  = mdl1.delivery_id
4957                 and   md.plan_id = mdl.plan_id
4958                 and   md.pickup_location_id <> mts.stop_location_id
4959                 and   md.dropoff_location_id <> mts.stop_location_id);
4960 */
4961 --Bug_Fix for 3696518
4962   (select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
4963                + nvl(mdl.allocated_fac_unloading_cost,0)
4964 	       + nvl(mdl.allocated_transport_cost,0)
4965 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
4966                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
4967   from mst_deliveries md
4968   , mst_delivery_legs mdl
4969   , mst_trips mt
4970   , mst_trip_stops mts
4971   where mt.plan_id = l_plan_id
4972   and mt.mode_of_transport = l_mode
4973   and mt.plan_id = mdl.plan_id
4974   and mt.trip_id = mdl.trip_id
4975   and mt.carrier_id = l_carrier_id
4976   and mts.stop_location_id = l_location_id
4977   and mdl.plan_id = md.plan_id
4978   and mdl.delivery_id = md.delivery_id
4979   and mt.plan_id = mts.plan_id
4980   and mt.trip_id = mts.trip_id
4981   and ( mts.stop_id = mdl.pick_up_stop_id
4982         or mts.stop_id = mdl.drop_off_stop_id )
4983   and md.pickup_location_id <> mts.stop_location_id
4984   and md.dropoff_location_id <> mts.stop_location_id );
4985 
4986 begin
4987   if (p_report_for = 0 OR p_report_for = 1) then
4988     open cur_total_cost_myfac (p_plan_id, p_myfac_id, p_mode);
4989     fetch cur_total_cost_myfac into l_total_cost_per_mode;
4990     close cur_total_cost_myfac;
4991   elsif (p_report_for = 2 OR p_report_for = 4) then
4992     open cur_total_cost_c_s (p_plan_id, p_myfac_id, p_report_for, p_report_for_id, p_mode);
4993     fetch cur_total_cost_c_s into l_total_cost_per_mode;
4994     close cur_total_cost_c_s;
4995   elsif p_report_for = 3 then
4996     --get the facility location
4997     select flp.location_id
4998     into l_loc_id
4999     from fte_location_parameters flp
5000     where flp.facility_id = p_myfac_id;
5001     open cur_total_cost_carr (p_plan_id, l_loc_id, p_report_for_id, p_mode);
5002     fetch cur_total_cost_carr into l_total_cost;
5003     close cur_total_cost_carr;
5004     open cur_total_cost_carr_cd (p_plan_id, l_loc_id, p_report_for_id, p_mode);
5005     fetch cur_total_cost_carr_cd into l_total_cost_per_mode_cd;
5006     close cur_total_cost_carr_cd;
5007     l_total_cost_per_mode := nvl(l_total_cost,0) + nvl(l_total_cost_per_mode_cd, 0);
5008   end if;
5009 
5010    return l_total_cost_per_mode;
5011 exception
5012 when others then
5013 	 return 0;
5014 end get_cost_per_mode_myfac;
5015 
5016 
5017 -- customer/supplier related functions
5018 function get_orders_c_s (p_plan_id       in number
5019                        , p_report_for    in number
5020 		       , p_report_for_id in number
5021 		       , p_c_s_ident     in number
5022 		       , p_cust_supp_id  in number)
5023 return number is
5024   l_plan_order_count number;
5025 
5029                               , l_myfac_id     in number) is
5026   cursor cur_plan_orders_myfac (l_plan_id      in number
5027                               , l_c_s_ident    in number
5028 			      , l_cust_supp_id in number
5030   select count(distinct mdd.source_header_number)
5031   from mst_delivery_details mdd
5032      , mst_deliveries md
5033      , mst_delivery_assignments mda
5034   where md.plan_id = mda.plan_id
5035   and md.delivery_id = mda.delivery_id
5036   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5037   and md.delivery_id in
5038         (select mdl.delivery_id
5039          from -- mst_trips t -- removing this join
5040               mst_trip_stops ts
5041             , mst_delivery_legs mdl
5042             , fte_location_parameters flp
5043          where mdl.plan_id = md.plan_id
5044          and ts.plan_id  = mdl.plan_id
5045 --         and ts.stop_id  = mdl.pick_up_stop_id
5046 -- Bug_Fix for 3694008
5047          and ( ts.stop_id = mdl.pick_up_stop_id
5048              or ts.stop_id = mdl.drop_off_stop_id )
5049          and ts.stop_location_id = flp.location_id
5050 	 and flp.facility_id = l_myfac_id ) -- end of subquery, as the join with t has been removed
5051          -- and ts.plan_id  = t.plan_id
5052          -- and ts.trip_id  = t.trip_id)
5053   and   mda.plan_id = mdd.plan_id
5054   and   mda.delivery_detail_id = mdd.delivery_detail_id
5055   and   md.plan_id = l_plan_id
5056   and   mdd.container_flag = 2
5057   and   mdd.split_from_delivery_detail_id is null;
5058 
5059 
5060   --considering both assigned and unassigned deliveries
5061   cursor cur_plan_orders_c_s (l_plan_id       in number
5062   			    , l_report_for    in number
5063   			    , l_report_for_id in number
5064                             , l_c_s_ident     in number
5065                             , l_cust_supp_id  in number) is
5066   select count(distinct dd.source_header_number)
5067   from (
5068         select mdd.source_header_number
5069         from mst_delivery_details mdd
5070            , mst_deliveries md
5071            , mst_delivery_assignments mda
5072         where md.plan_id = mda.plan_id
5073         and   md.delivery_id = mda.delivery_id
5074         and   mda.plan_id = mdd.plan_id
5075         and   mda.delivery_detail_id = mdd.delivery_detail_id
5076         and   md.plan_id = l_plan_id
5077         and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5078 	and decode ( l_report_for, 2, md.customer_id, 4, md.supplier_id, 0 ) = decode ( l_report_for, 2, l_report_for_id, 4, l_report_for_id, 0 )
5079         and   md.delivery_id in
5080                     (select mdl.delivery_id
5081                      from  mst_delivery_legs mdl
5082                          , mst_trip_stops mts
5083                      where mdl.plan_id = md.plan_id
5084                      and   mdl.plan_id = mts.plan_id
5085                      and ( mdl.pick_up_stop_id = mts.stop_id
5086                           or mdl.drop_off_stop_id = mts.stop_id))
5087   union all
5088   select mdd.source_header_number
5089   from mst_delivery_details mdd
5090      , mst_deliveries md
5091      , mst_delivery_assignments mda
5092   where md.plan_id = mda.plan_id
5093   and   md.delivery_id = mda.delivery_id
5094   and   mda.plan_id = mdd.plan_id
5095   and   mda.delivery_detail_id = mdd.delivery_detail_id
5096   and   md.plan_id = l_plan_id
5097   and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5098   and decode ( l_report_for, 2, md.customer_id, 4, md.supplier_id, 0 ) = decode ( l_report_for, 2, l_report_for_id, 4, l_report_for_id, 0 )
5099   and   not exists (select 1 from mst_delivery_legs mdl
5100                     where mdl.plan_id=md.plan_id
5101                     and   mdl.delivery_id = md.delivery_id)) dd;
5102 
5103   cursor cur_plan_orders_carr (l_plan_id      in number
5104                              , l_c_s_ident    in number
5105                              , l_cust_supp_id in number
5106 			     , l_carrier_id   in number) is
5107   select count(distinct dd.source_header_number)
5108   from (
5109         select mdd.source_header_number source_header_number
5110         from mst_delivery_details mdd
5111            , mst_deliveries md
5112            , mst_delivery_assignments mda
5113         where md.plan_id = mda.plan_id
5114         and   md.delivery_id = mda.delivery_id
5115         and   mda.plan_id = mdd.plan_id
5116         and   mda.delivery_detail_id = mdd.delivery_detail_id
5117         and   md.plan_id = l_plan_id
5118         and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5119         and   md.delivery_id in
5120                     (select mdl.delivery_id
5121                      from  mst_delivery_legs mdl
5122                          , mst_trip_stops mts
5123 			 , mst_trips mt
5124                      where mdl.plan_id = md.plan_id
5125                      and   mdl.plan_id = mts.plan_id
5126                      and ( mdl.pick_up_stop_id = mts.stop_id
5127                           or mdl.drop_off_stop_id = mts.stop_id)
5128 		     and   mdl.trip_id = mts.trip_id
5129 		     and   mt.plan_id = mts.plan_id
5130 		     and   mt.trip_id = mts.trip_id
5131 		     and   mt.carrier_id = l_carrier_id)) dd;
5132 begin
5133   if p_report_for = 1 then
5134     open cur_plan_orders_myfac (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id);
5135     fetch cur_plan_orders_myfac into l_plan_order_count;
5136     close cur_plan_orders_myfac;
5137   elsif (p_report_for = 0 OR p_report_for = 2 OR p_report_for = 4) then
5138     --open cur_plan_orders_c_s (p_plan_id, p_c_s_ident, p_cust_supp_id);
5139     open cur_plan_orders_c_s ( p_plan_id, p_report_for, p_report_for_id, p_c_s_ident, p_cust_supp_id );
5140     fetch cur_plan_orders_c_s into l_plan_order_count;
5141     close cur_plan_orders_c_s;
5142   elsif p_report_for = 3 then
5143     open cur_plan_orders_carr (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id);
5144     fetch cur_plan_orders_carr into l_plan_order_count;
5145     close cur_plan_orders_carr;
5146   end if;
5147 
5148   return l_plan_order_count;
5149 exception
5150 when others then
5151 	 return 0;
5152 end get_orders_c_s;
5153 
5154 
5155 -- Weight KPI
5156 function get_weight_c_s (p_plan_id       in number
5157                        , p_report_for    in number
5158    		       , p_report_for_id in number
5159 		       , p_c_s_ident     in number
5160 		       , p_cust_supp_id  in number)
5161 return number is
5162   l_weight number;
5163 
5164   cursor cur_weight_myfac (l_plan_id      in number
5165                          , l_c_s_ident    in number
5166 			 , l_cust_supp_id in number
5167                          , l_myfac_id     in number) is
5168   select nvl(sum(nvl(md.gross_weight,0)),0)
5169   from mst_deliveries md
5170      , fte_location_parameters flp
5171   where md.plan_id = l_plan_id
5172   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5173   and (md.pickup_location_id = flp.location_id
5174       or md.dropoff_location_id = flp.location_id)
5175   and flp.facility_id = l_myfac_id;
5176 
5177 
5178   cursor cur_weight_c_s (l_plan_id       in number
5179   		       , l_report_for	 in number
5180   		       , l_report_for_id in number
5181                        , l_c_s_ident     in number
5182   		       , l_cust_supp_id  in number) is
5183   select nvl(sum(nvl(md.gross_weight,0)),0)
5184   from mst_deliveries md
5185   where md.plan_id = l_plan_id
5186   and decode ( l_report_for, 2, md.customer_id, 4, md.supplier_id, 0 ) = decode ( l_report_for, 2, l_report_for_id, 4, l_report_for_id, 0 )
5187   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
5188 
5189   cursor cur_weight_carr (l_plan_id      in number
5193   select nvl(sum(nvl(md.gross_weight,0)),0)
5190                         , l_c_s_ident    in number
5191   		        , l_cust_supp_id in number
5192 			, l_carrier_id   in number) is
5194   from mst_deliveries md
5195   where md.plan_id = l_plan_id
5196   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5197   and md.delivery_id in (select distinct mdl.delivery_id
5198                          from mst_delivery_legs mdl
5199 			 , mst_trips mt
5200 			 where mdl.plan_id = md.plan_id
5201 			 and mt.plan_id = mdl.plan_id
5202 			 and mt.trip_id = mdl.trip_id
5203 			 and mt.carrier_id = l_carrier_id);
5204 begin
5205   if p_report_for = 1 then
5206     open cur_weight_myfac (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id);
5207     fetch cur_weight_myfac into l_weight;
5208     close cur_weight_myfac;
5209   elsif (p_report_for = 0 OR p_report_for = 2 OR p_report_for = 4) then
5210     --open cur_weight_c_s (p_plan_id, p_c_s_ident, p_cust_supp_id);
5211     open cur_weight_c_s ( p_plan_id, p_report_for, p_report_for_id, p_c_s_ident, p_cust_supp_id );
5212     fetch cur_weight_c_s into l_weight;
5213     close cur_weight_c_s;
5214   elsif p_report_for = 3 then
5215     open cur_weight_carr (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id);
5216     fetch cur_weight_carr into l_weight;
5217     close cur_weight_carr;
5218   end if;
5219 
5220 return l_weight;
5221 exception
5222 when others then
5223 	 return 0;
5224 end get_weight_c_s;
5225 
5226 
5227 -- Volums KPI
5228 function get_volume_c_s (p_plan_id       in number
5229                        , p_report_for    in number
5230 		       , p_report_for_id in number
5231 		       , p_c_s_ident     in number
5232 		       , p_cust_supp_id  in number)
5233 return number is
5234   l_volume number;
5235 
5236   cursor cur_volume_myfac (l_plan_id  in number
5237                          , l_c_s_ident in number
5238 			 , l_cust_supp_id in number
5239                          , l_myfac_id in number) is
5240   select nvl(sum(nvl(md.volume,0)),0)
5241   from mst_deliveries md
5242      , fte_location_parameters flp
5243   where md.plan_id = l_plan_id
5244   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5245   and (md.pickup_location_id = flp.location_id
5246       or md.dropoff_location_id = flp.location_id)
5247   and flp.facility_id = l_myfac_id;
5248 
5249 
5250   cursor cur_volume_c_s (l_plan_id       in number
5251                        , l_report_for    in number
5252                        , l_report_for_id in number
5253                        , l_c_s_ident     in number
5254   		       , l_cust_supp_id  in number) is
5255   select nvl(sum(nvl(md.volume,0)),0)
5256   from mst_deliveries md
5257   where md.plan_id = l_plan_id
5258   and decode ( l_report_for, 2, md.customer_id, 4, md.supplier_id, 0 ) = decode ( l_report_for, 2, l_report_for_id, 4, l_report_for_id, 0 )
5259   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
5260 
5261   cursor cur_volume_carr (l_plan_id      in number
5262                         , l_c_s_ident    in number
5263   		        , l_cust_supp_id in number
5264 			, l_carrier_id   in number) is
5265   select nvl(sum(nvl(md.volume,0)),0)
5266   from mst_deliveries md
5267   where md.plan_id = l_plan_id
5268   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5269   and md.delivery_id in (select distinct mdl.delivery_id
5270                          from mst_delivery_legs mdl
5271 			 , mst_trips mt
5272 			 where mdl.plan_id = md.plan_id
5273 			 and mt.plan_id = mdl.plan_id
5274 			 and mt.trip_id = mdl.trip_id
5275 			 and mt.carrier_id = l_carrier_id);
5276 begin
5277   if p_report_for = 1 then
5278     open cur_volume_myfac (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id);
5279     fetch cur_volume_myfac into l_volume;
5280     close cur_volume_myfac;
5281   elsif (p_report_for = 0 OR p_report_for = 2 OR p_report_for = 4) then
5282     open cur_volume_c_s (p_plan_id, p_report_for, p_report_for_id, p_c_s_ident, p_cust_supp_id);
5283     fetch cur_volume_c_s into l_volume;
5284     close cur_volume_c_s;
5285   elsif p_report_for = 3 then
5286     open cur_volume_carr (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id);
5287     fetch cur_volume_carr into l_volume;
5288     close cur_volume_carr;
5289   end if;
5290 
5291 return l_volume;
5292 exception
5293 when others then
5294 	 return 0;
5295 end get_volume_c_s;
5296 
5297 
5298 -- Pieces KPI
5299 function get_pieces_c_s (p_plan_id       in number
5300                        , p_report_for    in number
5301 		       , p_report_for_id in number
5302 		       , p_c_s_ident     in number
5303 		       , p_cust_supp_id  in number)
5304 return number is
5305   l_pieces number;
5306 
5307   cursor cur_pieces_myfac (l_plan_id  in number
5308                          , l_c_s_ident in number
5309 			 , l_cust_supp_id in number
5310                          , l_myfac_id in number) is
5311   select nvl(sum(nvl(md.number_of_pieces,0)),0)
5312   from mst_deliveries md
5313      , fte_location_parameters flp
5314   where md.plan_id = l_plan_id
5315   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5316   and (md.pickup_location_id = flp.location_id
5317       or md.dropoff_location_id = flp.location_id)
5318   and flp.facility_id = l_myfac_id;
5319 
5320 
5321   cursor cur_pieces_c_s (l_plan_id       in number
5322   		       , l_report_for 	 in number
5323   		       , l_report_for_id in number
5324                        , l_c_s_ident     in number
5325   		       , l_cust_supp_id  in number) is
5326   select nvl(sum(nvl(md.number_of_pieces,0)),0)
5327   from mst_deliveries md
5328   where md.plan_id = l_plan_id
5332   cursor cur_pieces_carr (l_plan_id      in number
5329   and decode ( l_report_for, 2, md.customer_id, 4, md.supplier_id, 0 ) = decode ( l_report_for, 2, l_report_for_id, 4, l_report_for_id, 0 )
5330   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id;
5331 
5333                         , l_c_s_ident    in number
5334   		        , l_cust_supp_id in number
5335 			, l_carrier_id   in number) is
5336   select nvl(sum(nvl(md.number_of_pieces,0)),0)
5337   from mst_deliveries md
5338   where md.plan_id = l_plan_id
5339   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5340   and md.delivery_id in (select distinct mdl.delivery_id
5341                          from mst_delivery_legs mdl
5342 			 , mst_trips mt
5343 			 where mdl.plan_id = md.plan_id
5344 			 and mt.plan_id = mdl.plan_id
5345 			 and mt.trip_id = mdl.trip_id
5346 			 and mt.carrier_id = l_carrier_id);
5347 begin
5348   if p_report_for = 1 then
5349     open cur_pieces_myfac (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id);
5350     fetch cur_pieces_myfac into l_pieces;
5351     close cur_pieces_myfac;
5352   elsif (p_report_for = 0 OR p_report_for = 2 OR p_report_for = 4) then
5353     --open cur_pieces_c_s (p_plan_id, p_c_s_ident, p_cust_supp_id);
5354     open cur_pieces_c_s (p_plan_id, p_report_for, p_report_for_id, p_c_s_ident, p_cust_supp_id);
5355     fetch cur_pieces_c_s into l_pieces;
5356     close cur_pieces_c_s;
5357   elsif p_report_for = 3 then
5358     open cur_pieces_carr (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id);
5359     fetch cur_pieces_carr into l_pieces;
5360     close cur_pieces_carr;
5361   end if;
5362 
5363 return l_pieces;
5364 exception
5365 when others then
5366 	 return 0;
5367 end get_pieces_c_s;
5368 
5369 
5370 function get_trips_per_mode_c_s (p_plan_id       in number
5371                                , p_report_for    in number
5372 		               , p_report_for_id in number
5373 			       , p_mode          in varchar2
5374 			       , p_c_s_ident     in number
5375 			       , p_cust_supp_id  in number)
5376 return number is
5377   l_trips_per_mode number;
5378 
5379   cursor cur_trips_myfac (l_plan_id  in number
5380                         , l_c_s_ident in number
5381 			, l_cust_supp_id in number
5382                         , l_myfac_id in number
5383 			, l_mode     in varchar2) is
5384   select count(*)
5385   from mst_trips mt
5386   , fte_location_parameters flp
5387   where mt.plan_id = l_plan_id
5388   and mt.mode_of_transport = l_mode
5389   and flp.location_id in (select distinct mts.stop_location_id
5390                           from mst_trip_stops mts
5391 			  , mst_delivery_legs mdl
5392 			  , mst_deliveries md
5393 			  where mts.plan_id = mt.plan_id
5394 			  and mts.trip_id = mt.trip_id
5395 			  and mdl.plan_id = mts.plan_id
5396 			  and mdl.trip_id = mts.trip_id
5397 			  and md.plan_id = mdl.plan_id
5398 			  and md.delivery_id = mdl.delivery_id
5399 			  and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id)
5400   and flp.facility_id = l_myfac_id;
5401 
5402   cursor cur_trips_c_s (l_plan_id       in number
5403   		      , l_report_for	in number
5404   		      , l_report_for_id	in number
5405                       , l_c_s_ident     in number
5406                       , l_cust_supp_id  in number
5407 		      , l_mode          in varchar2) is
5408   select count(mt.trip_id)
5409   from mst_trips mt
5410   where mt.plan_id = l_plan_id
5411   and   mt.trip_id in
5412                    (select distinct mts.trip_id
5413                     from mst_trip_stops mts
5414                     , mst_delivery_legs mdl
5415                     , mst_deliveries md
5416                     where md.plan_id = mt.plan_id
5417 		    and decode ( l_report_for, 2, md.customer_id, 4, md.supplier_id, 0 ) = decode ( l_report_for, 2, l_report_for_id, 4, l_report_for_id, 0 )
5418                     and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5419                     and mts.plan_id = md.plan_id
5420                     and mts.stop_location_id = md.dropoff_location_id
5421                     and mdl.plan_id = md.plan_id
5422                     and mdl.delivery_id = md.delivery_id
5423                     and mdl.trip_id = mts.trip_id
5424                     and mdl.drop_off_stop_id = mts.stop_id)
5425   and   mt.mode_of_transport = l_mode
5426   and   EXISTS
5427        (select ts.trip_id
5428         from mst_trip_stops ts
5429         where ts.plan_id = mt.plan_id
5430         and   ts.trip_id = mt.trip_id
5431         having count(ts.stop_id) >= 2
5432         group by ts.trip_id);
5433 
5434   cursor cur_trips_carr (l_plan_id      in number
5435                        , l_c_s_ident    in number
5436                        , l_cust_supp_id in number
5437 		       , l_carrier_id   in number
5438 		       , l_mode         in varchar2) is
5439   select count(mt.trip_id)
5440   from mst_trips mt
5441   where mt.plan_id = l_plan_id
5442   and   mt.carrier_id = l_carrier_id
5443   and   mt.trip_id in
5444                    (select distinct mts.trip_id
5445                     from mst_trip_stops mts
5446                     , mst_delivery_legs mdl
5447                     , mst_deliveries md
5448                     where md.plan_id = mt.plan_id
5449                     and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5450                     and mts.plan_id = md.plan_id
5451 --                    and mts.stop_location_id = md.dropoff_location_id
5452                     and mdl.plan_id = md.plan_id
5453                     and mdl.delivery_id = md.delivery_id
5454                     and mdl.trip_id = mts.trip_id
5455                     and mdl.drop_off_stop_id = mts.stop_id)
5456   and   mt.mode_of_transport = l_mode
5457   and   EXISTS
5458        (select ts.trip_id
5462         having count(ts.stop_id) >= 2
5459         from mst_trip_stops ts
5460         where ts.plan_id = mt.plan_id
5461         and   ts.trip_id = mt.trip_id
5463         group by ts.trip_id);
5464 begin
5465   if p_report_for = 1 then
5466     open cur_trips_myfac (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id, p_mode);
5467     fetch cur_trips_myfac into l_trips_per_mode;
5468     close cur_trips_myfac;
5469   elsif (p_report_for = 0 OR p_report_for = 2 OR p_report_for = 4) then
5470     --open cur_trips_c_s (p_plan_id, p_c_s_ident, p_cust_supp_id, p_mode);
5471     open cur_trips_c_s (p_plan_id, p_report_for, p_report_for_id, p_c_s_ident, p_cust_supp_id, p_mode);
5472     fetch cur_trips_c_s into l_trips_per_mode;
5473     close cur_trips_c_s;
5474   elsif p_report_for = 3 then
5475     open cur_trips_carr (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id, p_mode);
5476     fetch cur_trips_carr into l_trips_per_mode;
5477     close cur_trips_carr;
5478   end if;
5479 
5480    return l_trips_per_mode;
5481 exception
5482 when others then
5483 	 return 0;
5484 end get_trips_per_mode_c_s;
5485 
5486 
5487 function get_cost_per_mode_c_s (p_plan_id       in number
5488                               , p_report_for    in number
5489       		              , p_report_for_id in number
5490 		              , p_mode          in varchar2
5491 			      , p_c_s_ident     in number
5492 			      , p_cust_supp_id  in number)
5493 return number is
5494   l_total_cost_per_mode number;
5495 
5496   cursor cur_total_cost_myfac (l_plan_id  in number
5497                              , l_c_s_ident in number
5498 			     , l_cust_supp_id in number
5499                              , l_myfac_id in number
5500                              , l_mode     in varchar2) is
5501 /*
5502   select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
5503                + nvl(mdl.allocated_fac_unloading_cost,0)
5504 	       + nvl(mdl.allocated_transport_cost,0)
5505 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
5506                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
5507   from mst_delivery_legs mdl
5508   where mdl.plan_id = l_plan_id
5509   and mdl.delivery_id in
5510              ( select md.delivery_id
5511                from  mst_delivery_legs mdl1
5512                    , mst_deliveries md
5513                    , mst_trips mt
5514                    , mst_trip_stops mts
5515 		   , fte_location_parameters flp
5516                 where mt.plan_id = mdl1.plan_id
5517                 and   mt.trip_id  = mdl1.trip_id
5518                 and   mt.mode_of_transport = l_mode
5519                 and   mt.plan_id = mts.plan_id
5520                 and   mt.trip_id = mts.trip_id
5521                 and   mts.stop_location_id = flp.location_id
5522 		and   flp.facility_id = l_myfac_id
5523                 and   md.plan_id = mdl1.plan_id
5524                 and   md.delivery_id  = mdl1.delivery_id
5525                 and   md.plan_id = mdl.plan_id
5526                 and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5527                 and   (md.pickup_location_id = flp.location_id
5528 		       or md.dropoff_location_id = flp.location_id));
5529 */
5530 --Bug_Fix for 3696518
5531   (select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
5532                + nvl(mdl.allocated_fac_unloading_cost,0)
5533 	       + nvl(mdl.allocated_transport_cost,0)
5534 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
5535                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
5536   from mst_deliveries md
5537   , mst_delivery_legs mdl
5538   , mst_trips mt
5539   , fte_location_parameters flp
5540   where mt.plan_id = l_plan_id
5541   and mt.mode_of_transport = l_mode
5542   and mt.plan_id = mdl.plan_id
5543   and mt.trip_id = mdl.trip_id
5544   and mdl.plan_id = md.plan_id
5545   and mdl.delivery_id = md.delivery_id
5546   and flp.facility_id = l_myfac_id
5547   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5548   and ( md.pickup_location_id = flp.location_id
5549         or md.dropoff_location_id = flp.location_id ) );
5550 
5551   cursor cur_total_cost_c_s (l_plan_id       in number
5552   			   , l_report_for    in number
5553   			   , l_report_for_id in number
5554                            , l_c_s_ident     in number
5555                            , l_cust_supp_id  in number
5556 			   , l_mode          in varchar2) is
5557 /*
5558   select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
5559                + nvl(mdl.allocated_fac_unloading_cost,0)
5560 	       + nvl(mdl.allocated_transport_cost,0)
5561 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
5562                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
5563   from mst_delivery_legs mdl
5564   where mdl.plan_id = l_plan_id
5565   and mdl.delivery_id in
5566              ( select md.delivery_id
5567                from  mst_delivery_legs mdl1
5568                    , mst_deliveries md
5569                    , mst_trips mt
5570                    , mst_trip_stops mts
5571                 where mt.plan_id = mdl1.plan_id
5572                 and   mt.trip_id  = mdl1.trip_id
5573                 and   mt.mode_of_transport = l_mode
5574                 and   mt.plan_id = mts.plan_id
5575                 and   mt.trip_id = mts.trip_id
5576                 and   md.plan_id = mdl1.plan_id
5577                 and   md.delivery_id  = mdl1.delivery_id
5578                 and   md.plan_id = mdl.plan_id
5579   		and decode ( l_report_for, 2, md.customer_id, 4, md.supplier_id, 0 ) = decode ( l_report_for, 2, l_report_for_id, 4, l_report_for_id, 0 )
5580                 and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5581                 and   (md.pickup_location_id = mts.stop_location_id
5582 		       or md.dropoff_location_id = mts.stop_location_id));
5583 */
5584 --Bug_Fix for 3696518
5585   (select nvl(sum(nvl(mdl.allocated_fac_loading_cost,0)
5589                + nvl(mdl.allocated_fac_rec_hand_cost,0)),0)
5586                + nvl(mdl.allocated_fac_unloading_cost,0)
5587 	       + nvl(mdl.allocated_transport_cost,0)
5588 	       + nvl(mdl.allocated_fac_shp_hand_cost,0)
5590   from mst_deliveries md
5591   , mst_delivery_legs mdl
5592   , mst_trips mt
5593   where mt.plan_id = l_plan_id
5594   and mt.mode_of_transport = l_mode
5595   and mt.plan_id = mdl.plan_id
5596   and mt.trip_id = mdl.trip_id
5597   and mdl.plan_id = md.plan_id
5598   and mdl.delivery_id = md.delivery_id
5599   and decode ( l_report_for, 2, md.customer_id, 4, md.supplier_id, 0 ) = decode ( l_report_for, 2, l_report_for_id, 4, l_report_for_id, 0 )
5600   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id );
5601 
5602   cursor cur_total_cost_carr (l_plan_id      in number
5603                             , l_c_s_ident    in number
5604                             , l_cust_supp_id in number
5605                             , l_carrier_id   in number
5606 			    , l_mode         in varchar2) is
5607 /*  select nvl(sum(decode(mt.mode_of_transport
5608                        , 'TRUCK', (nvl(mt.total_basic_transport_cost,0)
5609                                  + nvl(mt.total_stop_cost,0)
5610 		                 + nvl(mt.total_load_unload_cost,0)
5611 			         + nvl(mt.total_layover_cost,0)
5612 			         + nvl(mt.total_accessorial_cost,0)
5613 			         + nvl(mt.total_handling_cost,0))
5614 		       , (nvl(mt.total_basic_transport_cost,0)
5615 		        + nvl(mt.total_accessorial_cost,0)))),0)
5616   from mst_trips mt
5617   where mt.plan_id = l_plan_id
5618   and mt.carrier_id = l_carrier_id
5619   and mt.mode_of_transport = l_mode
5620 --  and mt.continuous_move_id is null  --check
5621   and mt.trip_id in
5622                    (select distinct mts.trip_id
5623                     from mst_trip_stops mts
5624                     , mst_delivery_legs mdl
5625                     , mst_deliveries md
5626                     where md.plan_id = mt.plan_id
5627                     and mts.plan_id = md.plan_id
5628                     and mdl.plan_id = md.plan_id
5629                     and mdl.delivery_id = md.delivery_id
5630                     and mdl.trip_id = mts.trip_id
5631 --		    and (md.pickup_location_id = mts.stop_location_id
5632 --		        or md.dropoff_location_id = mts.stop_location_id)
5633 		    and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id);
5634 */
5635  select sum(nvl(mdl.allocated_fac_loading_cost,0)
5636           + nvl(mdl.allocated_fac_unloading_cost,0)
5637           + nvl(mdl.allocated_fac_shp_hand_cost,0)
5638 	  + nvl(mdl.allocated_fac_rec_hand_cost,0)
5639 	  + nvl(mdl.allocated_transport_cost,0))
5640   from  mst_deliveries md,
5641         mst_delivery_legs mdl,
5642         mst_trips mt
5643   where md.plan_id = l_plan_id
5644   and   decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5645   and   md.delivery_id = mdl.delivery_id
5646   and   mt.plan_id = mdl.plan_id
5647   and   mt.trip_id = mdl.trip_id
5648   and   mt.carrier_id = l_carrier_id
5649   and   mt.mode_of_transport = l_mode;
5650 
5651 begin
5652   if p_report_for = 1 then
5653     open cur_total_cost_myfac (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id, p_mode);
5654     fetch cur_total_cost_myfac into l_total_cost_per_mode;
5655     close cur_total_cost_myfac;
5656   elsif (p_report_for = 0 OR p_report_for = 2 OR p_report_for = 4) then
5657     --open cur_total_cost_c_s (p_plan_id, p_c_s_ident, p_cust_supp_id, p_mode);
5658     open cur_total_cost_c_s (p_plan_id, p_report_for, p_report_for_id, p_c_s_ident, p_cust_supp_id, p_mode);
5659     fetch cur_total_cost_c_s into l_total_cost_per_mode;
5660     close cur_total_cost_c_s;
5661   elsif p_report_for = 3 then
5662     open cur_total_cost_carr (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id, p_mode);
5663     fetch cur_total_cost_carr into l_total_cost_per_mode;
5664     close cur_total_cost_carr;
5665   end if;
5666 
5667    return nvl(l_total_cost_per_mode,0);
5668 exception
5669 when others then
5670 	 return 0;
5671 end get_cost_per_mode_c_s;
5672 
5673 
5674 function get_DTL_c_s (p_plan_id       in number
5675                      , p_report_for    in number
5676 		    , p_report_for_id in number
5677 		    , p_c_s_ident     in number
5678 		    , p_cust_supp_id  in varchar2)
5679 return number is
5680   l_DTL number;
5681 
5682   cursor cur_DTL_myfac (l_plan_id  in number
5683                       , l_c_s_ident    in number
5684                       , l_cust_supp_id in number
5685                       , l_myfac_id in number) is
5686 -- Bug_Fix for 3694008 -- optimized query
5687   SELECT count(mt.trip_id) num_stops
5688   FROM mst_trips mt
5689   WHERE mt.plan_id = l_plan_id
5690   AND mt.trip_id IN ( SELECT mdl.trip_id
5691 	                 FROM mst_deliveries md
5692                          , mst_delivery_legs mdl
5693                          , mst_trip_stops mts1
5694                          , fte_location_parameters flp
5695                          WHERE mdl.plan_id = mt.plan_id
5696                          AND md.plan_id = mdl.plan_id
5697                          AND md.delivery_id = mdl.delivery_id
5698                          AND decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5699                          AND mts1.plan_id = mdl.plan_id
5700                          AND mts1.trip_id = mdl.trip_id
5701                          -- and mts1.stop_id = mdl.pick_up_stop_id
5702                          -- Bug_Fix for 3694008
5703                          AND ( mts1.stop_id = mdl.pick_up_stop_id
5704                              OR mts1.stop_id = mdl.drop_off_stop_id )
5705                          AND flp.location_id = mts1.stop_location_id
5706                          AND flp.facility_id = l_myfac_id )
5710          FROM mst_trip_stops ts
5707   AND mt.mode_of_transport = 'TRUCK'
5708   AND EXISTS
5709        ( SELECT ts.trip_id
5711          WHERE ts.plan_id = mt.plan_id
5712          AND   ts.trip_id = mt.trip_id
5713          HAVING COUNT(ts.stop_id) = 2
5714          GROUP BY ts.trip_id );
5715 /*
5716   SELECT COUNT(*)
5717   FROM (SELECT DISTINCT mt.trip_id, count(*) num_stops
5718         FROM mst_trips mt
5719            , mst_trip_stops mts
5720 	    WHERE mt.plan_id = l_plan_id
5721 	    AND mt.trip_id in (SELECT DISTINCT mdl.trip_id
5722 	                       FROM   mst_deliveries md
5723 			                    , mst_delivery_legs mdl
5724 			                    , mst_trip_stops mts1
5725                                 , fte_location_parameters flp
5726 			               WHERE mdl.plan_id = mt.plan_id
5727 			               AND md.plan_id = mdl.plan_id
5728 			               AND md.delivery_id = mdl.delivery_id
5729 			               AND decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5730                            AND mts1.plan_id = mdl.plan_id
5731 			               AND mts1.trip_id = mdl.trip_id
5732                            --			   and mts1.stop_id = mdl.pick_up_stop_id
5733                            -- Bug_Fix for 3694008
5734                            AND ( mts1.stop_id = mdl.pick_up_stop_id
5735                                 OR mts1.stop_id = mdl.drop_off_stop_id )
5736                            AND flp.location_id = mts1.stop_location_id
5737                            AND flp.facility_id = l_myfac_id)
5738 	    AND mt.mode_of_transport = 'TRUCK'
5739 	    AND mts.plan_id = mt.plan_id
5740 	    AND mts.trip_id = mt.trip_id
5741 	    GROUP BY mt.trip_id) temp
5742         WHERE temp.num_stops = 2;
5743 */
5744 
5745   cursor cur_DTL_c_s (l_plan_id       in number
5746   		    , l_report_for    in number
5747   		    , l_report_for_id in number
5748                     , l_c_s_ident     in number
5749                     , l_cust_supp_id  in number) is
5750   select count(mt.trip_id)
5751   from mst_trips mt
5752   where mt.plan_id = l_plan_id
5753   and   mt.trip_id in
5754                    (select distinct mts.trip_id
5755                     from mst_trip_stops mts
5756                     , mst_delivery_legs mdl
5757                     , mst_deliveries md
5758                     where md.plan_id = mt.plan_id
5759                     and decode ( l_report_for, 2, md.customer_id, 4, md.supplier_id, 0 ) = decode ( l_report_for, 2, l_report_for_id, 4, l_report_for_id, 0 )
5760                     and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5761                     and mts.plan_id = md.plan_id
5762                     and mts.stop_location_id = md.dropoff_location_id
5763                     and mdl.plan_id = md.plan_id
5764                     and mdl.delivery_id = md.delivery_id
5765                     and mdl.trip_id = mts.trip_id
5766                     and mdl.drop_off_stop_id = mts.stop_id)
5767   and mt.mode_of_transport = 'TRUCK'
5768   and   EXISTS
5769        (select ts.trip_id
5770         from mst_trip_stops ts
5771         where ts.plan_id = mt.plan_id
5772         and   ts.trip_id = mt.trip_id
5773         having count(ts.stop_id) = 2
5774         group by ts.trip_id);
5775 
5776   cursor cur_DTL_carr (l_plan_id      in number
5777                      , l_c_s_ident    in number
5778                      , l_cust_supp_id in number
5779 		     , l_carrier_id   in number) is
5780   select count(mt.trip_id)
5781   from mst_trips mt
5782   where mt.plan_id = l_plan_id
5783   and   mt.carrier_id = l_carrier_id
5784   and   mt.trip_id in
5785                    (select distinct mts.trip_id
5786                     from mst_trip_stops mts
5787                     , mst_delivery_legs mdl
5788                     , mst_deliveries md
5789                     where md.plan_id = mt.plan_id
5790                     and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5791                     and mts.plan_id = md.plan_id
5792 --                    and mts.stop_location_id = md.dropoff_location_id
5796                     and mdl.drop_off_stop_id = mts.stop_id)
5793                     and mdl.plan_id = md.plan_id
5794                     and mdl.delivery_id = md.delivery_id
5795                     and mdl.trip_id = mts.trip_id
5797   and mt.mode_of_transport = 'TRUCK'
5798   and   EXISTS
5799        (select ts.trip_id
5800         from mst_trip_stops ts
5801         where ts.plan_id = mt.plan_id
5802         and   ts.trip_id = mt.trip_id
5803         having count(ts.stop_id) = 2
5804         group by ts.trip_id);
5805 begin
5806   if p_report_for = 1 then
5807     open cur_DTL_myfac (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id);
5808     fetch cur_DTL_myfac into l_DTL;
5809     close cur_DTL_myfac;
5810   elsif (p_report_for = 0 OR p_report_for = 2 OR p_report_for = 4) then
5811     --open cur_DTL_c_s (p_plan_id, p_c_s_ident, p_cust_supp_id);
5812     open cur_DTL_c_s (p_plan_id, p_report_for, p_report_for_id, p_c_s_ident, p_cust_supp_id);
5813     fetch cur_DTL_c_s into l_DTL;
5814     close cur_DTL_c_s;
5815   elsif p_report_for = 3 then
5816     open cur_DTL_carr (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id);
5817     fetch cur_DTL_carr into l_DTL;
5818     close cur_DTL_carr;
5819   end if;
5820 
5821    return l_DTL;
5822 exception
5823 when others then
5824 	 return 0;
5825 end get_DTL_c_s;
5826 
5827 
5828 function get_MTL_c_s (p_plan_id       in number
5829                     , p_report_for    in number
5830 		    , p_report_for_id in number
5831 		    , p_c_s_ident     in number
5832 		    , p_cust_supp_id  in varchar2)
5833 return number is
5834   l_MTL number;
5835 
5836   cursor cur_MTL_myfac (l_plan_id  in number
5837                       , l_c_s_ident    in number
5838                       , l_cust_supp_id in number
5839                       , l_myfac_id in number) is
5840 -- Bug_Fix for 3694008 -- optimized query
5841   SELECT count(mt.trip_id) num_stops
5842   FROM mst_trips mt
5843   WHERE mt.plan_id = l_plan_id
5844   AND mt.trip_id IN ( SELECT mdl.trip_id
5845 	                 FROM mst_deliveries md
5846                          , mst_delivery_legs mdl
5847                          , mst_trip_stops mts1
5848                          , fte_location_parameters flp
5849                          WHERE mdl.plan_id = mt.plan_id
5850                          AND md.plan_id = mdl.plan_id
5851                          AND md.delivery_id = mdl.delivery_id
5852                          AND decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5853                          AND mts1.plan_id = mdl.plan_id
5854                          AND mts1.trip_id = mdl.trip_id
5855                          -- and mts1.stop_id = mdl.pick_up_stop_id
5856                          -- Bug_Fix for 3694008
5857                          AND ( mts1.stop_id = mdl.pick_up_stop_id
5858                              OR mts1.stop_id = mdl.drop_off_stop_id )
5859                          AND flp.location_id = mts1.stop_location_id
5863        ( SELECT ts.trip_id
5860                          AND flp.facility_id = l_myfac_id )
5861   AND mt.mode_of_transport = 'TRUCK'
5862   AND EXISTS
5864          FROM mst_trip_stops ts
5865          WHERE ts.plan_id = mt.plan_id
5866          AND   ts.trip_id = mt.trip_id
5867          HAVING COUNT(ts.stop_id) > 2
5868          GROUP BY ts.trip_id );
5869 /*
5870   select count(*)
5871   from (select distinct mt.trip_id, count(*) num_stops
5872         from mst_trips mt
5873         , mst_trip_stops mts
5874 	where mt.plan_id = l_plan_id
5875 	and mt.trip_id in (select distinct mdl.trip_id
5876 	                   from mst_deliveries md
5877 			   , mst_delivery_legs mdl
5878 			   , mst_trip_stops mts1
5879                            , fte_location_parameters flp
5880 			   where mdl.plan_id = mt.plan_id
5881 			   and md.plan_id = mdl.plan_id
5882 			   and md.delivery_id = mdl.delivery_id
5883 			   and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5884                            and mts1.plan_id = mdl.plan_id
5885 			   and mts1.trip_id = mdl.trip_id
5886 --			   and mts1.stop_id = mdl.pick_up_stop_id
5887 -- Bug_Fix for 3694008
5888                            and ( mts1.stop_id = mdl.pick_up_stop_id
5889                                or mts1.stop_id = mdl.drop_off_stop_id )
5890                            and flp.location_id = mts1.stop_location_id
5891                            and flp.facility_id = l_myfac_id)
5892 	and mt.mode_of_transport = 'TRUCK'
5893 	and mts.plan_id = mt.plan_id
5894 	and mts.trip_id = mt.trip_id
5895 	group by mt.trip_id) temp
5896   where temp.num_stops > 2;
5897 */
5898 
5899   cursor cur_MTL_c_s (l_plan_id       in number
5900   		    , l_report_for    in number
5901   		    , l_report_for_id in number
5902                     , l_c_s_ident     in number
5903                     , l_cust_supp_id  in number) is
5904   select count(mt.trip_id)
5905   from mst_trips mt
5906   where mt.plan_id = l_plan_id
5907   and   mt.trip_id in
5908                    (select distinct mts.trip_id
5909                     from mst_trip_stops mts
5910                     , mst_delivery_legs mdl
5911                     , mst_deliveries md
5912                     where md.plan_id = mt.plan_id
5913   		    and decode ( l_report_for, 2, md.customer_id, 4, md.supplier_id, 0 ) = decode ( l_report_for, 2, l_report_for_id, 4, l_report_for_id, 0 )
5914                     and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5915                     and mts.plan_id = md.plan_id
5916                     and mts.stop_location_id = md.dropoff_location_id
5917                     and mdl.plan_id = md.plan_id
5918                     and mdl.delivery_id = md.delivery_id
5919                     and mdl.trip_id = mts.trip_id
5920                     and mdl.drop_off_stop_id = mts.stop_id)
5921   and mt.mode_of_transport = 'TRUCK'
5922   and   EXISTS
5923        (select ts.trip_id
5924         from mst_trip_stops ts
5925         where ts.plan_id = mt.plan_id
5926         and   ts.trip_id = mt.trip_id
5927         having count(ts.stop_id) > 2
5928         group by ts.trip_id);
5929 
5930   cursor cur_MTL_carr (l_plan_id      in number
5931                      , l_c_s_ident    in number
5935   from mst_trips mt
5932                      , l_cust_supp_id in number
5933 		     , l_carrier_id   in number) is
5934   select count(mt.trip_id)
5936   where mt.plan_id = l_plan_id
5937   and   mt.carrier_id = l_carrier_id
5938   and   mt.trip_id in
5939                    (select distinct mts.trip_id
5940                     from mst_trip_stops mts
5941                     , mst_delivery_legs mdl
5942                     , mst_deliveries md
5943                     where md.plan_id = mt.plan_id
5944                     and decode(l_c_s_ident, 2, md.customer_id, md.supplier_id) = l_cust_supp_id
5945                     and mts.plan_id = md.plan_id
5946 --                    and mts.stop_location_id = md.dropoff_location_id
5947                     and mdl.plan_id = md.plan_id
5948                     and mdl.delivery_id = md.delivery_id
5949                     and mdl.trip_id = mts.trip_id
5950                     and mdl.drop_off_stop_id = mts.stop_id)
5951   and mt.mode_of_transport = 'TRUCK'
5952   and   EXISTS
5953        (select ts.trip_id
5954         from mst_trip_stops ts
5955         where ts.plan_id = mt.plan_id
5956         and   ts.trip_id = mt.trip_id
5957         having count(ts.stop_id) > 2
5958         group by ts.trip_id);
5959 begin
5960   if p_report_for = 1 then
5961     open cur_MTL_myfac (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id);
5962     fetch cur_MTL_myfac into l_MTL;
5963     close cur_MTL_myfac;
5964   elsif (p_report_for = 0 OR p_report_for = 2 OR p_report_for = 4) then
5965     --open cur_MTL_c_s (p_plan_id, p_c_s_ident, p_cust_supp_id);
5966     open cur_MTL_c_s (p_plan_id, p_report_for, p_report_for_id, p_c_s_ident, p_cust_supp_id);
5967     fetch cur_MTL_c_s into l_MTL;
5968     close cur_MTL_c_s;
5969   elsif p_report_for = 3 then
5970     open cur_MTL_carr (p_plan_id, p_c_s_ident, p_cust_supp_id, p_report_for_id);
5971     fetch cur_MTL_carr into l_MTL;
5972     close cur_MTL_carr;
5973   end if;
5974 
5975    return l_MTL;
5976 exception
5977 when others then
5978 	 return 0;
5979 end get_MTL_c_s;
5980 
5981 
5982 PROCEDURE Populate_Master_Summary_GTT (p_plan_id       in number
5983                                      , p_report_for    in number
5984 				     , p_report_for_id in number default 0) is
5985   -- local variables
5986   l_c_s_ident number;
5987 BEGIN
5988 
5989   -- populate header section
5990   -- (view_id = 1)
5991   insert into mst_mast_sum_report_temp_gt
5992   ( view_id
5993   , plan_id
5994   , compile_designator
5995   , report_date
5996   , plan_start_date
5997   , plan_end_date
5998   , orders
5999   , order_groups
6000   , weight
6001   , volume
6002   , pieces
6003   , pallets
6004   , allocated_cost  ---not used anymore
6005   , plan_value
6006   , total_tl_count
6007   , ltl_count
6008   , parcel_count
6009   , tl_cost
6010   , ltl_cost
6011   , parcel_cost
6012   , transportation_cost
6013   , handling_cost
6014   , total_cost
6015   , tl_stops
6016   , tl_distance
6017   , percent_alloc_cost  ---not used anymore
6018   , percent_value
6019   )
6020  (SELECT 1
6021   , mp.plan_id
6022   , mp.compile_designator
6023   , sysdate
6024   , mp.start_date
6025   , mp.cutoff_date
6026   , mst_reports_pkg.get_plan_order_count(p_plan_id, p_report_for, p_report_for_id)
6027   , mst_reports_pkg.get_order_group_count(p_plan_id, p_report_for, p_report_for_id)
6028   , mst_reports_pkg.get_weight(p_plan_id, p_report_for, p_report_for_id)
6029   , mst_reports_pkg.get_volume(p_plan_id, p_report_for, p_report_for_id)
6030   , mst_reports_pkg.get_pieces(p_plan_id, p_report_for, p_report_for_id)
6031   , 0
6032   , 0
6033   , mst_reports_pkg.get_plan_value(p_plan_id, p_report_for, p_report_for_id)
6034   , mst_reports_pkg.get_trips_per_mode(p_plan_id, p_report_for, p_report_for_id,'TRUCK')
6035   , mst_reports_pkg.get_trips_per_mode(p_plan_id, p_report_for, p_report_for_id,'LTL')
6036   , mst_reports_pkg.get_trips_per_mode(p_plan_id, p_report_for, p_report_for_id,'PARCEL')
6037   , mst_reports_pkg.get_trans_cost_per_mode(p_plan_id, p_report_for, p_report_for_id,'TRUCK')
6038   , mst_reports_pkg.get_trans_cost_per_mode(p_plan_id, p_report_for, p_report_for_id,'LTL')
6039   , mst_reports_pkg.get_trans_cost_per_mode(p_plan_id, p_report_for, p_report_for_id,'PARCEL')
6040   , mst_reports_pkg.get_trans_cost_per_mode(p_plan_id, p_report_for, p_report_for_id,'TRUCK')
6041    +mst_reports_pkg.get_trans_cost_per_mode(p_plan_id, p_report_for, p_report_for_id,'LTL')
6042    +mst_reports_pkg.get_trans_cost_per_mode(p_plan_id, p_report_for, p_report_for_id,'PARCEL')
6043   , mst_reports_pkg.get_handl_cost_per_mode(p_plan_id, p_report_for, p_report_for_id,'TRUCK')
6044    +mst_reports_pkg.get_handl_cost_per_mode(p_plan_id, p_report_for, p_report_for_id,'LTL')
6045    +mst_reports_pkg.get_handl_cost_per_mode(p_plan_id, p_report_for, p_report_for_id,'PARCEL')
6046   , mst_reports_pkg.get_total_cost_per_mode(p_plan_id, p_report_for, p_report_for_id,'TRUCK')
6047    +mst_reports_pkg.get_total_cost_per_mode(p_plan_id, p_report_for, p_report_for_id,'LTL')
6048    +mst_reports_pkg.get_total_cost_per_mode(p_plan_id, p_report_for, p_report_for_id,'PARCEL')
6049   , mst_reports_pkg.get_stops_per_load(p_plan_id, p_report_for, p_report_for_id)
6050   , mst_reports_pkg.get_TL_distance(p_plan_id, p_report_for, p_report_for_id)
6051   , 0
6052   , ( ( mst_reports_pkg.get_total_cost_per_mode(p_plan_id, p_report_for, p_report_for_id,'TRUCK')
6053        +mst_reports_pkg.get_total_cost_per_mode(p_plan_id, p_report_for, p_report_for_id,'LTL')
6054        +mst_reports_pkg.get_total_cost_per_mode(p_plan_id, p_report_for, p_report_for_id,'PARCEL')
6055        ) / decode( mst_reports_pkg.get_plan_value(p_plan_id, p_report_for, p_report_for_id)
6056                  , 0, 1
6057 	         , mst_reports_pkg.get_plan_value(p_plan_id, p_report_for, p_report_for_id)) ) * 100
6061 
6058   from mst_plans mp
6059   where mp.plan_id = p_plan_id
6060  );
6062 
6063   -- populate Business Unit section
6064   -- (view_id = 2)
6065   insert into mst_mast_sum_report_temp_gt
6066   ( view_id
6067   , plan_id
6068   , facility_name
6069   , orders
6070   , weight
6071   , volume
6072   , pieces
6073   , tl_cost
6074   , ltl_cost
6075   , parcel_cost
6076   , total_cost
6077   , cost_per_unit_weight
6078   , cost_per_unit_volume
6079   , percent_value
6080   , percent_alloc_cost
6081   )
6082   (SELECT 2
6083   , temp.plan_id
6084   , fte.facility_code
6085   , mst_reports_pkg.get_orders_myfac(p_plan_id,p_report_for,p_report_for_id,fte.facility_id)
6086   , mst_reports_pkg.get_weight_myfac(p_plan_id,p_report_for,p_report_for_id,fte.facility_id)
6087   , mst_reports_pkg.get_volume_myfac(p_plan_id,p_report_for,p_report_for_id,fte.facility_id)
6088   , mst_reports_pkg.get_pieces_myfac(p_plan_id,p_report_for,p_report_for_id,fte.facility_id)
6089   , mst_reports_pkg.get_cost_per_mode_myfac(p_plan_id,p_report_for,p_report_for_id,'TRUCK',fte.facility_id)
6090   , mst_reports_pkg.get_cost_per_mode_myfac(p_plan_id,p_report_for,p_report_for_id,'LTL',fte.facility_id)
6091   , mst_reports_pkg.get_cost_per_mode_myfac(p_plan_id,p_report_for,p_report_for_id,'PARCEL',fte.facility_id)
6092   , mst_reports_pkg.get_cost_per_mode_myfac(p_plan_id,p_report_for,p_report_for_id,'TRUCK',fte.facility_id)
6093     +mst_reports_pkg.get_cost_per_mode_myfac(p_plan_id,p_report_for,p_report_for_id,'LTL',fte.facility_id)
6094     +mst_reports_pkg.get_cost_per_mode_myfac(p_plan_id,p_report_for,p_report_for_id,'PARCEL',fte.facility_id)
6095   ,( mst_reports_pkg.get_cost_per_mode_myfac(p_plan_id,p_report_for,p_report_for_id,'TRUCK',fte.facility_id)
6096     +mst_reports_pkg.get_cost_per_mode_myfac(p_plan_id,p_report_for,p_report_for_id,'LTL',fte.facility_id)
6097     +mst_reports_pkg.get_cost_per_mode_myfac(p_plan_id,p_report_for,p_report_for_id,'PARCEL',fte.facility_id)
6098    ) / decode( mst_reports_pkg.get_weight_myfac(p_plan_id,p_report_for,p_report_for_id,fte.facility_id)
6099               ,0 ,1
6100               , mst_reports_pkg.get_weight_myfac(p_plan_id,p_report_for,p_report_for_id,fte.facility_id))
6101   ,( mst_reports_pkg.get_cost_per_mode_myfac(p_plan_id,p_report_for,p_report_for_id,'TRUCK',fte.facility_id)
6102     +mst_reports_pkg.get_cost_per_mode_myfac(p_plan_id,p_report_for,p_report_for_id,'LTL',fte.facility_id)
6103     +mst_reports_pkg.get_cost_per_mode_myfac(p_plan_id,p_report_for,p_report_for_id,'PARCEL',fte.facility_id)
6104    ) / decode( mst_reports_pkg.get_volume_myfac(p_plan_id,p_report_for,p_report_for_id,fte.facility_id)
6105               ,0 ,1
6106 	      , mst_reports_pkg.get_volume_myfac(p_plan_id,p_report_for,p_report_for_id,fte.facility_id))
6107 , ( ( mst_reports_pkg.get_cost_per_mode_myfac(p_plan_id,p_report_for,p_report_for_id,'TRUCK',fte.facility_id)
6108      +mst_reports_pkg.get_cost_per_mode_myfac(p_plan_id,p_report_for,p_report_for_id,'LTL',fte.facility_id)
6109      +mst_reports_pkg.get_cost_per_mode_myfac(p_plan_id,p_report_for,p_report_for_id,'PARCEL',fte.facility_id)
6110      ) / decode( nvl(mst_reports_pkg.get_plan_value(p_plan_id,p_report_for,p_report_for_id),0)
6111                  , 0, 1
6112 		 , mst_reports_pkg.get_plan_value(p_plan_id,p_report_for,p_report_for_id)) ) * 100
6113   , 0
6114   from (select distinct mts.plan_id plan_id, mts.stop_location_id loc_id
6115         from mst_trip_stops mts
6116         , mst_trips mt
6117         where mts.plan_id = mt.plan_id
6118         and mts.trip_id = mt.trip_id) temp,
6119   mst_plans mp,
6120   wsh_location_owners wlo,
6121   wsh_locations wl,
6122   fte_location_parameters fte
6123   where mp.plan_id = temp.plan_id
6124   and temp.loc_id = fte.location_id
6125   and fte.location_id = wl.wsh_location_id
6126   and wl.wsh_location_id = wlo.wsh_location_id
6127   and wlo.owner_type = 1
6128   and mp.plan_id = p_plan_id
6129   and decode ( p_report_for, 1, fte.facility_id, p_report_for_id ) = p_report_for_id
6130 );
6131 
6132 
6133   -- populate Carrier section
6134   -- (view_id = 3)
6135   insert into mst_mast_sum_report_temp_gt
6136   ( view_id
6137   , plan_id
6138   , carrier_name
6139   , carr_moves
6140   , total_cost
6141   , mode_of_transport
6142   )
6143   (SELECT 3
6144   , mt.plan_id
6145   , wc.freight_code
6146   , mst_reports_pkg.get_carr_movements(p_plan_id,p_report_for,p_report_for_id,mt.carrier_id)
6147   , mst_reports_pkg.get_carr_cost(p_plan_id,p_report_for,p_report_for_id,mt.carrier_id)
6148   , mt.mode_of_transport
6149   from mst_trips mt
6150   , wsh_carriers wc
6151   WHERE mt.carrier_id = wc.carrier_id
6152   and mt.plan_id = p_plan_id
6153   and decode ( p_report_for, 3, mt.carrier_id, p_report_for_id ) = p_report_for_id
6154   group by mt.plan_id
6155   , mt.carrier_id
6156   , wc.freight_code
6157   ,mt.mode_of_transport
6158  );
6159 
6160 
6161 
6162   -- populate Originwise summary section
6163   -- (view_id = 4)
6164   insert into mst_mast_sum_report_temp_gt
6165   ( view_id
6166   , plan_id
6167   , origin_name
6168   , orders
6169   , weight
6170   , volume
6171   , pieces
6172   , mtl_count
6173   , dtl_count
6174   , ltl_count
6175   , parcel_count
6176   , tl_cost
6177   , ltl_cost
6178   , parcel_cost
6179   , total_cost
6180   , cost_per_unit_weight
6181   , cost_per_unit_volume
6182   , percent_value
6183   , percent_alloc_cost
6184   )
6185   (SELECT distinct 4
6186   , mp.plan_id
6187   , wl.state
6188   , mst_reports_pkg.get_orders_orig(p_plan_id,p_report_for,p_report_for_id,wl.state)
6189   , mst_reports_pkg.get_weight_orig(p_plan_id,p_report_for,p_report_for_id,wl.state)
6190   , mst_reports_pkg.get_volume_orig(p_plan_id,p_report_for,p_report_for_id,wl.state)
6191   , mst_reports_pkg.get_pieces_orig(p_plan_id,p_report_for,p_report_for_id,wl.state)
6192   , mst_reports_pkg.get_MTL_orig(p_plan_id,p_report_for,p_report_for_id,wl.state)
6193   , mst_reports_pkg.get_DTL_orig(p_plan_id,p_report_for,p_report_for_id,wl.state)
6197   , mst_reports_pkg.get_total_cost_mode_orig(p_plan_id,p_report_for,p_report_for_id,'LTL',wl.state)
6194   , mst_reports_pkg.get_LTL_orig(p_plan_id,p_report_for,p_report_for_id,wl.state)
6195   , mst_reports_pkg.get_PCL_orig(p_plan_id,p_report_for,p_report_for_id,wl.state)
6196   , mst_reports_pkg.get_total_cost_mode_orig(p_plan_id,p_report_for,p_report_for_id,'TRUCK',wl.state)
6198   , mst_reports_pkg.get_total_cost_mode_orig(p_plan_id,p_report_for,p_report_for_id,'PARCEL',wl.state)
6199   , mst_reports_pkg.get_total_cost_mode_orig(p_plan_id,p_report_for,p_report_for_id,'TRUCK',wl.state)
6200        +mst_reports_pkg.get_total_cost_mode_orig(p_plan_id,p_report_for,p_report_for_id,'LTL',wl.state)
6201        +mst_reports_pkg.get_total_cost_mode_orig(p_plan_id,p_report_for,p_report_for_id,'PARCEL',wl.state)
6202   , ( mst_reports_pkg.get_total_cost_mode_orig(p_plan_id,p_report_for,p_report_for_id,'TRUCK',wl.state)
6203      +mst_reports_pkg.get_total_cost_mode_orig(p_plan_id,p_report_for,p_report_for_id,'LTL',wl.state)
6204      +mst_reports_pkg.get_total_cost_mode_orig(p_plan_id,p_report_for,p_report_for_id,'PARCEL',wl.state)
6205     ) / decode( mst_reports_pkg.get_weight_orig(p_plan_id,p_report_for,p_report_for_id,wl.state)
6206                ,0 ,1
6207 	       ,mst_reports_pkg.get_weight_orig(p_plan_id,p_report_for,p_report_for_id,wl.state))
6208   , ( mst_reports_pkg.get_total_cost_mode_orig(p_plan_id,p_report_for,p_report_for_id,'TRUCK',wl.state)
6209      +mst_reports_pkg.get_total_cost_mode_orig(p_plan_id,p_report_for,p_report_for_id,'LTL',wl.state)
6210      +mst_reports_pkg.get_total_cost_mode_orig(p_plan_id,p_report_for,p_report_for_id,'PARCEL',wl.state)
6211     ) / decode( mst_reports_pkg.get_volume_orig(p_plan_id,p_report_for,p_report_for_id,wl.state)
6212                ,0 ,1
6213  	       ,mst_reports_pkg.get_volume_orig(p_plan_id,p_report_for,p_report_for_id,wl.state))
6214   ,( mst_reports_pkg.get_total_cost_mode_orig(p_plan_id,p_report_for,p_report_for_id,'TRUCK',wl.state)
6215     +mst_reports_pkg.get_total_cost_mode_orig(p_plan_id,p_report_for,p_report_for_id,'LTL',wl.state)
6216     +mst_reports_pkg.get_total_cost_mode_orig(p_plan_id,p_report_for,p_report_for_id,'PARCEL',wl.state)
6217    ) / decode( nvl(mst_reports_pkg.get_plan_value(p_plan_id,p_report_for,p_report_for_id),0)
6218              , 0, 1
6219  	     , mst_reports_pkg.get_plan_value(p_plan_id,p_report_for,p_report_for_id) ) * 100
6220   , 0
6221   from mst_plans mp
6222   ,wsh_locations wl
6223   WHERE mp.plan_id = p_plan_id
6224   and wl.wsh_location_id in (select distinct md.pickup_location_id
6225                              from mst_deliveries md
6226                              where md.plan_id = mp.plan_id)
6227   );
6228 
6229 
6230   -- populate Destinationwise summary section
6231   -- (view_id = 5)
6232   insert into mst_mast_sum_report_temp_gt
6233   ( view_id
6234   , plan_id
6235   , destination_name
6236   , orders
6237   , weight
6238   , volume
6239   , pieces
6240   , mtl_count
6241   , dtl_count
6242   , ltl_count
6243   , parcel_count
6244   , tl_cost
6245   , ltl_cost
6246   , parcel_cost
6247   , total_cost
6248   , cost_per_unit_weight
6249   , cost_per_unit_volume
6250   , percent_value
6251   , percent_alloc_cost
6252   )
6253   (SELECT distinct 5
6254   , mp.plan_id
6255   , wl.state
6256   , mst_reports_pkg.get_orders_dest(p_plan_id,p_report_for,p_report_for_id,wl.state)
6257   , mst_reports_pkg.get_weight_dest(p_plan_id,p_report_for,p_report_for_id,wl.state)
6258   , mst_reports_pkg.get_volume_dest(p_plan_id,p_report_for,p_report_for_id,wl.state)
6259   , mst_reports_pkg.get_pieces_dest(p_plan_id,p_report_for,p_report_for_id,wl.state)
6260   , mst_reports_pkg.get_MTL_dest(p_plan_id,p_report_for,p_report_for_id,wl.state)
6261   , mst_reports_pkg.get_DTL_dest(p_plan_id,p_report_for,p_report_for_id,wl.state)
6262   , mst_reports_pkg.get_LTL_dest(p_plan_id,p_report_for,p_report_for_id,wl.state)
6263   , mst_reports_pkg.get_PCL_dest(p_plan_id,p_report_for,p_report_for_id,wl.state)
6264   , mst_reports_pkg.get_total_cost_mode_dest(p_plan_id,p_report_for,p_report_for_id,'TRUCK',wl.state)
6265   , mst_reports_pkg.get_total_cost_mode_dest(p_plan_id,p_report_for,p_report_for_id,'LTL',wl.state)
6266   , mst_reports_pkg.get_total_cost_mode_dest(p_plan_id,p_report_for,p_report_for_id,'PARCEL',wl.state)
6267   , mst_reports_pkg.get_total_cost_mode_dest(p_plan_id,p_report_for,p_report_for_id,'TRUCK',wl.state)
6268     +mst_reports_pkg.get_total_cost_mode_dest(p_plan_id,p_report_for,p_report_for_id,'LTL',wl.state)
6269     +mst_reports_pkg.get_total_cost_mode_dest(p_plan_id,p_report_for,p_report_for_id,'PARCEL',wl.state)
6270   , ( mst_reports_pkg.get_total_cost_mode_dest(p_plan_id,p_report_for,p_report_for_id,'TRUCK',wl.state)
6271      +mst_reports_pkg.get_total_cost_mode_dest(p_plan_id,p_report_for,p_report_for_id,'LTL',wl.state)
6272      +mst_reports_pkg.get_total_cost_mode_dest(p_plan_id,p_report_for,p_report_for_id,'PARCEL',wl.state)
6273     ) / decode( mst_reports_pkg.get_weight_dest(p_plan_id,p_report_for,p_report_for_id,wl.state)
6274                ,0 ,1
6275                ,mst_reports_pkg.get_weight_dest(p_plan_id,p_report_for,p_report_for_id,wl.state))
6276   , ( mst_reports_pkg.get_total_cost_mode_dest(p_plan_id,p_report_for,p_report_for_id,'TRUCK',wl.state)
6277      +mst_reports_pkg.get_total_cost_mode_dest(p_plan_id,p_report_for,p_report_for_id,'LTL',wl.state)
6278      +mst_reports_pkg.get_total_cost_mode_dest(p_plan_id,p_report_for,p_report_for_id,'PARCEL',wl.state)
6279     ) / decode( mst_reports_pkg.get_volume_dest(p_plan_id,p_report_for,p_report_for_id,wl.state)
6280                ,0 ,1
6281                ,mst_reports_pkg.get_volume_dest(p_plan_id,p_report_for,p_report_for_id,wl.state))
6282   ,( mst_reports_pkg.get_total_cost_mode_dest(p_plan_id,p_report_for,p_report_for_id,'TRUCK',wl.state)
6283     +mst_reports_pkg.get_total_cost_mode_dest(p_plan_id,p_report_for,p_report_for_id,'LTL',wl.state)
6284     +mst_reports_pkg.get_total_cost_mode_dest(p_plan_id,p_report_for,p_report_for_id,'PARCEL',wl.state)
6285    ) / decode( nvl(mst_reports_pkg.get_plan_value(p_plan_id,p_report_for,p_report_for_id),0)
6286                , 0, 1
6290   ,wsh_locations wl
6287                , mst_reports_pkg.get_plan_value(p_plan_id,p_report_for,p_report_for_id) ) * 100
6288   , 0
6289   from mst_plans mp
6291   WHERE mp.plan_id = p_plan_id
6292   and wl.wsh_location_id in (select distinct md.dropoff_location_id
6293                              from mst_deliveries md
6294                              where md.plan_id = mp.plan_id)
6295   );
6296 
6297 
6298   -- populate Customer summary section
6299   -- (view_id = 6)
6300   --l_c_s_ident is the customer/supplier identifier.
6301   --it is set to 2 here as it is populating the customer data in the following sql
6302   l_c_s_ident := 2;
6303 
6304   insert into mst_mast_sum_report_temp_gt
6305   ( view_id
6306   , plan_id
6307   , customer_name
6308   , orders
6309   , weight
6310   , volume
6311   , pieces
6312   , mtl_count
6313   , dtl_count
6314   , ltl_count
6315   , parcel_count
6316   , tl_cost
6317   , ltl_cost
6318   , parcel_cost
6319   , total_cost
6320   , cost_per_unit_weight
6321   , cost_per_unit_volume
6322   , percent_value
6323   , percent_alloc_cost
6324   )
6325   (SELECT 6
6326   , mp.plan_id
6327   , hzp.party_name
6328   , mst_reports_pkg.get_orders_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,hzc.cust_account_id)
6329   , mst_reports_pkg.get_weight_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,hzc.cust_account_id)
6330   , mst_reports_pkg.get_volume_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,hzc.cust_account_id)
6331   , mst_reports_pkg.get_pieces_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,hzc.cust_account_id)
6332   , mst_reports_pkg.get_MTL_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,hzc.cust_account_id)
6333   , mst_reports_pkg.get_DTL_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,hzc.cust_account_id)
6334   , mst_reports_pkg.get_trips_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'LTL',l_c_s_ident,hzc.cust_account_id)
6335   , mst_reports_pkg.get_trips_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'PARCEL',l_c_s_ident,hzc.cust_account_id)
6336   , mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'TRUCK',l_c_s_ident,hzc.cust_account_id)
6337   , mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'LTL',l_c_s_ident,hzc.cust_account_id)
6338   , mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'PARCEL',l_c_s_ident,hzc.cust_account_id)
6339   , mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'TRUCK',l_c_s_ident,hzc.cust_account_id)
6340    +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'LTL',l_c_s_ident,hzc.cust_account_id)
6341    +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'PARCEL',l_c_s_ident,hzc.cust_account_id)
6342   , ( mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'TRUCK',l_c_s_ident,hzc.cust_account_id)
6343      +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'LTL',l_c_s_ident,hzc.cust_account_id)
6344      +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'PARCEL',l_c_s_ident,hzc.cust_account_id)
6345 	) / decode( mst_reports_pkg.get_weight_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,hzc.cust_account_id)
6346 	            ,0 ,1
6347 	            ,mst_reports_pkg.get_weight_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,hzc.cust_account_id))
6348   , ( mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'TRUCK',l_c_s_ident,hzc.cust_account_id)
6349      +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'LTL',l_c_s_ident,hzc.cust_account_id)
6350      +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'PARCEL',l_c_s_ident,hzc.cust_account_id)
6351 	) / decode( mst_reports_pkg.get_volume_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,hzc.cust_account_id)
6352 	            ,0 ,1
6353                     ,mst_reports_pkg.get_volume_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,hzc.cust_account_id))
6354   , (( mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'TRUCK',l_c_s_ident,hzc.cust_account_id)
6355       +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'LTL',l_c_s_ident,hzc.cust_account_id)
6356       +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'PARCEL',l_c_s_ident,hzc.cust_account_id)
6357      ) / decode( nvl(mst_reports_pkg.get_plan_value(p_plan_id,p_report_for,p_report_for_id),0)
6358                  , 0, 1
6359 		 , mst_reports_pkg.get_plan_value(p_plan_id,p_report_for,p_report_for_id)) ) * 100
6360   ,0
6361   from mst_plans mp
6362   , hz_parties hzp
6363   , hz_cust_accounts hzc
6364   WHERE mp.plan_id = p_plan_id
6365   and hzp.party_id = hzc.party_id
6366   and hzc.cust_account_id in (select distinct md.customer_id
6367                               from mst_deliveries md
6368                               where md.plan_id = mp.plan_id)
6369   and decode ( p_report_for, 2, hzc.cust_account_id, p_report_for_id ) = p_report_for_id
6370   );
6371 
6372 
6373   -- populate Supplier Summary section
6374   -- (view_id = 7)
6375   --l_c_s_ident is the customer/supplier identifier.
6376   --it is set to 4 here as it is populating the supplier data in the following sql
6377   l_c_s_ident := 4;
6378 
6379   insert into mst_mast_sum_report_temp_gt
6380   ( view_id
6381   , plan_id
6382   , supplier_name
6383   , orders
6384   , weight
6385   , volume
6386   , pieces
6387   , mtl_count
6388   , dtl_count
6389   , ltl_count
6390   , parcel_count
6391   , tl_cost
6392   , ltl_cost
6393   , parcel_cost
6394   , total_cost
6395   , cost_per_unit_weight
6396   , cost_per_unit_volume
6397   , percent_value
6398   , percent_alloc_cost
6399   )
6400   (SELECT 7
6401   , mp.plan_id
6402   , hz.party_name
6403   , mst_reports_pkg.get_orders_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,pov.vendor_id)
6407   , mst_reports_pkg.get_MTL_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,pov.vendor_id)
6404   , mst_reports_pkg.get_weight_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,pov.vendor_id)
6405   , mst_reports_pkg.get_volume_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,pov.vendor_id)
6406   , mst_reports_pkg.get_pieces_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,pov.vendor_id)
6408   , mst_reports_pkg.get_DTL_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,pov.vendor_id)
6409   , mst_reports_pkg.get_trips_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'LTL',l_c_s_ident,pov.vendor_id)
6410   , mst_reports_pkg.get_trips_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'PARCEL',l_c_s_ident,pov.vendor_id)
6411   , mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'TRUCK',l_c_s_ident,pov.vendor_id)
6412   , mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'LTL',l_c_s_ident,pov.vendor_id)
6413   , mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'PARCEL',l_c_s_ident,pov.vendor_id)
6414   , mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'TRUCK',l_c_s_ident,pov.vendor_id)
6415    +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'LTL',l_c_s_ident,pov.vendor_id)
6416    +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'PARCEL',l_c_s_ident,pov.vendor_id)
6417   , ( mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'TRUCK',l_c_s_ident,pov.vendor_id)
6418      +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'LTL',l_c_s_ident,pov.vendor_id)
6419      +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'PARCEL',l_c_s_ident,pov.vendor_id)
6420 	) / decode( mst_reports_pkg.get_weight_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,pov.vendor_id)
6421 	            ,0 ,1
6422 	            ,mst_reports_pkg.get_weight_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,pov.vendor_id))
6423   , ( mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'TRUCK',l_c_s_ident,pov.vendor_id)
6424      +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'LTL',l_c_s_ident,pov.vendor_id)
6425      +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'PARCEL',l_c_s_ident,pov.vendor_id)
6426 	) / decode( mst_reports_pkg.get_volume_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,pov.vendor_id)
6427 	            ,0 ,1
6428                     ,mst_reports_pkg.get_volume_c_s(p_plan_id,p_report_for,p_report_for_id,l_c_s_ident,pov.vendor_id))
6429   , (( mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'TRUCK',l_c_s_ident,pov.vendor_id)
6430       +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'LTL',l_c_s_ident,pov.vendor_id)
6431       +mst_reports_pkg.get_cost_per_mode_c_s(p_plan_id,p_report_for,p_report_for_id,'PARCEL',l_c_s_ident,pov.vendor_id)
6432      ) / decode( nvl(mst_reports_pkg.get_plan_value(p_plan_id,p_report_for,p_report_for_id),0)
6433                  , 0, 1
6434 		 , mst_reports_pkg.get_plan_value(p_plan_id,p_report_for,p_report_for_id)) ) * 100
6435   ,0
6436   from mst_plans mp
6437      , po_vendors pov
6438      , hz_relationships hzr
6439      , hz_parties hz
6440   WHERE mp.plan_id = p_plan_id
6441   and pov.vendor_id = hzr.subject_id
6442   and hzr.object_id = hz.party_id
6443   and hzr.relationship_type = 'POS_VENDOR_PARTY'
6444   and pov.vendor_id in (select mdd.supplier_id
6445                         from  mst_delivery_details mdd
6446                         where mdd.plan_id = mp.plan_id)
6447   and decode ( p_report_for, 4, pov.vendor_id , p_report_for_id ) = p_report_for_id
6448   );
6449 
6450 
6451 exception
6452   when others then
6453     null;
6454 
6455 END Populate_Master_Summary_GTT;
6456 
6457 
6458 function get_freight_classes_per_order (p_plan_id              in number
6459                                       , p_source_header_number in number)
6460 return varchar2 is
6461   l_freight_classes_str varchar2(500);
6462   l_freight_class       varchar2(40);
6463 
6464   cursor cur_freight_classes(l_plan_id              in number
6465                            , l_source_header_number in number) is
6466   select distinct mc.segment2
6467   from mtl_category_sets mcs
6468   , mtl_categories mc
6469   , mst_delivery_details mdd
6470   where mcs.structure_id = mc.structure_id
6471   and UPPER(mcs.category_set_name) = 'WSH_COMMODITY_CODE'    --'FREIGHT CLASS'
6472   and mc.category_id = mdd.commodity_code_cat_id             --mdd.freight_class_cat_id
6473   and mdd.plan_id = l_plan_id
6474   and mdd.source_header_number = l_source_header_number;
6475 
6476 begin
6477   open cur_freight_classes(p_plan_id, p_source_header_number);
6478   --will loop maximum 10 times to fetch 10 records. This is because the HLD puts an upper bound on this
6479   for i in 1..10 loop
6480     fetch cur_freight_classes into l_freight_class;
6481     exit when cur_freight_classes%NOTFOUND;
6482     l_freight_classes_str := l_freight_classes_str||' '||l_freight_class;
6483   end loop;
6484   close cur_freight_classes;
6485 
6486   return l_freight_classes_str;
6487 exception
6488   when others then
6489     null;
6490 end get_freight_classes_per_order;
6491 
6492 /*
6493  * To calculate the wait time at a particular stop for a given trip in a given plan
6494  */
6495 FUNCTION get_wait_time_at_stop (   p_plan_id 	IN NUMBER
6496                                  , p_trip_id 	IN NUMBER
6497                                  , p_stop_id 	IN NUMBER )
6498 RETURN VARCHAR2 IS
6499 
6500   CURSOR cur_wait_time (   l_plan_id 	IN NUMBER
6501                          , l_trip_id 	IN NUMBER
6502                          , l_stop_id 	IN NUMBER ) IS
6503   SELECT mts2.planned_arrival_date   planned_arrival_date,
6504          mts1.planned_departure_date planned_departure_date,
6505          mts1.drv_time_to_next_stop  drv_time_to_next_stop,
6506          mts1.total_layover_duration total_layover_duration
6507   FROM mst_trip_stops mts1
6508        ,mst_trip_stops mts2
6509   WHERE mts1.plan_id = l_plan_id
6510   AND   mts1.trip_id = l_trip_id
6511   AND   mts1.stop_id = l_stop_id
6512   AND   mts2.plan_id = mts1.plan_id
6513   AND   mts2.trip_id = mts1.trip_id
6514   AND   mts2.stop_sequence_number = ( SELECT MIN( mts3.stop_sequence_number )
6515                                       FROM mst_trip_stops mts3
6516                                       WHERE mts3.plan_id = mts1.plan_id
6517                                       AND  mts3.trip_id = mts1.trip_id
6518                                       AND  mts3.stop_sequence_number > mts1.stop_sequence_number );
6519  l_rec_wait_time cur_wait_time%ROWTYPE;
6520  l_wait_time NUMBER;
6521  l_wait_time_str VARCHAR2(100);
6522 BEGIN
6523 
6524   OPEN cur_wait_time ( p_plan_id, p_trip_id, p_stop_id );
6525   FETCH cur_wait_time INTO l_rec_wait_time;
6526   IF cur_wait_time%NOTFOUND THEN
6527     l_wait_time_str := '00:00';
6528   ELSE
6529     l_wait_time :=  NVL ( ( l_rec_wait_time.planned_arrival_date -
6530                             l_rec_wait_time.planned_departure_date ) * 24, 0 )-
6531                     NVL ( l_rec_wait_time.drv_time_to_next_stop, 0 )          -
6532                     NVL ( l_rec_wait_time.total_layover_duration, 0 )           ;
6533     IF l_wait_time < 0 THEN
6534         l_wait_time := 0;
6535     END IF;
6536     l_wait_time_str:= mst_wb_util.get_hr_min(l_wait_time);
6537   END IF;
6538   CLOSE cur_wait_time;
6539 
6540   RETURN l_wait_time_str;
6541 
6542 EXCEPTION
6543   WHEN OTHERS THEN
6544     l_wait_time_str := '00:00';
6545     RETURN l_wait_time_str;
6546 END get_wait_time_at_stop;
6547 
6548 END MST_REPORTS_PKG;