Credits from the purchase closest to expiry are always used first.
Credits are stored as a list of movements, not as a single number that gets overwritten. The balance above is the sum of the rows below it, which is why a question about a missing credit can be answered in seconds.