def deposit(card_num, amount_of_money): """存款""" acc_path = os.path.join(USER_PATH, card_num, "account.json") acc = utils.load_file(acc_path) acc["deposit"] += amount_of_money utils.dump_to_file(acc_path, acc) system.recode_trade(card_num, "0", "0", amount_of_money)
def charge_out(): """出账 """ global TODAY sdate_path = os.path.join(USER_PATH, "usersdate.json") usersdate_dict = utils.load_file(sdate_path) pending_cardnum = usersdate_dict[str(TODAY.day)] # last_month = TODAY.replace(months=-1) # str_currentmonth = TODAY.strftime("%Y%m") # str_lastmonth = last_month.strftime("%Y%m") if not pending_cardnum: return "今天没有要出账的账户" else: for cardnum in pending_cardnum: num_path = os.path.join(USER_PATH, cardnum) acc_path = os.path.join(num_path, "account.json") bill_path = os.path.join(num_path, "bill.json") bill_data = utils.load_file(bill_path) acc = utils.load_file(acc_path) # trade_data = utils.load_file(trade_path) bill_temp = acc["credit_total"] - acc["credit_balance"] now = arrow.now().for_json() if bill_temp > 0: bill_data[TODAY.format("YYYYMM")]["dept"] = bill_temp bill_data[TODAY.format("YYYYMM")]["repayment"] = 0 bill_data[TODAY.format("YYYYMM")]["bill_time"] = now else: bill_data[TODAY.format("YYYYMM")]["dept"] = bill_temp bill_data[TODAY.format("YYYYMM")]["repayment"] = 0 bill_data[TODAY.format("YYYYMM")]["bill_time"] = now utils.dump_to_file(bill_path, bill_data)
def register(username, password): """ 用户注册 :param username:用户名 :param password:密码 :return:注册成功返回username,失败返回False """ patt1 = "[^(\w|_)]" patt2 = "^[^a-zA-Z]+" m = hashlib.md5() m.update(bytes(password, encoding='utf-8')) m_password = m.hexdigest() if re.search(patt1, username) or re.match(patt2, username): print("用户名只能为英文字母开头的数字、字母、下划线的组合!") return False else: acc = utils.load_file(ACC_PATH) if acc.get(username): print("用户名已存在!") return False else: acc[username] = {} acc[username]["username"] = username acc[username]["pwd"] = m_password acc[username]["balance"] = 0 acc[username]["locked_flag"] = 0 utils.dump_to_file(ACC_PATH, acc) return username
def add_to_cart(dir1, dir2, dir1_dict, dir2_dict, chogood, good_dict, buy_num): """ 将商品添加至购物车 :param goods_file:商品数据文件 :param username:接收已登录的用户 :param input_dir1_num:用户输入的一级目录编号 :param input_dir2_num:用户输入的二级目录编号 :param input_pro_num:用户输入的商品编号 :param input_pro_cou:接收用户输入的购买数量 :return:添加成功-True,商品编号不正确-False,库存不够-"loq" """ global CART goods_dict = utils.load_file(GOODS_PATH) good = good_dict[chogood] g_info = goods_dict[dir1_dict[dir1]][dir2_dict[dir2]][good_dict[chogood]] if buy_num.isdigit(): buy_num = int(buy_num) g_qty = g_info["qty"] if buy_num <= g_qty: g_price = g_info["price"] if CART.get(good_dict[chogood]): CART[good]["qty"] += buy_num else: CART[good] = {} CART[good]["price"] = g_price CART[good]["qty"] = buy_num goods_dict[dir1_dict[dir1]][dir2_dict[dir2]][ good_dict[chogood]]["qty"] -= buy_num utils.dump_to_file(GOODS_PATH, goods_dict) return showcart() else: return "库存不足" else: return "数量必须为数字"
def lock(username): ad_path = os.path.join(DB_PATH, "credit", "admin", "admin.json") ad_info = utils.load_file(ad_path) if ad_info.get(username): ad_info[username]["lock_flag"] = 1 utils.dump_to_file(ad_path, ad_info) else: return "不存在此管理员"
def calculate_interest(card_num): num_path = os.path.join(USER_PATH, card_num) acc_path = os.path.join(num_path, "account.json") bill_path = os.path.join(num_path, "bill.json") trade_path = os.path.join(num_path, "trade.json") print(trade_path) bill_data = utils.load_file(bill_path) trade_data = utils.load_file(trade_path) acc = utils.load_file(acc_path) statement_date = acc["STATEMENT_DATE"] now_time = arrow.now() bill_date = now_time.replace(months=-1, day=statement_date) bill_key = bill_date.format("YYYYMM") if bill_data.get(bill_key): lower_str = bill_data[bill_key]["bill_time"] lower_time = arrow.Arrow.strptime(lower_str, "%Y-%m-%d %H:%M:%S") else: return "没有账单" repay_total = 0 for k in trade_data: trade_time = arrow.Arrow.strptime(k, "%Y-%m-%d %H:%M:%S") if lower_time < trade_time <= now_time: if trade_data[k]["trade_flag"] == "0": repay = trade_data[k]["amount"] repay_total += repay last_dept = bill_data[bill_key]["dept"] last_repayment = bill_data[bill_key]["repayment"] startday = now_time.replace(months=-2, day=statement_date) endday = now_time days = endday - startday # print(days.days) if last_dept > 0: if last_dept <= last_repayment: return "已还清" else: commission = 0 interest = (last_dept - last_repayment) * \ settings.EXPIRE_DAY_RATE * days.days # print(interest) interest = round(interest, 2) # print(interest) if repay_total / last_dept < 0.1: if bill_data[bill_key]["commission_flag"] == 0: repay_min = last_dept * 0.1 commission = (repay_min - repay_total) * 0.05 trade_key = now_time.strftime("%Y-%m-%d %H:%M:%S") trade_data[trade_key] = dict() trade_data[trade_key]["opera_type"] = "1" trade_data[trade_key]["trade_flag"] = "1" trade_data[trade_key]["amount"] = 0 trade_data[trade_key]["interest"] = interest trade_data[trade_key]["commission"] = commission utils.dump_to_file(bill_path, bill_data) utils.dump_to_file(trade_path, trade_data) else: return "欠款为0"
def lock(username): num_path = os.path.join(DB_PATH, "credit", "users", username) if os.path.exists(num_path): acc_path = os.path.join(DB_PATH, "credit", "users", username, "account.json") else: return "账户不存在" acc_info = utils.load_file(acc_path) acc_info[username]["state"] = 1 utils.dump_to_file(acc_path, acc_info)
def locking(username): """ 锁定用户函数 :param acc_file:账户文件 :param username:用户名 :return: """ global ACC_PATH acc = utils.load_file(ACC_PATH) user_info = acc[username] user_info["locked_flag"] = 1 utils.dump_to_file(ACC_PATH, acc)
def modify_account(card_num, new_total=None, new_date=None, new_state=None): acc_path = os.path.join(USER_PATH, card_num, "account.json") acc = utils.load_file(acc_path) if new_total: temp = acc["credit_total"] ran = new_total - temp acc["credit_total"] = new_total acc["credit_balance"] += ran if new_date: acc["STATEMENT_DATE"] = new_date if new_state: acc["state"] = new_state utils.dump_to_file(acc_path)
def repayment(card_num, amount_of_money): """还款""" acc_path = os.path.join(USER_PATH, card_num, "account.json") acc = utils.load_file(acc_path) amounts_owed = acc["credit_total"] - acc["credit_balance"] if amount_of_money >= amounts_owed: acc["credit_balance"] = acc["credit_total"] acc[deposit] += (amount_of_money - amounts_owed) utils.dump_to_file(acc_path, acc) else: acc["credit_balance"] += amount_of_money utils.dump_to_file(acc_path, acc) system.recode_trade(card_num, "0", "0", amount_of_money)
def spend(card_num, card_pwd, amount_of_money): """消费""" acc_path = os.path.join(USER_PATH, card_num, "account.json") acc = utils.load_file(acc_path) if acc["state"] == 0: if card_pwd != acc["pwd"]: return "支付密码不正确" else: if acc["credit_balance"] >= amount_of_money: acc["credit_balance"] -= amount_of_money utils.dump_to_file(acc_path, acc) system.recode_trade(card_num, "1", "1", amount_of_money) else: return "额度不足" else: return "您的账户已冻结"
def recode_trade(card_num, trade_flag, opera_type, amount, commission=0): """记录操作流水""" num_path = os.path.join(USER_PATH, card_num) trade_path = os.path.join(num_path, "trade.json") timenow = arrow.now().strftime("%Y-%m-%d %H:%M:%S") if os.path.exists(trade_path): trade = utils.load_file(trade_path) else: trade = dict() # print(trade) trade[timenow] = dict() trade[timenow]["trade_flag"] = trade_flag trade[timenow]["opera_type"] = opera_type trade[timenow]["amount"] = amount trade[timenow]["commission"] = commission print(trade_path) utils.dump_to_file(trade_path, trade)
def del_account(card_num): num_path = os.path.join(USER_PATH, card_num) acc_path = os.path.join(num_path, "account.json") # name_path = os.path.join(USER_PATH, "username.json") sdate_path = os.path.join(USER_PATH, "usersdate.json") acc = utils.load_file(acc_path) # name_list = utils.load_file(name_path) sdate = utils.load_file(sdate_path) balance = acc["credit_balance"] if balance < acc["credit_total"]: return "该用户有欠款未还清,不能注销!" else: acc["state"] = 2 for d in sdate: num_list = sdate[d] if card_num in num_list: num_list.remove(card_num) utils.dump_to_file(acc_path, acc) utils.dump_to_file(sdate_path, sdate)
def transfer_accounts(to_num, amount_of_money): """转账""" global USER_INFO num = USER_INFO["username"] acc_path = os.path.join(USER_PATH, num) to_acc_path = os.path.join(USER_PATH, to_num) acc_path = os.path.join(acc_path, "account.json") to_acc_path = os.path.join(acc_path, "account.json") acc = utils.load_file(acc_path) try: to_acc = utils.load_file(to_acc_path) except Exception as e: print(e) if acc["deposit"] + acc["credit_balance"] >= amount_of_money: temp = to_acc["credit_balance"] + amount_of_money commission = 0 if acc["deposit"] >= amount_of_money: acc["deposit"] -= amount_of_money if temp <= to_acc["credit_total"]: to_acc["credit_balance"] += amount_of_money else: temp1 = temp - to_acc["credit_total"] to_acc["credit_balance"] = to_acc["credit_total"] to_acc["deposit"] += temp1 else: temp2 = amount_of_money - acc["deposit"] acc["deposit"] = 0 commission = temp2 * settings.FETCH_MONEY_RATE acc["credit_balance"] -= temp2 + commission if temp <= to_acc["credit_total"]: to_acc["credit_balance"] += amount_of_money else: temp3 = temp - to_acc["credit_total"] to_acc["credit_balance"] = to_acc["credit_total"] to_acc["deposit"] += temp3 utils.dump_to_file(acc_path, acc) utils.dump_to_file(to_acc_path, to_acc) system.recode_trade(num, "1", "1", amount_of_money, commission) system.recode_trade(to_num, "0", "0", amount_of_money) else: return "余额不足!"
def create_account(card_num, username, role="user", credit_total=settings.CREDIT_TOTAL, pwd=settings.DEFAULT_PWD, statement_date=settings.STATEMENT_DATE): acc_path = os.path.join(USER_PATH, card_num, "account.json") name_path = os.path.join(USER_PATH, "username.json") sdate_path = os.path.join(USER_PATH, "usersdate.json") num_path = os.path.join(USER_PATH, card_num) username_list = utils.load_file(name_path) usersdate = utils.load_file(sdate_path) today = arrow.now() card_num = utils.to_num(card_num) repayment_date = today.replace(days=+settings.GRACE_PERIOD).format("DD") if not card_num: # print("卡号必须是16位数字") return "卡号必须是16位数字" if username in username_list: # print("用户名已存在") return "用户名已存在" mkdir_ret = utils.mkdir(num_path) if mkdir_ret: # print("账户已存在") return "账户已存在" else: acc_info = { "num": card_num, "username": username, "role": role, "credit_total": credit_total, "credit_balance": credit_total, "pwd": utils.encrypt(pwd), "state": 0, "deposit": 0, "statement_date": statement_date, "repayment_date": repayment_date } username_list.append(username) usersdate[str(statement_date)].append(card_num) utils.dump_to_file(sdate_path, usersdate) utils.dump_to_file(acc_path, acc_info) utils.dump_to_file(name_path, username_list) card_path = os.path.join(USER_PATH, "card.json") if os.path.exists(card_path): card = utils.load_file(card_path) else: card = list() card.append(card_num) utils.dump_to_file(card_path, card)
def draw_cash(pwd, amount_of_money): """ 取现 """ global USER_INFO card_num = USER_INFO["username"] print(card_num) if card_num: num_path = os.path.join(USER_PATH, card_num) acc_path = os.path.join(num_path, "account.json") acc = utils.load_file(acc_path) if acc["state"] == 0: print("状态正常") if utils.encrypt(pwd) != acc["pwd"]: print("取款密码不正确") return "取款密码不正确" else: print("密码正确") amount_of_money = float(amount_of_money) if amount_of_money <= acc["deposit"]: acc["deposit"] -= amount_of_money utils.dump_to_file(acc_path, acc) print("账户修改成功") system.recode_trade(card_num, "1", "1", amount_of_money, 0) else: temp = amount_of_money - acc["deposit"] if acc["credit_balance"] >= temp: acc["deposit"] = 0 temp_final = temp + temp * settings.FETCH_MONEY_RATE acc["credit_balance"] -= temp_final utils.dump_to_file(acc_path, acc) system.recode_trade(card_num, "1", "1", amount_of_money, temp_final) else: return "额度不足" else: return "您的账户已冻结"