1 package SL::TransNumber;
5 use parent qw(Rose::Object);
8 use List::MoreUtils qw(any none);
10 use SL::PrefixedNumber;
13 use Rose::Object::MakeMethods::Generic
15 scalar => [ qw(type id number save dbh dbh_provided business_id) ],
18 my @SUPPORTED_TYPES = qw(invoice credit_note customer vendor sales_delivery_order purchase_delivery_order sales_order purchase_order sales_quotation request_quotation part service assembly assortment letter);
22 my $self = $class->SUPER::new(@_);
24 croak "Invalid type " . $self->type if none { $_ eq $self->type } @SUPPORTED_TYPES;
26 $self->dbh_provided($self->dbh);
27 $self->dbh(SL::DB->client->dbh) if !$self->dbh;
28 $self->save(1) unless defined $self->save;
29 $self->business_id(undef) if $self->type ne 'customer';
37 my $type = $self->type;
38 my %filters = ( where => '' );
40 if (any { $_ eq $type } qw(invoice credit_note)) {
41 $filters{trans_number} = "invnumber";
42 $filters{numberfield} = $type eq 'credit_note' ? "cnnumber" : "invnumber";
43 $filters{table} = "ar";
45 } elsif (any { $_ eq $type } qw(customer vendor)) {
46 $filters{trans_number} = "${type}number";
47 $filters{numberfield} = "${type}number";
48 $filters{table} = $type;
50 } elsif ($type =~ /_delivery_order$/) {
51 $filters{trans_number} = "donumber";
52 $filters{numberfield} = $type eq 'sales_delivery_order' ? "sdonumber" : "pdonumber";
53 $filters{table} = "delivery_orders";
54 $filters{where} = $type =~ /^sales/ ? '(customer_id IS NOT NULL)' : '(vendor_id IS NOT NULL)';
56 } elsif ($type =~ /_order$/) {
57 $filters{trans_number} = "ordnumber";
58 $filters{numberfield} = $type eq 'sales_order' ? "sonumber" : "ponumber";
59 $filters{table} = "oe";
60 $filters{where} = 'NOT COALESCE(quotation, FALSE)';
61 $filters{where} .= $type =~ /^sales/ ? ' AND (customer_id IS NOT NULL)' : ' AND (vendor_id IS NOT NULL)';
63 } elsif ($type =~ /_quotation$/) {
64 $filters{trans_number} = "quonumber";
65 $filters{numberfield} = $type eq 'sales_quotation' ? "sqnumber" : "rfqnumber";
66 $filters{table} = "oe";
67 $filters{where} = 'COALESCE(quotation, FALSE)';
68 $filters{where} .= $type =~ /^sales/ ? ' AND (customer_id IS NOT NULL)' : ' AND (vendor_id IS NOT NULL)';
70 } elsif ($type =~ /^(part|service|assembly|assortment)$/) {
71 $filters{trans_number} = "partnumber";
72 my %numberfield_hash = ( service => 'servicenumber',
73 assembly => 'assemblynumber',
74 assortment => 'assortmentnumber',
75 part => 'articlenumber'
77 $filters{numberfield} = $numberfield_hash{$type};
78 $filters{table} = "parts";
79 } elsif ($type =~ /letter/) {
80 $filters{trans_number} = "letternumber";
81 $filters{numberfield} = "letternumber";
82 $filters{table} = "letter";
91 return undef if !$self->number;
93 my %filters = $self->_get_filters();
96 my @values = ($self->number);
98 push @where, $filters{where} if $filters{where};
101 push @where, qq|id <> ?|;
102 push @values, conv_i($self->id);
105 my $where_str = @where ? ' AND ' . join(' AND ', map { "($_)" } @where) : '';
107 SELECT $filters{trans_number}
109 WHERE ($filters{trans_number} = ?)
113 my ($existing_number) = selectfirst_array_query($main::form, $self->dbh, $query, @values);
115 return $existing_number ? 0 : 1;
121 my $form = $main::form;
122 my %filters = $self->_get_filters();
125 SL::DB->client->with_transaction(sub {
126 my $where = $filters{where} ? ' WHERE ' . $filters{where} : '';
128 SELECT DISTINCT $filters{trans_number}, 1 AS in_use
133 do_query($form, $self->dbh, "LOCK TABLE " . $filters{table}) || die $self->dbh->errstr;
134 my %numbers_in_use = selectall_as_map($form, $self->dbh, $query, $filters{trans_number}, 'in_use');
137 ($business_number) = selectfirst_array_query($form, $self->dbh, qq|SELECT customernumberinit FROM business WHERE id = ? FOR UPDATE|, $self->business_id) if $self->business_id;
138 $number = $business_number;
139 ($number) = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults FOR UPDATE|) if !$number;
140 if ($filters{numberfield} eq 'assemblynumber' and length($number) < 1) {
141 $filters{numberfield} = 'articlenumber';
142 ($number) = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults FOR UPDATE|) if !$number;
145 my $sequence = SL::PrefixedNumber->new(number => $number);
148 $number = $sequence->get_next;
149 } while ($numbers_in_use{$number});
152 if ($self->business_id && $business_number) {
153 do_query($form, $self->dbh, qq|UPDATE business SET customernumberinit = ? WHERE id = ?|, $number, $self->business_id);
155 do_query($form, $self->dbh, qq|UPDATE defaults SET $filters{numberfield} = ?|, $number);
160 }) or do { die SL::DB->client->error };