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): DecimalPick object IDs in the house range 50100–50199, and reference every other object by name — never by a literal object ID.
ApplyOldestFirst
PaymentEntryNois the"Entry No."of a"Cust. Ledger Entry". That entry must exist, its"Document Type"must bePayment, and it must still beOpen. Otherwise fail with an error whose text containsnot an open payment, having changed nothing in the ledger.- The invoices in scope are the
"Cust. Ledger Entry"records of the payment's own"Customer No."whose"Document Type"isInvoiceand whoseOpenflag is set. Closed entries, entries of any other document type, and other customers' entries are out of scope — the routine must never touch them. - Order: ascending
"Posting Date"; where two invoices share a posting date, ascending"Entry No.". Due dates play no part in the order. - 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
Openwith exactly the shortfall outstanding. Invoices after it are not touched at all.
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.
Already on Pro? Sign in