Select PP.cf$_purchase_cat1_desc Purch_Category1_Desc,
       IP.Part_NO,
       IP.Note_Text Legacy,
       IP.DESCRIPTION,
       IP.Contract Site,
       INV.Warehouse,
       sum(SMA.QTY_REQUIRED) Qty_required,
       nvl(INV.QtyOnHand, 0) "Qty on Hand",
       nvl(INV.QtyAvailable, 0) "Qty Available"

  From SHOP_MATERIAL_ALLOC_uiv SMA
 inner join INVENTORY_PART IP
    on SMA.PART_NO = IP.PART_NO
   and SMA.CONTRACT = IP.CONTRACT
  left join PURCHASE_PART_cfv PP
    on IP.Part_No = PP.Part_No
   and IP.Contract = PP.Contract

  left join (select PART_NO,
                    CONTRACT,
                    Warehouse,
                    sum(QTY_ONHAND) QtyOnHand,
                    sum(QTY_ONHAND) - sum(QTY_RESERVED) QtyAvailable
               from Inventory_Part_in_Stock_uiv
              where AVAILABILITY_CONTROL_ID is null
              Group By Contract, PART_NO, CONTRACT, Warehouse) INV
    on SMA.Part_No = INV.PART_NO
   and SMA.Contract = INV.Contract

 Where IP.CONTRACT LIKE '%&CONTRACT%'
   and report_sys.parse_parameter(PP.cf$_purchase_part_cat_1,
                                  '&[-C-BL]PurchasedCategory') = 'TRUE'
   and SMA.State not in ('Cancelled', 'Closed', 'Planned')
   and trunc(SMA.DATE_REQUIRED) between TO_DATE('&DATE_FROM', 'YYYY-MM-DD') AND
       TO_DATE('&DATE_TO', 'YYYY-MM-DD')

 Group by IP.Part_NO,
          PP.cf$_purchase_cat1_desc,
          IP.Note_Text,
          IP.Contract,
          INV.Warehouse,
          IP.DESCRIPTION,
          INV.QtyOnHand,
          INV.QtyAvailable

Having sum(SMA.QTY_REQUIRED) > 0

Order by IP.Part_NO

-- -- Lov reference is removed due to limitations in IFS cloud