return $number if $self->id && $number;
- my %conditions = $scoping_conditions ? ( query => [ $scoping_conditions->($spec_type) ] ) : ();
+ require SL::DB::Default;
+ require SL::DB::Business;
+
+ my %conditions = ( 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 );
+
+ }
+ }
+
my @numbers = map { $_->$number_column } @{ $self->_get_manager_class->get_all(%conditions) };
my %numbers_in_use = map { ( $_ => 1 ) } @numbers;
- require SL::DB::Default;
- my $defaults = SL::DB::Default->get;
- $number_range_column = 'articlenumber' if $number_range_column eq 'assemblynumber' and length($defaults->$number_range_column) < 1;
- my $sequence = SL::PrefixedNumber->new(number => ($defaults->$number_range_column || 1));
+ my $range_table = $business ? $business : SL::DB::Default->get;
+ my $start_number = $range_table->$number_range_column;
+ $start_number = $range_table->articlenumber if ($number_range_column eq 'assemblynumber') && (length($start_number) < 1);
+ my $sequence = SL::PrefixedNumber->new(number => $start_number);
$sequence->set_to_max(@numbers) if !$fill_holes_in_range;
my $new_number = $sequence->get_next;
$new_number = $sequence->get_next while $numbers_in_use{$new_number};
- $defaults->update_attributes($number_range_column => $new_number) if $params{update_defaults};
- $self->$number_column($new_number) if $params{update_record};
+ $range_table->update_attributes($number_range_column => $new_number) if $params{update_defaults};
+ $self->$number_column($new_number) if $params{update_record};
return $new_number;
}