dodil/erp-brick-purchasing

DODIL ERP brick: purchasing

tests/test_stories_delivery_costs.py

"""PUR-62 (delivery costs on the order and the receipt's unit cost) and PUR-63 (the difference handed to inventory)."""
from conftest import ALICE, PAYAPPROVER, SUPPLIER_B, api, bill, confirmed, must, receive, supplier_price, world


def import_order():
    world()
    with __import__("conftest").on_day("2026-01-01T09:00:00+00:00"):
        must(api("POST", "/account-map", ALICE, {"purpose": "delivery_cost_clearing", "category": "*", "account_id": "2160", "valid_from": "2026-01-02"}))
    supplier_price(price="15.0000")
    return confirmed(lines=[{"product_id": "COLA-075", "quantity": "1000", "delivery_costs": [{"kind": "freight", "amount": "600.00", "payee_bp_id": SUPPLIER_B},
                                                                                            {"kind": "duty", "percent": "4"}]}])


def test_pur62_the_receipt_hands_inventory_a_unit_cost_including_the_planned_costs(neighbours):
    o = import_order()
    receive(o, quantity="1000")
    (m,) = neighbours.inventory.moves.values()
    assert m["body"]["unit_cost"] == "16.2000" and sorted((c["kind"], c["amount"]) for c in m["body"]["delivery_costs"]) == [("duty", "600.00"), ("freight", "600.00")]


def freight_bill(o, amount="640.00"):
    return bill(o, bp=SUPPLIER_B, number="W-77", lines=[{"quantity": "1", "unit_code": "C62", "unit_price": amount, "delivery_cost_ref": "freight"}])


def test_pur62_the_forwarders_bill_fails_kw_until_a_second_person_accepts_it_and_the_difference_goes_to_inventory(neighbours):
    o = import_order()
    receive(o, quantity="1000")
    s, b = freight_bill(o)
    assert s == 200, b
    s, r = api("POST", f"/bills/{b['bill_id']}/approve", PAYAPPROVER, {})
    assert s == 409 and "delivery_cost" in str(r)
    must(api("POST", f"/bills/{b['bill_id']}/accept-variance", PAYAPPROVER, {"line_no": 1, "kind": "delivery_cost", "reason": "fuel surcharge agreed by phone"}))
    must(api("POST", f"/bills/{b['bill_id']}/approve", PAYAPPROVER, {}))
    adj = neighbours.inventory.adjustments[f"pur:{b['bill_id']}:1"]
    assert adj["amount"] == "40.00" and adj["source_kind"] == "purchase_delivery_cost" and adj["purchase_order_id"] == o["purchase_order_id"]
    again = must(api("POST", f"/bills/{b['bill_id']}/hand-over-assets", __import__("conftest").SYSTEM, {}))
    assert again["handovers"][0]["kind"] == "valuation" and again["handovers"][0]["status"] == "done" and len(neighbours.inventory.adjustments) == 1


def test_pur63_a_bill_at_the_planned_cost_hands_nothing_over(neighbours):
    o = import_order()
    receive(o, quantity="1000")
    s, b = freight_bill(o, "600.00")
    must(api("POST", f"/bills/{b['bill_id']}/approve", PAYAPPROVER, {}))
    assert neighbours.inventory.adjustments == {}