Beispiel #1
0
  def get(self):
    accounts_query = SavingsAccount.query()
    accounts = accounts_query.fetch(100)
    for account in accounts:
      today = util.getTodayForTimezone(account.timezone_name)
      yesterday = today + timedelta(days=-1)
      # The transaction time must be in UTC, but that really means it can't have a tzinfo.
      yesterday_transaction_time = datetime(
          yesterday.year,
          yesterday.month,
          yesterday.day,
          23, 59, 59, 999999, pytz.timezone(account.timezone_name)).astimezone(pytz.UTC).replace(tzinfo=None)
      logging.info('Transaction time is %s', yesterday_transaction_time)
      should_schedule_interest_payment = False
      if account.interest_compound_frequency == 'weekly':
        days_between = yesterday - account.getOpenDate()
        if days_between.days > 0 and days_between.days % 7 == 0:
          should_schedule_interest_payment = True
        else:
          logging.info('Not the right day to schedule weekly interest payment for %s', account.child_first_name)
      else:
        if yesterday > account.open_datetime and yesterday.day == account.open_datetime.day:
          should_schedule_interest_payment = True
        else:
          logging.info('Not the right day to schedule monthly interest payment for %s', account.child_first_name)

      if should_schedule_interest_payment:
        logging.info('It is the right day to pay interest for %s', account.child_first_name)
        if not AccountTransaction.hasInterestForDate(account, transaction_date=yesterday):
          interest_amount = int(account.calculateBalance(max_time=yesterday_transaction_time) * (account.interest_rate / 100))
          interest_transaction = AccountTransaction(parent=account.key)
          interest_transaction.savings_account = account.key
          interest_transaction.transaction_type = 'interest'
          interest_transaction.amount = interest_amount
          interest_transaction.transaction_time = yesterday_transaction_time
          interest_transaction.transaction_local_date = yesterday
          interest_transaction.put()
          task = taskqueue.add(
              url='/send_transaction_email',
              params={
                  'account': account.key.urlsafe(),
                  'transaction': interest_transaction.key.urlsafe(),
                  })
          logging.info('Interest payment %0.2f processed for %s', (interest_amount / 1000000.0), account.child_first_name)
        else:
          logging.info('Interest payment already processed for %s', account.child_first_name)