Server side scripting

Hello everybody
I was trying to create a doctype that will be called rental which will take the value of item,warehouse and number. And what I struggle is to decreasing the amount of item specified on the rental doctype creation from the item found on the specified warehouse.