I was trying to use the solution implemented in nuBuilder for auto-increment fields with #TT# variable; I several times read the wiki and seen video tutorial, but maybe I'm missing something.
What happens if the invoices form is launched and closed without generating an invoice record (eg if user remember having to do something else before)?
I observed that the #TT# variable is instantiated (and a new number generated and saved in the database) every time you open the edit screen, also if you are modifying an existing record.
This is also visible in the video tutorial: a record is created with invoice n°31 and then the number 32, without having first saved n° 31.
I don't think this is the right method.
Have I forgotten something?
Also, what if 1000 invoices are generated per month? The next_numbers table increase exponentially!
And if I want every January 1 restart the numbering?
To overcome this I decided to implement a different solution:
1) - in phpmyadmin I created a table "numerations" with this structure:
- Num_id: key / table name (repairs)
- Num_rep_number (field of the main table where to store the new number)
2) - in CustomCode tab - BeforeSave of the form I have added this code:
Code: Select all
/*
* Get a new number from numerations table and assign to repair table
*/
// Check if it's a new record
if('#recordID#' == -1) {
// Get last assigned number from numerations table
$result = nuRunQuery("SELECT num_repair FROM numerations WHERE num_id = 'repair' ");
$row = db_fetch_array($result);
$num = ($row[0] ? $row[0] : 0);
$num++;
// Update numerations table
nuRunQuery("UPDATE numerations SET num_repair = " . $num . " WHERE num_id = 'repair' ");
// Update form field
$_POST['rep_number'] = $num;
}
What's your opinion about this solution?
Thank you.