dodil/erp-brick-purchasing

DODIL ERP brick: purchasing

tests/test_stories_intercompany.py

"""PUR-64: a bill another of our companies sent; PUR-65: requests from sales-order and replenishment."""
from conftest import ALICE, BUYER, CLERK, INVENTORY, PAYAPPROVER, api, must, world

BODY = {"company_code": "UK02", "bill_id": "ic:INV-90", "supplier_company_code": "UK01", "supplier_invoice_no": "INV-90", "issue_date": "2026-10-20", "tax_point_date": "2026-10-20",
        "due_date": "2026-11-19", "currency": "GBP", "lines": [{"line_no": 1, "description": "management recharge", "quantity": "1", "unit_code": "C62", "unit_price": "500.00",
                                                              "net_amount": "500.00", "tax_code": "S20", "tax_amount": "100.00"}]}


def test_pur64_the_bill_is_recorded_once_posts_with_ic_reference_and_is_listed_open_for_gl(neighbours):
    world()
    r = must(api("POST", "/bills/intercompany", CLERK, BODY))
    assert r == {"bill_id": "ic:INV-90", "status": "recorded", "replay": False}
    assert must(api("POST", "/bills/intercompany", CLERK, BODY))["replay"] is True
    b = must(api("GET", "/bills/ic:INV-90", CLERK))
    assert b["company_code"] == "UK02" and b["partner_company"] == "UK01" and str(b["total"]) == "600.00" and all(x["ic_reference"] == "INV-90" for x in b["lines"])
    must(api("POST", "/bills/ic:INV-90/approve", PAYAPPROVER, {}))
    (j,) = neighbours.gl.posted("ic:INV-90", "approve")
    assert all(x.get("partner_company") == "UK01" and x.get("ic_reference") == "INV-90" for x in j["lines"])
    got = must(api("GET", "/intercompany/open?company_code=UK02&trading_partner=UK01&as_at=2026-11-30", ALICE))
    assert [(d["bill_id"], d["ic_reference"], str(d["outstanding"])) for d in got["documents"]] == [("ic:INV-90", "INV-90", "600.00")]
    assert must(api("GET", "/intercompany/open?company_code=UK02&trading_partner=UK03&as_at=2026-10-31", ALICE))["documents"] == []


def test_pur64_the_same_company_or_a_wrong_id_is_refused_and_access_is_checked():
    world()
    assert api("POST", "/bills/intercompany", CLERK, {**BODY, "supplier_company_code": "UK02"})[0] == 409
    assert api("POST", "/bills/intercompany", CLERK, {**BODY, "bill_id": "INV-90"})[0] == 422
    assert api("POST", "/bills/intercompany", BUYER, BODY)[0] == 403


def test_pur65_requests_from_sales_order_and_replenishment_are_keyed_and_idempotent():
    world()
    body = {"request_ref": "inv:repl:WH1:COLA-075:2026-11-03", "warehouse_id": "WH1", "location_id": "WH1/IN", "product_id": "COLA-075", "quantity": "40", "unit_code": "PK24",
            "needed_on": "2026-11-03"}
    s, r = api("POST", "/purchase-requests/from-replenishment", INVENTORY, body)
    assert s == 200, r
    assert r["request_ref"] == body["request_ref"] and r["status"] == "awaiting_approval"
    again = must(api("POST", "/purchase-requests/from-replenishment", INVENTORY, body))
    assert again["request_id"] == r["request_id"]
    assert api("POST", "/purchase-requests/from-replenishment", BUYER, body)[0] == 403