Hi,
I have a use case where a vehicle seller creates a database by adding vehicles to a master vehicle table as they are purchased into stock.
Not all the vehicles are necessarily sold. And when they are sold, they are not necessarily sold in the same chronological order as they were purchased.
When a vehicle is sold the vehicle should have an invoice number generated for it and recorded in the master vehicle table.
For argument sake, let's call this custom field "invoice_number".
So, programmatically I can think of two ways to achieve this with:
add_action( 'cred_save_data', 'custom_save_func', 10, 2 );
function custom_save_func( $post_id, $form_data ) {
//some code
};
I think this thread comes close to what I am trying to do.
https://toolset.com/forums/topic/automatically-add-sequential-value-in-a-field-when-form-is-saved/
A. Put ALL vehicle records into an array, filter out only those that have an existing invoice number into a new array, sort the array numerically by invoice number, get the the last invoice number, increment +1, save the newly generated invoice number into the "invoice_number" custom field for that vehicle record.
B. In a separate "Seller Data" table, have one record call "last_invoice_number".
When the vehicle is sold, look up the value of this record, increment +1, save the newly generated invoice number into the "invoice_number" custom field for that vehicle record, AND save the new value into the "last_invoice_number" of the Seller Data table (effectively advancing the saved field value by one).
Of the two options, B. seems more programmatically efficient with database resources since it doesn't need to cycle through the entire master vehicle records list.
BUT option A. seems less prone to error e.g. if the both the "invoice_number" and "last_invoice_number" aren't updated together for some reason (server glitch?) there is obviously potential for some invoice numbers to get duplicated.
Any advice on the best way to go about this?