Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 25c6dfec

Von Jan Büren vor mehr als 1 Jahr hinzugefügt

  • ID 25c6dfec1aaeec06811bcb53a2b73d70c1eaa05b
  • Vorgänger debe22e0
  • Nachfolger 6d1ff65b

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)

Unterschiede anzeigen:

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';
......
463 463
  return _round($self->amount - ( $self->amount * $percent_skonto) );
464 464

  
465 465
}
466
sub _add_bank_fx_fees {
467
  my ($self, %params)   = @_;
468
  my $amount = $params{fee};
469

  
470
  croak "no amount passed for bank fx fees"   unless abs(_round($amount)) >= 0.01;
471
  croak "no banktransaction.id passed"        unless $params{bt_id};
472
  croak "no bank chart id passed"             unless $params{bank_chart_id};
473
  croak "no banktransaction.transdate passed" unless ref $params{transdate_obj} eq 'DateTime';
474

  
475
  $params{memo}   //= '';
476
  $params{source} //= '';
477

  
478
  my ($credit, $debit);
479
  $credit = SL::DB::Chart->load_cached($params{bank_chart_id});
480
  $debit  = SL::DB::Manager::Chart->find_by(description => 'Nebenkosten des Geldverkehrs');
481
  croak("No such Chart ID")  unless ref $credit eq 'SL::DB::Chart' && ref $debit eq 'SL::DB::Chart';
482
  my $notes = SL::HTML::Util->strip($self->notes);
483

  
484
  my $current_transaction = SL::DB::GLTransaction->new(
485
         employee_id    => $self->employee_id,
486
         transdate      => $params{transdate_obj},
487
         notes          => $params{source} . ' ' . $params{memo},
488
         description    => $notes || $self->invnumber,
489
         reference      => t8('Automatic Foreign Exchange Bank Fees') . " "  . $self->invnumber,
490
         department_id  => $self->department_id ? $self->department_id : undef,
491
         imported       => 0, # not imported
492
         taxincluded    => 0,
493
    )->add_chart_booking(
494
         chart  => $debit,
495
         debit  => abs($amount),
496
         source => t8('Automatic Foreign Exchange Bank Fees') . " "  . $self->invnumber,
497
         memo   => $params{memo},
498
         tax_id => 0,
499
    )->add_chart_booking(
500
         chart  => $credit,
501
         credit => abs($amount),
502
         source => t8('Automatic Foreign Exchange Bank Fees') . " "  . $self->invnumber,
503
         memo   => $params{memo},
504
         tax_id => 0,
505
    )->post;
506

  
507
    # add a stable link acc_trans_id to bank_transactions.id
508
    foreach my $transaction (@{ $current_transaction->transactions }) {
509
      my %props_acc = (
510
           acc_trans_id        => $transaction->acc_trans_id,
511
           bank_transaction_id => $params{bt_id},
512
           gl                  => $current_transaction->id,
513
      );
514
      SL::DB::BankTransactionAccTrans->new(%props_acc)->save;
515
    }
516
    # Record a record link from banktransactions to gl
517
    my %props_rl = (
518
         from_table => 'bank_transactions',
519
         from_id    => $params{bt_id},
520
         to_table   => 'gl',
521
         to_id      => $current_transaction->id,
522
    );
523
    SL::DB::RecordLink->new(%props_rl)->save;
524
    # Record a record link from ap to gl
525
    # linked gl booking will appear in tab linked records
526
    # this is just a link for convenience
527
    %props_rl = (
528
         #from_table => $is_sales ? 'ar' : 'ap',
529
         from_table => 'ap',
530
         from_id    => $self->id,
531
         to_table   => 'gl',
532
         to_id      => $current_transaction->id,
533
    );
534
    SL::DB::RecordLink->new(%props_rl)->save;
535
}
466 536

  
467 537
sub _skonto_charts_and_tax_correction {
468 538
  my ($self, %params)   = @_;
......
646 716
  return @options;
647 717
}
648 718

  
649
sub exchangerate {
719
sub get_exchangerate {
650 720
  my ($self) = @_;
651 721

  
652 722
  return 1 if $self->currency_id == $::instance_conf->get_currency_id;
653 723

  
724
  # return record exchange rate if set
725
  return $self->exchangerate if $self->exchangerate > 0;
726

  
727
  # none defined check daily exchangerate at records transdate
654 728
  die "transdate isn't a DateTime object:" . ref($self->transdate) unless ref($self->transdate) eq 'DateTime';
729

  
655 730
  my $rate = SL::DB::Manager::Exchangerate->find_by(currency_id => $self->currency_id,
656 731
                                                    transdate   => $self->transdate,
657 732
                                                   );
......
680 755
  $self->currency_id == $::instance_conf->get_currency_id ? return 0 : return 1;
681 756
}
682 757

  
758
sub get_exchangerate_for_bank_transaction {
759
  validate_pos(
760
    @_,
761
      {  can       => [ qw(forex is_sales) ],
762
         callbacks => { 'has forex'      => sub { return $_[0]->forex } } },
763
      {  callbacks => {
764
           'is an integer'               => sub { return $_[0] =~ /^[1-9][0-9]*$/                                              },
765
           'is a valid bank transaction' => sub { ref SL::DB::BankTransaction->load_cached($_[0]) eq 'SL::DB::BankTransaction' },
766
           'has a valid valuta date'     => sub { ref SL::DB::BankTransaction->load_cached($_[0])->valutadate eq 'DateTime'    },
767
         },
768
      }
769
  );
770

  
771
  my ($self, $bt_id) = @_;
772

  
773
  my $bt   = SL::DB::BankTransaction->load_cached($bt_id);
774
  my $rate = SL::DB::Manager::Exchangerate->find_by(currency_id => $self->currency_id,
775
                                                    transdate   => $bt->valutadate,
776
                                                   );
777
  return undef unless $rate;
778

  
779
  return $self->is_sales ? $rate->buy : $rate->sell; # also undef if not defined
780
}
781

  
683 782
sub _round {
684 783
  my $value = shift;
685 784
  my $num_dec = 2;

Auch abrufbar als: Unified diff