Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision b9dbc9e3

Von Moritz Bunkus vor fast 14 Jahren hinzugefügt

  • ID b9dbc9e3d4f8821d9fd324b52dafb81b699a776e
  • Vorgänger 73df5fb5
  • Nachfolger dd04aff2

Helfer-Modul zum Erzeugen von eindeutigen Belegnummern

Conflicts:

SL/DB/DeliveryOrder.pm

Unterschiede anzeigen:

SL/DB/Default.pm
8 8
__PACKAGE__->meta->make_manager_class;
9 9

  
10 10
sub get_default_currency {
11
  my $self = _selfify(@_);
11
  my $self = shift->get;
12 12
  my @currencies = grep { $_ } split(/:/, $self->curr || '');
13 13
  return $currencies[0] || '';
14 14
}
15 15

  
16
sub _selfify {
16
sub get {
17 17
  my ($class_or_self) = @_;
18 18
  return $class_or_self if ref($class_or_self);
19 19
  return SL::DB::Manager::Default->get_all(limit => 1)->[0];
SL/DB/DeliveryOrder.pm
4 4

  
5 5
use SL::DB::MetaSetup::DeliveryOrder;
6 6
use SL::DB::Manager::DeliveryOrder;
7
use SL::DB::Helper::LinkedRecords;
8
use SL::DB::Helper::TransNumberGenerator;
7 9
use SL::DB::Order;
8 10

  
9 11
use List::Util qw(first);
SL/DB/Helper/TransNumberGenerator.pm
1
package SL::DB::Helper::TransNumberGenerator;
2

  
3
use strict;
4

  
5
use parent qw(Exporter);
6
our @EXPORT = qw(get_next_trans_number);
7

  
8
use Carp;
9
use List::Util qw(max);
10

  
11
use SL::DB::Default;
12

  
13
my $oe_scoping = sub {
14
  SL::DB::Manager::Order->type_filter($_[0]);
15
};
16

  
17
my $do_scoping = sub {
18
  SL::DB::Manager::DeliveryOrder->type_filter($_[0]);
19
};
20

  
21
my %specs = ( ar                      => { number_column => 'invnumber',                                                             fill_holes_in_range => 1 },
22
              sales_quotation         => { number_column => 'quonumber', number_range_column => 'sqnumber',  scoping => $oe_scoping,                          },
23
              sales_order             => { number_column => 'ordnumber', number_range_column => 'sonumber',  scoping => $oe_scoping,                          },
24
              request_quotation       => { number_column => 'quonumber', number_range_column => 'rfqnumber', scoping => $oe_scoping,                          },
25
              purchase_order          => { number_column => 'ordnumber', number_range_column => 'ponumber',  scoping => $oe_scoping,                          },
26
              sales_delivery_order    => { number_column => 'donumber',  number_range_column => 'sdonumber', scoping => $do_scoping, fill_holes_in_range => 1 },
27
              purchase_delivery_order => { number_column => 'donumber',  number_range_column => 'pdonumber', scoping => $do_scoping, fill_holes_in_range => 1 },
28
            );
29

  
30
sub get_next_trans_number {
31
  my ($self, %params) = @_;
32

  
33
  my $spec_type           = $specs{ $self->meta->table } ? $self->meta->table : $self->type;
34
  my $spec                = $specs{ $spec_type } || croak("Unsupported class " . ref($self));
35

  
36
  my $number_column       = $spec->{number_column};
37
  my $number              = $self->$number_column;
38
  my $number_range_column = $spec->{number_range_column} || $number_column;
39
  my $scoping_conditions  = $spec->{scoping};
40
  my $fill_holes_in_range = $spec->{fill_holes_in_range};
41

  
42
  return $number if $self->id && $number;
43

  
44
  my $re              = '^(.*?)(\d+)$';
45
  my %conditions      = $scoping_conditions ? ( query => [ $scoping_conditions->($spec_type) ] ) : ();
46
  my @numbers         = map { $_->$number_column } @{ $self->_get_manager_class->get_all(%conditions) };
47
  my %numbers_in_use  = map { ( $_ => 1 )        } @numbers;
48
  @numbers            = grep { $_ } map { my @matches = m/$re/; @matches ? $matches[-1] * 1 : undef } @numbers;
49

  
50
  my $defaults        = SL::DB::Default->get;
51
  my $number_range    = $defaults->$number_range_column;
52
  my @matches         = $number_range =~ m/$re/;
53
  my $prefix          = (2 != scalar(@matches)) ? ''  : $matches[ 0];
54
  my $ref_number      = !@matches               ? '1' : $matches[-1];
55
  my $min_places      = length($ref_number);
56

  
57
  my $new_number      = $fill_holes_in_range ? $ref_number : max($ref_number, @numbers);
58
  my $new_number_full = undef;
59

  
60
  while (1) {
61
    $new_number      =  $new_number + 1;
62
    my $new_number_s =  $new_number;
63
    $new_number_s    =~ s/\.\d+//g;
64
    $new_number_full =  $prefix . ('0' x max($min_places - length($new_number_s), 0)) . $new_number_s;
65
    last if !$numbers_in_use{$new_number_full};
66
  }
67

  
68
  $defaults->update_attributes($number_range_column => $new_number_full) if $params{update_defaults};
69
  $self->$number_column($new_number_full)                                if $params{update_record};
70

  
71
  return $new_number_full;
72
}
73

  
74
sub create_trans_number {
75
  my ($self, %params) = @_;
76

  
77
  return $self->get_next_trans_number(update_defaults => 1, update_record => 1, %params);
78
}
79

  
80
1;
SL/DB/Invoice.pm
12 12
use SL::DB::Manager::Invoice;
13 13
use SL::DB::Helper::LinkedRecords;
14 14
use SL::DB::Helper::PriceTaxCalculator;
15
use SL::DB::Helper::TransNumberGenerator;
15 16
use SL::DB::Employee;
16 17

  
17 18
__PACKAGE__->meta->add_relationship(
SL/DB/Order.pm
10 10
use SL::DB::Invoice;
11 11
use SL::DB::Helper::LinkedRecords;
12 12
use SL::DB::Helper::PriceTaxCalculator;
13
use SL::DB::Helper::TransNumberGenerator;
13 14

  
14 15
__PACKAGE__->meta->add_relationship(
15 16
  orderitems => {

Auch abrufbar als: Unified diff