#!/usr/bin/env python3
"""Retail parity check: the retail and van sales part of tools/smoke_test.py
run against a FRESH server with every answer recorded, so the Dart and the
Laravel servers can be compared field by field (values and number types).

    python3 laravel_server/tools/retail_check.py http://127.0.0.1:8096 --trace dart.json
    python3 laravel_server/tools/retail_check.py http://127.0.0.1:8093 --trace php.json
    python3 laravel_server/tools/retail_check.py --compare dart.json php.json
"""
import json, re, sys, time, uuid, urllib.request, urllib.error

if '--compare' in sys.argv:
    a, b = sys.argv[sys.argv.index('--compare') + 1:][:2]
    A, B = json.load(open(a)), json.load(open(b))
    diffs = []

    def walk(x, y, path):
        if type(x) is not type(y):
            diffs.append(f'{path}: {x!r} ({type(x).__name__}) vs {y!r} ({type(y).__name__})')
        elif isinstance(x, dict):
            for k in sorted(set(x) | set(y)):
                if k not in x or k not in y:
                    diffs.append(f'{path}.{k}: only in {"first" if k in x else "second"}')
                else:
                    walk(x[k], y[k], f'{path}.{k}')
        elif isinstance(x, list):
            if len(x) != len(y):
                diffs.append(f'{path}: {len(x)} vs {len(y)} entries')
            for i, (p, q) in enumerate(zip(x, y)):
                walk(p, q, f'{path}[{i}]')
        elif x != y:
            diffs.append(f'{path}: {x!r} vs {y!r}')

    for i, (p, q) in enumerate(zip(A, B)):
        if p['step'] != q['step']:
            diffs.append(f'step {i}: {p["step"]} vs {q["step"]}')
            break
        walk(p, q, f'{i}:{p["step"]}')
    if len(A) != len(B):
        diffs.append(f'{len(A)} vs {len(B)} recorded answers')
    print(f'compared {len(A)} recorded answers: {len(diffs)} differences')
    for d in diffs[:80]:
        print('  ', d)
    sys.exit(1 if diffs else 0)

BASE = sys.argv[1]
TRACE = sys.argv[sys.argv.index('--trace') + 1] if '--trace' in sys.argv else None
UUID = re.compile(r'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')
steps, known = [], {}
passed = 0
now = int(time.time() * 1000)


def norm(v, key=''):
    """Ids become placeholders (in order of first sight), clock times <clock>; numbers keep their type."""
    if isinstance(v, dict):
        return {norm(k): norm(x, k) for k, x in v.items() if k not in ('seq', 'updated_at')}
    if isinstance(v, list):
        return [norm(x) for x in v]
    if isinstance(v, str):
        v = UUID.sub(lambda m: known.setdefault(m.group(0), f'<id{len(known)}>'), v)
        if key == 'token':
            return '<token>'
        v = re.sub(r'https?://127\.0\.0\.1:\d+', '<base>', v)
        if key in ('link', 'url') or '/s/' in v:
            v = re.sub(r'/s/[a-z0-9]{20}', '/s/<token>', v)
        if key == 'code' and re.fullmatch(r'[A-Z0-9]{4,12}', v):
            return '<code>'
        return re.sub(r'/(order|account)/[A-Z0-9]{4,12}', r'/\1/<code>', v)
    if isinstance(v, int) and not isinstance(v, bool) and v > 1_000_000_000_000:
        return '<clock>'
    return v


def step_of(method, path):
    path = re.sub(r'/(menu|order)/[A-Z0-9]{4,12}', r'/\1/<code>', UUID.sub('<id>', path))
    return method + ' ' + re.sub(r'=\d{13}', '=<ms>', path)


def call(method, path, body=None, token=None, raw=False, headers=None):
    req = urllib.request.Request(BASE + path, method=method, data=None if body is None else json.dumps(body).encode(),
                                 headers={'content-type': 'application/json', **({'authorization': 'Bearer ' + token} if token else {}), **(headers or {})})
    try:
        with urllib.request.urlopen(req, timeout=120) as res:
            s, data, h = res.status, res.read(), dict(res.headers)
    except urllib.error.HTTPError as e:
        s, data, h = e.code, e.read(), dict(e.headers)
    if raw:
        steps.append({'step': step_of(method, path), 'status': s})
        return s, data, h
    try:
        out = json.loads(data or b'null')
    except ValueError:
        out = data.decode(errors='replace')
    steps.append({'step': step_of(method, path), 'status': s, 'body': norm(out)})
    return s, out, h


def check(cond, msg):
    global passed
    if not cond:
        print('FAIL:', msg)
    else:
        passed += 1
        print('ok  ', msg)


s, r, _ = call('POST', '/api/auth/setup', {'email': 'root@retail.test', 'password': 'rootpass1'})
if s != 200:
    sys.exit('The server must be fresh (no super admin yet): ' + str(r))
root = r['token']
s, presets, _ = call('GET', '/api/admin/feature-presets', token=root)
if s != 200 or 'Premium' not in presets:
    s, fl, _ = call('GET', '/api/admin/features', token=root)
    presets = {'Premium': list(fl.keys()) if isinstance(fl, dict) else [f['key'] for f in fl]}
# ======================================================================= retail ==
# A fresh store with every feature: products by size and colour, extra
# barcodes, units, batches, serials, price lists, credit limits, purchase
# orders, goods received, supplier returns, quotations, delivery notes,
# exchanges and reorder suggestions.
s, r, _ = call('POST', '/api/auth/register', {'business_name': 'Souq Retail', 'owner_name': 'Huda', 'email': 'owner@souq.test', 'password': 'souqpass1', 'pin': '4321'})
rt = r['token']; rbiz = r['subscription']['business_id']
s, prem, _ = call('POST', '/api/admin/packages', {'name': 'Retail Premium', 'duration_days': 365, 'max_devices': 9, 'features': presets['Premium']}, token=root)
call('POST', f'/api/admin/businesses/{rbiz}/activate', {'package_id': prem['id']}, token=root)
def rs(k, v): call('POST', '/api/data/settings', {'id': k, 'value': v}, token=rt)
rs('tax_rate', '5'); rs('tax_inclusive', '0'); rs('credit_sales', '1')
s, remp, _ = call('POST', '/api/pos/login', {'pin': '4321'}, token=rt)
s, rshift, _ = call('POST', '/api/pos/shift/open', {'device_id': 'rt-till', 'employee_id': remp['id'], 'opening_cash': 0}, token=rt)
rb = {'employee_id': remp['id'], 'device_id': 'rt-till', 'order_type': 'counter'}
def stock_of(item_id, location=''):
    s, locs, _ = call('GET', f'/api/stock/locations?item={item_id}', token=rt)
    return sum(x['qty'] for x in locs if x['location'] == location)

# size × colour
s, tee, _ = call('POST', '/api/data/items', {'name': 'Polo Shirt', 'sku': 'POLO', 'price': 5000, 'cost': 2000, 'track_stock': 1}, token=rt)
s, mx, _ = call('POST', f"/api/items/{tee['id']}/matrix", {'matrix': [{'name': 'Size', 'values': ['S', 'M', 'L']}, {'name': 'Colour', 'values': ['Navy', 'White']}]}, token=rt)
check(s == 200 and len(mx['children']) == 6 and any(c['name'] == 'Polo Shirt M / Navy' and c['sku'] == 'POLO-M-NAVY' for c in mx['children']), 'size × colour makes 6 products with their own SKU')
mnavy = next(c for c in mx['children'] if c['name'] == 'Polo Shirt M / Navy')
s, r, _ = call('POST', '/api/pos/sale', {**rb, 'lines': [{'item_id': tee['id'], 'price': 5000, 'qty': 1}], 'payment_type': 'cash', 'tendered': 5250}, token=rt)
check(s == 400 and 'size' in r['error'], 'the parent product cannot be sold, only a size / colour')
s, mx2, _ = call('POST', f"/api/items/{tee['id']}/matrix", {'matrix': [{'name': 'Size', 'values': ['M', 'L']}, {'name': 'Colour', 'values': ['Navy', 'White']}]}, token=rt)
check(len(mx2['children']) == 4 and any(c['id'] == mnavy['id'] for c in mx2['children']), 'dropping size S removes its 2 products and keeps the others')

# barcodes and units
s, water, _ = call('POST', '/api/data/items', {'name': 'Water 500 ml', 'price': 100, 'cost': 40, 'barcode': '629100100', 'barcodes': '629100101, 629100102', 'unit': 'pcs',
                                               'units': json.dumps([{'name': 'Carton', 'factor': 24, 'price': 2000, 'barcode': '1629100100'}]), 'track_stock': 1}, token=rt)
s, hit, _ = call('GET', '/api/items/by-code/629100102', token=rt)
check(s == 200 and hit['item']['id'] == water['id'] and hit['unit'] is None, 'an extra barcode finds the product')
s, hit, _ = call('GET', '/api/items/by-code/1629100100', token=rt)
check(hit['unit'] == 'Carton', 'the carton barcode picks the carton unit')

# suppliers, purchase order, goods received (units, batches, serials)
s, sup, _ = call('POST', '/api/data/suppliers', {'name': 'Gulf Traders'}, token=rt)
s, milk, _ = call('POST', '/api/data/items', {'name': 'Fresh Milk 1 L', 'price': 650, 'cost': 400, 'track_stock': 1, 'track_batch': 1, 'low_stock': 10, 'reorder_qty': 40, 'supplier_id': sup['id']}, token=rt)
s, phone, _ = call('POST', '/api/data/items', {'name': 'Phone Z5', 'price': 99900, 'cost': 70000, 'track_stock': 1, 'track_serial': 1, 'warranty_months': 12}, token=rt)
s, po, _ = call('POST', '/api/purchase-orders', {'supplier_id': sup['id'], 'status': 'ordered', 'lines': [
    {'item_id': water['id'], 'qty': 5, 'price': 900, 'unit': 'Carton'}, {'item_id': milk['id'], 'qty': 30, 'price': 400}]}, token=rt)
check(s == 200 and po['number'] == 'PO-1001' and po['status'] == 'ordered' and po['total'] == round((4500 + 12000) * 1.05), 'purchase order PO-1001 with VAT')
s, r, _ = call('POST', '/api/purchases', {'supplier_id': sup['id'], 'po_id': po['id'], 'lines': [{'item_id': milk['id'], 'qty': 10, 'cost': 400}]}, token=rt)
check(s == 400 and 'batch' in r['error'], 'batch items need a batch number')
s, g1, _ = call('POST', '/api/purchases', {'supplier_id': sup['id'], 'po_id': po['id'], 'number': 'INV-77', 'lines': [
    {'item_id': water['id'], 'qty': 5, 'cost': 900, 'unit': 'Carton'},
    {'item_id': milk['id'], 'qty': 6, 'cost': 400, 'batch': 'M-OLD', 'expiry': '2026-11-02'},
    {'item_id': milk['id'], 'qty': 10, 'cost': 400, 'batch': 'M-NEW', 'expiry': '2026-11-20'}]}, token=rt)
check(s == 200 and g1['grn_no'] == 'GRN-1001', 'goods received note GRN-1001')
check(abs(stock_of(water['id']) - 120) < 1e-6, '5 cartons received = 120 bottles in stock')
s, pos_, _ = call('GET', '/api/purchase-orders', token=rt)
po1 = next(x for x in pos_ if x['id'] == po['id'])
check(po1['status'] == 'partial' and next(l for l in po1['lines'] if l['item_id'] == milk['id'])['done'] == 16, 'the order shows what was received (part received)')
s, r, _ = call('POST', '/api/purchases', {'supplier_id': sup['id'], 'lines': [{'item_id': phone['id'], 'qty': 2, 'cost': 70000, 'serials': ['imei-111']}]}, token=rt)
check(s == 400 and 'serial' in r['error'], 'serial items need one serial number per unit')
s, g2, _ = call('POST', '/api/purchases', {'supplier_id': sup['id'], 'lines': [{'item_id': phone['id'], 'qty': 3, 'cost': 70000, 'serials': 'imei-111, imei-222\nimei-333'}]}, token=rt)
check(s == 200, 'phones received with 3 IMEI numbers')
s, r, _ = call('POST', '/api/purchases', {'supplier_id': sup['id'], 'lines': [{'item_id': phone['id'], 'qty': 1, 'cost': 70000, 'serials': ['IMEI-111']}]}, token=rt)
check(s == 400 and 'already in stock' in r['error'], 'the same IMEI cannot be received twice')
s, sl, _ = call('GET', f"/api/serials?item={phone['id']}&status=in_stock", token=rt)
check(len(sl) == 3 and all(x['grn_no'] for x in sl), 'serials in stock, with the GRN they came on')

# selling: FEFO batches, cartons, serials
s, r, _ = call('POST', '/api/pos/sale', {**rb, 'lines': [{'item_id': phone['id'], 'price': 99900, 'qty': 1}], 'payment_type': 'cash', 'tendered': 104895}, token=rt)
check(s == 400 and 'serial' in r['error'], 'selling a phone needs its IMEI')
s, ps, _ = call('POST', '/api/pos/sale', {**rb, 'lines': [{'item_id': phone['id'], 'price': 99900, 'qty': 1, 'serials': ['IMEI-222']}], 'payment_type': 'card'}, token=rt)
check(s == 200 and ps['lines'][0]['serials'] == 'IMEI-222', 'phone sold with its IMEI on the invoice')
s, r, _ = call('POST', '/api/pos/sale', {**rb, 'lines': [{'item_id': phone['id'], 'price': 99900, 'qty': 1, 'serials': ['IMEI-222']}], 'payment_type': 'card'}, token=rt)
check(s == 400 and 'already sold' in r['error'], 'a sold IMEI cannot be sold again')
s, ms, _ = call('POST', '/api/pos/sale', {**rb, 'lines': [{'item_id': milk['id'], 'price': 650, 'qty': 8}, {'item_id': water['id'], 'price': 2000, 'qty': 1, 'unit': 'Carton'}],
                                         'payment_type': 'cash', 'tendered': 999999}, token=rt)
mline = next(l for l in ms['lines'] if l['item_id'] == milk['id'])
check(s == 200 and mline['batch'] == 'M-OLD:6, M-NEW:2', 'milk leaves the batch that expires first (FEFO)')
check(abs(stock_of(water['id']) - 96) < 1e-6, 'a carton sold takes 24 bottles out of stock')
s, bt, _ = call('GET', f"/api/stock/batches?item={milk['id']}", token=rt)
check([(x['batch'], x['qty']) for x in bt] == [('M-NEW', 8)], 'batch stock: only the newer batch is left')
s, r, _ = call('POST', '/api/pos/sale', {**rb, 'lines': [{'item_id': water['id'], 'price': 1500, 'qty': 1, 'unit': 'Carton'}], 'payment_type': 'cash', 'tendered': 9999}, token=rt)
check(s == 200, 'the owner may change the carton price')

# refunds and voids bring serials and batches back
pline = ps['lines'][0]
s, pr, _ = call('POST', '/api/pos/refund', {'receipt_id': ps['id'], 'qtys': {pline['id']: 1}, 'reason': 'Faulty', 'employee_id': remp['id']}, token=rt)
s, sl, _ = call('GET', '/api/serials?q=IMEI-222', token=rt)
check(s == 200 and sl[0]['status'] == 'in_stock', 'refunded phone: its IMEI is in stock again')
s, r, _ = call('POST', '/api/pos/refund', {'receipt_id': ms['id'], 'qtys': {mline['id']: 1}, 'reason': 'Damaged', 'employee_id': remp['id']}, token=rt)
s, bt, _ = call('GET', f"/api/stock/batches?item={milk['id']}", token=rt)
check(dict((x['batch'], x['qty']) for x in bt) == {'M-NEW': 9}, 'a refunded bottle goes back to the batch it came from')

# price lists and customer groups
s, ws, _ = call('POST', '/api/data/price_lists', {'name': 'Wholesale', 'markup_pct': -10, 'groups': 'Trade', 'active': 1}, token=rt)
call('POST', '/api/data/price_list_items', {'id': f"{ws['id']}:{water['id']}:Carton", 'list_id': ws['id'], 'item_id': water['id'], 'unit': 'Carton', 'price': 1700}, token=rt)
s, trade, _ = call('POST', '/api/data/customers', {'name': 'Corner Grocery', 'phone': '0507000111', 'customer_group': 'Trade', 'credit_limit': 5000, 'credit_days': 30}, token=rt)
rs('cashier_price_change', '0')
s, cashier, _ = call('POST', '/api/data/employees', {'name': 'Cash Ier', 'role': 'cashier', 'pin': '5511', 'active': 1}, token=rt)
cb = {**rb, 'employee_id': cashier['id']}
s, r, _ = call('POST', '/api/pos/sale', {**cb, 'customer_id': trade['id'], 'lines': [{'item_id': water['id'], 'price': 2000, 'qty': 1, 'unit': 'Carton'}], 'payment_type': 'cash', 'tendered': 9999}, token=rt)
check(s == 403, 'a cashier cannot charge the retail price to a trade customer by hand')
s, tw, _ = call('POST', '/api/pos/sale', {**cb, 'customer_id': trade['id'], 'lines': [{'item_id': water['id'], 'price': 1700, 'qty': 1, 'unit': 'Carton'},
                                                                                  {'item_id': milk['id'], 'price': 585, 'qty': 1}], 'payment_type': 'cash', 'tendered': 9999}, token=rt)
check(s == 200 and tw['price_list'] == 'Wholesale', 'trade customer: wholesale prices (own carton price, and -10% on milk)')

# credit limit and payment terms
s, c1, _ = call('POST', '/api/pos/sale', {**rb, 'customer_id': trade['id'], 'lines': [{'item_id': milk['id'], 'price': 585, 'qty': 6}], 'payment_type': 'credit'}, token=rt)
check(s == 200 and c1['due_at'] and c1['due_at'] > c1['created_at'] + 29 * 86400000, 'credit invoice is due in 30 days')
s, r, _ = call('POST', '/api/pos/sale', {**rb, 'customer_id': trade['id'], 'lines': [{'item_id': milk['id'], 'price': 585, 'qty': 3}], 'payment_type': 'credit'}, token=rt)
check(s == 400 and r.get('code') == 'credit_limit' and 'credit limit' in r['error'], 'over the credit limit: refused')
s, r, _ = call('POST', '/api/pos/sale', {**rb, 'customer_id': trade['id'], 'lines': [{'item_id': milk['id'], 'price': 585, 'qty': 3}], 'payment_type': 'credit', 'override_credit': True}, token=rt)
check(s == 200, 'the owner can allow it')
s, acc, _ = call('GET', '/api/accounts/outstanding', token=rt)
check(any(a['id'] == trade['id'] and a['credit_limit'] == 5000 for a in acc), 'customer accounts show what is owed and the limit')
s, stt, _ = call('GET', f"/api/accounts/statement/{trade['id']}", token=rt)
check(s == 200 and stt['due'] > 5000 and '/account/' in stt['link'], 'statement with the pay link')
s, r, _ = call('POST', '/api/accounts/settle', {'customer_id': trade['id'], 'amount': stt['due'], 'payment_type': 'cash'}, token=rt)
check(s == 200 and r['due'] == 0, 'collect the whole balance: spread over the invoices')

# exchange
s, sh, _ = call('POST', '/api/pos/sale', {**rb, 'lines': [{'item_id': mnavy['id'], 'price': 5000, 'qty': 1}], 'payment_type': 'cash', 'tendered': 5250}, token=rt)
lwhite = next(c for c in mx2['children'] if c['name'] == 'Polo Shirt L / White')
s, ex, _ = call('POST', '/api/pos/exchange', {**rb, 'receipt_id': sh['id'], 'qtys': {sh['lines'][0]['id']: 1}, 'reason': 'Wrong size',
                                            'lines': [{'item_id': lwhite['id'], 'price': 5000, 'qty': 1}], 'payment_type': 'cash'}, token=rt)
check(s == 200 and ex['difference'] == 0 and ex['sale']['exchange_of'] == ex['refund']['id'], 'exchange M Navy for L White: nothing to pay')
check(abs(stock_of(mnavy['id']) - 0) < 1e-6 and abs(stock_of(lwhite['id']) + 1) < 1e-6, 'the returned shirt is back in stock, the new one out')

# returns to the supplier
s, sr, _ = call('POST', '/api/supplier-returns', {'supplier_id': sup['id'], 'purchase_id': g2['id'], 'reason': 'Faulty', 'lines': [{'item_id': phone['id'], 'qty': 1, 'serials': ['IMEI-333']}]}, token=rt)
check(s == 200 and sr['number'] == 'SR-1001' and sr['total'] == round(70000 * 1.05), 'phone returned to the supplier: SR-1001 with the credit')
s, sl, _ = call('GET', '/api/serials?q=IMEI-333', token=rt)
check(sl[0]['status'] == 'supplier_return', 'its IMEI is marked returned to the supplier')
s, sups, _ = call('GET', '/api/suppliers', token=rt)
check(next(x for x in sups if x['id'] == sup['id'])['due'] == g1['total'] + g2['total'] - sr['total'], 'what the supplier is owed drops by the return')

# reorder suggestions
call('POST', '/api/data/items', {'id': milk['id'], 'name': 'Fresh Milk 1 L', 'low_stock': 20}, token=rt)
have = stock_of(milk['id']) + 14  # 14 still to come on PO-1001
s, sg, _ = call('GET', '/api/purchase-orders/suggest', token=rt)
mg = next((x for g in sg for x in g['lines'] if x['item_id'] == milk['id']), None)
check(s == 200 and mg is not None and mg['qty'] == 40 - have, f'milk below its reorder level: suggest ordering up to 40, counting what is still on order {mg}')
s, made, _ = call('POST', '/api/purchase-orders/suggest', {}, token=rt)
check(len(made) == 1 and made[0]['supplier_id'] == sup['id'] and made[0]['status'] == 'draft', 'suggested purchase order made as a draft')

# quotation → invoice → delivery notes
s, q, _ = call('POST', '/api/quotations', {'customer_id': trade['id'], 'lines': [{'item_id': water['id'], 'qty': 2, 'unit': 'Carton'}, {'item_id': milk['id'], 'qty': 4, 'price': 600}], 'discount': 200}, token=rt)
check(s == 200 and q['number'] == 'QT-1001' and q['subtotal'] == 2 * 1700 + 2400 and q['total'] == round((5800 - 200) * 1.05), 'quotation at wholesale prices with a discount')
s, inv, _ = call('POST', f"/api/quotations/{q['id']}/invoice", {'employee_id': remp['id'], 'payment_type': 'credit', 'customer_ref': 'LPO-55', 'override_credit': True}, token=rt)
check(s == 200 and inv['quotation_id'] == q['id'] and inv['total'] == q['total'] and inv['customer_ref'] == 'LPO-55', 'quotation invoiced with the same total and the customer reference')
s, r, _ = call('POST', f"/api/quotations/{q['id']}/invoice", {'employee_id': remp['id']}, token=rt)
check(s == 400, 'a quotation is invoiced once')
s, dn, _ = call('POST', '/api/delivery-notes', {'receipt_id': inv['id'], 'lines': [{'item_id': water['id'], 'unit': 'Carton', 'qty': 1}]}, token=rt)
check(s == 200 and dn['number'] == 'DN-1001', 'first delivery note: 1 carton')
s, dn2, _ = call('POST', '/api/delivery-notes', {'receipt_id': inv['id']}, token=rt)
dl = json.loads(dn2['lines']) if isinstance(dn2['lines'], str) else dn2['lines']
check(s == 200 and sorted((l['item_id'], l['qty']) for l in dl) == sorted([(water['id'], 1), (milk['id'], 4)]), 'second delivery note: the rest')
s, r, _ = call('POST', '/api/delivery-notes', {'receipt_id': inv['id']}, token=rt)
check(s == 400 and 'already delivered' in r['error'], 'nothing left to deliver')
s, r, _ = call('POST', f"/api/delivery-notes/{dn['id']}", {'received_by': 'Ahmed (store keeper)'}, token=rt)
check(s == 200 and r['status'] == 'delivered', 'delivery note signed for')

# reports
rq = f'from={now - 86400000 * 30}&to={int(time.time() * 1000) + 86400000 * 30}&tz=240'
call('POST', '/api/purchases', {'supplier_id': sup['id'], 'lines': [{'item_id': milk['id'], 'qty': 20, 'cost': 400, 'batch': 'M-LATE', 'expiry': '2026-12-15'}]}, token=rt)
s, ex_r, _ = call('GET', f'/api/reports/expiry?{rq}', token=rt)
check(s == 200 and any(row[1] == 'M-LATE' and row[2] == '2026-12-15' for row in ex_r['tables'][0]['rows']), 'batches & expiry report')
s, se_r, _ = call('GET', f'/api/reports/serials?{rq}', token=rt)
check(s == 200 and len(se_r['tables'][1]['rows']) == 2, 'serial numbers report: 2 phones in stock')
s, val, _ = call('GET', f'/api/reports/stock_valuation?{rq}', token=rt)
check(not any(row[0] == 'Polo Shirt' for row in val['tables'][0]['rows']), 'size × colour parents are not counted in stock')
s, vat, _ = call('GET', f'/api/reports/vat_return?{rq}', token=rt)
check(s == 200, 'VAT return includes supplier returns')

# online shop: sizes and colours
s, olink, _ = call('GET', '/api/online/link', token=rt)
rs('online_ordering', '1'); rs('online_order_types', 'pickup,delivery')
s, sm, _ = call('GET', '/api/public/menu/' + olink['code'])
polo = next(i for i in sm['items'] if i['id'] == tee['id'])
kids = [i for i in sm['items'] if i.get('parent_id') == tee['id']]
check(s == 200 and polo.get('matrix') and len(kids) == 4 and any(k['label'] == 'M / Navy' for k in kids), 'online shop lists the polo with its 4 sizes / colours')
s, r, _ = call('POST', '/api/public/order/' + olink['code'], {'order_type': 'pickup', 'customer_name': 'Web', 'customer_phone': '0508887766', 'lines': [{'item_id': tee['id'], 'qty': 1}]})
check(s == 400 and 'size' in r['error'], 'online: the parent product cannot be ordered')
s, r, _ = call('POST', '/api/public/order/' + olink['code'], {'order_type': 'pickup', 'customer_name': 'Web', 'customer_phone': '0508887766', 'lines': [{'item_id': mnavy['id'], 'qty': 1}]})
check(s == 200 and r['total'] == 5250, 'online: a size / colour is ordered')
s, page, _ = call('GET', '/order/' + olink['code'], raw=True)
check(s == 200 and b'Loading shop' in page and b'waiter' not in page.lower(), 'the shop page is served, no waiter buttons')

# ===================================================================== van sales ==
# The van app works offline and syncs its rows; here they are pushed like the
# app does (POST /api/sync). The store loads the van, approves the day.
s, salesman, _ = call('POST', '/api/data/employees', {'name': 'Vasu Van', 'role': 'van_salesman', 'pin': '7788', 'active': 1}, token=rt)
s, van, _ = call('POST', '/api/data/vans', {'name': 'Van 1 Deira', 'plate': 'D 12345', 'salesman_id': salesman['id'], 'active': 1}, token=rt)
check(s == 200 and van['name'] == 'Van 1 Deira', 'a van is set up')
s, shop2, _ = call('POST', '/api/data/customers', {'name': 'Deira Mini Mart', 'phone': '0507001001', 'lat': 25.2711, 'lng': 55.3075, 'credit_limit': 100000}, token=rt)
s, route, _ = call('POST', '/api/data/routes', {'name': 'Deira daily', 'van_id': van['id'], 'days': 'mon,tue,wed,thu,fri,sat,sun',
                                               'customers': json.dumps([trade['id'], shop2['id']]), 'active': 1}, token=rt)
check(s == 200, 'route with two customers')
water_store = stock_of(water['id'])
s, ld, _ = call('POST', f"/api/vans/{van['id']}/transfer", {'kind': 'load', 'lines': [{'item_id': water['id'], 'qty': 2, 'unit': 'Carton'}, {'item_id': milk['id'], 'qty': 10}]}, token=rt)
check(s == 200 and ld['number'] == 'LD-1001', 'van loaded: LD-1001')
check(abs(stock_of(water['id'], van['id']) - 48) < 1e-6 and abs(stock_of(water['id']) - (water_store - 48)) < 1e-6, '2 cartons moved from the store into the van')
s, vs, _ = call('GET', f"/api/vans/{van['id']}/stock", token=rt)
check(any(x['item_id'] == milk['id'] and abs(x['qty'] - 10) < 1e-6 for x in vs), 'van stock shows the milk')
s, bt, _ = call('GET', f"/api/stock/batches?item={milk['id']}", token=rt)
check(any(x['location'] == van['id'] for x in bt), 'milk batches moved into the van (first-expiring-first)')
s, r, _ = call('POST', f"/api/vans/{van['id']}/transfer", {'kind': 'unload', 'lines': [{'item_id': water['id'], 'qty': 6}]}, token=rt)
check(s == 200 and r['number'] == 'UL-1001' and abs(stock_of(water['id'], van['id']) - 42) < 1e-6, 'unloading 6 bottles back to the store')

# the phone: a visit, a sale from the van, a cheque collection
day = time.strftime('%Y-%m-%d')
vid, vrid, vmid, cid = str(uuid.uuid4()), str(uuid.uuid4()), str(uuid.uuid4()), str(uuid.uuid4())
tnow = int(time.time() * 1000)
vday = {'id': str(uuid.uuid4()), 'van_id': van['id'], 'day': day, 'route_id': route['id'], 'salesman_id': salesman['id'], 'status': 'open', 'opened_at': tnow, 'opening_cash': 10000, 'updated_at': tnow}
visit = {'id': vid, 'van_id': van['id'], 'day': day, 'route_id': route['id'], 'customer_id': shop2['id'], 'stop_no': 2, 'planned': 1, 'status': 'visited',
         'checked_in_at': tnow, 'lat': 25.2712, 'lng': 55.3076, 'distance_m': 14, 'salesman_id': salesman['id'], 'updated_at': tnow}
missed = {'id': str(uuid.uuid4()), 'van_id': van['id'], 'day': day, 'route_id': route['id'], 'customer_id': trade['id'], 'stop_no': 1, 'planned': 1, 'status': 'missed',
          'reason': 'Shop closed', 'salesman_id': salesman['id'], 'updated_at': tnow}
vrec = {'id': vrid, 'number': 'VAN1-1', 'created_at': tnow, 'type': 'sale', 'employee_id': salesman['id'], 'customer_id': shop2['id'], 'subtotal': 4000, 'discount': 0,
        'net': 4000, 'tax': 200, 'total': 4200, 'cost': 1920, 'payment_type': 'credit', 'tendered': 0, 'points_earned': 0, 'points_redeemed': 0,
        'order_type': 'counter', 'van_id': van['id'], 'visit_id': vid, 'device_id': 'van-phone', 'updated_at': tnow}
vline = {'id': str(uuid.uuid4()), 'receipt_id': vrid, 'item_id': water['id'], 'name': 'Water 500 ml', 'qty': 2, 'price': 2000, 'cost': 960, 'total': 4000, 'discount': 0,
         'net': 4000, 'tax': 200, 'tax_category': 'standard', 'unit': 'Carton', 'factor': 24, 'updated_at': tnow}
vmove = {'id': vmid, 'item_id': water['id'], 'qty': -48, 'reason': 'sale', 'ref_id': vrid, 'created_at': tnow, 'location': van['id'], 'updated_at': tnow}
cash_sale = {'id': str(uuid.uuid4()), 'number': 'VAN1-2', 'created_at': tnow, 'type': 'sale', 'employee_id': salesman['id'], 'customer_id': shop2['id'], 'subtotal': 1000,
             'discount': 0, 'net': 1000, 'tax': 50, 'total': 1050, 'cost': 400, 'payment_type': 'cash', 'tendered': 1050, 'points_earned': 0, 'points_redeemed': 0,
             'order_type': 'counter', 'van_id': van['id'], 'visit_id': vid, 'updated_at': tnow}
cheque = {'id': cid, 'receipt_id': vrid, 'customer_id': shop2['id'], 'amount': 3000, 'payment_type': 'cheque', 'cheque_no': '004512', 'cheque_bank': 'ENBD',
          'cheque_date': day, 'collection_id': 'col-1', 'van_id': van['id'], 'visit_id': vid, 'created_at': tnow, 'employee_id': salesman['id'], 'updated_at': tnow}
s, r, _ = call('POST', '/api/sync', {'since': 0, 'device_id': 'van-phone', 'changes': {
    'van_days': [vday], 'visits': [visit, missed], 'receipts': [vrec, cash_sale], 'receipt_lines': [vline], 'stock_movements': [vmove], 'payments': [cheque]}}, token=rt)
check(s == 200, 'the van phone syncs its day: visits, invoices, stock and a cheque')
check(abs(stock_of(water['id'], van['id']) - (42 - 48)) < 1e-6, 'the van invoice took 2 cartons out of the van')
s, d, _ = call('GET', f'/api/receipts/{vrid}', token=rt)
check(s == 200 and d['due'] == 1200, 'cheque of 30.00 paid part of the 42.00 van invoice')

# close and approve the day
vday2 = {**vday, 'status': 'submitted', 'submitted_at': tnow, 'expected_cash': 10000 + 1050, 'counted_cash': 11000,
         'cheques': json.dumps([{'no': '004512', 'bank': 'ENBD', 'amount': 3000, 'customer': 'Deira Mini Mart'}]),
         'stock': json.dumps([{'item_id': water['id'], 'name': 'Water 500 ml', 'expected': -6, 'counted': 0}, {'item_id': milk['id'], 'name': 'Milk', 'expected': 10, 'counted': 9}]),
         'summary': json.dumps({'sales': 5250, 'invoices': 2, 'visits': 2, 'visited': 1, 'missed': 1}), 'updated_at': tnow + 1}
s, r, _ = call('POST', '/api/sync', {'since': 0, 'device_id': 'van-phone', 'changes': {'van_days': [vday2]}}, token=rt)
s, days, _ = call('GET', '/api/van-days?status=submitted', token=rt)
check(len(days) == 1 and days[0]['van_name'] == 'Van 1 Deira' and days[0]['salesman'] == 'Vasu Van', 'the closed day waits for the supervisor')
s, r, _ = call('POST', f"/api/van-days/{vday['id']}/decide", {'approve': True, 'note': 'OK, 0.50 short noted', 'employee_id': salesman['id']}, token=rt)
check(s == 403, 'a van salesman cannot approve days')
s, r, _ = call('POST', f"/api/van-days/{vday['id']}/decide", {'approve': True, 'note': 'OK, 0.50 short noted'}, token=rt)
check(s == 200 and r['status'] == 'approved', 'the supervisor approves the day')
check(abs(stock_of(milk['id'], van['id']) - 9) < 1e-6 and abs(stock_of(water['id'], van['id'])) < 1e-6, 'approval makes the counted quantities the van stock')
s, r, _ = call('POST', f"/api/van-days/{vday['id']}/decide", {'approve': True}, token=rt)
check(s == 400, 'a day is approved once')

# reports
vq = f'from={tnow - 86400000}&to={tnow + 86400000}&tz=240'
s, vr, _ = call('GET', f'/api/reports/van_sales?{vq}', token=rt)
row = vr['tables'][0]['rows'][0] if s == 200 and vr['tables'][0]['rows'] else []
check(s == 200 and row[:2] == ['Van 1 Deira', 'Vasu Van'] and row[2] == 2 and row[3] == 5250 and row[4] == 1050 and row[5] == 4200 and row[7] == 3000 and row[9] == 1,
      f'van sales report: per van and salesman {row}')
s, vc, _ = call('GET', f'/api/reports/van_collections?{vq}', token=rt)
check(s == 200 and vc['tables'][0]['rows'][0][6].startswith('004512') and vc['tables'][0]['rows'][0][7] == 3000, 'van collections report lists the cheque')
s, vv, _ = call('GET', f'/api/reports/van_visits?{vq}', token=rt)
check(s == 200 and any(r[4] == 'Missed' and r[7] == 'Shop closed' for r in vv['tables'][0]['rows']), 'route visits report shows the missed visit and why')


if TRACE:
    json.dump(steps, open(TRACE, 'w'), indent=1)
print(f'\n{passed} checks passed, {len(steps)} answers recorded.')
