-sub action_save_proposals {
- my ($self) = @_;
-
- foreach my $bt_id (@{ $::form->{proposal_ids} }) {
- my $bt = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
-
- my $arap = SL::DB::Manager::Invoice->find_by(id => $::form->{"proposed_invoice_$bt_id"});
- $arap = SL::DB::Manager::PurchaseInvoice->find_by(id => $::form->{"proposed_invoice_$bt_id"}) if not defined $arap;
-
- # check for existing record_link for that $bt and $arap
- # do this before any changes to $bt are made
- die t8("Bank transaction with id #1 has already been linked to #2.", $bt->id, $arap->displayable_name)
- if _existing_record_link($bt, $arap);
-
- #mark bt as booked
- $bt->invoice_amount($bt->amount);
- $bt->save;
-
- #pay invoice
- $arap->pay_invoice(chart_id => $bt->local_bank_account->chart_id,
- trans_id => $arap->id,
- amount => $arap->amount,
- transdate => $bt->transdate->to_kivitendo);
- $arap->save;
-
- #create record link
- my @props = (
- from_table => 'bank_transactions',
- from_id => $bt_id,
- to_table => $arap->is_sales ? 'ar' : 'ap',
- to_id => $arap->id,
- );
-
- SL::DB::RecordLink->new(@props)->save;
-
- # code duplicated in action_save_invoices!
- # "close" a sepa_export_item if it exists
- # currently only works, if there is only exactly one open sepa_export_item
- if ( my $seis = $arap->find_sepa_export_items({ executed => 0 }) ) {
- if ( scalar @$seis == 1 ) {
- # moved the execution and the check for sepa_export into a method,
- # this isn't part of a transaction, though
- $seis->[0]->set_executed if $arap->id == $seis->[0]->arap_id;
- }
- }
- }
-
- flash('ok', t8('#1 proposal(s) saved.', scalar @{ $::form->{proposal_ids} }));
-
- $self->action_list();
-}
-