计算扣费金额脚本

2024-03-06  本文已影响0人  楚糖的糖
# lower_limit = [7000, 2000]  # 保底(年保底,月保底)
# high_limit = [10000, 5000]  # 上限(年上限,月上限)
# original_price = [[1000, 1600], [800, 800], [2000]]  # 不同的价格的线索,每个月的放在一个列表中
# append_price = [["1", 900],["2", 600]]  # 补保底后追加的线索,第X月追加一笔x元
# refund_type = "alread"  # 是未扣保底前进行退费,alread是扣保底后进行退费
# refund_place = [["0", "1"]]  # 第X月第X条线索退费

目的:通过接口传递上述参数,脚本进行计算后,并输出相应结果

脚本1#price_step.py

#price_step.py

from datetime import datetime, timedelta
import calendar

def getMonthIter(start_month, end_month):
    # 时间格式为 %Y-%m
    out_month_arr = []
    dt_date = datetime.strptime(start_month, '%Y-%m-%d')
    dt_str = start_month
    while dt_str <= end_month:
        out_month_arr.append(dt_str)
        dt_date = dt_date + timedelta(days=calendar.monthrange(dt_date.year, dt_date.month)[1])
        dt_str = dt_date.strftime('%Y-%m-%d')
    return out_month_arr

def day_guarantee(day_interval,day_date,count_type,minday,high_limit,lower_limit):
    if count_type==1:
        if minday<day_interval:
            Month_lower = round((lower_limit[1] * day_interval / day_date),2)

        else:
            Month_lower=0
        Month_high=round((high_limit[1] * day_interval / day_date),2)
    else:
        Month_lower=lower_limit[1]
        Month_high=high_limit[1]
    return Month_lower,Month_high



from datetime import datetime



def calculate_limit():
    start_date = '2022-01-01'
    end_date = '2022-04-01'
    Month_high_limit = 400
    Year_high_limit = 3000
    Month_lower_limit = 200
    Year_lower_limit = 1500

    lower_limit = [Year_lower_limit, Month_lower_limit]  # 保底(年保底,月保底)
    high_limit = [Year_high_limit, Month_high_limit]  # 上限(年上限,月上限)
    minday = 0  # 不足X天的不需要计算月保底
    year_minday = 0  # 不足X天不计算年保底

    start_datetime = datetime.strptime(start_date, "%Y-%m-%d")
    end_datetime = datetime.strptime(end_date, "%Y-%m-%d")
    days = int(str(end_datetime - start_datetime).split()[0]) +1 # 计算间隔天数
    print(days)
    Year_high = round((Year_high_limit * days / 365),2)
    if days < year_minday:
        Year_lower = 0
    else:
        Year_lower = round((lower_limit[0] * days / 365),2)
    print("年保底和上限分别是{},{}".format(Year_lower, Year_high))
    getMonthdata=getMonthIter(start_date, end_date)
    # print(getMonthdata)
    guarantee_list=[]
    guarantee_data_list=[]
    for index in range(0, len(getMonthdata)):
        start_date = getMonthdata[index]
        # print(index,len(getMonthdata))
        start_datetime = datetime.strptime(start_date, "%Y-%m-%d")
        a=start_datetime.month
        date=start_datetime.day

        if a in (1,5,3,7,8,10,12):
            day_date=31
        elif a in (4,6,9,11):
            day_date = 30
        else:day_date = 28
        if index ==0:             #不足满月的,需要计算月保底和上限
            day_interval=day_date-date+1
            guarantee_data=day_guarantee(day_interval,day_date,1,minday,high_limit,lower_limit)   #计算保底上限
        elif index+1 ==len(getMonthdata):   #不足满月的,需要计算月保底和上限
            day_interval=end_datetime.day
            guarantee_data=day_guarantee(day_interval,day_date,1,minday,high_limit,lower_limit)   #计算保底上限
        else:
            day_interval=day_date
            guarantee_data=day_guarantee(day_interval,day_date, 0,minday,high_limit,lower_limit)
        print("{}月,使用天数{},月保底{},月上限{}".format(a,day_interval,guarantee_data[0],guarantee_data[1]))
        guarantee_list.append([index,day_interval,guarantee_data[0],guarantee_data[1]])
    return guarantee_list,Year_lower, Year_high
    # print(day_interval,guarantee_data)   #月天数,月保底,上限


#计算年保底:
# start_datetime = datetime.strptime(start_date, "%Y-%m-%d")
# end_datetime = datetime.strptime(end_date, "%Y-%m-%d")
# delta = (end_datetime -start_datetimestr).split()[0])
# days = delta.days
# print("日期之间的天数:", days)
calculate_data=calculate_limit()
print(calculate_data[0])  #月保底上限
print(calculate_data[1])  #年保底
print(calculate_data[2])  #年上限





if __name__ == '__main__':
    # day_difference()
    # day_guarantee(22)
    pass

脚本2#account.py

from flask import Flask, jsonify, request
import re

app = Flask(__name__)

# 使通过jsonify返回的中文显示正常,否则显示为ASCII码

app.json.ensure_ascii = False


# 保底lower_limit    月保底Month_lower_limit   年保底Year_lower_limit
# 上限high_limit     月上限Month_high_limit    年上限Year_high_limit
# 价格price   [0-2]200      [3-]100
# 折扣价discount_price   折扣discount 原价original_price
# 本次预扣withhold   本次实扣Actual_deduction   累计计价accumulate_pricing  累计实扣accumulate_price   累计数量count




def accumulate_original_price(lower_limit, high_limit, original_price,describe,accumulate_original_money):
    # lower_limit = [7000, 2000]  # 保底(年保底,月保底)
    # high_limit = [10000, 5000]  # 上限(年上限,月上限)
    # original_price = [[1000, 1600], [800, 800], [2000]]  # 不同的价格的线索,每个月的放在一个列表中
    # append_price = [["1", 900],["2", 600]]  # 补保底后追加的线索,第X月追加一笔x元
    # refund_type = "alread"  # 是未扣保底前进行退费,alread是扣保底后进行退费
    # refund_place = [["0", "1"]]  # 第X月第X条线索退费
    original_price_day=[]
    price_refunding_total = 0
    Month_high_limit = high_limit[1]
    Year_high_limit = high_limit[0]
    Month_lower_limit = lower_limit[1]
    Year_lower_limit = lower_limit[0]
    accumulate_pricing_list = []
    accumulate_price_list = []
    total_accumulate_price = 0
    total_accumulate_pricing = 0
    month_bu_total = 0
    exceed_Monthhigh_total = 0
    month_data = []
    print(original_price)
    for index, i in enumerate(original_price):
        accumulate_pricing = sum(i)
        if accumulate_original_money ==0:
            accumulate_pricing_data=accumulate_pricing
        else:
            accumulate_pricing_data=sum(accumulate_original_money[index])
        index_accumulate_pricing = "第{}个月计价:{}".format(index, accumulate_pricing_data)
        accumulate_pricing_list.append(accumulate_pricing_data)
        accumulate_pricing_total = accumulate_pricing
        if total_accumulate_price + accumulate_pricing > Year_high_limit:  # 加上新线索的计价已大于年上限
            accumulate_price = round(Year_high_limit - total_accumulate_price,2)


        else:
            if accumulate_pricing >= Month_high_limit:  # 大于月上限
                accumulate_price = Month_high_limit
                month_bu_lower = 0
                exceed_Month_high = accumulate_pricing - Month_high_limit
            elif accumulate_pricing <= Month_lower_limit:  # 小于月保底
                accumulate_price = Month_lower_limit
                month_bu_lower = round(accumulate_price - accumulate_pricing,2)   #补保底
                exceed_Month_high = 0
            else:
                accumulate_price = accumulate_pricing
                month_bu_lower = 0
                exceed_Month_high = 0
            month_bu_total += month_bu_lower
            exceed_Monthhigh_total += exceed_Month_high
            if month_bu_lower !=0 or exceed_Month_high !=0:
                lower_exceed_record="{}月补保底金额{},超出月上限的金额为{}".format(index, month_bu_lower, exceed_Month_high)
                print(lower_exceed_record)
                month_data.append(lower_exceed_record)
        total_money=accumulate_price
        print(total_money)
        day_list=[]
        for day_money in i:
            day_money_last=total_money
            total_money = round(total_money - int(day_money),2)
            day_money=round(day_money,2)
            if total_money <0:
                day_money=day_money_last   #余额扣为负数的时候不再扣费,只是把上一轮剩余的钱支出
                if day_money_last <0:
                    day_money=0
            day_list.append(day_money)
        original_price_day.append(day_list)
        # day_money=accumulate_price-i
        index_accumulate_price = "{}月实扣:{},金额分别是{}".format(index, accumulate_price,day_list)
        total_accumulate_pricing += accumulate_pricing_data  # 月累计计价
        total_accumulate_price += accumulate_price  # 月累计实扣
        index_totalaccumulate_pricing = "{}月累计预扣:{}".format(index, total_accumulate_pricing)
        index_totalaccumulate_price = "{}月累计实扣:{}".format(index, total_accumulate_price)
        accumulate_price_list.append(accumulate_price)
        month_data.append([index_accumulate_pricing, index_accumulate_price, index_totalaccumulate_pricing,
                           index_totalaccumulate_price])
    # print(original_price_day)
    print(month_data)
    pricing_total = sum(accumulate_pricing_list)
    price_total = sum(accumulate_price_list)
    if price_total > Year_high_limit:
        price_total = Year_high_limit
    else:
        price_total = price_total

    pricing_total_describe = "综合整个周期累计计价:{}".format(pricing_total)  # 累计计价
    # price_total_describe="根据每月上限{}和年上限{},综合累计实扣:{}".format(Month_high_limit, Year_high_limit, price_total)# 累计扣价
    price_total_describe = "综合累计实扣:{}".format(price_total)  # 累计扣价
    if price_total <= Year_lower_limit:  # 小于年保底
        bu_lower_limit = Year_lower_limit - price_total
        accumulate_price_data = Year_lower_limit
    else:
        accumulate_price_data = price_total
        bu_lower_limit = 0
    bu_lower_total = month_bu_total + bu_lower_limit
    bu_lower_limitdescrible = "还需要补年保底金额:{}".format(round(bu_lower_limit))  # 初次补年保底
    accumulate_price_datadescrible = "综合扣费(保底+实扣):{}".format(accumulate_price_data)  # 综合扣费(保底+实扣)
    # print("月已补保底金额+年补保底金额合计:{}".format(bu_lower_total))
    accumulate_describle = [pricing_total_describe, price_total_describe, bu_lower_limitdescrible,
                            accumulate_price_datadescrible, "************************************************************************************************************"]

    return describe,str(original_price), month_data, accumulate_describle,original_price_day,pricing_total,price_total


def price_data(lower_limit, high_limit, original_price, append_price, refund_place):
    Year_high_limit = high_limit[0]
    accumulate_original_money = 0
    # 按线索计算
    if original_price !=[]:

        accumulate_count_price=accumulate_original_price(lower_limit, high_limit, original_price,"按线索计算",accumulate_original_money)
        accumulate_money =accumulate_count_price[:4]   # 计算满足月保底,又满足年保底
        original_price_day=accumulate_count_price[4]
        pricing_total=accumulate_count_price[5]
        price_total=accumulate_count_price[6]
        print(original_price_day)
    else:
        accumulate_money="============================不进行计算"
    # 补保底后又追加的线索计算
    if append_price != []:
        for append_money in append_price:
            # print(append_money)
            append_place = int(append_money[0])
            original_price_day[append_place].append(append_money[1])
        accumulate_append_money = accumulate_original_price(lower_limit, high_limit, original_price_day,"新追加线索后,重新计算",accumulate_original_money)
    else:
        accumulate_append_money ="============================无新追加的线索"

    #    退费计算
    print("111",refund_place)
    if refund_place != []:
        for i in refund_place:
            retfund_place = int(i[0])
            retfund_times = int(i[1])
            renfund_money=int(original_price_day[retfund_place][retfund_times])
            renfund_money_total=round(pricing_total,2)-renfund_money
            print(renfund_money_total)
            if renfund_money_total <Year_high_limit:
                refund_price_data=round(price_total -renfund_money_total,2)    #实际退还的金额
                change_money=renfund_money-refund_price_data
                original_price_day[retfund_place][retfund_times]=change_money
                accumulate_original_money=original_price
                accumulate_original_money[retfund_place].pop(retfund_times)    #计价计算
                print("-----------",accumulate_original_money,"-----------")
        accumulate_refund_day=accumulate_original_price(lower_limit, high_limit, original_price_day,"退费",accumulate_original_money)
        accumulate_refund_money = accumulate_refund_day[:4]
    else:
        accumulate_refund_money="============================不进行退费计算"
    return accumulate_money,accumulate_append_money,accumulate_refund_money


@app.route("/settlement", methods=['POST'])
def settlement():
    clue = request.json.get("clue")
    limit = request.json.get("limit")
    lower_limit = limit["lower_limit"]
    high_limit = limit["high_limit"]
    original_price = clue["original_price"]
    append_price = clue["append_price"]
    refund_place = request.json.get("refund_place")
    data = lower_limit, high_limit, original_price, append_price, refund_place
    # print(lower_limit,high_limit,original_price,append_price,refund_place)
    settlement_data = price_data(lower_limit, high_limit, original_price, append_price, refund_place)
    return jsonify(
        {"code": 0, "settlement_data": settlement_data})


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=9089)
    # lower_limit = [373.97, 200]  # 保底(年保底,月保底)
    # high_limit = [747.95, 300]  # 上限(年上限,月上限)
    # original_price = [[100, 80], [100, 80, 80],[100,80,80,80]]  # 不同的价格的线索,每个月的放在一个列表中
    # append_price = []  # 补保底后追加的线索,第X月追加一笔x元
    # refund_type = "alread"  # 是未扣保底前进行退费,alread是扣保底后进行退费
    # refund_place = [["2", "0"]]  # 第X月第X条线索退费
    # price_data(lower_limit,high_limit,original_price,append_price,refund_place)

使用:
运行account.py脚本,打开postman,通过输入不同的保底,上限金额,以及每个月分别的扣费情况,扣费后又追加的金额,以及各月退费的金额计算出最后会扣除的金额

举例:接口和入参如下

http://xx.xx.xx.xx:9089/settlement

headers:Content-Type=application/json;charset=utf8

入参:


{

    "clue":{

        "original_price":[[1000, 1600], [800, 800], [2000]],    //不同的价格的线索,每个月的放在一个列表中

        "append_price":[["1", 900],["2", 3000]]            //补保底后追加的线索,第X月追加一笔x元

    },

    "limit":{

        "lower_limit":[7000, 2000],  //保底(年保底,月保底)

        "high_limit":[8000, 3000]     //上限(年上限,月上限)

    },

    "refund_place":[["0", "1"],["1", "1"]]   //第X月第X条线索退费

}
企业微信截图_17097967624742.png

响应结果输出如下:
{
"code": 0,
"settlement_data": [
[
"按线索计算",
"[[100, 80], [100, 80, 80], [100, 80, 80, 80]]",
[
"0月补保底金额20,超出月上限的金额为0",
[
"第0个月计价:180",
"0月实扣:200,金额分别是[100, 80]",
"0月累计预扣:180",
"0月累计实扣:200"
],
[
"第1个月计价:260",
"1月实扣:260,金额分别是[100, 80, 80]",
"1月累计预扣:440",
"1月累计实扣:460"
],
[
"第2个月计价:340",
"2月实扣:287.95,金额分别是[100, 80, 80, 27.95]",
"2月累计预扣:780",
"2月累计实扣:747.95"
]
],
[
"综合整个周期累计计价:780",
"综合累计实扣:747.95",
"还需要补年保底金额:0",
"综合扣费(保底+实扣):747.95",
"************************************************************************************************************"
]
],
"============================无新追加的线索",
[
"退费",
"[[100, 80], [100, 80, 80], [100, 32.05, 80, 27.95]]",
[
"0月补保底金额20,超出月上限的金额为0",
[
"第0个月计价:180",
"0月实扣:200,金额分别是[100, 80]",
"0月累计预扣:180",
"0月累计实扣:200"
],
[
"第1个月计价:260",
"1月实扣:260,金额分别是[100, 80, 80]",
"1月累计预扣:440",
"1月累计实扣:460"
],
[
"第2个月计价:260",
"2月实扣:240.0,金额分别是[100, 32.05, 80, 27.95]",
"2月累计预扣:700",
"2月累计实扣:700.0"
]
],
[
"综合整个周期累计计价:700",
"综合累计实扣:700.0",
"还需要补年保底金额:0",
"综合扣费(保底+实扣):700.0",
"************************************************************************************************************"
]
]
]
}

上一篇下一篇

猜你喜欢

热点阅读