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);
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";
82 return undef if !$self->number;
84 my %filters = $self->_get_filters();
87 my @values = ($self->number);
89 push @where, $filters{where} if $filters{where};
92 push @where, qq|id <> ?|;
93 push @values, conv_i($self->id);
96 my $where_str = @where ? ' AND ' . join(' AND ', map { "($_)" } @where) : '';
98 SELECT $filters{trans_number}
100 WHERE ($filters{trans_number} = ?)
104 my ($existing_number) = selectfirst_array_query($main::form, $self->dbh, $query, @values);
106 return $existing_number ? 0 : 1;
112 my $form = $main::form;
113 my %filters = $self->_get_filters();
115 $self->dbh->begin_work if $self->dbh->{AutoCommit};
116 do_query($form, $self->dbh, qq|LOCK TABLE defaults|);
117 do_query($form, $self->dbh, qq|LOCK TABLE business|) if $self->business_id;
119 my $where = $filters{where} ? ' WHERE ' . $filters{where} : '';
121 SELECT DISTINCT $filters{trans_number}, 1 AS in_use
126 my %numbers_in_use = selectall_as_map($form, $self->dbh, $query, $filters{trans_number}, 'in_use');
129 ($business_number) = selectfirst_array_query($form, $self->dbh, qq|SELECT customernumberinit FROM business WHERE id = ?|, $self->business_id) if $self->business_id;
130 my $number = $business_number;
131 ($number) = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults|) if !$number;
132 if ($filters{numberfield} eq 'assemblynumber' and length($number) < 1) {
133 $filters{numberfield} = 'articlenumber';
134 ($number) = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults|) if !$number;
137 my $sequence = SL::PrefixedNumber->new(number => $number);
140 $number = $sequence->get_next;
141 } while ($numbers_in_use{$number});
144 if ($self->business_id && $business_number) {
145 do_query($form, $self->dbh, qq|UPDATE business SET customernumberinit = ? WHERE id = ?|, $number, $self->business_id);
147 do_query($form, $self->dbh, qq|UPDATE defaults SET $filters{numberfield} = ?|, $number);
149 $self->dbh->commit if !$self->dbh_provided;