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.

19.4 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 { credit, debit } from "@carbon/utils";
import { describe, expect, it } from "vitest";
import {
  type BuildPurchaseInvoicePostingLinesInput,
  buildPurchaseInvoicePostingLines,
  type InvoicedPurchaseOrderLine,
  type PurchaseInvoiceItem,
  type PurchaseInvoiceJournalLine,
  type PurchaseInvoicePostingLine, calculatePurchasePostingAmounts } from "../../../app/modules/invoicing/invoicing.service";
const accounts: BuildPurchaseInvoicePostingLinesInput["accounts"] = {
  goodsReceivedNotInvoicedAccount: "grni",
  purchaseVarianceAccount: "ppv",
  indirectCostAccount: "indirect",
  workInProgressAccount: "wip",
  rawMaterialsAccount: "raw",
  finishedGoodsAccount: "finished"
};

const part: PurchaseInvoiceItem = {
  itemId: "item-1",
  itemTrackingType: "Inventory",
  replenishmentSystem: "Buy",
  itemPostingGroupId: "group-1",
  costingMethod: "FIFO"
};

function build(lines: PurchaseInvoicePostingLine[]) {
  return buildPurchaseInvoicePostingLines({
    invoice: {
      id: "inv-1",
      supplierId: "supplier-1",
      supplierReference: "S-9"
    },
    supplierTypeId: "supplier-type-1",
    accounts,
    payables: { accountId: "ap", accountDefaultRole: null },
    lines
  });
}

function amounts(
  inventoryQuantity: number,
  totalBaseCost: number,
  nominalBaseCost = totalBaseCost
) {
  return {
    inventoryQuantity,
    nominalBaseCost,
    totalBaseCost,
    inventoryUnitCost: totalBaseCost / inventoryQuantity
  };
}

function ordered(
  id: string,
  quantity: number,
  total: number,
  purchaseOrderLine: Partial<InvoicedPurchaseOrderLine>,
  item: PurchaseInvoiceItem = part
): PurchaseInvoicePostingLine {
  return {
    id,
    invoiceLineType: "Part",
    locationId: "loc-1",
    amounts: amounts(quantity, total),
    kind: "ordered",
    item,
    purchaseOrderLine: {
      purchaseOrderLineId: `pol-${id}`,
      quantityReceived: 0,
      quantityInvoiced: 0,
      receiptGroups: [],
      isOutsideProcessing: false,
      processId: null,
      coverage: { receiptLayers: [], onHandQuantity: 0 },
      ...purchaseOrderLine
    }
  };
}

/** Account, amount and quantity of each line, in order. */
const summary = (lines: PurchaseInvoiceJournalLine[]) =>
  lines.map(({ accountId, amount, quantity, accrual }) => ({
    accountId,
    amount,
    quantity,
    ...(accrual ? { accrual } : {})
  }));

describe("buildPurchaseInvoicePostingLines", () => {
  it("clears GR/IR at receipt cost and splits the variance between inventory and purchase variance", () => {
    // 10 received at 8, invoiced at 10: a variance of 20, 2 a unit. The
    // layer still holds 4 units, so 8 writes up inventory and 12 is PPV.
    const { lines, receivedVariances } = build([
      ordered("a", 10, 100, {
        quantityReceived: 10,
        receiptGroups: [{ quantity: 10, cost: 80 }],
        coverage: {
          receiptLayers: [
            { id: "layer-1", quantity: 10, remainingQuantity: 4 }
          ],
          onHandQuantity: 0
        }
      })
    ]);

    expect(summary(lines)).toEqual([
      { accountId: "grni", amount: debit("liability", 80), quantity: 10 },
      { accountId: "raw", amount: debit("asset", 8), quantity: 10 },
      { accountId: "ppv", amount: debit("expense", 12), quantity: 10 },
      { accountId: "ap", amount: credit("liability", 100), quantity: 10 }
    ]);
    expect(new Set(lines.map((line) => line.journalLineReference)).size).toBe(
      1
    );
    for (const line of lines) {
      expect(line.documentLineReference).toBe("purchase-invoice:pol-a");
      expect(line.documentType).toBe("Invoice");
      expect(line.documentId).toBe("inv-1");
      expect(line.externalDocumentId).toBe("S-9");
      expect(line.dimensions).toEqual({
        SupplierType: "supplier-type-1",
        ItemPostingGroup: "group-1",
        Item: "item-1",
        Supplier: "supplier-1",
        Location: "loc-1",
        CostCenter: null,
        Project: null,
        Process: null,
        FixedAssetClass: null
      });
    }
    expect(receivedVariances.get("a")).toMatchObject({
      quantity: 10,
      receiptCost: 80,
      variance: 20,
      allocation: {
        inventoryShare: 8,
        ppvShare: 12,
        perLayer: [
          { costLedgerId: "layer-1", appliedQuantity: 4, adjustmentCost: 8 }
        ]
      },
      selfHealQuantity: 0
    });
  });

  it("writes up an Average item's inventory only for the units still on hand", () => {
    // Average costing never draws a receipt layer down, so the layer still
    // reads 10 remaining after 6 of the 10 were sold. On-hand (4) decides.
    const { lines, receivedVariances } = build([
      ordered(
        "a",
        10,
        100,
        {
          quantityReceived: 10,
          receiptGroups: [{ quantity: 10, cost: 80 }],
          coverage: {
            receiptLayers: [
              { id: "layer-1", quantity: 10, remainingQuantity: 10 }
            ],
            onHandQuantity: 4
          }
        },
        { ...part, costingMethod: "Average" }
      )
    ]);

    expect(summary(lines)).toEqual([
      { accountId: "grni", amount: debit("liability", 80), quantity: 10 },
      { accountId: "raw", amount: debit("asset", 8), quantity: 10 },
      { accountId: "ppv", amount: debit("expense", 12), quantity: 10 },
      { accountId: "ap", amount: credit("liability", 100), quantity: 10 }
    ]);
    expect(receivedVariances.get("a")?.allocation.perLayer).toEqual([
      { costLedgerId: "layer-1", appliedQuantity: 4, adjustmentCost: 8 }
    ]);
  });

  it("shares an Average item's on-hand across the invoice's lines of that item", () => {
    // Two lines of 5, each 2 a unit over receipt cost, with 6 on hand: the
    // first line's 5 units are on hand, then only 1 of the second's.
    const average = { ...part, costingMethod: "Average" as const };
    const coverage = (id: string) => ({
      receiptLayers: [{ id, quantity: 5, remainingQuantity: 5 }],
      onHandQuantity: 6
    });
    const { lines } = build([
      ordered(
        "a",
        5,
        50,
        {
          quantityReceived: 5,
          receiptGroups: [{ quantity: 5, cost: 40 }],
          coverage: coverage("layer-a")
        },
        average
      ),
      ordered(
        "b",
        5,
        50,
        {
          quantityReceived: 5,
          receiptGroups: [{ quantity: 5, cost: 40 }],
          coverage: coverage("layer-b")
        },
        average
      )
    ]);

    expect(summary(lines)).toEqual([
      { accountId: "grni", amount: debit("liability", 40), quantity: 5 },
      { accountId: "raw", amount: debit("asset", 10), quantity: 5 },
      { accountId: "ap", amount: credit("liability", 50), quantity: 5 },
      { accountId: "grni", amount: debit("liability", 40), quantity: 5 },
      { accountId: "raw", amount: debit("asset", 2), quantity: 5 },
      { accountId: "ppv", amount: debit("expense", 8), quantity: 5 },
      { accountId: "ap", amount: credit("liability", 50), quantity: 5 }
    ]);
  });

  it("skips the units an earlier invoice cleared and accrues the units not yet received", () => {
    // Two receipts of 3 (at 6, then at 9); an earlier invoice cleared 3, so
    // this invoice of 5 clears the second receipt and accrues 2.
    const { lines, receivedVariances } = build([
      ordered("a", 5, 50, {
        quantityReceived: 6,
        quantityInvoiced: 3,
        receiptGroups: [
          { quantity: 3, cost: 18 },
          { quantity: 3, cost: 27 }
        ]
      })
    ]);

    expect(summary(lines)).toEqual([
      { accountId: "grni", amount: debit("liability", 27), quantity: 3 },
      { accountId: "ppv", amount: debit("expense", 3), quantity: 3 },
      { accountId: "ap", amount: credit("liability", 30), quantity: 3 },
      {
        accountId: "grni",
        amount: debit("liability", 20),
        quantity: 2,
        accrual: true
      },
      {
        accountId: "ap",
        amount: credit("liability", 20),
        quantity: 2,
        accrual: true
      }
    ]);
    // The cleared units and the accrued units are two pairs.
    expect(lines[0]!.journalLineReference).toBe(lines[2]!.journalLineReference);
    expect(lines[3]!.journalLineReference).toBe(lines[4]!.journalLineReference);
    expect(lines[0]!.journalLineReference).not.toBe(
      lines[3]!.journalLineReference
    );
    // No layer and nothing on hand: the whole variance is PPV.
    expect(receivedVariances.get("a")?.allocation.ppvShare).toBeCloseTo(3, 10);
  });

  it("expenses an unreceived service to indirect cost with no accrual", () => {
    const service = ordered("s", 2, 40, {});
    const { lines } = build([{ ...service, invoiceLineType: "Service" }]);
    expect(summary(lines)).toEqual([
      { accountId: "indirect", amount: debit("asset", 40), quantity: 2 },
      { accountId: "ap", amount: credit("liability", 40), quantity: 2 }
    ]);
  });

  it("shares a stored write-up across the lines of one item by their variance", () => {
    const writeUp = { coverage: { storedWriteUp: 6 } };
    const { lines } = build([
      ordered("a", 1, 30, {
        quantityReceived: 1,
        receiptGroups: [{ quantity: 1, cost: 20 }],
        ...writeUp
      }),
      ordered("b", 1, 40, {
        quantityReceived: 1,
        receiptGroups: [{ quantity: 1, cost: 20 }],
        ...writeUp
      })
    ]);
    // Variances 10 and 20 take 2 and 4 of the write-up.
    expect(summary(lines).filter((line) => line.accountId !== "grni")).toEqual([
      { accountId: "raw", amount: debit("asset", 2), quantity: 1 },
      { accountId: "ppv", amount: debit("expense", 8), quantity: 1 },
      { accountId: "ap", amount: credit("liability", 30), quantity: 1 },
      { accountId: "raw", amount: debit("asset", 4), quantity: 1 },
      { accountId: "ppv", amount: debit("expense", 16), quantity: 1 },
      { accountId: "ap", amount: credit("liability", 40), quantity: 1 }
    ]);
  });

  it("refuses a received line whose item has no cost record", () => {
    expect(() =>
      build([
        ordered(
          "a",
          1,
          10,
          { quantityReceived: 1, receiptGroups: [{ quantity: 1, cost: 8 }] },
          { ...part, costingMethod: null }
        )
      ])
    ).toThrow("Item item-1 on a purchase invoice has no cost record");
  });

  it("posts a lease payment to the liability and a variable charge to expense", () => {
    const base = { invoiceLineType: "Lease" as const, locationId: null };
    const { lines } = build([
      {
        ...base,
        id: "rent",
        amounts: amounts(1, 10000),
        kind: "lease",
        leaseId: "lease-1",
        account: {
          id: "lease-liability",
          name: "Lease Liabilities",
          class: "Liability"
        },
        extrasAccount: {
          id: "opex",
          name: "Operating Lease Expense",
          class: "Expense"
        }
      },
      {
        ...base,
        id: "cam",
        amounts: amounts(1, 250),
        kind: "lease",
        leaseId: "lease-1",
        account: {
          id: "variable",
          name: "Variable Lease Expense",
          class: "Expense"
        },
        extrasAccount: null
      }
    ]);

    expect(
      lines.map(({ accountId, amount }) => ({ accountId, amount }))
    ).toEqual([
      { accountId: "lease-liability", amount: debit("liability", 10000) },
      { accountId: "ap", amount: credit("liability", 10000) },
      { accountId: "variable", amount: debit("expense", 250) },
      { accountId: "ap", amount: credit("liability", 250) }
    ]);
    // Liability debit stores negative, AP credit positive: the pair nets out.
    expect(lines[0]!.amount + lines[1]!.amount).toBe(0);
    expect(lines.every((line) => line.dimensions.CostCenter == null)).toBe(
      true
    );
  });

  it("debits the lease liability with the nominal payment only, and its tax and freight to lease expense", () => {
    // A 10,000 payment with 825 tax and 50 shipping on the line, and 100 of
    // header freight: the schedule took 10,000 out of the liability, so that
    // is all the liability takes. AP owes the whole 10,975.
    const [rent] = calculatePurchasePostingAmounts({
      lines: [
        {
          id: "rent",
          invoiceLineType: "Lease",
          quantity: 1,
          conversionFactor: 1,
          unitPrice: 10000,
          shippingCost: 50,
          taxAmount: 825
        }
      ],
      exchangeRate: 1,
      supplierShippingCost: 100
    });
    const opex = {
      id: "opex",
      name: "Operating Lease Expense",
      class: "Expense" as const
    };
    const { lines } = build([
      {
        id: "rent",
        invoiceLineType: "Lease",
        locationId: null,
        amounts: rent!,
        kind: "lease",
        leaseId: "lease-1",
        account: {
          id: "lease-liability",
          name: "Lease Liabilities",
          class: "Liability"
        },
        extrasAccount: opex
      },
      {
        // A short-term payment expenses its whole total, tax included.
        id: "short",
        invoiceLineType: "Lease",
        locationId: null,
        amounts: amounts(1, 1100, 1000),
        kind: "lease",
        leaseId: "lease-1",
        account: opex,
        extrasAccount: null
      }
    ]);

    expect(
      lines.map(({ accountId, amount }) => ({ accountId, amount }))
    ).toEqual([
      { accountId: "lease-liability", amount: debit("liability", 10000) },
      { accountId: "opex", amount: debit("expense", 975) },
      { accountId: "ap", amount: credit("liability", 10975) },
      { accountId: "opex", amount: debit("expense", 1100) },
      { accountId: "ap", amount: credit("liability", 1100) }
    ]);
    // The rent's three lines share one reference and balance: the liability
    // debit stores negative, the expense debit and AP credit positive.
    expect(
      new Set(lines.slice(0, 3).map((l) => l.journalLineReference)).size
    ).toBe(1);
    expect(-lines[0]!.amount + lines[1]!.amount - lines[2]!.amount).toBe(0);
  });

  it("posts G/L and fixed asset lines", () => {
    const base = { invoiceLineType: "G/L Account" as const, locationId: null };
    const { lines, receivedVariances } = build([
      {
        ...base,
        id: "gl",
        amounts: amounts(1, 20),
        kind: "glAccount",
        purchaseOrderLineId: null,
        account: { id: "office", name: "Office Supplies" },
        costCenterId: "cc-1",
        projectId: "project-1"
      },
      {
        ...base,
        id: "fa-direct",
        invoiceLineType: "Fixed Asset",
        amounts: amounts(1, 500),
        kind: "fixedAsset",
        purchaseOrderLineId: null,
        purchaseOrderLineLocationId: null,
        assetLocationId: "loc-asset",
        fixedAssetClassId: "class-1",
        acquisition: { assetAccountId: "machinery" }
      },
      {
        ...base,
        id: "fa-received",
        invoiceLineType: "Fixed Asset",
        amounts: amounts(1, 510),
        kind: "fixedAsset",
        purchaseOrderLineId: "pol-fa",
        purchaseOrderLineLocationId: "loc-po",
        assetLocationId: "loc-asset",
        fixedAssetClassId: "class-1",
        acquisition: { receiptCost: 500 }
      }
    ]);

    expect(
      lines.map(
        ({ accountId, description, amount, documentLineReference }) => ({
          accountId,
          description,
          amount,
          documentLineReference
        })
      )
    ).toEqual([
      {
        accountId: "office",
        description: "Office Supplies",
        amount: debit("asset", 20),
        documentLineReference: null
      },
      {
        accountId: "ap",
        description: "Accounts Payable",
        amount: credit("liability", 20),
        documentLineReference: null
      },
      {
        accountId: "machinery",
        description: "Fixed Asset Acquisition",
        amount: debit("asset", 500),
        documentLineReference: null
      },
      {
        accountId: "ap",
        description: "Accounts Payable",
        amount: credit("liability", 500),
        documentLineReference: null
      },
      {
        accountId: "grni",
        description: "GR/IR Clearing",
        amount: debit("liability", 500),
        documentLineReference: "purchase-invoice:pol-fa"
      },
      {
        accountId: "ppv",
        description: "Purchase Price Variance",
        amount: debit("expense", 10),
        documentLineReference: "purchase-invoice:pol-fa"
      },
      {
        accountId: "ap",
        description: "Accounts Payable",
        amount: credit("liability", 510),
        documentLineReference: "purchase-invoice:pol-fa"
      }
    ]);
    // A G/L line carries no supplier type; its cost center and project do.
    expect(lines[0]!.dimensions).toMatchObject({
      SupplierType: null,
      Supplier: "supplier-1",
      CostCenter: "cc-1",
      Project: "project-1"
    });
    // A fixed asset line's location is its own, else the PO line's, else
    // the asset's.
    expect(lines[2]!.dimensions).toMatchObject({
      Location: "loc-asset",
      FixedAssetClass: "class-1"
    });
    expect(lines[4]!.dimensions).toMatchObject({ Location: "loc-po" });
    expect(receivedVariances.get("fa-received")?.variance).toBe(10);
    expect(receivedVariances.has("fa-direct")).toBe(false);
  });

  it("capitalizes tax and line shipping, and converts a foreign header freight to base", () => {
    // EUR invoice at 0.5 EUR per USD: the header freight of 20 EUR is 40 USD,
    // shared 115 : 85 by line cost. Line amounts are already base.
    const postingAmounts = calculatePurchasePostingAmounts({
      lines: [
        {
          id: "gl",
          invoiceLineType: "G/L Account",
          quantity: 2,
          conversionFactor: 1,
          unitPrice: 50,
          shippingCost: 5,
          taxAmount: 10
        },
        {
          id: "part",
          invoiceLineType: "Part",
          quantity: 1,
          conversionFactor: 1,
          unitPrice: 85,
          shippingCost: 0,
          taxAmount: 0
        }
      ],
      exchangeRate: 0.5,
      supplierShippingCost: 20
    });
    const [glAmounts, partAmounts] = postingAmounts;
    const { lines } = build([
      {
        id: "gl",
        invoiceLineType: "G/L Account",
        locationId: null,
        amounts: glAmounts!,
        kind: "glAccount",
        purchaseOrderLineId: null,
        account: { id: "office", name: "Office Supplies" },
        costCenterId: null,
        projectId: null
      },
      {
        id: "part",
        invoiceLineType: "Part",
        locationId: "loc-1",
        amounts: partAmounts!,
        kind: "direct",
        item: part,
        receivedWithInvoice: true
      }
    ]);

    expect(summary(lines)).toEqual([
      { accountId: "office", amount: debit("asset", 138), quantity: 2 },
      { accountId: "ap", amount: credit("liability", 138), quantity: 2 },
      { accountId: "raw", amount: debit("asset", 102), quantity: 1 },
      { accountId: "ap", amount: credit("liability", 102), quantity: 1 }
    ]);
    // A direct line names no PO line.
    expect(lines[2]!.documentLineReference).toBeUndefined();
  });

  it("debits WIP for a direct line whose receipt is not posted with the invoice", () => {
    const { lines } = build([
      {
        id: "part",
        invoiceLineType: "Part",
        locationId: "loc-1",
        amounts: amounts(1, 10),
        kind: "direct",
        item: part,
        receivedWithInvoice: false
      }
    ]);
    expect(lines[0]).toMatchObject({
      accountId: "wip",
      description: "WIP Account"
    });
  });
});