예제 #1
0
def get_bill():
    args = request.args
    house_info_id = args.get("house_code")
    year_mon = args.get("year_mon")
    ret = {"retCode": "0000", "retMsg": "", "billList": [], "lastMonth": "", "nextMonth": ""}
    year = None
    mon = None
    last_year = None
    last_mon = None
    next_year = None
    next_mon = None

    def get_last_mon(year, mon):
        last_mon = None
        last_year = None
        if mon == 1:
            last_year = year - 1
            last_mon = 12
        elif mon == 12:
            last_year = year
            last_mon -= 1
        else:
            last_year = year
            last_mon = mon - 1
        return (last_year, last_mon)

    def get_next_mon(year, mon):
        next_year = None
        next_mon = None
        if mon == 1:
            next_year = year
            next_mon += 1
        elif mon == 12:
            next_year = year + 1
            next_mon = 1
        else:
            next_year = year
            next_mon = mon + 1
        return (next_year, next_mon)

    try:
        year = int(year_mon[:4])
        mon = int(year_mon[4:])
        last_year, last_mon = get_last_mon(year, mon)
        next_year, next_mon = get_last_mon(year, mon)

    except ValueError as ex:
        ret = {"retCode": "3000", "retMsg": "年月格式错误,请按照201509的格式传递", "billList": [], "lastMonth": "", "nextMonth": ""}

    mon_list = [{"year": year, "mon": mon}, {"year": last_year, "mon": last_mon}, {"year": next_year, "mon": next_mon}]
    fees = HouseFee.get_house_fees(house_info_id=house_info_id, monList=mon_list)
    fees_dict = {}
    if fees:
        for fee in fees:
            fees_dict[fee.month] = fee
        target_fee = None
        total_fee = 0
        try:
            target_fee = fees_dict[mon]
            total_fee = target_fee.water_fee + target_fee.gas_fee + target_fee.electricity_fee
            ret["total"] = total_fee

            last_mon_fee = None
            if fees_dict.has_key(last_mon):
                ret["lastMonth"] = str(last_year) + "%02d" % last_mon
                last_mon_fee = fees_dict[last_mon]
                ret["billList"] = [
                    {
                        "FeeID": target_fee.id,
                        "fee": target_fee.water_fee,
                        "name": "水费",
                        "status": target_fee.water_fee_status,
                        "detailList": {"thisMon": target_fee.water_num, "lastMon": last_mon_fee.water_num},
                    },
                    {
                        "FeeID": target_fee.id,
                        "fee": target_fee.gas_fee,
                        "name": "燃气费",
                        "status": target_fee.gas_fee_status,
                        "detailList": {"thisMon": target_fee.gas_num, "lastMon": last_mon_fee.gas_num},
                    },
                    {
                        "FeeID": target_fee.id,
                        "fee": target_fee.electricity_fee,
                        "name": "电费",
                        "status": target_fee.electricity_fee_status,
                        "detailList": {"thisMon": target_fee.electricity_num, "lastMon": last_mon_fee.electricity_num},
                    },
                ]
            else:
                ret["billList"] = [
                    {
                        "FeeID": target_fee.id,
                        "fee": target_fee.water_fee,
                        "name": "水费",
                        "status": target_fee.water_fee_status,
                        "detailList": {"thisMon": target_fee.water_num, "lastMon": 0},
                    },
                    {
                        "FeeID": target_fee.id,
                        "fee": target_fee.gas_fee,
                        "name": "燃气费",
                        "status": target_fee.gas_fee_status,
                        "detailList": {"thisMon": target_fee.gas_num, "lastMon": 0},
                    },
                    {
                        "FeeID": target_fee.id,
                        "fee": target_fee.electricity_fee,
                        "name": "电费",
                        "status": target_fee.electricity_fee_status,
                        "detailList": {"thisMon": target_fee.electricity_num, "lastMon": 0},
                    },
                ]

        except IndexError as ex:
            if fees_dict.has_key(last_mon):
                ret["lastMonth"] = str(last_year) + "%02d" % last_mon

        if fees_dict.has_key(next_mon):
            ret["nextMonth"] = str(next_year) + "%02d" % next_mon

    else:
        pass

    return jsonify(ret)