107 lines
3.3 KiB
Python
107 lines
3.3 KiB
Python
import hashlib
|
|
import json
|
|
from dataclasses import dataclass, field
|
|
|
|
from .bambu_eu_variant_ids import BAMBU_EU_PRODUCTS
|
|
from .models import OrderItem
|
|
|
|
SHOP_URL = "https://eu.store.bambulab.com"
|
|
SHOP_PRODUCTS = {
|
|
name.casefold(): (slug, {row[:4]: row[4] for row in variants})
|
|
for name, (slug, variants) in BAMBU_EU_PRODUCTS.items()
|
|
}
|
|
SHOP_PRODUCT_NAMES = {"pla silk dual color": "pla silk multi-color"}
|
|
|
|
|
|
@dataclass
|
|
class CartLine:
|
|
key: str
|
|
product_url: str
|
|
exact_link: bool
|
|
entries: list[OrderItem] = field(default_factory=list)
|
|
|
|
@property
|
|
def variant(self):
|
|
return self.entries[0].variant
|
|
|
|
@property
|
|
def quantity(self):
|
|
return sum(entry.quantity for entry in self.entries)
|
|
|
|
|
|
@dataclass
|
|
class CartPlan:
|
|
lines: list[CartLine]
|
|
|
|
@property
|
|
def signature(self):
|
|
rows = [
|
|
(line.key, entry.pk, entry.variant_id, entry.quantity)
|
|
for line in self.lines
|
|
for entry in line.entries
|
|
]
|
|
prefix = OrderItem.EU_CART_QUOTE_PREFIX
|
|
digest = hashlib.sha256(json.dumps(sorted(rows)).encode()).hexdigest()
|
|
return prefix + digest[: 64 - len(prefix)]
|
|
|
|
|
|
def plan_cart(entries):
|
|
lines = {}
|
|
for entry in entries:
|
|
variant = entry.variant
|
|
if (
|
|
entry.status != OrderItem.Status.OPEN
|
|
or variant.manufacturer.name.casefold() != "bambu lab"
|
|
or not variant.is_active
|
|
):
|
|
continue
|
|
product = variant.product_type.name.strip().casefold()
|
|
slug, variants = SHOP_PRODUCTS.get(SHOP_PRODUCT_NAMES.get(product, product), ("", {}))
|
|
shop_id = variants.get(
|
|
(
|
|
variant.manufacturer_color_code.strip(),
|
|
variant.spool_type,
|
|
variant.net_weight_g,
|
|
str(variant.diameter_mm),
|
|
)
|
|
)
|
|
key = f"shop_{shop_id}" if shop_id else f"local_{variant.pk}"
|
|
product_url = f"{SHOP_URL}/products/{slug}" if slug else ""
|
|
if shop_id:
|
|
product_url += f"?id={shop_id}"
|
|
line = lines.setdefault(key, CartLine(key, product_url, bool(shop_id)))
|
|
line.entries.append(entry)
|
|
return CartPlan(
|
|
sorted(
|
|
lines.values(),
|
|
key=lambda line: (
|
|
line.variant.product_type.name.casefold(),
|
|
line.variant.color_label.casefold(),
|
|
line.key,
|
|
),
|
|
)
|
|
)
|
|
|
|
|
|
def _allocate_cents(total, weights):
|
|
if not total:
|
|
return [0] * len(weights)
|
|
divisor = sum(weights)
|
|
shares, remainders = zip(*(divmod(total * weight, divisor) for weight in weights), strict=True)
|
|
allocations = list(shares)
|
|
remaining = total - sum(shares)
|
|
for index in sorted(range(len(weights)), key=lambda index: -remainders[index])[:remaining]:
|
|
allocations[index] += 1
|
|
return allocations
|
|
|
|
|
|
def allocate_prices(plan, line_cents, total_cents):
|
|
# Distribute any discount on the whole cart proportionally, keeping the sum exact.
|
|
totals = _allocate_cents(total_cents, line_cents)
|
|
result = {}
|
|
for line, cents in zip(plan.lines, totals, strict=True):
|
|
entries = sorted(line.entries, key=lambda entry: entry.pk)
|
|
shares = _allocate_cents(cents, [entry.quantity for entry in entries])
|
|
result.update((entry.pk, share) for entry, share in zip(entries, shares, strict=True))
|
|
return result
|