Revision 99d9cae4
Von Bernd Bleßmann vor fast 4 Jahren hinzugefügt
- ID 99d9cae40679116072127a27c62db0f588deb5cb
- Vorgänger 9ec6503d
SL/Controller/CustomerVendor.pm | ||
---|---|---|
10 | 10 |
use SL::Helper::Flash; |
11 | 11 |
use SL::Locale::String; |
12 | 12 |
use SL::Util qw(trim); |
13 |
use SL::VATIDNr; |
|
13 | 14 |
use SL::Webdav; |
14 | 15 |
use SL::ZUGFeRD; |
15 | 16 |
use SL::Controller::Helper::GetModels; |
... | ... | |
132 | 133 |
} |
133 | 134 |
} |
134 | 135 |
|
136 |
sub _check_ustid_taxnumber_unique { |
|
137 |
my ($self) = @_; |
|
138 |
|
|
139 |
my %cfg; |
|
140 |
if ($self->is_vendor()) { |
|
141 |
%cfg = (should_check => $::instance_conf->get_vendor_ustid_taxnummer_unique, |
|
142 |
manager_class => 'SL::DB::Manager::Vendor', |
|
143 |
err_ustid => t8('A vendor with the same VAT ID already exists.'), |
|
144 |
err_taxnumber => t8('A vendor with the same taxnumber already exists.'), |
|
145 |
); |
|
146 |
|
|
147 |
} elsif ($self->is_customer()) { |
|
148 |
%cfg = (should_check => $::instance_conf->get_customer_ustid_taxnummer_unique, |
|
149 |
manager_class => 'SL::DB::Manager::Customer', |
|
150 |
err_ustid => t8('A customer with the same VAT ID already exists.'), |
|
151 |
err_taxnumber => t8('A customer with the same taxnumber already exists.'), |
|
152 |
); |
|
153 |
|
|
154 |
} else { |
|
155 |
return; |
|
156 |
} |
|
157 |
|
|
158 |
my @errors; |
|
159 |
|
|
160 |
if ($cfg{should_check}) { |
|
161 |
my $do_clean_taxnumber = sub { my $n = $_[0]; $n //= ''; $n =~ s{[[:space:].-]+}{}g; return $n}; |
|
162 |
|
|
163 |
my $clean_ustid = SL::VATIDNr->clean($self->{cv}->ustid); |
|
164 |
my $clean_taxnumber = $do_clean_taxnumber->($self->{cv}->taxnumber); |
|
165 |
|
|
166 |
if (!($clean_ustid || $clean_taxnumber)) { |
|
167 |
return t8('VAT ID and/or taxnumber must be given.'); |
|
168 |
|
|
169 |
} else { |
|
170 |
my $clean_number = $clean_ustid; |
|
171 |
if ($clean_number) { |
|
172 |
my $entries = $cfg{manager_class}->get_all(query => ['!id' => $self->{cv}->id, '!ustid' => undef, '!ustid' => ''], select => ['ustid'], distinct => 1); |
|
173 |
if (any { $clean_number eq SL::VATIDNr->clean($_->ustid) } @$entries) { |
|
174 |
push @errors, $cfg{err_ustid}; |
|
175 |
} |
|
176 |
} |
|
177 |
|
|
178 |
$clean_number = $clean_taxnumber; |
|
179 |
if ($clean_number) { |
|
180 |
my $entries = $cfg{manager_class}->get_all(query => ['!id' => $self->{cv}->id, '!taxnumber' => undef, '!taxnumber' => ''], select => ['taxnumber'], distinct => 1); |
|
181 |
if (any { $clean_number eq $do_clean_taxnumber->($_->taxnumber) } @$entries) { |
|
182 |
push @errors, $cfg{err_taxnumber}; |
|
183 |
} |
|
184 |
} |
|
185 |
} |
|
186 |
} |
|
187 |
|
|
188 |
return join "\n", @errors if @errors; |
|
189 |
return; |
|
190 |
} |
|
191 |
|
|
135 | 192 |
sub _save { |
136 | 193 |
my ($self) = @_; |
137 | 194 |
|
... | ... | |
185 | 242 |
} |
186 | 243 |
} |
187 | 244 |
|
188 |
$self->{cv}->ustid( trim($self->{cv}->ustid)) if $self->{cv}->ustid; |
|
189 |
$self->{cv}->taxnumber(trim($self->{cv}->taxnumber)) if $self->{cv}->taxnumber; |
|
190 |
|
|
191 |
if ( $self->is_vendor() && $::instance_conf->get_vendor_ustid_taxnummer_unique) { |
|
192 |
$::form->error(t8('VAT ID and/or taxnumber must be given.')) if !($self->{cv}->ustid || $self->{cv}->taxnumber); |
|
193 |
|
|
194 |
my $count; |
|
195 |
$count += SL::DB::Manager::Vendor->get_all_count(query => ['!id' => $self->{cv}->id, ustid => $self->{cv}->ustid]) if $self->{cv}->ustid; |
|
196 |
$count += SL::DB::Manager::Vendor->get_all_count(query => ['!id' => $self->{cv}->id, taxnumber => $self->{cv}->taxnumber]) if $self->{cv}->taxnumber; |
|
197 |
|
|
198 |
$::form->error(t8('A vendor with the same VAT ID or taxnumber already exists.')) if $count; |
|
199 |
} |
|
200 |
if ($self->is_customer() && $::instance_conf->get_customer_ustid_taxnummer_unique) { |
|
201 |
$::form->error(t8('VAT ID and/or taxnumber must be given.')) if !($self->{cv}->ustid || $self->{cv}->taxnumber); |
|
202 |
|
|
203 |
my $count; |
|
204 |
$count += SL::DB::Manager::Customer->get_all_count(query => ['!id' => $self->{cv}->id, ustid => $self->{cv}->ustid]) if $self->{cv}->ustid; |
|
205 |
$count += SL::DB::Manager::Customer->get_all_count(query => ['!id' => $self->{cv}->id, taxnumber => $self->{cv}->taxnumber]) if $self->{cv}->taxnumber; |
|
206 |
|
|
207 |
$::form->error(t8('A customer with the same VAT ID or taxnumber already exists.')) if $count; |
|
208 |
} |
|
245 |
my $ustid_taxnumber_error = $self->_check_ustid_taxnumber_unique; |
|
246 |
$::form->error($ustid_taxnumber_error) if $ustid_taxnumber_error; |
|
209 | 247 |
|
210 | 248 |
$self->{cv}->save(cascade => 1); |
211 | 249 |
|
Auch abrufbar als: Unified diff
Kunden/Lieferanten: UStId/Steuernr. eindeutig: refactored …
- Prüfung in eigene sub ausgelagert
- Code f. Kunde/Lieferant zusammengefasst
- auf "gesäuberte" Nummern prüfen
(SL::VATIDNr->clean für UStId und ähnlich f. Steuernummer)