Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show the price for the number of selected parts #496

Merged
merged 6 commits into from
Aug 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions partselector.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,23 @@ def update_subcategories(self, *_):
# search now that categories might have changed
self.search(None)

def get_price(self, quantity, prices) -> float:
"""Find the price for the number of selected parts accordning to the price ranges."""
price_ranges = prices.split(",")
min_quantity = int(price_ranges[0].split("-")[0])
if quantity <= min_quantity:
range, price = price_ranges[0].split(":")
return float(price)
for p in price_ranges:
range, price = p.split(":")
lower,upper = range.split("-")
if not upper: # upper bound of price ranges
return float(price)
lower = int(lower)
upper = int(upper)
if lower <= quantity < upper:
return float(price)

def populate_part_list(self, parts, search_duration):
"""Populate the list with the result of the search."""
search_duration_text = (
Expand All @@ -661,20 +678,10 @@ def populate_part_list(self, parts, search_duration):
self.result_count.SetLabel(f"{count} Results in {search_duration_text}")
for p in parts:
item = [str(c) for c in p]
# Munge price to be more readable
pricecol = 8 # Must match order in library.py search function
price = []
try:
for t in item[pricecol].split(","):
qty, p = t.split(":")
p = float(p)
if p < 1.0:
price.append(f"{qty}: {p * 100:.2f}c")
else:
price.append(f"{qty}: ${p:.2f}")
item[pricecol] = ", ".join(price)
except ValueError:
self.logger.warning("unable to parse price %s", item[pricecol])
pricecol = 8 # Must match order in library.py search function
price = round(self.get_price(len(self.parts), item[pricecol]) , 3)
sum = round(price * len(self.parts), 3)
item[pricecol] = f"{len(self.parts)} parts: ${price} each / ${sum} total"
self.part_list.AppendItem(item)

def select_part(self, *_):
Expand Down