Beispiel #1
0
    def interest(self, statis_date, withdraw_amount, rate_history):

        if 0 == self._amount:
            return 0.0

        diff_amount = withdraw_amount if withdraw_amount <= self._amount else self._amount
        assert diff_amount >= 0, "diff_amount = %d is less than 0 " % diff_amount

        # 不同月份的自然月天数
        days_3m = (toolkit.add_months_obj(self._start_date, 3) - self._start_date).days
        days_6m = (toolkit.add_months_obj(self._start_date, 6) - self._start_date).days
        days_9m = (toolkit.add_months_obj(self._start_date, 9) - self._start_date).days
        days_12m = (toolkit.add_months_obj(self._start_date, 12) - self._start_date).days

        n = (statis_date - self._start_date).days
        assert 0 <= n <= days_12m, "n=%d is out of one year, statis_date=%s and deposit=%s" % (n, statis_date, self)

        # 详细的分段计算公式,参考文档:<home>/doc/利息计算公式.docx
        earn_interest = diff_amount
        r = rate_history.get_rates(self._start_date)
        if 0 <= n < 7:
            earn_interest *= r['day_1'] * n / Y
        elif 7 <= n < days_3m:
            earn_interest *= r['day_7'] * n / Y
        elif days_3m <= n < days_3m + 7:
            earn_interest *= 0.25 * r['month_3'] + r['day_1'] * (n - days_3m) / Y
        elif days_3m + 7 <= n < days_6m:
            earn_interest *= 0.25 * r['month_3'] + r['day_7'] * (n - days_3m) / Y
        elif days_6m <= n < days_6m + 7:
            earn_interest *= 0.5 * r['month_6'] + r['day_1'] * (n - days_6m) / Y
        elif days_6m + 7 <= n < days_9m:
            earn_interest *= 0.5 * r['month_6'] + r['day_7'] * (n - days_6m) / Y
        elif days_9m <= n < days_9m + 7:
            earn_interest *= 0.25 * r['month_3'] + 0.5 * r['month_6'] + r['day_1'] * (n - days_9m) / Y
        elif days_9m + 7 <= n < days_12m:
            earn_interest *= 0.25 * r['month_3'] + 0.5 * r['month_6'] + r['day_7'] * (n - days_9m) / Y
        elif n == days_12m:
            earn_interest *= r['year_1']
        else:
            raise Exception("never thrown")

        return earn_interest
Beispiel #2
0
    def _cycle(self, storage_date):

        if not self._all_deposit:
            return 0.0  # 空,直接返回

        one_year_ago = toolkit.add_months_obj(storage_date, -12)
        if one_year_ago < self._earliest_date():
            return 0.0  # 没有满一年,直接返回

        # 满一年,
        # 1 计算利息
        # 2 取完账户的所有存款
        # 3 存到明天的账户
        earn = 0.0
        if one_year_ago in self._all_deposit:
            earn = self._all_deposit[one_year_ago].interest_all(storage_date, self._rate_history)
            all_inside = self._all_deposit[one_year_ago].withdraw_all()
            tomorrow_deposit = Deposit(storage_date + timedelta(days=1), all_inside)
            self._save(tomorrow_deposit)

        return earn