Revision 293fb807
Von Jan Büren vor mehr als 2 Jahren hinzugefügt
SL/DB/Helper/Payment.pm | ||
---|---|---|
581 | 581 |
croak "no amount passed to skonto_charts" unless abs(_round($amount)) >= 0.01; |
582 | 582 |
croak "no banktransaction.id passed to skonto_charts" unless $params{bt_id}; |
583 | 583 |
croak "no banktransaction.transdate passed to skonto_charts" unless ref $params{transdate_obj} eq 'DateTime'; |
584 |
#$main::lxdebug->message(0, 'id der transaktion' . $params{bt_id}); |
|
585 |
#$main::lxdebug->message(0, 'wert des skontos:' . $amount); |
|
584 |
|
|
586 | 585 |
my $is_sales = $self->is_sales; |
587 | 586 |
my (@skonto_charts, $inv_calc, $total_skonto_rounded); |
587 |
|
|
588 | 588 |
$inv_calc = $self->get_tax_and_amount_by_tax_chart_id(); |
589 |
#$main::lxdebug->message(0, 'lulu' . Dumper($inv_calc)); |
|
590 |
while (my ($tax_chart_id, $entry) = each %{ $inv_calc } ) { # foreach tax key = tax.id
|
|
591 |
#$main::lxdebug->message(0, 'was hier:' . $tax_chart_id);
|
|
589 |
|
|
590 |
# foreach tax.chart_id || $entry->{ta..id}
|
|
591 |
while (my ($tax_chart_id, $entry) = each %{ $inv_calc } ) {
|
|
592 | 592 |
my $tax = SL::DB::Manager::Tax->find_by(id => $entry->{tax_id}) || die "Can't find tax with id " . $tax_chart_id; |
593 | 593 |
die t8('no skonto_chart configured for taxkey #1 : #2 : #3', $tax->taxkey, $tax->taxdescription , $tax->rate * 100) |
594 | 594 |
unless $is_sales ? ref $tax->skonto_sales_chart : ref $tax->skonto_purchase_chart; |
595 |
#$main::lxdebug->message(0, 'was dort:' . $tax->id); |
|
595 |
|
|
596 |
# percent net amount |
|
596 | 597 |
my $transaction_net_skonto_percent = abs($entry->{netamount} / $self->amount); |
597 | 598 |
my $skonto_netamount_unrounded = abs($amount * $transaction_net_skonto_percent); |
598 |
#$main::lxdebug->message(0, 'ungerundet netto:' . $skonto_netamount_unrounded); |
|
599 |
# divide for tax
|
|
599 |
|
|
600 |
# percent tax amount
|
|
600 | 601 |
my $transaction_tax_skonto_percent = abs($entry->{tax} / $self->amount); |
601 | 602 |
my $skonto_taxamount_unrounded = abs($amount * $transaction_tax_skonto_percent); |
602 |
#$main::lxdebug->message(0, 'ungerundet steuer:' . $skonto_taxamount_unrounded); |
|
603 |
|
|
603 | 604 |
my $skonto_taxamount_rounded = _round($skonto_taxamount_unrounded); |
604 | 605 |
my $skonto_netamount_rounded = _round($skonto_netamount_unrounded); |
605 | 606 |
my $chart_id = $is_sales ? $tax->skonto_sales_chart->id : $tax->skonto_purchase_chart->id; |
606 | 607 |
|
608 |
# entry net + tax for caller |
|
607 | 609 |
my $rec_net = { |
608 | 610 |
chart_id => $chart_id, |
609 | 611 |
skonto_amount => _round($skonto_netamount_unrounded + $skonto_taxamount_unrounded), |
610 |
# skonto_amount => _round($skonto_netamount_unrounded) + _round($skonto_taxamount_unrounded), |
|
611 | 612 |
}; |
612 | 613 |
push @skonto_charts, $rec_net; |
613 | 614 |
$total_skonto_rounded += $rec_net->{skonto_amount}; |
... | ... | |
645 | 646 |
tax_id => 0, |
646 | 647 |
)->post; |
647 | 648 |
|
648 |
## add a stable link from ap to gl |
|
649 |
# not needed, BankTransactionAccTrans is already stable |
|
650 |
# furthermore the origin of the booking is the bank_transaction |
|
651 |
#my $arap = $self->is_sales ? 'ar' : 'ap'; |
|
652 |
#my %props_gl = ( |
|
653 |
# $arap . _id => $self->id, |
|
654 |
# gl_id => $current_transaction->id, |
|
655 |
# datev_export => 1, |
|
656 |
#); |
|
657 |
#if ($arap eq 'ap') { |
|
658 |
# require SL::DB::ApGl; |
|
659 |
# SL::DB::ApGl->new(%props_gl)->save; |
|
660 |
#} elsif ($arap eq 'ar') { |
|
661 |
# require SL::DB::ArGl; |
|
662 |
# SL::DB::ArGl->new(%props_gl)->save; |
|
663 |
#} else { die "Invalid state"; } |
|
664 |
#push @new_acc_ids, map { $_->acc_trans_id } @{ $current_transaction->transactions }; |
|
665 |
|
|
649 |
# add a stable link acc_trans_id to bank_transactions.id |
|
666 | 650 |
foreach my $transaction (@{ $current_transaction->transactions }) { |
667 | 651 |
my %props_acc = ( |
668 | 652 |
acc_trans_id => $transaction->acc_trans_id, |
... | ... | |
672 | 656 |
SL::DB::BankTransactionAccTrans->new(%props_acc)->save; |
673 | 657 |
} |
674 | 658 |
# Record a record link from banktransactions to gl |
675 |
# caller has to assign param bt_id |
|
676 | 659 |
my %props_rl = ( |
677 | 660 |
from_table => 'bank_transactions', |
678 | 661 |
from_id => $params{bt_id}, |
... | ... | |
693 | 676 |
|
694 | 677 |
} |
695 | 678 |
# check for rounding errors, at least for the payment chart |
696 |
# we ignore tax rounding errors as long as the user or calculated
|
|
697 |
# amount of skonto is fully assigned
|
|
679 |
# we ignore tax rounding errors as long as the amount (user input or calculated)
|
|
680 |
# is fully assigned.
|
|
698 | 681 |
# we simply alter one cent for the first skonto booking entry |
699 | 682 |
# should be correct for most of the cases (no invoices with mixed taxes) |
700 | 683 |
if ($total_skonto_rounded - $amount > 0.01) { |
701 | 684 |
# add one cent |
702 |
$main::lxdebug->message(0, 'Una mas!' . $total_skonto_rounded); |
|
703 | 685 |
$skonto_charts[0]->{skonto_amount} -= 0.01; |
704 | 686 |
} elsif ($amount - $total_skonto_rounded > 0.01) { |
705 | 687 |
# subtract one cent |
706 |
$main::lxdebug->message(0, 'Una menos!' . $total_skonto_rounded); |
|
707 | 688 |
$skonto_charts[0]->{skonto_amount} += 0.01; |
708 |
} else { $main::lxdebug->message(0, 'No rounding error'); }
|
|
689 |
} |
|
709 | 690 |
|
710 | 691 |
# return same array of skonto charts as sub skonto_charts |
711 | 692 |
return @skonto_charts; |
Auch abrufbar als: Unified diff
Payment-Helper _skonto_charts... debugs und kommentare aufgeräumt