X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FHelper%2FTransNumberGenerator.pm;h=9684677c22cfcd3be8db456e654e536e32d37f62;hb=b4fc493375b77b6e27414fe3f7773f8b1add00fd;hp=d3417860626e61bb17be654d3a040036bdd48d6c;hpb=a37f2a8b590b7537e2938fa9545f6eae0ca7ae6e;p=kivitendo-erp.git
diff --git a/SL/DB/Helper/TransNumberGenerator.pm b/SL/DB/Helper/TransNumberGenerator.pm
index d34178606..9684677c2 100644
--- a/SL/DB/Helper/TransNumberGenerator.pm
+++ b/SL/DB/Helper/TransNumberGenerator.pm
@@ -8,23 +8,32 @@ our @EXPORT = qw(get_next_trans_number create_trans_number);
use Carp;
use List::Util qw(max);
-use SL::DB::Default;
+use SL::PrefixedNumber;
-my $oe_scoping = sub {
+sub oe_scoping {
SL::DB::Manager::Order->type_filter($_[0]);
-};
+}
-my $do_scoping = sub {
+sub do_scoping {
SL::DB::Manager::DeliveryOrder->type_filter($_[0]);
-};
-
-my %specs = ( ar => { number_column => 'invnumber', fill_holes_in_range => 1 },
- sales_quotation => { number_column => 'quonumber', number_range_column => 'sqnumber', scoping => $oe_scoping, },
- sales_order => { number_column => 'ordnumber', number_range_column => 'sonumber', scoping => $oe_scoping, },
- request_quotation => { number_column => 'quonumber', number_range_column => 'rfqnumber', scoping => $oe_scoping, },
- purchase_order => { number_column => 'ordnumber', number_range_column => 'ponumber', scoping => $oe_scoping, },
- sales_delivery_order => { number_column => 'donumber', number_range_column => 'sdonumber', scoping => $do_scoping, fill_holes_in_range => 1 },
- purchase_delivery_order => { number_column => 'donumber', number_range_column => 'pdonumber', scoping => $do_scoping, fill_holes_in_range => 1 },
+}
+
+sub parts_scoping {
+ SL::DB::Manager::Part->type_filter($_[0]);
+}
+
+my %specs = ( ar => { number_column => 'invnumber', },
+ sales_quotation => { number_column => 'quonumber', number_range_column => 'sqnumber', scoping => \&oe_scoping, },
+ sales_order => { number_column => 'ordnumber', number_range_column => 'sonumber', scoping => \&oe_scoping, },
+ request_quotation => { number_column => 'quonumber', number_range_column => 'rfqnumber', scoping => \&oe_scoping, },
+ purchase_order => { number_column => 'ordnumber', number_range_column => 'ponumber', scoping => \&oe_scoping, },
+ sales_delivery_order => { number_column => 'donumber', number_range_column => 'sdonumber', scoping => \&do_scoping, },
+ purchase_delivery_order => { number_column => 'donumber', number_range_column => 'pdonumber', scoping => \&do_scoping, },
+ customer => { number_column => 'customernumber', number_range_column => 'customernumber', },
+ vendor => { number_column => 'vendornumber', number_range_column => 'vendornumber', },
+ part => { number_column => 'partnumber', number_range_column => 'articlenumber', scoping => \&parts_scoping, },
+ service => { number_column => 'partnumber', number_range_column => 'servicenumber', scoping => \&parts_scoping, },
+ assembly => { number_column => 'partnumber', number_range_column => 'assemblynumber', scoping => \&parts_scoping, },
);
sub get_next_trans_number {
@@ -37,38 +46,49 @@ sub get_next_trans_number {
my $number = $self->$number_column;
my $number_range_column = $spec->{number_range_column} || $number_column;
my $scoping_conditions = $spec->{scoping};
- my $fill_holes_in_range = $spec->{fill_holes_in_range};
+ my $fill_holes_in_range = !$spec->{keep_holes_in_range};
return $number if $self->id && $number;
- my $re = '^(.*?)(\d+)$';
- my %conditions = $scoping_conditions ? ( query => [ $scoping_conditions->($spec_type) ] ) : ();
- my @numbers = map { $_->$number_column } @{ $self->_get_manager_class->get_all(%conditions) };
- my %numbers_in_use = map { ( $_ => 1 ) } @numbers;
- @numbers = grep { $_ } map { my @matches = m/$re/; @matches ? $matches[-1] * 1 : undef } @numbers;
-
- my $defaults = SL::DB::Default->get;
- my $number_range = $defaults->$number_range_column;
- my @matches = $number_range =~ m/$re/;
- my $prefix = (2 != scalar(@matches)) ? '' : $matches[ 0];
- my $ref_number = !@matches ? '1' : $matches[-1];
- my $min_places = length($ref_number);
-
- my $new_number = $fill_holes_in_range ? $ref_number : max($ref_number, @numbers);
- my $new_number_full = undef;
-
- while (1) {
- $new_number = $new_number + 1;
- my $new_number_s = $new_number;
- $new_number_s =~ s/\.\d+//g;
- $new_number_full = $prefix . ('0' x max($min_places - length($new_number_s), 0)) . $new_number_s;
- last if !$numbers_in_use{$new_number_full};
+ require SL::DB::Default;
+ 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 );
+
+ }
}
- $defaults->update_attributes($number_range_column => $new_number_full) if $params{update_defaults};
- $self->$number_column($new_number_full) if $params{update_record};
+ my %numbers_in_use = map { ( $_->$number_column => 1 ) } @{ $self->_get_manager_class->get_all(%conditions_for_in_use) };
+
+ 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 // 0);
+
+ if (!$fill_holes_in_range) {
+ my @numbers = map { $_->$number_column } @{ $self->_get_manager_class->get_all(%conditions) };
+ $sequence->set_to_max(@numbers) ;
+ }
- return $new_number_full;
+ my $new_number = $sequence->get_next;
+ $new_number = $sequence->get_next while $numbers_in_use{$new_number};
+
+ $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;
}
sub create_trans_number {
@@ -78,3 +98,91 @@ sub create_trans_number {
}
1;
+
+__END__
+
+=pod
+
+=encoding utf8
+
+=head1 NAME
+
+SL::DB::Helper::TransNumberGenerator - A mixin for creating unique record numbers
+
+=head1 FUNCTIONS
+
+=over 4
+
+=item C
+
+Generates a new unique record number for the mixing class. Each record
+type (invoices, sales quotations, purchase orders etc) has its own
+number range. Within these ranges all numbers should be unique. The
+table C contains the last record number assigned for all of
+the number ranges.
+
+This function contains hard-coded knowledge about the modules it can
+be mixed into. This way the models themselves don't have to contain
+boilerplate code for the details like the the number range column's
+name in the C table.
+
+The process of creating a unique number involves the following steps:
+
+At first all existing record numbers for the current type are
+retrieved from the database as well as the last number assigned from
+the table C.
+
+The next step is separating the number range from C into two
+parts: an optional non-numeric prefix and its numeric suffix. The
+prefix, if present, will be kept intact.
+
+Now the number itself is increased as often as neccessary to create a
+unique one by comparing the generated numbers with the existing ones
+retrieved in the first step. In this step gaps in the assigned numbers
+are filled for all currently supported tables.
+
+After creating the unique record number this function can update
+C<$self> and the C table if requested. This is controlled
+with the following parameters:
+
+=over 2
+
+=item * C
+
+Determines whether or not C<$self>'s record number field is set to the
+newly generated number. C<$self> will not be saved even if this
+parameter is trueish. Defaults to false.
+
+=item * C
+
+Determines whether or not the number range value in the C
+table should be updated. Unlike C<$self> the C table will be
+saved. Defaults to false.
+
+=back
+
+Always returns the newly generated number. This function cannot fail
+and return a value. If it fails then it is due to exceptions.
+
+=item C
+
+Calls and returns L with the parameters
+C and C. C<%params> is passed
+to it as well.
+
+=back
+
+=head1 EXPORTS
+
+This mixin exports all of its functions: L and
+L. There are no optional exports.
+
+=head1 BUGS
+
+Nothing here yet.
+
+=head1 AUTHOR
+
+Moritz Bunkus Em.bunkus@linet-services.deE
+
+=cut