Skip to content

PaymentSuggestion

Invoice combinations summing exactly to a lump sum.
Implemented
Status
Implemented
Family
10 of 13
Registry
@lopifi/payment-suggestion
npx shadcn@latest add @lopifi/theme @lopifi/payment-suggestion

The component, rendering

Real data, the shipped component. Switch the theme to check the claims the spec makes below.

Preview

The four-layer spec

Given a lump sum, proposes the set of open invoices that sums to it exactly. Multiple valid combinations are ranked oldest-first. Shows the basis, not a bare confidence score.

Lifted verbatim from Receivables (AR). The doc and this page are the same paragraphs, so they cannot disagree.

What lands in your repository

One component is never one file. Everything below is copied into your project, where you own it.

Files

components/ledger/payment-suggestion.tsx

Lopifi dependencies · 7
buttoncnformat-moneyiconsmoneymoney.typestooltip
npm packages
@base-ui-components/reactclass-variance-authorityclsxtailwind-merge
Registry entry
/r/payment-suggestion.json

The whole component

No compiled bundle, no black box. This is the file the registry copies, and the file you will edit.

components/ledger/payment-suggestion.tsx
"use client";

import * as React from "react";
import { cn } from "@/lib/ledger/cn";
import type { MoneyValue } from "@/lib/ledger/money.types";
import { Button } from "@/components/ledger/button";
import { SparklesIcon } from "@/components/ledger/icons";
import { Money } from "@/components/ledger/money";

export type InvoiceCandidate = { id: string; number: string; balance: number; age: number };

export interface PaymentSuggestionProps {
  candidates: InvoiceCandidate[];
  className?: string;
  lumpSum: MoneyValue;
  onAccept?: (ids: string[]) => void;
}

/**
 * Given a lump sum, propose the sets of open invoices that sum to it
 * exactly. Multiple valid combinations are ranked oldest-first, and the
 * basis is shown — not a bare confidence score.
 */
export function PaymentSuggestion({ lumpSum, candidates, onAccept, className }: PaymentSuggestionProps) {
  const target = Number(lumpSum.amount);
  const combos = React.useMemo(() => exactSubsets(candidates, target).slice(0, 3), [candidates, target]);

  if (combos.length === 0) {
    return (
      <p className={cn("text-body text-text-secondary", className)}>
        No combination of open invoices sums to <Money size="sm" tone="inherit" value={lumpSum} /> exactly.
      </p>
    );
  }

  return (
    <div className={cn("space-y-2", className)}>
      <h3 className="flex items-center gap-2 text-label font-medium text-text-secondary">
        <SparklesIcon aria-hidden="true" className="size-3.5 text-accent" />
        Combinations summing to <Money size="xs" tone="inherit" value={lumpSum} /> exactly
      </h3>
      <ul className="space-y-2">
        {combos.map((combo, i) => (
          <li
            className="flex flex-wrap items-center gap-3 rounded-md border border-border bg-surface p-3"
            key={i}
          >
            <span className="flex-1">
              <span className="reference-numeric block text-body">
                {combo.map((c) => c.number).join(" + ")}
              </span>
              <span className="block text-caption text-text-secondary">
                {combo.length} invoices · oldest {Math.max(...combo.map((c) => c.age))} days
              </span>
            </span>
            <Button onClick={() => onAccept?.(combo.map((c) => c.id))} size="sm" variant="primary">
              Apply this set
            </Button>
          </li>
        ))}
      </ul>
    </div>
  );
}

/** Small-n exact subset search, ranked oldest-first. */
function exactSubsets(candidates: InvoiceCandidate[], target: number): InvoiceCandidate[][] {
  const results: InvoiceCandidate[][] = [];
  const sorted = [...candidates].sort((a, b) => b.age - a.age);
  const limit = Math.min(sorted.length, 16);

  const walk = (index: number, running: number, picked: InvoiceCandidate[]) => {
    if (results.length >= 8) return;
    if (running === target && picked.length > 0) {
      results.push([...picked]);
      return;
    }
    if (running > target || index >= limit) return;
    picked.push(sorted[index]);
    walk(index + 1, running + sorted[index].balance, picked);
    picked.pop();
    walk(index + 1, running, picked);
  };

  walk(0, 0, []);
  return results;
}

The accounting it encodes

Concepts this component renders. Each one is defined once, in the lexicon, and used identically in the code and the copy.

Also in Receivables (AR)

See also

A component is specified in one place, defined in another, and running in a third. All three, from here.

Elsewhere in the system

Where PaymentSuggestion sits in the rest of the system — the live section it renders in, the calculators that compute what it displays, and the ledger payloads it is fed from.

Components are copied into your repository and meant to be edited — rounding policy, fiscal calendar and compliance copy differ per company.

Told when Receivables (AR) changes

Components are copied into your repo, so a fix here does not reach you on its own. One email when a component in this family changes in a way that needs a migration — and nothing when it does not.

One address, no list rental, unsubscribe in one click.

Works in your QuickBooks Online or Xero file

Get your books closed every month

Lopifi is building AI bookkeeping that keeps your books closed on US GAAP as transactions happen, with a CPA review every quarter, for $200 a month. We’re not open yet.

$200/month when we open. No card now.