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";
74 $filters{where} = 'COALESCE(inventory_accno_id, 0) ' . ($type eq 'service' ? '= 0' : '<> 0 OR assembly');
83 return undef if !$self->number;
85 my %filters = $self->_get_filters();
88 my @values = ($self->number);
90 push @where, $filters{where} if $filters{where};
93 push @where, qq|id <> ?|;
94 push @values, conv_i($self->id);
97 my $where_str = @where ? ' AND ' . join(' AND ', map { "($_)" } @where) : '';
99 SELECT $filters{trans_number}
101 WHERE ($filters{trans_number} = ?)
105 my ($existing_number) = selectfirst_array_query($main::form, $self->dbh, $query, @values);
107 return $existing_number ? 0 : 1;
113 my $form = $main::form;
114 my %filters = $self->_get_filters();
116 $self->dbh->begin_work if $self->dbh->{AutoCommit};
117 do_query($form, $self->dbh, qq|LOCK TABLE defaults|);
118 do_query($form, $self->dbh, qq|LOCK TABLE business|) if $self->business_id;
120 my $where = $filters{where} ? ' WHERE ' . $filters{where} : '';
122 SELECT DISTINCT $filters{trans_number}, 1 AS in_use
127 my %numbers_in_use = selectall_as_map($form, $self->dbh, $query, $filters{trans_number}, 'in_use');
130 ($business_number) = selectfirst_array_query($form, $self->dbh, qq|SELECT customernumberinit FROM business WHERE id = ?|, $self->business_id) if $self->business_id;
131 my $number = $business_number;
132 ($number) = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults|) if !$number;
133 if ($filters{numberfield} eq 'assemblynumber' and length($number) < 1) {
134 $filters{numberfield} = 'articlenumber';
135 ($number) = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults|) if !$number;
138 my $sequence = SL::PrefixedNumber->new(number => $number);
141 $number = $sequence->get_next;
142 } while ($numbers_in_use{$number});
145 if ($self->business_id && $business_number) {
146 do_query($form, $self->dbh, qq|UPDATE business SET customernumberinit = ? WHERE id = ?|, $number, $self->business_id);
148 do_query($form, $self->dbh, qq|UPDATE defaults SET $filters{numberfield} = ?|, $number);
150 $self->dbh->commit if !$self->dbh_provided;