Here it is, it works on the identical reflines for Sales Invoices and Credit Notes.
Maybe it's too simple, but that's what I needed and I'm happy with it.
CAUTION - I haven't made data checks as I use alone this system.
For the moment it is not impossible to manually create Sales Invoice and Credit Note with same numbers.
I mean if you change the displayed reference number...
Also note, here it is with mb_strlen
This is the changed part in _parse_next function:
$prefix = $match[1];
$postfix = $match[3];
$db_info = get_systype_db_info($type);
$trans_table = $db_info[0];
$type_fld = $db_info[1];
$tno_fld = $db_info[2];
$ref_fld = $db_info[3];
$type_sec = ''; // change starts here
if($type== ST_CUSTCREDIT || $type== ST_SALESINVOICE)
{
$type_sec = '10,11'; // don't laugh, it works fine
}
else
{
$type_sec= $type;
}
// retrieve last ref number in the refline from original transaction table
$sql = "SELECT MAX(CAST(SUBSTR($ref_fld, ".(mb_strlen($prefix)+1).",LENGTH($ref_fld)-".(mb_strlen($postfix)+mb_strlen($prefix)).") AS UNSIGNED))"
." FROM `$trans_table` tbl
LEFT JOIN ".TB_PREF."voided v ON tbl.`$tno_fld`=v.id AND v.type in ($type_sec)"
." WHERE ISNULL(v.id)"
.($type_fld ? " AND tbl.`$type_fld`in ($type_sec)" : '')
." AND `$ref_fld` REGEXP ".db_escape('^'.preg_quote($prefix).'[0-9]*'.preg_quote($postfix).'$');
$result = db_query($sql, 'cannot retrieve last reference');
$result = db_fetch_row($result);