I have a system which based on what a user spent within a day they get bonus cash back. In essence for each $500 spent they get a $25 in a bonus wallet. If the amount is simple for example $1000 then its easy and we give $50. The problem arises if the user spent on visit A $400 whereas they get nothing. but on visit B they spent $600 which between those 2 visits they spent $1000 and considering that they did not get a bonus on visit A they should get $50 (2*25) on visit B. Keep in mind that the user can spend what ever they earn on their bonus wallet whenever they want within the day. I have attached my screenshot where I would like to find a way to programmatically do this calculation regarding the bonus. Any help is much appreciated.