+ push @skonto_charts, $rec_net;
+ $total_skonto_rounded += $rec_net->{skonto_amount};
+
+ # add-on: correct tax with one linked gl booking
+
+ # no skonto tax correction for dual tax (reverse charge) or rate = 0
+ next if ($tax->rate == 0 || $tax->reverse_charge_chart_id);
+
+ my ($credit, $debit);
+ $credit = SL::DB::Manager::Chart->find_by(id => $chart_id);
+ $debit = SL::DB::Manager::Chart->find_by(id => $tax_chart_id);
+ croak("No such Chart ID") unless ref $credit eq 'SL::DB::Chart' && ref $debit eq 'SL::DB::Chart';
+
+ my $current_transaction = SL::DB::GLTransaction->new(
+ employee_id => $self->employee_id,
+ transdate => $params{transdate_obj},
+ notes => $params{source} . ' ' . $params{memo},
+ description => $self->notes || $self->invnumber,
+ reference => t8('Skonto Tax Correction for') . " " . $tax->rate * 100 . '% ' . $self->invnumber,
+ department_id => $self->department_id ? $self->department_id : undef,
+ imported => 0, # not imported
+ taxincluded => 0,
+ )->add_chart_booking(
+ chart => $is_sales ? $debit : $credit,
+ debit => abs($skonto_taxamount_rounded),
+ source => t8('Skonto Tax Correction for') . " " . $self->invnumber,
+ memo => $params{memo},
+ tax_id => 0,
+ )->add_chart_booking(
+ chart => $is_sales ? $credit : $debit,
+ credit => abs($skonto_taxamount_rounded),
+ source => t8('Skonto Tax Correction for') . " " . $self->invnumber,
+ memo => $params{memo},
+ tax_id => 0,
+ )->post;
+
+ ## add a stable link from ap to gl
+ # not needed, BankTransactionAccTrans is already stable
+ # furthermore the origin of the booking is the bank_transaction
+ #my $arap = $self->is_sales ? 'ar' : 'ap';
+ #my %props_gl = (
+ # $arap . _id => $self->id,
+ # gl_id => $current_transaction->id,
+ # datev_export => 1,
+ #);
+ #if ($arap eq 'ap') {
+ # require SL::DB::ApGl;
+ # SL::DB::ApGl->new(%props_gl)->save;
+ #} elsif ($arap eq 'ar') {
+ # require SL::DB::ArGl;
+ # SL::DB::ArGl->new(%props_gl)->save;
+ #} else { die "Invalid state"; }
+ #push @new_acc_ids, map { $_->acc_trans_id } @{ $current_transaction->transactions };
+
+ foreach my $transaction (@{ $current_transaction->transactions }) {
+ my %props_acc = (
+ acc_trans_id => $transaction->acc_trans_id,
+ bank_transaction_id => $params{bt_id},
+ gl => $current_transaction->id,
+ );
+ SL::DB::BankTransactionAccTrans->new(%props_acc)->save;
+ }
+ # Record a record link from banktransactions to gl
+ # caller has to assign param bt_id
+ my %props_rl = (
+ from_table => 'bank_transactions',
+ from_id => $params{bt_id},
+ to_table => 'gl',
+ to_id => $current_transaction->id,
+ );
+ SL::DB::RecordLink->new(%props_rl)->save;
+ # Record a record link from arap to gl
+ # linked gl booking will appear in tab linked records
+ # this is just a link for convenience
+ %props_rl = (
+ from_table => $is_sales ? 'ar' : 'ap',
+ from_id => $self->id,
+ to_table => 'gl',
+ to_id => $current_transaction->id,
+ );
+ SL::DB::RecordLink->new(%props_rl)->save;