5031

Close the Invoice, Properly

Transactions
Medium
Pro
receivables
application
cust-ledger-entry
flowfield
posting

Close the Invoice, Properly

A customer wires one amount, three of their invoices are open, and someone has to decide where the money lands. In Business Central that decision is not arithmetic you write into the ledger — it is a posting, made by the same engine the Apply Entries action drives, and it leaves detailed ledger entries behind that can later be unapplied. Automating it is the job every accounts-receivable consultant is eventually handed.

Requirements

Create a codeunit named "Payment Applicator" with two public procedures:

procedure ApplyOldestFirst(PaymentEntryNo: Integer): Decimal
procedure RemainingAmount(EntryNo: Integer): Decimal

Pick object IDs in the house range 50100–50199, and reference every other object by name — never by a literal object ID.

ApplyOldestFirst

  1. PaymentEntryNo is the "Entry No." of a "Cust. Ledger Entry". That entry must exist, its "Document Type" must be Payment, and it must still be Open. Otherwise fail with an error whose text contains not an open payment, having changed nothing in the ledger.
  2. The invoices in scope are the "Cust. Ledger Entry" records of the payment's own "Customer No." whose "Document Type" is Invoice and whose Open flag is set. Closed entries, entries of any other document type, and other customers' entries are out of scope — the routine must never touch them.
  3. Order: ascending "Posting Date"; where two invoices share a posting date, ascending "Entry No.". Due dates play no part in the order.
  4. Allocation: walk that order and give each invoice as much of the payment as it still needs, until the payment runs out. The invoice the payment runs out on is applied partially: it stays Open with exactly the shortfall outstanding. Invoices after it are not touched at all.
The rest of the statement and 3 hints Unlock with Pro
ALBusiness Central 28.5
Pro
Pro problem

Solve it with Pro

You’re reading the opening of the statement. Pro unlocks the whole problem:

  • The full statement
  • Starter code in the browser editor
  • 15 grading tests, run on Business Central 28.5
  • 3 hints
  • The reference solution

Pro also opens all 92 Pro problems and lifts the daily limits on compile checks and submissions.

$10 / month
or $100 / year
Upgrade
This problem is part of Pro
Pro unlocks the full statement, tests, hints and solution. The code editor works on a computer.
Upgrade