Here’s the code. Basically, the user selects a billing day (the 1st of each month, or the 15th of each month). The start_date is when the “contract” begins, the expire_date is when it expires.
So, if today is the 3rd, and they want to be billed on the 15th, then simply go to the 15th day of the current month. However, if today is the 3rd and they want to be billed on the first, then get the 1st day of next month… etc.
if params[:billing_day] == 1 && start_date.day > 1
expire_date = start_date.at_beginning_of_month.next_month
elsif params[:billing_day] == 15 && start_date.day < 15
expire_date = start_date.change(:day => 15)
elsif params[:billing_day] == 15 && start_date.day > 15
expire_date = start_date.at_beginning_of_month.next_month.change(:day => 15)
else
expire_date = start_date.change(:day => params[:billing_day])
end
It just seems crazy, surely it can be simplified in Rails. Thanks!
I would write something along the lines of
You’d need to validate that a valid billing day was picked before this