1 package SL::TransNumber;
5 use parent qw(Rose::Object);
8 use List::MoreUtils qw(any none);
10 use SL::PrefixedNumber;
12 use Rose::Object::MakeMethods::Generic
14 scalar => [ qw(type id number save dbh dbh_provided business_id) ],
17 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 letter);
21 my $self = $class->SUPER::new(@_);
23 croak "Invalid type " . $self->type if none { $_ eq $self->type } @SUPPORTED_TYPES;
25 $self->dbh_provided($self->dbh);
26 $self->dbh($::form->get_standard_dbh) if !$self->dbh;
27 $self->save(1) unless defined $self->save;
28 $self->business_id(undef) if $self->type ne 'customer';
36 my $type = $self->type;
37 my %filters = ( where => '' );
39 if (any { $_ eq $type } qw(invoice credit_note)) {
40 $filters{trans_number} = "invnumber";
41 $filters{numberfield} = $type eq 'credit_note' ? "cnnumber" : "invnumber";
42 $filters{table} = "ar";
44 } elsif (any { $_ eq $type } qw(customer vendor)) {
45 $filters{trans_number} = "${type}number";
46 $filters{numberfield} = "${type}number";
47 $filters{table} = $type;
49 } elsif ($type =~ /_delivery_order$/) {
50 $filters{trans_number} = "donumber";
51 $filters{numberfield} = $type eq 'sales_delivery_order' ? "sdonumber" : "pdonumber";
52 $filters{table} = "delivery_orders";
53 $filters{where} = $type =~ /^sales/ ? '(customer_id IS NOT NULL)' : '(vendor_id IS NOT NULL)';
55 } elsif ($type =~ /_order$/) {
56 $filters{trans_number} = "ordnumber";
57 $filters{numberfield} = $type eq 'sales_order' ? "sonumber" : "ponumber";
58 $filters{table} = "oe";
59 $filters{where} = 'NOT COALESCE(quotation, FALSE)';
60 $filters{where} .= $type =~ /^sales/ ? ' AND (customer_id IS NOT NULL)' : ' AND (vendor_id IS NOT NULL)';
62 } elsif ($type =~ /_quotation$/) {
63 $filters{trans_number} = "quonumber";
64 $filters{numberfield} = $type eq 'sales_quotation' ? "sqnumber" : "rfqnumber";
65 $filters{table} = "oe";
66 $filters{where} = 'COALESCE(quotation, FALSE)';
67 $filters{where} .= $type =~ /^sales/ ? ' AND (customer_id IS NOT NULL)' : ' AND (vendor_id IS NOT NULL)';
69 } elsif ($type =~ /part|service|assembly/) {
70 $filters{trans_number} = "partnumber";
71 $filters{numberfield} = $type eq 'service' ? 'servicenumber' : 'articlenumber';
72 $filters{numberfield} = $type eq 'assembly' ? 'assemblynumber' : $filters{numberfield};
73 $filters{table} = "parts";
74 } elsif ($type =~ /letter/) {
75 $filters{trans_number} = "letternumber";
76 $filters{numberfield} = "letternumber";
77 $filters{table} = "letter";
86 return undef if !$self->number;
88 my %filters = $self->_get_filters();
91 my @values = ($self->number);
93 push @where, $filters{where} if $filters{where};
96 push @where, qq|id <> ?|;
97 push @values, conv_i($self->id);
100 my $where_str = @where ? ' AND ' . join(' AND ', map { "($_)" } @where) : '';
102 SELECT $filters{trans_number}
104 WHERE ($filters{trans_number} = ?)
108 my ($existing_number) = selectfirst_array_query($main::form, $self->dbh, $query, @values);
110 return $existing_number ? 0 : 1;
116 my $form = $main::form;
117 my %filters = $self->_get_filters();
119 $self->dbh->begin_work if $self->dbh->{AutoCommit};
121 my $where = $filters{where} ? ' WHERE ' . $filters{where} : '';
123 SELECT DISTINCT $filters{trans_number}, 1 AS in_use
128 do_query($form, $self->dbh, "LOCK TABLE " . $filters{table}) || die $self->dbh->errstr;
129 my %numbers_in_use = selectall_as_map($form, $self->dbh, $query, $filters{trans_number}, 'in_use');
132 ($business_number) = selectfirst_array_query($form, $self->dbh, qq|SELECT customernumberinit FROM business WHERE id = ? FOR UPDATE|, $self->business_id) if $self->business_id;
133 my $number = $business_number;
134 ($number) = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults FOR UPDATE|) if !$number;
135 if ($filters{numberfield} eq 'assemblynumber' and length($number) < 1) {
136 $filters{numberfield} = 'articlenumber';
137 ($number) = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults FOR UPDATE|) if !$number;
140 my $sequence = SL::PrefixedNumber->new(number => $number);
143 $number = $sequence->get_next;
144 } while ($numbers_in_use{$number});
147 if ($self->business_id && $business_number) {
148 do_query($form, $self->dbh, qq|UPDATE business SET customernumberinit = ? WHERE id = ?|, $number, $self->business_id);
150 do_query($form, $self->dbh, qq|UPDATE defaults SET $filters{numberfield} = ?|, $number);
152 $self->dbh->commit if !$self->dbh_provided;