# -*- encoding: utf8 -*-
import sys
import datetime
reload(sys)
sys.setdefaultencoding("utf-8")

# 取小数点 前两位
def get_2_flo(num):
    begin = str(num).find('.')
    if begin == -1:
        return round(begin, 2)
    return float(str(num)[:begin + 3])

#四舍五入
def get_2_round(num):
    begin = str(num).find('.')
    if begin == -1:
        return round(begin, 2)
    else:
        if str(num).__len__() - begin > 3:
            dot_3=int(str(num)[begin+3])
            if dot_3 >= 5:
                return get_2_flo(num)+0.01
            else:
                return get_2_flo(num)
        else:
            return float(str(num)[:begin + 3])


# 应还罚息计算
def cal_due_amount(deadline,repaid_at,require_fee,is_new=True):
    due_days=(repaid_at - deadline).days
    print '----逾期天数-----',due_days
    due_rate=0.001
    if due_days <= 3:
        due_rate = 0
    elif due_days >= 4 and due_days <= 7:
        due_rate = 0.0005
    elif due_days >= 8 and due_days <= 15:
        due_rate = 0.001
    elif due_days >= 16 and due_days <= 30:
        due_rate = 0.003
    elif due_days >= 31 :
        due_rate = 0.005
    #旧版本罚息公式
    if is_new == False:
        due_rate = 0.001
        return get_2_round(get_2_round(max(due_rate * require_fee,0.01)) * due_days)
    return get_2_round(due_rate * require_fee * due_days)



if __name__ == '__main__':
    deadline=datetime.date(2017,6,1)
    repaid_at=datetime.date(2017,10,5)
    require_fee=1012.34
    punish=cal_due_amount(deadline,repaid_at,require_fee)
    print '----罚息----',punish