5063

Apply the Payment

Algorithm
Medium
Pro
sorting
payment-discount
application

Apply the Payment

A customer wires one lump sum that is supposed to cover "the old invoices, minus the early-payment discount we agreed on". In Business Central that reconciliation is the Apply Entries engine; your job is that engine distilled to its core algorithm: walk the open invoices oldest-first, honor payment discounts while their window is open, split the money across partial applications, and account for every cent — including the ones you hand back.

Requirements

The starter contains a table named "Open Invoice" — the in-memory ledger your code works on. Keep its fields and keys exactly as shipped; the grading tests seed it and read it back:

  • "Invoice No." — Code[20], the primary key
  • "Posting Date" — Date
  • "Remaining Amount" — Decimal, what is still owed; 0 means the invoice is closed
  • "Pmt. Discount Date" — Date, the last day of the regular discount period
  • "Pmt. Discount Amount" — Decimal, the discount granted when the invoice settles inside its window

Create a codeunit named "Cash Application" with one public procedure:

procedure ApplyPayment(var OpenInvoice: Record "Open Invoice"; PaymentAmount: Decimal; PaymentDate: Date; GraceDays: Integer): Decimal

Rules:

  1. Process the invoices oldest first: ascending "Posting Date", ties broken by ascending "Invoice No.". The physical order of rows in the buffer must never influence the result.
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
  • 13 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