355 |
355 |
my $invoice_hash = delete $::form->{invoice_ids}; # each key (the bt line with a bt_id) contains an array of invoice_ids
|
356 |
356 |
my $skonto_hash = delete $::form->{invoice_skontos} || {}; # array containing the payment type, could be empty
|
357 |
357 |
|
|
358 |
# a bank_transaction may be assigned to several invoices, i.e. a customer
|
|
359 |
# might pay several open invoices with one transaction
|
|
360 |
|
358 |
361 |
while ( my ($bt_id, $invoice_ids) = each(%$invoice_hash) ) {
|
359 |
362 |
my $bank_transaction = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
|
360 |
363 |
my $sign = $bank_transaction->amount < 0 ? -1 : 1;
|
... | ... | |
372 |
375 |
return 1; } @invoices if $bank_transaction->amount < 0;
|
373 |
376 |
|
374 |
377 |
foreach my $invoice (@invoices) {
|
|
378 |
|
|
379 |
# Check if bank_transaction already has a link to the invoice, may only be linked once per invoice
|
|
380 |
# This might be caused by the user reloading a page and resending the form
|
|
381 |
die t8("Bank transaction with id #1 has already been linked to #2.", $bank_transaction->id, $invoice->displayable_name)
|
|
382 |
if _existing_record_link($bank_transaction, $invoice);
|
|
383 |
|
375 |
384 |
my $payment_type;
|
376 |
385 |
if ( defined $skonto_hash->{"$bt_id"} ) {
|
377 |
386 |
$payment_type = shift(@{ $skonto_hash->{"$bt_id"} });
|
... | ... | |
385 |
394 |
$bank_transaction->remote_bank_code));
|
386 |
395 |
last;
|
387 |
396 |
}
|
388 |
|
#pay invoice or go to the next bank transaction if the amount is not sufficiently high
|
|
397 |
# pay invoice or go to the next bank transaction if the amount is not sufficiently high
|
389 |
398 |
if ($invoice->amount <= $amount_of_transaction) {
|
390 |
399 |
$invoice->pay_invoice(chart_id => $bank_transaction->local_bank_account->chart_id,
|
391 |
400 |
trans_id => $invoice->id,
|
... | ... | |
410 |
419 |
$amount_of_transaction = 0;
|
411 |
420 |
}
|
412 |
421 |
|
413 |
|
#Record a link from the bank transaction to the invoice
|
|
422 |
# Record a record link from the bank transaction to the invoice
|
414 |
423 |
my @props = (
|
415 |
424 |
from_table => 'bank_transactions',
|
416 |
425 |
from_id => $bt_id,
|
... | ... | |
418 |
427 |
to_id => $invoice->id,
|
419 |
428 |
);
|
420 |
429 |
|
421 |
|
my $existing = SL::DB::Manager::RecordLink->get_all(where => \@props, limit => 1)->[0];
|
422 |
|
|
423 |
|
SL::DB::RecordLink->new(@props)->save if !$existing;
|
|
430 |
SL::DB::RecordLink->new(@props)->save;
|
424 |
431 |
}
|
425 |
432 |
$bank_transaction->save;
|
426 |
433 |
}
|
... | ... | |
432 |
439 |
my ($self) = @_;
|
433 |
440 |
|
434 |
441 |
foreach my $bt_id (@{ $::form->{proposal_ids} }) {
|
435 |
|
#mark bt as booked
|
436 |
442 |
my $bt = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
|
|
443 |
|
|
444 |
my $arap = SL::DB::Manager::Invoice->find_by(id => $::form->{"proposed_invoice_$bt_id"});
|
|
445 |
$arap = SL::DB::Manager::PurchaseInvoice->find_by(id => $::form->{"proposed_invoice_$bt_id"}) if not defined $arap;
|
|
446 |
|
|
447 |
# check for existing record_link for that $bt and $arap
|
|
448 |
# do this before any changes to $bt are made
|
|
449 |
die t8("Bank transaction with id #1 has already been linked to #2.", $bt->id, $arap->displayable_name)
|
|
450 |
if _existing_record_link($bt, $arap);
|
|
451 |
|
|
452 |
#mark bt as booked
|
437 |
453 |
$bt->invoice_amount($bt->amount);
|
438 |
454 |
$bt->save;
|
439 |
455 |
|
440 |
456 |
#pay invoice
|
441 |
|
my $arap = SL::DB::Manager::Invoice->find_by(id => $::form->{"proposed_invoice_$bt_id"});
|
442 |
|
$arap = SL::DB::Manager::PurchaseInvoice->find_by(id => $::form->{"proposed_invoice_$bt_id"}) if not defined $arap;
|
443 |
457 |
$arap->pay_invoice(chart_id => $bt->local_bank_account->chart_id,
|
444 |
458 |
trans_id => $arap->id,
|
445 |
459 |
amount => $arap->amount,
|
... | ... | |
454 |
468 |
to_id => $arap->id,
|
455 |
469 |
);
|
456 |
470 |
|
457 |
|
my $existing = SL::DB::Manager::RecordLink->get_all(where => \@props, limit => 1)->[0];
|
458 |
|
|
459 |
|
SL::DB::RecordLink->new(@props)->save if !$existing;
|
|
471 |
SL::DB::RecordLink->new(@props)->save;
|
460 |
472 |
}
|
461 |
473 |
|
462 |
474 |
flash('ok', t8('#1 proposal(s) saved.', scalar @{ $::form->{proposal_ids} }));
|
... | ... | |
554 |
566 |
);
|
555 |
567 |
}
|
556 |
568 |
|
|
569 |
sub _existing_record_link {
|
|
570 |
my ($bt, $invoice) = @_;
|
|
571 |
|
|
572 |
# check whether a record link from banktransaction $bt already exists to
|
|
573 |
# invoice $invoice, returns 1 if that is the case
|
|
574 |
|
|
575 |
die unless $bt->isa("SL::DB::BankTransaction") && ( $invoice->isa("SL::DB::Invoice") || $invoice->isa("SL::DB::PurchaseInvoice") );
|
|
576 |
|
|
577 |
my $linked_record_to_table = $invoice->is_sales ? 'Invoice' : 'PurchaseInvoice';
|
|
578 |
my $linked_records = $bt->linked_records( direction => 'to', to => $linked_record_to_table, query => [ id => $invoice->id ] );
|
|
579 |
|
|
580 |
return @$linked_records ? 1 : 0;
|
|
581 |
};
|
|
582 |
|
|
583 |
|
557 |
584 |
sub init_models {
|
558 |
585 |
my ($self) = @_;
|
559 |
586 |
|
Kontoauszug verbuchen - prüfen, ob Bankbuchung und Rechnung schon verlinkt
Eine Bankbuchung darf zwar mehrere Rechnungen begleichen, aber jede
Rechnung nur einmal. Daher wird vor dem verbuchen geprüft, ob es für die
Bankbuchung schon eine Verknüpfung zu der Rechnung gibt.