SELECT contract,
       EXTRACT(YEAR FROM DATE_CREATED) Year,
       EXTRACT(MONTH FROM DATE_CREATED) Period,
       IFSAPP.CUSTOMER_ORDER_API.Get_Customer_No(SOURCE_REF1) "Customer",
       IFSAPP.Cust_Ord_Customer_API.Get_Name(CUSTOMER_ORDER_API.Get_Customer_No(SOURCE_REF1)) "Customer Name",
       PART_NO,
       IFSAPP.Inventory_Part_API.Get_Description(CONTRACT, PART_NO) "Part Description",
       IFSAPP.INVENTORY_PART_API.Get_Part_Product_Family(CONTRACT, PART_NO) "Product Family",
       SUM(QUANTITY) as Quantity,
      TO_CHAR(SUM((Customer_Order_Line_API.Get_Base_Sale_Price_Total(source_ref1,
                                                 source_ref2,
                                                 source_ref3,
                                                 source_ref4) /
       (Customer_Order_Line_API.Get_Buy_Qty_Due(source_ref1,
                                                 source_ref2,
                                                 source_ref3,
                                                 source_ref4))) * Quantity),'999,999,999,999,999.99','NLS_NUMERIC_CHARACTERS=",."') as "Late Shipment $ MDT",              


       
       COUNT(HANDLING_UNIT_ID) as Pallets
  FROM INVENTORY_TRANSACTION_HIST2
 WHERE TRANSACTION_CODE = 'OESHIP'
AND (TRUNC(DATE_CREATED) >= to_date('&VALID_FROM','MM/DD/YYYY') AND
       TRUNC(DATE_CREATED) <= to_date('&VALID_TO','MM/DD/YYYY'))
   AND CONTRACT LIKE NVL('&Site', '%')
   AND PART_NO LIKE nvl('&PartNo', '%')
   AND (IFSAPP.CUSTOMER_ORDER_API.Get_Customer_No(SOURCE_REF1) is null or
       IFSAPP.CUSTOMER_ORDER_API.Get_Customer_No(SOURCE_REF1) LIKE
       NVL('&CustomerNo', '%'))
   AND (IFSAPP.INVENTORY_PART_API.Get_Part_Product_Family(CONTRACT, PART_NO) is null or
       IFSAPP.INVENTORY_PART_API.Get_Part_Product_Family(CONTRACT, PART_NO) LIKE
       NVL('&PartProductFamily', '%'))
       and (Customer_Order_Line_API.Get_Planned_Delivery_Date(source_ref1,
                                                 source_ref2,
                                                 source_ref3,
                                                 source_ref4))  <=   (DATE_CREATED + CUSTOMER_ORDER_API.Get_Delivery_Leadtime(source_ref1) - 1)                                    
                                                 
                                             

 GROUP BY PART_NO,
          contract,
          EXTRACT(YEAR FROM DATE_CREATED),
          EXTRACT(MONTH FROM DATE_CREATED),
          IFSAPP.CUSTOMER_ORDER_API.Get_Customer_No(SOURCE_REF1),
          IFSAPP.Cust_Ord_Customer_API.Get_Name(CUSTOMER_ORDER_API.Get_Customer_No(SOURCE_REF1)),
          IFSAPP.Inventory_Part_API.Get_Description(CONTRACT, PART_NO),
          IFSAPP.INVENTORY_PART_API.Get_Part_Product_Family(CONTRACT,
                                                            PART_NO)

 ORDER BY EXTRACT(YEAR FROM DATE_CREATED),
          EXTRACT(MONTH FROM DATE_CREATED),
          PART_NO,
          CONTRACT