Next transaction number retrieved from transaction table instead of sys_types
[fa-stable.git] / includes / systypes.inc
index 9f93fd541aeb4afbc38c05c0a0f782a6cb2ac452..4589e76bca9f622ab8e6ccd466c2efbe570cfbf5 100644 (file)
@@ -9,26 +9,29 @@
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
-Function get_next_trans_no ($trans_type){
 
-    // sql to get the next display transaction number
-       //these are maintained in the table sys_types
-    // Also updates the transaction number
+//-----------------------------------------------------------------------------------------
+//     Returns next transaction number.
+//     Used only for transactions stored in tables without autoincremented key.
+//
+function get_next_trans_no ($trans_type){
 
-    $sql = "SELECT type_no FROM ".TB_PREF."sys_types WHERE type_id = " . $trans_type;
+       $st = get_systype_db_info($trans_type);
 
-    $result = db_query($sql,"The next transaction number for $trans_type could not be retrieved");
-
-    $myrow = db_fetch_row($result);
-
-    $next_number = $myrow[0] + 1;
+       if (!($st && $st[0] && $st[2])) {
+               // this is in fact internal error condition.
+               display_error('Internal error: invalid type passed to get_next_trans_no()');
+               return 0;
+       }
+       $sql = "SELECT MAX(`$st[2]`) FROM $st[0]";
 
-    $sql = "UPDATE ".TB_PREF."sys_types SET type_no = $next_number WHERE type_id = $trans_type
-       AND type_no = ". $myrow[0]; //concurrency paranoic protection
+       if ($st[1] != null)
+                $sql .= " WHERE `$st[1]`=$trans_type";
 
-    db_query($sql,"The next transaction number for $trans_type could not be updated");
+    $result = db_query($sql,"The next transaction number for $trans_type could not be retrieved");
+    $myrow = db_fetch_row($result);
 
-    return $next_number;
+    return $myrow[0] + 1;
 }
 
 //-----------------------------------------------------------------------------