X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;ds=inline;f=SL%2FDB%2FHelper%2FTransNumberGenerator.pm;h=a28f11f71687e7eb9ca152d04939eeb40849ceb8;hb=128b0775bae87e3507fde183e8fb307e2a598b60;hp=2ac06edd033327442272c649c3a8830a2019b7f7;hpb=b9dbc9e3d4f8821d9fd324b52dafb81b699a776e;p=kivitendo-erp.git
diff --git a/SL/DB/Helper/TransNumberGenerator.pm b/SL/DB/Helper/TransNumberGenerator.pm
index 2ac06edd0..a28f11f71 100644
--- a/SL/DB/Helper/TransNumberGenerator.pm
+++ b/SL/DB/Helper/TransNumberGenerator.pm
@@ -3,28 +3,39 @@ package SL::DB::Helper::TransNumberGenerator;
use strict;
use parent qw(Exporter);
-our @EXPORT = qw(get_next_trans_number);
+our @EXPORT = qw(get_next_trans_number create_trans_number);
use Carp;
use List::Util qw(max);
-use SL::DB::Default;
+use SL::DBUtils ();
+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, },
+ assortment => { number_column => 'partnumber', number_range_column => 'assortmentnumber', scoping => \&parts_scoping, },
);
sub get_next_trans_number {
@@ -37,38 +48,70 @@ 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 );
+
+ }
+ }
+
+ # 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) ;
}
- $defaults->update_attributes($number_range_column => $new_number_full) if $params{update_defaults};
- $self->$number_column($new_number_full) if $params{update_record};
+ my $new_number = $sequence->get_next;
+ $new_number = $sequence->get_next while $numbers_in_use{$new_number};
- return $new_number_full;
+ $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 +121,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