-2

I have a setup where I would like to send automatic reminders for users if they haven't made a monthly rental payment. The landlord has the opinion to define the day of the month on which the tenant should make the payment(due_date). What I would like to achieve is that if the payment hasn't been made in 2 days since the due_date the tenant would be sent an email. Currently, I have the following task for this.

namespace :rent_due_reminder do
    desc "Sends monthly reminders to tenants who haven't paid their rent."
    task :send => :environment do
      Offer.all.each do |offer|
        if offer.status == "accepted" && offer.due_date.present?
            landlord = User.find_by(id: offer.landlord_id)
            tenant = User.find_by(id: offer.user_id)
            stripe_account = Stripe::Account.retrieve(landlord.merchant_id)        
            payments = Stripe::Charge.list(
                {
                limit: 100,
                expand: ['data.source_transfer.source_transaction.dispute', 'data.application_fee'],
                source: {object: 'all'}
                },
                { stripe_account: landlord.merchant_id }
            )
            due_date = "#{Date.today.year}-#{Date.today.month}-#{offer.due_date}".to_datetime        
            last_payment_date = DateTime.strptime(payments.data.last.created.to_s,'%s')
        
            if (SOME CONDITION)
                OfferMailer.with(offer: offer, tenant: tenant, landlord: landlord).rent_due.deliver
            end            
        end     
      end
    end
  end

So what I'm doing is that I'm picking the last payment and getting the date of that and this is what I should use to condition the mailer. Initially, I thought that I could just look and see whether last_payment_date < due_date + 2, but this didn't seem to work. I might be getting confused about this since if I have multiple landlords and some of them have their due date for example 30th of every month and some of them have 1st of every month, should I still be running the task monthly or how should I set this up? Apologies for the reasonably bad explanation, I myself am getting very confused with this also.

Daniel
  • 297
  • 3
  • 10

1 Answers1

0

There is a flaw in your logic. Even if a user made a payment on the due date, the last_payment date would still be less than due_date+2. So you need to add one more check that ensures that the payment was made for the current month.

e.g. If the due date for my rent is the 5th of every month you need to check the payment was made between 1st and 5th. Currently you are only checking if the last payment date was before 7th. That includes valid payments too.

Of course the above logic is flawed too because people could make advance payments and you will need to correct for that too. One way around it is to create a bill and ensure that all bills are paid rather than simply checking for last payments. That way you can calculate delinquencies for more than one month.

TheGeorgeous
  • 3,927
  • 2
  • 20
  • 33