保險交易計算


帳務格式:

===========
=== 美元 ===
===========


#存入世華美元: 共USD 
1030616: 500 台幣存
1030619: 4500 台幣存
1030820: 11800 台幣存
1040114: 5000 台幣存
1040126: 6300 台幣存
1040630: 5000 台幣存
1040818: 6000 台幣存
1040827: 5200 台幣存
1050119: 10900 台幣存
1050623: 1660 台幣存
1050706: 2980 台幣存
1050805: 22699 HSBC匯
1060125: 27980 HSBC匯
1070209: 27979 HSBC匯

===========
=== 台幣 ===
===========

#存入世華台幣: 共NTD 
1021221: 18379 結存
1030421: 1850000 匯入
1030421: 150000 國泰到期
1030502: 205529 國泰到期
1030618: 120000 匯入
1030912: 100000 匯入
1031117: 100000 匯入
1031230: 150000 匯入
1040113: 265000 現金
1040630: 150000 匯入
1040817: 150000 匯入
1050104: 150000 匯入
1050801: 145026 HSBC匯
1060315: 275000 現金
1061213: 100000 現金





import os, shutil, re

fname = '銀行存提紀錄.txt'
base_dir = '/Users/jerry/Desktop/mom/國泰保險'
filepath = os.path.join(base_dir, fname)

print('file:', filepath)

record_list = []

def print_record(list_in):
    for i in range(len(list_in)):
        record_dict = list_in[i]
        print("{}:{} {} {}".format(record_dict['date'], record_dict['money'], record_dict['currency'], record_dict['type']) )

def get_total_of_type(list_in, trans_type, currency=None):
    if currency is None:
        currency = "NTD"
    print("")
    print("==================================")
    total = int(0)
    for i in range(len(list_in)):
        record_dict = list_in[i]
        if ( (record_dict['type'] == trans_type) and (record_dict['currency'] == currency) ):
            print("{}:{}".format(record_dict['date'], record_dict['money']) )
            total += int(record_dict['money'])
    print("----------------------------------")
    print("{} 總金額 = {} {}".format(trans_type, total, currency))
    print("==================================")
    return total

    
        
with open(filepath) as fp:  
    line = fp.readline()
    line_cnt = 1
    while line:
        #parse
        if ( line[0] == '#' ): # a section begin
            # parse section's header
            x_list = line.split(': ')
            trans_type = x_list[0]
            #print("Type: ", trans_type)
            if ( 'NTD' in x_list[1] ):
                currency = 'NTD'
            elif ( 'USD' in x_list[1] ):
                currency = 'USD'
            else:
                print("No valid curency in the following:")
                print("Line {}: {}".format(line_cnt, line.strip()))
                break
            # parse section's content
            line = fp.readline()
            line_cnt += 1
            while line:
                #x_list = line.split(': ')
                line = line.strip()
                x_list = re.split(': | ', line)
                if ( len(x_list[0]) != 7 ): # a section end
                    break
                x_list[1] = x_list[1].replace(",", "")
                record_dict = dict()
                record_dict['type'] = ""
                record_dict['date'] = 0
                record_dict['money'] = 0
                record_dict['currency'] = ""
                record_dict['note'] = ""
                #print("{}({}): {} {}".format(trans_type, x_list[0], x_list[1], currency))
                record_dict['type'] = trans_type
                record_dict['date'] = x_list[0]
                record_dict['money'] = x_list[1]
                record_dict['currency'] = currency
                record_dict['note'] = ""
                #print("{}:{} {} {}".format(record_dict['date'], record_dict['money'], record_dict['currency'], record_dict['type']) )
                record_list.append(record_dict)
                line = fp.readline()
                line_cnt += 1  
        #next line
        line = fp.readline()
        line_cnt += 1
        
#print_record(record_list)
net_total = int(0)
total = int(0)
print("**********************")
print("* 存摺的金額存入紀錄 *")
print("**********************")
total  = get_total_of_type(record_list, "#存入世華美元", "USD")
net_total += (total * 30)
total  = get_total_of_type(record_list, "#存入世華台幣", "NTD")
net_total += total
print("")
print("* 存入台幣總金額(以匯率1:30) = NTD ${}".format(net_total))

print("")
print("=====================================================")
print("")

net_total = int(0)
total = int(0)
print("**********************")
print("* 台幣保單的金額交易紀錄 *")
print("**********************")
total  += get_total_of_type(record_list, "#國壽已從郵局提保費", "NTD")
total  += get_total_of_type(record_list, "#國壽已從世華提保費", "NTD")
total  -= get_total_of_type(record_list, "#國壽已匯入世華部分提領", "NTD")
total  -= get_total_of_type(record_list, "#國壽已匯入世華解約金", "NTD")
total  -= get_total_of_type(record_list, "#國壽已匯入保單借款", "NTD")
total  += get_total_of_type(record_list, "#國壽已從世華提保單還款", "NTD")
total  -= get_total_of_type(record_list, "#國壽已匯入世華保單配息", "NTD")
print("* 台幣保單的投入淨金額 = NTD ${} ".format(total))
print("計算方式:  國壽已從郵局提保費 + 國壽已從世華提保費 + 國壽已從世華提保單還款")
print("       - 國壽已匯入世華部分提領 - 國壽已匯入世華解約金 - 國壽已匯入保單借  - 國壽已匯入世華保單配息")
net_total += total
print("")
total = 0
print("**********************")
print("* 美元保單的金額交易紀錄 *")
print("**********************")
total += get_total_of_type(record_list, "#國壽已從世華提保費", "USD")
total -= get_total_of_type(record_list, "#國壽已匯入世華保單配息", "USD")
print("* 美元保單的投入淨金額 = USD ${}".format(total))
print("計算方式: (國壽已從世華提保費 -  國壽已匯入世華保單配息)")
net_total += (total * 30)
print("")

print("* 保單的投入淨台幣金額(以匯率1:30) = NTD ${}".format(net_total))

留言

熱門文章