- Two PDF layouts supported: description-before-stock-code and description-after - Clean up trailing inch-quote artifacts and multiple-comma noise - Importer PATCHes existing records when new description is better - Stock codes tab now shows real goods descriptions, not just codes
54 lines
1.9 KiB
Python
54 lines
1.9 KiB
Python
"""Price lookup helper — given a list of stock codes + quantities, return total cost."""
|
|
import os, sys, json, urllib.request, urllib.parse
|
|
PB_URL = "https://pb.agent.apphub.co.za"
|
|
PB_TOKEN = None
|
|
env_path = os.path.join(os.path.dirname(__file__), "..", ".env.local")
|
|
if os.path.exists(env_path):
|
|
for line in open(env_path):
|
|
if line.startswith("PB_TOKEN="):
|
|
PB_TOKEN = line.strip().split("=", 1)[1]
|
|
H = {"Authorization": PB_TOKEN}
|
|
|
|
def get(path):
|
|
r = urllib.request.Request(PB_URL + path, headers=H, method="GET")
|
|
with urllib.request.urlopen(r, timeout=30) as resp:
|
|
return json.loads(resp.read())
|
|
|
|
# (code, qty) — extracted from user's image
|
|
items = [
|
|
("JEITCS50", 1),
|
|
("JEITCL92", 1),
|
|
("JEICS19", 4),
|
|
("JEICS34", 1),
|
|
("JEICS14-P1", 2),
|
|
("JEICS27", 1),
|
|
("JEICS30", 2),
|
|
("JEICS17", 8),
|
|
]
|
|
|
|
print(f"{'Code':<15} {'Qty':>5} {'Avg£':>8} {'Min£':>8} {'Max£':>8} {'Lines':>6} {'Cost@avg':>10}")
|
|
print("-" * 70)
|
|
grand = 0.0
|
|
for code, qty in items:
|
|
f1 = 'code="' + code + '"'
|
|
sc = get('/api/collections/JEI_stock_codes/records?filter=' + urllib.parse.quote(f1) + '&perPage=1')
|
|
if not sc.get("items"):
|
|
print(f"{code:<15} {qty:>5} - NOT IN DB")
|
|
continue
|
|
sc_id = sc["items"][0]["id"]
|
|
f2 = 'stock_code="' + sc_id + '"'
|
|
lr = get('/api/collections/JEI_invoice_lines/records?filter=' + urllib.parse.quote(f2) + '&perPage=500')
|
|
lines = lr.get("items", [])
|
|
if not lines:
|
|
print(f"{code:<15} {qty:>5} - NO PURCHASES")
|
|
continue
|
|
prices = [l["unit_price"] for l in lines if l.get("unit_price")]
|
|
avg = sum(prices) / len(prices)
|
|
mn = min(prices)
|
|
mx = max(prices)
|
|
cost = qty * avg
|
|
grand += cost
|
|
print(f"{code:<15} {qty:>5} {avg:>8.2f} {mn:>8.2f} {mx:>8.2f} {len(lines):>6} {cost:>10.2f}")
|
|
|
|
print("-" * 70)
|
|
print(f"{'TOTAL @ avg':<45} {grand:>24.2f}")
|