def do_fix_rental_plans(self): today = datetime.datetime.now().date() def calc_next_payment_date(start_date, period): d = start_date.date() if isinstance(start_date, datetime.datetime) else start_date while d < today: d = inc_months(d, period, start_date) return d debug("Fixing rental plans...") qs = MemberRentalPlan.objects.filter(status=RentalPlanStatus.Active) debug("Going to fix %d of plans...", qs.count()) for p, i in itertools.izip(qs, itertools.count(1)): if i % 100 == 0: debug(i) expire_in = RentalPlan.get_expire_in(p.plan) if expire_in: p.expiration_date = inc_months(p.start_date, expire_in, p.start_date) p.next_payment_amount = None p.next_payment_date = None if p.status == RentalPlanStatus.Active and p.expiration_date < today: p.set_status(RentalPlanStatus.CanceledP, save=False) else: p.expiration_date = None _pd, amount = RentalPlan.get_next_payment(p.plan, p.start_date, datetime.datetime.now()) reccuring_period = RentalPlan.get_reccuring_period(p.plan) p.next_payment_amount = amount p.next_payment_date = calc_next_payment_date(p.start_date, reccuring_period) p.save()
def calc_next_payment_date(start_date, period): d = start_date.date() if isinstance(start_date, datetime.datetime) else start_date while d < today: d = inc_months(d, period, start_date) return d