This can easily be solved using Windowed Aggregates, supported in Firebird 3:
select *
,case when cumulative_qty <= sold_qty then qty
when sold_qty-(cumulative_qty - qty) < 0 then 0
else sold_qty-(cumulative_qty - qty)
end as qty_sold
,case when cumulative_qty <= sold_qty then qty
when sold_qty-(cumulative_qty - qty) < 0 then 0
else sold_qty-(cumulative_qty - qty)
end * price as value_sold
from
(
select *
,150 as sold_qty
-- cumulative sum of quantity in stock (FIFO)
,sum(qty) over (order by date rows unbounded preceding) as cumulative_qty
from tab
) as dt
;
Now you can calculate the sum:
select
sum(case when cumulative_qty <= sold_qty then qty
when sold_qty-(cumulative_qty - qty) < 0 then 0
else sold_qty-(cumulative_qty - qty)
end) as qty_sold
,sum(case when cumulative_qty <= sold_qty then qty
when sold_qty-(cumulative_qty - qty) < 0 then 0
else sold_qty-(cumulative_qty - qty)
end * price) as value_sold
from
(
select *
,150 as sold_qty
-- cumulative sum of quantity in stock (FIFO)
,sum(qty) over (order by date rows unbounded preceding) as cumulative_qty
from tab
) as dt
;
See db<>fiddle (using Postgres, but Firebird syntax should be the same)