Revision b6363c02
Von Jan Büren vor etwa 2 Jahren hinzugefügt
SL/Controller/BankTransaction.pm | ||
---|---|---|
675 | 675 |
# payment_type to the helper and get the corresponding bank_transaction values back |
676 | 676 |
# hotfix to get the signs right - compare absolute values and later set the signs |
677 | 677 |
# should be better done elsewhere - changing not_assigned_amount to abs feels seriously bogus |
678 |
|
|
678 |
# default open amount |
|
679 | 679 |
my $open_amount = $payment_type eq 'with_skonto_pt' ? $invoice->amount_less_skonto : $invoice->open_amount; |
680 |
$open_amount = abs($open_amount); |
|
681 |
$open_amount -= $free_skonto_amount if ($payment_type eq 'free_skonto'); |
|
682 |
my $not_assigned_amount = abs($bank_transaction->not_assigned_amount); |
|
683 |
my $amount_for_payment = ($open_amount < $not_assigned_amount) ? $open_amount : $not_assigned_amount; |
|
684 |
my $amount_for_booking = ($open_amount < $not_assigned_amount) ? $open_amount : $not_assigned_amount; |
|
685 |
|
|
686 |
# get the right direction for the payment bookings (all amounts < 0 are stornos, credit notes or negative ap) |
|
687 |
$amount_for_payment *= -1 if $invoice->amount < 0; |
|
688 |
$free_skonto_amount *= -1 if ($free_skonto_amount && $invoice->amount < 0); |
|
689 |
# get the right direction for the bank transaction |
|
690 |
# sign is simply the sign of amount in bank_transactions: positive for increase and negative for decrease |
|
691 |
$amount_for_booking *= $sign; |
|
692 |
|
|
693 |
# check exchangerate and if fx_loss calculate new booking_amount for this invoice |
|
694 |
if ($fx_rate > 0) { |
|
680 |
# if fx calc new open amount with skonto pt and set new exchange rate (default or for bank_transaction) |
|
681 |
if ($fx_rate > 0) { |
|
682 |
# 1. set new open amount |
|
695 | 683 |
die "Exchangerate without currency" unless $currency_id; |
696 | 684 |
die "Invoice currency differs from user input currency" unless $currency_id == $invoice->currency->id; |
697 |
|
|
698 |
# 1 set daily default or custom record exchange rate
|
|
685 |
$open_amount = $payment_type eq 'with_skonto_pt' ? $invoice->amount_less_skonto_fx($fx_rate) : $invoice->open_amount_fx($fx_rate); |
|
686 |
# 2. set daily default or custom record exchange rate
|
|
699 | 687 |
my $default_rate = $invoice->get_exchangerate_for_bank_transaction($bank_transaction->id); |
700 | 688 |
if (!$default_rate) { # set new daily default |
701 |
# helper |
|
702 | 689 |
my $buysell = $invoice->is_sales ? 'buy' : 'sell'; |
703 | 690 |
my $ex = SL::DB::Manager::Exchangerate->find_by(currency_id => $currency_id, |
704 | 691 |
transdate => $bank_transaction->valutadate) |
... | ... | |
709 | 696 |
} elsif ($default_rate != $fx_rate) { # set record (banktransaction) exchangerate |
710 | 697 |
$bank_transaction->exchangerate($fx_rate); # custom rate, will be displayed in ap, ir, is |
711 | 698 |
} elsif (abs($default_rate - $fx_rate) < 0.001) { |
712 |
# should be last valid state -> do nothing
|
|
699 |
# last valid state default rate is (nearly) the same as user input -> do nothing
|
|
713 | 700 |
} else { die "Invalid exchange rate state:" . $default_rate . " " . $fx_rate; } |
701 |
} # end fx hook |
|
702 |
|
|
703 |
# open amount is in default currency -> free_skonto is in default currency, no need to change |
|
704 |
$open_amount = abs($open_amount); |
|
705 |
$open_amount -= $free_skonto_amount if ($payment_type eq 'free_skonto'); |
|
706 |
my $not_assigned_amount = abs($bank_transaction->not_assigned_amount); |
|
707 |
my $amount_for_booking = ($open_amount < $not_assigned_amount) ? $open_amount : $not_assigned_amount; |
|
708 |
my $fx_fee_amount = $fx_book && ($open_amount < $not_assigned_amount) ? $not_assigned_amount - $open_amount : 0; |
|
709 |
my $amount_for_payment = $amount_for_booking; |
|
710 |
# add booking amount |
|
711 |
# $amount_for_booking |
|
714 | 712 |
|
713 |
# get the right direction for the payment bookings (all amounts < 0 are stornos, credit notes or negative ap) |
|
714 |
$amount_for_payment *= -1 if $invoice->amount < 0; |
|
715 |
$free_skonto_amount *= -1 if ($free_skonto_amount && $invoice->amount < 0); |
|
716 |
# get the right direction for the bank transaction |
|
717 |
# sign is simply the sign of amount in bank_transactions: positive for increase and negative for decrease |
|
718 |
$amount_for_booking *= $sign; |
|
719 |
|
|
720 |
$main::lxdebug->message(0, 'amount for payment:' . $amount_for_payment); |
|
721 |
# check exchangerate and if fx_loss calculate new booking_amount for this invoice |
|
722 |
#if ($fx_rate > 0) { |
|
723 |
# die "Exchangerate without currency" unless $currency_id; |
|
724 |
# die "Invoice currency differs from user input currency" unless $currency_id == $invoice->currency->id; |
|
725 |
|
|
726 |
|
|
727 |
# open_amount is open_amount for default currency |
|
728 |
# TODO |
|
729 |
# use helper to calc everything (even) |
|
715 | 730 |
# 2 if fx_loss, we probably need a higher amount to pay the original amount of the ap invoice |
716 |
if ($invoice->get_exchangerate < $fx_rate) { |
|
731 |
# no, just calc new open_amount and check if user wants a fee booking |
|
732 |
# my $open_amount_fx = $invoice->open_amount_fx($fx_rate); |
|
733 |
# $amount_for_payment = $fx_book ? $not_assigned_amount : $invoice->open_amount_fx($fx_rate); |
|
734 |
|
|
735 |
# die $open_amount_fx; |
|
736 |
# if ($invoice->get_exchangerate < $fx_rate) { |
|
717 | 737 |
# set whole bank_transaction amount -> pay_invoice will try to calculate losses and bank fees |
718 |
my $not_assigned_amount = abs($bank_transaction->not_assigned_amount); |
|
719 |
$amount_for_payment = $not_assigned_amount; |
|
720 |
$amount_for_payment *= -1 if $invoice->amount < 0; |
|
721 |
} elsif ($invoice->get_exchangerate >= $fx_rate) { |
|
738 |
# my $not_assigned_amount = abs($bank_transaction->not_assigned_amount);
|
|
739 |
# $amount_for_payment = $not_assigned_amount;
|
|
740 |
# $amount_for_payment *= -1 if $invoice->amount < 0;
|
|
741 |
# } elsif ($invoice->get_exchangerate >= $fx_rate) {
|
|
722 | 742 |
# if fx_gain do nothing, because gain |
723 | 743 |
# bla bla |
724 |
} else { |
|
725 |
die "Invalid exchange rate state for record:" . $invoice->get_exchangerate . " " . $fx_rate; |
|
726 |
} |
|
727 |
} |
|
744 |
# TODO copy paste |
|
745 |
# my $not_assigned_amount = abs($bank_transaction->not_assigned_amount); |
|
746 |
# $amount_for_payment = $not_assigned_amount; |
|
747 |
# $amount_for_payment *= -1 if $invoice->amount < 0; |
|
748 |
# } else { |
|
749 |
# die "Invalid exchange rate state for record:" . $invoice->get_exchangerate . " " . $fx_rate; |
|
750 |
# } |
|
751 |
#} |
|
752 |
#$main::lxdebug->message(0, 'amount for payment2:' . $amount_for_payment); |
|
728 | 753 |
# ... and then pay the invoice |
729 | 754 |
my @acc_ids = $invoice->pay_invoice(chart_id => $bank_transaction->local_bank_account->chart_id, |
730 | 755 |
trans_id => $invoice->id, |
... | ... | |
735 | 760 |
skonto_amount => $free_skonto_amount, |
736 | 761 |
exchangerate => $fx_rate, |
737 | 762 |
fx_book => $fx_book, |
763 |
fx_fee_amount => $fx_fee_amount, |
|
738 | 764 |
currency_id => $currency_id, |
739 | 765 |
bt_id => $bt_id, |
740 | 766 |
transdate => $bank_transaction->valutadate->to_kivitendo); |
741 | 767 |
# First element is the booked amount for accno bank |
742 |
my $booked_amount = shift @acc_ids; |
|
768 |
my $bank_amount = shift @acc_ids; |
|
769 |
$main::lxdebug->message(0, 'a' . $bank_amount->{return_bank_amount}); |
|
770 |
$main::lxdebug->message(0, 'b' . $amount_for_booking); |
|
743 | 771 |
if (!$invoice->forex) { |
744 |
die "Invalid state, calculated invoice_amount differs from expected invoice amount" unless abs(abs($booked_amount) - abs($amount_for_booking)) < 0.001;
|
|
772 |
die "Invalid state, calculated invoice_amount differs from expected invoice amount" unless (abs($bank_amount->{return_bank_amount}) - abs($amount_for_booking) < 0.001);
|
|
745 | 773 |
$bank_transaction->invoice_amount($bank_transaction->invoice_amount + $amount_for_booking); |
746 | 774 |
} else { |
747 |
$bank_transaction->invoice_amount($bank_transaction->invoice_amount + $booked_amount * $sign); |
|
775 |
die "Invalid state, calculated invoice_amount differs from expected invoice amount" unless $fx_book || (abs($bank_amount->{return_bank_amount}) - abs($amount_for_booking) < 0.001); |
|
776 |
$bank_transaction->invoice_amount($bank_transaction->invoice_amount + $bank_amount->{return_bank_amount}); |
|
777 |
#$bank_transaction->invoice_amount($bank_transaction->invoice_amount + $amount_for_booking); |
|
748 | 778 |
} |
749 | 779 |
# ... and record the origin via BankTransactionAccTrans |
750 | 780 |
if (scalar(@acc_ids) < 2) { |
Auch abrufbar als: Unified diff
Wechselkurs beim Bankauszug verbuchen.
> Testfälle i.O. (bank_transaction.t)
> manuelle Prüfung fast i.O. (sechs Fälle) Rundungsdifferenzen> Debugs n.i.O.
> automatisierte Testfälle n.i.O.