dodil/erp-brick-gl

DODIL ERP brick: general ledger

tests/fakes.py

"""Stand-ins for the neighbours gl asks, answering EXACTLY what their spec routes answer (shape and status codes)."""
from erp_platform import seam
from fastapi import FastAPI


class Payments:
    """payments GET /settlement?invoice_ids=<csv>&as_at= -> {as_at, invoices: [{invoice_id, currency, total, outstanding, state}], unknown}"""

    def __init__(self):
        self.invoices = {}          # invoice_id -> {currency, total, outstanding}
        self.calls = []
        app = FastAPI()

        @app.get("/settlement")
        def settlement(invoice_ids: str, as_at: str = ""):
            ids = [i for i in invoice_ids.split(",") if i][:200]
            self.calls.append(ids)
            found = [{"invoice_id": i, "state": "open", **self.invoices[i]} for i in ids if i in self.invoices]
            return {"as_at": as_at, "invoices": found, "unknown": [i for i in ids if i not in self.invoices]}
        self.app = app

    def install(self):
        seam.register_local("payments", self.app)
        return self


class Purchasing:
    """purchasing GET /bills/open?as_of= -> {as_of, items: [...]}; GET /intercompany/open"""

    def __init__(self):
        self.bills = []
        self.ic = []
        app = FastAPI()

        @app.get("/bills/open")
        def bills(as_of: str, bp_id: int = 0):
            return {"as_of": as_of, "items": self.bills}

        @app.get("/intercompany/open")
        def ic(company_code: str, trading_partner: str, as_at: str):
            return {"company_code": company_code, "trading_partner": trading_partner, "as_at": as_at, "documents": self.ic}
        self.app = app

    def install(self):
        seam.register_local("purchasing", self.app)
        return self


class Assets:
    """fixed-assets GET /assets/balances and GET /depreciation/status"""

    def __init__(self):
        self.balances = {}          # ledger_id -> accounts list
        self.posted = {}            # ledger_id -> bool
        app = FastAPI()

        @app.get("/assets/balances")
        def balances(company_code: str, ledger_id: str, as_of: str):
            return {"company_code": company_code, "ledger_id": ledger_id, "as_of": as_of, "accounts": self.balances.get(ledger_id, []),
                    "clearing": {"account_purpose": "asset_clearing", "open_acquisitions": []}}

        @app.get("/depreciation/status")
        def status(company_code: str, period_id: str, ledger_id: str = ""):
            lids = [ledger_id] if ledger_id else sorted(self.posted)
            return {"company_code": company_code, "period_id": period_id,
                    "areas": [{"area_code": f"A-{lg}", "ledger_id": lg, "posted": self.posted.get(lg, False), "run_id": "r1"} for lg in lids],
                    "pending_transactions": 0,
                    "readiness": [{"gate": "depreciation_posted", "met": self.posted.get(lg, False), "reason": "not posted", "fix": "run it"}
                                  for lg in lids]}
        self.app = app

    def install(self):
        seam.register_local("fixed-assets", self.app)
        return self