1 package SL::TransNumber;
5 use parent qw(Rose::Object);
8 use List::MoreUtils qw(any none);
10 use SL::PrefixedNumber;
12 use SL::DB::DeliveryOrder::TypeData;
14 use Rose::Object::MakeMethods::Generic
16 scalar => [ qw(type id number save dbh dbh_provided business_id) ],
19 my @SUPPORTED_TYPES = (
20 qw(invoice invoice_for_advance_payment final_invoice credit_note customer vendor sales_delivery_order purchase_delivery_order sales_order purchase_order sales_quotation request_quotation part service assembly assortment letter),
21 @{ SL::DB::DeliveryOrder::TypeData::valid_types() },
26 my $self = $class->SUPER::new(@_);
28 croak "Invalid type " . $self->type if none { $_ eq $self->type } @SUPPORTED_TYPES;
30 $self->dbh_provided($self->dbh);
31 $self->dbh(SL::DB->client->dbh) if !$self->dbh;
32 $self->save(1) unless defined $self->save;
33 $self->business_id(undef) if $self->type ne 'customer';
41 my $type = $self->type;
42 my %filters = ( where => '' );
44 if (any { $_ eq $type } qw(invoice invoice_for_advance_payment final_invoice credit_note)) {
45 $filters{trans_number} = "invnumber";
46 $filters{numberfield} = $type eq 'credit_note' ? "cnnumber" : "invnumber";
47 $filters{table} = "ar";
49 } elsif (any { $_ eq $type } qw(customer vendor)) {
50 $filters{trans_number} = "${type}number";
51 $filters{numberfield} = "${type}number";
52 $filters{table} = $type;
54 } elsif ($type =~ /_delivery_order$/ && SL::DB::DeliveryOrder::TypeData::is_valid_type($type)) {
55 $filters{trans_number} = SL::DB::DeliveryOrder::TypeData::get3($type, 'properties', 'nr_key'),
56 $filters{numberfield} = SL::DB::DeliveryOrder::TypeData::get3($type, 'properties', 'transnumber'),
57 $filters{table} = "delivery_orders";
58 $filters{where} = "order_type = ?";
59 $filters{values} = [ $::form->{type} ];
61 } elsif ($type =~ /_order$/) {
62 $filters{trans_number} = "ordnumber";
63 $filters{numberfield} = $type eq 'sales_order' ? "sonumber" : "ponumber";
64 $filters{table} = "oe";
65 $filters{where} = 'NOT COALESCE(quotation, FALSE)';
66 $filters{where} .= $type =~ /^sales/ ? ' AND (customer_id IS NOT NULL)' : ' AND (vendor_id IS NOT NULL)';
68 } elsif ($type =~ /_quotation$/) {
69 $filters{trans_number} = "quonumber";
70 $filters{numberfield} = $type eq 'sales_quotation' ? "sqnumber" : "rfqnumber";
71 $filters{table} = "oe";
72 $filters{where} = 'COALESCE(quotation, FALSE)';
73 $filters{where} .= $type =~ /^sales/ ? ' AND (customer_id IS NOT NULL)' : ' AND (vendor_id IS NOT NULL)';
75 } elsif ($type =~ /^(part|service|assembly|assortment)$/) {
76 $filters{trans_number} = "partnumber";
77 my %numberfield_hash = ( service => 'servicenumber',
78 assembly => 'assemblynumber',
79 assortment => 'assortmentnumber',
80 part => 'articlenumber'
82 $filters{numberfield} = $numberfield_hash{$type};
83 $filters{table} = "parts";
84 } elsif ($type =~ /letter/) {
85 $filters{trans_number} = "letternumber";
86 $filters{numberfield} = "letternumber";
87 $filters{table} = "letter";
96 return undef if !$self->number;
98 my %filters = $self->_get_filters();
101 my @values = ($self->number);
103 push @where, $filters{where} if $filters{where};
104 push @values, @{ $filters{values} } if $filters{values};
107 push @where, qq|id <> ?|;
108 push @values, conv_i($self->id);
111 my $where_str = @where ? ' AND ' . join(' AND ', map { "($_)" } @where) : '';
113 SELECT $filters{trans_number}
115 WHERE ($filters{trans_number} = ?)
119 my ($existing_number) = selectfirst_array_query($main::form, $self->dbh, $query, @values);
121 return $existing_number ? 0 : 1;
127 my $form = $main::form;
128 my %filters = $self->_get_filters();
131 SL::DB->client->with_transaction(sub {
132 my $where = $filters{where} ? ' WHERE ' . $filters{where} : '';
134 SELECT DISTINCT $filters{trans_number}, 1 AS in_use
139 do_query($form, $self->dbh, "LOCK TABLE " . $filters{table}) || die $self->dbh->errstr;
140 my %numbers_in_use = selectall_as_map($form, $self->dbh, $query, $filters{trans_number}, 'in_use', @{ $filters{values} // [] });
143 ($business_number) = selectfirst_array_query($form, $self->dbh, qq|SELECT customernumberinit FROM business WHERE id = ? FOR UPDATE|, $self->business_id) if $self->business_id;
144 $number = $business_number;
145 ($number) = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults FOR UPDATE|) if !$number;
146 if ($filters{numberfield} eq 'assemblynumber' and length($number) < 1) {
147 $filters{numberfield} = 'articlenumber';
148 ($number) = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults FOR UPDATE|) if !$number;
151 my $sequence = SL::PrefixedNumber->new(number => $number);
154 $number = $sequence->get_next;
155 } while ($numbers_in_use{$number});
158 if ($self->business_id && $business_number) {
159 do_query($form, $self->dbh, qq|UPDATE business SET customernumberinit = ? WHERE id = ?|, $number, $self->business_id);
161 do_query($form, $self->dbh, qq|UPDATE defaults SET $filters{numberfield} = ?|, $number);
166 }) or do { die SL::DB->client->error };