Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 66d468b0

Von Moritz Bunkus vor mehr als 8 Jahren hinzugefügt

  • ID 66d468b093e7c4510722f22b4a0c069cb95e6117
  • Vorgänger 58d09211
  • Nachfolger bbdb5edd

Bankauszug verbuchen: Warnungen/Fehler anzeigen; pro Zeile eine DB-Transaktion

Das Verbuchen von Bankauszügen wird nun in Datenbanktransaktionen
gekapselt. Damit die BenutzerIn bei einem Fehler nicht alles erneut
einstellen muss, wird eine Datenbanktransaktion pro
Banktransaktion benutzt.

Treten dabei Warnungen oder Fehler auf, so werden diese nun in einer
Tabelle übersichtlich dargestellt. Die Tabelle enthält sowohl die Daten
der Banktransaktion, bei der das Problem auftrat (entfernte
Kontonummer/BIC, entferne KontobesitzerIn, Betrag, Transaktionsdatum),
als auch Links zu den mit dieser Transaktion verknüpften
Rechnungen. Damit wird die BearbeiterIn in die Lage versetzt, die Fehler
genauer zu analysieren.

Unterschiede anzeigen:

SL/Controller/BankTransaction.pm
29 29

  
30 30
use Rose::Object::MakeMethods::Generic
31 31
(
32
 'scalar --get_set_init' => [ qw(models) ],
32
  'scalar --get_set_init' => [ qw(models problems) ],
33 33
);
34 34

  
35 35
__PACKAGE__->run_before('check_auth');
......
394 394
  #           '44' => [ '50', '51', 52' ]
395 395
  #         };
396 396

  
397
  my $skonto_hash  = delete $::form->{invoice_skontos} || {}; # array containing the payment type, could be empty
397
  $::form->{invoice_skontos} ||= {}; # hash of arrays containing the payment types, could be empty
398 398

  
399 399
  # a bank_transaction may be assigned to several invoices, i.e. a customer
400 400
  # might pay several open invoices with one transaction
401 401

  
402
  while ( my ($bt_id, $invoice_ids) = each(%$invoice_hash) ) {
403
    my $bank_transaction      = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
402
  $self->problems([]);
403

  
404
  while ( my ($bank_transaction_id, $invoice_ids) = each(%$invoice_hash) ) {
405
    push @{ $self->problems }, $self->save_single_bank_transaction(
406
      bank_transaction_id => $bank_transaction_id,
407
      invoice_ids         => $invoice_ids,
408
    );
409
  }
410

  
411
  $self->action_list();
412
}
413

  
414
sub save_single_bank_transaction {
415
  my ($self, %params) = @_;
416

  
417
  my %data = (
418
    %params,
419
    bank_transaction => SL::DB::Manager::BankTransaction->find_by(id => $params{bank_transaction_id}),
420
    invoices         => [],
421
  );
422

  
423
  if (!$data{bank_transaction}) {
424
    return {
425
      %data,
426
      result => 'error',
427
      message => $::locale->text('The ID #1 is not a valid database ID.', $data{bank_transaction_id}),
428
    };
429
  }
430

  
431
  my (@warnings);
432

  
433
  my $worker = sub {
434
    my $bt_id                 = $data{bank_transaction_id};
435
    my $bank_transaction      = $data{bank_transaction};
404 436
    my $sign                  = $bank_transaction->amount < 0 ? -1 : 1;
405 437
    my $amount_of_transaction = $sign * $bank_transaction->amount;
406 438

  
407 439
    my @invoices;
408
    foreach my $invoice_id (@{ $invoice_ids }) {
409
      push @invoices, (SL::DB::Manager::Invoice->find_by(id => $invoice_id) || SL::DB::Manager::PurchaseInvoice->find_by(id => $invoice_id));
440
    foreach my $invoice_id (@{ $params{invoice_ids} }) {
441
      my $invoice = SL::DB::Manager::Invoice->find_by(id => $invoice_id) || SL::DB::Manager::PurchaseInvoice->find_by(id => $invoice_id);
442
      if (!$invoice) {
443
        return {
444
          %data,
445
          result  => 'error',
446
          message => $::locale->text("The ID #1 is not a valid database ID.", $invoice_id),
447
        };
448
      }
449

  
450
      push @invoices, $invoice;
410 451
    }
452

  
453
    $data{invoices} = \@invoices;
454

  
411 455
    @invoices = sort { return 1 if ( $a->is_sales and $a->amount > 0);
412 456
                       return 1 if (!$a->is_sales and $a->amount < 0);
413 457
                       return -1;
......
425 469
      $n_invoices++ ;
426 470
      # Check if bank_transaction already has a link to the invoice, may only be linked once per invoice
427 471
      # This might be caused by the user reloading a page and resending the form
428
      die t8("Bank transaction with id #1 has already been linked to #2.", $bank_transaction->id, $invoice->displayable_name)
429
        if _existing_record_link($bank_transaction, $invoice);
472
      if (_existing_record_link($bank_transaction, $invoice)) {
473
        return {
474
          %data,
475
          result  => 'error',
476
          message => $::locale->text("Bank transaction with id #1 has already been linked to #2.", $bank_transaction->id, $invoice->displayable_name),
477
        };
478
      }
479

  
480
      if ($amount_of_transaction == 0) {
481
        push @warnings, {
482
          %data,
483
          result  => 'warning',
484
          message => $::locale->text('There are invoices which could not be paid by bank transaction #1 (Account number: #2, bank code: #3)!',
485
                                     $bank_transaction->purpose, $bank_transaction->remote_account_number, $bank_transaction->remote_bank_code),
486
        };
487
        last;
488
      }
430 489

  
431 490
      my $payment_type;
432
      if ( defined $skonto_hash->{"$bt_id"} ) {
433
        $payment_type = shift(@{ $skonto_hash->{"$bt_id"} });
491
      if ( defined $::form->{invoice_skontos}->{"$bt_id"} ) {
492
        $payment_type = shift(@{ $::form->{invoice_skontos}->{"$bt_id"} });
434 493
      } else {
435 494
        $payment_type = 'without_skonto';
436 495
      };
437
      if ($amount_of_transaction == 0) {
438
        flash('warning',  $::locale->text('There are invoices which could not be paid by bank transaction #1 (Account number: #2, bank code: #3)!',
439
                                          $bank_transaction->purpose,
440
                                          $bank_transaction->remote_account_number,
441
                                          $bank_transaction->remote_bank_code));
442
        last;
443
      }
496

  
444 497
      # pay invoice or go to the next bank transaction if the amount is not sufficiently high
445 498
      if ($invoice->open_amount <= $amount_of_transaction && $n_invoices < $max_invoices) {
446 499
        # first calculate new bank transaction amount ...
......
490 543

  
491 544
    }
492 545
    $bank_transaction->save;
493
  }
494 546

  
495
  $self->action_list();
547
    # 'undef' means 'no error' here.
548
    return undef;
549
  };
550

  
551
  my $error;
552
  my $rez = $data{bank_transaction}->db->with_transaction(sub {
553
    eval {
554
      $error = $worker->();
555
      1;
556

  
557
    } or do {
558
      $error = {
559
        %data,
560
        result  => 'error',
561
        message => $@,
562
      };
563
    };
564

  
565
    die if $error;
566
  });
567

  
568
  return grep { $_ } ($error, @warnings);
496 569
}
497 570

  
498 571
sub action_save_proposals {
......
651 724
  return @$linked_records ? 1 : 0;
652 725
};
653 726

  
727
sub init_problems { [] }
654 728

  
655 729
sub init_models {
656 730
  my ($self) = @_;

Auch abrufbar als: Unified diff