def jf_tax_calculator(character_owner_hash): total_owed = 0 total_paid = 0 payment_id_list = [] db_keys = g.mongo.db.api_keys.find_one({"_id": character_owner_hash}) if db_keys: for key in db_keys["keys"]: payment_id_list.append(key["character_id"]) caches.wallet_journal() db_wallet_payment_list = g.mongo.db.wallet_journal.find({"owner_id_1": {"$in": payment_id_list}, "ref_type_id": 10}) for wallet_payment in db_wallet_payment_list: total_paid += wallet_payment["amount"] contract_ids = set() corp_pay_list = g.mongo.db.contracts.find({"_id.service": "jf_service", "acceptor_id": {"$in": payment_id_list}, "status": "Completed"}) db_jf_insurance = g.mongo.db.preferences.find_one({"_id": "jf_insurance"}) db_jf_tax = g.mongo.db.preferences.find_one({"_id": "jf_tax"}) if not db_jf_insurance: db_jf_insurance = {"history": [{"valid_after": 0, "percentage": 0}]} if not db_jf_tax: db_jf_tax = {"history": [{"valid_after": 0, "percentage": 0}]} for pay_contract in corp_pay_list: contract_ids.add(pay_contract["_id"]["id"]) if pay_contract["collateral"] > 0: jf_insurance = conversions.valid_value(db_jf_insurance["history"], pay_contract["date_accepted"]) jf_tax = conversions.valid_value(db_jf_tax["history"], pay_contract["date_accepted"]) total_owed += pay_contract["reward"] * (jf_insurance["percentage"] + jf_tax["percentage"]) / 100 return total_paid, total_owed
def jf_tax_calculator(character_owner_hash): total_owed = 0 total_paid = 0 payment_id_list = [] db_keys = g.mongo.db.api_keys.find_one({"_id": character_owner_hash}) if db_keys: for key in db_keys["keys"]: payment_id_list.append(key["character_id"]) caches.wallet_journal() db_wallet_payment_list = g.mongo.db.wallet_journal.find({ "owner_id_1": { "$in": payment_id_list }, "ref_type_id": 10 }) for wallet_payment in db_wallet_payment_list: total_paid += wallet_payment["amount"] contract_ids = set() corp_pay_list = g.mongo.db.contracts.find({ "_id.service": "jf_service", "acceptor_id": { "$in": payment_id_list }, "status": "Completed" }) db_jf_insurance = g.mongo.db.preferences.find_one({"_id": "jf_insurance"}) db_jf_tax = g.mongo.db.preferences.find_one({"_id": "jf_tax"}) if not db_jf_insurance: db_jf_insurance = {"history": [{"valid_after": 0, "percentage": 0}]} if not db_jf_tax: db_jf_tax = {"history": [{"valid_after": 0, "percentage": 0}]} for pay_contract in corp_pay_list: contract_ids.add(pay_contract["_id"]["id"]) if pay_contract["collateral"] > 0: jf_insurance = conversions.valid_value( db_jf_insurance["history"], pay_contract["date_accepted"]) jf_tax = conversions.valid_value(db_jf_tax["history"], pay_contract["date_accepted"]) total_owed += pay_contract["reward"] * ( jf_insurance["percentage"] + jf_tax["percentage"]) / 100 return total_paid, total_owed
def admin(): route_list = [] # route = [_id, name, m3, corp] no_matches = [] general = "" corp = "" _id = "" name = "" start = "" end = "" collateral = "" edit = False if request.method == "GET": if request.args.get("action") == "delete": g.mongo.db.jf_routes.remove({"_id": int(request.args.get("_id"))}) elif request.args.get("action") == "fix": # Check station names station_name_corrector() elif request.args.get("action") == "edit": selected_route = g.mongo.db.jf_routes.find_one({"_id": int(request.args.get("_id"))}) edit = True _id = request.args.get("_id") name = selected_route["name"] start = selected_route["start"] end = selected_route["end"] # Prices last_time = 0 general = 0 corp = 0 collateral = 0 for price in selected_route["prices"]: if price["valid_after"] > last_time: corp = price["corp"] general = price["general"] last_time = price["valid_after"] collateral = price["collateral"] elif request.args.get("action") == "all": bulk_op = g.mongo.db.jf_routes.initialize_unordered_bulk_op() bulk_run = False for route in g.mongo.db.jf_routes.find(): all_last_time = 0 all_corp = 0 all_general = 0 for price in route["prices"]: if price["valid_after"] > all_last_time: all_last_time = price["valid_after"] all_corp = price["corp"] all_general = price["general"] bulk_run = True bulk_op.find({"_id": route["_id"]}).update({ "$push": { "prices": { "valid_after": int(time.time()), "corp": all_corp, "general": all_general, "collateral": float(request.args.get("collateral")) } } }) if bulk_run: bulk_op.execute() elif request.args.get("action") == "tax": g.mongo.db.preferences.update({"_id": "jf_insurance"}, {"$push": { "history": {"valid_after": int(time.time()), "percentage": float(request.args.get("insurance"))}}}, upsert=True) g.mongo.db.preferences.update({"_id": "jf_tax"}, {"$push": { "history": {"valid_after": int(time.time()), "percentage": float(request.args.get("tax"))}}}, upsert=True) g.mongo.db.preferences.update({"_id": "jf_reimbursement"}, {"amount": float(request.args.get("threshold"))}, upsert=True) elif request.method == "POST": if request.form.get("action") == "single": if request.form.get("_id"): g.mongo.db.jf_routes.update({"_id": int(request.form.get("_id"))}, { "$set": { "name": request.form.get("name"), "start": request.form.get("start").strip(), "end": request.form.get("end").strip() }, "$push": { "prices": { "valid_after": int(time.time()), "corp": float(request.form.get("corp", 0)), "general": float(request.form.get("general", 0)), "collateral": float(request.form.get("collateral", 0)) } } }, upsert=True) else: db_start = g.mongo.db.stations.find_one({"name": request.form.get("start").strip()}) db_end = g.mongo.db.stations.find_one({"name": request.form.get("end").strip()}) if db_start and db_end: g.mongo.db.jf_routes.update({"_id": int(str(db_start["_id"]) + str(db_end["_id"]))}, { "$setOnInsert": { "name": request.form.get("name"), "start": request.form.get("start").strip(), "end": request.form.get("end").strip(), "prices": [{ "valid_after": int(time.time()), "corp": float(request.form.get("corp", 0)), "general": float(request.form.get("general", 0)), "collateral": float(request.form.get("collateral", 0)) }] } }, upsert=True) else: if not db_start: no_matches.append(request.form.get("start").strip()) if not db_end: no_matches.append(request.form.get("end").strip()) elif request.form.get("action") == "multiple": request_station_split = request.form.get("stations").split("\n") station_list = [g.mongo.db.stations.find_one({"name": x.strip()}) for x in request_station_split] if station_list and None not in station_list: # Ensure there are stations to parse and all are matched bulk_run = False bulk_op = g.mongo.db.jf_routes.initialize_unordered_bulk_op() for start_station in station_list: for end_station in station_list: if start_station != end_station: route_id = int(str(start_station["_id"]) + str(end_station["_id"])) route_name_start = start_station["name"].split(" - ")[0] route_name_end = end_station["name"].split(" - ")[0] bulk_run = True prices = [request.form.get("corp", 0), request.form.get("general", 0), request.form.get("collateral", 0)] prices = [float(x) if x.strip() else 0 for x in prices] bulk_op.find({"_id": route_id}).upsert().update({ "$setOnInsert": { "name": route_name_start + " >> " + route_name_end, "start": start_station["name"].strip(), "end": end_station["name"].strip(), "prices": [{ "valid_after": int(time.time()), "corp": prices[0], "general": prices[1], "collateral": prices[2] }] } }) if bulk_run: bulk_op.execute() else: no_match_indexes = [i for i, x in enumerate(station_list) if x is None] no_matches = [request_station_split[n] for n in no_match_indexes] # Clear all after post general = "" corp = "" _id = "" name = "" start = "" end = "" collateral = "" edit = False for route in g.mongo.db.jf_routes.find(): last_time = 0 corp_price = 0 gen_price = 0 collateral_percent = 0 for price in route["prices"]: if price["valid_after"] > last_time: corp_price = price["corp"] gen_price = price["general"] last_time = price["valid_after"] collateral_percent = price["collateral"] route_list.append([route["_id"], route["name"], "{:0,.2f}".format(gen_price), "{:0,.2f}".format(corp_price), "{:0,.2f}".format(collateral_percent), route["start"], route["end"]]) db_jf_insurance = g.mongo.db.preferences.find_one({"_id": "jf_insurance"}) db_jf_tax = g.mongo.db.preferences.find_one({"_id": "jf_tax"}) db_jf_reimbursement = g.mongo.db.preferences.find_one({"_id": "jf_reimbursement"}) if db_jf_insurance and db_jf_tax and db_jf_reimbursement: jf_insurance = conversions.valid_value(db_jf_insurance["history"], time.time()) jf_tax = conversions.valid_value(db_jf_tax["history"], time.time()) jf_reimbursement = db_jf_reimbursement["amount"] # Formatting jf_insurance = "{:.02f}%".format(jf_insurance["percentage"]) jf_tax = "{:.02f}%".format(jf_tax["percentage"]) jf_reimbursement = "{:,.02f}".format(jf_reimbursement) else: jf_insurance = "0%" jf_tax = "0%" jf_reimbursement = "0.00" return render_template("jf_admin.html", route_list=route_list, general=general, corp=corp, _id=_id, name=name, start=start, end=end, edit=edit, collateral=collateral, jf_insurance=jf_insurance, jf_tax=jf_tax, jf_reimbursement=jf_reimbursement, no_matches=no_matches)
def home(item=""): cart_item_list = {} error_string = request.args.get("error_string") bulk_op_update = g.mongo.db.carts.initialize_unordered_bulk_op() bulk_run_update = False # Change qty if post from this page if request.method == "POST" and request.form.get("action") == "qty": for key, value in request.form.items(): if key != "action" and not key.startswith("DataTables"): if value.strip(): if int(float(value)) != 0: bulk_op_update.find({"_id": session["CharacterOwnerHash"]}).upsert().update({ "$set": {"items." + key: int(float(value))}}) else: bulk_op_update.find({"_id": session["CharacterOwnerHash"]}).upsert().update({ "$unset": {"items." + key: int(float(value))}}) bulk_run_update = True if request.method == "POST" and request.form.get("action") == "clear": g.mongo.db.carts.remove({"_id": session["CharacterOwnerHash"]}) # Add new item to database input_string = request.form.get("parse", session.get("fitting")) if item or input_string: if item: try: item_adjustment_list = item.split(":") for adjustment_item in item_adjustment_list: adjustment_item_info = adjustment_item.split(";") if request.args.get("action") == "edit": bulk_op_update.find({"_id": session["CharacterOwnerHash"]}).upsert().update({ "$set": {"items." + adjustment_item_info[0]: int(adjustment_item_info[1])}}) else: bulk_op_update.find({"_id": session["CharacterOwnerHash"]}).upsert().update({ "$inc": {"items." + adjustment_item_info[0]: int(adjustment_item_info[1])}}) bulk_run_update = True except IndexError: error_string = "Was not able to add {}".format(item) elif input_string: session.pop("fitting", None) parse_error = None parse_result = "" try: if input_string.startswith("["): eft_parser = conversions.eft_parsing(input_string) parse_result = eft_parser[3] # DNA String parse_error = eft_parser[4] else: parse_array = [] item_input, item_qty = conversions.manual_parsing(input_string)[1:3] pre_parse_db = g.mongo.db.items.find({"name": {"$in": item_input}}) for pre_parse_item in pre_parse_db: parse_array.append(str(pre_parse_item["_id"]) + ";" + str(item_qty[pre_parse_item["name"].upper()])) if len(parse_array) != len(item_input): error_string = "There is an item that could not be parsed. Check your input and try again." parse_result = ":".join(parse_array) except KeyError: error_string = "Could not parse the input. Please ensure it is correctly formatted." if parse_error: if parse_error == "parsing": error_string = "Could not parse the EFT-Formatted fit. Please ensure it is correctly formatted." else: error_string = parse_error parse_item_list = parse_result.split(":") for parse_item in parse_item_list: if parse_item: direct_info = parse_item.split(";") if len(direct_info) == 1: bulk_op_update.find({"_id": session["CharacterOwnerHash"]}).upsert().update({ "$inc": {"items." + direct_info[0]: 1}}) bulk_run_update = True else: bulk_op_update.find({"_id": session["CharacterOwnerHash"]}).upsert().update({ "$inc": {"items." + direct_info[0]: int(direct_info[1])}}) bulk_run_update = True if bulk_run_update: bulk_op_update.execute() if item or input_string: flash(error_string) return redirect(url_for("ordering.home")) else: error_string = get_flashed_messages() error_string = error_string[0] if error_string else None # Load cart total_volume = 0 sell_price = 0 current_cart = g.mongo.db.carts.find_one({"_id": session["CharacterOwnerHash"]}) fittings_info = [] fittings_breakdown = {} # Redirect to fittings if saving pack ordering_admin = auth_check("ordering_admin") if ordering_admin and request.form.get("action") == "pack": pack_fit = { "fit": "", "items": {}, "submitter": session["CharacterOwnerHash"], "price": 0, "volume": 0, "name": request.form.get("pack"), "notes": None, "dna": None, "ship": "Pack", "source": None, "doctrine": False } fit_array = [] dna_array = [] for table_key, table_info in current_cart.get("item_table", {}).items(): pack_fit["items"][table_info["name"]] = table_info["qty"] fit_array.append(table_info["name"] + " " + str(table_info["qty"])) dna_array.append(table_key + ";" + str(table_info["qty"])) pack_fit["fit"] = "\n".join(fit_array) pack_fit["dna"] = ":".join(dna_array) fit_id = g.mongo.db.fittings.insert(pack_fit) return redirect(url_for("fittings.fit", fit_id=fit_id)) # Determine processing cost order_db = g.mongo.db.preferences.find_one({"_id": "ordering"}) if session["UI_Corporation"]: order_tax = order_db.get("tax_corp", 0) if order_db else 0 else: order_tax = order_db.get("tax", 0) if order_db else 0 # Continue loading cart if current_cart and current_cart.get("items"): cart_item_list_pre = current_cart["items"] # Filter fittings from items fittings_id_list = [] for cart_item_id, cart_item_qty in cart_item_list_pre.items(): try: fittings_id_list.append(ObjectId(cart_item_id)) except bson.errors.InvalidId: cart_item_list[cart_item_id] = cart_item_qty # Unpack fittings for selected_fit in g.mongo.db.fittings.find({"_id": {"$in": fittings_id_list}}): fit_item_list = selected_fit["dna"].split(":") fittings_info.append([str(selected_fit["_id"]), "[Fit] " + selected_fit["name"], current_cart["items"][str(selected_fit["_id"])], "{:,.02f}".format(selected_fit["volume"]), "{:,.02f}".format(selected_fit["price"] * (1 + order_tax / 100)), "{:,.02f}".format(selected_fit["volume"] * current_cart["items"][str(selected_fit["_id"])]), "{:,.02f}".format(selected_fit["price"] * current_cart["items"][str(selected_fit["_id"])] * (1 + order_tax / 100)) ]) sell_price += selected_fit["price"] * current_cart["items"][str(selected_fit["_id"])] for fit_item in fit_item_list: if fit_item: item_info = fit_item.split(";") fittings_breakdown.setdefault(item_info[0], 0) if len(item_info) == 1: fittings_breakdown[item_info[0]] += 1 * current_cart["items"][str(selected_fit["_id"])] else: fittings_breakdown[item_info[0]] += int(item_info[1]) * current_cart["items"][ str(selected_fit["_id"])] cart_item_list_int = [int(x) for x in cart_item_list.keys()] fittings_breakdown_int = [int(x) for x in fittings_breakdown.keys()] prices_int = cart_item_list_int + fittings_breakdown_int prices, prices_usable = market_hub_prices(prices_int) if prices_int else ({}, True) full_cart = {} invoice_info = [["Name", "Qty", "Vol/Item", "Isk/Item + Markup", "Vol Subtotal", "Isk Subtotal w/ Markup"]] + fittings_info for db_item in g.mongo.db.items.find({"_id": {"$in": cart_item_list_int}}): invoice_info.append([ db_item["_id"], db_item["name"], cart_item_list[str(db_item["_id"])], "{:,.02f}".format(db_item["volume"]), "{:,.02f}".format(prices[db_item["_id"]]["sell"] * (1 + order_tax / 100)), "{:,.02f}".format(db_item["volume"] * cart_item_list[str(db_item["_id"])]), "{:,.02f}".format(prices[db_item["_id"]]["sell"] * cart_item_list[str(db_item["_id"])] * (1 + order_tax / 100)) ]) full_cart[str(db_item["_id"])] = { "name": db_item["name"], "qty": cart_item_list[str(db_item["_id"])], "volume": db_item["volume"], "price": prices[db_item["_id"]]["sell"], "volume_total": db_item["volume"] * cart_item_list[str(db_item["_id"])], "price_total": prices[db_item["_id"]]["sell"] * cart_item_list[str(db_item["_id"])] } total_volume += db_item["volume"] * cart_item_list[str(db_item["_id"])] sell_price += prices[db_item["_id"]]["sell"] * cart_item_list[str(db_item["_id"])] breakdown_info = [["Name", "Qty", "Vol/Item", "Isk/Item + Markup", "Vol Subtotal", "Isk Subtotal w/ Markup"]] for db_item_breakdown in g.mongo.db.items.find({"_id": {"$in": fittings_breakdown_int}}): breakdown_info.append([ db_item_breakdown["_id"], db_item_breakdown["name"], fittings_breakdown[str(db_item_breakdown["_id"])], "{:,.02f}".format(db_item_breakdown["volume"]), "{:,.02f}".format(prices[int(db_item_breakdown["_id"])]["sell"] * (1 + order_tax / 100)), "{:,.02f}".format(db_item_breakdown["volume"] * fittings_breakdown[str(db_item_breakdown["_id"])]), "{:,.02f}".format(prices[int(db_item_breakdown["_id"])]["sell"] * fittings_breakdown[str(db_item_breakdown["_id"])] * (1 + order_tax / 100)) ]) total_volume += db_item_breakdown["volume"] * fittings_breakdown[str(db_item_breakdown["_id"])] if full_cart.get(str(db_item_breakdown["_id"])): full_cart[str(db_item_breakdown["_id"])]["qty"] += fittings_breakdown[str(db_item_breakdown["_id"])] full_cart[str(db_item_breakdown["_id"])]["volume_total"] += (db_item_breakdown["volume"] * fittings_breakdown[ str(db_item_breakdown["_id"])]) full_cart[str(db_item_breakdown["_id"])]["price_total"] += (prices[int(db_item_breakdown["_id"])]["sell"] * fittings_breakdown[ str(db_item_breakdown["_id"])]) else: full_cart[str(db_item_breakdown["_id"])] = { "id": db_item_breakdown["_id"], "name": db_item_breakdown["name"], "qty": fittings_breakdown[str(db_item_breakdown["_id"])], "volume": db_item_breakdown["volume"], "price": prices[int(db_item_breakdown["_id"])]["sell"], "volume_total": db_item_breakdown["volume"] * fittings_breakdown[str(db_item_breakdown["_id"])], "price_total": (prices[int(db_item_breakdown["_id"])]["sell"] * fittings_breakdown[str(db_item_breakdown["_id"])]) } # List routes with open("configs/base.json") as base_config_file: base_config = json.load(base_config_file) market_hub_name = base_config["market_hub_name"] min_id_limit = base_config["market_hub_station"] * 100000000 max_id_limit = base_config["market_hub_station"] * 100000000 + 100000000 market_hub_routes = g.mongo.db.jf_routes.find({"_id": {"$gte": min_id_limit, "$lt": max_id_limit}}) if request.args.get("end"): selected_route = int(request.args.get("end")) else: selected_route = 0 valid_stations = [] route_name = "" new_cart = g.mongo.db.carts.find_one({"_id": session["CharacterOwnerHash"]}) for route in market_hub_routes: if request.args.get("end"): if route["_id"] == int(request.args.get("end")): valid_stations.append([route["_id"], route["end"], True]) selected_route = route["_id"] if selected_route == 0 else selected_route route_name = route["end"] else: valid_stations.append([route["_id"], route["end"], False]) elif new_cart: if route["_id"] == new_cart.get("route"): valid_stations.append([route["_id"], route["end"], True]) selected_route = route["_id"] if selected_route == 0 else selected_route route_name = route["end"] else: valid_stations.append([route["_id"], route["end"], False]) elif not request.args.get("end") and route["end"] == base_config["default_ship_to"]: valid_stations.append([route["_id"], route["end"], True]) selected_route = route["_id"] if selected_route == 0 else selected_route route_name = route["end"] else: valid_stations.append([route["_id"], route["end"], False]) # JF Calculations if selected_route == 0: selected_route = int(str(base_config["market_hub_station"]) + str(base_config["home_station"])) selected_route_info = g.mongo.db.jf_routes.find_one({"_id": selected_route}) if selected_route_info: rate_info = conversions.valid_value(selected_route_info["prices"], time.time()) if session.get("UI_Corporation"): jf_rate = rate_info["corp"] else: jf_rate = rate_info["general"] jf_total = jf_rate * total_volume # Min 1 Mil Isk if jf_total < 1000000: jf_total = 1000000 else: jf_rate = 0 jf_total = 0 order_tax_total = sell_price * order_tax / 100 order_total = jf_total + sell_price + order_tax_total # List of characters and notes character_list = [] db_api_list = g.mongo.db.api_keys.find_one({"_id": session["CharacterOwnerHash"]}) if not request.args.get("action") == "character" and current_cart and request.args.get("action") != "order": current_character = current_cart.get("contract_to") elif request.args.get("character"): current_character = request.args.get("character") else: current_character = session["CharacterName"] if not request.args.get("action") == "notes" and current_cart and request.args.get("action") != "order": notes = current_cart.get("notes", "") else: notes = request.args.get("notes", "") if db_api_list: for character in db_api_list["keys"]: if character["character_name"] == current_character: character_list.append((character["character_name"], True)) else: character_list.append((character["character_name"], False)) # Update DB g.mongo.db.carts.update({"_id": session["CharacterOwnerHash"]}, {"$set": { "item_table": full_cart, "route": selected_route, "volume": total_volume, "jf_rate": jf_rate, "jf_total": jf_total, "sell_price": sell_price, "order_total": order_total, "jf_end": route_name, "order_tax": order_tax, "order_tax_total": order_tax_total, "prices_usable": prices_usable, "notes": notes, "contract_to": current_character }}, upsert=True) if request.args.get("action") == "order": return redirect(url_for("ordering.invoice")) # Round order total order_total = round(order_total + 50000, -5) # Formatting total_volume = "{:,.02f}".format(total_volume) sell_price = "{:,.02f}".format(sell_price) jf_total = "{:,.02f}".format(jf_total) order_total = "{:,.02f}".format(order_total) order_tax = "{:,.02f}".format(order_tax) order_tax_total = "{:,.02f}".format(order_tax_total) return render_template("ordering.html", invoice_info=invoice_info, total_volume=total_volume, sell_price=sell_price, valid_stations=valid_stations, jf_rate=jf_rate, jf_total=jf_total, order_total=order_total, market_hub_name=market_hub_name, prices_usable=prices_usable, error_string=error_string, breakdown_info=breakdown_info, order_tax=order_tax, order_tax_total=order_tax_total, character_list=character_list, notes=notes, ordering_admin=ordering_admin)
def home(): prices_usable = True quote_ran = False error_id = request.args.get("error_id") if not error_id: error_list = [] else: error_list = ["Quote of id '{}' cannot be found. It's probably really old.".format(error_id)] if request.method == "POST" or request.form.get("action") == "quote": quote_ran = True with open("configs/base.json", "r") as base_config_file: base_config = json.load(base_config_file) # JF Calculations hub_to_home = base_config["market_hub_station"] * 100000000 + base_config["home_station"] selected_route_info = g.mongo.db.jf_routes.find_one({"_id": hub_to_home}) if selected_route_info: rate_info = conversions.valid_value(selected_route_info["prices"], time.time()) if session.get("UI_Corporation"): jf_rate = rate_info["corp"] else: jf_rate = rate_info["general"] else: jf_rate = 0 # Parsing if request.form.get("action") != "quote": input_string = request.form.get("input") else: input_string = request.form.get("saved_input").replace("|", "\n") item_names, item_input, item_qty, extra_errors = conversions.manual_parsing(input_string) refine_character = g.mongo.db.preferences.find_one({"_id": "refine_character"}) if refine_character: refine_id = refine_character["character_id"] else: refine_id = 0 item_prices, material_prices_list, item_materials, prices_usable = price_calc(item_input, refine_id, jf_rate) item_list = g.mongo.db.items.find({"name": {"$in": item_input}}) # Headers material_id_list = list(set(chain(*[x.keys() for x in item_materials.values()]))) material_name_db = g.mongo.db.items.find({"_id": {"$in": material_id_list}}) material_header = [] for material_name in material_name_db: material_header.append((material_name["_id"], material_name["name"])) material_header.sort(key=lambda x: x[0]) item_table = [["Name", "Qty"] + [x[1] for x in material_header]] price_table = [[ "Name", "Qty", "Our Price / Item", "Tax %", "No Tax", base_config["market_hub_name"] + " buy", base_config["market_hub_name"] + " sell", "Volume", "JF Price", "Sub Total" ]] # Items total_price = 0 total_buy_delta = 0 total_sell_delta = 0 parsed_item_list = [] for output_item in item_list: parsed_item_list.append(output_item["name"].upper()) jf_price = output_item["volume"] * jf_rate # Deltas buy_delta = item_prices[output_item["_id"]]["total"] - (item_prices[output_item["_id"]]["buy"] - jf_price) sell_delta = item_prices[output_item["_id"]]["total"] - (item_prices[output_item["_id"]]["sell"] - jf_price) total_price += item_prices[output_item["_id"]]["total"] * item_qty[output_item["name"].upper()] total_buy_delta += buy_delta * item_qty[output_item["name"].upper()] total_sell_delta += sell_delta * item_qty[output_item["name"].upper()] materials_row = [math.floor(item_materials[output_item["_id"]].get(x[0], 0) * item_qty[output_item["name"].upper()]) for x in material_header] # noinspection PyTypeChecker item_table.append([output_item["name"], item_qty[output_item["name"].upper()]] + materials_row) price_table.append([ output_item["name"], item_qty[output_item["name"].upper()], item_prices[output_item["_id"]]["total"], item_prices[output_item["_id"]]["tax"], item_prices[output_item["_id"]]["no_tax"], item_prices[output_item["_id"]]["buy"], item_prices[output_item["_id"]]["sell"], output_item["volume"], jf_price, item_prices[output_item["_id"]]["total"] * item_qty[output_item["name"].upper()] ]) # Check if all items parsed for item in item_names: if item.upper() not in parsed_item_list: error_list.append("The item '{}' could not be found.".format(item)) # Materials material_table = [["Name", base_config["market_hub_name"] + " buy", base_config["market_hub_name"] + " sell"]] for material_id, material_name in material_header: material_table.append([material_name, material_prices_list[material_id]["buy"], material_prices_list[material_id]["sell"]]) # Formatting item_table = [item_table[0]] + [row[:1] + ["{:,.02f}".format(value) for value in row[1:]] for row in item_table[1:]] price_table = [price_table[0]] + [row[:2] + ["{:,.02f}".format(value) for value in row[2:]] for row in price_table[1:]] material_table = [material_table[0]] + [row[:1] + ["{:,.02f}".format(value) for value in row[1:]] for row in material_table[1:]] total_buy_delta = "{:,.02f}".format(total_buy_delta) total_sell_delta = "{:,.02f}".format(total_sell_delta) if total_price < 100000: total_price = "{:,.02f}".format(total_price) else: total_price = "{:,.02f}".format(round(total_price + 50000, -5)) # GUI Tables quick_table = [x[:3] + [x[-1]] for x in price_table] else: item_table = [] price_table = [] material_table = [] total_buy_delta = 0 total_sell_delta = 0 total_price = 0 quick_table = [] input_string = None # Quote Saving if request.form.get("action") == "quote": quote_id = g.mongo.db.buyback_quotes.insert_one({ "item_table": item_table, "price_table": price_table, "material_table": material_table, "total_buy_delta": total_buy_delta, "total_sell_delta": total_sell_delta, "total_price": total_price, "quick_table": quick_table, "date_added": time.time() }) return redirect(url_for("buyback.quote", quote_id=quote_id.inserted_id)) input_string = input_string.strip().replace("\r\n", "\n").replace("\n", "|") if input_string else None return render_template("buyback.html", item_table=item_table, price_table=price_table, material_table=material_table, total_buy_delta=total_buy_delta, total_sell_delta=total_sell_delta, total_price=total_price, quick_table=quick_table, error_list=error_list, quote=quote_ran, input_string=input_string, prices_usable=prices_usable)
def home(item=""): cart_item_list = {} error_string = request.args.get("error_string") bulk_op_update = g.mongo.db.carts.initialize_unordered_bulk_op() bulk_run_update = False # Change qty if post from this page if request.method == "POST" and request.form.get("action") == "qty": for key, value in request.form.items(): if key != "action" and not key.startswith("DataTables"): if value.strip(): if int(float(value)) != 0: bulk_op_update.find({ "_id": session["CharacterOwnerHash"] }).upsert().update( {"$set": { "items." + key: int(float(value)) }}) else: bulk_op_update.find({ "_id": session["CharacterOwnerHash"] }).upsert().update( {"$unset": { "items." + key: int(float(value)) }}) bulk_run_update = True if request.method == "POST" and request.form.get("action") == "clear": g.mongo.db.carts.remove({"_id": session["CharacterOwnerHash"]}) # Add new item to database input_string = request.form.get("parse", session.get("fitting")) if item or input_string: if item: try: item_adjustment_list = item.split(":") for adjustment_item in item_adjustment_list: adjustment_item_info = adjustment_item.split(";") if request.args.get("action") == "edit": bulk_op_update.find({ "_id": session["CharacterOwnerHash"] }).upsert().update({ "$set": { "items." + adjustment_item_info[0]: int(adjustment_item_info[1]) } }) else: bulk_op_update.find({ "_id": session["CharacterOwnerHash"] }).upsert().update({ "$inc": { "items." + adjustment_item_info[0]: int(adjustment_item_info[1]) } }) bulk_run_update = True except IndexError: error_string = "Was not able to add {}".format(item) elif input_string: session.pop("fitting", None) parse_error = None parse_result = "" try: if input_string.startswith("["): eft_parser = conversions.eft_parsing(input_string) parse_result = eft_parser[3] # DNA String parse_error = eft_parser[4] else: parse_array = [] item_input, item_qty = conversions.manual_parsing( input_string)[1:3] pre_parse_db = g.mongo.db.items.find( {"name": { "$in": item_input }}) for pre_parse_item in pre_parse_db: parse_array.append( str(pre_parse_item["_id"]) + ";" + str(item_qty[pre_parse_item["name"].upper()])) if len(parse_array) != len(item_input): error_string = "There is an item that could not be parsed. Check your input and try again." parse_result = ":".join(parse_array) except KeyError: error_string = "Could not parse the input. Please ensure it is correctly formatted." if parse_error: if parse_error == "parsing": error_string = "Could not parse the EFT-Formatted fit. Please ensure it is correctly formatted." else: error_string = parse_error parse_item_list = parse_result.split(":") for parse_item in parse_item_list: if parse_item: direct_info = parse_item.split(";") if len(direct_info) == 1: bulk_op_update.find({ "_id": session["CharacterOwnerHash"] }).upsert().update( {"$inc": { "items." + direct_info[0]: 1 }}) bulk_run_update = True else: bulk_op_update.find({ "_id": session["CharacterOwnerHash"] }).upsert().update({ "$inc": { "items." + direct_info[0]: int(direct_info[1]) } }) bulk_run_update = True if bulk_run_update: bulk_op_update.execute() if item or input_string: flash(error_string) return redirect(url_for("ordering.home")) else: error_string = get_flashed_messages() error_string = error_string[0] if error_string else None # Load cart total_volume = 0 sell_price = 0 current_cart = g.mongo.db.carts.find_one( {"_id": session["CharacterOwnerHash"]}) fittings_info = [] fittings_breakdown = {} # Redirect to fittings if saving pack ordering_admin = auth_check("ordering_admin") if ordering_admin and request.form.get("action") == "pack": pack_fit = { "fit": "", "items": {}, "submitter": session["CharacterOwnerHash"], "price": 0, "volume": 0, "name": request.form.get("pack"), "notes": None, "dna": None, "ship": "Pack", "source": None, "doctrine": False } fit_array = [] dna_array = [] for table_key, table_info in current_cart.get("item_table", {}).items(): pack_fit["items"][table_info["name"]] = table_info["qty"] fit_array.append(table_info["name"] + " " + str(table_info["qty"])) dna_array.append(table_key + ";" + str(table_info["qty"])) pack_fit["fit"] = "\n".join(fit_array) pack_fit["dna"] = ":".join(dna_array) fit_id = g.mongo.db.fittings.insert(pack_fit) return redirect(url_for("fittings.fit", fit_id=fit_id)) # Determine processing cost order_db = g.mongo.db.preferences.find_one({"_id": "ordering"}) if session["UI_Corporation"]: order_tax = order_db.get("tax_corp", 0) if order_db else 0 else: order_tax = order_db.get("tax", 0) if order_db else 0 # Continue loading cart if current_cart and current_cart.get("items"): cart_item_list_pre = current_cart["items"] # Filter fittings from items fittings_id_list = [] for cart_item_id, cart_item_qty in cart_item_list_pre.items(): try: fittings_id_list.append(ObjectId(cart_item_id)) except bson.errors.InvalidId: cart_item_list[cart_item_id] = cart_item_qty # Unpack fittings for selected_fit in g.mongo.db.fittings.find( {"_id": { "$in": fittings_id_list }}): fit_item_list = selected_fit["dna"].split(":") fittings_info.append([ str(selected_fit["_id"]), "[Fit] " + selected_fit["name"], current_cart["items"][str(selected_fit["_id"])], "{:,.02f}".format(selected_fit["volume"]), "{:,.02f}".format( selected_fit["price"] * (1 + order_tax / 100)), "{:,.02f}".format( selected_fit["volume"] * current_cart["items"][str(selected_fit["_id"])]), "{:,.02f}".format( selected_fit["price"] * current_cart["items"][str(selected_fit["_id"])] * (1 + order_tax / 100)) ]) sell_price += selected_fit["price"] * current_cart["items"][str( selected_fit["_id"])] for fit_item in fit_item_list: if fit_item: item_info = fit_item.split(";") fittings_breakdown.setdefault(item_info[0], 0) if len(item_info) == 1: fittings_breakdown[ item_info[0]] += 1 * current_cart["items"][str( selected_fit["_id"])] else: fittings_breakdown[item_info[0]] += int( item_info[1]) * current_cart["items"][str( selected_fit["_id"])] cart_item_list_int = [int(x) for x in cart_item_list.keys()] fittings_breakdown_int = [int(x) for x in fittings_breakdown.keys()] prices_int = cart_item_list_int + fittings_breakdown_int prices, prices_usable = market_hub_prices(prices_int) if prices_int else ( {}, True) full_cart = {} invoice_info = [[ "Name", "Qty", "Vol/Item", "Isk/Item + Markup", "Vol Subtotal", "Isk Subtotal w/ Markup" ]] + fittings_info for db_item in g.mongo.db.items.find({"_id": {"$in": cart_item_list_int}}): invoice_info.append([ db_item["_id"], db_item["name"], cart_item_list[str(db_item["_id"])], "{:,.02f}".format(db_item["volume"]), "{:,.02f}".format( prices[db_item["_id"]]["sell"] * (1 + order_tax / 100)), "{:,.02f}".format(db_item["volume"] * cart_item_list[str(db_item["_id"])]), "{:,.02f}".format(prices[db_item["_id"]]["sell"] * cart_item_list[str(db_item["_id"])] * (1 + order_tax / 100)) ]) full_cart[str(db_item["_id"])] = { "name": db_item["name"], "qty": cart_item_list[str(db_item["_id"])], "volume": db_item["volume"], "price": prices[db_item["_id"]]["sell"], "volume_total": db_item["volume"] * cart_item_list[str(db_item["_id"])], "price_total": prices[db_item["_id"]]["sell"] * cart_item_list[str(db_item["_id"])] } total_volume += db_item["volume"] * cart_item_list[str(db_item["_id"])] sell_price += prices[db_item["_id"]]["sell"] * cart_item_list[str( db_item["_id"])] breakdown_info = [[ "Name", "Qty", "Vol/Item", "Isk/Item + Markup", "Vol Subtotal", "Isk Subtotal w/ Markup" ]] for db_item_breakdown in g.mongo.db.items.find( {"_id": { "$in": fittings_breakdown_int }}): breakdown_info.append([ db_item_breakdown["_id"], db_item_breakdown["name"], fittings_breakdown[str(db_item_breakdown["_id"])], "{:,.02f}".format(db_item_breakdown["volume"]), "{:,.02f}".format(prices[int(db_item_breakdown["_id"])]["sell"] * (1 + order_tax / 100)), "{:,.02f}".format( db_item_breakdown["volume"] * fittings_breakdown[str(db_item_breakdown["_id"])]), "{:,.02f}".format( prices[int(db_item_breakdown["_id"])]["sell"] * fittings_breakdown[str(db_item_breakdown["_id"])] * (1 + order_tax / 100)) ]) total_volume += db_item_breakdown["volume"] * fittings_breakdown[str( db_item_breakdown["_id"])] if full_cart.get(str(db_item_breakdown["_id"])): full_cart[str( db_item_breakdown["_id"])]["qty"] += fittings_breakdown[str( db_item_breakdown["_id"])] full_cart[str(db_item_breakdown["_id"])]["volume_total"] += ( db_item_breakdown["volume"] * fittings_breakdown[str(db_item_breakdown["_id"])]) full_cart[str(db_item_breakdown["_id"])]["price_total"] += ( prices[int(db_item_breakdown["_id"])]["sell"] * fittings_breakdown[str(db_item_breakdown["_id"])]) else: full_cart[str(db_item_breakdown["_id"])] = { "id": db_item_breakdown["_id"], "name": db_item_breakdown["name"], "qty": fittings_breakdown[str(db_item_breakdown["_id"])], "volume": db_item_breakdown["volume"], "price": prices[int(db_item_breakdown["_id"])]["sell"], "volume_total": db_item_breakdown["volume"] * fittings_breakdown[str(db_item_breakdown["_id"])], "price_total": (prices[int(db_item_breakdown["_id"])]["sell"] * fittings_breakdown[str(db_item_breakdown["_id"])]) } # List routes with open("configs/base.json") as base_config_file: base_config = json.load(base_config_file) market_hub_name = base_config["market_hub_name"] min_id_limit = base_config["market_hub_station"] * 100000000 max_id_limit = base_config["market_hub_station"] * 100000000 + 100000000 market_hub_routes = g.mongo.db.jf_routes.find( {"_id": { "$gte": min_id_limit, "$lt": max_id_limit }}) if request.args.get("end"): selected_route = int(request.args.get("end")) else: selected_route = 0 valid_stations = [] route_name = "" new_cart = g.mongo.db.carts.find_one( {"_id": session["CharacterOwnerHash"]}) for route in market_hub_routes: if request.args.get("end"): if route["_id"] == int(request.args.get("end")): valid_stations.append([route["_id"], route["end"], True]) selected_route = route[ "_id"] if selected_route == 0 else selected_route route_name = route["end"] else: valid_stations.append([route["_id"], route["end"], False]) elif new_cart: if route["_id"] == new_cart.get("route"): valid_stations.append([route["_id"], route["end"], True]) selected_route = route[ "_id"] if selected_route == 0 else selected_route route_name = route["end"] else: valid_stations.append([route["_id"], route["end"], False]) elif not request.args.get( "end") and route["end"] == base_config["default_ship_to"]: valid_stations.append([route["_id"], route["end"], True]) selected_route = route[ "_id"] if selected_route == 0 else selected_route route_name = route["end"] else: valid_stations.append([route["_id"], route["end"], False]) # JF Calculations if selected_route == 0: selected_route = int( str(base_config["market_hub_station"]) + str(base_config["home_station"])) selected_route_info = g.mongo.db.jf_routes.find_one( {"_id": selected_route}) if selected_route_info: rate_info = conversions.valid_value(selected_route_info["prices"], time.time()) if session.get("UI_Corporation"): jf_rate = rate_info["corp"] else: jf_rate = rate_info["general"] jf_total = jf_rate * total_volume # Min 1 Mil Isk if jf_total < 1000000: jf_total = 1000000 else: jf_rate = 0 jf_total = 0 order_tax_total = sell_price * order_tax / 100 order_total = jf_total + sell_price + order_tax_total # List of characters and notes character_list = [] db_api_list = g.mongo.db.api_keys.find_one( {"_id": session["CharacterOwnerHash"]}) if not request.args.get( "action") == "character" and current_cart and request.args.get( "action") != "order": current_character = current_cart.get("contract_to") elif request.args.get("character"): current_character = request.args.get("character") else: current_character = session["CharacterName"] if not request.args.get( "action") == "notes" and current_cart and request.args.get( "action") != "order": notes = current_cart.get("notes", "") else: notes = request.args.get("notes", "") if db_api_list: for character in db_api_list["keys"]: if character["character_name"] == current_character: character_list.append((character["character_name"], True)) else: character_list.append((character["character_name"], False)) # Update DB g.mongo.db.carts.update({"_id": session["CharacterOwnerHash"]}, { "$set": { "item_table": full_cart, "route": selected_route, "volume": total_volume, "jf_rate": jf_rate, "jf_total": jf_total, "sell_price": sell_price, "order_total": order_total, "jf_end": route_name, "order_tax": order_tax, "order_tax_total": order_tax_total, "prices_usable": prices_usable, "notes": notes, "contract_to": current_character } }, upsert=True) if request.args.get("action") == "order": return redirect(url_for("ordering.invoice")) # Round order total order_total = round(order_total + 50000, -5) # Formatting total_volume = "{:,.02f}".format(total_volume) sell_price = "{:,.02f}".format(sell_price) jf_total = "{:,.02f}".format(jf_total) order_total = "{:,.02f}".format(order_total) order_tax = "{:,.02f}".format(order_tax) order_tax_total = "{:,.02f}".format(order_tax_total) return render_template("ordering.html", invoice_info=invoice_info, total_volume=total_volume, sell_price=sell_price, valid_stations=valid_stations, jf_rate=jf_rate, jf_total=jf_total, order_total=order_total, market_hub_name=market_hub_name, prices_usable=prices_usable, error_string=error_string, breakdown_info=breakdown_info, order_tax=order_tax, order_tax_total=order_tax_total, character_list=character_list, notes=notes, ordering_admin=ordering_admin)
def fit(fit_id=None): if not fit_id: abort(404) # Redirect if fit is purchased if request.method == "GET" and request.args.get("action") == "purchase": return redirect(url_for("ordering.home", item=fit_id + ";" + request.args.get("multiply", 1))) selected_fit = None try: selected_fit = g.mongo.db.fittings.find_one({"_id": ObjectId(fit_id)}) except bson.errors.InvalidId: abort(404) if not selected_fit: return redirect(url_for("fittings.home", error="not_found")) elif request.method == "GET" and request.args.get("action") == "direct": # Use direct to cart session["fitting"] = selected_fit["fit"] return redirect(url_for("ordering.home")) # Check if fittings admin admin = auth_check("fittings_admin") # Delete Permissions if selected_fit["submitter"] == session["CharacterOwnerHash"] or admin: can_delete = True else: can_delete = False # Modifications notes_change = request.args.get("notes") if request.args.get("notes") else selected_fit["notes"] source_change = request.args.get("source") if request.args.get("source") else selected_fit.get("source") doctrine_change = bool(request.args.get("doctrine")) if request.args.get("doctrine") else False if request.args.get("action") == "delete" and can_delete: g.mongo.db.fittings.remove({"_id": ObjectId(fit_id)}) return redirect(url_for("fittings.home")) elif request.args.get("action") == "edit" and can_delete: g.mongo.db.fittings.update({"_id": ObjectId(fit_id)}, {"$set": { "notes": notes_change, "source": source_change, "doctrine": doctrine_change }}) return redirect(url_for("fittings.fit", fit_id=fit_id)) fit_by_line = selected_fit["fit"].splitlines() with open("resources/nameConversions.json") as name_conversions_file: name_conversions = json.load(name_conversions_file) actual_fit_items = [] pre_name_conversion = {} for pre_filter_item in selected_fit["items"].keys(): pre_filter_item = pre_filter_item.replace("Thermic", "Thermal") # EVE parallax patch actual_fit_items.append(name_conversions.get(pre_filter_item, pre_filter_item)) if name_conversions.get(pre_filter_item): pre_name_conversion[name_conversions.get(pre_filter_item)] = pre_filter_item # ID Matching item_list = list(g.mongo.db.items.find({"name": {"$in": actual_fit_items}})) item_prices, prices_usable = eve_central.market_hub_prices([x["_id"] for x in item_list]) item_table = [["Name", "Qty", "Isk/Item", "Vol/Item", "Total Isk", "Total Volume"]] total_fit_isk = 0 total_volume = 0 multiply = 1 if not request.args.get("multiply") else int(request.args.get("multiply")) for fit_item in item_list: try: # Check if thermal is correct in fit qty = selected_fit["items"][fit_item["name"]] * multiply except KeyError: try: # Check if it's an old fit with thermic qty = selected_fit["items"][fit_item["name"].replace("Thermal", "Thermic")] * multiply except KeyError: # Check if the item name has changed qty = selected_fit["items"][pre_name_conversion[fit_item["name"]]] * multiply isk_per_item = item_prices[fit_item["_id"]]["sell"] vol_per_item = fit_item["volume"] item_isk_total = qty * isk_per_item item_vol_total = qty * vol_per_item total_fit_isk += item_isk_total total_volume += item_vol_total # Formatting isk_per_item = "{:,.02f}".format(isk_per_item) vol_per_item = "{:,.02f}".format(vol_per_item) item_isk_total = "{:,.02f}".format(item_isk_total) item_vol_total = "{:,.02f}".format(item_vol_total) item_table.append([fit_item["name"], qty, isk_per_item, vol_per_item, item_isk_total, item_vol_total]) if multiply == 1 and prices_usable: g.mongo.db.fittings.update({"_id": ObjectId(fit_id)}, {"$set": { "price": total_fit_isk, "volume": total_volume}}) # List routes with open("configs/base.json") as base_config_file: base_config = json.load(base_config_file) market_hub_name = base_config["market_hub_name"] min_id_limit = base_config["market_hub_station"] * 100000000 max_id_limit = base_config["market_hub_station"] * 100000000 + 100000000 market_hub_routes = g.mongo.db.jf_routes.find({"_id": {"$gte": min_id_limit, "$lt": max_id_limit}}) if request.args.get("end"): selected_route = int(request.args.get("end")) else: selected_route = 0 valid_stations = [] current_route = g.mongo.db.carts.find_one({"_id": session["CharacterOwnerHash"]}) for route in market_hub_routes: if request.args.get("end") and route["_id"] == int(request.args.get("end")): valid_stations.append([route["_id"], route["end"], True]) g.mongo.db.carts.update({"_id": session["CharacterOwnerHash"]}, {"$set": {"route": route["_id"]}}, upsert=True) elif current_route: if route["_id"] == current_route.get("route"): valid_stations.append([route["_id"], route["end"], True]) selected_route = route["_id"] if selected_route == 0 else selected_route else: valid_stations.append([route["_id"], route["end"], False]) elif not request.args.get("end") and route["end"] == base_config["default_ship_to"]: valid_stations.append([route["_id"], route["end"], True]) g.mongo.db.carts.update({"_id": session["CharacterOwnerHash"]}, {"$set": {"route": route["_id"]}}, upsert=True) selected_route = route["_id"] if selected_route == 0 else selected_route else: valid_stations.append([route["_id"], route["end"], False]) # JF Calculations selected_route_info = g.mongo.db.jf_routes.find_one({"_id": selected_route}) if selected_route_info: rate_info = conversions.valid_value(selected_route_info["prices"], time.time()) if session.get("UI_Corporation"): jf_rate = rate_info["corp"] else: jf_rate = rate_info["general"] jf_total = jf_rate * total_volume else: jf_rate = 0 jf_total = 0 order_total = jf_total + total_fit_isk # Submission date date_added = ObjectId(fit_id).generation_time.strftime("%Y-%m-%d %H:%M:%S") # Formatting total_fit_isk = "{:,.02f}".format(total_fit_isk) total_volume = "{:,.02f}".format(total_volume) jf_total = "{:,.02f}".format(jf_total) jf_rate = "{:,.02f}".format(jf_rate) order_total = "{:,.02f}".format(order_total) return render_template("fittings_fit.html", item_table=item_table, fit_string=fit_by_line, total_fit_isk=total_fit_isk, total_volume=total_volume, valid_stations=valid_stations, market_hub_name=market_hub_name, jf_rate=jf_rate, jf_total=jf_total, order_total=order_total, dna_string=selected_fit["dna"], fit_name=selected_fit["name"], multiply=multiply, can_delete=can_delete, notes=selected_fit["notes"], source=selected_fit.get("source"), admin=admin, doctrine=selected_fit["doctrine"], prices_usable=prices_usable, category=selected_fit.get("category"), date_added=date_added)
def admin(): route_list = [] # route = [_id, name, m3, corp] no_matches = [] general = "" corp = "" _id = "" name = "" start = "" end = "" collateral = "" edit = False if request.method == "GET": if request.args.get("action") == "delete": g.mongo.db.jf_routes.remove({"_id": int(request.args.get("_id"))}) elif request.args.get("action") == "fix": # Check station names station_name_corrector() elif request.args.get("action") == "edit": selected_route = g.mongo.db.jf_routes.find_one( {"_id": int(request.args.get("_id"))}) edit = True _id = request.args.get("_id") name = selected_route["name"] start = selected_route["start"] end = selected_route["end"] # Prices last_time = 0 general = 0 corp = 0 collateral = 0 for price in selected_route["prices"]: if price["valid_after"] > last_time: corp = price["corp"] general = price["general"] last_time = price["valid_after"] collateral = price["collateral"] elif request.args.get("action") == "all": bulk_op = g.mongo.db.jf_routes.initialize_unordered_bulk_op() bulk_run = False for route in g.mongo.db.jf_routes.find(): all_last_time = 0 all_corp = 0 all_general = 0 for price in route["prices"]: if price["valid_after"] > all_last_time: all_last_time = price["valid_after"] all_corp = price["corp"] all_general = price["general"] bulk_run = True bulk_op.find({ "_id": route["_id"] }).update({ "$push": { "prices": { "valid_after": int(time.time()), "corp": all_corp, "general": all_general, "collateral": float(request.args.get("collateral")) } } }) if bulk_run: bulk_op.execute() elif request.args.get("action") == "tax": g.mongo.db.preferences.update({"_id": "jf_insurance"}, { "$push": { "history": { "valid_after": int(time.time()), "percentage": float(request.args.get("insurance")) } } }, upsert=True) g.mongo.db.preferences.update({"_id": "jf_tax"}, { "$push": { "history": { "valid_after": int(time.time()), "percentage": float(request.args.get("tax")) } } }, upsert=True) g.mongo.db.preferences.update( {"_id": "jf_reimbursement"}, {"amount": float(request.args.get("threshold"))}, upsert=True) elif request.method == "POST": if request.form.get("action") == "single": if request.form.get("_id"): g.mongo.db.jf_routes.update( {"_id": int(request.form.get("_id"))}, { "$set": { "name": request.form.get("name"), "start": request.form.get("start").strip(), "end": request.form.get("end").strip() }, "$push": { "prices": { "valid_after": int(time.time()), "corp": float(request.form.get("corp", 0)), "general": float(request.form.get("general", 0)), "collateral": float(request.form.get("collateral", 0)) } } }, upsert=True) else: db_start = g.mongo.db.stations.find_one( {"name": request.form.get("start").strip()}) db_end = g.mongo.db.stations.find_one( {"name": request.form.get("end").strip()}) if db_start and db_end: g.mongo.db.jf_routes.update( { "_id": int(str(db_start["_id"]) + str(db_end["_id"])) }, { "$setOnInsert": { "name": request.form.get("name"), "start": request.form.get("start").strip(), "end": request.form.get("end").strip(), "prices": [{ "valid_after": int(time.time()), "corp": float(request.form.get("corp", 0)), "general": float(request.form.get("general", 0)), "collateral": float(request.form.get("collateral", 0)) }] } }, upsert=True) else: if not db_start: no_matches.append(request.form.get("start").strip()) if not db_end: no_matches.append(request.form.get("end").strip()) elif request.form.get("action") == "multiple": request_station_split = request.form.get("stations").split("\n") station_list = [ g.mongo.db.stations.find_one({"name": x.strip()}) for x in request_station_split ] if station_list and None not in station_list: # Ensure there are stations to parse and all are matched bulk_run = False bulk_op = g.mongo.db.jf_routes.initialize_unordered_bulk_op() for start_station in station_list: for end_station in station_list: if start_station != end_station: route_id = int( str(start_station["_id"]) + str(end_station["_id"])) route_name_start = start_station["name"].split( " - ")[0] route_name_end = end_station["name"].split( " - ")[0] bulk_run = True prices = [ request.form.get("corp", 0), request.form.get("general", 0), request.form.get("collateral", 0) ] prices = [ float(x) if x.strip() else 0 for x in prices ] bulk_op.find({ "_id": route_id }).upsert().update({ "$setOnInsert": { "name": route_name_start + " >> " + route_name_end, "start": start_station["name"].strip(), "end": end_station["name"].strip(), "prices": [{ "valid_after": int(time.time()), "corp": prices[0], "general": prices[1], "collateral": prices[2] }] } }) if bulk_run: bulk_op.execute() else: no_match_indexes = [ i for i, x in enumerate(station_list) if x is None ] no_matches = [ request_station_split[n] for n in no_match_indexes ] # Clear all after post general = "" corp = "" _id = "" name = "" start = "" end = "" collateral = "" edit = False for route in g.mongo.db.jf_routes.find(): last_time = 0 corp_price = 0 gen_price = 0 collateral_percent = 0 for price in route["prices"]: if price["valid_after"] > last_time: corp_price = price["corp"] gen_price = price["general"] last_time = price["valid_after"] collateral_percent = price["collateral"] route_list.append([ route["_id"], route["name"], "{:0,.2f}".format(gen_price), "{:0,.2f}".format(corp_price), "{:0,.2f}".format(collateral_percent), route["start"], route["end"] ]) db_jf_insurance = g.mongo.db.preferences.find_one({"_id": "jf_insurance"}) db_jf_tax = g.mongo.db.preferences.find_one({"_id": "jf_tax"}) db_jf_reimbursement = g.mongo.db.preferences.find_one( {"_id": "jf_reimbursement"}) if db_jf_insurance and db_jf_tax and db_jf_reimbursement: jf_insurance = conversions.valid_value(db_jf_insurance["history"], time.time()) jf_tax = conversions.valid_value(db_jf_tax["history"], time.time()) jf_reimbursement = db_jf_reimbursement["amount"] # Formatting jf_insurance = "{:.02f}%".format(jf_insurance["percentage"]) jf_tax = "{:.02f}%".format(jf_tax["percentage"]) jf_reimbursement = "{:,.02f}".format(jf_reimbursement) else: jf_insurance = "0%" jf_tax = "0%" jf_reimbursement = "0.00" return render_template("jf_admin.html", route_list=route_list, general=general, corp=corp, _id=_id, name=name, start=start, end=end, edit=edit, collateral=collateral, jf_insurance=jf_insurance, jf_tax=jf_tax, jf_reimbursement=jf_reimbursement, no_matches=no_matches)
def fit(fit_id=None): if not fit_id: abort(404) # Redirect if fit is purchased if request.method == "GET" and request.args.get("action") == "purchase": return redirect( url_for("ordering.home", item=fit_id + ";" + request.args.get("multiply", 1))) selected_fit = None try: selected_fit = g.mongo.db.fittings.find_one({"_id": ObjectId(fit_id)}) except bson.errors.InvalidId: abort(404) if not selected_fit: return redirect(url_for("fittings.home", error="not_found")) elif request.method == "GET" and request.args.get("action") == "direct": # Use direct to cart session["fitting"] = selected_fit["fit"] return redirect(url_for("ordering.home")) # Check if fittings admin admin = auth_check("fittings_admin") # Delete Permissions if selected_fit["submitter"] == session["CharacterOwnerHash"] or admin: can_delete = True else: can_delete = False # Modifications notes_change = request.args.get("notes") if request.args.get( "notes") else selected_fit["notes"] source_change = request.args.get("source") if request.args.get( "source") else selected_fit.get("source") doctrine_change = bool(request.args.get("doctrine")) if request.args.get( "doctrine") else False if request.args.get("action") == "delete" and can_delete: g.mongo.db.fittings.remove({"_id": ObjectId(fit_id)}) return redirect(url_for("fittings.home")) elif request.args.get("action") == "edit" and can_delete: g.mongo.db.fittings.update({"_id": ObjectId(fit_id)}, { "$set": { "notes": notes_change, "source": source_change, "doctrine": doctrine_change } }) return redirect(url_for("fittings.fit", fit_id=fit_id)) fit_by_line = selected_fit["fit"].splitlines() with open("resources/nameConversions.json") as name_conversions_file: name_conversions = json.load(name_conversions_file) actual_fit_items = [] pre_name_conversion = {} for pre_filter_item in selected_fit["items"].keys(): pre_filter_item = pre_filter_item.replace( "Thermic", "Thermal") # EVE parallax patch actual_fit_items.append( name_conversions.get(pre_filter_item, pre_filter_item)) if name_conversions.get(pre_filter_item): pre_name_conversion[name_conversions.get( pre_filter_item)] = pre_filter_item # ID Matching item_list = list(g.mongo.db.items.find({"name": { "$in": actual_fit_items }})) item_prices, prices_usable = eve_central.market_hub_prices( [x["_id"] for x in item_list]) item_table = [[ "Name", "Qty", "Isk/Item", "Vol/Item", "Total Isk", "Total Volume" ]] total_fit_isk = 0 total_volume = 0 multiply = 1 if not request.args.get("multiply") else int( request.args.get("multiply")) for fit_item in item_list: try: # Check if thermal is correct in fit qty = selected_fit["items"][fit_item["name"]] * multiply except KeyError: try: # Check if it's an old fit with thermic qty = selected_fit["items"][fit_item["name"].replace( "Thermal", "Thermic")] * multiply except KeyError: # Check if the item name has changed qty = selected_fit["items"][pre_name_conversion[ fit_item["name"]]] * multiply isk_per_item = item_prices[fit_item["_id"]]["sell"] vol_per_item = fit_item["volume"] item_isk_total = qty * isk_per_item item_vol_total = qty * vol_per_item total_fit_isk += item_isk_total total_volume += item_vol_total # Formatting isk_per_item = "{:,.02f}".format(isk_per_item) vol_per_item = "{:,.02f}".format(vol_per_item) item_isk_total = "{:,.02f}".format(item_isk_total) item_vol_total = "{:,.02f}".format(item_vol_total) item_table.append([ fit_item["name"], qty, isk_per_item, vol_per_item, item_isk_total, item_vol_total ]) if multiply == 1 and prices_usable: g.mongo.db.fittings.update( {"_id": ObjectId(fit_id)}, {"$set": { "price": total_fit_isk, "volume": total_volume }}) # List routes with open("configs/base.json") as base_config_file: base_config = json.load(base_config_file) market_hub_name = base_config["market_hub_name"] min_id_limit = base_config["market_hub_station"] * 100000000 max_id_limit = base_config["market_hub_station"] * 100000000 + 100000000 market_hub_routes = g.mongo.db.jf_routes.find( {"_id": { "$gte": min_id_limit, "$lt": max_id_limit }}) if request.args.get("end"): selected_route = int(request.args.get("end")) else: selected_route = 0 valid_stations = [] current_route = g.mongo.db.carts.find_one( {"_id": session["CharacterOwnerHash"]}) for route in market_hub_routes: if request.args.get("end") and route["_id"] == int( request.args.get("end")): valid_stations.append([route["_id"], route["end"], True]) g.mongo.db.carts.update({"_id": session["CharacterOwnerHash"]}, {"$set": { "route": route["_id"] }}, upsert=True) elif current_route: if route["_id"] == current_route.get("route"): valid_stations.append([route["_id"], route["end"], True]) selected_route = route[ "_id"] if selected_route == 0 else selected_route else: valid_stations.append([route["_id"], route["end"], False]) elif not request.args.get( "end") and route["end"] == base_config["default_ship_to"]: valid_stations.append([route["_id"], route["end"], True]) g.mongo.db.carts.update({"_id": session["CharacterOwnerHash"]}, {"$set": { "route": route["_id"] }}, upsert=True) selected_route = route[ "_id"] if selected_route == 0 else selected_route else: valid_stations.append([route["_id"], route["end"], False]) # JF Calculations selected_route_info = g.mongo.db.jf_routes.find_one( {"_id": selected_route}) if selected_route_info: rate_info = conversions.valid_value(selected_route_info["prices"], time.time()) if session.get("UI_Corporation"): jf_rate = rate_info["corp"] else: jf_rate = rate_info["general"] jf_total = jf_rate * total_volume else: jf_rate = 0 jf_total = 0 order_total = jf_total + total_fit_isk # Submission date date_added = ObjectId(fit_id).generation_time.strftime("%Y-%m-%d %H:%M:%S") # Formatting total_fit_isk = "{:,.02f}".format(total_fit_isk) total_volume = "{:,.02f}".format(total_volume) jf_total = "{:,.02f}".format(jf_total) jf_rate = "{:,.02f}".format(jf_rate) order_total = "{:,.02f}".format(order_total) return render_template("fittings_fit.html", item_table=item_table, fit_string=fit_by_line, total_fit_isk=total_fit_isk, total_volume=total_volume, valid_stations=valid_stations, market_hub_name=market_hub_name, jf_rate=jf_rate, jf_total=jf_total, order_total=order_total, dna_string=selected_fit["dna"], fit_name=selected_fit["name"], multiply=multiply, can_delete=can_delete, notes=selected_fit["notes"], source=selected_fit.get("source"), admin=admin, doctrine=selected_fit["doctrine"], prices_usable=prices_usable, category=selected_fit.get("category"), date_added=date_added)
def home(): prices_usable = True quote_ran = False error_id = request.args.get("error_id") if not error_id: error_list = [] else: error_list = [ "Quote of id '{}' cannot be found. It's probably really old.". format(error_id) ] if request.method == "POST" or request.form.get("action") == "quote": quote_ran = True with open("configs/base.json", "r") as base_config_file: base_config = json.load(base_config_file) # JF Calculations hub_to_home = base_config[ "market_hub_station"] * 100000000 + base_config["home_station"] selected_route_info = g.mongo.db.jf_routes.find_one( {"_id": hub_to_home}) if selected_route_info: rate_info = conversions.valid_value(selected_route_info["prices"], time.time()) if session.get("UI_Corporation"): jf_rate = rate_info["corp"] else: jf_rate = rate_info["general"] else: jf_rate = 0 # Parsing if request.form.get("action") != "quote": input_string = request.form.get("input") else: input_string = request.form.get("saved_input").replace("|", "\n") item_names, item_input, item_qty, extra_errors = conversions.manual_parsing( input_string) refine_character = g.mongo.db.preferences.find_one( {"_id": "refine_character"}) if refine_character: refine_id = refine_character["character_id"] else: refine_id = 0 item_prices, material_prices_list, item_materials, prices_usable = price_calc( item_input, refine_id, jf_rate) item_list = g.mongo.db.items.find({"name": {"$in": item_input}}) # Headers material_id_list = list( set(chain(*[x.keys() for x in item_materials.values()]))) material_name_db = g.mongo.db.items.find( {"_id": { "$in": material_id_list }}) material_header = [] for material_name in material_name_db: material_header.append( (material_name["_id"], material_name["name"])) material_header.sort(key=lambda x: x[0]) item_table = [["Name", "Qty"] + [x[1] for x in material_header]] price_table = [[ "Name", "Qty", "Our Price / Item", "Tax %", "No Tax", base_config["market_hub_name"] + " buy", base_config["market_hub_name"] + " sell", "Volume", "JF Price", "Sub Total" ]] # Items total_price = 0 total_buy_delta = 0 total_sell_delta = 0 parsed_item_list = [] for output_item in item_list: parsed_item_list.append(output_item["name"].upper()) jf_price = output_item["volume"] * jf_rate # Deltas buy_delta = item_prices[output_item["_id"]]["total"] - ( item_prices[output_item["_id"]]["buy"] - jf_price) sell_delta = item_prices[output_item["_id"]]["total"] - ( item_prices[output_item["_id"]]["sell"] - jf_price) total_price += item_prices[output_item["_id"]]["total"] * item_qty[ output_item["name"].upper()] total_buy_delta += buy_delta * item_qty[ output_item["name"].upper()] total_sell_delta += sell_delta * item_qty[ output_item["name"].upper()] materials_row = [ math.floor(item_materials[output_item["_id"]].get(x[0], 0) * item_qty[output_item["name"].upper()]) for x in material_header ] # noinspection PyTypeChecker item_table.append( [output_item["name"], item_qty[output_item["name"].upper()]] + materials_row) price_table.append([ output_item["name"], item_qty[output_item["name"].upper()], item_prices[output_item["_id"]]["total"], item_prices[output_item["_id"]]["tax"], item_prices[output_item["_id"]]["no_tax"], item_prices[output_item["_id"]]["buy"], item_prices[output_item["_id"]]["sell"], output_item["volume"], jf_price, item_prices[output_item["_id"]]["total"] * item_qty[output_item["name"].upper()] ]) # Check if all items parsed for item in item_names: if item.upper() not in parsed_item_list: error_list.append( "The item '{}' could not be found.".format(item)) # Materials material_table = [[ "Name", base_config["market_hub_name"] + " buy", base_config["market_hub_name"] + " sell" ]] for material_id, material_name in material_header: material_table.append([ material_name, material_prices_list[material_id]["buy"], material_prices_list[material_id]["sell"] ]) # Formatting item_table = [item_table[0]] + [ row[:1] + ["{:,.02f}".format(value) for value in row[1:]] for row in item_table[1:] ] price_table = [price_table[0]] + [ row[:2] + ["{:,.02f}".format(value) for value in row[2:]] for row in price_table[1:] ] material_table = [material_table[0]] + [ row[:1] + ["{:,.02f}".format(value) for value in row[1:]] for row in material_table[1:] ] total_buy_delta = "{:,.02f}".format(total_buy_delta) total_sell_delta = "{:,.02f}".format(total_sell_delta) if total_price < 100000: total_price = "{:,.02f}".format(total_price) else: total_price = "{:,.02f}".format(round(total_price + 50000, -5)) # GUI Tables quick_table = [x[:3] + [x[-1]] for x in price_table] else: item_table = [] price_table = [] material_table = [] total_buy_delta = 0 total_sell_delta = 0 total_price = 0 quick_table = [] input_string = None # Quote Saving if request.form.get("action") == "quote": quote_id = g.mongo.db.buyback_quotes.insert_one({ "item_table": item_table, "price_table": price_table, "material_table": material_table, "total_buy_delta": total_buy_delta, "total_sell_delta": total_sell_delta, "total_price": total_price, "quick_table": quick_table, "date_added": time.time() }) return redirect(url_for("buyback.quote", quote_id=quote_id.inserted_id)) input_string = input_string.strip().replace("\r\n", "\n").replace( "\n", "|") if input_string else None return render_template("buyback.html", item_table=item_table, price_table=price_table, material_table=material_table, total_buy_delta=total_buy_delta, total_sell_delta=total_sell_delta, total_price=total_price, quick_table=quick_table, error_list=error_list, quote=quote_ran, input_string=input_string, prices_usable=prices_usable)