+ require SL::DB::Business;
+
+ my %conditions = ( query => [ $scoping_conditions ? $scoping_conditions->($spec_type) : () ] );
+ my %conditions_for_in_use = ( query => [ $scoping_conditions ? $scoping_conditions->($spec_type) : () ] );
+
+ my $business;
+ if ($spec_type =~ m{^(?:customer|vendor)$}) {
+ $business = $self->business_id ? SL::DB::Business->new(id => $self->business_id)->load : $self->business;
+ if ($business && (($business->customernumberinit // '') ne '')) {
+ $number_range_column = 'customernumberinit';
+ push @{ $conditions{query} }, ( business_id => $business->id );
+
+ } else {
+ undef $business;
+ push @{ $conditions{query} }, ( business_id => undef );
+
+ }
+ }
+
+ # Lock both the table where the new number is stored and the range
+ # table. The storage table has to be locked first in order to
+ # prevent deadlocks as the legacy code in SL/TransNumber.pm locks it
+ # first, too.
+
+ # For the storage table we have to use a full lock in order to
+ # prevent insertion of new entries while this routine is still
+ # working. For the range table we only need a row-level lock,
+ # therefore we're re-loading the row.
+ $self->db->dbh->do("LOCK " . $self->meta->table) || die $self->db->dbh->errstr;
+
+ my ($query_in_use, $bind_vals_in_use) = Rose::DB::Object::QueryBuilder::build_select(
+ dbh => $self->db->dbh,
+ select => $number_column,
+ tables => [ $self->meta->table ],
+ columns => { $self->meta->table => [ $self->meta->column_names ] },
+ query_is_sql => 1,
+ %conditions_for_in_use,
+ );
+
+ my @numbers = do { no warnings 'once'; SL::DBUtils::selectall_array_query($::form, $self->db->dbh, $query_in_use, @{ $bind_vals_in_use || [] }) };
+ my %numbers_in_use = map { ( $_ => 1 ) } @numbers;
+
+ my $range_table = ($business ? $business : SL::DB::Default->get)->load(for_update => 1);
+
+ my $start_number = $range_table->$number_range_column;
+ $start_number = $range_table->articlenumber if ($number_range_column =~ /^(assemblynumber|assortmentnumber)$/) && (length($start_number) < 1);
+ my $sequence = SL::PrefixedNumber->new(number => $start_number // 0);
+
+ if (!$fill_holes_in_range) {
+ $sequence->set_to_max(@numbers) ;
+ }