Custom function that Gives total sales of an item in the last 30 days

Custom function that Gives total sales of an item in the last 30 days

Hi,
I would like to have a custom field that is updated by a custom function.

The field "widget 1 sales" - total sales (Paid amount) of "widget 1" in the last 30 days.

The function - 
everytime "widget 1" has been paid on an invoice update "widget 1 sales" field on that customers account with the dollar amount in the last 30 days for that specific item.

Any help would be appreciated. I have been reading through the custom function tutorials but I don't see anything similar.