Revision d7369245
Von Moritz Bunkus vor fast 14 Jahren hinzugefügt
SL/TransNumber.pm | ||
---|---|---|
1 |
package SL::TransNumber; |
|
2 |
|
|
3 |
use strict; |
|
4 |
|
|
5 |
use parent qw(Rose::Object); |
|
6 |
|
|
7 |
use Carp; |
|
8 |
use List::MoreUtils qw(any none); |
|
9 |
use Readonly; |
|
10 |
use SL::DBUtils; |
|
11 |
|
|
12 |
use Rose::Object::MakeMethods::Generic |
|
13 |
( |
|
14 |
scalar => [ qw(type id number save dbh dbh_provided) ], |
|
15 |
); |
|
16 |
|
|
17 |
# has 'type' => ( is => 'rw', required => 1 ); |
|
18 |
# has 'id' => ( is => 'rw' ); |
|
19 |
# has 'number' => ( is => 'rw' ); |
|
20 |
# has 'save' => ( is => 'rw', default => 1 ); |
|
21 |
# has 'dbh' => ( is => 'rw' ); |
|
22 |
# has 'dbh_provided' => ( is => 'rw' ); |
|
23 |
|
|
24 |
Readonly my @SUPPORTED_TYPES => qw(invoice credit_note customer vendor sales_delivery_order purchase_delivery_order sales_order purchase_order sales_quotation request_quotation); |
|
25 |
|
|
26 |
sub new { |
|
27 |
my $class = shift; |
|
28 |
my $self = $class->SUPER::new(@_); |
|
29 |
|
|
30 |
croak "Invalid type " . $self->type if none { $_ eq $self->type } @SUPPORTED_TYPES; |
|
31 |
|
|
32 |
$self->dbh_provided($self->dbh); |
|
33 |
$self->dbh($::form->get_standard_dbh) if !$self->dbh; |
|
34 |
$self->save(1) unless defined $self->save; |
|
35 |
|
|
36 |
return $self; |
|
37 |
} |
|
38 |
|
|
39 |
sub _get_filters { |
|
40 |
my $self = shift; |
|
41 |
|
|
42 |
my $type = $self->type; |
|
43 |
my %filters = ( where => '' ); |
|
44 |
|
|
45 |
if (any { $_ eq $type } qw(invoice credit_note)) { |
|
46 |
$filters{trans_number} = "invnumber"; |
|
47 |
$filters{numberfield} = $type eq 'credit_note' ? "cnnumber" : "invnumber"; |
|
48 |
$filters{table} = "ar"; |
|
49 |
|
|
50 |
} elsif (any { $_ eq $type } qw(customer vendor)) { |
|
51 |
$filters{trans_number} = "${type}number"; |
|
52 |
$filters{numberfield} = "${type}number"; |
|
53 |
$filters{table} = $type; |
|
54 |
|
|
55 |
} elsif ($type =~ /_delivery_order$/) { |
|
56 |
$filters{trans_number} = "donumber"; |
|
57 |
$filters{numberfield} = $type eq 'sales_delivery_order' ? "sdonumber" : "pdonumber"; |
|
58 |
$filters{table} = "delivery_orders"; |
|
59 |
$filters{where} = $type =~ /^sales/ ? '(customer_id IS NOT NULL)' : '(vendor_id IS NOT NULL)'; |
|
60 |
|
|
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)'; |
|
67 |
|
|
68 |
} else { |
|
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)'; |
|
74 |
} |
|
75 |
|
|
76 |
return %filters; |
|
77 |
} |
|
78 |
|
|
79 |
sub is_unique { |
|
80 |
my $self = shift; |
|
81 |
|
|
82 |
return undef if !$self->number; |
|
83 |
|
|
84 |
my %filters = $self->_get_filters(); |
|
85 |
|
|
86 |
my @where; |
|
87 |
my @values = ($self->number); |
|
88 |
|
|
89 |
push @where, $filters{where} if $filters{where}; |
|
90 |
|
|
91 |
if ($self->id) { |
|
92 |
push @where, qq|id <> ?|; |
|
93 |
push @values, conv_i($self->id); |
|
94 |
} |
|
95 |
|
|
96 |
my $where_str = @where ? join(' AND ', map { "($_)" } @where) : ''; |
|
97 |
my $query = <<SQL; |
|
98 |
SELECT $filters{trans_number} |
|
99 |
FROM $filters{table} |
|
100 |
WHERE ($filters{trans_number} = ?) |
|
101 |
$where_str |
|
102 |
LIMIT 1 |
|
103 |
SQL |
|
104 |
my ($existing_number) = selectfirst_array_query($main::form, $self->dbh, $query, @values); |
|
105 |
|
|
106 |
return $existing_number ? 0 : 1; |
|
107 |
} |
|
108 |
|
|
109 |
sub create_unique { |
|
110 |
my $self = shift; |
|
111 |
|
|
112 |
my $form = $main::form; |
|
113 |
my %filters = $self->_get_filters(); |
|
114 |
|
|
115 |
do_query($form, $self->dbh, qq|LOCK TABLE defaults|); |
|
116 |
|
|
117 |
my $where = $filters{where} ? ' WHERE ' . $filters{where} : ''; |
|
118 |
my $query = <<SQL; |
|
119 |
SELECT DISTINCT $filters{trans_number}, 1 AS in_use |
|
120 |
FROM $filters{table} |
|
121 |
$where |
|
122 |
SQL |
|
123 |
|
|
124 |
my %numbers_in_use = selectall_as_map($form, $self->dbh, $query, $filters{trans_number}, 'in_use'); |
|
125 |
|
|
126 |
my ($number) = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults|); |
|
127 |
$number ||= ''; |
|
128 |
|
|
129 |
do { |
|
130 |
if ($number =~ m/\d+$/) { |
|
131 |
my $new_number = substr($number, $-[0]) * 1 + 1; |
|
132 |
my $len_diff = length($number) - $-[0] - length($new_number); |
|
133 |
$number = substr($number, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_number; |
|
134 |
|
|
135 |
} else { |
|
136 |
$number = $number . '1'; |
|
137 |
} |
|
138 |
} while ($numbers_in_use{$number}); |
|
139 |
|
|
140 |
if ($self->save) { |
|
141 |
do_query($form, $self->dbh, qq|UPDATE defaults SET $filters{numberfield} = ?|, $number); |
|
142 |
$self->dbh->commit if !$self->dbh_provided; |
|
143 |
} |
|
144 |
|
|
145 |
return $number; |
|
146 |
} |
|
147 |
|
|
148 |
1; |
Auch abrufbar als: Unified diff
Eine Klasse zum Check und zur Erzeugung von eindeutigen Belegnummern