Creascent invoice number

Hello, im trying to make a creascent invoice number.

I want 7 digits invoice i.e: 0000001

When add new one, add +1 to last one: 0000002

And if: 0000099 when add new one go to: 0000100.

How i can do that? If i try save number: 00000001, it only saves 1.

Thanks.

Invoice number = this invoice number +1

:slight_smile:

Did you read this part? :open_mouth:

If i try save number: 00000001, it only saves 1.

Im not asking how add +1 to last value, im asking how i can save 7 digits value using 0.

1 Like

Just keep in mind if you do incremental counts that if you permanently delete a record, you may inadvertently create duplicate invoice records.

For this purpose, it’s good to have some ongoing ledger. Or to have a mechanism to mark invoices as user deleted, but not actually deleted from the database.

Also, you want to format it as a text (numbers only). If you save it as a number, leading zeros will be omitted.

1 Like

Hi therealone,

I do these kinds of problems as follows:

  1. You need not one, but two fields, the first of type number, the second of type text.

  2. You do the calculations and incrementations on the number field only.

  3. Once you have the right n-digit number for your invoice, you transform it into a n-letter text element.

  4. You take the n-letter text element and superpose it on the last n zeros of the generic text element “0000000”, for instance “xyz” and “0000000” gives “0000xyz”

  5. You store the text “0000xyz” in the text field.

Example: extract the invoice number 67 from the number field => transform into text element “67” => superpose the two texts “0000000” and “67” right-aligned to give “0000067” => store the latter in the text field.

Note that numbers and texts are different formats of variables, you cannot mix them, but have to transform one into the other before doing operations on the pair. The text “0000000” transformed into a number gives 0, but the number 0 transformed back into a text gives “0”.

Hope this helps, good luck,

O.

1 Like

Oh sorry I quick reply, didn’t really pay a lot of attention. :sweat_smile:

Very good added points. Second to what you said :smiley:

But if you add a value to last record, doesn’t matters if he deletes any record.
If he deletes the last one, we will replace it with new one.

And if he deletes an old one, he will keep adding new records using last so that shouldnt be a problem.

Yes, what you’re saying is correct. So long as you are referencing a discrete field on the last invoice, you shouldn’t encounter unintended duplicates. @ones comment clarified the point.

My initial comment was based on the logic of a :count, function.

Let me jump in here: how do you superpose? I have never noitced a “superpose” option anywhere.

I had the same problem. I just store the invoice number in the database as a number which makes it easy to increment and use in searches. I always keep the last used invoice number in a database as well. Then when I need to display the number, like in a text field, I do zero padding as follows:

First, take the invoice number and add a number that’s one larger than the maximum possible invoice number. Example: Add 10000000 to invoice number to represent an invoice number from 1 to 9999999.
image

Then format the invoice number as a number with 0 decimal places. This turns the number into a string.

Finally, extract the number with the leading zeros using Regex.

Hope this helps.

BTW: The Regex pattern .{7}$ will extract the 7 least significate digits. Just change the number to match the number of digits to extract from the number string.

8 Likes

This topic was automatically closed after 70 days. New replies are no longer allowed.