I'm trying to generate invoice numbers that auto-increment: 2019-0001, 2019-0002, and so on.
The code works fine, but the problem arises with race conditions. If payments happen simultaneously I'm sometimes left with duplicate invoice numbers.
I've thought of just creating an auto_increment field in the database but we have a requirement that allows the user to set the starting number of the invoice. For example a user can set their invoices to start with 2019-0002 and then increment from there.
Is there a simple way to avoid duplicates?
Bonus points if the answer is compatible with WordPress as well, since the invoice numbers are custom post type titles, and I can't change the table design of posts.