Apply to customer entry (closing "by date")???

When I set on the customer card that their entries be closed “by date”, and when I post the customer payment through the journal, it applies to and closes the oldest ENTRY (entry no.) in the table customer ledger entry regardless of the date it was created!!! How do I make it apply to so it closes the oldest entry starting with the oldest DATE??? The sorting in the table customer ledger entry is done by the primary key Entry No. I am afraid I will mess up a lot of things if I set sorting by Date (making it a key in the table). Does anyone have any explanation or advice?! Please help!

This is in fact a known issue and also applies to item entry application (which also uses the lowest entry no. found and applies to that regardless of the posting date). If you are changing code here you need to be very carefull.

THANX! I realized that changing a primary key would mess up table relations, so that is not doable, unfortunately! Does anyone have any idea on how to solve this?