Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

5.9 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { describe, expect, it } from "vitest";
import {
  effectiveInvoiceAutomation,
  type PlannableLine,
  planRentalInvoices,
  RENTAL_HOLD_CHARGES,
  RENTAL_HOLD_EARLY_RETURN,
  rentalHoldRebill
} from "./rental-invoice-plan";

const line = (
  item: string,
  lineType: PlannableLine<string>["lineType"],
  overrides: Partial<PlannableLine<string>> = {}
): PlannableLine<string> => ({
  item,
  lineType,
  isAdjustment: false,
  invoiceSeparately: false,
  voidedInvoiceReadableId: null,
  ...overrides
});

describe("planRentalInvoices", () => {
  it("Draft Only keeps rent and charges on one unheld invoice", () => {
    expect(
      planRentalInvoices("Draft Only", [
        line("rent", "Rent"),
        line("damage", "Charge")
      ])
    ).toEqual([
      { lines: ["rent", "damage"], holdReason: null, role: "combined" }
    ]);
  });

  it("Post with rent only drafts one unheld rent invoice", () => {
    expect(planRentalInvoices("Post", [line("rent", "Rent")])).toEqual([
      { lines: ["rent"], holdReason: null, role: "rent" }
    ]);
  });

  it("Post splits off separately invoiced charges and holds them", () => {
    expect(
      planRentalInvoices("Post", [
        line("rent", "Rent"),
        line("damage", "Charge", { invoiceSeparately: true }),
        line("buyout", "Purchase Option")
      ])
    ).toEqual([
      { lines: ["rent"], holdReason: null, role: "rent" },
      {
        lines: ["damage", "buyout"],
        holdReason: RENTAL_HOLD_CHARGES,
        role: "charges"
      }
    ]);
  });

  it("puts a charge on the unheld rent invoice by default", () => {
    expect(
      planRentalInvoices("Post and Email", [
        line("rent", "Rent"),
        line("insurance", "Charge"),
        line("damage", "Charge", { invoiceSeparately: true })
      ])
    ).toEqual([
      { lines: ["rent", "insurance"], holdReason: null, role: "rent" },
      { lines: ["damage"], holdReason: RENTAL_HOLD_CHARGES, role: "charges" }
    ]);
  });

  it("drafts a charge unheld when no rent is due", () => {
    expect(planRentalInvoices("Post", [line("insurance", "Charge")])).toEqual([
      { lines: ["insurance"], holdReason: null, role: "rent" }
    ]);
  });

  it("always invoices a Purchase Option separately", () => {
    expect(
      planRentalInvoices("Post", [
        line("rent", "Rent"),
        line("buyout", "Purchase Option")
      ])
    ).toEqual([
      { lines: ["rent"], holdReason: null, role: "rent" },
      { lines: ["buyout"], holdReason: RENTAL_HOLD_CHARGES, role: "charges" }
    ]);
  });

  it("holds a rent invoice re-billing a voided charge", () => {
    expect(
      planRentalInvoices("Post", [
        line("rent", "Rent"),
        line("insurance", "Charge", { voidedInvoiceReadableId: "INV-7" })
      ])
    ).toEqual([
      {
        lines: ["rent", "insurance"],
        holdReason: rentalHoldRebill(["INV-7"]),
        role: "rent"
      }
    ]);
  });

  it("holds a rent invoice carrying an early-return adjustment", () => {
    expect(
      planRentalInvoices("Post and Email", [
        line("rent", "Rent"),
        line("credit", "Rent", { isAdjustment: true })
      ])
    ).toEqual([
      {
        lines: ["rent", "credit"],
        holdReason: RENTAL_HOLD_EARLY_RETURN,
        role: "rent"
      }
    ]);
  });

  it("Post with separate charges only drafts one held charges invoice", () => {
    expect(
      planRentalInvoices("Post", [
        line("damage", "Charge", { invoiceSeparately: true })
      ])
    ).toEqual([
      { lines: ["damage"], holdReason: RENTAL_HOLD_CHARGES, role: "charges" }
    ]);
  });

  it("holds a re-bill of a voided invoice", () => {
    const planned = planRentalInvoices("Post and Email", [
      line("p1", "Rent", { voidedInvoiceReadableId: "INV-7" }),
      line("p2", "Rent", { voidedInvoiceReadableId: "INV-7" }),
      line("p3", "Rent")
    ]);
    expect(planned).toEqual([
      {
        lines: ["p1", "p2", "p3"],
        holdReason: rentalHoldRebill(["INV-7"]),
        role: "rent"
      }
    ]);
    expect(planned[0]?.holdReason).toBe("Re-billing INV-7, which was voided");
  });

  it("the re-bill hold wins over the early-return hold", () => {
    const planned = planRentalInvoices("Post", [
      line("p1", "Rent", { voidedInvoiceReadableId: "INV-7" }),
      line("credit", "Rent", { isAdjustment: true })
    ]);
    expect(planned[0]?.holdReason).toBe(rentalHoldRebill(["INV-7"]));
  });

  it("names every voided invoice once, in first-seen order", () => {
    expect(rentalHoldRebill(["INV-7", "INV-9"])).toBe(
      "Re-billing INV-7, INV-9, which were voided"
    );
    const planned = planRentalInvoices("Post", [
      line("p1", "Rent", { voidedInvoiceReadableId: "INV-9" }),
      line("p2", "Rent", { voidedInvoiceReadableId: "INV-7" }),
      line("p3", "Rent", { voidedInvoiceReadableId: "INV-9" })
    ]);
    expect(planned[0]?.holdReason).toBe(rentalHoldRebill(["INV-9", "INV-7"]));
  });

  it("Draft Only never holds, even a re-bill", () => {
    expect(
      planRentalInvoices("Draft Only", [
        line("p1", "Rent", { voidedInvoiceReadableId: "INV-7" })
      ])
    ).toEqual([{ lines: ["p1"], holdReason: null, role: "combined" }]);
  });

  it("drafts nothing when nothing is due", () => {
    expect(planRentalInvoices("Draft Only", [])).toEqual([]);
    expect(planRentalInvoices("Post", [])).toEqual([]);
    expect(planRentalInvoices("Post and Email", [])).toEqual([]);
  });
});

describe("effectiveInvoiceAutomation", () => {
  it("falls back to the company mode", () => {
    expect(effectiveInvoiceAutomation(null, "Post")).toBe("Post");
  });

  it("prefers the agreement's override", () => {
    expect(effectiveInvoiceAutomation("Draft Only", "Post and Email")).toBe(
      "Draft Only"
    );
  });
});