0067

Customer Balance in a Date Window

flowfield
flowfilter
calcfields
customer

Customer Balance in a Date Window

Finance is building customer statements: for any statement period they need to know how much a customer's balance moved during the period, and what the running balance was at the end of any given day. Both numbers already live in the customer's ledger — the exercise is scoping them to a date window.

Requirements

Create a codeunit named "Customer Balance Window" with two public procedures:

procedure BalanceAsOf(CustomerNo: Code[20]; AsOfDate: Date): Decimal
procedure BalanceChangeBetween(CustomerNo: Code[20]; FromDate: Date; ToDate: Date): Decimal

Rules:

  1. BalanceChangeBetween returns the net amount, in local currency (LCY), posted to the customer's ledger with a posting date from FromDate through ToDate — both boundary days included.
  2. BalanceAsOf returns the customer's balance in LCY as of the end of AsOfDate: every entry posted on or before that day counts; every later entry does not.
  3. Both results are signed sums — payment and credit entries carry negative amounts and reduce the result.
  4. Only entries of the customer identified by CustomerNo count; CustomerNo always refers to an existing customer.
  5. A window (or an as-of date) that no entry falls into returns 0 — that is a normal answer, not an error.

What the tests check

The grading tests create fresh customers and post general-journal entries to them on several posting dates, with amounts generated at run time — the answers cannot be hardcoded. They assert exact amounts for: a window covering only the middle of three entries; a window whose FromDate and ToDate land exactly on posting dates (both boundary days must count, the days just outside must not); a window covering every entry; a window containing no entries (expect 0); a window mixing a positive and a negative entry (expect the signed net); a second customer posting inside the same window (their entries must not leak into the answer); an as-of date landing exactly on a posting date (that day counts, later entries do not — and a negative entry posted over a year earlier also counts, so the as-of answer must be a signed sum with no lower date bound); and an as-of date before the customer's first entry (expect 0). All seeded entries are in local currency.

Learn More

Hint 1
The Customer table already knows how to add up its own ledger — look at its FlowFields, and notice that some of their formulas are wired to a FlowFilter field called "Date Filter".
Hint 2
A FlowFilter doesn't filter which customer rows you see: SetRange("Date Filter", FromDate, ToDate) feeds the date-linked FlowFields the next time you CalcFields them. An as-of balance is just a window that starts at 0D, the empty date.
Hint 3
Pick the FlowField carefully: "Balance (LCY)" has no posting-date link in its formula, so it ignores the Date Filter and always returns the all-time balance — exactly the starter's bug. "Net Change (LCY)" is the one whose formula ties Posting Date to the Date Filter.
ALBusiness Central 28.5
Press Compile to check your code compiles — Submit runs the tests.
The code editor is desktop-only
Open this problem on a computer to write and run code. Reading the description, tests and discussion works fine here.