Revision 75760bfd
Von Jan Büren vor etwa 2 Jahren hinzugefügt
SL/DB/Helper/Payment.pm | ||
---|---|---|
4 | 4 |
|
5 | 5 |
use parent qw(Exporter); |
6 | 6 |
our @EXPORT = qw(pay_invoice); |
7 |
our @EXPORT_OK = qw(skonto_date amount_less_skonto within_skonto_period percent_skonto reference_account open_amount skonto_amount check_ valid_skonto_amount validate_payment_type get_payment_select_options_for_bank_transaction forex _skonto_charts_and_tax_correction);
|
|
7 |
our @EXPORT_OK = qw(skonto_date amount_less_skonto within_skonto_period percent_skonto reference_account open_amount skonto_amount valid_skonto_amount validate_payment_type get_payment_select_options_for_bank_transaction forex _skonto_charts_and_tax_correction get_exchangerate_for_bank_transaction get_exchangerate _add_bank_fx_fees);
|
|
8 | 8 |
our %EXPORT_TAGS = ( |
9 | 9 |
"ALL" => [@EXPORT, @EXPORT_OK], |
10 | 10 |
); |
... | ... | |
29 | 29 |
|
30 | 30 |
sub pay_invoice { |
31 | 31 |
my ($self, %params) = @_; |
32 |
|
|
32 |
# todo named params |
|
33 | 33 |
require SL::DB::Tax; |
34 | 34 |
|
35 | 35 |
my $is_sales = ref($self) eq 'SL::DB::Invoice'; |
... | ... | |
458 | 458 |
return _round($self->amount - ( $self->amount * $percent_skonto) ); |
459 | 459 |
|
460 | 460 |
} |
461 |
sub _add_bank_fx_fees { |
|
462 |
my ($self, %params) = @_; |
|
463 |
my $amount = $params{fee}; |
|
464 |
|
|
465 |
croak "no amount passed for bank fx fees" unless abs(_round($amount)) >= 0.01; |
|
466 |
croak "no banktransaction.id passed" unless $params{bt_id}; |
|
467 |
croak "no bank chart id passed" unless $params{bank_chart_id}; |
|
468 |
croak "no banktransaction.transdate passed" unless ref $params{transdate_obj} eq 'DateTime'; |
|
469 |
|
|
470 |
$params{memo} //= ''; |
|
471 |
$params{source} //= ''; |
|
472 |
|
|
473 |
my ($credit, $debit); |
|
474 |
$credit = SL::DB::Chart->load_cached($params{bank_chart_id}); |
|
475 |
$debit = SL::DB::Manager::Chart->find_by(description => 'Nebenkosten des Geldverkehrs'); |
|
476 |
croak("No such Chart ID") unless ref $credit eq 'SL::DB::Chart' && ref $debit eq 'SL::DB::Chart'; |
|
477 |
my $notes = SL::HTML::Util->strip($self->notes); |
|
478 |
|
|
479 |
my $current_transaction = SL::DB::GLTransaction->new( |
|
480 |
employee_id => $self->employee_id, |
|
481 |
transdate => $params{transdate_obj}, |
|
482 |
notes => $params{source} . ' ' . $params{memo}, |
|
483 |
description => $notes || $self->invnumber, |
|
484 |
reference => t8('Automatic Foreign Exchange Bank Fees') . " " . $self->invnumber, |
|
485 |
department_id => $self->department_id ? $self->department_id : undef, |
|
486 |
imported => 0, # not imported |
|
487 |
taxincluded => 0, |
|
488 |
)->add_chart_booking( |
|
489 |
chart => $debit, |
|
490 |
debit => abs($amount), |
|
491 |
source => t8('Automatic Foreign Exchange Bank Fees') . " " . $self->invnumber, |
|
492 |
memo => $params{memo}, |
|
493 |
tax_id => 0, |
|
494 |
)->add_chart_booking( |
|
495 |
chart => $credit, |
|
496 |
credit => abs($amount), |
|
497 |
source => t8('Automatic Foreign Exchange Bank Fees') . " " . $self->invnumber, |
|
498 |
memo => $params{memo}, |
|
499 |
tax_id => 0, |
|
500 |
)->post; |
|
501 |
|
|
502 |
# add a stable link acc_trans_id to bank_transactions.id |
|
503 |
foreach my $transaction (@{ $current_transaction->transactions }) { |
|
504 |
my %props_acc = ( |
|
505 |
acc_trans_id => $transaction->acc_trans_id, |
|
506 |
bank_transaction_id => $params{bt_id}, |
|
507 |
gl => $current_transaction->id, |
|
508 |
); |
|
509 |
SL::DB::BankTransactionAccTrans->new(%props_acc)->save; |
|
510 |
} |
|
511 |
# Record a record link from banktransactions to gl |
|
512 |
my %props_rl = ( |
|
513 |
from_table => 'bank_transactions', |
|
514 |
from_id => $params{bt_id}, |
|
515 |
to_table => 'gl', |
|
516 |
to_id => $current_transaction->id, |
|
517 |
); |
|
518 |
SL::DB::RecordLink->new(%props_rl)->save; |
|
519 |
# Record a record link from ap to gl |
|
520 |
# linked gl booking will appear in tab linked records |
|
521 |
# this is just a link for convenience |
|
522 |
%props_rl = ( |
|
523 |
#from_table => $is_sales ? 'ar' : 'ap', |
|
524 |
from_table => 'ap', |
|
525 |
from_id => $self->id, |
|
526 |
to_table => 'gl', |
|
527 |
to_id => $current_transaction->id, |
|
528 |
); |
|
529 |
SL::DB::RecordLink->new(%props_rl)->save; |
|
530 |
} |
|
461 | 531 |
|
462 | 532 |
sub _skonto_charts_and_tax_correction { |
463 | 533 |
my ($self, %params) = @_; |
... | ... | |
641 | 711 |
return @options; |
642 | 712 |
} |
643 | 713 |
|
644 |
sub exchangerate { |
|
714 |
sub get_exchangerate {
|
|
645 | 715 |
my ($self) = @_; |
646 | 716 |
|
647 | 717 |
return 1 if $self->currency_id == $::instance_conf->get_currency_id; |
648 | 718 |
|
719 |
# return record exchange rate if set |
|
720 |
return $self->exchangerate if $self->exchangerate > 0; |
|
721 |
|
|
722 |
# none defined check daily exchangerate at records transdate |
|
649 | 723 |
die "transdate isn't a DateTime object:" . ref($self->transdate) unless ref($self->transdate) eq 'DateTime'; |
724 |
|
|
650 | 725 |
my $rate = SL::DB::Manager::Exchangerate->find_by(currency_id => $self->currency_id, |
651 | 726 |
transdate => $self->transdate, |
652 | 727 |
); |
... | ... | |
675 | 750 |
$self->currency_id == $::instance_conf->get_currency_id ? return 0 : return 1; |
676 | 751 |
} |
677 | 752 |
|
753 |
sub get_exchangerate_for_bank_transaction { |
|
754 |
validate_pos( |
|
755 |
@_, |
|
756 |
{ can => [ qw(forex is_sales) ], |
|
757 |
callbacks => { 'has forex' => sub { return $_[0]->forex } } }, |
|
758 |
{ callbacks => { |
|
759 |
'is an integer' => sub { return $_[0] =~ /^[1-9][0-9]*$/ }, |
|
760 |
'is a valid bank transaction' => sub { ref SL::DB::BankTransaction->load_cached($_[0]) eq 'SL::DB::BankTransaction' }, |
|
761 |
'has a valid valuta date' => sub { ref SL::DB::BankTransaction->load_cached($_[0])->valutadate eq 'DateTime' }, |
|
762 |
}, |
|
763 |
} |
|
764 |
); |
|
765 |
|
|
766 |
my ($self, $bt_id) = @_; |
|
767 |
|
|
768 |
my $bt = SL::DB::BankTransaction->load_cached($bt_id); |
|
769 |
my $rate = SL::DB::Manager::Exchangerate->find_by(currency_id => $self->currency_id, |
|
770 |
transdate => $bt->valutadate, |
|
771 |
); |
|
772 |
return undef unless $rate; |
|
773 |
|
|
774 |
return $self->is_sales ? $rate->buy : $rate->sell; # also undef if not defined |
|
775 |
} |
|
776 |
|
|
678 | 777 |
sub _round { |
679 | 778 |
my $value = shift; |
680 | 779 |
my $num_dec = 2; |
Auch abrufbar als: Unified diff
Payment-Helper: neue Methoden f. Wechselkurs, Bankgebühren:
get_exchangerate: Holt den Belegwechselkurs oder den Tageswechselkurs
get_exchangerate_for_bank_transaction: Holt den Wechselkurs zum Valutatag
einer Bankbewegung
_add_bank_fx_fees: Bucht die Nebenkosten einer Auslandsüberweisung
im kreditorischen Fall (die Restmenge nach Währungskorrektur)