SELECT s.contract,
       s.DEPARTMENT_NO AS "DEPARTMENT",
       s.MACHINE_NO AS "MACHINE",
       s.PART_NO AS "PART NO",
       ifsapp.Inventory_Part_API.Get_Description(s.contract, s.part_no) AS "ITEM DESCRIPTION",
       s.ORDER_NO || '-' || s.Release_no || '-' || s.Sequence_no || '-1' AS "LOT BATCH",
       ifsapp.Shop_Order_Operation_API.Get_Remaining_Qty(s.order_no,
                                                         s.release_no,
                                                         s.sequence_no,
                                                         s.operation_no) AS "SO REMAINING QTY",
       CASE
         WHEN HU.max_quantity_capacity IS NOT NULL THEN
          round(ifsapp.Shop_Order_Operation_API.Get_Remaining_Qty(s.order_no,
                                                                  s.release_no,
                                                                  s.sequence_no,
                                                                  s.operation_no) /
                HU.max_quantity_capacity,
                1)
         ELSE
          0
       END AS "SO REMAINING HU",
       HU.Handling_Unit_type_id AS "HANDLING UNIT"
       --,ifsapp.Shop_Order_Operation_API.Get_Remaining_Mach_Hours(s.Order_no,s.Release_no,s.Sequence_no,s.Operation_no) Remaning_Hrs
       ,
       m.PART_NO AS "COMPONENT",
       i.Description AS "COMPONENT DESCRIPTION",
       ROUND(nvl(inv.Inv_Floor_Stock, 0), 3) AS "INV FLOOR STOCK",
       ROUND(m.Qty_Remaining, 3) AS "REMAINING QTY REQUIRED PER LOT/BATCH",
       ROUND(ifsapp.Shop_Order_Operation_API.Get_Remaining_Mach_Hours(s.Order_no,
                                                                      s.Release_no,
                                                                      s.Sequence_no,
                                                                      s.Operation_no),
             3) AS "TOTAL SO HOURS",
       CASE
         WHEN &Begin_within_x_hours >=
              ifsapp.Shop_Order_Operation_API.Get_Remaining_Mach_Hours(s.Order_no,
                                                                       s.Release_no,
                                                                       s.Sequence_no,
                                                                       s.Operation_no) THEN
          ROUND(m.Qty_Remaining, 3)
         ELSE
          ROUND(&Begin_within_x_hours * m.Qty_per_assembly *
                s.mach_run_factor,
                3) -- check this line for the error
       END AS "ESTIMATED REQUIRED INVENTORY FOR TIME PERIOD",
       ROUND(NVL(qty.total_remaining_qty_component, 0), 3) AS "REMAIN QTY REQUIRED PER COMPONENT",
       round(qty.total_remaining_qty_component -
             nvl(inv.Inv_Floor_Stock, 0),
             3) AS "TOTAL QTY TO STAGE PER COMPONENT"
       /*,m.QTY_Per_Assembly  AS  "QTY REQUIRED COMPONENT" */,
       to_char(s.OP_START_DATE, 'YYYY-MM-DD') AS "OP START DATE",
       to_char(s.OP_FINISH_DATE, 'YYYY-MM-DD') AS "OP FINISH DATE",
       i.type_Code AS "TYPE CODE",
       i.ACCOUNTING_GROUP AS "ACCOUNTING CODE",
       i.PART_PRODUCT_CODE AS "PRODUCT CODE",
       i.PART_PRODUCT_FAMILY AS " PROD FAMILY",
       i.CF$_PURCHASE_CAT2 AS "CAT2"

  From ifsapp.SHOP_ORDER_OPERATION_JOIN_cfv s
--left join ifsapp.PART_HANDLING_UNIT h on s.part_no=h.part_no and  (h.HANDLING_UNIT_TYPE_ID  like 'PALLET%'  or  h.HANDLING_UNIT_TYPE_ID ='BULK-VQR' )
 inner join ifsapp.SHOP_MATERIAL_ALLOC_UIV m
    on s.Order_no = m.order_no
   and s.Release_no = m.release_no
   and s.Sequence_no = m.sequence_no
  left join ifsapp.inventory_part_cfv i
    on i.part_no = m.part_no
   and i.contract = m.contract
  left join (Select part_no, contract, sum(qty_onhand) Inv_Floor_Stock
               from ifsapp.INVENTORY_PART_IN_STOCK_UIV
              where LOCATION_TYPE in ('Floor stock')
              group by part_no, contract) inv
    on inv.contract = i.contract
   and inv.part_no = i.part_no
  left join (Select part_no,
                    contract,
                    sum(Qty_Remaining) AS total_remaining_qty_component
               from ifsapp.SHOP_MATERIAL_ALLOC_UIV
              where STATE in ('Started', 'Released', 'Reserved')
              group by part_no, contract) qty
    ON qty.part_no = m.part_no
   AND qty.contract = m.contract

 OUTER apply (SELECT *
                FROM ifsapp.PART_HANDLING_UNIT
               WHERE Part_no = s.part_no
                 and rownum < 2
                 AND Handling_unit_type_id <> 'CARTON') HU
 Where s.OPERATION_NO = '10'
   and s.STATE in ('Started', 'Released', 'Reserved')
   and i.type_code_db not in ('6')
   and ifsapp.Shop_Order_Operation_API.Get_Remaining_Qty(s.order_no,
                                                         s.release_no,
                                                         s.sequence_no,
                                                         s.operation_no) > 0
   and s.Contract = '&[MC--L]Contract'
   and s.department_no = UPPER('&[MC--L]Department')
   and s.OP_START_DATE < (sysdate + (&Begin_within_x_hours) / 24)
   and i.ACCOUNTING_GROUP like nvl('&Accounting_Group', '%')
   and i.CF$_PURCHASE_CAT2 like nvl('&CAT2', '%')
 Order By m.part_no,
          s.DEPARTMENT_NO,
          s.WORK_CENTER_NO,
          s.MACHINE_NO,
          s.OP_START_DATE
