Operation Details
Description: Returns the percentage and estimated cost for an organization unit product case approval check with the lowest estimated cost.
Sterotype: ns
SQL
SELECT
  percentage,
  estimatedCost
INTO
  :percentage,
  :estimatedCost 
FROM
  CaseApprovalCheck,
  
  ( 
  SELECT
    DISTINCT(OrgUnitPositionLink.organisationUnitID) 
  FROM
    OrgUnitPositionLink,
    PositionHolderLink 
  WHERE PositionHolderLink.userName = :userName 
    AND OrgUnitPositionLink.positionID = PositionHolderLink.positionID 
    AND OrgUnitPositionLink.recordStatus = :statusCode 
    AND PositionHolderLink.recordStatus = :statusCode 
  ) OrgUnit 
WHERE CaseApprovalCheck.typeCode = :typeCode 
  AND CaseApprovalCheck.productID = :productID 
  AND CaseApprovalCheck.statusCode = :statusCode 
  AND 
  (
    CaseApprovalCheck.organisationUnitID IS NULL 
    OR CaseApprovalCheck.organisationUnitID = OrgUnit.organisationUnitID 
  ) 
  AND estimatedCost = 
  ( 
  SELECT
    MIN ( estimatedCost ) 
  FROM
    CaseApprovalCheck 
  WHERE CaseApprovalCheck.estimatedCost >= :estimatedCost 
    AND CaseApprovalCheck.typeCode = :typeCode 
    AND CaseApprovalCheck.productID = :productID 
    AND CaseApprovalCheck.statusCode = :statusCode 
    AND 
    (
      CaseApprovalCheck.organisationUnitID IS NULL 
      OR CaseApprovalCheck.organisationUnitID = OrgUnit.organisationUnitID 
    ) 
  )