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.

4.5 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 { expect, it } from "vitest";
import {
  getBillableQuantity,
  getCompletionAfterQuantityChange,
  getRemainingQuantityToInvoice,
  type ShortCloseInvoicingLine
} from "./short-close";

const line = (
  overrides: Partial<ShortCloseInvoicingLine>
): ShortCloseInvoicingLine => ({
  purchaseQuantity: 9,
  quantityReceived: 0,
  quantityInvoiced: 0,
  receivedComplete: false,
  ...overrides
});

it("open line bills the ordered quantity", () => {
  const l = line({ quantityReceived: 2 });
  expect(getBillableQuantity(l)).toEqual(9);
  expect(getRemainingQuantityToInvoice(l)).toEqual(9);
});

it("fully received line bills the ordered quantity", () => {
  const l = line({ quantityReceived: 9, receivedComplete: true });
  expect(getBillableQuantity(l)).toEqual(9);
  expect(getRemainingQuantityToInvoice(l)).toEqual(9);
});

it("short-closed line bills only the received quantity", () => {
  // Ordered 9, received 2, then Stop Receiving.
  const l = line({ quantityReceived: 2, receivedComplete: true });
  expect(getBillableQuantity(l)).toEqual(2);
  expect(getRemainingQuantityToInvoice(l)).toEqual(2);
});

it("short-closed line already invoiced for receipts has nothing left", () => {
  const l = line({
    quantityReceived: 2,
    quantityInvoiced: 2,
    receivedComplete: true
  });
  expect(getRemainingQuantityToInvoice(l)).toEqual(0);
});

it("short-closed with nothing received has nothing to invoice", () => {
  const l = line({ receivedComplete: true });
  expect(getBillableQuantity(l)).toEqual(0);
  expect(getRemainingQuantityToInvoice(l)).toEqual(0);
});

it("partial invoice against an open line leaves the remainder", () => {
  const l = line({ quantityReceived: 2, quantityInvoiced: 4 });
  expect(getRemainingQuantityToInvoice(l)).toEqual(5);
});

it("over-invoiced short-closed line clamps to zero", () => {
  const l = line({
    quantityReceived: 2,
    quantityInvoiced: 5,
    receivedComplete: true
  });
  expect(getRemainingQuantityToInvoice(l)).toEqual(0);
});

it("over-received line is not short-closed", () => {
  // Received more than ordered (over-receipt) then flagged complete: the
  // received < ordered guard keeps the billable quantity at the order.
  const l = line({ quantityReceived: 11, receivedComplete: true });
  expect(getBillableQuantity(l)).toEqual(9);
});

it("null quantities behave as zero", () => {
  const l = line({
    purchaseQuantity: null,
    quantityReceived: null,
    quantityInvoiced: null,
    receivedComplete: true
  });
  expect(getBillableQuantity(l)).toEqual(0);
  expect(getRemainingQuantityToInvoice(l)).toEqual(0);
});

it("raising an invoiced line's quantity reopens invoicing", () => {
  // Ordered 10 and invoiced 10, then reopened and raised to 20.
  const l = line({ purchaseQuantity: 10, quantityInvoiced: 10 });
  expect(getCompletionAfterQuantityChange(l, 20)).toEqual({
    receivedComplete: false,
    invoicedComplete: false
  });
});

it("raising a received line's quantity reopens receiving", () => {
  const l = line({
    purchaseQuantity: 10,
    quantityReceived: 10,
    receivedComplete: true
  });
  expect(getCompletionAfterQuantityChange(l, 20)).toEqual({
    receivedComplete: false,
    invoicedComplete: false
  });
});

it("lowering a line to what was received and invoiced completes it", () => {
  const l = line({
    purchaseQuantity: 20,
    quantityReceived: 10,
    quantityInvoiced: 10
  });
  expect(getCompletionAfterQuantityChange(l, 10)).toEqual({
    receivedComplete: true,
    invoicedComplete: true
  });
});

it("a short-closed line stays closed when its quantity changes", () => {
  // Ordered 9, received 2, Stop Receiving, then raised to 12.
  const l = line({ quantityReceived: 2, receivedComplete: true });
  expect(getCompletionAfterQuantityChange(l, 12)).toEqual({
    receivedComplete: true,
    invoicedComplete: false
  });
  expect(
    getCompletionAfterQuantityChange({ ...l, quantityInvoiced: 2 }, 12)
  ).toEqual({ receivedComplete: true, invoicedComplete: true });
});

it("a short-close settled by matching the quantity reopens on a later raise", () => {
  // Ordered 9, received 2, Stop Receiving, set to 2, then raised to 12.
  const settled = line({
    purchaseQuantity: 2,
    quantityReceived: 2,
    receivedComplete: true
  });
  expect(getCompletionAfterQuantityChange(settled, 12)).toEqual({
    receivedComplete: false,
    invoicedComplete: false
  });
});