use telekom
go

create procedure sp_uplata
(
	@idRacuna int
)
as
begin
	declare @idKorisnika int, @dug decimal(10,2)

	update racuni
	set datumUplate = GETDATE()
	where id = @idRacuna

	select @idKorisnika=idKorisnika
	from racuni
	where id = @idRacuna

	select @dug=sum(r.iznos)
	from racuni r
	where r.idKorisnika = @idKorisnika
	and r.datumUplate is null

	if @dug is null
	begin
		delete from tuzbe
		where idKorisnika = @idKorisnika
	end
end
go
