main
John Lauer Verify top-access cooler mount and reconcile current BOM with internal inventory pricing ae78bc0
#!/usr/bin/env python3
"""Read-only documented inventory fallback; no reservation or vendor refresh."""
import csv,json,urllib.request,urllib.parse,concurrent.futures,datetime
from pathlib import Path
from decimal import Decimal
ROOT=Path(__file__).resolve().parents[1]
OUT=ROOT/'docs/cost/inventory-2026-10-12';OUT.mkdir(exist_ok=True)
BASE='https://inventory-2w8z8d9ojzzl.adom.cloud'
rows=[r for r in csv.DictReader((ROOT/'docs/cost/procurement-review.csv').open()) if r['scope']=='board' and not r['unit_price_usd']]
def get(path,name):
 p=OUT/name
 if p.exists():return json.loads(p.read_text())
 with urllib.request.urlopen(BASE+path,timeout=30) as r:d=r.read()
 parsed=json.loads(d);p.write_bytes(d);return parsed

def review(r):
 mpn=r['mpn'];qty=int(r['quantity']);name=mpn.lower();result={'mpn':mpn,'quantity':qty,'refs':r['refs']}
 try:
  lookup=get('/parts/lookup?q='+urllib.parse.quote(mpn),'lookup-'+name+'.json')
  p=lookup.get('part',{})
  if not lookup.get('found') or p.get('mfrPN')!=mpn:
   return dict(result,status='no-exact-inventory-match',lookupMatch=lookup.get('match_type'))
  quote_name='quote-'+name+'.json'
  old=OUT/quote_name
  if old.exists() and json.loads(old.read_text()).get('qty')!=qty:quote_name='quote-'+name+'-qty'+str(qty)+'.json'
  q=get('/api/pricing/quote?'+urllib.parse.urlencode({'part_id':p['id'],'qty':qty}),quote_name)
  assert q['qty']==qty and q['part_id']==p['id']
  result.update(status='inventory-snapshot-not-reservation',onHand=p.get('qty_on_hand'),enoughReportedStock=p.get('qty_on_hand',0)>=qty,costBasis=q.get('cost_basis'),ourPrice=q.get('our_price'),lookupEvidence=str((OUT/('lookup-'+name+'.json')).relative_to(ROOT)),pricingEvidence=str((OUT/quote_name).relative_to(ROOT)))
  cost=q.get('cost_basis',{}).get('unit_cost')
  if cost is not None:result['historicalCostAllocationUsd']=str(Decimal(str(cost))*qty)
 except Exception as e:result.update(status='lookup-error',error=str(e))
 return result
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as ex:result=list(ex.map(review,rows))
summary={'checkedAtUtc':datetime.datetime.now(datetime.timezone.utc).isoformat(),'method':'Documented read-only HTTP fallback because CLI and referenced CLI source are absent. Cached snapshots reused; no inventory mutation or vendor search.','rows':result,'historicalCostAllocationSubtotalUsd':str(sum((Decimal(r['historicalCostAllocationUsd']) for r in result if 'historicalCostAllocationUsd'in r),Decimal(0))),'reportedSellExtendedSubtotalUsd':str(sum((Decimal(str(r['ourPrice']['extended'])) for r in result if r.get('ourPrice',{}).get('extended') is not None),Decimal(0))),'limitations':['Historical cost allocation is not a current purchase quote.','Adom sell pricing is separate from historical cost and may be based on distributor quantity tiers.','Reported stock is not reserved or physically counted for this build.','Missing matches remain unresolved; no substitutions were made.']}
summary['unroundedSellAllocationUsd']=str(sum((Decimal(str(r['ourPrice']['unit_price']))*r['quantity'] for r in result if r.get('ourPrice',{}).get('unit_price') is not None),Decimal(0)))
summary['reportedSufficientStockMpns']=sum(r.get('enoughReportedStock',False) for r in result)
summary['limitations'].append('Per-line extended prices are rounded by the service; zero extended price for a sub-cent allocation does not mean a free part.')
(OUT/'review.json').write_text(json.dumps(summary,indent=2)+'\n')
print(json.dumps({k:v for k,v in summary.items() if k!='rows'},indent=2))