- while ( my ($bt_id, $invoice_ids) = each(%$invoice_hash) ) {
- my $bank_transaction = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
- my $sign = $bank_transaction->amount < 0 ? -1 : 1;
+ # e.g. three partial payments with bt_ids 54, 55 and 56 for invoice with id 74:
+ # $invoice_hash = {
+ # '55' => [
+ # '74'
+ # ],
+ # '54' => [
+ # '74'
+ # ],
+ # '56' => [
+ # '74'
+ # ]
+ # };
+ #
+ # or if the payment with bt_id 44 is used to pay invoices with ids 50, 51 and 52
+ # $invoice_hash = {
+ # '44' => [ '50', '51', 52' ]
+ # };
+
+ $::form->{invoice_skontos} ||= {}; # hash of arrays containing the payment types, could be empty
+
+ # a bank_transaction may be assigned to several invoices, i.e. a customer
+ # might pay several open invoices with one transaction
+
+ $self->problems([]);
+
+ while ( my ($bank_transaction_id, $invoice_ids) = each(%$invoice_hash) ) {
+ push @{ $self->problems }, $self->save_single_bank_transaction(
+ bank_transaction_id => $bank_transaction_id,
+ invoice_ids => $invoice_ids,
+ );
+ }
+
+ $self->action_list();
+}
+
+sub save_single_bank_transaction {
+ my ($self, %params) = @_;
+
+ my %data = (
+ %params,
+ bank_transaction => SL::DB::Manager::BankTransaction->find_by(id => $params{bank_transaction_id}),
+ invoices => [],
+ );
+
+ if (!$data{bank_transaction}) {
+ return {
+ %data,
+ result => 'error',
+ message => $::locale->text('The ID #1 is not a valid database ID.', $data{bank_transaction_id}),
+ };
+ }
+
+ my (@warnings);
+
+ my $worker = sub {
+ my $bt_id = $data{bank_transaction_id};
+ my $bank_transaction = $data{bank_transaction};
+ my $sign = $bank_transaction->amount < 0 ? -1 : 1;