Revision 79629ddd
Von Bernd Bleßmann vor mehr als 1 Jahr hinzugefügt
SL/DB/PurchaseInvoice.pm | ||
---|---|---|
4 | 4 |
|
5 | 5 |
use Carp; |
6 | 6 |
use Data::Dumper; |
7 |
use List::Util qw(sum); |
|
7 | 8 |
|
8 | 9 |
use SL::DB::MetaSetup::PurchaseInvoice; |
9 | 10 |
use SL::DB::Manager::PurchaseInvoice; |
... | ... | |
224 | 225 |
return $acc_trans; |
225 | 226 |
} |
226 | 227 |
|
228 |
sub validate_acc_trans { |
|
229 |
my ($self, %params) = @_; |
|
230 |
# should be able to check unsaved invoice objects with several acc_trans lines |
|
231 |
|
|
232 |
die "validate_acc_trans can't check invoice object with empty transactions" unless $self->transactions; |
|
233 |
|
|
234 |
my @transactions = @{$self->transactions}; |
|
235 |
# die "invoice has no acc_transactions" unless scalar @transactions > 0; |
|
236 |
|
|
237 |
return 0 unless scalar @transactions > 0; |
|
238 |
return 0 unless $self->has_loaded_related('transactions'); |
|
239 |
|
|
240 |
$::lxdebug->message(LXDebug->DEBUG1(), sprintf("starting validatation of purchase invoice %s with trans_id %s and taxincluded %s\n", $self->invnumber // '', $self->id // '', $self->taxincluded // '')); |
|
241 |
foreach my $acc ( @transactions ) { |
|
242 |
$::lxdebug->message(LXDebug->DEBUG1(), sprintf("chart: %s amount: %s tax_id: %s link: %s\n", $acc->chart->accno, $acc->amount, $acc->tax_id, $acc->chart->link)); |
|
243 |
} |
|
244 |
|
|
245 |
my $acc_trans_sum = sum map { $_->amount } @transactions; |
|
246 |
|
|
247 |
unless ( $::form->round_amount($acc_trans_sum, 10) == 0 ) { |
|
248 |
my $string = "sum of acc_transactions isn't 0: $acc_trans_sum\n"; |
|
249 |
|
|
250 |
foreach my $trans ( @transactions ) { |
|
251 |
$string .= sprintf(" %s %s %s\n", $trans->chart->accno, $trans->taxkey, $trans->amount); |
|
252 |
} |
|
253 |
$::lxdebug->message(LXDebug->DEBUG1(), $string); |
|
254 |
return 0; |
|
255 |
} |
|
256 |
|
|
257 |
# only use the first AP entry, so it also works for paid invoices |
|
258 |
my @ap_transactions = map { $_->amount } grep { $_->chart_link eq 'AP' } @transactions; |
|
259 |
my $ap_sum = $ap_transactions[0]; |
|
260 |
# my $ap_sum = sum map { $_->amount } grep { $_->chart_link eq 'AP' } @transactions; |
|
261 |
|
|
262 |
my $sign = $self->vendor_id ? 1 : -1; |
|
263 |
|
|
264 |
unless ( $::form->round_amount($ap_sum * $sign, 2) == $::form->round_amount($self->amount, 2) ) { |
|
265 |
|
|
266 |
$::lxdebug->message(LXDebug->DEBUG1(), sprintf("debug: (ap_sum) %s = %s (amount)\n", $::form->round_amount($ap_sum * $sign, 2) , $::form->round_amount($self->amount, 2) ) ); |
|
267 |
foreach my $trans ( @transactions ) { |
|
268 |
$::lxdebug->message(LXDebug->DEBUG1(), sprintf(" %s %s %s %s\n", $trans->chart->accno, $trans->taxkey, $trans->amount, $trans->chart->link)); |
|
269 |
} |
|
270 |
|
|
271 |
die sprintf("sum of ap (%s) isn't equal to invoice amount (%s)", $::form->round_amount($ap_sum * $sign, 2), $::form->round_amount($self->amount, 2)); |
|
272 |
} |
|
273 |
|
|
274 |
return 1; |
|
275 |
} |
|
276 |
|
|
277 |
sub recalculate_amounts { |
|
278 |
my ($self, %params) = @_; |
|
279 |
# calculate and set amount and netamount from acc_trans objects |
|
280 |
|
|
281 |
croak ("Can only recalculate amounts for ap transactions") if $self->invoice; |
|
282 |
|
|
283 |
return undef unless $self->has_loaded_related('transactions'); |
|
284 |
|
|
285 |
my ($netamount, $taxamount); |
|
286 |
|
|
287 |
my @transactions = @{$self->transactions}; |
|
288 |
|
|
289 |
foreach my $acc ( @transactions ) { |
|
290 |
$netamount += $acc->amount if $acc->chart->link =~ /AP_amount/; |
|
291 |
$taxamount += $acc->amount if $acc->chart->link =~ /AP_tax/; |
|
292 |
} |
|
293 |
|
|
294 |
my $sign = $self->vendor_id ? -1 : 1; |
|
295 |
$self->amount (($netamount + $taxamount) * $sign); |
|
296 |
$self->netamount(($netamount) * $sign); |
|
297 |
} |
|
298 |
|
|
227 | 299 |
sub mark_as_paid { |
228 | 300 |
my ($self) = @_; |
229 | 301 |
|
... | ... | |
259 | 331 |
|
260 | 332 |
=over 4 |
261 | 333 |
|
334 |
=item C<create_ap_row> |
|
335 |
|
|
336 |
=item C<add_ap_amount_row> |
|
337 |
|
|
338 |
=item C<validate_acc_trans> |
|
339 |
|
|
340 |
=item C<recalculate_amounts> |
|
341 |
|
|
342 |
These functions are similar to the ones in the C<SL::DB::Invoice> module. See |
|
343 |
there for more information. |
|
344 |
|
|
262 | 345 |
=item C<mark_as_paid> |
263 | 346 |
|
264 | 347 |
Marks the invoice as paid by setting its C<paid> member to the value of C<amount>. |
Auch abrufbar als: Unified diff
S:D:PurchaseInvoice: Vorbereitung für Kreditorenbuchungsimport
validate_acc_trans - Prüfen ob alle acc_trans-Einträge aufgehen
recalculate_amount - anhand acc_trans-Zeilen amount und netamount berechnen
Kopie und Anpassung der Funktionen aus S:D:Invoice