計(jì)算扣費(fèi)金額腳本

# lower_limit = [7000, 2000]  # 保底(年保底颖医,月保底)
# high_limit = [10000, 5000]  # 上限(年上限,月上限)
# original_price = [[1000, 1600], [800, 800], [2000]]  # 不同的價(jià)格的線索挨决,每個(gè)月的放在一個(gè)列表中
# append_price = [["1", 900],["2", 600]]  # 補(bǔ)保底后追加的線索抽减,第X月追加一筆x元
# refund_type = "alread"  # 是未扣保底前進(jìn)行退費(fèi),alread是扣保底后進(jìn)行退費(fèi)
# refund_place = [["0", "1"]]  # 第X月第X條線索退費(fèi)

目的:通過(guò)接口傳遞上述參數(shù)例嘱,腳本進(jìn)行計(jì)算后,并輸出相應(yīng)結(jié)果

腳本1#price_step.py

#price_step.py

from datetime import datetime, timedelta
import calendar

def getMonthIter(start_month, end_month):
    # 時(shí)間格式為 %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天的不需要計(jì)算月保底
    year_minday = 0  # 不足X天不計(jì)算年保底

    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 # 計(jì)算間隔天數(shù)
    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:             #不足滿月的,需要計(jì)算月保底和上限
            day_interval=day_date-date+1
            guarantee_data=day_guarantee(day_interval,day_date,1,minday,high_limit,lower_limit)   #計(jì)算保底上限
        elif index+1 ==len(getMonthdata):   #不足滿月的蛮艰,需要計(jì)算月保底和上限
            day_interval=end_datetime.day
            guarantee_data=day_guarantee(day_interval,day_date,1,minday,high_limit,lower_limit)   #計(jì)算保底上限
        else:
            day_interval=day_date
            guarantee_data=day_guarantee(day_interval,day_date, 0,minday,high_limit,lower_limit)
        print("{}月,使用天數(shù){}腋腮,月保底{},月上限{}".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)   #月天數(shù),月保底即寡,上限


#計(jì)算年保底:
# 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("日期之間的天數(shù):", 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__)

# 使通過(guò)jsonify返回的中文顯示正常徊哑,否則顯示為ASCII碼

app.json.ensure_ascii = False


# 保底lower_limit    月保底Month_lower_limit   年保底Year_lower_limit
# 上限high_limit     月上限Month_high_limit    年上限Year_high_limit
# 價(jià)格price   [0-2]200      [3-]100
# 折扣價(jià)discount_price   折扣discount 原價(jià)original_price
# 本次預(yù)扣withhold   本次實(shí)扣Actual_deduction   累計(jì)計(jì)價(jià)accumulate_pricing  累計(jì)實(shí)扣accumulate_price   累計(jì)數(shù)量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]]  # 不同的價(jià)格的線索莺丑,每個(gè)月的放在一個(gè)列表中
    # append_price = [["1", 900],["2", 600]]  # 補(bǔ)保底后追加的線索,第X月追加一筆x元
    # refund_type = "alread"  # 是未扣保底前進(jìn)行退費(fèi)墩蔓,alread是扣保底后進(jìn)行退費(fèi)
    # refund_place = [["0", "1"]]  # 第X月第X條線索退費(fèi)
    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 = "第{}個(gè)月計(jì)價(jià):{}".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:  # 加上新線索的計(jì)價(jià)已大于年上限
            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)   #補(bǔ)保底
                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="{}月補(bǔ)保底金額{}梢莽,超出月上限的金額為{}".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   #余額扣為負(fù)數(shù)的時(shí)候不再扣費(fèi),只是把上一輪剩余的錢支出
                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 = "{}月實(shí)扣:{}奸披,金額分別是{}".format(index, accumulate_price,day_list)
        total_accumulate_pricing += accumulate_pricing_data  # 月累計(jì)計(jì)價(jià)
        total_accumulate_price += accumulate_price  # 月累計(jì)實(shí)扣
        index_totalaccumulate_pricing = "{}月累計(jì)預(yù)扣:{}".format(index, total_accumulate_pricing)
        index_totalaccumulate_price = "{}月累計(jì)實(shí)扣:{}".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 = "綜合整個(gè)周期累計(jì)計(jì)價(jià):{}".format(pricing_total)  # 累計(jì)計(jì)價(jià)
    # price_total_describe="根據(jù)每月上限{}和年上限{}昏名,綜合累計(jì)實(shí)扣:{}".format(Month_high_limit, Year_high_limit, price_total)# 累計(jì)扣價(jià)
    price_total_describe = "綜合累計(jì)實(shí)扣:{}".format(price_total)  # 累計(jì)扣價(jià)
    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 = "還需要補(bǔ)年保底金額:{}".format(round(bu_lower_limit))  # 初次補(bǔ)年保底
    accumulate_price_datadescrible = "綜合扣費(fèi)(保底+實(shí)扣):{}".format(accumulate_price_data)  # 綜合扣費(fèi)(保底+實(shí)扣)
    # print("月已補(bǔ)保底金額+年補(bǔ)保底金額合計(jì):{}".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
    # 按線索計(jì)算
    if original_price !=[]:

        accumulate_count_price=accumulate_original_price(lower_limit, high_limit, original_price,"按線索計(jì)算",accumulate_original_money)
        accumulate_money =accumulate_count_price[:4]   # 計(jì)算滿足月保底,又滿足年保底
        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="============================不進(jìn)行計(jì)算"
    # 補(bǔ)保底后又追加的線索計(jì)算
    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,"新追加線索后阵面,重新計(jì)算",accumulate_original_money)
    else:
        accumulate_append_money ="============================無(wú)新追加的線索"

    #    退費(fèi)計(jì)算
    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)    #實(shí)際退還的金額
                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)    #計(jì)價(jià)計(jì)算
                print("-----------",accumulate_original_money,"-----------")
        accumulate_refund_day=accumulate_original_price(lower_limit, high_limit, original_price_day,"退費(fèi)",accumulate_original_money)
        accumulate_refund_money = accumulate_refund_day[:4]
    else:
        accumulate_refund_money="============================不進(jìn)行退費(fèi)計(jì)算"
    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]]  # 不同的價(jià)格的線索膜钓,每個(gè)月的放在一個(gè)列表中
    # append_price = []  # 補(bǔ)保底后追加的線索嗽交,第X月追加一筆x元
    # refund_type = "alread"  # 是未扣保底前進(jìn)行退費(fèi)卿嘲,alread是扣保底后進(jìn)行退費(fèi)
    # refund_place = [["2", "0"]]  # 第X月第X條線索退費(fèi)
    # price_data(lower_limit,high_limit,original_price,append_price,refund_place)

使用:
運(yùn)行account.py腳本颂斜,打開postman,通過(guò)輸入不同的保底拾枣,上限金額沃疮,以及每個(gè)月分別的扣費(fèi)情況,扣費(fèi)后又追加的金額梅肤,以及各月退費(fèi)的金額計(jì)算出最后會(huì)扣除的金額

舉例:接口和入?yún)⑷缦?/p>

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

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

入?yún)ⅲ?

{

    "clue":{

        "original_price":[[1000, 1600], [800, 800], [2000]],    //不同的價(jià)格的線索司蔬,每個(gè)月的放在一個(gè)列表中

        "append_price":[["1", 900],["2", 3000]]            //補(bǔ)保底后追加的線索,第X月追加一筆x元

    },

    "limit":{

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

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

    },

    "refund_place":[["0", "1"],["1", "1"]]   //第X月第X條線索退費(fèi)

}
企業(yè)微信截圖_17097967624742.png

響應(yīng)結(jié)果輸出如下:
{
"code": 0,
"settlement_data": [
[
"按線索計(jì)算",
"[[100, 80], [100, 80, 80], [100, 80, 80, 80]]",
[
"0月補(bǔ)保底金額20,超出月上限的金額為0",
[
"第0個(gè)月計(jì)價(jià):180",
"0月實(shí)扣:200左医,金額分別是[100, 80]",
"0月累計(jì)預(yù)扣:180",
"0月累計(jì)實(shí)扣:200"
],
[
"第1個(gè)月計(jì)價(jià):260",
"1月實(shí)扣:260授帕,金額分別是[100, 80, 80]",
"1月累計(jì)預(yù)扣:440",
"1月累計(jì)實(shí)扣:460"
],
[
"第2個(gè)月計(jì)價(jià):340",
"2月實(shí)扣:287.95,金額分別是[100, 80, 80, 27.95]",
"2月累計(jì)預(yù)扣:780",
"2月累計(jì)實(shí)扣:747.95"
]
],
[
"綜合整個(gè)周期累計(jì)計(jì)價(jià):780",
"綜合累計(jì)實(shí)扣:747.95",
"還需要補(bǔ)年保底金額:0",
"綜合扣費(fèi)(保底+實(shí)扣):747.95",
"************************************************************************************************************"
]
],
"============================無(wú)新追加的線索",
[
"退費(fèi)",
"[[100, 80], [100, 80, 80], [100, 32.05, 80, 27.95]]",
[
"0月補(bǔ)保底金額20浮梢,超出月上限的金額為0",
[
"第0個(gè)月計(jì)價(jià):180",
"0月實(shí)扣:200跛十,金額分別是[100, 80]",
"0月累計(jì)預(yù)扣:180",
"0月累計(jì)實(shí)扣:200"
],
[
"第1個(gè)月計(jì)價(jià):260",
"1月實(shí)扣:260,金額分別是[100, 80, 80]",
"1月累計(jì)預(yù)扣:440",
"1月累計(jì)實(shí)扣:460"
],
[
"第2個(gè)月計(jì)價(jià):260",
"2月實(shí)扣:240.0秕硝,金額分別是[100, 32.05, 80, 27.95]",
"2月累計(jì)預(yù)扣:700",
"2月累計(jì)實(shí)扣:700.0"
]
],
[
"綜合整個(gè)周期累計(jì)計(jì)價(jià):700",
"綜合累計(jì)實(shí)扣:700.0",
"還需要補(bǔ)年保底金額:0",
"綜合扣費(fèi)(保底+實(shí)扣):700.0",
"************************************************************************************************************"
]
]
]
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末芥映,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌奈偏,老刑警劉巖坞嘀,帶你破解...
    沈念sama閱讀 206,602評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異霎苗,居然都是意外死亡姆吭,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,442評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門唁盏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)内狸,“玉大人,你說(shuō)我怎么就攤上這事厘擂±サ” “怎么了?”我有些...
    開封第一講書人閱讀 152,878評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵刽严,是天一觀的道長(zhǎng)昂灵。 經(jīng)常有香客問(wèn)我,道長(zhǎng)舞萄,這世上最難降的妖魔是什么眨补? 我笑而不...
    開封第一講書人閱讀 55,306評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮倒脓,結(jié)果婚禮上审胚,老公的妹妹穿的比我還像新娘纯衍。我一直安慰自己拳魁,他們只是感情好疾瓮,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,330評(píng)論 5 373
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著饲做,像睡著了一般线婚。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上盆均,一...
    開封第一講書人閱讀 49,071評(píng)論 1 285
  • 那天塞弊,我揣著相機(jī)與錄音,去河邊找鬼泪姨。 笑死游沿,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的驴娃。 我是一名探鬼主播奏候,決...
    沈念sama閱讀 38,382評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼唇敞!你這毒婦竟也來(lái)了蔗草?” 一聲冷哼從身側(cè)響起咒彤,我...
    開封第一講書人閱讀 37,006評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎咒精,沒(méi)想到半個(gè)月后镶柱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,512評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡模叙,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,965評(píng)論 2 325
  • 正文 我和宋清朗相戀三年歇拆,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片范咨。...
    茶點(diǎn)故事閱讀 38,094評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡故觅,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出渠啊,到底是詐尸還是另有隱情输吏,我是刑警寧澤,帶...
    沈念sama閱讀 33,732評(píng)論 4 323
  • 正文 年R本政府宣布替蛉,位于F島的核電站贯溅,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏躲查。R本人自食惡果不足惜它浅,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,283評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望镣煮。 院中可真熱鬧姐霍,春花似錦、人聲如沸怎静。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,286評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蚓聘。三九已至,卻和暖如春盟劫,著一層夾襖步出監(jiān)牢的瞬間夜牡,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,512評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工侣签, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留塘装,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,536評(píng)論 2 354
  • 正文 我出身青樓影所,卻偏偏與公主長(zhǎng)得像蹦肴,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子猴娩,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,828評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容