select
        vp.voyage_id,
        FROM_PORT_ID,
        coalesce(to_char(vah.VESSEL_ARRIVED_HARBOUR,
        'dd/mm/yyyy hh24:mi:ss'),
        '') as VESSEL_ARRIVED_HARBOUR ,
        coalesce(to_char(vah.BERTHED,
        'dd/mm/yyyy hh24:mi:ss'),
        '') as BERTHED ,
        coalesce(to_char(VESSEL_SAILED,
        'dd/mm/yyyy hh24:mi:ss'),
        '') as VESSEL_SAILED,
        coalesce(to_char(vdh.unberthed,
        'dd/mm/yyyy hh24:mi:ss'),
        '') as unberthed ,
        coalesce(to_char(vah.pilot_on_board,
        'dd/mm/yyyy hh24:mi:ss'),
        '') as pob ,
        coalesce(vah.BERTH_NO,
        '0') as BERTH_NO,
        coalesce(STAY_HRS + STAY_HRS_CON,
        '0') as Port_Stay,
        coalesce(vpe.PORT_DUES,
        '0') as PORT_DUES,
        case
            when vp.FROM_PORT_ID in ('IRBND', 'IRZBR')
        and (vessel_optr not in ('PAP')) then 0
        else coalesce(a.total,
            0)+ coalesce(b.total,
            0)
    end as AGENCY_CHARGES ,
        coalesce(vdh.MIS_COST,
        '0') as MIS_COST,
        coalesce(PORT_TARIFF_USD + coalesce(a.total,
        0)+ coalesce(b.total,
        0)+ coalesce(vdh.MIS_COST,
        '0')+ ITT.total_tues + 
      case
            when FROM_PORT_ID is not null then coalesce((
        select
                sum(coalesce(cost, 0)) as thccost
        from
                cal_thc_cost(vp.VOYAGE_ID,
                from_PORT_id,
                vp.PORT_SEQUENCE::text)
        group by
                port_sequence,
                from_PORT_id,
                voyage_id ),
            0)
        else 0
    end ,
        '0') as total ,
        coalesce(vdh.TOTAL_MOVES,
        '0') as TOTAL_MOVES,
        vp.PORT_SEQUENCE ,
        coalesce(hours,
        '0') as hours,
        coalesce(no_of_cranes,
        '0') as no_of_cranes ,
        export_moves + discharge_moves as LOADING_MOVES ,
        coalesce(AGENCY_TARIFF_USD,
        '0') as AGENCY_TARIFF,
        coalesce(PORT_TARIFF_USD,
        '0') as PORT_TARIFF ,
        (
    select
            status
    from
            thc_status(vp.VOYAGE_ID,
            from_PORT_id,
            vp.PORT_SEQUENCE::text)) as thc_status,
        case
            when FROM_PORT_ID is not null then coalesce((
        select
                sum(coalesce(cost, 0)) as thccost
        from
                cal_thc_cost(vp.VOYAGE_ID,
                from_PORT_id,
                vp.PORT_SEQUENCE::text)
        group by
                port_sequence,
                from_PORT_id,
                voyage_id ),
            0)
        else 0
    end as thccost,
        ITT.total_tues as totalTues
from
        voyage_port vp
left outer join vessel_arrival_hdr vah on
        vp.voyage_id = vah.voyage_id
    and vp.PORT_SEQUENCE :: integer = VAH.PORT_SEQUENCE :: integer
left outer join view_departure vdh on
        vp.voyage_id = vdh.voyage_no
    and vp.port_sequence :: integer = vdh.PORT_SEQUENCE :: integer
left outer join VIEW_PORT_EXP vpe on
        vp.voyage_id = vpe.voyage_id
    and vp.PORT_SEQUENCE :: integer = vpe.PORT_SEQUENCE :: integer
left outer join vw_loading_moves('CHIE-0031') r on
        vp.voyage_id = r.VOYAGE_ID
    and vp.PORT_SEQUENCE :: integer = r.PORT_SEQUENCE :: integer
left outer join fn_itt_total_counts_rvc('CHIE-0031') ITT on
        ITT.voyage_id = vp.voyage_id
    and ITT.PORT = vp.FROM_PORT_ID
    and ITT.PORT_SEQUENCE = vp.PORT_SEQUENCE
left outer join VIEW_PORT_AGENCY_COST vv on
        vp.voyage_id = vv.voyage_id
    and vp.PORT_SEQUENCE :: integer = vv.PORT_SEQ :: integer
left outer join agency_tariff_calc A on
        vp.voyage_id = A.VOYAGE
    and vp.PORT_SEQUENCE :: integer = A.PORT_SEQUENCE :: integer
left outer join agency_tariff_calc b on
        a.voyage = b.next_voy
    and a.port = b.next_port
    and a.port_sequence = b.next_seq
left outer join voyage v on
        v.voyage_id = A.VOYAGE
where
        vp.voyage_id = 'CHIE-0031'
    and vp.IS_NEXT_VOYAGE = 'N'

The above query shows

SQL Error [0A000]: ERROR: set-returning functions are not allowed in CASE
Hint: You might be able to move the set-returning function into a LATERAL FROM item.
Where: SQL function “cal_thc_cost” during startup

I know that the error lies in the below part

 case
            when FROM_PORT_ID is not null then coalesce((
        select
                sum(coalesce(cost, 0)) as thccost
        from
                cal_thc_cost(vp.VOYAGE_ID,
                from_PORT_id,
                vp.PORT_SEQUENCE::text)
        group by
                port_sequence,
                from_PORT_id,
                voyage_id ),
            0)
        else 0
    end 

Please correct me this query