1 package SL::TransNumber;
5 use parent qw(Rose::Object);
8 use List::MoreUtils qw(any none);
11 use Rose::Object::MakeMethods::Generic
13 scalar => [ qw(type id number save dbh dbh_provided business_id) ],
16 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);
20 my $self = $class->SUPER::new(@_);
22 croak "Invalid type " . $self->type if none { $_ eq $self->type } @SUPPORTED_TYPES;
24 $self->dbh_provided($self->dbh);
25 $self->dbh($::form->get_standard_dbh) if !$self->dbh;
26 $self->save(1) unless defined $self->save;
27 $self->business_id(undef) if $self->type ne 'customer';
35 my $type = $self->type;
36 my %filters = ( where => '' );
38 if (any { $_ eq $type } qw(invoice credit_note)) {
39 $filters{trans_number} = "invnumber";
40 $filters{numberfield} = $type eq 'credit_note' ? "cnnumber" : "invnumber";
41 $filters{table} = "ar";
43 } elsif (any { $_ eq $type } qw(customer vendor)) {
44 $filters{trans_number} = "${type}number";
45 $filters{numberfield} = "${type}number";
46 $filters{table} = $type;
48 } elsif ($type =~ /_delivery_order$/) {
49 $filters{trans_number} = "donumber";
50 $filters{numberfield} = $type eq 'sales_delivery_order' ? "sdonumber" : "pdonumber";
51 $filters{table} = "delivery_orders";
52 $filters{where} = $type =~ /^sales/ ? '(customer_id IS NOT NULL)' : '(vendor_id IS NOT NULL)';
54 } elsif ($type =~ /_order$/) {
55 $filters{trans_number} = "ordnumber";
56 $filters{numberfield} = $type eq 'sales_order' ? "sonumber" : "ponumber";
57 $filters{table} = "oe";
58 $filters{where} = 'NOT COALESCE(quotation, FALSE)';
59 $filters{where} .= $type =~ /^sales/ ? ' AND (customer_id IS NOT NULL)' : ' AND (vendor_id IS NOT NULL)';
61 } elsif ($type =~ /_quotation$/) {
62 $filters{trans_number} = "quonumber";
63 $filters{numberfield} = $type eq 'sales_quotation' ? "sqnumber" : "rfqnumber";
64 $filters{table} = "oe";
65 $filters{where} = 'COALESCE(quotation, FALSE)';
66 $filters{where} .= $type =~ /^sales/ ? ' AND (customer_id IS NOT NULL)' : ' AND (vendor_id IS NOT NULL)';
68 } elsif ($type =~ /part|service|assembly/) {
69 $filters{trans_number} = "partnumber";
70 $filters{numberfield} = $type eq 'service' ? 'servicenumber' : 'articlenumber';
71 $filters{table} = "parts";
72 $filters{where} = 'COALESCE(inventory_accno_id, 0) ' . ($type eq 'service' ? '=' : '<>') . ' 0';
81 return undef if !$self->number;
83 my %filters = $self->_get_filters();
86 my @values = ($self->number);
88 push @where, $filters{where} if $filters{where};
91 push @where, qq|id <> ?|;
92 push @values, conv_i($self->id);
95 my $where_str = @where ? ' AND ' . join(' AND ', map { "($_)" } @where) : '';
97 SELECT $filters{trans_number}
99 WHERE ($filters{trans_number} = ?)
103 my ($existing_number) = selectfirst_array_query($main::form, $self->dbh, $query, @values);
105 return $existing_number ? 0 : 1;
111 my $form = $main::form;
112 my %filters = $self->_get_filters();
114 $self->dbh->begin_work if $self->dbh->{AutoCommit};
115 do_query($form, $self->dbh, qq|LOCK TABLE defaults|);
116 do_query($form, $self->dbh, qq|LOCK TABLE business|) if $self->business_id;
118 my $where = $filters{where} ? ' WHERE ' . $filters{where} : '';
120 SELECT DISTINCT $filters{trans_number}, 1 AS in_use
125 my %numbers_in_use = selectall_as_map($form, $self->dbh, $query, $filters{trans_number}, 'in_use');
128 ($business_number) = selectfirst_array_query($form, $self->dbh, qq|SELECT customernumberinit FROM business WHERE id = ?|, $self->business_id) if $self->business_id;
129 my $number = $business_number;
130 ($number) = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults|) if !$number;
134 if ($number =~ m/\d+$/) {
135 my $new_number = substr($number, $-[0]) * 1 + 1;
136 my $len_diff = length($number) - $-[0] - length($new_number);
137 $number = substr($number, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_number;
140 $number = $number . '1';
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;