banque
This commit is contained in:
@@ -196,7 +196,7 @@ end;
|
||||
$$;
|
||||
|
||||
|
||||
create procedure transfer(
|
||||
create or replace procedure transfer(
|
||||
p_source int,
|
||||
p_destination int,
|
||||
p_amount numeric,
|
||||
@@ -206,23 +206,39 @@ language plpgsql
|
||||
as $$
|
||||
declare
|
||||
v_transaction_id int;
|
||||
rate_source numeric;
|
||||
rate_destination numeric;
|
||||
begin
|
||||
insert into transaction(amount, transaction_date)
|
||||
values (p_amount, p_date)
|
||||
returning id into v_transaction_id;
|
||||
|
||||
select rate into rate_source
|
||||
from exchange_rate er
|
||||
inner join account a
|
||||
on a.currency_code = er.currency_code
|
||||
where id = p_source and date < p_date
|
||||
order by date desc limit 1;
|
||||
|
||||
select rate into rate_destination
|
||||
from exchange_rate er
|
||||
inner join account a
|
||||
on a.currency_code = er.currency_code
|
||||
where id = p_destination and date < p_date
|
||||
order by date desc limit 1;
|
||||
|
||||
insert into operation (transaction_id, account_id, amount, direction)
|
||||
values (v_transaction_id, p_source, p_amount, 'DEBIT');
|
||||
|
||||
insert into operation (transaction_id, account_id, amount, direction)
|
||||
values (v_transaction_id, p_destination, p_amount, 'CREDIT');
|
||||
values (v_transaction_id, p_destination, p_amount / rate_source * rate_destination, 'CREDIT');
|
||||
|
||||
update account
|
||||
set balance = balance - p_amount
|
||||
where id = p_source;
|
||||
|
||||
update account
|
||||
set balance = balance + p_amount
|
||||
set balance = balance + round(p_amount / rate_source * rate_destination, 2)
|
||||
WHERE id = p_destination;
|
||||
end;
|
||||
$$;
|
||||
|
||||
Reference in New Issue
Block a user