broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    q = x['received']
    p = x['po_price']
    onhand = q - x['consumed']
    billed = min(x['invoice_qty'], q)
    d = x['invoice_price'] - p
    total = billed * d
    mismatch = x['invoice_qty'] != q
    if abs(total) <= x['tolerance']:
        return {'layer_value': onhand * p, 'cogs_adj': 0, 'writeoff': total, 'mismatch': mismatch}
    consumed_billed = billed - min(billed, onhand)
    onhand_billed = billed - consumed_billed
    return {'layer_value': onhand * p + onhand_billed * d, 'cogs_adj': consumed_billed * d, 'writeoff': 0, 'mismatch': mismatch}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 111, 'tolerance': 20}, {'layer_value': 666, 'cogs_adj': 44, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 6}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 51, 'tolerance': 4}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 4, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 10, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 399, 'tolerance': 100}, {'layer_value': 2793, 'cogs_adj': -505, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 112, 'tolerance': 20}, {'layer_value': 672, 'cogs_adj': 48, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 7}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 52, 'tolerance': 8}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 8, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 11, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 398, 'tolerance': 100}, {'layer_value': 2786, 'cogs_adj': -510, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 113, 'tolerance': 20}, {'layer_value': 678, 'cogs_adj': 52, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 2, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1440, 'cogs_adj': -20, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 8}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 53, 'tolerance': 12}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 12, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 12, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 397, 'tolerance': 100}, {'layer_value': 2779, 'cogs_adj': -515, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 114, 'tolerance': 20}, {'layer_value': 684, 'cogs_adj': 56, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 9}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 54, 'tolerance': 16}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 16, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 13, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 396, 'tolerance': 100}, {'layer_value': 2772, 'cogs_adj': -520, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 115, 'tolerance': 20}, {'layer_value': 690, 'cogs_adj': 60, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 10}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 55, 'tolerance': 20}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 20, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 14, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 395, 'tolerance': 100}, {'layer_value': 2765, 'cogs_adj': -525, 'writeoff': 0, 'mismatch': False}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    q = x['received']
    p = x['po_price']
    onhand = q - x['consumed']
    billed = min(x['invoice_qty'], q)
    d = x['invoice_price'] - p
    total = billed * d
    mismatch = x['invoice_qty'] != q
    if abs(total) <= x['tolerance']:
        return {'layer_value': onhand * p, 'cogs_adj': 0, 'writeoff': total, 'mismatch': mismatch}
    consumed_billed = billed * x['consumed'] // q
    onhand_billed = billed - consumed_billed
    return {'layer_value': onhand * p + onhand_billed * d, 'cogs_adj': consumed_billed * d, 'writeoff': 0, 'mismatch': mismatch}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 111, 'tolerance': 20}, {'layer_value': 666, 'cogs_adj': 44, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 6}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 51, 'tolerance': 4}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 4, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 10, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 399, 'tolerance': 100}, {'layer_value': 2793, 'cogs_adj': -505, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 112, 'tolerance': 20}, {'layer_value': 672, 'cogs_adj': 48, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 7}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 52, 'tolerance': 8}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 8, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 11, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 398, 'tolerance': 100}, {'layer_value': 2786, 'cogs_adj': -510, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 113, 'tolerance': 20}, {'layer_value': 678, 'cogs_adj': 52, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 2, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1440, 'cogs_adj': -20, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 8}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 53, 'tolerance': 12}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 12, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 12, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 397, 'tolerance': 100}, {'layer_value': 2779, 'cogs_adj': -515, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 114, 'tolerance': 20}, {'layer_value': 684, 'cogs_adj': 56, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 9}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 54, 'tolerance': 16}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 16, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 13, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 396, 'tolerance': 100}, {'layer_value': 2772, 'cogs_adj': -520, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 115, 'tolerance': 20}, {'layer_value': 690, 'cogs_adj': 60, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 10}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 55, 'tolerance': 20}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 20, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 14, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 395, 'tolerance': 100}, {'layer_value': 2765, 'cogs_adj': -525, 'writeoff': 0, 'mismatch': False}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Price corrections are pushed into the on-hand layer while shipped units keep the old cost.

Input {received, po_price, consumed, invoice_qty, invoice_price, tolerance}. Billed units = min(invoice_qty, received); total variance = billed*(invoice-po). If |total| <= tolerance the variance is written off and the layer is unchanged. Otherwise billed units are matched to consumed units first: consumed billed units adjust COGS, the rest revalue the on-hand layer. Return {layer_value, cogs_adj, writeoff, mismatch}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    q = x['received']
    p = x['po_price']
    onhand = q - x['consumed']
    billed = min(x['invoice_qty'], q)
    d = x['invoice_price'] - p
    total = billed * d
    mismatch = x['invoice_qty'] != q
    if abs(total) < x['tolerance']:
        return {'layer_value': onhand * p, 'cogs_adj': 0, 'writeoff': total, 'mismatch': mismatch}
    consumed_billed = min(billed, x['consumed'])
    onhand_billed = billed - consumed_billed
    return {'layer_value': onhand * p + onhand_billed * d, 'cogs_adj': consumed_billed * d, 'writeoff': 0, 'mismatch': mismatch}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 111, 'tolerance': 20}, {'layer_value': 666, 'cogs_adj': 44, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 6}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 51, 'tolerance': 4}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 4, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 10, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 399, 'tolerance': 100}, {'layer_value': 2793, 'cogs_adj': -505, 'writeoff': 0, 'mismatch': False}], ['small credit within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 99, 'tolerance': 6}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': -5, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 112, 'tolerance': 20}, {'layer_value': 672, 'cogs_adj': 48, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 7}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 52, 'tolerance': 8}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 8, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 11, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 398, 'tolerance': 100}, {'layer_value': 2786, 'cogs_adj': -510, 'writeoff': 0, 'mismatch': False}], ['small credit within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 99, 'tolerance': 7}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': -5, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 113, 'tolerance': 20}, {'layer_value': 678, 'cogs_adj': 52, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 2, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1440, 'cogs_adj': -20, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 8}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 53, 'tolerance': 12}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 12, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 12, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 397, 'tolerance': 100}, {'layer_value': 2779, 'cogs_adj': -515, 'writeoff': 0, 'mismatch': False}], ['small credit within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 99, 'tolerance': 8}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': -5, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 114, 'tolerance': 20}, {'layer_value': 684, 'cogs_adj': 56, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 9}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 54, 'tolerance': 16}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 16, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 13, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 396, 'tolerance': 100}, {'layer_value': 2772, 'cogs_adj': -520, 'writeoff': 0, 'mismatch': False}], ['small credit within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 99, 'tolerance': 9}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': -5, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 115, 'tolerance': 20}, {'layer_value': 690, 'cogs_adj': 60, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 10}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 55, 'tolerance': 20}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 20, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 14, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 395, 'tolerance': 100}, {'layer_value': 2765, 'cogs_adj': -525, 'writeoff': 0, 'mismatch': False}], ['small credit within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 99, 'tolerance': 10}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': -5, 'mismatch': False}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    q = x['received']
    p = x['po_price']
    onhand = q - x['consumed']
    billed = min(x['invoice_qty'], q)
    d = x['invoice_price'] - p
    total = billed * d
    mismatch = x['invoice_qty'] != q
    if abs(total) < x['tolerance'] + 1 and total >= 0:
        return {'layer_value': onhand * p, 'cogs_adj': 0, 'writeoff': total, 'mismatch': mismatch}
    consumed_billed = min(billed, x['consumed'])
    onhand_billed = billed - consumed_billed
    return {'layer_value': onhand * p + onhand_billed * d, 'cogs_adj': consumed_billed * d, 'writeoff': 0, 'mismatch': mismatch}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 111, 'tolerance': 20}, {'layer_value': 666, 'cogs_adj': 44, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 6}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 51, 'tolerance': 4}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 4, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 10, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 399, 'tolerance': 100}, {'layer_value': 2793, 'cogs_adj': -505, 'writeoff': 0, 'mismatch': False}], ['small credit within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 99, 'tolerance': 6}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': -5, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 112, 'tolerance': 20}, {'layer_value': 672, 'cogs_adj': 48, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 7}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 52, 'tolerance': 8}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 8, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 11, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 398, 'tolerance': 100}, {'layer_value': 2786, 'cogs_adj': -510, 'writeoff': 0, 'mismatch': False}], ['small credit within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 99, 'tolerance': 7}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': -5, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 113, 'tolerance': 20}, {'layer_value': 678, 'cogs_adj': 52, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 2, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1440, 'cogs_adj': -20, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 8}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 53, 'tolerance': 12}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 12, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 12, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 397, 'tolerance': 100}, {'layer_value': 2779, 'cogs_adj': -515, 'writeoff': 0, 'mismatch': False}], ['small credit within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 99, 'tolerance': 8}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': -5, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 114, 'tolerance': 20}, {'layer_value': 684, 'cogs_adj': 56, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 9}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 54, 'tolerance': 16}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 16, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 13, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 396, 'tolerance': 100}, {'layer_value': 2772, 'cogs_adj': -520, 'writeoff': 0, 'mismatch': False}], ['small credit within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 99, 'tolerance': 9}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': -5, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 115, 'tolerance': 20}, {'layer_value': 690, 'cogs_adj': 60, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 10}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 55, 'tolerance': 20}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 20, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 14, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 395, 'tolerance': 100}, {'layer_value': 2765, 'cogs_adj': -525, 'writeoff': 0, 'mismatch': False}], ['small credit within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 99, 'tolerance': 10}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': -5, 'mismatch': False}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A variance exactly equal to the tolerance is booked into inventory.

Input {received, po_price, consumed, invoice_qty, invoice_price, tolerance}. Billed units = min(invoice_qty, received); total variance = billed*(invoice-po). If |total| <= tolerance the variance is written off and the layer is unchanged. Otherwise billed units are matched to consumed units first: consumed billed units adjust COGS, the rest revalue the on-hand layer. Return {layer_value, cogs_adj, writeoff, mismatch}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    q = x['received']
    p = x['po_price']
    onhand = q - x['consumed']
    billed = min(x['invoice_qty'], q)
    d = x['invoice_price'] - p
    total = billed * d
    mismatch = x['invoice_qty'] != q
    if abs(d) <= x['tolerance']:
        return {'layer_value': onhand * p, 'cogs_adj': 0, 'writeoff': total, 'mismatch': mismatch}
    consumed_billed = min(billed, x['consumed'])
    onhand_billed = billed - consumed_billed
    return {'layer_value': onhand * p + onhand_billed * d, 'cogs_adj': consumed_billed * d, 'writeoff': 0, 'mismatch': mismatch}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 111, 'tolerance': 20}, {'layer_value': 666, 'cogs_adj': 44, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 6}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 51, 'tolerance': 4}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 4, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 10, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 399, 'tolerance': 100}, {'layer_value': 2793, 'cogs_adj': -505, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 112, 'tolerance': 20}, {'layer_value': 672, 'cogs_adj': 48, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 7}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 52, 'tolerance': 8}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 8, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 11, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 398, 'tolerance': 100}, {'layer_value': 2786, 'cogs_adj': -510, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 113, 'tolerance': 20}, {'layer_value': 678, 'cogs_adj': 52, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 2, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1440, 'cogs_adj': -20, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 8}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 53, 'tolerance': 12}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 12, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 12, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 397, 'tolerance': 100}, {'layer_value': 2779, 'cogs_adj': -515, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 114, 'tolerance': 20}, {'layer_value': 684, 'cogs_adj': 56, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 9}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 54, 'tolerance': 16}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 16, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 13, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 396, 'tolerance': 100}, {'layer_value': 2772, 'cogs_adj': -520, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 115, 'tolerance': 20}, {'layer_value': 690, 'cogs_adj': 60, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 10}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 55, 'tolerance': 20}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 20, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 14, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 395, 'tolerance': 100}, {'layer_value': 2765, 'cogs_adj': -525, 'writeoff': 0, 'mismatch': False}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    q = x['received']
    p = x['po_price']
    onhand = q - x['consumed']
    billed = min(x['invoice_qty'], q)
    d = x['invoice_price'] - p
    total = billed * d
    mismatch = x['invoice_qty'] != q
    if total <= x['tolerance']:
        return {'layer_value': onhand * p, 'cogs_adj': 0, 'writeoff': total, 'mismatch': mismatch}
    consumed_billed = min(billed, x['consumed'])
    onhand_billed = billed - consumed_billed
    return {'layer_value': onhand * p + onhand_billed * d, 'cogs_adj': consumed_billed * d, 'writeoff': 0, 'mismatch': mismatch}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 111, 'tolerance': 20}, {'layer_value': 666, 'cogs_adj': 44, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 6}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 51, 'tolerance': 4}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 4, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 10, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 399, 'tolerance': 100}, {'layer_value': 2793, 'cogs_adj': -505, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 112, 'tolerance': 20}, {'layer_value': 672, 'cogs_adj': 48, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 7}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 52, 'tolerance': 8}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 8, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 11, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 398, 'tolerance': 100}, {'layer_value': 2786, 'cogs_adj': -510, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 113, 'tolerance': 20}, {'layer_value': 678, 'cogs_adj': 52, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 2, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1440, 'cogs_adj': -20, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 8}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 53, 'tolerance': 12}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 12, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 12, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 397, 'tolerance': 100}, {'layer_value': 2779, 'cogs_adj': -515, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 114, 'tolerance': 20}, {'layer_value': 684, 'cogs_adj': 56, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 9}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 54, 'tolerance': 16}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 16, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 13, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 396, 'tolerance': 100}, {'layer_value': 2772, 'cogs_adj': -520, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 115, 'tolerance': 20}, {'layer_value': 690, 'cogs_adj': 60, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 10}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 55, 'tolerance': 20}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 20, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 14, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 395, 'tolerance': 100}, {'layer_value': 2765, 'cogs_adj': -525, 'writeoff': 0, 'mismatch': False}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Large invoices with a tiny per-unit difference are written off wholesale.

Input {received, po_price, consumed, invoice_qty, invoice_price, tolerance}. Billed units = min(invoice_qty, received); total variance = billed*(invoice-po). If |total| <= tolerance the variance is written off and the layer is unchanged. Otherwise billed units are matched to consumed units first: consumed billed units adjust COGS, the rest revalue the on-hand layer. Return {layer_value, cogs_adj, writeoff, mismatch}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    q = x['received']
    p = x['po_price']
    onhand = q - x['consumed']
    billed = x['invoice_qty']
    d = x['invoice_price'] - p
    total = billed * d
    mismatch = x['invoice_qty'] != q
    if abs(total) <= x['tolerance']:
        return {'layer_value': onhand * p, 'cogs_adj': 0, 'writeoff': total, 'mismatch': mismatch}
    consumed_billed = min(billed, x['consumed'])
    onhand_billed = billed - consumed_billed
    return {'layer_value': onhand * p + onhand_billed * d, 'cogs_adj': consumed_billed * d, 'writeoff': 0, 'mismatch': mismatch}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 111, 'tolerance': 20}, {'layer_value': 666, 'cogs_adj': 44, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 6}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 51, 'tolerance': 4}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 4, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 10, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 399, 'tolerance': 100}, {'layer_value': 2793, 'cogs_adj': -505, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 112, 'tolerance': 20}, {'layer_value': 672, 'cogs_adj': 48, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 7}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 52, 'tolerance': 8}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 8, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 11, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 398, 'tolerance': 100}, {'layer_value': 2786, 'cogs_adj': -510, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 113, 'tolerance': 20}, {'layer_value': 678, 'cogs_adj': 52, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 2, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1440, 'cogs_adj': -20, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 8}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 53, 'tolerance': 12}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 12, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 12, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 397, 'tolerance': 100}, {'layer_value': 2779, 'cogs_adj': -515, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 114, 'tolerance': 20}, {'layer_value': 684, 'cogs_adj': 56, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 9}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 54, 'tolerance': 16}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 16, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 13, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 396, 'tolerance': 100}, {'layer_value': 2772, 'cogs_adj': -520, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 115, 'tolerance': 20}, {'layer_value': 690, 'cogs_adj': 60, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 10}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 55, 'tolerance': 20}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 20, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 14, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 395, 'tolerance': 100}, {'layer_value': 2765, 'cogs_adj': -525, 'writeoff': 0, 'mismatch': False}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    q = x['received']
    p = x['po_price']
    onhand = q - x['consumed']
    billed = q
    d = x['invoice_price'] - p
    total = billed * d
    mismatch = x['invoice_qty'] != q
    if abs(total) <= x['tolerance']:
        return {'layer_value': onhand * p, 'cogs_adj': 0, 'writeoff': total, 'mismatch': mismatch}
    consumed_billed = min(billed, x['consumed'])
    onhand_billed = billed - consumed_billed
    return {'layer_value': onhand * p + onhand_billed * d, 'cogs_adj': consumed_billed * d, 'writeoff': 0, 'mismatch': mismatch}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 111, 'tolerance': 20}, {'layer_value': 666, 'cogs_adj': 44, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 6}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 51, 'tolerance': 4}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 4, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 10, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 399, 'tolerance': 100}, {'layer_value': 2793, 'cogs_adj': -505, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 112, 'tolerance': 20}, {'layer_value': 672, 'cogs_adj': 48, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 7}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 52, 'tolerance': 8}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 8, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 11, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 398, 'tolerance': 100}, {'layer_value': 2786, 'cogs_adj': -510, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 113, 'tolerance': 20}, {'layer_value': 678, 'cogs_adj': 52, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 2, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1440, 'cogs_adj': -20, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 8}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 53, 'tolerance': 12}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 12, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 12, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 397, 'tolerance': 100}, {'layer_value': 2779, 'cogs_adj': -515, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 114, 'tolerance': 20}, {'layer_value': 684, 'cogs_adj': 56, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 9}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 54, 'tolerance': 16}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 16, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 13, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 396, 'tolerance': 100}, {'layer_value': 2772, 'cogs_adj': -520, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 115, 'tolerance': 20}, {'layer_value': 690, 'cogs_adj': 60, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 10}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 55, 'tolerance': 20}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 20, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 14, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 395, 'tolerance': 100}, {'layer_value': 2765, 'cogs_adj': -525, 'writeoff': 0, 'mismatch': False}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
An invoice for more units than received inflates the variance and the layer.

Input {received, po_price, consumed, invoice_qty, invoice_price, tolerance}. Billed units = min(invoice_qty, received); total variance = billed*(invoice-po). If |total| <= tolerance the variance is written off and the layer is unchanged. Otherwise billed units are matched to consumed units first: consumed billed units adjust COGS, the rest revalue the on-hand layer. Return {layer_value, cogs_adj, writeoff, mismatch}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    q = x['received']
    p = x['po_price']
    onhand = q - x['consumed']
    billed = min(x['invoice_qty'], q)
    d = x['invoice_price'] - p
    total = billed * d
    mismatch = x['invoice_qty'] != q
    if abs(total) <= x['tolerance']:
        return {'layer_value': onhand * p, 'cogs_adj': 0, 'writeoff': total, 'mismatch': mismatch}
    consumed_billed = min(billed, x['consumed'])
    onhand_billed = billed - consumed_billed
    return {'layer_value': onhand * x['invoice_price'], 'cogs_adj': consumed_billed * d, 'writeoff': 0, 'mismatch': mismatch}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 111, 'tolerance': 20}, {'layer_value': 666, 'cogs_adj': 44, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 6}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 51, 'tolerance': 4}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 4, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 10, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 399, 'tolerance': 100}, {'layer_value': 2793, 'cogs_adj': -505, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 112, 'tolerance': 20}, {'layer_value': 672, 'cogs_adj': 48, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 7}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 52, 'tolerance': 8}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 8, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 11, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 398, 'tolerance': 100}, {'layer_value': 2786, 'cogs_adj': -510, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 113, 'tolerance': 20}, {'layer_value': 678, 'cogs_adj': 52, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 2, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1440, 'cogs_adj': -20, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 8}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 53, 'tolerance': 12}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 12, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 12, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 397, 'tolerance': 100}, {'layer_value': 2779, 'cogs_adj': -515, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 114, 'tolerance': 20}, {'layer_value': 684, 'cogs_adj': 56, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 9}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 54, 'tolerance': 16}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 16, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 13, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 396, 'tolerance': 100}, {'layer_value': 2772, 'cogs_adj': -520, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 115, 'tolerance': 20}, {'layer_value': 690, 'cogs_adj': 60, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 10}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 55, 'tolerance': 20}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 20, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 14, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 395, 'tolerance': 100}, {'layer_value': 2765, 'cogs_adj': -525, 'writeoff': 0, 'mismatch': False}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    q = x['received']
    p = x['po_price']
    onhand = q - x['consumed']
    billed = min(x['invoice_qty'], q)
    d = x['invoice_price'] - p
    total = billed * d
    mismatch = x['invoice_qty'] != q
    if abs(total) <= x['tolerance']:
        return {'layer_value': onhand * p, 'cogs_adj': 0, 'writeoff': total, 'mismatch': mismatch}
    consumed_billed = min(billed, x['consumed'])
    onhand_billed = billed - consumed_billed
    return {'layer_value': onhand * p + billed * d, 'cogs_adj': consumed_billed * d, 'writeoff': 0, 'mismatch': mismatch}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 111, 'tolerance': 20}, {'layer_value': 666, 'cogs_adj': 44, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 6}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 51, 'tolerance': 4}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 4, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 10, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 399, 'tolerance': 100}, {'layer_value': 2793, 'cogs_adj': -505, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 112, 'tolerance': 20}, {'layer_value': 672, 'cogs_adj': 48, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 7}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 52, 'tolerance': 8}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 8, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 11, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 398, 'tolerance': 100}, {'layer_value': 2786, 'cogs_adj': -510, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 113, 'tolerance': 20}, {'layer_value': 678, 'cogs_adj': 52, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 2, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1440, 'cogs_adj': -20, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 8}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 53, 'tolerance': 12}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 12, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 12, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 397, 'tolerance': 100}, {'layer_value': 2779, 'cogs_adj': -515, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 114, 'tolerance': 20}, {'layer_value': 684, 'cogs_adj': 56, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 3, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 1200, 'cogs_adj': -30, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 9}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 54, 'tolerance': 16}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 16, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 13, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 396, 'tolerance': 100}, {'layer_value': 2772, 'cogs_adj': -520, 'writeoff': 0, 'mismatch': False}]], [['price up partly consumed', {'received': 10, 'po_price': 100, 'consumed': 4, 'invoice_qty': 10, 'invoice_price': 115, 'tolerance': 20}, {'layer_value': 690, 'cogs_adj': 60, 'writeoff': 0, 'mismatch': False}], ['price down', {'received': 8, 'po_price': 250, 'consumed': 4, 'invoice_qty': 8, 'invoice_price': 240, 'tolerance': 5}, {'layer_value': 960, 'cogs_adj': -40, 'writeoff': 0, 'mismatch': False}], ['within tolerance', {'received': 5, 'po_price': 100, 'consumed': 1, 'invoice_qty': 5, 'invoice_price': 101, 'tolerance': 10}, {'layer_value': 400, 'cogs_adj': 0, 'writeoff': 5, 'mismatch': False}], ['tolerance edge', {'received': 4, 'po_price': 50, 'consumed': 0, 'invoice_qty': 4, 'invoice_price': 55, 'tolerance': 20}, {'layer_value': 200, 'cogs_adj': 0, 'writeoff': 20, 'mismatch': False}], ['under billed', {'received': 10, 'po_price': 30, 'consumed': 3, 'invoice_qty': 6, 'invoice_price': 40, 'tolerance': 0}, {'layer_value': 240, 'cogs_adj': 30, 'writeoff': 0, 'mismatch': True}], ['over billed', {'received': 6, 'po_price': 30, 'consumed': 2, 'invoice_qty': 14, 'invoice_price': 35, 'tolerance': 1}, {'layer_value': 140, 'cogs_adj': 10, 'writeoff': 0, 'mismatch': True}], ['large credit', {'received': 12, 'po_price': 500, 'consumed': 5, 'invoice_qty': 12, 'invoice_price': 395, 'tolerance': 100}, {'layer_value': 2765, 'cogs_adj': -525, 'writeoff': 0, 'mismatch': False}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Unbilled units on hand are revalued to the invoice price.

Input {received, po_price, consumed, invoice_qty, invoice_price, tolerance}. Billed units = min(invoice_qty, received); total variance = billed*(invoice-po). If |total| <= tolerance the variance is written off and the layer is unchanged. Otherwise billed units are matched to consumed units first: consumed billed units adjust COGS, the rest revalue the on-hand layer. Return {layer_value, cogs_adj, writeoff, mismatch}.

split: open-access