dodil/erp-brick-inventory

DODIL ERP brick: inventory

tests/world.py

"""Small builders the tests share: a warehouse, a catalogue, a purchase order, a received pallet."""
import fakes
from conftest import MANAGER, OPERATOR, api, must

from brick_inventory.app import app  # noqa: F401


def warehouse(wid="WH1", company="UK1", bins=("A/01", "A/02", "A/03"), zones=("A",), extra_bins=()):
    must(api("POST", "/warehouses", MANAGER, {"warehouse_id": wid, "company_code": company, "name": wid}))
    for z in zones:
        must(api("POST", "/locations", MANAGER, {"location_id": f"{wid}/{z}", "kind": "view"}))
    for b in bins:
        must(api("POST", "/locations", MANAGER, {"location_id": f"{wid}/{b}", "kind": "internal"}))
    for b, kw in extra_bins:
        must(api("POST", "/locations", MANAGER, {"location_id": f"{wid}/{b}", "kind": "internal", **kw}))


def catalogue():
    fakes.add_product("COLA-075", base="LTR", units={"PK24": (18, 1), "C62": (3, 4)}, warehouse="PK24", sales="C62")
    fakes.add_product("SCREW-M4", base="C62", units={"BX100": (100, 1)})
    fakes.add_product("DRILL-500", base="C62", units={})
    fakes.add_product("HOST-M", base="MON", kind="service")
    fakes.add_product("GLOVES", base="C62", units={"BX100": (100, 1)}, issue="BX100")


def purchase(po, product_id, ordered, unit_code, price, line=1, **kw):
    fakes.add_po(po, {line: dict(product_id=product_id, unit_code=unit_code, ordered=ordered, price=price, **kw)})


def receive(product="COLA-075", quantity="8", unit="PK24", price="21.60", location="WH1/A/01", po=None, note=None, **kw):
    """Goods in through the scanner, the real way: scan -> purchasing -> POST /receipts."""
    po = po or f"PO-{len(fakes.POS) + 1:04d}"
    if po not in fakes.POS:
        purchase(po, product, "100000", unit, price, **{k: v for k, v in kw.items() if k.startswith("pricing")})
    line = {"line_no": 1, "quantity": quantity, "unit_code": unit, **{k: v for k, v in kw.items() if k in ("lot_code", "serials", "expires_on")}}
    return must(api("POST", "/receipts/scan", OPERATOR, {"purchase_order_id": po, "delivery_note": note or f"DN-{len(fakes.RECEIPTS) + 1}",
                                                          "location_id": location, "lines": [line]}))